1 /***************************************************************************/
5 /* FreeType utility functions for bitmaps (body). */
7 /* Copyright 2004-2009, 2011 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 = NULL;
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->pitch > 0 &&
421 (FT_ULong)target->rows > FT_ULONG_MAX / target->pitch )
422 return FT_Err_Invalid_Argument;
424 if ( target->rows * target->pitch > old_size &&
425 FT_QREALLOC( target->buffer,
426 old_size, target->rows * target->pitch ) )
432 error = FT_Err_Invalid_Argument;
435 switch ( source->pixel_mode )
437 case FT_PIXEL_MODE_MONO:
439 FT_Byte* s = source->buffer;
440 FT_Byte* t = target->buffer;
444 target->num_grays = 2;
446 for ( i = source->rows; i > 0; i-- )
453 /* get the full bytes */
454 for ( j = source->width >> 3; j > 0; j-- )
456 FT_Int val = ss[0]; /* avoid a byte->int cast on each line */
459 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7 );
460 tt[1] = (FT_Byte)( ( val & 0x40 ) >> 6 );
461 tt[2] = (FT_Byte)( ( val & 0x20 ) >> 5 );
462 tt[3] = (FT_Byte)( ( val & 0x10 ) >> 4 );
463 tt[4] = (FT_Byte)( ( val & 0x08 ) >> 3 );
464 tt[5] = (FT_Byte)( ( val & 0x04 ) >> 2 );
465 tt[6] = (FT_Byte)( ( val & 0x02 ) >> 1 );
466 tt[7] = (FT_Byte)( val & 0x01 );
472 /* get remaining pixels (if any) */
473 j = source->width & 7;
481 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7);
494 case FT_PIXEL_MODE_GRAY:
495 case FT_PIXEL_MODE_LCD:
496 case FT_PIXEL_MODE_LCD_V:
498 FT_Int width = source->width;
499 FT_Byte* s = source->buffer;
500 FT_Byte* t = target->buffer;
501 FT_Int s_pitch = source->pitch;
502 FT_Int t_pitch = target->pitch;
506 target->num_grays = 256;
508 for ( i = source->rows; i > 0; i-- )
510 FT_ARRAY_COPY( t, s, width );
519 case FT_PIXEL_MODE_GRAY2:
521 FT_Byte* s = source->buffer;
522 FT_Byte* t = target->buffer;
526 target->num_grays = 4;
528 for ( i = source->rows; i > 0; i-- )
535 /* get the full bytes */
536 for ( j = source->width >> 2; j > 0; j-- )
541 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
542 tt[1] = (FT_Byte)( ( val & 0x30 ) >> 4 );
543 tt[2] = (FT_Byte)( ( val & 0x0C ) >> 2 );
544 tt[3] = (FT_Byte)( ( val & 0x03 ) );
550 j = source->width & 3;
558 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
571 case FT_PIXEL_MODE_GRAY4:
573 FT_Byte* s = source->buffer;
574 FT_Byte* t = target->buffer;
578 target->num_grays = 16;
580 for ( i = source->rows; i > 0; i-- )
587 /* get the full bytes */
588 for ( j = source->width >> 1; j > 0; j-- )
593 tt[0] = (FT_Byte)( ( val & 0xF0 ) >> 4 );
594 tt[1] = (FT_Byte)( ( val & 0x0F ) );
600 if ( source->width & 1 )
601 tt[0] = (FT_Byte)( ( ss[0] & 0xF0 ) >> 4 );
618 /* documentation is in ftbitmap.h */
620 FT_EXPORT_DEF( FT_Error )
621 FT_GlyphSlot_Own_Bitmap( FT_GlyphSlot slot )
623 if ( slot && slot->format == FT_GLYPH_FORMAT_BITMAP &&
624 !( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
630 FT_Bitmap_New( &bitmap );
631 error = FT_Bitmap_Copy( slot->library, &slot->bitmap, &bitmap );
635 slot->bitmap = bitmap;
636 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
643 /* documentation is in ftbitmap.h */
645 FT_EXPORT_DEF( FT_Error )
646 FT_Bitmap_Done( FT_Library library,
653 return FT_Err_Invalid_Library_Handle;
656 return FT_Err_Invalid_Argument;
658 memory = library->memory;
660 FT_FREE( bitmap->buffer );
661 *bitmap = null_bitmap;