Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Texture Atlas Maker

, 1 Apr 2012
A utility to create texture atlases for 2D OpenGL games
AtlasExample_iphone_project.zip
AtlasExample
.DS_Store
__MACOSX
AtlasExample
._.DS_Store
AtlasExample
.DS_Store
AtlasExample
._.DS_Store
AtlasAppDelegate.mm
AtlasExample-Info.plist
AtlasExample-Prefix.pch
._AtlasExample-Prefix.pch
EAGLView.mm
en.lproj
InfoPlist.strings
en.lproj
._InfoPlist.strings
MainWindow.xib
ImagePNG.mm
._ImagePNG.mm
images0.tga
main.m
._main.m
Shaders
Shader.fsh
Shaders
._Shader.fsh
Shader.vsh
._Shader.vsh
AtlasExample.xcodeproj
project.pbxproj
project.xcworkspace
contents.xcworkspacedata
xcuserdata
lefty.xcuserdatad
UserInterfaceState.xcuserstate
xcuserdata
lefty.xcuserdatad
xcdebugger
Breakpoints.xcbkptlist
xcschemes
AtlasExample.xcscheme
xcschememanagement.plist
AtlasExampleTests
AtlasExampleTests-Info.plist
AtlasExampleTests-Prefix.pch
AtlasExampleTests
._AtlasExampleTests-Prefix.pch
AtlasExampleTests.m
._AtlasExampleTests.m
en.lproj
InfoPlist.strings
en.lproj
._InfoPlist.strings
AtlasExample_iPhone_project_2.1.zip
.DS_Store
._.DS_Store
.DS_Store
._.DS_Store
AtlasAppDelegate.mm
AtlasExample-Info.plist
AtlasExample-Prefix.pch
._AtlasExample-Prefix.pch
EAGLView.mm
InfoPlist.strings
._InfoPlist.strings
MainWindow.xib
ImagePNG.mm
._ImagePNG.mm
imagesNelly0.tga
main.m
._main.m
Shader.fsh
._Shader.fsh
Shader.vsh
._Shader.vsh
project.pbxproj
contents.xcworkspacedata
UserInterfaceState.xcuserstate
Breakpoints.xcbkptlist
AtlasExample.xcscheme
xcschememanagement.plist
AtlasExampleTests-Info.plist
AtlasExampleTests-Prefix.pch
._AtlasExampleTests-Prefix.pch
AtlasExampleTests.m
._AtlasExampleTests.m
InfoPlist.strings
._InfoPlist.strings
AtlasMaker.zip
AtlasMaker
.classpath
.project
bin
icon.png
images
icon.png
output.png
settings.png
output.png
settings.png
src
AtlasMaker_jar.zip
AtlasMaker_jar_2.0.zip
AtlasMaker_jar_2.1.zip
AtlasMaker_jar_2.2.1.zip
AtlasMaker_jar_2.2.zip
AtlasMaker_source.2.2.1.zip
.classpath
.project
.settings
org.eclipse.jdt.core.prefs
atlasMaker.properties
icon.png
icon.png
settings.png
settings.png
AtlasMaker_source.zip
.classpath
.project
icon.png
icon.png
output.png
settings.png
output.png
settings.png
AtlasMaker_source_2.0.zip
.classpath
.project
atlasMaker.properties
icon.png
icon.png
settings.png
settings.png
AtlasMaker_source_2.1.zip
.classpath
.project
org.eclipse.jdt.core.prefs
atlasMaker.properties
icon.png
icon.png
settings.png
settings.png
AtlasMaker_source_2.2.zip
.classpath
.project
org.eclipse.jdt.core.prefs
icon.png
icon.png
settings.png
settings.png
#include "PreCompile.h"
#include "TargaReader.h"
#include "CommonBuffer.h"

TargaReader::TargaReader()
{
}

TargaReader::~TargaReader()
{
}

const int TYPE_NO_IMAGE = 0; 
const int TYPE_UNCOMPRESSED_COLORMAP = 1; 
const int TYPE_UNCOMPRESSED_BGR = 2; 
const int TYPE_UNCOMPRESSED_MONO = 3; 
const int TYPE_RLE_COLORMAP = 9; 
const int TYPE_RLE_BGR = 10; 
const int TYPE_RLE_MONO = 11; 
//const int TGA_T_TO_B_BIT = 32;

void TargaReader::Load(const string& fileName)
{
	FILE *pFile = NULL;
	try
	{
		// Open for binary reading
		pFile = fopen(fileName.c_str(), "rb");
		if (!pFile)
			throw RacException("Could not load targa from " , fileName);
		// first read in the header
		int bytesRead = (int)fread((void*)&m_targaHeader, sizeof(m_targaHeader), 1, pFile);
		if (bytesRead != 1)
			throw RacException("Cannot read targa file header " , fileName);
		m_dataSize = m_targaHeader.m_width*m_targaHeader.m_height*m_targaHeader.m_bitsPerPixel/8;
		Allocator<char> &bufferOut = g_commonBuffer.GetBufferOut();
		bufferOut.Init(m_dataSize);
		m_pData = bufferOut.GetCache();
		switch (m_targaHeader.m_imageType)
		{
			case TYPE_UNCOMPRESSED_BGR:
			case TYPE_UNCOMPRESSED_MONO:
				// load directly into output buffer
				bytesRead = fread(m_pData, m_dataSize, 1, pFile);
				if (bytesRead != 1) 
					throw RacException("Error reading file " , fileName);
				break;
			case TYPE_RLE_BGR:
			case TYPE_RLE_MONO:
				ReadRLE(pFile);
				break;
			default:
				throw RacException("Unsupported targa type " , fileName);
		}
		fclose(pFile);
	}
	catch (RacException e)
	{
		if (pFile) fclose(pFile);
		throw(e);
	}
}


void TargaReader::ReadRLE(FILE *pFile)
{
	Allocator<char> &bufferIn = g_commonBuffer.GetBufferIn();
	fseek(pFile, 0, 2 /* SEEK_END */);
	int bufferInSize = (int)ftell(pFile) - sizeof(m_targaHeader);
	bufferIn.Init(bufferInSize);
	fseek(pFile, sizeof(m_targaHeader), 0 /* SEEK_SET */);
	int bytesRead = fread(bufferIn.GetCache(), bufferInSize, 1, pFile);
	if (bytesRead != 1) 
		throw RacException("Error reading targa file");

	if (m_targaHeader.m_bitsPerPixel == 16)
		ReadRLE16(bufferIn.GetCache(), bufferInSize, m_targaHeader.m_width*m_targaHeader.m_height);
	else if (m_targaHeader.m_bitsPerPixel == 32)
		ReadRLE32(bufferIn.GetCache(), bufferInSize, m_targaHeader.m_width*m_targaHeader.m_height);
	else
		throw RacException("Unsupported targa bits per pixel ", m_targaHeader.m_bitsPerPixel);
}


void TargaReader::ReadRLE16(const char* pDataIn, int bufferInSize, int totalPixels)
{
	char byte;
	short* pDataOutShort = (short*)g_commonBuffer.GetBufferOut().GetCache();
	short* pDataOutShortLast = pDataOutShort + totalPixels;
	const char* pDataInLast = pDataIn + bufferInSize;
	short pixel;
	short* pBlockEnd;
	int count;
	int bytesCopied;
    while (pDataOutShort < pDataOutShortLast)
    {
		byte = *pDataIn;
		pDataIn++;
		count = (byte & 0x7f)+1;
        if (byte & 0x80)
        {
			pixel = *((short*)pDataIn);
			pDataIn += 2;	
			pBlockEnd = pDataOutShort + count;
//				memset16(pDataIn, pixel, count);
			do
			{
				*pDataOutShort = pixel;
				pDataOutShort++;
			} while (pDataOutShort < pBlockEnd);
        }
        else /* RAW packet */
        {
			bytesCopied = count<<1;		// bytes = count*2 (2 bytes per pixel)
			memcpy(pDataOutShort, pDataIn, bytesCopied);
			pDataOutShort += count;
			pDataIn += bytesCopied;
        }
    }
	if (pDataIn > pDataInLast)
		throw RacException("error reading Targa RLE block");
}



void TargaReader::ReadRLE32(const char* pDataIn, int bufferInSize, int totalPixels)
{
	char byte;
	int* pDataOutInt = (int*)g_commonBuffer.GetBufferOut().GetCache();
	int* pDataOutIntLast = pDataOutInt + totalPixels;
	const char* pDataInLast = pDataIn + bufferInSize;
	int pixel;
	int* pBlockEnd;
	int count;
	int bytesCopied;
    while (pDataOutInt < pDataOutIntLast)
    {
		byte = *pDataIn;
		pDataIn++;
		count = (byte & 0x7f)+1;
        if (byte & 0x80)
        {
			pixel = *((int*)pDataIn);
			pDataIn += 4;	
			pBlockEnd = pDataOutInt + count;
//				memset16(pDataIn, pixel, count);
			do
			{
				*pDataOutInt = pixel;
				pDataOutInt++;
			} while (pDataOutInt < pBlockEnd);
        }
        else /* RAW packet */
        {
			bytesCopied = count<<2;		// bytes = count*4 (4 bytes per pixel)
			memcpy(pDataOutInt, pDataIn, bytesCopied);
			pDataOutInt += count;
			pDataIn += bytesCopied;
        }
    }
	if (pDataIn > pDataInLast)
		throw RacException("error reading Targa RLE block");
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

ed welch
Software Developer Astronautz
Spain Spain
After working in the software industry for many years, I've started my own games company that specialises in strategy games for mobile platforms.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 1 Apr 2012
Article Copyright 2012 by ed welch
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid