[C++] Lightest-weight way of creating an image file?

suszterpatt

Senior member
Jun 17, 2005
927
1
81
I'm writing a small console app (virtual world management thingy, not very interesting), and I'd like to be able to create output in the form of an image file representing the world's current state. The catch is that I'd like to avoid dragging in libraries larger than life (e.g. libpng) just for that single feature, so I'd need something relatively simple.

Right now BMP seems the most straightforward, but I'd rather use a compressed fromat if possible. Any suggestions?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,631
4,558
75
BMP is about the simplest format you can use. Compression is always going to make creating a file more difficult, and more time-consuming.

If you're not using many colors, and you can find a small library, GIF would probably be the way to go. Otherwise, you might try PCX. It's not very well compressed, but it is compressed.
 

Red Squirrel

No Lifer
May 24, 2003
70,166
13,573
126
www.anyf.ca
BMP is definably the easiest route. You can actually read that like a book in a hex editor. The header part I'm not too too sure how to read but guessing it's not too hard to figure out. But once you get to the body each 3 bytes is basically a pixel in RGB format. Though I think they're all reversed so the first pixel is actually the lowest right pixel of the picture and is stored as BGR - I may be wrong, been a while, but experiment with a hex editor you'll see how easy it is. Now to make your program do that, and you're set.

The closest thing to a library you'll need is fstream, but that's pretty standard and no need to add anything special to the system.
 

deveraux

Senior member
Mar 21, 2004
284
0
71
You can find the BMP header info easily on the internet. Even wikipedia has an article on it with all the header info and their offsets from file index 0. As RedSquirrel mentioned above, the image is stored as BGR instead of RGB.

Never done image compression so I can't help on that but echoing what others have already said, if you are trying to keep the execution fast / not linking large libs, just avoid compression (unless there are other limitations on the system).
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,631
4,558
75
Vector graphics, good idea degibson!

If you need something supported in a browser, you might prefer SVG. It even works in IE, with a plugin.
 

suszterpatt

Senior member
Jun 17, 2005
927
1
81
Wow, haven't thought of vector graphics...I love AT. :p

Still, I'm afraid PostScript won't work for me. Let me throw out an example of what I want to do:

Say the world is 10kx10k units (2D), and I'm creating a 1kx1k raster image. That makes one pixel correspond to a 10x10 square of the world. I'd like to make a heat map of sorts: that is, the more objects of interest there are in a 10x10 square, the brighter the pixel will be. This can be easily done by calculating the correct brightness value for each pixel, but the only way I can think of making this work with vector graphics is stacking translucent shapes on one another against a dark background, so that overlaps create a brighter area. And, as luck would have it, PostScript doesn't support translucency.

I think I'll stick with BMP for ease of... everything. :) Thanks.