AlbumShaper  1.0a3
Defines | 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.

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

{
  int c1, c2;
  c1 = NEXTBYTE();
  c2 = NEXTBYTE();
  if (c1 != 0xFF || c2 != M_SOI)
    return false;
  else
  {
    *res = c2;
    return true;
  }
}
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().

{
  //open file
  if ((infile = fopen(filename, READ_BINARY)) == NULL)
    return false;

  //this is scan_JPEG_header (int verbose)
  //Parse the marker stream until SOFn is seen;
  int marker;
  
  //Expect SOI at start of file
  if (!first_marker(&marker))
  {
    fclose(infile);
    return false;
  }
    
    /* Scan miscellaneous markers until we reach SOFn. */
  for (;;) 
  {
    if(!next_marker(&marker))
    {
      fclose(infile);
      return false;
    }

    switch (marker) 
    {
      /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
       * treated as SOFn.  C4 in particular is actually DHT.
       */
    case M_SOF0:    /* Baseline */
    case M_SOF1:    /* Extended sequential, Huffman */
    case M_SOF2:    /* Progressive, Huffman */
    case M_SOF3:    /* Lossless, Huffman */
    case M_SOF5:    /* Differential sequential, Huffman */
    case M_SOF6:    /* Differential progressive, Huffman */
    case M_SOF7:    /* Differential lossless, Huffman */
    case M_SOF9:    /* Extended sequential, arithmetic */
    case M_SOF10:   /* Progressive, arithmetic */
    case M_SOF11:   /* Lossless, arithmetic */
    case M_SOF13:   /* Differential sequential, arithmetic */
    case M_SOF14:   /* Differential progressive, arithmetic */
    case M_SOF15:   /* Differential lossless, arithmetic */      
      if(!process_SOFn(width, height))
      {
        fclose(infile);
        return false;
      }
      else
      {
        fclose(infile);
        return true;
      }
    case M_SOS:     /* stop before hitting compressed data */
    {
      fclose(infile);
      return false;
    }
    case M_EOI:     /* in case it's a tables-only JPEG stream */
    {
      fclose(infile);
      return false;
    }
    default:      /* Anything else just gets skipped */
      skip_variable();    /* we assume it has a parameter count... */
      break;
    }
  } /* end loop */


//cout << "ERROR!\n";
return false;

}
bool next_marker ( int *  res)

Definition at line 172 of file jpegSize.cpp.

References read_1_byte().

Referenced by getJPEGSize().

{
  int c;
  int discarded_bytes = 0;

  /* Find 0xFF byte; count and skip any non-FFs. */
  if(!read_1_byte(&c))
    return false;
  while (c != 0xFF) 
  {
    discarded_bytes++;
    if(!read_1_byte(&c))
      return false;
  }
  /* Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
   * are legal as pad bytes, so don't count them in discarded_bytes.
   */
  do 
  {
    if(!read_1_byte(&c))
      return false;
  } while (c == 0xFF);

//  if (discarded_bytes != 0) { cout << "Warning: garbage data found in JPEG file\n"; }

  *res = c;
  return true;
}
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().

{
  unsigned int length;
  unsigned int image_height, image_width;
  int data_precision;
  
  if(!read_2_bytes(&length) ||
      !read_1_byte(&data_precision) ||
      !read_2_bytes(&image_height) ||
      !read_2_bytes(&image_width) )
      return false;

  width = (int) image_width;
  height = (int) image_height;
  return true;   
}
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().

{
  int c = NEXTBYTE();
  if (c == EOF)
    return false;
  else
  {
    *res = c;
    return true;
  }
}
bool read_2_bytes ( unsigned int *  res)

Definition at line 216 of file jpegSize.cpp.

References NEXTBYTE.

Referenced by process_SOFn(), and skip_variable().

{
  int c1, c2;
  c1 = NEXTBYTE();
  if (c1 == EOF)
    return false;
  c2 = NEXTBYTE();
  if (c2 == EOF)
    return false;
  *res = (((unsigned int) c1) << 8) + ((unsigned int) c2);
  return true;
}
bool skip_variable ( )

Definition at line 237 of file jpegSize.cpp.

References read_1_byte(), and read_2_bytes().

Referenced by getJPEGSize().

{
  unsigned int length;

  /* Get the marker parameter length count */
  if(!read_2_bytes(&length))
    return false;
  /* Length includes itself, so must be at least 2 */
  if (length < 2)
    return false;
  length -= 2;
  /* Skip over the remaining bytes */
  while (length > 0) 
  {
    int tmp;
    if(!read_1_byte(&tmp))
      return false;
    length--;
  }
  return false;
}

Variable Documentation

FILE* infile

Definition at line 56 of file jpegSize.cpp.

Referenced by getJPEGSize().