cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP N N GGGG % |
| 7 | % P P NN N G % |
| 8 | % PPPP N N N G GG % |
| 9 | % P N NN G G % |
| 10 | % P N N GGG % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Portable Network Graphics Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % Glenn Randers-Pehrson % |
| 18 | % November 1997 % |
| 19 | % % |
| 20 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 21 | % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 22 | % dedicated to making software imaging solutions freely available. % |
| 23 | % % |
| 24 | % You may not use this file except in compliance with the License. You may % |
| 25 | % obtain a copy of the License at % |
| 26 | % % |
| 27 | % http://www.imagemagick.org/script/license.php % |
| 28 | % % |
| 29 | % Unless required by applicable law or agreed to in writing, software % |
| 30 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 31 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 32 | % See the License for the specific language governing permissions and % |
| 33 | % limitations under the License. % |
| 34 | % % |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 36 | % |
| 37 | % |
| 38 | */ |
| 39 | |
| 40 | |
| 41 | /* |
| 42 | Include declarations. |
| 43 | */ |
| 44 | #include "magick/studio.h" |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 45 | #include "magick/artifact.h" |
cristy | 5a2ca48 | 2009-10-14 18:24:56 +0000 | [diff] [blame] | 46 | #include "magick/attribute.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 47 | #include "magick/blob.h" |
| 48 | #include "magick/blob-private.h" |
| 49 | #include "magick/cache.h" |
| 50 | #include "magick/color.h" |
| 51 | #include "magick/color-private.h" |
cristy | 4ccd4c0 | 2010-04-25 00:43:15 +0000 | [diff] [blame] | 52 | #include "magick/colormap.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 53 | #include "magick/colorspace.h" |
| 54 | #include "magick/constitute.h" |
| 55 | #include "magick/enhance.h" |
| 56 | #include "magick/exception.h" |
| 57 | #include "magick/exception-private.h" |
| 58 | #include "magick/geometry.h" |
cristy | f2e1166 | 2009-10-14 01:24:43 +0000 | [diff] [blame] | 59 | #include "magick/histogram.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 60 | #include "magick/image.h" |
| 61 | #include "magick/image-private.h" |
| 62 | #include "magick/layer.h" |
| 63 | #include "magick/list.h" |
| 64 | #include "magick/log.h" |
| 65 | #include "magick/magick.h" |
| 66 | #include "magick/memory_.h" |
| 67 | #include "magick/module.h" |
| 68 | #include "magick/monitor.h" |
| 69 | #include "magick/monitor-private.h" |
| 70 | #include "magick/option.h" |
| 71 | #include "magick/quantum-private.h" |
| 72 | #include "magick/profile.h" |
| 73 | #include "magick/property.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 74 | #include "magick/resource_.h" |
| 75 | #include "magick/semaphore.h" |
| 76 | #include "magick/quantum-private.h" |
| 77 | #include "magick/static.h" |
| 78 | #include "magick/statistic.h" |
| 79 | #include "magick/string_.h" |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 80 | #include "magick/string-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 81 | #include "magick/transform.h" |
| 82 | #include "magick/utility.h" |
| 83 | #if defined(MAGICKCORE_PNG_DELEGATE) |
glennrp | 286a635 | 2009-11-09 02:58:50 +0000 | [diff] [blame] | 84 | |
glennrp | 7ef138c | 2009-11-10 13:50:20 +0000 | [diff] [blame] | 85 | /* Suppress libpng pedantic warnings that were added in |
| 86 | * libpng-1.2.41 and libpng-1.4.0. If you are working on |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 87 | * migration to libpng-1.5, remove these defines and then |
glennrp | 7ef138c | 2009-11-10 13:50:20 +0000 | [diff] [blame] | 88 | * fix any code that generates warnings. |
| 89 | */ |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 90 | /* #define PNG_DEPRECATED Use of this function is deprecated */ |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 91 | /* #define PNG_USE_RESULT The result of this function must be checked */ |
| 92 | /* #define PNG_NORETURN This function does not return */ |
| 93 | /* #define PNG_ALLOCATED The result of the function is new memory */ |
glennrp | 8371ecc | 2011-02-19 19:15:38 +0000 | [diff] [blame] | 94 | /* #define PNG_DEPSTRUCT Access to this struct member is deprecated */ |
glennrp | 5b92734 | 2011-04-14 14:31:48 +0000 | [diff] [blame] | 95 | |
| 96 | /* PNG_PTR_NORETURN does not work on some platforms, in libpng-1.5.x */ |
cristy | 75cfe70 | 2011-04-14 14:27:17 +0000 | [diff] [blame] | 97 | #define PNG_PTR_NORETURN |
glennrp | 286a635 | 2009-11-09 02:58:50 +0000 | [diff] [blame] | 98 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 99 | #include "png.h" |
| 100 | #include "zlib.h" |
| 101 | |
| 102 | /* ImageMagick differences */ |
| 103 | #define first_scene scene |
| 104 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 105 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 106 | /* |
| 107 | Optional declarations. Define or undefine them as you like. |
| 108 | */ |
| 109 | /* #define PNG_DEBUG -- turning this on breaks VisualC compiling */ |
| 110 | |
| 111 | /* |
| 112 | Features under construction. Define these to work on them. |
| 113 | */ |
| 114 | #undef MNG_OBJECT_BUFFERS |
| 115 | #undef MNG_BASI_SUPPORTED |
| 116 | #define MNG_COALESCE_LAYERS /* In 5.4.4, this interfered with MMAP'ed files. */ |
| 117 | #define MNG_INSERT_LAYERS /* Troublesome, but seem to work as of 5.4.4 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 118 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 119 | # define JNG_SUPPORTED /* Not finished as of 5.5.2. See "To do" comments. */ |
| 120 | #endif |
| 121 | #if !defined(RGBColorMatchExact) |
| 122 | #define IsPNGColorEqual(color,target) \ |
| 123 | (((color).red == (target).red) && \ |
| 124 | ((color).green == (target).green) && \ |
| 125 | ((color).blue == (target).blue)) |
| 126 | #endif |
| 127 | |
| 128 | /* |
| 129 | Establish thread safety. |
| 130 | setjmp/longjmp is claimed to be safe on these platforms: |
| 131 | setjmp/longjmp is alleged to be unsafe on these platforms: |
| 132 | */ |
| 133 | #ifndef SETJMP_IS_THREAD_SAFE |
| 134 | #define PNG_SETJMP_NOT_THREAD_SAFE |
| 135 | #endif |
| 136 | |
| 137 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
| 138 | static SemaphoreInfo |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 139 | *ping_semaphore = (SemaphoreInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 140 | #endif |
| 141 | |
| 142 | /* |
| 143 | This temporary until I set up malloc'ed object attributes array. |
| 144 | Recompile with MNG_MAX_OBJECTS=65536L to avoid this limit but |
| 145 | waste more memory. |
| 146 | */ |
| 147 | #define MNG_MAX_OBJECTS 256 |
| 148 | |
| 149 | /* |
| 150 | If this not defined, spec is interpreted strictly. If it is |
| 151 | defined, an attempt will be made to recover from some errors, |
| 152 | including |
| 153 | o global PLTE too short |
| 154 | */ |
| 155 | #undef MNG_LOOSE |
| 156 | |
| 157 | /* |
| 158 | Don't try to define PNG_MNG_FEATURES_SUPPORTED here. Make sure |
| 159 | it's defined in libpng/pngconf.h, version 1.0.9 or later. It won't work |
| 160 | with earlier versions of libpng. From libpng-1.0.3a to libpng-1.0.8, |
| 161 | PNG_READ|WRITE_EMPTY_PLTE were used but those have been deprecated in |
| 162 | libpng in favor of PNG_MNG_FEATURES_SUPPORTED, so we set them here. |
| 163 | PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and |
| 164 | will be enabled by default in libpng-1.2.0. |
| 165 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 166 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
| 167 | # ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 168 | # define PNG_READ_EMPTY_PLTE_SUPPORTED |
| 169 | # endif |
| 170 | # ifndef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 171 | # define PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 172 | # endif |
| 173 | #endif |
| 174 | |
| 175 | /* |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 176 | Maximum valid size_t in PNG/MNG chunks is (2^31)-1 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | This macro is only defined in libpng-1.0.3 and later. |
| 178 | Previously it was PNG_MAX_UINT but that was deprecated in libpng-1.2.6 |
| 179 | */ |
| 180 | #ifndef PNG_UINT_31_MAX |
| 181 | #define PNG_UINT_31_MAX (png_uint_32) 0x7fffffffL |
| 182 | #endif |
| 183 | |
| 184 | /* |
| 185 | Constant strings for known chunk types. If you need to add a chunk, |
| 186 | add a string holding the name here. To make the code more |
| 187 | portable, we use ASCII numbers like this, not characters. |
| 188 | */ |
| 189 | |
| 190 | static png_byte FARDATA mng_MHDR[5]={ 77, 72, 68, 82, (png_byte) '\0'}; |
| 191 | static png_byte FARDATA mng_BACK[5]={ 66, 65, 67, 75, (png_byte) '\0'}; |
| 192 | static png_byte FARDATA mng_BASI[5]={ 66, 65, 83, 73, (png_byte) '\0'}; |
| 193 | static png_byte FARDATA mng_CLIP[5]={ 67, 76, 73, 80, (png_byte) '\0'}; |
| 194 | static png_byte FARDATA mng_CLON[5]={ 67, 76, 79, 78, (png_byte) '\0'}; |
| 195 | static png_byte FARDATA mng_DEFI[5]={ 68, 69, 70, 73, (png_byte) '\0'}; |
| 196 | static png_byte FARDATA mng_DHDR[5]={ 68, 72, 68, 82, (png_byte) '\0'}; |
| 197 | static png_byte FARDATA mng_DISC[5]={ 68, 73, 83, 67, (png_byte) '\0'}; |
| 198 | static png_byte FARDATA mng_ENDL[5]={ 69, 78, 68, 76, (png_byte) '\0'}; |
| 199 | static png_byte FARDATA mng_FRAM[5]={ 70, 82, 65, 77, (png_byte) '\0'}; |
| 200 | static png_byte FARDATA mng_IEND[5]={ 73, 69, 78, 68, (png_byte) '\0'}; |
| 201 | static png_byte FARDATA mng_IHDR[5]={ 73, 72, 68, 82, (png_byte) '\0'}; |
| 202 | static png_byte FARDATA mng_JHDR[5]={ 74, 72, 68, 82, (png_byte) '\0'}; |
| 203 | static png_byte FARDATA mng_LOOP[5]={ 76, 79, 79, 80, (png_byte) '\0'}; |
| 204 | static png_byte FARDATA mng_MAGN[5]={ 77, 65, 71, 78, (png_byte) '\0'}; |
| 205 | static png_byte FARDATA mng_MEND[5]={ 77, 69, 78, 68, (png_byte) '\0'}; |
| 206 | static png_byte FARDATA mng_MOVE[5]={ 77, 79, 86, 69, (png_byte) '\0'}; |
| 207 | static png_byte FARDATA mng_PAST[5]={ 80, 65, 83, 84, (png_byte) '\0'}; |
| 208 | static png_byte FARDATA mng_PLTE[5]={ 80, 76, 84, 69, (png_byte) '\0'}; |
| 209 | static png_byte FARDATA mng_SAVE[5]={ 83, 65, 86, 69, (png_byte) '\0'}; |
| 210 | static png_byte FARDATA mng_SEEK[5]={ 83, 69, 69, 75, (png_byte) '\0'}; |
| 211 | static png_byte FARDATA mng_SHOW[5]={ 83, 72, 79, 87, (png_byte) '\0'}; |
| 212 | static png_byte FARDATA mng_TERM[5]={ 84, 69, 82, 77, (png_byte) '\0'}; |
| 213 | static png_byte FARDATA mng_bKGD[5]={ 98, 75, 71, 68, (png_byte) '\0'}; |
| 214 | static png_byte FARDATA mng_cHRM[5]={ 99, 72, 82, 77, (png_byte) '\0'}; |
| 215 | static png_byte FARDATA mng_gAMA[5]={103, 65, 77, 65, (png_byte) '\0'}; |
| 216 | static png_byte FARDATA mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'}; |
| 217 | static png_byte FARDATA mng_nEED[5]={110, 69, 69, 68, (png_byte) '\0'}; |
| 218 | static png_byte FARDATA mng_pHYg[5]={112, 72, 89, 103, (png_byte) '\0'}; |
| 219 | static png_byte FARDATA mng_vpAg[5]={118, 112, 65, 103, (png_byte) '\0'}; |
| 220 | static png_byte FARDATA mng_pHYs[5]={112, 72, 89, 115, (png_byte) '\0'}; |
| 221 | static png_byte FARDATA mng_sBIT[5]={115, 66, 73, 84, (png_byte) '\0'}; |
| 222 | static png_byte FARDATA mng_sRGB[5]={115, 82, 71, 66, (png_byte) '\0'}; |
| 223 | static png_byte FARDATA mng_tRNS[5]={116, 82, 78, 83, (png_byte) '\0'}; |
| 224 | |
| 225 | #if defined(JNG_SUPPORTED) |
| 226 | static png_byte FARDATA mng_IDAT[5]={ 73, 68, 65, 84, (png_byte) '\0'}; |
| 227 | static png_byte FARDATA mng_JDAT[5]={ 74, 68, 65, 84, (png_byte) '\0'}; |
| 228 | static png_byte FARDATA mng_JDAA[5]={ 74, 68, 65, 65, (png_byte) '\0'}; |
| 229 | static png_byte FARDATA mng_JdAA[5]={ 74, 100, 65, 65, (png_byte) '\0'}; |
| 230 | static png_byte FARDATA mng_JSEP[5]={ 74, 83, 69, 80, (png_byte) '\0'}; |
| 231 | static png_byte FARDATA mng_oFFs[5]={111, 70, 70, 115, (png_byte) '\0'}; |
| 232 | #endif |
| 233 | |
| 234 | /* |
| 235 | Other known chunks that are not yet supported by ImageMagick: |
| 236 | static png_byte FARDATA mng_hIST[5]={104, 73, 83, 84, (png_byte) '\0'}; |
| 237 | static png_byte FARDATA mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'}; |
| 238 | static png_byte FARDATA mng_iTXt[5]={105, 84, 88, 116, (png_byte) '\0'}; |
| 239 | static png_byte FARDATA mng_sPLT[5]={115, 80, 76, 84, (png_byte) '\0'}; |
| 240 | static png_byte FARDATA mng_sTER[5]={115, 84, 69, 82, (png_byte) '\0'}; |
| 241 | static png_byte FARDATA mng_tEXt[5]={116, 69, 88, 116, (png_byte) '\0'}; |
| 242 | static png_byte FARDATA mng_tIME[5]={116, 73, 77, 69, (png_byte) '\0'}; |
| 243 | static png_byte FARDATA mng_zTXt[5]={122, 84, 88, 116, (png_byte) '\0'}; |
| 244 | */ |
| 245 | |
| 246 | typedef struct _MngBox |
| 247 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 248 | long |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 249 | left, |
| 250 | right, |
| 251 | top, |
| 252 | bottom; |
| 253 | } MngBox; |
| 254 | |
| 255 | typedef struct _MngPair |
| 256 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 257 | volatile long |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | a, |
| 259 | b; |
| 260 | } MngPair; |
| 261 | |
| 262 | #ifdef MNG_OBJECT_BUFFERS |
| 263 | typedef struct _MngBuffer |
| 264 | { |
| 265 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 266 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 267 | height, |
| 268 | width; |
| 269 | |
| 270 | Image |
| 271 | *image; |
| 272 | |
| 273 | png_color |
| 274 | plte[256]; |
| 275 | |
| 276 | int |
| 277 | reference_count; |
| 278 | |
| 279 | unsigned char |
| 280 | alpha_sample_depth, |
| 281 | compression_method, |
| 282 | color_type, |
| 283 | concrete, |
| 284 | filter_method, |
| 285 | frozen, |
| 286 | image_type, |
| 287 | interlace_method, |
| 288 | pixel_sample_depth, |
| 289 | plte_length, |
| 290 | sample_depth, |
| 291 | viewable; |
| 292 | } MngBuffer; |
| 293 | #endif |
| 294 | |
| 295 | typedef struct _MngInfo |
| 296 | { |
| 297 | |
| 298 | #ifdef MNG_OBJECT_BUFFERS |
| 299 | MngBuffer |
| 300 | *ob[MNG_MAX_OBJECTS]; |
| 301 | #endif |
| 302 | |
| 303 | Image * |
| 304 | image; |
| 305 | |
| 306 | RectangleInfo |
| 307 | page; |
| 308 | |
| 309 | int |
| 310 | adjoin, |
| 311 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 312 | bytes_in_read_buffer, |
| 313 | found_empty_plte, |
| 314 | #endif |
| 315 | equal_backgrounds, |
| 316 | equal_chrms, |
| 317 | equal_gammas, |
| 318 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 319 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 320 | equal_palettes, |
| 321 | #endif |
| 322 | equal_physs, |
| 323 | equal_srgbs, |
| 324 | framing_mode, |
| 325 | have_global_bkgd, |
| 326 | have_global_chrm, |
| 327 | have_global_gama, |
| 328 | have_global_phys, |
| 329 | have_global_sbit, |
| 330 | have_global_srgb, |
| 331 | have_saved_bkgd_index, |
| 332 | have_write_global_chrm, |
| 333 | have_write_global_gama, |
| 334 | have_write_global_plte, |
| 335 | have_write_global_srgb, |
| 336 | need_fram, |
| 337 | object_id, |
| 338 | old_framing_mode, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 339 | saved_bkgd_index; |
| 340 | |
| 341 | int |
| 342 | new_number_colors; |
| 343 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 344 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 345 | image_found, |
| 346 | loop_count[256], |
| 347 | loop_iteration[256], |
| 348 | scenes_found, |
| 349 | x_off[MNG_MAX_OBJECTS], |
| 350 | y_off[MNG_MAX_OBJECTS]; |
| 351 | |
| 352 | MngBox |
| 353 | clip, |
| 354 | frame, |
| 355 | image_box, |
| 356 | object_clip[MNG_MAX_OBJECTS]; |
| 357 | |
| 358 | unsigned char |
| 359 | /* These flags could be combined into one byte */ |
| 360 | exists[MNG_MAX_OBJECTS], |
| 361 | frozen[MNG_MAX_OBJECTS], |
| 362 | loop_active[256], |
| 363 | invisible[MNG_MAX_OBJECTS], |
| 364 | viewable[MNG_MAX_OBJECTS]; |
| 365 | |
| 366 | MagickOffsetType |
| 367 | loop_jump[256]; |
| 368 | |
| 369 | png_colorp |
| 370 | global_plte; |
| 371 | |
| 372 | png_color_8 |
| 373 | global_sbit; |
| 374 | |
| 375 | png_byte |
| 376 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 377 | read_buffer[8], |
| 378 | #endif |
| 379 | global_trns[256]; |
| 380 | |
| 381 | float |
| 382 | global_gamma; |
| 383 | |
| 384 | ChromaticityInfo |
| 385 | global_chrm; |
| 386 | |
| 387 | RenderingIntent |
| 388 | global_srgb_intent; |
| 389 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 390 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 391 | delay, |
| 392 | global_plte_length, |
| 393 | global_trns_length, |
| 394 | global_x_pixels_per_unit, |
| 395 | global_y_pixels_per_unit, |
| 396 | mng_width, |
| 397 | mng_height, |
| 398 | ticks_per_second; |
| 399 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 400 | MagickBooleanType |
| 401 | need_blob; |
| 402 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 403 | unsigned int |
| 404 | IsPalette, |
| 405 | global_phys_unit_type, |
| 406 | basi_warning, |
| 407 | clon_warning, |
| 408 | dhdr_warning, |
| 409 | jhdr_warning, |
| 410 | magn_warning, |
| 411 | past_warning, |
| 412 | phyg_warning, |
| 413 | phys_warning, |
| 414 | sbit_warning, |
| 415 | show_warning, |
| 416 | mng_type, |
| 417 | write_mng, |
| 418 | write_png_colortype, |
| 419 | write_png_depth, |
| 420 | write_png8, |
| 421 | write_png24, |
| 422 | write_png32; |
| 423 | |
| 424 | #ifdef MNG_BASI_SUPPORTED |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 425 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 426 | basi_width, |
| 427 | basi_height; |
| 428 | |
| 429 | unsigned int |
| 430 | basi_depth, |
| 431 | basi_color_type, |
| 432 | basi_compression_method, |
| 433 | basi_filter_type, |
| 434 | basi_interlace_method, |
| 435 | basi_red, |
| 436 | basi_green, |
| 437 | basi_blue, |
| 438 | basi_alpha, |
| 439 | basi_viewable; |
| 440 | #endif |
| 441 | |
| 442 | png_uint_16 |
| 443 | magn_first, |
| 444 | magn_last, |
| 445 | magn_mb, |
| 446 | magn_ml, |
| 447 | magn_mr, |
| 448 | magn_mt, |
| 449 | magn_mx, |
| 450 | magn_my, |
| 451 | magn_methx, |
| 452 | magn_methy; |
| 453 | |
| 454 | PixelPacket |
| 455 | mng_global_bkgd; |
| 456 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 457 | /* Added at version 6.6.6-7 */ |
| 458 | MagickBooleanType |
| 459 | ping_exclude_bKGD, |
| 460 | ping_exclude_cHRM, |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 461 | ping_exclude_date, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 462 | ping_exclude_EXIF, |
| 463 | ping_exclude_gAMA, |
| 464 | ping_exclude_iCCP, |
| 465 | /* ping_exclude_iTXt, */ |
| 466 | ping_exclude_oFFs, |
| 467 | ping_exclude_pHYs, |
| 468 | ping_exclude_sRGB, |
| 469 | ping_exclude_tEXt, |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 470 | ping_exclude_tRNS, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 471 | ping_exclude_vpAg, |
| 472 | ping_exclude_zCCP, /* hex-encoded iCCP */ |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 473 | ping_exclude_zTXt, |
| 474 | ping_preserve_colormap; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 475 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 476 | } MngInfo; |
| 477 | #endif /* VER */ |
| 478 | |
| 479 | /* |
| 480 | Forward declarations. |
| 481 | */ |
| 482 | static MagickBooleanType |
| 483 | WritePNGImage(const ImageInfo *,Image *); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 484 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 485 | static MagickBooleanType |
| 486 | WriteMNGImage(const ImageInfo *,Image *); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 487 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 488 | #if defined(JNG_SUPPORTED) |
| 489 | static MagickBooleanType |
| 490 | WriteJNGImage(const ImageInfo *,Image *); |
| 491 | #endif |
| 492 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 493 | #if PNG_LIBPNG_VER > 10011 |
| 494 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 495 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 496 | #if (MAGICKCORE_QUANTUM_DEPTH >= 16) |
| 497 | static MagickBooleanType |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 498 | LosslessReduceDepthOK(Image *image) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 499 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 500 | /* Reduce bit depth if it can be reduced losslessly from 16+ to 8. |
| 501 | * |
| 502 | * This is true if the high byte and the next highest byte of |
| 503 | * each sample of the image, the colormap, and the background color |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 504 | * are equal to each other. We check this by seeing if the samples |
| 505 | * are unchanged when we scale them down to 8 and back up to Quantum. |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 506 | * |
| 507 | * We don't use the method GetImageDepth() because it doesn't check |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 508 | * background and doesn't handle PseudoClass specially. |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 509 | */ |
| 510 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 511 | #define QuantumToCharToQuantumEqQuantum(quantum) \ |
| 512 | ((ScaleCharToQuantum((unsigned char) ScaleQuantumToChar(quantum))) == quantum) |
| 513 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 514 | MagickBooleanType |
| 515 | ok_to_reduce=MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 516 | |
glennrp | 03e11f6 | 2011-04-22 13:30:16 +0000 | [diff] [blame] | 517 | if (image->depth >= 16) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 518 | { |
| 519 | |
| 520 | const PixelPacket |
| 521 | *p; |
| 522 | |
| 523 | ok_to_reduce= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 524 | QuantumToCharToQuantumEqQuantum(image->background_color.red) && |
| 525 | QuantumToCharToQuantumEqQuantum(image->background_color.green) && |
| 526 | QuantumToCharToQuantumEqQuantum(image->background_color.blue) ? |
| 527 | MagickTrue : MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 528 | |
| 529 | if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass) |
| 530 | { |
| 531 | int indx; |
| 532 | |
| 533 | for (indx=0; indx < (ssize_t) image->colors; indx++) |
| 534 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 535 | ok_to_reduce=( |
| 536 | QuantumToCharToQuantumEqQuantum( |
| 537 | image->colormap[indx].red) && |
| 538 | QuantumToCharToQuantumEqQuantum( |
| 539 | image->colormap[indx].green) && |
| 540 | QuantumToCharToQuantumEqQuantum( |
| 541 | image->colormap[indx].blue)) ? |
| 542 | MagickTrue : MagickFalse; |
| 543 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 544 | if (ok_to_reduce == MagickFalse) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 545 | break; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | if ((ok_to_reduce != MagickFalse) && |
| 550 | (image->storage_class != PseudoClass)) |
| 551 | { |
| 552 | ssize_t |
| 553 | y; |
| 554 | |
| 555 | register ssize_t |
| 556 | x; |
| 557 | |
| 558 | for (y=0; y < (ssize_t) image->rows; y++) |
| 559 | { |
| 560 | p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); |
| 561 | |
| 562 | if (p == (const PixelPacket *) NULL) |
| 563 | { |
| 564 | ok_to_reduce = MagickFalse; |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 569 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 570 | ok_to_reduce= |
| 571 | QuantumToCharToQuantumEqQuantum(GetRedPixelComponent(p)) && |
| 572 | QuantumToCharToQuantumEqQuantum(GetGreenPixelComponent(p)) && |
| 573 | QuantumToCharToQuantumEqQuantum(GetBluePixelComponent(p)) ? |
| 574 | MagickTrue : MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 575 | |
| 576 | if (ok_to_reduce == MagickFalse) |
| 577 | break; |
| 578 | |
| 579 | p++; |
| 580 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 581 | if (x >= 0) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (ok_to_reduce != MagickFalse) |
| 587 | { |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 588 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 589 | " OK to reduce PNG bit depth to 8 without loss of info"); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 590 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 591 | else |
| 592 | { |
| 593 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 594 | " Not OK to reduce PNG bit depth to 8 without loss of info"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 595 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | return ok_to_reduce; |
| 599 | } |
| 600 | #endif /* MAGICKCORE_QUANTUM_DEPTH >= 16 */ |
| 601 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 602 | static int |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 603 | Magick_RenderingIntent_to_PNG_RenderingIntent(const RenderingIntent intent) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 604 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 605 | switch (intent) |
| 606 | { |
| 607 | case PerceptualIntent: |
| 608 | return 0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 609 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 610 | case RelativeIntent: |
| 611 | return 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 612 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 613 | case SaturationIntent: |
| 614 | return 2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 615 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 616 | case AbsoluteIntent: |
| 617 | return 3; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 618 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 619 | default: |
| 620 | return -1; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | static RenderingIntent |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 625 | Magick_RenderingIntent_from_PNG_RenderingIntent(const int ping_intent) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 626 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 627 | switch (ping_intent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 628 | { |
| 629 | case 0: |
| 630 | return PerceptualIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 631 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 632 | case 1: |
| 633 | return RelativeIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 634 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 635 | case 2: |
| 636 | return SaturationIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 637 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 638 | case 3: |
| 639 | return AbsoluteIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 640 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 641 | default: |
| 642 | return UndefinedIntent; |
| 643 | } |
| 644 | } |
| 645 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 646 | static inline ssize_t MagickMax(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 647 | { |
| 648 | if (x > y) |
| 649 | return(x); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 650 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 651 | return(y); |
| 652 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 653 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 654 | static inline ssize_t MagickMin(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 655 | { |
| 656 | if (x < y) |
| 657 | return(x); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 658 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 659 | return(y); |
| 660 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 661 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 662 | |
| 663 | /* |
| 664 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 665 | % % |
| 666 | % % |
| 667 | % % |
| 668 | % I m a g e I s G r a y % |
| 669 | % % |
| 670 | % % |
| 671 | % % |
| 672 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 673 | % % |
| 674 | % Like IsGrayImage except does not change DirectClass to PseudoClass % |
| 675 | % % |
| 676 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 677 | */ |
| 678 | static MagickBooleanType ImageIsGray(Image *image) |
| 679 | { |
| 680 | register const PixelPacket |
| 681 | *p; |
| 682 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 683 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 684 | i, |
| 685 | x, |
| 686 | y; |
| 687 | |
| 688 | assert(image != (Image *) NULL); |
| 689 | assert(image->signature == MagickSignature); |
| 690 | if (image->debug != MagickFalse) |
| 691 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 692 | |
| 693 | if (image->storage_class == PseudoClass) |
| 694 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 695 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 696 | if (IsGray(image->colormap+i) == MagickFalse) |
| 697 | return(MagickFalse); |
| 698 | return(MagickTrue); |
| 699 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 700 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 701 | { |
| 702 | p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); |
| 703 | if (p == (const PixelPacket *) NULL) |
| 704 | return(MagickFalse); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 705 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 706 | { |
| 707 | if (IsGray(p) == MagickFalse) |
| 708 | return(MagickFalse); |
| 709 | p++; |
| 710 | } |
| 711 | } |
| 712 | return(MagickTrue); |
| 713 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 714 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 715 | #endif /* MAGICKCORE_PNG_DELEGATE */ |
| 716 | |
| 717 | /* |
| 718 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 719 | % % |
| 720 | % % |
| 721 | % % |
| 722 | % I s M N G % |
| 723 | % % |
| 724 | % % |
| 725 | % % |
| 726 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 727 | % |
| 728 | % IsMNG() returns MagickTrue if the image format type, identified by the |
| 729 | % magick string, is MNG. |
| 730 | % |
| 731 | % The format of the IsMNG method is: |
| 732 | % |
| 733 | % MagickBooleanType IsMNG(const unsigned char *magick,const size_t length) |
| 734 | % |
| 735 | % A description of each parameter follows: |
| 736 | % |
| 737 | % o magick: compare image format pattern against these bytes. |
| 738 | % |
| 739 | % o length: Specifies the length of the magick string. |
| 740 | % |
| 741 | % |
| 742 | */ |
| 743 | static MagickBooleanType IsMNG(const unsigned char *magick,const size_t length) |
| 744 | { |
| 745 | if (length < 8) |
| 746 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 747 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 748 | if (memcmp(magick,"\212MNG\r\n\032\n",8) == 0) |
| 749 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 750 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 751 | return(MagickFalse); |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 756 | % % |
| 757 | % % |
| 758 | % % |
| 759 | % I s J N G % |
| 760 | % % |
| 761 | % % |
| 762 | % % |
| 763 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 764 | % |
| 765 | % IsJNG() returns MagickTrue if the image format type, identified by the |
| 766 | % magick string, is JNG. |
| 767 | % |
| 768 | % The format of the IsJNG method is: |
| 769 | % |
| 770 | % MagickBooleanType IsJNG(const unsigned char *magick,const size_t length) |
| 771 | % |
| 772 | % A description of each parameter follows: |
| 773 | % |
| 774 | % o magick: compare image format pattern against these bytes. |
| 775 | % |
| 776 | % o length: Specifies the length of the magick string. |
| 777 | % |
| 778 | % |
| 779 | */ |
| 780 | static MagickBooleanType IsJNG(const unsigned char *magick,const size_t length) |
| 781 | { |
| 782 | if (length < 8) |
| 783 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 784 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 785 | if (memcmp(magick,"\213JNG\r\n\032\n",8) == 0) |
| 786 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 787 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 788 | return(MagickFalse); |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 793 | % % |
| 794 | % % |
| 795 | % % |
| 796 | % I s P N G % |
| 797 | % % |
| 798 | % % |
| 799 | % % |
| 800 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 801 | % |
| 802 | % IsPNG() returns MagickTrue if the image format type, identified by the |
| 803 | % magick string, is PNG. |
| 804 | % |
| 805 | % The format of the IsPNG method is: |
| 806 | % |
| 807 | % MagickBooleanType IsPNG(const unsigned char *magick,const size_t length) |
| 808 | % |
| 809 | % A description of each parameter follows: |
| 810 | % |
| 811 | % o magick: compare image format pattern against these bytes. |
| 812 | % |
| 813 | % o length: Specifies the length of the magick string. |
| 814 | % |
| 815 | */ |
| 816 | static MagickBooleanType IsPNG(const unsigned char *magick,const size_t length) |
| 817 | { |
| 818 | if (length < 8) |
| 819 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 820 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 821 | if (memcmp(magick,"\211PNG\r\n\032\n",8) == 0) |
| 822 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 823 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | return(MagickFalse); |
| 825 | } |
| 826 | |
| 827 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 828 | #if defined(__cplusplus) || defined(c_plusplus) |
| 829 | extern "C" { |
| 830 | #endif |
| 831 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 832 | #if (PNG_LIBPNG_VER > 10011) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 833 | static size_t WriteBlobMSBULong(Image *image,const size_t value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 834 | { |
| 835 | unsigned char |
| 836 | buffer[4]; |
| 837 | |
| 838 | assert(image != (Image *) NULL); |
| 839 | assert(image->signature == MagickSignature); |
| 840 | buffer[0]=(unsigned char) (value >> 24); |
| 841 | buffer[1]=(unsigned char) (value >> 16); |
| 842 | buffer[2]=(unsigned char) (value >> 8); |
| 843 | buffer[3]=(unsigned char) value; |
| 844 | return((size_t) WriteBlob(image,4,buffer)); |
| 845 | } |
| 846 | |
| 847 | static void PNGLong(png_bytep p,png_uint_32 value) |
| 848 | { |
| 849 | *p++=(png_byte) ((value >> 24) & 0xff); |
| 850 | *p++=(png_byte) ((value >> 16) & 0xff); |
| 851 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 852 | *p++=(png_byte) (value & 0xff); |
| 853 | } |
| 854 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 855 | #if defined(JNG_SUPPORTED) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 856 | static void PNGsLong(png_bytep p,png_int_32 value) |
| 857 | { |
| 858 | *p++=(png_byte) ((value >> 24) & 0xff); |
| 859 | *p++=(png_byte) ((value >> 16) & 0xff); |
| 860 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 861 | *p++=(png_byte) (value & 0xff); |
| 862 | } |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 863 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 864 | |
| 865 | static void PNGShort(png_bytep p,png_uint_16 value) |
| 866 | { |
| 867 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 868 | *p++=(png_byte) (value & 0xff); |
| 869 | } |
| 870 | |
| 871 | static void PNGType(png_bytep p,png_bytep type) |
| 872 | { |
| 873 | (void) CopyMagickMemory(p,type,4*sizeof(png_byte)); |
| 874 | } |
| 875 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 876 | static void LogPNGChunk(MagickBooleanType logging, png_bytep type, |
| 877 | size_t length) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 878 | { |
| 879 | if (logging != MagickFalse) |
| 880 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 881 | " Writing %c%c%c%c chunk, length: %.20g", |
| 882 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 883 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 884 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 885 | |
| 886 | #if defined(__cplusplus) || defined(c_plusplus) |
| 887 | } |
| 888 | #endif |
| 889 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 890 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 891 | /* |
| 892 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 893 | % % |
| 894 | % % |
| 895 | % % |
| 896 | % R e a d P N G I m a g e % |
| 897 | % % |
| 898 | % % |
| 899 | % % |
| 900 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 901 | % |
| 902 | % ReadPNGImage() reads a Portable Network Graphics (PNG) or |
| 903 | % Multiple-image Network Graphics (MNG) image file and returns it. It |
| 904 | % allocates the memory necessary for the new Image structure and returns a |
| 905 | % pointer to the new image or set of images. |
| 906 | % |
| 907 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 908 | % |
| 909 | % The format of the ReadPNGImage method is: |
| 910 | % |
| 911 | % Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 912 | % |
| 913 | % A description of each parameter follows: |
| 914 | % |
| 915 | % o image_info: the image info. |
| 916 | % |
| 917 | % o exception: return any errors or warnings in this structure. |
| 918 | % |
| 919 | % To do, more or less in chronological order (as of version 5.5.2, |
| 920 | % November 26, 2002 -- glennrp -- see also "To do" under WriteMNGImage): |
| 921 | % |
| 922 | % Get 16-bit cheap transparency working. |
| 923 | % |
| 924 | % (At this point, PNG decoding is supposed to be in full MNG-LC compliance) |
| 925 | % |
| 926 | % Preserve all unknown and not-yet-handled known chunks found in input |
| 927 | % PNG file and copy them into output PNG files according to the PNG |
| 928 | % copying rules. |
| 929 | % |
| 930 | % (At this point, PNG encoding should be in full MNG compliance) |
| 931 | % |
| 932 | % Provide options for choice of background to use when the MNG BACK |
| 933 | % chunk is not present or is not mandatory (i.e., leave transparent, |
| 934 | % user specified, MNG BACK, PNG bKGD) |
| 935 | % |
| 936 | % Implement LOOP/ENDL [done, but could do discretionary loops more |
| 937 | % efficiently by linking in the duplicate frames.]. |
| 938 | % |
| 939 | % Decode and act on the MHDR simplicity profile (offer option to reject |
| 940 | % files or attempt to process them anyway when the profile isn't LC or VLC). |
| 941 | % |
| 942 | % Upgrade to full MNG without Delta-PNG. |
| 943 | % |
| 944 | % o BACK [done a while ago except for background image ID] |
| 945 | % o MOVE [done 15 May 1999] |
| 946 | % o CLIP [done 15 May 1999] |
| 947 | % o DISC [done 19 May 1999] |
| 948 | % o SAVE [partially done 19 May 1999 (marks objects frozen)] |
| 949 | % o SEEK [partially done 19 May 1999 (discard function only)] |
| 950 | % o SHOW |
| 951 | % o PAST |
| 952 | % o BASI |
| 953 | % o MNG-level tEXt/iTXt/zTXt |
| 954 | % o pHYg |
| 955 | % o pHYs |
| 956 | % o sBIT |
| 957 | % o bKGD |
| 958 | % o iTXt (wait for libpng implementation). |
| 959 | % |
| 960 | % Use the scene signature to discover when an identical scene is |
| 961 | % being reused, and just point to the original image->exception instead |
| 962 | % of storing another set of pixels. This not specific to MNG |
| 963 | % but could be applied generally. |
| 964 | % |
| 965 | % Upgrade to full MNG with Delta-PNG. |
| 966 | % |
| 967 | % JNG tEXt/iTXt/zTXt |
| 968 | % |
| 969 | % We will not attempt to read files containing the CgBI chunk. |
| 970 | % They are really Xcode files meant for display on the iPhone. |
| 971 | % These are not valid PNG files and it is impossible to recover |
| 972 | % the orginal PNG from files that have been converted to Xcode-PNG, |
| 973 | % since irretrievable loss of color data has occurred due to the |
| 974 | % use of premultiplied alpha. |
| 975 | */ |
| 976 | |
| 977 | #if defined(__cplusplus) || defined(c_plusplus) |
| 978 | extern "C" { |
| 979 | #endif |
| 980 | |
| 981 | /* |
| 982 | This the function that does the actual reading of data. It is |
| 983 | the same as the one supplied in libpng, except that it receives the |
| 984 | datastream from the ReadBlob() function instead of standard input. |
| 985 | */ |
| 986 | static void png_get_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 987 | { |
| 988 | Image |
| 989 | *image; |
| 990 | |
| 991 | image=(Image *) png_get_io_ptr(png_ptr); |
| 992 | if (length) |
| 993 | { |
| 994 | png_size_t |
| 995 | check; |
| 996 | |
| 997 | check=(png_size_t) ReadBlob(image,(size_t) length,data); |
| 998 | if (check != length) |
| 999 | { |
| 1000 | char |
| 1001 | msg[MaxTextExtent]; |
| 1002 | |
| 1003 | (void) FormatMagickString(msg,MaxTextExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1004 | "Expected %.20g bytes; found %.20g bytes",(double) length, |
| 1005 | (double) check); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1006 | png_warning(png_ptr,msg); |
| 1007 | png_error(png_ptr,"Read Exception"); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | #if !defined(PNG_READ_EMPTY_PLTE_SUPPORTED) && \ |
| 1013 | !defined(PNG_MNG_FEATURES_SUPPORTED) |
| 1014 | /* We use mng_get_data() instead of png_get_data() if we have a libpng |
| 1015 | * older than libpng-1.0.3a, which was the first to allow the empty |
| 1016 | * PLTE, or a newer libpng in which PNG_MNG_FEATURES_SUPPORTED was |
| 1017 | * ifdef'ed out. Earlier versions would crash if the bKGD chunk was |
| 1018 | * encountered after an empty PLTE, so we have to look ahead for bKGD |
| 1019 | * chunks and remove them from the datastream that is passed to libpng, |
| 1020 | * and store their contents for later use. |
| 1021 | */ |
| 1022 | static void mng_get_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1023 | { |
| 1024 | MngInfo |
| 1025 | *mng_info; |
| 1026 | |
| 1027 | Image |
| 1028 | *image; |
| 1029 | |
| 1030 | png_size_t |
| 1031 | check; |
| 1032 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1033 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1034 | i; |
| 1035 | |
| 1036 | i=0; |
| 1037 | mng_info=(MngInfo *) png_get_io_ptr(png_ptr); |
| 1038 | image=(Image *) mng_info->image; |
| 1039 | while (mng_info->bytes_in_read_buffer && length) |
| 1040 | { |
| 1041 | data[i]=mng_info->read_buffer[i]; |
| 1042 | mng_info->bytes_in_read_buffer--; |
| 1043 | length--; |
| 1044 | i++; |
| 1045 | } |
| 1046 | if (length) |
| 1047 | { |
| 1048 | check=(png_size_t) ReadBlob(image,(size_t) length,(char *) data); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1049 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1050 | if (check != length) |
| 1051 | png_error(png_ptr,"Read Exception"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1052 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1053 | if (length == 4) |
| 1054 | { |
| 1055 | if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) && |
| 1056 | (data[3] == 0)) |
| 1057 | { |
| 1058 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1059 | (char *) mng_info->read_buffer); |
| 1060 | mng_info->read_buffer[4]=0; |
| 1061 | mng_info->bytes_in_read_buffer=4; |
| 1062 | if (memcmp(mng_info->read_buffer,mng_PLTE,4) == 0) |
| 1063 | mng_info->found_empty_plte=MagickTrue; |
| 1064 | if (memcmp(mng_info->read_buffer,mng_IEND,4) == 0) |
| 1065 | { |
| 1066 | mng_info->found_empty_plte=MagickFalse; |
| 1067 | mng_info->have_saved_bkgd_index=MagickFalse; |
| 1068 | } |
| 1069 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1070 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1071 | if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) && |
| 1072 | (data[3] == 1)) |
| 1073 | { |
| 1074 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1075 | (char *) mng_info->read_buffer); |
| 1076 | mng_info->read_buffer[4]=0; |
| 1077 | mng_info->bytes_in_read_buffer=4; |
| 1078 | if (memcmp(mng_info->read_buffer,mng_bKGD,4) == 0) |
| 1079 | if (mng_info->found_empty_plte) |
| 1080 | { |
| 1081 | /* |
| 1082 | Skip the bKGD data byte and CRC. |
| 1083 | */ |
| 1084 | check=(png_size_t) |
| 1085 | ReadBlob(image,5,(char *) mng_info->read_buffer); |
| 1086 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1087 | (char *) mng_info->read_buffer); |
| 1088 | mng_info->saved_bkgd_index=mng_info->read_buffer[0]; |
| 1089 | mng_info->have_saved_bkgd_index=MagickTrue; |
| 1090 | mng_info->bytes_in_read_buffer=0; |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | #endif |
| 1097 | |
| 1098 | static void png_put_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1099 | { |
| 1100 | Image |
| 1101 | *image; |
| 1102 | |
| 1103 | image=(Image *) png_get_io_ptr(png_ptr); |
| 1104 | if (length) |
| 1105 | { |
| 1106 | png_size_t |
| 1107 | check; |
| 1108 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1109 | check=(png_size_t) WriteBlob(image,(size_t) length,data); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1110 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1111 | if (check != length) |
| 1112 | png_error(png_ptr,"WriteBlob Failed"); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | static void png_flush_data(png_structp png_ptr) |
| 1117 | { |
| 1118 | (void) png_ptr; |
| 1119 | } |
| 1120 | |
| 1121 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 1122 | static int PalettesAreEqual(Image *a,Image *b) |
| 1123 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1124 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1125 | i; |
| 1126 | |
| 1127 | if ((a == (Image *) NULL) || (b == (Image *) NULL)) |
| 1128 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1129 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1130 | if (a->storage_class != PseudoClass || b->storage_class != PseudoClass) |
| 1131 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1132 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1133 | if (a->colors != b->colors) |
| 1134 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1135 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1136 | for (i=0; i < (ssize_t) a->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1137 | { |
| 1138 | if ((a->colormap[i].red != b->colormap[i].red) || |
| 1139 | (a->colormap[i].green != b->colormap[i].green) || |
| 1140 | (a->colormap[i].blue != b->colormap[i].blue)) |
| 1141 | return((int) MagickFalse); |
| 1142 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1143 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1144 | return((int) MagickTrue); |
| 1145 | } |
| 1146 | #endif |
| 1147 | |
| 1148 | static void MngInfoDiscardObject(MngInfo *mng_info,int i) |
| 1149 | { |
| 1150 | if (i && (i < MNG_MAX_OBJECTS) && (mng_info != (MngInfo *) NULL) && |
| 1151 | mng_info->exists[i] && !mng_info->frozen[i]) |
| 1152 | { |
| 1153 | #ifdef MNG_OBJECT_BUFFERS |
| 1154 | if (mng_info->ob[i] != (MngBuffer *) NULL) |
| 1155 | { |
| 1156 | if (mng_info->ob[i]->reference_count > 0) |
| 1157 | mng_info->ob[i]->reference_count--; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1158 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1159 | if (mng_info->ob[i]->reference_count == 0) |
| 1160 | { |
| 1161 | if (mng_info->ob[i]->image != (Image *) NULL) |
| 1162 | mng_info->ob[i]->image=DestroyImage(mng_info->ob[i]->image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1163 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1164 | mng_info->ob[i]=DestroyString(mng_info->ob[i]); |
| 1165 | } |
| 1166 | } |
| 1167 | mng_info->ob[i]=(MngBuffer *) NULL; |
| 1168 | #endif |
| 1169 | mng_info->exists[i]=MagickFalse; |
| 1170 | mng_info->invisible[i]=MagickFalse; |
| 1171 | mng_info->viewable[i]=MagickFalse; |
| 1172 | mng_info->frozen[i]=MagickFalse; |
| 1173 | mng_info->x_off[i]=0; |
| 1174 | mng_info->y_off[i]=0; |
| 1175 | mng_info->object_clip[i].left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1176 | mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1177 | mng_info->object_clip[i].top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1178 | mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 1182 | static void MngInfoFreeStruct(MngInfo *mng_info, |
| 1183 | MagickBooleanType *have_mng_structure) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1184 | { |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 1185 | if (*have_mng_structure != MagickFalse && (mng_info != (MngInfo *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1186 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1187 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1188 | i; |
| 1189 | |
| 1190 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 1191 | MngInfoDiscardObject(mng_info,i); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1192 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1193 | if (mng_info->global_plte != (png_colorp) NULL) |
| 1194 | mng_info->global_plte=(png_colorp) |
| 1195 | RelinquishMagickMemory(mng_info->global_plte); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1196 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1197 | mng_info=(MngInfo *) RelinquishMagickMemory(mng_info); |
| 1198 | *have_mng_structure=MagickFalse; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | static MngBox mng_minimum_box(MngBox box1,MngBox box2) |
| 1203 | { |
| 1204 | MngBox |
| 1205 | box; |
| 1206 | |
| 1207 | box=box1; |
| 1208 | if (box.left < box2.left) |
| 1209 | box.left=box2.left; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1210 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1211 | if (box.top < box2.top) |
| 1212 | box.top=box2.top; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1213 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1214 | if (box.right > box2.right) |
| 1215 | box.right=box2.right; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1216 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1217 | if (box.bottom > box2.bottom) |
| 1218 | box.bottom=box2.bottom; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1220 | return box; |
| 1221 | } |
| 1222 | |
| 1223 | static MngBox mng_read_box(MngBox previous_box,char delta_type,unsigned char *p) |
| 1224 | { |
| 1225 | MngBox |
| 1226 | box; |
| 1227 | |
| 1228 | /* |
| 1229 | Read clipping boundaries from DEFI, CLIP, FRAM, or PAST chunk. |
| 1230 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1231 | box.left=(ssize_t) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 1232 | box.right=(ssize_t) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]); |
| 1233 | box.top=(ssize_t) ((p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11]); |
| 1234 | box.bottom=(ssize_t) ((p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1235 | if (delta_type != 0) |
| 1236 | { |
| 1237 | box.left+=previous_box.left; |
| 1238 | box.right+=previous_box.right; |
| 1239 | box.top+=previous_box.top; |
| 1240 | box.bottom+=previous_box.bottom; |
| 1241 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1242 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1243 | return(box); |
| 1244 | } |
| 1245 | |
| 1246 | static MngPair mng_read_pair(MngPair previous_pair,int delta_type, |
| 1247 | unsigned char *p) |
| 1248 | { |
| 1249 | MngPair |
| 1250 | pair; |
| 1251 | /* |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1252 | Read two ssize_ts from CLON, MOVE or PAST chunk |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1253 | */ |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1254 | pair.a=(long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 1255 | pair.b=(long) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1256 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1257 | if (delta_type != 0) |
| 1258 | { |
| 1259 | pair.a+=previous_pair.a; |
| 1260 | pair.b+=previous_pair.b; |
| 1261 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1262 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1263 | return(pair); |
| 1264 | } |
| 1265 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1266 | static long mng_get_long(unsigned char *p) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1267 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1268 | return((long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1271 | static void MagickPNGErrorHandler(png_struct *ping,png_const_charp message) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1272 | { |
| 1273 | Image |
| 1274 | *image; |
| 1275 | |
| 1276 | image=(Image *) png_get_error_ptr(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1277 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1278 | if (image->debug != MagickFalse) |
| 1279 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1280 | " libpng-%s error: %s", PNG_LIBPNG_VER_STRING,message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1281 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1282 | (void) ThrowMagickException(&image->exception,GetMagickModule(),CoderError, |
| 1283 | message,"`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1284 | |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 1285 | #if (PNG_LIBPNG_VER < 10500) |
glennrp | 8371ecc | 2011-02-19 19:15:38 +0000 | [diff] [blame] | 1286 | /* A warning about deprecated use of jmpbuf here is unavoidable if you |
| 1287 | * are building with libpng-1.4.x and can be ignored. |
| 1288 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1289 | longjmp(ping->jmpbuf,1); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1290 | #else |
| 1291 | png_longjmp(ping,1); |
| 1292 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1295 | static void MagickPNGWarningHandler(png_struct *ping,png_const_charp message) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1296 | { |
| 1297 | Image |
| 1298 | *image; |
| 1299 | |
| 1300 | if (LocaleCompare(message, "Missing PLTE before tRNS") == 0) |
| 1301 | png_error(ping, message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1302 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1303 | image=(Image *) png_get_error_ptr(ping); |
| 1304 | if (image->debug != MagickFalse) |
| 1305 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | cc23b9a | 2010-05-09 22:37:43 +0000 | [diff] [blame] | 1306 | " libpng-%s warning: %s", PNG_LIBPNG_VER_STRING,message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1307 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1308 | (void) ThrowMagickException(&image->exception,GetMagickModule(),CoderWarning, |
| 1309 | message,"`%s'",image->filename); |
| 1310 | } |
| 1311 | |
| 1312 | #ifdef PNG_USER_MEM_SUPPORTED |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1313 | static png_voidp Magick_png_malloc(png_structp png_ptr,png_uint_32 size) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1314 | { |
| 1315 | #if (PNG_LIBPNG_VER < 10011) |
| 1316 | png_voidp |
| 1317 | ret; |
| 1318 | |
| 1319 | png_ptr=png_ptr; |
| 1320 | ret=((png_voidp) AcquireMagickMemory((size_t) size)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1321 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1322 | if (ret == NULL) |
| 1323 | png_error("Insufficient memory."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1324 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1325 | return(ret); |
| 1326 | #else |
| 1327 | png_ptr=png_ptr; |
| 1328 | return((png_voidp) AcquireMagickMemory((size_t) size)); |
| 1329 | #endif |
| 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | Free a pointer. It is removed from the list at the same time. |
| 1334 | */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1335 | static png_free_ptr Magick_png_free(png_structp png_ptr,png_voidp ptr) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1336 | { |
| 1337 | png_ptr=png_ptr; |
| 1338 | ptr=RelinquishMagickMemory(ptr); |
| 1339 | return((png_free_ptr) NULL); |
| 1340 | } |
| 1341 | #endif |
| 1342 | |
| 1343 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1344 | } |
| 1345 | #endif |
| 1346 | |
| 1347 | static int |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1348 | Magick_png_read_raw_profile(Image *image, const ImageInfo *image_info, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1349 | png_textp text,int ii) |
| 1350 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1351 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1352 | i; |
| 1353 | |
| 1354 | register unsigned char |
| 1355 | *dp; |
| 1356 | |
| 1357 | register png_charp |
| 1358 | sp; |
| 1359 | |
| 1360 | png_uint_32 |
| 1361 | length, |
| 1362 | nibbles; |
| 1363 | |
| 1364 | StringInfo |
| 1365 | *profile; |
| 1366 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1367 | const unsigned char |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1368 | unhex[103]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1369 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1370 | 0,0,0,0,0,0,0,0,0,1, 2,3,4,5,6,7,8,9,0,0, |
| 1371 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1372 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,10,11,12, |
| 1373 | 13,14,15}; |
| 1374 | |
| 1375 | sp=text[ii].text+1; |
| 1376 | /* look for newline */ |
| 1377 | while (*sp != '\n') |
| 1378 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1379 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1380 | /* look for length */ |
| 1381 | while (*sp == '\0' || *sp == ' ' || *sp == '\n') |
| 1382 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1383 | |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 1384 | length=(png_uint_32) StringToLong(sp); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1385 | |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 1386 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1387 | " length: %lu",(unsigned long) length); |
| 1388 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1389 | while (*sp != ' ' && *sp != '\n') |
| 1390 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1391 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1392 | /* allocate space */ |
| 1393 | if (length == 0) |
| 1394 | { |
| 1395 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1396 | CoderWarning,"UnableToCopyProfile","`%s'","invalid profile length"); |
| 1397 | return(MagickFalse); |
| 1398 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1399 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1400 | profile=AcquireStringInfo(length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1401 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1402 | if (profile == (StringInfo *) NULL) |
| 1403 | { |
| 1404 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1405 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 1406 | "unable to copy profile"); |
| 1407 | return(MagickFalse); |
| 1408 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1409 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1410 | /* copy profile, skipping white space and column 1 "=" signs */ |
| 1411 | dp=GetStringInfoDatum(profile); |
| 1412 | nibbles=length*2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1413 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1414 | for (i=0; i < (ssize_t) nibbles; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1415 | { |
| 1416 | while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f') |
| 1417 | { |
| 1418 | if (*sp == '\0') |
| 1419 | { |
| 1420 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1421 | CoderWarning,"UnableToCopyProfile","`%s'","ran out of data"); |
| 1422 | profile=DestroyStringInfo(profile); |
| 1423 | return(MagickFalse); |
| 1424 | } |
| 1425 | sp++; |
| 1426 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1427 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1428 | if (i%2 == 0) |
| 1429 | *dp=(unsigned char) (16*unhex[(int) *sp++]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1430 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1431 | else |
| 1432 | (*dp++)+=unhex[(int) *sp++]; |
| 1433 | } |
| 1434 | /* |
| 1435 | We have already read "Raw profile type. |
| 1436 | */ |
| 1437 | (void) SetImageProfile(image,&text[ii].key[17],profile); |
| 1438 | profile=DestroyStringInfo(profile); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1439 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1440 | if (image_info->verbose) |
| 1441 | (void) printf(" Found a generic profile, type %s\n",&text[ii].key[17]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1442 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1443 | return MagickTrue; |
| 1444 | } |
| 1445 | |
| 1446 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 1447 | static int read_vpag_chunk_callback(png_struct *ping, png_unknown_chunkp chunk) |
| 1448 | { |
| 1449 | Image |
| 1450 | *image; |
| 1451 | |
| 1452 | |
| 1453 | /* The unknown chunk structure contains the chunk data: |
| 1454 | png_byte name[5]; |
| 1455 | png_byte *data; |
| 1456 | png_size_t size; |
| 1457 | |
| 1458 | Note that libpng has already taken care of the CRC handling. |
| 1459 | */ |
| 1460 | |
| 1461 | |
| 1462 | if (chunk->name[0] != 118 || chunk->name[1] != 112 || |
| 1463 | chunk->name[2] != 65 ||chunk-> name[3] != 103) |
| 1464 | return(0); /* Did not recognize */ |
| 1465 | |
| 1466 | /* recognized vpAg */ |
| 1467 | |
| 1468 | if (chunk->size != 9) |
| 1469 | return(-1); /* Error return */ |
| 1470 | |
| 1471 | if (chunk->data[8] != 0) |
| 1472 | return(0); /* ImageMagick requires pixel units */ |
| 1473 | |
| 1474 | image=(Image *) png_get_user_chunk_ptr(ping); |
| 1475 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1476 | image->page.width=(size_t) ((chunk->data[0] << 24) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1477 | (chunk->data[1] << 16) | (chunk->data[2] << 8) | chunk->data[3]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1478 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1479 | image->page.height=(size_t) ((chunk->data[4] << 24) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1480 | (chunk->data[5] << 16) | (chunk->data[6] << 8) | chunk->data[7]); |
| 1481 | |
| 1482 | /* Return one of the following: */ |
| 1483 | /* return(-n); chunk had an error */ |
| 1484 | /* return(0); did not recognize */ |
| 1485 | /* return(n); success */ |
| 1486 | |
| 1487 | return(1); |
| 1488 | |
| 1489 | } |
| 1490 | #endif |
| 1491 | |
| 1492 | /* |
| 1493 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1494 | % % |
| 1495 | % % |
| 1496 | % % |
| 1497 | % R e a d O n e P N G I m a g e % |
| 1498 | % % |
| 1499 | % % |
| 1500 | % % |
| 1501 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1502 | % |
| 1503 | % ReadOnePNGImage() reads a Portable Network Graphics (PNG) image file |
| 1504 | % (minus the 8-byte signature) and returns it. It allocates the memory |
| 1505 | % necessary for the new Image structure and returns a pointer to the new |
| 1506 | % image. |
| 1507 | % |
| 1508 | % The format of the ReadOnePNGImage method is: |
| 1509 | % |
| 1510 | % Image *ReadOnePNGImage(MngInfo *mng_info, const ImageInfo *image_info, |
| 1511 | % ExceptionInfo *exception) |
| 1512 | % |
| 1513 | % A description of each parameter follows: |
| 1514 | % |
| 1515 | % o mng_info: Specifies a pointer to a MngInfo structure. |
| 1516 | % |
| 1517 | % o image_info: the image info. |
| 1518 | % |
| 1519 | % o exception: return any errors or warnings in this structure. |
| 1520 | % |
| 1521 | */ |
| 1522 | static Image *ReadOnePNGImage(MngInfo *mng_info, |
| 1523 | const ImageInfo *image_info, ExceptionInfo *exception) |
| 1524 | { |
| 1525 | /* Read one PNG image */ |
| 1526 | |
glennrp | cc95c3f | 2011-04-18 16:46:48 +0000 | [diff] [blame] | 1527 | /* To do: Read the tIME chunk into the date:modify property */ |
| 1528 | /* To do: Read the tEXt/Creation Time chunk into the date:create property */ |
| 1529 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1530 | Image |
| 1531 | *image; |
| 1532 | |
| 1533 | int |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 1534 | intent, |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 1535 | num_raw_profiles, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1536 | num_text, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 1537 | num_text_total, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1538 | num_passes, |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1539 | pass, |
| 1540 | ping_bit_depth, |
| 1541 | ping_color_type, |
| 1542 | ping_interlace_method, |
| 1543 | ping_compression_method, |
| 1544 | ping_filter_method, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 1545 | ping_num_trans, |
| 1546 | unit_type; |
| 1547 | |
| 1548 | double |
| 1549 | file_gamma; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1550 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 1551 | LongPixelPacket |
| 1552 | transparent_color; |
| 1553 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1554 | MagickBooleanType |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 1555 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1556 | status; |
| 1557 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1558 | png_bytep |
| 1559 | ping_trans_alpha; |
| 1560 | |
| 1561 | png_color_16p |
| 1562 | ping_background, |
| 1563 | ping_trans_color; |
| 1564 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1565 | png_info |
| 1566 | *end_info, |
| 1567 | *ping_info; |
| 1568 | |
| 1569 | png_struct |
| 1570 | *ping; |
| 1571 | |
| 1572 | png_textp |
| 1573 | text; |
| 1574 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1575 | png_uint_32 |
| 1576 | ping_height, |
| 1577 | ping_width, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 1578 | ping_rowbytes, |
| 1579 | x_resolution, |
| 1580 | y_resolution; |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1581 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1582 | QuantumInfo |
| 1583 | *quantum_info; |
| 1584 | |
| 1585 | unsigned char |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1586 | *ping_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1587 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1588 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1589 | y; |
| 1590 | |
| 1591 | register unsigned char |
| 1592 | *p; |
| 1593 | |
| 1594 | register IndexPacket |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 1595 | *indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1596 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1597 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1598 | i, |
| 1599 | x; |
| 1600 | |
| 1601 | register PixelPacket |
| 1602 | *q; |
| 1603 | |
| 1604 | size_t |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 1605 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1606 | row_offset; |
| 1607 | |
cristy | eb3b22a | 2011-03-31 20:16:11 +0000 | [diff] [blame] | 1608 | ssize_t |
| 1609 | j; |
| 1610 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1611 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 1612 | png_byte unused_chunks[]= |
| 1613 | { |
| 1614 | 104, 73, 83, 84, (png_byte) '\0', /* hIST */ |
| 1615 | 105, 84, 88, 116, (png_byte) '\0', /* iTXt */ |
| 1616 | 112, 67, 65, 76, (png_byte) '\0', /* pCAL */ |
| 1617 | 115, 67, 65, 76, (png_byte) '\0', /* sCAL */ |
| 1618 | 115, 80, 76, 84, (png_byte) '\0', /* sPLT */ |
| 1619 | 116, 73, 77, 69, (png_byte) '\0', /* tIME */ |
| 1620 | }; |
| 1621 | #endif |
| 1622 | |
| 1623 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 1624 | " Enter ReadOnePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1625 | |
| 1626 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1627 | LockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1628 | #endif |
| 1629 | |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 1630 | #if (PNG_LIBPNG_VER < 10200) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1631 | if (image_info->verbose) |
| 1632 | printf("Your PNG library (libpng-%s) is rather old.\n", |
| 1633 | PNG_LIBPNG_VER_STRING); |
| 1634 | #endif |
| 1635 | |
glennrp | 61b4c95 | 2009-11-10 20:40:41 +0000 | [diff] [blame] | 1636 | #if (PNG_LIBPNG_VER >= 10400) |
| 1637 | # ifndef PNG_TRANSFORM_GRAY_TO_RGB /* Added at libpng-1.4.0beta67 */ |
| 1638 | if (image_info->verbose) |
| 1639 | { |
| 1640 | printf("Your PNG library (libpng-%s) is an old beta version.\n", |
| 1641 | PNG_LIBPNG_VER_STRING); |
| 1642 | printf("Please update it.\n"); |
| 1643 | } |
| 1644 | # endif |
| 1645 | #endif |
| 1646 | |
| 1647 | |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 1648 | quantum_info = (QuantumInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1649 | image=mng_info->image; |
| 1650 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 1651 | if (logging != MagickFalse) |
| 1652 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1653 | " image->matte=%d",(int) image->matte); |
| 1654 | |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 1655 | /* Set to an out-of-range color unless tRNS chunk is present */ |
| 1656 | transparent_color.red=65537; |
| 1657 | transparent_color.green=65537; |
| 1658 | transparent_color.blue=65537; |
| 1659 | transparent_color.opacity=65537; |
| 1660 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 1661 | num_text = 0; |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 1662 | num_text_total = 0; |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 1663 | num_raw_profiles = 0; |
| 1664 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1665 | /* |
| 1666 | Allocate the PNG structures |
| 1667 | */ |
| 1668 | #ifdef PNG_USER_MEM_SUPPORTED |
| 1669 | ping=png_create_read_struct_2(PNG_LIBPNG_VER_STRING, image, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1670 | MagickPNGErrorHandler,MagickPNGWarningHandler, NULL, |
| 1671 | (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1672 | #else |
| 1673 | ping=png_create_read_struct(PNG_LIBPNG_VER_STRING,image, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1674 | MagickPNGErrorHandler,MagickPNGWarningHandler); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1675 | #endif |
| 1676 | if (ping == (png_struct *) NULL) |
| 1677 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1678 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1679 | ping_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1680 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1681 | if (ping_info == (png_info *) NULL) |
| 1682 | { |
| 1683 | png_destroy_read_struct(&ping,(png_info **) NULL,(png_info **) NULL); |
| 1684 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1685 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1686 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1687 | end_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1688 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1689 | if (end_info == (png_info *) NULL) |
| 1690 | { |
| 1691 | png_destroy_read_struct(&ping,&ping_info,(png_info **) NULL); |
| 1692 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1693 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1694 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1695 | ping_pixels=(unsigned char *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1696 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1697 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1698 | { |
| 1699 | /* |
| 1700 | PNG image is corrupt. |
| 1701 | */ |
| 1702 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
| 1703 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1704 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1705 | #endif |
| 1706 | if (logging != MagickFalse) |
| 1707 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1708 | " exit ReadOnePNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1709 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1710 | if (image != (Image *) NULL) |
cristy | 7b755eb | 2009-12-05 14:51:29 +0000 | [diff] [blame] | 1711 | { |
| 1712 | InheritException(exception,&image->exception); |
| 1713 | image->columns=0; |
| 1714 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1715 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1716 | return(GetFirstImageInList(image)); |
| 1717 | } |
| 1718 | /* |
| 1719 | Prepare PNG for reading. |
| 1720 | */ |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1721 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1722 | mng_info->image_found++; |
| 1723 | png_set_sig_bytes(ping,8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1724 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1725 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 1726 | { |
| 1727 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 1728 | (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES); |
| 1729 | png_set_read_fn(ping,image,png_get_data); |
| 1730 | #else |
| 1731 | #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) |
| 1732 | png_permit_empty_plte(ping,MagickTrue); |
| 1733 | png_set_read_fn(ping,image,png_get_data); |
| 1734 | #else |
| 1735 | mng_info->image=image; |
| 1736 | mng_info->bytes_in_read_buffer=0; |
| 1737 | mng_info->found_empty_plte=MagickFalse; |
| 1738 | mng_info->have_saved_bkgd_index=MagickFalse; |
| 1739 | png_set_read_fn(ping,mng_info,mng_get_data); |
| 1740 | #endif |
| 1741 | #endif |
| 1742 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1743 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1744 | else |
| 1745 | png_set_read_fn(ping,image,png_get_data); |
| 1746 | |
| 1747 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 1748 | /* Ignore unused chunks and all unknown chunks except for vpAg */ |
| 1749 | png_set_keep_unknown_chunks(ping, 1, NULL, 0); |
| 1750 | png_set_keep_unknown_chunks(ping, 2, mng_vpAg, 1); |
| 1751 | png_set_keep_unknown_chunks(ping, 1, unused_chunks, |
| 1752 | (int)sizeof(unused_chunks)/5); |
| 1753 | /* Callback for other unknown chunks */ |
| 1754 | png_set_read_user_chunk_fn(ping, image, read_vpag_chunk_callback); |
| 1755 | #endif |
| 1756 | |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 1757 | #if (PNG_LIBPNG_VER < 10400) |
| 1758 | # if defined(PNG_USE_PNGGCCRD) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \ |
| 1759 | (PNG_LIBPNG_VER >= 10200) && (PNG_LIBPNG_VER < 10220) && defined(__i386__) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1760 | /* Disable thread-unsafe features of pnggccrd */ |
| 1761 | if (png_access_version_number() >= 10200) |
| 1762 | { |
| 1763 | png_uint_32 mmx_disable_mask=0; |
| 1764 | png_uint_32 asm_flags; |
| 1765 | |
| 1766 | mmx_disable_mask |= ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \ |
| 1767 | | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ |
| 1768 | | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ |
| 1769 | | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ); |
| 1770 | asm_flags=png_get_asm_flags(ping); |
| 1771 | png_set_asm_flags(ping, asm_flags & ~mmx_disable_mask); |
| 1772 | } |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 1773 | # endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1774 | #endif |
| 1775 | |
| 1776 | png_read_info(ping,ping_info); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1777 | |
| 1778 | png_get_IHDR(ping,ping_info,&ping_width,&ping_height, |
| 1779 | &ping_bit_depth,&ping_color_type, |
| 1780 | &ping_interlace_method,&ping_compression_method, |
| 1781 | &ping_filter_method); |
| 1782 | |
| 1783 | (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans, |
| 1784 | &ping_trans_color); |
| 1785 | |
| 1786 | (void) png_get_bKGD(ping, ping_info, &ping_background); |
| 1787 | |
| 1788 | if (ping_bit_depth < 8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1789 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1790 | if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)) |
| 1791 | { |
| 1792 | png_set_packing(ping); |
| 1793 | ping_bit_depth = 8; |
| 1794 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1795 | } |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1796 | |
| 1797 | image->depth=ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1798 | image->depth=GetImageQuantumDepth(image,MagickFalse); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1799 | image->interlace=ping_interlace_method != 0 ? PNGInterlace : NoInterlace; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1800 | if (logging != MagickFalse) |
| 1801 | { |
| 1802 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1803 | " PNG width: %.20g, height: %.20g", |
| 1804 | (double) ping_width, (double) ping_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1805 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1806 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1807 | " PNG color_type: %d, bit_depth: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1808 | ping_color_type, ping_bit_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1809 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1810 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1811 | " PNG compression_method: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1812 | ping_compression_method); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1813 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1814 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1815 | " PNG interlace_method: %d, filter_method: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1816 | ping_interlace_method,ping_filter_method); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1819 | #ifdef PNG_READ_iCCP_SUPPORTED |
| 1820 | if (png_get_valid(ping,ping_info,PNG_INFO_iCCP)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1821 | { |
| 1822 | int |
| 1823 | compression; |
| 1824 | |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 1825 | #if (PNG_LIBPNG_VER < 10500) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1826 | png_charp |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 1827 | info; |
| 1828 | #else |
| 1829 | png_bytep |
| 1830 | info; |
| 1831 | #endif |
| 1832 | |
| 1833 | png_charp |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1834 | name; |
| 1835 | |
| 1836 | png_uint_32 |
| 1837 | profile_length; |
| 1838 | |
| 1839 | (void) png_get_iCCP(ping,ping_info,&name,(int *) &compression,&info, |
| 1840 | &profile_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1841 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1842 | if (profile_length != 0) |
| 1843 | { |
| 1844 | StringInfo |
| 1845 | *profile; |
| 1846 | |
| 1847 | if (logging != MagickFalse) |
| 1848 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1849 | " Reading PNG iCCP chunk."); |
| 1850 | profile=AcquireStringInfo(profile_length); |
| 1851 | SetStringInfoDatum(profile,(const unsigned char *) info); |
| 1852 | (void) SetImageProfile(image,"icc",profile); |
| 1853 | profile=DestroyStringInfo(profile); |
| 1854 | } |
| 1855 | } |
| 1856 | #endif |
| 1857 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 1858 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1859 | if (mng_info->have_global_srgb) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1860 | image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent |
| 1861 | (mng_info->global_srgb_intent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1862 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1863 | if (png_get_sRGB(ping,ping_info,&intent)) |
| 1864 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1865 | image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent |
| 1866 | (intent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1867 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1868 | if (logging != MagickFalse) |
| 1869 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1870 | " Reading PNG sRGB chunk: rendering_intent: %d",intent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | #endif |
| 1874 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1875 | if (!png_get_gAMA(ping,ping_info,&file_gamma)) |
| 1876 | if (mng_info->have_global_gama) |
| 1877 | png_set_gAMA(ping,ping_info,mng_info->global_gamma); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1878 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1879 | if (png_get_gAMA(ping,ping_info,&file_gamma)) |
| 1880 | { |
| 1881 | image->gamma=(float) file_gamma; |
| 1882 | if (logging != MagickFalse) |
| 1883 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1884 | " Reading PNG gAMA chunk: gamma: %f",file_gamma); |
| 1885 | } |
| 1886 | } |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1887 | if (!png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
| 1888 | { |
| 1889 | if (mng_info->have_global_chrm != MagickFalse) |
| 1890 | { |
| 1891 | (void) png_set_cHRM(ping,ping_info, |
| 1892 | mng_info->global_chrm.white_point.x, |
| 1893 | mng_info->global_chrm.white_point.y, |
| 1894 | mng_info->global_chrm.red_primary.x, |
| 1895 | mng_info->global_chrm.red_primary.y, |
| 1896 | mng_info->global_chrm.green_primary.x, |
| 1897 | mng_info->global_chrm.green_primary.y, |
| 1898 | mng_info->global_chrm.blue_primary.x, |
| 1899 | mng_info->global_chrm.blue_primary.y); |
| 1900 | } |
| 1901 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1902 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1903 | if (png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1904 | { |
| 1905 | (void) png_get_cHRM(ping,ping_info, |
| 1906 | &image->chromaticity.white_point.x, |
| 1907 | &image->chromaticity.white_point.y, |
| 1908 | &image->chromaticity.red_primary.x, |
| 1909 | &image->chromaticity.red_primary.y, |
| 1910 | &image->chromaticity.green_primary.x, |
| 1911 | &image->chromaticity.green_primary.y, |
| 1912 | &image->chromaticity.blue_primary.x, |
| 1913 | &image->chromaticity.blue_primary.y); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1914 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1915 | if (logging != MagickFalse) |
| 1916 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1917 | " Reading PNG cHRM chunk."); |
| 1918 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1919 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1920 | if (image->rendering_intent != UndefinedIntent) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1921 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1922 | png_set_sRGB(ping,ping_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1923 | Magick_RenderingIntent_to_PNG_RenderingIntent |
| 1924 | (image->rendering_intent)); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1925 | png_set_gAMA(ping,ping_info,0.45455f); |
| 1926 | png_set_cHRM(ping,ping_info, |
| 1927 | 0.6400f, 0.3300f, 0.3000f, 0.6000f, |
| 1928 | 0.1500f, 0.0600f, 0.3127f, 0.3290f); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1929 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1930 | #if defined(PNG_oFFs_SUPPORTED) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1931 | if (png_get_valid(ping,ping_info,PNG_INFO_oFFs)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1932 | { |
cristy | 905ef80 | 2011-02-23 00:29:18 +0000 | [diff] [blame] | 1933 | image->page.x=(ssize_t) png_get_x_offset_pixels(ping, ping_info); |
| 1934 | image->page.y=(ssize_t) png_get_y_offset_pixels(ping, ping_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1935 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1936 | if (logging != MagickFalse) |
| 1937 | if (image->page.x || image->page.y) |
| 1938 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1939 | " Reading PNG oFFs chunk: x: %.20g, y: %.20g.",(double) |
| 1940 | image->page.x,(double) image->page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1941 | } |
| 1942 | #endif |
| 1943 | #if defined(PNG_pHYs_SUPPORTED) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1944 | if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
| 1945 | { |
| 1946 | if (mng_info->have_global_phys) |
| 1947 | { |
| 1948 | png_set_pHYs(ping,ping_info, |
| 1949 | mng_info->global_x_pixels_per_unit, |
| 1950 | mng_info->global_y_pixels_per_unit, |
| 1951 | mng_info->global_phys_unit_type); |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | if (png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1956 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1957 | /* |
| 1958 | Set image resolution. |
| 1959 | */ |
| 1960 | (void) png_get_pHYs(ping,ping_info,&x_resolution,&y_resolution, |
cristy | 0881b52 | 2010-04-24 23:45:19 +0000 | [diff] [blame] | 1961 | &unit_type); |
| 1962 | image->x_resolution=(double) x_resolution; |
| 1963 | image->y_resolution=(double) y_resolution; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1964 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1965 | if (unit_type == PNG_RESOLUTION_METER) |
| 1966 | { |
| 1967 | image->units=PixelsPerCentimeterResolution; |
| 1968 | image->x_resolution=(double) x_resolution/100.0; |
| 1969 | image->y_resolution=(double) y_resolution/100.0; |
| 1970 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1971 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1972 | if (logging != MagickFalse) |
| 1973 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1974 | " Reading PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.", |
| 1975 | (double) x_resolution,(double) y_resolution,unit_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1976 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1977 | #endif |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 1978 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1979 | if (png_get_valid(ping,ping_info,PNG_INFO_PLTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1980 | { |
| 1981 | int |
| 1982 | number_colors; |
| 1983 | |
| 1984 | png_colorp |
| 1985 | palette; |
| 1986 | |
| 1987 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1988 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1989 | if ((number_colors == 0) && |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1990 | ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1991 | { |
| 1992 | if (mng_info->global_plte_length) |
| 1993 | { |
| 1994 | png_set_PLTE(ping,ping_info,mng_info->global_plte, |
| 1995 | (int) mng_info->global_plte_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1996 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1997 | if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1998 | if (mng_info->global_trns_length) |
| 1999 | { |
| 2000 | if (mng_info->global_trns_length > |
| 2001 | mng_info->global_plte_length) |
| 2002 | (void) ThrowMagickException(&image->exception, |
| 2003 | GetMagickModule(),CoderError, |
| 2004 | "global tRNS has more entries than global PLTE", |
| 2005 | "`%s'",image_info->filename); |
| 2006 | png_set_tRNS(ping,ping_info,mng_info->global_trns, |
| 2007 | (int) mng_info->global_trns_length,NULL); |
| 2008 | } |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2009 | #ifdef PNG_READ_bKGD_SUPPORTED |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2010 | if ( |
| 2011 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 2012 | mng_info->have_saved_bkgd_index || |
| 2013 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2014 | png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2015 | { |
| 2016 | png_color_16 |
| 2017 | background; |
| 2018 | |
| 2019 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 2020 | if (mng_info->have_saved_bkgd_index) |
| 2021 | background.index=mng_info->saved_bkgd_index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2022 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2023 | if (png_get_valid(ping, ping_info, PNG_INFO_bKGD)) |
| 2024 | background.index=ping_background->index; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2025 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2026 | background.red=(png_uint_16) |
| 2027 | mng_info->global_plte[background.index].red; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2028 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2029 | background.green=(png_uint_16) |
| 2030 | mng_info->global_plte[background.index].green; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2031 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2032 | background.blue=(png_uint_16) |
| 2033 | mng_info->global_plte[background.index].blue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2034 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2035 | png_set_bKGD(ping,ping_info,&background); |
| 2036 | } |
| 2037 | #endif |
| 2038 | } |
| 2039 | else |
| 2040 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 2041 | CoderError,"No global PLTE in file","`%s'", |
| 2042 | image_info->filename); |
| 2043 | } |
| 2044 | } |
| 2045 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2046 | #ifdef PNG_READ_bKGD_SUPPORTED |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2047 | if (mng_info->have_global_bkgd && |
| 2048 | (!png_get_valid(ping,ping_info,PNG_INFO_bKGD))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2049 | image->background_color=mng_info->mng_global_bkgd; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2050 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2051 | if (png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2052 | { |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2053 | unsigned int |
| 2054 | bkgd_scale; |
| 2055 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2056 | /* |
| 2057 | Set image background color. |
| 2058 | */ |
| 2059 | if (logging != MagickFalse) |
| 2060 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2061 | " Reading PNG bKGD chunk."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2062 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2063 | /* Scale background components to 16-bit, then scale |
| 2064 | * to quantum depth |
| 2065 | */ |
| 2066 | if (logging != MagickFalse) |
| 2067 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2068 | " raw ping_background=(%d,%d,%d).",ping_background->red, |
| 2069 | ping_background->green,ping_background->blue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2070 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2071 | bkgd_scale = 1; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2072 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2073 | if (ping_bit_depth == 1) |
| 2074 | bkgd_scale = 255; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2075 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2076 | else if (ping_bit_depth == 2) |
| 2077 | bkgd_scale = 85; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2078 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2079 | else if (ping_bit_depth == 4) |
| 2080 | bkgd_scale = 17; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2081 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2082 | if (ping_bit_depth <= 8) |
| 2083 | bkgd_scale *= 257; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2084 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2085 | ping_background->red *= bkgd_scale; |
| 2086 | ping_background->green *= bkgd_scale; |
| 2087 | ping_background->blue *= bkgd_scale; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2088 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2089 | if (logging != MagickFalse) |
| 2090 | { |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2091 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2092 | " bkgd_scale=%d.",bkgd_scale); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2093 | |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2094 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2095 | " ping_background=(%d,%d,%d).",ping_background->red, |
| 2096 | ping_background->green,ping_background->blue); |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2097 | } |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2098 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2099 | image->background_color.red= |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2100 | ScaleShortToQuantum(ping_background->red); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2101 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2102 | image->background_color.green= |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2103 | ScaleShortToQuantum(ping_background->green); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2104 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2105 | image->background_color.blue= |
| 2106 | ScaleShortToQuantum(ping_background->blue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2107 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2108 | image->background_color.opacity=OpaqueOpacity; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2109 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2110 | if (logging != MagickFalse) |
| 2111 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2112 | " image->background_color=(%.20g,%.20g,%.20g).", |
| 2113 | (double) image->background_color.red, |
| 2114 | (double) image->background_color.green, |
| 2115 | (double) image->background_color.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2116 | } |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2117 | #endif /* PNG_READ_bKGD_SUPPORTED */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2118 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2119 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2120 | { |
| 2121 | /* |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2122 | Image has a tRNS chunk. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2123 | */ |
| 2124 | int |
| 2125 | max_sample; |
| 2126 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 2127 | size_t |
| 2128 | one=1; |
| 2129 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2130 | if (logging != MagickFalse) |
| 2131 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2132 | " Reading PNG tRNS chunk."); |
| 2133 | |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 2134 | max_sample = (int) ((one << ping_bit_depth) - 1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2135 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2136 | if ((ping_color_type == PNG_COLOR_TYPE_GRAY && |
| 2137 | (int)ping_trans_color->gray > max_sample) || |
| 2138 | (ping_color_type == PNG_COLOR_TYPE_RGB && |
| 2139 | ((int)ping_trans_color->red > max_sample || |
| 2140 | (int)ping_trans_color->green > max_sample || |
| 2141 | (int)ping_trans_color->blue > max_sample))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2142 | { |
| 2143 | if (logging != MagickFalse) |
| 2144 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2145 | " Ignoring PNG tRNS chunk with out-of-range sample."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2146 | png_free_data(ping, ping_info, PNG_FREE_TRNS, 0); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2147 | png_set_invalid(ping,ping_info,PNG_INFO_tRNS); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2148 | image->matte=MagickFalse; |
| 2149 | } |
| 2150 | else |
| 2151 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2152 | int |
| 2153 | scale_to_short; |
| 2154 | |
| 2155 | scale_to_short = 65535L/((1UL << ping_bit_depth)-1); |
| 2156 | |
| 2157 | /* Scale transparent_color to short */ |
| 2158 | transparent_color.red= scale_to_short*ping_trans_color->red; |
| 2159 | transparent_color.green= scale_to_short*ping_trans_color->green; |
| 2160 | transparent_color.blue= scale_to_short*ping_trans_color->blue; |
| 2161 | transparent_color.opacity= scale_to_short*ping_trans_color->gray; |
glennrp | 05eb4a9 | 2010-07-08 02:21:09 +0000 | [diff] [blame] | 2162 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2163 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2164 | { |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 2165 | if (logging != MagickFalse) |
| 2166 | { |
| 2167 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2168 | " Raw tRNS graylevel is %d.",ping_trans_color->gray); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2169 | |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 2170 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2171 | " scaled graylevel is %d.",transparent_color.opacity); |
| 2172 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2173 | transparent_color.red=transparent_color.opacity; |
| 2174 | transparent_color.green=transparent_color.opacity; |
| 2175 | transparent_color.blue=transparent_color.opacity; |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | #if defined(PNG_READ_sBIT_SUPPORTED) |
| 2180 | if (mng_info->have_global_sbit) |
| 2181 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2182 | if (!png_get_valid(ping,ping_info,PNG_INFO_sBIT)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2183 | png_set_sBIT(ping,ping_info,&mng_info->global_sbit); |
| 2184 | } |
| 2185 | #endif |
| 2186 | num_passes=png_set_interlace_handling(ping); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2187 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2188 | png_read_update_info(ping,ping_info); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2189 | |
| 2190 | ping_rowbytes=png_get_rowbytes(ping,ping_info); |
| 2191 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2192 | /* |
| 2193 | Initialize image structure. |
| 2194 | */ |
| 2195 | mng_info->image_box.left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2196 | mng_info->image_box.right=(ssize_t) ping_width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2197 | mng_info->image_box.top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2198 | mng_info->image_box.bottom=(ssize_t) ping_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2199 | if (mng_info->mng_type == 0) |
| 2200 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2201 | mng_info->mng_width=ping_width; |
| 2202 | mng_info->mng_height=ping_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2203 | mng_info->frame=mng_info->image_box; |
| 2204 | mng_info->clip=mng_info->image_box; |
| 2205 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2206 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2207 | else |
| 2208 | { |
| 2209 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 2210 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2211 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2212 | image->compression=ZipCompression; |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2213 | image->columns=ping_width; |
| 2214 | image->rows=ping_height; |
| 2215 | if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) || |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2216 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2217 | { |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 2218 | size_t |
| 2219 | one; |
| 2220 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2221 | image->storage_class=PseudoClass; |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 2222 | one=1; |
| 2223 | image->colors=one << ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2224 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 2225 | if (image->colors > 256) |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 2226 | image->colors=256; |
| 2227 | #else |
| 2228 | if (image->colors > 65536L) |
| 2229 | image->colors=65536L; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2230 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2231 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2232 | { |
| 2233 | int |
| 2234 | number_colors; |
| 2235 | |
| 2236 | png_colorp |
| 2237 | palette; |
| 2238 | |
| 2239 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2240 | image->colors=(size_t) number_colors; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2241 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2242 | if (logging != MagickFalse) |
| 2243 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2244 | " Reading PNG PLTE chunk: number_colors: %d.",number_colors); |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | if (image->storage_class == PseudoClass) |
| 2249 | { |
| 2250 | /* |
| 2251 | Initialize image colormap. |
| 2252 | */ |
| 2253 | if (AcquireImageColormap(image,image->colors) == MagickFalse) |
| 2254 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2255 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2256 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2257 | { |
| 2258 | int |
| 2259 | number_colors; |
| 2260 | |
| 2261 | png_colorp |
| 2262 | palette; |
| 2263 | |
| 2264 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2265 | |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2266 | for (i=0; i < (ssize_t) number_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2267 | { |
| 2268 | image->colormap[i].red=ScaleCharToQuantum(palette[i].red); |
| 2269 | image->colormap[i].green=ScaleCharToQuantum(palette[i].green); |
| 2270 | image->colormap[i].blue=ScaleCharToQuantum(palette[i].blue); |
| 2271 | } |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2272 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 2273 | for ( ; i < (ssize_t) image->colors; i++) |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2274 | { |
| 2275 | image->colormap[i].red=0; |
| 2276 | image->colormap[i].green=0; |
| 2277 | image->colormap[i].blue=0; |
| 2278 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2279 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2280 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2281 | else |
| 2282 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2283 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2284 | scale; |
| 2285 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2286 | scale=(QuantumRange/((1UL << ping_bit_depth)-1)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2287 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2288 | if (scale < 1) |
| 2289 | scale=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2290 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2291 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2292 | { |
| 2293 | image->colormap[i].red=(Quantum) (i*scale); |
| 2294 | image->colormap[i].green=(Quantum) (i*scale); |
| 2295 | image->colormap[i].blue=(Quantum) (i*scale); |
| 2296 | } |
| 2297 | } |
| 2298 | } |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2299 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2300 | /* Set some properties for reporting by "identify" */ |
| 2301 | { |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2302 | char |
| 2303 | msg[MaxTextExtent]; |
| 2304 | |
| 2305 | /* encode ping_width, ping_height, ping_bit_depth, ping_color_type, |
| 2306 | ping_interlace_method in value */ |
| 2307 | |
glennrp | 7cdb11c | 2011-03-31 18:17:25 +0000 | [diff] [blame] | 2308 | (void) FormatMagickString(msg,MaxTextExtent, |
| 2309 | "%d, %d",(int) ping_width, (int) ping_height); |
| 2310 | (void) SetImageProperty(image,"PNG:IHDR.width,height ",msg); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2311 | |
| 2312 | (void) FormatMagickString(msg,MaxTextExtent,"%d",(int) ping_bit_depth); |
| 2313 | (void) SetImageProperty(image,"PNG:IHDR.bit_depth ",msg); |
| 2314 | |
| 2315 | (void) FormatMagickString(msg,MaxTextExtent,"%d",(int) ping_color_type); |
| 2316 | (void) SetImageProperty(image,"PNG:IHDR.color_type ",msg); |
| 2317 | |
| 2318 | (void) FormatMagickString(msg,MaxTextExtent,"%d", |
| 2319 | (int) ping_interlace_method); |
| 2320 | (void) SetImageProperty(image,"PNG:IHDR.interlace_method",msg); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2321 | } |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2322 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2323 | /* |
| 2324 | Read image scanlines. |
| 2325 | */ |
| 2326 | if (image->delay != 0) |
| 2327 | mng_info->scenes_found++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2328 | |
glennrp | 0ca69b1 | 2010-07-26 01:57:52 +0000 | [diff] [blame] | 2329 | if ((mng_info->mng_type == 0 && (image->ping != MagickFalse)) || ( |
glennrp | 347e40f | 2010-06-06 11:27:30 +0000 | [diff] [blame] | 2330 | (image_info->number_scenes != 0) && (mng_info->scenes_found > (ssize_t) |
| 2331 | (image_info->first_scene+image_info->number_scenes)))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2332 | { |
| 2333 | if (logging != MagickFalse) |
| 2334 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2335 | " Skipping PNG image data for scene %.20g",(double) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2336 | mng_info->scenes_found-1); |
| 2337 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
| 2338 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2339 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2340 | #endif |
| 2341 | if (logging != MagickFalse) |
| 2342 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2343 | " exit ReadOnePNGImage()."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2344 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2345 | return(image); |
| 2346 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2347 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2348 | if (logging != MagickFalse) |
| 2349 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2350 | " Reading PNG IDAT chunk(s)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2351 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2352 | if (num_passes > 1) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2353 | ping_pixels=(unsigned char *) AcquireQuantumMemory(image->rows, |
| 2354 | ping_rowbytes*sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2355 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2356 | else |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2357 | ping_pixels=(unsigned char *) AcquireQuantumMemory(ping_rowbytes, |
| 2358 | sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2359 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2360 | if (ping_pixels == (unsigned char *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2361 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2362 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2363 | if (logging != MagickFalse) |
| 2364 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2365 | " Converting PNG pixels to pixel packets"); |
| 2366 | /* |
| 2367 | Convert PNG pixels to pixel packets. |
| 2368 | */ |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2369 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2370 | { |
| 2371 | /* |
| 2372 | PNG image is corrupt. |
| 2373 | */ |
| 2374 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
| 2375 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2376 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2377 | #endif |
| 2378 | if (quantum_info != (QuantumInfo *) NULL) |
| 2379 | quantum_info = DestroyQuantumInfo(quantum_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2380 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2381 | if (ping_pixels != (unsigned char *) NULL) |
| 2382 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2383 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2384 | if (logging != MagickFalse) |
| 2385 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2386 | " exit ReadOnePNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2387 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2388 | if (image != (Image *) NULL) |
cristy | 7b755eb | 2009-12-05 14:51:29 +0000 | [diff] [blame] | 2389 | { |
| 2390 | InheritException(exception,&image->exception); |
| 2391 | image->columns=0; |
| 2392 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2393 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2394 | return(GetFirstImageInList(image)); |
| 2395 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2396 | |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 2397 | quantum_info=AcquireQuantumInfo(image_info,image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2398 | |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 2399 | if (quantum_info == (QuantumInfo *) NULL) |
| 2400 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2401 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2402 | { |
| 2403 | |
| 2404 | MagickBooleanType |
| 2405 | found_transparent_pixel; |
| 2406 | |
| 2407 | found_transparent_pixel=MagickFalse; |
| 2408 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2409 | if (image->storage_class == DirectClass) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2410 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2411 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2412 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2413 | /* |
| 2414 | Convert image to DirectClass pixel packets. |
| 2415 | */ |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 2416 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 2417 | int |
| 2418 | depth; |
| 2419 | |
| 2420 | depth=(ssize_t) ping_bit_depth; |
| 2421 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2422 | image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) || |
| 2423 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || |
| 2424 | (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ? |
| 2425 | MagickTrue : MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2426 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2427 | for (y=0; y < (ssize_t) image->rows; y++) |
| 2428 | { |
| 2429 | if (num_passes > 1) |
| 2430 | row_offset=ping_rowbytes*y; |
| 2431 | |
| 2432 | else |
| 2433 | row_offset=0; |
| 2434 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2435 | png_read_row(ping,ping_pixels+row_offset,NULL); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2436 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| 2437 | |
| 2438 | if (q == (PixelPacket *) NULL) |
| 2439 | break; |
| 2440 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2441 | if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 2442 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2443 | GrayQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2444 | |
| 2445 | else if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2446 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2447 | GrayAlphaQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2448 | |
| 2449 | else if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 2450 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2451 | RGBAQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2452 | |
| 2453 | else if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
| 2454 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2455 | IndexQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2456 | |
| 2457 | else /* ping_color_type == PNG_COLOR_TYPE_RGB */ |
| 2458 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2459 | RGBQuantum,ping_pixels+row_offset,exception); |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2460 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2461 | if (found_transparent_pixel == MagickFalse) |
| 2462 | { |
| 2463 | /* Is there a transparent pixel in the row? */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2464 | if (y== 0 && logging != MagickFalse) |
| 2465 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2466 | " Looking for cheap transparent pixel"); |
| 2467 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2468 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 2469 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 2470 | if ((ping_color_type == PNG_COLOR_TYPE_RGBA || |
| 2471 | ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) && |
| 2472 | (q->opacity != OpaqueOpacity)) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2473 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2474 | if (logging != MagickFalse) |
| 2475 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2476 | " ...got one."); |
| 2477 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2478 | found_transparent_pixel = MagickTrue; |
| 2479 | break; |
| 2480 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 2481 | if ((ping_color_type == PNG_COLOR_TYPE_RGB || |
| 2482 | ping_color_type == PNG_COLOR_TYPE_GRAY) && |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2483 | (ScaleQuantumToShort(q->red) == transparent_color.red && |
| 2484 | ScaleQuantumToShort(q->green) == transparent_color.green && |
| 2485 | ScaleQuantumToShort(q->blue) == transparent_color.blue)) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 2486 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2487 | if (logging != MagickFalse) |
| 2488 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2489 | " ...got one."); |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 2490 | found_transparent_pixel = MagickTrue; |
| 2491 | break; |
| 2492 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2493 | q++; |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | if ((image->previous == (Image *) NULL) && (num_passes == 1)) |
| 2498 | { |
| 2499 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 2500 | image->rows); |
| 2501 | |
| 2502 | if (status == MagickFalse) |
| 2503 | break; |
| 2504 | } |
| 2505 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | if ((image->previous == (Image *) NULL) && (num_passes != 1)) |
| 2510 | { |
| 2511 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 2512 | if (status == MagickFalse) |
| 2513 | break; |
| 2514 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2515 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2516 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2517 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2518 | else /* image->storage_class != DirectClass */ |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2519 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2520 | for (pass=0; pass < num_passes; pass++) |
| 2521 | { |
| 2522 | Quantum |
| 2523 | *quantum_scanline; |
| 2524 | |
| 2525 | register Quantum |
| 2526 | *r; |
| 2527 | |
| 2528 | /* |
| 2529 | Convert grayscale image to PseudoClass pixel packets. |
| 2530 | */ |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2531 | image->matte=ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2532 | MagickTrue : MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2533 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2534 | quantum_scanline=(Quantum *) AcquireQuantumMemory(image->columns, |
| 2535 | (image->matte ? 2 : 1)*sizeof(*quantum_scanline)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2536 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2537 | if (quantum_scanline == (Quantum *) NULL) |
| 2538 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2539 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2540 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2541 | { |
| 2542 | if (num_passes > 1) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2543 | row_offset=ping_rowbytes*y; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2544 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2545 | else |
| 2546 | row_offset=0; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2547 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2548 | png_read_row(ping,ping_pixels+row_offset,NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2549 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2550 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2551 | if (q == (PixelPacket *) NULL) |
| 2552 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2553 | |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 2554 | indexes=GetAuthenticIndexQueue(image); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2555 | p=ping_pixels+row_offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2556 | r=quantum_scanline; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2557 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2558 | switch (ping_bit_depth) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2559 | { |
| 2560 | case 1: |
| 2561 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2562 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2563 | bit; |
| 2564 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2565 | for (x=(ssize_t) image->columns-7; x > 0; x-=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2566 | { |
| 2567 | for (bit=7; bit >= 0; bit--) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2568 | *r++=ScaleCharToQuantum( |
| 2569 | (unsigned char) ((*p) & (0x01 << bit) ? 0xff : 0x00)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2570 | p++; |
| 2571 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2572 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2573 | if ((image->columns % 8) != 0) |
| 2574 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2575 | for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2576 | *r++=ScaleCharToQuantum( |
| 2577 | (unsigned char) ((*p) & (0x01 << bit) ? 0xff : 0x00)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2578 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2579 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2580 | break; |
| 2581 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2582 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2583 | case 2: |
| 2584 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2585 | for (x=(ssize_t) image->columns-3; x > 0; x-=4) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2586 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2587 | *r++=ScaleCharToQuantum((unsigned char) (0x55*(*p >> 6) & 0x03)); |
| 2588 | *r++=ScaleCharToQuantum((unsigned char) (0x55*(*p >> 4) & 0x03)); |
| 2589 | *r++=ScaleCharToQuantum((unsigned char) (0x55*(*p >> 2) & 0x03)); |
| 2590 | *r++=ScaleCharToQuantum((unsigned char) (0x55*(*p++) & 0x03)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2591 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2592 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2593 | if ((image->columns % 4) != 0) |
| 2594 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2595 | for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2596 | *r++=ScaleCharToQuantum((unsigned char) |
| 2597 | (0x55*((*p >> (i*2)) & 0x03))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2598 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2599 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2600 | break; |
| 2601 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2602 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2603 | case 4: |
| 2604 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2605 | for (x=(ssize_t) image->columns-1; x > 0; x-=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2606 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2607 | *r++=ScaleCharToQuantum((unsigned char) (0x11*(*p >> 4) & 0x0f)); |
| 2608 | *r++=ScaleCharToQuantum((unsigned char) (0x11*(*p++) & 0x0f)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2609 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2610 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2611 | if ((image->columns % 2) != 0) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2612 | *r++=ScaleCharToQuantum((unsigned char) (0x11*(*p >> 4) & 0x0f)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2613 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2614 | break; |
| 2615 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2616 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2617 | case 8: |
| 2618 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2619 | if (ping_color_type == 4) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2620 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2621 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2622 | *r++=ScaleCharToQuantum((unsigned char) *p++); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2623 | /* In image.h, OpaqueOpacity is 0 |
| 2624 | * TransparentOpacity is QuantumRange |
| 2625 | * In a PNG datastream, Opaque is QuantumRange |
| 2626 | * and Transparent is 0. |
| 2627 | */ |
| 2628 | q->opacity=ScaleCharToQuantum((unsigned char) (255-(*p++))); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2629 | if (q->opacity != OpaqueOpacity) |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 2630 | found_transparent_pixel = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2631 | q++; |
| 2632 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2633 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2634 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2635 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2636 | *r++=ScaleCharToQuantum((unsigned char) *p++); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2637 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2638 | break; |
| 2639 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2640 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2641 | case 16: |
| 2642 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2643 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2644 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2645 | #if MAGICKCORE_QUANTUM_DEPTH == 8 |
| 2646 | *r++=*p++; |
| 2647 | p++; /* skip low byte */ |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 2648 | |
| 2649 | if (ping_color_type == 4) |
| 2650 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2651 | q->opacity=(Quantum) (QuantumRange - *p++); |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 2652 | if (q->opacity != OpaqueOpacity) |
| 2653 | found_transparent_pixel = MagickTrue; |
| 2654 | p++; |
| 2655 | q++; |
| 2656 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2657 | |
| 2658 | #else /* MAGICKCORE_QUANTUM_DEPTH >= 16 */ |
| 2659 | *r++ = ScaleShortToQuantum( |
| 2660 | (unsigned short) (((*p) << 8) | (*(p+1)))); |
| 2661 | p+=2; |
| 2662 | |
| 2663 | if (ping_color_type == 4) |
| 2664 | { |
| 2665 | q->opacity = QuantumRange - ScaleShortToQuantum( |
| 2666 | (unsigned short) (((*p) << 8) | (*(p+1)))); |
| 2667 | p+=2; |
| 2668 | |
| 2669 | if (q->opacity != OpaqueOpacity) |
| 2670 | found_transparent_pixel = MagickTrue; |
| 2671 | q++; |
| 2672 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2673 | #endif |
| 2674 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2675 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2676 | break; |
| 2677 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2678 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2679 | default: |
| 2680 | break; |
| 2681 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 2682 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2683 | /* |
| 2684 | Transfer image scanline. |
| 2685 | */ |
| 2686 | r=quantum_scanline; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2687 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2688 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 80ac8b9 | 2010-05-08 13:36:57 +0000 | [diff] [blame] | 2689 | indexes[x]=(IndexPacket) (*r++); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2690 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2691 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 2692 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2693 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 2694 | if ((image->previous == (Image *) NULL) && (num_passes == 1)) |
| 2695 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2696 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 2697 | image->rows); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2698 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 2699 | if (status == MagickFalse) |
| 2700 | break; |
| 2701 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2702 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2703 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 2704 | if ((image->previous == (Image *) NULL) && (num_passes != 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2705 | { |
| 2706 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2707 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2708 | if (status == MagickFalse) |
| 2709 | break; |
| 2710 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2711 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2712 | quantum_scanline=(Quantum *) RelinquishMagickMemory(quantum_scanline); |
| 2713 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2714 | |
| 2715 | image->matte=found_transparent_pixel; |
| 2716 | |
| 2717 | if (logging != MagickFalse) |
| 2718 | { |
| 2719 | if (found_transparent_pixel != MagickFalse) |
| 2720 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2721 | " Found transparent pixel"); |
| 2722 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 2723 | { |
| 2724 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2725 | " No transparent pixel was found"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2726 | |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 2727 | ping_color_type&=0x03; |
| 2728 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 2732 | if (quantum_info != (QuantumInfo *) NULL) |
| 2733 | quantum_info=DestroyQuantumInfo(quantum_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2734 | |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 2735 | if (image->storage_class == PseudoClass) |
| 2736 | { |
cristy | aeb2cbc | 2010-05-07 13:28:58 +0000 | [diff] [blame] | 2737 | MagickBooleanType |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 2738 | matte; |
| 2739 | |
| 2740 | matte=image->matte; |
| 2741 | image->matte=MagickFalse; |
| 2742 | (void) SyncImage(image); |
cristy | aeb2cbc | 2010-05-07 13:28:58 +0000 | [diff] [blame] | 2743 | image->matte=matte; |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 2744 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2745 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2746 | png_read_end(ping,end_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2747 | |
| 2748 | if (image_info->number_scenes != 0 && mng_info->scenes_found-1 < |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2749 | (ssize_t) image_info->first_scene && image->delay != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2750 | { |
| 2751 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2752 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2753 | image->colors=2; |
| 2754 | (void) SetImageBackgroundColor(image); |
| 2755 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2756 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2757 | #endif |
| 2758 | if (logging != MagickFalse) |
| 2759 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2760 | " exit ReadOnePNGImage() early."); |
| 2761 | return(image); |
| 2762 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2763 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2764 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2765 | { |
| 2766 | ClassType |
| 2767 | storage_class; |
| 2768 | |
| 2769 | /* |
| 2770 | Image has a transparent background. |
| 2771 | */ |
| 2772 | storage_class=image->storage_class; |
| 2773 | image->matte=MagickTrue; |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 2774 | |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 2775 | /* Balfour fix from imagemagick discourse server, 5 Feb 2010 */ |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 2776 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2777 | if (storage_class == PseudoClass) |
| 2778 | { |
| 2779 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 2780 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2781 | for (x=0; x < ping_num_trans; x++) |
| 2782 | { |
| 2783 | image->colormap[x].opacity = |
| 2784 | ScaleCharToQuantum((unsigned char)(255-ping_trans_alpha[x])); |
| 2785 | } |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 2786 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2787 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2788 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 2789 | { |
| 2790 | for (x=0; x < (int) image->colors; x++) |
| 2791 | { |
| 2792 | if (ScaleQuantumToShort(image->colormap[x].red) == |
| 2793 | transparent_color.opacity) |
| 2794 | { |
| 2795 | image->colormap[x].opacity = (Quantum) TransparentOpacity; |
| 2796 | } |
| 2797 | } |
| 2798 | } |
| 2799 | (void) SyncImage(image); |
| 2800 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2801 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2802 | #if 1 /* Should have already been done above, but glennrp problem P10 |
| 2803 | * needs this. |
| 2804 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2805 | else |
| 2806 | { |
| 2807 | for (y=0; y < (ssize_t) image->rows; y++) |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 2808 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2809 | image->storage_class=storage_class; |
| 2810 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 2811 | |
| 2812 | if (q == (PixelPacket *) NULL) |
| 2813 | break; |
| 2814 | |
| 2815 | indexes=GetAuthenticIndexQueue(image); |
| 2816 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2817 | /* Caution: on a Q8 build, this does not distinguish between |
| 2818 | * 16-bit colors that differ only in the low byte |
| 2819 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2820 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 2821 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2822 | if (ScaleQuantumToShort(q->red) == transparent_color.red && |
| 2823 | ScaleQuantumToShort(q->green) == transparent_color.green && |
| 2824 | ScaleQuantumToShort(q->blue) == transparent_color.blue) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 2825 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2826 | q->opacity=(Quantum) TransparentOpacity; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 2827 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2828 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 2829 | #if 0 /* I have not found a case where this is needed. */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2830 | else |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 2831 | { |
| 2832 | q->opacity=(Quantum) OpaqueOpacity; |
| 2833 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2834 | #endif |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2835 | |
| 2836 | q++; |
| 2837 | } |
| 2838 | |
| 2839 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 2840 | break; |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 2841 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2842 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2843 | #endif |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 2844 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2845 | image->storage_class=DirectClass; |
| 2846 | } |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 2847 | |
cristy | b40fc46 | 2010-08-08 00:49:49 +0000 | [diff] [blame] | 2848 | if ((ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 2849 | (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 2850 | image->colorspace=GRAYColorspace; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2851 | |
cristy | eb3b22a | 2011-03-31 20:16:11 +0000 | [diff] [blame] | 2852 | for (j = 0; j < 2; j++) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2853 | { |
| 2854 | if (j == 0) |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 2855 | status = png_get_text(ping,ping_info,&text,&num_text) != 0 ? |
| 2856 | MagickTrue : MagickFalse; |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2857 | else |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 2858 | status = png_get_text(ping,end_info,&text,&num_text) != 0 ? |
| 2859 | MagickTrue : MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2860 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2861 | if (status != MagickFalse) |
| 2862 | for (i=0; i < (ssize_t) num_text; i++) |
| 2863 | { |
| 2864 | /* Check for a profile */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2865 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2866 | if (logging != MagickFalse) |
| 2867 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2868 | " Reading PNG text chunk"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2869 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2870 | if (memcmp(text[i].key, "Raw profile type ",17) == 0) |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 2871 | { |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2872 | (void) Magick_png_read_raw_profile(image,image_info,text,(int) i); |
| 2873 | num_raw_profiles++; |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 2874 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2875 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2876 | else |
| 2877 | { |
| 2878 | char |
| 2879 | *value; |
| 2880 | |
| 2881 | length=text[i].text_length; |
| 2882 | value=(char *) AcquireQuantumMemory(length+MaxTextExtent, |
| 2883 | sizeof(*value)); |
| 2884 | if (value == (char *) NULL) |
| 2885 | { |
| 2886 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 2887 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 2888 | image->filename); |
| 2889 | break; |
| 2890 | } |
| 2891 | *value='\0'; |
| 2892 | (void) ConcatenateMagickString(value,text[i].text,length+2); |
| 2893 | |
| 2894 | /* Don't save "density" or "units" property if we have a pHYs |
| 2895 | * chunk |
| 2896 | */ |
| 2897 | if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs) || |
| 2898 | (LocaleCompare(text[i].key,"density") != 0 && |
| 2899 | LocaleCompare(text[i].key,"units") != 0)) |
| 2900 | (void) SetImageProperty(image,text[i].key,value); |
| 2901 | |
| 2902 | if (logging != MagickFalse) |
| 2903 | { |
| 2904 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2905 | " length: %lu",(unsigned long) length); |
| 2906 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2907 | " Keyword: %s",text[i].key); |
| 2908 | } |
| 2909 | |
| 2910 | value=DestroyString(value); |
| 2911 | } |
| 2912 | } |
| 2913 | num_text_total += num_text; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2914 | } |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 2915 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2916 | #ifdef MNG_OBJECT_BUFFERS |
| 2917 | /* |
| 2918 | Store the object if necessary. |
| 2919 | */ |
| 2920 | if (object_id && !mng_info->frozen[object_id]) |
| 2921 | { |
| 2922 | if (mng_info->ob[object_id] == (MngBuffer *) NULL) |
| 2923 | { |
| 2924 | /* |
| 2925 | create a new object buffer. |
| 2926 | */ |
| 2927 | mng_info->ob[object_id]=(MngBuffer *) |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 2928 | AcquireMagickMemory(sizeof(MngBuffer)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2929 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2930 | if (mng_info->ob[object_id] != (MngBuffer *) NULL) |
| 2931 | { |
| 2932 | mng_info->ob[object_id]->image=(Image *) NULL; |
| 2933 | mng_info->ob[object_id]->reference_count=1; |
| 2934 | } |
| 2935 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 2936 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2937 | if ((mng_info->ob[object_id] == (MngBuffer *) NULL) || |
| 2938 | mng_info->ob[object_id]->frozen) |
| 2939 | { |
| 2940 | if (mng_info->ob[object_id] == (MngBuffer *) NULL) |
| 2941 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 2942 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 2943 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2944 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2945 | if (mng_info->ob[object_id]->frozen) |
| 2946 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 2947 | ResourceLimitError,"Cannot overwrite frozen MNG object buffer", |
| 2948 | "`%s'",image->filename); |
| 2949 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2950 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2951 | else |
| 2952 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2953 | |
| 2954 | if (mng_info->ob[object_id]->image != (Image *) NULL) |
| 2955 | mng_info->ob[object_id]->image=DestroyImage |
| 2956 | (mng_info->ob[object_id]->image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2957 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2958 | mng_info->ob[object_id]->image=CloneImage(image,0,0,MagickTrue, |
| 2959 | &image->exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2960 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2961 | if (mng_info->ob[object_id]->image != (Image *) NULL) |
| 2962 | mng_info->ob[object_id]->image->file=(FILE *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2963 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2964 | else |
| 2965 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 2966 | ResourceLimitError,"Cloning image for object buffer failed", |
| 2967 | "`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2968 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2969 | if (ping_width > 250000L || ping_height > 250000L) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2970 | png_error(ping,"PNG Image dimensions are too large."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2971 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2972 | mng_info->ob[object_id]->width=ping_width; |
| 2973 | mng_info->ob[object_id]->height=ping_height; |
| 2974 | mng_info->ob[object_id]->color_type=ping_color_type; |
| 2975 | mng_info->ob[object_id]->sample_depth=ping_bit_depth; |
| 2976 | mng_info->ob[object_id]->interlace_method=ping_interlace_method; |
| 2977 | mng_info->ob[object_id]->compression_method= |
| 2978 | ping_compression_method; |
| 2979 | mng_info->ob[object_id]->filter_method=ping_filter_method; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2980 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2981 | if (png_get_valid(ping,ping_info,PNG_INFO_PLTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2982 | { |
| 2983 | int |
| 2984 | number_colors; |
| 2985 | |
| 2986 | png_colorp |
| 2987 | plte; |
| 2988 | |
| 2989 | /* |
| 2990 | Copy the PLTE to the object buffer. |
| 2991 | */ |
| 2992 | png_get_PLTE(ping,ping_info,&plte,&number_colors); |
| 2993 | mng_info->ob[object_id]->plte_length=number_colors; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 2994 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2995 | for (i=0; i < number_colors; i++) |
| 2996 | { |
| 2997 | mng_info->ob[object_id]->plte[i]=plte[i]; |
| 2998 | } |
| 2999 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3000 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3001 | else |
| 3002 | mng_info->ob[object_id]->plte_length=0; |
| 3003 | } |
| 3004 | } |
| 3005 | #endif |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3006 | |
| 3007 | /* Set image->matte to MagickTrue if the input colortype supports |
| 3008 | * alpha or if a valid tRNS chunk is present, no matter whether there |
| 3009 | * is actual transparency present. |
| 3010 | */ |
| 3011 | image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) || |
| 3012 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || |
| 3013 | (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ? |
| 3014 | MagickTrue : MagickFalse; |
| 3015 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3016 | /* Set more properties for identify to retrieve */ |
| 3017 | { |
| 3018 | char |
| 3019 | msg[MaxTextExtent]; |
| 3020 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3021 | if (num_text_total != 0) |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3022 | { |
| 3023 | /* libpng doesn't tell us whether they were tEXt, zTXt, or iTXt */ |
| 3024 | (void) FormatMagickString(msg,MaxTextExtent, |
glennrp | 613276d | 2011-03-30 21:46:49 +0000 | [diff] [blame] | 3025 | "%d tEXt/zTXt/iTXt chunks were found", num_text_total); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3026 | (void) SetImageProperty(image,"PNG:text ",msg); |
| 3027 | } |
| 3028 | |
| 3029 | if (num_raw_profiles != 0) |
| 3030 | { |
| 3031 | (void) FormatMagickString(msg,MaxTextExtent, |
| 3032 | "%d were found", num_raw_profiles); |
| 3033 | (void) SetImageProperty(image,"PNG:text-encoded profiles",msg); |
| 3034 | } |
| 3035 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3036 | if (png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3037 | { |
| 3038 | (void) FormatMagickString(msg,MaxTextExtent,"%s", |
| 3039 | "chunk was found (see Chromaticity, above)"); |
| 3040 | (void) SetImageProperty(image,"PNG:cHRM ",msg); |
| 3041 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3042 | |
| 3043 | if (png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3044 | { |
| 3045 | (void) FormatMagickString(msg,MaxTextExtent,"%s", |
| 3046 | "chunk was found (see Background color, above)"); |
| 3047 | (void) SetImageProperty(image,"PNG:bKGD ",msg); |
| 3048 | } |
| 3049 | |
| 3050 | (void) FormatMagickString(msg,MaxTextExtent,"%s", |
| 3051 | "chunk was found"); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3052 | |
| 3053 | if (png_get_valid(ping,ping_info,PNG_INFO_iCCP)) |
| 3054 | (void) SetImageProperty(image,"PNG:iCCP ",msg); |
| 3055 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3056 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
| 3057 | (void) SetImageProperty(image,"PNG:tRNS ",msg); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3058 | |
| 3059 | #if defined(PNG_sRGB_SUPPORTED) |
| 3060 | if (png_get_valid(ping,ping_info,PNG_INFO_sRGB)) |
| 3061 | { |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3062 | (void) FormatMagickString(msg,MaxTextExtent, |
| 3063 | "intent=%d (See Rendering intent)", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3064 | (int) intent); |
| 3065 | (void) SetImageProperty(image,"PNG:sRGB ",msg); |
| 3066 | } |
| 3067 | #endif |
| 3068 | |
| 3069 | if (png_get_valid(ping,ping_info,PNG_INFO_gAMA)) |
| 3070 | { |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3071 | (void) FormatMagickString(msg,MaxTextExtent, |
| 3072 | "gamma=%.8g (See Gamma, above)", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3073 | file_gamma); |
| 3074 | (void) SetImageProperty(image,"PNG:gAMA ",msg); |
| 3075 | } |
| 3076 | |
| 3077 | #if defined(PNG_pHYs_SUPPORTED) |
| 3078 | if (png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
| 3079 | { |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3080 | (void) FormatMagickString(msg,MaxTextExtent, |
| 3081 | "x_res=%.10g, y_res=%.10g, units=%d", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3082 | (double) x_resolution,(double) y_resolution, unit_type); |
| 3083 | (void) SetImageProperty(image,"PNG:pHYs ",msg); |
| 3084 | } |
| 3085 | #endif |
| 3086 | |
| 3087 | #if defined(PNG_oFFs_SUPPORTED) |
| 3088 | if (png_get_valid(ping,ping_info,PNG_INFO_oFFs)) |
| 3089 | { |
| 3090 | (void) FormatMagickString(msg,MaxTextExtent,"x_off=%.20g, y_off=%.20g", |
| 3091 | (double) image->page.x,(double) image->page.y); |
| 3092 | (void) SetImageProperty(image,"PNG:oFFs ",msg); |
| 3093 | } |
| 3094 | #endif |
| 3095 | |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3096 | if ((image->page.width != 0 && image->page.width != image->columns) || |
| 3097 | (image->page.height != 0 && image->page.height != image->rows)) |
| 3098 | { |
| 3099 | (void) FormatMagickString(msg,MaxTextExtent, |
| 3100 | "width=%.20g, height=%.20g", |
| 3101 | (double) image->page.width,(double) image->page.height); |
| 3102 | (void) SetImageProperty(image,"PNG:vpAg ",msg); |
| 3103 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3106 | /* |
| 3107 | Relinquish resources. |
| 3108 | */ |
| 3109 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
| 3110 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3111 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3112 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3113 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3114 | #endif |
| 3115 | |
| 3116 | if (logging != MagickFalse) |
| 3117 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3118 | " exit ReadOnePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3119 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3120 | return(image); |
| 3121 | |
| 3122 | /* end of reading one PNG image */ |
| 3123 | } |
| 3124 | |
| 3125 | static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 3126 | { |
| 3127 | Image |
| 3128 | *image, |
| 3129 | *previous; |
| 3130 | |
| 3131 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 3132 | have_mng_structure, |
| 3133 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3134 | status; |
| 3135 | |
| 3136 | MngInfo |
| 3137 | *mng_info; |
| 3138 | |
| 3139 | char |
| 3140 | magic_number[MaxTextExtent]; |
| 3141 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3142 | ssize_t |
| 3143 | count; |
| 3144 | |
| 3145 | /* |
| 3146 | Open image file. |
| 3147 | */ |
| 3148 | assert(image_info != (const ImageInfo *) NULL); |
| 3149 | assert(image_info->signature == MagickSignature); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3150 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3151 | if (image_info->debug != MagickFalse) |
| 3152 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 3153 | image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3154 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3155 | assert(exception != (ExceptionInfo *) NULL); |
| 3156 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 3157 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadPNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3158 | image=AcquireImage(image_info); |
| 3159 | mng_info=(MngInfo *) NULL; |
| 3160 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3161 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3162 | if (status == MagickFalse) |
| 3163 | ThrowReaderException(FileOpenError,"UnableToOpenFile"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3164 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3165 | /* |
| 3166 | Verify PNG signature. |
| 3167 | */ |
| 3168 | count=ReadBlob(image,8,(unsigned char *) magic_number); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3169 | |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 3170 | if (count < 8 || memcmp(magic_number,"\211PNG\r\n\032\n",8) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3171 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3172 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3173 | /* |
| 3174 | Allocate a MngInfo structure. |
| 3175 | */ |
| 3176 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 3177 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3178 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3179 | if (mng_info == (MngInfo *) NULL) |
| 3180 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3181 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3182 | /* |
| 3183 | Initialize members of the MngInfo structure. |
| 3184 | */ |
| 3185 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 3186 | mng_info->image=image; |
| 3187 | have_mng_structure=MagickTrue; |
| 3188 | |
| 3189 | previous=image; |
| 3190 | image=ReadOnePNGImage(mng_info,image_info,exception); |
| 3191 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3192 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3193 | if (image == (Image *) NULL) |
| 3194 | { |
| 3195 | if (previous != (Image *) NULL) |
| 3196 | { |
| 3197 | if (previous->signature != MagickSignature) |
| 3198 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3199 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3200 | (void) CloseBlob(previous); |
| 3201 | (void) DestroyImageList(previous); |
| 3202 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3203 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3204 | if (logging != MagickFalse) |
| 3205 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3206 | "exit ReadPNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3207 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3208 | return((Image *) NULL); |
| 3209 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3210 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3211 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3212 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3213 | if ((image->columns == 0) || (image->rows == 0)) |
| 3214 | { |
| 3215 | if (logging != MagickFalse) |
| 3216 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3217 | "exit ReadPNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3218 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3219 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 3220 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3221 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 3222 | #if 0 /* This is probably redundant now */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3223 | if (LocaleCompare(image_info->magick,"PNG8") == 0) |
| 3224 | { |
| 3225 | (void) SetImageType(image,PaletteType); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3226 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3227 | if (image->matte != MagickFalse) |
| 3228 | { |
| 3229 | /* To do: Reduce to binary transparency */ |
| 3230 | } |
| 3231 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 3232 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3233 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3234 | if (LocaleCompare(image_info->magick,"PNG24") == 0) |
| 3235 | { |
| 3236 | (void) SetImageType(image,TrueColorType); |
| 3237 | image->matte=MagickFalse; |
| 3238 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3239 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3240 | if (LocaleCompare(image_info->magick,"PNG32") == 0) |
| 3241 | (void) SetImageType(image,TrueColorMatteType); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3242 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3243 | if (logging != MagickFalse) |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3244 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3245 | " page.w: %.20g, page.h: %.20g,page.x: %.20g, page.y: %.20g.", |
| 3246 | (double) image->page.width,(double) image->page.height, |
| 3247 | (double) image->page.x,(double) image->page.y); |
| 3248 | |
| 3249 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3250 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadPNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3251 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3252 | return(image); |
| 3253 | } |
| 3254 | |
| 3255 | |
| 3256 | |
| 3257 | #if defined(JNG_SUPPORTED) |
| 3258 | /* |
| 3259 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3260 | % % |
| 3261 | % % |
| 3262 | % % |
| 3263 | % R e a d O n e J N G I m a g e % |
| 3264 | % % |
| 3265 | % % |
| 3266 | % % |
| 3267 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3268 | % |
| 3269 | % ReadOneJNGImage() reads a JPEG Network Graphics (JNG) image file |
| 3270 | % (minus the 8-byte signature) and returns it. It allocates the memory |
| 3271 | % necessary for the new Image structure and returns a pointer to the new |
| 3272 | % image. |
| 3273 | % |
| 3274 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 3275 | % |
| 3276 | % The format of the ReadOneJNGImage method is: |
| 3277 | % |
| 3278 | % Image *ReadOneJNGImage(MngInfo *mng_info, const ImageInfo *image_info, |
| 3279 | % ExceptionInfo *exception) |
| 3280 | % |
| 3281 | % A description of each parameter follows: |
| 3282 | % |
| 3283 | % o mng_info: Specifies a pointer to a MngInfo structure. |
| 3284 | % |
| 3285 | % o image_info: the image info. |
| 3286 | % |
| 3287 | % o exception: return any errors or warnings in this structure. |
| 3288 | % |
| 3289 | */ |
| 3290 | static Image *ReadOneJNGImage(MngInfo *mng_info, |
| 3291 | const ImageInfo *image_info, ExceptionInfo *exception) |
| 3292 | { |
| 3293 | Image |
| 3294 | *alpha_image, |
| 3295 | *color_image, |
| 3296 | *image, |
| 3297 | *jng_image; |
| 3298 | |
| 3299 | ImageInfo |
| 3300 | *alpha_image_info, |
| 3301 | *color_image_info; |
| 3302 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 3303 | MagickBooleanType |
| 3304 | logging; |
| 3305 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3306 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3307 | y; |
| 3308 | |
| 3309 | MagickBooleanType |
| 3310 | status; |
| 3311 | |
| 3312 | png_uint_32 |
| 3313 | jng_height, |
| 3314 | jng_width; |
| 3315 | |
| 3316 | png_byte |
| 3317 | jng_color_type, |
| 3318 | jng_image_sample_depth, |
| 3319 | jng_image_compression_method, |
| 3320 | jng_image_interlace_method, |
| 3321 | jng_alpha_sample_depth, |
| 3322 | jng_alpha_compression_method, |
| 3323 | jng_alpha_filter_method, |
| 3324 | jng_alpha_interlace_method; |
| 3325 | |
| 3326 | register const PixelPacket |
| 3327 | *s; |
| 3328 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3329 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3330 | i, |
| 3331 | x; |
| 3332 | |
| 3333 | register PixelPacket |
| 3334 | *q; |
| 3335 | |
| 3336 | register unsigned char |
| 3337 | *p; |
| 3338 | |
| 3339 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3340 | read_JSEP, |
| 3341 | reading_idat, |
| 3342 | skip_to_iend; |
| 3343 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3344 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3345 | length; |
| 3346 | |
| 3347 | jng_alpha_compression_method=0; |
| 3348 | jng_alpha_sample_depth=8; |
| 3349 | jng_color_type=0; |
| 3350 | jng_height=0; |
| 3351 | jng_width=0; |
| 3352 | alpha_image=(Image *) NULL; |
| 3353 | color_image=(Image *) NULL; |
| 3354 | alpha_image_info=(ImageInfo *) NULL; |
| 3355 | color_image_info=(ImageInfo *) NULL; |
| 3356 | |
| 3357 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 3358 | " Enter ReadOneJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3359 | |
| 3360 | image=mng_info->image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3361 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3362 | if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL) |
| 3363 | { |
| 3364 | /* |
| 3365 | Allocate next image structure. |
| 3366 | */ |
| 3367 | if (logging != MagickFalse) |
| 3368 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3369 | " AcquireNextImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3370 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3371 | AcquireNextImage(image_info,image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3372 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3373 | if (GetNextImageInList(image) == (Image *) NULL) |
| 3374 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3375 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3376 | image=SyncNextImageInList(image); |
| 3377 | } |
| 3378 | mng_info->image=image; |
| 3379 | |
| 3380 | /* |
| 3381 | Signature bytes have already been read. |
| 3382 | */ |
| 3383 | |
| 3384 | read_JSEP=MagickFalse; |
| 3385 | reading_idat=MagickFalse; |
| 3386 | skip_to_iend=MagickFalse; |
| 3387 | for (;;) |
| 3388 | { |
| 3389 | char |
| 3390 | type[MaxTextExtent]; |
| 3391 | |
| 3392 | unsigned char |
| 3393 | *chunk; |
| 3394 | |
| 3395 | unsigned int |
| 3396 | count; |
| 3397 | |
| 3398 | /* |
| 3399 | Read a new JNG chunk. |
| 3400 | */ |
| 3401 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 3402 | 2*GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3403 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3404 | if (status == MagickFalse) |
| 3405 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3406 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3407 | type[0]='\0'; |
| 3408 | (void) ConcatenateMagickString(type,"errr",MaxTextExtent); |
| 3409 | length=ReadBlobMSBLong(image); |
| 3410 | count=(unsigned int) ReadBlob(image,4,(unsigned char *) type); |
| 3411 | |
| 3412 | if (logging != MagickFalse) |
| 3413 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 3414 | " Reading JNG chunk type %c%c%c%c, length: %.20g", |
| 3415 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3416 | |
| 3417 | if (length > PNG_UINT_31_MAX || count == 0) |
| 3418 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3419 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3420 | p=NULL; |
| 3421 | chunk=(unsigned char *) NULL; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3422 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3423 | if (length) |
| 3424 | { |
| 3425 | chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3426 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3427 | if (chunk == (unsigned char *) NULL) |
| 3428 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3429 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3430 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3431 | chunk[i]=(unsigned char) ReadBlobByte(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3432 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3433 | p=chunk; |
| 3434 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3435 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3436 | (void) ReadBlobMSBLong(image); /* read crc word */ |
| 3437 | |
| 3438 | if (skip_to_iend) |
| 3439 | { |
| 3440 | if (length) |
| 3441 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3442 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3443 | continue; |
| 3444 | } |
| 3445 | |
| 3446 | if (memcmp(type,mng_JHDR,4) == 0) |
| 3447 | { |
| 3448 | if (length == 16) |
| 3449 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3450 | jng_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3451 | (p[2] << 8) | p[3]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3452 | jng_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3453 | (p[6] << 8) | p[7]); |
| 3454 | jng_color_type=p[8]; |
| 3455 | jng_image_sample_depth=p[9]; |
| 3456 | jng_image_compression_method=p[10]; |
| 3457 | jng_image_interlace_method=p[11]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3458 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3459 | image->interlace=jng_image_interlace_method != 0 ? PNGInterlace : |
| 3460 | NoInterlace; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3461 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3462 | jng_alpha_sample_depth=p[12]; |
| 3463 | jng_alpha_compression_method=p[13]; |
| 3464 | jng_alpha_filter_method=p[14]; |
| 3465 | jng_alpha_interlace_method=p[15]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3466 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3467 | if (logging != MagickFalse) |
| 3468 | { |
| 3469 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 3470 | " jng_width: %16lu",(unsigned long) jng_width); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3471 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3472 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 3473 | " jng_width: %16lu",(unsigned long) jng_height); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3474 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3475 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3476 | " jng_color_type: %16d",jng_color_type); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3477 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3478 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3479 | " jng_image_sample_depth: %3d", |
| 3480 | jng_image_sample_depth); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3481 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3482 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3483 | " jng_image_compression_method:%3d", |
| 3484 | jng_image_compression_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3485 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3486 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3487 | " jng_image_interlace_method: %3d", |
| 3488 | jng_image_interlace_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3489 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3490 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3491 | " jng_alpha_sample_depth: %3d", |
| 3492 | jng_alpha_sample_depth); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3493 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3494 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3495 | " jng_alpha_compression_method:%3d", |
| 3496 | jng_alpha_compression_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3497 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3498 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3499 | " jng_alpha_filter_method: %3d", |
| 3500 | jng_alpha_filter_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3501 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3502 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3503 | " jng_alpha_interlace_method: %3d", |
| 3504 | jng_alpha_interlace_method); |
| 3505 | } |
| 3506 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3507 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3508 | if (length) |
| 3509 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3510 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3511 | continue; |
| 3512 | } |
| 3513 | |
| 3514 | |
| 3515 | if ((reading_idat == MagickFalse) && (read_JSEP == MagickFalse) && |
| 3516 | ((memcmp(type,mng_JDAT,4) == 0) || (memcmp(type,mng_JdAA,4) == 0) || |
| 3517 | (memcmp(type,mng_IDAT,4) == 0) || (memcmp(type,mng_JDAA,4) == 0))) |
| 3518 | { |
| 3519 | /* |
| 3520 | o create color_image |
| 3521 | o open color_blob, attached to color_image |
| 3522 | o if (color type has alpha) |
| 3523 | open alpha_blob, attached to alpha_image |
| 3524 | */ |
| 3525 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 3526 | color_image_info=(ImageInfo *)AcquireMagickMemory(sizeof(ImageInfo)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3527 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3528 | if (color_image_info == (ImageInfo *) NULL) |
| 3529 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3530 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3531 | GetImageInfo(color_image_info); |
| 3532 | color_image=AcquireImage(color_image_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3533 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3534 | if (color_image == (Image *) NULL) |
| 3535 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 3536 | |
| 3537 | if (logging != MagickFalse) |
| 3538 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3539 | " Creating color_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3540 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3541 | (void) AcquireUniqueFilename(color_image->filename); |
| 3542 | status=OpenBlob(color_image_info,color_image,WriteBinaryBlobMode, |
| 3543 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3544 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3545 | if (status == MagickFalse) |
| 3546 | return((Image *) NULL); |
| 3547 | |
| 3548 | if ((image_info->ping == MagickFalse) && (jng_color_type >= 12)) |
| 3549 | { |
| 3550 | alpha_image_info=(ImageInfo *) |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 3551 | AcquireMagickMemory(sizeof(ImageInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3552 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3553 | if (alpha_image_info == (ImageInfo *) NULL) |
| 3554 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3555 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3556 | GetImageInfo(alpha_image_info); |
| 3557 | alpha_image=AcquireImage(alpha_image_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3558 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3559 | if (alpha_image == (Image *) NULL) |
| 3560 | { |
| 3561 | alpha_image=DestroyImage(alpha_image); |
| 3562 | ThrowReaderException(ResourceLimitError, |
| 3563 | "MemoryAllocationFailed"); |
| 3564 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3565 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3566 | if (logging != MagickFalse) |
| 3567 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3568 | " Creating alpha_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3569 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3570 | (void) AcquireUniqueFilename(alpha_image->filename); |
| 3571 | status=OpenBlob(alpha_image_info,alpha_image,WriteBinaryBlobMode, |
| 3572 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3573 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3574 | if (status == MagickFalse) |
| 3575 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3576 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3577 | if (jng_alpha_compression_method == 0) |
| 3578 | { |
| 3579 | unsigned char |
| 3580 | data[18]; |
| 3581 | |
| 3582 | if (logging != MagickFalse) |
| 3583 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3584 | " Writing IHDR chunk to alpha_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3585 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3586 | (void) WriteBlob(alpha_image,8,(const unsigned char *) |
| 3587 | "\211PNG\r\n\032\n"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3588 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3589 | (void) WriteBlobMSBULong(alpha_image,13L); |
| 3590 | PNGType(data,mng_IHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 3591 | LogPNGChunk(logging,mng_IHDR,13L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3592 | PNGLong(data+4,jng_width); |
| 3593 | PNGLong(data+8,jng_height); |
| 3594 | data[12]=jng_alpha_sample_depth; |
| 3595 | data[13]=0; /* color_type gray */ |
| 3596 | data[14]=0; /* compression method 0 */ |
| 3597 | data[15]=0; /* filter_method 0 */ |
| 3598 | data[16]=0; /* interlace_method 0 */ |
| 3599 | (void) WriteBlob(alpha_image,17,data); |
| 3600 | (void) WriteBlobMSBULong(alpha_image,crc32(0,data,17)); |
| 3601 | } |
| 3602 | } |
| 3603 | reading_idat=MagickTrue; |
| 3604 | } |
| 3605 | |
| 3606 | if (memcmp(type,mng_JDAT,4) == 0) |
| 3607 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3608 | /* Copy chunk to color_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3609 | |
| 3610 | if (logging != MagickFalse) |
| 3611 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3612 | " Copying JDAT chunk data to color_blob."); |
| 3613 | |
| 3614 | (void) WriteBlob(color_image,length,chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3615 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3616 | if (length) |
| 3617 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3618 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3619 | continue; |
| 3620 | } |
| 3621 | |
| 3622 | if (memcmp(type,mng_IDAT,4) == 0) |
| 3623 | { |
| 3624 | png_byte |
| 3625 | data[5]; |
| 3626 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3627 | /* Copy IDAT header and chunk data to alpha_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3628 | |
| 3629 | if (image_info->ping == MagickFalse) |
| 3630 | { |
| 3631 | if (logging != MagickFalse) |
| 3632 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3633 | " Copying IDAT chunk data to alpha_blob."); |
| 3634 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3635 | (void) WriteBlobMSBULong(alpha_image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3636 | PNGType(data,mng_IDAT); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 3637 | LogPNGChunk(logging,mng_IDAT,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3638 | (void) WriteBlob(alpha_image,4,data); |
| 3639 | (void) WriteBlob(alpha_image,length,chunk); |
| 3640 | (void) WriteBlobMSBULong(alpha_image, |
| 3641 | crc32(crc32(0,data,4),chunk,(uInt) length)); |
| 3642 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3643 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3644 | if (length) |
| 3645 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3646 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3647 | continue; |
| 3648 | } |
| 3649 | |
| 3650 | if ((memcmp(type,mng_JDAA,4) == 0) || (memcmp(type,mng_JdAA,4) == 0)) |
| 3651 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3652 | /* Copy chunk data to alpha_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3653 | |
| 3654 | if (image_info->ping == MagickFalse) |
| 3655 | { |
| 3656 | if (logging != MagickFalse) |
| 3657 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3658 | " Copying JDAA chunk data to alpha_blob."); |
| 3659 | |
| 3660 | (void) WriteBlob(alpha_image,length,chunk); |
| 3661 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3662 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3663 | if (length) |
| 3664 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3665 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3666 | continue; |
| 3667 | } |
| 3668 | |
| 3669 | if (memcmp(type,mng_JSEP,4) == 0) |
| 3670 | { |
| 3671 | read_JSEP=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3672 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3673 | if (length) |
| 3674 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3675 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3676 | continue; |
| 3677 | } |
| 3678 | |
| 3679 | if (memcmp(type,mng_bKGD,4) == 0) |
| 3680 | { |
| 3681 | if (length == 2) |
| 3682 | { |
| 3683 | image->background_color.red=ScaleCharToQuantum(p[1]); |
| 3684 | image->background_color.green=image->background_color.red; |
| 3685 | image->background_color.blue=image->background_color.red; |
| 3686 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3687 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3688 | if (length == 6) |
| 3689 | { |
| 3690 | image->background_color.red=ScaleCharToQuantum(p[1]); |
| 3691 | image->background_color.green=ScaleCharToQuantum(p[3]); |
| 3692 | image->background_color.blue=ScaleCharToQuantum(p[5]); |
| 3693 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3694 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3695 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 3696 | continue; |
| 3697 | } |
| 3698 | |
| 3699 | if (memcmp(type,mng_gAMA,4) == 0) |
| 3700 | { |
| 3701 | if (length == 4) |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 3702 | image->gamma=((float) mng_get_long(p))*0.00001; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3703 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3704 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 3705 | continue; |
| 3706 | } |
| 3707 | |
| 3708 | if (memcmp(type,mng_cHRM,4) == 0) |
| 3709 | { |
| 3710 | if (length == 32) |
| 3711 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 3712 | image->chromaticity.white_point.x=0.00001*mng_get_long(p); |
| 3713 | image->chromaticity.white_point.y=0.00001*mng_get_long(&p[4]); |
| 3714 | image->chromaticity.red_primary.x=0.00001*mng_get_long(&p[8]); |
| 3715 | image->chromaticity.red_primary.y=0.00001*mng_get_long(&p[12]); |
| 3716 | image->chromaticity.green_primary.x=0.00001*mng_get_long(&p[16]); |
| 3717 | image->chromaticity.green_primary.y=0.00001*mng_get_long(&p[20]); |
| 3718 | image->chromaticity.blue_primary.x=0.00001*mng_get_long(&p[24]); |
| 3719 | image->chromaticity.blue_primary.y=0.00001*mng_get_long(&p[28]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3720 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3721 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3722 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 3723 | continue; |
| 3724 | } |
| 3725 | |
| 3726 | if (memcmp(type,mng_sRGB,4) == 0) |
| 3727 | { |
| 3728 | if (length == 1) |
| 3729 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 3730 | image->rendering_intent= |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3731 | Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3732 | image->gamma=0.45455f; |
| 3733 | image->chromaticity.red_primary.x=0.6400f; |
| 3734 | image->chromaticity.red_primary.y=0.3300f; |
| 3735 | image->chromaticity.green_primary.x=0.3000f; |
| 3736 | image->chromaticity.green_primary.y=0.6000f; |
| 3737 | image->chromaticity.blue_primary.x=0.1500f; |
| 3738 | image->chromaticity.blue_primary.y=0.0600f; |
| 3739 | image->chromaticity.white_point.x=0.3127f; |
| 3740 | image->chromaticity.white_point.y=0.3290f; |
| 3741 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3742 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3743 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 3744 | continue; |
| 3745 | } |
| 3746 | |
| 3747 | if (memcmp(type,mng_oFFs,4) == 0) |
| 3748 | { |
| 3749 | if (length > 8) |
| 3750 | { |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 3751 | image->page.x=(ssize_t) mng_get_long(p); |
| 3752 | image->page.y=(ssize_t) mng_get_long(&p[4]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3753 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3754 | if ((int) p[8] != 0) |
| 3755 | { |
| 3756 | image->page.x/=10000; |
| 3757 | image->page.y/=10000; |
| 3758 | } |
| 3759 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3760 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3761 | if (length) |
| 3762 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3763 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3764 | continue; |
| 3765 | } |
| 3766 | |
| 3767 | if (memcmp(type,mng_pHYs,4) == 0) |
| 3768 | { |
| 3769 | if (length > 8) |
| 3770 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 3771 | image->x_resolution=(double) mng_get_long(p); |
| 3772 | image->y_resolution=(double) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3773 | if ((int) p[8] == PNG_RESOLUTION_METER) |
| 3774 | { |
| 3775 | image->units=PixelsPerCentimeterResolution; |
| 3776 | image->x_resolution=image->x_resolution/100.0f; |
| 3777 | image->y_resolution=image->y_resolution/100.0f; |
| 3778 | } |
| 3779 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3780 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3781 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 3782 | continue; |
| 3783 | } |
| 3784 | |
| 3785 | #if 0 |
| 3786 | if (memcmp(type,mng_iCCP,4) == 0) |
| 3787 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 3788 | /* To do: */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3789 | if (length) |
| 3790 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3791 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3792 | continue; |
| 3793 | } |
| 3794 | #endif |
| 3795 | |
| 3796 | if (length) |
| 3797 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 3798 | |
| 3799 | if (memcmp(type,mng_IEND,4)) |
| 3800 | continue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3801 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3802 | break; |
| 3803 | } |
| 3804 | |
| 3805 | |
| 3806 | /* IEND found */ |
| 3807 | |
| 3808 | /* |
| 3809 | Finish up reading image data: |
| 3810 | |
| 3811 | o read main image from color_blob. |
| 3812 | |
| 3813 | o close color_blob. |
| 3814 | |
| 3815 | o if (color_type has alpha) |
| 3816 | if alpha_encoding is PNG |
| 3817 | read secondary image from alpha_blob via ReadPNG |
| 3818 | if alpha_encoding is JPEG |
| 3819 | read secondary image from alpha_blob via ReadJPEG |
| 3820 | |
| 3821 | o close alpha_blob. |
| 3822 | |
| 3823 | o copy intensity of secondary image into |
| 3824 | opacity samples of main image. |
| 3825 | |
| 3826 | o destroy the secondary image. |
| 3827 | */ |
| 3828 | |
| 3829 | (void) CloseBlob(color_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3830 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3831 | if (logging != MagickFalse) |
| 3832 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3833 | " Reading jng_image from color_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3834 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3835 | (void) FormatMagickString(color_image_info->filename,MaxTextExtent,"%s", |
| 3836 | color_image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3837 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3838 | color_image_info->ping=MagickFalse; /* To do: avoid this */ |
| 3839 | jng_image=ReadImage(color_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3840 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3841 | if (jng_image == (Image *) NULL) |
| 3842 | return((Image *) NULL); |
| 3843 | |
| 3844 | (void) RelinquishUniqueFileResource(color_image->filename); |
| 3845 | color_image=DestroyImage(color_image); |
| 3846 | color_image_info=DestroyImageInfo(color_image_info); |
| 3847 | |
| 3848 | if (jng_image == (Image *) NULL) |
| 3849 | return((Image *) NULL); |
| 3850 | |
| 3851 | if (logging != MagickFalse) |
| 3852 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3853 | " Copying jng_image pixels to main image."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3854 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3855 | image->rows=jng_height; |
| 3856 | image->columns=jng_width; |
| 3857 | length=image->columns*sizeof(PixelPacket); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3858 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3859 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3860 | { |
| 3861 | s=GetVirtualPixels(jng_image,0,y,image->columns,1,&image->exception); |
| 3862 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 3863 | (void) CopyMagickMemory(q,s,length); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3864 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3865 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3866 | break; |
| 3867 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3868 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3869 | jng_image=DestroyImage(jng_image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3870 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3871 | if (image_info->ping == MagickFalse) |
| 3872 | { |
| 3873 | if (jng_color_type >= 12) |
| 3874 | { |
| 3875 | if (jng_alpha_compression_method == 0) |
| 3876 | { |
| 3877 | png_byte |
| 3878 | data[5]; |
| 3879 | (void) WriteBlobMSBULong(alpha_image,0x00000000L); |
| 3880 | PNGType(data,mng_IEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 3881 | LogPNGChunk(logging,mng_IEND,0L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3882 | (void) WriteBlob(alpha_image,4,data); |
| 3883 | (void) WriteBlobMSBULong(alpha_image,crc32(0,data,4)); |
| 3884 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3885 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3886 | (void) CloseBlob(alpha_image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3887 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3888 | if (logging != MagickFalse) |
| 3889 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3890 | " Reading opacity from alpha_blob."); |
| 3891 | |
| 3892 | (void) FormatMagickString(alpha_image_info->filename,MaxTextExtent, |
| 3893 | "%s",alpha_image->filename); |
| 3894 | |
| 3895 | jng_image=ReadImage(alpha_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3896 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3897 | if (jng_image != (Image *) NULL) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3898 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3899 | { |
| 3900 | s=GetVirtualPixels(jng_image,0,y,image->columns,1, |
| 3901 | &image->exception); |
| 3902 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3903 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3904 | if (image->matte != MagickFalse) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3905 | for (x=(ssize_t) image->columns; x != 0; x--,q++,s++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3906 | q->opacity=(Quantum) QuantumRange-s->red; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3907 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3908 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3909 | for (x=(ssize_t) image->columns; x != 0; x--,q++,s++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3910 | { |
| 3911 | q->opacity=(Quantum) QuantumRange-s->red; |
| 3912 | if (q->opacity != OpaqueOpacity) |
| 3913 | image->matte=MagickTrue; |
| 3914 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3915 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3916 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3917 | break; |
| 3918 | } |
| 3919 | (void) RelinquishUniqueFileResource(alpha_image->filename); |
| 3920 | alpha_image=DestroyImage(alpha_image); |
| 3921 | alpha_image_info=DestroyImageInfo(alpha_image_info); |
| 3922 | if (jng_image != (Image *) NULL) |
| 3923 | jng_image=DestroyImage(jng_image); |
| 3924 | } |
| 3925 | } |
| 3926 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3927 | /* Read the JNG image. */ |
| 3928 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3929 | if (mng_info->mng_type == 0) |
| 3930 | { |
| 3931 | mng_info->mng_width=jng_width; |
| 3932 | mng_info->mng_height=jng_height; |
| 3933 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3934 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3935 | if (image->page.width == 0 && image->page.height == 0) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3936 | { |
| 3937 | image->page.width=jng_width; |
| 3938 | image->page.height=jng_height; |
| 3939 | } |
| 3940 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3941 | if (image->page.x == 0 && image->page.y == 0) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3942 | { |
| 3943 | image->page.x=mng_info->x_off[mng_info->object_id]; |
| 3944 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 3945 | } |
| 3946 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3947 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3948 | { |
| 3949 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 3950 | } |
| 3951 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3952 | mng_info->image_found++; |
| 3953 | status=SetImageProgress(image,LoadImagesTag,2*TellBlob(image), |
| 3954 | 2*GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3955 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3956 | if (logging != MagickFalse) |
| 3957 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3958 | " exit ReadOneJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3959 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3960 | return(image); |
| 3961 | } |
| 3962 | |
| 3963 | /* |
| 3964 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3965 | % % |
| 3966 | % % |
| 3967 | % % |
| 3968 | % R e a d J N G I m a g e % |
| 3969 | % % |
| 3970 | % % |
| 3971 | % % |
| 3972 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3973 | % |
| 3974 | % ReadJNGImage() reads a JPEG Network Graphics (JNG) image file |
| 3975 | % (including the 8-byte signature) and returns it. It allocates the memory |
| 3976 | % necessary for the new Image structure and returns a pointer to the new |
| 3977 | % image. |
| 3978 | % |
| 3979 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 3980 | % |
| 3981 | % The format of the ReadJNGImage method is: |
| 3982 | % |
| 3983 | % Image *ReadJNGImage(const ImageInfo *image_info, ExceptionInfo |
| 3984 | % *exception) |
| 3985 | % |
| 3986 | % A description of each parameter follows: |
| 3987 | % |
| 3988 | % o image_info: the image info. |
| 3989 | % |
| 3990 | % o exception: return any errors or warnings in this structure. |
| 3991 | % |
| 3992 | */ |
| 3993 | |
| 3994 | static Image *ReadJNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 3995 | { |
| 3996 | Image |
| 3997 | *image, |
| 3998 | *previous; |
| 3999 | |
| 4000 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4001 | have_mng_structure, |
| 4002 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4003 | status; |
| 4004 | |
| 4005 | MngInfo |
| 4006 | *mng_info; |
| 4007 | |
| 4008 | char |
| 4009 | magic_number[MaxTextExtent]; |
| 4010 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4011 | size_t |
| 4012 | count; |
| 4013 | |
| 4014 | /* |
| 4015 | Open image file. |
| 4016 | */ |
| 4017 | assert(image_info != (const ImageInfo *) NULL); |
| 4018 | assert(image_info->signature == MagickSignature); |
| 4019 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename); |
| 4020 | assert(exception != (ExceptionInfo *) NULL); |
| 4021 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4022 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4023 | image=AcquireImage(image_info); |
| 4024 | mng_info=(MngInfo *) NULL; |
| 4025 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4026 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4027 | if (status == MagickFalse) |
| 4028 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4029 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4030 | if (LocaleCompare(image_info->magick,"JNG") != 0) |
| 4031 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4032 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4033 | /* Verify JNG signature. */ |
| 4034 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4035 | count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4036 | |
glennrp | 3b8763e | 2011-02-21 12:08:18 +0000 | [diff] [blame] | 4037 | if (count < 8 || memcmp(magic_number,"\213JNG\r\n\032\n",8) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4038 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4039 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4040 | /* Allocate a MngInfo structure. */ |
| 4041 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4042 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4043 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(*mng_info)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4044 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4045 | if (mng_info == (MngInfo *) NULL) |
| 4046 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4047 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4048 | /* Initialize members of the MngInfo structure. */ |
| 4049 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4050 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 4051 | have_mng_structure=MagickTrue; |
| 4052 | |
| 4053 | mng_info->image=image; |
| 4054 | previous=image; |
| 4055 | image=ReadOneJNGImage(mng_info,image_info,exception); |
| 4056 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4057 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4058 | if (image == (Image *) NULL) |
| 4059 | { |
| 4060 | if (IsImageObject(previous) != MagickFalse) |
| 4061 | { |
| 4062 | (void) CloseBlob(previous); |
| 4063 | (void) DestroyImageList(previous); |
| 4064 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4065 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4066 | if (logging != MagickFalse) |
| 4067 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4068 | "exit ReadJNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4069 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4070 | return((Image *) NULL); |
| 4071 | } |
| 4072 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4073 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4074 | if (image->columns == 0 || image->rows == 0) |
| 4075 | { |
| 4076 | if (logging != MagickFalse) |
| 4077 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4078 | "exit ReadJNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4079 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4080 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 4081 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4082 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4083 | if (logging != MagickFalse) |
| 4084 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4085 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4086 | return(image); |
| 4087 | } |
| 4088 | #endif |
| 4089 | |
| 4090 | static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 4091 | { |
| 4092 | char |
| 4093 | page_geometry[MaxTextExtent]; |
| 4094 | |
| 4095 | Image |
| 4096 | *image, |
| 4097 | *previous; |
| 4098 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4099 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4100 | logging, |
| 4101 | have_mng_structure; |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4102 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4103 | volatile int |
| 4104 | first_mng_object, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4105 | object_id, |
| 4106 | term_chunk_found, |
| 4107 | skip_to_iend; |
| 4108 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4109 | volatile ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4110 | image_count=0; |
| 4111 | |
| 4112 | MagickBooleanType |
| 4113 | status; |
| 4114 | |
| 4115 | MagickOffsetType |
| 4116 | offset; |
| 4117 | |
| 4118 | MngInfo |
| 4119 | *mng_info; |
| 4120 | |
| 4121 | MngBox |
| 4122 | default_fb, |
| 4123 | fb, |
| 4124 | previous_fb; |
| 4125 | |
| 4126 | #if defined(MNG_INSERT_LAYERS) |
| 4127 | PixelPacket |
| 4128 | mng_background_color; |
| 4129 | #endif |
| 4130 | |
| 4131 | register unsigned char |
| 4132 | *p; |
| 4133 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4134 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4135 | i; |
| 4136 | |
| 4137 | size_t |
| 4138 | count; |
| 4139 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4140 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4141 | loop_level; |
| 4142 | |
| 4143 | volatile short |
| 4144 | skipping_loop; |
| 4145 | |
| 4146 | #if defined(MNG_INSERT_LAYERS) |
| 4147 | unsigned int |
| 4148 | mandatory_back=0; |
| 4149 | #endif |
| 4150 | |
| 4151 | volatile unsigned int |
| 4152 | #ifdef MNG_OBJECT_BUFFERS |
| 4153 | mng_background_object=0, |
| 4154 | #endif |
| 4155 | mng_type=0; /* 0: PNG or JNG; 1: MNG; 2: MNG-LC; 3: MNG-VLC */ |
| 4156 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4157 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4158 | default_frame_timeout, |
| 4159 | frame_timeout, |
| 4160 | #if defined(MNG_INSERT_LAYERS) |
| 4161 | image_height, |
| 4162 | image_width, |
| 4163 | #endif |
| 4164 | length; |
| 4165 | |
glennrp | 38ea083 | 2010-06-02 18:50:28 +0000 | [diff] [blame] | 4166 | /* These delays are all measured in image ticks_per_second, |
| 4167 | * not in MNG ticks_per_second |
| 4168 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4169 | volatile size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4170 | default_frame_delay, |
| 4171 | final_delay, |
| 4172 | final_image_delay, |
| 4173 | frame_delay, |
| 4174 | #if defined(MNG_INSERT_LAYERS) |
| 4175 | insert_layers, |
| 4176 | #endif |
| 4177 | mng_iterations=1, |
| 4178 | simplicity=0, |
| 4179 | subframe_height=0, |
| 4180 | subframe_width=0; |
| 4181 | |
| 4182 | previous_fb.top=0; |
| 4183 | previous_fb.bottom=0; |
| 4184 | previous_fb.left=0; |
| 4185 | previous_fb.right=0; |
| 4186 | default_fb.top=0; |
| 4187 | default_fb.bottom=0; |
| 4188 | default_fb.left=0; |
| 4189 | default_fb.right=0; |
| 4190 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4191 | /* Open image file. */ |
| 4192 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4193 | assert(image_info != (const ImageInfo *) NULL); |
| 4194 | assert(image_info->signature == MagickSignature); |
| 4195 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename); |
| 4196 | assert(exception != (ExceptionInfo *) NULL); |
| 4197 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4198 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4199 | image=AcquireImage(image_info); |
| 4200 | mng_info=(MngInfo *) NULL; |
| 4201 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4202 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4203 | if (status == MagickFalse) |
| 4204 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4205 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4206 | first_mng_object=MagickFalse; |
| 4207 | skipping_loop=(-1); |
| 4208 | have_mng_structure=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4209 | |
| 4210 | /* Allocate a MngInfo structure. */ |
| 4211 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4212 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4213 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4214 | if (mng_info == (MngInfo *) NULL) |
| 4215 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4216 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4217 | /* Initialize members of the MngInfo structure. */ |
| 4218 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4219 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 4220 | mng_info->image=image; |
| 4221 | have_mng_structure=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4222 | |
| 4223 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 4224 | { |
| 4225 | char |
| 4226 | magic_number[MaxTextExtent]; |
| 4227 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4228 | /* Verify MNG signature. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4229 | count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number); |
| 4230 | if (memcmp(magic_number,"\212MNG\r\n\032\n",8) != 0) |
| 4231 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4232 | |
| 4233 | /* Initialize some nonzero members of the MngInfo structure. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4234 | for (i=0; i < MNG_MAX_OBJECTS; i++) |
| 4235 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4236 | mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX; |
| 4237 | mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4238 | } |
| 4239 | mng_info->exists[0]=MagickTrue; |
| 4240 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4241 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4242 | first_mng_object=MagickTrue; |
| 4243 | mng_type=0; |
| 4244 | #if defined(MNG_INSERT_LAYERS) |
| 4245 | insert_layers=MagickFalse; /* should be False when converting or mogrifying */ |
| 4246 | #endif |
| 4247 | default_frame_delay=0; |
| 4248 | default_frame_timeout=0; |
| 4249 | frame_delay=0; |
| 4250 | final_delay=1; |
| 4251 | mng_info->ticks_per_second=1UL*image->ticks_per_second; |
| 4252 | object_id=0; |
| 4253 | skip_to_iend=MagickFalse; |
| 4254 | term_chunk_found=MagickFalse; |
| 4255 | mng_info->framing_mode=1; |
| 4256 | #if defined(MNG_INSERT_LAYERS) |
| 4257 | mandatory_back=MagickFalse; |
| 4258 | #endif |
| 4259 | #if defined(MNG_INSERT_LAYERS) |
| 4260 | mng_background_color=image->background_color; |
| 4261 | #endif |
| 4262 | default_fb=mng_info->frame; |
| 4263 | previous_fb=mng_info->frame; |
| 4264 | do |
| 4265 | { |
| 4266 | char |
| 4267 | type[MaxTextExtent]; |
| 4268 | |
| 4269 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 4270 | { |
| 4271 | unsigned char |
| 4272 | *chunk; |
| 4273 | |
| 4274 | /* |
| 4275 | Read a new chunk. |
| 4276 | */ |
| 4277 | type[0]='\0'; |
| 4278 | (void) ConcatenateMagickString(type,"errr",MaxTextExtent); |
| 4279 | length=ReadBlobMSBLong(image); |
| 4280 | count=(size_t) ReadBlob(image,4,(unsigned char *) type); |
| 4281 | |
| 4282 | if (logging != MagickFalse) |
| 4283 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4284 | " Reading MNG chunk type %c%c%c%c, length: %.20g", |
| 4285 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4286 | |
| 4287 | if (length > PNG_UINT_31_MAX) |
| 4288 | status=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4289 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4290 | if (count == 0) |
| 4291 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4292 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4293 | p=NULL; |
| 4294 | chunk=(unsigned char *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4295 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4296 | if (length) |
| 4297 | { |
| 4298 | chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4299 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4300 | if (chunk == (unsigned char *) NULL) |
| 4301 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4302 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4303 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4304 | chunk[i]=(unsigned char) ReadBlobByte(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4305 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4306 | p=chunk; |
| 4307 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4308 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4309 | (void) ReadBlobMSBLong(image); /* read crc word */ |
| 4310 | |
| 4311 | #if !defined(JNG_SUPPORTED) |
| 4312 | if (memcmp(type,mng_JHDR,4) == 0) |
| 4313 | { |
| 4314 | skip_to_iend=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4315 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4316 | if (mng_info->jhdr_warning == 0) |
| 4317 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 4318 | CoderError,"JNGCompressNotSupported","`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4319 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4320 | mng_info->jhdr_warning++; |
| 4321 | } |
| 4322 | #endif |
| 4323 | if (memcmp(type,mng_DHDR,4) == 0) |
| 4324 | { |
| 4325 | skip_to_iend=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4326 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4327 | if (mng_info->dhdr_warning == 0) |
| 4328 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 4329 | CoderError,"DeltaPNGNotSupported","`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4330 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4331 | mng_info->dhdr_warning++; |
| 4332 | } |
| 4333 | if (memcmp(type,mng_MEND,4) == 0) |
| 4334 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4335 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4336 | if (skip_to_iend) |
| 4337 | { |
| 4338 | if (memcmp(type,mng_IEND,4) == 0) |
| 4339 | skip_to_iend=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4340 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4341 | if (length) |
| 4342 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4343 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4344 | if (logging != MagickFalse) |
| 4345 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4346 | " Skip to IEND."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4347 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4348 | continue; |
| 4349 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4350 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4351 | if (memcmp(type,mng_MHDR,4) == 0) |
| 4352 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4353 | mng_info->mng_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4354 | (p[2] << 8) | p[3]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4355 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4356 | mng_info->mng_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4357 | (p[6] << 8) | p[7]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4358 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4359 | if (logging != MagickFalse) |
| 4360 | { |
| 4361 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4362 | " MNG width: %.20g",(double) mng_info->mng_width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4363 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4364 | " MNG height: %.20g",(double) mng_info->mng_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4365 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4366 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4367 | p+=8; |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4368 | mng_info->ticks_per_second=(size_t) mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4369 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4370 | if (mng_info->ticks_per_second == 0) |
| 4371 | default_frame_delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4372 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4373 | else |
| 4374 | default_frame_delay=1UL*image->ticks_per_second/ |
| 4375 | mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4376 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4377 | frame_delay=default_frame_delay; |
| 4378 | simplicity=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4379 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4380 | if (length > 16) |
| 4381 | { |
| 4382 | p+=16; |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4383 | simplicity=(size_t) mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4384 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4385 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4386 | mng_type=1; /* Full MNG */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4387 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4388 | if ((simplicity != 0) && ((simplicity | 11) == 11)) |
| 4389 | mng_type=2; /* LC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4390 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4391 | if ((simplicity != 0) && ((simplicity | 9) == 9)) |
| 4392 | mng_type=3; /* VLC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4393 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4394 | #if defined(MNG_INSERT_LAYERS) |
| 4395 | if (mng_type != 3) |
| 4396 | insert_layers=MagickTrue; |
| 4397 | #endif |
| 4398 | if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL) |
| 4399 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4400 | /* Allocate next image structure. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4401 | AcquireNextImage(image_info,image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4402 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4403 | if (GetNextImageInList(image) == (Image *) NULL) |
| 4404 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4405 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4406 | image=SyncNextImageInList(image); |
| 4407 | mng_info->image=image; |
| 4408 | } |
| 4409 | |
| 4410 | if ((mng_info->mng_width > 65535L) || |
| 4411 | (mng_info->mng_height > 65535L)) |
| 4412 | ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4413 | |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4414 | (void) FormatMagickString(page_geometry,MaxTextExtent, |
| 4415 | "%.20gx%.20g+0+0",(double) mng_info->mng_width,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4416 | mng_info->mng_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4417 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4418 | mng_info->frame.left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4419 | mng_info->frame.right=(ssize_t) mng_info->mng_width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4420 | mng_info->frame.top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4421 | mng_info->frame.bottom=(ssize_t) mng_info->mng_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4422 | mng_info->clip=default_fb=previous_fb=mng_info->frame; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4423 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4424 | for (i=0; i < MNG_MAX_OBJECTS; i++) |
| 4425 | mng_info->object_clip[i]=mng_info->frame; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4426 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4427 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4428 | continue; |
| 4429 | } |
| 4430 | |
| 4431 | if (memcmp(type,mng_TERM,4) == 0) |
| 4432 | { |
| 4433 | int |
| 4434 | repeat=0; |
| 4435 | |
| 4436 | |
| 4437 | if (length) |
| 4438 | repeat=p[0]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4439 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4440 | if (repeat == 3) |
| 4441 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4442 | final_delay=(png_uint_32) mng_get_long(&p[2]); |
| 4443 | mng_iterations=(png_uint_32) mng_get_long(&p[6]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4444 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4445 | if (mng_iterations == PNG_UINT_31_MAX) |
| 4446 | mng_iterations=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4447 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4448 | image->iterations=mng_iterations; |
| 4449 | term_chunk_found=MagickTrue; |
| 4450 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4451 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4452 | if (logging != MagickFalse) |
| 4453 | { |
| 4454 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4455 | " repeat=%d",repeat); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4456 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4457 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4458 | " final_delay=%.20g",(double) final_delay); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4459 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4460 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4461 | " image->iterations=%.20g",(double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4462 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4463 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4464 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4465 | continue; |
| 4466 | } |
| 4467 | if (memcmp(type,mng_DEFI,4) == 0) |
| 4468 | { |
| 4469 | if (mng_type == 3) |
| 4470 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 4471 | CoderError,"DEFI chunk found in MNG-VLC datastream","`%s'", |
| 4472 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4473 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4474 | object_id=(p[0] << 8) | p[1]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4475 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4476 | if (mng_type == 2 && object_id != 0) |
| 4477 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 4478 | CoderError,"Nonzero object_id in MNG-LC datastream","`%s'", |
| 4479 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4480 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4481 | if (object_id > MNG_MAX_OBJECTS) |
| 4482 | { |
| 4483 | /* |
| 4484 | Instead ofsuing a warning we should allocate a larger |
| 4485 | MngInfo structure and continue. |
| 4486 | */ |
| 4487 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 4488 | CoderError,"object id too large","`%s'",image->filename); |
| 4489 | object_id=MNG_MAX_OBJECTS; |
| 4490 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4491 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4492 | if (mng_info->exists[object_id]) |
| 4493 | if (mng_info->frozen[object_id]) |
| 4494 | { |
| 4495 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4496 | (void) ThrowMagickException(&image->exception, |
| 4497 | GetMagickModule(),CoderError, |
| 4498 | "DEFI cannot redefine a frozen MNG object","`%s'", |
| 4499 | image->filename); |
| 4500 | continue; |
| 4501 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4502 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4503 | mng_info->exists[object_id]=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4504 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4505 | if (length > 2) |
| 4506 | mng_info->invisible[object_id]=p[2]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4507 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4508 | /* |
| 4509 | Extract object offset info. |
| 4510 | */ |
| 4511 | if (length > 11) |
| 4512 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4513 | mng_info->x_off[object_id]=(ssize_t) ((p[4] << 24) | |
| 4514 | (p[5] << 16) | (p[6] << 8) | p[7]); |
| 4515 | |
| 4516 | mng_info->y_off[object_id]=(ssize_t) ((p[8] << 24) | |
| 4517 | (p[9] << 16) | (p[10] << 8) | p[11]); |
| 4518 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4519 | if (logging != MagickFalse) |
| 4520 | { |
| 4521 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4522 | " x_off[%d]: %.20g",object_id,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4523 | mng_info->x_off[object_id]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4524 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4525 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4526 | " y_off[%d]: %.20g",object_id,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4527 | mng_info->y_off[object_id]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4528 | } |
| 4529 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4530 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4531 | /* |
| 4532 | Extract object clipping info. |
| 4533 | */ |
| 4534 | if (length > 27) |
| 4535 | mng_info->object_clip[object_id]=mng_read_box(mng_info->frame,0, |
| 4536 | &p[12]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4538 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4539 | continue; |
| 4540 | } |
| 4541 | if (memcmp(type,mng_bKGD,4) == 0) |
| 4542 | { |
| 4543 | mng_info->have_global_bkgd=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4544 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4545 | if (length > 5) |
| 4546 | { |
| 4547 | mng_info->mng_global_bkgd.red= |
| 4548 | ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4549 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4550 | mng_info->mng_global_bkgd.green= |
| 4551 | ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4552 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4553 | mng_info->mng_global_bkgd.blue= |
| 4554 | ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4555 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4556 | mng_info->have_global_bkgd=MagickTrue; |
| 4557 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4558 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4559 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4560 | continue; |
| 4561 | } |
| 4562 | if (memcmp(type,mng_BACK,4) == 0) |
| 4563 | { |
| 4564 | #if defined(MNG_INSERT_LAYERS) |
| 4565 | if (length > 6) |
| 4566 | mandatory_back=p[6]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4567 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4568 | else |
| 4569 | mandatory_back=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4570 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4571 | if (mandatory_back && length > 5) |
| 4572 | { |
| 4573 | mng_background_color.red= |
| 4574 | ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4575 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4576 | mng_background_color.green= |
| 4577 | ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4578 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4579 | mng_background_color.blue= |
| 4580 | ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4581 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4582 | mng_background_color.opacity=OpaqueOpacity; |
| 4583 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4584 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4585 | #ifdef MNG_OBJECT_BUFFERS |
| 4586 | if (length > 8) |
| 4587 | mng_background_object=(p[7] << 8) | p[8]; |
| 4588 | #endif |
| 4589 | #endif |
| 4590 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4591 | continue; |
| 4592 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4593 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4594 | if (memcmp(type,mng_PLTE,4) == 0) |
| 4595 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4596 | /* Read global PLTE. */ |
| 4597 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4598 | if (length && (length < 769)) |
| 4599 | { |
| 4600 | if (mng_info->global_plte == (png_colorp) NULL) |
| 4601 | mng_info->global_plte=(png_colorp) AcquireQuantumMemory(256, |
| 4602 | sizeof(*mng_info->global_plte)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4603 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4604 | for (i=0; i < (ssize_t) (length/3); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4605 | { |
| 4606 | mng_info->global_plte[i].red=p[3*i]; |
| 4607 | mng_info->global_plte[i].green=p[3*i+1]; |
| 4608 | mng_info->global_plte[i].blue=p[3*i+2]; |
| 4609 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4610 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 4611 | mng_info->global_plte_length=(unsigned int) (length/3); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4612 | } |
| 4613 | #ifdef MNG_LOOSE |
| 4614 | for ( ; i < 256; i++) |
| 4615 | { |
| 4616 | mng_info->global_plte[i].red=i; |
| 4617 | mng_info->global_plte[i].green=i; |
| 4618 | mng_info->global_plte[i].blue=i; |
| 4619 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4620 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4621 | if (length) |
| 4622 | mng_info->global_plte_length=256; |
| 4623 | #endif |
| 4624 | else |
| 4625 | mng_info->global_plte_length=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4626 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4627 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4628 | continue; |
| 4629 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4630 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4631 | if (memcmp(type,mng_tRNS,4) == 0) |
| 4632 | { |
| 4633 | /* read global tRNS */ |
| 4634 | |
| 4635 | if (length < 257) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4636 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4637 | mng_info->global_trns[i]=p[i]; |
| 4638 | |
| 4639 | #ifdef MNG_LOOSE |
| 4640 | for ( ; i < 256; i++) |
| 4641 | mng_info->global_trns[i]=255; |
| 4642 | #endif |
cristy | 12560f3 | 2010-06-03 16:51:08 +0000 | [diff] [blame] | 4643 | mng_info->global_trns_length=(unsigned int) length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4644 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4645 | continue; |
| 4646 | } |
| 4647 | if (memcmp(type,mng_gAMA,4) == 0) |
| 4648 | { |
| 4649 | if (length == 4) |
| 4650 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4651 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4652 | igamma; |
| 4653 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4654 | igamma=mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4655 | mng_info->global_gamma=((float) igamma)*0.00001; |
| 4656 | mng_info->have_global_gama=MagickTrue; |
| 4657 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4658 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4659 | else |
| 4660 | mng_info->have_global_gama=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4661 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4662 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4663 | continue; |
| 4664 | } |
| 4665 | |
| 4666 | if (memcmp(type,mng_cHRM,4) == 0) |
| 4667 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4668 | /* Read global cHRM */ |
| 4669 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4670 | if (length == 32) |
| 4671 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4672 | mng_info->global_chrm.white_point.x=0.00001*mng_get_long(p); |
| 4673 | mng_info->global_chrm.white_point.y=0.00001*mng_get_long(&p[4]); |
| 4674 | mng_info->global_chrm.red_primary.x=0.00001*mng_get_long(&p[8]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4675 | mng_info->global_chrm.red_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4676 | mng_get_long(&p[12]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4677 | mng_info->global_chrm.green_primary.x=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4678 | mng_get_long(&p[16]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4679 | mng_info->global_chrm.green_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4680 | mng_get_long(&p[20]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4681 | mng_info->global_chrm.blue_primary.x=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4682 | mng_get_long(&p[24]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4683 | mng_info->global_chrm.blue_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4684 | mng_get_long(&p[28]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4685 | mng_info->have_global_chrm=MagickTrue; |
| 4686 | } |
| 4687 | else |
| 4688 | mng_info->have_global_chrm=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4689 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4690 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4691 | continue; |
| 4692 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4693 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4694 | if (memcmp(type,mng_sRGB,4) == 0) |
| 4695 | { |
| 4696 | /* |
| 4697 | Read global sRGB. |
| 4698 | */ |
| 4699 | if (length) |
| 4700 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 4701 | mng_info->global_srgb_intent= |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 4702 | Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4703 | mng_info->have_global_srgb=MagickTrue; |
| 4704 | } |
| 4705 | else |
| 4706 | mng_info->have_global_srgb=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4707 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4708 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4709 | continue; |
| 4710 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4711 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4712 | if (memcmp(type,mng_iCCP,4) == 0) |
| 4713 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4714 | /* To do: */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4715 | |
| 4716 | /* |
| 4717 | Read global iCCP. |
| 4718 | */ |
| 4719 | if (length) |
| 4720 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4721 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4722 | continue; |
| 4723 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4724 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4725 | if (memcmp(type,mng_FRAM,4) == 0) |
| 4726 | { |
| 4727 | if (mng_type == 3) |
| 4728 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 4729 | CoderError,"FRAM chunk found in MNG-VLC datastream","`%s'", |
| 4730 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4731 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4732 | if ((mng_info->framing_mode == 2) || (mng_info->framing_mode == 4)) |
| 4733 | image->delay=frame_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4734 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4735 | frame_delay=default_frame_delay; |
| 4736 | frame_timeout=default_frame_timeout; |
| 4737 | fb=default_fb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4738 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4739 | if (length) |
| 4740 | if (p[0]) |
| 4741 | mng_info->framing_mode=p[0]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4742 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4743 | if (logging != MagickFalse) |
| 4744 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4745 | " Framing_mode=%d",mng_info->framing_mode); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4746 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4747 | if (length > 6) |
| 4748 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4749 | /* Note the delay and frame clipping boundaries. */ |
| 4750 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4751 | p++; /* framing mode */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4752 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4753 | while (*p && ((p-chunk) < (ssize_t) length)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4754 | p++; /* frame name */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4755 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4756 | p++; /* frame name terminator */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4757 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4758 | if ((p-chunk) < (ssize_t) (length-4)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4759 | { |
| 4760 | int |
| 4761 | change_delay, |
| 4762 | change_timeout, |
| 4763 | change_clipping; |
| 4764 | |
| 4765 | change_delay=(*p++); |
| 4766 | change_timeout=(*p++); |
| 4767 | change_clipping=(*p++); |
| 4768 | p++; /* change_sync */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4769 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4770 | if (change_delay) |
| 4771 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4772 | frame_delay=1UL*image->ticks_per_second* |
| 4773 | mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4774 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4775 | if (mng_info->ticks_per_second != 0) |
| 4776 | frame_delay/=mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4777 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 4778 | else |
| 4779 | frame_delay=PNG_UINT_31_MAX; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4780 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4781 | if (change_delay == 2) |
| 4782 | default_frame_delay=frame_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4783 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4784 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4785 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4786 | if (logging != MagickFalse) |
| 4787 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4788 | " Framing_delay=%.20g",(double) frame_delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4789 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4790 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4791 | if (change_timeout) |
| 4792 | { |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 4793 | frame_timeout=1UL*image->ticks_per_second* |
| 4794 | mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4795 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 4796 | if (mng_info->ticks_per_second != 0) |
| 4797 | frame_timeout/=mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4798 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 4799 | else |
| 4800 | frame_timeout=PNG_UINT_31_MAX; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4801 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4802 | if (change_delay == 2) |
| 4803 | default_frame_timeout=frame_timeout; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4804 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4805 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4806 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4807 | if (logging != MagickFalse) |
| 4808 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4809 | " Framing_timeout=%.20g",(double) frame_timeout); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4810 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4811 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4812 | if (change_clipping) |
| 4813 | { |
| 4814 | fb=mng_read_box(previous_fb,(char) p[0],&p[1]); |
| 4815 | p+=17; |
| 4816 | previous_fb=fb; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4817 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4818 | if (logging != MagickFalse) |
| 4819 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4820 | " Frame_clip: L=%.20g R=%.20g T=%.20g B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4821 | (double) fb.left,(double) fb.right,(double) fb.top, |
| 4822 | (double) fb.bottom); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4823 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4824 | if (change_clipping == 2) |
| 4825 | default_fb=fb; |
| 4826 | } |
| 4827 | } |
| 4828 | } |
| 4829 | mng_info->clip=fb; |
| 4830 | mng_info->clip=mng_minimum_box(fb,mng_info->frame); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4831 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4832 | subframe_width=(size_t) (mng_info->clip.right |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4833 | -mng_info->clip.left); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4834 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4835 | subframe_height=(size_t) (mng_info->clip.bottom |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4836 | -mng_info->clip.top); |
| 4837 | /* |
| 4838 | Insert a background layer behind the frame if framing_mode is 4. |
| 4839 | */ |
| 4840 | #if defined(MNG_INSERT_LAYERS) |
| 4841 | if (logging != MagickFalse) |
| 4842 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4843 | " subframe_width=%.20g, subframe_height=%.20g",(double) |
| 4844 | subframe_width,(double) subframe_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4845 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4846 | if (insert_layers && (mng_info->framing_mode == 4) && |
| 4847 | (subframe_width) && (subframe_height)) |
| 4848 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4849 | /* Allocate next image structure. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4850 | if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL) |
| 4851 | { |
| 4852 | AcquireNextImage(image_info,image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4853 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4854 | if (GetNextImageInList(image) == (Image *) NULL) |
| 4855 | { |
| 4856 | image=DestroyImageList(image); |
| 4857 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 4858 | return((Image *) NULL); |
| 4859 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4860 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4861 | image=SyncNextImageInList(image); |
| 4862 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4863 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4864 | mng_info->image=image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4865 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4866 | if (term_chunk_found) |
| 4867 | { |
| 4868 | image->start_loop=MagickTrue; |
| 4869 | image->iterations=mng_iterations; |
| 4870 | term_chunk_found=MagickFalse; |
| 4871 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4872 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4873 | else |
| 4874 | image->start_loop=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4875 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4876 | image->columns=subframe_width; |
| 4877 | image->rows=subframe_height; |
| 4878 | image->page.width=subframe_width; |
| 4879 | image->page.height=subframe_height; |
| 4880 | image->page.x=mng_info->clip.left; |
| 4881 | image->page.y=mng_info->clip.top; |
| 4882 | image->background_color=mng_background_color; |
| 4883 | image->matte=MagickFalse; |
| 4884 | image->delay=0; |
| 4885 | (void) SetImageBackgroundColor(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4886 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4887 | if (logging != MagickFalse) |
| 4888 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4889 | " Insert backgd layer, L=%.20g, R=%.20g T=%.20g, B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4890 | (double) mng_info->clip.left,(double) mng_info->clip.right, |
| 4891 | (double) mng_info->clip.top,(double) mng_info->clip.bottom); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4892 | } |
| 4893 | #endif |
| 4894 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4895 | continue; |
| 4896 | } |
| 4897 | if (memcmp(type,mng_CLIP,4) == 0) |
| 4898 | { |
| 4899 | unsigned int |
| 4900 | first_object, |
| 4901 | last_object; |
| 4902 | |
| 4903 | /* |
| 4904 | Read CLIP. |
| 4905 | */ |
| 4906 | first_object=(p[0] << 8) | p[1]; |
| 4907 | last_object=(p[2] << 8) | p[3]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4908 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4909 | for (i=(int) first_object; i <= (int) last_object; i++) |
| 4910 | { |
| 4911 | if (mng_info->exists[i] && !mng_info->frozen[i]) |
| 4912 | { |
| 4913 | MngBox |
| 4914 | box; |
| 4915 | |
| 4916 | box=mng_info->object_clip[i]; |
| 4917 | mng_info->object_clip[i]=mng_read_box(box,(char) p[4],&p[5]); |
| 4918 | } |
| 4919 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4920 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4921 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4922 | continue; |
| 4923 | } |
| 4924 | if (memcmp(type,mng_SAVE,4) == 0) |
| 4925 | { |
| 4926 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 4927 | if (mng_info->exists[i]) |
| 4928 | { |
| 4929 | mng_info->frozen[i]=MagickTrue; |
| 4930 | #ifdef MNG_OBJECT_BUFFERS |
| 4931 | if (mng_info->ob[i] != (MngBuffer *) NULL) |
| 4932 | mng_info->ob[i]->frozen=MagickTrue; |
| 4933 | #endif |
| 4934 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4935 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4936 | if (length) |
| 4937 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4938 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4939 | continue; |
| 4940 | } |
| 4941 | |
| 4942 | if ((memcmp(type,mng_DISC,4) == 0) || (memcmp(type,mng_SEEK,4) == 0)) |
| 4943 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4944 | /* Read DISC or SEEK. */ |
| 4945 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4946 | if ((length == 0) || !memcmp(type,mng_SEEK,4)) |
| 4947 | { |
| 4948 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 4949 | MngInfoDiscardObject(mng_info,i); |
| 4950 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4951 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4952 | else |
| 4953 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4954 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4955 | j; |
| 4956 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4957 | for (j=0; j < (ssize_t) length; j+=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4958 | { |
| 4959 | i=p[j] << 8 | p[j+1]; |
| 4960 | MngInfoDiscardObject(mng_info,i); |
| 4961 | } |
| 4962 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4963 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4964 | if (length) |
| 4965 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4966 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4967 | continue; |
| 4968 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4969 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4970 | if (memcmp(type,mng_MOVE,4) == 0) |
| 4971 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4972 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4973 | first_object, |
| 4974 | last_object; |
| 4975 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4976 | /* read MOVE */ |
| 4977 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4978 | first_object=(p[0] << 8) | p[1]; |
| 4979 | last_object=(p[2] << 8) | p[3]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4980 | for (i=(ssize_t) first_object; i <= (ssize_t) last_object; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4981 | { |
| 4982 | if (mng_info->exists[i] && !mng_info->frozen[i]) |
| 4983 | { |
| 4984 | MngPair |
| 4985 | new_pair; |
| 4986 | |
| 4987 | MngPair |
| 4988 | old_pair; |
| 4989 | |
| 4990 | old_pair.a=mng_info->x_off[i]; |
| 4991 | old_pair.b=mng_info->y_off[i]; |
| 4992 | new_pair=mng_read_pair(old_pair,(int) p[4],&p[5]); |
| 4993 | mng_info->x_off[i]=new_pair.a; |
| 4994 | mng_info->y_off[i]=new_pair.b; |
| 4995 | } |
| 4996 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4997 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4998 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4999 | continue; |
| 5000 | } |
| 5001 | |
| 5002 | if (memcmp(type,mng_LOOP,4) == 0) |
| 5003 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5004 | ssize_t loop_iters=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5005 | loop_level=chunk[0]; |
| 5006 | mng_info->loop_active[loop_level]=1; /* mark loop active */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5007 | |
| 5008 | /* Record starting point. */ |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5009 | loop_iters=mng_get_long(&chunk[1]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5010 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5011 | if (logging != MagickFalse) |
| 5012 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5013 | " LOOP level %.20g has %.20g iterations ",(double) loop_level, |
| 5014 | (double) loop_iters); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5015 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5016 | if (loop_iters == 0) |
| 5017 | skipping_loop=loop_level; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5018 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5019 | else |
| 5020 | { |
| 5021 | mng_info->loop_jump[loop_level]=TellBlob(image); |
| 5022 | mng_info->loop_count[loop_level]=loop_iters; |
| 5023 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5024 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5025 | mng_info->loop_iteration[loop_level]=0; |
| 5026 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5027 | continue; |
| 5028 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5029 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5030 | if (memcmp(type,mng_ENDL,4) == 0) |
| 5031 | { |
| 5032 | loop_level=chunk[0]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5033 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5034 | if (skipping_loop > 0) |
| 5035 | { |
| 5036 | if (skipping_loop == loop_level) |
| 5037 | { |
| 5038 | /* |
| 5039 | Found end of zero-iteration loop. |
| 5040 | */ |
| 5041 | skipping_loop=(-1); |
| 5042 | mng_info->loop_active[loop_level]=0; |
| 5043 | } |
| 5044 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5045 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5046 | else |
| 5047 | { |
| 5048 | if (mng_info->loop_active[loop_level] == 1) |
| 5049 | { |
| 5050 | mng_info->loop_count[loop_level]--; |
| 5051 | mng_info->loop_iteration[loop_level]++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5052 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5053 | if (logging != MagickFalse) |
| 5054 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5055 | " ENDL: LOOP level %.20g has %.20g remaining iters ", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5056 | (double) loop_level,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5057 | mng_info->loop_count[loop_level]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5058 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5059 | if (mng_info->loop_count[loop_level] != 0) |
| 5060 | { |
| 5061 | offset=SeekBlob(image,mng_info->loop_jump[loop_level], |
| 5062 | SEEK_SET); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5063 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5064 | if (offset < 0) |
| 5065 | ThrowReaderException(CorruptImageError, |
| 5066 | "ImproperImageHeader"); |
| 5067 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5068 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5069 | else |
| 5070 | { |
| 5071 | short |
| 5072 | last_level; |
| 5073 | |
| 5074 | /* |
| 5075 | Finished loop. |
| 5076 | */ |
| 5077 | mng_info->loop_active[loop_level]=0; |
| 5078 | last_level=(-1); |
| 5079 | for (i=0; i < loop_level; i++) |
| 5080 | if (mng_info->loop_active[i] == 1) |
| 5081 | last_level=(short) i; |
| 5082 | loop_level=last_level; |
| 5083 | } |
| 5084 | } |
| 5085 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5086 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5087 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5088 | continue; |
| 5089 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5090 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5091 | if (memcmp(type,mng_CLON,4) == 0) |
| 5092 | { |
| 5093 | if (mng_info->clon_warning == 0) |
| 5094 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 5095 | CoderError,"CLON is not implemented yet","`%s'", |
| 5096 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5097 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5098 | mng_info->clon_warning++; |
| 5099 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5100 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5101 | if (memcmp(type,mng_MAGN,4) == 0) |
| 5102 | { |
| 5103 | png_uint_16 |
| 5104 | magn_first, |
| 5105 | magn_last, |
| 5106 | magn_mb, |
| 5107 | magn_ml, |
| 5108 | magn_mr, |
| 5109 | magn_mt, |
| 5110 | magn_mx, |
| 5111 | magn_my, |
| 5112 | magn_methx, |
| 5113 | magn_methy; |
| 5114 | |
| 5115 | if (length > 1) |
| 5116 | magn_first=(p[0] << 8) | p[1]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5117 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5118 | else |
| 5119 | magn_first=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5120 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5121 | if (length > 3) |
| 5122 | magn_last=(p[2] << 8) | p[3]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5123 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5124 | else |
| 5125 | magn_last=magn_first; |
| 5126 | #ifndef MNG_OBJECT_BUFFERS |
| 5127 | if (magn_first || magn_last) |
| 5128 | if (mng_info->magn_warning == 0) |
| 5129 | { |
| 5130 | (void) ThrowMagickException(&image->exception, |
| 5131 | GetMagickModule(),CoderError, |
| 5132 | "MAGN is not implemented yet for nonzero objects", |
| 5133 | "`%s'",image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5134 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5135 | mng_info->magn_warning++; |
| 5136 | } |
| 5137 | #endif |
| 5138 | if (length > 4) |
| 5139 | magn_methx=p[4]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5140 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5141 | else |
| 5142 | magn_methx=0; |
| 5143 | |
| 5144 | if (length > 6) |
| 5145 | magn_mx=(p[5] << 8) | p[6]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5146 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5147 | else |
| 5148 | magn_mx=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5149 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5150 | if (magn_mx == 0) |
| 5151 | magn_mx=1; |
| 5152 | |
| 5153 | if (length > 8) |
| 5154 | magn_my=(p[7] << 8) | p[8]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5155 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5156 | else |
| 5157 | magn_my=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5158 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5159 | if (magn_my == 0) |
| 5160 | magn_my=1; |
| 5161 | |
| 5162 | if (length > 10) |
| 5163 | magn_ml=(p[9] << 8) | p[10]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5164 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5165 | else |
| 5166 | magn_ml=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5167 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5168 | if (magn_ml == 0) |
| 5169 | magn_ml=1; |
| 5170 | |
| 5171 | if (length > 12) |
| 5172 | magn_mr=(p[11] << 8) | p[12]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5173 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5174 | else |
| 5175 | magn_mr=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5176 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5177 | if (magn_mr == 0) |
| 5178 | magn_mr=1; |
| 5179 | |
| 5180 | if (length > 14) |
| 5181 | magn_mt=(p[13] << 8) | p[14]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5182 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5183 | else |
| 5184 | magn_mt=magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5185 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5186 | if (magn_mt == 0) |
| 5187 | magn_mt=1; |
| 5188 | |
| 5189 | if (length > 16) |
| 5190 | magn_mb=(p[15] << 8) | p[16]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5191 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5192 | else |
| 5193 | magn_mb=magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5194 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5195 | if (magn_mb == 0) |
| 5196 | magn_mb=1; |
| 5197 | |
| 5198 | if (length > 17) |
| 5199 | magn_methy=p[17]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5200 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5201 | else |
| 5202 | magn_methy=magn_methx; |
| 5203 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5204 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5205 | if (magn_methx > 5 || magn_methy > 5) |
| 5206 | if (mng_info->magn_warning == 0) |
| 5207 | { |
| 5208 | (void) ThrowMagickException(&image->exception, |
| 5209 | GetMagickModule(),CoderError, |
| 5210 | "Unknown MAGN method in MNG datastream","`%s'", |
| 5211 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5212 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5213 | mng_info->magn_warning++; |
| 5214 | } |
| 5215 | #ifdef MNG_OBJECT_BUFFERS |
| 5216 | /* Magnify existing objects in the range magn_first to magn_last */ |
| 5217 | #endif |
| 5218 | if (magn_first == 0 || magn_last == 0) |
| 5219 | { |
| 5220 | /* Save the magnification factors for object 0 */ |
| 5221 | mng_info->magn_mb=magn_mb; |
| 5222 | mng_info->magn_ml=magn_ml; |
| 5223 | mng_info->magn_mr=magn_mr; |
| 5224 | mng_info->magn_mt=magn_mt; |
| 5225 | mng_info->magn_mx=magn_mx; |
| 5226 | mng_info->magn_my=magn_my; |
| 5227 | mng_info->magn_methx=magn_methx; |
| 5228 | mng_info->magn_methy=magn_methy; |
| 5229 | } |
| 5230 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5231 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5232 | if (memcmp(type,mng_PAST,4) == 0) |
| 5233 | { |
| 5234 | if (mng_info->past_warning == 0) |
| 5235 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 5236 | CoderError,"PAST is not implemented yet","`%s'", |
| 5237 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5238 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5239 | mng_info->past_warning++; |
| 5240 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5241 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5242 | if (memcmp(type,mng_SHOW,4) == 0) |
| 5243 | { |
| 5244 | if (mng_info->show_warning == 0) |
| 5245 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 5246 | CoderError,"SHOW is not implemented yet","`%s'", |
| 5247 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5248 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5249 | mng_info->show_warning++; |
| 5250 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5251 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5252 | if (memcmp(type,mng_sBIT,4) == 0) |
| 5253 | { |
| 5254 | if (length < 4) |
| 5255 | mng_info->have_global_sbit=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5256 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5257 | else |
| 5258 | { |
| 5259 | mng_info->global_sbit.gray=p[0]; |
| 5260 | mng_info->global_sbit.red=p[0]; |
| 5261 | mng_info->global_sbit.green=p[1]; |
| 5262 | mng_info->global_sbit.blue=p[2]; |
| 5263 | mng_info->global_sbit.alpha=p[3]; |
| 5264 | mng_info->have_global_sbit=MagickTrue; |
| 5265 | } |
| 5266 | } |
| 5267 | if (memcmp(type,mng_pHYs,4) == 0) |
| 5268 | { |
| 5269 | if (length > 8) |
| 5270 | { |
| 5271 | mng_info->global_x_pixels_per_unit= |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5272 | (size_t) mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5273 | mng_info->global_y_pixels_per_unit= |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5274 | (size_t) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5275 | mng_info->global_phys_unit_type=p[8]; |
| 5276 | mng_info->have_global_phys=MagickTrue; |
| 5277 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5278 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5279 | else |
| 5280 | mng_info->have_global_phys=MagickFalse; |
| 5281 | } |
| 5282 | if (memcmp(type,mng_pHYg,4) == 0) |
| 5283 | { |
| 5284 | if (mng_info->phyg_warning == 0) |
| 5285 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 5286 | CoderError,"pHYg is not implemented.","`%s'",image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5287 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5288 | mng_info->phyg_warning++; |
| 5289 | } |
| 5290 | if (memcmp(type,mng_BASI,4) == 0) |
| 5291 | { |
| 5292 | skip_to_iend=MagickTrue; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5293 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5294 | if (mng_info->basi_warning == 0) |
| 5295 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 5296 | CoderError,"BASI is not implemented yet","`%s'", |
| 5297 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5298 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5299 | mng_info->basi_warning++; |
| 5300 | #ifdef MNG_BASI_SUPPORTED |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5301 | basi_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5302 | (p[2] << 8) | p[3]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5303 | basi_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5304 | (p[6] << 8) | p[7]); |
| 5305 | basi_color_type=p[8]; |
| 5306 | basi_compression_method=p[9]; |
| 5307 | basi_filter_type=p[10]; |
| 5308 | basi_interlace_method=p[11]; |
| 5309 | if (length > 11) |
| 5310 | basi_red=(p[12] << 8) & p[13]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5311 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5312 | else |
| 5313 | basi_red=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5314 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5315 | if (length > 13) |
| 5316 | basi_green=(p[14] << 8) & p[15]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5317 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5318 | else |
| 5319 | basi_green=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5320 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5321 | if (length > 15) |
| 5322 | basi_blue=(p[16] << 8) & p[17]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5323 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5324 | else |
| 5325 | basi_blue=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5326 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5327 | if (length > 17) |
| 5328 | basi_alpha=(p[18] << 8) & p[19]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5329 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5330 | else |
| 5331 | { |
| 5332 | if (basi_sample_depth == 16) |
| 5333 | basi_alpha=65535L; |
| 5334 | else |
| 5335 | basi_alpha=255; |
| 5336 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5337 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5338 | if (length > 19) |
| 5339 | basi_viewable=p[20]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5340 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5341 | else |
| 5342 | basi_viewable=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5343 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5344 | #endif |
| 5345 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5346 | continue; |
| 5347 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5348 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5349 | if (memcmp(type,mng_IHDR,4) |
| 5350 | #if defined(JNG_SUPPORTED) |
| 5351 | && memcmp(type,mng_JHDR,4) |
| 5352 | #endif |
| 5353 | ) |
| 5354 | { |
| 5355 | /* Not an IHDR or JHDR chunk */ |
| 5356 | if (length) |
| 5357 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5358 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5359 | continue; |
| 5360 | } |
| 5361 | /* Process IHDR */ |
| 5362 | if (logging != MagickFalse) |
| 5363 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5364 | " Processing %c%c%c%c chunk",type[0],type[1],type[2],type[3]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5365 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5366 | mng_info->exists[object_id]=MagickTrue; |
| 5367 | mng_info->viewable[object_id]=MagickTrue; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5368 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5369 | if (mng_info->invisible[object_id]) |
| 5370 | { |
| 5371 | if (logging != MagickFalse) |
| 5372 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5373 | " Skipping invisible object"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5374 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5375 | skip_to_iend=MagickTrue; |
| 5376 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5377 | continue; |
| 5378 | } |
| 5379 | #if defined(MNG_INSERT_LAYERS) |
| 5380 | if (length < 8) |
| 5381 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5382 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5383 | image_width=(size_t) mng_get_long(p); |
| 5384 | image_height=(size_t) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5385 | #endif |
| 5386 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5387 | |
| 5388 | /* |
| 5389 | Insert a transparent background layer behind the entire animation |
| 5390 | if it is not full screen. |
| 5391 | */ |
| 5392 | #if defined(MNG_INSERT_LAYERS) |
| 5393 | if (insert_layers && mng_type && first_mng_object) |
| 5394 | { |
| 5395 | if ((mng_info->clip.left > 0) || (mng_info->clip.top > 0) || |
| 5396 | (image_width < mng_info->mng_width) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5397 | (mng_info->clip.right < (ssize_t) mng_info->mng_width) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5398 | (image_height < mng_info->mng_height) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5399 | (mng_info->clip.bottom < (ssize_t) mng_info->mng_height)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5400 | { |
| 5401 | if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL) |
| 5402 | { |
| 5403 | /* |
| 5404 | Allocate next image structure. |
| 5405 | */ |
| 5406 | AcquireNextImage(image_info,image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5407 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5408 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5409 | { |
| 5410 | image=DestroyImageList(image); |
| 5411 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5412 | return((Image *) NULL); |
| 5413 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5414 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5415 | image=SyncNextImageInList(image); |
| 5416 | } |
| 5417 | mng_info->image=image; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5418 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5419 | if (term_chunk_found) |
| 5420 | { |
| 5421 | image->start_loop=MagickTrue; |
| 5422 | image->iterations=mng_iterations; |
| 5423 | term_chunk_found=MagickFalse; |
| 5424 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5425 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5426 | else |
| 5427 | image->start_loop=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5428 | |
| 5429 | /* Make a background rectangle. */ |
| 5430 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5431 | image->delay=0; |
| 5432 | image->columns=mng_info->mng_width; |
| 5433 | image->rows=mng_info->mng_height; |
| 5434 | image->page.width=mng_info->mng_width; |
| 5435 | image->page.height=mng_info->mng_height; |
| 5436 | image->page.x=0; |
| 5437 | image->page.y=0; |
| 5438 | image->background_color=mng_background_color; |
| 5439 | (void) SetImageBackgroundColor(image); |
| 5440 | if (logging != MagickFalse) |
| 5441 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5442 | " Inserted transparent background layer, W=%.20g, H=%.20g", |
| 5443 | (double) mng_info->mng_width,(double) mng_info->mng_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5444 | } |
| 5445 | } |
| 5446 | /* |
| 5447 | Insert a background layer behind the upcoming image if |
| 5448 | framing_mode is 3, and we haven't already inserted one. |
| 5449 | */ |
| 5450 | if (insert_layers && (mng_info->framing_mode == 3) && |
| 5451 | (subframe_width) && (subframe_height) && (simplicity == 0 || |
| 5452 | (simplicity & 0x08))) |
| 5453 | { |
| 5454 | if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL) |
| 5455 | { |
| 5456 | /* |
| 5457 | Allocate next image structure. |
| 5458 | */ |
| 5459 | AcquireNextImage(image_info,image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5460 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5461 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5462 | { |
| 5463 | image=DestroyImageList(image); |
| 5464 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5465 | return((Image *) NULL); |
| 5466 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5467 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5468 | image=SyncNextImageInList(image); |
| 5469 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5470 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5471 | mng_info->image=image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5472 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5473 | if (term_chunk_found) |
| 5474 | { |
| 5475 | image->start_loop=MagickTrue; |
| 5476 | image->iterations=mng_iterations; |
| 5477 | term_chunk_found=MagickFalse; |
| 5478 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5479 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5480 | else |
| 5481 | image->start_loop=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5482 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5483 | image->delay=0; |
| 5484 | image->columns=subframe_width; |
| 5485 | image->rows=subframe_height; |
| 5486 | image->page.width=subframe_width; |
| 5487 | image->page.height=subframe_height; |
| 5488 | image->page.x=mng_info->clip.left; |
| 5489 | image->page.y=mng_info->clip.top; |
| 5490 | image->background_color=mng_background_color; |
| 5491 | image->matte=MagickFalse; |
| 5492 | (void) SetImageBackgroundColor(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5493 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5494 | if (logging != MagickFalse) |
| 5495 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5496 | " Insert background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5497 | (double) mng_info->clip.left,(double) mng_info->clip.right, |
| 5498 | (double) mng_info->clip.top,(double) mng_info->clip.bottom); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5499 | } |
| 5500 | #endif /* MNG_INSERT_LAYERS */ |
| 5501 | first_mng_object=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5502 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5503 | if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL) |
| 5504 | { |
| 5505 | /* |
| 5506 | Allocate next image structure. |
| 5507 | */ |
| 5508 | AcquireNextImage(image_info,image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5509 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5510 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5511 | { |
| 5512 | image=DestroyImageList(image); |
| 5513 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5514 | return((Image *) NULL); |
| 5515 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5516 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5517 | image=SyncNextImageInList(image); |
| 5518 | } |
| 5519 | mng_info->image=image; |
| 5520 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 5521 | GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5522 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5523 | if (status == MagickFalse) |
| 5524 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5525 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5526 | if (term_chunk_found) |
| 5527 | { |
| 5528 | image->start_loop=MagickTrue; |
| 5529 | term_chunk_found=MagickFalse; |
| 5530 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5531 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5532 | else |
| 5533 | image->start_loop=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5534 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5535 | if (mng_info->framing_mode == 1 || mng_info->framing_mode == 3) |
| 5536 | { |
| 5537 | image->delay=frame_delay; |
| 5538 | frame_delay=default_frame_delay; |
| 5539 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5540 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5541 | else |
| 5542 | image->delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5543 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5544 | image->page.width=mng_info->mng_width; |
| 5545 | image->page.height=mng_info->mng_height; |
| 5546 | image->page.x=mng_info->x_off[object_id]; |
| 5547 | image->page.y=mng_info->y_off[object_id]; |
| 5548 | image->iterations=mng_iterations; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5549 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5550 | /* |
| 5551 | Seek back to the beginning of the IHDR or JHDR chunk's length field. |
| 5552 | */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5553 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5554 | if (logging != MagickFalse) |
| 5555 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5556 | " Seeking back to beginning of %c%c%c%c chunk",type[0],type[1], |
| 5557 | type[2],type[3]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5558 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5559 | offset=SeekBlob(image,-((ssize_t) length+12),SEEK_CUR); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5560 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5561 | if (offset < 0) |
| 5562 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 5563 | } |
| 5564 | |
| 5565 | previous=image; |
| 5566 | mng_info->image=image; |
| 5567 | mng_info->mng_type=mng_type; |
| 5568 | mng_info->object_id=object_id; |
| 5569 | |
| 5570 | if (memcmp(type,mng_IHDR,4) == 0) |
| 5571 | image=ReadOnePNGImage(mng_info,image_info,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5572 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5573 | #if defined(JNG_SUPPORTED) |
| 5574 | else |
| 5575 | image=ReadOneJNGImage(mng_info,image_info,exception); |
| 5576 | #endif |
| 5577 | |
| 5578 | if (image == (Image *) NULL) |
| 5579 | { |
| 5580 | if (IsImageObject(previous) != MagickFalse) |
| 5581 | { |
| 5582 | (void) DestroyImageList(previous); |
| 5583 | (void) CloseBlob(previous); |
| 5584 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5585 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5586 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5587 | return((Image *) NULL); |
| 5588 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5589 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5590 | if (image->columns == 0 || image->rows == 0) |
| 5591 | { |
| 5592 | (void) CloseBlob(image); |
| 5593 | image=DestroyImageList(image); |
| 5594 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5595 | return((Image *) NULL); |
| 5596 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5597 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5598 | mng_info->image=image; |
| 5599 | |
| 5600 | if (mng_type) |
| 5601 | { |
| 5602 | MngBox |
| 5603 | crop_box; |
| 5604 | |
| 5605 | if (mng_info->magn_methx || mng_info->magn_methy) |
| 5606 | { |
| 5607 | png_uint_32 |
| 5608 | magnified_height, |
| 5609 | magnified_width; |
| 5610 | |
| 5611 | if (logging != MagickFalse) |
| 5612 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5613 | " Processing MNG MAGN chunk"); |
| 5614 | |
| 5615 | if (mng_info->magn_methx == 1) |
| 5616 | { |
| 5617 | magnified_width=mng_info->magn_ml; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5618 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5619 | if (image->columns > 1) |
| 5620 | magnified_width += mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5621 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5622 | if (image->columns > 2) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5623 | magnified_width += (png_uint_32) |
| 5624 | ((image->columns-2)*(mng_info->magn_mx)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5625 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5626 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5627 | else |
| 5628 | { |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 5629 | magnified_width=(png_uint_32) image->columns; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5630 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5631 | if (image->columns > 1) |
| 5632 | magnified_width += mng_info->magn_ml-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5633 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5634 | if (image->columns > 2) |
| 5635 | magnified_width += mng_info->magn_mr-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5636 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5637 | if (image->columns > 3) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5638 | magnified_width += (png_uint_32) |
| 5639 | ((image->columns-3)*(mng_info->magn_mx-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5640 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5641 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5642 | if (mng_info->magn_methy == 1) |
| 5643 | { |
| 5644 | magnified_height=mng_info->magn_mt; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5645 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5646 | if (image->rows > 1) |
| 5647 | magnified_height += mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5648 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5649 | if (image->rows > 2) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5650 | magnified_height += (png_uint_32) |
| 5651 | ((image->rows-2)*(mng_info->magn_my)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5652 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5653 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5654 | else |
| 5655 | { |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 5656 | magnified_height=(png_uint_32) image->rows; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5657 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5658 | if (image->rows > 1) |
| 5659 | magnified_height += mng_info->magn_mt-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5660 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5661 | if (image->rows > 2) |
| 5662 | magnified_height += mng_info->magn_mb-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5663 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5664 | if (image->rows > 3) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5665 | magnified_height += (png_uint_32) |
| 5666 | ((image->rows-3)*(mng_info->magn_my-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5667 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5668 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5669 | if (magnified_height > image->rows || |
| 5670 | magnified_width > image->columns) |
| 5671 | { |
| 5672 | Image |
| 5673 | *large_image; |
| 5674 | |
| 5675 | int |
| 5676 | yy; |
| 5677 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5678 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5679 | m, |
| 5680 | y; |
| 5681 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5682 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5683 | x; |
| 5684 | |
| 5685 | register PixelPacket |
| 5686 | *n, |
| 5687 | *q; |
| 5688 | |
| 5689 | PixelPacket |
| 5690 | *next, |
| 5691 | *prev; |
| 5692 | |
| 5693 | png_uint_16 |
| 5694 | magn_methx, |
| 5695 | magn_methy; |
| 5696 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5697 | /* Allocate next image structure. */ |
| 5698 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5699 | if (logging != MagickFalse) |
| 5700 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5701 | " Allocate magnified image"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5702 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5703 | AcquireNextImage(image_info,image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5704 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5705 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5706 | { |
| 5707 | image=DestroyImageList(image); |
| 5708 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5709 | return((Image *) NULL); |
| 5710 | } |
| 5711 | |
| 5712 | large_image=SyncNextImageInList(image); |
| 5713 | |
| 5714 | large_image->columns=magnified_width; |
| 5715 | large_image->rows=magnified_height; |
| 5716 | |
| 5717 | magn_methx=mng_info->magn_methx; |
| 5718 | magn_methy=mng_info->magn_methy; |
| 5719 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 5720 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5721 | #define QM unsigned short |
| 5722 | if (magn_methx != 1 || magn_methy != 1) |
| 5723 | { |
| 5724 | /* |
| 5725 | Scale pixels to unsigned shorts to prevent |
| 5726 | overflow of intermediate values of interpolations |
| 5727 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5728 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5729 | { |
| 5730 | q=GetAuthenticPixels(image,0,y,image->columns,1, |
| 5731 | exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5732 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5733 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5734 | { |
| 5735 | q->red=ScaleQuantumToShort(q->red); |
| 5736 | q->green=ScaleQuantumToShort(q->green); |
| 5737 | q->blue=ScaleQuantumToShort(q->blue); |
| 5738 | q->opacity=ScaleQuantumToShort(q->opacity); |
| 5739 | q++; |
| 5740 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5741 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5742 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 5743 | break; |
| 5744 | } |
| 5745 | } |
| 5746 | #else |
| 5747 | #define QM Quantum |
| 5748 | #endif |
| 5749 | |
| 5750 | if (image->matte != MagickFalse) |
| 5751 | (void) SetImageBackgroundColor(large_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5752 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5753 | else |
| 5754 | { |
| 5755 | large_image->background_color.opacity=OpaqueOpacity; |
| 5756 | (void) SetImageBackgroundColor(large_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5757 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5758 | if (magn_methx == 4) |
| 5759 | magn_methx=2; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5760 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5761 | if (magn_methx == 5) |
| 5762 | magn_methx=3; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5763 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5764 | if (magn_methy == 4) |
| 5765 | magn_methy=2; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5766 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5767 | if (magn_methy == 5) |
| 5768 | magn_methy=3; |
| 5769 | } |
| 5770 | |
| 5771 | /* magnify the rows into the right side of the large image */ |
| 5772 | |
| 5773 | if (logging != MagickFalse) |
| 5774 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5775 | " Magnify the rows to %.20g",(double) large_image->rows); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5776 | m=(ssize_t) mng_info->magn_mt; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5777 | yy=0; |
| 5778 | length=(size_t) image->columns; |
| 5779 | next=(PixelPacket *) AcquireQuantumMemory(length,sizeof(*next)); |
| 5780 | prev=(PixelPacket *) AcquireQuantumMemory(length,sizeof(*prev)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5781 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5782 | if ((prev == (PixelPacket *) NULL) || |
| 5783 | (next == (PixelPacket *) NULL)) |
| 5784 | { |
| 5785 | image=DestroyImageList(image); |
| 5786 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5787 | ThrowReaderException(ResourceLimitError, |
| 5788 | "MemoryAllocationFailed"); |
| 5789 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5790 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5791 | n=GetAuthenticPixels(image,0,0,image->columns,1,exception); |
| 5792 | (void) CopyMagickMemory(next,n,length); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5793 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5794 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5795 | { |
| 5796 | if (y == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5797 | m=(ssize_t) mng_info->magn_mt; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5798 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5799 | else if (magn_methy > 1 && y == (ssize_t) image->rows-2) |
| 5800 | m=(ssize_t) mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5801 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5802 | else if (magn_methy <= 1 && y == (ssize_t) image->rows-1) |
| 5803 | m=(ssize_t) mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5804 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5805 | else if (magn_methy > 1 && y == (ssize_t) image->rows-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5806 | m=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5807 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5808 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5809 | m=(ssize_t) mng_info->magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5810 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5811 | n=prev; |
| 5812 | prev=next; |
| 5813 | next=n; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5814 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5815 | if (y < (ssize_t) image->rows-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5816 | { |
| 5817 | n=GetAuthenticPixels(image,0,y+1,image->columns,1, |
| 5818 | exception); |
| 5819 | (void) CopyMagickMemory(next,n,length); |
| 5820 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5821 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5822 | for (i=0; i < m; i++, yy++) |
| 5823 | { |
| 5824 | register PixelPacket |
| 5825 | *pixels; |
| 5826 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5827 | assert(yy < (ssize_t) large_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5828 | pixels=prev; |
| 5829 | n=next; |
| 5830 | q=GetAuthenticPixels(large_image,0,yy,large_image->columns, |
| 5831 | 1,exception); |
| 5832 | q+=(large_image->columns-image->columns); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5833 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5834 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5835 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 5836 | /* To do: get color as function of indexes[x] */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5837 | /* |
| 5838 | if (image->storage_class == PseudoClass) |
| 5839 | { |
| 5840 | } |
| 5841 | */ |
| 5842 | |
| 5843 | if (magn_methy <= 1) |
| 5844 | { |
| 5845 | *q=(*pixels); /* replicate previous */ |
| 5846 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5847 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5848 | else if (magn_methy == 2 || magn_methy == 4) |
| 5849 | { |
| 5850 | if (i == 0) |
| 5851 | *q=(*pixels); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5852 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5853 | else |
| 5854 | { |
| 5855 | /* Interpolate */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5856 | (*q).red=(QM) (((ssize_t) (2*i*((*n).red |
| 5857 | -(*pixels).red)+m))/((ssize_t) (m*2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5858 | +(*pixels).red); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5859 | (*q).green=(QM) (((ssize_t) (2*i*((*n).green |
| 5860 | -(*pixels).green)+m))/((ssize_t) (m*2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5861 | +(*pixels).green); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5862 | (*q).blue=(QM) (((ssize_t) (2*i*((*n).blue |
| 5863 | -(*pixels).blue)+m))/((ssize_t) (m*2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5864 | +(*pixels).blue); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5865 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5866 | if (image->matte != MagickFalse) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5867 | (*q).opacity=(QM) (((ssize_t) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5868 | (2*i*((*n).opacity |
| 5869 | -(*pixels).opacity)+m)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5870 | /((ssize_t) (m*2))+(*pixels).opacity); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5871 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5872 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5873 | if (magn_methy == 4) |
| 5874 | { |
| 5875 | /* Replicate nearest */ |
| 5876 | if (i <= ((m+1) << 1)) |
| 5877 | (*q).opacity=(*pixels).opacity+0; |
| 5878 | else |
| 5879 | (*q).opacity=(*n).opacity+0; |
| 5880 | } |
| 5881 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5882 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5883 | else /* if (magn_methy == 3 || magn_methy == 5) */ |
| 5884 | { |
| 5885 | /* Replicate nearest */ |
| 5886 | if (i <= ((m+1) << 1)) |
| 5887 | *q=(*pixels); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5888 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5889 | else |
| 5890 | *q=(*n); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5891 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5892 | if (magn_methy == 5) |
| 5893 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5894 | (*q).opacity=(QM) (((ssize_t) (2*i*((*n).opacity |
| 5895 | -(*pixels).opacity)+m))/((ssize_t) (m*2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5896 | +(*pixels).opacity); |
| 5897 | } |
| 5898 | } |
| 5899 | n++; |
| 5900 | q++; |
| 5901 | pixels++; |
| 5902 | } /* x */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5903 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5904 | if (SyncAuthenticPixels(large_image,exception) == 0) |
| 5905 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5906 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5907 | } /* i */ |
| 5908 | } /* y */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5909 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5910 | prev=(PixelPacket *) RelinquishMagickMemory(prev); |
| 5911 | next=(PixelPacket *) RelinquishMagickMemory(next); |
| 5912 | |
| 5913 | length=image->columns; |
| 5914 | |
| 5915 | if (logging != MagickFalse) |
| 5916 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5917 | " Delete original image"); |
| 5918 | |
| 5919 | DeleteImageFromList(&image); |
| 5920 | |
| 5921 | image=large_image; |
| 5922 | |
| 5923 | mng_info->image=image; |
| 5924 | |
| 5925 | /* magnify the columns */ |
| 5926 | if (logging != MagickFalse) |
| 5927 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5928 | " Magnify the columns to %.20g",(double) image->columns); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5929 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5930 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5931 | { |
| 5932 | register PixelPacket |
| 5933 | *pixels; |
| 5934 | |
| 5935 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 5936 | pixels=q+(image->columns-length); |
| 5937 | n=pixels+1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5938 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5939 | for (x=(ssize_t) (image->columns-length); |
| 5940 | x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5941 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5942 | if (x == (ssize_t) (image->columns-length)) |
| 5943 | m=(ssize_t) mng_info->magn_ml; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5944 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5945 | else if (magn_methx > 1 && x == (ssize_t) image->columns-2) |
| 5946 | m=(ssize_t) mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5947 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5948 | else if (magn_methx <= 1 && x == (ssize_t) image->columns-1) |
| 5949 | m=(ssize_t) mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5950 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5951 | else if (magn_methx > 1 && x == (ssize_t) image->columns-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5952 | m=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5953 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5954 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5955 | m=(ssize_t) mng_info->magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5956 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5957 | for (i=0; i < m; i++) |
| 5958 | { |
| 5959 | if (magn_methx <= 1) |
| 5960 | { |
| 5961 | /* replicate previous */ |
| 5962 | *q=(*pixels); |
| 5963 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5964 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5965 | else if (magn_methx == 2 || magn_methx == 4) |
| 5966 | { |
| 5967 | if (i == 0) |
| 5968 | *q=(*pixels); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5969 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5970 | else |
| 5971 | { |
| 5972 | /* Interpolate */ |
| 5973 | (*q).red=(QM) ((2*i*((*n).red |
| 5974 | -(*pixels).red)+m) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5975 | /((ssize_t) (m*2))+(*pixels).red); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5976 | (*q).green=(QM) ((2*i*((*n).green |
| 5977 | -(*pixels).green) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5978 | +m)/((ssize_t) (m*2))+(*pixels).green); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5979 | (*q).blue=(QM) ((2*i*((*n).blue |
| 5980 | -(*pixels).blue)+m) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5981 | /((ssize_t) (m*2))+(*pixels).blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5982 | if (image->matte != MagickFalse) |
| 5983 | (*q).opacity=(QM) ((2*i*((*n).opacity |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5984 | -(*pixels).opacity)+m)/((ssize_t) (m*2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5985 | +(*pixels).opacity); |
| 5986 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5987 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5988 | if (magn_methx == 4) |
| 5989 | { |
| 5990 | /* Replicate nearest */ |
| 5991 | if (i <= ((m+1) << 1)) |
| 5992 | (*q).opacity=(*pixels).opacity+0; |
| 5993 | else |
| 5994 | (*q).opacity=(*n).opacity+0; |
| 5995 | } |
| 5996 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5997 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5998 | else /* if (magn_methx == 3 || magn_methx == 5) */ |
| 5999 | { |
| 6000 | /* Replicate nearest */ |
| 6001 | if (i <= ((m+1) << 1)) |
| 6002 | *q=(*pixels); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6003 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6004 | else |
| 6005 | *q=(*n); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6006 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6007 | if (magn_methx == 5) |
| 6008 | { |
| 6009 | /* Interpolate */ |
| 6010 | (*q).opacity=(QM) ((2*i*((*n).opacity |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6011 | -(*pixels).opacity)+m) /((ssize_t) (m*2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6012 | +(*pixels).opacity); |
| 6013 | } |
| 6014 | } |
| 6015 | q++; |
| 6016 | } |
| 6017 | n++; |
| 6018 | p++; |
| 6019 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6020 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6021 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6022 | break; |
| 6023 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 6024 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6025 | if (magn_methx != 1 || magn_methy != 1) |
| 6026 | { |
| 6027 | /* |
| 6028 | Rescale pixels to Quantum |
| 6029 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6030 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6031 | { |
| 6032 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6033 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6034 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6035 | { |
| 6036 | q->red=ScaleShortToQuantum(q->red); |
| 6037 | q->green=ScaleShortToQuantum(q->green); |
| 6038 | q->blue=ScaleShortToQuantum(q->blue); |
| 6039 | q->opacity=ScaleShortToQuantum(q->opacity); |
| 6040 | q++; |
| 6041 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6042 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6043 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6044 | break; |
| 6045 | } |
| 6046 | } |
| 6047 | #endif |
| 6048 | if (logging != MagickFalse) |
| 6049 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6050 | " Finished MAGN processing"); |
| 6051 | } |
| 6052 | } |
| 6053 | |
| 6054 | /* |
| 6055 | Crop_box is with respect to the upper left corner of the MNG. |
| 6056 | */ |
| 6057 | crop_box.left=mng_info->image_box.left+mng_info->x_off[object_id]; |
| 6058 | crop_box.right=mng_info->image_box.right+mng_info->x_off[object_id]; |
| 6059 | crop_box.top=mng_info->image_box.top+mng_info->y_off[object_id]; |
| 6060 | crop_box.bottom=mng_info->image_box.bottom+mng_info->y_off[object_id]; |
| 6061 | crop_box=mng_minimum_box(crop_box,mng_info->clip); |
| 6062 | crop_box=mng_minimum_box(crop_box,mng_info->frame); |
| 6063 | crop_box=mng_minimum_box(crop_box,mng_info->object_clip[object_id]); |
| 6064 | if ((crop_box.left != (mng_info->image_box.left |
| 6065 | +mng_info->x_off[object_id])) || |
| 6066 | (crop_box.right != (mng_info->image_box.right |
| 6067 | +mng_info->x_off[object_id])) || |
| 6068 | (crop_box.top != (mng_info->image_box.top |
| 6069 | +mng_info->y_off[object_id])) || |
| 6070 | (crop_box.bottom != (mng_info->image_box.bottom |
| 6071 | +mng_info->y_off[object_id]))) |
| 6072 | { |
| 6073 | if (logging != MagickFalse) |
| 6074 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6075 | " Crop the PNG image"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6076 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6077 | if ((crop_box.left < crop_box.right) && |
| 6078 | (crop_box.top < crop_box.bottom)) |
| 6079 | { |
| 6080 | Image |
| 6081 | *im; |
| 6082 | |
| 6083 | RectangleInfo |
| 6084 | crop_info; |
| 6085 | |
| 6086 | /* |
| 6087 | Crop_info is with respect to the upper left corner of |
| 6088 | the image. |
| 6089 | */ |
| 6090 | crop_info.x=(crop_box.left-mng_info->x_off[object_id]); |
| 6091 | crop_info.y=(crop_box.top-mng_info->y_off[object_id]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6092 | crop_info.width=(size_t) (crop_box.right-crop_box.left); |
| 6093 | crop_info.height=(size_t) (crop_box.bottom-crop_box.top); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6094 | image->page.width=image->columns; |
| 6095 | image->page.height=image->rows; |
| 6096 | image->page.x=0; |
| 6097 | image->page.y=0; |
| 6098 | im=CropImage(image,&crop_info,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6099 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6100 | if (im != (Image *) NULL) |
| 6101 | { |
| 6102 | image->columns=im->columns; |
| 6103 | image->rows=im->rows; |
| 6104 | im=DestroyImage(im); |
| 6105 | image->page.width=image->columns; |
| 6106 | image->page.height=image->rows; |
| 6107 | image->page.x=crop_box.left; |
| 6108 | image->page.y=crop_box.top; |
| 6109 | } |
| 6110 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6111 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6112 | else |
| 6113 | { |
| 6114 | /* |
| 6115 | No pixels in crop area. The MNG spec still requires |
| 6116 | a layer, though, so make a single transparent pixel in |
| 6117 | the top left corner. |
| 6118 | */ |
| 6119 | image->columns=1; |
| 6120 | image->rows=1; |
| 6121 | image->colors=2; |
| 6122 | (void) SetImageBackgroundColor(image); |
| 6123 | image->page.width=1; |
| 6124 | image->page.height=1; |
| 6125 | image->page.x=0; |
| 6126 | image->page.y=0; |
| 6127 | } |
| 6128 | } |
| 6129 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 6130 | image=mng_info->image; |
| 6131 | #endif |
| 6132 | } |
| 6133 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 6134 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
| 6135 | /* PNG does not handle depths greater than 16 so reduce it even |
| 6136 | * if lossy |
| 6137 | */ |
| 6138 | if (image->depth > 16) |
| 6139 | image->depth=16; |
| 6140 | #endif |
| 6141 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 6142 | #if (MAGICKCORE_QUANTUM_DEPTH > 8) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 6143 | if (LosslessReduceDepthOK(image) != MagickFalse) |
| 6144 | image->depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6145 | #endif |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6146 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6147 | GetImageException(image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6148 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6149 | if (image_info->number_scenes != 0) |
| 6150 | { |
| 6151 | if (mng_info->scenes_found > |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6152 | (ssize_t) (image_info->first_scene+image_info->number_scenes)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6153 | break; |
| 6154 | } |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6155 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6156 | if (logging != MagickFalse) |
| 6157 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6158 | " Finished reading image datastream."); |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6159 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6160 | } while (LocaleCompare(image_info->magick,"MNG") == 0); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6161 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6162 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6163 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6164 | if (logging != MagickFalse) |
| 6165 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6166 | " Finished reading all image datastreams."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6167 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6168 | #if defined(MNG_INSERT_LAYERS) |
| 6169 | if (insert_layers && !mng_info->image_found && (mng_info->mng_width) && |
| 6170 | (mng_info->mng_height)) |
| 6171 | { |
| 6172 | /* |
| 6173 | Insert a background layer if nothing else was found. |
| 6174 | */ |
| 6175 | if (logging != MagickFalse) |
| 6176 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6177 | " No images found. Inserting a background layer."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6178 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6179 | if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL) |
| 6180 | { |
| 6181 | /* |
| 6182 | Allocate next image structure. |
| 6183 | */ |
| 6184 | AcquireNextImage(image_info,image); |
| 6185 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6186 | { |
| 6187 | image=DestroyImageList(image); |
| 6188 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6189 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6190 | if (logging != MagickFalse) |
| 6191 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6192 | " Allocation failed, returning NULL."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6193 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6194 | return((Image *) NULL); |
| 6195 | } |
| 6196 | image=SyncNextImageInList(image); |
| 6197 | } |
| 6198 | image->columns=mng_info->mng_width; |
| 6199 | image->rows=mng_info->mng_height; |
| 6200 | image->page.width=mng_info->mng_width; |
| 6201 | image->page.height=mng_info->mng_height; |
| 6202 | image->page.x=0; |
| 6203 | image->page.y=0; |
| 6204 | image->background_color=mng_background_color; |
| 6205 | image->matte=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6206 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6207 | if (image_info->ping == MagickFalse) |
| 6208 | (void) SetImageBackgroundColor(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6209 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6210 | mng_info->image_found++; |
| 6211 | } |
| 6212 | #endif |
| 6213 | image->iterations=mng_iterations; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6214 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6215 | if (mng_iterations == 1) |
| 6216 | image->start_loop=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6217 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6218 | while (GetPreviousImageInList(image) != (Image *) NULL) |
| 6219 | { |
| 6220 | image_count++; |
| 6221 | if (image_count > 10*mng_info->image_found) |
| 6222 | { |
| 6223 | if (logging != MagickFalse) |
| 6224 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," No beginning"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6225 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6226 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 6227 | CoderError,"Linked list is corrupted, beginning of list not found", |
| 6228 | "`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6229 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6230 | return((Image *) NULL); |
| 6231 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6232 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6233 | image=GetPreviousImageInList(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6234 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6235 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6236 | { |
| 6237 | if (logging != MagickFalse) |
| 6238 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," Corrupt list"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6239 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6240 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 6241 | CoderError,"Linked list is corrupted; next_image is NULL","`%s'", |
| 6242 | image_info->filename); |
| 6243 | } |
| 6244 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6245 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6246 | if (mng_info->ticks_per_second && mng_info->image_found > 1 && |
| 6247 | GetNextImageInList(image) == |
| 6248 | (Image *) NULL) |
| 6249 | { |
| 6250 | if (logging != MagickFalse) |
| 6251 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6252 | " First image null"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6253 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6254 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 6255 | CoderError,"image->next for first image is NULL but shouldn't be.", |
| 6256 | "`%s'",image_info->filename); |
| 6257 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6258 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6259 | if (mng_info->image_found == 0) |
| 6260 | { |
| 6261 | if (logging != MagickFalse) |
| 6262 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6263 | " No visible images found."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6264 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6265 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 6266 | CoderError,"No visible images in file","`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6267 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6268 | if (image != (Image *) NULL) |
| 6269 | image=DestroyImageList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6270 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6271 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6272 | return((Image *) NULL); |
| 6273 | } |
| 6274 | |
| 6275 | if (mng_info->ticks_per_second) |
| 6276 | final_delay=1UL*MagickMax(image->ticks_per_second,1L)* |
| 6277 | final_delay/mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6278 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6279 | else |
| 6280 | image->start_loop=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6281 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6282 | /* Find final nonzero image delay */ |
| 6283 | final_image_delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6284 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6285 | while (GetNextImageInList(image) != (Image *) NULL) |
| 6286 | { |
| 6287 | if (image->delay) |
| 6288 | final_image_delay=image->delay; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6289 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6290 | image=GetNextImageInList(image); |
| 6291 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6292 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6293 | if (final_delay < final_image_delay) |
| 6294 | final_delay=final_image_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6295 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6296 | image->delay=final_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6297 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6298 | if (logging != MagickFalse) |
| 6299 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6300 | " image->delay=%.20g, final_delay=%.20g",(double) image->delay, |
| 6301 | (double) final_delay); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6302 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6303 | if (logging != MagickFalse) |
| 6304 | { |
| 6305 | int |
| 6306 | scene; |
| 6307 | |
| 6308 | scene=0; |
| 6309 | image=GetFirstImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6310 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6311 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6312 | " Before coalesce:"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6313 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6314 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6315 | " scene 0 delay=%.20g",(double) image->delay); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6316 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6317 | while (GetNextImageInList(image) != (Image *) NULL) |
| 6318 | { |
| 6319 | image=GetNextImageInList(image); |
| 6320 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6321 | " scene %.20g delay=%.20g",(double) scene++,(double) image->delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6322 | } |
| 6323 | } |
| 6324 | |
| 6325 | image=GetFirstImageInList(image); |
| 6326 | #ifdef MNG_COALESCE_LAYERS |
| 6327 | if (insert_layers) |
| 6328 | { |
| 6329 | Image |
| 6330 | *next_image, |
| 6331 | *next; |
| 6332 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6333 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6334 | scene; |
| 6335 | |
| 6336 | if (logging != MagickFalse) |
| 6337 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," Coalesce Images"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6338 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6339 | scene=image->scene; |
| 6340 | next_image=CoalesceImages(image,&image->exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6341 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6342 | if (next_image == (Image *) NULL) |
| 6343 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6344 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6345 | image=DestroyImageList(image); |
| 6346 | image=next_image; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6347 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6348 | for (next=image; next != (Image *) NULL; next=next_image) |
| 6349 | { |
| 6350 | next->page.width=mng_info->mng_width; |
| 6351 | next->page.height=mng_info->mng_height; |
| 6352 | next->page.x=0; |
| 6353 | next->page.y=0; |
| 6354 | next->scene=scene++; |
| 6355 | next_image=GetNextImageInList(next); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6356 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6357 | if (next_image == (Image *) NULL) |
| 6358 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6359 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6360 | if (next->delay == 0) |
| 6361 | { |
| 6362 | scene--; |
| 6363 | next_image->previous=GetPreviousImageInList(next); |
| 6364 | if (GetPreviousImageInList(next) == (Image *) NULL) |
| 6365 | image=next_image; |
| 6366 | else |
| 6367 | next->previous->next=next_image; |
| 6368 | next=DestroyImage(next); |
| 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | #endif |
| 6373 | |
| 6374 | while (GetNextImageInList(image) != (Image *) NULL) |
| 6375 | image=GetNextImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6376 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6377 | image->dispose=BackgroundDispose; |
| 6378 | |
| 6379 | if (logging != MagickFalse) |
| 6380 | { |
| 6381 | int |
| 6382 | scene; |
| 6383 | |
| 6384 | scene=0; |
| 6385 | image=GetFirstImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6386 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6387 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6388 | " After coalesce:"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6389 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6390 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6391 | " scene 0 delay=%.20g dispose=%.20g",(double) image->delay, |
| 6392 | (double) image->dispose); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6393 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6394 | while (GetNextImageInList(image) != (Image *) NULL) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 6395 | { |
| 6396 | image=GetNextImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6397 | |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 6398 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6399 | " scene %.20g delay=%.20g dispose=%.20g",(double) scene++, |
| 6400 | (double) image->delay,(double) image->dispose); |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 6401 | } |
| 6402 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6403 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6404 | image=GetFirstImageInList(image); |
| 6405 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6406 | have_mng_structure=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6407 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6408 | if (logging != MagickFalse) |
| 6409 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6410 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6411 | return(GetFirstImageInList(image)); |
| 6412 | } |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 6413 | #else /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6414 | static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 6415 | { |
| 6416 | printf("Your PNG library is too old: You have libpng-%s\n", |
| 6417 | PNG_LIBPNG_VER_STRING); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6418 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6419 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 6420 | "PNG library is too old","`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6421 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6422 | return(Image *) NULL; |
| 6423 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6424 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6425 | static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 6426 | { |
| 6427 | return(ReadPNGImage(image_info,exception)); |
| 6428 | } |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 6429 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6430 | #endif |
| 6431 | |
| 6432 | /* |
| 6433 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6434 | % % |
| 6435 | % % |
| 6436 | % % |
| 6437 | % R e g i s t e r P N G I m a g e % |
| 6438 | % % |
| 6439 | % % |
| 6440 | % % |
| 6441 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6442 | % |
| 6443 | % RegisterPNGImage() adds properties for the PNG image format to |
| 6444 | % the list of supported formats. The properties include the image format |
| 6445 | % tag, a method to read and/or write the format, whether the format |
| 6446 | % supports the saving of more than one frame to the same file or blob, |
| 6447 | % whether the format supports native in-memory I/O, and a brief |
| 6448 | % description of the format. |
| 6449 | % |
| 6450 | % The format of the RegisterPNGImage method is: |
| 6451 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6452 | % size_t RegisterPNGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6453 | % |
| 6454 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6455 | ModuleExport size_t RegisterPNGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6456 | { |
| 6457 | char |
| 6458 | version[MaxTextExtent]; |
| 6459 | |
| 6460 | MagickInfo |
| 6461 | *entry; |
| 6462 | |
| 6463 | static const char |
| 6464 | *PNGNote= |
| 6465 | { |
| 6466 | "See http://www.libpng.org/ for details about the PNG format." |
| 6467 | }, |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6468 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6469 | *JNGNote= |
| 6470 | { |
| 6471 | "See http://www.libpng.org/pub/mng/ for details about the JNG\n" |
| 6472 | "format." |
| 6473 | }, |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6474 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6475 | *MNGNote= |
| 6476 | { |
| 6477 | "See http://www.libpng.org/pub/mng/ for details about the MNG\n" |
| 6478 | "format." |
| 6479 | }; |
| 6480 | |
| 6481 | *version='\0'; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6482 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6483 | #if defined(PNG_LIBPNG_VER_STRING) |
| 6484 | (void) ConcatenateMagickString(version,"libpng ",MaxTextExtent); |
| 6485 | (void) ConcatenateMagickString(version,PNG_LIBPNG_VER_STRING,MaxTextExtent); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6486 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6487 | if (LocaleCompare(PNG_LIBPNG_VER_STRING,png_get_header_ver(NULL)) != 0) |
| 6488 | { |
| 6489 | (void) ConcatenateMagickString(version,",",MaxTextExtent); |
| 6490 | (void) ConcatenateMagickString(version,png_get_libpng_ver(NULL), |
| 6491 | MaxTextExtent); |
| 6492 | } |
| 6493 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6494 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6495 | entry=SetMagickInfo("MNG"); |
| 6496 | entry->seekable_stream=MagickTrue; /* To do: eliminate this. */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6497 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6498 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 6499 | entry->decoder=(DecodeImageHandler *) ReadMNGImage; |
| 6500 | entry->encoder=(EncodeImageHandler *) WriteMNGImage; |
| 6501 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6502 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6503 | entry->magick=(IsImageFormatHandler *) IsMNG; |
| 6504 | entry->description=ConstantString("Multiple-image Network Graphics"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6505 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6506 | if (*version != '\0') |
| 6507 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6508 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6509 | entry->module=ConstantString("PNG"); |
| 6510 | entry->note=ConstantString(MNGNote); |
| 6511 | (void) RegisterMagickInfo(entry); |
| 6512 | |
| 6513 | entry=SetMagickInfo("PNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6514 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6515 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 6516 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 6517 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 6518 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6519 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6520 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 6521 | entry->adjoin=MagickFalse; |
| 6522 | entry->description=ConstantString("Portable Network Graphics"); |
| 6523 | entry->module=ConstantString("PNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6524 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6525 | if (*version != '\0') |
| 6526 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6527 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6528 | entry->note=ConstantString(PNGNote); |
| 6529 | (void) RegisterMagickInfo(entry); |
| 6530 | |
| 6531 | entry=SetMagickInfo("PNG8"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6532 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6533 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 6534 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 6535 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 6536 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6538 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 6539 | entry->adjoin=MagickFalse; |
| 6540 | entry->description=ConstantString( |
| 6541 | "8-bit indexed with optional binary transparency"); |
| 6542 | entry->module=ConstantString("PNG"); |
| 6543 | (void) RegisterMagickInfo(entry); |
| 6544 | |
| 6545 | entry=SetMagickInfo("PNG24"); |
| 6546 | *version='\0'; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6547 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6548 | #if defined(ZLIB_VERSION) |
| 6549 | (void) ConcatenateMagickString(version,"zlib ",MaxTextExtent); |
| 6550 | (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6551 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6552 | if (LocaleCompare(ZLIB_VERSION,zlib_version) != 0) |
| 6553 | { |
| 6554 | (void) ConcatenateMagickString(version,",",MaxTextExtent); |
| 6555 | (void) ConcatenateMagickString(version,zlib_version,MaxTextExtent); |
| 6556 | } |
| 6557 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6558 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6559 | if (*version != '\0') |
| 6560 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6561 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6562 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 6563 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 6564 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 6565 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6566 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6567 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 6568 | entry->adjoin=MagickFalse; |
| 6569 | entry->description=ConstantString("opaque 24-bit RGB"); |
| 6570 | entry->module=ConstantString("PNG"); |
| 6571 | (void) RegisterMagickInfo(entry); |
| 6572 | |
| 6573 | entry=SetMagickInfo("PNG32"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6574 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6575 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 6576 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 6577 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 6578 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6579 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6580 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 6581 | entry->adjoin=MagickFalse; |
| 6582 | entry->description=ConstantString("opaque or transparent 32-bit RGBA"); |
| 6583 | entry->module=ConstantString("PNG"); |
| 6584 | (void) RegisterMagickInfo(entry); |
| 6585 | |
| 6586 | entry=SetMagickInfo("JNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6587 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6588 | #if defined(JNG_SUPPORTED) |
| 6589 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 6590 | entry->decoder=(DecodeImageHandler *) ReadJNGImage; |
| 6591 | entry->encoder=(EncodeImageHandler *) WriteJNGImage; |
| 6592 | #endif |
| 6593 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6594 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6595 | entry->magick=(IsImageFormatHandler *) IsJNG; |
| 6596 | entry->adjoin=MagickFalse; |
| 6597 | entry->description=ConstantString("JPEG Network Graphics"); |
| 6598 | entry->module=ConstantString("PNG"); |
| 6599 | entry->note=ConstantString(JNGNote); |
| 6600 | (void) RegisterMagickInfo(entry); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6601 | |
cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 6602 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6603 | ping_semaphore=AllocateSemaphoreInfo(); |
cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 6604 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6605 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6606 | return(MagickImageCoderSignature); |
| 6607 | } |
| 6608 | |
| 6609 | /* |
| 6610 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6611 | % % |
| 6612 | % % |
| 6613 | % % |
| 6614 | % U n r e g i s t e r P N G I m a g e % |
| 6615 | % % |
| 6616 | % % |
| 6617 | % % |
| 6618 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6619 | % |
| 6620 | % UnregisterPNGImage() removes format registrations made by the |
| 6621 | % PNG module from the list of supported formats. |
| 6622 | % |
| 6623 | % The format of the UnregisterPNGImage method is: |
| 6624 | % |
| 6625 | % UnregisterPNGImage(void) |
| 6626 | % |
| 6627 | */ |
| 6628 | ModuleExport void UnregisterPNGImage(void) |
| 6629 | { |
| 6630 | (void) UnregisterMagickInfo("MNG"); |
| 6631 | (void) UnregisterMagickInfo("PNG"); |
| 6632 | (void) UnregisterMagickInfo("PNG8"); |
| 6633 | (void) UnregisterMagickInfo("PNG24"); |
| 6634 | (void) UnregisterMagickInfo("PNG32"); |
| 6635 | (void) UnregisterMagickInfo("JNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6636 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6637 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6638 | if (ping_semaphore != (SemaphoreInfo *) NULL) |
| 6639 | DestroySemaphoreInfo(&ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6640 | #endif |
| 6641 | } |
| 6642 | |
| 6643 | #if defined(MAGICKCORE_PNG_DELEGATE) |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 6644 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6645 | /* |
| 6646 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6647 | % % |
| 6648 | % % |
| 6649 | % % |
| 6650 | % W r i t e M N G I m a g e % |
| 6651 | % % |
| 6652 | % % |
| 6653 | % % |
| 6654 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6655 | % |
| 6656 | % WriteMNGImage() writes an image in the Portable Network Graphics |
| 6657 | % Group's "Multiple-image Network Graphics" encoded image format. |
| 6658 | % |
| 6659 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 6660 | % |
| 6661 | % The format of the WriteMNGImage method is: |
| 6662 | % |
| 6663 | % MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image) |
| 6664 | % |
| 6665 | % A description of each parameter follows. |
| 6666 | % |
| 6667 | % o image_info: the image info. |
| 6668 | % |
| 6669 | % o image: The image. |
| 6670 | % |
| 6671 | % |
| 6672 | % To do (as of version 5.5.2, November 26, 2002 -- glennrp -- see also |
| 6673 | % "To do" under ReadPNGImage): |
| 6674 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6675 | % Preserve all unknown and not-yet-handled known chunks found in input |
| 6676 | % PNG file and copy them into output PNG files according to the PNG |
| 6677 | % copying rules. |
| 6678 | % |
| 6679 | % Write the iCCP chunk at MNG level when (icc profile length > 0) |
| 6680 | % |
| 6681 | % Improve selection of color type (use indexed-colour or indexed-colour |
| 6682 | % with tRNS when 256 or fewer unique RGBA values are present). |
| 6683 | % |
| 6684 | % Figure out what to do with "dispose=<restore-to-previous>" (dispose == 3) |
| 6685 | % This will be complicated if we limit ourselves to generating MNG-LC |
| 6686 | % files. For now we ignore disposal method 3 and simply overlay the next |
| 6687 | % image on it. |
| 6688 | % |
| 6689 | % Check for identical PLTE's or PLTE/tRNS combinations and use a |
| 6690 | % global MNG PLTE or PLTE/tRNS combination when appropriate. |
| 6691 | % [mostly done 15 June 1999 but still need to take care of tRNS] |
| 6692 | % |
| 6693 | % Check for identical sRGB and replace with a global sRGB (and remove |
| 6694 | % gAMA/cHRM if sRGB is found; check for identical gAMA/cHRM and |
| 6695 | % replace with global gAMA/cHRM (or with sRGB if appropriate; replace |
| 6696 | % local gAMA/cHRM with local sRGB if appropriate). |
| 6697 | % |
| 6698 | % Check for identical sBIT chunks and write global ones. |
| 6699 | % |
| 6700 | % Provide option to skip writing the signature tEXt chunks. |
| 6701 | % |
| 6702 | % Use signatures to detect identical objects and reuse the first |
| 6703 | % instance of such objects instead of writing duplicate objects. |
| 6704 | % |
| 6705 | % Use a smaller-than-32k value of compression window size when |
| 6706 | % appropriate. |
| 6707 | % |
| 6708 | % Encode JNG datastreams. Mostly done as of 5.5.2; need to write |
| 6709 | % ancillary text chunks and save profiles. |
| 6710 | % |
| 6711 | % Provide an option to force LC files (to ensure exact framing rate) |
| 6712 | % instead of VLC. |
| 6713 | % |
| 6714 | % Provide an option to force VLC files instead of LC, even when offsets |
| 6715 | % are present. This will involve expanding the embedded images with a |
| 6716 | % transparent region at the top and/or left. |
| 6717 | */ |
| 6718 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6719 | static void |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6720 | Magick_png_write_raw_profile(const ImageInfo *image_info,png_struct *ping, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6721 | png_info *ping_info, unsigned char *profile_type, unsigned char |
| 6722 | *profile_description, unsigned char *profile_data, png_uint_32 length) |
| 6723 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6724 | png_textp |
| 6725 | text; |
| 6726 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6727 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6728 | i; |
| 6729 | |
| 6730 | unsigned char |
| 6731 | *sp; |
| 6732 | |
| 6733 | png_charp |
| 6734 | dp; |
| 6735 | |
| 6736 | png_uint_32 |
| 6737 | allocated_length, |
| 6738 | description_length; |
| 6739 | |
| 6740 | unsigned char |
| 6741 | hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6742 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6743 | if (LocaleNCompare((char *) profile_type+1, "ng-chunk-",9) == 0) |
| 6744 | return; |
| 6745 | |
| 6746 | if (image_info->verbose) |
| 6747 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6748 | (void) printf("writing raw profile: type=%s, length=%.20g\n", |
| 6749 | (char *) profile_type, (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6750 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6751 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6752 | text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text)); |
| 6753 | description_length=(png_uint_32) strlen((const char *) profile_description); |
| 6754 | allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20 |
| 6755 | + description_length); |
| 6756 | text[0].text=(png_charp) png_malloc(ping,allocated_length); |
| 6757 | text[0].key=(png_charp) png_malloc(ping, (png_uint_32) 80); |
| 6758 | text[0].key[0]='\0'; |
| 6759 | (void) ConcatenateMagickString(text[0].key, |
| 6760 | "Raw profile type ",MaxTextExtent); |
| 6761 | (void) ConcatenateMagickString(text[0].key,(const char *) profile_type,62); |
| 6762 | sp=profile_data; |
| 6763 | dp=text[0].text; |
| 6764 | *dp++='\n'; |
| 6765 | (void) CopyMagickString(dp,(const char *) profile_description, |
| 6766 | allocated_length); |
| 6767 | dp+=description_length; |
| 6768 | *dp++='\n'; |
| 6769 | (void) FormatMagickString(dp,allocated_length- |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 6770 | (png_size_t) (dp-text[0].text),"%8lu ",(unsigned long) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6771 | dp+=8; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6772 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6773 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6774 | { |
| 6775 | if (i%36 == 0) |
| 6776 | *dp++='\n'; |
| 6777 | *(dp++)=(char) hex[((*sp >> 4) & 0x0f)]; |
| 6778 | *(dp++)=(char) hex[((*sp++ ) & 0x0f)]; |
| 6779 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6780 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6781 | *dp++='\n'; |
| 6782 | *dp='\0'; |
| 6783 | text[0].text_length=(png_size_t) (dp-text[0].text); |
| 6784 | text[0].compression=image_info->compression == NoCompression || |
| 6785 | (image_info->compression == UndefinedCompression && |
| 6786 | text[0].text_length < 128) ? -1 : 0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6787 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6788 | if (text[0].text_length <= allocated_length) |
| 6789 | png_set_text(ping,ping_info,text,1); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6790 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6791 | png_free(ping,text[0].text); |
| 6792 | png_free(ping,text[0].key); |
| 6793 | png_free(ping,text); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6794 | } |
| 6795 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6796 | static MagickBooleanType Magick_png_write_chunk_from_profile(Image *image, |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 6797 | const char *string, MagickBooleanType logging) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6798 | { |
| 6799 | char |
| 6800 | *name; |
| 6801 | |
| 6802 | const StringInfo |
| 6803 | *profile; |
| 6804 | |
| 6805 | unsigned char |
| 6806 | *data; |
| 6807 | |
| 6808 | png_uint_32 length; |
| 6809 | |
| 6810 | ResetImageProfileIterator(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6811 | |
| 6812 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 6813 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6814 | profile=GetImageProfile(image,name); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6815 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6816 | if (profile != (const StringInfo *) NULL) |
| 6817 | { |
| 6818 | StringInfo |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6819 | *ping_profile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6820 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6821 | if (LocaleNCompare(name,string,11) == 0) |
| 6822 | { |
| 6823 | if (logging != MagickFalse) |
| 6824 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6825 | " Found %s profile",name); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6826 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6827 | ping_profile=CloneStringInfo(profile); |
| 6828 | data=GetStringInfoDatum(ping_profile), |
| 6829 | length=(png_uint_32) GetStringInfoLength(ping_profile); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6830 | data[4]=data[3]; |
| 6831 | data[3]=data[2]; |
| 6832 | data[2]=data[1]; |
| 6833 | data[1]=data[0]; |
| 6834 | (void) WriteBlobMSBULong(image,length-5); /* data length */ |
| 6835 | (void) WriteBlob(image,length-1,data+1); |
| 6836 | (void) WriteBlobMSBULong(image,crc32(0,data+1,(uInt) length-1)); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6837 | ping_profile=DestroyStringInfo(ping_profile); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6838 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6839 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6840 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6841 | name=GetNextImageProfile(image); |
| 6842 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6843 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6844 | return(MagickTrue); |
| 6845 | } |
| 6846 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 6847 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6848 | /* Write one PNG image */ |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 6849 | static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info, |
| 6850 | const ImageInfo *IMimage_info,Image *IMimage) |
| 6851 | { |
| 6852 | Image |
| 6853 | *image; |
| 6854 | |
| 6855 | ImageInfo |
| 6856 | *image_info; |
| 6857 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6858 | char |
| 6859 | s[2]; |
| 6860 | |
| 6861 | const char |
| 6862 | *name, |
| 6863 | *property, |
| 6864 | *value; |
| 6865 | |
| 6866 | const StringInfo |
| 6867 | *profile; |
| 6868 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6869 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6870 | num_passes, |
glennrp | cecd576 | 2010-03-23 12:07:49 +0000 | [diff] [blame] | 6871 | pass; |
| 6872 | |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 6873 | png_byte |
| 6874 | ping_trans_alpha[256]; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 6875 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 6876 | png_color |
| 6877 | palette[257]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6878 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 6879 | png_color_16 |
| 6880 | ping_background, |
| 6881 | ping_trans_color; |
| 6882 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6883 | png_info |
| 6884 | *ping_info; |
| 6885 | |
| 6886 | png_struct |
| 6887 | *ping; |
| 6888 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 6889 | png_uint_32 |
| 6890 | ping_height, |
| 6891 | ping_width; |
| 6892 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6893 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6894 | y; |
| 6895 | |
| 6896 | MagickBooleanType |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 6897 | image_matte, |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 6898 | logging, |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 6899 | matte, |
| 6900 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 6901 | ping_have_blob, |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 6902 | ping_have_cheap_transparency, |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 6903 | ping_have_color, |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 6904 | ping_have_non_bw, |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 6905 | ping_have_PLTE, |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 6906 | ping_have_bKGD, |
| 6907 | ping_have_pHYs, |
| 6908 | ping_have_tRNS, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 6909 | |
| 6910 | ping_exclude_bKGD, |
| 6911 | ping_exclude_cHRM, |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 6912 | ping_exclude_date, |
glennrp | e4e2d79 | 2011-02-21 12:11:27 +0000 | [diff] [blame] | 6913 | /* ping_exclude_EXIF, */ |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 6914 | ping_exclude_gAMA, |
| 6915 | ping_exclude_iCCP, |
| 6916 | /* ping_exclude_iTXt, */ |
| 6917 | ping_exclude_oFFs, |
| 6918 | ping_exclude_pHYs, |
| 6919 | ping_exclude_sRGB, |
| 6920 | ping_exclude_tEXt, |
glennrp | e4e2d79 | 2011-02-21 12:11:27 +0000 | [diff] [blame] | 6921 | /* ping_exclude_tRNS, */ |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 6922 | ping_exclude_vpAg, |
| 6923 | ping_exclude_zCCP, /* hex-encoded iCCP */ |
| 6924 | ping_exclude_zTXt, |
| 6925 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 6926 | ping_preserve_colormap, |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 6927 | ping_need_colortype_warning, |
| 6928 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 6929 | status, |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 6930 | tried_333, |
| 6931 | tried_444; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6932 | |
| 6933 | QuantumInfo |
| 6934 | *quantum_info; |
| 6935 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6936 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6937 | i, |
| 6938 | x; |
| 6939 | |
| 6940 | unsigned char |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6941 | *ping_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6942 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 6943 | volatile int |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 6944 | image_colors, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6945 | ping_bit_depth, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 6946 | ping_color_type, |
| 6947 | ping_interlace_method, |
| 6948 | ping_compression_method, |
| 6949 | ping_filter_method, |
| 6950 | ping_num_trans; |
| 6951 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6952 | volatile size_t |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 6953 | image_depth, |
| 6954 | old_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6955 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6956 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6957 | quality, |
| 6958 | rowbytes, |
| 6959 | save_image_depth; |
| 6960 | |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 6961 | int |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 6962 | j, |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 6963 | number_colors, |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 6964 | number_opaque, |
| 6965 | number_semitransparent, |
| 6966 | number_transparent, |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 6967 | ping_pHYs_unit_type; |
| 6968 | |
| 6969 | png_uint_32 |
| 6970 | ping_pHYs_x_resolution, |
| 6971 | ping_pHYs_y_resolution; |
| 6972 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6973 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 6974 | " Enter WriteOnePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6975 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 6976 | image = CloneImage(IMimage,0,0,MagickFalse,&IMimage->exception); |
| 6977 | image_info=(ImageInfo *) CloneImageInfo(IMimage_info); |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 6978 | if (image_info == (ImageInfo *) NULL) |
glennrp | 7b2cc79 | 2011-04-18 16:46:02 +0000 | [diff] [blame] | 6979 | ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed"); |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 6980 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6981 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 6982 | LockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6983 | #endif |
| 6984 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 6985 | /* Initialize some stuff */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6986 | ping_bit_depth=0, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 6987 | ping_color_type=0, |
| 6988 | ping_interlace_method=0, |
| 6989 | ping_compression_method=0, |
| 6990 | ping_filter_method=0, |
| 6991 | ping_num_trans = 0; |
| 6992 | |
| 6993 | ping_background.red = 0; |
| 6994 | ping_background.green = 0; |
| 6995 | ping_background.blue = 0; |
| 6996 | ping_background.gray = 0; |
| 6997 | ping_background.index = 0; |
| 6998 | |
| 6999 | ping_trans_color.red=0; |
| 7000 | ping_trans_color.green=0; |
| 7001 | ping_trans_color.blue=0; |
| 7002 | ping_trans_color.gray=0; |
| 7003 | |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 7004 | ping_pHYs_unit_type = 0; |
| 7005 | ping_pHYs_x_resolution = 0; |
| 7006 | ping_pHYs_y_resolution = 0; |
| 7007 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 7008 | ping_have_blob=MagickFalse; |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 7009 | ping_have_color=MagickTrue; |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7010 | ping_have_non_bw=MagickTrue; |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7011 | ping_have_PLTE=MagickFalse; |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 7012 | ping_have_bKGD=MagickFalse; |
| 7013 | ping_have_pHYs=MagickFalse; |
| 7014 | ping_have_tRNS=MagickFalse; |
| 7015 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7016 | ping_exclude_bKGD=mng_info->ping_exclude_bKGD; |
| 7017 | ping_exclude_cHRM=mng_info->ping_exclude_cHRM; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 7018 | ping_exclude_date=mng_info->ping_exclude_date; |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 7019 | /* ping_exclude_EXIF=mng_info->ping_exclude_EXIF; */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7020 | ping_exclude_gAMA=mng_info->ping_exclude_gAMA; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7021 | ping_exclude_iCCP=mng_info->ping_exclude_iCCP; |
| 7022 | /* ping_exclude_iTXt=mng_info->ping_exclude_iTXt; */ |
| 7023 | ping_exclude_oFFs=mng_info->ping_exclude_oFFs; |
| 7024 | ping_exclude_pHYs=mng_info->ping_exclude_pHYs; |
| 7025 | ping_exclude_sRGB=mng_info->ping_exclude_sRGB; |
| 7026 | ping_exclude_tEXt=mng_info->ping_exclude_tEXt; |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 7027 | /* ping_exclude_tRNS=mng_info->ping_exclude_tRNS; */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7028 | ping_exclude_vpAg=mng_info->ping_exclude_vpAg; |
| 7029 | ping_exclude_zCCP=mng_info->ping_exclude_zCCP; /* hex-encoded iCCP in zTXt */ |
| 7030 | ping_exclude_zTXt=mng_info->ping_exclude_zTXt; |
| 7031 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7032 | ping_preserve_colormap = mng_info->ping_preserve_colormap; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7033 | ping_need_colortype_warning = MagickFalse; |
| 7034 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7035 | number_opaque = 0; |
| 7036 | number_semitransparent = 0; |
| 7037 | number_transparent = 0; |
| 7038 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7039 | if (logging != MagickFalse) |
| 7040 | { |
| 7041 | if (image->storage_class == UndefinedClass) |
| 7042 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7043 | " storage_class=UndefinedClass"); |
| 7044 | if (image->storage_class == DirectClass) |
| 7045 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7046 | " storage_class=DirectClass"); |
| 7047 | if (image->storage_class == PseudoClass) |
| 7048 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7049 | " storage_class=PseudoClass"); |
| 7050 | } |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 7051 | |
| 7052 | if (image->storage_class != PseudoClass && image->colormap != NULL) |
| 7053 | { |
| 7054 | /* Free the bogus colormap; it can cause trouble later */ |
| 7055 | if (logging != MagickFalse) |
| 7056 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7057 | " Freeing bogus colormap"); |
| 7058 | (void *) RelinquishMagickMemory(image->colormap); |
| 7059 | image->colormap=NULL; |
| 7060 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7061 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7062 | if (image->colorspace != RGBColorspace) |
| 7063 | (void) TransformImageColorspace(image,RGBColorspace); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7064 | |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 7065 | /* |
| 7066 | Sometimes we get PseudoClass images whose RGB values don't match |
| 7067 | the colors in the colormap. This code syncs the RGB values. |
| 7068 | */ |
| 7069 | if (image->depth <= 8 && image->taint && image->storage_class == PseudoClass) |
| 7070 | (void) SyncImage(image); |
| 7071 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 7072 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 7073 | if (image->depth > 8) |
| 7074 | { |
| 7075 | if (logging != MagickFalse) |
| 7076 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7077 | " Reducing PNG bit depth to 8 since this is a Q8 build."); |
| 7078 | |
| 7079 | image->depth=8; |
| 7080 | } |
| 7081 | #endif |
| 7082 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 7083 | #if 0 /* To do: Option to use the original colormap */ |
| 7084 | if (ping_preserve_colormap != MagickFalse) |
| 7085 | { |
| 7086 | } |
| 7087 | #endif |
| 7088 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7089 | #if 0 /* To do: respect the -depth option */ |
glennrp | cc95c3f | 2011-04-18 16:46:48 +0000 | [diff] [blame] | 7090 | if (image->depth < MAGICKCORE_QUANTUM_DEPTH) |
| 7091 | { |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 7092 | } |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 7093 | #endif |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 7094 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 7095 | /* To do: set to next higher multiple of 8 */ |
| 7096 | if (image->depth < 8) |
glennrp | 70e68a8 | 2011-04-01 22:51:14 +0000 | [diff] [blame] | 7097 | image->depth=8; |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 7098 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 7099 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
| 7100 | /* PNG does not handle depths greater than 16 so reduce it even |
| 7101 | * if lossy |
| 7102 | */ |
| 7103 | if (image->depth > 16) |
| 7104 | image->depth=16; |
| 7105 | #endif |
| 7106 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7107 | #if (MAGICKCORE_QUANTUM_DEPTH > 8) |
glennrp | c722dd8 | 2011-02-24 05:13:21 +0000 | [diff] [blame] | 7108 | if (image->depth == 16 && mng_info->write_png_depth != 16) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7109 | if (mng_info->write_png8 || LosslessReduceDepthOK(image) != MagickFalse) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 7110 | image->depth = 8; |
| 7111 | #endif |
| 7112 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7113 | /* Normally we run this just once, but in the case of writing PNG8 |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 7114 | * we reduce the transparency to binary and run again, then if there |
| 7115 | * are still too many colors we reduce to a simple 4-4-4-1, then 3-3-3-1 |
| 7116 | * RGBA palette and run again, and finally to a simple 3-3-2-1 RGBA |
| 7117 | * palette. The final reduction can only fail if there are still 256 |
| 7118 | * colors present and one of them has both transparent and opaque instances. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7119 | */ |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7120 | |
| 7121 | tried_333 = MagickFalse; |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7122 | tried_444 = MagickFalse; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7123 | |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7124 | for (j=0; j<5; j++) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7125 | { |
| 7126 | /* BUILD_PALETTE |
| 7127 | * |
| 7128 | * Sometimes we get DirectClass images that have 256 colors or fewer. |
| 7129 | * This code will build a colormap. |
| 7130 | * |
| 7131 | * Also, sometimes we get PseudoClass images with an out-of-date |
| 7132 | * colormap. This code will replace the colormap with a new one. |
| 7133 | * Sometimes we get PseudoClass images that have more than 256 colors. |
| 7134 | * This code will delete the colormap and change the image to |
| 7135 | * DirectClass. |
| 7136 | * |
| 7137 | * If image->matte is MagickFalse, we ignore the opacity channel |
| 7138 | * even though it sometimes contains left-over non-opaque values. |
| 7139 | * |
| 7140 | * Also we gather some information (number of opaque, transparent, |
| 7141 | * and semitransparent pixels, and whether the image has any non-gray |
| 7142 | * pixels or only black-and-white pixels) that we might need later. |
| 7143 | * |
| 7144 | * Even if the user wants to force GrayAlpha or RGBA (colortype 4 or 6) |
| 7145 | * we need to check for bogus non-opaque values, at least. |
| 7146 | */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 7147 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7148 | ExceptionInfo |
| 7149 | *exception; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 7150 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7151 | int |
| 7152 | n; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 7153 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7154 | PixelPacket |
| 7155 | opaque[260], |
| 7156 | semitransparent[260], |
| 7157 | transparent[260]; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7158 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7159 | register IndexPacket |
| 7160 | *indexes; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7161 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7162 | register const PixelPacket |
| 7163 | *s, |
| 7164 | *q; |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 7165 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7166 | register PixelPacket |
| 7167 | *r; |
| 7168 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7169 | if (logging != MagickFalse) |
| 7170 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7171 | " Enter BUILD_PALETTE:"); |
| 7172 | |
| 7173 | if (logging != MagickFalse) |
| 7174 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7175 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7176 | " image->columns=%.20g",(double) image->columns); |
| 7177 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7178 | " image->rows=%.20g",(double) image->rows); |
| 7179 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7180 | " image->matte=%.20g",(double) image->matte); |
| 7181 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7182 | " image->depth=%.20g",(double) image->depth); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7183 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7184 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 7185 | { |
| 7186 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7187 | " Original colormap:"); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7188 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7189 | " i (red,green,blue,opacity)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 7190 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7191 | for (i=0; i < 256; i++) |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 7192 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7193 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7194 | " %d (%d,%d,%d,%d)", |
| 7195 | (int) i, |
| 7196 | (int) image->colormap[i].red, |
| 7197 | (int) image->colormap[i].green, |
| 7198 | (int) image->colormap[i].blue, |
| 7199 | (int) image->colormap[i].opacity); |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 7200 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 7201 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7202 | for (i=image->colors - 10; i < (ssize_t) image->colors; i++) |
| 7203 | { |
| 7204 | if (i > 255) |
| 7205 | { |
| 7206 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7207 | " %d (%d,%d,%d,%d)", |
| 7208 | (int) i, |
| 7209 | (int) image->colormap[i].red, |
| 7210 | (int) image->colormap[i].green, |
| 7211 | (int) image->colormap[i].blue, |
| 7212 | (int) image->colormap[i].opacity); |
| 7213 | } |
| 7214 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7215 | } |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 7216 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7217 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7218 | " image->colors=%d",(int) image->colors); |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 7219 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7220 | if (image->colors == 0) |
| 7221 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7222 | " (zero means unknown)"); |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 7223 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7224 | if (ping_preserve_colormap == MagickFalse) |
| 7225 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7226 | " Regenerate the colormap"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7227 | } |
| 7228 | |
| 7229 | exception=(&image->exception); |
| 7230 | |
| 7231 | image_colors=0; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7232 | number_opaque = 0; |
| 7233 | number_semitransparent = 0; |
| 7234 | number_transparent = 0; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7235 | |
| 7236 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7237 | { |
| 7238 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 7239 | |
| 7240 | if (q == (PixelPacket *) NULL) |
| 7241 | break; |
| 7242 | |
| 7243 | for (x=0; x < (ssize_t) image->columns; x++) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7244 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7245 | if (image->matte == MagickFalse || q->opacity == OpaqueOpacity) |
| 7246 | { |
| 7247 | if (number_opaque < 259) |
| 7248 | { |
| 7249 | if (number_opaque == 0) |
| 7250 | { |
| 7251 | opaque[0]=*q; |
| 7252 | opaque[0].opacity=OpaqueOpacity; |
| 7253 | number_opaque=1; |
| 7254 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 7255 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7256 | for (i=0; i< (ssize_t) number_opaque; i++) |
| 7257 | { |
| 7258 | if (IsColorEqual(opaque+i, (PixelPacket *) q)) |
| 7259 | break; |
| 7260 | } |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 7261 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7262 | if (i == (ssize_t) number_opaque && |
| 7263 | number_opaque < 259) |
| 7264 | { |
| 7265 | number_opaque++; |
| 7266 | opaque[i] = *q; |
| 7267 | opaque[i].opacity = OpaqueOpacity; |
| 7268 | } |
| 7269 | } |
| 7270 | } |
| 7271 | else if (q->opacity == TransparentOpacity) |
| 7272 | { |
| 7273 | if (number_transparent < 259) |
| 7274 | { |
| 7275 | if (number_transparent == 0) |
| 7276 | { |
| 7277 | transparent[0]=*q; |
| 7278 | ping_trans_color.red=(unsigned short)(q->red); |
| 7279 | ping_trans_color.green=(unsigned short) (q->green); |
| 7280 | ping_trans_color.blue=(unsigned short) (q->blue); |
| 7281 | ping_trans_color.gray=(unsigned short) (q->blue); |
| 7282 | number_transparent = 1; |
| 7283 | } |
| 7284 | |
| 7285 | for (i=0; i< (ssize_t) number_transparent; i++) |
| 7286 | { |
| 7287 | if (IsColorEqual(transparent+i, (PixelPacket *) q)) |
| 7288 | break; |
| 7289 | } |
| 7290 | |
| 7291 | if (i == (ssize_t) number_transparent && |
| 7292 | number_transparent < 259) |
| 7293 | { |
| 7294 | number_transparent++; |
| 7295 | transparent[i] = *q; |
| 7296 | } |
| 7297 | } |
| 7298 | } |
| 7299 | else |
| 7300 | { |
| 7301 | if (number_semitransparent < 259) |
| 7302 | { |
| 7303 | if (number_semitransparent == 0) |
| 7304 | { |
| 7305 | semitransparent[0]=*q; |
| 7306 | number_semitransparent = 1; |
| 7307 | } |
| 7308 | |
| 7309 | for (i=0; i< (ssize_t) number_semitransparent; i++) |
| 7310 | { |
| 7311 | if (IsColorEqual(semitransparent+i, |
| 7312 | (PixelPacket *) q) && |
| 7313 | q->opacity == semitransparent[i].opacity) |
| 7314 | break; |
| 7315 | } |
| 7316 | |
| 7317 | if (i == (ssize_t) number_semitransparent && |
| 7318 | number_semitransparent < 259) |
| 7319 | { |
| 7320 | number_semitransparent++; |
| 7321 | semitransparent[i] = *q; |
| 7322 | } |
| 7323 | } |
| 7324 | } |
| 7325 | q++; |
| 7326 | } |
| 7327 | } |
| 7328 | |
| 7329 | if (ping_exclude_bKGD == MagickFalse) |
| 7330 | { |
| 7331 | /* Add the background color to the palette, if it |
| 7332 | * isn't already there. |
| 7333 | */ |
| 7334 | for (i=0; i<number_opaque; i++) |
| 7335 | { |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 7336 | if (IsColorEqual(opaque+i, &image->background_color)) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7337 | break; |
| 7338 | } |
| 7339 | |
| 7340 | if (number_opaque < 259 && i == number_opaque) |
| 7341 | { |
| 7342 | opaque[i]=image->background_color; |
| 7343 | opaque[i].opacity = OpaqueOpacity; |
| 7344 | number_opaque++; |
| 7345 | } |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 7346 | else if (logging != MagickFalse) |
| 7347 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7348 | " No room in the colormap to add background color"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7349 | } |
| 7350 | |
| 7351 | image_colors=number_opaque+number_transparent+number_semitransparent; |
| 7352 | |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 7353 | if (mng_info->write_png8 != MagickFalse && image_colors > 256) |
| 7354 | { |
| 7355 | /* No room for the background color; remove it. */ |
| 7356 | number_opaque--; |
| 7357 | image_colors--; |
| 7358 | } |
| 7359 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7360 | if (logging != MagickFalse) |
| 7361 | { |
| 7362 | if (image_colors > 256) |
| 7363 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7364 | " image has more than 256 colors"); |
| 7365 | |
| 7366 | else |
| 7367 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7368 | " image has %d colors",image_colors); |
| 7369 | } |
| 7370 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7371 | if (ping_preserve_colormap != MagickFalse) |
| 7372 | break; |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7373 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7374 | if (mng_info->write_png_colortype != 7) /* We won't need this info */ |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7375 | { |
| 7376 | ping_have_color=MagickFalse; |
| 7377 | ping_have_non_bw=MagickFalse; |
| 7378 | |
| 7379 | if(image_colors > 256) |
| 7380 | { |
| 7381 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7382 | { |
| 7383 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 7384 | |
| 7385 | if (q == (PixelPacket *) NULL) |
| 7386 | break; |
| 7387 | |
| 7388 | /* Worst case is black-and-white; we are looking at every |
| 7389 | * pixel twice. |
| 7390 | */ |
| 7391 | |
| 7392 | if (ping_have_color == MagickFalse) |
| 7393 | { |
| 7394 | s=q; |
| 7395 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7396 | { |
| 7397 | if (s->red != s->green || s->red != s->blue) |
| 7398 | { |
| 7399 | ping_have_color=MagickTrue; |
| 7400 | ping_have_non_bw=MagickTrue; |
| 7401 | break; |
| 7402 | } |
| 7403 | s++; |
| 7404 | } |
| 7405 | } |
| 7406 | |
| 7407 | if (ping_have_non_bw == MagickFalse) |
| 7408 | { |
| 7409 | s=q; |
| 7410 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7411 | { |
| 7412 | if (s->red != 0 && s->red != QuantumRange) |
| 7413 | { |
| 7414 | ping_have_non_bw=MagickTrue; |
| 7415 | } |
| 7416 | s++; |
| 7417 | } |
| 7418 | } |
| 7419 | } |
| 7420 | } |
| 7421 | } |
| 7422 | |
| 7423 | if (image_colors < 257) |
| 7424 | { |
| 7425 | PixelPacket |
| 7426 | colormap[260]; |
| 7427 | |
| 7428 | /* |
| 7429 | * Initialize image colormap. |
glennrp | 97fd3d0 | 2011-02-23 14:58:06 +0000 | [diff] [blame] | 7430 | */ |
| 7431 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7432 | if (logging != MagickFalse) |
| 7433 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7434 | " Sort the new colormap"); |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7435 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7436 | /* Sort palette, transparent first */; |
| 7437 | |
| 7438 | n = 0; |
| 7439 | |
| 7440 | for (i=0; i<number_transparent; i++) |
| 7441 | colormap[n++] = transparent[i]; |
| 7442 | |
| 7443 | for (i=0; i<number_semitransparent; i++) |
| 7444 | colormap[n++] = semitransparent[i]; |
| 7445 | |
| 7446 | for (i=0; i<number_opaque; i++) |
| 7447 | colormap[n++] = opaque[i]; |
| 7448 | |
| 7449 | |
| 7450 | /* image_colors < 257; search the colormap instead of the pixels |
| 7451 | * to get ping_have_color and ping_have_non_bw |
| 7452 | */ |
| 7453 | for (i=0; i<n; i++) |
| 7454 | { |
| 7455 | if (ping_have_color == MagickFalse) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7456 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7457 | if (colormap[i].red != colormap[i].green || |
| 7458 | colormap[i].red != colormap[i].blue) |
| 7459 | { |
| 7460 | ping_have_color=MagickTrue; |
| 7461 | ping_have_non_bw=MagickTrue; |
| 7462 | break; |
| 7463 | } |
| 7464 | } |
| 7465 | |
| 7466 | if (ping_have_non_bw == MagickFalse) |
| 7467 | { |
| 7468 | if (colormap[i].red != 0 && colormap[i].red != QuantumRange) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7469 | ping_have_non_bw=MagickTrue; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7470 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7471 | } |
| 7472 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7473 | if ((mng_info->ping_exclude_tRNS == MagickFalse || |
| 7474 | (number_transparent == 0 && number_semitransparent == 0)) && |
| 7475 | (((mng_info->write_png_colortype-1) == |
| 7476 | PNG_COLOR_TYPE_PALETTE) || |
| 7477 | (mng_info->write_png_colortype == 0))) |
| 7478 | { |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7479 | if (logging != MagickFalse) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 7480 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7481 | if (n != (ssize_t) image_colors) |
| 7482 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7483 | " image_colors (%d) and n (%d) don't match", |
| 7484 | image_colors, n); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7485 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7486 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7487 | " AcquireImageColormap"); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7488 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7489 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7490 | image->colors = image_colors; |
| 7491 | |
| 7492 | if (AcquireImageColormap(image,image_colors) == |
| 7493 | MagickFalse) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7494 | ThrowWriterException(ResourceLimitError, |
| 7495 | "MemoryAllocationFailed"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7496 | |
| 7497 | for (i=0; i< (ssize_t) image_colors; i++) |
| 7498 | image->colormap[i] = colormap[i]; |
| 7499 | |
| 7500 | if (logging != MagickFalse) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7501 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7502 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7503 | " image->colors=%d (%d)", |
| 7504 | (int) image->colors, image_colors); |
| 7505 | |
| 7506 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7507 | " Update the pixel indexes"); |
| 7508 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7509 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7510 | /* Sync the pixel indices with the new colormap */ |
| 7511 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7512 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7513 | { |
| 7514 | q=GetAuthenticPixels(image,0,y,image->columns,1, |
| 7515 | exception); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7516 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7517 | if (q == (PixelPacket *) NULL) |
| 7518 | break; |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7519 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7520 | indexes=GetAuthenticIndexQueue(image); |
| 7521 | |
| 7522 | for (x=0; x < (ssize_t) image->columns; x++) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7523 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7524 | for (i=0; i< (ssize_t) image_colors; i++) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7525 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7526 | if ((image->matte == MagickFalse || |
| 7527 | image->colormap[i].opacity == q->opacity) && |
| 7528 | (IsColorEqual(&image->colormap[i], |
| 7529 | (PixelPacket *) q))) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7530 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7531 | indexes[x]=(IndexPacket) i; |
| 7532 | break; |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7533 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7534 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7535 | q++; |
| 7536 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7537 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7538 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7539 | break; |
| 7540 | } |
| 7541 | } |
| 7542 | } |
| 7543 | |
| 7544 | if (logging != MagickFalse) |
| 7545 | { |
| 7546 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7547 | " image->colors=%d", (int) image->colors); |
| 7548 | |
| 7549 | if (image->colormap != NULL) |
| 7550 | { |
| 7551 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7552 | " i (red,green,blue,opacity)"); |
| 7553 | |
| 7554 | for (i=0; i < (ssize_t) image->colors; i++) |
| 7555 | { |
cristy | 7298848 | 2011-03-29 16:34:38 +0000 | [diff] [blame] | 7556 | if (i < 300 || i >= (ssize_t) image->colors - 10) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7557 | { |
| 7558 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7559 | " %d (%d,%d,%d,%d)", |
| 7560 | (int) i, |
| 7561 | (int) image->colormap[i].red, |
| 7562 | (int) image->colormap[i].green, |
| 7563 | (int) image->colormap[i].blue, |
| 7564 | (int) image->colormap[i].opacity); |
| 7565 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7566 | } |
| 7567 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7568 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7569 | if (number_transparent < 257) |
| 7570 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7571 | " number_transparent = %d", |
| 7572 | number_transparent); |
| 7573 | else |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7574 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7575 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7576 | " number_transparent > 256"); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7577 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7578 | if (number_opaque < 257) |
| 7579 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7580 | " number_opaque = %d", |
| 7581 | number_opaque); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7582 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7583 | else |
| 7584 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7585 | " number_opaque > 256"); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7586 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7587 | if (number_semitransparent < 257) |
| 7588 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7589 | " number_semitransparent = %d", |
| 7590 | number_semitransparent); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7591 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7592 | else |
| 7593 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7594 | " number_semitransparent > 256"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 7595 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7596 | if (ping_have_non_bw == MagickFalse) |
| 7597 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7598 | " All pixels and the background are black or white"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 7599 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7600 | else if (ping_have_color == MagickFalse) |
| 7601 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7602 | " All pixels and the background are gray"); |
| 7603 | |
| 7604 | else |
| 7605 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7606 | " At least one pixel or the background is non-gray"); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 7607 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 7608 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7609 | " Exit BUILD_PALETTE:"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7610 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7611 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7612 | if (mng_info->write_png8 == MagickFalse) |
| 7613 | break; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7614 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7615 | /* Make any reductions necessary for the PNG8 format */ |
| 7616 | if (image_colors <= 256 && |
| 7617 | image_colors != 0 && image->colormap != NULL && |
| 7618 | number_semitransparent == 0 && |
| 7619 | number_transparent <= 1) |
| 7620 | break; |
| 7621 | |
| 7622 | /* PNG8 can't have semitransparent colors so we threshold the |
| 7623 | * opacity to 0 or OpaqueOpacity |
| 7624 | */ |
| 7625 | if (number_semitransparent != 0) |
| 7626 | { |
| 7627 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7628 | " Thresholding the alpha channel to binary"); |
| 7629 | |
| 7630 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7631 | { |
| 7632 | r=GetAuthenticPixels(image,0,y,image->columns,1, |
| 7633 | exception); |
| 7634 | |
| 7635 | if (r == (PixelPacket *) NULL) |
| 7636 | break; |
| 7637 | |
| 7638 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7639 | { |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7640 | r->opacity = r->opacity > TransparentOpacity/2 ? |
| 7641 | TransparentOpacity : OpaqueOpacity; |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7642 | r++; |
| 7643 | } |
| 7644 | |
| 7645 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7646 | break; |
| 7647 | |
| 7648 | if (image_colors != 0 && image_colors <= 256 && |
| 7649 | image->colormap != NULL) |
| 7650 | for (i=0; i<image_colors; i++) |
| 7651 | image->colormap[i].opacity = |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7652 | image->colormap[i].opacity > TransparentOpacity/2 ? |
| 7653 | TransparentOpacity : OpaqueOpacity; |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7654 | } |
| 7655 | continue; |
| 7656 | } |
| 7657 | |
| 7658 | /* PNG8 can't have more than 256 colors so we quantize the pixels and |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 7659 | * background color to the 4-4-4-1, 3-3-3-1 or 3-3-2-1 palette. If the |
| 7660 | * image is mostly gray, the 4-4-4-1 palette is likely to end up with 256 |
| 7661 | * colors or less. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7662 | */ |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7663 | if (tried_444 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
| 7664 | { |
| 7665 | if (logging != MagickFalse) |
| 7666 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7667 | " Quantizing the background color to 4-4-4"); |
| 7668 | |
| 7669 | tried_444 = MagickTrue; |
| 7670 | |
| 7671 | image->background_color.red= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7672 | ScaleCharToQuantum( |
| 7673 | (ScaleQuantumToChar(image->background_color.red) & 0xf0) | |
| 7674 | (ScaleQuantumToChar(image->background_color.red) & 0xf0) >> 4); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7675 | image->background_color.green= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7676 | ScaleCharToQuantum( |
| 7677 | (ScaleQuantumToChar(image->background_color.green) & 0xf0) | |
| 7678 | (ScaleQuantumToChar(image->background_color.green) & 0xf0) >> 4); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7679 | image->background_color.blue= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7680 | ScaleCharToQuantum( |
| 7681 | (ScaleQuantumToChar(image->background_color.blue) & 0xf0) | |
| 7682 | (ScaleQuantumToChar(image->background_color.blue) & 0xf0) >> 4); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7683 | |
| 7684 | if (logging != MagickFalse) |
| 7685 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7686 | " Quantizing the pixel colors to 4-4-4"); |
| 7687 | |
| 7688 | if (image->colormap == NULL) |
| 7689 | { |
| 7690 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7691 | { |
| 7692 | r=GetAuthenticPixels(image,0,y,image->columns,1, |
| 7693 | exception); |
| 7694 | |
| 7695 | if (r == (PixelPacket *) NULL) |
| 7696 | break; |
| 7697 | |
| 7698 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7699 | { |
| 7700 | if (r->opacity == TransparentOpacity) |
| 7701 | { |
| 7702 | r->red = image->background_color.red; |
| 7703 | r->green = image->background_color.green; |
| 7704 | r->blue = image->background_color.blue; |
| 7705 | } |
| 7706 | else |
| 7707 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7708 | r->red=ScaleCharToQuantum( |
| 7709 | (ScaleQuantumToChar(r->red) & 0xf0) | |
| 7710 | (ScaleQuantumToChar(r->red) & 0xf0) >> 4); |
| 7711 | r->green=ScaleCharToQuantum( |
| 7712 | (ScaleQuantumToChar(r->green) & 0xf0) | |
| 7713 | (ScaleQuantumToChar(r->green) & 0xf0) >> 4); |
| 7714 | r->blue=ScaleCharToQuantum( |
| 7715 | (ScaleQuantumToChar(r->blue) & 0xf0) | |
| 7716 | (ScaleQuantumToChar(r->blue) & 0xf0) >> 4); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7717 | } |
| 7718 | r++; |
| 7719 | } |
| 7720 | |
| 7721 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7722 | break; |
| 7723 | } |
| 7724 | } |
| 7725 | |
| 7726 | else /* Should not reach this; colormap already exists and |
| 7727 | must be <= 256 */ |
| 7728 | { |
| 7729 | if (logging != MagickFalse) |
| 7730 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7731 | " Quantizing the colormap to 4-4-4"); |
| 7732 | for (i=0; i<image_colors; i++) |
| 7733 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7734 | image->colormap[i].red=ScaleCharToQuantum( |
| 7735 | (ScaleQuantumToChar(image->colormap[i].red) & 0xf0) | |
| 7736 | (ScaleQuantumToChar(image->colormap[i].red) & 0xf0) >> 4); |
| 7737 | image->colormap[i].green=ScaleCharToQuantum( |
| 7738 | (ScaleQuantumToChar(image->colormap[i].green) & 0xf0) | |
| 7739 | (ScaleQuantumToChar(image->colormap[i].green) & 0xf0) >> 4); |
| 7740 | image->colormap[i].blue=ScaleCharToQuantum( |
| 7741 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xf0) | |
| 7742 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xf0 >> 4)); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7743 | } |
| 7744 | } |
| 7745 | continue; |
| 7746 | } |
| 7747 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7748 | if (tried_333 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
| 7749 | { |
| 7750 | if (logging != MagickFalse) |
| 7751 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7752 | " Quantizing the background color to 3-3-3"); |
| 7753 | |
| 7754 | tried_333 = MagickTrue; |
| 7755 | |
| 7756 | image->background_color.red= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7757 | ScaleCharToQuantum( |
| 7758 | (ScaleQuantumToChar(image->background_color.red) & 0xe0) | |
| 7759 | (ScaleQuantumToChar(image->background_color.red) & 0xe0) >> 3 | |
| 7760 | (ScaleQuantumToChar(image->background_color.red) & 0xc0) >> 6); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7761 | image->background_color.green= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7762 | ScaleCharToQuantum( |
| 7763 | (ScaleQuantumToChar(image->background_color.green) & 0xe0) | |
| 7764 | (ScaleQuantumToChar(image->background_color.green) & 0xe0) >> 3 | |
| 7765 | (ScaleQuantumToChar(image->background_color.green) & 0xc0) >> 6); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7766 | image->background_color.blue= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7767 | ScaleCharToQuantum( |
| 7768 | (ScaleQuantumToChar(image->background_color.blue) & 0xe0) | |
| 7769 | (ScaleQuantumToChar(image->background_color.blue) & 0xe0) >> 3 | |
| 7770 | (ScaleQuantumToChar(image->background_color.blue) & 0xc0) >> 6); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7771 | |
| 7772 | if (logging != MagickFalse) |
| 7773 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 7774 | " Quantizing the pixel colors to 3-3-3-1"); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7775 | |
| 7776 | if (image->colormap == NULL) |
| 7777 | { |
| 7778 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7779 | { |
| 7780 | r=GetAuthenticPixels(image,0,y,image->columns,1, |
| 7781 | exception); |
| 7782 | |
| 7783 | if (r == (PixelPacket *) NULL) |
| 7784 | break; |
| 7785 | |
| 7786 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7787 | { |
| 7788 | if (r->opacity == TransparentOpacity) |
| 7789 | { |
| 7790 | r->red = image->background_color.red; |
| 7791 | r->green = image->background_color.green; |
| 7792 | r->blue = image->background_color.blue; |
| 7793 | } |
| 7794 | else |
| 7795 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7796 | r->red=ScaleCharToQuantum( |
| 7797 | (ScaleQuantumToChar(r->red) & 0xe0) | |
| 7798 | (ScaleQuantumToChar(r->red) & 0xe0) >> 3 | |
| 7799 | (ScaleQuantumToChar(r->red) & 0xc0) >> 6); |
| 7800 | r->green=ScaleCharToQuantum( |
| 7801 | (ScaleQuantumToChar(r->green) & 0xe0) | |
| 7802 | (ScaleQuantumToChar(r->green) & 0xe0) >> 3 | |
| 7803 | (ScaleQuantumToChar(r->green) & 0xc0) >> 6); |
| 7804 | r->blue=ScaleCharToQuantum( |
| 7805 | (ScaleQuantumToChar(r->blue) & 0xe0) | |
| 7806 | (ScaleQuantumToChar(r->blue) & 0xe0) >> 3 | |
| 7807 | (ScaleQuantumToChar(r->blue) & 0xc0) >> 6); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7808 | } |
| 7809 | r++; |
| 7810 | } |
| 7811 | |
| 7812 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7813 | break; |
| 7814 | } |
| 7815 | } |
| 7816 | |
| 7817 | else /* Should not reach this; colormap already exists and |
| 7818 | must be <= 256 */ |
| 7819 | { |
| 7820 | if (logging != MagickFalse) |
| 7821 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 7822 | " Quantizing the colormap to 3-3-3-1"); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7823 | for (i=0; i<image_colors; i++) |
| 7824 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7825 | image->colormap[i].red=ScaleCharToQuantum( |
| 7826 | (ScaleQuantumToChar(image->colormap[i].red) & 0xe0) | |
| 7827 | (ScaleQuantumToChar(image->colormap[i].red) & 0xe0) >> 3 | |
| 7828 | (ScaleQuantumToChar(image->colormap[i].red) & 0xc0) >> 6); |
| 7829 | image->colormap[i].green=ScaleCharToQuantum( |
| 7830 | (ScaleQuantumToChar(image->colormap[i].green) & 0xe0) | |
| 7831 | (ScaleQuantumToChar(image->colormap[i].green) & 0xe0) >> 3 | |
| 7832 | (ScaleQuantumToChar(image->colormap[i].green) & 0xc0) >> 6); |
| 7833 | image->colormap[i].blue=ScaleCharToQuantum( |
| 7834 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xe0) | |
| 7835 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xe0) >> 3 | |
| 7836 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) >> 6); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7837 | } |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7838 | } |
| 7839 | continue; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7840 | } |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7841 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7842 | if (image_colors == 0 || image_colors > 256) |
| 7843 | { |
| 7844 | if (logging != MagickFalse) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7845 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7846 | " Quantizing the background color to 3-3-2"); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7847 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7848 | /* Red and green were already done so we only quantize the blue |
| 7849 | * channel |
| 7850 | */ |
| 7851 | |
| 7852 | image->background_color.blue=ScaleCharToQuantum( |
| 7853 | (ScaleQuantumToChar(image->background_color.blue) & 0xc0) | |
| 7854 | (ScaleQuantumToChar(image->background_color.blue) & 0xc0) >> 2 | |
| 7855 | (ScaleQuantumToChar(image->background_color.blue) & 0xc0) >> 4 | |
| 7856 | (ScaleQuantumToChar(image->background_color.blue) & 0xc0) >> 6); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7857 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7858 | if (logging != MagickFalse) |
| 7859 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 7860 | " Quantizing the pixel colors to 3-3-2-1"); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7861 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7862 | if (image->colormap == NULL) |
| 7863 | { |
| 7864 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7865 | { |
| 7866 | r=GetAuthenticPixels(image,0,y,image->columns,1, |
| 7867 | exception); |
| 7868 | |
| 7869 | if (r == (PixelPacket *) NULL) |
| 7870 | break; |
| 7871 | |
| 7872 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7873 | { |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7874 | if (r->opacity == TransparentOpacity) |
| 7875 | { |
| 7876 | r->red = image->background_color.red; |
| 7877 | r->green = image->background_color.green; |
| 7878 | r->blue = image->background_color.blue; |
| 7879 | } |
| 7880 | else |
| 7881 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7882 | r->blue=ScaleCharToQuantum( |
| 7883 | (ScaleQuantumToChar(r->blue) & 0xc0) | |
| 7884 | (ScaleQuantumToChar(r->blue) & 0xc0) >> 2 | |
| 7885 | (ScaleQuantumToChar(r->blue) & 0xc0) >> 4 | |
| 7886 | (ScaleQuantumToChar(r->blue) & 0xc0) >> 6); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7887 | } |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 7888 | r++; |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7889 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7890 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7891 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7892 | break; |
| 7893 | } |
| 7894 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7895 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7896 | else /* Should not reach this; colormap already exists and |
| 7897 | must be <= 256 */ |
| 7898 | { |
| 7899 | if (logging != MagickFalse) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7900 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 7901 | " Quantizing the colormap to 3-3-2-1"); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7902 | for (i=0; i<image_colors; i++) |
| 7903 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame^] | 7904 | image->colormap[i].blue=ScaleCharToQuantum( |
| 7905 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) | |
| 7906 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) >> 2 | |
| 7907 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) >> 4 | |
| 7908 | (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) >> 6); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 7909 | } |
| 7910 | } |
| 7911 | continue; |
| 7912 | } |
| 7913 | break; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 7914 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7915 | /* END OF BUILD_PALETTE */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 7916 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7917 | /* If we are excluding the tRNS chunk and there is transparency, |
| 7918 | * then we must write a Gray-Alpha (color-type 4) or RGBA (color-type 6) |
| 7919 | * PNG. |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7920 | */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7921 | if (mng_info->ping_exclude_tRNS != MagickFalse && |
| 7922 | (number_transparent != 0 || number_semitransparent != 0)) |
| 7923 | { |
| 7924 | int colortype=mng_info->write_png_colortype; |
| 7925 | |
| 7926 | if (ping_have_color == MagickFalse) |
| 7927 | mng_info->write_png_colortype = 5; |
| 7928 | |
| 7929 | else |
| 7930 | mng_info->write_png_colortype = 7; |
| 7931 | |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7932 | if (colortype != 0 && |
| 7933 | mng_info->write_png_colortype != (ssize_t) colortype) |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7934 | ping_need_colortype_warning=MagickTrue; |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 7935 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7936 | } |
| 7937 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7938 | /* See if cheap transparency is possible. It is only possible |
| 7939 | * when there is a single transparent color, no semitransparent |
| 7940 | * color, and no opaque color that has the same RGB components |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 7941 | * as the transparent color. We only need this information if |
| 7942 | * we are writing a PNG with colortype 0 or 2, and we have not |
| 7943 | * excluded the tRNS chunk. |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7944 | */ |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 7945 | if (number_transparent == 1 && |
| 7946 | mng_info->write_png_colortype < 4) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7947 | { |
| 7948 | ping_have_cheap_transparency = MagickTrue; |
| 7949 | |
| 7950 | if (number_semitransparent != 0) |
| 7951 | ping_have_cheap_transparency = MagickFalse; |
| 7952 | |
| 7953 | else if (image_colors == 0 || image_colors > 256 || |
| 7954 | image->colormap == NULL) |
| 7955 | { |
| 7956 | ExceptionInfo |
| 7957 | *exception; |
| 7958 | |
| 7959 | register const PixelPacket |
| 7960 | *q; |
| 7961 | |
| 7962 | exception=(&image->exception); |
| 7963 | |
| 7964 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7965 | { |
| 7966 | q=GetVirtualPixels(image,0,y,image->columns,1, exception); |
| 7967 | |
| 7968 | if (q == (PixelPacket *) NULL) |
| 7969 | break; |
| 7970 | |
| 7971 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7972 | { |
| 7973 | if (q->opacity != TransparentOpacity && |
| 7974 | (unsigned short) q->red == ping_trans_color.red && |
| 7975 | (unsigned short) q->green == ping_trans_color.green && |
| 7976 | (unsigned short) q->blue == ping_trans_color.blue) |
| 7977 | { |
| 7978 | ping_have_cheap_transparency = MagickFalse; |
| 7979 | break; |
| 7980 | } |
| 7981 | |
| 7982 | q++; |
| 7983 | } |
| 7984 | |
| 7985 | if (ping_have_cheap_transparency == MagickFalse) |
| 7986 | break; |
| 7987 | } |
| 7988 | } |
| 7989 | else |
| 7990 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 7991 | /* Assuming that image->colormap[0] is the one transparent color |
| 7992 | * and that all others are opaque. |
| 7993 | */ |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7994 | if (image_colors > 1) |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 7995 | for (i=1; i<image_colors; i++) |
| 7996 | if (image->colormap[i].red == image->colormap[0].red && |
| 7997 | image->colormap[i].green == image->colormap[0].green && |
| 7998 | image->colormap[i].blue == image->colormap[0].blue) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7999 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8000 | ping_have_cheap_transparency = MagickFalse; |
| 8001 | break; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8002 | } |
| 8003 | } |
| 8004 | |
| 8005 | if (logging != MagickFalse) |
| 8006 | { |
| 8007 | if (ping_have_cheap_transparency == MagickFalse) |
| 8008 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8009 | " Cheap transparency is not possible."); |
| 8010 | |
| 8011 | else |
| 8012 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8013 | " Cheap transparency is possible."); |
| 8014 | } |
| 8015 | } |
| 8016 | else |
| 8017 | ping_have_cheap_transparency = MagickFalse; |
| 8018 | |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8019 | image_depth=image->depth; |
| 8020 | |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 8021 | quantum_info = (QuantumInfo *) NULL; |
| 8022 | number_colors=0; |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 8023 | image_colors=(int) image->colors; |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 8024 | image_matte=image->matte; |
| 8025 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8026 | mng_info->IsPalette=image->storage_class == PseudoClass && |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 8027 | image_colors <= 256 && image->colormap != NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8028 | |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 8029 | if ((mng_info->write_png_colortype == 4 || mng_info->write_png8) && |
| 8030 | (image->colors == 0 || image->colormap == NULL)) |
| 8031 | { |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 8032 | image_info=DestroyImageInfo(image_info); |
| 8033 | image=DestroyImage(image); |
glennrp | 15e0155 | 2011-03-06 23:29:17 +0000 | [diff] [blame] | 8034 | (void) ThrowMagickException(&IMimage->exception, |
| 8035 | GetMagickModule(),CoderError, |
| 8036 | "Cannot write PNG8 or color-type 3; colormap is NULL", |
| 8037 | "`%s'",IMimage->filename); |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 8038 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
| 8039 | UnlockSemaphoreInfo(ping_semaphore); |
| 8040 | #endif |
| 8041 | return(MagickFalse); |
| 8042 | } |
| 8043 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8044 | /* |
| 8045 | Allocate the PNG structures |
| 8046 | */ |
| 8047 | #ifdef PNG_USER_MEM_SUPPORTED |
| 8048 | ping=png_create_write_struct_2(PNG_LIBPNG_VER_STRING,image, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 8049 | MagickPNGErrorHandler,MagickPNGWarningHandler,(void *) NULL, |
| 8050 | (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8051 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8052 | #else |
| 8053 | ping=png_create_write_struct(PNG_LIBPNG_VER_STRING,image, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 8054 | MagickPNGErrorHandler,MagickPNGWarningHandler); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8055 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8056 | #endif |
| 8057 | if (ping == (png_struct *) NULL) |
| 8058 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8059 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8060 | ping_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8061 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8062 | if (ping_info == (png_info *) NULL) |
| 8063 | { |
| 8064 | png_destroy_write_struct(&ping,(png_info **) NULL); |
| 8065 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 8066 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8067 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8068 | png_set_write_fn(ping,image,png_put_data,png_flush_data); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 8069 | ping_pixels=(unsigned char *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8070 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8071 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8072 | { |
| 8073 | /* |
| 8074 | PNG write failed. |
| 8075 | */ |
| 8076 | #ifdef PNG_DEBUG |
| 8077 | if (image_info->verbose) |
| 8078 | (void) printf("PNG write has failed.\n"); |
| 8079 | #endif |
| 8080 | png_destroy_write_struct(&ping,&ping_info); |
| 8081 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 8082 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8083 | #endif |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 8084 | if (ping_have_blob != MagickFalse) |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 8085 | (void) CloseBlob(image); |
| 8086 | image_info=DestroyImageInfo(image_info); |
| 8087 | image=DestroyImage(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8088 | return(MagickFalse); |
| 8089 | } |
| 8090 | /* |
| 8091 | Prepare PNG for writing. |
| 8092 | */ |
| 8093 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 8094 | if (mng_info->write_mng) |
| 8095 | (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES); |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8096 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8097 | #else |
| 8098 | # ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 8099 | if (mng_info->write_mng) |
| 8100 | png_permit_empty_plte(ping,MagickTrue); |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8101 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8102 | # endif |
| 8103 | #endif |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8104 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8105 | x=0; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8106 | |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 8107 | ping_width=(png_uint_32) image->columns; |
| 8108 | ping_height=(png_uint_32) image->rows; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8109 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8110 | if (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32) |
| 8111 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8112 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8113 | if (mng_info->write_png_depth != 0) |
| 8114 | image_depth=mng_info->write_png_depth; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8116 | /* Adjust requested depth to next higher valid depth if necessary */ |
| 8117 | if (image_depth > 8) |
| 8118 | image_depth=16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8119 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8120 | if ((image_depth > 4) && (image_depth < 8)) |
| 8121 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8122 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8123 | if (image_depth == 3) |
| 8124 | image_depth=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8125 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8126 | if (logging != MagickFalse) |
| 8127 | { |
| 8128 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 8129 | " width=%.20g",(double) ping_width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8130 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 8131 | " height=%.20g",(double) ping_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8132 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 8133 | " image_matte=%.20g",(double) image->matte); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8134 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8135 | " image->depth=%.20g",(double) image->depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8136 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8137 | " Tentative ping_bit_depth=%.20g",(double) image_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8138 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8139 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8140 | save_image_depth=image_depth; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8141 | ping_bit_depth=(png_byte) save_image_depth; |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8142 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8143 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8144 | #if defined(PNG_pHYs_SUPPORTED) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8145 | if (ping_exclude_pHYs == MagickFalse) |
| 8146 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8147 | if ((image->x_resolution != 0) && (image->y_resolution != 0) && |
| 8148 | (!mng_info->write_mng || !mng_info->equal_physs)) |
| 8149 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8150 | if (logging != MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8151 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8152 | " Setting up pHYs chunk"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8153 | |
| 8154 | if (image->units == PixelsPerInchResolution) |
| 8155 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8156 | ping_pHYs_unit_type=PNG_RESOLUTION_METER; |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 8157 | ping_pHYs_x_resolution= |
| 8158 | (png_uint_32) ((100.0*image->x_resolution+0.5)/2.54); |
| 8159 | ping_pHYs_y_resolution= |
| 8160 | (png_uint_32) ((100.0*image->y_resolution+0.5)/2.54); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8161 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8162 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8163 | else if (image->units == PixelsPerCentimeterResolution) |
| 8164 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8165 | ping_pHYs_unit_type=PNG_RESOLUTION_METER; |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 8166 | ping_pHYs_x_resolution=(png_uint_32) (100.0*image->x_resolution+0.5); |
| 8167 | ping_pHYs_y_resolution=(png_uint_32) (100.0*image->y_resolution+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8168 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 8169 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8170 | else |
| 8171 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8172 | ping_pHYs_unit_type=PNG_RESOLUTION_UNKNOWN; |
| 8173 | ping_pHYs_x_resolution=(png_uint_32) image->x_resolution; |
| 8174 | ping_pHYs_y_resolution=(png_uint_32) image->y_resolution; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8175 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 8176 | |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 8177 | if (logging != MagickFalse) |
| 8178 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8179 | " Set up PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.", |
| 8180 | (double) ping_pHYs_x_resolution,(double) ping_pHYs_y_resolution, |
| 8181 | (int) ping_pHYs_unit_type); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 8182 | ping_have_pHYs = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8183 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8184 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8185 | #endif |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8186 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8187 | if (ping_exclude_bKGD == MagickFalse) |
| 8188 | { |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8189 | if ((!mng_info->adjoin || !mng_info->equal_backgrounds)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8190 | { |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8191 | unsigned int |
| 8192 | mask; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8193 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8194 | mask=0xffff; |
| 8195 | if (ping_bit_depth == 8) |
| 8196 | mask=0x00ff; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8197 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8198 | if (ping_bit_depth == 4) |
| 8199 | mask=0x000f; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8200 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8201 | if (ping_bit_depth == 2) |
| 8202 | mask=0x0003; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8203 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8204 | if (ping_bit_depth == 1) |
| 8205 | mask=0x0001; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8206 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8207 | ping_background.red=(png_uint_16) |
| 8208 | (ScaleQuantumToShort(image->background_color.red) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8209 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8210 | ping_background.green=(png_uint_16) |
| 8211 | (ScaleQuantumToShort(image->background_color.green) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8212 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8213 | ping_background.blue=(png_uint_16) |
| 8214 | (ScaleQuantumToShort(image->background_color.blue) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8215 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8216 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8217 | if (logging != MagickFalse) |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 8218 | { |
| 8219 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8220 | " Setting up bKGD chunk (1)"); |
| 8221 | |
| 8222 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8223 | " ping_bit_depth=%d",ping_bit_depth); |
| 8224 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8225 | |
| 8226 | ping_have_bKGD = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8227 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8228 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8229 | /* |
| 8230 | Select the color type. |
| 8231 | */ |
| 8232 | matte=image_matte; |
| 8233 | old_bit_depth=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8234 | |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 8235 | if (mng_info->IsPalette && mng_info->write_png8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8236 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8237 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8238 | /* To do: make this a function cause it's used twice, except |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8239 | for reducing the sample depth from 8. */ |
| 8240 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8241 | number_colors=image_colors; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8242 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8243 | ping_have_tRNS=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8244 | |
| 8245 | /* |
| 8246 | Set image palette. |
| 8247 | */ |
| 8248 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
| 8249 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8250 | if (logging != MagickFalse) |
| 8251 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8252 | " Setting up PLTE chunk with %d colors (%d)", |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 8253 | number_colors, image_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8254 | |
| 8255 | for (i=0; i < (ssize_t) number_colors; i++) |
| 8256 | { |
| 8257 | palette[i].red=ScaleQuantumToChar(image->colormap[i].red); |
| 8258 | palette[i].green=ScaleQuantumToChar(image->colormap[i].green); |
| 8259 | palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue); |
| 8260 | if (logging != MagickFalse) |
| 8261 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8262 | #if MAGICKCORE_QUANTUM_DEPTH == 8 |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8263 | " %3ld (%3d,%3d,%3d)", |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8264 | #else |
| 8265 | " %5ld (%5d,%5d,%5d)", |
| 8266 | #endif |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8267 | (long) i,palette[i].red,palette[i].green,palette[i].blue); |
| 8268 | |
| 8269 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8270 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8271 | ping_have_PLTE=MagickTrue; |
| 8272 | image_depth=ping_bit_depth; |
| 8273 | ping_num_trans=0; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8274 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 8275 | if (matte != MagickFalse) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8276 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8277 | /* |
| 8278 | Identify which colormap entry is transparent. |
| 8279 | */ |
| 8280 | assert(number_colors <= 256); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8281 | assert(image->colormap != NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8282 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8283 | for (i=0; i < (ssize_t) number_transparent; i++) |
| 8284 | ping_trans_alpha[i]=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8285 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8286 | |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8287 | ping_num_trans=(unsigned short) (number_transparent + |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8288 | number_semitransparent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8289 | |
| 8290 | if (ping_num_trans == 0) |
| 8291 | ping_have_tRNS=MagickFalse; |
| 8292 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8293 | else |
| 8294 | ping_have_tRNS=MagickTrue; |
| 8295 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8296 | |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 8297 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8298 | { |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 8299 | /* |
| 8300 | * Identify which colormap entry is the background color. |
| 8301 | */ |
| 8302 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8303 | for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++) |
| 8304 | if (IsPNGColorEqual(ping_background,image->colormap[i])) |
| 8305 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8306 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8307 | ping_background.index=(png_byte) i; |
| 8308 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8309 | } /* end of write_png8 */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8310 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8311 | else if (mng_info->write_png24) |
| 8312 | { |
| 8313 | image_matte=MagickFalse; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8314 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8315 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8316 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8317 | else if (mng_info->write_png32) |
| 8318 | { |
| 8319 | image_matte=MagickTrue; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8320 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8321 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8322 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8323 | else /* mng_info->write_pngNN not specified */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8324 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8325 | image_depth=ping_bit_depth; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8326 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8327 | if (mng_info->write_png_colortype != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8328 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8329 | ping_color_type=(png_byte) mng_info->write_png_colortype-1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8330 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8331 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 8332 | ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8333 | image_matte=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8334 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8335 | else |
| 8336 | image_matte=MagickFalse; |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 8337 | |
| 8338 | if (logging != MagickFalse) |
| 8339 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8340 | " PNG colortype %d was specified:",(int) ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8341 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8342 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 8343 | else /* write_png_colortype not specified */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8344 | { |
| 8345 | if (logging != MagickFalse) |
| 8346 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8347 | " Selecting PNG colortype:"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8348 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8349 | ping_color_type=(png_byte) ((matte != MagickFalse)? |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8350 | PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8351 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8352 | if (image_info->type == TrueColorType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8353 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8354 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8355 | image_matte=MagickFalse; |
| 8356 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8357 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8358 | if (image_info->type == TrueColorMatteType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8359 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8360 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8361 | image_matte=MagickTrue; |
| 8362 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8363 | |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 8364 | if (image_info->type == PaletteType || |
| 8365 | image_info->type == PaletteMatteType) |
| 8366 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
| 8367 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 8368 | if (mng_info->write_png_colortype == 0 && |
| 8369 | (image_info->type == UndefinedType || |
| 8370 | image_info->type == OptimizeType)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8371 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 8372 | if (ping_have_color == MagickFalse) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8373 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 8374 | if (image_matte == MagickFalse) |
| 8375 | { |
| 8376 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY; |
| 8377 | image_matte=MagickFalse; |
| 8378 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8379 | |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 8380 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 8381 | { |
| 8382 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA; |
| 8383 | image_matte=MagickTrue; |
| 8384 | } |
| 8385 | } |
| 8386 | else |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8387 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 8388 | if (image_matte == MagickFalse) |
| 8389 | { |
| 8390 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
| 8391 | image_matte=MagickFalse; |
| 8392 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8393 | |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 8394 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 8395 | { |
| 8396 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGBA; |
| 8397 | image_matte=MagickTrue; |
| 8398 | } |
| 8399 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8400 | } |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 8401 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8402 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8403 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8404 | if (logging != MagickFalse) |
| 8405 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8406 | " Selected PNG colortype=%d",ping_color_type); |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 8407 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8408 | if (ping_bit_depth < 8) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8409 | { |
| 8410 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 8411 | ping_color_type == PNG_COLOR_TYPE_RGB || |
| 8412 | ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 8413 | ping_bit_depth=8; |
| 8414 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8415 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8416 | old_bit_depth=ping_bit_depth; |
| 8417 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8418 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8419 | { |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8420 | if (image->matte == MagickFalse && ping_have_non_bw == MagickFalse) |
| 8421 | ping_bit_depth=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8422 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8423 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8424 | if (ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8425 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 8426 | size_t one = 1; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8427 | ping_bit_depth=1; |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 8428 | |
| 8429 | if (image->colors == 0) |
| 8430 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8431 | /* DO SOMETHING */ |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 8432 | (void) ThrowMagickException(&image->exception, |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 8433 | GetMagickModule(),CoderError, |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 8434 | "image has 0 colors", "`%s'",""); |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 8435 | } |
| 8436 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 8437 | while ((int) (one << ping_bit_depth) < (ssize_t) image_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8438 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8439 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8440 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8441 | if (logging != MagickFalse) |
| 8442 | { |
| 8443 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8444 | " Number of colors: %.20g",(double) image_colors); |
| 8445 | |
| 8446 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8447 | " Tentative PNG bit depth: %d",ping_bit_depth); |
| 8448 | } |
| 8449 | |
| 8450 | if (ping_bit_depth < (int) mng_info->write_png_depth) |
| 8451 | ping_bit_depth = mng_info->write_png_depth; |
| 8452 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8453 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8454 | image_depth=ping_bit_depth; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8455 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8456 | if (logging != MagickFalse) |
| 8457 | { |
| 8458 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 8459 | " Tentative PNG color type: %.20g",(double) ping_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8460 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8461 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 8462 | " image_info->type: %.20g",(double) image_info->type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8463 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8464 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 8465 | " image_depth: %.20g",(double) image_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8466 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8467 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8468 | |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8469 | " image->depth: %.20g",(double) image->depth); |
| 8470 | |
| 8471 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 8472 | " ping_bit_depth: %.20g",(double) ping_bit_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8473 | } |
| 8474 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 8475 | if (matte != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8476 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8477 | if (mng_info->IsPalette) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8478 | { |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 8479 | if (mng_info->write_png_colortype == 0) |
| 8480 | { |
| 8481 | ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8482 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 8483 | if (ping_have_color != MagickFalse) |
| 8484 | ping_color_type=PNG_COLOR_TYPE_RGBA; |
| 8485 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8486 | |
| 8487 | /* |
| 8488 | * Determine if there is any transparent color. |
| 8489 | */ |
| 8490 | if (number_transparent + number_semitransparent == 0) |
| 8491 | { |
| 8492 | /* |
| 8493 | No transparent pixels are present. Change 4 or 6 to 0 or 2. |
| 8494 | */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8495 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8496 | image_matte=MagickFalse; |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 8497 | |
| 8498 | if (mng_info->write_png_colortype == 0) |
| 8499 | ping_color_type&=0x03; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8500 | } |
| 8501 | |
| 8502 | else |
| 8503 | { |
| 8504 | unsigned int |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 8505 | mask; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8506 | |
| 8507 | mask=0xffff; |
| 8508 | |
| 8509 | if (ping_bit_depth == 8) |
| 8510 | mask=0x00ff; |
| 8511 | |
| 8512 | if (ping_bit_depth == 4) |
| 8513 | mask=0x000f; |
| 8514 | |
| 8515 | if (ping_bit_depth == 2) |
| 8516 | mask=0x0003; |
| 8517 | |
| 8518 | if (ping_bit_depth == 1) |
| 8519 | mask=0x0001; |
| 8520 | |
| 8521 | ping_trans_color.red=(png_uint_16) |
| 8522 | (ScaleQuantumToShort(image->colormap[0].red) & mask); |
| 8523 | |
| 8524 | ping_trans_color.green=(png_uint_16) |
| 8525 | (ScaleQuantumToShort(image->colormap[0].green) & mask); |
| 8526 | |
| 8527 | ping_trans_color.blue=(png_uint_16) |
| 8528 | (ScaleQuantumToShort(image->colormap[0].blue) & mask); |
| 8529 | |
| 8530 | ping_trans_color.gray=(png_uint_16) |
| 8531 | (ScaleQuantumToShort(PixelIntensityToQuantum( |
| 8532 | image->colormap)) & mask); |
| 8533 | |
| 8534 | ping_trans_color.index=(png_byte) 0; |
| 8535 | |
| 8536 | ping_have_tRNS=MagickTrue; |
| 8537 | } |
| 8538 | |
| 8539 | if (ping_have_tRNS != MagickFalse) |
| 8540 | { |
| 8541 | /* |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8542 | * Determine if there is one and only one transparent color |
| 8543 | * and if so if it is fully transparent. |
| 8544 | */ |
| 8545 | if (ping_have_cheap_transparency == MagickFalse) |
| 8546 | ping_have_tRNS=MagickFalse; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8547 | } |
| 8548 | |
| 8549 | if (ping_have_tRNS != MagickFalse) |
| 8550 | { |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 8551 | if (mng_info->write_png_colortype == 0) |
| 8552 | ping_color_type &= 0x03; /* changes 4 or 6 to 0 or 2 */ |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8553 | |
| 8554 | if (image_depth == 8) |
| 8555 | { |
| 8556 | ping_trans_color.red&=0xff; |
| 8557 | ping_trans_color.green&=0xff; |
| 8558 | ping_trans_color.blue&=0xff; |
| 8559 | ping_trans_color.gray&=0xff; |
| 8560 | } |
| 8561 | } |
| 8562 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8563 | else |
| 8564 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8565 | if (image_depth == 8) |
| 8566 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8567 | ping_trans_color.red&=0xff; |
| 8568 | ping_trans_color.green&=0xff; |
| 8569 | ping_trans_color.blue&=0xff; |
| 8570 | ping_trans_color.gray&=0xff; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8571 | } |
| 8572 | } |
| 8573 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8574 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8575 | matte=image_matte; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8576 | |
glennrp | 2e09f55 | 2010-11-14 00:38:48 +0000 | [diff] [blame] | 8577 | if (ping_have_tRNS != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8578 | image_matte=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8579 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 8580 | if ((mng_info->IsPalette) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8581 | mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8582 | ping_have_color == MagickFalse && |
| 8583 | (image_matte == MagickFalse || image_depth >= 8)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8584 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 8585 | size_t one=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8586 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8587 | if (image_matte != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 8588 | ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8589 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 8590 | else if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_GRAY_ALPHA) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8591 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8592 | ping_color_type=PNG_COLOR_TYPE_GRAY; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8593 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8594 | if (save_image_depth == 16 && image_depth == 8) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8595 | { |
| 8596 | if (logging != MagickFalse) |
| 8597 | { |
| 8598 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8599 | " Scaling ping_trans_color (0)"); |
| 8600 | } |
| 8601 | ping_trans_color.gray*=0x0101; |
| 8602 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8603 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8604 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8605 | if (image_depth > MAGICKCORE_QUANTUM_DEPTH) |
| 8606 | image_depth=MAGICKCORE_QUANTUM_DEPTH; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8607 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 8608 | if ((image_colors == 0) || ((ssize_t) image_colors-1 > MaxColormapSize)) |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 8609 | image_colors=(int) (one << image_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8610 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8611 | if (image_depth > 8) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8612 | ping_bit_depth=16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8613 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8614 | else |
| 8615 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8616 | ping_bit_depth=8; |
| 8617 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8618 | { |
| 8619 | if(!mng_info->write_png_depth) |
| 8620 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8621 | ping_bit_depth=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8622 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 8623 | while ((int) (one << ping_bit_depth) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 8624 | < (ssize_t) image_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8625 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8626 | } |
| 8627 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8628 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8629 | else if (ping_color_type == |
| 8630 | PNG_COLOR_TYPE_GRAY && image_colors < 17 && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8631 | mng_info->IsPalette) |
| 8632 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8633 | /* Check if grayscale is reducible */ |
glennrp | 1a0aaa6 | 2011-03-07 17:40:17 +0000 | [diff] [blame] | 8634 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8635 | int |
| 8636 | depth_4_ok=MagickTrue, |
| 8637 | depth_2_ok=MagickTrue, |
| 8638 | depth_1_ok=MagickTrue; |
| 8639 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 8640 | for (i=0; i < (ssize_t) image_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8641 | { |
| 8642 | unsigned char |
| 8643 | intensity; |
| 8644 | |
| 8645 | intensity=ScaleQuantumToChar(image->colormap[i].red); |
| 8646 | |
| 8647 | if ((intensity & 0x0f) != ((intensity & 0xf0) >> 4)) |
| 8648 | depth_4_ok=depth_2_ok=depth_1_ok=MagickFalse; |
| 8649 | else if ((intensity & 0x03) != ((intensity & 0x0c) >> 2)) |
| 8650 | depth_2_ok=depth_1_ok=MagickFalse; |
glennrp | 4bf8973 | 2011-03-21 13:48:28 +0000 | [diff] [blame] | 8651 | else if ((intensity & 0x01) != ((intensity & 0x02) >> 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8652 | depth_1_ok=MagickFalse; |
| 8653 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8654 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8655 | if (depth_1_ok && mng_info->write_png_depth <= 1) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 8656 | ping_bit_depth=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8657 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8658 | else if (depth_2_ok && mng_info->write_png_depth <= 2) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 8659 | ping_bit_depth=2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8660 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8661 | else if (depth_4_ok && mng_info->write_png_depth <= 4) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 8662 | ping_bit_depth=4; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8663 | } |
| 8664 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8665 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8666 | image_depth=ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8667 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8668 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8669 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8670 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8671 | if (mng_info->IsPalette) |
| 8672 | { |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 8673 | number_colors=image_colors; |
| 8674 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8675 | if (image_depth <= 8) |
| 8676 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8677 | /* |
| 8678 | Set image palette. |
| 8679 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8680 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8681 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 8682 | if (mng_info->have_write_global_plte && matte == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8683 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 8684 | png_set_PLTE(ping,ping_info,NULL,0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8685 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 8686 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 8687 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8688 | " Setting up empty PLTE chunk"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8689 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8690 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8691 | else |
| 8692 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 8693 | for (i=0; i < (ssize_t) number_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8694 | { |
| 8695 | palette[i].red=ScaleQuantumToChar(image->colormap[i].red); |
| 8696 | palette[i].green=ScaleQuantumToChar(image->colormap[i].green); |
| 8697 | palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue); |
| 8698 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8699 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 8700 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8701 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 98156a3 | 2009-12-09 15:32:44 +0000 | [diff] [blame] | 8702 | " Setting up PLTE chunk with %d colors", |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 8703 | number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8704 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 8705 | ping_have_PLTE=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8706 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8707 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8708 | /* color_type is PNG_COLOR_TYPE_PALETTE */ |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8709 | if (mng_info->write_png_depth == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8710 | { |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 8711 | size_t |
| 8712 | one; |
| 8713 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8714 | ping_bit_depth=1; |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 8715 | one=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8716 | |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 8717 | while ((one << ping_bit_depth) < number_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8718 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8719 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8720 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8721 | ping_num_trans=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8722 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 8723 | if (matte != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8724 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8725 | /* |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8726 | * Set up trans_colors array. |
| 8727 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8728 | assert(number_colors <= 256); |
| 8729 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8730 | ping_num_trans=(unsigned short) (number_transparent + |
| 8731 | number_semitransparent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8732 | |
| 8733 | if (ping_num_trans == 0) |
| 8734 | ping_have_tRNS=MagickFalse; |
| 8735 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8736 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8737 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8738 | if (logging != MagickFalse) |
| 8739 | { |
| 8740 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8741 | " Scaling ping_trans_color (1)"); |
| 8742 | } |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8743 | ping_have_tRNS=MagickTrue; |
| 8744 | |
| 8745 | for (i=0; i < ping_num_trans; i++) |
| 8746 | { |
| 8747 | ping_trans_alpha[i]= (png_byte) (255- |
| 8748 | ScaleQuantumToChar(image->colormap[i].opacity)); |
| 8749 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8750 | } |
| 8751 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8752 | } |
| 8753 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8754 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8755 | else |
| 8756 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8757 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8758 | if (image_depth < 8) |
| 8759 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8760 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8761 | if ((save_image_depth == 16) && (image_depth == 8)) |
| 8762 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8763 | if (logging != MagickFalse) |
| 8764 | { |
| 8765 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8766 | " Scaling ping_trans_color from (%d,%d,%d)", |
| 8767 | (int) ping_trans_color.red, |
| 8768 | (int) ping_trans_color.green, |
| 8769 | (int) ping_trans_color.blue); |
| 8770 | } |
| 8771 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8772 | ping_trans_color.red*=0x0101; |
| 8773 | ping_trans_color.green*=0x0101; |
| 8774 | ping_trans_color.blue*=0x0101; |
| 8775 | ping_trans_color.gray*=0x0101; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8776 | |
| 8777 | if (logging != MagickFalse) |
| 8778 | { |
| 8779 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8780 | " to (%d,%d,%d)", |
| 8781 | (int) ping_trans_color.red, |
| 8782 | (int) ping_trans_color.green, |
| 8783 | (int) ping_trans_color.blue); |
| 8784 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8785 | } |
| 8786 | } |
| 8787 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 8788 | if (ping_bit_depth < (ssize_t) mng_info->write_png_depth) |
| 8789 | ping_bit_depth = (ssize_t) mng_info->write_png_depth; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8790 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8791 | /* |
| 8792 | Adjust background and transparency samples in sub-8-bit grayscale files. |
| 8793 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8794 | if (ping_bit_depth < 8 && ping_color_type == |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8795 | PNG_COLOR_TYPE_GRAY) |
| 8796 | { |
| 8797 | png_uint_16 |
| 8798 | maxval; |
| 8799 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 8800 | size_t |
| 8801 | one=1; |
| 8802 | |
cristy | 22ffd97 | 2010-06-03 16:51:47 +0000 | [diff] [blame] | 8803 | maxval=(png_uint_16) ((one << ping_bit_depth)-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8804 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8805 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8806 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8807 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8808 | ping_background.gray=(png_uint_16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8809 | (QuantumScale*(maxval*(PixelIntensity(&image->background_color)))); |
| 8810 | |
| 8811 | if (logging != MagickFalse) |
| 8812 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8813 | " Setting up bKGD chunk (2)"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 8814 | |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 8815 | ping_have_bKGD = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8816 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8817 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8818 | ping_trans_color.gray=(png_uint_16) (QuantumScale*(maxval* |
| 8819 | ping_trans_color.gray)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8820 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 8821 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8822 | if (ping_exclude_bKGD == MagickFalse) |
| 8823 | { |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 8824 | if (mng_info->IsPalette && (int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 8825 | { |
| 8826 | /* |
| 8827 | Identify which colormap entry is the background color. |
| 8828 | */ |
| 8829 | |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 8830 | number_colors=image_colors; |
| 8831 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8832 | for (i=0; i < (ssize_t) MagickMax(1L*number_colors,1L); i++) |
| 8833 | if (IsPNGColorEqual(image->background_color,image->colormap[i])) |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 8834 | break; |
| 8835 | |
| 8836 | ping_background.index=(png_byte) i; |
| 8837 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 8838 | if (logging != MagickFalse) |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8839 | { |
| 8840 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8841 | " Setting up bKGD chunk with index=%d",(int) i); |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8842 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8843 | |
cristy | 13d0704 | 2010-11-21 20:56:18 +0000 | [diff] [blame] | 8844 | if (i < (ssize_t) number_colors) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8845 | { |
| 8846 | ping_have_bKGD = MagickTrue; |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 8847 | |
| 8848 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8849 | { |
| 8850 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8851 | " background =(%d,%d,%d)", |
| 8852 | (int) ping_background.red, |
| 8853 | (int) ping_background.green, |
| 8854 | (int) ping_background.blue); |
| 8855 | } |
| 8856 | } |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 8857 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8858 | else /* Can't happen */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8859 | { |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 8860 | if (logging != MagickFalse) |
| 8861 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8862 | " No room in PLTE to add bKGD color"); |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8863 | ping_have_bKGD = MagickFalse; |
| 8864 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 8865 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8866 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 8867 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8868 | if (logging != MagickFalse) |
| 8869 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8870 | " PNG color type: %d",ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8871 | /* |
| 8872 | Initialize compression level and filtering. |
| 8873 | */ |
| 8874 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8875 | { |
| 8876 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8877 | " Setting up deflate compression"); |
| 8878 | |
| 8879 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8880 | " Compression buffer size: 32768"); |
| 8881 | } |
| 8882 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8883 | png_set_compression_buffer_size(ping,32768L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8884 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8885 | if (logging != MagickFalse) |
| 8886 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8887 | " Compression mem level: 9"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8888 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8889 | png_set_compression_mem_level(ping, 9); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8890 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8891 | quality=image->quality == UndefinedCompressionQuality ? 75UL : |
| 8892 | image->quality; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8893 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8894 | if (quality > 9) |
| 8895 | { |
| 8896 | int |
| 8897 | level; |
| 8898 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 8899 | level=(int) MagickMin((ssize_t) quality/10,9); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8900 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8901 | if (logging != MagickFalse) |
| 8902 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8903 | " Compression level: %d",level); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8904 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8905 | png_set_compression_level(ping,level); |
| 8906 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8907 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8908 | else |
| 8909 | { |
| 8910 | if (logging != MagickFalse) |
| 8911 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8912 | " Compression strategy: Z_HUFFMAN_ONLY"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8913 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8914 | png_set_compression_strategy(ping, Z_HUFFMAN_ONLY); |
| 8915 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8916 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8917 | if (logging != MagickFalse) |
| 8918 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8919 | " Setting up filtering"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8920 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8921 | #if defined(PNG_MNG_FEATURES_SUPPORTED) && defined(PNG_INTRAPIXEL_DIFFERENCING) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8922 | /* This became available in libpng-1.0.9. Output must be a MNG. */ |
| 8923 | if (mng_info->write_mng && ((quality % 10) == 7)) |
| 8924 | { |
| 8925 | if (logging != MagickFalse) |
| 8926 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8927 | " Filter_type: PNG_INTRAPIXEL_DIFFERENCING"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8928 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8929 | ping_filter_method=PNG_INTRAPIXEL_DIFFERENCING; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8930 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8931 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8932 | else |
| 8933 | if (logging != MagickFalse) |
| 8934 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8935 | " Filter_type: 0"); |
| 8936 | #endif |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8937 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8938 | { |
| 8939 | int |
| 8940 | base_filter; |
| 8941 | |
| 8942 | if ((quality % 10) > 5) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8943 | base_filter=PNG_ALL_FILTERS; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8944 | |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 8945 | else |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8946 | if ((quality % 10) != 5) |
| 8947 | base_filter=(int) quality % 10; |
| 8948 | |
| 8949 | else |
| 8950 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 8951 | ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) || |
| 8952 | (quality < 50)) |
| 8953 | base_filter=PNG_NO_FILTERS; |
| 8954 | |
| 8955 | else |
| 8956 | base_filter=PNG_ALL_FILTERS; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8957 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8958 | if (logging != MagickFalse) |
| 8959 | { |
| 8960 | if (base_filter == PNG_ALL_FILTERS) |
| 8961 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8962 | " Base filter method: ADAPTIVE"); |
| 8963 | else |
| 8964 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8965 | " Base filter method: NONE"); |
| 8966 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8967 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8968 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,base_filter); |
| 8969 | } |
| 8970 | |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 8971 | if ((ping_exclude_tEXt == MagickFalse || ping_exclude_zTXt == MagickFalse) && |
| 8972 | (ping_exclude_iCCP == MagickFalse || ping_exclude_zCCP == MagickFalse)) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8973 | { |
| 8974 | ResetImageProfileIterator(image); |
| 8975 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8976 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8977 | profile=GetImageProfile(image,name); |
| 8978 | |
| 8979 | if (profile != (StringInfo *) NULL) |
| 8980 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8981 | #ifdef PNG_WRITE_iCCP_SUPPORTED |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8982 | if ((LocaleCompare(name,"ICC") == 0) || |
| 8983 | (LocaleCompare(name,"ICM") == 0)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8984 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8985 | |
| 8986 | if (ping_exclude_iCCP == MagickFalse) |
| 8987 | { |
| 8988 | png_set_iCCP(ping,ping_info,(const png_charp) name,0, |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 8989 | #if (PNG_LIBPNG_VER < 10500) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8990 | (png_charp) GetStringInfoDatum(profile), |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 8991 | #else |
| 8992 | (png_const_bytep) GetStringInfoDatum(profile), |
| 8993 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8994 | (png_uint_32) GetStringInfoLength(profile)); |
| 8995 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 8996 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8997 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 8998 | else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8999 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9000 | if (ping_exclude_zCCP == MagickFalse) |
| 9001 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9002 | Magick_png_write_raw_profile(image_info,ping,ping_info, |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9003 | (unsigned char *) name,(unsigned char *) name, |
| 9004 | GetStringInfoDatum(profile), |
| 9005 | (png_uint_32) GetStringInfoLength(profile)); |
| 9006 | } |
| 9007 | } |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 9008 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9009 | if (logging != MagickFalse) |
| 9010 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9011 | " Setting up text chunk with %s profile",name); |
| 9012 | |
| 9013 | name=GetNextImageProfile(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9014 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9015 | } |
| 9016 | |
| 9017 | #if defined(PNG_WRITE_sRGB_SUPPORTED) |
| 9018 | if ((mng_info->have_write_global_srgb == 0) && |
| 9019 | ((image->rendering_intent != UndefinedIntent) || |
| 9020 | (image->colorspace == sRGBColorspace))) |
| 9021 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9022 | if (ping_exclude_sRGB == MagickFalse) |
| 9023 | { |
| 9024 | /* |
| 9025 | Note image rendering intent. |
| 9026 | */ |
| 9027 | if (logging != MagickFalse) |
| 9028 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9029 | " Setting up sRGB chunk"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9030 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9031 | (void) png_set_sRGB(ping,ping_info,( |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9032 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
| 9033 | image->rendering_intent))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9034 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9035 | if (ping_exclude_gAMA == MagickFalse) |
| 9036 | png_set_gAMA(ping,ping_info,0.45455); |
| 9037 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9038 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9039 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9040 | if ((!mng_info->write_mng) || (!png_get_valid(ping,ping_info,PNG_INFO_sRGB))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9041 | #endif |
| 9042 | { |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9043 | if (ping_exclude_gAMA == MagickFalse && |
| 9044 | (ping_exclude_sRGB == MagickFalse || |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9045 | (image->gamma < .45 || image->gamma > .46))) |
| 9046 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9047 | if ((mng_info->have_write_global_gama == 0) && (image->gamma != 0.0)) |
| 9048 | { |
| 9049 | /* |
| 9050 | Note image gamma. |
| 9051 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 9052 | */ |
| 9053 | if (logging != MagickFalse) |
| 9054 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9055 | " Setting up gAMA chunk"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9056 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9057 | png_set_gAMA(ping,ping_info,image->gamma); |
| 9058 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9059 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9060 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9061 | if (ping_exclude_cHRM == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9062 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9063 | if ((mng_info->have_write_global_chrm == 0) && |
| 9064 | (image->chromaticity.red_primary.x != 0.0)) |
| 9065 | { |
| 9066 | /* |
| 9067 | Note image chromaticity. |
| 9068 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 9069 | */ |
| 9070 | PrimaryInfo |
| 9071 | bp, |
| 9072 | gp, |
| 9073 | rp, |
| 9074 | wp; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9075 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9076 | wp=image->chromaticity.white_point; |
| 9077 | rp=image->chromaticity.red_primary; |
| 9078 | gp=image->chromaticity.green_primary; |
| 9079 | bp=image->chromaticity.blue_primary; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9080 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9081 | if (logging != MagickFalse) |
| 9082 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9083 | " Setting up cHRM chunk"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9084 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9085 | png_set_cHRM(ping,ping_info,wp.x,wp.y,rp.x,rp.y,gp.x,gp.y, |
| 9086 | bp.x,bp.y); |
| 9087 | } |
| 9088 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9089 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9090 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9091 | ping_interlace_method=image_info->interlace != NoInterlace; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9092 | |
| 9093 | if (mng_info->write_mng) |
| 9094 | png_set_sig_bytes(ping,8); |
| 9095 | |
| 9096 | /* Bail out if cannot meet defined PNG:bit-depth or PNG:color-type */ |
| 9097 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9098 | if (mng_info->write_png_colortype != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9099 | { |
| 9100 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9101 | if (ping_have_color != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9102 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9103 | ping_color_type = PNG_COLOR_TYPE_RGB; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9104 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9105 | if (ping_bit_depth < 8) |
| 9106 | ping_bit_depth=8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9107 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9108 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9109 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9110 | if (ping_have_color != MagickFalse) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9111 | ping_color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9112 | } |
| 9113 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9114 | if (ping_need_colortype_warning != MagickFalse || |
| 9115 | ((mng_info->write_png_depth && |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9116 | (int) mng_info->write_png_depth != ping_bit_depth) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9117 | (mng_info->write_png_colortype && |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9118 | ((int) mng_info->write_png_colortype-1 != ping_color_type && |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 9119 | mng_info->write_png_colortype != 7 && |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9120 | !(mng_info->write_png_colortype == 5 && ping_color_type == 0))))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9121 | { |
| 9122 | if (logging != MagickFalse) |
| 9123 | { |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9124 | if (ping_need_colortype_warning != MagickFalse) |
| 9125 | { |
| 9126 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9127 | " Image has transparency but tRNS chunk was excluded"); |
| 9128 | } |
| 9129 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9130 | if (mng_info->write_png_depth) |
| 9131 | { |
| 9132 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9133 | " Defined PNG:bit-depth=%u, Computed depth=%u", |
| 9134 | mng_info->write_png_depth, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9135 | ping_bit_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9136 | } |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9137 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9138 | if (mng_info->write_png_colortype) |
| 9139 | { |
| 9140 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9141 | " Defined PNG:color-type=%u, Computed color type=%u", |
| 9142 | mng_info->write_png_colortype-1, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9143 | ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9144 | } |
| 9145 | } |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9146 | |
glennrp | 3bd2e41 | 2010-08-10 13:34:52 +0000 | [diff] [blame] | 9147 | png_warning(ping, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9148 | "Cannot write image with defined PNG:bit-depth or PNG:color-type."); |
| 9149 | } |
| 9150 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9151 | if (image_matte != MagickFalse && image->matte == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9152 | { |
| 9153 | /* Add an opaque matte channel */ |
| 9154 | image->matte = MagickTrue; |
| 9155 | (void) SetImageOpacity(image,0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9156 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9157 | if (logging != MagickFalse) |
| 9158 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9159 | " Added an opaque matte channel"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9160 | } |
| 9161 | |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 9162 | if (number_transparent != 0 || number_semitransparent != 0) |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 9163 | { |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9164 | if (ping_color_type < 4) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9165 | { |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9166 | ping_have_tRNS=MagickTrue; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9167 | if (logging != MagickFalse) |
| 9168 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9169 | " Setting ping_have_tRNS=MagickTrue."); |
| 9170 | } |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 9171 | } |
| 9172 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9173 | if (logging != MagickFalse) |
| 9174 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9175 | " Writing PNG header chunks"); |
| 9176 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9177 | png_set_IHDR(ping,ping_info,ping_width,ping_height, |
| 9178 | ping_bit_depth,ping_color_type, |
| 9179 | ping_interlace_method,ping_compression_method, |
| 9180 | ping_filter_method); |
| 9181 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9182 | if (ping_color_type == 3 && ping_have_PLTE != MagickFalse) |
| 9183 | { |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9184 | png_set_PLTE(ping,ping_info,palette,number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9185 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9186 | if (logging != MagickFalse) |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9187 | { |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9188 | for (i=0; i< (ssize_t) number_colors; i++) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9189 | { |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9190 | if (i < ping_num_trans) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9191 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9192 | " PLTE[%d] = (%d,%d,%d), tRNS[%d] = (%d)", |
| 9193 | (int) i, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9194 | (int) palette[i].red, |
| 9195 | (int) palette[i].green, |
| 9196 | (int) palette[i].blue, |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9197 | (int) i, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9198 | (int) ping_trans_alpha[i]); |
| 9199 | else |
| 9200 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9201 | " PLTE[%d] = (%d,%d,%d)", |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9202 | (int) i, |
| 9203 | (int) palette[i].red, |
| 9204 | (int) palette[i].green, |
| 9205 | (int) palette[i].blue); |
| 9206 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9207 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9208 | } |
| 9209 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9210 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9211 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9212 | if (ping_have_bKGD != MagickFalse) |
| 9213 | png_set_bKGD(ping,ping_info,&ping_background); |
| 9214 | } |
| 9215 | |
| 9216 | if (ping_exclude_pHYs == MagickFalse) |
| 9217 | { |
| 9218 | if (ping_have_pHYs != MagickFalse) |
| 9219 | { |
| 9220 | png_set_pHYs(ping,ping_info, |
| 9221 | ping_pHYs_x_resolution, |
| 9222 | ping_pHYs_y_resolution, |
| 9223 | ping_pHYs_unit_type); |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9224 | |
| 9225 | if (logging) |
| 9226 | { |
| 9227 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9228 | " Setting up pHYs chunk"); |
| 9229 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9230 | " x_resolution=%lu", |
| 9231 | (unsigned long) ping_pHYs_x_resolution); |
| 9232 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9233 | " y_resolution=%lu", |
| 9234 | (unsigned long) ping_pHYs_y_resolution); |
| 9235 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9236 | " unit_type=%lu", |
| 9237 | (unsigned long) ping_pHYs_unit_type); |
| 9238 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9239 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9240 | } |
| 9241 | |
| 9242 | #if defined(PNG_oFFs_SUPPORTED) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9243 | if (ping_exclude_oFFs == MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9244 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9245 | if (image->page.x || image->page.y) |
| 9246 | { |
| 9247 | png_set_oFFs(ping,ping_info,(png_int_32) image->page.x, |
| 9248 | (png_int_32) image->page.y, 0); |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9249 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9250 | if (logging != MagickFalse) |
| 9251 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9252 | " Setting up oFFs chunk with x=%d, y=%d, units=0", |
| 9253 | (int) image->page.x, (int) image->page.y); |
| 9254 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9255 | } |
| 9256 | #endif |
| 9257 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 9258 | if (mng_info->need_blob != MagickFalse) |
| 9259 | { |
| 9260 | if (OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception) == |
| 9261 | MagickFalse) |
| 9262 | png_error(ping,"WriteBlob Failed"); |
| 9263 | |
| 9264 | ping_have_blob=MagickTrue; |
| 9265 | } |
| 9266 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9267 | png_write_info_before_PLTE(ping, ping_info); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9268 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9269 | if (ping_have_tRNS != MagickFalse && ping_color_type < 4) |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9270 | { |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9271 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9272 | { |
| 9273 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9274 | " Calling png_set_tRNS with num_trans=%d",ping_num_trans); |
| 9275 | } |
| 9276 | |
| 9277 | if (ping_color_type == 3) |
| 9278 | (void) png_set_tRNS(ping, ping_info, |
| 9279 | ping_trans_alpha, |
| 9280 | ping_num_trans, |
| 9281 | NULL); |
| 9282 | |
| 9283 | else |
| 9284 | { |
| 9285 | (void) png_set_tRNS(ping, ping_info, |
| 9286 | NULL, |
| 9287 | 0, |
| 9288 | &ping_trans_color); |
| 9289 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9290 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9291 | { |
| 9292 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9293 | " tRNS color =(%d,%d,%d)", |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9294 | (int) ping_trans_color.red, |
| 9295 | (int) ping_trans_color.green, |
| 9296 | (int) ping_trans_color.blue); |
| 9297 | } |
| 9298 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9299 | } |
| 9300 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9301 | /* write any png-chunk-b profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9302 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-b",logging); |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 9303 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9304 | png_write_info(ping,ping_info); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9305 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9306 | /* write any PNG-chunk-m profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9307 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-m",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9308 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9309 | if (ping_exclude_vpAg == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9310 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9311 | if ((image->page.width != 0 && image->page.width != image->columns) || |
| 9312 | (image->page.height != 0 && image->page.height != image->rows)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9313 | { |
| 9314 | unsigned char |
| 9315 | chunk[14]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9316 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9317 | (void) WriteBlobMSBULong(image,9L); /* data length=8 */ |
| 9318 | PNGType(chunk,mng_vpAg); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 9319 | LogPNGChunk(logging,mng_vpAg,9L); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9320 | PNGLong(chunk+4,(png_uint_32) image->page.width); |
| 9321 | PNGLong(chunk+8,(png_uint_32) image->page.height); |
| 9322 | chunk[12]=0; /* unit = pixels */ |
| 9323 | (void) WriteBlob(image,13,chunk); |
| 9324 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 9325 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9326 | } |
| 9327 | |
| 9328 | #if (PNG_LIBPNG_VER == 10206) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9329 | /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9330 | #define PNG_HAVE_IDAT 0x04 |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9331 | ping->mode |= PNG_HAVE_IDAT; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9332 | #undef PNG_HAVE_IDAT |
| 9333 | #endif |
| 9334 | |
| 9335 | png_set_packing(ping); |
| 9336 | /* |
| 9337 | Allocate memory. |
| 9338 | */ |
| 9339 | rowbytes=image->columns; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9340 | if (image_depth > 8) |
| 9341 | rowbytes*=2; |
cristy | 7202c10 | 2010-05-05 19:18:28 +0000 | [diff] [blame] | 9342 | switch (ping_color_type) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9343 | { |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9344 | case PNG_COLOR_TYPE_RGB: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9345 | rowbytes*=3; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9346 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9347 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9348 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 9349 | rowbytes*=2; |
| 9350 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9351 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9352 | case PNG_COLOR_TYPE_RGBA: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9353 | rowbytes*=4; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9354 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9355 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9356 | default: |
| 9357 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9358 | } |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9359 | |
| 9360 | if (logging != MagickFalse) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9361 | { |
| 9362 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9363 | " Writing PNG image data"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9364 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9365 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9366 | " Allocating %.20g bytes of memory for pixels",(double) rowbytes); |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9367 | } |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9368 | ping_pixels=(unsigned char *) AcquireQuantumMemory(rowbytes, |
| 9369 | sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9370 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9371 | if (ping_pixels == (unsigned char *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9372 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9373 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9374 | /* |
| 9375 | Initialize image scanlines. |
| 9376 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9377 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9378 | { |
| 9379 | /* |
| 9380 | PNG write failed. |
| 9381 | */ |
| 9382 | #ifdef PNG_DEBUG |
| 9383 | if (image_info->verbose) |
| 9384 | (void) printf("PNG write has failed.\n"); |
| 9385 | #endif |
| 9386 | png_destroy_write_struct(&ping,&ping_info); |
| 9387 | if (quantum_info != (QuantumInfo *) NULL) |
| 9388 | quantum_info=DestroyQuantumInfo(quantum_info); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9389 | if (ping_pixels != (unsigned char *) NULL) |
| 9390 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9391 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9392 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9393 | #endif |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 9394 | if (ping_have_blob != MagickFalse) |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 9395 | (void) CloseBlob(image); |
| 9396 | image_info=DestroyImageInfo(image_info); |
| 9397 | image=DestroyImage(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9398 | return(MagickFalse); |
| 9399 | } |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 9400 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 9401 | if (quantum_info == (QuantumInfo *) NULL) |
| 9402 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9403 | quantum_info->format=UndefinedQuantumFormat; |
| 9404 | quantum_info->depth=image_depth; |
| 9405 | num_passes=png_set_interlace_handling(ping); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9406 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9407 | if ((!mng_info->write_png8 && !mng_info->write_png24 && |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9408 | !mng_info->write_png32) && |
| 9409 | (mng_info->IsPalette || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9410 | (image_info->type == BilevelType)) && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9411 | image_matte == MagickFalse && |
| 9412 | ping_have_non_bw == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9413 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9414 | /* Palette, Bilevel, or Opaque Monochrome */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9415 | register const PixelPacket |
| 9416 | *p; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9417 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9418 | quantum_info->depth=8; |
| 9419 | for (pass=0; pass < num_passes; pass++) |
| 9420 | { |
| 9421 | /* |
| 9422 | Convert PseudoClass image to a PNG monochrome image. |
| 9423 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9424 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9425 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9426 | if (logging != MagickFalse && y == 0) |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9427 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9428 | " Writing row of pixels (0)"); |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9429 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9430 | p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9431 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9432 | if (p == (const PixelPacket *) NULL) |
| 9433 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9434 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9435 | if (mng_info->IsPalette) |
| 9436 | { |
| 9437 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9438 | quantum_info,GrayQuantum,ping_pixels,&image->exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9439 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_PALETTE && |
| 9440 | mng_info->write_png_depth && |
| 9441 | mng_info->write_png_depth != old_bit_depth) |
| 9442 | { |
| 9443 | /* Undo pixel scaling */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9444 | for (i=0; i < (ssize_t) image->columns; i++) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9445 | *(ping_pixels+i)=(unsigned char) (*(ping_pixels+i) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9446 | >> (8-old_bit_depth)); |
| 9447 | } |
| 9448 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9449 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9450 | else |
| 9451 | { |
| 9452 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9453 | quantum_info,RedQuantum,ping_pixels,&image->exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9454 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9455 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9456 | if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9457 | for (i=0; i < (ssize_t) image->columns; i++) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9458 | *(ping_pixels+i)=(unsigned char) ((*(ping_pixels+i) > 127) ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9459 | 255 : 0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9460 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9461 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9462 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9463 | " Writing row of pixels (1)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9464 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9465 | png_write_row(ping,ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9466 | } |
| 9467 | if (image->previous == (Image *) NULL) |
| 9468 | { |
| 9469 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 9470 | if (status == MagickFalse) |
| 9471 | break; |
| 9472 | } |
| 9473 | } |
| 9474 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9475 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9476 | else /* Not Palette, Bilevel, or Opaque Monochrome */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9477 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9478 | if ((!mng_info->write_png8 && !mng_info->write_png24 && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9479 | !mng_info->write_png32) && |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9480 | (image_matte != MagickFalse || |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9481 | (ping_bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9482 | (mng_info->IsPalette) && ping_have_color == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9483 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9484 | register const PixelPacket |
| 9485 | *p; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9486 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9487 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9488 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9489 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9490 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9491 | { |
| 9492 | p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9493 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9494 | if (p == (const PixelPacket *) NULL) |
| 9495 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9496 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9497 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9498 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9499 | if (mng_info->IsPalette) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9500 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9501 | quantum_info,GrayQuantum,ping_pixels,&image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9502 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9503 | else |
| 9504 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9505 | quantum_info,RedQuantum,ping_pixels,&image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9506 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9507 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9508 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9509 | " Writing GRAY PNG pixels (2)"); |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9510 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9511 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9512 | else /* PNG_COLOR_TYPE_GRAY_ALPHA */ |
| 9513 | { |
| 9514 | if (logging != MagickFalse && y == 0) |
| 9515 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9516 | " Writing GRAY_ALPHA PNG pixels (2)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9517 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9518 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9519 | quantum_info,GrayAlphaQuantum,ping_pixels,&image->exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9520 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9521 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9522 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9523 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9524 | " Writing row of pixels (2)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9525 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9526 | png_write_row(ping,ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9527 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9528 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9529 | if (image->previous == (Image *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9530 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9531 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 9532 | if (status == MagickFalse) |
| 9533 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9534 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9535 | } |
| 9536 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9537 | |
| 9538 | else |
| 9539 | { |
| 9540 | register const PixelPacket |
| 9541 | *p; |
| 9542 | |
| 9543 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9544 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9545 | if ((image_depth > 8) || (mng_info->write_png24 || |
| 9546 | mng_info->write_png32 || |
| 9547 | (!mng_info->write_png8 && !mng_info->IsPalette))) |
| 9548 | { |
| 9549 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9550 | { |
| 9551 | p=GetVirtualPixels(image,0,y,image->columns,1, |
| 9552 | &image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9553 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9554 | if (p == (const PixelPacket *) NULL) |
| 9555 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9556 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9557 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 9558 | { |
| 9559 | if (image->storage_class == DirectClass) |
| 9560 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9561 | quantum_info,RedQuantum,ping_pixels,&image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9562 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9563 | else |
| 9564 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9565 | quantum_info,GrayQuantum,ping_pixels,&image->exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9566 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9567 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9568 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 9569 | { |
| 9570 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9571 | quantum_info,GrayAlphaQuantum,ping_pixels, |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9572 | &image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9573 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9574 | if (logging != MagickFalse && y == 0) |
| 9575 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9576 | " Writing GRAY_ALPHA PNG pixels (3)"); |
| 9577 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9578 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9579 | else if (image_matte != MagickFalse) |
| 9580 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9581 | quantum_info,RGBAQuantum,ping_pixels,&image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9582 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9583 | else |
| 9584 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9585 | quantum_info,RGBQuantum,ping_pixels,&image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9586 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9587 | if (logging != MagickFalse && y == 0) |
| 9588 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9589 | " Writing row of pixels (3)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9590 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9591 | png_write_row(ping,ping_pixels); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9592 | } |
| 9593 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9594 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9595 | else |
| 9596 | /* not ((image_depth > 8) || (mng_info->write_png24 || |
| 9597 | mng_info->write_png32 || |
| 9598 | (!mng_info->write_png8 && !mng_info->IsPalette))) */ |
| 9599 | { |
| 9600 | if ((ping_color_type != PNG_COLOR_TYPE_GRAY) && |
| 9601 | (ping_color_type != PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 9602 | { |
| 9603 | if (logging != MagickFalse) |
| 9604 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9605 | " pass %d, Image Is not GRAY or GRAY_ALPHA",pass); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9606 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9607 | quantum_info->depth=8; |
| 9608 | image_depth=8; |
| 9609 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9610 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9611 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9612 | { |
| 9613 | if (logging != MagickFalse && y == 0) |
| 9614 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9615 | " pass %d, Image Is RGB, 16-bit GRAY, or GRAY_ALPHA",pass); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9616 | |
glennrp | 770d193 | 2011-03-06 22:11:17 +0000 | [diff] [blame] | 9617 | p=GetVirtualPixels(image,0,y,image->columns,1, |
| 9618 | &image->exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9619 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9620 | if (p == (const PixelPacket *) NULL) |
| 9621 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9622 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9623 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 9624 | { |
glennrp | 4bf8973 | 2011-03-21 13:48:28 +0000 | [diff] [blame] | 9625 | quantum_info->depth=image->depth; |
| 9626 | |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 9627 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9628 | quantum_info,GrayQuantum,ping_pixels,&image->exception); |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 9629 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9630 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9631 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 9632 | { |
| 9633 | if (logging != MagickFalse && y == 0) |
| 9634 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9635 | " Writing GRAY_ALPHA PNG pixels (4)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9636 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9637 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9638 | quantum_info,GrayAlphaQuantum,ping_pixels, |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9639 | &image->exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9640 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9641 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9642 | else |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9643 | { |
glennrp | 179d075 | 2011-03-17 13:02:10 +0000 | [diff] [blame] | 9644 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 9645 | quantum_info,IndexQuantum,ping_pixels,&image->exception); |
| 9646 | |
| 9647 | if (logging != MagickFalse && y <= 2) |
| 9648 | { |
| 9649 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a7d6db | 2011-03-17 13:24:10 +0000 | [diff] [blame] | 9650 | " Writing row of non-gray pixels (4)"); |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 9651 | |
| 9652 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9653 | " ping_pixels[0]=%d,ping_pixels[1]=%d", |
| 9654 | (int)ping_pixels[0],(int)ping_pixels[1]); |
| 9655 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9656 | } |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9657 | png_write_row(ping,ping_pixels); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9658 | } |
| 9659 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9660 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9661 | if (image->previous == (Image *) NULL) |
| 9662 | { |
| 9663 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 9664 | if (status == MagickFalse) |
| 9665 | break; |
| 9666 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9667 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9668 | } |
| 9669 | } |
| 9670 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 9671 | if (quantum_info != (QuantumInfo *) NULL) |
| 9672 | quantum_info=DestroyQuantumInfo(quantum_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9673 | |
| 9674 | if (logging != MagickFalse) |
| 9675 | { |
| 9676 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 9677 | " Wrote PNG image data"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9678 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9679 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9680 | " Width: %.20g",(double) ping_width); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9681 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9682 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9683 | " Height: %.20g",(double) ping_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9684 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9685 | if (mng_info->write_png_depth) |
| 9686 | { |
| 9687 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9688 | " Defined PNG:bit-depth: %d",mng_info->write_png_depth); |
| 9689 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9690 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9691 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9692 | " PNG bit-depth written: %d",ping_bit_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9693 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9694 | if (mng_info->write_png_colortype) |
| 9695 | { |
| 9696 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9697 | " Defined PNG:color-type: %d",mng_info->write_png_colortype-1); |
| 9698 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9699 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9700 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9701 | " PNG color-type written: %d",ping_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9702 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9703 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9704 | " PNG Interlace method: %d",ping_interlace_method); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9705 | } |
| 9706 | /* |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 9707 | Generate text chunks after IDAT. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9708 | */ |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9709 | if (ping_exclude_tEXt == MagickFalse || ping_exclude_zTXt == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9710 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9711 | ResetImagePropertyIterator(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9712 | property=GetNextImageProperty(image); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9713 | while (property != (const char *) NULL) |
| 9714 | { |
| 9715 | png_textp |
| 9716 | text; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9717 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9718 | value=GetImageProperty(image,property); |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 9719 | |
| 9720 | /* Don't write any "png:" properties; those are just for "identify" */ |
| 9721 | if (LocaleNCompare(property,"png:",4) != 0 && |
| 9722 | |
| 9723 | /* Suppress density and units if we wrote a pHYs chunk */ |
| 9724 | (ping_exclude_pHYs != MagickFalse || |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9725 | LocaleCompare(property,"density") != 0 || |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 9726 | LocaleCompare(property,"units") != 0) && |
| 9727 | |
| 9728 | /* Suppress the IM-generated Date:create and Date:modify */ |
| 9729 | (ping_exclude_date == MagickFalse || |
| 9730 | LocaleNCompare(property, "Date:",5) != 0)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9731 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 9732 | if (value != (const char *) NULL) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9733 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 9734 | text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text)); |
| 9735 | text[0].key=(char *) property; |
| 9736 | text[0].text=(char *) value; |
| 9737 | text[0].text_length=strlen(value); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9738 | |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 9739 | if (ping_exclude_tEXt != MagickFalse) |
| 9740 | text[0].compression=PNG_TEXT_COMPRESSION_zTXt; |
| 9741 | |
| 9742 | else if (ping_exclude_zTXt != MagickFalse) |
| 9743 | text[0].compression=PNG_TEXT_COMPRESSION_NONE; |
| 9744 | |
| 9745 | else |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9746 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 9747 | text[0].compression=image_info->compression == NoCompression || |
| 9748 | (image_info->compression == UndefinedCompression && |
| 9749 | text[0].text_length < 128) ? PNG_TEXT_COMPRESSION_NONE : |
| 9750 | PNG_TEXT_COMPRESSION_zTXt ; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9751 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9752 | |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 9753 | if (logging != MagickFalse) |
| 9754 | { |
| 9755 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9756 | " Setting up text chunk"); |
| 9757 | |
| 9758 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9759 | " keyword: %s",text[0].key); |
| 9760 | } |
| 9761 | |
| 9762 | png_set_text(ping,ping_info,text,1); |
| 9763 | png_free(ping,text); |
| 9764 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9765 | } |
| 9766 | property=GetNextImageProperty(image); |
| 9767 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9768 | } |
| 9769 | |
| 9770 | /* write any PNG-chunk-e profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9771 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-e",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9772 | |
| 9773 | if (logging != MagickFalse) |
| 9774 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9775 | " Writing PNG end info"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9776 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9777 | png_write_end(ping,ping_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9778 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9779 | if (mng_info->need_fram && (int) image->dispose == BackgroundDispose) |
| 9780 | { |
| 9781 | if (mng_info->page.x || mng_info->page.y || |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9782 | (ping_width != mng_info->page.width) || |
| 9783 | (ping_height != mng_info->page.height)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9784 | { |
| 9785 | unsigned char |
| 9786 | chunk[32]; |
| 9787 | |
| 9788 | /* |
| 9789 | Write FRAM 4 with clipping boundaries followed by FRAM 1. |
| 9790 | */ |
| 9791 | (void) WriteBlobMSBULong(image,27L); /* data length=27 */ |
| 9792 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 9793 | LogPNGChunk(logging,mng_FRAM,27L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9794 | chunk[4]=4; |
| 9795 | chunk[5]=0; /* frame name separator (no name) */ |
| 9796 | chunk[6]=1; /* flag for changing delay, for next frame only */ |
| 9797 | chunk[7]=0; /* flag for changing frame timeout */ |
| 9798 | chunk[8]=1; /* flag for changing frame clipping for next frame */ |
| 9799 | chunk[9]=0; /* flag for changing frame sync_id */ |
| 9800 | PNGLong(chunk+10,(png_uint_32) (0L)); /* temporary 0 delay */ |
| 9801 | chunk[14]=0; /* clipping boundaries delta type */ |
| 9802 | PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */ |
| 9803 | PNGLong(chunk+19, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9804 | (png_uint_32) (mng_info->page.x + ping_width)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9805 | PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */ |
| 9806 | PNGLong(chunk+27, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9807 | (png_uint_32) (mng_info->page.y + ping_height)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9808 | (void) WriteBlob(image,31,chunk); |
| 9809 | (void) WriteBlobMSBULong(image,crc32(0,chunk,31)); |
| 9810 | mng_info->old_framing_mode=4; |
| 9811 | mng_info->framing_mode=1; |
| 9812 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9813 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9814 | else |
| 9815 | mng_info->framing_mode=3; |
| 9816 | } |
| 9817 | if (mng_info->write_mng && !mng_info->need_fram && |
| 9818 | ((int) image->dispose == 3)) |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 9819 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9820 | CoderError,"Cannot convert GIF with disposal method 3 to MNG-LC", |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 9821 | "`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9822 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9823 | /* |
| 9824 | Free PNG resources. |
| 9825 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9826 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9827 | png_destroy_write_struct(&ping,&ping_info); |
| 9828 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9829 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9830 | |
| 9831 | #if defined(PNG_SETJMP_NOT_THREAD_SAFE) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9832 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9833 | #endif |
| 9834 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 9835 | if (ping_have_blob != MagickFalse) |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 9836 | (void) CloseBlob(image); |
| 9837 | |
| 9838 | image_info=DestroyImageInfo(image_info); |
| 9839 | image=DestroyImage(image); |
| 9840 | |
| 9841 | /* Store bit depth actually written */ |
| 9842 | s[0]=(char) ping_bit_depth; |
| 9843 | s[1]='\0'; |
| 9844 | |
| 9845 | (void) SetImageProperty(IMimage,"png:bit-depth-written",s); |
| 9846 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9847 | if (logging != MagickFalse) |
| 9848 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9849 | " exit WriteOnePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9850 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9851 | return(MagickTrue); |
| 9852 | /* End write one PNG image */ |
| 9853 | } |
| 9854 | |
| 9855 | /* |
| 9856 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 9857 | % % |
| 9858 | % % |
| 9859 | % % |
| 9860 | % W r i t e P N G I m a g e % |
| 9861 | % % |
| 9862 | % % |
| 9863 | % % |
| 9864 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 9865 | % |
| 9866 | % WritePNGImage() writes a Portable Network Graphics (PNG) or |
| 9867 | % Multiple-image Network Graphics (MNG) image file. |
| 9868 | % |
| 9869 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 9870 | % |
| 9871 | % The format of the WritePNGImage method is: |
| 9872 | % |
| 9873 | % MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image) |
| 9874 | % |
| 9875 | % A description of each parameter follows: |
| 9876 | % |
| 9877 | % o image_info: the image info. |
| 9878 | % |
| 9879 | % o image: The image. |
| 9880 | % |
| 9881 | % Returns MagickTrue on success, MagickFalse on failure. |
| 9882 | % |
| 9883 | % Communicating with the PNG encoder: |
| 9884 | % |
| 9885 | % While the datastream written is always in PNG format and normally would |
| 9886 | % be given the "png" file extension, this method also writes the following |
| 9887 | % pseudo-formats which are subsets of PNG: |
| 9888 | % |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9889 | % o PNG8: An 8-bit indexed PNG datastream is written. If the image has |
| 9890 | % a depth greater than 8, the depth is reduced. If transparency |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9891 | % is present, the tRNS chunk must only have values 0 and 255 |
| 9892 | % (i.e., transparency is binary: fully opaque or fully |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9893 | % transparent). If other values are present they will be |
| 9894 | % 50%-thresholded to binary transparency. If more than 256 |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 9895 | % colors are present, they will be quantized to the 4-4-4-1, |
| 9896 | % 3-3-3-1, or 3-3-2-1 palette. |
| 9897 | % |
| 9898 | % If you want better quantization or dithering of the colors |
| 9899 | % or alpha than that, you need to do it before calling the |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9900 | % PNG encoder. The pixels contain 8-bit indices even if |
| 9901 | % they could be represented with 1, 2, or 4 bits. Grayscale |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9902 | % images will be written as indexed PNG files even though the |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9903 | % PNG grayscale type might be slightly more efficient. Please |
| 9904 | % note that writing to the PNG8 format may result in loss |
| 9905 | % of color and alpha data. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9906 | % |
| 9907 | % o PNG24: An 8-bit per sample RGB PNG datastream is written. The tRNS |
| 9908 | % chunk can be present to convey binary transparency by naming |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9909 | % one of the colors as transparent. The only loss incurred |
| 9910 | % is reduction of sample depth to 8. If the image has more |
| 9911 | % than one transparent color, has semitransparent pixels, or |
| 9912 | % has an opaque pixel with the same RGB components as the |
| 9913 | % transparent color, an image is not written. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9914 | % |
| 9915 | % o PNG32: An 8-bit per sample RGBA PNG is written. Partial |
| 9916 | % transparency is permitted, i.e., the alpha sample for |
| 9917 | % each pixel can have any value from 0 to 255. The alpha |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9918 | % channel is present even if the image is fully opaque. |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9919 | % The only loss in data is the reduction of the sample depth |
| 9920 | % to 8. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9921 | % |
| 9922 | % o -define: For more precise control of the PNG output, you can use the |
| 9923 | % Image options "png:bit-depth" and "png:color-type". These |
| 9924 | % can be set from the commandline with "-define" and also |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 9925 | % from the application programming interfaces. The options |
| 9926 | % are case-independent and are converted to lowercase before |
| 9927 | % being passed to this encoder. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9928 | % |
| 9929 | % png:color-type can be 0, 2, 3, 4, or 6. |
| 9930 | % |
| 9931 | % When png:color-type is 0 (Grayscale), png:bit-depth can |
| 9932 | % be 1, 2, 4, 8, or 16. |
| 9933 | % |
| 9934 | % When png:color-type is 2 (RGB), png:bit-depth can |
| 9935 | % be 8 or 16. |
| 9936 | % |
| 9937 | % When png:color-type is 3 (Indexed), png:bit-depth can |
| 9938 | % be 1, 2, 4, or 8. This refers to the number of bits |
| 9939 | % used to store the index. The color samples always have |
| 9940 | % bit-depth 8 in indexed PNG files. |
| 9941 | % |
| 9942 | % When png:color-type is 4 (Gray-Matte) or 6 (RGB-Matte), |
| 9943 | % png:bit-depth can be 8 or 16. |
| 9944 | % |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9945 | % If the image cannot be written without loss with the requested bit-depth |
| 9946 | % and color-type, a PNG file will not be written, and the encoder will |
| 9947 | % return MagickFalse. |
| 9948 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9949 | % Since image encoders should not be responsible for the "heavy lifting", |
| 9950 | % the user should make sure that ImageMagick has already reduced the |
| 9951 | % image depth and number of colors and limit transparency to binary |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9952 | % transparency prior to attempting to write the image with depth, color, |
| 9953 | % or transparency limitations. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9954 | % |
glennrp | 97cefe2 | 2011-04-22 16:17:00 +0000 | [diff] [blame] | 9955 | % To do: Enforce the previous paragraph. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9956 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9957 | % Note that another definition, "png:bit-depth-written" exists, but it |
| 9958 | % is not intended for external use. It is only used internally by the |
| 9959 | % PNG encoder to inform the JNG encoder of the depth of the alpha channel. |
| 9960 | % |
| 9961 | % It is possible to request that the PNG encoder write previously-formatted |
| 9962 | % ancillary chunks in the output PNG file, using the "-profile" commandline |
| 9963 | % option as shown below or by setting the profile via a programming |
| 9964 | % interface: |
| 9965 | % |
| 9966 | % -profile PNG-chunk-x:<file> |
| 9967 | % |
| 9968 | % where x is a location flag and <file> is a file containing the chunk |
| 9969 | % name in the first 4 bytes, then a colon (":"), followed by the chunk data. |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 9970 | % This encoder will compute the chunk length and CRC, so those must not |
| 9971 | % be included in the file. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9972 | % |
| 9973 | % "x" can be "b" (before PLTE), "m" (middle, i.e., between PLTE and IDAT), |
| 9974 | % or "e" (end, i.e., after IDAT). If you want to write multiple chunks |
| 9975 | % of the same type, then add a short unique string after the "x" to prevent |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 9976 | % subsequent profiles from overwriting the preceding ones, e.g., |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9977 | % |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 9978 | % -profile PNG-chunk-b01:file01 -profile PNG-chunk-b02:file02 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9979 | % |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 9980 | % As of version 6.6.6 the following optimizations are always done: |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9981 | % |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 9982 | % o 32-bit depth is reduced to 16. |
| 9983 | % o 16-bit depth is reduced to 8 if all pixels contain samples whose |
| 9984 | % high byte and low byte are identical. |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9985 | % o Palette is sorted to remove unused entries and to put a |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9986 | % transparent color first, if BUILD_PNG_PALETTE is defined. |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9987 | % o Opaque matte channel is removed when writing an indexed PNG. |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 9988 | % o Grayscale images are reduced to 1, 2, or 4 bit depth if |
| 9989 | % this can be done without loss and a larger bit depth N was not |
| 9990 | % requested via the "-define PNG:bit-depth=N" option. |
| 9991 | % o If matte channel is present but only one transparent color is |
| 9992 | % present, RGB+tRNS is written instead of RGBA |
| 9993 | % o Opaque matte channel is removed (or added, if color-type 4 or 6 |
| 9994 | % was requested when converting an opaque image). |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9995 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9996 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 9997 | */ |
| 9998 | static MagickBooleanType WritePNGImage(const ImageInfo *image_info, |
| 9999 | Image *image) |
| 10000 | { |
| 10001 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 10002 | excluding, |
| 10003 | logging, |
| 10004 | have_mng_structure, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10005 | status; |
| 10006 | |
| 10007 | MngInfo |
| 10008 | *mng_info; |
| 10009 | |
| 10010 | const char |
| 10011 | *value; |
| 10012 | |
| 10013 | int |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 10014 | i, |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10015 | source; |
| 10016 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10017 | /* |
| 10018 | Open image file. |
| 10019 | */ |
| 10020 | assert(image_info != (const ImageInfo *) NULL); |
| 10021 | assert(image_info->signature == MagickSignature); |
| 10022 | assert(image != (Image *) NULL); |
| 10023 | assert(image->signature == MagickSignature); |
| 10024 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 10025 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WritePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10026 | /* |
| 10027 | Allocate a MngInfo structure. |
| 10028 | */ |
| 10029 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 10030 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10031 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10032 | if (mng_info == (MngInfo *) NULL) |
| 10033 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10034 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10035 | /* |
| 10036 | Initialize members of the MngInfo structure. |
| 10037 | */ |
| 10038 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 10039 | mng_info->image=image; |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 10040 | mng_info->equal_backgrounds=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10041 | have_mng_structure=MagickTrue; |
| 10042 | |
| 10043 | /* See if user has requested a specific PNG subformat */ |
| 10044 | |
| 10045 | mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0; |
| 10046 | mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0; |
| 10047 | mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0; |
| 10048 | |
| 10049 | if (mng_info->write_png8) |
| 10050 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10051 | mng_info->write_png_colortype = /* 3 */ 4; |
| 10052 | mng_info->write_png_depth = 8; |
| 10053 | image->depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10054 | } |
| 10055 | |
| 10056 | if (mng_info->write_png24) |
| 10057 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10058 | mng_info->write_png_colortype = /* 2 */ 3; |
| 10059 | mng_info->write_png_depth = 8; |
| 10060 | image->depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10061 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10062 | if (image->matte == MagickTrue) |
| 10063 | (void) SetImageType(image,TrueColorMatteType); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10064 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10065 | else |
| 10066 | (void) SetImageType(image,TrueColorType); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10067 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10068 | (void) SyncImage(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10069 | } |
| 10070 | |
| 10071 | if (mng_info->write_png32) |
| 10072 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10073 | mng_info->write_png_colortype = /* 6 */ 7; |
| 10074 | mng_info->write_png_depth = 8; |
| 10075 | image->depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10076 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10077 | if (image->matte == MagickTrue) |
| 10078 | (void) SetImageType(image,TrueColorMatteType); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10079 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10080 | else |
| 10081 | (void) SetImageType(image,TrueColorType); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10082 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10083 | (void) SyncImage(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10084 | } |
| 10085 | |
| 10086 | value=GetImageOption(image_info,"png:bit-depth"); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10087 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10088 | if (value != (char *) NULL) |
| 10089 | { |
| 10090 | if (LocaleCompare(value,"1") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10091 | mng_info->write_png_depth = 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10092 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10093 | else if (LocaleCompare(value,"2") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10094 | mng_info->write_png_depth = 2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10095 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10096 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10097 | mng_info->write_png_depth = 4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10098 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10099 | else if (LocaleCompare(value,"8") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10100 | mng_info->write_png_depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10101 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10102 | else if (LocaleCompare(value,"16") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10103 | mng_info->write_png_depth = 16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10104 | |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 10105 | else |
| 10106 | (void) ThrowMagickException(&image->exception, |
| 10107 | GetMagickModule(),CoderWarning, |
| 10108 | "ignoring invalid defined png:bit-depth", |
| 10109 | "=%s",value); |
| 10110 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10111 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10112 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 10113 | " png:bit-depth=%d was defined.\n",mng_info->write_png_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10114 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10116 | value=GetImageOption(image_info,"png:color-type"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10117 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10118 | if (value != (char *) NULL) |
| 10119 | { |
| 10120 | /* We must store colortype+1 because 0 is a valid colortype */ |
| 10121 | if (LocaleCompare(value,"0") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10122 | mng_info->write_png_colortype = 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10123 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10124 | else if (LocaleCompare(value,"2") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10125 | mng_info->write_png_colortype = 3; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10126 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10127 | else if (LocaleCompare(value,"3") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10128 | mng_info->write_png_colortype = 4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10129 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10130 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10131 | mng_info->write_png_colortype = 5; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10132 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10133 | else if (LocaleCompare(value,"6") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10134 | mng_info->write_png_colortype = 7; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10135 | |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 10136 | else |
| 10137 | (void) ThrowMagickException(&image->exception, |
| 10138 | GetMagickModule(),CoderWarning, |
| 10139 | "ignoring invalid defined png:color-type", |
| 10140 | "=%s",value); |
| 10141 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10142 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10143 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10144 | " png:color-type=%d was defined.\n",mng_info->write_png_colortype-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10145 | } |
| 10146 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10147 | /* Check for chunks to be excluded: |
| 10148 | * |
glennrp | 0dff56c | 2011-01-29 19:10:02 +0000 | [diff] [blame] | 10149 | * The default is to not exclude any known chunks except for any |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10150 | * listed in the "unused_chunks" array, above. |
| 10151 | * |
| 10152 | * Chunks can be listed for exclusion via a "PNG:exclude-chunk" |
| 10153 | * define (in the image properties or in the image artifacts) |
| 10154 | * or via a mng_info member. For convenience, in addition |
| 10155 | * to or instead of a comma-separated list of chunks, the |
| 10156 | * "exclude-chunk" string can be simply "all" or "none". |
| 10157 | * |
| 10158 | * The exclude-chunk define takes priority over the mng_info. |
| 10159 | * |
| 10160 | * A "PNG:include-chunk" define takes priority over both the |
| 10161 | * mng_info and the "PNG:exclude-chunk" define. Like the |
| 10162 | * "exclude-chunk" string, it can define "all" or "none" as |
glennrp | 0dff56c | 2011-01-29 19:10:02 +0000 | [diff] [blame] | 10163 | * well as a comma-separated list. Chunks that are unknown to |
| 10164 | * ImageMagick are always excluded, regardless of their "copy-safe" |
| 10165 | * status according to the PNG specification, and even if they |
| 10166 | * appear in the "include-chunk" list. |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10167 | * |
| 10168 | * Finally, all chunks listed in the "unused_chunks" array are |
| 10169 | * automatically excluded, regardless of the other instructions |
| 10170 | * or lack thereof. |
| 10171 | * |
| 10172 | * if you exclude sRGB but not gAMA (recommended), then sRGB chunk |
| 10173 | * will not be written and the gAMA chunk will only be written if it |
| 10174 | * is not between .45 and .46, or approximately (1.0/2.2). |
| 10175 | * |
| 10176 | * If you exclude tRNS and the image has transparency, the colortype |
| 10177 | * is forced to be 4 or 6 (GRAY_ALPHA or RGB_ALPHA). |
| 10178 | * |
| 10179 | * The -strip option causes StripImage() to set the png:include-chunk |
| 10180 | * artifact to "none,gama". |
| 10181 | */ |
| 10182 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10183 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 10184 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10185 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10186 | mng_info->ping_exclude_EXIF=MagickFalse; /* hex-encoded EXIF in zTXt */ |
| 10187 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10188 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 10189 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 10190 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 10191 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 10192 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 10193 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10194 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10195 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 10196 | mng_info->ping_exclude_zCCP=MagickFalse; /* hex-encoded iCCP in zTXt */ |
| 10197 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 10198 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 10199 | mng_info->ping_preserve_colormap=MagickFalse; |
| 10200 | |
| 10201 | value=GetImageArtifact(image,"png:preserve-colormap"); |
| 10202 | if (value == NULL) |
| 10203 | value=GetImageOption(image_info,"png:preserve-colormap"); |
| 10204 | if (value != NULL) |
| 10205 | mng_info->ping_preserve_colormap=MagickTrue; |
| 10206 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10207 | excluding=MagickFalse; |
| 10208 | |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10209 | for (source=0; source<1; source++) |
| 10210 | { |
| 10211 | if (source==0) |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 10212 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10213 | value=GetImageArtifact(image,"png:exclude-chunk"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 10214 | |
| 10215 | if (value == NULL) |
| 10216 | value=GetImageArtifact(image,"png:exclude-chunks"); |
| 10217 | } |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10218 | else |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 10219 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10220 | value=GetImageOption(image_info,"png:exclude-chunk"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10221 | |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 10222 | if (value == NULL) |
| 10223 | value=GetImageOption(image_info,"png:exclude-chunks"); |
| 10224 | } |
| 10225 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10226 | if (value != NULL) |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 10227 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10228 | |
| 10229 | size_t |
| 10230 | last; |
| 10231 | |
| 10232 | excluding=MagickTrue; |
| 10233 | |
| 10234 | if (logging != MagickFalse) |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10235 | { |
| 10236 | if (source == 0) |
| 10237 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10238 | " png:exclude-chunk=%s found in image artifacts.\n", value); |
| 10239 | else |
| 10240 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10241 | " png:exclude-chunk=%s found in image properties.\n", value); |
| 10242 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10243 | |
| 10244 | last=strlen(value); |
| 10245 | |
| 10246 | for (i=0; i<(int) last; i+=5) |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 10247 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10248 | |
| 10249 | if (LocaleNCompare(value+i,"all",3) == 0) |
| 10250 | { |
| 10251 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 10252 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10253 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10254 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 10255 | mng_info->ping_exclude_gAMA=MagickTrue; |
| 10256 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 10257 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 10258 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 10259 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 10260 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 10261 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10262 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10263 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 10264 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 10265 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 10266 | i--; |
| 10267 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10268 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10269 | if (LocaleNCompare(value+i,"none",4) == 0) |
| 10270 | { |
| 10271 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 10272 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10273 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10274 | mng_info->ping_exclude_EXIF=MagickFalse; |
| 10275 | mng_info->ping_exclude_gAMA=MagickFalse; |
| 10276 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 10277 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 10278 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 10279 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 10280 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 10281 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10282 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10283 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 10284 | mng_info->ping_exclude_zCCP=MagickFalse; |
| 10285 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 10286 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10287 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10288 | if (LocaleNCompare(value+i,"bkgd",4) == 0) |
| 10289 | mng_info->ping_exclude_bKGD=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10290 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10291 | if (LocaleNCompare(value+i,"chrm",4) == 0) |
| 10292 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10293 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10294 | if (LocaleNCompare(value+i,"date",4) == 0) |
| 10295 | mng_info->ping_exclude_date=MagickTrue; |
| 10296 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10297 | if (LocaleNCompare(value+i,"exif",4) == 0) |
| 10298 | mng_info->ping_exclude_EXIF=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10299 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10300 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 10301 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10302 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10303 | if (LocaleNCompare(value+i,"iccp",4) == 0) |
| 10304 | mng_info->ping_exclude_iCCP=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10305 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10306 | /* |
| 10307 | if (LocaleNCompare(value+i,"itxt",4) == 0) |
| 10308 | mng_info->ping_exclude_iTXt=MagickTrue; |
| 10309 | */ |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10310 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10311 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 10312 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10313 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10314 | if (LocaleNCompare(value+i,"offs",4) == 0) |
| 10315 | mng_info->ping_exclude_oFFs=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10316 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10317 | if (LocaleNCompare(value+i,"phys",4) == 0) |
| 10318 | mng_info->ping_exclude_pHYs=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10319 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10320 | if (LocaleNCompare(value+i,"srgb",4) == 0) |
| 10321 | mng_info->ping_exclude_sRGB=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10322 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10323 | if (LocaleNCompare(value+i,"text",4) == 0) |
| 10324 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10325 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10326 | if (LocaleNCompare(value+i,"trns",4) == 0) |
| 10327 | mng_info->ping_exclude_tRNS=MagickTrue; |
| 10328 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10329 | if (LocaleNCompare(value+i,"vpag",4) == 0) |
| 10330 | mng_info->ping_exclude_vpAg=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10331 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10332 | if (LocaleNCompare(value+i,"zccp",4) == 0) |
| 10333 | mng_info->ping_exclude_zCCP=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10334 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10335 | if (LocaleNCompare(value+i,"ztxt",4) == 0) |
| 10336 | mng_info->ping_exclude_zTXt=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10337 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10338 | } |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 10339 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10340 | } |
| 10341 | |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10342 | for (source=0; source<1; source++) |
| 10343 | { |
| 10344 | if (source==0) |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 10345 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10346 | value=GetImageArtifact(image,"png:include-chunk"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 10347 | |
| 10348 | if (value == NULL) |
| 10349 | value=GetImageArtifact(image,"png:include-chunks"); |
| 10350 | } |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10351 | else |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 10352 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 10353 | value=GetImageOption(image_info,"png:include-chunk"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10354 | |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 10355 | if (value == NULL) |
| 10356 | value=GetImageOption(image_info,"png:include-chunks"); |
| 10357 | } |
| 10358 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10359 | if (value != NULL) |
| 10360 | { |
| 10361 | size_t |
| 10362 | last; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10363 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10364 | excluding=MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10365 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10366 | if (logging != MagickFalse) |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10367 | { |
| 10368 | if (source == 0) |
| 10369 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10370 | " png:include-chunk=%s found in image artifacts.\n", value); |
| 10371 | else |
| 10372 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10373 | " png:include-chunk=%s found in image properties.\n", value); |
| 10374 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10375 | |
| 10376 | last=strlen(value); |
| 10377 | |
| 10378 | for (i=0; i<(int) last; i+=5) |
| 10379 | { |
| 10380 | if (LocaleNCompare(value+i,"all",3) == 0) |
| 10381 | { |
| 10382 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 10383 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10384 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10385 | mng_info->ping_exclude_EXIF=MagickFalse; |
| 10386 | mng_info->ping_exclude_gAMA=MagickFalse; |
| 10387 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 10388 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 10389 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 10390 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 10391 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 10392 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10393 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10394 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 10395 | mng_info->ping_exclude_zCCP=MagickFalse; |
| 10396 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 10397 | i--; |
| 10398 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10399 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10400 | if (LocaleNCompare(value+i,"none",4) == 0) |
| 10401 | { |
| 10402 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 10403 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10404 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10405 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 10406 | mng_info->ping_exclude_gAMA=MagickTrue; |
| 10407 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 10408 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 10409 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 10410 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 10411 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 10412 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10413 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10414 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 10415 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 10416 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 10417 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10418 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10419 | if (LocaleNCompare(value+i,"bkgd",4) == 0) |
| 10420 | mng_info->ping_exclude_bKGD=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10421 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10422 | if (LocaleNCompare(value+i,"chrm",4) == 0) |
| 10423 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10424 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10425 | if (LocaleNCompare(value+i,"date",4) == 0) |
| 10426 | mng_info->ping_exclude_date=MagickFalse; |
| 10427 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10428 | if (LocaleNCompare(value+i,"exif",4) == 0) |
| 10429 | mng_info->ping_exclude_EXIF=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10430 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10431 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 10432 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10433 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10434 | if (LocaleNCompare(value+i,"iccp",4) == 0) |
| 10435 | mng_info->ping_exclude_iCCP=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10436 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10437 | /* |
| 10438 | if (LocaleNCompare(value+i,"itxt",4) == 0) |
| 10439 | mng_info->ping_exclude_iTXt=MagickFalse; |
| 10440 | */ |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10441 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10442 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 10443 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10444 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10445 | if (LocaleNCompare(value+i,"offs",4) == 0) |
| 10446 | mng_info->ping_exclude_oFFs=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10447 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10448 | if (LocaleNCompare(value+i,"phys",4) == 0) |
| 10449 | mng_info->ping_exclude_pHYs=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10450 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10451 | if (LocaleNCompare(value+i,"srgb",4) == 0) |
| 10452 | mng_info->ping_exclude_sRGB=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10453 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10454 | if (LocaleNCompare(value+i,"text",4) == 0) |
| 10455 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10456 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10457 | if (LocaleNCompare(value+i,"trns",4) == 0) |
| 10458 | mng_info->ping_exclude_tRNS=MagickFalse; |
| 10459 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10460 | if (LocaleNCompare(value+i,"vpag",4) == 0) |
| 10461 | mng_info->ping_exclude_vpAg=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10462 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10463 | if (LocaleNCompare(value+i,"zccp",4) == 0) |
| 10464 | mng_info->ping_exclude_zCCP=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10465 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10466 | if (LocaleNCompare(value+i,"ztxt",4) == 0) |
| 10467 | mng_info->ping_exclude_zTXt=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10468 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10469 | } |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 10470 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10471 | } |
| 10472 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10473 | if (excluding != MagickFalse && logging != MagickFalse) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10474 | { |
| 10475 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10476 | " Chunks to be excluded from the output PNG:"); |
| 10477 | if (mng_info->ping_exclude_bKGD != MagickFalse) |
| 10478 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10479 | " bKGD"); |
| 10480 | if (mng_info->ping_exclude_cHRM != MagickFalse) |
| 10481 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10482 | " cHRM"); |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10483 | if (mng_info->ping_exclude_date != MagickFalse) |
| 10484 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10485 | " date"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10486 | if (mng_info->ping_exclude_EXIF != MagickFalse) |
| 10487 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10488 | " EXIF"); |
| 10489 | if (mng_info->ping_exclude_gAMA != MagickFalse) |
| 10490 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10491 | " gAMA"); |
| 10492 | if (mng_info->ping_exclude_iCCP != MagickFalse) |
| 10493 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10494 | " iCCP"); |
| 10495 | /* |
| 10496 | if (mng_info->ping_exclude_iTXt != MagickFalse) |
| 10497 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10498 | " iTXt"); |
| 10499 | */ |
| 10500 | if (mng_info->ping_exclude_oFFs != MagickFalse) |
| 10501 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10502 | " oFFs"); |
| 10503 | if (mng_info->ping_exclude_pHYs != MagickFalse) |
| 10504 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10505 | " pHYs"); |
| 10506 | if (mng_info->ping_exclude_sRGB != MagickFalse) |
| 10507 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10508 | " sRGB"); |
| 10509 | if (mng_info->ping_exclude_tEXt != MagickFalse) |
| 10510 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10511 | " tEXt"); |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 10512 | if (mng_info->ping_exclude_tRNS != MagickFalse) |
| 10513 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10514 | " tRNS"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10515 | if (mng_info->ping_exclude_vpAg != MagickFalse) |
| 10516 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10517 | " vpAg"); |
| 10518 | if (mng_info->ping_exclude_zCCP != MagickFalse) |
| 10519 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10520 | " zCCP"); |
| 10521 | if (mng_info->ping_exclude_zTXt != MagickFalse) |
| 10522 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10523 | " zTXt"); |
| 10524 | } |
| 10525 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 10526 | mng_info->need_blob = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10527 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 10528 | status=WriteOnePNGImage(mng_info,image_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10529 | |
| 10530 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10531 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10532 | if (logging != MagickFalse) |
| 10533 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WritePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10534 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10535 | return(status); |
| 10536 | } |
| 10537 | |
| 10538 | #if defined(JNG_SUPPORTED) |
| 10539 | |
| 10540 | /* Write one JNG image */ |
| 10541 | static MagickBooleanType WriteOneJNGImage(MngInfo *mng_info, |
| 10542 | const ImageInfo *image_info,Image *image) |
| 10543 | { |
| 10544 | Image |
| 10545 | *jpeg_image; |
| 10546 | |
| 10547 | ImageInfo |
| 10548 | *jpeg_image_info; |
| 10549 | |
| 10550 | MagickBooleanType |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10551 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10552 | status; |
| 10553 | |
| 10554 | size_t |
| 10555 | length; |
| 10556 | |
| 10557 | unsigned char |
| 10558 | *blob, |
| 10559 | chunk[80], |
| 10560 | *p; |
| 10561 | |
| 10562 | unsigned int |
| 10563 | jng_alpha_compression_method, |
| 10564 | jng_alpha_sample_depth, |
| 10565 | jng_color_type, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10566 | transparent; |
| 10567 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10568 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10569 | jng_quality; |
| 10570 | |
| 10571 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 10572 | " Enter WriteOneJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10573 | |
| 10574 | blob=(unsigned char *) NULL; |
| 10575 | jpeg_image=(Image *) NULL; |
| 10576 | jpeg_image_info=(ImageInfo *) NULL; |
| 10577 | |
| 10578 | status=MagickTrue; |
| 10579 | transparent=image_info->type==GrayscaleMatteType || |
| 10580 | image_info->type==TrueColorMatteType; |
| 10581 | jng_color_type=10; |
| 10582 | jng_alpha_sample_depth=0; |
| 10583 | jng_quality=image_info->quality == 0UL ? 75UL : image_info->quality; |
| 10584 | jng_alpha_compression_method=0; |
| 10585 | |
| 10586 | if (image->matte != MagickFalse) |
| 10587 | { |
| 10588 | /* if any pixels are transparent */ |
| 10589 | transparent=MagickTrue; |
| 10590 | if (image_info->compression==JPEGCompression) |
| 10591 | jng_alpha_compression_method=8; |
| 10592 | } |
| 10593 | |
| 10594 | if (transparent) |
| 10595 | { |
| 10596 | jng_color_type=14; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10597 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10598 | /* Create JPEG blob, image, and image_info */ |
| 10599 | if (logging != MagickFalse) |
| 10600 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10601 | " Creating jpeg_image_info for opacity."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10602 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10603 | jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10604 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10605 | if (jpeg_image_info == (ImageInfo *) NULL) |
| 10606 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10607 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10608 | if (logging != MagickFalse) |
| 10609 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10610 | " Creating jpeg_image."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10611 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10612 | jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10613 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10614 | if (jpeg_image == (Image *) NULL) |
| 10615 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10616 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10617 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 10618 | status=SeparateImageChannel(jpeg_image,OpacityChannel); |
| 10619 | status=NegateImage(jpeg_image,MagickFalse); |
| 10620 | jpeg_image->matte=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10621 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10622 | if (jng_quality >= 1000) |
| 10623 | jpeg_image_info->quality=jng_quality/1000; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10624 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10625 | else |
| 10626 | jpeg_image_info->quality=jng_quality; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10627 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10628 | jpeg_image_info->type=GrayscaleType; |
| 10629 | (void) SetImageType(jpeg_image,GrayscaleType); |
| 10630 | (void) AcquireUniqueFilename(jpeg_image->filename); |
| 10631 | (void) FormatMagickString(jpeg_image_info->filename,MaxTextExtent, |
| 10632 | "%s",jpeg_image->filename); |
| 10633 | } |
| 10634 | |
| 10635 | /* To do: check bit depth of PNG alpha channel */ |
| 10636 | |
| 10637 | /* Check if image is grayscale. */ |
| 10638 | if (image_info->type != TrueColorMatteType && image_info->type != |
| 10639 | TrueColorType && ImageIsGray(image)) |
| 10640 | jng_color_type-=2; |
| 10641 | |
| 10642 | if (transparent) |
| 10643 | { |
| 10644 | if (jng_alpha_compression_method==0) |
| 10645 | { |
| 10646 | const char |
| 10647 | *value; |
| 10648 | |
| 10649 | /* Encode opacity as a grayscale PNG blob */ |
| 10650 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
| 10651 | &image->exception); |
| 10652 | if (logging != MagickFalse) |
| 10653 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10654 | " Creating PNG blob."); |
| 10655 | length=0; |
| 10656 | |
| 10657 | (void) CopyMagickString(jpeg_image_info->magick,"PNG",MaxTextExtent); |
| 10658 | (void) CopyMagickString(jpeg_image->magick,"PNG",MaxTextExtent); |
| 10659 | jpeg_image_info->interlace=NoInterlace; |
| 10660 | |
| 10661 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length, |
| 10662 | &image->exception); |
| 10663 | |
| 10664 | /* Retrieve sample depth used */ |
| 10665 | value=GetImageProperty(jpeg_image,"png:bit-depth-written"); |
| 10666 | if (value != (char *) NULL) |
| 10667 | jng_alpha_sample_depth= (unsigned int) value[0]; |
| 10668 | } |
| 10669 | else |
| 10670 | { |
| 10671 | /* Encode opacity as a grayscale JPEG blob */ |
| 10672 | |
| 10673 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
| 10674 | &image->exception); |
| 10675 | |
| 10676 | (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent); |
| 10677 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 10678 | jpeg_image_info->interlace=NoInterlace; |
| 10679 | if (logging != MagickFalse) |
| 10680 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10681 | " Creating blob."); |
| 10682 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length, |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 10683 | &image->exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10684 | jng_alpha_sample_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10685 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10686 | if (logging != MagickFalse) |
| 10687 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10688 | " Successfully read jpeg_image into a blob, length=%.20g.", |
| 10689 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10690 | |
| 10691 | } |
| 10692 | /* Destroy JPEG image and image_info */ |
| 10693 | jpeg_image=DestroyImage(jpeg_image); |
| 10694 | (void) RelinquishUniqueFileResource(jpeg_image_info->filename); |
| 10695 | jpeg_image_info=DestroyImageInfo(jpeg_image_info); |
| 10696 | } |
| 10697 | |
| 10698 | /* Write JHDR chunk */ |
| 10699 | (void) WriteBlobMSBULong(image,16L); /* chunk data length=16 */ |
| 10700 | PNGType(chunk,mng_JHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10701 | LogPNGChunk(logging,mng_JHDR,16L); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 10702 | PNGLong(chunk+4,(png_uint_32) image->columns); |
| 10703 | PNGLong(chunk+8,(png_uint_32) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10704 | chunk[12]=jng_color_type; |
| 10705 | chunk[13]=8; /* sample depth */ |
| 10706 | chunk[14]=8; /*jng_image_compression_method */ |
| 10707 | chunk[15]=(unsigned char) (image_info->interlace == NoInterlace ? 0 : 8); |
| 10708 | chunk[16]=jng_alpha_sample_depth; |
| 10709 | chunk[17]=jng_alpha_compression_method; |
| 10710 | chunk[18]=0; /*jng_alpha_filter_method */ |
| 10711 | chunk[19]=0; /*jng_alpha_interlace_method */ |
| 10712 | (void) WriteBlob(image,20,chunk); |
| 10713 | (void) WriteBlobMSBULong(image,crc32(0,chunk,20)); |
| 10714 | if (logging != MagickFalse) |
| 10715 | { |
| 10716 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 10717 | " JNG width:%15lu",(unsigned long) image->columns); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10718 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10719 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 10720 | " JNG height:%14lu",(unsigned long) image->rows); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10721 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10722 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10723 | " JNG color type:%10d",jng_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10724 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10725 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10726 | " JNG sample depth:%8d",8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10727 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10728 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10729 | " JNG compression:%9d",8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10730 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10731 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10732 | " JNG interlace:%11d",0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10733 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10734 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10735 | " JNG alpha depth:%9d",jng_alpha_sample_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10736 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10737 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10738 | " JNG alpha compression:%3d",jng_alpha_compression_method); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10739 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10740 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10741 | " JNG alpha filter:%8d",0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10742 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10743 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10744 | " JNG alpha interlace:%5d",0); |
| 10745 | } |
| 10746 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10747 | /* Write any JNG-chunk-b profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10748 | (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-b",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10749 | |
| 10750 | /* |
| 10751 | Write leading ancillary chunks |
| 10752 | */ |
| 10753 | |
| 10754 | if (transparent) |
| 10755 | { |
| 10756 | /* |
| 10757 | Write JNG bKGD chunk |
| 10758 | */ |
| 10759 | |
| 10760 | unsigned char |
| 10761 | blue, |
| 10762 | green, |
| 10763 | red; |
| 10764 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10765 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10766 | num_bytes; |
| 10767 | |
| 10768 | if (jng_color_type == 8 || jng_color_type == 12) |
| 10769 | num_bytes=6L; |
| 10770 | else |
| 10771 | num_bytes=10L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10772 | (void) WriteBlobMSBULong(image,(size_t) (num_bytes-4L)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10773 | PNGType(chunk,mng_bKGD); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10774 | LogPNGChunk(logging,mng_bKGD,(size_t) (num_bytes-4L)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10775 | red=ScaleQuantumToChar(image->background_color.red); |
| 10776 | green=ScaleQuantumToChar(image->background_color.green); |
| 10777 | blue=ScaleQuantumToChar(image->background_color.blue); |
| 10778 | *(chunk+4)=0; |
| 10779 | *(chunk+5)=red; |
| 10780 | *(chunk+6)=0; |
| 10781 | *(chunk+7)=green; |
| 10782 | *(chunk+8)=0; |
| 10783 | *(chunk+9)=blue; |
| 10784 | (void) WriteBlob(image,(size_t) num_bytes,chunk); |
| 10785 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) num_bytes)); |
| 10786 | } |
| 10787 | |
| 10788 | if ((image->colorspace == sRGBColorspace || image->rendering_intent)) |
| 10789 | { |
| 10790 | /* |
| 10791 | Write JNG sRGB chunk |
| 10792 | */ |
| 10793 | (void) WriteBlobMSBULong(image,1L); |
| 10794 | PNGType(chunk,mng_sRGB); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10795 | LogPNGChunk(logging,mng_sRGB,1L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10796 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10797 | if (image->rendering_intent != UndefinedIntent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 10798 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10799 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 10800 | (image->rendering_intent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10801 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10802 | else |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 10803 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10804 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 10805 | (PerceptualIntent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10806 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10807 | (void) WriteBlob(image,5,chunk); |
| 10808 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 10809 | } |
| 10810 | else |
| 10811 | { |
| 10812 | if (image->gamma != 0.0) |
| 10813 | { |
| 10814 | /* |
| 10815 | Write JNG gAMA chunk |
| 10816 | */ |
| 10817 | (void) WriteBlobMSBULong(image,4L); |
| 10818 | PNGType(chunk,mng_gAMA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10819 | LogPNGChunk(logging,mng_gAMA,4L); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10820 | PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10821 | (void) WriteBlob(image,8,chunk); |
| 10822 | (void) WriteBlobMSBULong(image,crc32(0,chunk,8)); |
| 10823 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10824 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10825 | if ((mng_info->equal_chrms == MagickFalse) && |
| 10826 | (image->chromaticity.red_primary.x != 0.0)) |
| 10827 | { |
| 10828 | PrimaryInfo |
| 10829 | primary; |
| 10830 | |
| 10831 | /* |
| 10832 | Write JNG cHRM chunk |
| 10833 | */ |
| 10834 | (void) WriteBlobMSBULong(image,32L); |
| 10835 | PNGType(chunk,mng_cHRM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10836 | LogPNGChunk(logging,mng_cHRM,32L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10837 | primary=image->chromaticity.white_point; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10838 | PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5)); |
| 10839 | PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10840 | primary=image->chromaticity.red_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10841 | PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5)); |
| 10842 | PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10843 | primary=image->chromaticity.green_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10844 | PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5)); |
| 10845 | PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10846 | primary=image->chromaticity.blue_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10847 | PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5)); |
| 10848 | PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10849 | (void) WriteBlob(image,36,chunk); |
| 10850 | (void) WriteBlobMSBULong(image,crc32(0,chunk,36)); |
| 10851 | } |
| 10852 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10853 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10854 | if (image->x_resolution && image->y_resolution && !mng_info->equal_physs) |
| 10855 | { |
| 10856 | /* |
| 10857 | Write JNG pHYs chunk |
| 10858 | */ |
| 10859 | (void) WriteBlobMSBULong(image,9L); |
| 10860 | PNGType(chunk,mng_pHYs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10861 | LogPNGChunk(logging,mng_pHYs,9L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10862 | if (image->units == PixelsPerInchResolution) |
| 10863 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10864 | PNGLong(chunk+4,(png_uint_32) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10865 | (image->x_resolution*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10866 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10867 | PNGLong(chunk+8,(png_uint_32) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10868 | (image->y_resolution*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10869 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10870 | chunk[12]=1; |
| 10871 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10872 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10873 | else |
| 10874 | { |
| 10875 | if (image->units == PixelsPerCentimeterResolution) |
| 10876 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10877 | PNGLong(chunk+4,(png_uint_32) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10878 | (image->x_resolution*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10879 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10880 | PNGLong(chunk+8,(png_uint_32) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10881 | (image->y_resolution*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10882 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10883 | chunk[12]=1; |
| 10884 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10885 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10886 | else |
| 10887 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10888 | PNGLong(chunk+4,(png_uint_32) (image->x_resolution+0.5)); |
| 10889 | PNGLong(chunk+8,(png_uint_32) (image->y_resolution+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10890 | chunk[12]=0; |
| 10891 | } |
| 10892 | } |
| 10893 | (void) WriteBlob(image,13,chunk); |
| 10894 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 10895 | } |
| 10896 | |
| 10897 | if (mng_info->write_mng == 0 && (image->page.x || image->page.y)) |
| 10898 | { |
| 10899 | /* |
| 10900 | Write JNG oFFs chunk |
| 10901 | */ |
| 10902 | (void) WriteBlobMSBULong(image,9L); |
| 10903 | PNGType(chunk,mng_oFFs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10904 | LogPNGChunk(logging,mng_oFFs,9L); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10905 | PNGsLong(chunk+4,(ssize_t) (image->page.x)); |
| 10906 | PNGsLong(chunk+8,(ssize_t) (image->page.y)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10907 | chunk[12]=0; |
| 10908 | (void) WriteBlob(image,13,chunk); |
| 10909 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 10910 | } |
| 10911 | if (mng_info->write_mng == 0 && (image->page.width || image->page.height)) |
| 10912 | { |
| 10913 | (void) WriteBlobMSBULong(image,9L); /* data length=8 */ |
| 10914 | PNGType(chunk,mng_vpAg); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10915 | LogPNGChunk(logging,mng_vpAg,9L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10916 | PNGLong(chunk+4,(png_uint_32) image->page.width); |
| 10917 | PNGLong(chunk+8,(png_uint_32) image->page.height); |
| 10918 | chunk[12]=0; /* unit = pixels */ |
| 10919 | (void) WriteBlob(image,13,chunk); |
| 10920 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 10921 | } |
| 10922 | |
| 10923 | |
| 10924 | if (transparent) |
| 10925 | { |
| 10926 | if (jng_alpha_compression_method==0) |
| 10927 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10928 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10929 | i; |
| 10930 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10931 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10932 | len; |
| 10933 | |
| 10934 | /* Write IDAT chunk header */ |
| 10935 | if (logging != MagickFalse) |
| 10936 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10937 | " Write IDAT chunks from blob, length=%.20g.",(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 10938 | length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10939 | |
| 10940 | /* Copy IDAT chunks */ |
| 10941 | len=0; |
| 10942 | p=blob+8; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10943 | for (i=8; i<(ssize_t) length; i+=len+12) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10944 | { |
| 10945 | len=(*p<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3)); |
| 10946 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10947 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10948 | if (*(p)==73 && *(p+1)==68 && *(p+2)==65 && *(p+3)==84) /* IDAT */ |
| 10949 | { |
| 10950 | /* Found an IDAT chunk. */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10951 | (void) WriteBlobMSBULong(image,(size_t) len); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10952 | LogPNGChunk(logging,mng_IDAT,(size_t) len); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10953 | (void) WriteBlob(image,(size_t) len+4,p); |
| 10954 | (void) WriteBlobMSBULong(image, |
| 10955 | crc32(0,p,(uInt) len+4)); |
| 10956 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10957 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10958 | else |
| 10959 | { |
| 10960 | if (logging != MagickFalse) |
| 10961 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10962 | " Skipping %c%c%c%c chunk, length=%.20g.", |
| 10963 | *(p),*(p+1),*(p+2),*(p+3),(double) len); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10964 | } |
| 10965 | p+=(8+len); |
| 10966 | } |
| 10967 | } |
| 10968 | else |
| 10969 | { |
| 10970 | /* Write JDAA chunk header */ |
| 10971 | if (logging != MagickFalse) |
| 10972 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10973 | " Write JDAA chunk, length=%.20g.",(double) length); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10974 | (void) WriteBlobMSBULong(image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10975 | PNGType(chunk,mng_JDAA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10976 | LogPNGChunk(logging,mng_JDAA,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10977 | /* Write JDAT chunk(s) data */ |
| 10978 | (void) WriteBlob(image,4,chunk); |
| 10979 | (void) WriteBlob(image,length,blob); |
| 10980 | (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob, |
| 10981 | (uInt) length)); |
| 10982 | } |
| 10983 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 10984 | } |
| 10985 | |
| 10986 | /* Encode image as a JPEG blob */ |
| 10987 | if (logging != MagickFalse) |
| 10988 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10989 | " Creating jpeg_image_info."); |
| 10990 | jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info); |
| 10991 | if (jpeg_image_info == (ImageInfo *) NULL) |
| 10992 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 10993 | |
| 10994 | if (logging != MagickFalse) |
| 10995 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10996 | " Creating jpeg_image."); |
| 10997 | |
| 10998 | jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception); |
| 10999 | if (jpeg_image == (Image *) NULL) |
| 11000 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 11001 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 11002 | |
| 11003 | (void) AcquireUniqueFilename(jpeg_image->filename); |
| 11004 | (void) FormatMagickString(jpeg_image_info->filename,MaxTextExtent,"%s", |
| 11005 | jpeg_image->filename); |
| 11006 | |
| 11007 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
| 11008 | &image->exception); |
| 11009 | |
| 11010 | if (logging != MagickFalse) |
| 11011 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11012 | " Created jpeg_image, %.20g x %.20g.",(double) jpeg_image->columns, |
| 11013 | (double) jpeg_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11014 | |
| 11015 | if (jng_color_type == 8 || jng_color_type == 12) |
| 11016 | jpeg_image_info->type=GrayscaleType; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11017 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11018 | jpeg_image_info->quality=jng_quality % 1000; |
| 11019 | (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent); |
| 11020 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11021 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11022 | if (logging != MagickFalse) |
| 11023 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11024 | " Creating blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11025 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11026 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,&image->exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11027 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11028 | if (logging != MagickFalse) |
| 11029 | { |
| 11030 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11031 | " Successfully read jpeg_image into a blob, length=%.20g.", |
| 11032 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11033 | |
| 11034 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11035 | " Write JDAT chunk, length=%.20g.",(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11036 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11037 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11038 | /* Write JDAT chunk(s) */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11039 | (void) WriteBlobMSBULong(image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11040 | PNGType(chunk,mng_JDAT); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11041 | LogPNGChunk(logging,mng_JDAT,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11042 | (void) WriteBlob(image,4,chunk); |
| 11043 | (void) WriteBlob(image,length,blob); |
| 11044 | (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,(uInt) length)); |
| 11045 | |
| 11046 | jpeg_image=DestroyImage(jpeg_image); |
| 11047 | (void) RelinquishUniqueFileResource(jpeg_image_info->filename); |
| 11048 | jpeg_image_info=DestroyImageInfo(jpeg_image_info); |
| 11049 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 11050 | |
| 11051 | /* Write any JNG-chunk-e profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11052 | (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-e",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11053 | |
| 11054 | /* Write IEND chunk */ |
| 11055 | (void) WriteBlobMSBULong(image,0L); |
| 11056 | PNGType(chunk,mng_IEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11057 | LogPNGChunk(logging,mng_IEND,0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11058 | (void) WriteBlob(image,4,chunk); |
| 11059 | (void) WriteBlobMSBULong(image,crc32(0,chunk,4)); |
| 11060 | |
| 11061 | if (logging != MagickFalse) |
| 11062 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11063 | " exit WriteOneJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11064 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11065 | return(status); |
| 11066 | } |
| 11067 | |
| 11068 | |
| 11069 | /* |
| 11070 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11071 | % % |
| 11072 | % % |
| 11073 | % % |
| 11074 | % W r i t e J N G I m a g e % |
| 11075 | % % |
| 11076 | % % |
| 11077 | % % |
| 11078 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11079 | % |
| 11080 | % WriteJNGImage() writes a JPEG Network Graphics (JNG) image file. |
| 11081 | % |
| 11082 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 11083 | % |
| 11084 | % The format of the WriteJNGImage method is: |
| 11085 | % |
| 11086 | % MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image) |
| 11087 | % |
| 11088 | % A description of each parameter follows: |
| 11089 | % |
| 11090 | % o image_info: the image info. |
| 11091 | % |
| 11092 | % o image: The image. |
| 11093 | % |
| 11094 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11095 | */ |
| 11096 | static MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image) |
| 11097 | { |
| 11098 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 11099 | have_mng_structure, |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11100 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11101 | status; |
| 11102 | |
| 11103 | MngInfo |
| 11104 | *mng_info; |
| 11105 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11106 | /* |
| 11107 | Open image file. |
| 11108 | */ |
| 11109 | assert(image_info != (const ImageInfo *) NULL); |
| 11110 | assert(image_info->signature == MagickSignature); |
| 11111 | assert(image != (Image *) NULL); |
| 11112 | assert(image->signature == MagickSignature); |
| 11113 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 11114 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11115 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
| 11116 | if (status == MagickFalse) |
| 11117 | return(status); |
| 11118 | |
| 11119 | /* |
| 11120 | Allocate a MngInfo structure. |
| 11121 | */ |
| 11122 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 11123 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11124 | if (mng_info == (MngInfo *) NULL) |
| 11125 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 11126 | /* |
| 11127 | Initialize members of the MngInfo structure. |
| 11128 | */ |
| 11129 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 11130 | mng_info->image=image; |
| 11131 | have_mng_structure=MagickTrue; |
| 11132 | |
| 11133 | (void) WriteBlob(image,8,(const unsigned char *) "\213JNG\r\n\032\n"); |
| 11134 | |
| 11135 | status=WriteOneJNGImage(mng_info,image_info,image); |
| 11136 | (void) CloseBlob(image); |
| 11137 | |
| 11138 | (void) CatchImageException(image); |
| 11139 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 11140 | if (logging != MagickFalse) |
| 11141 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteJNGImage()"); |
| 11142 | return(status); |
| 11143 | } |
| 11144 | #endif |
| 11145 | |
| 11146 | |
| 11147 | |
| 11148 | static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image) |
| 11149 | { |
| 11150 | const char |
| 11151 | *option; |
| 11152 | |
| 11153 | Image |
| 11154 | *next_image; |
| 11155 | |
| 11156 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 11157 | have_mng_structure, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11158 | status; |
| 11159 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11160 | volatile MagickBooleanType |
| 11161 | logging; |
| 11162 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11163 | MngInfo |
| 11164 | *mng_info; |
| 11165 | |
| 11166 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11167 | image_count, |
| 11168 | need_iterations, |
| 11169 | need_matte; |
| 11170 | |
| 11171 | volatile int |
| 11172 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 11173 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 11174 | need_local_plte, |
| 11175 | #endif |
| 11176 | all_images_are_gray, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11177 | need_defi, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11178 | use_global_plte; |
| 11179 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11180 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11181 | i; |
| 11182 | |
| 11183 | unsigned char |
| 11184 | chunk[800]; |
| 11185 | |
| 11186 | volatile unsigned int |
| 11187 | write_jng, |
| 11188 | write_mng; |
| 11189 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11190 | volatile size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11191 | scene; |
| 11192 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11193 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11194 | final_delay=0, |
| 11195 | initial_delay; |
| 11196 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 11197 | #if (PNG_LIBPNG_VER < 10200) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11198 | if (image_info->verbose) |
| 11199 | printf("Your PNG library (libpng-%s) is rather old.\n", |
| 11200 | PNG_LIBPNG_VER_STRING); |
| 11201 | #endif |
| 11202 | |
| 11203 | /* |
| 11204 | Open image file. |
| 11205 | */ |
| 11206 | assert(image_info != (const ImageInfo *) NULL); |
| 11207 | assert(image_info->signature == MagickSignature); |
| 11208 | assert(image != (Image *) NULL); |
| 11209 | assert(image->signature == MagickSignature); |
| 11210 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 11211 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteMNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11212 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
| 11213 | if (status == MagickFalse) |
| 11214 | return(status); |
| 11215 | |
| 11216 | /* |
| 11217 | Allocate a MngInfo structure. |
| 11218 | */ |
| 11219 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 11220 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11221 | if (mng_info == (MngInfo *) NULL) |
| 11222 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 11223 | /* |
| 11224 | Initialize members of the MngInfo structure. |
| 11225 | */ |
| 11226 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 11227 | mng_info->image=image; |
| 11228 | have_mng_structure=MagickTrue; |
| 11229 | write_mng=LocaleCompare(image_info->magick,"MNG") == 0; |
| 11230 | |
| 11231 | /* |
| 11232 | * See if user has requested a specific PNG subformat to be used |
| 11233 | * for all of the PNGs in the MNG being written, e.g., |
| 11234 | * |
| 11235 | * convert *.png png8:animation.mng |
| 11236 | * |
| 11237 | * To do: check -define png:bit_depth and png:color_type as well, |
| 11238 | * or perhaps use mng:bit_depth and mng:color_type instead for |
| 11239 | * global settings. |
| 11240 | */ |
| 11241 | |
| 11242 | mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0; |
| 11243 | mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0; |
| 11244 | mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0; |
| 11245 | |
| 11246 | write_jng=MagickFalse; |
| 11247 | if (image_info->compression == JPEGCompression) |
| 11248 | write_jng=MagickTrue; |
| 11249 | |
| 11250 | mng_info->adjoin=image_info->adjoin && |
| 11251 | (GetNextImageInList(image) != (Image *) NULL) && write_mng; |
| 11252 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11253 | if (logging != MagickFalse) |
| 11254 | { |
| 11255 | /* Log some info about the input */ |
| 11256 | Image |
| 11257 | *p; |
| 11258 | |
| 11259 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11260 | " Checking input image(s)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11261 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11262 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11263 | " Image_info depth: %.20g",(double) image_info->depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11264 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11265 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11266 | " Type: %d",image_info->type); |
| 11267 | |
| 11268 | scene=0; |
| 11269 | for (p=image; p != (Image *) NULL; p=GetNextImageInList(p)) |
| 11270 | { |
| 11271 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11272 | " Scene: %.20g",(double) scene++); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11273 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11274 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11275 | " Image depth: %.20g",(double) p->depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11276 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11277 | if (p->matte) |
| 11278 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11279 | " Matte: True"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11280 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11281 | else |
| 11282 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11283 | " Matte: False"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11284 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11285 | if (p->storage_class == PseudoClass) |
| 11286 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11287 | " Storage class: PseudoClass"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11288 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11289 | else |
| 11290 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11291 | " Storage class: DirectClass"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11292 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11293 | if (p->colors) |
| 11294 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11295 | " Number of colors: %.20g",(double) p->colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11296 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11297 | else |
| 11298 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11299 | " Number of colors: unspecified"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11300 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11301 | if (mng_info->adjoin == MagickFalse) |
| 11302 | break; |
| 11303 | } |
| 11304 | } |
| 11305 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11306 | use_global_plte=MagickFalse; |
| 11307 | all_images_are_gray=MagickFalse; |
| 11308 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 11309 | need_local_plte=MagickTrue; |
| 11310 | #endif |
| 11311 | need_defi=MagickFalse; |
| 11312 | need_matte=MagickFalse; |
| 11313 | mng_info->framing_mode=1; |
| 11314 | mng_info->old_framing_mode=1; |
| 11315 | |
| 11316 | if (write_mng) |
| 11317 | if (image_info->page != (char *) NULL) |
| 11318 | { |
| 11319 | /* |
| 11320 | Determine image bounding box. |
| 11321 | */ |
| 11322 | SetGeometry(image,&mng_info->page); |
| 11323 | (void) ParseMetaGeometry(image_info->page,&mng_info->page.x, |
| 11324 | &mng_info->page.y,&mng_info->page.width,&mng_info->page.height); |
| 11325 | } |
| 11326 | if (write_mng) |
| 11327 | { |
| 11328 | unsigned int |
| 11329 | need_geom; |
| 11330 | |
| 11331 | unsigned short |
| 11332 | red, |
| 11333 | green, |
| 11334 | blue; |
| 11335 | |
| 11336 | mng_info->page=image->page; |
| 11337 | need_geom=MagickTrue; |
| 11338 | if (mng_info->page.width || mng_info->page.height) |
| 11339 | need_geom=MagickFalse; |
| 11340 | /* |
| 11341 | Check all the scenes. |
| 11342 | */ |
| 11343 | initial_delay=image->delay; |
| 11344 | need_iterations=MagickFalse; |
| 11345 | mng_info->equal_chrms=image->chromaticity.red_primary.x != 0.0; |
| 11346 | mng_info->equal_physs=MagickTrue, |
| 11347 | mng_info->equal_gammas=MagickTrue; |
| 11348 | mng_info->equal_srgbs=MagickTrue; |
| 11349 | mng_info->equal_backgrounds=MagickTrue; |
| 11350 | image_count=0; |
| 11351 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 11352 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 11353 | all_images_are_gray=MagickTrue; |
| 11354 | mng_info->equal_palettes=MagickFalse; |
| 11355 | need_local_plte=MagickFalse; |
| 11356 | #endif |
| 11357 | for (next_image=image; next_image != (Image *) NULL; ) |
| 11358 | { |
| 11359 | if (need_geom) |
| 11360 | { |
| 11361 | if ((next_image->columns+next_image->page.x) > mng_info->page.width) |
| 11362 | mng_info->page.width=next_image->columns+next_image->page.x; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11363 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11364 | if ((next_image->rows+next_image->page.y) > mng_info->page.height) |
| 11365 | mng_info->page.height=next_image->rows+next_image->page.y; |
| 11366 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11367 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11368 | if (next_image->page.x || next_image->page.y) |
| 11369 | need_defi=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11370 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11371 | if (next_image->matte) |
| 11372 | need_matte=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11373 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11374 | if ((int) next_image->dispose >= BackgroundDispose) |
| 11375 | if (next_image->matte || next_image->page.x || next_image->page.y || |
| 11376 | ((next_image->columns < mng_info->page.width) && |
| 11377 | (next_image->rows < mng_info->page.height))) |
| 11378 | mng_info->need_fram=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11379 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11380 | if (next_image->iterations) |
| 11381 | need_iterations=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11382 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11383 | final_delay=next_image->delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11384 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11385 | if (final_delay != initial_delay || final_delay > 1UL* |
| 11386 | next_image->ticks_per_second) |
| 11387 | mng_info->need_fram=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11388 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11389 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 11390 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 11391 | /* |
| 11392 | check for global palette possibility. |
| 11393 | */ |
| 11394 | if (image->matte != MagickFalse) |
| 11395 | need_local_plte=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11396 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11397 | if (need_local_plte == 0) |
| 11398 | { |
| 11399 | if (ImageIsGray(image) == MagickFalse) |
| 11400 | all_images_are_gray=MagickFalse; |
| 11401 | mng_info->equal_palettes=PalettesAreEqual(image,next_image); |
| 11402 | if (use_global_plte == 0) |
| 11403 | use_global_plte=mng_info->equal_palettes; |
| 11404 | need_local_plte=!mng_info->equal_palettes; |
| 11405 | } |
| 11406 | #endif |
| 11407 | if (GetNextImageInList(next_image) != (Image *) NULL) |
| 11408 | { |
| 11409 | if (next_image->background_color.red != |
| 11410 | next_image->next->background_color.red || |
| 11411 | next_image->background_color.green != |
| 11412 | next_image->next->background_color.green || |
| 11413 | next_image->background_color.blue != |
| 11414 | next_image->next->background_color.blue) |
| 11415 | mng_info->equal_backgrounds=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11416 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11417 | if (next_image->gamma != next_image->next->gamma) |
| 11418 | mng_info->equal_gammas=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11419 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11420 | if (next_image->rendering_intent != |
| 11421 | next_image->next->rendering_intent) |
| 11422 | mng_info->equal_srgbs=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11423 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11424 | if ((next_image->units != next_image->next->units) || |
| 11425 | (next_image->x_resolution != next_image->next->x_resolution) || |
| 11426 | (next_image->y_resolution != next_image->next->y_resolution)) |
| 11427 | mng_info->equal_physs=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11428 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11429 | if (mng_info->equal_chrms) |
| 11430 | { |
| 11431 | if (next_image->chromaticity.red_primary.x != |
| 11432 | next_image->next->chromaticity.red_primary.x || |
| 11433 | next_image->chromaticity.red_primary.y != |
| 11434 | next_image->next->chromaticity.red_primary.y || |
| 11435 | next_image->chromaticity.green_primary.x != |
| 11436 | next_image->next->chromaticity.green_primary.x || |
| 11437 | next_image->chromaticity.green_primary.y != |
| 11438 | next_image->next->chromaticity.green_primary.y || |
| 11439 | next_image->chromaticity.blue_primary.x != |
| 11440 | next_image->next->chromaticity.blue_primary.x || |
| 11441 | next_image->chromaticity.blue_primary.y != |
| 11442 | next_image->next->chromaticity.blue_primary.y || |
| 11443 | next_image->chromaticity.white_point.x != |
| 11444 | next_image->next->chromaticity.white_point.x || |
| 11445 | next_image->chromaticity.white_point.y != |
| 11446 | next_image->next->chromaticity.white_point.y) |
| 11447 | mng_info->equal_chrms=MagickFalse; |
| 11448 | } |
| 11449 | } |
| 11450 | image_count++; |
| 11451 | next_image=GetNextImageInList(next_image); |
| 11452 | } |
| 11453 | if (image_count < 2) |
| 11454 | { |
| 11455 | mng_info->equal_backgrounds=MagickFalse; |
| 11456 | mng_info->equal_chrms=MagickFalse; |
| 11457 | mng_info->equal_gammas=MagickFalse; |
| 11458 | mng_info->equal_srgbs=MagickFalse; |
| 11459 | mng_info->equal_physs=MagickFalse; |
| 11460 | use_global_plte=MagickFalse; |
| 11461 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 11462 | need_local_plte=MagickTrue; |
| 11463 | #endif |
| 11464 | need_iterations=MagickFalse; |
| 11465 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11466 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11467 | if (mng_info->need_fram == MagickFalse) |
| 11468 | { |
| 11469 | /* |
| 11470 | Only certain framing rates 100/n are exactly representable without |
| 11471 | the FRAM chunk but we'll allow some slop in VLC files |
| 11472 | */ |
| 11473 | if (final_delay == 0) |
| 11474 | { |
| 11475 | if (need_iterations != MagickFalse) |
| 11476 | { |
| 11477 | /* |
| 11478 | It's probably a GIF with loop; don't run it *too* fast. |
| 11479 | */ |
glennrp | 0261712 | 2010-07-28 13:07:35 +0000 | [diff] [blame] | 11480 | if (mng_info->adjoin) |
glennrp | d908de4 | 2010-07-28 13:28:27 +0000 | [diff] [blame] | 11481 | { |
| 11482 | final_delay=10; |
| 11483 | (void) ThrowMagickException(&image->exception, |
| 11484 | GetMagickModule(),CoderWarning, |
| 11485 | "input has zero delay between all frames; assuming", |
| 11486 | " 10 cs `%s'",""); |
| 11487 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11488 | } |
| 11489 | else |
| 11490 | mng_info->ticks_per_second=0; |
| 11491 | } |
| 11492 | if (final_delay != 0) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11493 | mng_info->ticks_per_second=(png_uint_32) |
| 11494 | (image->ticks_per_second/final_delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11495 | if (final_delay > 50) |
| 11496 | mng_info->ticks_per_second=2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11497 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11498 | if (final_delay > 75) |
| 11499 | mng_info->ticks_per_second=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11500 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11501 | if (final_delay > 125) |
| 11502 | mng_info->need_fram=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11503 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11504 | if (need_defi && final_delay > 2 && (final_delay != 4) && |
| 11505 | (final_delay != 5) && (final_delay != 10) && (final_delay != 20) && |
| 11506 | (final_delay != 25) && (final_delay != 50) && (final_delay != |
| 11507 | 1UL*image->ticks_per_second)) |
| 11508 | mng_info->need_fram=MagickTrue; /* make it exact; cannot be VLC */ |
| 11509 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11510 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11511 | if (mng_info->need_fram != MagickFalse) |
| 11512 | mng_info->ticks_per_second=1UL*image->ticks_per_second; |
| 11513 | /* |
| 11514 | If pseudocolor, we should also check to see if all the |
| 11515 | palettes are identical and write a global PLTE if they are. |
| 11516 | ../glennrp Feb 99. |
| 11517 | */ |
| 11518 | /* |
| 11519 | Write the MNG version 1.0 signature and MHDR chunk. |
| 11520 | */ |
| 11521 | (void) WriteBlob(image,8,(const unsigned char *) "\212MNG\r\n\032\n"); |
| 11522 | (void) WriteBlobMSBULong(image,28L); /* chunk data length=28 */ |
| 11523 | PNGType(chunk,mng_MHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11524 | LogPNGChunk(logging,mng_MHDR,28L); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 11525 | PNGLong(chunk+4,(png_uint_32) mng_info->page.width); |
| 11526 | PNGLong(chunk+8,(png_uint_32) mng_info->page.height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11527 | PNGLong(chunk+12,mng_info->ticks_per_second); |
| 11528 | PNGLong(chunk+16,0L); /* layer count=unknown */ |
| 11529 | PNGLong(chunk+20,0L); /* frame count=unknown */ |
| 11530 | PNGLong(chunk+24,0L); /* play time=unknown */ |
| 11531 | if (write_jng) |
| 11532 | { |
| 11533 | if (need_matte) |
| 11534 | { |
| 11535 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 11536 | PNGLong(chunk+28,27L); /* simplicity=LC+JNG */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11538 | else |
| 11539 | PNGLong(chunk+28,25L); /* simplicity=VLC+JNG */ |
| 11540 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11541 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11542 | else |
| 11543 | { |
| 11544 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 11545 | PNGLong(chunk+28,19L); /* simplicity=LC+JNG, no transparency */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11546 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11547 | else |
| 11548 | PNGLong(chunk+28,17L); /* simplicity=VLC+JNG, no transparency */ |
| 11549 | } |
| 11550 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11551 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11552 | else |
| 11553 | { |
| 11554 | if (need_matte) |
| 11555 | { |
| 11556 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 11557 | PNGLong(chunk+28,11L); /* simplicity=LC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11558 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11559 | else |
| 11560 | PNGLong(chunk+28,9L); /* simplicity=VLC */ |
| 11561 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11562 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11563 | else |
| 11564 | { |
| 11565 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 11566 | PNGLong(chunk+28,3L); /* simplicity=LC, no transparency */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11567 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11568 | else |
| 11569 | PNGLong(chunk+28,1L); /* simplicity=VLC, no transparency */ |
| 11570 | } |
| 11571 | } |
| 11572 | (void) WriteBlob(image,32,chunk); |
| 11573 | (void) WriteBlobMSBULong(image,crc32(0,chunk,32)); |
| 11574 | option=GetImageOption(image_info,"mng:need-cacheoff"); |
| 11575 | if (option != (const char *) NULL) |
| 11576 | { |
| 11577 | size_t |
| 11578 | length; |
| 11579 | |
| 11580 | /* |
| 11581 | Write "nEED CACHEOFF" to turn playback caching off for streaming MNG. |
| 11582 | */ |
| 11583 | PNGType(chunk,mng_nEED); |
| 11584 | length=CopyMagickString((char *) chunk+4,"CACHEOFF",20); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11585 | (void) WriteBlobMSBULong(image,(size_t) length); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11586 | LogPNGChunk(logging,mng_nEED,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11587 | length+=4; |
| 11588 | (void) WriteBlob(image,length,chunk); |
| 11589 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) length)); |
| 11590 | } |
| 11591 | if ((GetPreviousImageInList(image) == (Image *) NULL) && |
| 11592 | (GetNextImageInList(image) != (Image *) NULL) && |
| 11593 | (image->iterations != 1)) |
| 11594 | { |
| 11595 | /* |
| 11596 | Write MNG TERM chunk |
| 11597 | */ |
| 11598 | (void) WriteBlobMSBULong(image,10L); /* data length=10 */ |
| 11599 | PNGType(chunk,mng_TERM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11600 | LogPNGChunk(logging,mng_TERM,10L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11601 | chunk[4]=3; /* repeat animation */ |
| 11602 | chunk[5]=0; /* show last frame when done */ |
| 11603 | PNGLong(chunk+6,(png_uint_32) (mng_info->ticks_per_second* |
| 11604 | final_delay/MagickMax(image->ticks_per_second,1))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11605 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11606 | if (image->iterations == 0) |
| 11607 | PNGLong(chunk+10,PNG_UINT_31_MAX); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11608 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11609 | else |
| 11610 | PNGLong(chunk+10,(png_uint_32) image->iterations); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11611 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11612 | if (logging != MagickFalse) |
| 11613 | { |
| 11614 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11615 | " TERM delay: %.20g",(double) (mng_info->ticks_per_second* |
| 11616 | final_delay/MagickMax(image->ticks_per_second,1))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11617 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11618 | if (image->iterations == 0) |
| 11619 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11620 | " TERM iterations: %.20g",(double) PNG_UINT_31_MAX); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11621 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11622 | else |
| 11623 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11624 | " Image iterations: %.20g",(double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11625 | } |
| 11626 | (void) WriteBlob(image,14,chunk); |
| 11627 | (void) WriteBlobMSBULong(image,crc32(0,chunk,14)); |
| 11628 | } |
| 11629 | /* |
| 11630 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 11631 | */ |
| 11632 | if ((image->colorspace == sRGBColorspace || image->rendering_intent) && |
| 11633 | mng_info->equal_srgbs) |
| 11634 | { |
| 11635 | /* |
| 11636 | Write MNG sRGB chunk |
| 11637 | */ |
| 11638 | (void) WriteBlobMSBULong(image,1L); |
| 11639 | PNGType(chunk,mng_sRGB); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11640 | LogPNGChunk(logging,mng_sRGB,1L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11641 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11642 | if (image->rendering_intent != UndefinedIntent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 11643 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11644 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 11645 | (image->rendering_intent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11646 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11647 | else |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 11648 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11649 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
| 11650 | (PerceptualIntent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11651 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11652 | (void) WriteBlob(image,5,chunk); |
| 11653 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 11654 | mng_info->have_write_global_srgb=MagickTrue; |
| 11655 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11656 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11657 | else |
| 11658 | { |
| 11659 | if (image->gamma && mng_info->equal_gammas) |
| 11660 | { |
| 11661 | /* |
| 11662 | Write MNG gAMA chunk |
| 11663 | */ |
| 11664 | (void) WriteBlobMSBULong(image,4L); |
| 11665 | PNGType(chunk,mng_gAMA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11666 | LogPNGChunk(logging,mng_gAMA,4L); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11667 | PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11668 | (void) WriteBlob(image,8,chunk); |
| 11669 | (void) WriteBlobMSBULong(image,crc32(0,chunk,8)); |
| 11670 | mng_info->have_write_global_gama=MagickTrue; |
| 11671 | } |
| 11672 | if (mng_info->equal_chrms) |
| 11673 | { |
| 11674 | PrimaryInfo |
| 11675 | primary; |
| 11676 | |
| 11677 | /* |
| 11678 | Write MNG cHRM chunk |
| 11679 | */ |
| 11680 | (void) WriteBlobMSBULong(image,32L); |
| 11681 | PNGType(chunk,mng_cHRM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11682 | LogPNGChunk(logging,mng_cHRM,32L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11683 | primary=image->chromaticity.white_point; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11684 | PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5)); |
| 11685 | PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11686 | primary=image->chromaticity.red_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11687 | PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5)); |
| 11688 | PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11689 | primary=image->chromaticity.green_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11690 | PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5)); |
| 11691 | PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11692 | primary=image->chromaticity.blue_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11693 | PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5)); |
| 11694 | PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11695 | (void) WriteBlob(image,36,chunk); |
| 11696 | (void) WriteBlobMSBULong(image,crc32(0,chunk,36)); |
| 11697 | mng_info->have_write_global_chrm=MagickTrue; |
| 11698 | } |
| 11699 | } |
| 11700 | if (image->x_resolution && image->y_resolution && mng_info->equal_physs) |
| 11701 | { |
| 11702 | /* |
| 11703 | Write MNG pHYs chunk |
| 11704 | */ |
| 11705 | (void) WriteBlobMSBULong(image,9L); |
| 11706 | PNGType(chunk,mng_pHYs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11707 | LogPNGChunk(logging,mng_pHYs,9L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11708 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11709 | if (image->units == PixelsPerInchResolution) |
| 11710 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11711 | PNGLong(chunk+4,(png_uint_32) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11712 | (image->x_resolution*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11713 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11714 | PNGLong(chunk+8,(png_uint_32) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11715 | (image->y_resolution*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11716 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11717 | chunk[12]=1; |
| 11718 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11719 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11720 | else |
| 11721 | { |
| 11722 | if (image->units == PixelsPerCentimeterResolution) |
| 11723 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11724 | PNGLong(chunk+4,(png_uint_32) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11725 | (image->x_resolution*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11726 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11727 | PNGLong(chunk+8,(png_uint_32) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11728 | (image->y_resolution*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11729 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11730 | chunk[12]=1; |
| 11731 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11732 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11733 | else |
| 11734 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 11735 | PNGLong(chunk+4,(png_uint_32) (image->x_resolution+0.5)); |
| 11736 | PNGLong(chunk+8,(png_uint_32) (image->y_resolution+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11737 | chunk[12]=0; |
| 11738 | } |
| 11739 | } |
| 11740 | (void) WriteBlob(image,13,chunk); |
| 11741 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 11742 | } |
| 11743 | /* |
| 11744 | Write MNG BACK chunk and global bKGD chunk, if the image is transparent |
| 11745 | or does not cover the entire frame. |
| 11746 | */ |
| 11747 | if (write_mng && (image->matte || image->page.x > 0 || |
| 11748 | image->page.y > 0 || (image->page.width && |
| 11749 | (image->page.width+image->page.x < mng_info->page.width)) |
| 11750 | || (image->page.height && (image->page.height+image->page.y |
| 11751 | < mng_info->page.height)))) |
| 11752 | { |
| 11753 | (void) WriteBlobMSBULong(image,6L); |
| 11754 | PNGType(chunk,mng_BACK); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11755 | LogPNGChunk(logging,mng_BACK,6L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11756 | red=ScaleQuantumToShort(image->background_color.red); |
| 11757 | green=ScaleQuantumToShort(image->background_color.green); |
| 11758 | blue=ScaleQuantumToShort(image->background_color.blue); |
| 11759 | PNGShort(chunk+4,red); |
| 11760 | PNGShort(chunk+6,green); |
| 11761 | PNGShort(chunk+8,blue); |
| 11762 | (void) WriteBlob(image,10,chunk); |
| 11763 | (void) WriteBlobMSBULong(image,crc32(0,chunk,10)); |
| 11764 | if (mng_info->equal_backgrounds) |
| 11765 | { |
| 11766 | (void) WriteBlobMSBULong(image,6L); |
| 11767 | PNGType(chunk,mng_bKGD); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11768 | LogPNGChunk(logging,mng_bKGD,6L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11769 | (void) WriteBlob(image,10,chunk); |
| 11770 | (void) WriteBlobMSBULong(image,crc32(0,chunk,10)); |
| 11771 | } |
| 11772 | } |
| 11773 | |
| 11774 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 11775 | if ((need_local_plte == MagickFalse) && |
| 11776 | (image->storage_class == PseudoClass) && |
| 11777 | (all_images_are_gray == MagickFalse)) |
| 11778 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11779 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11780 | data_length; |
| 11781 | |
| 11782 | /* |
| 11783 | Write MNG PLTE chunk |
| 11784 | */ |
| 11785 | data_length=3*image->colors; |
| 11786 | (void) WriteBlobMSBULong(image,data_length); |
| 11787 | PNGType(chunk,mng_PLTE); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11788 | LogPNGChunk(logging,mng_PLTE,data_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11789 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11790 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11791 | { |
| 11792 | chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red) & 0xff; |
| 11793 | chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green) & 0xff; |
| 11794 | chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue) & 0xff; |
| 11795 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11796 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11797 | (void) WriteBlob(image,data_length+4,chunk); |
| 11798 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) (data_length+4))); |
| 11799 | mng_info->have_write_global_plte=MagickTrue; |
| 11800 | } |
| 11801 | #endif |
| 11802 | } |
| 11803 | scene=0; |
| 11804 | mng_info->delay=0; |
| 11805 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 11806 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 11807 | mng_info->equal_palettes=MagickFalse; |
| 11808 | #endif |
| 11809 | do |
| 11810 | { |
| 11811 | if (mng_info->adjoin) |
| 11812 | { |
| 11813 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 11814 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 11815 | /* |
| 11816 | If we aren't using a global palette for the entire MNG, check to |
| 11817 | see if we can use one for two or more consecutive images. |
| 11818 | */ |
| 11819 | if (need_local_plte && use_global_plte && !all_images_are_gray) |
| 11820 | { |
| 11821 | if (mng_info->IsPalette) |
| 11822 | { |
| 11823 | /* |
| 11824 | When equal_palettes is true, this image has the same palette |
| 11825 | as the previous PseudoClass image |
| 11826 | */ |
| 11827 | mng_info->have_write_global_plte=mng_info->equal_palettes; |
| 11828 | mng_info->equal_palettes=PalettesAreEqual(image,image->next); |
| 11829 | if (mng_info->equal_palettes && !mng_info->have_write_global_plte) |
| 11830 | { |
| 11831 | /* |
| 11832 | Write MNG PLTE chunk |
| 11833 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11834 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11835 | data_length; |
| 11836 | |
| 11837 | data_length=3*image->colors; |
| 11838 | (void) WriteBlobMSBULong(image,data_length); |
| 11839 | PNGType(chunk,mng_PLTE); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11840 | LogPNGChunk(logging,mng_PLTE,data_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11841 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11842 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11843 | { |
| 11844 | chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red); |
| 11845 | chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green); |
| 11846 | chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue); |
| 11847 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11848 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11849 | (void) WriteBlob(image,data_length+4,chunk); |
| 11850 | (void) WriteBlobMSBULong(image,crc32(0,chunk, |
| 11851 | (uInt) (data_length+4))); |
| 11852 | mng_info->have_write_global_plte=MagickTrue; |
| 11853 | } |
| 11854 | } |
| 11855 | else |
| 11856 | mng_info->have_write_global_plte=MagickFalse; |
| 11857 | } |
| 11858 | #endif |
| 11859 | if (need_defi) |
| 11860 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11861 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11862 | previous_x, |
| 11863 | previous_y; |
| 11864 | |
| 11865 | if (scene) |
| 11866 | { |
| 11867 | previous_x=mng_info->page.x; |
| 11868 | previous_y=mng_info->page.y; |
| 11869 | } |
| 11870 | else |
| 11871 | { |
| 11872 | previous_x=0; |
| 11873 | previous_y=0; |
| 11874 | } |
| 11875 | mng_info->page=image->page; |
| 11876 | if ((mng_info->page.x != previous_x) || |
| 11877 | (mng_info->page.y != previous_y)) |
| 11878 | { |
| 11879 | (void) WriteBlobMSBULong(image,12L); /* data length=12 */ |
| 11880 | PNGType(chunk,mng_DEFI); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11881 | LogPNGChunk(logging,mng_DEFI,12L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11882 | chunk[4]=0; /* object 0 MSB */ |
| 11883 | chunk[5]=0; /* object 0 LSB */ |
| 11884 | chunk[6]=0; /* visible */ |
| 11885 | chunk[7]=0; /* abstract */ |
| 11886 | PNGLong(chunk+8,(png_uint_32) mng_info->page.x); |
| 11887 | PNGLong(chunk+12,(png_uint_32) mng_info->page.y); |
| 11888 | (void) WriteBlob(image,16,chunk); |
| 11889 | (void) WriteBlobMSBULong(image,crc32(0,chunk,16)); |
| 11890 | } |
| 11891 | } |
| 11892 | } |
| 11893 | |
| 11894 | mng_info->write_mng=write_mng; |
| 11895 | |
| 11896 | if ((int) image->dispose >= 3) |
| 11897 | mng_info->framing_mode=3; |
| 11898 | |
| 11899 | if (mng_info->need_fram && mng_info->adjoin && |
| 11900 | ((image->delay != mng_info->delay) || |
| 11901 | (mng_info->framing_mode != mng_info->old_framing_mode))) |
| 11902 | { |
| 11903 | if (image->delay == mng_info->delay) |
| 11904 | { |
| 11905 | /* |
| 11906 | Write a MNG FRAM chunk with the new framing mode. |
| 11907 | */ |
| 11908 | (void) WriteBlobMSBULong(image,1L); /* data length=1 */ |
| 11909 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11910 | LogPNGChunk(logging,mng_FRAM,1L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11911 | chunk[4]=(unsigned char) mng_info->framing_mode; |
| 11912 | (void) WriteBlob(image,5,chunk); |
| 11913 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 11914 | } |
| 11915 | else |
| 11916 | { |
| 11917 | /* |
| 11918 | Write a MNG FRAM chunk with the delay. |
| 11919 | */ |
| 11920 | (void) WriteBlobMSBULong(image,10L); /* data length=10 */ |
| 11921 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11922 | LogPNGChunk(logging,mng_FRAM,10L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11923 | chunk[4]=(unsigned char) mng_info->framing_mode; |
| 11924 | chunk[5]=0; /* frame name separator (no name) */ |
| 11925 | chunk[6]=2; /* flag for changing default delay */ |
| 11926 | chunk[7]=0; /* flag for changing frame timeout */ |
| 11927 | chunk[8]=0; /* flag for changing frame clipping */ |
| 11928 | chunk[9]=0; /* flag for changing frame sync_id */ |
| 11929 | PNGLong(chunk+10,(png_uint_32) |
| 11930 | ((mng_info->ticks_per_second* |
| 11931 | image->delay)/MagickMax(image->ticks_per_second,1))); |
| 11932 | (void) WriteBlob(image,14,chunk); |
| 11933 | (void) WriteBlobMSBULong(image,crc32(0,chunk,14)); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 11934 | mng_info->delay=(png_uint_32) image->delay; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11935 | } |
| 11936 | mng_info->old_framing_mode=mng_info->framing_mode; |
| 11937 | } |
| 11938 | |
| 11939 | #if defined(JNG_SUPPORTED) |
| 11940 | if (image_info->compression == JPEGCompression) |
| 11941 | { |
| 11942 | ImageInfo |
| 11943 | *write_info; |
| 11944 | |
| 11945 | if (logging != MagickFalse) |
| 11946 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11947 | " Writing JNG object."); |
| 11948 | /* To do: specify the desired alpha compression method. */ |
| 11949 | write_info=CloneImageInfo(image_info); |
| 11950 | write_info->compression=UndefinedCompression; |
| 11951 | status=WriteOneJNGImage(mng_info,write_info,image); |
| 11952 | write_info=DestroyImageInfo(write_info); |
| 11953 | } |
| 11954 | else |
| 11955 | #endif |
| 11956 | { |
| 11957 | if (logging != MagickFalse) |
| 11958 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11959 | " Writing PNG object."); |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 11960 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 11961 | mng_info->need_blob = MagickFalse; |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 11962 | mng_info->ping_preserve_colormap = MagickFalse; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 11963 | |
| 11964 | /* We don't want any ancillary chunks written */ |
| 11965 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 11966 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11967 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 11968 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 11969 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 11970 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 11971 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 11972 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 11973 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 11974 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 11975 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11976 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 11977 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 11978 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 11979 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 11980 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11981 | status=WriteOnePNGImage(mng_info,image_info,image); |
| 11982 | } |
| 11983 | |
| 11984 | if (status == MagickFalse) |
| 11985 | { |
| 11986 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 11987 | (void) CloseBlob(image); |
| 11988 | return(MagickFalse); |
| 11989 | } |
| 11990 | (void) CatchImageException(image); |
| 11991 | if (GetNextImageInList(image) == (Image *) NULL) |
| 11992 | break; |
| 11993 | image=SyncNextImageInList(image); |
| 11994 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 11995 | GetImageListLength(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11996 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11997 | if (status == MagickFalse) |
| 11998 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11999 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12000 | } while (mng_info->adjoin); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12001 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12002 | if (write_mng) |
| 12003 | { |
| 12004 | while (GetPreviousImageInList(image) != (Image *) NULL) |
| 12005 | image=GetPreviousImageInList(image); |
| 12006 | /* |
| 12007 | Write the MEND chunk. |
| 12008 | */ |
| 12009 | (void) WriteBlobMSBULong(image,0x00000000L); |
| 12010 | PNGType(chunk,mng_MEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12011 | LogPNGChunk(logging,mng_MEND,0L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12012 | (void) WriteBlob(image,4,chunk); |
| 12013 | (void) WriteBlobMSBULong(image,crc32(0,chunk,4)); |
| 12014 | } |
| 12015 | /* |
| 12016 | Relinquish resources. |
| 12017 | */ |
| 12018 | (void) CloseBlob(image); |
| 12019 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12020 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12021 | if (logging != MagickFalse) |
| 12022 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12023 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12024 | return(MagickTrue); |
| 12025 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 12026 | #else /* PNG_LIBPNG_VER > 10011 */ |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 12027 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12028 | static MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image) |
| 12029 | { |
| 12030 | image=image; |
| 12031 | printf("Your PNG library is too old: You have libpng-%s\n", |
| 12032 | PNG_LIBPNG_VER_STRING); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12033 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12034 | ThrowBinaryException(CoderError,"PNG library is too old", |
| 12035 | image_info->filename); |
| 12036 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 12037 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12038 | static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image) |
| 12039 | { |
| 12040 | return(WritePNGImage(image_info,image)); |
| 12041 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 12042 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12043 | #endif |