47 const unsigned long TGA_NO_IMAGE_DATA = 0;
48 const unsigned long TGA_8BIT_UNCOMPRESSED = 1;
49 const unsigned long TGA_RGB_UNCOMPRESSED = 2;
50 const unsigned long TGA_GRAYSCALE_UNCOMPRESSED = 3;
51 const unsigned long TGA_8BIT_COMPRESSED = 9;
52 const unsigned long TGA_RGB_COMPRESSED = 10;
53 const unsigned long TGA_GRAYSCALE_COMPRESSED = 11;
57 unsigned char IdFieldSize;
58 unsigned char HasColMap;
59 unsigned char ImageType;
60 unsigned char ColMapOrigin[2];
61 unsigned char ColMapCount_lo;
62 unsigned char ColMapCount_hi;
63 unsigned char ColMapEntrySize;
64 unsigned char ImageOrigins[4];
65 unsigned char Width_lo;
66 unsigned char Width_hi;
67 unsigned char Height_lo;
68 unsigned char Height_hi;
69 unsigned char BitsPerPixel;
70 unsigned char ImageDescriptor;
112 memset(&header, 0,
sizeof(header));
113 file->
read( &header,
sizeof(STGAHeader) );
115 unsigned int colmap_offset = 18 + header.IdFieldSize;
116 unsigned int pixels_offset = colmap_offset +
117 (header.ColMapCount_lo + header.ColMapCount_hi*256)*header.ColMapEntrySize/8;
118 unsigned int w = header.Width_lo + header.Width_hi*256;
119 unsigned int h = header.Height_lo + header.Height_hi*256;
122 unsigned int bpp = header.BitsPerPixel;
123 const char *type =
"";
124 switch(header.ImageType)
126 case TGA_NO_IMAGE_DATA: type =
"TGA_NO_IMAGE_DATA";
break;
127 case TGA_8BIT_UNCOMPRESSED: type =
"TGA_8BIT_UNCOMPRESSED";
break;
128 case TGA_RGB_UNCOMPRESSED: type =
"TGA_RGB_UNCOMPRESSED";
break;
129 case TGA_GRAYSCALE_UNCOMPRESSED: type =
"TGA_GRAYSCALE_UNCOMPRESSED";
break;
130 case TGA_8BIT_COMPRESSED: type =
"TGA_8BIT_COMPRESSED";
break;
131 case TGA_RGB_COMPRESSED: type =
"TGA_RGB_COMPRESSED";
break;
132 case TGA_GRAYSCALE_COMPRESSED: type =
"TGA_GRAYSCALE_COMPRESSED";
break;
134 Log::debug(
Say(
"TGA %s: w=%n, h=%n, bpp=%n/%n %s\n") << file->
path() << w << h << bpp << header.ColMapEntrySize << type);
137 if (header.ImageType == TGA_NO_IMAGE_DATA)
140 if (header.ImageType == TGA_RGB_COMPRESSED)
143 switch(header.BitsPerPixel)
145 case 32: pixsize = 4;
break;
146 case 24: pixsize = 3;
break;
147 case 16: pixsize = 2;
break;
157 while(pix < pixcount)
159 unsigned char header_ch = 0;
160 file->
read(&header_ch, 1);
161 if (header_ch >= 128)
163 int count = header_ch - 128 + 1;
164 unsigned char bgra[4];
165 file->
read(bgra, pixsize);
168 memcpy((
unsigned char*)img->
pixels() + pix*pixsize, bgra, pixsize);
174 int count = header_ch + 1;
175 file->
read((
unsigned char*)img->
pixels() + pix*pixsize, pixsize*count);
180 switch(header.BitsPerPixel)
193 Log::error(
Say(
"TGA ERROR: TGA_RGB_COMPRESSED %nbpp not supported.\n") << header.BitsPerPixel );
200 if (header.ImageType == TGA_RGB_UNCOMPRESSED)
204 switch(header.BitsPerPixel)
206 case 32: pixsize = 4;
break;
207 case 24: pixsize = 3;
break;
208 case 16: pixsize = 2;
break;
216 switch(header.BitsPerPixel)
228 Log::error(
Say(
"TGA ERROR: TGA_RGB_UNCOMPRESSED %nbpp not supported.\n") << header.BitsPerPixel );
235 if (header.ImageType == TGA_8BIT_UNCOMPRESSED || header.ImageType == TGA_8BIT_COMPRESSED)
237 if (header.BitsPerPixel == 8)
240 unsigned int colmap_count = header.ColMapCount_lo + header.ColMapCount_hi*256;
242 if (header.ColMapEntrySize == 24)
246 file->
read(palette, colmap_count*3);
251 if (header.ImageType == TGA_8BIT_UNCOMPRESSED)
260 while(pix < pixcount)
262 unsigned char header_ch = 0;
263 file->
read(&header_ch, 1);
264 if (header_ch >= 128)
266 int count = header_ch - 128 + 1;
267 unsigned char bgra[4];
268 file->
read(bgra, pixsize);
271 memcpy((
unsigned char*)img->
pixels() + pix*pixsize, bgra, pixsize);
277 int count = header_ch + 1;
278 file->
read((
unsigned char*)img->
pixels() + pix*pixsize, pixsize*count);
287 else if (header.ColMapEntrySize == 32)
291 file->
read(palette, colmap_count*4);
293 file->
seekSet( pixels_offset );
295 if (header.ImageType == TGA_8BIT_UNCOMPRESSED)
304 while(pix < pixcount)
306 unsigned char header_ch = 0;
307 file->
read(&header_ch, 1);
308 if (header_ch >= 128)
310 int count = header_ch - 128 + 1;
311 unsigned char bgra[4];
312 file->
read(bgra, pixsize);
315 memcpy((
unsigned char*)img->
pixels() + pix*pixsize, bgra, pixsize);
321 int count = header_ch + 1;
322 file->
read((
unsigned char*)img->
pixels() + pix*pixsize, pixsize*count);
333 Log::error(
Say(
"TGA ERROR: TGA_8BIT_UNCOMPRESSED entry size = %n not supported.\n") << header.ColMapEntrySize );
340 Log::error(
Say(
"TGA ERROR: TGA_8BIT_UNCOMPRESSED %nbpp bit not supported.\n") << header.BitsPerPixel );
346 if (header.ImageType == TGA_GRAYSCALE_UNCOMPRESSED || header.ImageType == TGA_GRAYSCALE_COMPRESSED)
348 if (header.BitsPerPixel == 8)
352 if (header.ImageType == TGA_GRAYSCALE_UNCOMPRESSED)
361 while(pix < pixcount)
363 unsigned char header_ch = 0;
364 file->
read(&header_ch, 1);
365 if (header_ch >= 128)
367 int count = header_ch - 128 + 1;
368 unsigned char bgra[4];
369 file->
read(bgra, pixsize);
372 memcpy((
unsigned char*)img->
pixels() + pix*pixsize, bgra, pixsize);
378 int count = header_ch + 1;
379 file->
read((
unsigned char*)img->
pixels() + pix*pixsize, pixsize*count);
387 Log::error(
Say(
"TGA ERROR: TGA_GRAYSCALE_UNCOMPRESSED %nbpp not supported.\n") << header.BitsPerPixel );
395 Log::error(
Say(
"TGA ERROR: this type %n not supported.\n") << header.ImageType);
400 if ((header.ImageDescriptor & (1<<5)))
421 int w = src->
width();
423 int d = src->
depth();
437 Log::error(
Say(
"saveTGA('%s'): could not convert image to IT_UNSIGNED_BYTE.\n") << fout->
path() );
447 Log::error(
Say(
"saveTGA('%s'): could not convert image to IF_BGRA.\n") << fout->
path() );
459 memset(&header, 0,
sizeof(STGAHeader));
462 header.ImageType = TGA_RGB_UNCOMPRESSED;
463 header.Width_lo = (
unsigned char)(w & 0x00FF);
464 header.Width_hi = (
unsigned char)(w >> 8);
465 header.Height_lo = (
unsigned char)(h & 0x00FF);
466 header.Height_hi = (
unsigned char)(h >> 8);
467 header.BitsPerPixel = 32;
468 fout->
write(&header,
sizeof(header));
479 fout->
write(
"TRUEVISION-XFILE.", 18);
492 memset(&header, 0,
sizeof(header));
493 file->
read(&header,
sizeof(STGAHeader) );
496 memset(signature, 0, 17);
498 file->
read(signature, 16);
503 if (strcmp(
"TRUEVISION-XFILE", signature) == 0)
508 switch( header.ImageType )
524 unsigned int width = header.Width_lo + header.Width_hi*256;
525 unsigned int height = header.Height_lo + header.Height_hi*256;
526 unsigned int bpp = header.BitsPerPixel;
528 if (width * height == 0)
548 Log::warning(
Say(
"isTGA: the file '%s' looks like a TGA but is missing the 'TRUEVISION-XFILE' signature.\n") << file->
path() );
static void debug(const String &message)
Use this function to provide extra information useful to investigate and solve problems.
String toLowerCase() const
Returns the lower-case version of a String.
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
VLCORE_EXPORT FileSystem * defFileSystem()
Returns the default FileSystem used by VisualizationLibrary.
VLCORE_EXPORT bool isTGA(VirtualFile *file)
A TGA file is accepted only if it has a 'TGA' extension.
An abstract class representing a file.
void convert8ToRGBA(const TPalette3x256 &palette, void *buf, int w, int h, unsigned char alpha, int bytealign=1)
A simple String formatting class.
const unsigned char * pixels() const
Raw pointer to pixels.
void setObjectName(const char *name)
The name of the object, by default set to the object's class name in debug builds.
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
void swapBytes32_BGRA_RGBA(void *buf, int bytecount)
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
ref< Image > convertType(EImageType new_type) const
Converts the type() of an image.
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
virtual ref< VirtualFile > locateFile(const String &full_path, const String &alternate_path=String()) const
Looks for a VirtualFile on the disk and in the currently active FileSystem.
VLCORE_EXPORT ref< Image > loadTGA(VirtualFile *file)
int requiredMemory() const
Returns the number of bytes requested to store the image.
virtual void close()=0
Closes the file.
long long writeUInt32(unsigned int data, bool little_endian_data=true)
Writes a single entry. Returns the number of bytes written.
const String & path() const
Returns the path of the file.
void convertA1R5G5B5ToRGBA(void *buf, int size, unsigned char alpha)
Visualization Library main namespace.
void convertRGBToRGBA(void *buf, int w, int h, unsigned char alpha, int bytealign=1)
long long write(const void *buffer, long long byte_count)
Writes byte_count bytes to a file. Returns the number of bytes actually written.
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
unsigned char TPalette3x256[256 *3]
bool seekSet(long long offset)
Changes the current read/write position of a file.
ref< Image > convertFormat(EImageFormat new_format) const
Converts the format() of an image.
A VirtualFile that operates on regular disk files.
Implements a generic 1d, 2d, 3d and cubemap image that can have mipmaps.
The ref<> class is used to reference-count an Object.
void allocate2D(int x, int y, int bytealign, EImageFormat format, EImageType type)
bool seekEnd(long long offset)
Changes the current read/write position of a file.
std::string toStdString() const
Returns a UTF8 encoded std::string.
bool endsWith(const String &str) const
Returns true if a String ends with the specified String str.
VLCORE_EXPORT bool saveTGA(const Image *src, const String &path)
unsigned char TPalette4x256[256 *4]
EImageFormat format() const