Ticket #36053: patch-src-hpdf_image_png.c.diff
File patch-src-hpdf_image_png.c.diff, 13.0 KB (added by jwhowse4, 12 years ago) |
---|
-
src/hpdf_image_png.c
old new ReadPngData_Interlaced (HPDF_Dict image, 109 109 png_infop info_ptr) 110 110 { 111 111 png_uint_32 len = png_get_rowbytes(png_ptr, info_ptr); 112 png_uint_32 height = png_get_image_height(png_ptr, info_ptr); 112 113 png_bytep* row_pointers = HPDF_GetMem (image->mmgr, 113 info_ptr->height * sizeof (png_bytep));114 height * sizeof (png_bytep)); 114 115 115 116 if (row_pointers) { 116 117 HPDF_UINT i; 117 118 118 HPDF_MemSet (row_pointers, 0, info_ptr->height * sizeof (png_bytep));119 for (i = 0; i < (HPDF_UINT) info_ptr->height; i++) {119 HPDF_MemSet (row_pointers, 0, height * sizeof (png_bytep)); 120 for (i = 0; i < (HPDF_UINT)height; i++) { 120 121 row_pointers[i] = HPDF_GetMem (image->mmgr, len); 121 122 122 123 if (image->error->error_no != HPDF_OK) … … ReadPngData_Interlaced (HPDF_Dict image, 126 127 if (image->error->error_no == HPDF_OK) { 127 128 png_read_image(png_ptr, row_pointers); 128 129 if (image->error->error_no == HPDF_OK) { /* add this line */ 129 for (i = 0; i < (HPDF_UINT) info_ptr->height; i++) {130 for (i = 0; i < (HPDF_UINT)height; i++) { 130 131 if (HPDF_Stream_Write (image->stream, row_pointers[i], len) != 131 132 HPDF_OK) 132 133 break; … … ReadPngData_Interlaced (HPDF_Dict image, 135 136 } 136 137 137 138 /* clean up */ 138 for (i = 0; i < (HPDF_UINT) info_ptr->height; i++) {139 for (i = 0; i < (HPDF_UINT)height; i++) { 139 140 HPDF_FreeMem (image->mmgr, row_pointers[i]); 140 141 } 141 142 … … ReadPngData (HPDF_Dict image, 151 152 png_infop info_ptr) 152 153 { 153 154 png_uint_32 len = png_get_rowbytes(png_ptr, info_ptr); 155 png_uint_32 height = png_get_image_height(png_ptr, info_ptr); 154 156 png_bytep buf_ptr = HPDF_GetMem (image->mmgr, len); 155 157 156 158 if (buf_ptr) { 157 159 HPDF_UINT i; 158 160 159 for (i = 0; i < (HPDF_UINT) info_ptr->height; i++) {161 for (i = 0; i < (HPDF_UINT)height; i++) { 160 162 png_read_rows(png_ptr, (png_byte**)&buf_ptr, NULL, 1); 161 163 if (image->error->error_no != HPDF_OK) 162 164 break; … … ReadTransparentPaletteData (HPDF_Dict image, 182 184 HPDF_STATUS ret = HPDF_OK; 183 185 HPDF_UINT i, j; 184 186 png_bytep *row_ptr; 187 png_uint_32 height = png_get_image_height(png_ptr, info_ptr); 188 png_uint_32 width = png_get_image_width(png_ptr, info_ptr); 185 189 186 row_ptr = HPDF_GetMem (image->mmgr, info_ptr->height * sizeof(png_bytep));190 row_ptr = HPDF_GetMem (image->mmgr, height * sizeof(png_bytep)); 187 191 if (!row_ptr) { 188 192 return HPDF_FAILD_TO_ALLOC_MEM; 189 193 } else { 190 194 png_uint_32 len = png_get_rowbytes(png_ptr, info_ptr); 191 195 192 for (i = 0; i < (HPDF_UINT) info_ptr->height; i++) {196 for (i = 0; i < (HPDF_UINT)height; i++) { 193 197 row_ptr[i] = HPDF_GetMem(image->mmgr, len); 194 198 if (!row_ptr[i]) { 195 for (; i > =0; i--) {199 for (; i > 0; i--) { 196 200 HPDF_FreeMem (image->mmgr, row_ptr[i]); 197 201 } 198 202 HPDF_FreeMem (image->mmgr, row_ptr); … … ReadTransparentPaletteData (HPDF_Dict image, 207 211 goto Error; 208 212 } 209 213 210 for (j = 0; j < info_ptr->height; j++) {211 for (i = 0; i < info_ptr->width; i++) {212 smask_data[ info_ptr->width * j + i] = (row_ptr[j][i] < num_trans) ? trans[row_ptr[j][i]] : 0xFF;214 for (j = 0; j < height; j++) { 215 for (i = 0; i < width; i++) { 216 smask_data[width * j + i] = (row_ptr[j][i] < num_trans) ? trans[row_ptr[j][i]] : 0xFF; 213 217 } 214 218 215 if (HPDF_Stream_Write (image->stream, row_ptr[j], info_ptr->width) != HPDF_OK) {219 if (HPDF_Stream_Write (image->stream, row_ptr[j], width) != HPDF_OK) { 216 220 ret = HPDF_FILE_IO_ERROR; 217 221 goto Error; 218 222 } 219 223 } 220 224 221 225 Error: 222 for (i = 0; i < (HPDF_UINT) info_ptr->height; i++) {226 for (i = 0; i < (HPDF_UINT)height; i++) { 223 227 HPDF_FreeMem (image->mmgr, row_ptr[i]); 224 228 } 225 229 … … ReadTransparentPngData (HPDF_Dict image, 238 242 HPDF_UINT i, j; 239 243 png_bytep *row_ptr, row; 240 244 png_byte color_type; 245 png_uint_32 height = png_get_image_height(png_ptr, info_ptr); 246 png_uint_32 width = png_get_image_width(png_ptr, info_ptr); 241 247 242 248 color_type = png_get_color_type(png_ptr, info_ptr); 243 249 … … ReadTransparentPngData (HPDF_Dict image, 245 251 return HPDF_INVALID_PNG_IMAGE; 246 252 } 247 253 248 row_ptr = HPDF_GetMem (image->mmgr, info_ptr->height * sizeof(png_bytep));254 row_ptr = HPDF_GetMem (image->mmgr, height * sizeof(png_bytep)); 249 255 if (!row_ptr) { 250 256 return HPDF_FAILD_TO_ALLOC_MEM; 251 257 } else { 252 258 png_uint_32 len = png_get_rowbytes(png_ptr, info_ptr); 253 259 254 for (i = 0; i < (HPDF_UINT) info_ptr->height; i++) {260 for (i = 0; i < (HPDF_UINT)height; i++) { 255 261 row_ptr[i] = HPDF_GetMem(image->mmgr, len); 256 262 if (!row_ptr[i]) { 257 for (; i > =0; i--) {263 for (; i > 0; i--) { 258 264 HPDF_FreeMem (image->mmgr, row_ptr[i]); 259 265 } 260 266 HPDF_FreeMem (image->mmgr, row_ptr); … … ReadTransparentPngData (HPDF_Dict image, 271 277 272 278 switch (color_type) { 273 279 case PNG_COLOR_TYPE_RGB_ALPHA: 274 row_len = 3 * info_ptr->width * sizeof(png_byte);275 for (j = 0; j < info_ptr->height; j++) {276 for (i = 0; i < info_ptr->width; i++) {280 row_len = 3 * width * sizeof(png_byte); 281 for (j = 0; j < height; j++) { 282 for (i = 0; i < width; i++) { 277 283 row = row_ptr[j]; 278 284 memmove(row + (3 * i), row + (4*i), 3); 279 smask_data[ info_ptr->width * j + i] = row[4 * i + 3];285 smask_data[width * j + i] = row[4 * i + 3]; 280 286 } 281 287 282 288 if (HPDF_Stream_Write (image->stream, row, row_len) != HPDF_OK) { … … ReadTransparentPngData (HPDF_Dict image, 286 292 } 287 293 break; 288 294 case PNG_COLOR_TYPE_GRAY_ALPHA: 289 row_len = info_ptr->width * sizeof(png_byte);290 for (j = 0; j < info_ptr->height; j++) {291 for (i = 0; i < info_ptr->width; i++) {295 row_len = width * sizeof(png_byte); 296 for (j = 0; j < height; j++) { 297 for (i = 0; i < width; i++) { 292 298 row = row_ptr[j]; 293 299 row[i] = row[2 * i]; 294 smask_data[ info_ptr->width * j + i] = row[2 * i + 1];300 smask_data[width * j + i] = row[2 * i + 1]; 295 301 } 296 302 297 303 if (HPDF_Stream_Write (image->stream, row, row_len) != HPDF_OK) { … … ReadTransparentPngData (HPDF_Dict image, 306 312 } 307 313 308 314 Error: 309 for (i = 0; i < (HPDF_UINT) info_ptr->height; i++) {315 for (i = 0; i < (HPDF_UINT)height; i++) { 310 316 HPDF_FreeMem (image->mmgr, row_ptr[i]); 311 317 } 312 318 … … LoadPngData (HPDF_Dict image, 415 421 416 422 { 417 423 HPDF_STATUS ret = HPDF_OK; 418 424 png_uint_32 width, height; 425 int bit_depth, color_type; 419 426 png_structp png_ptr = NULL; 420 427 png_infop info_ptr = NULL; 421 428 … … LoadPngData (HPDF_Dict image, 447 454 goto Exit; 448 455 } 449 456 457 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); 458 450 459 /* 16bit images are not supported. */ 451 if ( info_ptr->bit_depth == 16) {460 if (bit_depth == 16) { 452 461 png_set_strip_16(png_ptr); 453 462 } 454 463 … … LoadPngData (HPDF_Dict image, 458 467 } 459 468 460 469 /* check palette-based images for transparent areas and load them immediately if found */ 461 if (xref && PNG_COLOR_TYPE_PALETTE & info_ptr->color_type) {470 if (xref && PNG_COLOR_TYPE_PALETTE & color_type) { 462 471 png_bytep trans; 463 472 int num_trans; 464 473 HPDF_Dict smask; … … LoadPngData (HPDF_Dict image, 478 487 smask->header.obj_class |= HPDF_OSUBCLASS_XOBJECT; 479 488 ret = HPDF_Dict_AddName (smask, "Type", "XObject"); 480 489 ret += HPDF_Dict_AddName (smask, "Subtype", "Image"); 481 ret += HPDF_Dict_AddNumber (smask, "Width", (HPDF_UINT) info_ptr->width);482 ret += HPDF_Dict_AddNumber (smask, "Height", (HPDF_UINT) info_ptr->height);490 ret += HPDF_Dict_AddNumber (smask, "Width", (HPDF_UINT)width); 491 ret += HPDF_Dict_AddNumber (smask, "Height", (HPDF_UINT)height); 483 492 ret += HPDF_Dict_AddName (smask, "ColorSpace", "DeviceGray"); 484 ret += HPDF_Dict_AddNumber (smask, "BitsPerComponent", (HPDF_UINT) info_ptr->bit_depth);493 ret += HPDF_Dict_AddNumber (smask, "BitsPerComponent", (HPDF_UINT)bit_depth); 485 494 486 495 if (ret != HPDF_OK) { 487 496 HPDF_Dict_Free(smask); … … LoadPngData (HPDF_Dict image, 489 498 goto Exit; 490 499 } 491 500 492 smask_data = HPDF_GetMem(image->mmgr, info_ptr->width * info_ptr->height);501 smask_data = HPDF_GetMem(image->mmgr, width * height); 493 502 if (!smask_data) { 494 503 HPDF_Dict_Free(smask); 495 504 ret = HPDF_FAILD_TO_ALLOC_MEM; … … LoadPngData (HPDF_Dict image, 503 512 goto Exit; 504 513 } 505 514 506 if (HPDF_Stream_Write(smask->stream, smask_data, info_ptr->width * info_ptr->height) != HPDF_OK) {515 if (HPDF_Stream_Write(smask->stream, smask_data, width * height) != HPDF_OK) { 507 516 HPDF_FreeMem(image->mmgr, smask_data); 508 517 HPDF_Dict_Free(smask); 509 518 ret = HPDF_FILE_IO_ERROR; … … LoadPngData (HPDF_Dict image, 513 522 514 523 515 524 ret += CreatePallet(image, png_ptr, info_ptr); 516 ret += HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT) info_ptr->width);517 ret += HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT) info_ptr->height);518 ret += HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT) info_ptr->bit_depth);525 ret += HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width); 526 ret += HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height); 527 ret += HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT)bit_depth); 519 528 ret += HPDF_Dict_Add (image, "SMask", smask); 520 529 521 530 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); … … no_transparent_color_in_palette: 526 535 527 536 /* read images with alpha channel right away 528 537 we have to do this because image transparent mask must be added to the Xref */ 529 if (xref && PNG_COLOR_MASK_ALPHA & info_ptr->color_type) {538 if (xref && PNG_COLOR_MASK_ALPHA & color_type) { 530 539 HPDF_Dict smask; 531 540 png_bytep smask_data; 532 541 … … no_transparent_color_in_palette: 539 548 smask->header.obj_class |= HPDF_OSUBCLASS_XOBJECT; 540 549 ret = HPDF_Dict_AddName (smask, "Type", "XObject"); 541 550 ret += HPDF_Dict_AddName (smask, "Subtype", "Image"); 542 ret += HPDF_Dict_AddNumber (smask, "Width", (HPDF_UINT) info_ptr->width);543 ret += HPDF_Dict_AddNumber (smask, "Height", (HPDF_UINT) info_ptr->height);551 ret += HPDF_Dict_AddNumber (smask, "Width", (HPDF_UINT)width); 552 ret += HPDF_Dict_AddNumber (smask, "Height", (HPDF_UINT)height); 544 553 ret += HPDF_Dict_AddName (smask, "ColorSpace", "DeviceGray"); 545 ret += HPDF_Dict_AddNumber (smask, "BitsPerComponent", (HPDF_UINT) info_ptr->bit_depth);554 ret += HPDF_Dict_AddNumber (smask, "BitsPerComponent", (HPDF_UINT)bit_depth); 546 555 547 556 if (ret != HPDF_OK) { 548 557 HPDF_Dict_Free(smask); … … no_transparent_color_in_palette: 550 559 goto Exit; 551 560 } 552 561 553 smask_data = HPDF_GetMem(image->mmgr, info_ptr->width * info_ptr->height);562 smask_data = HPDF_GetMem(image->mmgr, width * height); 554 563 if (!smask_data) { 555 564 HPDF_Dict_Free(smask); 556 565 ret = HPDF_FAILD_TO_ALLOC_MEM; … … no_transparent_color_in_palette: 564 573 goto Exit; 565 574 } 566 575 567 if (HPDF_Stream_Write(smask->stream, smask_data, info_ptr->width * info_ptr->height) != HPDF_OK) {576 if (HPDF_Stream_Write(smask->stream, smask_data, width * height) != HPDF_OK) { 568 577 HPDF_FreeMem(image->mmgr, smask_data); 569 578 HPDF_Dict_Free(smask); 570 579 ret = HPDF_FILE_IO_ERROR; … … no_transparent_color_in_palette: 572 581 } 573 582 HPDF_FreeMem(image->mmgr, smask_data); 574 583 575 if ( info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {584 if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { 576 585 ret += HPDF_Dict_AddName (image, "ColorSpace", "DeviceGray"); 577 586 } else { 578 587 ret += HPDF_Dict_AddName (image, "ColorSpace", "DeviceRGB"); 579 588 } 580 ret += HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT) info_ptr->width);581 ret += HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT) info_ptr->height);582 ret += HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT) info_ptr->bit_depth);589 ret += HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width); 590 ret += HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height); 591 ret += HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT)bit_depth); 583 592 ret += HPDF_Dict_Add (image, "SMask", smask); 584 593 585 594 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); … … no_transparent_color_in_palette: 589 598 /* if the image has color palette, copy the pallet of the image to 590 599 * create color map. 591 600 */ 592 if ( info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)601 if (color_type == PNG_COLOR_TYPE_PALETTE) 593 602 ret = CreatePallet(image, png_ptr, info_ptr); 594 else if ( info_ptr->color_type == PNG_COLOR_TYPE_GRAY)603 else if (color_type == PNG_COLOR_TYPE_GRAY) 595 604 ret = HPDF_Dict_AddName (image, "ColorSpace", "DeviceGray"); 596 605 else 597 606 ret = HPDF_Dict_AddName (image, "ColorSpace", "DeviceRGB"); … … no_transparent_color_in_palette: 617 626 } 618 627 619 628 /* setting the info of the image. */ 620 if (HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT) info_ptr->width)629 if (HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width) 621 630 != HPDF_OK) 622 631 goto Exit; 623 632 624 if (HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT) info_ptr->height)633 if (HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height) 625 634 != HPDF_OK) 626 635 goto Exit; 627 636 628 637 if (HPDF_Dict_AddNumber (image, "BitsPerComponent", 629 (HPDF_UINT) info_ptr->bit_depth) != HPDF_OK)638 (HPDF_UINT)bit_depth) != HPDF_OK) 630 639 goto Exit; 631 640 632 641 /* clean up */