-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppm_to_png.c
230 lines (190 loc) · 4.34 KB
/
ppm_to_png.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Program
* This program converts a PPM file to PPM format.
*
* Compile
* gcc -Wall -Wextra ppm_to_png.c -o ppm_to_png -lpng
*
* Execution
* ./ppm_to_png --input test.ppm --output output.png
**/
#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
/* #define PNG_DEBUG 3 */
#include <png.h>
static const char short_options[] = "hi:o:";
static const struct option long_options[] =
{
{ "help", no_argument, NULL, 'h' },
{ "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o' },
{ 0, 0, 0, 0 }
};
/* Show program usage */
static void usage(int argc, char **argv)
{
printf(
"\n"
"Usage: %s [options]\n"
" Options:\n"
" -h | --help Show help message\n"
" -i | --input Source PPM image\n"
" -o | --output Output PNG image\n"
"\n",
argv[0]
);
(void)argc;
}
/**
* Entry function
*/
int main(int argc, char *argv[])
{
int err = 0;
int opt;
int option_index;
char *input;
char *output;
printf("Convert PPM file to PNG\n");
for (;;) {
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
if (opt == -1)
break;
switch (opt) {
case 'i':
input = optarg;
break;
case 'o':
output = optarg;
break;
case 'h':
default:
usage(argc, argv);
exit(EXIT_FAILURE);
break;
}
}
printf("Input file: %s\n", input);
printf("Output file: %s\n", output);
/* Open file */
FILE *fip = fopen(input, "rb");
if (!fip) {
printf("Unable to open input file(%s)\n", input);
return -1;
}
char str[80];
fgets(str, 80, fip);
if (str[0] != 'P' || str[1] != '6') {
printf("PPM format is not found\n");
err = -1;
goto error1;
}
int width = 0, height = 0;
/* Read comment */
while (1) {
fgets(str, 80, fip);
if (str[0] == '#') {
/* Ignore it */
}
else {
/* Tokenize it to extract resolution */
char *pch = strtok(str, " ");
if (pch) {
width = atoi(pch);
pch = strtok(NULL, " ");
if (pch)
height = atoi(pch);
}
printf("%dx%d\n", width, height);
break;
}
}
/* Get max color */
fgets(str, 80, fip);
int x, y;
char r, g, b;
/* Allocate memory for PNG rows */
png_bytep *row_pointers = (png_bytep *) malloc(sizeof(png_bytep) * height);
if (row_pointers == NULL) {
printf("Fail to allocate memory\n");
err = -1;
goto error1;
}
for (y = 0; y < height; ++y) {
row_pointers[y] = (png_bytep) malloc(sizeof(png_byte) * 4 * width);
if (row_pointers[y] == NULL) {
printf("Fail to allocate memory\n");
err = -1;
goto error2;
}
}
for (y = 0; y < height; ++y) {
png_bytep rowp = row_pointers[y];
for (x = 0; x < width; ++x) {
/* Read RGB from PPM */
fscanf(fip, "%c%c%c", &r, &g, &b);
/* Prepare buffer for PNG */
rowp[x * 4 + 0] = r;
rowp[x * 4 + 1] = g;
rowp[x * 4 + 2] = b;
rowp[x * 4 + 3] = 0xFF;
}
}
/* Create file */
FILE *fop = fopen(output, "wb");
if(!fop) {
printf("Fail to open file (%s)\n", output);
err = -1;
goto error2;
}
/* Write PNG structure */
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
printf("Fail to create png structure\n");
err = -1;
goto error3;
}
png_infop png_info_ptr = png_create_info_struct(png_ptr);
if (!png_info_ptr) {
printf("Fail to create info structure\n");
err = -1;
goto error3;
}
if (setjmp(png_jmpbuf(png_ptr))) {
printf("Fail to set png jmp\n");
err = -1;
goto error3;
}
png_init_io(png_ptr, fop);
/* Format: RGBA format, Bit Depth:8 */
png_set_IHDR(
png_ptr,
png_info_ptr,
width, height,
8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
/* Write PNG info and data */
png_write_info(png_ptr, png_info_ptr);
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, NULL);
error3:
fclose(fop);
error2:
for (y = 0; y < height; ++y) {
if (row_pointers[y])
free(row_pointers[y]);
}
if (row_pointers)
free(row_pointers);
error1:
fclose(fip);
printf("Good bye!\n");
return err;
}