File: rand_filter.c

package info (click to toggle)
camserv 0.42-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,884 kB
  • ctags: 1,123
  • sloc: ansic: 19,694; sh: 7,454; makefile: 206
file content (130 lines) | stat: -rw-r--r-- 3,216 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/time.h>

#include "camconfig.h"
#include "video.h"
#include "filter.h"
#include "log.h"

#define MODNAME "randfilter"

typedef struct rand_filter_st {
  int colored_pixels;
  int num_perline;
} RandFilter;


/*
 * filter_init:  Standard filter initialization routine.
 *
 * Filter variables:  randmod_perline = max # of random pixels per line
 *                    randmod_colordpix = Use colored pixels
 */

void *filter_init( CamConfig *ccfg, char *section_name ){
  RandFilter *res;

  if( (res = malloc( sizeof( *res ))) == NULL ){
    camserv_log( MODNAME, 
		 "FATAL!  Could not allocate space for random filter!" );
    return NULL;
  }

  res->num_perline = camconfig_query_def_int( ccfg, section_name,
					      "num_perline", 20 );
  res->colored_pixels = camconfig_query_def_int( ccfg, section_name,
						 "coloredpix",1);
  return res;
}

/*
 * filter_deinit:  Standard filter deinit routine 
 */

void filter_deinit( void *filter_dat ){
  RandFilter *rfilt = filter_dat;

  free( rfilt );
}

void filter_func( char *in_data, char **out_data, void *cldat, 
		  const Video_Info *vinfo_in, Video_Info *vinfo_out )
{
  RandFilter *rfilt = cldat;
  int rowspan, i, j, randval;
  unsigned char *cp, *outp;

  *vinfo_out = *vinfo_in;
  *out_data = in_data;

  if( vinfo_in->is_black_white ) { /* UNTESTED */
    rowspan = vinfo_in->width;
    
    for( i=0, cp = in_data; i< vinfo_in->height; i++, cp += rowspan ) {
      randval = random() % 100;
      for( j=0; j< randval; j++ ){
	outp = cp + (random() % vinfo_in->width );
	*outp = random() % 256;
      }
    }
  } else {
    /* Pick random spots per line */
    rowspan = vinfo_in->width * 3;

    for( i=0, cp = in_data; i< vinfo_in->height; i++, cp += rowspan ){
      randval = random() % rfilt->num_perline;
      if( rfilt->colored_pixels ) {
	for( j=0; j< randval; j++ ){
	  outp = cp + 3*(random() % vinfo_in->width );
	  *(outp + 0) = random() % 255;
	  *(outp + 1) = random() % 255;
	  *(outp + 2) = random() % 255;
	}
      } else {
	for( j=0; j< randval; j++ ){
	  outp = cp + 3*(random() % vinfo_in->width );
	  *(outp + 0) = 255 - *(outp + 0 );
	  *(outp + 1) = 255 - *(outp + 1 );
	  *(outp + 2) = 255 - *(outp + 2 );
	}
      }
    }
  }
}

void filter_validation(){
  Filter_Init_Func init = filter_init;
  Filter_Deinit_Func deinit = filter_deinit;
  Filter_Func_Func func = filter_func;

  if( init != NULL && deinit != NULL && func != NULL ) return;
}



/*
 * modinfo_query:  Routine to return information about the variables
 *                 accessed by this particular module.
 *
 * Return values:  Returns a malloced ModInfo structure, for which
 *                 the caller must free, or NULL on failure.
 */

ModInfo *modinfo_query(){
  ModInfo *res;

  if( (res = modinfo_create( 2 )) == NULL )
    return NULL;

  modinfo_varname_set( res, 0, "num_perline" );
  modinfo_desc_set( res, 0, "Maximum number of speckles per line" );
  res->vars[ 0 ].type = MODINFO_TYPE_INT;

  modinfo_varname_set( res, 1, "coloredpix" );
  modinfo_desc_set( res, 1, "Enable colored pixels (1==on, 0==off)");
  res->vars[ 1 ].type = MODINFO_TYPE_INT;

  return res;
}