home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Hack-Phreak Scene Programs
/
cleanhpvac.zip
/
cleanhpvac
/
PCX2IMG.ZIP
/
PCX2IMG.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-07
|
5KB
|
225 lines
/* Please email and changes to knights@gov.on.ca or to the TOR-VR-SIG BBS! */
#include <sys\types.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <math.h>
#include <dos.h>
#include <process.h>
#include <io.h>
#include <mem.h>
#include <io.h>
#include <conio.h>
int infile;
unsigned char ackpal[256][3];
unsigned char pcxpal[256][3];
unsigned char img[64][64];
char _far *ptr;
/* Find the color in ACK's palette that closelt matches the PCX's color
This is done by treating the colors as a 3D cube, red is the x axis,
green is the y and blue is the z. I draw a line between the two
3D points, the shortest line is the result.
These routines SUCK, they are really slow, great job for a math co,
no time to look through graphics gems. Email me: knights@gov.on.ca if
you have a faster routine please!
*/
int find_closest(int chkcol)
{
int i;
double dist;
double savedist;
int savecol;
long dx,dy,dz;
savedist=100000000000;
// printf("%d %d %d =",pcxpal[chkcol][0],pcxpal[chkcol][1],pcxpal[chkcol][2]);
for (i=0; i<256; i++)
{
dx=ackpal[i][0]-pcxpal[chkcol][0];
dy=ackpal[i][1]-pcxpal[chkcol][1];
dz=ackpal[i][2]-pcxpal[chkcol][2];
dist=dx*dx+dy*dy+dz*dz;
if (dist < savedist)
{
savedist=dist;
savecol=i;
}
}
// printf("%d %d %d <\n",ackpal[savecol][0],ackpal[savecol][1],ackpal[savecol][2]);
return(savecol);
}
void putpalette(void)
{
union REGS inregs, outregs;
struct SREGS segregs;
inregs.x.bx = 0; /* first register */
inregs.x.cx = 255; /* 255 of them */
inregs.h.ah = 0x10;
inregs.h.al = 0x12; /* set them */
inregs.x.dx = FP_OFF( &ackpal[0][0] );
segregs.es = FP_SEG( &ackpal[0][0] );
int86x(0x10, &inregs, &outregs, &segregs );
}
/* This PCX routine I "borrowed" from something, it was really poorly
written, so I fixed it up and made some assumptions .... should
work in most PCX files though ...
*/
int read_pcx(char file_name[])
{
FILE *fsave;
int color1,color0;
int pass,x1,y1,x2,y2,x,y,i;
long position;
if ((fsave = fopen(file_name, "rb")) == NULL)
{
printf("\nCan't find infile %s.\n",file_name);
exit(0);
}
else
{
fgetc(fsave);
}
fseek(fsave,0L,SEEK_END);
position=ftell(fsave);
position=position-769;
fseek(fsave,position,SEEK_SET);
pass = fgetc(fsave);
for (i = 0; i < 256; i++)
{
pcxpal[i][0] = fgetc(fsave)/4.047;
pcxpal[i][1] = fgetc(fsave)/4.047;
pcxpal[i][2] = fgetc(fsave)/4.047;
}
fseek(fsave,0L,SEEK_SET);
fgetc(fsave);
for (i = 1; i < 4; i++)
fgetc(fsave);
x1 = getw(fsave);
y1 = getw(fsave);
x2 = getw(fsave);
y2 = getw(fsave);
for (i = 12; i < 16; i++)
fgetc(fsave);
for (i = 0; i < 48; i++)
fgetc(fsave);
for (i = 64; i < 128; i++)
fgetc(fsave);
x=0;
for (y = 0; y+y1 < y2; y++)
{
printf("%d..",y2-y);
x = 0;
while (x <= x2-x1)
{
color1 = fgetc(fsave);
if ((color1 & 0xC0) != 0xC0)
{
color1=find_closest(color1);
if ((x < 64) && (y < 64))
img[y][x]=color1;
x++;
}
else
{
pass = color1 & 0x3F;
color0 = fgetc(fsave);
color0=find_closest(color0);
while (pass-- > 0)
{
if ((x < 64) && (y < 64))
img[y][x]=color0;
x++;
}
}
}
}
fclose(fsave);
return(x2);
}
void main(argc, argv)
int argc;
char * argv[];
{
int outfile;
int i,x,y;
char tt[]={64,0,64,0};
puts("PCX2IMG, to create IMG files for ACK3D");
puts("");
puts("By Shawn Knight");
puts(" internet: knights@gov.on.ca");
puts(" TOR-VR-SIG BBS: Sysop -or- Shawn Knight");
puts(" (416) 631-6625 16.8K dual");
puts("");
if (argc < 4)
{
puts("PCX2IMG <file.PCX> <palette in> <file.IMG>");
puts("");
puts(" file.PCX=input file");
puts(" file.IMG=output file");
puts(" palette in is the ACK3D palette file (called PAL in ACKKIT)");
puts("");
puts("The PCX file should be 64 by 64. If not it will be cropped.");
puts("No scaling done here! :-)");
exit(1);
}
infile = open(argv[2], O_BINARY | O_RDONLY,S_IREAD);
if (infile == -1)
{
puts("PAL file not found...");
exit(1);
}
read(infile, (char *)&ackpal, 768);
close(infile);
outfile = open(argv[3], O_BINARY | O_RDWR | O_CREAT,S_IREAD | S_IWRITE);
if (outfile == -1)
{
puts("can not create output file");
exit( 1 );
}
puts("Press any key to write IMG file after it's displayed!");
// initialize the stupid array, the long way
for (x=0; x<=63; x++)
for (y=0; y<=63; y++)
img[y][x]=0;
read_pcx(argv[1]);
_asm mov al,0x13;
_asm mov ah,0;
_asm int 0x10;
putpalette();
puts("\n\n\n\n\n\n\n");
ptr=(char far *)0xA0000000; /* For direct video writing */
for (i=0; i<=64; i++)
{
memcpy((char far *)ptr, (char far *)&img[i][0], 64);
ptr+=320;
}
getch();
write(outfile, tt, 4);
write(outfile, img, 64*64);
close(outfile);
_asm mov al,3;
_asm mov ah,0;
_asm int 0x10;
puts("SK WAS HERE ...");
}