-
Notifications
You must be signed in to change notification settings - Fork 1
/
decompresstopgx.c
184 lines (161 loc) · 6.59 KB
/
decompresstopgx.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
/*
* Copyright (C)2017 D. R. Commander. All Rights Reserved.
* Copyright (C)2017 Fraunhofer IIS. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the libjpeg-turbo Project nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* This program uses the libjpeg API to save the YCbCr planes from an arbitrary
JPEG file into a PGX file set (used in JPEG compliance testing.) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <jpeglib.h>
#define THROW(msg) { \
printf("ERROR in line %d:\n%s\n", __LINE__, msg); \
retval = -1; goto bailout; \
}
#define DSTATE_START 200
#define PAD(v, p) ((v + (p) - 1) & (~((p) - 1)))
int main(int argc, char **argv)
{
struct jpeg_error_mgr jerr;
struct jpeg_decompress_struct dinfo;
unsigned char *jpegBuf = NULL;
FILE *file = NULL;
int retval = 0, i, pw[MAX_COMPONENTS], ph[MAX_COMPONENTS], row;
/* The real component dimensions after clipping, using the formulae in
A.1.1 */
int rw[MAX_COMPONENTS], rh[MAX_COMPONENTS];
long jpegSize = 0;
JSAMPROW *outbuf[MAX_COMPONENTS];
JSAMPLE *plane[MAX_COMPONENTS];
char filename[256];
FILE *pgxheader = NULL, *pgxfiles[MAX_COMPONENTS] = { NULL };
if (argc < 3) {
printf("USAGE: %s <JPEG file> <output file (.pgx)\n", argv[0]);
return 1;
}
for (i = 0; i < MAX_COMPONENTS; i++) {
outbuf[i] = NULL; plane[i] = NULL;
}
jpeg_create_decompress(&dinfo);
if ((file = fopen(argv[1], "rb")) == NULL)
THROW(strerror(errno));
if (fseek(file, 0, SEEK_END) < 0)
THROW(strerror(errno));
if ((jpegSize = ftell(file)) < 0)
THROW(strerror(errno));
if (jpegSize < 1)
THROW("Input file contains no data");
if (fseek(file, 0, SEEK_SET) < 0)
THROW(strerror(errno));
if ((jpegBuf = (unsigned char *)malloc(jpegSize)) == NULL)
THROW(strerror(errno));
if (fread(jpegBuf, jpegSize, 1, file) < 1)
THROW(strerror(errno));
fclose(file); file = NULL;
dinfo.err = jpeg_std_error(&jerr);
jpeg_mem_src(&dinfo, jpegBuf, jpegSize);
jpeg_read_header(&dinfo, TRUE);
jpeg_calc_output_dimensions(&dinfo);
if (!(pgxheader = fopen(argv[2], "wb")))
THROW(strerror(errno));
printf("Image dimensions: %ux%u pixels\n", dinfo.output_width,
dinfo.output_height);
/* For the purpose of reference testing, we need the *precise* output sample
* dimensions and not the dimensions rounded up to the nearest multiple of
* the DCT block size. For that, we need to know the maximum sampling
* factors.
*/
printf("Maximum sampling factors: %dx%d\n",
dinfo.max_h_samp_factor, dinfo.max_v_samp_factor);
for (i = 0; i < dinfo.num_components; i++) {
jpeg_component_info *compptr = &dinfo.comp_info[i];
FILE *hdr;
pw[i] = compptr->width_in_blocks * DCTSIZE;
ph[i] = compptr->height_in_blocks * DCTSIZE;
if ((plane[i] = (JSAMPLE *)malloc(sizeof(JSAMPLE) * pw[i] *
ph[i])) == NULL)
THROW(strerror(errno));
if ((outbuf[i] = (JSAMPROW *)malloc(sizeof(JSAMPROW) * ph[i])) == NULL)
THROW(strerror(errno));
for (row = 0; row < ph[i]; row++)
outbuf[i][row] = &plane[i][row * pw[i]];
/* Compute the component dimensions following A.1.1 */
rw[i] = (dinfo.output_width * compptr->h_samp_factor +
dinfo.max_h_samp_factor - 1) / dinfo.max_h_samp_factor;
rh[i] = (dinfo.output_height * compptr->v_samp_factor +
dinfo.max_v_samp_factor - 1) / dinfo.max_v_samp_factor;
printf(" Component %d: %dx%d samples, %dx%d pixels (sampling factor: %dx%d)\n", i,
pw[i], ph[i], rw[i], rh[i],
compptr->h_samp_factor, compptr->v_samp_factor);
snprintf(filename, sizeof(filename), "%s_%d.h", argv[2], i);
if (!(hdr = fopen(filename, "w")))
THROW(strerror(errno));
fprintf(hdr, "PG ML +%d %d %d\n", dinfo.data_precision, rw[i], rh[i]);
fclose(hdr);
snprintf(filename, sizeof(filename), "%s_%d.raw", argv[2], i);
fprintf(pgxheader, "%s\n", filename);
if (!(pgxfiles[i] = fopen(filename, "wb")))
THROW(strerror(errno));
}
dinfo.raw_data_out = TRUE;
jpeg_start_decompress(&dinfo);
for (row = 0; row < (int)dinfo.output_height;
row += dinfo.max_v_samp_factor * DCTSIZE) {
JSAMPARRAY yuvptr[MAX_COMPONENTS];
for (i = 0; i < dinfo.num_components; i++) {
jpeg_component_info *compptr = &dinfo.comp_info[i];
yuvptr[i] = &outbuf[i][row * compptr->v_samp_factor /
dinfo.max_v_samp_factor];
}
jpeg_read_raw_data(&dinfo, yuvptr, dinfo.max_v_samp_factor * DCTSIZE);
}
jpeg_finish_decompress(&dinfo);
for (i = 0; i < dinfo.num_components; i++) {
int y;
/* At the edges, extra data is included in the DCT blocks we do not want to
* compare.
*/
for (y = 0; y < rh[i]; y++) {
JSAMPLE *data = plane[i] + pw[i] * y;
if (fwrite(data, rw[i], (dinfo.data_precision > 8) ? 2 : 1,
pgxfiles[i]) < 1)
THROW(strerror(errno));
}
}
bailout:
if (dinfo.global_state > DSTATE_START) jpeg_abort_decompress(&dinfo);
free(jpegBuf);
if (file) fclose(file);
if (pgxheader) fclose(pgxheader);
for (i = 0; i < MAX_COMPONENTS; i++) {
free(outbuf[i]);
free(plane[i]);
if (pgxfiles[i]) fclose(pgxfiles[i]);
}
return retval;
}