Alter color palette of *.GIF files (for D1/Infinity graphics) so that brightness and contrast fit into D2 - kudos to Paul Siramy for indirectly giving me the idea to write this.
Notes: units.dat = D2 unit palette, palshift.dat = color shift with fixup for brightness / contrast (made in Paintshop Pro and converted via PalAnalyzer).
Sample (open both at the same time):
http://img145.imageshack.us/img145/5410/a1pw8.gif - before adjustment
http://img145.imageshack.us/img145/5608 ... gedvh1.gif - after adjustment
Code: Select all
#include <cstdio>
#include <cstdlib>
#include <cstring>
#pragma pack(1)
struct RgbColor
{
unsigned char r;
unsigned char g;
unsigned char b;
};
struct Palette
{
RgbColor color[256];
};
#pragma pack()
int __cdecl main(int argc, char** argv)
{
if (argc != 2)
{
printf("CORRECT USAGE: <FILENAME>");
return 0;
}
FILE* fpal = fopen("units.dat","rb");
if (fpal == 0)
{
printf("COULDN'T OPEN UNITS.DAT");
return 0;
}
Palette unit_pal;
fread(unit_pal.color,1,256*3,fpal);
fclose(fpal);
FILE* shift = fopen("palshift.dat","rb");
if (shift == 0)
{
printf("COULDN'T OPEN PALSHIFT.DAT");
return 0;
}
unsigned char palshift[256];
fread(palshift,1,256,shift);
fclose(shift);
char* file_name = argv[1];
FILE* file = fopen(file_name,"rb");
if (file == 0)
{
printf("FILE DOESN'T EXIST OR CANNOT BE OPENED");
return 0;
}
fseek(file,0,SEEK_END);
int size = ftell(file);
rewind(file);
if (size <= 0)
{
printf("INVALID FILE SIZE!");
return 0;
}
unsigned char* buffer = (unsigned char*)malloc(size);
if (buffer == 0)
{
printf("COULDN'T ALLOCATE BUFFER");
fclose(file);
return 0;
}
size = fread(buffer,1,size,file);
fclose(file);
char type[7];
memcpy(type,buffer,7);
type[6] = 0;
if (strcmp(type,"GIF89a") != 0)
{
free(buffer);
printf("WRONG FILE FORMAT, PLEASE USE GIF 89A");
return 0;
}
if ((buffer[10] & 1) == 0)
{
free(buffer);
printf("PLEASE USE A GIF FILE WITH A GLOBAL PALETTE");
return 0;
}
if ((buffer[10] & 128) == 0)
{
free(buffer);
printf("PLEASE USE A GIF FILE WITH A 8-BIT PALETTE");
return 0;
}
Palette* palette = (Palette*)(buffer + 13);
int trans = buffer[11];
for (int i = 0; i < 256; i++)
{
int idx = palshift[i];
if (i != trans)
{
int r = unit_pal.color[idx].b;
int g = unit_pal.color[idx].g;
int b = unit_pal.color[idx].r;
palette->color[i].r = r;
palette->color[i].g = g;
palette->color[i].b = b;
}
}
char* f = file_name;
while(*f != 0)
{
if (*f == '.') {
*f = 0;
break;
}
f++;
}
char new_name[256];
sprintf(new_name,"RECALIBRATED_%s.gif",file_name);
FILE* out = fopen(new_name,"wb");
if (out != 0)
{
fwrite(buffer,1,size,out);
fclose(out);
}
free(buffer);
return 0;
}
A modified version of the above
Code: Select all
#pragma comment(linker,"/FILEALIGN:512")
#include <cstdio>
#include <cstdlib>
#include <cstring>
#pragma pack(1)
struct RgbColor
{
unsigned char r;
unsigned char g;
unsigned char b;
};
struct Palette
{
RgbColor color[256];
};
#pragma pack()
int __cdecl main(int argc, char** argv)
{
if (argc != 5)
{
printf("CORRECT USAGE: <FILENAME>");
return 0;
}
FILE* fpal = fopen("units.dat","rb");
if (fpal == 0)
{
printf("COULDN'T OPEN UNITS.DAT");
return 0;
}
int nPal = atoi(argv[3]);
Palette unit_pal;
fread(unit_pal.color,1,256*3,fpal);
fclose(fpal);
FILE* shift = fopen(argv[4],"rb");
if (shift == 0)
{
printf("COULDN'T OPEN PALSHIFT.DAT");
return 0;
}
unsigned char* palshift = (unsigned char*)malloc(nPal*256);
if (palshift == 0)
{
printf("COULDN'T ALLOCATE BUFFER");
return 0;
}
fread(palshift,1,nPal*256,shift);
fclose(shift);
char* file_name = argv[1];
FILE* file = fopen(file_name,"rb");
if (file == 0)
{
free(palshift);
printf("FILE DOESN'T EXIST OR CANNOT BE OPENED");
return 0;
}
fseek(file,0,SEEK_END);
int size = ftell(file);
rewind(file);
if (size <= 0)
{
free(palshift);
printf("INVALID FILE SIZE!");
return 0;
}
unsigned char* buffer = (unsigned char*)malloc(size);
if (buffer == 0)
{
free(palshift);
printf("COULDN'T ALLOCATE BUFFER");
fclose(file);
return 0;
}
size = fread(buffer,1,size,file);
fclose(file);
char type[7];
memcpy(type,buffer,7);
type[6] = 0;
if (strcmp(type,"GIF89a") != 0)
{
free(palshift);
free(buffer);
printf("WRONG FILE FORMAT, PLEASE USE GIF 89A");
return 0;
}
if ((buffer[10] & 1) == 0)
{
free(palshift);
free(buffer);
printf("PLEASE USE A GIF FILE WITH A GLOBAL PALETTE");
return 0;
}
if ((buffer[10] & 128) == 0)
{
free(palshift);
free(buffer);
printf("PLEASE USE A GIF FILE WITH A 8-BIT PALETTE");
return 0;
}
int offset = atoi(argv[2]) * 256;
Palette* palette = (Palette*)(buffer + 13);
int trans = buffer[11];
for (int i = 0; i < 256; i++)
{
int idx = palshift[offset+i];
if (i != trans)
{
int r = unit_pal.color[idx].b;
int g = unit_pal.color[idx].g;
int b = unit_pal.color[idx].r;
palette->color[i].r = r;
palette->color[i].g = g;
palette->color[i].b = b;
}
}
char* f = file_name;
while(*f != 0)
{
if (*f == '.') {
*f = 0;
break;
}
f++;
}
char new_name[256];
sprintf(new_name,"%s_changed.gif",file_name);
FILE* out = fopen(new_name,"wb");
if (out != 0)
{
fwrite(buffer,1,size,out);
fclose(out);
}
free(palshift);
free(buffer);
return 0;
}