Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 06-27-2008, 09:45 PM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
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.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”

Last edited by Tigairius; 06-27-2008 at 11:32 PM..
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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