/* ClutAdd V0.2 A TIM file CLUT table appender. Copyright 1998, Dominic F. A. Collins. Permission is granted to freely copy and use this software, subject to the restrictions outlined in the help text. */ #define MAXCLUTS 50 #define BUFFER_SIZE 65536 #define NAMELENGTH 64 #define LINES 22 #include #include #include char clutbsel=0; char debugf=0; const char headerid[]={0x10, 0x00, 0x00, 0x00}; // Header file ID, with file version. const char ext[4][5]={"\0",".tim",".clt",".pal"}; void MakeCLUTSpace(size_t csize); char TestTIM(char *data); char *cluts[2]={NULL, NULL}; // Pointer to raw tile data. char *oldclut=NULL; void * buf=NULL; int i,j; unsigned int k; char inp[NAMELENGTH]; /* Input parameter. */ char timinfilename[NAMELENGTH]; // TIM Input filename. char clutinfilename[MAXCLUTS][NAMELENGTH]; // CLUT Input filename. char clutintype[MAXCLUTS]; char timoutfilename[NAMELENGTH]="A.TIM"; // TIM Output filename. char cnum=0; char ctype=1; char datatype=1; size_t totlength; size_t clutlength[2]={0, 0}; size_t tempsize; int clutcols=256; unsigned int black=0x8000; unsigned int transbit=0; char clutflag=1; FILE * iostream; // Used for all file I/O. FILE * iotwo; // Used to filter TIM file main(int paramlength, char *param[]) { timinfilename[0]=0; memset( &clutintype, 0, (size_t)MAXCLUTS ); for (i=1; i>3); if (k==0) { k=black; } else { k^=transbit; } *(char*)(cluts[clutbsel]+tempsize+i+i)=k; *(char*)(cluts[clutbsel]+tempsize+i+i+1)=(k>>8); } free(buf); break; default: puts("Unknown error!\n"); break; } fclose(iostream); } } if (clutlength[clutbsel]==0) { printf("\nNo CLUTS found!\nType CLUTADD -? for help\n"); exit(1); } printf("\nAdding CLUTs to TIM file '%s'\nLoading...", timinfilename); if ((iostream=fopen(timinfilename, "rb"))==NULL) { strcpy(inp, timinfilename); // This 'if' statement prevents a silly string overflow bug with strcat(). if (strlen(timinfilename)<(NAMELENGTH-4)) { strcat(inp, ".tim"); } if ((iostream=fopen(&inp[0], "rb"))==NULL) { printf("File not found\nUnable to add CLUTs\n"); exit(1); } } fseek(iostream, 0, SEEK_END); totlength=ftell(iostream); if (debugf) { printf("Length: %ld...", totlength); } fseek(iostream, 0, SEEK_SET); if ((fread(&inp[0], 1, 20, iostream))!=20) { printf("File read error!\n"); exit(1); } if (TestTIM(&inp[0])) { printf("Bad TIM header\n"); exit(2); } printf("Creating output file '%s'\n", timoutfilename); iotwo=fopen(timoutfilename, "wb"); if (iotwo==NULL) { printf("Failed to create output file!\n"); exit(1); } if (!(inp[4]&(1<<3))) { printf("TIM has no CLUT!\n"); clutflag=0; inp[4]=((inp[4])&(0xf0))|((1<<3)|4); fseek(iostream, 8, SEEK_SET); tempsize=clutlength[clutbsel]+12; inp[16]=clutcols&0xff; inp[17]=clutcols>>8; inp[12]=0; inp[13]=0; inp[14]=0xe0; inp[15]=0x01; } else { tempsize=inp[8]+(inp[9]<<8)+(inp[10]<<16)+(inp[11]<<24)+clutlength[clutbsel]; clutcols=inp[16]+(inp[17]<<8); } inp[8]=(tempsize)&(0xff); inp[9]=(tempsize>>8)&(0xff); inp[10]=(tempsize>>16)&(0xff); inp[11]=(tempsize>>24)&(0xff); tempsize-=12; i=tempsize/clutcols; i=i/2; inp[18]=i&(0xff); inp[19]=(i>>8)&(0xff); tempsize-=clutlength[clutbsel]; if ((fwrite(&inp[0], 1, 20, iotwo))!=20) { printf("Error writing file!\n"); exit(1); } if (clutflag) { oldclut=malloc(tempsize); if (oldclut==NULL) { printf("Not enough memory to copy old CLUTs!\n"); exit(1); } if ((fread( oldclut, 1, tempsize, iostream))>1); if (c&1) { puts(help[i]); j--; } if (j==0) { puts("Press return to continue..."); getchar(); j=LINES; } } }