Loading TGA files

Started by
5 comments, last by dinotrack 23 years, 9 months ago
Does anyone have any links or examples of simple C code for loading TGA files? I would like to be able to read 8-bit greyscale, as well as 24 or 32bit RGBA TGA files. By the way: I have downloaded Nate Miller''s TGA loading code, but it seems awfully complex, and I don''t quite understand it. So please, don''t just point me to that. Thanks! -Jonathan
Advertisement
TGA files are really easy to read/write..
find the TGA format specs on the net, and use fopen() or something, to read all the color bytes out.


What a coincedence...here''s a tutorial for you:
Custom Resources explains how to include TGA resources in your exe for OpenGL texture mapping. You probably don''t need texture mapping - just ignore the last few lines of code...

Hope this helps,
- Bas

You can find a 8-bit TGA loader at my page:
www.netpar.com.br/blazter
Get the Xfrog source code.
If you have any problems, e-mail me.

blazter

Edited by - blazter on July 5, 2000 11:41:32 PM
Hi,

if you still need help:

Here''s my quick-hack of Nate Millers great TGA loader code... Support for some formats is ripped, so everything is short and simple. This code generates a windows bitmap, if you want to use it in OpenGL, you simply can pass the loaded array to GL and rip the (very messy !) win32 bmp code...

Here it goes:

// TGAImage.cpp: Implementierung der Klasse CTGAImage.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TGAImage.h"

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

CTGAImage::CTGAImage()
{

}

CTGAImage::~CTGAImage()
{

}

bool CTGAImage::LoadImage(char szFileName[])
{
////////////////////////////////////////////////////////////////////////
// Loads a TGA image
////////////////////////////////////////////////////////////////////////

unsigned char type[4];
unsigned char info[7];
unsigned int i, j, iPos;
FILE *iFile;

if (!(iFile = fopen(szFileName, "r+bt")))
{
COutputWindow:rintLine("Fehler beim ˆffnen der TGA Datei !");
return FALSE;
}

// Read in colormap info and image type, byte 0 ignored
fread(&type, sizeof(unsigned char), 3, iFile);

// Seek past the header and useless info
fseek(iFile, 12, SEEK_SET);
fread(&info, sizeof(unsigned char), 6, iFile);

// Skip past image identification
fseek(iFile, type[0], SEEK_CUR);

// Wrong file type ?
if (type[1] != 0 // (type[2] != 2 && type[2] != 3))
{
COutputWindow:rintLine("Datei ist von unbekanntem Typ oder keine TGA !");
return FALSE;
}

SetImageWidth(info[0] + info[1] * 256);
SetImageHeight(info[2] + info[3] * 256);
SetImageBitDepth(info[4]);

// Make sure we are loading a supported type
if (GetImageBitDepth() != 24)
{
COutputWindow:rintLine("Keine 24-Bit TGA Datei !");
return FALSE;
}

// Kill the old stuff
CImage::CleanUp();

// Get RGB data out of the stream
m_pData = GetRGB(iFile, GetImageWidth() * GetImageHeight());

fclose(iFile);

// No image data
if (m_pData == 0)
{
COutputWindow:rintLine("Fehler beim Lesen der TGA !");
return FALSE;
}

// Create and setup the new bitmap
m_hDC = CreateCompatibleDC(NULL);
m_hBitmap = CreateCompatibleBitmap(GetDC(NULL), GetImageWidth(), GetImageHeight());
SelectObject(m_hDC, m_hBitmap);

// Draw image into the bitmap
for (i=0; i for (j=0; j {
iPos = (j * GetImageWidth() * 3) + (i * 3);

// Switch R with B to convert from BGR to RGB
SetPixelV(m_hDC, i, j,
RGB(m_pData[iPos+2], m_pData[iPos+1], m_pData[iPos]));
}

return TRUE;
}

unsigned char* CTGAImage::GetRGB(FILE *strm, int size)
{
unsigned char *rgb;
int bread;

// Alocate space for image bytes
rgb = new unsigned char[size * 3];

if (rgb == 0)
return 0;

// Read image bytes
bread = fread (rgb, sizeof(unsigned char), size * 3, strm);

// All bytes properly read ?
if (bread != size * 3)
{
delete [] rgb;
return 0;
}

// Flip image

unsigned int iLineSize = sizeof(unsigned char) * GetImageWidth() * 3;
unsigned char* pLine1 = 0;
unsigned char* pLine2 = 0;
unsigned char* pTemp = new unsigned char[GetImageWidth() * 3];

for (unsigned int i=0; i {
// Set pointers to the lines that should be flipped
pLine1 = rgb + GetImageWidth() * 3 * i;
pLine2 = rgb + GetImageWidth() * 3 * (GetImageHeight() - i - 1);

// Copy Line1 into Temp
memcpy(pTemp, pLine1, iLineSize);
// Copy Line2 into Line1
memcpy(pLine1, pLine2, iLineSize);
// Copy Temp into into Line2
memcpy(pLine2, pTemp, iLineSize);
}

// Kill temp line
delete [] pTemp;
pTemp;

return rgb;
}

--------------------------
www.gamedev.net/hosted/glvelocity
glvelocity.gamedev.net
www.glvelocity.com
Tim--------------------------glvelocity.gamedev.netwww.gamedev.net/hosted/glvelocity
Ooops ;-)) Thoose "COutputWindow" are calls to a static member function of my debug console class, just rip them out...

Tim


--------------------------
www.gamedev.net/hosted/glvelocity
glvelocity.gamedev.net
www.glvelocity.com
Tim--------------------------glvelocity.gamedev.netwww.gamedev.net/hosted/glvelocity
There's some really easy to understand code in "d3dtextr.cpp" in in the dx sdk directory. Look in "...\samples\multimedia\d3dim\src\d3dframe" The code is to load the file as a texture but it's easy to change.

Opps, sorry the code only reads 24 and 32 bit files, i didn't read your post thoroughly.

+AA_970+

Edited by - +AA_970+ on July 7, 2000 4:12:13 PM

This topic is closed to new replies.

Advertisement