This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
/*
2014-10-31 - v1.0 - ANSI C, Windows
2026-10-13 - v1.1 - ANSI C, multi-platform
- clean code rewrite and reduce
*/
#pragma pack(push, 1)
typedef struct {
uint8_t IDLength;
uint8_t ColorMapType;
uint8_t ImageType;
uint16_t CMapStart;
uint16_t CMapLength;
uint8_t CMapDepth;
uint16_t XOffset;
uint16_t YOffset;
uint16_t Width;
uint16_t Height;
uint8_t PixelDepth;
uint8_t ImageDescriptor;
} tga_head;
#pragma pack(pop)
static char *basename(char *s) {
char *r;
if (s) {
for (r = s; *r; r++) {
if ((*r == ':') || (*r == '/') || (*r == '\\')) {
s = &r[1];
}
}
}
return(s);
}
static void newext(char *name, char *ext) {
char *p;
if (name && ext) {
for (; (*name == '.'); name++);
for (p = NULL; *name; name++) {
if (*name == '.') {
p = name;
}
}
strcpy(p ? p : name, ext);
}
}
static void dumpdata(char *name, void *data, uint32_t size) {
FILE *f;
if (name) {
f = fopen(name, "wb");
if (f) {
if (data && size) {
fwrite(data, size, 1, f);
}
fclose(f);
}
}
}
static void readdata(FILE *fl, void *data, uint32_t size) {
if (data && size) {
memset(data, 0, size);
if (fl) {
fread(data, size, 1, fl);
}
}
}
static void csx_decode(uint8_t *ou, uint8_t *op, uint32_t *offs, uint32_t w, uint32_t h) {
uint8_t *p, *u, b;
uint32_t ps, us;
if (ou && op && offs && w && h) {
while (h--) {
ps = offs[1] - offs[0];
us = w;
p = &op[*offs]; offs++;
u = ou;
while (ps && us) {
b = *p;
p++; ps--;
/* skip one */
if (b == 0x69) {
u++; us--;
continue;
}
/* fill many */
if (b == 0x6A) {
if (ps < 2) { break; }
b = (p[1] < us) ? p[1] : us;
memset(u, p[0], b);
p += 2; ps -= 2;
u += b; us -= b;
continue;
}
/* fill one */
if (b == 0x6B) {
if (ps < 1) { break; }
*u = *p;
p++; ps--;
u++; us--;
continue;
}
/* skip many */
if (b == 0x6C) {
if (ps < 1) { break; }
b = (p[0] < us) ? p[0] : us;
p++; ps--;
u += b; us -= b;
continue;
}
/* copy as is */
*u = b;
u++; us--;
}
ou += w;
}
}
}
/*
uint32_t npal; // must be not zero
uint32_t tpal; // transparent palette color
uint32_t cmap[npal]; // BGR0
uint32_t width; // must be non-zero
uint32_t height; // must be non-zero
uint32_t offs[height]; // rows offsets
uint32_t size; // must be non-zero (game allocated "size+2" for some reason)
uint8_t data[size]; // packed image data
*/
int main(int argc, char *argv[]) {
uint32_t npal, tpal, cmap[256], width, height, *offs, sz, k, i;
tga_head *th;
uint8_t *p;
FILE *fl;
char s[260 + 4];
printf(
"GoldenLand .CSX to .TGA image converter v1.1\n"
"(c) CTPAX-X Team 2014,2026\n"
"http://www.CTPAX-X.org/\n\n"
);
if (argc != 2) {
printf("Usage: csxtotga <filename.csx>\n\n");
return(1);
}
fl = fopen(argv[1], "rb");
if (!fl) {
printf("Error: can't open input file.\n\n");
return(2);
}
fseek(fl, 0, SEEK_END);
sz = ftell(fl);
fseek(fl, 0, SEEK_SET);
/* read palette colors */
readdata(fl, &npal, 4);
/* old code: read again if zero (some strange files) */
if (!npal) { readdata(fl, &npal, 4); }
if ((sz <= 32) || (!npal)) {
fclose(fl);
printf("Error: invalid input file format [1].\n\n");
return(3);
}
/* transparent color */
readdata(fl, &tpal, 4);
/* doesn't make sense more than 256 colors */
i = (npal <= 256) ? npal : 256;
/* read color map */
memset(cmap, 0, sizeof(cmap));
readdata(fl, cmap, i * 4);
/* if there are more - skip it */
fseek(fl, ftell(fl) + ((npal - i) * 4), SEEK_SET);
readdata(fl, &width, 4);
readdata(fl, &height, 4);
if ((!width) || (!height)) {
fclose(fl);
printf("Error: invalid input file format [2].\n\n");
return(4);
}
/* offset and data size */
i = (height + 1) * 4;
offs = (uint32_t *) malloc(i);
if (!offs) {
fclose(fl);
printf("Error: not enough memory for offs table.\n\n");
return(5);
}
readdata(fl, offs, i);
if (!offs[height]) {
free(offs);
fclose(fl);
printf("Error: invalid input file format [3].\n\n");
return(6);
}
sz = sizeof(th[0]) + (256 * 3) + (width * height);
/* allocate single buffer for packed and decoded image */
p = (uint8_t *) malloc(offs[height] + sz);
if (!p) {
free(offs);
fclose(fl);
printf("Error: not enough memory for image data.\n\n");
return(7);
}
readdata(fl, p, offs[height]);
fclose(fl);
strcpy(s, basename(argv[1]));
printf("%s -> ", s);
newext(s, ".tga");
printf("%s\n", s);
/* init TGA header */
th = (tga_head *) &p[offs[height]];
memset(th, 0, sizeof(th[0]));
th->ImageDescriptor = 0x20;
th->ColorMapType = 1;
th->ImageType = 1;
th->CMapLength = 256;
th->CMapDepth = 24;
th->PixelDepth = 8;
th->Width = width;
th->Height = height;
/* copy palette */
k = 256;
for (i = 0; i < 256; i++) {
memcpy(&p[offs[height] + sizeof(th[0]) + (i * 3)], &cmap[i], 3);
/* find transparent color */
if ((k == 256) && (cmap[i] == tpal)) {
k = i;
}
}
/* fill image with transparent color index */
i = width * height;
memset(&p[offs[height] + sz - i], (k & 0xFF), i);
/* decode image */
csx_decode(&p[offs[height] + sz - i], p, offs, width, height);
/* dump to disk */
dumpdata(s, th, sz);
free(p);
free(offs);
printf("\ndone\n\n");
return(0);
}