2011-11-24 09:22:24 -05:00
|
|
|
/*- pngtopng
|
|
|
|
*
|
2011-11-25 08:27:51 -05:00
|
|
|
* COPYRIGHT: Written by John Cunningham Bowler, 2011.
|
|
|
|
* To the extent possible under law, the author has waived all copyright and
|
|
|
|
* related or neighboring rights to this work. This work is published from:
|
|
|
|
* United States.
|
2011-11-24 09:22:24 -05:00
|
|
|
*
|
2011-11-27 22:39:13 -05:00
|
|
|
* Read a PNG and write it out in a fixed format, using the 'simplified API'
|
|
|
|
* that was introduced in libpng-1.6.0.
|
2011-11-24 09:22:24 -05:00
|
|
|
*
|
|
|
|
* This sample code is just the code from the top of 'example.c' with some error
|
|
|
|
* handling added. See example.c for more comments.
|
|
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* Normally use <png.h> here to get the installed libpng, but this is done to
|
|
|
|
* ensure the code picks up the local libpng implementation:
|
|
|
|
*/
|
|
|
|
#include "../../png.h"
|
|
|
|
|
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
int result = 1;
|
|
|
|
|
|
|
|
if (argc == 3)
|
|
|
|
{
|
|
|
|
png_image image;
|
|
|
|
|
|
|
|
memset(&image, 0, sizeof image);
|
|
|
|
|
|
|
|
if (png_image_begin_read_from_file(&image, argv[1]))
|
|
|
|
{
|
|
|
|
png_bytep buffer;
|
2012-03-02 14:13:15 -05:00
|
|
|
|
2011-11-24 09:22:24 -05:00
|
|
|
/* Change this to try different formats! */
|
|
|
|
image.format = PNG_FORMAT_RGBA;
|
|
|
|
|
|
|
|
buffer = malloc(PNG_IMAGE_SIZE(image));
|
|
|
|
|
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
|
|
|
if (png_image_finish_read(&image, NULL/*background*/, buffer,
|
|
|
|
0/*row_stride*/))
|
|
|
|
{
|
|
|
|
if (png_image_write_to_file(&image, argv[2],
|
|
|
|
0/*convert_to_8bit*/, buffer, 0/*row_stride*/))
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
else
|
|
|
|
fprintf(stderr, "pngtopng: write %s: %s\n", argv[2],
|
2011-11-27 22:39:13 -05:00
|
|
|
image.message);
|
|
|
|
|
|
|
|
free(buffer);
|
2011-11-24 09:22:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2011-11-27 22:39:13 -05:00
|
|
|
{
|
|
|
|
fprintf(stderr, "pngtopng: read %s: %s\n", argv[1],
|
|
|
|
image.message);
|
|
|
|
|
|
|
|
/* This is the only place where a 'free' is required; libpng does
|
|
|
|
* the cleanup on error and success, but in this case we couldn't
|
|
|
|
* complete the read because of running out of memory.
|
|
|
|
*/
|
|
|
|
png_image_free(&image);
|
|
|
|
}
|
2011-11-24 09:22:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
fprintf(stderr, "pngtopng: out of memory: %lu bytes\n",
|
|
|
|
(unsigned long)PNG_IMAGE_SIZE(image));
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
/* Failed to read the first argument: */
|
|
|
|
fprintf(stderr, "pngtopng: %s: %s\n", argv[1], image.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
/* Wrong number of arguments */
|
|
|
|
fprintf(stderr, "pngtopng: usage: pngtopng input-file output-file\n");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|