mugi wrote:i'll test out the tile encoder and see if it works, but i'd assume it does.
dont really see any reason why he'd write 2 un-tiling apps

The original files having the palette near the end
the decoded (tgas) have it near the beginning, right?
So I'm confused why both original codes place the palette near the end.
(edit: this is a bug with the first code maybe you should edit it to not confuse other users.)
it'slikely that the tile encoder has the same bug than the decoder though, where it changes 0A to 0D0A :/
need to check that out.
It doesn't write in binary mode so it surely will. (It's not a bug it's the wrong OS I guess.)
So here the slightly modified
encoder from paulguy:
(compile it to paulguy_enc.exe for example)
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define TILE_WIDTH (16)
#define TILE_HEIGHT (8)
int main(int argc, char **argv) {
int offset, width, height, paloffset;
long delta, filesize;
FILE *infile, *outfile;
char buffer[TILE_WIDTH+1]; // you can delete the +1 I guess, just debugging remains
char outfileName[256], palbuffer[1024+1];
int i, j, k;
bool bErr= false ;
if(argc < 6) {
fprintf(stderr, "USAGE: %s <infileName> <offset> <width> <height> <paloffset>\n"\
"\tOutput goes to infileName+'.tga'.\n", argv[0]);
exit(EXIT_FAILURE);
}
offset = atoi(argv[2]);
width = atoi(argv[3]);
height = atoi(argv[4]);
paloffset = atoi(argv[5]);
if (offset < 0) {
fprintf(stderr, "Offset must be positive.\n");
bErr= true;
}
if (width < TILE_WIDTH) {
fprintf(stderr, "Width must be greater than TILE_WIDTH (%d).", TILE_WIDTH);
bErr= true;
}
if (height < TILE_HEIGHT ) {
fprintf(stderr, "Height must be greater than TILE_HEIGHT (%d).", TILE_HEIGHT);
bErr= true;
}
if (bErr) exit(EXIT_FAILURE) ;
if (width % TILE_WIDTH != 0)
fprintf(stderr, "WARNING: width should be a multiple of %d.\n", TILE_WIDTH);
if (height % TILE_HEIGHT != 0)
fprintf(stderr, "WARNING: height should be a multiple of %d.\n", TILE_HEIGHT);
infile = fopen(argv[1], "rb");
if(infile == NULL) {
fprintf(stderr, "Failed to open %s.", argv[1]);
exit(EXIT_FAILURE);
}
strcpy(outfileName, argv[1]) ; //strcat(outfileName, ".tga") ;
strcat(outfileName, ".tpl") ;
outfile = fopen(outfileName, "wb");
if(outfile == NULL) {
fprintf(stderr, "Failed to open %s", outfileName);
fclose(infile) ;
exit(EXIT_FAILURE);
}
fseek(infile, 0, SEEK_END);
filesize = ftell(infile);
if(offset > filesize) {
fprintf(stderr, "*** Don't set the offset past the filesize (%d)!\n", filesize);
fclose(infile); fclose(outfile) ;
exit(EXIT_FAILURE);
}
delta= filesize - offset ;
if(width * height + offset > filesize) {
fprintf(stderr, "Read with set width:%i, height:%i and offset:%i would go past the end of the file.\n\n>>> change w to %d or h to %d\n", width, height, offset, delta/height, delta/width);
bErr= true ;
}
if(paloffset < 0 || paloffset + 1024 > filesize) {
fprintf(stderr, "Palette offset must be a positive value and within 1024 bytes from the end of the file.\n");
bErr= true ;
}
if (bErr) {
fclose(infile); fclose(outfile) ;
exit(EXIT_FAILURE);
}
fseek(infile, 0, SEEK_SET);
for(i = 0; i < height; i += TILE_HEIGHT) {
for(j = 0; j < width; j += TILE_WIDTH) {
for(k = 0; k < TILE_HEIGHT; k++) {
fseek(infile, offset + (i * width) + j + (k * width), SEEK_SET);
fread(buffer, TILE_WIDTH, 1, infile);
fwrite(buffer, TILE_WIDTH, 1, outfile);
}
}
}
fprintf(stderr, "%d B. pic data written\n", width * height);
fseek(infile, paloffset, SEEK_SET);
fread(palbuffer, 1024, 1, infile);
fwrite(palbuffer, 1024, 1, outfile);
fprintf(stderr, "Palette written\n");
fclose(infile); fclose(outfile);
exit(EXIT_SUCCESS);
}
Now it's rather simple to test: decode a .tpl then encode the resulting file. If you get the same as the original, well, then it's an encoder.
edit: yes, it works.
Use this cmd line:
PaulGuy_enc egbg00.tga 1042 480 360 18
(We have an offset of +18 to the pixel data start (1042=1024+18) and the palette start (18=0+18)
because of the tga header in the egbg00.tga)