home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / devel5 / pcxmodey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-27  |  3.3 KB  |  165 lines

  1. /* Load a PCX file in mode Y */
  2.  
  3. /* THIS CODE IS CURRENTLY MODE Y DEPENDENT!
  4.    IT WILL NOT WORK IN OTHER MODES */
  5.  
  6. /* Written by Bernie Roehl, April 1992 based on code by Dave Stampe */
  7.  
  8. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  9.    May be freely used to write software for release into the public domain;
  10.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  11.    for permission to incorporate any part of this software into their
  12.    products!
  13.  
  14.      ATTRIBUTION:  If you use any part of this source code or the libraries
  15.      in your projects, you must give attribution to REND386, Dave Stampe,
  16.      and Bernie Roehl in your documentation, source code, and at startup
  17.      of your program.  Let's keep the freeware ball rolling!
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <dos.h>
  22. #include <alloc.h>
  23.  
  24. #include "f3dkitd.h"  /* reset_hdwe() */
  25.  
  26. static int getbyte(int *c, int *count, FILE *in)
  27. {
  28.     if (feof(in)) return EOF;
  29.     *c = getc(in) & 0xFF;
  30.     if ((*c & 0xC0) == 0xC0) {
  31.         *count = *c & 0x3F;
  32.         if (feof(in)) return EOF;
  33.         *c = getc(in) & 0xFF;
  34.     }
  35.     else
  36.         *count = 1;
  37.     return NULL;
  38. }
  39.  
  40. static void putbyte(int c, int count, FILE *out)
  41. {
  42.     if (count == 0)
  43.         return;
  44.     if (count > 1 || (c & 0xC0) == 0xC0)
  45.         putc(count | 0xC0, out);
  46.     putc(c, out);
  47. }
  48.  
  49. static unsigned char far *screen = MK_FP(0xA000, 0);
  50.  
  51. static void write_line(char far *buffer, char far *scr)
  52. {
  53.     int i, plane;
  54.     char *add, *buff;
  55.  
  56.     for (plane = 0; plane < 4; ++plane)
  57.     {
  58.         outport(0x3C4, (1<<(plane+8))+2);
  59.         add = scr;
  60.         buff = &buffer[plane];
  61.         for (i = 0; i < 80; ++i)
  62.         {
  63.             *add++ = *buff;
  64.             buff += 4;
  65.         }
  66.     }
  67. }
  68.  
  69. load_pcx(FILE *in, int page)
  70. {
  71.     int c, count;
  72.     char buff[330];
  73.     char *buffer = &buff[0];
  74.     char *scr = &screen[16000*page];
  75.     unsigned nread = 0;
  76.  
  77.     reset_hdwe();
  78.     fseek(in, 128L, SEEK_SET); /* skip PCX header */
  79.     while (getbyte(&c, &count, in) != EOF)
  80.         while (count--)
  81.         {
  82.             *buffer++ = c;
  83.             if (++nread == 320)
  84.             {
  85.                 write_line(&buff[0],scr);
  86.                 buffer = &buff[0];
  87.                 nread = 0;
  88.                 scr += 80;
  89.             }
  90.         }
  91.     return 0;
  92. }
  93.  
  94. struct {
  95.     unsigned char manu, hard, encod, bitpx;
  96.     unsigned int x1, y1, x2, y2;
  97.     unsigned int hres, vres;
  98.     unsigned char palette[48];
  99.     unsigned char vmode, nplanes;
  100.     unsigned int bytesPerLine;
  101.     char unused[128-68];
  102. }
  103. pccHeader =
  104.     10, 5, 1, 8, 0, 0, 319, 199, 75, 75, { 
  105.         0     }
  106.     , 0x13, 1, 320, 0 };
  107.  
  108. struct { 
  109.     unsigned char r, g, b; 
  110. }
  111. palbuff[256];
  112.  
  113.  
  114. /* NEED A FUNCTION FOR THIS IN VIDEO DRIVER */
  115.  
  116.  
  117. static char get_pixel(unsigned int adr, int page)
  118. {
  119.     outport(0x3CE, ((adr&3)<<8)+4); /* select plane to read */
  120.     return *(char *)(MK_FP((0xA000+(1008*page)),adr>>2));
  121. }
  122.  
  123.  
  124. save_pcx(FILE *out, int page)
  125. {
  126.     unsigned c, oldc, count;
  127.     unsigned nput = 1;
  128.     union REGS r;
  129.  
  130.     reset_hdwe();
  131.     fwrite(&pccHeader, 128, 1, out);
  132.     count = 1;
  133.     oldc = get_pixel(0,page);
  134.     while ((nput>>2) < 16000)
  135.     {
  136.         c = get_pixel(nput++, page);
  137.         if (c != oldc)
  138.         {
  139.             putbyte(oldc, count, out);
  140.             oldc = c;
  141.             count = 1;
  142.         }
  143.         else if (++count >= 63)
  144.         {
  145.             putbyte(oldc, count, out);
  146.             count = 0;
  147.         }
  148.     }
  149.     putbyte(oldc, count, out);
  150.  
  151.     putc(0x0C, out);
  152.     for (count = 0; count < 256; ++count)
  153.     {
  154.         r.x.ax = 0x1015;
  155.         r.x.bx = count; /* write pallete */
  156.         int86(0x10, &r, &r);
  157.         putc(r.h.dh<<2, out);
  158.         putc(r.h.ch<<2, out);
  159.         putc(r.h.cl<<2, out);
  160.     }
  161.     return 0;
  162. }
  163.  
  164.