-
Notifications
You must be signed in to change notification settings - Fork 2
/
flipjpeg.c
247 lines (206 loc) · 7 KB
/
flipjpeg.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* flipjpeg.c
*
* Copyright (c) 1997 Everett Lipman
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* The GNU General Public License is available at
*
* http://www.fsf.org/copyleft/gpl.html
*
* Alternatively, you can write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place - Suite 330
* Boston, MA 02111-1307, USA
*
* ---------------------------------------------------------------
*
* This program will do a lossless vertical inversion of a jpeg
* file. It will only work properly if the jpeg image has a height
* in pixels which is a multiple of 8.
*
* This program requires the Independent JPEG Group's free JPEG software.
* You can get a copy at
*
* ftp://ftp.uu.net/graphics/jpeg
*
* Most of the code here was taken from the program jpegtran.c, which
* is part of the IJG software package. Since I have little experience
* using the IJG package, I am not sure if this program will run on any
* platform other than Sun Solaris (UNIX).
*
*/
#include <stdio.h>
#include <string.h>
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
#include "jversion.h" /* for version message */
static const char * progname; /* program name for error messages */
LOCAL(void)
usage (void)
/*
* complain about bad command line
*/
{
fprintf(stderr, "\nusage: flipjpeg < inputfile > outputfile\n\n");
exit(EXIT_FAILURE);
}
/*
* The main program.
*/
int
main (int argc, char **argv)
{
struct jpeg_decompress_struct srcinfo;
struct jpeg_compress_struct dstinfo;
struct jpeg_error_mgr jsrcerr, jdsterr;
jvirt_barray_ptr *coef_arrays;
JDIMENSION i, compnum, rownum, blocknum;
size_t block_row_size;
JBLOCKARRAY coef_buffers[MAX_COMPONENTS];
JBLOCKARRAY row_ptrs[MAX_COMPONENTS];
FILE * input_file;
FILE * output_file;
if (argc != 1) usage();
progname = argv[0];
if (progname == NULL || progname[0] == 0)
progname = "flipjpeg"; /* in case C library doesn't provide it */
/*
* Initialize the JPEG decompression object with default error handling.
*/
srcinfo.err = jpeg_std_error(&jsrcerr);
jpeg_create_decompress(&srcinfo);
/*
* Initialize the JPEG compression object with default error handling.
*/
dstinfo.err = jpeg_std_error(&jdsterr);
jpeg_create_compress(&dstinfo);
jsrcerr.trace_level = jdsterr.trace_level;
srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
/*
* Use stdin and stdout
*/
input_file = read_stdin();
output_file = write_stdout();
/*
* Specify data source for decompression
*/
jpeg_stdio_src(&srcinfo, input_file);
/*
* Read file header
*/
(void) jpeg_read_header(&srcinfo, TRUE);
/*
* Allocate memory to hold entire coefficient array for all components
*/
for (compnum=0; compnum<srcinfo.num_components; compnum++)
coef_buffers[compnum] = ((&dstinfo)->mem->alloc_barray)
((j_common_ptr) &dstinfo, JPOOL_IMAGE,
srcinfo.comp_info[compnum].width_in_blocks,
srcinfo.comp_info[compnum].height_in_blocks);
/*
* Read source file as DCT coefficients
*/
coef_arrays = jpeg_read_coefficients(&srcinfo);
/*
* Initialize destination compression parameters from source values
*/
jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
/***************************************************************************/
/*
* print out some info about the image
*/
/*
* fprintf(stderr,"\ncomponents: %d image width: %d image height: %d\n\n",
* srcinfo.num_components, srcinfo.image_width, srcinfo.image_height);
*
* for (compnum=0; compnum < srcinfo.num_components; compnum++)
* fprintf(stderr,
* " component: %d width in blocks: %d height in blocks: %d\n",
* compnum,srcinfo.comp_info[compnum].width_in_blocks,
* srcinfo.comp_info[compnum].height_in_blocks);
* fprintf(stderr,"\n");
*/
/***************************************************************************/
/*
* Copy coefficients into new real array, flipping the sign
* of each coefficient corresponding to a DCT basis
* function which is asymmetric in the vertical direction.
*/
for (compnum=0; compnum<srcinfo.num_components; compnum++)
{
block_row_size = (size_t) SIZEOF(JCOEF)*DCTSIZE2
*srcinfo.comp_info[compnum].width_in_blocks;
for (rownum=0; rownum<srcinfo.comp_info[compnum].height_in_blocks; rownum++)
{
row_ptrs[compnum] = ((&dstinfo)->mem->access_virt_barray)
((j_common_ptr) &dstinfo, coef_arrays[compnum],
rownum, (JDIMENSION) 1, FALSE);
for (blocknum=0; blocknum<srcinfo.comp_info[compnum].width_in_blocks;
blocknum++)
for (i=0; i<DCTSIZE2; i++)
{
if ( (i/DCTSIZE)%2 == 1 )
{
coef_buffers[compnum][rownum][blocknum][i] =
-row_ptrs[compnum][0][blocknum][i];
}
else
{
coef_buffers[compnum][rownum][blocknum][i] =
row_ptrs[compnum][0][blocknum][i];
}
}
}
}
/***************************************************************************/
/*
* Write coefficient block rows back into virtual array, in
* upside-down order.
*/
for (compnum=0; compnum<srcinfo.num_components; compnum++)
{
block_row_size = (size_t) SIZEOF(JCOEF)*DCTSIZE2
*srcinfo.comp_info[compnum].width_in_blocks;
for (rownum=0; rownum<srcinfo.comp_info[compnum].height_in_blocks; rownum++)
{
row_ptrs[compnum] = ((&dstinfo)->mem->access_virt_barray)
((j_common_ptr) &dstinfo, coef_arrays[compnum],
rownum, (JDIMENSION) 1, TRUE);
memcpy(row_ptrs[compnum][0][0],
coef_buffers[compnum]
[(srcinfo.comp_info[compnum].height_in_blocks-1)-rownum][0],
block_row_size);
}
}
/***************************************************************************/
/*
* Specify data destination for compression
*/
jpeg_stdio_dest(&dstinfo, output_file);
/*
* Start compressor
*/
jpeg_write_coefficients(&dstinfo, coef_arrays);
/*
* Finish compression and release memory
*/
jpeg_finish_compress(&dstinfo);
jpeg_destroy_compress(&dstinfo);
(void) jpeg_finish_decompress(&srcinfo);
jpeg_destroy_decompress(&srcinfo);
/*
* All done.
*/
exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
return 0; /* suppress no-return-value warnings */
}