52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
#include "stdafx.h"
|
|
|
|
#include <stdarg.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
#include "KTGA_IO.h"
|
|
#include <kfile/KStream.h>
|
|
#include <dump/XException.h>
|
|
|
|
|
|
//=============================================================================
|
|
bool TGAReadImage (KStream* stream, int* width, int* height, int* bitDepth, BYTE** pixels)
|
|
{
|
|
//===================//
|
|
// Read TARGA header //
|
|
//===================//
|
|
int nHeaderSize = sizeof(TGAHeaderInfo);
|
|
TGAHeaderInfo TGAHeader;
|
|
if (stream->Read((&TGAHeader), nHeaderSize) != nHeaderSize)
|
|
{
|
|
_oprint( "ERROR! Bad Targa header!\n" );
|
|
return false;
|
|
}
|
|
|
|
(*width) = TGAHeader.imwidth;
|
|
(*height) = TGAHeader.imheight;
|
|
(*bitDepth) = TGAHeader.imdepth;
|
|
int numTexels = (*width) * (*height) * ((*bitDepth)/8);
|
|
|
|
//============================//
|
|
// Allocate memory for texels //
|
|
//============================//
|
|
(*pixels) = new BYTE [numTexels];
|
|
if ((*pixels) == NULL)
|
|
{
|
|
_oprint( "ERROR allocating memory for pixels\n");
|
|
return false;
|
|
}
|
|
|
|
//=========================//
|
|
// Read texels into memory //
|
|
//=========================//
|
|
if (stream->Read( (*pixels), sizeof(BYTE)*numTexels) != (unsigned)numTexels)
|
|
{
|
|
_oprint( "ERROR! Couldn't read texels!\n");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} |