1 $OpenBSD: patch-xvpng_c,v 1.2 2011/08/29 18:07:44 espie Exp $
3 Fix build with png-1.5.
5 --- xvpng.c.orig Mon May 14 02:53:28 2007
6 +++ xvpng.c Mon Aug 29 15:05:31 2011
14 /*** Stuff for PNG Dialog box ***/
16 #define COMPRESSION 6 /* default zlib compression level, not max
17 (Z_BEST_COMPRESSION) */
19 -#define HAVE_tRNS (info_ptr->valid & PNG_INFO_tRNS)
20 +#define HAVE_tRNS (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
24 @@ -435,6 +436,16 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
38 png_color palette[256];
40 byte r1[256], g1[256], b1[256]; /* storage for deduped palette */
41 @@ -444,6 +455,7 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
45 + int num_text, max_text;
47 if ((png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
48 png_xv_error, png_xv_warning)) == NULL) {
49 @@ -458,7 +470,7 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
53 - if (setjmp(png_ptr->jmpbuf)) {
54 + if (setjmp(png_jmpbuf(png_ptr))) {
55 png_destroy_write_struct(&png_ptr, &info_ptr);
58 @@ -489,8 +501,8 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
59 png_set_filter(png_ptr, 0, filter);
62 - info_ptr->width = w;
63 - info_ptr->height = h;
65 + info_tmp.height = h;
66 if (w <= 0 || h <= 0) {
67 SetISTR(ISTR_WARNING, "%s: image dimensions out of range (%dx%d)",
69 @@ -498,7 +510,8 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
73 - info_ptr->interlace_type = interCB.val ? 1 : 0;
74 + info_tmp.interlace_type =
75 + interCB.val ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE;
77 linesize = 0; /* quiet a compiler warning */
79 @@ -542,40 +555,40 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
80 png_destroy_write_struct(&png_ptr, &info_ptr);
83 - info_ptr->color_type = PNG_COLOR_TYPE_RGB;
84 - info_ptr->bit_depth = 8;
85 + info_tmp.color_type = PNG_COLOR_TYPE_RGB;
86 + info_tmp.bit_depth = 8;
87 + info_tmp.use_palette = 0;
88 } else /* ptype == PIC8 */ {
90 - info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
91 + info_tmp.color_type = PNG_COLOR_TYPE_PALETTE;
93 - info_ptr->bit_depth = 1;
94 + info_tmp.bit_depth = 1;
97 - info_ptr->bit_depth = 2;
98 + info_tmp.bit_depth = 2;
100 if (numuniqcols <= 16)
101 - info_ptr->bit_depth = 4;
102 + info_tmp.bit_depth = 4;
104 - info_ptr->bit_depth = 8;
105 + info_tmp.bit_depth = 8;
107 for (i = 0; i < numuniqcols; i++) {
108 palette[i].red = r1[i];
109 palette[i].green = g1[i];
110 palette[i].blue = b1[i];
112 - info_ptr->num_palette = numuniqcols;
113 - info_ptr->palette = palette;
114 - info_ptr->valid |= PNG_INFO_PLTE;
115 + info_tmp.use_palette = 1;
119 else if (colorType == F_GREYSCALE || colorType == F_BWDITHER) {
120 - info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
121 + info_tmp.color_type = PNG_COLOR_TYPE_GRAY;
122 + info_tmp.use_palette = 0;
123 if (colorType == F_BWDITHER) {
124 /* shouldn't happen */
125 if (ptype == PIC24) FatalError("PIC24 and B/W Stipple in WritePNG()");
127 - info_ptr->bit_depth = 1;
128 + info_tmp.bit_depth = 1;
129 if (MONO(r1[0], g1[0], b1[0]) > MONO(r1[1], g1[1], b1[1])) {
132 @@ -595,7 +608,7 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
133 png_destroy_write_struct(&png_ptr, &info_ptr);
136 - info_ptr->bit_depth = 8;
137 + info_tmp.bit_depth = 8;
139 else /* ptype == PIC8 */ {
141 @@ -617,7 +630,7 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
143 remap[i]=0; /* shouldn't be necessary, but... */
145 - info_ptr->bit_depth = 8;
146 + info_tmp.bit_depth = 8;
148 /* Note that this fails most of the time because of gamma */
149 /* (and that would be a bug: GRR FIXME) */
150 @@ -636,7 +649,7 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
151 for (i = 0; i < numuniqcols; i++) {
154 - info_ptr->bit_depth = 4;
155 + info_tmp.bit_depth = 4;
157 /* try to adjust to 2-bit precision grayscale */
159 @@ -652,7 +665,7 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
160 for (i = 0; i < numuniqcols; i++) {
163 - info_ptr->bit_depth = 2;
164 + info_tmp.bit_depth = 2;
166 /* try to adjust to 1-bit precision grayscale */
168 @@ -668,7 +681,7 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
169 for (i = 0; i < numuniqcols; i++) {
172 - info_ptr->bit_depth = 1;
173 + info_tmp.bit_depth = 1;
177 @@ -677,6 +690,20 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
179 png_error(png_ptr, "Unknown colorstyle in WritePNG");
181 + png_set_IHDR(png_ptr, info_ptr,
182 + info_tmp.width, info_tmp.height,
183 + info_tmp.bit_depth, info_tmp.color_type,
184 + info_tmp.interlace_type, PNG_COMPRESSION_TYPE_BASE,
185 + PNG_FILTER_TYPE_BASE);
186 + if (info_tmp.use_palette) {
188 + * info_ptr->num_palette = numuniqcols;
189 + * info_ptr->palette = palette;
190 + * info_ptr->valid |= PNG_INFO_PLTE;
192 + png_set_PLTE(png_ptr, info_ptr, palette, numuniqcols);
195 if ((text = (png_textp)malloc(sizeof(png_text)))) {
196 sprintf(software, "XV %s", REVDATE);
198 @@ -684,21 +711,29 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
199 text->key = "Software";
200 text->text = software;
201 text->text_length = strlen(text->text);
204 - info_ptr->max_text = 1;
205 - info_ptr->num_text = 1;
206 - info_ptr->text = text;
208 + * info_ptr->max_text = 1;
209 + * info_ptr->num_text = 1;
210 + * info_ptr->text = text;
212 + png_set_text(png_ptr, info_ptr, text, 1);
213 + num_text = max_text = 1;
216 Display_Gamma = gDial.val; /* Save the current gamma for loading */
218 // GRR FIXME: add .Xdefaults option to omit writing gamma (size, cumulative errors when editing)--alternatively, modify save box to include "omit" checkbox
219 - info_ptr->gamma = 1.0/gDial.val;
220 - info_ptr->valid |= PNG_INFO_gAMA;
222 + * info_ptr->gamma = 1.0/gDial.val;
223 + * info_ptr->valid |= PNG_INFO_gAMA;
225 + png_set_gAMA(png_ptr, info_ptr, 1.0/gDial.val);
227 png_write_info(png_ptr, info_ptr);
229 - if (info_ptr->bit_depth < 8)
230 + if (info_tmp.bit_depth < 8)
231 png_set_packing(png_ptr);
233 pass=png_set_interlace_handling(png_ptr);
234 @@ -711,13 +746,13 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
237 for (j = 0; j < h; ++j) {
238 - if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
239 + if (info_tmp.color_type == PNG_COLOR_TYPE_GRAY) {
241 for (k = 0; k < w; ++k)
242 png_line[k] = ptype==PIC24 ? MONO(p[k*3], p[k*3+1], p[k*3+2]) :
244 png_write_row(png_ptr, png_line);
245 - } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
246 + } else if (info_tmp.color_type == PNG_COLOR_TYPE_PALETTE) {
248 for (k = 0; k < w; ++k)
249 png_line[k] = pc2nc[p[k]];
250 @@ -743,24 +778,26 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
251 strcpy(savecmnt, picComments);
254 - info_ptr->num_text = 0;
256 + png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
259 comment = strchr(key, ':');
262 /* Allocate a larger structure for comments if necessary */
263 - if (info_ptr->num_text >= info_ptr->max_text)
264 + if (num_text >= max_text)
267 - realloc(text, (info_ptr->num_text + 2)*sizeof(png_text))) == NULL)
268 + realloc(text, (num_text + 2)*sizeof(png_text))) == NULL)
275 - tp = &text[info_ptr->num_text];
276 - info_ptr->max_text += 2;
277 + tp = &text[num_text];
282 @@ -810,7 +847,7 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
285 tp->compression = tp->text_length > 640 ? 0 : -1;
286 - info_ptr->num_text++;
291 @@ -834,27 +871,29 @@ int WritePNG(fp, pic, ptype, w, h, rmap, gmap, bmap, n
293 tp->text_length = q - key;
294 tp->compression = tp->text_length > 750 ? 0 : -1;
295 - info_ptr->num_text++;
299 } while (key && *key);
300 + png_set_text(png_ptr, info_ptr, text, num_text);
303 - info_ptr->num_text = 0;
304 + png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
307 - info_ptr->text = text;
309 - png_convert_from_time_t(&(info_ptr->mod_time), time(NULL));
310 - info_ptr->valid |= PNG_INFO_tIME;
314 + png_convert_from_time_t(&mod_time, time(NULL));
315 + png_set_tIME(png_ptr, info_ptr, &mod_time);
318 png_write_end(png_ptr, info_ptr);
319 fflush(fp); /* just in case we core-dump before finishing... */
323 - /* must do this or png_destroy_write_struct() 0.97+ will free text again: */
324 - info_ptr->text = (png_textp)NULL;
328 @@ -886,6 +925,8 @@ int LoadPNG(fname, pinfo)
335 fbasename = BaseName(fname);
337 @@ -921,7 +962,7 @@ int LoadPNG(fname, pinfo)
338 FatalError("malloc failure in LoadPNG");
341 - if (setjmp(png_ptr->jmpbuf)) {
342 + if (setjmp(png_jmpbuf(png_ptr))) {
344 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
345 if (!read_anything) {
346 @@ -945,8 +986,8 @@ int LoadPNG(fname, pinfo)
348 png_read_info(png_ptr, info_ptr);
350 - pinfo->w = pinfo->normw = info_ptr->width;
351 - pinfo->h = pinfo->normh = info_ptr->height;
352 + pinfo->w = pinfo->normw = png_get_image_width(png_ptr, info_ptr);
353 + pinfo->h = pinfo->normh = png_get_image_height(png_ptr, info_ptr);
354 if (pinfo->w <= 0 || pinfo->h <= 0) {
355 SetISTR(ISTR_WARNING, "%s: image dimensions out of range (%dx%d)",
356 fbasename, pinfo->w, pinfo->h);
357 @@ -957,9 +998,9 @@ int LoadPNG(fname, pinfo)
358 pinfo->frmType = F_PNG;
360 sprintf(pinfo->fullInfo, "PNG, %d bit ",
361 - info_ptr->bit_depth * info_ptr->channels);
362 + png_get_bit_depth(png_ptr,info_ptr) * png_get_channels(png_ptr, info_ptr));
364 - switch(info_ptr->color_type) {
365 + switch(png_get_color_type(png_ptr, info_ptr)) {
366 case PNG_COLOR_TYPE_PALETTE:
367 strcat(pinfo->fullInfo, "palette color");
369 @@ -983,15 +1024,20 @@ int LoadPNG(fname, pinfo)
371 sprintf(pinfo->fullInfo + strlen(pinfo->fullInfo),
372 ", %sinterlaced. (%d bytes)",
373 - info_ptr->interlace_type ? "" : "non-", filesize);
374 + png_get_interlace_type(png_ptr, info_ptr) ? "" : "non-", filesize);
376 - sprintf(pinfo->shrtInfo, "%lux%lu PNG", info_ptr->width, info_ptr->height);
377 + sprintf(pinfo->shrtInfo, "%lux%lu PNG",
378 + png_get_image_width(png_ptr, info_ptr),
379 + png_get_image_height(png_ptr, info_ptr));
381 - if (info_ptr->bit_depth < 8)
382 + if (png_get_bit_depth(png_ptr, info_ptr) < 8)
383 png_set_packing(png_ptr);
385 - if (info_ptr->valid & PNG_INFO_gAMA)
386 - png_set_gamma(png_ptr, Display_Gamma, info_ptr->gamma);
387 + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
389 + png_get_gAMA(png_ptr, info_ptr, &gamma);
390 + png_set_gamma(png_ptr, Display_Gamma, gamma);
394 * png_set_gamma(png_ptr, Display_Gamma, 0.45);
395 @@ -1000,7 +1046,7 @@ int LoadPNG(fname, pinfo)
396 gray_to_rgb = 0; /* quiet a compiler warning */
399 - if (info_ptr->bit_depth == 16) {
400 + if (png_get_bit_depth(png_ptr, info_ptr) == 16) {
401 my_background.red = imagebgR;
402 my_background.green = imagebgG;
403 my_background.blue = imagebgB;
404 @@ -1013,8 +1059,8 @@ int LoadPNG(fname, pinfo)
406 png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN,
408 - if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
409 - (info_ptr->color_type == PNG_COLOR_TYPE_GRAY && HAVE_tRNS)) &&
410 + if ((png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA ||
411 + (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY && HAVE_tRNS)) &&
412 (imagebgR != imagebgG || imagebgR != imagebgB)) /* i.e., colored bg */
414 png_set_gray_to_rgb(png_ptr);
415 @@ -1022,8 +1068,10 @@ int LoadPNG(fname, pinfo)
419 - if (info_ptr->valid & PNG_INFO_bKGD) {
420 - png_set_background(png_ptr, &info_ptr->background,
421 + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD)) {
422 + png_color_16p background;
423 + png_get_bKGD(png_ptr, info_ptr, &background);
424 + png_set_background(png_ptr, background,
425 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
427 my_background.red = my_background.green = my_background.blue =
428 @@ -1033,13 +1081,13 @@ int LoadPNG(fname, pinfo)
432 - if (info_ptr->bit_depth == 16)
433 + if (png_get_bit_depth(png_ptr, info_ptr) == 16)
434 png_set_strip_16(png_ptr);
436 - if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
437 - info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
438 + if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY ||
439 + png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA)
441 - if (info_ptr->bit_depth == 1)
442 + if (png_get_bit_depth(png_ptr, info_ptr) == 1)
443 pinfo->colType = F_BWDITHER;
445 pinfo->colType = F_GREYSCALE;
446 @@ -1050,8 +1098,8 @@ int LoadPNG(fname, pinfo)
448 png_read_update_info(png_ptr, info_ptr);
450 - if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
451 - info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || gray_to_rgb)
452 + if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB ||
453 + png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB_ALPHA || gray_to_rgb)
455 linesize = 3 * pinfo->w;
456 if (linesize/3 < pinfo->w) { /* know pinfo->w > 0 (see above) */
457 @@ -1065,16 +1113,20 @@ int LoadPNG(fname, pinfo)
461 - if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
462 - info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
463 + if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY ||
464 + png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA) {
465 for (i = 0; i < 256; i++)
466 pinfo->r[i] = pinfo->g[i] = pinfo->b[i] = i;
468 + png_colorp palette;
471 + png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
472 pinfo->colType = F_FULLCOLOR;
473 - for (i = 0; i < info_ptr->num_palette; i++) {
474 - pinfo->r[i] = info_ptr->palette[i].red;
475 - pinfo->g[i] = info_ptr->palette[i].green;
476 - pinfo->b[i] = info_ptr->palette[i].blue;
477 + for (i = 0; i < num_palette; i++) {
478 + pinfo->r[i] = palette[i].red;
479 + pinfo->g[i] = palette[i].green;
480 + pinfo->b[i] = palette[i].blue;
484 @@ -1092,7 +1144,17 @@ int LoadPNG(fname, pinfo)
485 png_error(png_ptr, "can't allocate space for PNG image");
488 - png_start_read_image(png_ptr);
490 + * In png 1.5 (or at least 1.5.1beta06) calling this after calling
491 + * png_read_update_info() does nothing besides issue a misleading
492 + * warning message. The png docs are not at all clear on what an
493 + * application is *supposed* to do, so I'm not sure if this is a
494 + * problem with xv or with libpng. However, for now I'll comment
495 + * this out as according to the png source that should be harmless
496 + * and we don't want to see the warning message every time someone
499 + /*png_start_read_image(png_ptr);*/
501 for (i = 0; i < pass; i++) {
502 byte *p = pinfo->pic;
503 @@ -1106,22 +1168,24 @@ int LoadPNG(fname, pinfo)
505 png_read_end(png_ptr, info_ptr);
507 - if (info_ptr->num_text > 0) {
508 + png_get_text(png_ptr, info_ptr, &text, &num_text);
509 + if (num_text > 0) {
512 - for (i = 0; i < info_ptr->num_text; i++)
513 - commentsize += strlen(info_ptr->text[i].key) + 1 +
514 - info_ptr->text[i].text_length + 2;
515 + for (i = 0; i < num_text; i++)
516 + commentsize += strlen(text[i].key) + 1 +
517 + text[i].text_length + 2;
519 if ((pinfo->comment = malloc(commentsize)) == NULL) {
520 png_warning(png_ptr,"can't allocate comment string");
523 pinfo->comment[0] = '\0';
524 - for (i = 0; i < info_ptr->num_text; i++) {
525 - strcat(pinfo->comment, info_ptr->text[i].key);
526 + for (i = 0; i < num_text; i++) {
527 + strcat(pinfo->comment, text[i].key);
528 strcat(pinfo->comment, "::");
529 - strcat(pinfo->comment, info_ptr->text[i].text);
530 + if (text[i].text_length != 0)
531 + strcat(pinfo->comment, text[i].text);
532 strcat(pinfo->comment, "\n");
535 @@ -1143,7 +1207,7 @@ png_xv_error(png_ptr, message)
537 SetISTR(ISTR_WARNING,"%s: libpng error: %s", fbasename, message);
539 - longjmp(png_ptr->jmpbuf, 1);
540 + longjmp(png_jmpbuf(png_ptr), 1);