Simple Image Saving code c++

A major annoyance for me is saving images from c++. I can’t really find any libraries that I like, they are either too complex for the task or they don’t do what I want them to do. So I have created my own, extremely lightweight image library.

All you need to run this is two files, the cpp file and the header. I chose .tga because it is one of the easiest, straightforward formats to save to.

Here is some example code of saving an image with this library:


#include "Image.h"

int main(int argc, char **argv) {
   //declare image
   short width = 256;
   short height = 256;

   TGAImage *img = new TGAImage(width,height);

   //declare a temporary color variable
   Colour c;

   //Loop through image and set all pixels to red
   for(int x=0; x<width; x++)
      for(int y=0; ysetPixel(c,x,y);
      }

   //write the image to disk
   string filename = "/Users/daniel/test.tga";
   img->WriteImage(filename);

   return 0;
}

This outputs a red image like so:
Anyway, have fun using this in your projects,
Here is the download link for the source: Download
For more information on the TGA file format visit the following links:
http://paulbourke.net/dataformats/tga/ 
http://en.wikipedia.org/wiki/Truevision_TGA

Advertisements

3 thoughts on “Simple Image Saving code c++

  1. Thanks, one bug though:
    int TGAImage::convert2dto1d(int x, int y) {
    return m_width * x + y;
    }
    should be:
    int TGAImage::convert2dto1d(int x, int y) {
    return m_width * y + x;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s