Skip to content

Saving An Image

Brett Anthony edited this page Jul 6, 2019 · 2 revisions

At this point we're going to assume you've read the opening an image guide. Therefore we will not be explaining what this code does as it speaks for itself in all honesty. The general workflow is to generate a Parameters object based on the desired output format of the image, and from there set the input format. If you have any questions try reading our documentation.

Table Of Contents

C++ Example

#include <iostream>
#include "xTGA/xTGA.h"

int main()
{
	// Create an image of 3 horizontal bars, one red, one green, and one blue, each starting at zero luminance and reaching peak luminance at 500 pixels.
	uint16 width = 500;
	uint16 height = 300;
	xtga::pixelformats::RGB888* ImageBuffer = new xtga::pixelformats::RGB888[width * height];

	// Write Red Riser
	for (int i = 0; i < 500 * 100; ++i)
	{
		ImageBuffer[i].R = (float)(i % 500 + 1) / 500 * 255;
		ImageBuffer[i].G = 0;
		ImageBuffer[i].B = 0;
	}

	// Write Green Riser
	for (int i = 0, j = 100 * 500; j < 500 * 200; ++j, ++i)
	{
		ImageBuffer[j].R = 0;
		ImageBuffer[j].G = (float)(i % 500 + 1) / 500 * 255;
		ImageBuffer[j].B = 0;
	}

	// Write Blue Riser
	for (int i = 0, j = 200 * 500; j < 500 * 300; ++j, ++i)
	{
		ImageBuffer[j].R = 0;
		ImageBuffer[j].G = 0;
		ImageBuffer[j].B = (float)(i % 500 + 1) / 500 * 255;
	}

	auto config = xtga::Parameters::BGR24();
	config.InputFormat = xtga::pixelformats::PIXELFORMATS::RGB888;

	xtga::ERRORCODE err;

	auto File = xtga::TGAFile::Alloc(ImageBuffer, width, height, config, &err);
	delete[] ImageBuffer;

	if (err != xtga::ERRORCODE::NONE)
	{
		std::cout << "Unexpected Error: " << (uint16)err << '\n';
		return -1;
	}

	File->SaveFile("Output.tga", &err);
	xtga::TGAFile::Free(File);

	if (err != xtga::ERRORCODE::NONE)
	{
		std::cout << "Unexpected Error: " << (uint16)err << '\n';
		return -1;
	}

	return 0;
}

C Example

The only notable difference here is the xtga_FreeMem() function which is a general function for freeing memory from the library. It should only ever be used if the object was created by the library, but lacks a Free() function designated for it.

#include <stdio.h>
#include <stdlib.h>
#include "xTGA/xTGA_C.h"

typedef struct
{
	uchar R;
	uchar G;
	uchar B;
} RGB888_t;

int main()
{
	// Create an image of 3 horizontal bars, one red, one green, and one blue, each starting at zero luminance and reaching peak luminance at 500 pixels.
	uint16 width = 500;
	uint16 height = 300;
	RGB888_t* ImageBuffer = malloc(sizeof(RGB888_t) * width * height);

	// Write Red Riser
	for (int i = 0; i < 500 * 100; ++i)
	{
		ImageBuffer[i].R = (float)(i % 500 + 1) / 500 * 255;
		ImageBuffer[i].G = 0;
		ImageBuffer[i].B = 0;
	}

	// Write Green Riser
	for (int i = 0, j = 100 * 500; j < 500 * 200; ++j, ++i)
	{
		ImageBuffer[j].R = 0;
		ImageBuffer[j].G = (float)(i % 500 + 1) / 500 * 255;
		ImageBuffer[j].B = 0;
	}

	// Write Blue Riser
	for (int i = 0, j = 200 * 500; j < 500 * 300; ++j, ++i)
	{
		ImageBuffer[j].R = 0;
		ImageBuffer[j].G = 0;
		ImageBuffer[j].B = (float)(i % 500 + 1) / 500 * 255;
	}

	xtga_Parameters* config = xtga_Parameters_BGR24();
	xtga_Parameters_set_input_format(config, xtga_PIXELFORMATS_RGB888);

	xtga_ERRORCODE_e err;

	xtga_TGAFile* File = xtga_TGAFile_Alloc_FromBuffer(ImageBuffer, width, height, config, &err);
	free(ImageBuffer);
	xtga_FreeMem(&config);

	if (err != xtga_ERRORCODE_NONE)
	{
		printf("Unexpected Error: %u\n", (uint16)err);
		return -1;
	}

	xtga_TGAFile_SaveFile(File, "Output.tga", &err);
	xtga_TGAFile_Free(&File);

	if (err != xtga_ERRORCODE_NONE)
	{
		printf("Unexpected Error: %u\n", (uint16)err);
		return -1;
	}

	return 0;
}
Clone this wiki locally