Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:15 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Size of sprite / framebuffer

Author Message
elk1984

Senior Member

Joined: 11/07/2020
Location: United Kingdom
Posts: 228
Posted: 04:47pm 27 Sep 2020
Copy link to clipboard 
Print this post

At risk of someone pointing me straight to the manual (I have looked and checked in Peter's excellent Graphics Programming on the Colour Maximite 2) - is there a way to get the size of a sprite, loaded image or framebuffer.

The use case is I'd like to load a bitmap bounded by a box.  Of course I do know the size of the bitmap I'm loading, but if there was a way to get it from the file I'd prefer not to put hardcoded values in my program.


Thanks


elk1984
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 05:12pm 27 Sep 2020
Copy link to clipboard 
Print this post

You are asking three different things. For sprite use the SPRITE function for framebuffer use MM.INFO. For a BMP file you are going to need to read the file header and decode it.-
 
elk1984

Senior Member

Joined: 11/07/2020
Location: United Kingdom
Posts: 228
Posted: 06:13pm 27 Sep 2020
Copy link to clipboard 
Print this post

  matherp said  You are asking three different things. For sprite use the SPRITE function for framebuffer use MM.INFO. For a BMP file you are going to need to read the file header and decode it.-


Thanks - the mixing up probably reflects my head  .  What I'm trying to do is load a graphic into a sprite to put on screen in a bounded box.  If I use SPRITE LOADPNG, is there a way once it is loaded to access the height and width?
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:33pm 27 Sep 2020
Copy link to clipboard 
Print this post

These SUbs are from my FileManager program.
They give the vital stats for image files.

sub jpgInfo f$, onDisk AS INTEGER, iWdth AS INTEGER, iHt AS INTEGER, info$
 LOCAL INTEGER block
 LOCAL x$
 onDisk = MM.INFO(filesize f$)
 info$ = ""
 OPEN f$ FOR INPUT AS #2
 DO
   block = ASC(INPUT$(1,#2)) ' flag for start of block
   IF block = 255 THEN
     block = ASC(INPUT$(1,#2)) ' block type
     IF block = &hC0 THEN
       x$ = INPUT$(3,#2)
       iHt = ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
       iWdth = ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
       EXIT DO
     elseif block = &hC2 THEN
       info$ = "Progressive"
       x$ = INPUT$(3,#2)
       iHt = ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
       iWdth = ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
       exit do
     ENDIF
   ENDIF
 LOOP
 CLOSE #2
END SUB
 
sub bmpInfo f$, onDisk AS INTEGER, iWdth AS INTEGER, iHt AS INTEGER, info$
 LOCAL x$
 onDisk = MM.INFO(filesize f$)
 OPEN f$ FOR INPUT AS #2
 x$ = INPUT$(18,#2) ' skip this
 iWdth = ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256
 iWdth = iWdth +ASC(INPUT$(1,#2))*2^16+ASC(INPUT$(1,#2))*2^24
 iHt = ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256
 iHt = iHt +ASC(INPUT$(1,#2))*2^16+ASC(INPUT$(1,#2))*2^24
 x$ = INPUT$(2,#2) ' colour planes - not used
 info$ = str$(ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256)+" bit"
 CLOSE #2
END SUB
 
sub gifInfo f$, onDisk AS INTEGER, iWdth AS INTEGER, iHt AS INTEGER, info$
 LOCAL x$, iBits as integer
 onDisk = MM.INFO(filesize f$)
 OPEN f$ FOR INPUT AS #2
 x$ = INPUT$(6,#2) ' skip this GIF89a
 iWdth = ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256
 iHt = ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256
 x$ = INPUT$(1,#2) ' colour table start and bits
 iBits = (asc(x$) and &b00000111) + 1
 'if (asc(x$) and &b10000000) then iBits = 0 - iBits ' indicate animated image
 CLOSE #2
 info$ = str$(iBits)+" bit"
END SUB
 
sub pngInfo f$, onDisk AS INTEGER, iWdth AS INTEGER, iHt AS INTEGER, info$
 ' data from https://en.wikipedia.org/wiki/Portable_Network_Graphics
 LOCAL x$, iBits as integer
 onDisk = MM.INFO(filesize f$)
 OPEN f$ FOR INPUT AS #2
 x$ = INPUT$(12,#2)
 x$ = INPUT$(4,#2) ' should be IHDR
 iWdth = ASC(INPUT$(1,#2))*2^24 + ASC(INPUT$(1,#2))*2^16
 iWdth = iWdth +ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
 iHt = ASC(INPUT$(1,#2))*2^24 + ASC(INPUT$(1,#2))*2^16
 iHt = iHt +ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
 iBits = ASC(INPUT$(1,#2)) ' colour bits/pixel
 select case ASC(INPUT$(1,#2)) ' colour type
   case 0
     info$ = str$(iBits)+" bit Grayscale"
   case 2
     info$ = str$(iBits*3)+" bit Truecolour"
   case 3
     info$ = str$(iBits)+" bit Palettised"
   case 4
     info$ = str$(iBits*2)+" bit Grayscale+Alpha"
   case 6
     info$ = str$(iBits*4)+" bit RGBA"
   case else
     info$ = str$(iBits)+" bit/pixel"
 end select
 
 CLOSE #2
END SUB


Given the file name, they return sizes plus other info.

Jim
VK7JH
MMedit
 
elk1984

Senior Member

Joined: 11/07/2020
Location: United Kingdom
Posts: 228
Posted: 07:18am 28 Sep 2020
Copy link to clipboard 
Print this post

  TassyJim said  These SUbs are from my FileManager program.
They give the vital stats for image files.

sub jpgInfo f$, onDisk AS INTEGER, iWdth AS INTEGER, iHt AS INTEGER, info$
 LOCAL INTEGER block
 LOCAL x$
 onDisk = MM.INFO(filesize f$)
 info$ = ""
 OPEN f$ FOR INPUT AS #2
 DO
   block = ASC(INPUT$(1,#2)) ' flag for start of block
   IF block = 255 THEN
     block = ASC(INPUT$(1,#2)) ' block type
     IF block = &hC0 THEN
       x$ = INPUT$(3,#2)
       iHt = ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
       iWdth = ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
       EXIT DO
     elseif block = &hC2 THEN
       info$ = "Progressive"
       x$ = INPUT$(3,#2)
       iHt = ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
       iWdth = ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
       exit do
     ENDIF
   ENDIF
 LOOP
 CLOSE #2
END SUB
 
sub bmpInfo f$, onDisk AS INTEGER, iWdth AS INTEGER, iHt AS INTEGER, info$
 LOCAL x$
 onDisk = MM.INFO(filesize f$)
 OPEN f$ FOR INPUT AS #2
 x$ = INPUT$(18,#2) ' skip this
 iWdth = ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256
 iWdth = iWdth +ASC(INPUT$(1,#2))*2^16+ASC(INPUT$(1,#2))*2^24
 iHt = ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256
 iHt = iHt +ASC(INPUT$(1,#2))*2^16+ASC(INPUT$(1,#2))*2^24
 x$ = INPUT$(2,#2) ' colour planes - not used
 info$ = str$(ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256)+" bit"
 CLOSE #2
END SUB
 
sub gifInfo f$, onDisk AS INTEGER, iWdth AS INTEGER, iHt AS INTEGER, info$
 LOCAL x$, iBits as integer
 onDisk = MM.INFO(filesize f$)
 OPEN f$ FOR INPUT AS #2
 x$ = INPUT$(6,#2) ' skip this GIF89a
 iWdth = ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256
 iHt = ASC(INPUT$(1,#2))+ASC(INPUT$(1,#2))*256
 x$ = INPUT$(1,#2) ' colour table start and bits
 iBits = (asc(x$) and &b00000111) + 1
 'if (asc(x$) and &b10000000) then iBits = 0 - iBits ' indicate animated image
 CLOSE #2
 info$ = str$(iBits)+" bit"
END SUB
 
sub pngInfo f$, onDisk AS INTEGER, iWdth AS INTEGER, iHt AS INTEGER, info$
 ' data from https://en.wikipedia.org/wiki/Portable_Network_Graphics
 LOCAL x$, iBits as integer
 onDisk = MM.INFO(filesize f$)
 OPEN f$ FOR INPUT AS #2
 x$ = INPUT$(12,#2)
 x$ = INPUT$(4,#2) ' should be IHDR
 iWdth = ASC(INPUT$(1,#2))*2^24 + ASC(INPUT$(1,#2))*2^16
 iWdth = iWdth +ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
 iHt = ASC(INPUT$(1,#2))*2^24 + ASC(INPUT$(1,#2))*2^16
 iHt = iHt +ASC(INPUT$(1,#2))*256 + ASC(INPUT$(1,#2))
 iBits = ASC(INPUT$(1,#2)) ' colour bits/pixel
 select case ASC(INPUT$(1,#2)) ' colour type
   case 0
     info$ = str$(iBits)+" bit Grayscale"
   case 2
     info$ = str$(iBits*3)+" bit Truecolour"
   case 3
     info$ = str$(iBits)+" bit Palettised"
   case 4
     info$ = str$(iBits*2)+" bit Grayscale+Alpha"
   case 6
     info$ = str$(iBits*4)+" bit RGBA"
   case else
     info$ = str$(iBits)+" bit/pixel"
 end select
 
 CLOSE #2
END SUB


Given the file name, they return sizes plus other info.

Jim


That's fantastic - thanks TassyJim
 
elk1984

Senior Member

Joined: 11/07/2020
Location: United Kingdom
Posts: 228
Posted: 06:07pm 28 Sep 2020
Copy link to clipboard 
Print this post

Thanks for the inspiration Jim.  Using your code as a basis, I've reworked as an exercise in improving my knowledge of file handling, arrays etc.

The program needs 4 images (test.bmp, test.gif, test.jpg and test.png) in the same directory as the basic file.

image-info.zip

Any review comments welcome, especially that would help make this standalone and reusable for others.
Edited 2020-09-29 04:15 by elk1984
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025