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.
PHP Code:
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.
PHP Code:
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:
PHP Code:
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):
PHP Code:
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.