Visualization Library 2.0.0-b5

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ftbitmap.c
Go to the documentation of this file.
1 /***************************************************************************/
2 /* */
3 /* ftbitmap.c */
4 /* */
5 /* FreeType utility functions for bitmaps (body). */
6 /* */
7 /* Copyright 2004-2009, 2011, 2013 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
15 /* */
16 /***************************************************************************/
17 
18 
19 #include <ft2build.h>
20 #include FT_INTERNAL_DEBUG_H
21 
22 #include FT_BITMAP_H
23 #include FT_IMAGE_H
24 #include FT_INTERNAL_OBJECTS_H
25 
26 
27  static
28  const FT_Bitmap null_bitmap = { 0, 0, 0, 0, 0, 0, 0, 0 };
29 
30 
31  /* documentation is in ftbitmap.h */
32 
33  FT_EXPORT_DEF( void )
35  {
36  *abitmap = null_bitmap;
37  }
38 
39 
40  /* documentation is in ftbitmap.h */
41 
46  {
47  FT_Memory memory = library->memory;
49  FT_Int pitch = source->pitch;
50  FT_ULong size;
51 
52 
53  if ( source == target )
54  return FT_Err_Ok;
55 
56  if ( source->buffer == NULL )
57  {
58  *target = *source;
59 
60  return FT_Err_Ok;
61  }
62 
63  if ( pitch < 0 )
64  pitch = -pitch;
65  size = (FT_ULong)( pitch * source->rows );
66 
67  if ( target->buffer )
68  {
69  FT_Int target_pitch = target->pitch;
70  FT_ULong target_size;
71 
72 
73  if ( target_pitch < 0 )
74  target_pitch = -target_pitch;
75  target_size = (FT_ULong)( target_pitch * target->rows );
76 
77  if ( target_size != size )
78  (void)FT_QREALLOC( target->buffer, target_size, size );
79  }
80  else
81  (void)FT_QALLOC( target->buffer, size );
82 
83  if ( !error )
84  {
85  unsigned char *p;
86 
87 
88  p = target->buffer;
89  *target = *source;
90  target->buffer = p;
91 
92  FT_MEM_COPY( target->buffer, source->buffer, size );
93  }
94 
95  return error;
96  }
97 
98 
99  static FT_Error
100  ft_bitmap_assure_buffer( FT_Memory memory,
101  FT_Bitmap* bitmap,
102  FT_UInt xpixels,
103  FT_UInt ypixels )
104  {
105  FT_Error error;
106  int pitch;
107  int new_pitch;
108  FT_UInt bpp;
109  FT_Int i, width, height;
110  unsigned char* buffer = NULL;
111 
112 
113  width = bitmap->width;
114  height = bitmap->rows;
115  pitch = bitmap->pitch;
116  if ( pitch < 0 )
117  pitch = -pitch;
118 
119  switch ( bitmap->pixel_mode )
120  {
121  case FT_PIXEL_MODE_MONO:
122  bpp = 1;
123  new_pitch = ( width + xpixels + 7 ) >> 3;
124  break;
125  case FT_PIXEL_MODE_GRAY2:
126  bpp = 2;
127  new_pitch = ( width + xpixels + 3 ) >> 2;
128  break;
129  case FT_PIXEL_MODE_GRAY4:
130  bpp = 4;
131  new_pitch = ( width + xpixels + 1 ) >> 1;
132  break;
133  case FT_PIXEL_MODE_GRAY:
134  case FT_PIXEL_MODE_LCD:
135  case FT_PIXEL_MODE_LCD_V:
136  bpp = 8;
137  new_pitch = ( width + xpixels );
138  break;
139  default:
140  return FT_THROW( Invalid_Glyph_Format );
141  }
142 
143  /* if no need to allocate memory */
144  if ( ypixels == 0 && new_pitch <= pitch )
145  {
146  /* zero the padding */
147  FT_Int bit_width = pitch * 8;
148  FT_Int bit_last = ( width + xpixels ) * bpp;
149 
150 
151  if ( bit_last < bit_width )
152  {
153  FT_Byte* line = bitmap->buffer + ( bit_last >> 3 );
154  FT_Byte* end = bitmap->buffer + pitch;
155  FT_Int shift = bit_last & 7;
156  FT_UInt mask = 0xFF00U >> shift;
157  FT_Int count = height;
158 
159 
160  for ( ; count > 0; count--, line += pitch, end += pitch )
161  {
162  FT_Byte* write = line;
163 
164 
165  if ( shift > 0 )
166  {
167  write[0] = (FT_Byte)( write[0] & mask );
168  write++;
169  }
170  if ( write < end )
171  FT_MEM_ZERO( write, end-write );
172  }
173  }
174 
175  return FT_Err_Ok;
176  }
177 
178  if ( FT_QALLOC_MULT( buffer, new_pitch, bitmap->rows + ypixels ) )
179  return error;
180 
181  if ( bitmap->pitch > 0 )
182  {
183  FT_Int len = ( width * bpp + 7 ) >> 3;
184 
185 
186  for ( i = 0; i < bitmap->rows; i++ )
187  FT_MEM_COPY( buffer + new_pitch * ( ypixels + i ),
188  bitmap->buffer + pitch * i, len );
189  }
190  else
191  {
192  FT_Int len = ( width * bpp + 7 ) >> 3;
193 
194 
195  for ( i = 0; i < bitmap->rows; i++ )
196  FT_MEM_COPY( buffer + new_pitch * i,
197  bitmap->buffer + pitch * i, len );
198  }
199 
200  FT_FREE( bitmap->buffer );
201  bitmap->buffer = buffer;
202 
203  if ( bitmap->pitch < 0 )
204  new_pitch = -new_pitch;
205 
206  /* set pitch only, width and height are left untouched */
207  bitmap->pitch = new_pitch;
208 
209  return FT_Err_Ok;
210  }
211 
212 
213  /* documentation is in ftbitmap.h */
214 
217  FT_Bitmap* bitmap,
218  FT_Pos xStrength,
219  FT_Pos yStrength )
220  {
221  FT_Error error;
222  unsigned char* p;
223  FT_Int i, x, y, pitch;
224  FT_Int xstr, ystr;
225 
226 
227  if ( !library )
228  return FT_THROW( Invalid_Library_Handle );
229 
230  if ( !bitmap || !bitmap->buffer )
231  return FT_THROW( Invalid_Argument );
232 
233  if ( ( ( FT_PIX_ROUND( xStrength ) >> 6 ) > FT_INT_MAX ) ||
234  ( ( FT_PIX_ROUND( yStrength ) >> 6 ) > FT_INT_MAX ) )
235  return FT_THROW( Invalid_Argument );
236 
237  xstr = (FT_Int)FT_PIX_ROUND( xStrength ) >> 6;
238  ystr = (FT_Int)FT_PIX_ROUND( yStrength ) >> 6;
239 
240  if ( xstr == 0 && ystr == 0 )
241  return FT_Err_Ok;
242  else if ( xstr < 0 || ystr < 0 )
243  return FT_THROW( Invalid_Argument );
244 
245  switch ( bitmap->pixel_mode )
246  {
247  case FT_PIXEL_MODE_GRAY2:
248  case FT_PIXEL_MODE_GRAY4:
249  {
250  FT_Bitmap tmp;
251  FT_Int align;
252 
253 
254  if ( bitmap->pixel_mode == FT_PIXEL_MODE_GRAY2 )
255  align = ( bitmap->width + xstr + 3 ) / 4;
256  else
257  align = ( bitmap->width + xstr + 1 ) / 2;
258 
259  FT_Bitmap_New( &tmp );
260 
261  error = FT_Bitmap_Convert( library, bitmap, &tmp, align );
262  if ( error )
263  return error;
264 
265  FT_Bitmap_Done( library, bitmap );
266  *bitmap = tmp;
267  }
268  break;
269 
270  case FT_PIXEL_MODE_MONO:
271  if ( xstr > 8 )
272  xstr = 8;
273  break;
274 
275  case FT_PIXEL_MODE_LCD:
276  xstr *= 3;
277  break;
278 
279  case FT_PIXEL_MODE_LCD_V:
280  ystr *= 3;
281  break;
282  }
283 
284  error = ft_bitmap_assure_buffer( library->memory, bitmap, xstr, ystr );
285  if ( error )
286  return error;
287 
288  pitch = bitmap->pitch;
289  if ( pitch > 0 )
290  p = bitmap->buffer + pitch * ystr;
291  else
292  {
293  pitch = -pitch;
294  p = bitmap->buffer + pitch * ( bitmap->rows - 1 );
295  }
296 
297  /* for each row */
298  for ( y = 0; y < bitmap->rows ; y++ )
299  {
300  /*
301  * Horizontally:
302  *
303  * From the last pixel on, make each pixel or'ed with the
304  * `xstr' pixels before it.
305  */
306  for ( x = pitch - 1; x >= 0; x-- )
307  {
308  unsigned char tmp;
309 
310 
311  tmp = p[x];
312  for ( i = 1; i <= xstr; i++ )
313  {
314  if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO )
315  {
316  p[x] |= tmp >> i;
317 
318  /* the maximum value of 8 for `xstr' comes from here */
319  if ( x > 0 )
320  p[x] |= p[x - 1] << ( 8 - i );
321 
322 #if 0
323  if ( p[x] == 0xff )
324  break;
325 #endif
326  }
327  else
328  {
329  if ( x - i >= 0 )
330  {
331  if ( p[x] + p[x - i] > bitmap->num_grays - 1 )
332  {
333  p[x] = (unsigned char)(bitmap->num_grays - 1);
334  break;
335  }
336  else
337  {
338  p[x] = (unsigned char)(p[x] + p[x-i]);
339  if ( p[x] == bitmap->num_grays - 1 )
340  break;
341  }
342  }
343  else
344  break;
345  }
346  }
347  }
348 
349  /*
350  * Vertically:
351  *
352  * Make the above `ystr' rows or'ed with it.
353  */
354  for ( x = 1; x <= ystr; x++ )
355  {
356  unsigned char* q;
357 
358 
359  q = p - bitmap->pitch * x;
360  for ( i = 0; i < pitch; i++ )
361  q[i] |= p[i];
362  }
363 
364  p += bitmap->pitch;
365  }
366 
367  bitmap->width += xstr;
368  bitmap->rows += ystr;
369 
370  return FT_Err_Ok;
371  }
372 
373 
374  /* documentation is in ftbitmap.h */
375 
379  FT_Bitmap *target,
380  FT_Int alignment )
381  {
383  FT_Memory memory;
384 
385 
386  if ( !library )
387  return FT_THROW( Invalid_Library_Handle );
388 
389  memory = library->memory;
390 
391  switch ( source->pixel_mode )
392  {
393  case FT_PIXEL_MODE_MONO:
394  case FT_PIXEL_MODE_GRAY:
395  case FT_PIXEL_MODE_GRAY2:
396  case FT_PIXEL_MODE_GRAY4:
397  case FT_PIXEL_MODE_LCD:
398  case FT_PIXEL_MODE_LCD_V:
399  {
400  FT_Int pad;
401  FT_Long old_size;
402 
403 
404  old_size = target->rows * target->pitch;
405  if ( old_size < 0 )
406  old_size = -old_size;
407 
408  target->pixel_mode = FT_PIXEL_MODE_GRAY;
409  target->rows = source->rows;
410  target->width = source->width;
411 
412  pad = 0;
413  if ( alignment > 0 )
414  {
415  pad = source->width % alignment;
416  if ( pad != 0 )
417  pad = alignment - pad;
418  }
419 
420  target->pitch = source->width + pad;
421 
422  if ( target->pitch > 0 &&
423  (FT_ULong)target->rows > FT_ULONG_MAX / target->pitch )
424  return FT_THROW( Invalid_Argument );
425 
426  if ( target->rows * target->pitch > old_size &&
427  FT_QREALLOC( target->buffer,
428  old_size, target->rows * target->pitch ) )
429  return error;
430  }
431  break;
432 
433  default:
434  error = FT_THROW( Invalid_Argument );
435  }
436 
437  switch ( source->pixel_mode )
438  {
439  case FT_PIXEL_MODE_MONO:
440  {
441  FT_Byte* s = source->buffer;
442  FT_Byte* t = target->buffer;
443  FT_Int i;
444 
445 
446  target->num_grays = 2;
447 
448  for ( i = source->rows; i > 0; i-- )
449  {
450  FT_Byte* ss = s;
451  FT_Byte* tt = t;
452  FT_Int j;
453 
454 
455  /* get the full bytes */
456  for ( j = source->width >> 3; j > 0; j-- )
457  {
458  FT_Int val = ss[0]; /* avoid a byte->int cast on each line */
459 
460 
461  tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7 );
462  tt[1] = (FT_Byte)( ( val & 0x40 ) >> 6 );
463  tt[2] = (FT_Byte)( ( val & 0x20 ) >> 5 );
464  tt[3] = (FT_Byte)( ( val & 0x10 ) >> 4 );
465  tt[4] = (FT_Byte)( ( val & 0x08 ) >> 3 );
466  tt[5] = (FT_Byte)( ( val & 0x04 ) >> 2 );
467  tt[6] = (FT_Byte)( ( val & 0x02 ) >> 1 );
468  tt[7] = (FT_Byte)( val & 0x01 );
469 
470  tt += 8;
471  ss += 1;
472  }
473 
474  /* get remaining pixels (if any) */
475  j = source->width & 7;
476  if ( j > 0 )
477  {
478  FT_Int val = *ss;
479 
480 
481  for ( ; j > 0; j-- )
482  {
483  tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7);
484  val <<= 1;
485  tt += 1;
486  }
487  }
488 
489  s += source->pitch;
490  t += target->pitch;
491  }
492  }
493  break;
494 
495 
496  case FT_PIXEL_MODE_GRAY:
497  case FT_PIXEL_MODE_LCD:
498  case FT_PIXEL_MODE_LCD_V:
499  {
500  FT_Int width = source->width;
501  FT_Byte* s = source->buffer;
502  FT_Byte* t = target->buffer;
503  FT_Int s_pitch = source->pitch;
504  FT_Int t_pitch = target->pitch;
505  FT_Int i;
506 
507 
508  target->num_grays = 256;
509 
510  for ( i = source->rows; i > 0; i-- )
511  {
512  FT_ARRAY_COPY( t, s, width );
513 
514  s += s_pitch;
515  t += t_pitch;
516  }
517  }
518  break;
519 
520 
521  case FT_PIXEL_MODE_GRAY2:
522  {
523  FT_Byte* s = source->buffer;
524  FT_Byte* t = target->buffer;
525  FT_Int i;
526 
527 
528  target->num_grays = 4;
529 
530  for ( i = source->rows; i > 0; i-- )
531  {
532  FT_Byte* ss = s;
533  FT_Byte* tt = t;
534  FT_Int j;
535 
536 
537  /* get the full bytes */
538  for ( j = source->width >> 2; j > 0; j-- )
539  {
540  FT_Int val = ss[0];
541 
542 
543  tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
544  tt[1] = (FT_Byte)( ( val & 0x30 ) >> 4 );
545  tt[2] = (FT_Byte)( ( val & 0x0C ) >> 2 );
546  tt[3] = (FT_Byte)( ( val & 0x03 ) );
547 
548  ss += 1;
549  tt += 4;
550  }
551 
552  j = source->width & 3;
553  if ( j > 0 )
554  {
555  FT_Int val = ss[0];
556 
557 
558  for ( ; j > 0; j-- )
559  {
560  tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
561  val <<= 2;
562  tt += 1;
563  }
564  }
565 
566  s += source->pitch;
567  t += target->pitch;
568  }
569  }
570  break;
571 
572 
573  case FT_PIXEL_MODE_GRAY4:
574  {
575  FT_Byte* s = source->buffer;
576  FT_Byte* t = target->buffer;
577  FT_Int i;
578 
579 
580  target->num_grays = 16;
581 
582  for ( i = source->rows; i > 0; i-- )
583  {
584  FT_Byte* ss = s;
585  FT_Byte* tt = t;
586  FT_Int j;
587 
588 
589  /* get the full bytes */
590  for ( j = source->width >> 1; j > 0; j-- )
591  {
592  FT_Int val = ss[0];
593 
594 
595  tt[0] = (FT_Byte)( ( val & 0xF0 ) >> 4 );
596  tt[1] = (FT_Byte)( ( val & 0x0F ) );
597 
598  ss += 1;
599  tt += 2;
600  }
601 
602  if ( source->width & 1 )
603  tt[0] = (FT_Byte)( ( ss[0] & 0xF0 ) >> 4 );
604 
605  s += source->pitch;
606  t += target->pitch;
607  }
608  }
609  break;
610 
611 
612  default:
613  ;
614  }
615 
616  return error;
617  }
618 
619 
620  /* documentation is in ftbitmap.h */
621 
624  {
625  if ( slot && slot->format == FT_GLYPH_FORMAT_BITMAP &&
626  !( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
627  {
628  FT_Bitmap bitmap;
629  FT_Error error;
630 
631 
632  FT_Bitmap_New( &bitmap );
633  error = FT_Bitmap_Copy( slot->library, &slot->bitmap, &bitmap );
634  if ( error )
635  return error;
636 
637  slot->bitmap = bitmap;
638  slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
639  }
640 
641  return FT_Err_Ok;
642  }
643 
644 
645  /* documentation is in ftbitmap.h */
646 
649  FT_Bitmap *bitmap )
650  {
651  FT_Memory memory;
652 
653 
654  if ( !library )
655  return FT_THROW( Invalid_Library_Handle );
656 
657  if ( !bitmap )
658  return FT_THROW( Invalid_Argument );
659 
660  memory = library->memory;
661 
662  FT_FREE( bitmap->buffer );
663  *bitmap = null_bitmap;
664 
665  return FT_Err_Ok;
666  }
667 
668 
669 /* END */
GLint GLint GLsizei GLsizei height
int FT_Error
Definition: fttypes.h:296
FT_GlyphSlot_Own_Bitmap(FT_GlyphSlot slot)
Definition: ftbitmap.c:623
FT_Bitmap_New(FT_Bitmap *abitmap)
Definition: ftbitmap.c:34
signed long FT_Long
Definition: fttypes.h:238
unsigned long FT_ULong
Definition: fttypes.h:249
FT_BEGIN_HEADER typedef signed long FT_Pos
Definition: ftimage.h:59
GLfloat GLfloat p
int write(int fd, const char *buf, int nbytes)
Definition: tif_acorn.c:325
#define FT_MEM_ZERO(dest, count)
Definition: ftmemory.h:208
#define NULL
Definition: ftobjs.h:61
GLint GLint GLint GLint GLint GLint y
signed int FT_Int
Definition: fttypes.h:216
#define FT_ARRAY_COPY(dest, source, count)
Definition: ftmemory.h:216
GLdouble GLdouble GLdouble GLdouble q
int rows
Definition: ftimage.h:312
#define FT_ULONG_MAX
Definition: ftstdlib.h:67
return FT_THROW(Missing_Property)
unsigned char * buffer
Definition: ftimage.h:315
GLsizei GLsizei GLchar * source
GLint GLint GLsizei width
#define FT_QALLOC(ptr, size)
Definition: ftmemory.h:273
GLint GLint GLint GLint GLint x
FT_Library library
Definition: cffdrivr.c:414
int pitch
Definition: ftimage.h:314
return FT_Err_Ok
Definition: ftbbox.c:645
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
#define FT_GLYPH_OWN_BITMAP
Definition: ftobjs.h:409
png_uint_32 i
Definition: png.h:2640
FT_Bitmap_Embolden(FT_Library library, FT_Bitmap *bitmap, FT_Pos xStrength, FT_Pos yStrength)
Definition: ftbitmap.c:216
#define const
Definition: zconf.h:91
#define FT_QREALLOC(ptr, cursz, newsz)
Definition: ftmemory.h:276
unsigned char FT_Byte
Definition: fttypes.h:150
FT_Memory memory
Definition: ftobjs.h:860
#define FT_FREE(ptr)
Definition: ftmemory.h:286
GLenum GLint GLuint mask
GLenum GLsizei len
#define FT_EXPORT_DEF(x)
Definition: ftconfig.h:32
#define FT_QALLOC_MULT(ptr, count, item_size)
Definition: ftmemory.h:279
FT_Error error
Definition: cffdrivr.c:411
GLuint GLfloat * val
short num_grays
Definition: ftimage.h:316
GLuint buffer
FT_Bitmap_Copy(FT_Library library, const FT_Bitmap *source, FT_Bitmap *target)
Definition: ftbitmap.c:43
FT_Bitmap_Done(FT_Library library, FT_Bitmap *bitmap)
Definition: ftbitmap.c:648
GLuint GLuint end
typedefFT_BEGIN_HEADER struct FT_MemoryRec_ * FT_Memory
Definition: ftsystem.h:66
int width
Definition: ftimage.h:313
unsigned int FT_UInt
Definition: fttypes.h:227
GLenum target
GLdouble s
char pixel_mode
Definition: ftimage.h:317
GLuint GLuint GLsizei count
#define FT_INT_MAX
Definition: ftstdlib.h:64
GLdouble GLdouble t
#define FT_MEM_COPY(dest, source, count)
Definition: ftmemory.h:203
GLsizeiptr size
#define FT_PIX_ROUND(x)
Definition: ftobjs.h:81
FT_Bitmap_Convert(FT_Library library, const FT_Bitmap *source, FT_Bitmap *target, FT_Int alignment)
Definition: ftbitmap.c:377