AlbumShaper 1.0a3
jpegSize.cpp
Go to the documentation of this file.
1#include "jpegSize.h"
2
3#include <stdio.h>
4
5/*
6 Most of the code that follows was adapted from IJG's rdjpgcom.c file.
7
8 * Copyright (C) 1994-1997, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 *
11 * This file contains a very simple stand-alone application that displays
12 * the text in COM (comment) markers in a JFIF file.
13 * This may be useful as an example of the minimum logic needed to parse
14 * JPEG markers.
15 */
16
17/* Return next input byte, or EOF if no more */
18#define NEXTBYTE() getc(infile)
19
20/*
21 * JPEG markers consist of one or more 0xFF bytes, followed by a marker
22 * code byte (which is not an FF). Here are the marker codes of interest
23 * in this program. (See jdmarker.c for a more complete list.)
24 */
25
26#define M_SOF0 0xC0 /* Start Of Frame N */
27#define M_SOF1 0xC1 /* N indicates which compression process */
28#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
29#define M_SOF3 0xC3
30#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
31#define M_SOF6 0xC6
32#define M_SOF7 0xC7
33#define M_SOF9 0xC9
34#define M_SOF10 0xCA
35#define M_SOF11 0xCB
36#define M_SOF13 0xCD
37#define M_SOF14 0xCE
38#define M_SOF15 0xCF
39#define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
40#define M_EOI 0xD9 /* End Of Image (end of datastream) */
41#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
42#define M_APP0 0xE0 /* Application-specific marker, type N */
43#define M_APP12 0xEC /* (we don't bother to list all 16 APPn's) */
44#define M_COM 0xFE /* COMment */
45
46#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
47#define READ_BINARY "r"
48#else
49#ifdef VMS /* VMS is very nonstandard */
50#define READ_BINARY "rb", "ctx=stm"
51#else /* standard ANSI-compliant case */
52#define READ_BINARY "rb"
53#endif
54#endif
55
56FILE * infile;
57
58bool process_SOFn (int& width, int& height);
59bool skip_variable ();
60bool read_1_byte (int* res);
61bool read_2_bytes (unsigned int* res);
62bool first_marker (int* res);
63bool next_marker (int* res);
64
65bool getJPEGSize( const char* filename, int& width, int& height )
66{
67 //open file
68 if ((infile = fopen(filename, READ_BINARY)) == NULL)
69 return false;
70
71 //this is scan_JPEG_header (int verbose)
72 //Parse the marker stream until SOFn is seen;
73 int marker;
74
75 //Expect SOI at start of file
76 if (!first_marker(&marker))
77 {
78 fclose(infile);
79 return false;
80 }
81
82 /* Scan miscellaneous markers until we reach SOFn. */
83 for (;;)
84 {
85 if(!next_marker(&marker))
86 {
87 fclose(infile);
88 return false;
89 }
90
91 switch (marker)
92 {
93 /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
94 * treated as SOFn. C4 in particular is actually DHT.
95 */
96 case M_SOF0: /* Baseline */
97 case M_SOF1: /* Extended sequential, Huffman */
98 case M_SOF2: /* Progressive, Huffman */
99 case M_SOF3: /* Lossless, Huffman */
100 case M_SOF5: /* Differential sequential, Huffman */
101 case M_SOF6: /* Differential progressive, Huffman */
102 case M_SOF7: /* Differential lossless, Huffman */
103 case M_SOF9: /* Extended sequential, arithmetic */
104 case M_SOF10: /* Progressive, arithmetic */
105 case M_SOF11: /* Lossless, arithmetic */
106 case M_SOF13: /* Differential sequential, arithmetic */
107 case M_SOF14: /* Differential progressive, arithmetic */
108 case M_SOF15: /* Differential lossless, arithmetic */
110 {
111 fclose(infile);
112 return false;
113 }
114 else
115 {
116 fclose(infile);
117 return true;
118 }
119 case M_SOS: /* stop before hitting compressed data */
120 {
121 fclose(infile);
122 return false;
123 }
124 case M_EOI: /* in case it's a tables-only JPEG stream */
125 {
126 fclose(infile);
127 return false;
128 }
129 default: /* Anything else just gets skipped */
130 skip_variable(); /* we assume it has a parameter count... */
131 break;
132 }
133 } /* end loop */
134
135
136//cout << "ERROR!\n";
137return false;
138
139}
140
141
142/*
143 * Read the initial marker, which should be SOI.
144 * For a JFIF file, the first two bytes of the file should be literally
145 * 0xFF M_SOI. To be more general, we could use next_marker, but if the
146 * input file weren't actually JPEG at all, next_marker might read the whole
147 * file and then return a misleading error message...
148 */
149bool first_marker (int* res)
150{
151 int c1, c2;
152 c1 = NEXTBYTE();
153 c2 = NEXTBYTE();
154 if (c1 != 0xFF || c2 != M_SOI)
155 return false;
156 else
157 {
158 *res = c2;
159 return true;
160 }
161}
162
163/*
164 * Find the next JPEG marker and return its marker code.
165 * We expect at least one FF byte, possibly more if the compressor used FFs
166 * to pad the file.
167 * There could also be non-FF garbage between markers. The treatment of such
168 * garbage is unspecified; we choose to skip over it but emit a warning msg.
169 * NB: this routine must not be used after seeing SOS marker, since it will
170 * not deal correctly with FF/00 sequences in the compressed image data...
171 */
172bool next_marker (int* res)
173{
174 int c;
175 int discarded_bytes = 0;
176
177 /* Find 0xFF byte; count and skip any non-FFs. */
178 if(!read_1_byte(&c))
179 return false;
180 while (c != 0xFF)
181 {
182 discarded_bytes++;
183 if(!read_1_byte(&c))
184 return false;
185 }
186 /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
187 * are legal as pad bytes, so don't count them in discarded_bytes.
188 */
189 do
190 {
191 if(!read_1_byte(&c))
192 return false;
193 } while (c == 0xFF);
194
195// if (discarded_bytes != 0) { cout << "Warning: garbage data found in JPEG file\n"; }
196
197 *res = c;
198 return true;
199}
200
201/* Read one byte, testing for EOF */
202bool read_1_byte (int* res)
203{
204 int c = NEXTBYTE();
205 if (c == EOF)
206 return false;
207 else
208 {
209 *res = c;
210 return true;
211 }
212}
213
214/* Read 2 bytes, convert to unsigned int */
215/* All 2-byte quantities in JPEG markers are MSB first */
216bool read_2_bytes (unsigned int* res)
217{
218 int c1, c2;
219 c1 = NEXTBYTE();
220 if (c1 == EOF)
221 return false;
222 c2 = NEXTBYTE();
223 if (c2 == EOF)
224 return false;
225 *res = (((unsigned int) c1) << 8) + ((unsigned int) c2);
226 return true;
227}
228
229/*
230 * Most types of marker are followed by a variable-length parameter segment.
231 * This routine skips over the parameters for any marker we don't otherwise
232 * want to process.
233 * Note that we MUST skip the parameter segment explicitly in order not to
234 * be fooled by 0xFF bytes that might appear within the parameter segment;
235 * such bytes do NOT introduce new markers.
236 */
238/* Skip over an unknown or uninteresting variable-length marker */
239{
240 unsigned int length;
241
242 /* Get the marker parameter length count */
243 if(!read_2_bytes(&length))
244 return false;
245 /* Length includes itself, so must be at least 2 */
246 if (length < 2)
247 return false;
248 length -= 2;
249 /* Skip over the remaining bytes */
250 while (length > 0)
251 {
252 int tmp;
253 if(!read_1_byte(&tmp))
254 return false;
255 length--;
256 }
257 return false;
258}
259
260//Process a SOFn marker - get image dimensions
261bool process_SOFn (int& width, int& height)
262{
263 unsigned int length;
264 unsigned int image_height, image_width;
265 int data_precision;
266
267 if(!read_2_bytes(&length) ||
268 !read_1_byte(&data_precision) ||
269 !read_2_bytes(&image_height) ||
270 !read_2_bytes(&image_width) )
271 return false;
272
273 width = (int) image_width;
274 height = (int) image_height;
275 return true;
276}
int width
Definition blur.cpp:79
int height
Definition blur.cpp:79
bool first_marker(int *res)
Definition jpegSize.cpp:149
bool getJPEGSize(const char *filename, int &width, int &height)
Definition jpegSize.cpp:65
bool read_1_byte(int *res)
Definition jpegSize.cpp:202
#define M_SOF7
Definition jpegSize.cpp:32
bool next_marker(int *res)
Definition jpegSize.cpp:172
#define M_SOF6
Definition jpegSize.cpp:31
#define M_EOI
Definition jpegSize.cpp:40
#define M_SOF11
Definition jpegSize.cpp:35
#define M_SOF9
Definition jpegSize.cpp:33
#define M_SOF15
Definition jpegSize.cpp:38
#define READ_BINARY
Definition jpegSize.cpp:52
#define NEXTBYTE()
Definition jpegSize.cpp:18
#define M_SOF3
Definition jpegSize.cpp:29
#define M_SOF13
Definition jpegSize.cpp:36
#define M_SOF10
Definition jpegSize.cpp:34
#define M_SOF1
Definition jpegSize.cpp:27
bool read_2_bytes(unsigned int *res)
Definition jpegSize.cpp:216
bool skip_variable()
Definition jpegSize.cpp:237
bool process_SOFn(int &width, int &height)
Definition jpegSize.cpp:261
#define M_SOI
Definition jpegSize.cpp:39
#define M_SOF5
Definition jpegSize.cpp:30
#define M_SOF14
Definition jpegSize.cpp:37
#define M_SOF0
Definition jpegSize.cpp:26
#define M_SOF2
Definition jpegSize.cpp:28
FILE * infile
Definition jpegSize.cpp:56
#define M_SOS
Definition jpegSize.cpp:41