Image Dimensions (original) (raw)

These utility routines look at the headers of JPEG and PNG files to find the pixel dimensions of any images supplied by the user for cover art and figures.


int ImageFiles::get_JPEG_dimensions(FILE *JPEG_file, unsigned int *width, unsigned int *height) { unsigned int sig, length; int marker;

if (![BinaryFiles::read_int16](6-bf.html#SP1)(JPEG_file, &sig)) return FALSE;
if (sig != 0xFFD8) return FALSE; 

do {
    do {
        marker = getc(JPEG_file);
        if (marker == EOF) return FALSE;
    } while (marker != 0xff); 

    do {
        marker = getc(JPEG_file);
    } while (marker == 0xff); 

    if (![BinaryFiles::read_int16](6-bf.html#SP1)(JPEG_file, &length)) return FALSE; 

    switch(marker) {
        
        case 0xc0:
        case 0xc1: case 0xc2: case 0xc3:
        case 0xc5: case 0xc6: case 0xc7:
        case 0xc9: case 0xca: case 0xcb:
        case 0xcd: case 0xce: case 0xcf: {

            
            if (getc(JPEG_file) == EOF) return FALSE; 

            if (![BinaryFiles::read_int16](6-bf.html#SP1)(JPEG_file, height)) return FALSE;
            if (![BinaryFiles::read_int16](6-bf.html#SP1)(JPEG_file, width)) return FALSE;

            return TRUE;
        }
        default:
            if (fseek(JPEG_file, (long) (length - 2), SEEK_CUR) != 0) return FALSE; 
    }
}
while (marker != EOF);

return FALSE;

}

int ImageFiles::get_PNG_dimensions(FILE *PNG_file, unsigned int *width, unsigned int *height) { unsigned int sig1, sig2, length, type;

if (![BinaryFiles::read_int32](6-bf.html#SP1)(PNG_file, &sig1)) return FALSE;
if (![BinaryFiles::read_int32](6-bf.html#SP1)(PNG_file, &sig2)) return FALSE;
if ((sig1 != 0x89504e47) || (sig2 != 0x0d0a1a0a)) return FALSE;


if (![BinaryFiles::read_int32](6-bf.html#SP1)(PNG_file, &length)) return FALSE;
if (![BinaryFiles::read_int32](6-bf.html#SP1)(PNG_file, &type)) return FALSE;


if (type != 0x49484452) return FALSE;


if (![BinaryFiles::read_int32](6-bf.html#SP1)(PNG_file, width)) return FALSE;
if (![BinaryFiles::read_int32](6-bf.html#SP1)(PNG_file, height)) return FALSE;
return TRUE;

}