Mercurial > hg > forks > libbpg
annotate bpgdec.c @ 21:0525128adebf
Check for malloc fail.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 27 Apr 2017 10:50:45 +0300 |
parents | aa1551967670 |
children | d0ec05fbab88 |
rev | line source |
---|---|
0 | 1 /* |
2 * BPG decoder command line utility | |
3 * | |
4 * Copyright (c) 2014 Fabrice Bellard | |
5 * | |
6 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 * of this software and associated documentation files (the "Software"), to deal | |
8 * in the Software without restriction, including without limitation the rights | |
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 * copies of the Software, and to permit persons to whom the Software is | |
11 * furnished to do so, subject to the following conditions: | |
12 * | |
13 * The above copyright notice and this permission notice shall be included in | |
14 * all copies or substantial portions of the Software. | |
15 * | |
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 * THE SOFTWARE. | |
23 */ | |
24 | |
25 #include <stdlib.h> | |
26 #include <stdio.h> | |
27 #include <math.h> | |
28 #include <getopt.h> | |
29 #include <inttypes.h> | |
20 | 30 #include <string.h> |
31 | |
0 | 32 |
33 /* define it to include PNG output */ | |
34 #define USE_PNG | |
35 | |
36 #ifdef USE_PNG | |
37 #include <png.h> | |
38 #endif | |
39 | |
40 #include "libbpg.h" | |
41 | |
42 static void ppm_save(BPGDecoderContext *img, const char *filename) | |
43 { | |
44 BPGImageInfo img_info_s, *img_info = &img_info_s; | |
45 FILE *f; | |
46 int w, h, y; | |
47 uint8_t *rgb_line; | |
48 | |
49 bpg_decoder_get_info(img, img_info); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
50 |
0 | 51 w = img_info->width; |
52 h = img_info->height; | |
53 | |
54 rgb_line = malloc(3 * w); | |
55 | |
56 f = fopen(filename,"wb"); | |
57 if (!f) { | |
58 fprintf(stderr, "%s: I/O error\n", filename); | |
59 exit(1); | |
60 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
61 |
0 | 62 fprintf(f, "P6\n%d %d\n%d\n", w, h, 255); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
63 |
0 | 64 bpg_decoder_start(img, BPG_OUTPUT_FORMAT_RGB24); |
65 for (y = 0; y < h; y++) { | |
66 bpg_decoder_get_line(img, rgb_line); | |
67 fwrite(rgb_line, 1, w * 3, f); | |
68 } | |
69 fclose(f); | |
70 | |
71 free(rgb_line); | |
72 } | |
73 | |
74 #ifdef USE_PNG | |
75 static void png_write_data (png_structp png_ptr, png_bytep data, | |
76 png_size_t length) | |
77 { | |
78 FILE *f; | |
79 int ret; | |
80 | |
81 f = png_get_io_ptr(png_ptr); | |
82 ret = fwrite(data, 1, length, f); | |
83 if (ret != length) | |
84 png_error(png_ptr, "PNG Write Error"); | |
85 } | |
86 | |
87 static void png_save(BPGDecoderContext *img, const char *filename, int bit_depth) | |
88 { | |
89 BPGImageInfo img_info_s, *img_info = &img_info_s; | |
90 FILE *f; | |
91 png_structp png_ptr; | |
92 png_infop info_ptr; | |
93 png_bytep row_pointer; | |
94 int y, color_type, bpp; | |
95 BPGDecoderOutputFormat out_fmt; | |
96 | |
97 if (bit_depth != 8 && bit_depth != 16) { | |
98 fprintf(stderr, "Only bit_depth = 8 or 16 are supported for PNG output\n"); | |
99 exit(1); | |
100 } | |
101 | |
102 bpg_decoder_get_info(img, img_info); | |
103 | |
104 f = fopen(filename, "wb"); | |
105 if (!f) { | |
106 fprintf(stderr, "%s: I/O error\n", filename); | |
107 exit(1); | |
108 } | |
109 | |
110 png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING, | |
111 NULL, | |
112 NULL, /* error */ | |
113 NULL, /* warning */ | |
114 NULL, | |
115 NULL, | |
116 NULL); | |
117 info_ptr = png_create_info_struct(png_ptr); | |
118 png_set_write_fn(png_ptr, (png_voidp)f, &png_write_data, NULL); | |
119 | |
120 if (setjmp(png_jmpbuf(png_ptr)) != 0) { | |
121 fprintf(stderr, "PNG write error\n"); | |
122 exit(1); | |
123 } | |
124 | |
125 if (img_info->has_alpha) | |
126 color_type = PNG_COLOR_TYPE_RGB_ALPHA; | |
127 else | |
128 color_type = PNG_COLOR_TYPE_RGB; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
129 |
0 | 130 png_set_IHDR(png_ptr, info_ptr, img_info->width, img_info->height, |
131 bit_depth, color_type, PNG_INTERLACE_NONE, | |
132 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); | |
133 | |
134 png_write_info(png_ptr, info_ptr); | |
135 | |
136 #if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__ | |
137 if (bit_depth == 16) { | |
138 png_set_swap(png_ptr); | |
139 } | |
140 #endif | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
141 |
0 | 142 if (bit_depth == 16) { |
143 if (img_info->has_alpha) | |
144 out_fmt = BPG_OUTPUT_FORMAT_RGBA64; | |
145 else | |
146 out_fmt = BPG_OUTPUT_FORMAT_RGB48; | |
147 } else { | |
148 if (img_info->has_alpha) | |
149 out_fmt = BPG_OUTPUT_FORMAT_RGBA32; | |
150 else | |
151 out_fmt = BPG_OUTPUT_FORMAT_RGB24; | |
152 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
153 |
0 | 154 bpg_decoder_start(img, out_fmt); |
155 | |
156 bpp = (3 + img_info->has_alpha) * (bit_depth / 8); | |
157 row_pointer = (png_bytep)png_malloc(png_ptr, img_info->width * bpp); | |
158 for (y = 0; y < img_info->height; y++) { | |
159 bpg_decoder_get_line(img, row_pointer); | |
160 png_write_row(png_ptr, row_pointer); | |
161 } | |
162 png_free(png_ptr, row_pointer); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
163 |
0 | 164 png_write_end(png_ptr, NULL); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
165 |
0 | 166 png_destroy_write_struct(&png_ptr, &info_ptr); |
167 | |
168 fclose(f); | |
169 } | |
170 #endif /* USE_PNG */ | |
171 | |
172 static void bpg_show_info(const char *filename, int show_extensions) | |
173 { | |
174 uint8_t *buf; | |
175 int buf_len, ret, buf_len_max; | |
176 FILE *f; | |
177 BPGImageInfo p_s, *p = &p_s; | |
178 BPGExtensionData *first_md, *md; | |
179 static const char *format_str[6] = { | |
180 "Gray", | |
181 "4:2:0", | |
182 "4:2:2", | |
183 "4:4:4", | |
184 "4:2:0_video", | |
185 "4:2:2_video", | |
186 }; | |
187 static const char *color_space_str[BPG_CS_COUNT] = { | |
188 "YCbCr", | |
189 "RGB", | |
190 "YCgCo", | |
191 "YCbCr_BT709", | |
192 "YCbCr_BT2020", | |
193 }; | |
194 static const char *extension_tag_str[] = { | |
195 "Unknown", | |
196 "EXIF", | |
197 "ICC profile", | |
198 "XMP", | |
199 "Thumbnail", | |
200 "Animation control", | |
201 }; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
202 |
0 | 203 f = fopen(filename, "rb"); |
204 if (!f) { | |
205 fprintf(stderr, "Could not open %s\n", filename); | |
206 exit(1); | |
207 } | |
208 | |
209 if (show_extensions) { | |
210 fseek(f, 0, SEEK_END); | |
211 buf_len_max = ftell(f); | |
212 fseek(f, 0, SEEK_SET); | |
213 } else { | |
214 /* if no extension are shown, just need the header */ | |
215 buf_len_max = BPG_DECODER_INFO_BUF_SIZE; | |
216 } | |
217 buf = malloc(buf_len_max); | |
218 buf_len = fread(buf, 1, buf_len_max, f); | |
219 | |
220 ret = bpg_decoder_get_info_from_buf(p, show_extensions ? &first_md : NULL, | |
221 buf, buf_len); | |
222 free(buf); | |
223 fclose(f); | |
224 if (ret < 0) { | |
225 fprintf(stderr, "Not a BPG image\n"); | |
226 exit(1); | |
227 } | |
228 printf("size=%dx%d color_space=%s", | |
229 p->width, p->height, | |
230 p->format == BPG_FORMAT_GRAY ? "Gray" : color_space_str[p->color_space]); | |
231 if (p->has_w_plane) { | |
232 printf(" w_plane=%d", p->has_w_plane); | |
233 } | |
234 if (p->has_alpha) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
235 printf(" alpha=%d premul=%d", |
0 | 236 p->has_alpha, p->premultiplied_alpha); |
237 } | |
238 printf(" format=%s limited_range=%d bit_depth=%d animation=%d\n", | |
239 format_str[p->format], | |
240 p->limited_range, | |
241 p->bit_depth, | |
242 p->has_animation); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
243 |
0 | 244 if (first_md) { |
245 const char *tag_name; | |
246 printf("Extension data:\n"); | |
247 for(md = first_md; md != NULL; md = md->next) { | |
248 if (md->tag <= 5) | |
249 tag_name = extension_tag_str[md->tag]; | |
250 else | |
251 tag_name = extension_tag_str[0]; | |
252 printf(" tag=%d (%s) length=%d\n", | |
253 md->tag, tag_name, md->buf_len); | |
254 } | |
255 bpg_decoder_free_extension_data(first_md); | |
256 } | |
257 } | |
258 | |
259 static void help(void) | |
260 { | |
261 printf("BPG Image Decoder version " CONFIG_BPG_VERSION "\n" | |
262 "usage: bpgdec [options] infile\n" | |
263 "Options:\n" | |
264 "-o outfile.[ppm|png] set the output filename (default = out.png)\n" | |
265 "-b bit_depth PNG output only: use bit_depth per component (8 or 16, default = 8)\n" | |
266 "-i display information about the image\n"); | |
267 exit(1); | |
268 } | |
269 | |
270 int main(int argc, char **argv) | |
271 { | |
272 FILE *f; | |
273 BPGDecoderContext *img; | |
274 uint8_t *buf; | |
275 int buf_len, bit_depth, c, show_info; | |
276 const char *outfilename, *filename, *p; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
277 |
0 | 278 outfilename = "out.png"; |
279 bit_depth = 8; | |
280 show_info = 0; | |
281 for(;;) { | |
282 c = getopt(argc, argv, "ho:b:i"); | |
283 if (c == -1) | |
284 break; | |
285 switch(c) { | |
286 case 'h': | |
287 show_help: | |
288 help(); | |
289 break; | |
290 case 'o': | |
291 outfilename = optarg; | |
292 break; | |
293 case 'b': | |
294 bit_depth = atoi(optarg); | |
295 break; | |
296 case 'i': | |
297 show_info = 1; | |
298 break; | |
299 default: | |
300 exit(1); | |
301 } | |
302 } | |
303 | |
304 if (optind >= argc) | |
305 goto show_help; | |
306 | |
307 filename = argv[optind++]; | |
308 | |
309 if (show_info) { | |
310 bpg_show_info(filename, 1); | |
311 return 0; | |
312 } | |
313 | |
314 f = fopen(filename, "rb"); | |
315 if (!f) { | |
316 fprintf(stderr, "Could not open %s\n", filename); | |
317 exit(1); | |
318 } | |
319 | |
320 fseek(f, 0, SEEK_END); | |
321 buf_len = ftell(f); | |
322 fseek(f, 0, SEEK_SET); | |
323 | |
324 buf = malloc(buf_len); | |
21 | 325 if (!buf) { |
326 fprintf(stderr, "Could not allocate memory for buffer.\n"); | |
327 exit(1); | |
328 } | |
329 | |
0 | 330 if (fread(buf, 1, buf_len, f) != buf_len) { |
331 fprintf(stderr, "Error while reading file\n"); | |
332 exit(1); | |
333 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
334 |
0 | 335 fclose(f); |
336 | |
337 img = bpg_decoder_open(); | |
338 | |
339 if (bpg_decoder_decode(img, buf, buf_len) < 0) { | |
340 fprintf(stderr, "Could not decode image\n"); | |
341 exit(1); | |
342 } | |
343 free(buf); | |
344 | |
345 #ifdef USE_PNG | |
346 p = strrchr(outfilename, '.'); | |
21 | 347 if (p && strcasecmp(p + 1, "ppm") != 0) { |
0 | 348 png_save(img, outfilename, bit_depth); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
349 } else |
0 | 350 #endif |
351 { | |
352 ppm_save(img, outfilename); | |
353 } | |
354 | |
355 bpg_decoder_close(img); | |
356 | |
357 return 0; | |
358 } |