Tutorial5

Reading and Writing Targa Images

Adapted from Mark Pingel's Tutorial
Modified by Chi Man Liu in 2007

LibTarga is a library for reading and writing images in the TARGA image file format. It was written by Alex Mohr for use by students in CS559, and for other graphics projects. We also provide you with TargaImage, a class wrapper for LibTarga. TargaImage provides methods to read and write Targa images along with a few utility methods. This tutorial gives examples of reading and writing Targa images using TargaImage. For more detailed documentation, please see:

 

Getting the Files and Setting Up

  1. Download the following files to your Visual Studio project folder.
     
     
  2. Right click on project "Your Project Name" at solution explorer in Visual Studio and choose Add->Add Existing Item...
     
  3. Select the libtarga.c, libtarga.h, TargaImage.cpp and TargaImage.h from the Add Existing Item window's file explorer.
     
  4. Click Open.
     
  5. Check the solution explorer. libtarga.c and TargaImage.cpp should be added to the folder '''Source Files, while libtarga.h and TargaImage.h should be added to Header Files.
 

Reading a Targa Image

There are two ways to create a TargaImage instance. The first is to use TargaImage::blankImage which returns an empty image (RGBA = 0 for all pixels); the second is reading a Targa image from a file. In this tutorial, we use the second approach.

The following sample code reads a targa image from input.tga:

TargaImage *image = TargaImage::readImage("input.tga");

Note: Constructors of TargaImage are private. You won't be able to use the "new" operator to create images.

To get the width and height of the image, use

unsigned int wdt = image->width();
unsigned int hgt = image->height();
 

Writing a Targa Image

Writing a Targa file is also very simple:

 
image->write("output.tga");
 

Checking for Errors

Reads and writes may fail for various reasons, such as file not found. In that case, we can use the LibTargaerror handling routines to identify the error. The following expression returns a string description of the most recent error.

 
tga_error_string(tga_get_last_error())

You may print the error message and take appropriate actions.

 

Sample Program

Here is a simple example about how to use it. Since I have put comment on the code, I won't discompose it to explain each line's function.

1. Modify Main

 
#include <iostream>
#include "libtarga.h"
#include "TargaImage.h"

using namespace std;

int main(int argc, char** args)
{
	// Check whether the argument is four or not
	printf("The argc %d\n", argc);
	if (argc != 4) {
		cerr << "Did not have enough input arguments which should be \n";
		cerr << "1. Source 2. Destination filenames 3. Size of the square\n";
		return 0;
	}

	// Get the parameters
	TargaImage *image = NULL;
	char* input = args[1];
	char* output = args[2];
	int radius = atoi(args[3]);

	// Check the input name
	if (!input) {
		cerr << "No input is given.\n";
		return 0;
	}// if

	// Check the output name
	if (!output) {
		cerr << "No output is given.\n";
		return 0;
	}// if

	// Check the square size
	if(radius <= 0) {
		cerr << "The size of square is negative.\n";
		return 0;
	}

	// Load in the image
	image = TargaImage::readImage(input);
	// Chech whether it succeed or not and print out necessary error message
	if (image == NULL) {
		cerr << "TGA Error: " << tga_error_string(tga_get_last_error()) << "\n";
		return 0;
	}

	unsigned int width = image->width();
	unsigned int height = image->height();
	unsigned char* image_data = image->pixels();

	// Check the square size
	if (radius * 2 > width || radius * 2 > height) {
		cerr << "The size of square bigger than image.\n";
		return 0;
	}

	// Copy the image center
	int sourceCenterX = width / 2;
	int sourceCenterY = height / 2;

	// Go through the center of the square
	for (int yOffset = -radius; yOffset <= radius; ++yOffset)  {
		// Compute the index at Y coordinate
		int sourceY = sourceCenterY + yOffset;
		if (sourceY >=0 && sourceY < height) { // Check whether Y is over boundary
			for (int xOffset = -radius; xOffset <= radius; ++xOffset) {
				int sourceX = sourceCenterX + xOffset; // Compute the index at X coordinate
				if (sourceX >= 0 && sourceX < width) { // Check whether X is over boundary
					int index = (sourceY * width + sourceX) * 4; // Compute the index for this pixel

					image_data[index]   = 0;   // Change red to 0
					image_data[index+1] = 255; // Change green to 1
					image_data[index+2] = 0;   // Change blue to 0
					image_data[index+3] = 255; // Change alpha to 1
				} // end of if sourceX
			} // end of for
		} // end of if sourceY
	} // end of for

	// Save the image
	if (!image->write(output)) {
		cerr << "TGA Save Error: " << tga_error_string(tga_get_last_error()) << "\n";
		return 0;
	}

	cout << "Finish image loading and processing\n";
	return 0;

}

2. Change the command line argument

You need to input command line argument with "Flower.tga test.tga 100". How can you do it. Please refer to command line argument tutorial

3. Output

After building the entire project and running it. You will find a file name "test.tga" inside the project directory. When you use the irfan or other image viewers to open it, you should be abel to see this picture

Source Code