Here's a function to read the dimensions of image files (supports BMP, PNG, and DDS) without having to actually load the image into memory. I'm not sure of what use this has, but someone asked for a way to do this and as I thought it would be interesting to code, I did it.
Function(s) and example code:
` File number constant
#constant IMAGEINFOFILE 1
` Globals to store dimensions
global IMAGEWIDTH as dword
global IMAGEHEIGHT as dword
global IMAGEDEPTH as word
GetImageFileDimensions("image.bmp")
print "image.bmp details: "+str$(IMAGEWIDTH)+"x"+str$(IMAGEHEIGHT)+" "+str$(IMAGEDEPTH)+"-bit"
GetImageFileDimensions("image.png")
print "image.png details: "+str$(IMAGEWIDTH)+"x"+str$(IMAGEHEIGHT)+" "+str$(IMAGEDEPTH)+"-bit"
GetImageFileDimensions("image.dds")
print "image.dds details: "+str$(IMAGEWIDTH)+"x"+str$(IMAGEHEIGHT)+" "+str$(IMAGEDEPTH)+"-bit"
wait key
end
function GetImageFileDimensions(fileName as string)
local bitDepth as byte
local colorType as byte
local tempVar as integer
fileExt$ = lower$(right$(fileName, 4))
select fileExt$
` .BMP
case ".bmp"
open to read IMAGEINFOFILE, fileName
` Check for BMP signature
read word IMAGEINFOFILE, tempVar
if tempVar <> 19778 then goto GetImageFileDimensions_Error
` Read dimensions
skip bytes IMAGEINFOFILE, 16
read long IMAGEINFOFILE, IMAGEWIDTH
read long IMAGEINFOFILE, IMAGEHEIGHT
skip bytes IMAGEINFOFILE, 2
read word IMAGEINFOFILE, IMAGEDEPTH
close file IMAGEINFOFILE
endcase
` .PNG
case ".png"
open to read IMAGEINFOFILE, fileName
` Check for PNG signature
read long IMAGEINFOFILE, tempVar
read long IMAGEINFOFILE, tempVar2
if tempVar <> 0x474e5089 or tempVar2 <> 0xa1a0a0d then goto GetImageFileDimensions_Error
` Read dimensions
skip bytes IMAGEINFOFILE, 8
read long IMAGEINFOFILE, tempVar
IMAGEWIDTH = ConvertEndian(tempVar)
read long IMAGEINFOFILE, tempVar
IMAGEHEIGHT = ConvertEndian(tempVar)
` Calculate depth
read byte IMAGEINFOFILE, bitDepth
read byte IMAGEINFOFILE, colorType
select colorType
case 2
IMAGEDEPTH = bitDepth * 3
endcase
case 4
IMAGEDEPTH = bitDepth * 2
endcase
case 6
IMAGEDEPTH = bitDepth * 4
endcase
case default
IMAGEDEPTH = bitDepth
endcase
endselect
close file IMAGEINFOFILE
endcase
` .DDS (Doesn't return bit depth of compressed files)
case ".dds"
open to read IMAGEINFOFILE, fileName
` Check for DDS signature
read long IMAGEINFOFILE, tempVar
if tempVar <> 0x20534444 then goto GetImageFileDimensions_Error
` Read dimensions
skip bytes IMAGEINFOFILE, 8
read long IMAGEINFOFILE, IMAGEHEIGHT
read long IMAGEINFOFILE, IMAGEWIDTH
` Read depth if possible
skip bytes IMAGEINFOFILE, 60
read long IMAGEINFOFILE, tempVar
if tempVar && 0x00000040
skip bytes IMAGEINFOFILE, 4
read long IMAGEINFOFILE, tempVar
IMAGEDEPTH = tempVar
else
IMAGEDEPTH = 0
endif
close file IMAGEINFOFILE
endcase
` Unsupported file format
case default
exit prompt "GetImageFileDimensions", "File format not supported"
end
endcase
endselect
exitfunction 1
GetImageFileDimensions_Error:
IMAGEWIDTH = 0
IMAGEHEIGHT = 0
IMAGEDEPTH = 0
close file IMAGEINFOFILE
endfunction 0
function ConvertEndian(value as dword) ` Required to convert some PNG format values
result = ((value && 0xFF) << 24) || ((value && 0xFF00) << 8) || ((value && 0xFF0000) >> 8) || ((value && 0xFF000000) >> 24)
endfunction result