Saving Bitmaps as Files (Experiment)

Saving Bitmaps as Files

[SIZE=2]Ok, this isn't as much as a script as it is a tool to save bitmaps as data, then recover them later. It was nothing more than 10 minutes of working with the get_pixel and set_pixel methods. Anyways, here's the mini-code

module BitmapDump
  def self.save_bitmap(bitmap, filename)
    red = Table.new(bitmap.width, bitmap.height)
    green = Table.new(bitmap.width, bitmap.height)
    blue = Table.new(bitmap.width, bitmap.height)
    alpha = Table.new(bitmap.width, bitmap.height)
    for i in 0...bitmap.width
      for j in 0...bitmap.height
        color = bitmap.get_pixel(i, j)
        red[i, j] = color.red
        green[i, j] = color.green
        blue[i, j] = color.blue
        alpha[i, j] = color.alpha
      end
    end
    file = File.open("#{filename}.rxdata", 'wb')
      Marshal.dump([red, green, blue, alpha], file)
    file.close
  end
  def self.read_bitmap(filename)
    file = File.open("#{filename}.rxdata", 'rb')
      colors = Marshal.load(file)
    file.close
    red, green, blue, alpha = colors[0], colors[1], colors[2], colors[3], colors[4]
    bitmap = Bitmap.new(red.xsize, red.ysize)
    for i in 0...bitmap.width
      for j in 0...bitmap.height
        bitmap.set_pixel(i, j, Color.new(red[i, j], green[i, j], blue[i, j], alpha[i, j]))
      end
    end
    return bitmap
  end
end
Expand to see the code.

It is fairly simple to use:

To save a bitmap, simply use:
[/SIZE]
[SIZE=4][SIZE=2]BitmapDump.[/SIZE][/SIZE][SIZE=4][SIZE=2]save_bitmap(bitmap, filename)[/SIZE][/SIZE]
Expand to see the code.
[SIZE=2]

It will then save the bitmap as data in your game folder. I would make a new folder called Bitmap Data or something and have stuff save there, but that may just be me.

To Recover Saved Bitmap, use:
[/SIZE]
[SIZE=4][SIZE=2]bitmap = BitmapDump.read_bitmap(filename)[/SIZE][/SIZE]
Expand to see the code.
[SIZE=2]



Just a little trick someone might find some use for (I will be soon. ;))
[/SIZE]
is it possible to save s screenshot with it? what should be put for "bitmap" then?
No, it's not the screenshot script.

What it does is a walkaround to saving actual Bitmap objects since you can't normally dump bitmap data (I could never figure it out XD).

Bitmaps would be something like:'
bitmap = Bitmap.new(width, height)
Expand to see the code.


Actual objects of the Bitmap class.

I made this because I plan to do something like a visual equipment / character creator system, so rather than redrawing visual equipment sprites over the player every frame, this will take all layers, draw them on a bitmap and save the actual bitmap file.

I remember someone asking how to do this just a little while ago...
ok, I see. I'm sure this will become useful anyway :)
cool. Might use it for character creation. Great idea.

Edit; I think I asked for this. Can't remember.. stupid flu.
I use some kind of similar methods to do this, and I was wondering what would be the filesize of a 640*480 bitmap?
(too lazy to test...)
* goes off to test... *

Well, this little test was about 2.4 kb
bitmap = Bitmap.new(640, 480)
for i in 0...640
  color = Color.new(rand(255), rand(255), rand(255))
  bitmap.fill_rect(Rect.new(0, i, 640, 1), color)
end
Expand to see the code.
how come? what kind of bitmap is it? a bitmap of that size and color depth would have 900KB. 24 bits * 640 * 480 / 8 = 921,6kB = 900KB
is this script indexing colors somehow? it doesn't seem so but I can't really tell, and I'm curious :>
Basically, all it does is create tables of color values for each pixel. then saves the tables. It doesn't save how normal bitmap do.

You actually end up with 4 basically matricies with the red, green, blue and alpha values for every pixel.

I could have saved actuall color objects, but I feared that that may actually take more space then this.

Your tables would look something like this:

[ [255, 078, 050, 025],
  [000, 054, 100, 128],
  [000, 200, 100, 240],
  [042, 074, 182, 053] ]
Expand to see the code.


That would be for a 4 * 4 bitmap, and there would be 4 of those tables controlling each color value.

Something like that.
thx this is the only I needed to finish correctly the Character creator, because it have a bug, when you exit the game, you lost your character even when you save the variable ;).
So this thing incrypts the image and compresses it? And it also makes scripting character creators and things like that easier?
This sounds really good.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=7677