OLD | NEW |
1 /***************************************************************************/ | 1 /***************************************************************************/ |
2 /* */ | 2 /* */ |
3 /* pngshim.c */ | 3 /* pngshim.c */ |
4 /* */ | 4 /* */ |
5 /* PNG Bitmap glyph support. */ | 5 /* PNG Bitmap glyph support. */ |
6 /* */ | 6 /* */ |
7 /* Copyright 2013, 2014 by Google, Inc. */ | 7 /* Copyright 2013-2015 by */ |
| 8 /* Google, Inc. */ |
8 /* Written by Stuart Gill and Behdad Esfahbod. */ | 9 /* Written by Stuart Gill and Behdad Esfahbod. */ |
9 /* */ | 10 /* */ |
10 /* This file is part of the FreeType project, and may only be used, */ | 11 /* 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 /* modified, and distributed under the terms of the FreeType project */ |
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ | 13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ |
13 /* this file you indicate that you have read the license and */ | 14 /* this file you indicate that you have read the license and */ |
14 /* understand and accept it fully. */ | 15 /* understand and accept it fully. */ |
15 /* */ | 16 /* */ |
16 /***************************************************************************/ | 17 /***************************************************************************/ |
17 | 18 |
(...skipping 11 matching lines...) Expand all Loading... |
29 #define PNG_SKIP_SETJMP_CHECK 1 | 30 #define PNG_SKIP_SETJMP_CHECK 1 |
30 #include <png.h> | 31 #include <png.h> |
31 #include "pngshim.h" | 32 #include "pngshim.h" |
32 | 33 |
33 #include "sferrors.h" | 34 #include "sferrors.h" |
34 | 35 |
35 | 36 |
36 /* This code is freely based on cairo-png.c. There's so many ways */ | 37 /* This code is freely based on cairo-png.c. There's so many ways */ |
37 /* to call libpng, and the way cairo does it is defacto standard. */ | 38 /* to call libpng, and the way cairo does it is defacto standard. */ |
38 | 39 |
39 static int | 40 static unsigned int |
40 multiply_alpha( int alpha, | 41 multiply_alpha( unsigned int alpha, |
41 int color ) | 42 unsigned int color ) |
42 { | 43 { |
43 int temp = ( alpha * color ) + 0x80; | 44 unsigned int temp = alpha * color + 0x80; |
44 | 45 |
45 | 46 |
46 return ( temp + ( temp >> 8 ) ) >> 8; | 47 return ( temp + ( temp >> 8 ) ) >> 8; |
47 } | 48 } |
48 | 49 |
49 | 50 |
50 /* Premultiplies data and converts RGBA bytes => native endian. */ | 51 /* Premultiplies data and converts RGBA bytes => native endian. */ |
51 static void | 52 static void |
52 premultiply_data( png_structp png, | 53 premultiply_data( png_structp png, |
53 png_row_infop row_info, | 54 png_row_infop row_info, |
(...skipping 20 matching lines...) Expand all Loading... |
74 unsigned int blue = base[2]; | 75 unsigned int blue = base[2]; |
75 | 76 |
76 | 77 |
77 if ( alpha != 0xFF ) | 78 if ( alpha != 0xFF ) |
78 { | 79 { |
79 red = multiply_alpha( alpha, red ); | 80 red = multiply_alpha( alpha, red ); |
80 green = multiply_alpha( alpha, green ); | 81 green = multiply_alpha( alpha, green ); |
81 blue = multiply_alpha( alpha, blue ); | 82 blue = multiply_alpha( alpha, blue ); |
82 } | 83 } |
83 | 84 |
84 base[0] = blue; | 85 base[0] = (unsigned char)blue; |
85 base[1] = green; | 86 base[1] = (unsigned char)green; |
86 base[2] = red; | 87 base[2] = (unsigned char)red; |
87 base[3] = alpha; | 88 base[3] = (unsigned char)alpha; |
88 } | 89 } |
89 } | 90 } |
90 } | 91 } |
91 | 92 |
92 | 93 |
93 /* Converts RGBx bytes to BGRA. */ | 94 /* Converts RGBx bytes to BGRA. */ |
94 static void | 95 static void |
95 convert_bytes_to_data( png_structp png, | 96 convert_bytes_to_data( png_structp png, |
96 png_row_infop row_info, | 97 png_row_infop row_info, |
97 png_bytep data ) | 98 png_bytep data ) |
98 { | 99 { |
99 unsigned int i; | 100 unsigned int i; |
100 | 101 |
101 FT_UNUSED( png ); | 102 FT_UNUSED( png ); |
102 | 103 |
103 | 104 |
104 for ( i = 0; i < row_info->rowbytes; i += 4 ) | 105 for ( i = 0; i < row_info->rowbytes; i += 4 ) |
105 { | 106 { |
106 unsigned char* base = &data[i]; | 107 unsigned char* base = &data[i]; |
107 unsigned int red = base[0]; | 108 unsigned int red = base[0]; |
108 unsigned int green = base[1]; | 109 unsigned int green = base[1]; |
109 unsigned int blue = base[2]; | 110 unsigned int blue = base[2]; |
110 | 111 |
111 | 112 |
112 base[0] = blue; | 113 base[0] = (unsigned char)blue; |
113 base[1] = green; | 114 base[1] = (unsigned char)green; |
114 base[2] = red; | 115 base[2] = (unsigned char)red; |
115 base[3] = 0xFF; | 116 base[3] = 0xFF; |
116 } | 117 } |
117 } | 118 } |
118 | 119 |
119 | 120 |
120 /* Use error callback to avoid png writing to stderr. */ | 121 /* Use error callback to avoid png writing to stderr. */ |
121 static void | 122 static void |
122 error_callback( png_structp png, | 123 error_callback( png_structp png, |
123 png_const_charp error_msg ) | 124 png_const_charp error_msg ) |
124 { | 125 { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 NULL, NULL ); | 251 NULL, NULL ); |
251 | 252 |
252 if ( error || | 253 if ( error || |
253 ( !populate_map_and_metrics && | 254 ( !populate_map_and_metrics && |
254 ( (FT_Int)imgWidth != metrics->width || | 255 ( (FT_Int)imgWidth != metrics->width || |
255 (FT_Int)imgHeight != metrics->height ) ) ) | 256 (FT_Int)imgHeight != metrics->height ) ) ) |
256 goto DestroyExit; | 257 goto DestroyExit; |
257 | 258 |
258 if ( populate_map_and_metrics ) | 259 if ( populate_map_and_metrics ) |
259 { | 260 { |
260 FT_Long size; | 261 FT_ULong size; |
261 | 262 |
262 | 263 |
263 metrics->width = (FT_Int)imgWidth; | 264 metrics->width = (FT_UShort)imgWidth; |
264 metrics->height = (FT_Int)imgHeight; | 265 metrics->height = (FT_UShort)imgHeight; |
265 | 266 |
266 map->width = metrics->width; | 267 map->width = metrics->width; |
267 map->rows = metrics->height; | 268 map->rows = metrics->height; |
268 map->pixel_mode = FT_PIXEL_MODE_BGRA; | 269 map->pixel_mode = FT_PIXEL_MODE_BGRA; |
269 map->pitch = map->width * 4; | 270 map->pitch = (int)( map->width * 4 ); |
270 map->num_grays = 256; | 271 map->num_grays = 256; |
271 | 272 |
272 /* reject too large bitmaps similarly to the rasterizer */ | 273 /* reject too large bitmaps similarly to the rasterizer */ |
273 if ( map->rows > 0x7FFF || map->width > 0x7FFF ) | 274 if ( map->rows > 0x7FFF || map->width > 0x7FFF ) |
274 { | 275 { |
275 error = FT_THROW( Array_Too_Large ); | 276 error = FT_THROW( Array_Too_Large ); |
276 goto DestroyExit; | 277 goto DestroyExit; |
277 } | 278 } |
278 | 279 |
279 /* this doesn't overflow: 0x7FFF * 0x7FFF * 4 < 2^32 */ | 280 /* this doesn't overflow: 0x7FFF * 0x7FFF * 4 < 2^32 */ |
280 size = map->rows * map->pitch; | 281 size = map->rows * (FT_ULong)map->pitch; |
281 | 282 |
282 error = ft_glyphslot_alloc_bitmap( slot, size ); | 283 error = ft_glyphslot_alloc_bitmap( slot, size ); |
283 if ( error ) | 284 if ( error ) |
284 goto DestroyExit; | 285 goto DestroyExit; |
285 } | 286 } |
286 | 287 |
287 /* convert palette/gray image to rgb */ | 288 /* convert palette/gray image to rgb */ |
288 if ( color_type == PNG_COLOR_TYPE_PALETTE ) | 289 if ( color_type == PNG_COLOR_TYPE_PALETTE ) |
289 png_set_palette_to_rgb( png ); | 290 png_set_palette_to_rgb( png ); |
290 | 291 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 FT_Stream_Close( &stream ); | 369 FT_Stream_Close( &stream ); |
369 | 370 |
370 Exit: | 371 Exit: |
371 return error; | 372 return error; |
372 } | 373 } |
373 | 374 |
374 #endif /* FT_CONFIG_OPTION_USE_PNG */ | 375 #endif /* FT_CONFIG_OPTION_USE_PNG */ |
375 | 376 |
376 | 377 |
377 /* END */ | 378 /* END */ |
OLD | NEW |