1 /***************************************************************************/
5 /* FreeType utility functions for bitmaps (body). */
7 /* Copyright 2004, 2005, 2006, 2007, 2008, 2009 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
22 #include FT_INTERNAL_OBJECTS_H
26 const FT_Bitmap null_bitmap = { 0, 0, 0, 0, 0, 0, 0, 0 };
29 /* documentation is in ftbitmap.h */
32 FT_Bitmap_New( FT_Bitmap *abitmap )
34 *abitmap = null_bitmap;
38 /* documentation is in ftbitmap.h */
40 FT_EXPORT_DEF( FT_Error )
41 FT_Bitmap_Copy( FT_Library library,
42 const FT_Bitmap *source,
45 FT_Memory memory = library->memory;
46 FT_Error error = FT_Err_Ok;
47 FT_Int pitch = source->pitch;
51 if ( source == target )
54 if ( source->buffer == NULL )
63 size = (FT_ULong)( pitch * source->rows );
67 FT_Int target_pitch = target->pitch;
71 if ( target_pitch < 0 )
72 target_pitch = -target_pitch;
73 target_size = (FT_ULong)( target_pitch * target->rows );
75 if ( target_size != size )
76 (void)FT_QREALLOC( target->buffer, target_size, size );
79 (void)FT_QALLOC( target->buffer, size );
90 FT_MEM_COPY( target->buffer, source->buffer, size );
98 ft_bitmap_assure_buffer( FT_Memory memory,
107 FT_Int i, width, height;
108 unsigned char* buffer;
111 width = bitmap->width;
112 height = bitmap->rows;
113 pitch = bitmap->pitch;
117 switch ( bitmap->pixel_mode )
119 case FT_PIXEL_MODE_MONO:
121 new_pitch = ( width + xpixels + 7 ) >> 3;
123 case FT_PIXEL_MODE_GRAY2:
125 new_pitch = ( width + xpixels + 3 ) >> 2;
127 case FT_PIXEL_MODE_GRAY4:
129 new_pitch = ( width + xpixels + 1 ) >> 1;
131 case FT_PIXEL_MODE_GRAY:
132 case FT_PIXEL_MODE_LCD:
133 case FT_PIXEL_MODE_LCD_V:
135 new_pitch = ( width + xpixels );
138 return FT_Err_Invalid_Glyph_Format;
141 /* if no need to allocate memory */
142 if ( ypixels == 0 && new_pitch <= pitch )
144 /* zero the padding */
145 FT_Int bit_width = pitch * 8;
146 FT_Int bit_last = ( width + xpixels ) * bpp;
149 if ( bit_last < bit_width )
151 FT_Byte* line = bitmap->buffer + ( bit_last >> 3 );
152 FT_Byte* end = bitmap->buffer + pitch;
153 FT_Int shift = bit_last & 7;
154 FT_UInt mask = 0xFF00U >> shift;
155 FT_Int count = height;
158 for ( ; count > 0; count--, line += pitch, end += pitch )
160 FT_Byte* write = line;
165 write[0] = (FT_Byte)( write[0] & mask );
169 FT_MEM_ZERO( write, end-write );
176 if ( FT_QALLOC_MULT( buffer, new_pitch, bitmap->rows + ypixels ) )
179 if ( bitmap->pitch > 0 )
181 FT_Int len = ( width * bpp + 7 ) >> 3;
184 for ( i = 0; i < bitmap->rows; i++ )
185 FT_MEM_COPY( buffer + new_pitch * ( ypixels + i ),
186 bitmap->buffer + pitch * i, len );
190 FT_Int len = ( width * bpp + 7 ) >> 3;
193 for ( i = 0; i < bitmap->rows; i++ )
194 FT_MEM_COPY( buffer + new_pitch * i,
195 bitmap->buffer + pitch * i, len );
198 FT_FREE( bitmap->buffer );
199 bitmap->buffer = buffer;
201 if ( bitmap->pitch < 0 )
202 new_pitch = -new_pitch;
204 /* set pitch only, width and height are left untouched */
205 bitmap->pitch = new_pitch;
211 /* documentation is in ftbitmap.h */
213 FT_EXPORT_DEF( FT_Error )
214 FT_Bitmap_Embolden( FT_Library library,
221 FT_Int i, x, y, pitch;
226 return FT_Err_Invalid_Library_Handle;
228 if ( !bitmap || !bitmap->buffer )
229 return FT_Err_Invalid_Argument;
231 if ( ( ( FT_PIX_ROUND( xStrength ) >> 6 ) > FT_INT_MAX ) ||
232 ( ( FT_PIX_ROUND( yStrength ) >> 6 ) > FT_INT_MAX ) )
233 return FT_Err_Invalid_Argument;
235 xstr = (FT_Int)FT_PIX_ROUND( xStrength ) >> 6;
236 ystr = (FT_Int)FT_PIX_ROUND( yStrength ) >> 6;
238 if ( xstr == 0 && ystr == 0 )
240 else if ( xstr < 0 || ystr < 0 )
241 return FT_Err_Invalid_Argument;
243 switch ( bitmap->pixel_mode )
245 case FT_PIXEL_MODE_GRAY2:
246 case FT_PIXEL_MODE_GRAY4:
252 if ( bitmap->pixel_mode == FT_PIXEL_MODE_GRAY2 )
253 align = ( bitmap->width + xstr + 3 ) / 4;
255 align = ( bitmap->width + xstr + 1 ) / 2;
257 FT_Bitmap_New( &tmp );
259 error = FT_Bitmap_Convert( library, bitmap, &tmp, align );
263 FT_Bitmap_Done( library, bitmap );
268 case FT_PIXEL_MODE_MONO:
273 case FT_PIXEL_MODE_LCD:
277 case FT_PIXEL_MODE_LCD_V:
282 error = ft_bitmap_assure_buffer( library->memory, bitmap, xstr, ystr );
286 pitch = bitmap->pitch;
288 p = bitmap->buffer + pitch * ystr;
292 p = bitmap->buffer + pitch * ( bitmap->rows - 1 );
296 for ( y = 0; y < bitmap->rows ; y++ )
301 * From the last pixel on, make each pixel or'ed with the
302 * `xstr' pixels before it.
304 for ( x = pitch - 1; x >= 0; x-- )
310 for ( i = 1; i <= xstr; i++ )
312 if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO )
316 /* the maximum value of 8 for `xstr' comes from here */
318 p[x] |= p[x - 1] << ( 8 - i );
329 if ( p[x] + p[x - i] > bitmap->num_grays - 1 )
331 p[x] = (unsigned char)(bitmap->num_grays - 1);
336 p[x] = (unsigned char)(p[x] + p[x-i]);
337 if ( p[x] == bitmap->num_grays - 1 )
350 * Make the above `ystr' rows or'ed with it.
352 for ( x = 1; x <= ystr; x++ )
357 q = p - bitmap->pitch * x;
358 for ( i = 0; i < pitch; i++ )
365 bitmap->width += xstr;
366 bitmap->rows += ystr;
372 /* documentation is in ftbitmap.h */
374 FT_EXPORT_DEF( FT_Error )
375 FT_Bitmap_Convert( FT_Library library,
376 const FT_Bitmap *source,
380 FT_Error error = FT_Err_Ok;
385 return FT_Err_Invalid_Library_Handle;
387 memory = library->memory;
389 switch ( source->pixel_mode )
391 case FT_PIXEL_MODE_MONO:
392 case FT_PIXEL_MODE_GRAY:
393 case FT_PIXEL_MODE_GRAY2:
394 case FT_PIXEL_MODE_GRAY4:
395 case FT_PIXEL_MODE_LCD:
396 case FT_PIXEL_MODE_LCD_V:
402 old_size = target->rows * target->pitch;
404 old_size = -old_size;
406 target->pixel_mode = FT_PIXEL_MODE_GRAY;
407 target->rows = source->rows;
408 target->width = source->width;
413 pad = source->width % alignment;
415 pad = alignment - pad;
418 target->pitch = source->width + pad;
420 if ( target->rows * target->pitch > old_size &&
421 FT_QREALLOC( target->buffer,
422 old_size, target->rows * target->pitch ) )
428 error = FT_Err_Invalid_Argument;
431 switch ( source->pixel_mode )
433 case FT_PIXEL_MODE_MONO:
435 FT_Byte* s = source->buffer;
436 FT_Byte* t = target->buffer;
440 target->num_grays = 2;
442 for ( i = source->rows; i > 0; i-- )
449 /* get the full bytes */
450 for ( j = source->width >> 3; j > 0; j-- )
452 FT_Int val = ss[0]; /* avoid a byte->int cast on each line */
455 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7 );
456 tt[1] = (FT_Byte)( ( val & 0x40 ) >> 6 );
457 tt[2] = (FT_Byte)( ( val & 0x20 ) >> 5 );
458 tt[3] = (FT_Byte)( ( val & 0x10 ) >> 4 );
459 tt[4] = (FT_Byte)( ( val & 0x08 ) >> 3 );
460 tt[5] = (FT_Byte)( ( val & 0x04 ) >> 2 );
461 tt[6] = (FT_Byte)( ( val & 0x02 ) >> 1 );
462 tt[7] = (FT_Byte)( val & 0x01 );
468 /* get remaining pixels (if any) */
469 j = source->width & 7;
477 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7);
490 case FT_PIXEL_MODE_GRAY:
491 case FT_PIXEL_MODE_LCD:
492 case FT_PIXEL_MODE_LCD_V:
494 FT_Int width = source->width;
495 FT_Byte* s = source->buffer;
496 FT_Byte* t = target->buffer;
497 FT_Int s_pitch = source->pitch;
498 FT_Int t_pitch = target->pitch;
502 target->num_grays = 256;
504 for ( i = source->rows; i > 0; i-- )
506 FT_ARRAY_COPY( t, s, width );
515 case FT_PIXEL_MODE_GRAY2:
517 FT_Byte* s = source->buffer;
518 FT_Byte* t = target->buffer;
522 target->num_grays = 4;
524 for ( i = source->rows; i > 0; i-- )
531 /* get the full bytes */
532 for ( j = source->width >> 2; j > 0; j-- )
537 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
538 tt[1] = (FT_Byte)( ( val & 0x30 ) >> 4 );
539 tt[2] = (FT_Byte)( ( val & 0x0C ) >> 2 );
540 tt[3] = (FT_Byte)( ( val & 0x03 ) );
546 j = source->width & 3;
554 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
567 case FT_PIXEL_MODE_GRAY4:
569 FT_Byte* s = source->buffer;
570 FT_Byte* t = target->buffer;
574 target->num_grays = 16;
576 for ( i = source->rows; i > 0; i-- )
583 /* get the full bytes */
584 for ( j = source->width >> 1; j > 0; j-- )
589 tt[0] = (FT_Byte)( ( val & 0xF0 ) >> 4 );
590 tt[1] = (FT_Byte)( ( val & 0x0F ) );
596 if ( source->width & 1 )
597 tt[0] = (FT_Byte)( ( ss[0] & 0xF0 ) >> 4 );
614 /* documentation is in ftbitmap.h */
616 FT_EXPORT_DEF( FT_Error )
617 FT_GlyphSlot_Own_Bitmap( FT_GlyphSlot slot )
619 if ( slot && slot->format == FT_GLYPH_FORMAT_BITMAP &&
620 !( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
626 FT_Bitmap_New( &bitmap );
627 error = FT_Bitmap_Copy( slot->library, &slot->bitmap, &bitmap );
631 slot->bitmap = bitmap;
632 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
639 /* documentation is in ftbitmap.h */
641 FT_EXPORT_DEF( FT_Error )
642 FT_Bitmap_Done( FT_Library library,
649 return FT_Err_Invalid_Library_Handle;
652 return FT_Err_Invalid_Argument;
654 memory = library->memory;
656 FT_FREE( bitmap->buffer );
657 *bitmap = null_bitmap;