Exporting images from ZGE

All topics about ZGameEditor goes here.

Moderator: Moderators

User avatar
Ats
Posts: 770
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Exporting images from ZGE

Post by Ats »

Hello. A lot of image programs such as nomacs can't open the TGA files produced by the snippet. After a little research, it appears that a "TRUE" TGA file needs some additional information:
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).
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.

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 );
}
User avatar
Kjell
Posts: 1915
Joined: Sat Feb 23, 2008 11:15 pm

Re: Exporting images from ZGE

Post by Kjell »

Hi Ats,
Ats wrote: Fri Mar 04, 2022 10:20 amMaybe 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.
You could do something like this:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <Source>
<![CDATA[//

void glReadPixels(int x, int y, int width, int height, int format, int type, xptr data){}]]>
      </Source>
    </ZExternalLibrary>
    <ZLibrary Comment="Screenshot" HasInitializer="1">
      <Source>
<![CDATA[//

private byte[18] header;
private byte[18] footer;

{
  header[2] = 2; // Uncompressed RGB
  header[16] = 32; // Bits per pixel

  string s = "TRUEVISION-XFILE.";

  for(int i=0; i<length(s); i++)
  {
    footer[i] = ord(subStr(s, i, 1));
  }

  footer[17] = 0;
}

//

void captureTGA(string fileName)
{
  int w, h;

  w = App.ViewportWidth;
  h = App.ViewportHeight;

  header[12] = w;
  header[13] = w >> 8;

  header[14] = h;
  header[15] = h >> 8;

  byte[] buffer;
  buffer.SizeDim1 = w * h * 4 + 36;

  int j = buffer.SizeDim1 - 18;

  for(int i=0; i<18; i++)
  {
    buffer[i] = header[i];
    buffer[j++] = footer[i];
  }

  glReadPixels(App.ViewportX, App.ViewportY, w, h, 0x80E1, 0x1401, buffer[18]);
  @FileAction(File: @File(FileName: fileName, Encoding: 1, TargetArray: buffer), Action: 1);
}]]>
      </Source>
    </ZLibrary>
  </OnLoaded>
  <OnUpdate>
    <KeyPress Comment="Press S to capture" CharCode="83" RepeatDelay="0.25">
      <OnPressed>
        <ZExpression>
          <Expression>
<![CDATA[//

captureTGA("capture.tga");]]>
          </Expression>
        </ZExpression>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <OnRender>
    <RenderNet VertexColors="255">
      <RenderVertexExpression>
<![CDATA[//

Color.R = Vertex.X + 0.5;
Color.G = Vertex.Y + 0.5;
Color.B = 1;]]>
      </RenderVertexExpression>
    </RenderNet>
  </OnRender>
</ZApplication>
K
User avatar
Ats
Posts: 770
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Exporting images from ZGE

Post by Ats »

Wow, thanks Kjell, I've learned so much from the changes you made in order to optimize the snippet :shock:
Post Reply