Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   GS2 BMP Image Saver (https://forums.graalonline.com/forums/showthread.php?t=80268)

Tigairius 06-27-2008 09:45 PM

GS2 BMP Image Saver
 
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 pathwhbits 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(filenamewhbppcolors) {
  
temp.filename filename.ends(".bmp") ? filename:(filename ".bmp");
  for (
temp.0temp.colors.size(); temp.++) {
    
temp.save @= convertrgbtoascii(colors[c]);
  }
  
temp.char(0); // null char
  
temp.imgsize 55 save.length(); 
  
temp."BM" // magic number [leave alone]
    
char(imgsize) @ // file size
    
// reserved [leave alone]
    
// reserved [leave alone]
    
"6" // offset
    
char(40) @ //size of header 
    
char(w) @ // px w
    
char(h) @ // px h
    
char(1) @ a  // color plane [leave alone]
    
char(bpp == null 24 bpp) @ // bits per px [24 = default]
    
char(0) @ // compression method [leave it be for bmp ;)]
    
char(0) @ // image size [leave 0 for regular bmp]
    
char(0) @ // horizontal resolution 
    
char(0) @ // vertical resolution 
    
char(0) @ // number of colors
    
char(0) @ a// number of important colors
  
temp.@= save// BGR (raster scan is backwards)
  
temp.@= char(0);
  
temp.f.savestring(filename0);
  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 = {
    {
25500}, {02550}, {00255}
  };
  
  
SaveBMPImage("levels/images/untitled"3124colors);


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-27-2008 11:09 PM

BRAVO! Good work :)

Admins 06-27-2008 11:28 PM

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-27-2008 11:32 PM

Quote:

Originally Posted by Stefan (Post 1399817)
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:

NPC Code:

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



NPC Code:

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 03: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:

PHP Code:

getimgpixel("block.png"23);
player.chat a[0]*255 SPC a[0]*256

Will give you like 41 41.16078


All times are GMT +2. The time now is 11:53 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.