| OLD | NEW |
| 1 #if !defined(_FXFT_VERSION_) || _FXFT_VERSION_ == 2501 | |
| 2 /***************************************************************************/ | 1 /***************************************************************************/ |
| 3 /* */ | 2 /* */ |
| 4 /* ftbitmap.c */ | 3 /* ftbitmap.c */ |
| 5 /* */ | 4 /* */ |
| 6 /* FreeType utility functions for bitmaps (body). */ | 5 /* FreeType utility functions for bitmaps (body). */ |
| 7 /* */ | 6 /* */ |
| 8 /* Copyright 2004-2009, 2011, 2013 by */ | 7 /* Copyright 2004-2009, 2011, 2013, 2014 by */ |
| 9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ | 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ |
| 10 /* */ | 9 /* */ |
| 11 /* This file is part of the FreeType project, and may only be used, */ | 10 /* This file is part of the FreeType project, and may only be used, */ |
| 12 /* modified, and distributed under the terms of the FreeType project */ | 11 /* modified, and distributed under the terms of the FreeType project */ |
| 13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ | 12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ |
| 14 /* this file you indicate that you have read the license and */ | 13 /* this file you indicate that you have read the license and */ |
| 15 /* understand and accept it fully. */ | 14 /* understand and accept it fully. */ |
| 16 /* */ | 15 /* */ |
| 17 /***************************************************************************/ | 16 /***************************************************************************/ |
| 18 | 17 |
| 19 #define FT2_BUILD_LIBRARY | 18 |
| 20 #include "../../include/ft2build.h" | 19 #include <ft2build.h> |
| 21 #include "../../include/freetype/internal/ftdebug.h" | 20 #include FT_INTERNAL_DEBUG_H |
| 22 #include "../../include/freetype/ftbitmap.h" | 21 |
| 23 #include "../../include/freetype/ftimage.h" | 22 #include FT_BITMAP_H |
| 24 #include "../../include/freetype/internal/ftobjs.h" | 23 #include FT_IMAGE_H |
| 24 #include FT_INTERNAL_OBJECTS_H |
| 25 | 25 |
| 26 | 26 |
| 27 static | 27 static |
| 28 const FT_Bitmap null_bitmap = { 0, 0, 0, 0, 0, 0, 0, 0 }; | 28 const FT_Bitmap null_bitmap = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 29 | 29 |
| 30 | 30 |
| 31 /* documentation is in ftbitmap.h */ | 31 /* documentation is in ftbitmap.h */ |
| 32 | 32 |
| 33 FT_EXPORT_DEF( void ) | 33 FT_EXPORT_DEF( void ) |
| 34 FT_Bitmap_New( FT_Bitmap *abitmap ) | 34 FT_Bitmap_New( FT_Bitmap *abitmap ) |
| 35 { | 35 { |
| 36 *abitmap = null_bitmap; | 36 if ( abitmap ) |
| 37 *abitmap = null_bitmap; |
| 37 } | 38 } |
| 38 | 39 |
| 39 | 40 |
| 40 /* documentation is in ftbitmap.h */ | 41 /* documentation is in ftbitmap.h */ |
| 41 | 42 |
| 42 FT_EXPORT_DEF( FT_Error ) | 43 FT_EXPORT_DEF( FT_Error ) |
| 43 FT_Bitmap_Copy( FT_Library library, | 44 FT_Bitmap_Copy( FT_Library library, |
| 44 const FT_Bitmap *source, | 45 const FT_Bitmap *source, |
| 45 FT_Bitmap *target) | 46 FT_Bitmap *target) |
| 46 { | 47 { |
| 47 FT_Memory memory = library->memory; | 48 FT_Memory memory; |
| 48 FT_Error error = FT_Err_Ok; | 49 FT_Error error = FT_Err_Ok; |
| 49 FT_Int pitch = source->pitch; | |
| 50 FT_ULong size; | |
| 51 | 50 |
| 51 FT_Int pitch; |
| 52 FT_ULong size; |
| 53 |
| 54 FT_Int source_pitch_sign, target_pitch_sign; |
| 55 |
| 56 |
| 57 if ( !library ) |
| 58 return FT_THROW( Invalid_Library_Handle ); |
| 59 |
| 60 if ( !source || !target ) |
| 61 return FT_THROW( Invalid_Argument ); |
| 52 | 62 |
| 53 if ( source == target ) | 63 if ( source == target ) |
| 54 return FT_Err_Ok; | 64 return FT_Err_Ok; |
| 55 | 65 |
| 66 source_pitch_sign = source->pitch < 0 ? -1 : 1; |
| 67 target_pitch_sign = target->pitch < 0 ? -1 : 1; |
| 68 |
| 56 if ( source->buffer == NULL ) | 69 if ( source->buffer == NULL ) |
| 57 { | 70 { |
| 58 *target = *source; | 71 *target = *source; |
| 72 if ( source_pitch_sign != target_pitch_sign ) |
| 73 target->pitch = -target->pitch; |
| 59 | 74 |
| 60 return FT_Err_Ok; | 75 return FT_Err_Ok; |
| 61 } | 76 } |
| 62 | 77 |
| 78 memory = library->memory; |
| 79 pitch = source->pitch; |
| 80 |
| 63 if ( pitch < 0 ) | 81 if ( pitch < 0 ) |
| 64 pitch = -pitch; | 82 pitch = -pitch; |
| 65 size = (FT_ULong)( pitch * source->rows ); | 83 size = (FT_ULong)pitch * source->rows; |
| 66 | 84 |
| 67 if ( target->buffer ) | 85 if ( target->buffer ) |
| 68 { | 86 { |
| 69 FT_Int target_pitch = target->pitch; | 87 FT_Int target_pitch = target->pitch; |
| 70 FT_ULong target_size; | 88 FT_ULong target_size; |
| 71 | 89 |
| 72 | 90 |
| 73 if ( target_pitch < 0 ) | 91 if ( target_pitch < 0 ) |
| 74 target_pitch = -target_pitch; | 92 target_pitch = -target_pitch; |
| 75 target_size = (FT_ULong)( target_pitch * target->rows ); | 93 target_size = (FT_ULong)target_pitch * target->rows; |
| 76 | 94 |
| 77 if ( target_size != size ) | 95 if ( target_size != size ) |
| 78 (void)FT_QREALLOC( target->buffer, target_size, size ); | 96 (void)FT_QREALLOC( target->buffer, target_size, size ); |
| 79 } | 97 } |
| 80 else | 98 else |
| 81 (void)FT_QALLOC( target->buffer, size ); | 99 (void)FT_QALLOC( target->buffer, size ); |
| 82 | 100 |
| 83 if ( !error ) | 101 if ( !error ) |
| 84 { | 102 { |
| 85 unsigned char *p; | 103 unsigned char *p; |
| 86 | 104 |
| 87 | 105 |
| 88 p = target->buffer; | 106 p = target->buffer; |
| 89 *target = *source; | 107 *target = *source; |
| 90 target->buffer = p; | 108 target->buffer = p; |
| 91 | 109 |
| 92 FT_MEM_COPY( target->buffer, source->buffer, size ); | 110 if ( source_pitch_sign == target_pitch_sign ) |
| 111 FT_MEM_COPY( target->buffer, source->buffer, size ); |
| 112 else |
| 113 { |
| 114 /* take care of bitmap flow */ |
| 115 FT_UInt i; |
| 116 FT_Byte* s = source->buffer; |
| 117 FT_Byte* t = target->buffer; |
| 118 |
| 119 |
| 120 t += pitch * ( target->rows - 1 ); |
| 121 |
| 122 for ( i = target->rows; i > 0; i-- ) |
| 123 { |
| 124 FT_ARRAY_COPY( t, s, pitch ); |
| 125 |
| 126 s += pitch; |
| 127 t -= pitch; |
| 128 } |
| 129 } |
| 93 } | 130 } |
| 94 | 131 |
| 95 return error; | 132 return error; |
| 96 } | 133 } |
| 97 | 134 |
| 98 | 135 |
| 136 /* Enlarge `bitmap' horizontally and vertically by `xpixels' */ |
| 137 /* and `ypixels', respectively. */ |
| 138 |
| 99 static FT_Error | 139 static FT_Error |
| 100 ft_bitmap_assure_buffer( FT_Memory memory, | 140 ft_bitmap_assure_buffer( FT_Memory memory, |
| 101 FT_Bitmap* bitmap, | 141 FT_Bitmap* bitmap, |
| 102 FT_UInt xpixels, | 142 FT_UInt xpixels, |
| 103 FT_UInt ypixels ) | 143 FT_UInt ypixels ) |
| 104 { | 144 { |
| 105 FT_Error error; | 145 FT_Error error; |
| 106 int pitch; | 146 int pitch; |
| 107 int new_pitch; | 147 int new_pitch; |
| 108 FT_UInt bpp; | 148 FT_UInt bpp; |
| 109 FT_Int i, width, height; | 149 FT_UInt i, width, height; |
| 110 unsigned char* buffer = NULL; | 150 unsigned char* buffer = NULL; |
| 111 | 151 |
| 112 | 152 |
| 113 width = bitmap->width; | 153 width = bitmap->width; |
| 114 height = bitmap->rows; | 154 height = bitmap->rows; |
| 115 pitch = bitmap->pitch; | 155 pitch = bitmap->pitch; |
| 116 if ( pitch < 0 ) | 156 if ( pitch < 0 ) |
| 117 pitch = -pitch; | 157 pitch = -pitch; |
| 118 | 158 |
| 119 switch ( bitmap->pixel_mode ) | 159 switch ( bitmap->pixel_mode ) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 137 new_pitch = ( width + xpixels ); | 177 new_pitch = ( width + xpixels ); |
| 138 break; | 178 break; |
| 139 default: | 179 default: |
| 140 return FT_THROW( Invalid_Glyph_Format ); | 180 return FT_THROW( Invalid_Glyph_Format ); |
| 141 } | 181 } |
| 142 | 182 |
| 143 /* if no need to allocate memory */ | 183 /* if no need to allocate memory */ |
| 144 if ( ypixels == 0 && new_pitch <= pitch ) | 184 if ( ypixels == 0 && new_pitch <= pitch ) |
| 145 { | 185 { |
| 146 /* zero the padding */ | 186 /* zero the padding */ |
| 147 FT_Int bit_width = pitch * 8; | 187 FT_UInt bit_width = pitch * 8; |
| 148 FT_Int bit_last = ( width + xpixels ) * bpp; | 188 FT_UInt bit_last = ( width + xpixels ) * bpp; |
| 149 | 189 |
| 150 | 190 |
| 151 if ( bit_last < bit_width ) | 191 if ( bit_last < bit_width ) |
| 152 { | 192 { |
| 153 FT_Byte* line = bitmap->buffer + ( bit_last >> 3 ); | 193 FT_Byte* line = bitmap->buffer + ( bit_last >> 3 ); |
| 154 FT_Byte* end = bitmap->buffer + pitch; | 194 FT_Byte* end = bitmap->buffer + pitch; |
| 155 FT_Int shift = bit_last & 7; | 195 FT_UInt shift = bit_last & 7; |
| 156 FT_UInt mask = 0xFF00U >> shift; | 196 FT_UInt mask = 0xFF00U >> shift; |
| 157 FT_Int count = height; | 197 FT_UInt count = height; |
| 158 | 198 |
| 159 | 199 |
| 160 for ( ; count > 0; count--, line += pitch, end += pitch ) | 200 for ( ; count > 0; count--, line += pitch, end += pitch ) |
| 161 { | 201 { |
| 162 FT_Byte* write = line; | 202 FT_Byte* write = line; |
| 163 | 203 |
| 164 | 204 |
| 165 if ( shift > 0 ) | 205 if ( shift > 0 ) |
| 166 { | 206 { |
| 167 write[0] = (FT_Byte)( write[0] & mask ); | 207 write[0] = (FT_Byte)( write[0] & mask ); |
| 168 write++; | 208 write++; |
| 169 } | 209 } |
| 170 if ( write < end ) | 210 if ( write < end ) |
| 171 FT_MEM_ZERO( write, end-write ); | 211 FT_MEM_ZERO( write, end - write ); |
| 172 } | 212 } |
| 173 } | 213 } |
| 174 | 214 |
| 175 return FT_Err_Ok; | 215 return FT_Err_Ok; |
| 176 } | 216 } |
| 177 | 217 |
| 218 /* otherwise allocate new buffer */ |
| 178 if ( FT_QALLOC_MULT( buffer, new_pitch, bitmap->rows + ypixels ) ) | 219 if ( FT_QALLOC_MULT( buffer, new_pitch, bitmap->rows + ypixels ) ) |
| 179 return error; | 220 return error; |
| 180 | 221 |
| 222 /* new rows get added at the top of the bitmap, */ |
| 223 /* thus take care of the flow direction */ |
| 181 if ( bitmap->pitch > 0 ) | 224 if ( bitmap->pitch > 0 ) |
| 182 { | 225 { |
| 183 FT_Int len = ( width * bpp + 7 ) >> 3; | 226 FT_UInt len = ( width * bpp + 7 ) >> 3; |
| 184 | 227 |
| 185 | 228 |
| 186 for ( i = 0; i < bitmap->rows; i++ ) | 229 for ( i = 0; i < bitmap->rows; i++ ) |
| 187 FT_MEM_COPY( buffer + new_pitch * ( ypixels + i ), | 230 FT_MEM_COPY( buffer + new_pitch * ( ypixels + i ), |
| 188 bitmap->buffer + pitch * i, len ); | 231 bitmap->buffer + pitch * i, len ); |
| 189 } | 232 } |
| 190 else | 233 else |
| 191 { | 234 { |
| 192 FT_Int len = ( width * bpp + 7 ) >> 3; | 235 FT_UInt len = ( width * bpp + 7 ) >> 3; |
| 193 | 236 |
| 194 | 237 |
| 195 for ( i = 0; i < bitmap->rows; i++ ) | 238 for ( i = 0; i < bitmap->rows; i++ ) |
| 196 FT_MEM_COPY( buffer + new_pitch * i, | 239 FT_MEM_COPY( buffer + new_pitch * i, |
| 197 bitmap->buffer + pitch * i, len ); | 240 bitmap->buffer + pitch * i, len ); |
| 198 } | 241 } |
| 199 | 242 |
| 200 FT_FREE( bitmap->buffer ); | 243 FT_FREE( bitmap->buffer ); |
| 201 bitmap->buffer = buffer; | 244 bitmap->buffer = buffer; |
| 202 | 245 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 213 /* documentation is in ftbitmap.h */ | 256 /* documentation is in ftbitmap.h */ |
| 214 | 257 |
| 215 FT_EXPORT_DEF( FT_Error ) | 258 FT_EXPORT_DEF( FT_Error ) |
| 216 FT_Bitmap_Embolden( FT_Library library, | 259 FT_Bitmap_Embolden( FT_Library library, |
| 217 FT_Bitmap* bitmap, | 260 FT_Bitmap* bitmap, |
| 218 FT_Pos xStrength, | 261 FT_Pos xStrength, |
| 219 FT_Pos yStrength ) | 262 FT_Pos yStrength ) |
| 220 { | 263 { |
| 221 FT_Error error; | 264 FT_Error error; |
| 222 unsigned char* p; | 265 unsigned char* p; |
| 223 FT_Int i, x, y, pitch; | 266 FT_Int i, x, pitch; |
| 267 FT_UInt y; |
| 224 FT_Int xstr, ystr; | 268 FT_Int xstr, ystr; |
| 225 | 269 |
| 226 | 270 |
| 227 if ( !library ) | 271 if ( !library ) |
| 228 return FT_THROW( Invalid_Library_Handle ); | 272 return FT_THROW( Invalid_Library_Handle ); |
| 229 | 273 |
| 230 if ( !bitmap || !bitmap->buffer ) | 274 if ( !bitmap || !bitmap->buffer ) |
| 231 return FT_THROW( Invalid_Argument ); | 275 return FT_THROW( Invalid_Argument ); |
| 232 | 276 |
| 233 if ( ( ( FT_PIX_ROUND( xStrength ) >> 6 ) > FT_INT_MAX ) || | 277 if ( ( ( FT_PIX_ROUND( xStrength ) >> 6 ) > FT_INT_MAX ) || |
| 234 ( ( FT_PIX_ROUND( yStrength ) >> 6 ) > FT_INT_MAX ) ) | 278 ( ( FT_PIX_ROUND( yStrength ) >> 6 ) > FT_INT_MAX ) ) |
| 235 return FT_THROW( Invalid_Argument ); | 279 return FT_THROW( Invalid_Argument ); |
| 236 | 280 |
| 237 xstr = (FT_Int)FT_PIX_ROUND( xStrength ) >> 6; | 281 xstr = (FT_Int)FT_PIX_ROUND( xStrength ) >> 6; |
| 238 ystr = (FT_Int)FT_PIX_ROUND( yStrength ) >> 6; | 282 ystr = (FT_Int)FT_PIX_ROUND( yStrength ) >> 6; |
| 239 | 283 |
| 240 if ( xstr == 0 && ystr == 0 ) | 284 if ( xstr == 0 && ystr == 0 ) |
| 241 return FT_Err_Ok; | 285 return FT_Err_Ok; |
| 242 else if ( xstr < 0 || ystr < 0 ) | 286 else if ( xstr < 0 || ystr < 0 ) |
| 243 return FT_THROW( Invalid_Argument ); | 287 return FT_THROW( Invalid_Argument ); |
| 244 | 288 |
| 245 switch ( bitmap->pixel_mode ) | 289 switch ( bitmap->pixel_mode ) |
| 246 { | 290 { |
| 247 case FT_PIXEL_MODE_GRAY2: | 291 case FT_PIXEL_MODE_GRAY2: |
| 248 case FT_PIXEL_MODE_GRAY4: | 292 case FT_PIXEL_MODE_GRAY4: |
| 249 { | 293 { |
| 250 FT_Bitmap tmp; | 294 FT_Bitmap tmp; |
| 251 FT_Int align; | |
| 252 | 295 |
| 253 | 296 |
| 254 if ( bitmap->pixel_mode == FT_PIXEL_MODE_GRAY2 ) | 297 /* convert to 8bpp */ |
| 255 align = ( bitmap->width + xstr + 3 ) / 4; | |
| 256 else | |
| 257 align = ( bitmap->width + xstr + 1 ) / 2; | |
| 258 | |
| 259 FT_Bitmap_New( &tmp ); | 298 FT_Bitmap_New( &tmp ); |
| 260 | 299 error = FT_Bitmap_Convert( library, bitmap, &tmp, 1 ); |
| 261 error = FT_Bitmap_Convert( library, bitmap, &tmp, align ); | |
| 262 if ( error ) | 300 if ( error ) |
| 263 return error; | 301 return error; |
| 264 | 302 |
| 265 FT_Bitmap_Done( library, bitmap ); | 303 FT_Bitmap_Done( library, bitmap ); |
| 266 *bitmap = tmp; | 304 *bitmap = tmp; |
| 267 } | 305 } |
| 268 break; | 306 break; |
| 269 | 307 |
| 270 case FT_PIXEL_MODE_MONO: | 308 case FT_PIXEL_MODE_MONO: |
| 271 if ( xstr > 8 ) | 309 if ( xstr > 8 ) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 282 | 320 |
| 283 case FT_PIXEL_MODE_BGRA: | 321 case FT_PIXEL_MODE_BGRA: |
| 284 /* We don't embolden color glyphs. */ | 322 /* We don't embolden color glyphs. */ |
| 285 return FT_Err_Ok; | 323 return FT_Err_Ok; |
| 286 } | 324 } |
| 287 | 325 |
| 288 error = ft_bitmap_assure_buffer( library->memory, bitmap, xstr, ystr ); | 326 error = ft_bitmap_assure_buffer( library->memory, bitmap, xstr, ystr ); |
| 289 if ( error ) | 327 if ( error ) |
| 290 return error; | 328 return error; |
| 291 | 329 |
| 330 /* take care of bitmap flow */ |
| 292 pitch = bitmap->pitch; | 331 pitch = bitmap->pitch; |
| 293 if ( pitch > 0 ) | 332 if ( pitch > 0 ) |
| 294 p = bitmap->buffer + pitch * ystr; | 333 p = bitmap->buffer + pitch * ystr; |
| 295 else | 334 else |
| 296 { | 335 { |
| 297 pitch = -pitch; | 336 pitch = -pitch; |
| 298 p = bitmap->buffer + pitch * ( bitmap->rows - 1 ); | 337 p = bitmap->buffer + pitch * ( bitmap->rows - 1 ); |
| 299 } | 338 } |
| 300 | 339 |
| 301 /* for each row */ | 340 /* for each row */ |
| 302 for ( y = 0; y < bitmap->rows ; y++ ) | 341 for ( y = 0; y < bitmap->rows ; y++ ) |
| 303 { | 342 { |
| 304 /* | 343 /* |
| 305 * Horizontally: | 344 * Horizontally: |
| 306 * | 345 * |
| 307 * From the last pixel on, make each pixel or'ed with the | 346 * From the last pixel on, make each pixel or'ed with the |
| 308 * `xstr' pixels before it. | 347 * `xstr' pixels before it. |
| 309 */ | 348 */ |
| 310 for ( x = pitch - 1; x >= 0; x-- ) | 349 for ( x = pitch - 1; x >= 0; x-- ) |
| 311 { | 350 { |
| 312 unsigned char tmp; | 351 unsigned char tmp; |
| 313 | 352 |
| 314 | 353 |
| 315 tmp = p[x]; | 354 tmp = p[x]; |
| 316 for ( i = 1; i <= xstr; i++ ) | 355 for ( i = 1; i <= xstr; i++ ) |
| 317 { | 356 { |
| 318 if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO ) | 357 if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO ) |
| 319 { | 358 { |
| 320 p[x] |= tmp >> i; | 359 p[x] |= tmp >> i; |
| 321 | 360 |
| 322 /* the maximum value of 8 for `xstr' comes from here */ | 361 /* the maximum value of 8 for `xstr' comes from here */ |
| 323 if ( x > 0 ) | 362 if ( x > 0 ) |
| 324 p[x] |= p[x - 1] << ( 8 - i ); | 363 p[x] |= p[x - 1] << ( 8 - i ); |
| 325 | 364 |
| 326 #if 0 | 365 #if 0 |
| 327 if ( p[x] == 0xff ) | 366 if ( p[x] == 0xff ) |
| 328 break; | 367 break; |
| 329 #endif | 368 #endif |
| 330 } | 369 } |
| 331 else | 370 else |
| 332 { | 371 { |
| 333 if ( x - i >= 0 ) | 372 if ( x - i >= 0 ) |
| 334 { | 373 { |
| 335 if ( p[x] + p[x - i] > bitmap->num_grays - 1 ) | 374 if ( p[x] + p[x - i] > bitmap->num_grays - 1 ) |
| 336 { | 375 { |
| 337 p[x] = (unsigned char)(bitmap->num_grays - 1); | 376 p[x] = (unsigned char)( bitmap->num_grays - 1 ); |
| 338 break; | 377 break; |
| 339 } | 378 } |
| 340 else | 379 else |
| 341 { | 380 { |
| 342 p[x] = (unsigned char)(p[x] + p[x-i]); | 381 p[x] = (unsigned char)( p[x] + p[x - i] ); |
| 343 if ( p[x] == bitmap->num_grays - 1 ) | 382 if ( p[x] == bitmap->num_grays - 1 ) |
| 344 break; | 383 break; |
| 345 } | 384 } |
| 346 } | 385 } |
| 347 else | 386 else |
| 348 break; | 387 break; |
| 349 } | 388 } |
| 350 } | 389 } |
| 351 } | 390 } |
| 352 | 391 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 368 p += bitmap->pitch; | 407 p += bitmap->pitch; |
| 369 } | 408 } |
| 370 | 409 |
| 371 bitmap->width += xstr; | 410 bitmap->width += xstr; |
| 372 bitmap->rows += ystr; | 411 bitmap->rows += ystr; |
| 373 | 412 |
| 374 return FT_Err_Ok; | 413 return FT_Err_Ok; |
| 375 } | 414 } |
| 376 | 415 |
| 377 | 416 |
| 378 FT_Byte | 417 static FT_Byte |
| 379 ft_gray_for_premultiplied_srgb_bgra( const FT_Byte* bgra ) | 418 ft_gray_for_premultiplied_srgb_bgra( const FT_Byte* bgra ) |
| 380 { | 419 { |
| 381 FT_Long a = bgra[3]; | 420 FT_UInt a = bgra[3]; |
| 382 FT_Long b = bgra[0]; | 421 FT_UInt l; |
| 383 FT_Long g = bgra[1]; | |
| 384 FT_Long r = bgra[2]; | |
| 385 FT_Long l; | |
| 386 | 422 |
| 387 | 423 |
| 424 /* Short-circuit transparent color to avoid division by zero. */ |
| 425 if ( !a ) |
| 426 return 0; |
| 427 |
| 388 /* | 428 /* |
| 389 * Luminosity for sRGB is defined using ~0.2126,0.7152,0.0722 | 429 * Luminosity for sRGB is defined using ~0.2126,0.7152,0.0722 |
| 390 * coefficients for RGB channels *on the linear colors*. | 430 * coefficients for RGB channels *on the linear colors*. |
| 391 * A gamma of 2.2 is fair to assume. And then, we need to | 431 * A gamma of 2.2 is fair to assume. And then, we need to |
| 392 * undo the premultiplication too. | 432 * undo the premultiplication too. |
| 393 * | 433 * |
| 394 * http://accessibility.kde.org/hsl-adjusted.php | 434 * http://accessibility.kde.org/hsl-adjusted.php |
| 395 * | 435 * |
| 396 * We do the computation with integers only. | 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. |
| 439 * |
| 397 */ | 440 */ |
| 398 | 441 |
| 399 /* Undo premultification, get the number in a 16.16 form. */ | 442 l = ( 4732UL /* 0.0722 * 65536 */ * bgra[0] * bgra[0] + |
| 400 b = FT_MulDiv( b, 65536, a ); | 443 46871UL /* 0.7152 * 65536 */ * bgra[1] * bgra[1] + |
| 401 g = FT_MulDiv( g, 65536, a ); | 444 13933UL /* 0.2126 * 65536 */ * bgra[2] * bgra[2] ) >> 16; |
| 402 r = FT_MulDiv( r, 65536, a ); | |
| 403 a = a * 256; | |
| 404 | |
| 405 /* Apply gamma of 2.0 instead of 2.2. */ | |
| 406 b = FT_MulFix( b, b ); | |
| 407 g = FT_MulFix( g, g ); | |
| 408 r = FT_MulFix( r, r ); | |
| 409 | |
| 410 /* Apply coefficients. */ | |
| 411 b = FT_MulFix( b, 4731 /* 0.0722 * 65536 */ ); | |
| 412 g = FT_MulFix( g, 46871 /* 0.7152 * 65536 */ ); | |
| 413 r = FT_MulFix( r, 13933 /* 0.2126 * 65536 */ ); | |
| 414 | |
| 415 l = r + g + b; | |
| 416 | 445 |
| 417 /* | 446 /* |
| 418 * Final transparency can be determined this way: | 447 * Final transparency can be determined as follows. |
| 419 * | 448 * |
| 420 * - If alpha is zero, we want 0. | 449 * - If alpha is zero, we want 0. |
| 421 * - If alpha is zero and luminosity is zero, we want 255. | 450 * - If alpha is zero and luminosity is zero, we want 255. |
| 422 * - If alpha is zero and luminosity is one, we want 0. | 451 * - If alpha is zero and luminosity is one, we want 0. |
| 423 * | 452 * |
| 424 * So the formula is a * (1 - l). | 453 * So the formula is a * (1 - l) = a - l * a. |
| 454 * |
| 455 * We still need to undo premultiplication by dividing l by a*a. |
| 456 * |
| 425 */ | 457 */ |
| 426 | 458 |
| 427 return (FT_Byte)( FT_MulFix( 65535 - l, a ) >> 8 ); | 459 return (FT_Byte)( a - l / a ); |
| 428 } | 460 } |
| 429 | 461 |
| 430 | 462 |
| 431 /* documentation is in ftbitmap.h */ | 463 /* documentation is in ftbitmap.h */ |
| 432 | 464 |
| 433 FT_EXPORT_DEF( FT_Error ) | 465 FT_EXPORT_DEF( FT_Error ) |
| 434 FT_Bitmap_Convert( FT_Library library, | 466 FT_Bitmap_Convert( FT_Library library, |
| 435 const FT_Bitmap *source, | 467 const FT_Bitmap *source, |
| 436 FT_Bitmap *target, | 468 FT_Bitmap *target, |
| 437 FT_Int alignment ) | 469 FT_Int alignment ) |
| 438 { | 470 { |
| 439 FT_Error error = FT_Err_Ok; | 471 FT_Error error = FT_Err_Ok; |
| 440 FT_Memory memory; | 472 FT_Memory memory; |
| 441 | 473 |
| 474 FT_Byte* s; |
| 475 FT_Byte* t; |
| 476 |
| 442 | 477 |
| 443 if ( !library ) | 478 if ( !library ) |
| 444 return FT_THROW( Invalid_Library_Handle ); | 479 return FT_THROW( Invalid_Library_Handle ); |
| 445 | 480 |
| 481 if ( !source || !target ) |
| 482 return FT_THROW( Invalid_Argument ); |
| 483 |
| 446 memory = library->memory; | 484 memory = library->memory; |
| 447 | 485 |
| 448 switch ( source->pixel_mode ) | 486 switch ( source->pixel_mode ) |
| 449 { | 487 { |
| 450 case FT_PIXEL_MODE_MONO: | 488 case FT_PIXEL_MODE_MONO: |
| 451 case FT_PIXEL_MODE_GRAY: | 489 case FT_PIXEL_MODE_GRAY: |
| 452 case FT_PIXEL_MODE_GRAY2: | 490 case FT_PIXEL_MODE_GRAY2: |
| 453 case FT_PIXEL_MODE_GRAY4: | 491 case FT_PIXEL_MODE_GRAY4: |
| 454 case FT_PIXEL_MODE_LCD: | 492 case FT_PIXEL_MODE_LCD: |
| 455 case FT_PIXEL_MODE_LCD_V: | 493 case FT_PIXEL_MODE_LCD_V: |
| 456 case FT_PIXEL_MODE_BGRA: | 494 case FT_PIXEL_MODE_BGRA: |
| 457 { | 495 { |
| 458 FT_Int pad; | 496 FT_Int pad, old_target_pitch, target_pitch; |
| 459 FT_Long old_size; | 497 FT_ULong old_size; |
| 460 | 498 |
| 461 | 499 |
| 462 old_size = target->rows * target->pitch; | 500 old_target_pitch = target->pitch; |
| 463 if ( old_size < 0 ) | 501 if ( old_target_pitch < 0 ) |
| 464 old_size = -old_size; | 502 old_target_pitch = -old_target_pitch; |
| 503 |
| 504 old_size = target->rows * old_target_pitch; |
| 465 | 505 |
| 466 target->pixel_mode = FT_PIXEL_MODE_GRAY; | 506 target->pixel_mode = FT_PIXEL_MODE_GRAY; |
| 467 target->rows = source->rows; | 507 target->rows = source->rows; |
| 468 target->width = source->width; | 508 target->width = source->width; |
| 469 | 509 |
| 470 pad = 0; | 510 pad = 0; |
| 471 if ( alignment > 0 ) | 511 if ( alignment > 0 ) |
| 472 { | 512 { |
| 473 pad = source->width % alignment; | 513 pad = source->width % alignment; |
| 474 if ( pad != 0 ) | 514 if ( pad != 0 ) |
| 475 pad = alignment - pad; | 515 pad = alignment - pad; |
| 476 } | 516 } |
| 477 | 517 |
| 478 target->pitch = source->width + pad; | 518 target_pitch = source->width + pad; |
| 479 | 519 |
| 480 if ( target->pitch > 0 && | 520 if ( target_pitch > 0 && |
| 481 (FT_ULong)target->rows > FT_ULONG_MAX / target->pitch ) | 521 (FT_ULong)target->rows > FT_ULONG_MAX / target_pitch ) |
| 482 return FT_THROW( Invalid_Argument ); | 522 return FT_THROW( Invalid_Argument ); |
| 483 | 523 |
| 484 if ( target->rows * target->pitch > old_size && | 524 if ( target->rows * target_pitch > old_size && |
| 485 FT_QREALLOC( target->buffer, | 525 FT_QREALLOC( target->buffer, |
| 486 old_size, target->rows * target->pitch ) ) | 526 old_size, target->rows * target_pitch ) ) |
| 487 return error; | 527 return error; |
| 528 |
| 529 target->pitch = target->pitch < 0 ? -target_pitch : target_pitch; |
| 488 } | 530 } |
| 489 break; | 531 break; |
| 490 | 532 |
| 491 default: | 533 default: |
| 492 error = FT_THROW( Invalid_Argument ); | 534 error = FT_THROW( Invalid_Argument ); |
| 493 } | 535 } |
| 494 | 536 |
| 537 s = source->buffer; |
| 538 t = target->buffer; |
| 539 |
| 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 ); |
| 545 |
| 495 switch ( source->pixel_mode ) | 546 switch ( source->pixel_mode ) |
| 496 { | 547 { |
| 497 case FT_PIXEL_MODE_MONO: | 548 case FT_PIXEL_MODE_MONO: |
| 498 { | 549 { |
| 499 FT_Byte* s = source->buffer; | 550 FT_UInt i; |
| 500 FT_Byte* t = target->buffer; | |
| 501 FT_Int i; | |
| 502 | 551 |
| 503 | 552 |
| 504 target->num_grays = 2; | 553 target->num_grays = 2; |
| 505 | 554 |
| 506 for ( i = source->rows; i > 0; i-- ) | 555 for ( i = source->rows; i > 0; i-- ) |
| 507 { | 556 { |
| 508 FT_Byte* ss = s; | 557 FT_Byte* ss = s; |
| 509 FT_Byte* tt = t; | 558 FT_Byte* tt = t; |
| 510 FT_Int j; | 559 FT_UInt j; |
| 511 | 560 |
| 512 | 561 |
| 513 /* get the full bytes */ | 562 /* get the full bytes */ |
| 514 for ( j = source->width >> 3; j > 0; j-- ) | 563 for ( j = source->width >> 3; j > 0; j-- ) |
| 515 { | 564 { |
| 516 FT_Int val = ss[0]; /* avoid a byte->int cast on each line */ | 565 FT_Int val = ss[0]; /* avoid a byte->int cast on each line */ |
| 517 | 566 |
| 518 | 567 |
| 519 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7 ); | 568 tt[0] = (FT_Byte)( ( val & 0x80 ) >> 7 ); |
| 520 tt[1] = (FT_Byte)( ( val & 0x40 ) >> 6 ); | 569 tt[1] = (FT_Byte)( ( val & 0x40 ) >> 6 ); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 548 t += target->pitch; | 597 t += target->pitch; |
| 549 } | 598 } |
| 550 } | 599 } |
| 551 break; | 600 break; |
| 552 | 601 |
| 553 | 602 |
| 554 case FT_PIXEL_MODE_GRAY: | 603 case FT_PIXEL_MODE_GRAY: |
| 555 case FT_PIXEL_MODE_LCD: | 604 case FT_PIXEL_MODE_LCD: |
| 556 case FT_PIXEL_MODE_LCD_V: | 605 case FT_PIXEL_MODE_LCD_V: |
| 557 { | 606 { |
| 558 FT_Int width = source->width; | 607 FT_Int width = source->width; |
| 559 FT_Byte* s = source->buffer; | 608 FT_UInt i; |
| 560 FT_Byte* t = target->buffer; | |
| 561 FT_Int s_pitch = source->pitch; | |
| 562 FT_Int t_pitch = target->pitch; | |
| 563 FT_Int i; | |
| 564 | 609 |
| 565 | 610 |
| 566 target->num_grays = 256; | 611 target->num_grays = 256; |
| 567 | 612 |
| 568 for ( i = source->rows; i > 0; i-- ) | 613 for ( i = source->rows; i > 0; i-- ) |
| 569 { | 614 { |
| 570 FT_ARRAY_COPY( t, s, width ); | 615 FT_ARRAY_COPY( t, s, width ); |
| 571 | 616 |
| 572 s += s_pitch; | 617 s += source->pitch; |
| 573 t += t_pitch; | 618 t += target->pitch; |
| 574 } | 619 } |
| 575 } | 620 } |
| 576 break; | 621 break; |
| 577 | 622 |
| 578 | 623 |
| 579 case FT_PIXEL_MODE_GRAY2: | 624 case FT_PIXEL_MODE_GRAY2: |
| 580 { | 625 { |
| 581 FT_Byte* s = source->buffer; | 626 FT_UInt i; |
| 582 FT_Byte* t = target->buffer; | |
| 583 FT_Int i; | |
| 584 | 627 |
| 585 | 628 |
| 586 target->num_grays = 4; | 629 target->num_grays = 4; |
| 587 | 630 |
| 588 for ( i = source->rows; i > 0; i-- ) | 631 for ( i = source->rows; i > 0; i-- ) |
| 589 { | 632 { |
| 590 FT_Byte* ss = s; | 633 FT_Byte* ss = s; |
| 591 FT_Byte* tt = t; | 634 FT_Byte* tt = t; |
| 592 FT_Int j; | 635 FT_UInt j; |
| 593 | 636 |
| 594 | 637 |
| 595 /* get the full bytes */ | 638 /* get the full bytes */ |
| 596 for ( j = source->width >> 2; j > 0; j-- ) | 639 for ( j = source->width >> 2; j > 0; j-- ) |
| 597 { | 640 { |
| 598 FT_Int val = ss[0]; | 641 FT_Int val = ss[0]; |
| 599 | 642 |
| 600 | 643 |
| 601 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 ); | 644 tt[0] = (FT_Byte)( ( val & 0xC0 ) >> 6 ); |
| 602 tt[1] = (FT_Byte)( ( val & 0x30 ) >> 4 ); | 645 tt[1] = (FT_Byte)( ( val & 0x30 ) >> 4 ); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 623 | 666 |
| 624 s += source->pitch; | 667 s += source->pitch; |
| 625 t += target->pitch; | 668 t += target->pitch; |
| 626 } | 669 } |
| 627 } | 670 } |
| 628 break; | 671 break; |
| 629 | 672 |
| 630 | 673 |
| 631 case FT_PIXEL_MODE_GRAY4: | 674 case FT_PIXEL_MODE_GRAY4: |
| 632 { | 675 { |
| 633 FT_Byte* s = source->buffer; | 676 FT_UInt i; |
| 634 FT_Byte* t = target->buffer; | |
| 635 FT_Int i; | |
| 636 | 677 |
| 637 | 678 |
| 638 target->num_grays = 16; | 679 target->num_grays = 16; |
| 639 | 680 |
| 640 for ( i = source->rows; i > 0; i-- ) | 681 for ( i = source->rows; i > 0; i-- ) |
| 641 { | 682 { |
| 642 FT_Byte* ss = s; | 683 FT_Byte* ss = s; |
| 643 FT_Byte* tt = t; | 684 FT_Byte* tt = t; |
| 644 FT_Int j; | 685 FT_UInt j; |
| 645 | 686 |
| 646 | 687 |
| 647 /* get the full bytes */ | 688 /* get the full bytes */ |
| 648 for ( j = source->width >> 1; j > 0; j-- ) | 689 for ( j = source->width >> 1; j > 0; j-- ) |
| 649 { | 690 { |
| 650 FT_Int val = ss[0]; | 691 FT_Int val = ss[0]; |
| 651 | 692 |
| 652 | 693 |
| 653 tt[0] = (FT_Byte)( ( val & 0xF0 ) >> 4 ); | 694 tt[0] = (FT_Byte)( ( val & 0xF0 ) >> 4 ); |
| 654 tt[1] = (FT_Byte)( ( val & 0x0F ) ); | 695 tt[1] = (FT_Byte)( ( val & 0x0F ) ); |
| 655 | 696 |
| 656 ss += 1; | 697 ss += 1; |
| 657 tt += 2; | 698 tt += 2; |
| 658 } | 699 } |
| 659 | 700 |
| 660 if ( source->width & 1 ) | 701 if ( source->width & 1 ) |
| 661 tt[0] = (FT_Byte)( ( ss[0] & 0xF0 ) >> 4 ); | 702 tt[0] = (FT_Byte)( ( ss[0] & 0xF0 ) >> 4 ); |
| 662 | 703 |
| 663 s += source->pitch; | 704 s += source->pitch; |
| 664 t += target->pitch; | 705 t += target->pitch; |
| 665 } | 706 } |
| 666 } | 707 } |
| 667 break; | 708 break; |
| 668 | 709 |
| 710 |
| 669 case FT_PIXEL_MODE_BGRA: | 711 case FT_PIXEL_MODE_BGRA: |
| 670 { | 712 { |
| 671 FT_Byte* s = source->buffer; | 713 FT_UInt i; |
| 672 FT_Byte* t = target->buffer; | |
| 673 FT_Int s_pitch = source->pitch; | |
| 674 FT_Int t_pitch = target->pitch; | |
| 675 FT_Int i; | |
| 676 | 714 |
| 677 | 715 |
| 678 target->num_grays = 256; | 716 target->num_grays = 256; |
| 679 | 717 |
| 680 for ( i = source->rows; i > 0; i-- ) | 718 for ( i = source->rows; i > 0; i-- ) |
| 681 { | 719 { |
| 682 FT_Byte* ss = s; | 720 FT_Byte* ss = s; |
| 683 FT_Byte* tt = t; | 721 FT_Byte* tt = t; |
| 684 FT_Int j; | 722 FT_UInt j; |
| 685 | 723 |
| 686 | 724 |
| 687 for ( j = source->width; j > 0; j-- ) | 725 for ( j = source->width; j > 0; j-- ) |
| 688 { | 726 { |
| 689 tt[0] = ft_gray_for_premultiplied_srgb_bgra( ss ); | 727 tt[0] = ft_gray_for_premultiplied_srgb_bgra( ss ); |
| 690 | 728 |
| 691 ss += 4; | 729 ss += 4; |
| 692 tt += 1; | 730 tt += 1; |
| 693 } | 731 } |
| 694 | 732 |
| 695 s += s_pitch; | 733 s += source->pitch; |
| 696 t += t_pitch; | 734 t += target->pitch; |
| 697 } | 735 } |
| 698 } | 736 } |
| 699 break; | 737 break; |
| 700 | 738 |
| 701 default: | 739 default: |
| 702 ; | 740 ; |
| 703 } | 741 } |
| 704 | 742 |
| 705 return error; | 743 return error; |
| 706 } | 744 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 memory = library->memory; | 787 memory = library->memory; |
| 750 | 788 |
| 751 FT_FREE( bitmap->buffer ); | 789 FT_FREE( bitmap->buffer ); |
| 752 *bitmap = null_bitmap; | 790 *bitmap = null_bitmap; |
| 753 | 791 |
| 754 return FT_Err_Ok; | 792 return FT_Err_Ok; |
| 755 } | 793 } |
| 756 | 794 |
| 757 | 795 |
| 758 /* END */ | 796 /* END */ |
| 759 #endif | |
| 760 | |
| OLD | NEW |