revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / png / contrib / examples / pngtopng.c
blob904b972382f408798976ecc8e1ea8561db114fe0
1 /*- pngtopng
3 * COPYRIGHT: Written by John Cunningham Bowler, 2011, 2017.
4 * To the extent possible under law, the author has waived all copyright and
5 * related or neighboring rights to this work. This work is published from:
6 * United States.
8 * Last changed in libpng 1.6.29 [March 16, 2017]
10 * Read a PNG and write it out in a fixed format, using the 'simplified API'
11 * that was introduced in libpng-1.6.0.
13 * This sample code is just the code from the top of 'example.c' with some error
14 * handling added. See example.c for more comments.
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
21 /* Normally use <png.h> here to get the installed libpng, but this is done to
22 * ensure the code picks up the local libpng implementation:
24 #include "../../png.h"
25 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && \
26 defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
28 int main(int argc, const char **argv)
30 int result = 1;
32 if (argc == 3)
34 png_image image;
36 /* Only the image structure version number needs to be set. */
37 memset(&image, 0, sizeof image);
38 image.version = PNG_IMAGE_VERSION;
40 if (png_image_begin_read_from_file(&image, argv[1]))
42 png_bytep buffer;
44 /* Change this to try different formats! If you set a colormap format
45 * then you must also supply a colormap below.
47 image.format = PNG_FORMAT_RGBA;
49 buffer = malloc(PNG_IMAGE_SIZE(image));
51 if (buffer != NULL)
53 if (png_image_finish_read(&image, NULL/*background*/, buffer,
54 0/*row_stride*/, NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP */))
56 if (png_image_write_to_file(&image, argv[2],
57 0/*convert_to_8bit*/, buffer, 0/*row_stride*/,
58 NULL/*colormap*/))
59 result = 0;
61 else
62 fprintf(stderr, "pngtopng: write %s: %s\n", argv[2],
63 image.message);
66 else
67 fprintf(stderr, "pngtopng: read %s: %s\n", argv[1],
68 image.message);
70 free(buffer);
73 else
75 fprintf(stderr, "pngtopng: out of memory: %lu bytes\n",
76 (unsigned long)PNG_IMAGE_SIZE(image));
78 /* This is the only place where a 'free' is required; libpng does
79 * the cleanup on error and success, but in this case we couldn't
80 * complete the read because of running out of memory and so libpng
81 * has not got to the point where it can do cleanup.
83 png_image_free(&image);
87 else
88 /* Failed to read the first argument: */
89 fprintf(stderr, "pngtopng: %s: %s\n", argv[1], image.message);
92 else
93 /* Wrong number of arguments */
94 fprintf(stderr, "pngtopng: usage: pngtopng input-file output-file\n");
96 return result;
98 #endif /* READ && WRITE */