PDA

View Full Version : GS2 BMP Image Saver


Tigairius
06-27-2008, 10:45 PM
It was very difficult for me to decide on what kind of method I wanted to use to do this. I wasn't sure if the colors should be a multi-dimensional array for the sake of easy functionality & testing, or an array, each color having it's own specific number that has to be calculated before the image is generated the sake of clarity. I thought I was going to do a GPaint program in GS2, but the screenshot functions won't work properly for me, and only save 256x256px or so on the top left corner of my screen, so I decided to call it a dead project, however, I don't want to let it go to waste, so I will provide you with my latest function.


SaveBMPImage(file path, w, h, bits per pixel (default is 24 for clearest picture), color definitions);


I also noticed that Photoshop saved a filetype called ".raw" but it can rarely be retrieved by any programs, however it would be easy to implement, so if anyone wants it just let me know and I'll post it in here.

I tried to make the comments as clear as possible, but without researching on how images work and how they're generated, it may still be hard to understand what's going on in the script.


function convertrgbtoascii(c) {
return char(int(c[0])) @ char(int(c[1])) @ char(int(c[2]));
}

function SaveBMPImage(filename, w, h, bpp, colors) {
temp.filename = filename.ends(".bmp") ? filename:(filename @ ".bmp");
for (temp.c = 0; temp.c < colors.size(); temp.c ++) {
temp.save @= convertrgbtoascii(colors[c]);
}
temp.a = char(0); // null char
temp.imgsize = 55 + save.length();
temp.f = "BM" // magic number [leave alone]
@ char(imgsize) @ a @ a @ a // file size
@ a @ a // reserved [leave alone]
@ a @ a // reserved [leave alone]
@ "6" @ a @ a @ a // offset
@ char(40) @ a @ a @ a //size of header
@ char(w) @ a @ a @ a // px w
@ char(h) @ a @ a @ a // px h
@ char(1) @ a // color plane [leave alone]
@ char(bpp == null ? 24 : bpp) @ a // bits per px [24 = default]
@ char(0) @ a @ a @ a // compression method [leave it be for bmp ;)]
@ char(0) @ a @ a @ a // image size [leave 0 for regular bmp]
@ char(0) @ a @ a @ a // horizontal resolution
@ char(0) @ a @ a @ a // vertical resolution
@ char(0) @ a @ a @ a // number of colors
@ char(0) @ a @ a @ a; // number of important colors
temp.f @= save; // BGR (raster scan is backwards)
temp.f @= char(0);
temp.f.savestring(filename, 0);
return imgsize;
}

You can sort of debug your scripts if you decide to edit the function by adding this before the 'return imgsize;' on the function:

echo("Image Generated: " @ imgsize @ " bytes (" @ imgsize * 0.01 @ " KB) large in: " @ filename);


And an example of this function being used serverside (it works clientside too):

function onCreated() {
onSaveTest();
}

function onSaveTest(){
temp.colors = {
{255, 0, 0}, {0, 255, 0}, {0, 0, 255}
};

SaveBMPImage("levels/images/untitled", 3, 1, 24, colors);
}


Will save an image 3 pixels wide, and 1 pixel tall, pixels going in order of: blue, green, red. It will be saved in the levels/images/ folder as untitled.bmp.

You can also change the bpp (24) to other modes, like 8 or 16 to get less color quality.

You will notice that the channels are reversed (more specifically the blue & red switched places), this is due to the fact that raster scans are performed backwards (scans used by programs to read the image's data).

I was going to use the LZW algorithm to try to do GIFs and PNGs but it fell apart and I never finished working on it. I may provide an update later on supporting PNGs and GIFs.

cbk1994
06-28-2008, 12:09 AM
BRAVO! Good work :)

Admins
06-28-2008, 12:28 AM
Hmmm can eventually document makescreenshot2() better, next Graal version will also allow loading saved images (drawingpanel.saveimage()).

Generating images on-the-fly by script is quite interesting though, might need some optimization for big images. The function convertrgbtoascii() is missing though ?

Tigairius
06-28-2008, 12:32 AM
Generating images on-the-fly by script is quite interesting though, might need some optimization for big images. The function convertrgbtoascii() is missing though ?

Yeah, it is, forgot! Thanks for reminding me.
Edited the top post.

And these are the functions I was thinking of using instead of the multidimensional arrays:


function ColorToInt(r, g, b) {
return r * 256 ^ 2 + 256 * g + b;
}



function IntToColor(i) {
return {int((i / 256 ^ 2)) % 256, int((i / 256)) % 256, (i - 256 ^ 2) % 256};
}

ColorToInt(r, g, b) converts 3 numbers in to it's own unique number (thanks to PFA for that function/idea)
IntToColor(i) outputs the r, g, b generated by ColorToInt.

zokemon
06-29-2008, 04:20 AM
Looks very nice! Should be *255 though not 256 since color ranges go from 0 - 255 (I know there are 256 posibilities). Example:

a = getimgpixel("block.png", 2, 3);
player.chat = a[0]*255 SPC a[0]*256;

Will give you like 41 41.16078