1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
struct Size
{
int Width=0;
int Height=0;
};
Size GetImageSize(const char *filename)
{
//Get File Data:
FILE* ImageFile = fopen(filename, "rb"); //read the file
char header[256];
fread(header, sizeof(unsigned char), 256, ImageFile); // read the 256-byte header
//Get Image Format:
int stringsize=strlen(filename);
char format[4]={filename[stringsize-3],filename[stringsize-2],filename[stringsize-1],'\0'};
//getting the size, by it's own format:
int width=0;
int height=0;
if(strcmp(format, "bmp")==0 || strcmp(format, "BMP")==0)
{
// extract image BMP height and width from header from 18 and 22 bytes:
width = *(int*)&header[18];
height= *(int*)&header[22];
printf("File format BMP:\nWidth: %d\nHeight: %d", width,height);
}
else if(strcmp(format, "jpg")==0 || strcmp(format, "JPG")==0)
{
size_t index =0;
do
{
if( strcmp(header, "SOF0")==0)
{
printf("hello");
width = *(int*)&header[index+2];
height= *(int*)&header[index+4];
break;
}
index++;
}while(index<strlen(header));
// extract image height and width from header
printf("File format JPG:\nWidth: %d\nHeight: %d", width,height);
}
else if(strcmp(format, "gif")==0 || strcmp(format, "GIF")==0)
{
// extract image height and width from header
int test=sizeof(GIFHEAD) + sizeof(BYTE) + sizeof(WORD)*3;
width = *(int*)&header[test];
height= *(int*)&header[test+sizeof(WORD)];
printf("File format GIF:\nWidth: %d\nHeight: %d", width,height);
}
fclose(ImageFile);
//return the image size:
Size Image;
Image.Width=width;
Image.Height=height;
return Image;
}
|