1 /***************************************************************************/
5 /* FreeType utility functions for bitmaps (body). */
7 /* Copyright 2004-2018 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_Init( FT_Bitmap *abitmap )
37 *abitmap = null_bitmap;
41 /* deprecated function name; retained for ABI compatibility */
44 FT_Bitmap_New( FT_Bitmap *abitmap )
47 *abitmap = null_bitmap;
51 /* documentation is in ftbitmap.h */
53 FT_EXPORT_DEF( FT_Error )
54 FT_Bitmap_Copy( FT_Library library,
55 const FT_Bitmap *source,
59 FT_Error error = FT_Err_Ok;
64 FT_Int source_pitch_sign, target_pitch_sign;
68 return FT_THROW( Invalid_Library_Handle );
70 if ( !source || !target )
71 return FT_THROW( Invalid_Argument );
73 if ( source == target )
76 source_pitch_sign = source->pitch < 0 ? -1 : 1;
77 target_pitch_sign = target->pitch < 0 ? -1 : 1;
79 if ( !source->buffer )
82 if ( source_pitch_sign != target_pitch_sign )
83 target->pitch = -target->pitch;
88 memory = library->memory;
89 pitch = source->pitch;
93 size = (FT_ULong)pitch * source->rows;
97 FT_Int target_pitch = target->pitch;
101 if ( target_pitch < 0 )
102 target_pitch = -target_pitch;
103 target_size = (FT_ULong)target_pitch * target->rows;
105 if ( target_size != size )
106 (void)FT_QREALLOC( target->buffer, target_size, size );
109 (void)FT_QALLOC( target->buffer, size );
120 if ( source_pitch_sign == target_pitch_sign )
121 FT_MEM_COPY( target->buffer, source->buffer, size );
124 /* take care of bitmap flow */
126 FT_Byte* s = source->buffer;
127 FT_Byte* t = target->buffer;
130 t += (FT_ULong)pitch * ( target->rows - 1 );
132 for ( i = target->rows; i > 0; i-- )
134 FT_ARRAY_COPY( t, s, pitch );
146 /* Enlarge `bitmap' horizontally and vertically by `xpixels' */
147 /* and `ypixels', respectively. */
150 ft_bitmap_assure_buffer( FT_Memory memory,
157 unsigned int new_pitch;
159 FT_UInt width, height;
160 unsigned char* buffer = NULL;
163 width = bitmap->width;
164 height = bitmap->rows;
165 pitch = (unsigned int)FT_ABS( bitmap->pitch );
167 switch ( bitmap->pixel_mode )
169 case FT_PIXEL_MODE_MONO:
171 new_pitch = ( width + xpixels + 7 ) >> 3;
173 case FT_PIXEL_MODE_GRAY2:
175 new_pitch = ( width + xpixels + 3 ) >> 2;
177 case FT_PIXEL_MODE_GRAY4:
179 new_pitch = ( width + xpixels + 1 ) >> 1;
181 case FT_PIXEL_MODE_GRAY:
182 case FT_PIXEL_MODE_LCD:
183 case FT_PIXEL_MODE_LCD_V:
185 new_pitch = width + xpixels;
188 return FT_THROW( Invalid_Glyph_Format );
191 /* if no need to allocate memory */
192 if ( ypixels == 0 && new_pitch <= pitch )
194 /* zero the padding */
195 FT_UInt bit_width = pitch * 8;
196 FT_UInt bit_last = ( width + xpixels ) * bpp;
199 if ( bit_last < bit_width )
201 FT_Byte* line = bitmap->buffer + ( bit_last >> 3 );
202 FT_Byte* end = bitmap->buffer + pitch;
203 FT_UInt shift = bit_last & 7;
204 FT_UInt mask = 0xFF00U >> shift;
205 FT_UInt count = height;
208 for ( ; count > 0; count--, line += pitch, end += pitch )
210 FT_Byte* write = line;
215 write[0] = (FT_Byte)( write[0] & mask );
219 FT_MEM_ZERO( write, end - write );
226 /* otherwise allocate new buffer */
227 if ( FT_QALLOC_MULT( buffer, bitmap->rows + ypixels, new_pitch ) )
230 /* new rows get added at the top of the bitmap, */
231 /* thus take care of the flow direction */
232 if ( bitmap->pitch > 0 )
234 FT_UInt len = ( width * bpp + 7 ) >> 3;
236 unsigned char* in = bitmap->buffer;
237 unsigned char* out = buffer;
239 unsigned char* limit = bitmap->buffer + pitch * bitmap->rows;
240 unsigned int delta = new_pitch - pitch;
243 FT_MEM_ZERO( out, new_pitch * ypixels );
244 out += new_pitch * ypixels;
248 FT_MEM_COPY( out, in, len );
252 FT_MEM_ZERO( out, delta );
258 FT_UInt len = ( width * bpp + 7 ) >> 3;
260 unsigned char* in = bitmap->buffer;
261 unsigned char* out = buffer;
263 unsigned char* limit = bitmap->buffer + pitch * bitmap->rows;
264 unsigned int delta = new_pitch - pitch;
269 FT_MEM_COPY( out, in, len );
273 FT_MEM_ZERO( out, delta );
277 FT_MEM_ZERO( out, new_pitch * ypixels );
280 FT_FREE( bitmap->buffer );
281 bitmap->buffer = buffer;
283 /* set pitch only, width and height are left untouched */
284 if ( bitmap->pitch < 0 )
285 bitmap->pitch = -(int)new_pitch;
287 bitmap->pitch = (int)new_pitch;
293 /* documentation is in ftbitmap.h */
295 FT_EXPORT_DEF( FT_Error )
296 FT_Bitmap_Embolden( FT_Library library,
309 return FT_THROW( Invalid_Library_Handle );
311 if ( !bitmap || !bitmap->buffer )
312 return FT_THROW( Invalid_Argument );
314 if ( ( ( FT_PIX_ROUND( xStrength ) >> 6 ) > FT_INT_MAX ) ||
315 ( ( FT_PIX_ROUND( yStrength ) >> 6 ) > FT_INT_MAX ) )
316 return FT_THROW( Invalid_Argument );
318 xstr = (FT_Int)FT_PIX_ROUND( xStrength ) >> 6;
319 ystr = (FT_Int)FT_PIX_ROUND( yStrength ) >> 6;
321 if ( xstr == 0 && ystr == 0 )
323 else if ( xstr < 0 || ystr < 0 )
324 return FT_THROW( Invalid_Argument );
326 switch ( bitmap->pixel_mode )
328 case FT_PIXEL_MODE_GRAY2:
329 case FT_PIXEL_MODE_GRAY4:
334 /* convert to 8bpp */
335 FT_Bitmap_Init( &tmp );
336 error = FT_Bitmap_Convert( library, bitmap, &tmp, 1 );
340 FT_Bitmap_Done( library, bitmap );
345 case FT_PIXEL_MODE_MONO:
350 case FT_PIXEL_MODE_LCD:
354 case FT_PIXEL_MODE_LCD_V:
358 case FT_PIXEL_MODE_BGRA:
359 /* We don't embolden color glyphs. */
363 error = ft_bitmap_assure_buffer( library->memory, bitmap,
364 (FT_UInt)xstr, (FT_UInt)ystr );
368 /* take care of bitmap flow */
369 pitch = bitmap->pitch;
371 p = bitmap->buffer + pitch * ystr;
375 p = bitmap->buffer + (FT_UInt)pitch * ( bitmap->rows - 1 );
379 for ( y = 0; y < bitmap->rows; y++ )
384 * From the last pixel on, make each pixel or'ed with the
385 * `xstr' pixels before it.
387 for ( x = pitch - 1; x >= 0; x-- )
393 for ( i = 1; i <= xstr; i++ )
395 if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO )
399 /* the maximum value of 8 for `xstr' comes from here */
401 p[x] |= p[x - 1] << ( 8 - i );
412 if ( p[x] + p[x - i] > bitmap->num_grays - 1 )
414 p[x] = (unsigned char)( bitmap->num_grays - 1 );
419 p[x] = (unsigned char)( p[x] + p[x - i] );
420 if ( p[x] == bitmap->num_grays - 1 )
433 * Make the above `ystr' rows or'ed with it.
435 for ( x = 1; x <= ystr; x++ )
440 q = p - bitmap->pitch * x;
441 for ( i = 0; i < pitch; i++ )
448 bitmap->width += (FT_UInt)xstr;
449 bitmap->rows += (FT_UInt)ystr;
456 ft_gray_for_premultiplied_srgb_bgra( const FT_Byte* bgra )
462 /* Short-circuit transparent color to avoid division by zero. */
467 * Luminosity for sRGB is defined using ~0.2126,0.7152,0.0722
468 * coefficients for RGB channels *on the linear colors*.
469 * A gamma of 2.2 is fair to assume. And then, we need to
470 * undo the premultiplication too.
472 * https://accessibility.kde.org/hsl-adjusted.php
474 * We do the computation with integers only, applying a gamma of 2.0.
475 * We guarantee 32-bit arithmetic to avoid overflow but the resulting
476 * luminosity fits into 16 bits.
480 l = ( 4732UL /* 0.0722 * 65536 */ * bgra[0] * bgra[0] +
481 46871UL /* 0.7152 * 65536 */ * bgra[1] * bgra[1] +
482 13933UL /* 0.2126 * 65536 */ * bgra[2] * bgra[2] ) >> 16;
485 * Final transparency can be determined as follows.
487 * - If alpha is zero, we want 0.
488 * - If alpha is zero and luminosity is zero, we want 255.
489 * - If alpha is zero and luminosity is one, we want 0.
491 * So the formula is a * (1 - l) = a - l * a.
493 * We still need to undo premultiplication by dividing l by a*a.
497 return (FT_Byte)( a - l / a );
501 /* documentation is in ftbitmap.h */
503 FT_EXPORT_DEF( FT_Error )
504 FT_Bitmap_Convert( FT_Library library,
505 const FT_Bitmap *source,
509 FT_Error error = FT_Err_Ok;
517 return FT_THROW( Invalid_Library_Handle );
519 if ( !source || !target )
520 return FT_THROW( Invalid_Argument );
522 memory = library->memory;
524 switch ( source->pixel_mode )
526 case FT_PIXEL_MODE_MONO:
527 case FT_PIXEL_MODE_GRAY:
528 case FT_PIXEL_MODE_GRAY2:
529 case FT_PIXEL_MODE_GRAY4:
530 case FT_PIXEL_MODE_LCD:
531 case FT_PIXEL_MODE_LCD_V:
532 case FT_PIXEL_MODE_BGRA:
534 FT_Int pad, old_target_pitch, target_pitch;
538 old_target_pitch = target->pitch;
539 if ( old_target_pitch < 0 )
540 old_target_pitch = -old_target_pitch;
542 old_size = target->rows * (FT_UInt)old_target_pitch;
544 target->pixel_mode = FT_PIXEL_MODE_GRAY;
545 target->rows = source->rows;
546 target->width = source->width;
551 pad = (FT_Int)source->width % alignment;
553 pad = alignment - pad;
556 target_pitch = (FT_Int)source->width + pad;
558 if ( target_pitch > 0 &&
559 (FT_ULong)target->rows > FT_ULONG_MAX / (FT_ULong)target_pitch )
560 return FT_THROW( Invalid_Argument );
562 if ( FT_QREALLOC( target->buffer,
563 old_size, target->rows * (FT_UInt)target_pitch ) )
566 target->pitch = target->pitch < 0 ? -target_pitch : target_pitch;
571 error = FT_THROW( Invalid_Argument );
577 /* take care of bitmap flow */
578 if ( source->pitch < 0 )
579 s -= source->pitch * (FT_Int)( source->rows - 1 );
580 if ( target->pitch < 0 )
581 t -= target->pitch * (FT_Int)( target->rows - 1 );
583 switch ( source->pixel_mode )
585 case FT_PIXEL_MODE_MONO:
590 target->num_grays = 2;
592 for ( i = source->rows; i > 0; i-- )
599 /* get the full bytes */
600 for ( j = source->width >> 3; j > 0; j-- )
602 FT_Int val = ss[0]; /* avoid a byte->int cast on each line */
605 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7 );
606 tt[1] = (FT_Byte)( ( val & 0x40 ) >> 6 );
607 tt[2] = (FT_Byte)( ( val & 0x20 ) >> 5 );
608 tt[3] = (FT_Byte)( ( val & 0x10 ) >> 4 );
609 tt[4] = (FT_Byte)( ( val & 0x08 ) >> 3 );
610 tt[5] = (FT_Byte)( ( val & 0x04 ) >> 2 );
611 tt[6] = (FT_Byte)( ( val & 0x02 ) >> 1 );
612 tt[7] = (FT_Byte)( val & 0x01 );
618 /* get remaining pixels (if any) */
619 j = source->width & 7;
627 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7);
640 case FT_PIXEL_MODE_GRAY:
641 case FT_PIXEL_MODE_LCD:
642 case FT_PIXEL_MODE_LCD_V:
644 FT_UInt width = source->width;
648 target->num_grays = 256;
650 for ( i = source->rows; i > 0; i-- )
652 FT_ARRAY_COPY( t, s, width );
661 case FT_PIXEL_MODE_GRAY2:
666 target->num_grays = 4;
668 for ( i = source->rows; i > 0; i-- )
675 /* get the full bytes */
676 for ( j = source->width >> 2; j > 0; j-- )
681 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
682 tt[1] = (FT_Byte)( ( val & 0x30 ) >> 4 );
683 tt[2] = (FT_Byte)( ( val & 0x0C ) >> 2 );
684 tt[3] = (FT_Byte)( ( val & 0x03 ) );
690 j = source->width & 3;
698 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 );
711 case FT_PIXEL_MODE_GRAY4:
716 target->num_grays = 16;
718 for ( i = source->rows; i > 0; i-- )
725 /* get the full bytes */
726 for ( j = source->width >> 1; j > 0; j-- )
731 tt[0] = (FT_Byte)( ( val & 0xF0 ) >> 4 );
732 tt[1] = (FT_Byte)( ( val & 0x0F ) );
738 if ( source->width & 1 )
739 tt[0] = (FT_Byte)( ( ss[0] & 0xF0 ) >> 4 );
748 case FT_PIXEL_MODE_BGRA:
753 target->num_grays = 256;
755 for ( i = source->rows; i > 0; i-- )
762 for ( j = source->width; j > 0; j-- )
764 tt[0] = ft_gray_for_premultiplied_srgb_bgra( ss );
784 /* documentation is in ftbitmap.h */
786 FT_EXPORT_DEF( FT_Error )
787 FT_GlyphSlot_Own_Bitmap( FT_GlyphSlot slot )
789 if ( slot && slot->format == FT_GLYPH_FORMAT_BITMAP &&
790 !( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) )
796 FT_Bitmap_Init( &bitmap );
797 error = FT_Bitmap_Copy( slot->library, &slot->bitmap, &bitmap );
801 slot->bitmap = bitmap;
802 slot->internal->flags |= FT_GLYPH_OWN_BITMAP;
809 /* documentation is in ftbitmap.h */
811 FT_EXPORT_DEF( FT_Error )
812 FT_Bitmap_Done( FT_Library library,
819 return FT_THROW( Invalid_Library_Handle );
822 return FT_THROW( Invalid_Argument );
824 memory = library->memory;
826 FT_FREE( bitmap->buffer );
827 *bitmap = null_bitmap;