Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 355025 | Differences between
and this patch

Collapse All | Expand All

(-)netpbm-10.51.00/converter/other/pamrgbatopng.c.orig (-4 / +6 lines)
Lines 101-110 Link Here
101
    if (!infoP)
101
    if (!infoP)
102
        pm_error("Could not allocate PNG info structure");
102
        pm_error("Could not allocate PNG info structure");
103
    else {
103
    else {
104
        infoP->width      = pamP->width;
104
    	png_set_IHDR(pngP, infoP,
105
        infoP->height     = pamP->height;
105
			pamP->width, pamP->height, 8,
106
        infoP->bit_depth  = 8;
106
			PNG_COLOR_TYPE_RGB_ALPHA,
107
        infoP->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
107
			PNG_INTERLACE_NONE,
108
			PNG_COMPRESSION_TYPE_DEFAULT,
109
			PNG_FILTER_TYPE_DEFAULT);
108
        
110
        
109
        png_init_io(pngP, ofP);
111
        png_init_io(pngP, ofP);
110
112
(-)netpbm-10.51.00/converter/other/pngtxt.c.orig (-10 / +12 lines)
Lines 240-251 Link Here
240
240
241
241
242
void 
242
void 
243
pnmpng_read_text (png_info * const info_ptr, 
243
pnmpng_read_text (struct pngx* const pngxP, 
244
                  FILE *     const tfp, 
244
                  FILE *     const tfp, 
245
                  bool       const ztxt,
245
                  bool       const ztxt,
246
                  bool       const verbose) {
246
                  bool       const verbose) {
247
247
248
    const char * textline;
248
    const char * textline;
249
    struct png_text_struct* text;
249
    unsigned int lineLength;
250
    unsigned int lineLength;
250
    unsigned int commentIdx;
251
    unsigned int commentIdx;
251
    bool noCommentsYet;
252
    bool noCommentsYet;
Lines 257-264 Link Here
257
258
258
    allocatedComments = 256;  /* initial value */
259
    allocatedComments = 256;  /* initial value */
259
260
260
    MALLOCARRAY(info_ptr->text, allocatedComments);
261
    MALLOCARRAY(text, allocatedComments);
261
    if (info_ptr->text == NULL) 
262
    if (text == NULL) 
262
        pm_error("unable to allocate memory for comment array");
263
        pm_error("unable to allocate memory for comment array");
263
264
264
    commentIdx = 0;
265
    commentIdx = 0;
Lines 273-279 Link Here
273
            if (lineLength == 0) {
274
            if (lineLength == 0) {
274
                /* skip this empty line */
275
                /* skip this empty line */
275
            } else {
276
            } else {
276
                handleArrayAllocation(&info_ptr->text, &allocatedComments,
277
                handleArrayAllocation(&text, &allocatedComments,
277
                                      commentIdx);
278
                                      commentIdx);
278
                if ((textline[0] != ' ') && (textline[0] != '\t')) {
279
                if ((textline[0] != ' ') && (textline[0] != '\t')) {
279
                    /* Line doesn't start with white space, which
280
                    /* Line doesn't start with white space, which
Lines 285-291 Link Here
285
                        ++commentIdx;
286
                        ++commentIdx;
286
                    noCommentsYet = FALSE;
287
                    noCommentsYet = FALSE;
287
288
288
                    startComment(&info_ptr->text[commentIdx], 
289
                    startComment(&text[commentIdx], 
289
                                 textline, lineLength, ztxt);
290
                                 textline, lineLength, ztxt);
290
                } else {
291
                } else {
291
                    /* Line starts with whitespace, which means it is
292
                    /* Line starts with whitespace, which means it is
Lines 295-301 Link Here
295
                        pm_error("Invalid comment file format: "
296
                        pm_error("Invalid comment file format: "
296
                                 "first line is a continuation line! "
297
                                 "first line is a continuation line! "
297
                                 "(It starts with whitespace)");
298
                                 "(It starts with whitespace)");
298
                    continueComment(&info_ptr->text[commentIdx], 
299
                    continueComment(&text[commentIdx], 
299
                                    textline, lineLength);
300
                                    textline, lineLength);
300
                }
301
                }
301
            }
302
            }
Lines 303-314 Link Here
303
        }
304
        }
304
    } 
305
    } 
305
    if (noCommentsYet)
306
    if (noCommentsYet)
306
        info_ptr->num_text = 0;
307
        png_set_text(pngxP->png_ptr, pngxP->info_ptr, NULL, 0);
307
    else
308
    else
308
        info_ptr->num_text = commentIdx + 1;
309
        png_set_text(pngxP->png_ptr, pngxP->info_ptr, text, commentIdx + 1);
309
310
    /* TODO: does this leak memory if noCommentsYet is true? */
310
    if (verbose)
311
    if (verbose)
311
        pm_message("%d comments placed in text chunk", info_ptr->num_text);
312
        pm_message("%d comments placed in text chunk",
313
	    noCommentsYet ? 0 : commentIdx+1);
312
}
314
}
313
315
314
316
(-)netpbm-10.51.00/converter/other/pnmtopng.c.orig (-85 / +84 lines)
Lines 58-64 Link Here
58
#include <assert.h>
58
#include <assert.h>
59
#include <string.h> /* strcat() */
59
#include <string.h> /* strcat() */
60
#include <limits.h>
60
#include <limits.h>
61
#include <png.h>    /* includes zlib.h and setjmp.h */
61
#include <png.h>    /* includes setjmp.h */
62
#include <zlib.h>
62
63
63
#include "pm_c_util.h"
64
#include "pm_c_util.h"
64
#include "pnm.h"
65
#include "pnm.h"
Lines 2180-2186 Link Here
2180
            gray *               const alpha_mask,
2181
            gray *               const alpha_mask,
2181
            colorhash_table      const cht,
2182
            colorhash_table      const cht,
2182
            coloralphahash_table const caht,
2183
            coloralphahash_table const caht,
2183
            png_info *           const info_ptr,
2184
            struct pngx *        const pngxP,
2184
            xelval               const png_maxval,
2185
            xelval               const png_maxval,
2185
            unsigned int         const depth) {
2186
            unsigned int         const depth) {
2186
            
2187
            
Lines 2192-2211 Link Here
2192
        xel p_png;
2193
        xel p_png;
2193
        xel const p = xelrow[col];
2194
        xel const p = xelrow[col];
2194
        PPM_DEPTH(p_png, p, maxval, png_maxval);
2195
        PPM_DEPTH(p_png, p, maxval, png_maxval);
2195
        if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
2196
	png_byte color_type = png_get_color_type(pngxP->png_ptr,
2196
            info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
2197
			pngxP->info_ptr);
2198
        if (color_type == PNG_COLOR_TYPE_GRAY ||
2199
            color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
2197
            if (depth == 16)
2200
            if (depth == 16)
2198
                *pp++ = PNM_GET1(p_png) >> 8;
2201
                *pp++ = PNM_GET1(p_png) >> 8;
2199
            *pp++ = PNM_GET1(p_png) & 0xff;
2202
            *pp++ = PNM_GET1(p_png) & 0xff;
2200
        } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
2203
        } else if (color_type == PNG_COLOR_TYPE_PALETTE) {
2201
            unsigned int paletteIndex;
2204
            unsigned int paletteIndex;
2202
            if (alpha)
2205
            if (alpha)
2203
                paletteIndex = lookupColorAlpha(caht, &p, &alpha_mask[col]);
2206
                paletteIndex = lookupColorAlpha(caht, &p, &alpha_mask[col]);
2204
            else
2207
            else
2205
                paletteIndex = ppm_lookupcolor(cht, &p);
2208
                paletteIndex = ppm_lookupcolor(cht, &p);
2206
            *pp++ = paletteIndex;
2209
            *pp++ = paletteIndex;
2207
        } else if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
2210
        } else if (color_type == PNG_COLOR_TYPE_RGB ||
2208
                   info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
2211
                   color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
2209
            if (depth == 16)
2212
            if (depth == 16)
2210
                *pp++ = PPM_GETR(p_png) >> 8;
2213
                *pp++ = PPM_GETR(p_png) >> 8;
2211
            *pp++ = PPM_GETR(p_png) & 0xff;
2214
            *pp++ = PPM_GETR(p_png) & 0xff;
Lines 2218-2224 Link Here
2218
        } else
2221
        } else
2219
            pm_error("INTERNAL ERROR: undefined color_type");
2222
            pm_error("INTERNAL ERROR: undefined color_type");
2220
                
2223
                
2221
        if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) {
2224
        if (color_type & PNG_COLOR_MASK_ALPHA) {
2222
            int const png_alphaval = (int)
2225
            int const png_alphaval = (int)
2223
                alpha_mask[col] * (float) png_maxval / maxval + 0.5;
2226
                alpha_mask[col] * (float) png_maxval / maxval + 0.5;
2224
            if (depth == 16)
2227
            if (depth == 16)
Lines 2274-2280 Link Here
2274
            
2277
            
2275
            makePngLine(line, xelrow, cols, maxval,
2278
            makePngLine(line, xelrow, cols, maxval,
2276
                        alpha, alpha ? alpha_mask[row] : NULL,
2279
                        alpha, alpha ? alpha_mask[row] : NULL,
2277
                        cht, caht, pngxP->info_ptr, png_maxval, depth);
2280
                        cht, caht, pngxP, png_maxval, depth);
2278
2281
2279
            png_write_row(pngxP->png_ptr, line);
2282
            png_write_row(pngxP->png_ptr, line);
2280
        }
2283
        }
Lines 2293-2299 Link Here
2293
            unsigned int const rows,
2296
            unsigned int const rows,
2294
            xelval       const maxval,
2297
            xelval       const maxval,
2295
            int          const format,
2298
            int          const format,
2296
            png_info *   const info_ptr,
2299
            struct pngx* const pngxP,
2297
            bool         const verbose) {
2300
            bool         const verbose) {
2298
2301
2299
    if (histRequested) {
2302
    if (histRequested) {
Lines 2324-2331 Link Here
2324
                        histogram[i] = chv[chvIndex].value;
2327
                        histogram[i] = chv[chvIndex].value;
2325
                }
2328
                }
2326
            
2329
            
2327
                info_ptr->valid |= PNG_INFO_hIST;
2330
                png_set_hIST(pngxP->png_ptr, pngxP->info_ptr, histogram);
2328
                info_ptr->hist = histogram;
2329
                if (verbose)
2331
                if (verbose)
2330
                    pm_message("histogram created in PNG stream");
2332
                    pm_message("histogram created in PNG stream");
2331
            }
2333
            }
Lines 2336-2376 Link Here
2336
2338
2337
2339
2338
2340
2339
static void
2341
static png_byte
2340
setColorType(struct pngx * const pngxP,
2342
setColorType(struct pngx * const pngxP,
2341
             bool          const colorMapped,
2343
             bool          const colorMapped,
2342
             int           const pnmType,
2344
             int           const pnmType,
2343
             bool          const alpha) {
2345
             bool          const alpha) {
2344
2346
2347
    png_byte color_type = 0;
2348
2345
    if (colorMapped)
2349
    if (colorMapped)
2346
        pngxP->info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
2350
        color_type = PNG_COLOR_TYPE_PALETTE;
2347
    else if (pnmType == PPM_TYPE)
2351
    else if (pnmType == PPM_TYPE)
2348
        pngxP->info_ptr->color_type = PNG_COLOR_TYPE_RGB;
2352
        color_type = PNG_COLOR_TYPE_RGB;
2349
    else
2353
    else
2350
        pngxP->info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
2354
        color_type = PNG_COLOR_TYPE_GRAY;
2351
2355
2352
    if (alpha && pngxP->info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
2356
    if (alpha && color_type != PNG_COLOR_TYPE_PALETTE)
2353
        pngxP->info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
2357
        color_type |= PNG_COLOR_MASK_ALPHA;
2358
    return color_type;
2354
}
2359
}
2355
2360
2356
2361
2357
2362
2358
static void
2363
static void
2359
doGamaChunk(struct cmdlineInfo const cmdline,
2364
doGamaChunk(struct cmdlineInfo const cmdline,
2360
            png_info *         const info_ptr) {
2365
            struct pngx *      const pngxP) {
2361
            
2366
            
2362
    if (cmdline.gammaSpec) {
2367
    if (cmdline.gammaSpec) {
2363
        /* gAMA chunk */
2368
        /* gAMA chunk */
2364
        info_ptr->valid |= PNG_INFO_gAMA;
2369
        png_set_gAMA(pngxP->png_ptr, pngxP->info_ptr, cmdline.gamma);
2365
        info_ptr->gamma = cmdline.gamma;
2366
    }
2370
    }
2367
}
2371
}
2368
2372
2369
2373
2370
2374
/* TODO: needs porting */
2375
#if 0
2371
static void
2376
static void
2372
doChrmChunk(struct cmdlineInfo const cmdline,
2377
doChrmChunk(struct cmdlineInfo const cmdline,
2373
            png_info *         const info_ptr) {
2378
            struct pngx *      const pngxP) {
2374
2379
2375
    if (cmdline.rgbSpec) {
2380
    if (cmdline.rgbSpec) {
2376
        /* cHRM chunk */
2381
        /* cHRM chunk */
Lines 2386-2405 Link Here
2386
        info_ptr->y_blue  = cmdline.rgb.by;
2391
        info_ptr->y_blue  = cmdline.rgb.by;
2387
    }
2392
    }
2388
}
2393
}
2394
#endif
2389
2395
2390
2396
2391
2397
2392
static void
2398
static void
2393
doPhysChunk(struct cmdlineInfo const cmdline,
2399
doPhysChunk(struct cmdlineInfo const cmdline,
2394
            png_info *         const info_ptr) {
2400
            struct pngx *      const pngxP) {
2395
2401
2396
    if (cmdline.sizeSpec) {
2402
    if (cmdline.sizeSpec) {
2397
        /* pHYS chunk */
2403
        /* pHYS chunk */
2398
        info_ptr->valid |= PNG_INFO_pHYs;
2404
        png_set_pHYs(pngxP->png_ptr, pngxP->info_ptr,
2399
2405
                        cmdline.size.x, cmdline.size.y,
2400
        info_ptr->x_pixels_per_unit = cmdline.size.x;
2406
                        cmdline.size.unit);
2401
        info_ptr->y_pixels_per_unit = cmdline.size.y;
2402
        info_ptr->phys_unit_type    = cmdline.size.unit;
2403
    }
2407
    }
2404
}
2408
}
2405
2409
Lines 2408-2420 Link Here
2408
2412
2409
static void
2413
static void
2410
doTimeChunk(struct cmdlineInfo const cmdline,
2414
doTimeChunk(struct cmdlineInfo const cmdline,
2411
            png_info *         const info_ptr) {
2415
            struct pngx *      const pngxP) {
2412
2416
2413
    if (cmdline.modtimeSpec) {
2417
    if (cmdline.modtimeSpec) {
2414
        /* tIME chunk */
2418
        /* tIME chunk */
2415
        info_ptr->valid |= PNG_INFO_tIME;
2419
        png_timep ptime;
2416
2420
2417
        png_convert_from_time_t(&info_ptr->mod_time, cmdline.modtime);
2421
        png_convert_from_time_t(ptime, cmdline.modtime);
2422
        png_set_tIME(pngxP->png_ptr, pngxP->info_ptr, ptime);
2418
    }
2423
    }
2419
}
2424
}
2420
2425
Lines 2450-2457 Link Here
2450
            pixel         const transColor,
2455
            pixel         const transColor,
2451
            xelval        const maxval,
2456
            xelval        const maxval,
2452
            xelval        const pngMaxval) {
2457
            xelval        const pngMaxval) {
2453
2458
    png_byte color_type = png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
2454
    switch (pngxP->info_ptr->color_type) {
2459
    switch (color_type) {
2455
    case PNG_COLOR_TYPE_PALETTE:
2460
    case PNG_COLOR_TYPE_PALETTE:
2456
        if (transPaletteSize > 0) {
2461
        if (transPaletteSize > 0) {
2457
            png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
2462
            png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
Lines 2483-2489 Link Here
2483
2488
2484
static void
2489
static void
2485
doBkgdChunk(bool         const bkgdRequested,
2490
doBkgdChunk(bool         const bkgdRequested,
2486
            png_info *   const info_ptr,
2491
            struct pngx* const pngxP,
2487
            unsigned int const backgroundIndex,
2492
            unsigned int const backgroundIndex,
2488
            pixel        const backColor,
2493
            pixel        const backColor,
2489
            xelval       const maxval,
2494
            xelval       const maxval,
Lines 2491-2523 Link Here
2491
            bool         const verbose) {
2496
            bool         const verbose) {
2492
    
2497
    
2493
    if (bkgdRequested) {
2498
    if (bkgdRequested) {
2494
        info_ptr->valid |= PNG_INFO_bKGD;
2499
        png_color_16 bg = xelToPngColor_16(backColor, maxval, pngMaxval);
2495
        if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
2500
        png_set_bKGD(pngxP->png_ptr, pngxP->info_ptr, &bg);
2496
            info_ptr->background.index = backgroundIndex;
2501
        if (verbose)
2497
        } else {
2502
            pm_message("Writing bKGD chunk with background color "
2498
            info_ptr->background = 
2503
                       " {gray, red, green, blue} = {%d, %d, %d, %d}",
2499
                xelToPngColor_16(backColor, maxval, pngMaxval);
2504
                       bg.gray, 
2500
            if (verbose)
2505
                       bg.red, 
2501
                pm_message("Writing bKGD chunk with background color "
2506
                       bg.green, 
2502
                           " {gray, red, green, blue} = {%d, %d, %d, %d}",
2507
                       bg.blue ); 
2503
                           info_ptr->background.gray, 
2504
                           info_ptr->background.red, 
2505
                           info_ptr->background.green, 
2506
                           info_ptr->background.blue ); 
2507
        }
2508
    }
2508
    }
2509
}
2509
}
2510
2510
2511
2511
2512
2512
2513
static void
2513
static void
2514
doSbitChunk(png_info * const pngInfoP,
2514
doSbitChunk(struct pngx* const pngxP,
2515
            xelval     const pngMaxval,
2515
            xelval     const pngMaxval,
2516
            xelval     const maxval,
2516
            xelval     const maxval,
2517
            bool       const alpha,
2517
            bool       const alpha,
2518
            xelval     const alphaMaxval) {
2518
            xelval     const alphaMaxval) {
2519
2519
    png_byte color_type = png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
2520
    if (pngInfoP->color_type != PNG_COLOR_TYPE_PALETTE &&
2520
    if (color_type != PNG_COLOR_TYPE_PALETTE &&
2521
        (pngMaxval > maxval || (alpha && pngMaxval > alphaMaxval))) {
2521
        (pngMaxval > maxval || (alpha && pngMaxval > alphaMaxval))) {
2522
2522
2523
        /* We're writing in a bit depth that doesn't match the maxval
2523
        /* We're writing in a bit depth that doesn't match the maxval
Lines 2535-2562 Link Here
2535
           PNG doesn't allow it, we don't attempt to create such an
2535
           PNG doesn't allow it, we don't attempt to create such an
2536
           sBIT chunk.
2536
           sBIT chunk.
2537
        */
2537
        */
2538
2538
        png_color_8 sig_bit;
2539
        pngInfoP->valid |= PNG_INFO_sBIT;
2540
2539
2541
        {
2540
        {
2542
            int const sbitval = pm_maxvaltobits(MIN(maxval, pngMaxval));
2541
            int const sbitval = pm_maxvaltobits(MIN(maxval, pngMaxval));
2543
2542
2544
            if (pngInfoP->color_type & PNG_COLOR_MASK_COLOR) {
2543
            if (color_type & PNG_COLOR_MASK_COLOR) {
2545
                pngInfoP->sig_bit.red   = sbitval;
2544
                sig_bit.red   = sbitval;
2546
                pngInfoP->sig_bit.green = sbitval;
2545
                sig_bit.green = sbitval;
2547
                pngInfoP->sig_bit.blue  = sbitval;
2546
                sig_bit.blue  = sbitval;
2548
            } else
2547
            } else
2549
                pngInfoP->sig_bit.gray = sbitval;
2548
                sig_bit.gray = sbitval;
2550
            
2549
            
2551
            if (verbose)
2550
            if (verbose)
2552
                pm_message("Writing sBIT chunk with bits = %d", sbitval);
2551
                pm_message("Writing sBIT chunk with bits = %d", sbitval);
2553
        }
2552
        }
2554
        if (pngInfoP->color_type & PNG_COLOR_MASK_ALPHA) {
2553
        if (color_type & PNG_COLOR_MASK_ALPHA) {
2555
            pngInfoP->sig_bit.alpha =
2554
            sig_bit.alpha =
2556
                pm_maxvaltobits(MIN(alphaMaxval, pngMaxval));
2555
                pm_maxvaltobits(MIN(alphaMaxval, pngMaxval));
2557
            if (verbose)
2556
            if (verbose)
2558
                pm_message("  alpha bits = %d", pngInfoP->sig_bit.alpha);
2557
                pm_message("  alpha bits = %d", sig_bit.alpha);
2559
        }
2558
        }
2559
        png_set_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sig_bit);
2560
    }
2560
    }
2561
}
2561
}
2562
2562
Lines 2755-2804 Link Here
2755
    pm_error ("setjmp returns error condition (2)");
2755
    pm_error ("setjmp returns error condition (2)");
2756
  }
2756
  }
2757
2757
2758
  pngxP->info_ptr->width = cols;
2758
  png_byte color_type = setColorType(pngxP, colorMapped, pnm_type, alpha);
2759
  pngxP->info_ptr->height = rows;
2759
  png_set_IHDR(pngxP->png_ptr, pngxP->info_ptr,
2760
  pngxP->info_ptr->bit_depth = depth;
2760
                cols, rows, depth, color_type,
2761
2761
                cmdline.interlace,
2762
  setColorType(pngxP, colorMapped, pnm_type, alpha);
2762
                PNG_COMPRESSION_TYPE_DEFAULT,
2763
2763
                PNG_FILTER_TYPE_DEFAULT);
2764
  pngxP->info_ptr->interlace_type = cmdline.interlace;
2764
2765
2765
  doGamaChunk(cmdline, pngxP);
2766
  doGamaChunk(cmdline, pngxP->info_ptr);
2766
  /* TODO: port doChrmChunk */
2767
2767
#if 0
2768
  doChrmChunk(cmdline, pngxP->info_ptr);
2768
  doChrmChunk(cmdline, pngxP);
2769
2769
#endif
2770
  doPhysChunk(cmdline, pngxP->info_ptr);
2770
  doPhysChunk(cmdline, pngxP);
2771
2771
2772
  if (pngxP->info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
2772
  if (color_type == PNG_COLOR_TYPE_PALETTE) {
2773
2773
2774
    /* creating PNG palette (Not counting the transparency palette) */
2774
    /* creating PNG palette (Not counting the transparency palette) */
2775
2775
2776
    createPngPalette(palette_pnm, palette_size, maxval,
2776
    createPngPalette(palette_pnm, palette_size, maxval,
2777
                     trans_pnm, trans_size, alpha_maxval, 
2777
                     trans_pnm, trans_size, alpha_maxval, 
2778
                     palette, trans);
2778
                     palette, trans);
2779
    pngxP->info_ptr->valid |= PNG_INFO_PLTE;
2779
    png_set_PLTE(pngxP->png_ptr, pngxP->info_ptr, palette, palette_size);
2780
    pngxP->info_ptr->palette = palette;
2781
    pngxP->info_ptr->num_palette = palette_size;
2782
2780
2783
    doHistChunk(cmdline.hist, palette_pnm, ifP, rasterPos,
2781
    doHistChunk(cmdline.hist, palette_pnm, ifP, rasterPos,
2784
                cols, rows, maxval, format,
2782
                cols, rows, maxval, format,
2785
                pngxP->info_ptr, cmdline.verbose);
2783
                pngxP, cmdline.verbose);
2786
  }
2784
  }
2787
2785
2788
  doTrnsChunk(pngxP, trans, trans_size,
2786
  doTrnsChunk(pngxP, trans, trans_size,
2789
              transparent, transcolor, maxval, png_maxval);
2787
              transparent, transcolor, maxval, png_maxval);
2790
2788
2791
  doBkgdChunk(!!cmdline.background, pngxP->info_ptr,
2789
  doBkgdChunk(!!cmdline.background, pngxP,
2792
              background_index, backcolor,
2790
              background_index, backcolor,
2793
              maxval, png_maxval, cmdline.verbose);
2791
              maxval, png_maxval, cmdline.verbose);
2794
2792
2795
  doSbitChunk(pngxP->info_ptr, png_maxval, maxval, alpha, alpha_maxval);
2793
  doSbitChunk(pngxP, png_maxval, maxval, alpha, alpha_maxval);
2796
2794
2797
  /* tEXT and zTXT chunks */
2795
  /* tEXT and zTXT chunks */
2798
  if (cmdline.text || cmdline.ztxt)
2796
  if (cmdline.text || cmdline.ztxt)
2799
      pnmpng_read_text(pngxP->info_ptr, tfP, !!cmdline.ztxt, cmdline.verbose);
2797
      pnmpng_read_text(pngxP->info_ptr, tfP, !!cmdline.ztxt, cmdline.verbose);
2800
2798
2801
  doTimeChunk(cmdline, pngxP->info_ptr);
2799
  doTimeChunk(cmdline, pngxP);
2802
2800
2803
  if (cmdline.filterSet != 0)
2801
  if (cmdline.filterSet != 0)
2804
      png_set_filter(pngxP->png_ptr, 0, cmdline.filterSet);
2802
      png_set_filter(pngxP->png_ptr, 0, cmdline.filterSet);
Lines 2809-2815 Link Here
2809
2807
2810
  /* write the png-info struct */
2808
  /* write the png-info struct */
2811
  png_write_info(pngxP->png_ptr, pngxP->info_ptr);
2809
  png_write_info(pngxP->png_ptr, pngxP->info_ptr);
2812
2810
#if 0
2811
  /* should no longer be necessary */
2813
  if (cmdline.text || cmdline.ztxt)
2812
  if (cmdline.text || cmdline.ztxt)
2814
      /* prevent from being written twice with png_write_end */
2813
      /* prevent from being written twice with png_write_end */
2815
      pngxP->info_ptr->num_text = 0;
2814
      pngxP->info_ptr->num_text = 0;
Lines 2817-2823 Link Here
2817
  if (cmdline.modtime)
2816
  if (cmdline.modtime)
2818
      /* prevent from being written twice with png_write_end */
2817
      /* prevent from being written twice with png_write_end */
2819
      pngxP->info_ptr->valid &= ~PNG_INFO_tIME;
2818
      pngxP->info_ptr->valid &= ~PNG_INFO_tIME;
2820
2819
#endif
2821
  /* let libpng take care of, e.g., bit-depth conversions */
2820
  /* let libpng take care of, e.g., bit-depth conversions */
2822
  png_set_packing(pngxP->png_ptr);
2821
  png_set_packing(pngxP->png_ptr);
2823
2822
(-)netpbm-10.51.00/converter/other/pngtopam.c.orig (-126 / +186 lines)
Lines 171-194 Link Here
171
171
172
static unsigned int
172
static unsigned int
173
computePngLineSize(struct pngx * const pngxP) {
173
computePngLineSize(struct pngx * const pngxP) {
174
175
    unsigned int const bytesPerSample =
174
    unsigned int const bytesPerSample =
176
        pngxP->info_ptr->bit_depth == 16 ? 2 : 1;
175
        png_get_bit_depth(pngxP->png_ptr, pngxP->info_ptr) == 16 ? 2 : 1;
177
176
178
    unsigned int samplesPerPixel;
177
    unsigned int samplesPerPixel;
178
    png_byte color_type = png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
179
    unsigned int width = png_get_image_width(pngxP->png_ptr, pngxP->info_ptr);
179
180
180
    switch (pngxP->info_ptr->color_type) {
181
    switch (color_type) {
181
    case PNG_COLOR_TYPE_GRAY_ALPHA: samplesPerPixel = 2; break;
182
    case PNG_COLOR_TYPE_GRAY_ALPHA: samplesPerPixel = 2; break;
182
    case PNG_COLOR_TYPE_RGB:        samplesPerPixel = 3; break;
183
    case PNG_COLOR_TYPE_RGB:        samplesPerPixel = 3; break;
183
    case PNG_COLOR_TYPE_RGB_ALPHA:  samplesPerPixel = 4; break;
184
    case PNG_COLOR_TYPE_RGB_ALPHA:  samplesPerPixel = 4; break;
184
    default:                        samplesPerPixel = 1;
185
    default:                        samplesPerPixel = 1;
185
    }
186
    }
186
187
187
    if (UINT_MAX / bytesPerSample / samplesPerPixel < pngxP->info_ptr->width)
188
    if (UINT_MAX / bytesPerSample / samplesPerPixel < width)
188
        pm_error("Width %u of PNG is uncomputably large",
189
        pm_error("Width %u of PNG is uncomputably large", width);
189
                 (unsigned int)pngxP->info_ptr->width);
190
       
190
       
191
    return pngxP->info_ptr->width * bytesPerSample * samplesPerPixel;
191
    return width * bytesPerSample * samplesPerPixel;
192
}
192
}
193
193
194
194
Lines 201-214 Link Here
201
201
202
    png_byte ** pngImage;
202
    png_byte ** pngImage;
203
    unsigned int row;
203
    unsigned int row;
204
    unsigned int height = png_get_image_height(pngxP->png_ptr, pngxP->info_ptr);
204
205
205
    MALLOCARRAY(pngImage, pngxP->info_ptr->height);
206
    MALLOCARRAY(pngImage, height);
206
207
207
    if (pngImage == NULL)
208
    if (pngImage == NULL)
208
        pm_error("couldn't allocate space for %u PNG raster rows",
209
        pm_error("couldn't allocate space for %u PNG raster rows", height);
209
                 (unsigned int)pngxP->info_ptr->height);
210
210
211
    for (row = 0; row < pngxP->info_ptr->height; ++row) {
211
    for (row = 0; row < height; ++row) {
212
        MALLOCARRAY(pngImage[row], lineSize);
212
        MALLOCARRAY(pngImage[row], lineSize);
213
        if (pngImage[row] == NULL)
213
        if (pngImage[row] == NULL)
214
            pm_error("couldn't allocate space for %uth row of PNG raster",
214
            pm_error("couldn't allocate space for %uth row of PNG raster",
Lines 225-231 Link Here
225
225
226
    unsigned int row;
226
    unsigned int row;
227
227
228
    for (row = 0; row < pngxP->info_ptr->height; ++row)
228
    for (row = 0; row < png_get_image_height(pngxP->png_ptr, pngxP->info_ptr); ++row)
229
        free(pngRaster[row]);
229
        free(pngRaster[row]);
230
230
231
    free(pngRaster);
231
    free(pngRaster);
Lines 252-258 Link Here
252
252
253
    allocPngRaster(pngxP, &pngRaster);
253
    allocPngRaster(pngxP, &pngRaster);
254
254
255
    if (pngxP->info_ptr->bit_depth < 8)
255
    if (png_get_bit_depth(pngxP->png_ptr, pngxP->info_ptr) < 8)
256
        png_set_packing(pngxP->png_ptr);
256
        png_set_packing(pngxP->png_ptr);
257
257
258
    png_read_image(pngxP->png_ptr, pngRaster);
258
    png_read_image(pngxP->png_ptr, pngRaster);
Lines 382-413 Link Here
382
saveText(struct pngx * const pngxP,
382
saveText(struct pngx * const pngxP,
383
         FILE *        const tfP) {
383
         FILE *        const tfP) {
384
384
385
    png_info * const info_ptr = pngxP->info_ptr;
386
387
    unsigned int i;
385
    unsigned int i;
386
    png_textp text;
387
    int num_text;
388
    png_get_text(pngxP->png_ptr, pngxP->info_ptr, &text, &num_text);
388
389
389
    for (i = 0 ; i < info_ptr->num_text; ++i) {
390
    for (i = 0 ; i < num_text; ++i) {
390
        unsigned int j;
391
        unsigned int j;
391
        j = 0;
392
        j = 0;
392
393
393
        while (info_ptr->text[i].key[j] != '\0' &&
394
        while (text[i].key[j] != '\0' &&
394
               info_ptr->text[i].key[j] != ' ')
395
               text[i].key[j] != ' ')
395
            ++j;    
396
            ++j;    
396
397
397
        if (info_ptr->text[i].key[j] != ' ') {
398
        if (text[i].key[j] != ' ') {
398
            fprintf(tfP, "%s", info_ptr->text[i].key);
399
            fprintf(tfP, "%s", text[i].key);
399
            for (j = strlen (info_ptr->text[i].key); j < 15; ++j)
400
            for (j = strlen (text[i].key); j < 15; ++j)
400
                putc(' ', tfP);
401
                putc(' ', tfP);
401
        } else {
402
        } else {
402
            fprintf(tfP, "\"%s\"", info_ptr->text[i].key);
403
            fprintf(tfP, "\"%s\"", text[i].key);
403
            for (j = strlen (info_ptr->text[i].key); j < 13; ++j)
404
            for (j = strlen (text[i].key); j < 13; ++j)
404
                putc(' ', tfP);
405
                putc(' ', tfP);
405
        }
406
        }
406
        putc(' ', tfP); /* at least one space between key and text */
407
        putc(' ', tfP); /* at least one space between key and text */
407
    
408
    
408
        for (j = 0; j < info_ptr->text[i].text_length; ++j) {
409
        for (j = 0; j < text[i].text_length; ++j) {
409
            putc(info_ptr->text[i].text[j], tfP);
410
            putc(text[i].text[j], tfP);
410
            if (info_ptr->text[i].text[j] == '\n') {
411
            if (text[i].text[j] == '\n') {
411
                unsigned int k;
412
                unsigned int k;
412
                for (k = 0; k < 16; ++k)
413
                for (k = 0; k < 16; ++k)
413
                    putc(' ', tfP);
414
                    putc(' ', tfP);
Lines 427-440 Link Here
427
        "July", "August", "September", "October", "November", "December"
428
        "July", "August", "September", "October", "November", "December"
428
    };
429
    };
429
430
430
    if (pngxP->info_ptr->valid & PNG_INFO_tIME) {
431
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_tIME)) {
432
        png_timep mod_time;
433
        png_get_tIME(pngxP->png_ptr, pngxP->info_ptr, &mod_time);
434
431
        pm_message("modification time: %02d %s %d %02d:%02d:%02d",
435
        pm_message("modification time: %02d %s %d %02d:%02d:%02d",
432
                   pngxP->info_ptr->mod_time.day,
436
                   mod_time->day,
433
                   month[pngxP->info_ptr->mod_time.month],
437
                   month[mod_time->month],
434
                   pngxP->info_ptr->mod_time.year,
438
                   mod_time->year,
435
                   pngxP->info_ptr->mod_time.hour,
439
                   mod_time->hour,
436
                   pngxP->info_ptr->mod_time.minute,
440
                   mod_time->minute,
437
                   pngxP->info_ptr->mod_time.second);
441
                   mod_time->second);
438
    }
442
    }
439
}
443
}
440
444
Lines 443-453 Link Here
443
static void
447
static void
444
dumpPngInfo(struct pngx * const pngxP) {
448
dumpPngInfo(struct pngx * const pngxP) {
445
449
446
    png_info * const info_ptr = pngxP->info_ptr;
447
    const char *type_string;
450
    const char *type_string;
448
    const char *filter_string;
451
    const char *filter_string;
452
    int color_type, filter_type, bit_depth;
453
    int interlace_type, filter_method, compression_type;
454
    png_color* palette = NULL;
455
    int num_palette = 0;
456
    png_color_16p bg;
457
    double gamma;
458
459
    unsigned int width = png_get_image_width(pngxP->png_ptr, pngxP->info_ptr);
460
    unsigned int height = png_get_image_height(pngxP->png_ptr, pngxP->info_ptr);
461
462
    png_get_PLTE(pngxP->png_ptr, pngxP->info_ptr, &palette, &num_palette);
463
    png_get_IHDR(pngxP->png_ptr, pngxP->info_ptr, NULL, NULL, &bit_depth,
464
            &color_type, &interlace_type, &compression_type, &filter_method);
465
    png_get_bKGD(pngxP->png_ptr, pngxP->info_ptr, &bg);
466
    png_get_gAMA(pngxP->png_ptr, pngxP->info_ptr, &gamma);
449
467
450
    switch (info_ptr->color_type) {
468
    switch (color_type) {
451
      case PNG_COLOR_TYPE_GRAY:
469
      case PNG_COLOR_TYPE_GRAY:
452
        type_string = "gray";
470
        type_string = "gray";
453
        break;
471
        break;
Lines 469-558 Link Here
469
        break;
487
        break;
470
    }
488
    }
471
489
472
    switch (info_ptr->filter_type) {
490
    switch (filter_type) {
473
    case PNG_FILTER_TYPE_BASE:
491
    case PNG_FILTER_TYPE_BASE:
474
        asprintfN(&filter_string, "base filter");
492
        asprintfN(&filter_string, "base filter");
475
        break;
493
        break;
476
    default:
494
    default:
477
        asprintfN(&filter_string, "unknown filter type %d", 
495
        asprintfN(&filter_string, "unknown filter type %d", 
478
                  info_ptr->filter_type);
496
                  filter_type);
479
    }
497
    }
480
498
481
    pm_message("reading a %ldw x %ldh image, %d bit%s",
499
    pm_message("reading a %uw x %uh image, %d bit%s",
482
               info_ptr->width, info_ptr->height,
500
               width, height,
483
               info_ptr->bit_depth, info_ptr->bit_depth > 1 ? "s" : "");
501
               bit_depth, bit_depth > 1 ? "s" : "");
484
    pm_message("%s, %s, %s",
502
    pm_message("%s, %s, %s",
485
               type_string,
503
               type_string,
486
               info_ptr->interlace_type ? 
504
               interlace_type ? 
487
               "Adam7 interlaced" : "not interlaced",
505
               "Adam7 interlaced" : "not interlaced",
488
               filter_string);
506
               filter_string);
489
    pm_message("background {index, gray, red, green, blue} = "
507
    pm_message("background {index, gray, red, green, blue} = "
490
               "{%d, %d, %d, %d, %d}",
508
               "{%d, %d, %d, %d, %d}",
491
               info_ptr->background.index,
509
               bg->index,
492
               info_ptr->background.gray,
510
               bg->gray,
493
               info_ptr->background.red,
511
               bg->red,
494
               info_ptr->background.green,
512
               bg->green,
495
               info_ptr->background.blue);
513
               bg->blue);
496
514
497
    strfree(filter_string);
515
    strfree(filter_string);
498
516
499
    if (info_ptr->valid & PNG_INFO_tRNS)
517
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_tRNS)) {
518
        png_bytep trans_alpha;
519
        png_color_16p trans_color;
520
        int num_trans;
521
        png_get_tRNS(pngxP->png_ptr, pngxP->info_ptr, &trans_alpha,
522
                        &num_trans, &trans_color);
500
        pm_message("tRNS chunk (transparency): %u entries",
523
        pm_message("tRNS chunk (transparency): %u entries",
501
                   info_ptr->num_trans);
524
                   num_trans);
502
    else
525
    } else
503
        pm_message("tRNS chunk (transparency): not present");
526
        pm_message("tRNS chunk (transparency): not present");
504
527
505
    if (info_ptr->valid & PNG_INFO_gAMA)
528
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_gAMA))
506
        pm_message("gAMA chunk (image gamma): gamma = %4.2f", info_ptr->gamma);
529
        pm_message("gAMA chunk (image gamma): gamma = %4.2f", gamma);
507
    else
530
    else
508
        pm_message("gAMA chunk (image gamma): not present");
531
        pm_message("gAMA chunk (image gamma): not present");
509
532
510
    if (info_ptr->valid & PNG_INFO_sBIT)
533
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_sBIT))
511
        pm_message("sBIT chunk: present");
534
        pm_message("sBIT chunk: present");
512
    else
535
    else
513
        pm_message("sBIT chunk: not present");
536
        pm_message("sBIT chunk: not present");
514
537
515
    if (info_ptr->valid & PNG_INFO_cHRM)
538
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_cHRM))
516
        pm_message("cHRM chunk: present");
539
        pm_message("cHRM chunk: present");
517
    else
540
    else
518
        pm_message("cHRM chunk: not present");
541
        pm_message("cHRM chunk: not present");
519
542
520
    if (info_ptr->valid & PNG_INFO_PLTE)
543
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_PLTE))
521
        pm_message("PLTE chunk: %d entries", info_ptr->num_palette);
544
        pm_message("PLTE chunk: %d entries", num_palette);
522
    else
545
    else
523
        pm_message("PLTE chunk: not present");
546
        pm_message("PLTE chunk: not present");
524
547
525
    if (info_ptr->valid & PNG_INFO_bKGD)
548
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_bKGD))
526
        pm_message("bKGD chunk: present");
549
        pm_message("bKGD chunk: present");
527
    else
550
    else
528
        pm_message("bKGD chunk: not present");
551
        pm_message("bKGD chunk: not present");
529
552
530
    if (info_ptr->valid & PNG_INFO_PLTE)
553
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_hIST))
531
        pm_message("hIST chunk: present");
554
        pm_message("hIST chunk: present");
532
    else
555
    else
533
        pm_message("hIST chunk: not present");
556
        pm_message("hIST chunk: not present");
534
557
535
    if (info_ptr->valid & PNG_INFO_pHYs)
558
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_pHYs))
536
        pm_message("pHYs chunk: present");
559
        pm_message("pHYs chunk: present");
537
    else
560
    else
538
        pm_message("pHYs chunk: not present");
561
        pm_message("pHYs chunk: not present");
539
562
540
    if (info_ptr->valid & PNG_INFO_oFFs)
563
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_oFFs))
541
        pm_message("oFFs chunk: present");
564
        pm_message("oFFs chunk: present");
542
    else
565
    else
543
        pm_message("oFFs chunk: not present");
566
        pm_message("oFFs chunk: not present");
544
567
545
    if (info_ptr->valid & PNG_INFO_tIME)
568
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_tIME))
546
        pm_message("tIME chunk: present");
569
        pm_message("tIME chunk: present");
547
    else
570
    else
548
        pm_message("tIME chunk: not present");
571
        pm_message("tIME chunk: not present");
549
572
550
    if (info_ptr->valid & PNG_INFO_pCAL)
573
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_pCAL))
551
        pm_message("pCAL chunk: present");
574
        pm_message("pCAL chunk: present");
552
    else
575
    else
553
        pm_message("pCAL chunk: not present");
576
        pm_message("pCAL chunk: not present");
554
577
555
    if (info_ptr->valid & PNG_INFO_sRGB)
578
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_sRGB))
556
        pm_message("sRGB chunk: present");
579
        pm_message("sRGB chunk: present");
557
    else
580
    else
558
        pm_message("sRGB chunk: not present");
581
        pm_message("sRGB chunk: not present");
Lines 606-612 Link Here
606
           pixels, and just do it ourselves.
629
           pixels, and just do it ourselves.
607
        */
630
        */
608
    
631
    
609
        switch (pngxP->info_ptr->color_type) {
632
        switch (png_get_color_type(pngxP->png_ptr, pngxP->info_ptr)) {
610
        case PNG_COLOR_TYPE_GRAY:
633
        case PNG_COLOR_TYPE_GRAY:
611
            retval = color.r == gammaCorrect(transColorP->gray, totalgamma);
634
            retval = color.r == gammaCorrect(transColorP->gray, totalgamma);
612
            break;
635
            break;
Lines 633-641 Link Here
633
        *totalgammaP = -1.0;
656
        *totalgammaP = -1.0;
634
    else {
657
    else {
635
        float imageGamma;
658
        float imageGamma;
636
        if (pngxP->info_ptr->valid & PNG_INFO_gAMA)
659
        if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_gAMA)) {
637
            imageGamma = pngxP->info_ptr->gamma;
660
            double gamma;
638
        else {
661
            png_get_gAMA(pngxP->png_ptr, pngxP->info_ptr, &gamma);
662
            imageGamma = gamma;
663
        } else {
639
            if (verbose)
664
            if (verbose)
640
                pm_message("PNG doesn't specify image gamma.  Assuming 1.0");
665
                pm_message("PNG doesn't specify image gamma.  Assuming 1.0");
641
            imageGamma = 1.0;
666
            imageGamma = 1.0;
Lines 652-659 Link Here
652
            *totalgammaP = imageGamma * displaygamma;
677
            *totalgammaP = imageGamma * displaygamma;
653
            /* in case of gamma-corrections, sBIT's as in the
678
            /* in case of gamma-corrections, sBIT's as in the
654
               PNG-file are not valid anymore 
679
               PNG-file are not valid anymore 
655
            */
680
681
               -- TODO: Will need to find another way of doing this. --
656
            pngxP->info_ptr->valid &= ~PNG_INFO_sBIT;
682
            pngxP->info_ptr->valid &= ~PNG_INFO_sBIT;
683
            */
657
            if (verbose)
684
            if (verbose)
658
                pm_message("image gamma is %4.2f, "
685
                pm_message("image gamma is %4.2f, "
659
                           "converted for display gamma of %4.2f",
686
                           "converted for display gamma of %4.2f",
Lines 669-675 Link Here
669
696
670
    bool retval;
697
    bool retval;
671
698
672
    if (pngxP->info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
699
    if (png_get_color_type(pngxP->png_ptr, pngxP->info_ptr)
700
                    == PNG_COLOR_TYPE_PALETTE) {
673
        if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) {
701
        if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) {
674
            bool foundGray;
702
            bool foundGray;
675
            unsigned int i;
703
            unsigned int i;
Lines 703-723 Link Here
703
                   png_byte *    const fgSbitP,
731
                   png_byte *    const fgSbitP,
704
                   bool *        const notUniformP) {
732
                   bool *        const notUniformP) {
705
733
706
    png_info * const pngInfoP = pngxP->info_ptr;
734
    png_color_8p sig_bit;
735
    png_byte color_type = png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
736
737
    png_get_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sig_bit);
707
738
708
    if (pngInfoP->color_type == PNG_COLOR_TYPE_RGB ||
739
    if (color_type == PNG_COLOR_TYPE_RGB ||
709
        pngInfoP->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
740
        color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
710
        pngInfoP->color_type == PNG_COLOR_TYPE_PALETTE) {
741
        color_type == PNG_COLOR_TYPE_PALETTE) {
711
        if (pngInfoP->sig_bit.red == pngInfoP->sig_bit.blue &&
742
        if (sig_bit->red == sig_bit->blue &&
712
            pngInfoP->sig_bit.red == pngInfoP->sig_bit.green) {
743
            sig_bit->red == sig_bit->green) {
713
            *notUniformP = false;
744
            *notUniformP = false;
714
            *fgSbitP     = pngInfoP->sig_bit.red;
745
            *fgSbitP     = sig_bit->red;
715
        } else
746
        } else
716
            *notUniformP = true;
747
            *notUniformP = true;
717
    } else {
748
    } else {
718
        /* It has only a gray channel so it's obviously uniform */
749
        /* It has only a gray channel so it's obviously uniform */
719
        *notUniformP = false;
750
        *notUniformP = false;
720
        *fgSbitP     = pngInfoP->sig_bit.gray;
751
        *fgSbitP     = sig_bit->gray;
721
    }
752
    }
722
}
753
}
723
754
Lines 728-733 Link Here
728
                 enum alpha_handling const alphaHandling,
759
                 enum alpha_handling const alphaHandling,
729
                 png_byte *          const componentSbitP,
760
                 png_byte *          const componentSbitP,
730
                 bool *              const notUniformP) {
761
                 bool *              const notUniformP) {
762
    png_color_8p sig_bit;
763
    png_get_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sig_bit);
731
764
732
    switch (alphaHandling) {
765
    switch (alphaHandling) {
733
766
Lines 736-742 Link Here
736
           the alpha Sbit
769
           the alpha Sbit
737
        */
770
        */
738
        *notUniformP = false;
771
        *notUniformP = false;
739
        *componentSbitP = pngxP->info_ptr->sig_bit.alpha;
772
        *componentSbitP = sig_bit->alpha;
740
        break;
773
        break;
741
    case ALPHA_NONE:
774
    case ALPHA_NONE:
742
    case ALPHA_MIX:
775
    case ALPHA_MIX:
Lines 755-761 Link Here
755
        if (fgNotUniform)
788
        if (fgNotUniform)
756
            *notUniformP = true;
789
            *notUniformP = true;
757
        else {
790
        else {
758
            if (fgSbit == pngxP->info_ptr->sig_bit.alpha) {
791
            if (fgSbit == sig_bit->alpha) {
759
                *notUniformP    = false;
792
                *notUniformP    = false;
760
                *componentSbitP = fgSbit;
793
                *componentSbitP = fgSbit;
761
            } else
794
            } else
Lines 781-791 Link Here
781
                 shift);
814
                 shift);
782
    else {
815
    else {
783
        unsigned int i;
816
        unsigned int i;
784
        
817
        png_color* palette = NULL;
785
        for (i = 0; i < pngxP->info_ptr->num_palette; ++i) {
818
        int num_palette = 0;
786
            pngxP->info_ptr->palette[i].red   >>= (8 - shift);
819
787
            pngxP->info_ptr->palette[i].green >>= (8 - shift);
820
        png_get_PLTE(pngxP->png_ptr, pngxP->info_ptr, &palette, &num_palette);
788
            pngxP->info_ptr->palette[i].blue  >>= (8 - shift);
821
822
        for (i = 0; i < num_palette; ++i) {
823
            palette[i].red   >>= (8 - shift);
824
            palette[i].green >>= (8 - shift);
825
            palette[i].blue  >>= (8 - shift);
789
        }
826
        }
790
    }
827
    }
791
}
828
}
Lines 817-822 Link Here
817
        /* The number of high-order significant bits in each RGB component.
854
        /* The number of high-order significant bits in each RGB component.
818
           Meaningless if they aren't all the same (i.e. 'notUniform')
855
           Meaningless if they aren't all the same (i.e. 'notUniform')
819
        */
856
        */
857
    png_byte color_type = png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
858
    png_byte bit_depth = png_get_bit_depth(pngxP->png_ptr, pngxP->info_ptr);
820
859
821
    getComponentSbit(pngxP, alphaHandling, &componentSigBit, &notUniform);
860
    getComponentSbit(pngxP, alphaHandling, &componentSigBit, &notUniform);
822
861
Lines 824-830 Link Here
824
        pm_message("This program cannot handle "
863
        pm_message("This program cannot handle "
825
                   "different bit depths for color channels");
864
                   "different bit depths for color channels");
826
        pm_message("writing file with %u bit resolution",
865
        pm_message("writing file with %u bit resolution",
827
                   pngxP->info_ptr->bit_depth);
866
                   bit_depth);
828
        *succeededP = false;
867
        *succeededP = false;
829
        *errorLevelP = PNMTOPNG_WARNING_LEVEL;
868
        *errorLevelP = PNMTOPNG_WARNING_LEVEL;
830
    } else if (componentSigBit > 15) {
869
    } else if (componentSigBit > 15) {
Lines 835-855 Link Here
835
        *errorLevelP = PNMTOPNG_WARNING_LEVEL;
874
        *errorLevelP = PNMTOPNG_WARNING_LEVEL;
836
    } else {
875
    } else {
837
        if (alphaHandling == ALPHA_MIX &&
876
        if (alphaHandling == ALPHA_MIX &&
838
            (pngxP->info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
877
            (color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
839
             pngxP->info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
878
             color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
840
             paletteHasPartialTransparency(pngxP)))
879
             paletteHasPartialTransparency(pngxP)))
841
            *succeededP = false;
880
            *succeededP = false;
842
        else {
881
        else {
843
            if (componentSigBit < pngxP->info_ptr->bit_depth) {
882
            if (componentSigBit < bit_depth) {
844
                pm_message("Image has fewer significant bits, "
883
                pm_message("Image has fewer significant bits, "
845
                           "writing file with %u bits", componentSigBit);
884
                           "writing file with %u bits", componentSigBit);
846
                *maxvalP = (1l << componentSigBit) - 1;
885
                *maxvalP = (1l << componentSigBit) - 1;
847
                *succeededP = true;
886
                *succeededP = true;
848
887
849
                if (pngxP->info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
888
                if (color_type == PNG_COLOR_TYPE_PALETTE)
850
                    shiftPalette(pngxP, componentSigBit);
889
                    shiftPalette(pngxP, componentSigBit);
851
                else
890
                else {
852
                    png_set_shift(pngxP->png_ptr, &pngxP->info_ptr->sig_bit);
891
                    png_color_8p sig_bit;
892
893
                    if (png_get_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sig_bit))
894
                            png_set_shift(pngxP->png_ptr, sig_bit);
895
                }
853
            } else
896
            } else
854
                *succeededP = false;
897
                *succeededP = false;
855
        }
898
        }
Lines 872-892 Link Here
872
915
873
  Also set up *pngxP for the corresponding significant bits.
916
  Also set up *pngxP for the corresponding significant bits.
874
-----------------------------------------------------------------------------*/
917
-----------------------------------------------------------------------------*/
875
    png_info * const pngInfoP = pngxP->info_ptr;
876
877
    bool gotItFromSbit;
918
    bool gotItFromSbit;
919
    png_byte color_type = png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
878
    
920
    
879
    if (pngInfoP->valid & PNG_INFO_sBIT)
921
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_sBIT))
880
        computeMaxvalFromSbit(pngxP, alphaHandling,
922
        computeMaxvalFromSbit(pngxP, alphaHandling,
881
                              maxvalP, &gotItFromSbit, errorLevelP);
923
                              maxvalP, &gotItFromSbit, errorLevelP);
882
    else
924
    else
883
        gotItFromSbit = false;
925
        gotItFromSbit = false;
884
926
885
    if (!gotItFromSbit) {
927
    if (!gotItFromSbit) {
886
        if (pngInfoP->color_type == PNG_COLOR_TYPE_PALETTE) {
928
        if (color_type == PNG_COLOR_TYPE_PALETTE) {
887
            if (alphaHandling == ALPHA_ONLY) {
929
            if (alphaHandling == ALPHA_ONLY) {
888
                if (pngInfoP->color_type == PNG_COLOR_TYPE_GRAY ||
930
                if (color_type == PNG_COLOR_TYPE_GRAY ||
889
                    pngInfoP->color_type == PNG_COLOR_TYPE_RGB)
931
                    color_type == PNG_COLOR_TYPE_RGB)
890
                    /* The alpha mask will be all opaque, so maxval 1
932
                    /* The alpha mask will be all opaque, so maxval 1
891
                       is plenty
933
                       is plenty
892
                    */
934
                    */
Lines 903-909 Link Here
903
                /* Use same maxval as PNG palette for simplicity */
945
                /* Use same maxval as PNG palette for simplicity */
904
                *maxvalP = 255;
946
                *maxvalP = 255;
905
        } else {
947
        } else {
906
            *maxvalP = (1l << pngInfoP->bit_depth) - 1;
948
            *maxvalP = (1l << png_get_bit_depth(pngxP->png_ptr,
949
                                    pngxP->info_ptr)) - 1;
907
        }
950
        }
908
    }
951
    }
909
}
952
}
Lines 914-932 Link Here
914
imageHasColor(struct pngx * const pngxP) {
957
imageHasColor(struct pngx * const pngxP) {
915
958
916
    bool retval;
959
    bool retval;
960
    png_byte color_type = png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
917
961
918
    if (pngxP->info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
962
    if (color_type == PNG_COLOR_TYPE_GRAY ||
919
        pngxP->info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
963
        color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
920
964
921
        retval = FALSE;
965
        retval = FALSE;
922
    else if (pngxP->info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
966
    else if (color_type == PNG_COLOR_TYPE_PALETTE) {
923
        bool foundColor;
967
        bool foundColor;
924
        unsigned int i;
968
        unsigned int i;
925
            
969
        png_color* palette = NULL;
970
        int num_palette = 0;
971
972
        png_get_PLTE(pngxP->png_ptr, pngxP->info_ptr, &palette, &num_palette);
973
926
        for (i = 0, foundColor = FALSE;
974
        for (i = 0, foundColor = FALSE;
927
             i < pngxP->info_ptr->num_palette && !foundColor;
975
             i < num_palette && !foundColor;
928
             ++i) {
976
             ++i) {
929
            if (isColor(pngxP->info_ptr->palette[i]))
977
            if (isColor(palette[i]))
930
                foundColor = TRUE;
978
                foundColor = TRUE;
931
        }
979
        }
932
        retval = foundColor;
980
        retval = foundColor;
Lines 1002-1020 Link Here
1002
        bgColorP->g = PPM_GETG(backcolor);
1050
        bgColorP->g = PPM_GETG(backcolor);
1003
        bgColorP->b = PPM_GETB(backcolor);
1051
        bgColorP->b = PPM_GETB(backcolor);
1004
1052
1005
    } else if (pngxP->info_ptr->valid & PNG_INFO_bKGD) {
1053
    } else if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_bKGD)) {
1006
        /* didn't manage to get libpng to work (bugs?) concerning background
1054
        /* didn't manage to get libpng to work (bugs?) concerning background
1007
           processing, therefore we do our own.
1055
           processing, therefore we do our own.
1008
        */
1056
        */
1009
        switch (pngxP->info_ptr->color_type) {
1057
        png_color_16p bg;
1058
        png_get_bKGD(pngxP->png_ptr, pngxP->info_ptr, &bg);
1059
1060
        switch (png_get_color_type(pngxP->png_ptr, pngxP->info_ptr)) {
1010
        case PNG_COLOR_TYPE_GRAY:
1061
        case PNG_COLOR_TYPE_GRAY:
1011
        case PNG_COLOR_TYPE_GRAY_ALPHA:
1062
        case PNG_COLOR_TYPE_GRAY_ALPHA:
1012
            bgColorP->r = bgColorP->g = bgColorP->b = 
1063
            bgColorP->r = bgColorP->g = bgColorP->b = 
1013
                gammaCorrect(pngxP->info_ptr->background.gray, totalgamma);
1064
                gammaCorrect(bg->gray, totalgamma);
1014
            break;
1065
            break;
1015
        case PNG_COLOR_TYPE_PALETTE: {
1066
        case PNG_COLOR_TYPE_PALETTE: {
1016
            png_color const rawBgcolor = 
1067
            png_color* palette = NULL;
1017
                pngxP->info_ptr->palette[pngxP->info_ptr->background.index];
1068
            int num_palette = 0;
1069
            png_get_PLTE(pngxP->png_ptr, pngxP->info_ptr, &palette, &num_palette);
1070
1071
            png_color const rawBgcolor = palette[bg->index];
1018
            bgColorP->r = gammaCorrect(rawBgcolor.red, totalgamma);
1072
            bgColorP->r = gammaCorrect(rawBgcolor.red, totalgamma);
1019
            bgColorP->g = gammaCorrect(rawBgcolor.green, totalgamma);
1073
            bgColorP->g = gammaCorrect(rawBgcolor.green, totalgamma);
1020
            bgColorP->b = gammaCorrect(rawBgcolor.blue, totalgamma);
1074
            bgColorP->b = gammaCorrect(rawBgcolor.blue, totalgamma);
Lines 1022-1032 Link Here
1022
        break;
1076
        break;
1023
        case PNG_COLOR_TYPE_RGB:
1077
        case PNG_COLOR_TYPE_RGB:
1024
        case PNG_COLOR_TYPE_RGB_ALPHA: {
1078
        case PNG_COLOR_TYPE_RGB_ALPHA: {
1025
            png_color_16 const rawBgcolor = pngxP->info_ptr->background;
1079
            bgColorP->r = gammaCorrect(bg->red,   totalgamma);
1026
            
1080
            bgColorP->g = gammaCorrect(bg->green, totalgamma);
1027
            bgColorP->r = gammaCorrect(rawBgcolor.red,   totalgamma);
1081
            bgColorP->b = gammaCorrect(bg->blue,  totalgamma);
1028
            bgColorP->g = gammaCorrect(rawBgcolor.green, totalgamma);
1029
            bgColorP->b = gammaCorrect(rawBgcolor.blue,  totalgamma);
1030
        }
1082
        }
1031
        break;
1083
        break;
1032
        }
1084
        }
Lines 1041-1050 Link Here
1041
warnNonsquarePixels(struct pngx * const pngxP,
1093
warnNonsquarePixels(struct pngx * const pngxP,
1042
                    int *         const errorLevelP) {
1094
                    int *         const errorLevelP) {
1043
1095
1044
    if (pngxP->info_ptr->valid & PNG_INFO_pHYs) {
1096
    if (png_get_valid(pngxP->png_ptr, pngxP->info_ptr, PNG_INFO_pHYs)) {
1045
        float const r =
1097
        png_uint_32 xppu, yppu;
1046
            (float)pngxP->info_ptr->x_pixels_per_unit /
1098
        int unit;
1047
            pngxP->info_ptr->y_pixels_per_unit;
1099
        png_get_pHYs(pngxP->png_ptr, pngxP->info_ptr, &xppu, &yppu, &unit);
1100
1101
        float const r = (float)xppu / yppu;
1048
1102
1049
        if (r != 1.0) {
1103
        if (r != 1.0) {
1050
            pm_message ("warning - non-square pixels; "
1104
            pm_message ("warning - non-square pixels; "
Lines 1085-1091 Link Here
1085
1139
1086
1140
1087
1141
1088
#define GET_PNG_VAL(p) getPngVal(&(p), pngxP->info_ptr->bit_depth)
1142
#define GET_PNG_VAL(p) getPngVal(&(p), \
1143
                png_get_bit_depth(pngxP->png_ptr, pngxP->info_ptr))
1089
1144
1090
1145
1091
1146
Lines 1100-1109 Link Here
1100
1155
1101
    const png_byte * pngPixelP;
1156
    const png_byte * pngPixelP;
1102
    unsigned int col;
1157
    unsigned int col;
1158
    png_color* palette = NULL;
1159
    int num_palette = 0;
1160
    png_byte color_type = png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
1161
1162
    if (color_type == PNG_COLOR_TYPE_PALETTE)
1163
        png_get_PLTE(pngxP->png_ptr, pngxP->info_ptr, &palette, &num_palette);
1103
1164
1104
    pngPixelP = &pngRasterRow[0];  /* initial value */
1165
    pngPixelP = &pngRasterRow[0];  /* initial value */
1105
    for (col = 0; col < pngxP->info_ptr->width; ++col) {
1166
    for (col = 0; col < png_get_image_width(pngxP->png_ptr, pngxP->info_ptr); ++col) {
1106
        switch (pngxP->info_ptr->color_type) {
1167
        switch (color_type) {
1107
        case PNG_COLOR_TYPE_GRAY: {
1168
        case PNG_COLOR_TYPE_GRAY: {
1108
            pngcolor fgColor;
1169
            pngcolor fgColor;
1109
            fgColor.r = fgColor.g = fgColor.b = GET_PNG_VAL(pngPixelP);
1170
            fgColor.r = fgColor.g = fgColor.b = GET_PNG_VAL(pngPixelP);
Lines 1126-1132 Link Here
1126
1187
1127
        case PNG_COLOR_TYPE_PALETTE: {
1188
        case PNG_COLOR_TYPE_PALETTE: {
1128
            png_uint_16 const index        = GET_PNG_VAL(pngPixelP);
1189
            png_uint_16 const index        = GET_PNG_VAL(pngPixelP);
1129
            png_color   const paletteColor = pngxP->info_ptr->palette[index];
1190
            png_color   const paletteColor = palette[index];
1130
1191
1131
            pngcolor fgColor;
1192
            pngcolor fgColor;
1132
1193
Lines 1165-1172 Link Here
1165
        break;
1226
        break;
1166
1227
1167
        default:
1228
        default:
1168
            pm_error("unknown PNG color type: %d",
1229
            pm_error("unknown PNG color type: %d", color_type);
1169
                     pngxP->info_ptr->color_type);
1170
        }
1230
        }
1171
    }
1231
    }
1172
}
1232
}
Lines 1226-1232 Link Here
1226
1286
1227
    tuplerow = pnm_allocpamrow(pamP);
1287
    tuplerow = pnm_allocpamrow(pamP);
1228
1288
1229
    for (row = 0; row < pngxP->info_ptr->height; ++row) {
1289
    for (row = 0; row < png_get_image_height(pngxP->png_ptr, pngxP->info_ptr); ++row) {
1230
        makeTupleRow(pamP, tuplerow, pngxP, pngRaster[row], bgColor,
1290
        makeTupleRow(pamP, tuplerow, pngxP, pngRaster[row], bgColor,
1231
                     alphaHandling, totalgamma);
1291
                     alphaHandling, totalgamma);
1232
1292
Lines 1280-1287 Link Here
1280
    pam.len         = PAM_STRUCT_SIZE(maxval);
1340
    pam.len         = PAM_STRUCT_SIZE(maxval);
1281
    pam.file        = stdout;
1341
    pam.file        = stdout;
1282
    pam.plainformat = 0;
1342
    pam.plainformat = 0;
1283
    pam.height      = pngxP->info_ptr->height;
1343
    pam.height      = png_get_image_height(pngxP->png_ptr, pngxP->info_ptr);
1284
    pam.width       = pngxP->info_ptr->width;
1344
    pam.width       = png_get_image_width(pngxP->png_ptr, pngxP->info_ptr);
1285
    pam.maxval      = maxval;
1345
    pam.maxval      = maxval;
1286
1346
1287
    determineOutputType(pngxP, cmdline.alpha, bgColor, maxval,
1347
    determineOutputType(pngxP, cmdline.alpha, bgColor, maxval,
(-)netpbm-10.51.00/converter/other/pngtxt.h.orig (-1 / +2 lines)
Lines 3-11 Link Here
3
3
4
#include "pm_c_util.h"
4
#include "pm_c_util.h"
5
#include <png.h>
5
#include <png.h>
6
#include "pngx.h"
6
7
7
void 
8
void 
8
pnmpng_read_text (png_info * const info_ptr, 
9
pnmpng_read_text (struct pngx* const pngxP, 
9
                  FILE *     const tfp, 
10
                  FILE *     const tfp, 
10
                  bool const ztxt,
11
                  bool const ztxt,
11
                  bool const verbose);
12
                  bool const verbose);

Return to bug 355025