File: text_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 (491 lines) | stat: -rw-r--r-- 15,096 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*  camserv - An internet streaming picture application
 *
 *  Copyright (C) 1999  Jon Travis (jon@dsndata.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>

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

#include "font_6x11.h"
#include "font_8x8.h"

#define MODNAME "textfilter"

#define COLOR_TRANSPARENT  0xff000000
#define COLOR_DEFAULT_BG   0x00000000  /* Colour defaults */
#define COLOR_DEFAULT_FG   0x00ffffff  /* Colour defaults */

typedef struct text_filter_st {
  FixedFont *font;
  int x, y;  /* Coordinates to place the text */
  int fg_color, bg_color;
  char txt[ 1024 ];
  int txt_mangle;  /* true if text is to be mangled via fmt_text, else false */
  /* valid if bg_color is a real colour */
  unsigned char bg_rval, bg_gval, bg_bval, bg_bwval;  
  /* valid if fg_color is a real colour */
  unsigned char fg_rval, fg_gval, fg_bval, fg_bwval;  
} TextFilter;


/*
 * valid_rgb_color:  Determine if a given string represents a valid
 *                   colour for the text filter.
 *    
 * Arguments:        color = A string to test 
 *
 * Return values:    Returns 0 if color is not of the form #RRGGBB, else 1
 */

static
int valid_rgb_color( const char *color ){
  int i;

  if( !color || strlen( color ) != 7 || color[ 0 ] != '#' )
    return 0;

  for( i=1; i< 7; i++ ){
    if( color[ i ] >=0 || color[ i ] <= 9 ) continue;
    if( color[ i ] >= 'A' || color[ i ] <= 'F' ) continue;
    if( color[ i ] >= 'a' || color[ i ] <= 'f' ) continue;
    return 0;
  }
  return 1;
}

/*
 * process_color:  Process a color input, and return the integer value
 *                 of the colour components.
 *
 * Arguments:      color = String version of the color to process
 *                 is_bg = 1 if the color is the bg color, else 0
 *                 filt_num = Filter number of the current filter
 *                 rint = Location to store the red component of the color
 *                 gint = Location to store the green component of the color
 *                 bint = Location to store the blue component of the color
 *
 * Return values:  Always returns a valid colour value.  If the given colour
 *                 is an invalid one, the default will be used.  In the case
 *                 of transparency, the component values are not set, else
 *                 each color component value is set, and the colour is
 *                 returned.
 */

static
int process_color( const char *color, int is_bg, char *secname,
		   unsigned char *rint, unsigned char *gint, 
		   unsigned char *bint )
{
  if( !color || !strlen( color )){
    camserv_log( MODNAME, "Invalid [%s]:%s color given.  "
		 "Using default", secname, is_bg ? "bg" : "fg" );
    if( is_bg ) {
      *rint = (0x00ff0000 & COLOR_DEFAULT_BG) >> 16;
      *gint = (0x0000ff00 & COLOR_DEFAULT_BG) >> 8;
      *bint = (0x000000ff & COLOR_DEFAULT_BG) >> 0;
      return COLOR_DEFAULT_BG;
    } else {
      *rint = (0x00ff0000 & COLOR_DEFAULT_FG) >> 16;
      *gint = (0x0000ff00 & COLOR_DEFAULT_FG) >> 8;
      *bint = (0x000000ff & COLOR_DEFAULT_FG) >> 0;
      return COLOR_DEFAULT_FG;
    }
  }

  if( !strcmp( color, "transparent" ) )
    return COLOR_TRANSPARENT;
  else {
    int rf, gf, bf;

    if( !valid_rgb_color( color )){
      camserv_log( MODNAME, "Invalid [%s]:%s color given.  "
		   "Format should be: #RRGGBB, or 'transparent",
		   secname, is_bg ? "bg" : "fg" );
      if( is_bg ){
	*rint = (0x00ff0000 & COLOR_DEFAULT_BG) >> 16;
	*gint = (0x0000ff00 & COLOR_DEFAULT_BG) >> 8;
	*bint = (0x000000ff & COLOR_DEFAULT_BG) >> 0;
	return COLOR_DEFAULT_BG; 
      } else {
	*rint = (0x00ff0000 & COLOR_DEFAULT_FG) >> 16;
	*gint = (0x0000ff00 & COLOR_DEFAULT_FG) >> 8;
	*bint = (0x000000ff & COLOR_DEFAULT_FG) >> 0;
	return COLOR_DEFAULT_FG;
      }
    }
    sscanf( color, "%*c%2x%2x%2x", &rf, &gf, &bf );
    *rint = rf;
    *gint = gf;
    *bint = bf;
    return (*rint << 16) | (*gint << 8) | *bint;
  }
}

static
int valid_bw_color( const char *colour ) {
  int i;

  if( !colour || strlen( colour ) != 3 || colour[ 0 ] != '#' )
    return 0;

  for( i=1; i< 3; i++ ){
    if( colour[ i ] >=0 || colour[ i ] <= 9 ) continue;
    if( colour[ i ] >= 'A' || colour[ i ] <= 'F' ) continue;
    if( colour[ i ] >= 'a' || colour[ i ] <= 'f' ) continue;
    return 0;
  }
  return 1;
}

static
int process_bw( const char *strcolour, int is_bg, char *secname, 
		unsigned char *resval )
{
  if( !strcolour || !strlen( strcolour )){
    camserv_log( MODNAME, "Invalid [%s]:%s value given.  "
		 "Using default", secname, is_bg ? "bg" : "fg" );
    if( is_bg ) {
      *resval = (0x00ff0000 & COLOR_DEFAULT_BG) >> 16;
      return COLOR_DEFAULT_BG;
    } else {
      *resval = (0x00ff0000 & COLOR_DEFAULT_FG) >> 16;
      return COLOR_DEFAULT_FG;
    }
  }

  if( !strcmp( strcolour, "transparent" ) )
    return COLOR_TRANSPARENT;
  else {
    int bwval;
    
    if( !valid_bw_color( strcolour ) ){
      camserv_log( MODNAME, "Invalid [%s]:%s color given.  "
		   "Format should be: #CC, or 'transparent'",
		   secname, is_bg ? "bg" : "fg" );
      if( is_bg ) {
	*resval = (0x00ff0000 & COLOR_DEFAULT_BG) >> 16;
	return COLOR_DEFAULT_BG;
      } else {
	*resval = (0x00ff0000 & COLOR_DEFAULT_FG) >> 16;
	return COLOR_DEFAULT_FG;
      }
    }
    sscanf( strcolour, "%*c%2x", &bwval );
    *resval = bwval;
    return (*resval << 16);
  }
}

/*
 * filter_init:  Standard filter initialization routine.
 *
 * Filter variables:  textmod0_bg  = #RRGGBB or 'transparent' BG color of txt
 *                    textmod0_fg  = #RRGGBB or 'transparent' FG color of txt
 *                    textmod0_x   = X location of text
 *                    textmod0_y   = Y location of text
 *                   *textmod0_txt = Text to place
 *                    textmod0_mangle = 1 if txt must be mangled, else 0
 *                    textmod0_fontname = Fontname to use '6x11' or '8x8'
 *
 *                    Vars prefixed by '*' are required.  The '0' in all
 *                    of the variable names must be replaced with the
 *                    filter number currently assigned to the current text
 *                    filter.
 */                 

void *filter_init( CamConfig *ccfg, char *secname ){
  TextFilter *res;
  const char *fontname, *tmptxt;
  int err, is_bw;
  char buf[ 256 ];

  is_bw = camconfig_query_int( ccfg, SEC_VIDEO, VIDCONFIG_ISB_N_W, &err );
  if( err ) {
    camserv_log( MODNAME, "FATAL!  Configuration inconsistancy!");
    return NULL;
  }

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

  if( !is_bw ) {
    res->bg_color = process_color( camconfig_query_str( ccfg, secname, "bg" ),
				   1, secname,
				   &res->bg_rval,&res->bg_gval, &res->bg_bval);
    res->fg_color = process_color( camconfig_query_str( ccfg, secname, "fg" ),
				   1, secname,
				   &res->fg_rval,&res->fg_gval, &res->fg_bval);
  } else {
    res->bg_color = process_bw( camconfig_query_str( ccfg, secname, "bg" ), 
				0, secname,
				&res->bg_bwval );
    res->fg_color = process_bw( camconfig_query_str( ccfg, secname, "fg" ), 
				1, secname,
				&res->fg_bwval );
  }

  res->x = camconfig_query_def_int( ccfg, secname, "x", 0 );
  res->y = camconfig_query_def_int( ccfg, secname, "y", 0 );
  res->txt_mangle = camconfig_query_def_int( ccfg, secname, "mangle", 0 );
  tmptxt = camconfig_query_str( ccfg, secname, "text" );

  if( !tmptxt ) {
    camserv_log( MODNAME,
		 "FATAL!  %s configuration var invalid or unavailable", buf );
    free( res );
    return NULL;
  }
  strncpy( res->txt, tmptxt, sizeof( res->txt ));
  res->txt[ sizeof( res->txt ) - 1 ] = '\0';

  fontname = camconfig_query_str( ccfg, secname, "fontname" );
  
  if( fontname && !strcmp( fontname, "6x11" )) {
    res->font = fixed_font_create( font_6x11, 6, 11 );
  } else if( fontname && !strcmp( fontname, "8x8" )) {
    res->font = fixed_font_create( font_8x8, 8, 8 );
  } else {
    camserv_log( MODNAME, "Invalid [%s]:fontname, %s ... Using 6x11", secname,
		 fontname );
    res->font = fixed_font_create( font_6x11, 6, 11 );
  }

  if( res == NULL ){
    camserv_log( MODNAME, "FATAL!  Could not create font: %s", fontname );
    free( res );
    return NULL;
  }

  return res;
}

/*
 * filter_deinit:  Standard filter deinitialization routine
 */

void filter_deinit( void *filter_dat ){
  TextFilter *tfilt = filter_dat;

  fixed_font_destroy( tfilt->font );
  free( tfilt );
}

static
int txt_is_offscreen( const TextFilter *tfilt, const Video_Info *vinfo_in,
		      int txtlen )
{
  /* We must be able to place at least 1 char */
  if( tfilt->x >= (vinfo_in->width - tfilt->font->width)) 
    return 1; /* Too far right */
  else if( tfilt->x < 0 && 
	   (tfilt->x + tfilt->font->width * txtlen < tfilt->font->width))
    return 1; /* Too far left! */

  if( tfilt->y >= vinfo_in->height  ) 
    return 1; /* Below the picture */
  else if( tfilt->y < 0 && -tfilt->y >= tfilt->font->height ) 
    return 1; /* Too far above the picture */
  
  return 0;
}

void filter_func( char *in_data, char **out_data, void *cldat, 
		  const Video_Info *vinfo_in, Video_Info *vinfo_out )
{
  TextFilter *tfilt = cldat;
  unsigned char *outp, *packp, *init_outp;
  char *txtp, use_txt[ 1024 ];
  int i, y, last_y, start_y, first_char, last_char, use_txt_len;
  int Bpp; /* Bytes for each pixel -- 1 == B&W, 3 == RGB */

  *vinfo_out = *vinfo_in;
  *out_data = in_data;
  
  if( tfilt->txt_mangle ) 
    cam_fmt_buf( tfilt->txt, use_txt, sizeof( use_txt ) );
  else
    strncpy( use_txt, tfilt->txt, sizeof( use_txt ) );

  use_txt[ sizeof( use_txt ) - 1 ] = '\0';
  use_txt_len = strlen( use_txt );

  /* First, stupidity checking to see if we are even going to be able
     to put anything on the picture */
  if( txt_is_offscreen( tfilt, vinfo_in, use_txt_len ) )
    return;

  if( tfilt->y < 0 && -tfilt->y < tfilt->font->height ) {
    start_y = -tfilt->y;  /* Straddling the picture */
  } else
    start_y = 0;

  /* Figure out the ending, in case we might run off the bottom */
  if( tfilt->y + tfilt->font->height > vinfo_in->height ) 
    last_y = tfilt->font->height - 
      (tfilt->y + tfilt->font->height - vinfo_in->height);
  else
    last_y = tfilt->font->height;

  /* This isn't the best situation, but it is more optimal than doing
     the compare for each pixel.  On a per-char basis, figure out where
     we can place the first full char on the screen.  I.e. if char[0] is off
     to the left somewhere, we don't need to place it. */
  
  if( tfilt->x < 0 )
    first_char = (-tfilt->x / tfilt->font->width) + 1;
  else
    first_char = 0;
  
  if( tfilt->x + tfilt->font->width * use_txt_len > vinfo_in->width )
    last_char = (vinfo_in->width - tfilt->x) / tfilt->font->width - 1;
  else
    last_char = use_txt_len - 1;

  if( vinfo_in->is_black_white ) Bpp = 1;
  else Bpp = 3;

  /* outp will contain the index into the picture, for the pixel
     manglifiation */
  if( tfilt->y < 0 )
    init_outp = in_data + Bpp * (0 * vinfo_in->width + tfilt->x);
  else
    init_outp = in_data + Bpp * (tfilt->y * vinfo_in->width + tfilt->x);

  outp = init_outp;

  for( y=start_y;
       y< last_y;
       y++, outp = init_outp + (y - start_y) * Bpp * vinfo_in->width )
  {

    outp += first_char * Bpp * tfilt->font->width;

    /* For each letter, print it's rasterized line */
    for( txtp=&use_txt[0] + first_char; 
	 txtp <= &use_txt[0] + last_char; txtp++ ){
      packp = tfilt->font->chars[ (int) *txtp ].line_data[ y ];
      while( *packp != FFONT_RUN_TERM_CHAR ) {
	if( *packp < 16 ) {
	  if( tfilt->fg_color != COLOR_TRANSPARENT ) {
	    if( Bpp == 3 ) {
	      for( i=0; i <= *packp; i++ ){  /* Turn the pixel on */
		*outp++ = tfilt->fg_rval;
		*outp++ = tfilt->fg_gval;
		*outp++ = tfilt->fg_bval;
	      } 
	    } else {
	      for( i=0; i <= *packp; i++ ){
		*outp++ = tfilt->fg_bwval;
	      }
	    }
	  } else {
	    outp += Bpp * (*packp + 1);
	  }
	} else if( *packp < 32 ) {
	  if( tfilt->bg_color != COLOR_TRANSPARENT ) {
	    if( Bpp == 3 ) {
	      for( i=16; i<= *packp; i++ ){  /* Turn the pixel off */
		*outp++ = tfilt->bg_rval;
		*outp++ = tfilt->bg_gval;
		*outp++ = tfilt->bg_bval;
	      }
	    } else {
	      for( i=16; i<= *packp; i++ ){
		*outp++ = tfilt->bg_bwval;
	      }
	    }
	  } else {
	    outp += Bpp * (*packp - 15);
	  }
	} else {
	  camserv_log( MODNAME, "BOGUS PACKED FONT!");
	}
	packp++;
      }
    }
  }
}

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( 7 )) == NULL )
    return NULL;

  modinfo_varname_set( res, 0, "bg" );
  modinfo_desc_set( res, 0, "Text Background Color (#CC if B&W, #RRGGBB, or "
		    "'transparent')");
  res->vars[ 0 ].type = MODINFO_TYPE_STR;
  
  modinfo_varname_set( res, 1, "fg" );
  modinfo_desc_set( res, 1, "Text Foreground Color (#CC if B&W, #RRGGBB, or "
		    "'transparent')");
  res->vars[ 1 ].type = MODINFO_TYPE_STR;

  modinfo_varname_set( res, 2, "x" );
  modinfo_desc_set( res, 2, "X pixel location of the text (from the left)" );
  res->vars[ 2 ].type = MODINFO_TYPE_INT;

  modinfo_varname_set( res, 3, "y" );
  modinfo_desc_set( res, 3, "Y pixel location of the text (from the top)" );
  res->vars[ 3 ].type = MODINFO_TYPE_INT;

  modinfo_varname_set( res, 4, "mangle" );
  modinfo_desc_set( res, 4, "Enable text mangling (1==on, 0==off)" );
  res->vars[ 4 ].type = MODINFO_TYPE_INT;

  modinfo_varname_set( res, 5, "text" );
  modinfo_desc_set( res, 5, "Text to display" );
  res->vars[ 5 ].type = MODINFO_TYPE_STR;

  modinfo_varname_set( res, 6, "fontname" );
  modinfo_desc_set( res, 6, "Font to display text in ('6x11' or '8x8')" );
  res->vars[ 6 ].type = MODINFO_TYPE_STR;
  

  return res;
}