Load PNG from memory

BlitzPlus Forums/BlitzPlus Programming/Load PNG from memory

Imphenzia(Posted 2003) [#1]
Loading PNG images from the memory instead of the drive would be nice, but it's not still possible I suppose =)

I'd like to do this so I can encrypt png files and then load the file into memory, decode the encryption in the memorybank then use the LoadImage command on the memory bank. I am quite certain it's not possible with blitz, and I also assume that a PNG decoder written in blitz would be too slow.

(I would like to avoid using the existing "packers" if possible, that's not the solution I've got in mind for a number of reasons)

Any thoughts?


Seldon(Posted 2003) [#2]
Using DECLS, you could try to use the API commands for retrieving files from an EXE's resources (LoadResource(), LockResource(), etc..). You must use a program like ResHacker to add all the PNG files into your Blitz EXE. I think you can get a valid pointer to data, but then I don't know if you can use Blitz bank commands with it. I don't have B+ yet so I have to try (but still you'd need to code your own PNG decoder in Blitz).

Another solution is to pack the PNG files into an external DLL's resources. You call the DLL from the Blitz program and you get a pointer to the bitmap data (in Blitz, you pass a bank). Then, using a Bank2Image function you poke the bitmap data to a valid image created with Blitz. I think this is a solution that can work indeed.

The DLL should unpack and decode the PNG files into flat bitmap data. Such task could be done easily with PureBasic (it has an internal PNG decoder and a general packer).


Clyde(Posted 2003) [#3]
What benefit would this have? As Im interested in reading on your findings!


Imphenzia(Posted 2003) [#4]
Mikey, not sure whether you're asking about the benefit of my question or Seldon's comments. Anyhow, in my case, I want to encrypt a .PNG file as it is saved to the disk so that any graphics program doesn't recognize it allowing easy modifications etc.

The benefit of being able to do this is that a file can be loaded from the disk into a memory bank and manipulated into a recognized PNG file in the process. Following this, the image can be loaded as an image into the video memory using a LoadImage command from a memory bank.

The reason why I want to avoid bundling files with my executables or as pak files is that the game is allowing custom addon packs (and there's over 100MB of graphics to begin with). My car / circuit editors would then export the images to the encrypted file format. Another reason for avoiding so called "packers" is that I believe they uncompress the files temporarily which means that if the computer crashes, the files may be left exposed (I think).

Stefan


Perturbatio(Posted 2003) [#5]
I once wrote some functions that were the start of loading PNG's from a bank and saving to a bank (It should be in the code archives somewhere).


Seldon(Posted 2003) [#6]
I think the idea of a DLL keeping the images in its resources as packed/compressed data can work (you have just to change the DLL and you have another set of gfx). I've already such kind of thing working and I could make it indeed. Since the data can be compressed, the use of PNG is not that useful, that's why I'd use normal BMP.

The Blitz program should have two functions:

ListImages() -> it returns the list of images

image_handle=LoadPackedImage("name") -> it loads a packed/compressed image from inside the DLL.

The best would be to have a configuration tool to put inside the DLL all the needed images. I have no time (work) to realize the configuration tool, but I can do the DLL and Blitz functions easily. If someone wants to start a colaboration, we could do a nice thing for all Blitz users (it can work for both B2D/3D or B3D/B+ using DECLS).


Perturbatio(Posted 2003) [#7]
The problem with storing the images as a res is that it's a pain in the butt to write resources to an executable module in win9x/ME since it doesn't support UpdateResource (and it's companion commands).


Perturbatio(Posted 2003) [#8]
They could however be stored as a compressed file with optional encryption (i.e. blowfish), the individual files could be relatively easily unencrypted and decompressed in memory.


Skitchy(Posted 2003) [#9]
Just use MoleBox :
http://www.molebox.com/

If you want to protect EVERYTHING (music,sounds,images,models,textures,dll's) it's perfect. Costs about £70, but I think it was worth every penny.
:)


Seldon(Posted 2003) [#10]
Yes, dealing with resources needs some work. You can use an external file of course, the reason I suggested a DLL is that since it'd be very difficult to write a good compression&encryption function with Blitz (since it has no pointers, etc..) , to have a DLL with files inside, it seems more handy. Besides, usually people don't pay attention to DLL if they're looking for pictures.


Seldon(Posted 2003) [#11]
Little update:

I successfully did a tiny converter that takes a BMP/PNG/JPG/TGA/TIFF picture, decodes to Blitz's Image format and compresses it. A 27KB BMP has been compressed in 882 bytes !! The data will be stored in the resources of a DLL that will be called by Blitz (I still have to finish this part).


Imphenzia(Posted 2003) [#12]
It's a good feature, especially for smaller games I think! Is it using RLE compression? Does that util actually create the DLL by the way?


Seldon(Posted 2003) [#13]
That's what I'd like to do. At the moment, it creates a .RES file to be added to DLL using a tool like ResHacker (btw, do you know some other tools like this?). The DLL is general purpose, you don't need to change it when you change the pictures inside.


Seldon(Posted 2003) [#14]
Ok, just to let you know that I've successfully included a compressed image in the Blitz's EXE as a real resource. The program loads data from resources, uncompresses all and gets a normal Blitz image.

No temporary saving on hard-disk, no waste of memory and very fast speed (it's faster than loading it from disk).

From Blitz's side you have only to do:

image=LoadResourceImage(num_of_resource)

and you get a valid Blitz image handle. At the moment I'm using an external DLL (13KB long) to make decompression, with B+ I may not need any DLL, but unfortunately there's no efficient compression/decompression routine written in Blitz. Does someone know some compression code to look and possibly translate in Blitz ?

If someone wants the sample source in Blitz, the translater tool and the DLL, email me at: h.seldon@...


Imphenzia(Posted 2003) [#15]
There are some compression routines in the code archives for RLE and Huffman, but I don't think they are fast enough.


Seldon(Posted 2003) [#16]
Thank you. I tried the Huffmann staff, it'd be fast enough but the compression ratio is low. My DLL compressed a 24bit 500KB BMP in 12KB. The Blitz EXE was only 12KB longer and it taked 13 millisecs to show the image in a 800x600 screen (I've a Celeron@1GHz). The Huffmann code did 124KB for the same BMP.. not so bad, maybe I could use it. Though I want to try to make the LZW stuff on Blitz (hard work I know :-).

The Blitz Real Packer
---------------------

- Loads several formats (BMP,PNG,TIFF,JPG)
- Compresses and packs your pictures inside your Blitz's EXE as real Windows resources
- Real resource means: 1) no temporary swapping on HD 2) no waste of memory as the only used memory will be the one needed by CreateImage() 3) professional look of your applications
- Easy to use: in your source you've only to do:
image=LoadResourceMemory(num_of_resource)
- Compatible with Blitz 2D and 3D/B+ .
- GUI to manage all the pictures

... soon available :-)