-
Notifications
You must be signed in to change notification settings - Fork 6
/
bpgdec.c.diff
363 lines (363 loc) · 9.84 KB
/
bpgdec.c.diff
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
28d27
< #include <getopt.h>
30,37d28
<
< /* define it to include PNG output */
< #define USE_PNG
<
< #ifdef USE_PNG
< #include <png.h>
< #endif
<
40,42c31,33
< static void ppm_save(BPGDecoderContext *img, const char *filename)
< {
< BPGImageInfo img_info_s, *img_info = &img_info_s;
---
> static void rgba_save(BPGDecoderContext *img, const char *filename){
> BPGImageInfo img_info_s,*img_info=&img_info_s;
> BPGDecoderOutputFormat out_fmt=BPG_OUTPUT_FORMAT_RGBA32;
44,198c35,52
< int w, h, y;
< uint8_t *rgb_line;
<
< bpg_decoder_get_info(img, img_info);
<
< w = img_info->width;
< h = img_info->height;
<
< rgb_line = malloc(3 * w);
<
< f = fopen(filename,"wb");
< if (!f) {
< fprintf(stderr, "%s: I/O error\n", filename);
< exit(1);
< }
<
< fprintf(f, "P6\n%d %d\n%d\n", w, h, 255);
<
< bpg_decoder_start(img, BPG_OUTPUT_FORMAT_RGB24);
< for (y = 0; y < h; y++) {
< bpg_decoder_get_line(img, rgb_line);
< fwrite(rgb_line, 1, w * 3, f);
< }
< fclose(f);
<
< free(rgb_line);
< }
<
< #ifdef USE_PNG
< static void png_write_data (png_structp png_ptr, png_bytep data,
< png_size_t length)
< {
< FILE *f;
< int ret;
<
< f = png_get_io_ptr(png_ptr);
< ret = fwrite(data, 1, length, f);
< if (ret != length)
< png_error(png_ptr, "PNG Write Error");
< }
<
< static void png_save(BPGDecoderContext *img, const char *filename, int bit_depth)
< {
< BPGImageInfo img_info_s, *img_info = &img_info_s;
< FILE *f;
< png_structp png_ptr;
< png_infop info_ptr;
< png_bytep row_pointer;
< int y, color_type, bpp;
< BPGDecoderOutputFormat out_fmt;
<
< if (bit_depth != 8 && bit_depth != 16) {
< fprintf(stderr, "Only bit_depth = 8 or 16 are supported for PNG output\n");
< exit(1);
< }
<
< bpg_decoder_get_info(img, img_info);
<
< f = fopen(filename, "wb");
< if (!f) {
< fprintf(stderr, "%s: I/O error\n", filename);
< exit(1);
< }
<
< png_ptr = png_create_write_struct_2(PNG_LIBPNG_VER_STRING,
< NULL,
< NULL, /* error */
< NULL, /* warning */
< NULL,
< NULL,
< NULL);
< info_ptr = png_create_info_struct(png_ptr);
< png_set_write_fn(png_ptr, (png_voidp)f, &png_write_data, NULL);
<
< if (setjmp(png_jmpbuf(png_ptr)) != 0) {
< fprintf(stderr, "PNG write error\n");
< exit(1);
< }
<
< if (img_info->has_alpha)
< color_type = PNG_COLOR_TYPE_RGB_ALPHA;
< else
< color_type = PNG_COLOR_TYPE_RGB;
<
< png_set_IHDR(png_ptr, info_ptr, img_info->width, img_info->height,
< bit_depth, color_type, PNG_INTERLACE_NONE,
< PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
<
< png_write_info(png_ptr, info_ptr);
<
< #if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
< if (bit_depth == 16) {
< png_set_swap(png_ptr);
< }
< #endif
<
< if (bit_depth == 16) {
< if (img_info->has_alpha)
< out_fmt = BPG_OUTPUT_FORMAT_RGBA64;
< else
< out_fmt = BPG_OUTPUT_FORMAT_RGB48;
< } else {
< if (img_info->has_alpha)
< out_fmt = BPG_OUTPUT_FORMAT_RGBA32;
< else
< out_fmt = BPG_OUTPUT_FORMAT_RGB24;
< }
<
< bpg_decoder_start(img, out_fmt);
<
< bpp = (3 + img_info->has_alpha) * (bit_depth / 8);
< row_pointer = (png_bytep)png_malloc(png_ptr, img_info->width * bpp);
< for (y = 0; y < img_info->height; y++) {
< bpg_decoder_get_line(img, row_pointer);
< png_write_row(png_ptr, row_pointer);
< }
< png_free(png_ptr, row_pointer);
<
< png_write_end(png_ptr, NULL);
<
< png_destroy_write_struct(&png_ptr, &info_ptr);
<
< fclose(f);
< }
< #endif /* USE_PNG */
<
< static void bpg_show_info(const char *filename, int show_extensions)
< {
< uint8_t *buf;
< int buf_len, ret, buf_len_max;
< FILE *f;
< BPGImageInfo p_s, *p = &p_s;
< BPGExtensionData *first_md, *md;
< static const char *format_str[6] = {
< "Gray",
< "4:2:0",
< "4:2:2",
< "4:4:4",
< "4:2:0_video",
< "4:2:2_video",
< };
< static const char *color_space_str[BPG_CS_COUNT] = {
< "YCbCr",
< "RGB",
< "YCgCo",
< "YCbCr_BT709",
< "YCbCr_BT2020",
< };
< static const char *extension_tag_str[] = {
< "Unknown",
< "EXIF",
< "ICC profile",
< "XMP",
< "Thumbnail",
< "Animation control",
---
> int w,h,y,pnum,pden;
> bpg_decoder_get_info(img,img_info);
> w=img_info->width;
> h=img_info->height;
> f=fopen(filename,"wb");
> if(!f) exit(1);
> uint8_t *rgba_line=malloc(4*w);;
> fwrite(&w,sizeof(int),1,f);
> fwrite(&h,sizeof(int),1,f);
> while(1){
> if(bpg_decoder_start(img,out_fmt)<0) break;
> bpg_decoder_get_frame_duration(img,&pnum,&pden);
> fwrite(&pnum,sizeof(int),1,f);
> fwrite(&pden,sizeof(int),1,f);
> for(y=0;y<h; y++){
> bpg_decoder_get_line(img,rgba_line);
> fwrite(rgba_line,1,w*4,f);
> };
200,220d53
<
< f = fopen(filename, "rb");
< if (!f) {
< fprintf(stderr, "Could not open %s\n", filename);
< exit(1);
< }
<
< if (show_extensions) {
< fseek(f, 0, SEEK_END);
< buf_len_max = ftell(f);
< fseek(f, 0, SEEK_SET);
< } else {
< /* if no extension are shown, just need the header */
< buf_len_max = BPG_DECODER_INFO_BUF_SIZE;
< }
< buf = malloc(buf_len_max);
< buf_len = fread(buf, 1, buf_len_max, f);
<
< ret = bpg_decoder_get_info_from_buf(p, show_extensions ? &first_md : NULL,
< buf, buf_len);
< free(buf);
222,254c55
< if (ret < 0) {
< fprintf(stderr, "Not a BPG image\n");
< exit(1);
< }
< printf("size=%dx%d color_space=%s",
< p->width, p->height,
< p->format == BPG_FORMAT_GRAY ? "Gray" : color_space_str[p->color_space]);
< if (p->has_w_plane) {
< printf(" w_plane=%d", p->has_w_plane);
< }
< if (p->has_alpha) {
< printf(" alpha=%d premul=%d",
< p->has_alpha, p->premultiplied_alpha);
< }
< printf(" format=%s limited_range=%d bit_depth=%d animation=%d\n",
< format_str[p->format],
< p->limited_range,
< p->bit_depth,
< p->has_animation);
<
< if (first_md) {
< const char *tag_name;
< printf("Extension data:\n");
< for(md = first_md; md != NULL; md = md->next) {
< if (md->tag <= 5)
< tag_name = extension_tag_str[md->tag];
< else
< tag_name = extension_tag_str[0];
< printf(" tag=%d (%s) length=%d\n",
< md->tag, tag_name, md->buf_len);
< }
< bpg_decoder_free_extension_data(first_md);
< }
---
> free(rgba_line);
257,327c58,65
< static void help(void)
< {
< printf("BPG Image Decoder version " CONFIG_BPG_VERSION "\n"
< "usage: bpgdec [options] infile\n"
< "Options:\n"
< "-o outfile.[ppm|png] set the output filename (default = out.png)\n"
< "-b bit_depth PNG output only: use bit_depth per component (8 or 16, default = 8)\n"
< "-i display information about the image\n");
< exit(1);
< }
<
< int main(int argc, char **argv)
< {
< FILE *f;
< BPGDecoderContext *img;
< uint8_t *buf;
< int buf_len, bit_depth, c, show_info;
< const char *outfilename, *filename, *p;
<
< outfilename = "out.png";
< bit_depth = 8;
< show_info = 0;
< for(;;) {
< c = getopt(argc, argv, "ho:b:i");
< if (c == -1)
< break;
< switch(c) {
< case 'h':
< show_help:
< help();
< break;
< case 'o':
< outfilename = optarg;
< break;
< case 'b':
< bit_depth = atoi(optarg);
< break;
< case 'i':
< show_info = 1;
< break;
< default:
< exit(1);
< }
< }
<
< if (optind >= argc)
< goto show_help;
<
< filename = argv[optind++];
<
< if (show_info) {
< bpg_show_info(filename, 1);
< return 0;
< }
<
< f = fopen(filename, "rb");
< if (!f) {
< fprintf(stderr, "Could not open %s\n", filename);
< exit(1);
< }
<
< fseek(f, 0, SEEK_END);
< buf_len = ftell(f);
< fseek(f, 0, SEEK_SET);
<
< buf = malloc(buf_len);
< if (fread(buf, 1, buf_len, f) != buf_len) {
< fprintf(stderr, "Error while reading file\n");
< exit(1);
< }
<
---
> int main(int argc, char **argv){
> FILE *f=fopen(argv[1],"rb");
> if(!f) exit(1);
> fseek(f,0,SEEK_END);
> int buf_len=ftell(f);
> fseek(f,0,SEEK_SET);
> uint8_t *buf=malloc(buf_len);
> if(fread(buf,1,buf_len,f)!=buf_len) exit(1);
329,335c67,68
<
< img = bpg_decoder_open();
<
< if (bpg_decoder_decode(img, buf, buf_len) < 0) {
< fprintf(stderr, "Could not decode image\n");
< exit(1);
< }
---
> BPGDecoderContext *img=bpg_decoder_open();
> if(bpg_decoder_decode(img,buf,buf_len)<0) exit(1);
337,350c70
<
< #ifdef USE_PNG
< p = strrchr(outfilename, '.');
< if (p)
< p++;
<
< if (p && strcasecmp(p, "ppm") != 0) {
< png_save(img, outfilename, bit_depth);
< } else
< #endif
< {
< ppm_save(img, outfilename);
< }
<
---
> rgba_save(img,argv[2]);
352d71
<