AlbumShaper  1.0a3
Macros | Functions | Variables
jpegSize.cpp File Reference
#include "jpegSize.h"
#include <stdio.h>
Include dependency graph for jpegSize.cpp:

Go to the source code of this file.

Macros

#define NEXTBYTE()   getc(infile)
 
#define M_SOF0   0xC0 /* Start Of Frame N */
 
#define M_SOF1   0xC1 /* N indicates which compression process */
 
#define M_SOF2   0xC2 /* Only SOF0-SOF2 are now in common use */
 
#define M_SOF3   0xC3
 
#define M_SOF5   0xC5 /* NB: codes C4 and CC are NOT SOF markers */
 
#define M_SOF6   0xC6
 
#define M_SOF7   0xC7
 
#define M_SOF9   0xC9
 
#define M_SOF10   0xCA
 
#define M_SOF11   0xCB
 
#define M_SOF13   0xCD
 
#define M_SOF14   0xCE
 
#define M_SOF15   0xCF
 
#define M_SOI   0xD8 /* Start Of Image (beginning of datastream) */
 
#define M_EOI   0xD9 /* End Of Image (end of datastream) */
 
#define M_SOS   0xDA /* Start Of Scan (begins compressed data) */
 
#define M_APP0   0xE0 /* Application-specific marker, type N */
 
#define M_APP12   0xEC /* (we don't bother to list all 16 APPn's) */
 
#define M_COM   0xFE /* COMment */
 
#define READ_BINARY   "rb"
 

Functions

bool process_SOFn (int &width, int &height)
 
bool skip_variable ()
 
bool read_1_byte (int *res)
 
bool read_2_bytes (unsigned int *res)
 
bool first_marker (int *res)
 
bool next_marker (int *res)
 
bool getJPEGSize (const char *filename, int &width, int &height)
 

Variables

FILE * infile
 

Macro Definition Documentation

§ M_APP0

#define M_APP0   0xE0 /* Application-specific marker, type N */

Definition at line 42 of file jpegSize.cpp.

§ M_APP12

#define M_APP12   0xEC /* (we don't bother to list all 16 APPn's) */

Definition at line 43 of file jpegSize.cpp.

§ M_COM

#define M_COM   0xFE /* COMment */

Definition at line 44 of file jpegSize.cpp.

§ M_EOI

#define M_EOI   0xD9 /* End Of Image (end of datastream) */

Definition at line 40 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF0

#define M_SOF0   0xC0 /* Start Of Frame N */

Definition at line 26 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF1

#define M_SOF1   0xC1 /* N indicates which compression process */

Definition at line 27 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF10

#define M_SOF10   0xCA

Definition at line 34 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF11

#define M_SOF11   0xCB

Definition at line 35 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF13

#define M_SOF13   0xCD

Definition at line 36 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF14

#define M_SOF14   0xCE

Definition at line 37 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF15

#define M_SOF15   0xCF

Definition at line 38 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF2

#define M_SOF2   0xC2 /* Only SOF0-SOF2 are now in common use */

Definition at line 28 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF3

#define M_SOF3   0xC3

Definition at line 29 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF5

#define M_SOF5   0xC5 /* NB: codes C4 and CC are NOT SOF markers */

Definition at line 30 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF6

#define M_SOF6   0xC6

Definition at line 31 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF7

#define M_SOF7   0xC7

Definition at line 32 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOF9

#define M_SOF9   0xC9

Definition at line 33 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ M_SOI

#define M_SOI   0xD8 /* Start Of Image (beginning of datastream) */

Definition at line 39 of file jpegSize.cpp.

Referenced by first_marker().

§ M_SOS

#define M_SOS   0xDA /* Start Of Scan (begins compressed data) */

Definition at line 41 of file jpegSize.cpp.

Referenced by getJPEGSize().

§ NEXTBYTE

#define NEXTBYTE ( )    getc(infile)

Definition at line 18 of file jpegSize.cpp.

Referenced by first_marker(), read_1_byte(), and read_2_bytes().

§ READ_BINARY

#define READ_BINARY   "rb"

Definition at line 52 of file jpegSize.cpp.

Referenced by getJPEGSize().

Function Documentation

§ first_marker()

bool first_marker ( int *  res)

Definition at line 149 of file jpegSize.cpp.

References M_SOI, and NEXTBYTE.

Referenced by getJPEGSize().

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 }
#define NEXTBYTE()
Definition: jpegSize.cpp:18
#define M_SOI
Definition: jpegSize.cpp:39

§ getJPEGSize()

bool getJPEGSize ( const char *  filename,
int &  width,
int &  height 
)

Definition at line 65 of file jpegSize.cpp.

References first_marker(), infile, M_EOI, M_SOF0, M_SOF1, M_SOF10, M_SOF11, M_SOF13, M_SOF14, M_SOF15, M_SOF2, M_SOF3, M_SOF5, M_SOF6, M_SOF7, M_SOF9, M_SOS, next_marker(), process_SOFn(), READ_BINARY, and skip_variable().

Referenced by getImageSize(), and isJpeg().

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 */
109  if(!process_SOFn(width, height))
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";
137 return false;
138 
139 }
#define M_SOF7
Definition: jpegSize.cpp:32
#define M_SOF10
Definition: jpegSize.cpp:34
#define M_SOF3
Definition: jpegSize.cpp:29
bool skip_variable()
Definition: jpegSize.cpp:237
#define M_SOF13
Definition: jpegSize.cpp:36
bool next_marker(int *res)
Definition: jpegSize.cpp:172
#define READ_BINARY
Definition: jpegSize.cpp:52
#define M_SOF15
Definition: jpegSize.cpp:38
#define M_SOF0
Definition: jpegSize.cpp:26
#define M_SOF14
Definition: jpegSize.cpp:37
int width
Definition: blur.cpp:79
FILE * infile
Definition: jpegSize.cpp:56
#define M_SOF1
Definition: jpegSize.cpp:27
bool first_marker(int *res)
Definition: jpegSize.cpp:149
#define M_SOF2
Definition: jpegSize.cpp:28
#define M_SOS
Definition: jpegSize.cpp:41
#define M_EOI
Definition: jpegSize.cpp:40
#define M_SOF6
Definition: jpegSize.cpp:31
#define M_SOF9
Definition: jpegSize.cpp:33
#define M_SOF5
Definition: jpegSize.cpp:30
bool process_SOFn(int &width, int &height)
Definition: jpegSize.cpp:261
#define M_SOF11
Definition: jpegSize.cpp:35
int height
Definition: blur.cpp:79

§ next_marker()

bool next_marker ( int *  res)

Definition at line 172 of file jpegSize.cpp.

References read_1_byte().

Referenced by getJPEGSize().

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 }
bool read_1_byte(int *res)
Definition: jpegSize.cpp:202

§ process_SOFn()

bool process_SOFn ( int &  width,
int &  height 
)

Definition at line 261 of file jpegSize.cpp.

References read_1_byte(), and read_2_bytes().

Referenced by getJPEGSize().

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 }
bool read_1_byte(int *res)
Definition: jpegSize.cpp:202
int width
Definition: blur.cpp:79
bool read_2_bytes(unsigned int *res)
Definition: jpegSize.cpp:216
int height
Definition: blur.cpp:79

§ read_1_byte()

bool read_1_byte ( int *  res)

Definition at line 202 of file jpegSize.cpp.

References NEXTBYTE.

Referenced by next_marker(), process_SOFn(), and skip_variable().

203 {
204  int c = NEXTBYTE();
205  if (c == EOF)
206  return false;
207  else
208  {
209  *res = c;
210  return true;
211  }
212 }
#define NEXTBYTE()
Definition: jpegSize.cpp:18

§ read_2_bytes()

bool read_2_bytes ( unsigned int *  res)

Definition at line 216 of file jpegSize.cpp.

References NEXTBYTE.

Referenced by process_SOFn(), and skip_variable().

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 }
#define NEXTBYTE()
Definition: jpegSize.cpp:18

§ skip_variable()

bool skip_variable ( )

Definition at line 237 of file jpegSize.cpp.

References read_1_byte(), and read_2_bytes().

Referenced by getJPEGSize().

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 }
bool read_1_byte(int *res)
Definition: jpegSize.cpp:202
bool read_2_bytes(unsigned int *res)
Definition: jpegSize.cpp:216

Variable Documentation

§ infile

FILE* infile

Definition at line 56 of file jpegSize.cpp.

Referenced by getJPEGSize().