IonicWind Software

IWBasic => Games and Graphics => Topic started by: billhsln on February 20, 2007, 11:27:40 AM

Title: JPG file specs
Post by: billhsln on February 20, 2007, 11:27:40 AM
I know there is a command to get the height and width of a BMP (GETBITMAPSIZE).  Is there an equivalent for JPG and other format graphic files?

Thanks,
Bill
Title: Re: JPG file specs
Post by: LarryMc on February 20, 2007, 12:51:37 PM
I use a 3rd party dll that allows me load various types of images as bitmaps.  It also allows me to scale the images, and adjust contrast/brightness and other various functions
You can find the dll and the source for a program that I use it in at the following location.

http://www.codingmonkeys.com/index.php?action=tpmod;dl=item31 (http://www.codingmonkeys.com/index.php?action=tpmod;dl=item31)


Title: Re: JPG file specs
Post by: billhsln on February 20, 2007, 02:17:07 PM
The program will load the file, what I want to do is display it's height and width.

I started with IMGVIEW.EBA and modified it to display 2 graphic files side by side with the dimensions on top.  However GETBITMAPSIZE only seems to work for BMP's and I am using JPG's.  JPG's return 0x0 from GETBITMAPSIZE.   So, what I am looking for is a subroutine to get a JPG's dimensions.

Bill

Here is my code.

REM image viewer v1.1 Requires EBASIC Version 1.0 or greater
REM Shows usage of a @NOAUTODRAW window and manipulation of loaded images
REM Compile as a WINDOWS target

def w1:window
def bitmap1,bitmap2:uint
def bmw1,bmh1,bmw2,bmh2:uint
def x,y,w,h,mw,mh,scale:int
def filename,filter:string

getscreensize(w,h)
w-=40
h-=40

OPENWINDOW w1,0,0,w,h,@MINBOX|@MAXBOX|@SIZE|@NOAUTODRAW,0,"Image viewer",&mainwindow
BEGINMENU w1
MENUTITLE "&File"
MENUITEM "&Load Image 1",0,1
MENUITEM "Load &Image 2",0,2
MENUITEM "&Quit",0,3
MENUTITLE "Options"
MENUITEM "Scale To Window",0,4
ENDMENU

x=0
y=0
scale = 1
run = 1

waituntil run = 0

DeleteImage bitmap1,@IMGSCALABLE
DeleteImage bitmap2,@IMGSCALABLE

closewindow w1
end

SUB mainwindow
select @CLASS
case @IDMENUPICK
select @MENUNUM
case 1
filter = "Image files (*.bmp *.jpg *.gif)|*.bmp;*.jpg;*.gif|All Files (*.*)|*.*||"
filename = filerequest("Load Bitmap",w1,1,filter,"bmp")
if (len(filename) > 0)
    if (bitmap1)
DeleteImage bitmap1,@IMGSCALABLE
RECT w1,x,y,w,h,RGB(255,255,255),RGB(255,255,255)
endif
bitmap1 = LoadImage(filename,@IMGSCALABLE)
getbitmapsize(bitmap1,bmw1,bmh1)
UpdateImage()
move w1,0,0
print w1, str$(bmw1) + "x" + str$(bmh1)
endif
case 2
filter = "Image files (*.bmp *.jpg *.gif)|*.bmp;*.jpg;*.gif|All Files (*.*)|*.*||"
filename = filerequest("Load Bitmap",w1,1,filter,"bmp")
if (len(filename) > 0)
    if (bitmap2)
DeleteImage bitmap2,@IMGSCALABLE
RECT w1,x,y,w,h,RGB(255,255,255),RGB(255,255,255)
endif
bitmap2 = LoadImage(filename,@IMGSCALABLE)
getbitmapsize(bitmap2,bmw2,bmh2)
UpdateImage()
move w1,mw,0
print w1, str$(bmw2) + "x" + str$(bmh2)
endif
case 3
run = 0
case 4
IF scale THEN scale = 0 ELSE scale = 1
RECT w1,x,y,w,h,RGB(255,255,255),RGB(255,255,255)
UpdateImage()
endselect
case @IDMENUINIT
CheckMenuItem w1,4,(scale = 1)
case @IDCLOSEWINDOW
run=0
case @IDSIZE
GetClientSize w1,x,y,w,h
mw = (w - x)/2
mh = (h - y)/2
if (scale)
UpdateImage()
endif
case @IDPAINT
UpdateImage()
case @IDCREATE
CenterWindow w1
endselect
return
ENDSUB

SUB UpdateImage
if (bitmap1)
if (scale)
ShowImage w1,bitmap1,@IMGSCALABLE,x,y+20,mw-5,h-20
else
ShowImage w1,bitmap1,@IMGSCALABLE,x,y+20
endif
endif
if (bitmap2)
if (scale)
ShowImage w1,bitmap2,@IMGSCALABLE,mw+5,y+20,mw-5,h-20
else
ShowImage w1,bitmap2,@IMGSCALABLE,mw+5,y+20
endif
endif
RETURN
ENDSUB
Title: Re: JPG file specs
Post by: Ionic Wind Support Team on February 20, 2007, 02:38:20 PM
You need to use an image library, or some other method.  GETBITMAPSIZE does just what the name indicates.  Returns the size of a bitmap format image. 

There used to be some code on the old IBasic forums, written by Trondoc (god rest his soul), that would extract the sizes from a jpg file itself.

Paul.
Title: Re: JPG file specs
Post by: LarryMc on February 20, 2007, 02:55:33 PM
Quote from: Paul Turley on February 20, 2007, 02:38:20 PM
There used to be some code on the old IBasic forums, written by Trondoc (god rest his soul), that would extract the sizes from a jpg file itself.

Paul.

Here's the link to the code Paul is talking about.http://www.web-helper.net/PyxiaSAQ/ViewArticle.asp?1 (http://www.web-helper.net/PyxiaSAQ/ViewArticle.asp?1)
Title: Re: JPG file specs
Post by: billhsln on February 20, 2007, 03:06:17 PM
Thanks Larry and Paul.  That was exactly what I was looking for.

Bill
Title: Re: JPG file specs
Post by: Ionic Wind Support Team on February 20, 2007, 03:11:11 PM
It's in IBasic Standard code so will probably need some tweaking to make run in Emergence.
Title: Re: JPG file specs
Post by: billhsln on February 20, 2007, 03:53:22 PM
Compiled clean in EBasic.  Will be using it as a subroutine, so might not be too much of a problem.

Thanks again,
Bill
Title: Re: JPG file specs
Post by: barry on February 21, 2007, 01:46:34 PM
I had similar questions some time back in IBasic and someone posted the following code, which I used.  It worked just fine.  It compiled in a version of EBasic when I first got it and probably still will.

Barry


'GetPicSize

GLOBAL GetPicSize
SUB GetPicSize(filename:STRING, w:INT byref, h:INT byref),INT
string tmpstring

tmpstring = ucase$(right$(filename,3))
if tmpstring = "BMP"
GetBmpSize(filename, w, h)
else
if tmpstring = "JPG"
GetJpegSize(filename, w, h)
else
GetGifSize(filename, w, h)
endif
endif
return 0
EndSub

'GetJpegSize.inc

Sub GetJpegSize(FileName:string, W:int byref, H:int byref),int
BFILE F
ISTRING tmp[256]
INT a, b
IF OpenFile(F, FileName, "r") Then Return 0
__read F, tmp, 256
CloseFile F
b=0
For a = 0 to 255
IF tmp[a] = 255
b++
IF (b = 5) and (a<252)
a += 5
H = tmp[a] * 256 + tmp[a+1] :' reversed order
W = tmp[a+2] * 256 + tmp[a+3]
Return ( (W*H) <> 0)
Endif
Endif
Next a
Return 0
EndSub

' GetGifSize

Sub GetGifSize(FileName:string, W:word byref, H:word byref),int
  BFILE F
  if openfile(F, FileName, "r") then return 0
  read F, W
  read F, W
  read F, W
  read F, W
  read F, H
  closefile F
  return 0
endsub

'GetBmpSize
Sub GetBmpSize(FileName:string, W:int byref, H:int byref),int
int bitmap

bitmap = LoadImage(FileName,@IMGBITMAP)
GetBitmapSize(bitmap, W, H)
DeleteImage bitmap,@IMGBITMAP
Return 0
EndSub

Title: Re: JPG file specs
Post by: billhsln on February 21, 2007, 03:31:35 PM
Thanks Barry.  Some of my graphics files were not coming up with numbers.  So maybe this code will fix it.

Thanks again,
Bill