1 /***************************************************************************/
5 /* FreeType utility functions for bitmaps (body). */
7 /* Copyright 2004-2009, 2011, 2013, 2014 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 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
24 #include FT_INTERNAL_OBJECTS_H
28 const FT_Bitmap null_bitmap = { 0, 0, 0, 0, 0, 0, 0, 0 };
31 /* documentation is in ftbitmap.h */
34 FT_Bitmap_New( FT_Bitmap *abitmap )
37 *abitmap = null_bitmap;
41 /* documentation is in ftbitmap.h */
43 FT_EXPORT_DEF( FT_Error )
44 FT_Bitmap_Copy( FT_Library library,
45 const FT_Bitmap *source,
49 FT_Error error = FT_Err_Ok;
54 FT_Int source_pitch_sign, target_pitch_sign;
58 return FT_THROW( Invalid_Library_Handle );
60 if ( !source || !target )
61 return FT_THROW( Invalid_Argument );
63 if ( source == target )
66 source_pitch_sign = source->pitch < 0 ? -1 : 1;
67 target_pitch_sign = target->pitch < 0 ? -1 : 1;
69 if ( source->buffer == NULL )
72 if ( source_pitch_sign != target_pitch_sign )
73 target->pitch = -target->pitch;
78 memory = library->memory;
79 pitch = source->pitch;
83 size = (FT_ULong)pitch * source->rows;
87 FT_Int target_pitch = target->pitch;
91 if ( target_pitch < 0 )
92 target_pitch = -target_pitch;
93 target_size = (FT_ULong)target_pitch * target->rows;
95 if ( target_size != size )
96 (void)FT_QREALLOC( target->buffer, target_size, size );
99 (void)FT_QALLOC( target->buffer, size );
110 if ( source_pitch_sign == target_pitch_sign )
111 FT_MEM_COPY( target->buffer, source->buffer, size );
114 /* take care of bitmap flow */
116 FT_Byte* s = source->buffer;
117 FT_Byte* t = target->buffer;
120 t += pitch * ( target->rows - 1 );
122 for ( i = target->rows; i > 0; i-- )
124 FT_ARRAY_COPY( t, s, pitch );
136 /* Enlarge `bitmap' horizontally and vertically by `xpixels' */
137 /* and `ypixels', respectively. */
140 ft_bitmap_assure_buffer( FT_Memory memory,
149 FT_UInt i, width, height;
150 unsigned char* buffer = NULL;
153 width = bitmap->width;
154 height = bitmap->rows;
155 pitch = bitmap->pitch;
159 switch ( bitmap->pixel_mode )
161 case FT_PIXEL_MODE_MONO:
163 new_pitch = ( width + xpixels + 7 ) >> 3;
165 case FT_PIXEL_MODE_GRAY2:
167 new_pitch = ( width + xpixels + 3 ) >> 2;
169 case FT_PIXEL_MODE_GRAY4:
171 new_pitch = ( width + xpixels + 1 ) >> 1;
173 case FT_PIXEL_MODE_GRAY:
174 case FT_PIXEL_MODE_LCD:
175 case FT_PIXEL_MODE_LCD_V:
177 new_pitch = ( width + xpixels );
180 return FT_THROW( Invalid_Glyph_Format );
183 /* if no need to allocate memory */
184 if ( ypixels == 0 && new_pitch <= pitch )
186 /* zero the padding */
187 FT_UInt bit_width = pitch * 8;
188 FT_UInt bit_last = ( width + xpixels ) * bpp;
191 if ( bit_last < bit_width )
193 FT_Byte* line = bitmap->buffer + ( bit_last >> 3 );
194 FT_Byte* end = bitmap->buffer + pitch;
195 FT_UInt shift = bit_last & 7;
196 FT_UInt mask = 0xFF00U >> shift;
197 FT_UInt count = height;
200 for ( ; count > 0; count--, line += pitch, end += pitch )
202 FT_Byte* write = line;
207 write[0] = (FT_Byte)( write[0] & mask );
211 FT_MEM_ZERO( write, end - write );
218 /* otherwise allocate new buffer */
219 if ( FT_QALLOC_MULT( buffer, new_pitch, bitmap->rows + ypixels ) )
222 /* new rows get added at the top of the bitmap, */
223 /* thus take care of the flow direction */
224 if ( bitmap->pitch > 0 )
226 FT_UInt len = ( width * bpp + 7 ) >> 3;
229 for ( i = 0; i < bitmap->rows; i++ )
230 FT_MEM_COPY( buffer + new_pitch * ( ypixels + i ),
231 bitmap->buffer + pitch * i, len );
235 FT_UInt len = ( width * bpp + 7 ) >> 3;
238 for ( i = 0; i < bitmap->rows; i++ )
239 FT_MEM_COPY( buffer + new_pitch * i,
240 bitmap->buffer + pitch * i, len );
243 FT_FREE( bitmap->buffer );
244 bitmap->buffer = buffer;
246 if ( bitmap->pitch < 0 )
247 new_pitch = -new_pitch;
249 /* set pitch only, width and height are left untouched */
250 bitmap->pitch = new_pitch;
256 /* documentation is in ftbitmap.h */
258 FT_EXPORT_DEF( FT_Error )
259 FT_Bitmap_Embolden( FT_Library library,
272 return FT_THROW( Invalid_Library_Handle );
274 if ( !bitmap || !bitmap->buffer )
275 return FT_THROW( Invalid_Argument );
277 if ( ( ( FT_PIX_ROUND( xStrength ) >> 6 ) > FT_INT_MAX ) ||
278 ( ( FT_PIX_ROUND( yStrength ) >> 6 ) > FT_INT_MAX ) )
279 return FT_THROW( Invalid_Argument );
281 xstr = (FT_Int)FT_PIX_ROUND( xStrength ) >> 6;
282 ystr = (FT_Int)FT_PIX_ROUND( yStrength ) >> 6;
284 if ( xstr == 0 && ystr == 0 )
286 else if ( xstr < 0 || ystr < 0 )
287 return FT_THROW( Invalid_Argument );
289 switch ( bitmap->pixel_mode )
291 case FT_PIXEL_MODE_GRAY2:
292 case FT_PIXEL_MODE_GRAY4:
297 /* convert to 8bpp */
298 FT_Bitmap_New( &tmp );
299 error = FT_Bitmap_Convert( library, bitmap, &tmp, 1 );
303 FT_Bitmap_Done( library, bitmap );
308 case FT_PIXEL_MODE_MONO:
313 case FT_PIXEL_MODE_LCD:
317 case FT_PIXEL_MODE_LCD_V:
321 case FT_PIXEL_MODE_BGRA:
322 /* We don't embolden color glyphs. */
326 error = ft_bitmap_assure_buffer( library->memory, bitmap, xstr, ystr );
330 /* take care of bitmap flow */
331 pitch = bitmap->pitch;
333 p = bitmap->buffer + pitch * ystr;
337 p = bitmap->buffer + pitch * ( bitmap->rows - 1 );
341 for ( y = 0; y < bitmap->rows ; y++ )
346 * From the last pixel on, make each pixel or'ed with the
347 * `xstr' pixels before it.
349 for ( x = pitch - 1; x >= 0; x-- )
355 for ( i = 1; i <= xstr; i++ )
357 if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO )
361 /* the maximum value of 8 for `xstr' comes from here */
363 p[x] |= p[x - 1] << ( 8 - i );
374 if ( p[x] + p[x - i] > bitmap->num_grays - 1 )
376 p[x] = (unsigned char)( bitmap->num_grays - 1 );
381 p[x] = (unsigned char)( p[x] + p[x - i] );
382 if ( p[x] == bitmap->num_grays - 1 )
395 * Make the above `ystr' rows or'ed with it.
397 for ( x = 1; x <= ystr; x++ )
402 q = p - bitmap->pitch * x;
403 for ( i = 0; i < pitch; i++ )
410 bitmap->width += xstr;
411 bitmap->rows += ystr;
418 ft_gray_for_premultiplied_srgb_bgra( const FT_Byte* bgra )
424 /* Short-circuit transparent color to avoid division by zero. */
429 * Luminosity for sRGB is defined using ~0.2126,0.7152,0.0722
430 * coefficients for RGB channels *on the linear colors*.
431 * A gamma of 2.2 is fair to assume. And then, we need to
432 * undo the premultiplication too.
434 * http://accessibility.kde.org/hsl-adjusted.php
436 * We do the computation with integers only, applying a gamma of 2.0.
437 * We guarantee 32-bit arithmetic to avoid overflow but the resulting
438 * luminosity fits into 16 bits.
442 l = ( 4732UL /* 0.0722 * 65536 */ * bgra[0] * bgra[0] +
443 46871UL /* 0.7152 * 65536 */ * bgra[1] * bgra[1] +
444 13933UL /* 0.2126 * 65536 */ * bgra[2] * bgra[2] ) >> 16;
447 * Final transparency can be determined as follows.
449 * - If alpha is zero, we want 0.
450 * - If alpha is zero and luminosity is zero, we want 255.
451 * - If alpha is zero and luminosity is one, we want 0.
453 * So the formula is a * (1 - l) = a - l * a.
455 * We still need to undo premultiplication by dividing l by a*a.
459 return (FT_Byte)( a - l / a );
463 /* documentation is in ftbitmap.h */
465 FT_EXPORT_DEF( FT_Error )
466 FT_Bitmap_Convert( FT_Library library,
467 const FT_Bitmap *source,
471 FT_Error error = FT_Err_Ok;
479 return FT_THROW( Invalid_Library_Handle );
481 if ( !source || !target )
482 return FT_THROW( Invalid_Argument );
484 memory = library->memory;
486 switch ( source->pixel_mode )
488 case FT_PIXEL_MODE_MONO:
489 case FT_PIXEL_MODE_GRAY:
490 case FT_PIXEL_MODE_GRAY2:
491 case FT_PIXEL_MODE_GRAY4:
492 case FT_PIXEL_MODE_LCD:
493 case FT_PIXEL_MODE_LCD_V:
494 case FT_PIXEL_MODE_BGRA:
496 FT_Int pad, old_target_pitch, target_pitch;
500 old_target_pitch = target->pitch;
501 if ( old_target_pitch < 0 )
502 old_target_pitch = -old_target_pitch;
504 old_size = target->rows * old_target_pitch;
506 target->pixel_mode = FT_PIXEL_MODE_GRAY;
507 target->rows = source->rows;
508 target->width = source->width;
513 pad = source->width % alignment;
515 pad = alignment - pad;
518 target_pitch = source->width + pad;
520 if ( target_pitch > 0 &&
521 (FT_ULong)target->rows > FT_ULONG_MAX / target_pitch )
522 return FT_THROW( Invalid_Argument );
524 if ( target->rows * target_pitch > old_size &&
525 FT_QREALLOC( target->buffer,
526 old_size, target->rows * target_pitch ) )
529 target->pitch = target->pitch < 0 ? -target_pitch : target_pitch;
534 error = FT_THROW( Invalid_Argument );
540 /* take care of bitmap flow */
541 if ( source->pitch < 0 )
542 s -= source->pitch * ( source->rows - 1 );
543 if ( target->pitch < 0 )
544 t -= target->pitch * ( target->rows - 1 );
546 switch ( source->pixel_mode )
548 case FT_PIXEL_MODE_MONO:
553 target->num_grays = 2;
555 for ( i = source->rows; i > 0; i-- )
562 /* get the full bytes */
563 for ( j = source->width >> 3; j > 0; j-- )
565 FT_Int val = ss[0]; /* avoid a byte->int cast on each line */
568 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7 );
569 tt[1] = (FT_Byte)( ( val & 0x40 ) >> 6 );
570 tt[2] = (FT_Byte)( ( val & 0x20 ) >> 5 );
571 tt[3] = (FT_Byte)( ( val & 0x10 ) >> 4 );
572 tt[4] = (FT_Byte)( ( val & 0x08 ) >> 3 );
573 tt[5] = (FT_Byte)( ( val & 0x04 ) >> 2 );
574 tt[6] = (FT_Byte)( ( val & 0x02 ) >> 1 );
575 tt[7] = (FT_Byte)( val & 0x01 );
581 /* get remaining pixels (if any) */
582 j = source->width & 7;
590 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7);
603 case FT_PIXEL_MODE_GRAY:
604 case FT_PIXEL_MODE_LCD:
605 case FT_PIXEL_MODE_LCD_V:
607 FT_Int width = source->width;
611 target->num_grays = 256;
613 for ( i = source->rows; i > 0; i-- )
615 FT_ARRAY_COPY( t, s, width );
624 case FT_PIXEL_MODE_GRAY2:
629 target->num_grays = 4;
631 for ( i = source->rows; i > 0; i-- )
638 /* get the full bytes */
639 for ( j = source->width >> 2; j > 0; j-- )
644 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
645 tt[1] = (FT_Byte)( ( val & 0x30 ) >> 4 );
646 tt[2] = (FT_Byte)( ( val & 0x0C ) >> 2 );
647 tt[3] = (FT_Byte)( ( val & 0x03 ) );
653 j = source->width & 3;
661 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
674 case FT_PIXEL_MODE_GRAY4:
679 target->num_grays = 16;
681 for ( i = source->rows; i > 0; i-- )
688 /* get the full bytes */
689 for ( j = source->width >> 1; j > 0; j-- )
694 tt[0] = (FT_Byte)( ( val & 0xF0 ) >> 4 );
695 tt[1] = (FT_Byte)( ( val & 0x0F ) );
701 if ( source->width & 1 )
702 tt[0] = (FT_Byte)( ( ss[0] & 0xF0 ) >> 4 );
711 case FT_PIXEL_MODE_BGRA:
716 target->num_grays = 256;
718 for ( i = source->rows; i > 0; i-- )
725 for ( j = source->width; j > 0; j-- )
727 tt[0] = ft_gray_for_premultiplied_srgb_bgra( ss );
747 /* documentation is in ftbitmap.h */
749 FT_EXPORT_DEF( FT_Error )
750 FT_GlyphSlot_Own_Bitmap( FT_GlyphSlot slot )
752 if ( slot && slot->format == FT_GLYPH_FORMAT_BITMAP &&
753 !( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
759 FT_Bitmap_New( &bitmap );
760 error = FT_Bitmap_Copy( slot->library, &slot->bitmap, &bitmap );
764 slot->bitmap = bitmap;
765 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
772 /* documentation is in ftbitmap.h */
774 FT_EXPORT_DEF( FT_Error )
775 FT_Bitmap_Done( FT_Library library,
782 return FT_THROW( Invalid_Library_Handle );
785 return FT_THROW( Invalid_Argument );
787 memory = library->memory;
789 FT_FREE( bitmap->buffer );
790 *bitmap = null_bitmap;