image attributes

BlitzMax Forums/BlitzMax Programming/image attributes

Nigel Brown(Posted 2008) [#1]
Is it possible to read an images width/height without having to load the image first? I need to load a multi-frame image with settings that can be determined from the file i.e.

LoadAnimImage("filename", width, height, 0, height/width, 0 )


Dreamora(Posted 2008) [#2]
No its not.
not without writing the code for it yourself and parsing the image file header depending on the image type.


Yan(Posted 2008) [#3]
pixmap:TPixmap = LoadPixmap("filename")
image:TImage = LoadAnimImage(pixmap, ...ETC...)

??


plash(Posted 2008) [#4]
?


tonyg(Posted 2008) [#5]
Windows Explorer manages to get them from somewhere. It might be a case of a good trawl through the API or that Internet thingy.
<edit> Doesn't seem to be an API and ms forums suggest .net image.width which doesn't help.


Nigel Brown(Posted 2008) [#6]
Thanks all, if the only way to find an image width / height is to load the image guess thats what I should do.


plash(Posted 2008) [#7]
Pretty straight forward: http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256AFB00139E67

Though its only for JPG/GIF and you have yet to mention what kind of image you want to analyze.

As Dreamora noted you can parse the header to figure out stuff like this, it all depends on the type of image (you should be able to find header info on wiki).


Bremer(Posted 2008) [#8]
I have made these functions and so far for all png images it seems to work, but with the jpg I have found a few images where it shows the sizes divided by 10 and I have not figured out why yet. But its a start and perhaps someone can improve on it.

Local width:Int = 0
Local height:Int = 0

imageWidthHeightPNG("5.png",width,height)
Print Hex(width)+","+width
Print Hex(height)+","+height

imageWidthHeightJPG("5.jpg",width,height)
Print Hex(width)+","+width
Print Hex(height)+","+height

End

Function imageWidthHeightPNG(imagePath:String,width:Int Var, height:Int Var)
	Local file:TStream = OpenStream(imagePath)
	If ReadShort(file) <> $5089 Then Return False	' not a png image file
	SeekStream(file,18)
	width = ReadByte(file)*256
	width :+ ReadByte(file)
	SeekStream(file,22)
	height = ReadByte(file)*256
	height :+ ReadByte(file)
	CloseStream(file)
End Function

Function imageWidthHeightJPG(imagePath:String,width:Int Var, height:Int Var)
	Local file:TStream = OpenStream(imagePath)
	If ReadShort(file) <> $D8FF Then Return False	' not a jpg image file
	Local b1:Byte,b2:Byte,test:Byte=$E1
	Local jump:Int
	While Not Eof(file)
		' locate jump instructions
		Repeat
			b1 = ReadByte(file)
		Until b1 = $FF
		b2 = ReadByte(file)
		If b2 > $E0 And b2 < $EE Then	' jump instruction found
			jump = ReadByte(file)*256+ReadByte(file)
			SeekStream(file,StreamPos(file)+jump-2)
		End If
		If b2 = $EE Then	' no more jumps
			' find width & height info
			Repeat
				b1 = ReadByte(file)
				If b1 = $FF Then b2 = ReadByte(file)
			Until (b1 = $FF And b2 = $C0) Or (b1 = $FF And b2 = $C1) Or (b1 = $FF And b2 = $C2) Or (b1 = $FF And b2 = $C3)
			' skip 3 bytes
			SeekStream(file,StreamPos(file)+3)
			height = ReadByte(file)*256
			height :+ ReadByte(file)
			width = ReadByte(file)*256
			width :+ ReadByte(file)
			CloseFile(file)
			Return
		End If
	Wend
End Function


[edit] I think that I figured out the jpg function. Some jpg files has embedded thumbnail images, and those need to be jumped past to get to the info of the actual image. So far I have not found any jpg images the above function cannot handle.


Hotshot2005(Posted 2008) [#9]
It got error saying "Unhandled Exception" and I am sure you can fixed da da bugs :)


Bremer(Posted 2008) [#10]
It got error saying "Unhandled Exception" and I am sure you can fixed da da bugs :)


You have to supply your own images to try it out. So change the paths from "5.png" in the function calls to paths of images you know exist on your system. Otherwise you will get errors ofcause since it cannot process a file which does not exist.