home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCX2IMG.ZIP / PCX2IMG.C < prev    next >
C/C++ Source or Header  |  1993-07-07  |  5KB  |  225 lines

  1. /* Please email and changes to knights@gov.on.ca or to the TOR-VR-SIG BBS! */
  2.  
  3. #include <sys\types.h>
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <sys\stat.h>
  7. #include <math.h>
  8. #include <dos.h>
  9. #include <process.h>
  10. #include <io.h>
  11. #include <mem.h>
  12. #include <io.h>
  13. #include <conio.h>
  14.  
  15. int infile;
  16. unsigned char ackpal[256][3];
  17. unsigned char pcxpal[256][3];
  18. unsigned char img[64][64];
  19. char _far *ptr;
  20.  
  21. /* Find the color in ACK's palette that closelt matches the PCX's color
  22.  
  23. This is done by treating the colors as a 3D cube, red is the x axis,
  24. green is the y and blue is the z.  I draw a line between the two
  25. 3D points, the shortest line is the result.
  26.  
  27. These routines SUCK, they are really slow, great job for a math co,
  28. no time to look through graphics gems.  Email me: knights@gov.on.ca if
  29. you have a faster routine please!
  30. */
  31.  
  32. int find_closest(int chkcol)
  33.     {
  34.     int i;
  35.     double dist;
  36.     double savedist;
  37.     int savecol;
  38.     long dx,dy,dz;
  39.  
  40.     savedist=100000000000;
  41.  
  42. //    printf("%d %d %d =",pcxpal[chkcol][0],pcxpal[chkcol][1],pcxpal[chkcol][2]);
  43.     for (i=0; i<256; i++)
  44.         {
  45.         dx=ackpal[i][0]-pcxpal[chkcol][0];
  46.         dy=ackpal[i][1]-pcxpal[chkcol][1];
  47.         dz=ackpal[i][2]-pcxpal[chkcol][2];
  48.         dist=dx*dx+dy*dy+dz*dz;
  49.         if (dist < savedist)
  50.             {
  51.             savedist=dist;
  52.             savecol=i;
  53.             }
  54.         }
  55. //    printf("%d %d %d <\n",ackpal[savecol][0],ackpal[savecol][1],ackpal[savecol][2]);
  56.     return(savecol);
  57.     }
  58.  
  59. void putpalette(void)
  60.     {
  61.     union REGS inregs, outregs;
  62.     struct SREGS segregs;
  63.  
  64.     inregs.x.bx = 0;    /* first register */
  65.     inregs.x.cx = 255;    /* 255 of them */
  66.     inregs.h.ah = 0x10;
  67.     inregs.h.al = 0x12; /* set them */
  68.     inregs.x.dx = FP_OFF( &ackpal[0][0] );
  69.     segregs.es = FP_SEG( &ackpal[0][0] );
  70.     int86x(0x10, &inregs, &outregs, &segregs );
  71.     }
  72.  
  73.  
  74. /* This PCX routine I "borrowed" from something, it was really poorly
  75. written, so I fixed it up and made some assumptions ....  should
  76. work in most PCX files though ...
  77. */
  78. int read_pcx(char file_name[])
  79. {
  80.     FILE *fsave;
  81.     int color1,color0;
  82.     int pass,x1,y1,x2,y2,x,y,i;
  83.     long position;
  84.  
  85.     if ((fsave = fopen(file_name, "rb")) == NULL)
  86.         {
  87.         printf("\nCan't find infile %s.\n",file_name);
  88.         exit(0);
  89.         }
  90.     else
  91.         {
  92.         fgetc(fsave);
  93.         }
  94.  
  95.     fseek(fsave,0L,SEEK_END);
  96.     position=ftell(fsave);
  97.     position=position-769;
  98.     fseek(fsave,position,SEEK_SET);
  99.     pass = fgetc(fsave);
  100.     for (i = 0; i < 256; i++)
  101.         {
  102.         pcxpal[i][0] = fgetc(fsave)/4.047;
  103.         pcxpal[i][1] = fgetc(fsave)/4.047;
  104.         pcxpal[i][2] = fgetc(fsave)/4.047;
  105.         }
  106.     fseek(fsave,0L,SEEK_SET);
  107.     fgetc(fsave);
  108.     for (i = 1; i < 4; i++)
  109.         fgetc(fsave);
  110.     x1 = getw(fsave);
  111.     y1 = getw(fsave);
  112.     x2 = getw(fsave);
  113.     y2 = getw(fsave);
  114.     for (i = 12; i < 16; i++)
  115.         fgetc(fsave);
  116.     for (i = 0; i < 48; i++)
  117.         fgetc(fsave);
  118.     for (i = 64; i < 128; i++)
  119.         fgetc(fsave);
  120.  
  121.     x=0;
  122.     for (y = 0; y+y1 < y2; y++)
  123.         {
  124.         printf("%d..",y2-y);
  125.         x = 0;
  126.         while (x <= x2-x1)
  127.             {
  128.             color1 = fgetc(fsave);
  129.             if ((color1 & 0xC0) != 0xC0)
  130.                 {
  131.                 color1=find_closest(color1);
  132.                 if ((x < 64) && (y < 64))
  133.                       img[y][x]=color1;
  134.                 x++;
  135.                 }
  136.             else
  137.                 {
  138.                 pass = color1 & 0x3F;
  139.                 color0 = fgetc(fsave);
  140.                 color0=find_closest(color0);
  141.                 while (pass-- > 0)
  142.                     {
  143.                     if ((x < 64) && (y < 64))
  144.                         img[y][x]=color0;
  145.                     x++;
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.     fclose(fsave);
  151.     return(x2);
  152. }
  153.  
  154. void main(argc, argv)
  155.     int argc;
  156.     char * argv[];
  157.     {
  158.     int outfile;
  159.     int i,x,y;
  160.     char tt[]={64,0,64,0};
  161.  
  162.     puts("PCX2IMG, to create IMG files for ACK3D");
  163.     puts("");
  164.     puts("By Shawn Knight");
  165.     puts("  internet:        knights@gov.on.ca");
  166.     puts("  TOR-VR-SIG BBS:  Sysop -or- Shawn Knight");
  167.     puts("                   (416) 631-6625 16.8K dual");
  168.     puts("");
  169.     if (argc < 4)
  170.         {
  171.         puts("PCX2IMG <file.PCX> <palette in> <file.IMG>");
  172.         puts("");
  173.         puts("  file.PCX=input file");
  174.         puts("  file.IMG=output file");
  175.         puts("  palette in is the ACK3D palette file (called PAL in ACKKIT)");
  176.         puts("");
  177.         puts("The PCX file should be 64 by 64.  If not it will be cropped.");
  178.         puts("No scaling done here!  :-)");
  179.  
  180.         exit(1);
  181.         }
  182.     infile = open(argv[2], O_BINARY | O_RDONLY,S_IREAD);
  183.     if (infile == -1)
  184.         {
  185.         puts("PAL file not found...");
  186.         exit(1);
  187.         }
  188.     read(infile, (char *)&ackpal, 768);
  189.     close(infile);
  190.     outfile = open(argv[3], O_BINARY | O_RDWR | O_CREAT,S_IREAD | S_IWRITE);
  191.     if (outfile == -1)
  192.         {
  193.         puts("can not create output file");
  194.         exit( 1 );
  195.         }
  196.  
  197.     puts("Press any key to write IMG file after it's displayed!");
  198.     // initialize the stupid array, the long way
  199.     for (x=0; x<=63; x++)
  200.         for (y=0; y<=63; y++)
  201.             img[y][x]=0;
  202.  
  203.     read_pcx(argv[1]);
  204.     _asm mov al,0x13;
  205.     _asm mov ah,0;
  206.     _asm int 0x10;
  207.     putpalette();
  208.     puts("\n\n\n\n\n\n\n");
  209.     ptr=(char far *)0xA0000000;      /* For direct video writing     */
  210.     for (i=0; i<=64; i++)
  211.         {
  212.         memcpy((char far *)ptr, (char far *)&img[i][0], 64);
  213.         ptr+=320;
  214.         }
  215.     getch();
  216.     write(outfile, tt, 4);
  217.     write(outfile, img, 64*64);
  218.     close(outfile);
  219.     _asm mov al,3;
  220.     _asm mov ah,0;
  221.     _asm int 0x10;
  222.     puts("SK WAS HERE ...");
  223.     }
  224.  
  225.