1 This patch to Andreas Dilger's XV PNG patch 1.2d addresses the following
2 bugs, buglets and typos:
4 - fixes a core-dump triggered by an unfortunate "feature" in libpng 0.97
5 and later (i.e., libpng frees text buffers it didn't allocate)
7 - forces text comments inherited from GIF or elsewhere to be in proper
8 PNG format (i.e., Unix-style "LF" line-endings); inherited PNG comments
9 are still assumed to be in proper format
11 - corrects argument types for png_xv_error() and png_xv_warning() to
12 match what png_create_write_struct() and png_create_read_struct()
15 - changes default ">>" increment on compression dial from 2 to 3 so that
16 maximum compression (level 9) is one click away from default (level 6)
18 - fixes some comment typos and removes overlooked debug code
20 This code has been tested with various versions of libpng, most recently
21 version 1.0.1c4 (precursor to 1.0.2), with no apparent problems.
23 Greg Roelofs, newt@pobox.com
28 *** xvpng.c.stock Thu Jun 13 16:42:11 1996
29 --- xvpng.c Sun May 10 13:02:07 1998
36 + #define LF 10 /* a.k.a. '\n' on ASCII machines */
37 + #define CR 13 /* a.k.a. '\r' on ASCII machines */
39 /*** local functions ***/
40 static void drawPD PARM((int, int, int, int));
41 static void clickPD PARM((int, int));
44 static int WritePNG PARM((FILE *, byte *, int, int, int,
45 byte *, byte *, byte *, int));
47 ! static void png_xv_error PARM((png_struct *png_ptr, char *message));
48 ! static void png_xv_warning PARM((png_struct *png_ptr, char *message));
50 /*** local variables ***/
51 static char *filename;
53 static int WritePNG PARM((FILE *, byte *, int, int, int,
54 byte *, byte *, byte *, int));
56 ! static void png_xv_error PARM((png_structp png_ptr,
57 ! png_const_charp message));
58 ! static void png_xv_warning PARM((png_structp png_ptr,
59 ! png_const_charp message));
61 /*** local variables ***/
62 static char *filename;
65 XSelectInput(theDisp, pngW, ExposureMask | ButtonPressMask | KeyPressMask);
67 DCreate(&cDial, pngW, 12, 25, DWIDE, DHIGH, (double)Z_NO_COMPRESSION,
68 ! (double)Z_BEST_COMPRESSION, COMPRESSION, 1.0, 2.0,
69 infofg, infobg, hicol, locol, "Compression", NULL);
71 DCreate(&gDial, pngW, DWIDE+27, 25, DWIDE, DHIGH, 1.0, 3.5,DISPLAY_GAMMA,0.01,0.2,
73 XSelectInput(theDisp, pngW, ExposureMask | ButtonPressMask | KeyPressMask);
75 DCreate(&cDial, pngW, 12, 25, DWIDE, DHIGH, (double)Z_NO_COMPRESSION,
76 ! (double)Z_BEST_COMPRESSION, COMPRESSION, 1.0, 3.0,
77 infofg, infobg, hicol, locol, "Compression", NULL);
79 DCreate(&gDial, pngW, DWIDE+27, 25, DWIDE, DHIGH, 1.0, 3.5,DISPLAY_GAMMA,0.01,0.2,
84 for(j = 0; j < h; j++) {
86 if(info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
88 for(k = 0; k < w; k++)
89 png_line[k] = ptype==PIC24 ? MONO(p[k*3], p[k*3+1], p[k*3+2]) :
91 png_write_row(png_ptr, png_line);
92 ! } else /* rbg or palette */
93 png_write_row(png_ptr, p);
94 if((j & 0x1f) == 0) WaitCursor();
99 for(j = 0; j < h; j++) {
100 if(info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
102 for(k = 0; k < w; k++)
103 png_line[k] = ptype==PIC24 ? MONO(p[k*3], p[k*3+1], p[k*3+2]) :
105 png_write_row(png_ptr, png_line);
106 ! } else /* RGB or palette */
107 png_write_row(png_ptr, p);
108 if((j & 0x1f) == 0) WaitCursor();
115 ! /* It is just a generic comment */
119 ! tp->text_length = strlen(tp->text);
120 tp->compression = tp->text_length > 750 ? 0 : -1;
121 info_ptr->num_text++;
127 ! /* Just a generic comment: make sure line-endings are valid for PNG */
129 + char *p=key, *q=key; /* only deleting chars, not adding any */
132 + if (*p == CR) { /* lone CR or CR/LF: EOL either way */
133 + *q++ = LF; /* LF is the only allowed PNG line-ending */
134 + if (p[1] == LF) /* get rid of any original LF */
136 + } else if (*p == LF) /* lone LF */
142 + *q = '\0'; /* unnecessary...but what the heck */
145 ! tp->text_length = q - key;
146 tp->compression = tp->text_length > 750 ? 0 : -1;
147 info_ptr->num_text++;
151 info_ptr->valid |= PNG_INFO_tIME;
153 png_write_end(png_ptr, info_ptr);
154 ! png_destroy_write_struct(&png_ptr, &info_ptr);
167 info_ptr->valid |= PNG_INFO_tIME;
169 png_write_end(png_ptr, info_ptr);
170 ! fflush(fp); /* just in case we core-dump before finishing... */
175 + /* must do this or png_destroy_write_struct() 0.97+ will free text again: */
176 + info_ptr->text = (png_textp)NULL;
180 + savecmnt = (char *)NULL;
184 + png_destroy_write_struct(&png_ptr, &info_ptr);
192 /*******************************************/
194 png_xv_error(png_ptr, message)
199 + png_structp png_ptr;
200 + png_const_charp message;
202 SetISTR(ISTR_WARNING,"%s: libpng error: %s", fbasename, message);
207 /*******************************************/
209 png_xv_warning(png_ptr, message)
214 + png_structp png_ptr;
215 + png_const_charp message;