inc/jpegSize.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void jpegSize (char *fileName, int *width, int *height)


Function Documentation

void jpegSize ( char *  fileName,
int *  width,
int *  height 
)

Definition at line 31 of file jpegSize.c.

References errAbort(), FALSE, freez(), M_EOI, M_JFIF, 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_SOI, M_SOS, MAX_SECTIONS, mustOpen(), needMem(), and TRUE.

00034 {
00035 FILE * infile = mustOpen(fileName, "r"); 
00036 int sectionsRead = 0;
00037 boolean done = FALSE;
00038 boolean foundJFIF = FALSE;
00039 /* Scan the JPEG headers. */
00040 if (fgetc(infile) != 0xff || fgetc(infile) != M_SOI)
00041     errAbort("error reading jpg header: %s",fileName);
00042 while(!done)
00043     {
00044     int itemlen;
00045     int marker = 0;
00046     int ll,lh, got;
00047     int a=0;
00048     uchar * data;
00049 
00050     if (sectionsRead >= MAX_SECTIONS)
00051         errAbort("Too many sections in jpg file: %s",fileName);
00052 
00053     for (a=0;a<7;a++)
00054         {
00055         marker = fgetc(infile);
00056         if (marker != 0xff) 
00057             break;
00058         if (a >= 6)
00059             errAbort("too many padding bytes: %s",fileName);
00060         }
00061 
00062     /* 0xff is legal padding, but if we get that many, something's wrong. */
00063     if (marker == 0xff)
00064         errAbort("too many padding bytes: %s",fileName);
00065 
00066     /* Read the length of the section. */
00067     lh = fgetc(infile);
00068     ll = fgetc(infile);
00069 
00070     itemlen = (lh << 8) | ll;
00071 
00072     if (itemlen < 2)
00073         errAbort("invalid jpeg marker: %s",fileName);
00074 
00075     data = (uchar *)needMem(itemlen);
00076     if (data == NULL)
00077         errAbort("Could not allocate %d bytes memory", itemlen);
00078 
00079     /* Store first two pre-read bytes. */
00080     data[0] = (uchar)lh;
00081     data[1] = (uchar)ll;
00082 
00083     got = fread(data+2, 1, itemlen-2, infile); /* Read the whole section. */
00084     if (got != itemlen-2)
00085         errAbort("Premature end of file?: %s",fileName);
00086     
00087     ++sectionsRead;
00088 
00089     switch(marker)
00090         {
00091         case M_SOS:   /* stop before hitting compressed data */
00092             done = TRUE;
00093             break;
00094         case M_EOI:   /* in case it's a tables-only JPEG stream */
00095             errAbort("No image in jpeg!: %s",fileName);
00096         case M_JFIF:
00097             /* Regular jpegs always have this tag, 
00098                exif images have the exif marker instead or in addition 
00099                - could add check to make sure this is present
00100             */
00101             foundJFIF = TRUE;
00102             break;
00103 
00104         case M_SOF0:
00105         case M_SOF1:
00106         case M_SOF2:
00107         case M_SOF3:
00108         case M_SOF5:
00109         case M_SOF6:
00110         case M_SOF7:
00111         case M_SOF9:
00112         case M_SOF10:
00113         case M_SOF11:
00114         case M_SOF13:
00115         case M_SOF14:       
00116         case M_SOF15:
00117             *height = data[3]*256+data[4];
00118             *width  = data[5]*256+data[6];
00119             done = TRUE;
00120             break;
00121         default:
00122             /* Skip any other sections. */
00123             break;
00124         }
00125         
00126     freez(&data);
00127     
00128     }
00129 fclose(infile);
00130 if (!foundJFIF)
00131     errAbort("JFIF marker not found jpeg: %s",fileName);
00132 return;
00133 }

Here is the call graph for this function:


Generated on Tue Dec 25 19:02:41 2007 for blat by  doxygen 1.5.2