summaryrefslogtreecommitdiffstats
path: root/network/mosaic-ck/mosaic_png_fix.diff
blob: 322f203d778d23373544cdce19ec622624d848cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
diff -Naur mosaic-ck/src/readPNG.c mosaic-ck.patched/src/readPNG.c
--- mosaic-ck/src/readPNG.c	2009-04-25 14:26:02.000000000 -0400
+++ mosaic-ck.patched/src/readPNG.c	2016-10-28 03:24:50.970270499 -0400
@@ -1,5 +1,3 @@
-/* Changes for Mosaic-CK (C)2009 Cameron Kaiser */
-
 /****************************************************************************
  * NCSA Mosaic for the X Window System                                      *
  * Software Development Group                                               *
@@ -96,7 +94,15 @@
     png_struct *png_ptr;
     png_info *info_ptr;
 
-    double screen_gamma;
+    png_uint_32 raw_width, raw_height, rowbytes;
+    int bit_depth, color_type, interlace_type, compression_type, filter_type;
+
+    png_uint_32 have_palette;
+    png_colorp palette;
+    int num_palette;
+    png_uint_16p hist = NULL;
+
+    double gamma, screen_gamma;
 
     png_byte *png_pixels=NULL, **row_pointers=NULL;
     int i, j;
@@ -117,9 +123,9 @@
         if(ret != 8)
             return 0;
         
-        ret = png_check_sig(buf, 8);
+        ret = png_sig_cmp(buf, 0, 8);
         
-        if(!ret)
+        if(ret)
             return(0);
     }
 
@@ -128,18 +134,20 @@
     rewind(infile);
 
         /* allocate the structures */
+    /*png_ptr = (png_struct *)malloc(sizeof(png_struct));*/
     png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
     if(!png_ptr)
         return 0;
 
-    info_ptr = (png_info *)malloc(sizeof(png_info));
+    /* initialize the structures */
+    info_ptr = png_create_info_struct(png_ptr);
     if(!info_ptr) {
-        png_destroy_read_struct(&png_ptr, NULL, NULL);
+        /*free(png_ptr);*/
         return 0;
     }
 
         /* Establish the setjmp return context for png_error to use. */
-    if (setjmp(png_ptr->jmpbuf)) {
+    if (setjmp(png_jmpbuf(png_ptr))) {
         
 #ifndef DISABLE_TRACE
         if (srcTrace) {
@@ -147,19 +155,26 @@
         }
 #endif
 
+        /*png_read_destroy(png_ptr, info_ptr, (png_info *)0); */
 	png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
 
         if(png_pixels != NULL)
             free((char *)png_pixels);
         if(row_pointers != NULL)
             free((png_byte **)row_pointers);
-	png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+                
+        /*free((char *)png_ptr);*/
+        free((char *)info_ptr);
         
         return 0;
     }
 
-        /* initialize the structure */
-    png_info_init(info_ptr);
+#ifdef SAM_NO
+    /* SWP -- Hopefully to fix cores on bad PNG files */
+    png_set_message_fn(png_ptr,png_get_msg_ptr(png_ptr),NULL,NULL); 
+#endif
+
+    /*png_read_init(png_ptr);*/
     
         /* set up the input control */
     png_init_io(png_ptr, infile);
@@ -169,20 +184,26 @@
     
         /* setup other stuff using the fields of png_info. */
     
-    *width = (int)png_ptr->width;
-    *height = (int)png_ptr->height;
+    png_get_IHDR(png_ptr, info_ptr, &raw_width, &raw_height, &bit_depth,
+                 &color_type, &interlace_type, &compression_type,
+                 &filter_type);
+    rowbytes = png_get_rowbytes(png_ptr, info_ptr);
+    have_palette = png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
+
+    *width = (int)raw_width;
+    *height = (int)raw_height;
 
 #ifndef DISABLE_TRACE
     if (srcTrace) {
-        fprintf(stderr,"\n\nBEFORE\nheight = %d\n", (int)png_ptr->width);
-        fprintf(stderr,"width = %d\n", (int)png_ptr->height);
-        fprintf(stderr,"bit depth = %d\n", info_ptr->bit_depth);
-        fprintf(stderr,"color type = %d\n", info_ptr->color_type);
-        fprintf(stderr,"compression type = %d\n", info_ptr->compression_type);
-        fprintf(stderr,"filter type = %d\n", info_ptr->filter_type);
-        fprintf(stderr,"interlace type = %d\n", info_ptr->interlace_type);
-        fprintf(stderr,"num colors = %d\n",info_ptr->num_palette);
-        fprintf(stderr,"rowbytes = %d\n", info_ptr->rowbytes);
+        fprintf(stderr,"\n\nBEFORE\nwidth = %d\n", *width);
+        fprintf(stderr,"height = %d\n", *height);
+        fprintf(stderr,"bit depth = %d\n", bit_depth);
+        fprintf(stderr,"color type = %d\n", color_type);
+        fprintf(stderr,"compression type = %d\n", compression_type);
+        fprintf(stderr,"filter type = %d\n", filter_type);
+        fprintf(stderr,"interlace type = %d\n", interlace_type);
+        fprintf(stderr,"num colors = %d\n", num_palette);
+        fprintf(stderr,"rowbytes = %d\n", rowbytes);
     }
 #endif
 
@@ -205,16 +226,16 @@
 #endif
 
         /* strip pixels in 16-bit images down to 8 bits */
-    if (info_ptr->bit_depth == 16)
+    if (bit_depth == 16)
         png_set_strip_16(png_ptr);
 
 
         /* If it is a color image then check if it has a palette. If not
            then dither the image to 256 colors, and make up a palette */
-    if (info_ptr->color_type==PNG_COLOR_TYPE_RGB ||
-        info_ptr->color_type==PNG_COLOR_TYPE_RGB_ALPHA) {
+    if (color_type==PNG_COLOR_TYPE_RGB ||
+        color_type==PNG_COLOR_TYPE_RGB_ALPHA) {
 
-        if(! (info_ptr->valid & PNG_INFO_PLTE)) {
+        if(!have_palette) {
 
 #ifndef DISABLE_TRACE
             if (srcTrace) {
@@ -232,9 +253,7 @@
 
                 /* this should probably be dithering to 
                    Rdata.colors_per_inlined_image colors */
-            png_set_dither(png_ptr, std_color_cube, 
-                           216, 
-                           216, NULL, 1);
+            png_set_quantize(png_ptr, std_color_cube, 216, 216, NULL, 1);
             
         } else {
 #ifndef DISABLE_TRACE
@@ -243,10 +262,9 @@
             }
 #endif
  
-            png_set_dither(png_ptr, info_ptr->palette, 
-                           info_ptr->num_palette,
-                           get_pref_int(eCOLORS_PER_INLINED_IMAGE), 
-                           info_ptr->hist, 1);
+            png_get_hIST(png_ptr, info_ptr, &hist);
+            png_set_quantize(png_ptr, palette, num_palette,
+                             get_pref_int(eCOLORS_PER_INLINED_IMAGE), hist, 1);
             
         }
     }
@@ -255,14 +273,14 @@
            small as they can. This expands pixels to 1 pixel per byte, and
            if a transparency value is supplied, an alpha channel is
            built.*/
-    if (info_ptr->bit_depth < 8)
+    if (bit_depth < 8)
         png_set_packing(png_ptr);
 
 
         /* have libpng handle the gamma conversion */
 
     if (get_pref_boolean(eUSE_SCREEN_GAMMA)) { /*SWP*/
-        if (info_ptr->bit_depth != 16) {  /* temporary .. glennrp */
+        if (bit_depth != 16) {  /* temporary .. glennrp */
             screen_gamma=(double)(get_pref_float(eSCREEN_GAMMA));
             
 #ifndef DISABLE_TRACE
@@ -270,13 +288,13 @@
                 fprintf(stderr,"screen gamma=%f\n",screen_gamma);
             }
 #endif
-            if (info_ptr->valid & PNG_INFO_gAMA) {
+            if (png_get_gAMA(png_ptr, info_ptr, &gamma)) {
 #ifndef DISABLE_TRACE
                 if (srcTrace) {
-                    printf("setting gamma=%f\n",info_ptr->gamma);
+                    printf("setting gamma=%f\n", gamma);
                 }
 #endif
-                png_set_gamma(png_ptr, screen_gamma, (double)info_ptr->gamma);
+                png_set_gamma(png_ptr, screen_gamma, gamma);
             }
             else {
 #ifndef DISABLE_TRACE
@@ -289,34 +307,39 @@
         }
     }
     
-    if (info_ptr->interlace_type)
+    if (interlace_type)
         png_set_interlace_handling(png_ptr);
 
     png_read_update_info(png_ptr, info_ptr);
     
+    png_get_IHDR(png_ptr, info_ptr, &raw_width, &raw_height, &bit_depth,
+                 &color_type, &interlace_type, &compression_type,
+                 &filter_type);
+    rowbytes = png_get_rowbytes(png_ptr, info_ptr);
+    have_palette = png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
+
 #ifndef DISABLE_TRACE
     if (srcTrace) {
-        fprintf(stderr,"\n\nAFTER\nheight = %d\n", (int)png_ptr->width);
-        fprintf(stderr,"width = %d\n", (int)png_ptr->height);
-        fprintf(stderr,"bit depth = %d\n", info_ptr->bit_depth);
-        fprintf(stderr,"color type = %d\n", info_ptr->color_type);
-        fprintf(stderr,"compression type = %d\n", info_ptr->compression_type);
-        fprintf(stderr,"filter type = %d\n", info_ptr->filter_type);
-        fprintf(stderr,"interlace type = %d\n", info_ptr->interlace_type);
-        fprintf(stderr,"num colors = %d\n",info_ptr->num_palette);
-        fprintf(stderr,"rowbytes = %d\n", info_ptr->rowbytes);
+        fprintf(stderr,"\n\nAFTER\nwidth = %d\n", *width);
+        fprintf(stderr,"height = %d\n", *height);
+        fprintf(stderr,"bit depth = %d\n", bit_depth);
+        fprintf(stderr,"color type = %d\n", color_type);
+        fprintf(stderr,"compression type = %d\n", compression_type);
+        fprintf(stderr,"filter type = %d\n", filter_type);
+        fprintf(stderr,"interlace type = %d\n", interlace_type);
+        fprintf(stderr,"num colors = %d\n",num_palette);
+        fprintf(stderr,"rowbytes = %d\n", rowbytes);
     }
 #endif
 
         /* allocate the pixel grid which we will need to send to 
            png_read_image(). */
-    png_pixels = (png_byte *)malloc(info_ptr->rowbytes * 
-                                    (*height) * sizeof(png_byte));
+    png_pixels = (png_byte *)malloc(rowbytes * (*height) * sizeof(png_byte));
     
 
     row_pointers = (png_byte **) malloc((*height) * sizeof(png_byte *));
     for (i=0; i < *height; i++)
-        row_pointers[i]=png_pixels+(info_ptr->rowbytes*i);
+        row_pointers[i]=png_pixels+(rowbytes*i);
 
     
         /* FINALLY - read the darn thing. */
@@ -325,13 +348,13 @@
     
         /* now that we have the (transformed to 8-bit RGB) image, we have
            to copy the resulting palette to our colormap. */
-    if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) {
-        if (info_ptr->valid & PNG_INFO_PLTE) {
+    if (color_type & PNG_COLOR_MASK_COLOR) {
+        if (have_palette) {
             
-            for (i=0; i < info_ptr->num_palette; i++) {
-                colrs[i].red = info_ptr->palette[i].red << 8;
-                colrs[i].green = info_ptr->palette[i].green << 8;
-                colrs[i].blue = info_ptr->palette[i].blue << 8;
+            for (i=0; i < num_palette; i++) {
+                colrs[i].red = palette[i].red << 8;
+                colrs[i].green = palette[i].green << 8;
+                colrs[i].blue = palette[i].blue << 8;
                 colrs[i].pixel = i;
                 colrs[i].flags = DoRed|DoGreen|DoBlue;
             }
@@ -366,7 +389,7 @@
 
         /* if there is an alpha channel, we have to get rid of it in the
            pixmap, since I don't do anything with it yet */
-    if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) {
+    if (color_type & PNG_COLOR_MASK_ALPHA) {
 
 #ifndef DISABLE_TRACE
         if (srcTrace) {
@@ -405,10 +428,14 @@
     free((png_byte **)row_pointers);
     
         /* clean up after the read, and free any memory allocated */
+    /*png_read_destroy(png_ptr, info_ptr, (png_info *)0);*/
+	png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+    
 
-        /* free the structure */
-        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
-
+        /* free the structures */
+    /*free((char *)png_ptr);*/
+    free((char *)info_ptr);
+    
     return pixmap;
 }