| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | $NetBSD: patch-utils_TkCximage_src_CxImage_ximapng.cpp,v 1.2 2011/08/01 00:43:58 dholland Exp $ | | 1 | $NetBSD: patch-utils_TkCximage_src_CxImage_ximapng.cpp,v 1.3 2011/08/01 01:16:50 dholland Exp $ |
2 | | | 2 | |
3 | Fix build with png-1.5. | | 3 | Fix build with png-1.5. |
4 | | | 4 | |
5 | --- utils/TkCximage/src/CxImage/ximapng.cpp.orig 2010-01-29 09:09:27.000000000 +0000 | | 5 | --- utils/TkCximage/src/CxImage/ximapng.cpp.orig 2010-01-29 09:09:27.000000000 +0000 |
6 | +++ utils/TkCximage/src/CxImage/ximapng.cpp | | 6 | +++ utils/TkCximage/src/CxImage/ximapng.cpp |
7 | @@ -15,7 +15,7 @@ | | 7 | @@ -15,7 +15,7 @@ |
8 | void CxImagePNG::ima_png_error(png_struct *png_ptr, char *message) | | 8 | void CxImagePNG::ima_png_error(png_struct *png_ptr, char *message) |
9 | { | | 9 | { |
10 | strcpy(info.szLastError,message); | | 10 | strcpy(info.szLastError,message); |
11 | - longjmp(png_ptr->jmpbuf, 1); | | 11 | - longjmp(png_ptr->jmpbuf, 1); |
12 | + longjmp(png_jmpbuf(png_ptr), 1); | | 12 | + longjmp(png_jmpbuf(png_ptr), 1); |
13 | } | | 13 | } |
14 | //////////////////////////////////////////////////////////////////////////////// | | 14 | //////////////////////////////////////////////////////////////////////////////// |
| @@ -258,36 +258,38 @@ Fix build with png-1.5. | | | @@ -258,36 +258,38 @@ Fix build with png-1.5. |
258 | //go on | | 258 | //go on |
259 | @@ -340,6 +341,11 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 259 | @@ -340,6 +341,11 @@ bool CxImagePNG::Encode(CxFile *hFile) |
260 | BYTE trans[256]; //for transparency (don't move) | | 260 | BYTE trans[256]; //for transparency (don't move) |
261 | png_struct *png_ptr; | | 261 | png_struct *png_ptr; |
262 | png_info *info_ptr; | | 262 | png_info *info_ptr; |
263 | + png_uint_32 width, height; | | 263 | + png_uint_32 width, height; |
264 | + png_byte bit_depth; | | 264 | + png_byte bit_depth; |
265 | + png_byte color_type; | | 265 | + png_byte color_type; |
266 | + png_byte interlace_type; | | 266 | + png_byte interlace_type; |
267 | + BYTE channels; | | 267 | + BYTE channels; |
268 | | | 268 | |
269 | cx_try | | 269 | cx_try |
270 | { | | 270 | { |
271 | @@ -362,7 +368,7 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 271 | @@ -362,9 +368,8 @@ bool CxImagePNG::Encode(CxFile *hFile) |
272 | /* Set error handling. REQUIRED if you aren't supplying your own | | 272 | /* Set error handling. REQUIRED if you aren't supplying your own |
273 | * error hadnling functions in the png_create_write_struct() call. | | 273 | * error hadnling functions in the png_create_write_struct() call. |
274 | */ | | 274 | */ |
275 | - if (setjmp(png_ptr->jmpbuf)){ | | 275 | - if (setjmp(png_ptr->jmpbuf)){ |
276 | + if (setjmp(png_jmpbuf(png_ptr))){ | | 276 | + if (setjmp(png_jmpbuf(png_ptr))){ |
277 | /* If we get here, we had a problem reading the file */ | | 277 | /* If we get here, we had a problem reading the file */ |
278 | if (info_ptr->palette) free(info_ptr->palette); | | 278 | - if (info_ptr->palette) free(info_ptr->palette); |
279 | png_destroy_write_struct(&png_ptr, (png_infopp)&info_ptr); | | 279 | png_destroy_write_struct(&png_ptr, (png_infopp)&info_ptr); |
280 | @@ -376,20 +382,18 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 280 | cx_throw("Error saving PNG file"); |
| | | 281 | } |
| | | 282 | @@ -376,20 +381,18 @@ bool CxImagePNG::Encode(CxFile *hFile) |
281 | png_set_write_fn(png_ptr,hFile,/*(png_rw_ptr)*/user_write_data,/*(png_flush_ptr)*/user_flush_data); | | 283 | png_set_write_fn(png_ptr,hFile,/*(png_rw_ptr)*/user_write_data,/*(png_flush_ptr)*/user_flush_data); |
282 | | | 284 | |
283 | /* set the file information here */ | | 285 | /* set the file information here */ |
284 | - info_ptr->width = GetWidth(); | | 286 | - info_ptr->width = GetWidth(); |
285 | - info_ptr->height = GetHeight(); | | 287 | - info_ptr->height = GetHeight(); |
286 | - info_ptr->pixel_depth = (BYTE)GetBpp(); | | 288 | - info_ptr->pixel_depth = (BYTE)GetBpp(); |
287 | - info_ptr->channels = (GetBpp()>8) ? (BYTE)3: (BYTE)1; | | 289 | - info_ptr->channels = (GetBpp()>8) ? (BYTE)3: (BYTE)1; |
288 | - info_ptr->bit_depth = (BYTE)(GetBpp()/info_ptr->channels); | | 290 | - info_ptr->bit_depth = (BYTE)(GetBpp()/info_ptr->channels); |
289 | - info_ptr->compression_type = info_ptr->filter_type = 0; | | 291 | - info_ptr->compression_type = info_ptr->filter_type = 0; |
290 | - info_ptr->valid = 0; | | 292 | - info_ptr->valid = 0; |
291 | + width = GetWidth(); | | 293 | + width = GetWidth(); |
292 | + height = GetHeight(); | | 294 | + height = GetHeight(); |
293 | + //pixel_depth = (BYTE)GetBpp(); | | 295 | + //pixel_depth = (BYTE)GetBpp(); |
| @@ -295,95 +297,166 @@ Fix build with png-1.5. | | | @@ -295,95 +297,166 @@ Fix build with png-1.5. |
295 | + bit_depth = (BYTE)(GetBpp()/channels); | | 297 | + bit_depth = (BYTE)(GetBpp()/channels); |
296 | | | 298 | |
297 | switch(GetCodecOption(CXIMAGE_FORMAT_PNG)){ | | 299 | switch(GetCodecOption(CXIMAGE_FORMAT_PNG)){ |
298 | case 1: | | 300 | case 1: |
299 | - info_ptr->interlace_type = PNG_INTERLACE_ADAM7; | | 301 | - info_ptr->interlace_type = PNG_INTERLACE_ADAM7; |
300 | + interlace_type = PNG_INTERLACE_ADAM7; | | 302 | + interlace_type = PNG_INTERLACE_ADAM7; |
301 | break; | | 303 | break; |
302 | default: | | 304 | default: |
303 | - info_ptr->interlace_type = PNG_INTERLACE_NONE; | | 305 | - info_ptr->interlace_type = PNG_INTERLACE_NONE; |
304 | + interlace_type = PNG_INTERLACE_NONE; | | 306 | + interlace_type = PNG_INTERLACE_NONE; |
305 | } | | 307 | } |
306 | | | 308 | |
307 | /* set compression level */ | | 309 | /* set compression level */ |
308 | @@ -399,19 +403,19 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 310 | @@ -399,19 +402,19 @@ bool CxImagePNG::Encode(CxFile *hFile) |
309 | | | 311 | |
310 | if (GetNumColors()){ | | 312 | if (GetNumColors()){ |
311 | if (bGrayScale){ | | 313 | if (bGrayScale){ |
312 | - info_ptr->color_type = PNG_COLOR_TYPE_GRAY; | | 314 | - info_ptr->color_type = PNG_COLOR_TYPE_GRAY; |
313 | + color_type = PNG_COLOR_TYPE_GRAY; | | 315 | + color_type = PNG_COLOR_TYPE_GRAY; |
314 | } else { | | 316 | } else { |
315 | - info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; | | 317 | - info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; |
316 | + color_type = PNG_COLOR_TYPE_PALETTE; | | 318 | + color_type = PNG_COLOR_TYPE_PALETTE; |
317 | } | | 319 | } |
318 | } else { | | 320 | } else { |
319 | - info_ptr->color_type = PNG_COLOR_TYPE_RGB; | | 321 | - info_ptr->color_type = PNG_COLOR_TYPE_RGB; |
320 | + color_type = PNG_COLOR_TYPE_RGB; | | 322 | + color_type = PNG_COLOR_TYPE_RGB; |
321 | } | | 323 | } |
322 | #if CXIMAGE_SUPPORT_ALPHA | | 324 | #if CXIMAGE_SUPPORT_ALPHA |
323 | if (AlphaIsValid()){ | | 325 | if (AlphaIsValid()){ |
324 | - info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; | | 326 | - info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
325 | - info_ptr->channels++; | | 327 | - info_ptr->channels++; |
326 | - info_ptr->bit_depth = 8; | | 328 | - info_ptr->bit_depth = 8; |
327 | - info_ptr->pixel_depth += 8; | | 329 | - info_ptr->pixel_depth += 8; |
328 | + color_type |= PNG_COLOR_MASK_ALPHA; | | 330 | + color_type |= PNG_COLOR_MASK_ALPHA; |
329 | + channels++; | | 331 | + channels++; |
330 | + bit_depth = 8; | | 332 | + bit_depth = 8; |
331 | + //pixel_depth += 8; | | 333 | + //pixel_depth += 8; |
332 | } | | 334 | } |
333 | #endif | | 335 | #endif |
334 | | | 336 | |
335 | @@ -428,8 +432,8 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 337 | @@ -428,29 +431,21 @@ bool CxImagePNG::Encode(CxFile *hFile) |
336 | /* set metrics */ | | 338 | /* set metrics */ |
337 | png_set_pHYs(png_ptr, info_ptr, head.biXPelsPerMeter, head.biYPelsPerMeter, PNG_RESOLUTION_METER); | | 339 | png_set_pHYs(png_ptr, info_ptr, head.biXPelsPerMeter, head.biYPelsPerMeter, PNG_RESOLUTION_METER); |
338 | | | 340 | |
339 | - png_set_IHDR(png_ptr, info_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth, | | 341 | - png_set_IHDR(png_ptr, info_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth, |
340 | - info_ptr->color_type, info_ptr->interlace_type, | | 342 | - info_ptr->color_type, info_ptr->interlace_type, |
341 | + png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, | | 343 | + png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, |
342 | + color_type, interlace_type, | | 344 | + color_type, interlace_type, |
343 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); | | 345 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
344 | | | 346 | |
345 | //<DP> simple transparency | | 347 | //<DP> simple transparency |
346 | @@ -469,13 +473,7 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 348 | if (info.nBkgndIndex >= 0){ |
| | | 349 | - info_ptr->num_trans = 1; |
| | | 350 | - info_ptr->valid |= PNG_INFO_tRNS; |
| | | 351 | -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4 |
| | | 352 | - info_ptr->trans_alpha = trans; |
| | | 353 | - info_ptr->trans_color.index = (BYTE)info.nBkgndIndex; |
| | | 354 | - info_ptr->trans_color.red = tc.rgbRed; |
| | | 355 | - info_ptr->trans_color.green = tc.rgbGreen; |
| | | 356 | - info_ptr->trans_color.blue = tc.rgbBlue; |
| | | 357 | - info_ptr->trans_color.gray = info_ptr->trans_color.index; |
| | | 358 | -#else |
| | | 359 | - info_ptr->trans = trans; |
| | | 360 | - info_ptr->trans_values.index = (BYTE)info.nBkgndIndex; |
| | | 361 | - info_ptr->trans_values.red = tc.rgbRed; |
| | | 362 | - info_ptr->trans_values.green = tc.rgbGreen; |
| | | 363 | - info_ptr->trans_values.blue = tc.rgbBlue; |
| | | 364 | - info_ptr->trans_values.gray = info_ptr->trans_values.index; |
| | | 365 | -#endif |
| | | 366 | + png_color_16 trans_color; |
| | | 367 | + |
| | | 368 | + trans_color.index = (BYTE)info.nBkgndIndex; |
| | | 369 | + trans_color.red = tc.rgbRed; |
| | | 370 | + trans_color.green = tc.rgbGreen; |
| | | 371 | + trans_color.blue = tc.rgbBlue; |
| | | 372 | + trans_color.gray = (BYTE)info.nBkgndIndex; |
| | | 373 | + |
| | | 374 | + png_set_tRNS(png_ptr, info_ptr, trans, 1, &trans_color); |
| | | 375 | |
| | | 376 | // the transparency indexes start from 0 for non grayscale palette |
| | | 377 | if (!bGrayScale && head.biClrUsed && info.nBkgndIndex) |
| | | 378 | @@ -459,30 +454,27 @@ bool CxImagePNG::Encode(CxFile *hFile) |
| | | 379 | |
| | | 380 | /* set the palette if there is one */ |
| | | 381 | if (GetPalette()){ |
| | | 382 | - if (!bGrayScale){ |
| | | 383 | - info_ptr->valid |= PNG_INFO_PLTE; |
| | | 384 | - } |
| | | 385 | - |
| | | 386 | int nc = GetClrImportant(); |
| | | 387 | if (nc==0) nc = GetNumColors(); |
| | | 388 | |
347 | if (info.bAlphaPaletteEnabled){ | | 389 | if (info.bAlphaPaletteEnabled){ |
348 | for(WORD ip=0; ip<nc;ip++) | | 390 | for(WORD ip=0; ip<nc;ip++) |
349 | trans[ip]=GetPaletteColor((BYTE)ip).rgbReserved; | | 391 | trans[ip]=GetPaletteColor((BYTE)ip).rgbReserved; |
350 | - info_ptr->num_trans = (WORD)nc; | | 392 | - info_ptr->num_trans = (WORD)nc; |
351 | - info_ptr->valid |= PNG_INFO_tRNS; | | 393 | - info_ptr->valid |= PNG_INFO_tRNS; |
352 | -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4 | | 394 | -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4 |
353 | - info_ptr->trans_alpha = trans; | | 395 | - info_ptr->trans_alpha = trans; |
354 | -#else | | 396 | -#else |
355 | - info_ptr->trans = trans; | | 397 | - info_ptr->trans = trans; |
356 | -#endif | | 398 | -#endif |
357 | + png_set_tRNS(png_ptr, info_ptr, trans, (WORD)nc, NULL); | | 399 | + png_set_tRNS(png_ptr, info_ptr, trans, (WORD)nc, NULL); |
358 | } | | 400 | } |
359 | | | 401 | |
360 | // copy the palette colors | | 402 | // copy the palette colors |
361 | @@ -496,8 +494,8 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 403 | - info_ptr->palette = new png_color[nc]; |
| | | 404 | - info_ptr->num_palette = (png_uint_16) nc; |
| | | 405 | - for (int i=0; i<nc; i++) |
| | | 406 | - GetPaletteColor(i, &info_ptr->palette[i].red, &info_ptr->palette[i].green, &info_ptr->palette[i].blue); |
| | | 407 | + if (!bGrayScale) { |
| | | 408 | + png_color *palette; |
| | | 409 | + |
| | | 410 | + palette = (png_color *)png_malloc(png_ptr, nc*sizeof(palette[0])); |
| | | 411 | + for (int i=0; i<nc; i++) |
| | | 412 | + GetPaletteColor(i, &palette[i].red, &palette[i].green, &palette[i].blue); |
| | | 413 | + png_set_PLTE(png_ptr, info_ptr, palette, nc); |
| | | 414 | + png_data_freer(png_ptr, info_ptr, |
| | | 415 | + PNG_DESTROY_WILL_FREE_DATA, |
| | | 416 | + PNG_FREE_PLTE); |
| | | 417 | + } |
| | | 418 | } |
| | | 419 | |
| | | 420 | #if CXIMAGE_SUPPORT_ALPHA // <vho> |
| | | 421 | @@ -496,8 +488,8 @@ bool CxImagePNG::Encode(CxFile *hFile) |
362 | } } } | | 422 | } } } |
363 | #endif // CXIMAGE_SUPPORT_ALPHA // <vho> | | 423 | #endif // CXIMAGE_SUPPORT_ALPHA // <vho> |
364 | | | 424 | |
365 | - int row_size = max(info.dwEffWidth, info_ptr->width*info_ptr->channels*(info_ptr->bit_depth/8)); | | 425 | - int row_size = max(info.dwEffWidth, info_ptr->width*info_ptr->channels*(info_ptr->bit_depth/8)); |
366 | - info_ptr->rowbytes = row_size; | | 426 | - info_ptr->rowbytes = row_size; |
367 | + int row_size = max(info.dwEffWidth, png_get_rowbytes(png_ptr, info_ptr)); | | 427 | + int row_size = max(info.dwEffWidth, png_get_rowbytes(png_ptr, info_ptr)); |
368 | + //info_ptr->rowbytes = row_size; | | 428 | + //info_ptr->rowbytes = row_size; |
369 | BYTE *row_pointers = new BYTE[row_size]; | | 429 | BYTE *row_pointers = new BYTE[row_size]; |
370 | | | 430 | |
371 | /* write the file information */ | | 431 | /* write the file information */ |
372 | @@ -515,7 +513,7 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 432 | @@ -515,7 +507,7 @@ bool CxImagePNG::Encode(CxFile *hFile) |
373 | if (AlphaIsValid()){ | | 433 | if (AlphaIsValid()){ |
374 | for (long ax=head.biWidth-1; ax>=0;ax--){ | | 434 | for (long ax=head.biWidth-1; ax>=0;ax--){ |
375 | c = BlindGetPixelColor(ax,ay); | | 435 | c = BlindGetPixelColor(ax,ay); |
376 | - int px = ax * info_ptr->channels; | | 436 | - int px = ax * info_ptr->channels; |
377 | + int px = ax * png_get_channels(png_ptr, info_ptr); | | 437 | + int px = ax * png_get_channels(png_ptr, info_ptr); |
378 | if (!bGrayScale){ | | 438 | if (!bGrayScale){ |
379 | row_pointers[px++]=c.rgbRed; | | 439 | row_pointers[px++]=c.rgbRed; |
380 | row_pointers[px++]=c.rgbGreen; | | 440 | row_pointers[px++]=c.rgbGreen; |
381 | @@ -530,7 +528,7 @@ bool CxImagePNG::Encode(CxFile *hFile) | | 441 | @@ -530,7 +522,7 @@ bool CxImagePNG::Encode(CxFile *hFile) |
382 | #endif //CXIMAGE_SUPPORT_ALPHA // <vho> | | 442 | #endif //CXIMAGE_SUPPORT_ALPHA // <vho> |
383 | { | | 443 | { |
384 | iter.GetRow(row_pointers, row_size); | | 444 | iter.GetRow(row_pointers, row_size); |
385 | - if (info_ptr->color_type == PNG_COLOR_TYPE_RGB) //HACK BY OP | | 445 | - if (info_ptr->color_type == PNG_COLOR_TYPE_RGB) //HACK BY OP |
386 | + if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB) //HACK BY OP | | 446 | + if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB) //HACK BY OP |
387 | RGBtoBGR(row_pointers, row_size); | | 447 | RGBtoBGR(row_pointers, row_size); |
388 | png_write_row(png_ptr, row_pointers); | | 448 | png_write_row(png_ptr, row_pointers); |
389 | } | | 449 | } |
| | | 450 | @@ -547,12 +539,6 @@ bool CxImagePNG::Encode(CxFile *hFile) |
| | | 451 | /* It is REQUIRED to call this to finish writing the rest of the file */ |
| | | 452 | png_write_end(png_ptr, info_ptr); |
| | | 453 | |
| | | 454 | - /* if you malloced the palette, free it here */ |
| | | 455 | - if (info_ptr->palette){ |
| | | 456 | - delete [] (info_ptr->palette); |
| | | 457 | - info_ptr->palette = NULL; |
| | | 458 | - } |
| | | 459 | - |
| | | 460 | /* clean up after the write, and free any memory allocated */ |
| | | 461 | png_destroy_write_struct(&png_ptr, (png_infopp)&info_ptr); |
| | | 462 | |