| Make PBM actually work. Before it obviously did not since it |
| did not take width or height values. libpng takes bitmaps in |
| a packed format (I added a call to png_set_packing() but this |
| is actually the default), which does not require any special |
| processing apart from inverting the image white-to-black. |
| |
| While here, initialize dimension values so if sscanf fails |
| at least we have known invalid values. |
| |
| |
| --- libpng-1.2.49/contrib/pngminus/pnm2png.c.orig 2013-05-07 13:01:10.385847623 -0700 |
| +++ libpng-1.2.49/contrib/pngminus/pnm2png.c 2013-05-07 18:39:30.821466525 -0700 |
| @@ -197,15 +197,16 @@ |
| char height_token[16]; |
| char maxval_token[16]; |
| int color_type; |
| - png_uint_32 width, alpha_width; |
| - png_uint_32 height, alpha_height; |
| - png_uint_32 maxval; |
| + png_uint_32 width = 0, alpha_width = 0; |
| + png_uint_32 height = 0, alpha_height = 0; |
| + png_uint_32 maxval = 0; |
| int bit_depth = 0; |
| int channels; |
| int alpha_depth = 0; |
| int alpha_present; |
| int row, col; |
| BOOL raw, alpha_raw = FALSE; |
| + BOOL packed_bitmap = FALSE; |
| png_uint_32 tmp16; |
| int i; |
| |
| @@ -220,7 +221,12 @@ |
| { |
| raw = (type_token[1] == '4'); |
| color_type = PNG_COLOR_TYPE_GRAY; |
| + get_token(pnm_file, width_token); |
| + sscanf (width_token, "%lu", &width); |
| + get_token(pnm_file, height_token); |
| + sscanf (height_token, "%lu", &height); |
| bit_depth = 1; |
| + packed_bitmap = TRUE; |
| } |
| else if ((type_token[1] == '2') || (type_token[1] == '5')) |
| { |
| @@ -329,8 +335,12 @@ |
| |
| alpha_present = (channels - 1) % 2; |
| |
| - /* row_bytes is the width x number of channels x (bit-depth / 8) */ |
| - row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2); |
| + if (packed_bitmap) |
| + /* row data is as many bytes as can fit width x channels x bit_depth */ |
| + row_bytes = (width * channels * bit_depth + 7) / 8; |
| + else |
| + /* row_bytes is the width x number of channels x (bit-depth / 8) */ |
| + row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2); |
| |
| if ((png_pixels = (png_byte *) malloc (row_bytes * height * sizeof (png_byte))) == NULL) |
| return FALSE; |
| @@ -340,40 +350,45 @@ |
| |
| for (row = 0; row < height; row++) |
| { |
| - for (col = 0; col < width; col++) |
| - { |
| - for (i = 0; i < (channels - alpha_present); i++) |
| + if (packed_bitmap) { |
| + for (i = 0; i < row_bytes; i++) |
| + /* png supports this format natively so no conversion is needed */ |
| + *pix_ptr++ = get_data (pnm_file, 8); |
| + } else { |
| + for (col = 0; col < width; col++) |
| { |
| - if (raw) |
| - *pix_ptr++ = get_data (pnm_file, bit_depth); |
| - else |
| - if (bit_depth <= 8) |
| - *pix_ptr++ = get_value (pnm_file, bit_depth); |
| + for (i = 0; i < (channels - alpha_present); i++) |
| + { |
| + if (raw) |
| + *pix_ptr++ = get_data (pnm_file, bit_depth); |
| else |
| - { |
| - tmp16 = get_value (pnm_file, bit_depth); |
| - *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF); |
| - pix_ptr++; |
| - *pix_ptr = (png_byte) (tmp16 & 0xFF); |
| - pix_ptr++; |
| - } |
| - } |
| - |
| - if (alpha) /* read alpha-channel from pgm file */ |
| - { |
| - if (alpha_raw) |
| - *pix_ptr++ = get_data (alpha_file, alpha_depth); |
| - else |
| - if (alpha_depth <= 8) |
| - *pix_ptr++ = get_value (alpha_file, bit_depth); |
| + if (bit_depth <= 8) |
| + *pix_ptr++ = get_value (pnm_file, bit_depth); |
| + else |
| + { |
| + tmp16 = get_value (pnm_file, bit_depth); |
| + *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF); |
| + pix_ptr++; |
| + *pix_ptr = (png_byte) (tmp16 & 0xFF); |
| + pix_ptr++; |
| + } |
| + } |
| + |
| + if (alpha) /* read alpha-channel from pgm file */ |
| + { |
| + if (alpha_raw) |
| + *pix_ptr++ = get_data (alpha_file, alpha_depth); |
| else |
| - { |
| - tmp16 = get_value (alpha_file, bit_depth); |
| - *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF); |
| - *pix_ptr++ = (png_byte) (tmp16 & 0xFF); |
| - } |
| - } /* if alpha */ |
| - |
| + if (alpha_depth <= 8) |
| + *pix_ptr++ = get_value (alpha_file, bit_depth); |
| + else |
| + { |
| + tmp16 = get_value (alpha_file, bit_depth); |
| + *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF); |
| + *pix_ptr++ = (png_byte) (tmp16 & 0xFF); |
| + } |
| + } /* if alpha */ |
| + } /* if packed_bitmap */ |
| } /* end for col */ |
| } /* end for row */ |
| |
| @@ -390,6 +405,12 @@ |
| return FALSE; |
| } |
| |
| + if (packed_bitmap == TRUE) |
| + { |
| + png_set_packing (png_ptr); |
| + png_set_invert_mono (png_ptr); |
| + } |
| + |
| /* setjmp() must be called in every function that calls a PNG-reading libpng function */ |
| if (setjmp (png_jmpbuf(png_ptr))) |
| { |