So here's my modified screenDump function that produce compatible TGA files. Maybe it can be optimized, as I didn't know any another way to store the magical sentence as bytes at the end of the file.http://fileformats.archiveteam.org/wiki/TGA wrote:TGA files have no signature at the beginning of the file. They can be identified fairly reliably by testing whether the first 18 bytes have sensible values for TGA format, but that is nontrivial.
Some, but not all, TGA files have a signature at the end of the file. In that case, the last 18 bytes of the file are the ASCII characters "TRUEVISION-XFILE.", followed by a NUL byte (0x00).
Code: Select all
void saveScreenDump(string filename)
{
//Based on code by Kjell http://www.emix8.org/forum/viewtopic.php?f=1&t=1156&p=7242&hilit=save+image#p7242
int w = App.ViewportWidth;
int h = App.ViewportHeight;
byte[18] header;
header[2] = 2;
header[16] = 32;
header[17] = 8;
header[12] = w;
header[13] = w >> 8;
header[14] = h;
header[15] = h >> 8;
byte[18] footer;
footer[0] = 0x54; // T
footer[1] = 0x52; // R
footer[2] = 0x55; // U
footer[3] = 0x45; // E
footer[4] = 0x56; // V
footer[5] = 0x49; // I
footer[6] = 0x53; // S
footer[7] = 0x49; // I
footer[8] = 0x4F; // O
footer[9] = 0x4E; // N
footer[10] = 0x2D; // -
footer[11] = 0x58; // X
footer[12] = 0x46; // F
footer[13] = 0x49; // I
footer[14] = 0x4C; // L
footer[15] = 0x45; // E
footer[16] = 0x2E; // .
footer[17] = 0x00; // followed by a NUL byte
byte[] buffer;
buffer.SizeDim1 = header.SizeDim1 + w*h*4 + footer.SizeDim1;
int dst=0;
for(int i=0; i<header.SizeDim1; i++) buffer[dst++] = header[i];
glReadPixels(App.ViewportX, App.ViewportY, w, h, 0x80E1, 0x1401, buffer[dst]);
dst = buffer.SizeDim1 - footer.SizeDim1;
for(int i=0; i<footer.SizeDim1; i++) buffer[dst++] = footer[i];
@FileAction( File : @File(FileName : filename, Encoding : 1, TargetArray : buffer), Action : 1 );
}