Loading Image Files


PokeParadox

Founder of Pirate Games - Penjin Coder
Staff member
Joined
Dec 8, 2005
Messages
6,603
Age
40
Location
UK
Website
pokeparadox.itch.io
WEBSITE
https://github.com/pokeparadox
YOUTUBE
pokeparadox
I'm using Ryleh's minimal lib, and I'm just wondering how to load images. I've tried figuring out myself, and haven't been too sucessful.

I would at least like to be able to load BMPs but would prefer to load PNGs.

And related, how would I go about creating a "sprite" in the minimal lib?

Sorry if I'm being a lame-ass! :)
 
There's no built in support, so you need to use the standard file i/o system. BMPs are really easy to parse though, so your probably best off with those.
 
PokeParadox posted on Mar 23 2006 at 01:35 PM said:
I load them to rects? Is that right?


............


You blit them. You copy one area of memory (the source -- which is where your particular image data is stored) to another area of memory (the destination -- an offset in the framebuffer).
 
Last edited by a moderator:
I use a custom tool on the PC that i wrote myself to convert BMP files to my own file format. This file format can store several images in the one file. The image data is stored in the file in the same 16-bit RGB565 format that the GP2X uses natively. On the GP2X side, I load my custom file in one file read and my images are instantly ready to use for blitting using the hardware blitter.

Of course, this is my own custom solution and is only one of many ways to load images.
 
pertch posted on Mar 24 2006 at 03:07 AM said:
PokeParadox posted on Mar 23 2006 at 01:35 PM said:
I load them to rects? Is that right?
You blit them. You copy one area of memory (the source -- which is where your particular image data is stored) to another area of memory (the destination -- an offset in the framebuffer).
OK this is kinda getting me frustrated... I am asking how to blit them... >_>

Now please someone give me a straightforward answer and/or point me in the right direction to read up on this or even a simple source example.
 
Last edited by a moderator:
You've already been given several straightforward answers. You are using the minimal lib. It's called minimal because it contains the minimum needed to access the hardware. It doesn't contain various image/video/sound parsers. What you're supposed to do is either implement some bmp/png etc loading functions or use something like libpng which will do the loading and parsing for you. You then end up with a block of memory you can blit (memcpy) to the framebuffer. If you are looking for a simple load-parse-blit solution that already exists then you'll either have to wait and see if someone posts their code for you, or switch to a higher level library like SDL.
 
Thanks, I get patronised yet again. Fine I will work at it myself.

Incidently, thanks Slygamer, your answer was actually fairly useful.

EDIT: Sorry, I'm probably coming off as rude... I'm not in one of my better moods.

I know I have to load, parse then blit... I was merely asking for a push in the right direction in doing so. I have had a look at the libpng docs, and I don't think I fully understand them.
 
This code is from MAME GP2X. I show a previously loaded BMP with 320x240 256 colors:

Code:
static void load_bmp_8bpp(unsigned char *out, unsigned char *in) 
{
	int i,x,y;
	unsigned char r,g,b,c;

	in+=14; /* Skip HEADER */
	in+=40; /* Skip INFOHD */
	
	/* Set Palette */
	for (i=0;i<256;i++) {
  b=*in++;
  g=*in++;
  r=*in++;
  c=*in++;
  gp2x_video_color8(i,r,g,b);
	}
	gp2x_video_setpalette();
	/* Set Bitmap */	
	for (y=239;y!=-1;y--) {
  for (x=0;x<320;x++) {
  	*out++=in[x+y*320];
  }
	}
}
 
PokeParadox posted on Mar 24 2006 at 09:16 AM said:
Thanks, I get patronised yet again. Fine I will work at it myself.

Incidently, thanks Slygamer, your answer was actually fairly useful.

EDIT: Sorry, I'm probably coming off as rude... I'm not in one of my better moods.

I know I have to load, parse then blit... I was merely asking for a push in the right direction in doing so. I have had a look at the libpng docs, and I don't think I fully understand them.
To load and parse, you need to read from the file and interpret the file format. All the file formats are completely different so you probably want to pick one to use. BMP is easiest but the files are large. You should be able to google on BMP FILE FORMAT or something similar to get detailed file format information. if you want to use PNG or JPG those formats are very complex so the only practical way to use them is to use libraries -- for example libpng.a. Using libpng is not completely simple but there is sample code on the internet you can work from. Expect to spend a few evenings working through how to get png's to load.

Once you have it in memory, "blitting" can be done two ways. You could try to use the hardware blitter on the gp2x, but that is an advanced topic and I wouldn't start there myself (although if you search the archives of this forum there is enough info to get that working). Alternatively, you can just copy the memory.

Just copying the memory is pretty simple. The minlib gives you a pointer to the frame buffer, which in the default 16-bit mode is just a pointer to 320x240 16-bit numbers. The top row is the first 320 of those, then comes the second row, and so on. So you can use code like this to set pixels on the screen buffer:

void SetPixel(unsigned short *screen, int x, int y, unsigned short color)
{
screen[y * 320 + x] = color;
}

for efficiency you'll want to copy more data inside of a loop instead of calling this function for each pixel, like in the previous example posted in this thread illustrates. For further efficiency you can optimize things using assembler -- I think Radek posted some blitting functions that are very efficient a couple weeks ago.
 
Last edited by a moderator:
Back
Top