gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Extract bmp files from game boy camera .sav

#177938 - rombus - Wed May 15, 2013 3:21 am

Hello everyone! I'm trying to develop a linux native image extractor for game boy camera .sav's

From what I understand, the 4 shades of grey are represented with 2 bits. And so far I've managed to read every byte and mapp it to a color. But all I end up having is a black square.

Anyone has any information about how can I achieve this functionality?

Any tips are appreciated. I couldn't find much information on this topic.

Here's the algorithm I'm using to convert each byte from the sav to a RGB pixel. putColor, adds 3 values to the file (RGB values)

In pastebin http://pastebin.com/uSMT0Wpj


Thanks in advance!

#177939 - sverx - Wed May 15, 2013 8:53 am

A black square? Then it either means your file is all filled with zeros or you've got a problem in your code...
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS / GBA|A DS Homebrewer's Diary

#177940 - rombus - Wed May 15, 2013 12:00 pm

Thanks for the quick response sverx.
When I analyze the output.bmp the file is a lot bigger than it shoud be, and I notice too that the first data bytes are full of zeros. I'm kind of stuck with this. The fact that I'm picking every 2 bits and converting them to 3 R-G-B values into the output.bmp is right?

Here's a link for a zip file where I uploaded my code and the test .sav and .bmp I'm using.

totem.sav is the sav file from the gb camera.
totem1.bmp is the first photo of that sav (extracted with a win2 program)
When the program finish running it will generate a out.bmp (black in this case :c )

https://docs.google.com/file/d/0B1icVXjWL_BUMG5RZ0VDdmg2SW8/edit?usp=sharing

I'm copying the header from totem1.bmp though I programmed a little header in my code that I'm not using right now just to leave it out of generating problems.

#177941 - sverx - Thu May 16, 2013 10:10 am

totem1.bmp is a 16 color bitmap (4 bpp), which is using only palette entries number 0 (black), 7 (light gray), 8 (dark gray) and 15 (white) of a 'standard' 16 colors (a.k.a. EGA) palette.
To do the same, your program should read one byte from the original image (2bpp) and write 2 bytes (4 nibbles). You can achieve this with a little LUT and few bit manipulations such as

Code:
outbyte[0]=LUT[inbyte&0x03]|(LUT[(inbyte>>2)&0x03]<<4);
outbyte[1]=LUT[(inbyte>>4)&0x03]|(LUT[(inbyte>>6)&0x03]<<4);

where LUT[] is {0,7,8,15} of course :)
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS / GBA|A DS Homebrewer's Diary

#177942 - rombus - Thu May 16, 2013 9:42 pm

Thanks a lot sverx!

That is very useful. I'll need a couple of days to digest what you said as I'm kind of a noob in respect of treating with .bmp files.

When I have news I will post them here!


cheers :D

#177948 - rombus - Sat May 18, 2013 4:36 pm

Sverx! I got the colors working, from having a black square now I have a mess of black, white, light grey and dark grey pixels lol.

I quite didn't understand the code you share with me. And more importantly I don't think I understand what information I have to write to the file in the extra byte. Since I have to turn every byte of the binary into 2 bytes of the bmp, what do I have to put in that extra byte?

Edit: mm I think I'm already turning the 2bpp image into 4bpp image. But somehow I'm not getting the results I want. Heres the code updated: https://docs.google.com/file/d/0B1icVXjWL_BUUnRKSF9iV2xGOFk/edit?usp=sharing

#177949 - sverx - Mon May 20, 2013 1:24 pm

The matter is that you've got 4 pixels in each byte in the 'source' image, but you want to convert it into a 16 colors bitmap, so you can store only 2 pixels in a single byte. What you're doing in your code is storing one pixel only per byte, making the other pixel equal to 0 implicitly.

So, you can either make you program read one byte a time (remember it's 4 pixel) and make it write 2 bytes OR you can make it read 'half' byte and write 1 byte (2 pixels). But you can't write an half byte to the file...

The code I suggested you takes the input byte ('inbyte' = 4 pixels) and calculates the 2 output bytes outbyte[0] and outbyte[1] that you have to write in the file. It makes them using bit manipulations instead of using if-then-else constructs... it takes 2 bits from the original inbyte, uses them as indexes for LUT access and stores the result in the correct outbyte[] element. Check how bit manipulations works (I'm using 'AND' and 'LOGICAL RIGHT SHIFT' operators only, to extract the 2 bit values... and I'm using the 'OR' and 'LOGICAL LEFT SHIFT' operators to combine the 2 LUT values together)

I hope this helps you :)
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS / GBA|A DS Homebrewer's Diary

#177950 - rombus - Wed May 22, 2013 2:47 am

do'hhhhhhhhhhhhhh

after reading endless times your responses I now understand what you mean about what I was doing. I was trying to write 1 nibble at a time, and that's why I have a 0 nibble implicit. I will keep trying with this new info you gave me.

Thanks a lot!

#177951 - sverx - Wed May 22, 2013 12:18 pm

I really suggest you to read some about bitwise operators. C is king in this and in console programming you'll easily use these operators a lot.
I would start with http://en.wikipedia.org/wiki/Bitwise_operations_in_C to get an idea :)
_________________
libXM7|NDS programming tutorial (Italiano)|Waimanu DS / GBA|A DS Homebrewer's Diary

#177958 - rombus - Mon Jun 03, 2013 12:11 pm

Hey sverx! I finally got the program working. Thanks a lot! I uploaded here: http://code.google.com/p/rgbcdumper/ it may not be that efficient, but it works :-)

Totally understood what you explained to me in your 2? post and it's implemented in the code also.

Cheers!