1 | /*- pngtopng
|
---|
2 | *
|
---|
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.
|
---|
7 | *
|
---|
8 | * Read a PNG and write it out in a fixed format, using the 'simplified API'
|
---|
9 | * that was introduced in libpng-1.6.0.
|
---|
10 | *
|
---|
11 | * This sample code is just the code from the top of 'example.c' with some error
|
---|
12 | * handling added. See example.c for more comments.
|
---|
13 | */
|
---|
14 | #include <stddef.h>
|
---|
15 | #include <stdlib.h>
|
---|
16 | #include <string.h>
|
---|
17 | #include <stdio.h>
|
---|
18 |
|
---|
19 | /* Normally use <png.h> here to get the installed libpng, but this is done to
|
---|
20 | * ensure the code picks up the local libpng implementation:
|
---|
21 | */
|
---|
22 | #include "../../png.h"
|
---|
23 | #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && \
|
---|
24 | defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
|
---|
25 |
|
---|
26 | int main(int argc, const char **argv)
|
---|
27 | {
|
---|
28 | int result = 1;
|
---|
29 |
|
---|
30 | if (argc == 3)
|
---|
31 | {
|
---|
32 | png_image image;
|
---|
33 |
|
---|
34 | /* Only the image structure version number needs to be set. */
|
---|
35 | memset(&image, 0, sizeof image);
|
---|
36 | image.version = PNG_IMAGE_VERSION;
|
---|
37 |
|
---|
38 | if (png_image_begin_read_from_file(&image, argv[1]))
|
---|
39 | {
|
---|
40 | png_bytep buffer;
|
---|
41 |
|
---|
42 | /* Change this to try different formats! If you set a colormap format
|
---|
43 | * then you must also supply a colormap below.
|
---|
44 | */
|
---|
45 | image.format = PNG_FORMAT_RGBA;
|
---|
46 |
|
---|
47 | buffer = malloc(PNG_IMAGE_SIZE(image));
|
---|
48 |
|
---|
49 | if (buffer != NULL)
|
---|
50 | {
|
---|
51 | if (png_image_finish_read(&image, NULL/*background*/, buffer,
|
---|
52 | 0/*row_stride*/, NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP */))
|
---|
53 | {
|
---|
54 | if (png_image_write_to_file(&image, argv[2],
|
---|
55 | 0/*convert_to_8bit*/, buffer, 0/*row_stride*/,
|
---|
56 | NULL/*colormap*/))
|
---|
57 | result = 0;
|
---|
58 |
|
---|
59 | else
|
---|
60 | fprintf(stderr, "pngtopng: write %s: %s\n", argv[2],
|
---|
61 | image.message);
|
---|
62 | }
|
---|
63 |
|
---|
64 | else
|
---|
65 | fprintf(stderr, "pngtopng: read %s: %s\n", argv[1],
|
---|
66 | image.message);
|
---|
67 |
|
---|
68 | free(buffer);
|
---|
69 | }
|
---|
70 |
|
---|
71 | else
|
---|
72 | {
|
---|
73 | fprintf(stderr, "pngtopng: out of memory: %lu bytes\n",
|
---|
74 | (unsigned long)PNG_IMAGE_SIZE(image));
|
---|
75 |
|
---|
76 | /* This is the only place where a 'free' is required; libpng does
|
---|
77 | * the cleanup on error and success, but in this case we couldn't
|
---|
78 | * complete the read because of running out of memory and so libpng
|
---|
79 | * has not got to the point where it can do cleanup.
|
---|
80 | */
|
---|
81 | png_image_free(&image);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | else
|
---|
86 | /* Failed to read the first argument: */
|
---|
87 | fprintf(stderr, "pngtopng: %s: %s\n", argv[1], image.message);
|
---|
88 | }
|
---|
89 |
|
---|
90 | else
|
---|
91 | /* Wrong number of arguments */
|
---|
92 | fprintf(stderr, "pngtopng: usage: pngtopng input-file output-file\n");
|
---|
93 |
|
---|
94 | return result;
|
---|
95 | }
|
---|
96 | #endif /* READ && WRITE */
|
---|