📄 regispal.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "..\..\3Dlib\fileio.h"
#include "..\..\3Dlib\filemem.h"
#include "..\..\3Dlib\image.h"
#include "..\..\3Dlib\inline.h"
#include "..\..\3Dlib\texture.h"
struct PaletteStructure {
char *palette;
PaletteStructure *next;
};
union IntChar {
int i;
char c[4];
};
void InsertImage (char *texfile, GLint i);
int SearchPalette (char *imagepal);
void RemovePalette ();
void InsertPalette (PaletteStructure *pal);
int ComparePalette (int *pal1, int *pal2, int total);
PaletteStructure *Head;
PaletteStructure *Current;
CTextureFileMemory fi;
CFileIO myfile;
int *PaletteHandle;
int TotalPalette;
void main (int argc, char *argv[])
{
puts ("\nRegisPal v1.0 by Erick Jap\n");
if (argc < 2) {
puts ("Usage: RegisPal texlistfile [option]");
puts ("where:");
puts ("texlistfile --> Texture list file (output from RegisTex) or image file");
puts ("options:");
puts ("-s --> Input is an image file (LBM,PCX,GIF) not texture list file");
puts ("-dtexdir --> Location of texture files (Default is current directory)");
puts ("-opallistfile --> pallistfile is output file (Default is palette.lst)");
exit (1);
}
int i;
int imageflag;
char infile[80], outfile[80], texdir[80], texfile[256];
strcpy (infile, argv[1]);
strcpy (outfile, "PALETTE.LST");
texdir[0] = '\0';
imageflag = 0;
for (i=2;i < argc;i++) {
if (argv[i][0] == '-') {
if (argv[i][1] == 'o' || argv[i][1] == 'O') {
strcpy ((char *) outfile, (const char *) &(argv[i][2]));
}
else if (argv[i][1] == 'd' || argv[i][1] == 'D') {
strcpy ((char *) texdir, (const char *) &(argv[i][2]));
}
else if (argv[i][1] == 's' || argv[i][1] == 'S') imageflag = 1;
}
}
char filename[TEXTURE_NAME_SIZE];
int totaltexture;
TotalPalette = 0;
PaletteHandle = 0;
if (myfile.openread (outfile)) {
totaltexture = myfile.read_int();
for (i=0; i < totaltexture;i++) myfile.read_int();
TotalPalette = myfile.read_int();
for (i=0; i < TotalPalette;i++) {
char *curpal = (char *) glAllocateMemory (256*4);
if (!curpal) {
printf ("Can not allocate palette memory!\n");
myfile.closefile ();
RemovePalette();
exit (1);
}
PaletteStructure *newpal = (PaletteStructure *) glAllocateMemory (sizeof(PaletteStructure));
if (!newpal) {
printf ("Can not allocate palette structure!\n");
glReleaseMemory ((char *) curpal);
myfile.closefile ();
RemovePalette();
exit (1);
}
newpal -> palette = curpal;
newpal -> next = 0;
myfile.readdata (curpal, 256*4);
InsertPalette (newpal);
}
myfile.closefile ();
}
else {
Head = Current = 0;
TotalPalette = 0;
}
if (!myfile.openread (infile)) {
printf ("Can not open %s!\n", infile);
exit (1);
}
if (!myfile.openwrite (outfile, 1)) {
printf ("Can not open %s!\n", outfile);
myfile.closefile ();
exit (1);
}
if (imageflag) {
totaltexture = 0;
PaletteHandle = 0;
InsertImage (infile, 0);
}
else {
totaltexture = myfile.read_int ();
PaletteHandle = (int *) glAllocateMemory (totaltexture * 4);
if (!PaletteHandle) {
printf ("Can not allocate palette handle memory!\n");
myfile.closefile ();
myfile.closefile (1);
exit (1);
}
memset (PaletteHandle, 0, totaltexture * 4);
while (1) {
i = myfile.read_int(); // texture id
if (i == -1) break;
strcpy (texfile, texdir);
myfile.readdata (filename, TEXTURE_NAME_SIZE);
strcat (texfile, filename);
InsertImage (texfile, i);
}
}
myfile.closefile ();
IntChar var;
var.i = totaltexture;
myfile.writedata (var.c, 4);
for (i=0; i < totaltexture;i++) {
var.i = PaletteHandle[i];
myfile.writedata (var.c, 4);
}
var.i = TotalPalette;
myfile.writedata (var.c, 4);
Current = Head;
while (Current) {
myfile.writedata (Current -> palette, 256*4);
Current = Current -> next;
}
myfile.closefile (1);
glReleaseMemory ((char *) PaletteHandle);
RemovePalette();
exit (0);
}
void InsertImage (char *texfile, GLint i)
{
GLint j = fi.glOpenFileMem ((GLbyte *) texfile);
if (j == 1) {
fi.imageType = CheckImageType ((GLbyte *) texfile);
if (fi.imageType == IMAGE_TYPE_UNKNOWN) {
fi.glCloseFileMem ();
printf ("--- Warning! Image (%s) type is not supported!\n", texfile);
}
else {
fi.glReadFileMem ();
j = ReadTextureImage (&fi);
if (j == GOOD_READ) {
glReleaseMemory ((char *) fi.image.image);
char *curpal = (char *) glAllocateMemory (256*4);
if (curpal) {
int *palptr = (int *) curpal;
char *imgpal = (char *) fi.image.palette;
IntChar var;
var.c[3] = (char) 0xff;
for (j=0; j < 256;j++) {
var.c[0] = *imgpal++;
var.c[1] = *imgpal++;
var.c[2] = *imgpal++;
*palptr++ = var.i;
}
glReleaseMemory ((char *) fi.image.palette);
j = SearchPalette (curpal);
if (j != -1) {
glReleaseMemory ((char *) curpal);
}
else {
PaletteStructure *newpal = (PaletteStructure *) glAllocateMemory (sizeof(PaletteStructure));
if (!newpal) {
glReleaseMemory ((char *) curpal);
printf ("Can not allocate palette memory for file %s!\n", texfile);
myfile.closefile ();
myfile.closefile (1);
glReleaseMemory ((char *) PaletteHandle);
RemovePalette();
exit (1);
}
newpal -> palette = (char *) curpal;
newpal -> next = 0;
InsertPalette (newpal);
j = TotalPalette++;
}
if (PaletteHandle) {
printf ("Processing File %s -- Palette Used #%d\n", texfile, j);
PaletteHandle[i] = j;
}
}
else {
printf ("Can not allocate palette for storage!\n");
myfile.closefile ();
myfile.closefile (1);
glReleaseMemory ((char *) PaletteHandle);
RemovePalette();
exit (1);
}
}
else {
printf ("Error reading file %s!\n", texfile);
myfile.closefile ();
myfile.closefile (1);
glReleaseMemory ((char *) PaletteHandle);
RemovePalette();
exit (1);
}
}
}
else printf ("---- Warning! Can not open file %s!\n", texfile);
}
int SearchPalette (char *imagepal)
{
if (Head) {
PaletteStructure *pal = Head;
GLint i = 0;
while (pal) {
if (ComparePalette ((int *) imagepal, (int *) pal -> palette, 256))
return i;
pal = pal -> next;
i++;
}
}
return -1;
}
void RemovePalette ()
{
while (Head) {
Current = Head;
Head = Head -> next;
glReleaseMemory ((char *) Current -> palette);
glReleaseMemory ((char *) Current);
}
}
void InsertPalette (PaletteStructure *pal)
{
if (Head) Current -> next = pal;
else Head = pal;
Current = pal;
}
int ComparePalette (int *pal1, int *pal2, int total)
{
int i;
for (i=0; i < total; i++) {
if (*pal1++ != *pal2++) return 0;
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -