JPEG files consist of a series of component structures with each structure beginning with a 2 byte marker. The image dimensions are stored in the "Start Of Frame" structure. The first byte of a marker is always 0xFF. The second byte specifies the type of component structure. Some markers are stand alone while others are followed by additional bytes. If a component structure is more than just the marker, the two bytes immediately following the marker specify the size of the structure including the structure size bytes, but not the marker bytes. The 2 byte size, as well as the 2 byte image dimensions, are stored in big-endian format, that is, the first byte is more significant than the second.

To find the image dimensions, the JPEG file is scanned from the beginning. Component structures are skipped until the Start Of Frame structure is found. There is only one Start Of Frame structure per image, however, images can contain thumbnails images that are nested within application specific structures. This prohibits the file from simply being scanned for a Start Of Frame marker. There are 13 different Start Of Frame markers, but the image dimensions are stored the same way for all of them. JPEG files may also contain padding bytes before any of the markers. These padding bytes have the value of 0xFF.

In the following code segment, Data is a pointer to the file's array of bytes and Size is the number of bytes in the file.

Code:
int Width;
int Height;
int Index;
unsigned char Marker;
int StructureSize;

Width = 0;
Height = 0;
Index = 0;
while (Index < Size)
{
	if (Data[Index] == 0xFF)
	{
		// Get the component structure marker.
		Index = Index + 1;
		do
		{
			if (Index < Size)
			{
				Marker = Data[Index];
				Index = Index + 1;
			}
			else
			{
				Marker = 0xFF;
			}
		} while ((Marker == 0xFF) && (Index < Size));

		// Determine the componsent structure size.
		if (((Marker >= 0xD0) && (Marker <= 0xD9)) || (Marker == 0x01))
		{
			StructureSize = 0;
		}
		else if (Index < (Size - 1))
		{
			StructureSize = (Data[Index] << 8) | Data[Index + 1];
		}
		else
		{
			StructureSize = 1;
		}
		
		// Check if the marker is for the Start Of Frame component structure.
		if (((Marker & 0xF0) == 0xC0) && (Marker != 0xC4) && (Marker != 0xC8) && (Marker != 0xCC) && (Index < (Size - 6)))
		{
			Height = (Data[Index + 3] << 8) | Data[Index + 4];
			Width = (Data[Index + 5] << 8) | Data[Index + 6];
			Index = Size;
		}

		Index = Index + StructureSize;
	} // end if (Data[Index] == 0xFF)
	else
	{
		Index = Size;
	}
} // end while (Index < Size)