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>
#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)
char *basename(char *s) {
char *r;
if (s) {
for (r = s; *r; r++) {
if ((*r == ':') || (*r == '/') || (*r == '\\')) {
s = &r[1];
}
}
}
return(s);
}
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);
}
}
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);
}
}
}
void readdata(FILE *fl, void *data, uint32_t size) {
if (data && size) {
memset(data, 0, size);
if (fl) {
fread(data, size, 1, fl);
}
}
}
/*
.IFF/.BFF
uint32_t width;
uint32_t height;
uint32_t data[width * height];
uint32_t unknown; // this one may or maynot exists
.B16
uint32_t unknown;
uint32_t width;
uint32_t height;
uint16_t data[width * height];
*/
int main(int argc, char *argv[]) {
tga_head *th;
uint32_t sz, head[3], t;
char s[260+4];
uint8_t *p;
FILE *fl;
printf(
"Times of Conflict .B16/.BFF/.IFF images to .TGA converter v1.0\n"
"(c) CTPAX-X Team 2026\n"
"http://www.CTPAX-X.org/\n\n"
);
if (argc != 2) {
printf("Usage: toctotga <filename.ext>\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);
readdata(fl, &head, sizeof(head));
t = 0;
do {
if ((sizeof(head) + (head[1] * head[2] * 2)) == sz) {
head[0] = head[1];
head[1] = head[2];
t = 2;
break;
}
if ((sizeof(head) - sizeof(head[0]) + (head[0] * head[1] * 4)) == sz) {
fseek(fl, sizeof(head) - sizeof(head[0]), SEEK_SET);
t = 4;
break;
}
if ((sizeof(head) - sizeof(head[0]) + (head[0] * head[1] * 4) + sizeof(head[0])) == sz) {
fseek(fl, sizeof(head) - sizeof(head[0]), SEEK_SET);
t = 4;
break;
}
} while (0);
if (!t) {
fclose(fl);
printf("Error: invalid input file format.\n\n");
return(3);
}
sz = (sizeof(th[0])) + (head[0] * head[1] * t);
p = (uint8_t *) malloc(sz);
if (!p) {
fclose(fl);
printf("Error: not enough memory for input file.\n\n");
return(4);
}
th = (tga_head *) p;
memset(th, 0, sizeof(th[0]));
th->ImageDescriptor = 0x20;
th->ImageType = 2;
th->PixelDepth = t * 8;
th->Width = head[0];
th->Height = head[1];
readdata(fl, &p[sizeof(th[0])], sz - sizeof(th[0]));
fclose(fl);
strcpy(s, basename(argv[1]));
printf("%s -> ", s);
strcat(s, ".tga");
printf("%s\n", s);
dumpdata(s, p, sz);
free(p);
printf("\ndone\n\n");
return(0);
}