| OLD | NEW |
| 1 | 1 |
| 2 /* pngwtran.c - transforms the data in a row for PNG writers | 2 /* pngwtran.c - transforms the data in a row for PNG writers |
| 3 * | 3 * |
| 4 * Last changed in libpng 1.6.0 [February 14, 2013] | 4 * Last changed in libpng 1.6.18 [July 23, 2015] |
| 5 * Copyright (c) 1998-2013 Glenn Randers-Pehrson | 5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson |
| 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) | 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) | 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
| 8 * | 8 * |
| 9 * This code is released under the libpng license. | 9 * This code is released under the libpng license. |
| 10 * For conditions of distribution and use, see the disclaimer | 10 * For conditions of distribution and use, see the disclaimer |
| 11 * and license in png.h | 11 * and license in png.h |
| 12 */ | 12 */ |
| 13 |
| 13 #include "pngpriv.h" | 14 #include "pngpriv.h" |
| 14 | 15 |
| 15 #ifdef PNG_WRITE_SUPPORTED | 16 #ifdef PNG_WRITE_SUPPORTED |
| 16 | |
| 17 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED | 17 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED |
| 18 |
| 19 #ifdef PNG_WRITE_PACK_SUPPORTED |
| 20 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The |
| 21 * row_info bit depth should be 8 (one pixel per byte). The channels |
| 22 * should be 1 (this only happens on grayscale and paletted images). |
| 23 */ |
| 24 static void |
| 25 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) |
| 26 { |
| 27 png_debug(1, "in png_do_pack"); |
| 28 |
| 29 if (row_info->bit_depth == 8 && |
| 30 row_info->channels == 1) |
| 31 { |
| 32 switch ((int)bit_depth) |
| 33 { |
| 34 case 1: |
| 35 { |
| 36 png_bytep sp, dp; |
| 37 int mask, v; |
| 38 png_uint_32 i; |
| 39 png_uint_32 row_width = row_info->width; |
| 40 |
| 41 sp = row; |
| 42 dp = row; |
| 43 mask = 0x80; |
| 44 v = 0; |
| 45 |
| 46 for (i = 0; i < row_width; i++) |
| 47 { |
| 48 if (*sp != 0) |
| 49 v |= mask; |
| 50 |
| 51 sp++; |
| 52 |
| 53 if (mask > 1) |
| 54 mask >>= 1; |
| 55 |
| 56 else |
| 57 { |
| 58 mask = 0x80; |
| 59 *dp = (png_byte)v; |
| 60 dp++; |
| 61 v = 0; |
| 62 } |
| 63 } |
| 64 |
| 65 if (mask != 0x80) |
| 66 *dp = (png_byte)v; |
| 67 |
| 68 break; |
| 69 } |
| 70 |
| 71 case 2: |
| 72 { |
| 73 png_bytep sp, dp; |
| 74 unsigned int shift; |
| 75 int v; |
| 76 png_uint_32 i; |
| 77 png_uint_32 row_width = row_info->width; |
| 78 |
| 79 sp = row; |
| 80 dp = row; |
| 81 shift = 6; |
| 82 v = 0; |
| 83 |
| 84 for (i = 0; i < row_width; i++) |
| 85 { |
| 86 png_byte value; |
| 87 |
| 88 value = (png_byte)(*sp & 0x03); |
| 89 v |= (value << shift); |
| 90 |
| 91 if (shift == 0) |
| 92 { |
| 93 shift = 6; |
| 94 *dp = (png_byte)v; |
| 95 dp++; |
| 96 v = 0; |
| 97 } |
| 98 |
| 99 else |
| 100 shift -= 2; |
| 101 |
| 102 sp++; |
| 103 } |
| 104 |
| 105 if (shift != 6) |
| 106 *dp = (png_byte)v; |
| 107 |
| 108 break; |
| 109 } |
| 110 |
| 111 case 4: |
| 112 { |
| 113 png_bytep sp, dp; |
| 114 unsigned int shift; |
| 115 int v; |
| 116 png_uint_32 i; |
| 117 png_uint_32 row_width = row_info->width; |
| 118 |
| 119 sp = row; |
| 120 dp = row; |
| 121 shift = 4; |
| 122 v = 0; |
| 123 |
| 124 for (i = 0; i < row_width; i++) |
| 125 { |
| 126 png_byte value; |
| 127 |
| 128 value = (png_byte)(*sp & 0x0f); |
| 129 v |= (value << shift); |
| 130 |
| 131 if (shift == 0) |
| 132 { |
| 133 shift = 4; |
| 134 *dp = (png_byte)v; |
| 135 dp++; |
| 136 v = 0; |
| 137 } |
| 138 |
| 139 else |
| 140 shift -= 4; |
| 141 |
| 142 sp++; |
| 143 } |
| 144 |
| 145 if (shift != 4) |
| 146 *dp = (png_byte)v; |
| 147 |
| 148 break; |
| 149 } |
| 150 |
| 151 default: |
| 152 break; |
| 153 } |
| 154 |
| 155 row_info->bit_depth = (png_byte)bit_depth; |
| 156 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); |
| 157 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, |
| 158 row_info->width); |
| 159 } |
| 160 } |
| 161 #endif |
| 162 |
| 163 #ifdef PNG_WRITE_SHIFT_SUPPORTED |
| 164 /* Shift pixel values to take advantage of whole range. Pass the |
| 165 * true number of bits in bit_depth. The row should be packed |
| 166 * according to row_info->bit_depth. Thus, if you had a row of |
| 167 * bit depth 4, but the pixels only had values from 0 to 7, you |
| 168 * would pass 3 as bit_depth, and this routine would translate the |
| 169 * data to 0 to 15. |
| 170 */ |
| 171 static void |
| 172 png_do_shift(png_row_infop row_info, png_bytep row, |
| 173 png_const_color_8p bit_depth) |
| 174 { |
| 175 png_debug(1, "in png_do_shift"); |
| 176 |
| 177 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
| 178 { |
| 179 int shift_start[4], shift_dec[4]; |
| 180 int channels = 0; |
| 181 |
| 182 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) |
| 183 { |
| 184 shift_start[channels] = row_info->bit_depth - bit_depth->red; |
| 185 shift_dec[channels] = bit_depth->red; |
| 186 channels++; |
| 187 |
| 188 shift_start[channels] = row_info->bit_depth - bit_depth->green; |
| 189 shift_dec[channels] = bit_depth->green; |
| 190 channels++; |
| 191 |
| 192 shift_start[channels] = row_info->bit_depth - bit_depth->blue; |
| 193 shift_dec[channels] = bit_depth->blue; |
| 194 channels++; |
| 195 } |
| 196 |
| 197 else |
| 198 { |
| 199 shift_start[channels] = row_info->bit_depth - bit_depth->gray; |
| 200 shift_dec[channels] = bit_depth->gray; |
| 201 channels++; |
| 202 } |
| 203 |
| 204 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
| 205 { |
| 206 shift_start[channels] = row_info->bit_depth - bit_depth->alpha; |
| 207 shift_dec[channels] = bit_depth->alpha; |
| 208 channels++; |
| 209 } |
| 210 |
| 211 /* With low row depths, could only be grayscale, so one channel */ |
| 212 if (row_info->bit_depth < 8) |
| 213 { |
| 214 png_bytep bp = row; |
| 215 png_size_t i; |
| 216 unsigned int mask; |
| 217 png_size_t row_bytes = row_info->rowbytes; |
| 218 |
| 219 if (bit_depth->gray == 1 && row_info->bit_depth == 2) |
| 220 mask = 0x55; |
| 221 |
| 222 else if (row_info->bit_depth == 4 && bit_depth->gray == 3) |
| 223 mask = 0x11; |
| 224 |
| 225 else |
| 226 mask = 0xff; |
| 227 |
| 228 for (i = 0; i < row_bytes; i++, bp++) |
| 229 { |
| 230 int j; |
| 231 unsigned int v, out; |
| 232 |
| 233 v = *bp; |
| 234 out = 0; |
| 235 |
| 236 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) |
| 237 { |
| 238 if (j > 0) |
| 239 out |= v << j; |
| 240 |
| 241 else |
| 242 out |= (v >> (-j)) & mask; |
| 243 } |
| 244 |
| 245 *bp = (png_byte)(out & 0xff); |
| 246 } |
| 247 } |
| 248 |
| 249 else if (row_info->bit_depth == 8) |
| 250 { |
| 251 png_bytep bp = row; |
| 252 png_uint_32 i; |
| 253 png_uint_32 istop = channels * row_info->width; |
| 254 |
| 255 for (i = 0; i < istop; i++, bp++) |
| 256 { |
| 257 |
| 258 const unsigned int c = i%channels; |
| 259 int j; |
| 260 unsigned int v, out; |
| 261 |
| 262 v = *bp; |
| 263 out = 0; |
| 264 |
| 265 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) |
| 266 { |
| 267 if (j > 0) |
| 268 out |= v << j; |
| 269 |
| 270 else |
| 271 out |= v >> (-j); |
| 272 } |
| 273 |
| 274 *bp = (png_byte)(out & 0xff); |
| 275 } |
| 276 } |
| 277 |
| 278 else |
| 279 { |
| 280 png_bytep bp; |
| 281 png_uint_32 i; |
| 282 png_uint_32 istop = channels * row_info->width; |
| 283 |
| 284 for (bp = row, i = 0; i < istop; i++) |
| 285 { |
| 286 const unsigned int c = i%channels; |
| 287 int j; |
| 288 unsigned int value, v; |
| 289 |
| 290 v = png_get_uint_16(bp); |
| 291 value = 0; |
| 292 |
| 293 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) |
| 294 { |
| 295 if (j > 0) |
| 296 value |= v << j; |
| 297 |
| 298 else |
| 299 value |= v >> (-j); |
| 300 } |
| 301 *bp++ = (png_byte)((value >> 8) & 0xff); |
| 302 *bp++ = (png_byte)(value & 0xff); |
| 303 } |
| 304 } |
| 305 } |
| 306 } |
| 307 #endif |
| 308 |
| 309 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
| 310 static void |
| 311 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) |
| 312 { |
| 313 png_debug(1, "in png_do_write_swap_alpha"); |
| 314 |
| 315 { |
| 316 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 317 { |
| 318 if (row_info->bit_depth == 8) |
| 319 { |
| 320 /* This converts from ARGB to RGBA */ |
| 321 png_bytep sp, dp; |
| 322 png_uint_32 i; |
| 323 png_uint_32 row_width = row_info->width; |
| 324 |
| 325 for (i = 0, sp = dp = row; i < row_width; i++) |
| 326 { |
| 327 png_byte save = *(sp++); |
| 328 *(dp++) = *(sp++); |
| 329 *(dp++) = *(sp++); |
| 330 *(dp++) = *(sp++); |
| 331 *(dp++) = save; |
| 332 } |
| 333 } |
| 334 |
| 335 #ifdef PNG_WRITE_16BIT_SUPPORTED |
| 336 else |
| 337 { |
| 338 /* This converts from AARRGGBB to RRGGBBAA */ |
| 339 png_bytep sp, dp; |
| 340 png_uint_32 i; |
| 341 png_uint_32 row_width = row_info->width; |
| 342 |
| 343 for (i = 0, sp = dp = row; i < row_width; i++) |
| 344 { |
| 345 png_byte save[2]; |
| 346 save[0] = *(sp++); |
| 347 save[1] = *(sp++); |
| 348 *(dp++) = *(sp++); |
| 349 *(dp++) = *(sp++); |
| 350 *(dp++) = *(sp++); |
| 351 *(dp++) = *(sp++); |
| 352 *(dp++) = *(sp++); |
| 353 *(dp++) = *(sp++); |
| 354 *(dp++) = save[0]; |
| 355 *(dp++) = save[1]; |
| 356 } |
| 357 } |
| 358 #endif /* WRITE_16BIT */ |
| 359 } |
| 360 |
| 361 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 362 { |
| 363 if (row_info->bit_depth == 8) |
| 364 { |
| 365 /* This converts from AG to GA */ |
| 366 png_bytep sp, dp; |
| 367 png_uint_32 i; |
| 368 png_uint_32 row_width = row_info->width; |
| 369 |
| 370 for (i = 0, sp = dp = row; i < row_width; i++) |
| 371 { |
| 372 png_byte save = *(sp++); |
| 373 *(dp++) = *(sp++); |
| 374 *(dp++) = save; |
| 375 } |
| 376 } |
| 377 |
| 378 #ifdef PNG_WRITE_16BIT_SUPPORTED |
| 379 else |
| 380 { |
| 381 /* This converts from AAGG to GGAA */ |
| 382 png_bytep sp, dp; |
| 383 png_uint_32 i; |
| 384 png_uint_32 row_width = row_info->width; |
| 385 |
| 386 for (i = 0, sp = dp = row; i < row_width; i++) |
| 387 { |
| 388 png_byte save[2]; |
| 389 save[0] = *(sp++); |
| 390 save[1] = *(sp++); |
| 391 *(dp++) = *(sp++); |
| 392 *(dp++) = *(sp++); |
| 393 *(dp++) = save[0]; |
| 394 *(dp++) = save[1]; |
| 395 } |
| 396 } |
| 397 #endif /* WRITE_16BIT */ |
| 398 } |
| 399 } |
| 400 } |
| 401 #endif |
| 402 |
| 403 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
| 404 static void |
| 405 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) |
| 406 { |
| 407 png_debug(1, "in png_do_write_invert_alpha"); |
| 408 |
| 409 { |
| 410 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 411 { |
| 412 if (row_info->bit_depth == 8) |
| 413 { |
| 414 /* This inverts the alpha channel in RGBA */ |
| 415 png_bytep sp, dp; |
| 416 png_uint_32 i; |
| 417 png_uint_32 row_width = row_info->width; |
| 418 |
| 419 for (i = 0, sp = dp = row; i < row_width; i++) |
| 420 { |
| 421 /* Does nothing |
| 422 *(dp++) = *(sp++); |
| 423 *(dp++) = *(sp++); |
| 424 *(dp++) = *(sp++); |
| 425 */ |
| 426 sp+=3; dp = sp; |
| 427 *dp = (png_byte)(255 - *(sp++)); |
| 428 } |
| 429 } |
| 430 |
| 431 #ifdef PNG_WRITE_16BIT_SUPPORTED |
| 432 else |
| 433 { |
| 434 /* This inverts the alpha channel in RRGGBBAA */ |
| 435 png_bytep sp, dp; |
| 436 png_uint_32 i; |
| 437 png_uint_32 row_width = row_info->width; |
| 438 |
| 439 for (i = 0, sp = dp = row; i < row_width; i++) |
| 440 { |
| 441 /* Does nothing |
| 442 *(dp++) = *(sp++); |
| 443 *(dp++) = *(sp++); |
| 444 *(dp++) = *(sp++); |
| 445 *(dp++) = *(sp++); |
| 446 *(dp++) = *(sp++); |
| 447 *(dp++) = *(sp++); |
| 448 */ |
| 449 sp+=6; dp = sp; |
| 450 *(dp++) = (png_byte)(255 - *(sp++)); |
| 451 *dp = (png_byte)(255 - *(sp++)); |
| 452 } |
| 453 } |
| 454 #endif /* WRITE_16BIT */ |
| 455 } |
| 456 |
| 457 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 458 { |
| 459 if (row_info->bit_depth == 8) |
| 460 { |
| 461 /* This inverts the alpha channel in GA */ |
| 462 png_bytep sp, dp; |
| 463 png_uint_32 i; |
| 464 png_uint_32 row_width = row_info->width; |
| 465 |
| 466 for (i = 0, sp = dp = row; i < row_width; i++) |
| 467 { |
| 468 *(dp++) = *(sp++); |
| 469 *(dp++) = (png_byte)(255 - *(sp++)); |
| 470 } |
| 471 } |
| 472 |
| 473 #ifdef PNG_WRITE_16BIT_SUPPORTED |
| 474 else |
| 475 { |
| 476 /* This inverts the alpha channel in GGAA */ |
| 477 png_bytep sp, dp; |
| 478 png_uint_32 i; |
| 479 png_uint_32 row_width = row_info->width; |
| 480 |
| 481 for (i = 0, sp = dp = row; i < row_width; i++) |
| 482 { |
| 483 /* Does nothing |
| 484 *(dp++) = *(sp++); |
| 485 *(dp++) = *(sp++); |
| 486 */ |
| 487 sp+=2; dp = sp; |
| 488 *(dp++) = (png_byte)(255 - *(sp++)); |
| 489 *dp = (png_byte)(255 - *(sp++)); |
| 490 } |
| 491 } |
| 492 #endif /* WRITE_16BIT */ |
| 493 } |
| 494 } |
| 495 } |
| 496 #endif |
| 497 |
| 18 /* Transform the data according to the user's wishes. The order of | 498 /* Transform the data according to the user's wishes. The order of |
| 19 * transformations is significant. | 499 * transformations is significant. |
| 20 */ | 500 */ |
| 21 void /* PRIVATE */ | 501 void /* PRIVATE */ |
| 22 png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info) | 502 png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info) |
| 23 { | 503 { |
| 24 png_debug(1, "in png_do_write_transformations"); | 504 png_debug(1, "in png_do_write_transformations"); |
| 25 | 505 |
| 26 if (png_ptr == NULL) | 506 if (png_ptr == NULL) |
| 27 return; | 507 return; |
| 28 | 508 |
| 29 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED | 509 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
| 30 if (png_ptr->transformations & PNG_USER_TRANSFORM) | 510 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) |
| 31 if (png_ptr->write_user_transform_fn != NULL) | 511 if (png_ptr->write_user_transform_fn != NULL) |
| 32 (*(png_ptr->write_user_transform_fn)) /* User write transform | 512 (*(png_ptr->write_user_transform_fn)) /* User write transform |
| 33 function */ | 513 function */ |
| 34 (png_ptr, /* png_ptr */ | 514 (png_ptr, /* png_ptr */ |
| 35 row_info, /* row_info: */ | 515 row_info, /* row_info: */ |
| 36 /* png_uint_32 width; width of row */ | 516 /* png_uint_32 width; width of row */ |
| 37 /* png_size_t rowbytes; number of bytes in row */ | 517 /* png_size_t rowbytes; number of bytes in row */ |
| 38 /* png_byte color_type; color type of pixels */ | 518 /* png_byte color_type; color type of pixels */ |
| 39 /* png_byte bit_depth; bit depth of samples */ | 519 /* png_byte bit_depth; bit depth of samples */ |
| 40 /* png_byte channels; number of channels (1-4) */ | 520 /* png_byte channels; number of channels (1-4) */ |
| 41 /* png_byte pixel_depth; bits per pixel (depth*channels) */ | 521 /* png_byte pixel_depth; bits per pixel (depth*channels) */ |
| 42 png_ptr->row_buf + 1); /* start of pixel data for row */ | 522 png_ptr->row_buf + 1); /* start of pixel data for row */ |
| 43 #endif | 523 #endif |
| 44 | 524 |
| 45 #ifdef PNG_WRITE_FILLER_SUPPORTED | 525 #ifdef PNG_WRITE_FILLER_SUPPORTED |
| 46 if (png_ptr->transformations & PNG_FILLER) | 526 if ((png_ptr->transformations & PNG_FILLER) != 0) |
| 47 png_do_strip_channel(row_info, png_ptr->row_buf + 1, | 527 png_do_strip_channel(row_info, png_ptr->row_buf + 1, |
| 48 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER)); | 528 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER)); |
| 49 #endif | 529 #endif |
| 50 | 530 |
| 51 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED | 531 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED |
| 52 if (png_ptr->transformations & PNG_PACKSWAP) | 532 if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
| 53 png_do_packswap(row_info, png_ptr->row_buf + 1); | 533 png_do_packswap(row_info, png_ptr->row_buf + 1); |
| 54 #endif | 534 #endif |
| 55 | 535 |
| 56 #ifdef PNG_WRITE_PACK_SUPPORTED | 536 #ifdef PNG_WRITE_PACK_SUPPORTED |
| 57 if (png_ptr->transformations & PNG_PACK) | 537 if ((png_ptr->transformations & PNG_PACK) != 0) |
| 58 png_do_pack(row_info, png_ptr->row_buf + 1, | 538 png_do_pack(row_info, png_ptr->row_buf + 1, |
| 59 (png_uint_32)png_ptr->bit_depth); | 539 (png_uint_32)png_ptr->bit_depth); |
| 60 #endif | 540 #endif |
| 61 | 541 |
| 62 #ifdef PNG_WRITE_SWAP_SUPPORTED | 542 #ifdef PNG_WRITE_SWAP_SUPPORTED |
| 63 if (png_ptr->transformations & PNG_SWAP_BYTES) | 543 # ifdef PNG_16BIT_SUPPORTED |
| 544 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) |
| 64 png_do_swap(row_info, png_ptr->row_buf + 1); | 545 png_do_swap(row_info, png_ptr->row_buf + 1); |
| 546 # endif |
| 65 #endif | 547 #endif |
| 66 | 548 |
| 67 #ifdef PNG_WRITE_SHIFT_SUPPORTED | 549 #ifdef PNG_WRITE_SHIFT_SUPPORTED |
| 68 if (png_ptr->transformations & PNG_SHIFT) | 550 if ((png_ptr->transformations & PNG_SHIFT) != 0) |
| 69 png_do_shift(row_info, png_ptr->row_buf + 1, | 551 png_do_shift(row_info, png_ptr->row_buf + 1, |
| 70 &(png_ptr->shift)); | 552 &(png_ptr->shift)); |
| 71 #endif | 553 #endif |
| 72 | 554 |
| 73 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED | 555 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
| 74 if (png_ptr->transformations & PNG_SWAP_ALPHA) | 556 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0) |
| 75 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1); | 557 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1); |
| 76 #endif | 558 #endif |
| 77 | 559 |
| 78 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED | 560 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
| 79 if (png_ptr->transformations & PNG_INVERT_ALPHA) | 561 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) |
| 80 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1); | 562 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1); |
| 81 #endif | 563 #endif |
| 82 | 564 |
| 83 #ifdef PNG_WRITE_BGR_SUPPORTED | 565 #ifdef PNG_WRITE_BGR_SUPPORTED |
| 84 if (png_ptr->transformations & PNG_BGR) | 566 if ((png_ptr->transformations & PNG_BGR) != 0) |
| 85 png_do_bgr(row_info, png_ptr->row_buf + 1); | 567 png_do_bgr(row_info, png_ptr->row_buf + 1); |
| 86 #endif | 568 #endif |
| 87 | 569 |
| 88 #ifdef PNG_WRITE_INVERT_SUPPORTED | 570 #ifdef PNG_WRITE_INVERT_SUPPORTED |
| 89 if (png_ptr->transformations & PNG_INVERT_MONO) | 571 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) |
| 90 png_do_invert(row_info, png_ptr->row_buf + 1); | 572 png_do_invert(row_info, png_ptr->row_buf + 1); |
| 91 #endif | 573 #endif |
| 92 } | 574 } |
| 93 | 575 #endif /* WRITE_TRANSFORMS */ |
| 94 #ifdef PNG_WRITE_PACK_SUPPORTED | 576 #endif /* WRITE */ |
| 95 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The | |
| 96 * row_info bit depth should be 8 (one pixel per byte). The channels | |
| 97 * should be 1 (this only happens on grayscale and paletted images). | |
| 98 */ | |
| 99 void /* PRIVATE */ | |
| 100 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) | |
| 101 { | |
| 102 png_debug(1, "in png_do_pack"); | |
| 103 | |
| 104 if (row_info->bit_depth == 8 && | |
| 105 row_info->channels == 1) | |
| 106 { | |
| 107 switch ((int)bit_depth) | |
| 108 { | |
| 109 case 1: | |
| 110 { | |
| 111 png_bytep sp, dp; | |
| 112 int mask, v; | |
| 113 png_uint_32 i; | |
| 114 png_uint_32 row_width = row_info->width; | |
| 115 | |
| 116 sp = row; | |
| 117 dp = row; | |
| 118 mask = 0x80; | |
| 119 v = 0; | |
| 120 | |
| 121 for (i = 0; i < row_width; i++) | |
| 122 { | |
| 123 if (*sp != 0) | |
| 124 v |= mask; | |
| 125 | |
| 126 sp++; | |
| 127 | |
| 128 if (mask > 1) | |
| 129 mask >>= 1; | |
| 130 | |
| 131 else | |
| 132 { | |
| 133 mask = 0x80; | |
| 134 *dp = (png_byte)v; | |
| 135 dp++; | |
| 136 v = 0; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 if (mask != 0x80) | |
| 141 *dp = (png_byte)v; | |
| 142 | |
| 143 break; | |
| 144 } | |
| 145 | |
| 146 case 2: | |
| 147 { | |
| 148 png_bytep sp, dp; | |
| 149 int shift, v; | |
| 150 png_uint_32 i; | |
| 151 png_uint_32 row_width = row_info->width; | |
| 152 | |
| 153 sp = row; | |
| 154 dp = row; | |
| 155 shift = 6; | |
| 156 v = 0; | |
| 157 | |
| 158 for (i = 0; i < row_width; i++) | |
| 159 { | |
| 160 png_byte value; | |
| 161 | |
| 162 value = (png_byte)(*sp & 0x03); | |
| 163 v |= (value << shift); | |
| 164 | |
| 165 if (shift == 0) | |
| 166 { | |
| 167 shift = 6; | |
| 168 *dp = (png_byte)v; | |
| 169 dp++; | |
| 170 v = 0; | |
| 171 } | |
| 172 | |
| 173 else | |
| 174 shift -= 2; | |
| 175 | |
| 176 sp++; | |
| 177 } | |
| 178 | |
| 179 if (shift != 6) | |
| 180 *dp = (png_byte)v; | |
| 181 | |
| 182 break; | |
| 183 } | |
| 184 | |
| 185 case 4: | |
| 186 { | |
| 187 png_bytep sp, dp; | |
| 188 int shift, v; | |
| 189 png_uint_32 i; | |
| 190 png_uint_32 row_width = row_info->width; | |
| 191 | |
| 192 sp = row; | |
| 193 dp = row; | |
| 194 shift = 4; | |
| 195 v = 0; | |
| 196 | |
| 197 for (i = 0; i < row_width; i++) | |
| 198 { | |
| 199 png_byte value; | |
| 200 | |
| 201 value = (png_byte)(*sp & 0x0f); | |
| 202 v |= (value << shift); | |
| 203 | |
| 204 if (shift == 0) | |
| 205 { | |
| 206 shift = 4; | |
| 207 *dp = (png_byte)v; | |
| 208 dp++; | |
| 209 v = 0; | |
| 210 } | |
| 211 | |
| 212 else | |
| 213 shift -= 4; | |
| 214 | |
| 215 sp++; | |
| 216 } | |
| 217 | |
| 218 if (shift != 4) | |
| 219 *dp = (png_byte)v; | |
| 220 | |
| 221 break; | |
| 222 } | |
| 223 | |
| 224 default: | |
| 225 break; | |
| 226 } | |
| 227 | |
| 228 row_info->bit_depth = (png_byte)bit_depth; | |
| 229 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); | |
| 230 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, | |
| 231 row_info->width); | |
| 232 } | |
| 233 } | |
| 234 #endif | |
| 235 | |
| 236 #ifdef PNG_WRITE_SHIFT_SUPPORTED | |
| 237 /* Shift pixel values to take advantage of whole range. Pass the | |
| 238 * true number of bits in bit_depth. The row should be packed | |
| 239 * according to row_info->bit_depth. Thus, if you had a row of | |
| 240 * bit depth 4, but the pixels only had values from 0 to 7, you | |
| 241 * would pass 3 as bit_depth, and this routine would translate the | |
| 242 * data to 0 to 15. | |
| 243 */ | |
| 244 void /* PRIVATE */ | |
| 245 png_do_shift(png_row_infop row_info, png_bytep row, | |
| 246 png_const_color_8p bit_depth) | |
| 247 { | |
| 248 png_debug(1, "in png_do_shift"); | |
| 249 | |
| 250 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE) | |
| 251 { | |
| 252 int shift_start[4], shift_dec[4]; | |
| 253 int channels = 0; | |
| 254 | |
| 255 if (row_info->color_type & PNG_COLOR_MASK_COLOR) | |
| 256 { | |
| 257 shift_start[channels] = row_info->bit_depth - bit_depth->red; | |
| 258 shift_dec[channels] = bit_depth->red; | |
| 259 channels++; | |
| 260 | |
| 261 shift_start[channels] = row_info->bit_depth - bit_depth->green; | |
| 262 shift_dec[channels] = bit_depth->green; | |
| 263 channels++; | |
| 264 | |
| 265 shift_start[channels] = row_info->bit_depth - bit_depth->blue; | |
| 266 shift_dec[channels] = bit_depth->blue; | |
| 267 channels++; | |
| 268 } | |
| 269 | |
| 270 else | |
| 271 { | |
| 272 shift_start[channels] = row_info->bit_depth - bit_depth->gray; | |
| 273 shift_dec[channels] = bit_depth->gray; | |
| 274 channels++; | |
| 275 } | |
| 276 | |
| 277 if (row_info->color_type & PNG_COLOR_MASK_ALPHA) | |
| 278 { | |
| 279 shift_start[channels] = row_info->bit_depth - bit_depth->alpha; | |
| 280 shift_dec[channels] = bit_depth->alpha; | |
| 281 channels++; | |
| 282 } | |
| 283 | |
| 284 /* With low row depths, could only be grayscale, so one channel */ | |
| 285 if (row_info->bit_depth < 8) | |
| 286 { | |
| 287 png_bytep bp = row; | |
| 288 png_size_t i; | |
| 289 unsigned int mask; | |
| 290 png_size_t row_bytes = row_info->rowbytes; | |
| 291 | |
| 292 if (bit_depth->gray == 1 && row_info->bit_depth == 2) | |
| 293 mask = 0x55; | |
| 294 | |
| 295 else if (row_info->bit_depth == 4 && bit_depth->gray == 3) | |
| 296 mask = 0x11; | |
| 297 | |
| 298 else | |
| 299 mask = 0xff; | |
| 300 | |
| 301 for (i = 0; i < row_bytes; i++, bp++) | |
| 302 { | |
| 303 int j; | |
| 304 unsigned int v, out; | |
| 305 | |
| 306 v = *bp; | |
| 307 out = 0; | |
| 308 | |
| 309 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) | |
| 310 { | |
| 311 if (j > 0) | |
| 312 out |= v << j; | |
| 313 | |
| 314 else | |
| 315 out |= (v >> (-j)) & mask; | |
| 316 } | |
| 317 | |
| 318 *bp = (png_byte)(out & 0xff); | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 else if (row_info->bit_depth == 8) | |
| 323 { | |
| 324 png_bytep bp = row; | |
| 325 png_uint_32 i; | |
| 326 png_uint_32 istop = channels * row_info->width; | |
| 327 | |
| 328 for (i = 0; i < istop; i++, bp++) | |
| 329 { | |
| 330 | |
| 331 const unsigned int c = i%channels; | |
| 332 int j; | |
| 333 unsigned int v, out; | |
| 334 | |
| 335 v = *bp; | |
| 336 out = 0; | |
| 337 | |
| 338 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) | |
| 339 { | |
| 340 if (j > 0) | |
| 341 out |= v << j; | |
| 342 | |
| 343 else | |
| 344 out |= v >> (-j); | |
| 345 } | |
| 346 | |
| 347 *bp = (png_byte)(out & 0xff); | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 else | |
| 352 { | |
| 353 png_bytep bp; | |
| 354 png_uint_32 i; | |
| 355 png_uint_32 istop = channels * row_info->width; | |
| 356 | |
| 357 for (bp = row, i = 0; i < istop; i++) | |
| 358 { | |
| 359 const unsigned int c = i%channels; | |
| 360 int j; | |
| 361 unsigned int value, v; | |
| 362 | |
| 363 v = png_get_uint_16(bp); | |
| 364 value = 0; | |
| 365 | |
| 366 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) | |
| 367 { | |
| 368 if (j > 0) | |
| 369 value |= v << j; | |
| 370 | |
| 371 else | |
| 372 value |= v >> (-j); | |
| 373 } | |
| 374 *bp++ = (png_byte)((value >> 8) & 0xff); | |
| 375 *bp++ = (png_byte)(value & 0xff); | |
| 376 } | |
| 377 } | |
| 378 } | |
| 379 } | |
| 380 #endif | |
| 381 | |
| 382 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED | |
| 383 void /* PRIVATE */ | |
| 384 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) | |
| 385 { | |
| 386 png_debug(1, "in png_do_write_swap_alpha"); | |
| 387 | |
| 388 { | |
| 389 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | |
| 390 { | |
| 391 if (row_info->bit_depth == 8) | |
| 392 { | |
| 393 /* This converts from ARGB to RGBA */ | |
| 394 png_bytep sp, dp; | |
| 395 png_uint_32 i; | |
| 396 png_uint_32 row_width = row_info->width; | |
| 397 | |
| 398 for (i = 0, sp = dp = row; i < row_width; i++) | |
| 399 { | |
| 400 png_byte save = *(sp++); | |
| 401 *(dp++) = *(sp++); | |
| 402 *(dp++) = *(sp++); | |
| 403 *(dp++) = *(sp++); | |
| 404 *(dp++) = save; | |
| 405 } | |
| 406 } | |
| 407 | |
| 408 #ifdef PNG_WRITE_16BIT_SUPPORTED | |
| 409 else | |
| 410 { | |
| 411 /* This converts from AARRGGBB to RRGGBBAA */ | |
| 412 png_bytep sp, dp; | |
| 413 png_uint_32 i; | |
| 414 png_uint_32 row_width = row_info->width; | |
| 415 | |
| 416 for (i = 0, sp = dp = row; i < row_width; i++) | |
| 417 { | |
| 418 png_byte save[2]; | |
| 419 save[0] = *(sp++); | |
| 420 save[1] = *(sp++); | |
| 421 *(dp++) = *(sp++); | |
| 422 *(dp++) = *(sp++); | |
| 423 *(dp++) = *(sp++); | |
| 424 *(dp++) = *(sp++); | |
| 425 *(dp++) = *(sp++); | |
| 426 *(dp++) = *(sp++); | |
| 427 *(dp++) = save[0]; | |
| 428 *(dp++) = save[1]; | |
| 429 } | |
| 430 } | |
| 431 #endif /* PNG_WRITE_16BIT_SUPPORTED */ | |
| 432 } | |
| 433 | |
| 434 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | |
| 435 { | |
| 436 if (row_info->bit_depth == 8) | |
| 437 { | |
| 438 /* This converts from AG to GA */ | |
| 439 png_bytep sp, dp; | |
| 440 png_uint_32 i; | |
| 441 png_uint_32 row_width = row_info->width; | |
| 442 | |
| 443 for (i = 0, sp = dp = row; i < row_width; i++) | |
| 444 { | |
| 445 png_byte save = *(sp++); | |
| 446 *(dp++) = *(sp++); | |
| 447 *(dp++) = save; | |
| 448 } | |
| 449 } | |
| 450 | |
| 451 #ifdef PNG_WRITE_16BIT_SUPPORTED | |
| 452 else | |
| 453 { | |
| 454 /* This converts from AAGG to GGAA */ | |
| 455 png_bytep sp, dp; | |
| 456 png_uint_32 i; | |
| 457 png_uint_32 row_width = row_info->width; | |
| 458 | |
| 459 for (i = 0, sp = dp = row; i < row_width; i++) | |
| 460 { | |
| 461 png_byte save[2]; | |
| 462 save[0] = *(sp++); | |
| 463 save[1] = *(sp++); | |
| 464 *(dp++) = *(sp++); | |
| 465 *(dp++) = *(sp++); | |
| 466 *(dp++) = save[0]; | |
| 467 *(dp++) = save[1]; | |
| 468 } | |
| 469 } | |
| 470 #endif /* PNG_WRITE_16BIT_SUPPORTED */ | |
| 471 } | |
| 472 } | |
| 473 } | |
| 474 #endif | |
| 475 | |
| 476 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED | |
| 477 void /* PRIVATE */ | |
| 478 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) | |
| 479 { | |
| 480 png_debug(1, "in png_do_write_invert_alpha"); | |
| 481 | |
| 482 { | |
| 483 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | |
| 484 { | |
| 485 if (row_info->bit_depth == 8) | |
| 486 { | |
| 487 /* This inverts the alpha channel in RGBA */ | |
| 488 png_bytep sp, dp; | |
| 489 png_uint_32 i; | |
| 490 png_uint_32 row_width = row_info->width; | |
| 491 | |
| 492 for (i = 0, sp = dp = row; i < row_width; i++) | |
| 493 { | |
| 494 /* Does nothing | |
| 495 *(dp++) = *(sp++); | |
| 496 *(dp++) = *(sp++); | |
| 497 *(dp++) = *(sp++); | |
| 498 */ | |
| 499 sp+=3; dp = sp; | |
| 500 *(dp++) = (png_byte)(255 - *(sp++)); | |
| 501 } | |
| 502 } | |
| 503 | |
| 504 #ifdef PNG_WRITE_16BIT_SUPPORTED | |
| 505 else | |
| 506 { | |
| 507 /* This inverts the alpha channel in RRGGBBAA */ | |
| 508 png_bytep sp, dp; | |
| 509 png_uint_32 i; | |
| 510 png_uint_32 row_width = row_info->width; | |
| 511 | |
| 512 for (i = 0, sp = dp = row; i < row_width; i++) | |
| 513 { | |
| 514 /* Does nothing | |
| 515 *(dp++) = *(sp++); | |
| 516 *(dp++) = *(sp++); | |
| 517 *(dp++) = *(sp++); | |
| 518 *(dp++) = *(sp++); | |
| 519 *(dp++) = *(sp++); | |
| 520 *(dp++) = *(sp++); | |
| 521 */ | |
| 522 sp+=6; dp = sp; | |
| 523 *(dp++) = (png_byte)(255 - *(sp++)); | |
| 524 *(dp++) = (png_byte)(255 - *(sp++)); | |
| 525 } | |
| 526 } | |
| 527 #endif /* PNG_WRITE_16BIT_SUPPORTED */ | |
| 528 } | |
| 529 | |
| 530 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | |
| 531 { | |
| 532 if (row_info->bit_depth == 8) | |
| 533 { | |
| 534 /* This inverts the alpha channel in GA */ | |
| 535 png_bytep sp, dp; | |
| 536 png_uint_32 i; | |
| 537 png_uint_32 row_width = row_info->width; | |
| 538 | |
| 539 for (i = 0, sp = dp = row; i < row_width; i++) | |
| 540 { | |
| 541 *(dp++) = *(sp++); | |
| 542 *(dp++) = (png_byte)(255 - *(sp++)); | |
| 543 } | |
| 544 } | |
| 545 | |
| 546 #ifdef PNG_WRITE_16BIT_SUPPORTED | |
| 547 else | |
| 548 { | |
| 549 /* This inverts the alpha channel in GGAA */ | |
| 550 png_bytep sp, dp; | |
| 551 png_uint_32 i; | |
| 552 png_uint_32 row_width = row_info->width; | |
| 553 | |
| 554 for (i = 0, sp = dp = row; i < row_width; i++) | |
| 555 { | |
| 556 /* Does nothing | |
| 557 *(dp++) = *(sp++); | |
| 558 *(dp++) = *(sp++); | |
| 559 */ | |
| 560 sp+=2; dp = sp; | |
| 561 *(dp++) = (png_byte)(255 - *(sp++)); | |
| 562 *(dp++) = (png_byte)(255 - *(sp++)); | |
| 563 } | |
| 564 } | |
| 565 #endif /* PNG_WRITE_16BIT_SUPPORTED */ | |
| 566 } | |
| 567 } | |
| 568 } | |
| 569 #endif | |
| 570 #endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */ | |
| 571 | |
| 572 #ifdef PNG_MNG_FEATURES_SUPPORTED | |
| 573 /* Undoes intrapixel differencing */ | |
| 574 void /* PRIVATE */ | |
| 575 png_do_write_intrapixel(png_row_infop row_info, png_bytep row) | |
| 576 { | |
| 577 png_debug(1, "in png_do_write_intrapixel"); | |
| 578 | |
| 579 if ((row_info->color_type & PNG_COLOR_MASK_COLOR)) | |
| 580 { | |
| 581 int bytes_per_pixel; | |
| 582 png_uint_32 row_width = row_info->width; | |
| 583 if (row_info->bit_depth == 8) | |
| 584 { | |
| 585 png_bytep rp; | |
| 586 png_uint_32 i; | |
| 587 | |
| 588 if (row_info->color_type == PNG_COLOR_TYPE_RGB) | |
| 589 bytes_per_pixel = 3; | |
| 590 | |
| 591 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | |
| 592 bytes_per_pixel = 4; | |
| 593 | |
| 594 else | |
| 595 return; | |
| 596 | |
| 597 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) | |
| 598 { | |
| 599 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff); | |
| 600 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff); | |
| 601 } | |
| 602 } | |
| 603 | |
| 604 #ifdef PNG_WRITE_16BIT_SUPPORTED | |
| 605 else if (row_info->bit_depth == 16) | |
| 606 { | |
| 607 png_bytep rp; | |
| 608 png_uint_32 i; | |
| 609 | |
| 610 if (row_info->color_type == PNG_COLOR_TYPE_RGB) | |
| 611 bytes_per_pixel = 6; | |
| 612 | |
| 613 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | |
| 614 bytes_per_pixel = 8; | |
| 615 | |
| 616 else | |
| 617 return; | |
| 618 | |
| 619 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) | |
| 620 { | |
| 621 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); | |
| 622 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); | |
| 623 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); | |
| 624 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); | |
| 625 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); | |
| 626 *(rp ) = (png_byte)((red >> 8) & 0xff); | |
| 627 *(rp + 1) = (png_byte)(red & 0xff); | |
| 628 *(rp + 4) = (png_byte)((blue >> 8) & 0xff); | |
| 629 *(rp + 5) = (png_byte)(blue & 0xff); | |
| 630 } | |
| 631 } | |
| 632 #endif /* PNG_WRITE_16BIT_SUPPORTED */ | |
| 633 } | |
| 634 } | |
| 635 #endif /* PNG_MNG_FEATURES_SUPPORTED */ | |
| 636 #endif /* PNG_WRITE_SUPPORTED */ | |
| OLD | NEW |