jpegSize.cpp File Reference

#include "jpegSize.h"
#include <stdio.h>

Include dependency graph for jpegSize.cpp:

Go to the source code of this file.

Defines

#define NEXTBYTE()   getc(infile)
#define M_SOF0   0xC0
#define M_SOF1   0xC1
#define M_SOF2   0xC2
#define M_SOF3   0xC3
#define M_SOF5   0xC5
#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
#define M_EOI   0xD9
#define M_SOS   0xDA
#define M_APP0   0xE0
#define M_APP12   0xEC
#define M_COM   0xFE
#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


Define Documentation

#define M_APP0   0xE0

Definition at line 42 of file jpegSize.cpp.

#define M_APP12   0xEC

Definition at line 43 of file jpegSize.cpp.

#define M_COM   0xFE

Definition at line 44 of file jpegSize.cpp.

#define M_EOI   0xD9

Definition at line 40 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF0   0xC0

Definition at line 26 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF1   0xC1

Definition at line 27 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF10   0xCA

Definition at line 34 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF11   0xCB

Definition at line 35 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF13   0xCD

Definition at line 36 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF14   0xCE

Definition at line 37 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF15   0xCF

Definition at line 38 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF2   0xC2

Definition at line 28 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF3   0xC3

Definition at line 29 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF5   0xC5

Definition at line 30 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF6   0xC6

Definition at line 31 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF7   0xC7

Definition at line 32 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOF9   0xC9

Definition at line 33 of file jpegSize.cpp.

Referenced by getJPEGSize().

#define M_SOI   0xD8

Definition at line 39 of file jpegSize.cpp.

Referenced by first_marker().

#define M_SOS   0xDA

Definition at line 41 of file jpegSize.cpp.

Referenced by getJPEGSize().

 
#define NEXTBYTE (  )     getc(infile)

Definition at line 18 of file jpegSize.cpp.

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

#define READ_BINARY   "rb"

Definition at line 52 of file jpegSize.cpp.

Referenced by getJPEGSize().


Function Documentation

bool first_marker ( int *  res  ) 

Definition at line 149 of file jpegSize.cpp.

References M_SOI, and NEXTBYTE.

Referenced by getJPEGSize().

00150 {
00151   int c1, c2;
00152   c1 = NEXTBYTE();
00153   c2 = NEXTBYTE();
00154   if (c1 != 0xFF || c2 != M_SOI)
00155     return false;
00156   else
00157   {
00158     *res = c2;
00159     return true;
00160   }
00161 }

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().

00066 {
00067   //open file
00068   if ((infile = fopen(filename, READ_BINARY)) == NULL)
00069     return false;
00070 
00071   //this is scan_JPEG_header (int verbose)
00072   //Parse the marker stream until SOFn is seen;
00073   int marker;
00074   
00075   //Expect SOI at start of file
00076   if (!first_marker(&marker))
00077   {
00078     fclose(infile);
00079     return false;
00080   }
00081     
00082     /* Scan miscellaneous markers until we reach SOFn. */
00083   for (;;) 
00084   {
00085     if(!next_marker(&marker))
00086     {
00087       fclose(infile);
00088       return false;
00089     }
00090 
00091     switch (marker) 
00092     {
00093       /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
00094        * treated as SOFn.  C4 in particular is actually DHT.
00095        */
00096     case M_SOF0:    /* Baseline */
00097     case M_SOF1:    /* Extended sequential, Huffman */
00098     case M_SOF2:    /* Progressive, Huffman */
00099     case M_SOF3:    /* Lossless, Huffman */
00100     case M_SOF5:    /* Differential sequential, Huffman */
00101     case M_SOF6:    /* Differential progressive, Huffman */
00102     case M_SOF7:    /* Differential lossless, Huffman */
00103     case M_SOF9:    /* Extended sequential, arithmetic */
00104     case M_SOF10:   /* Progressive, arithmetic */
00105     case M_SOF11:   /* Lossless, arithmetic */
00106     case M_SOF13:   /* Differential sequential, arithmetic */
00107     case M_SOF14:   /* Differential progressive, arithmetic */
00108     case M_SOF15:   /* Differential lossless, arithmetic */      
00109       if(!process_SOFn(width, height))
00110       {
00111         fclose(infile);
00112         return false;
00113       }
00114       else
00115       {
00116         fclose(infile);
00117         return true;
00118       }
00119     case M_SOS:     /* stop before hitting compressed data */
00120     {
00121       fclose(infile);
00122       return false;
00123     }
00124     case M_EOI:     /* in case it's a tables-only JPEG stream */
00125     {
00126       fclose(infile);
00127       return false;
00128     }
00129     default:      /* Anything else just gets skipped */
00130       skip_variable();    /* we assume it has a parameter count... */
00131       break;
00132     }
00133   } /* end loop */
00134 
00135 
00136 //cout << "ERROR!\n";
00137 return false;
00138 
00139 }

bool next_marker ( int *  res  ) 

Definition at line 172 of file jpegSize.cpp.

References read_1_byte().

Referenced by getJPEGSize().

00173 {
00174   int c;
00175   int discarded_bytes = 0;
00176 
00177   /* Find 0xFF byte; count and skip any non-FFs. */
00178   if(!read_1_byte(&c))
00179     return false;
00180   while (c != 0xFF) 
00181   {
00182     discarded_bytes++;
00183     if(!read_1_byte(&c))
00184       return false;
00185   }
00186   /* Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
00187    * are legal as pad bytes, so don't count them in discarded_bytes.
00188    */
00189   do 
00190   {
00191     if(!read_1_byte(&c))
00192       return false;
00193   } while (c == 0xFF);
00194 
00195 //  if (discarded_bytes != 0) { cout << "Warning: garbage data found in JPEG file\n"; }
00196 
00197   *res = c;
00198   return true;
00199 }

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().

00262 {
00263   unsigned int length;
00264   unsigned int image_height, image_width;
00265   int data_precision;
00266   
00267   if(!read_2_bytes(&length) ||
00268       !read_1_byte(&data_precision) ||
00269       !read_2_bytes(&image_height) ||
00270       !read_2_bytes(&image_width) )
00271       return false;
00272 
00273   width = (int) image_width;
00274   height = (int) image_height;
00275   return true;   
00276 }

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().

00203 {
00204   int c = NEXTBYTE();
00205   if (c == EOF)
00206     return false;
00207   else
00208   {
00209     *res = c;
00210     return true;
00211   }
00212 }

bool read_2_bytes ( unsigned int *  res  ) 

Definition at line 216 of file jpegSize.cpp.

References NEXTBYTE.

Referenced by process_SOFn(), and skip_variable().

00217 {
00218   int c1, c2;
00219   c1 = NEXTBYTE();
00220   if (c1 == EOF)
00221     return false;
00222   c2 = NEXTBYTE();
00223   if (c2 == EOF)
00224     return false;
00225   *res = (((unsigned int) c1) << 8) + ((unsigned int) c2);
00226   return true;
00227 }

bool skip_variable (  ) 

Definition at line 237 of file jpegSize.cpp.

References read_1_byte(), and read_2_bytes().

Referenced by getJPEGSize().

00239 {
00240   unsigned int length;
00241 
00242   /* Get the marker parameter length count */
00243   if(!read_2_bytes(&length))
00244     return false;
00245   /* Length includes itself, so must be at least 2 */
00246   if (length < 2)
00247     return false;
00248   length -= 2;
00249   /* Skip over the remaining bytes */
00250   while (length > 0) 
00251   {
00252     int tmp;
00253     if(!read_1_byte(&tmp))
00254       return false;
00255     length--;
00256   }
00257   return false;
00258 }


Variable Documentation

FILE* infile

Definition at line 56 of file jpegSize.cpp.

Referenced by getJPEGSize().


Generated on Thu Jan 3 10:54:41 2008 for AlbumShaper by  doxygen 1.5.4