home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / PSCALE.ZIP / PUTSTEST.CPP < prev   
Encoding:
C/C++ Source or Header  |  1993-06-06  |  4.5 KB  |  208 lines

  1.  
  2.  
  3. #include <iostream.h>
  4. #include <fstream.h>
  5. #include <dos.h>
  6. #include <conio.h>
  7.  
  8. //    NOTES:
  9.  
  10. /*
  11.     This is a rough first draft, very sloppy, but I'm just happy to finally
  12.     see the damned thing working.  Such as it is, consider it to be released
  13.     into the public domain, to be folded, spindled and mutilated as needs be.
  14.  
  15.     The assembler will need quite a bit of optimization, and get's sketchy
  16.     when you try to scale too small.  The trouble is that the division factor
  17.     goes over the byte limit, and gives a divide error.  I've already shifted
  18.     the whole thing by two bits to compensate, and limited the range to 64
  19.     pixels, instead of 256.
  20.  
  21.     It might also make sense to create a special case handler for when no
  22.     scaling is necassary.
  23.  
  24.     The calling function should be changed to accept pointers instead of
  25.     ints, I just didn't want to deal with it.
  26.  
  27.     Thanks to all in the Game Design forum.
  28.  
  29.     Jesse Montrose [76646,3302]
  30.  
  31. */
  32.  
  33.  
  34. typedef unsigned char byte;
  35. typedef unsigned int word;
  36.  
  37. extern "C" void PutScale (int imageSeg, int imageOff, int imageWidth,
  38.     int imageHeight, int destSeg, int destOff, int destWidth,
  39.     int destStart, int SizeX, int SizeY);
  40.  
  41. int LoadPCX(char *name);
  42.  
  43. byte far *image;
  44. int imageWidth;        // original bitmap width
  45. int imageHeight;    // and height
  46.  
  47. void main (int argc, char *argv[]) {
  48.  
  49.  
  50.     if (argc < 2) {
  51.         cout << "\n\nusage: PUTSCALE image.pcx\n\n";
  52.         return;
  53.     }
  54.  
  55.     cout << "\n\n    Controls: UP-ARROW, DOWN-ARROW, ESC.";
  56.     cout << "\n\n   I guess you can figure out what they do.\n\n";
  57.     while (!kbhit());
  58.  
  59.     char *filename=argv[1];
  60.  
  61.     asm {
  62.         push ax
  63.         mov ah,0x00
  64.         mov al,0x13
  65.         int 0x10
  66.         pop ax
  67.     }
  68.  
  69.     if (!LoadPCX (filename))
  70.         return;
  71.  
  72.     int imageSeg=FP_SEG(image);
  73.     int imageOff=FP_OFF(image);
  74.  
  75.     int SizeX=imageWidth;    // new size x to scale to
  76.     int SizeY=imageHeight;    // new size y to scale to
  77.  
  78.     int destWidth=320;        // dest could also be another bitmap
  79.     int destStart=destWidth*100+40;
  80.     int destSeg=0xa000;        // video seg
  81.     int destOff=0;
  82.  
  83.     char ch='\0';
  84.  
  85.     while (ch!=27) {
  86.         PutScale (imageSeg, imageOff, imageWidth, imageHeight, destSeg,
  87.             destOff, destWidth, destStart, SizeX, SizeY);
  88.  
  89.         ch=getch();
  90.         switch (ch) {
  91.             case 72:    // up arrow, increase size as if moving closer
  92.  
  93.                 if (SizeX+4<61) {
  94.                     SizeX+=4;
  95.                     SizeY+=4;
  96.                 }
  97.                 break;
  98.             case 80:    // down arrow, decrease size as if moving away
  99.                 if (SizeX-4>3) {
  100.                     SizeX-=4;
  101.                     SizeY-=4;
  102.                 }
  103.                 break;
  104.         }
  105.     }
  106.  
  107.     // back to text mode.
  108.     asm {
  109.         push    ax
  110.         mov    ah,0x00
  111.         mov    al,0x03
  112.         int    0x10
  113.         pop    ax
  114.     }
  115. }
  116.  
  117. struct pcx_hdr {
  118.     char Mfg;                // manufacturer, always 0xa0
  119.     char Ver;                // encoder version number
  120.     char Enc;                // encoding code, always 1
  121.     char Bpp;                // bits per pixel, 8 in mode 0x13
  122.     int     Xmin,Ymin;            // image origin, usually 0,0
  123.     int     Xmax,Ymax;            // image dimensions
  124.     int     Hres;                // horizontal resolution value
  125.     int     Vres;                // vertical resolution value
  126.     char Pal[48];            // palette (not in mode 0x13)
  127.     char Reserved;           // who knows?
  128.     char ClrPlanes;            // number of planes, 1 in mode 0x13
  129.     int     Bpl;                // bytes per line, 80 in mode 0x13
  130.     int     PalType;            // Grey or Color palette flag
  131.     char Filler[58];        // Zsoft wanted a 128 byte header
  132. };
  133.  
  134. int LoadPCX(char *name) {
  135.  
  136.     ifstream pcx (name, ios::binary);
  137.     if (!pcx) {
  138.         cout << "Couldn't open " << name << "\n";
  139.         return 0;
  140.     }
  141.  
  142.     unsigned int     bytes=0;       // counts unpacked bytes
  143.     unsigned char    c;             // byte being processed
  144.     unsigned int     runlen;        // length of packet
  145.     unsigned int num_bytes;
  146.  
  147.     char pcxColors[3*256];
  148.  
  149.     pcx_hdr pcxh;
  150.  
  151.     pcx.read ( (char*) &pcxh, sizeof (pcxh) );
  152.  
  153.     imageWidth=pcxh.Xmax+1;
  154.     imageHeight=pcxh.Ymax+1;
  155.  
  156.     num_bytes = imageWidth*imageHeight;
  157.  
  158.     image= new far byte[num_bytes+1];
  159.     if (image==NULL) {
  160.         cout << "out of mem on file " << name << "\n";
  161.         return 0;
  162.     }
  163.  
  164.     do {
  165.         pcx.get (c);
  166.  
  167.         if ((c & 0xc0) == 0xc0)    {    // high 2 bits set is packet
  168.             runlen = (c & 0x3f);    // AND off the high bits
  169.             pcx.get (c);
  170.             while(runlen--)    (image[bytes++]=c);
  171.         } else
  172.             image[bytes++]=c;
  173.     } while (bytes<num_bytes);
  174.  
  175.     pcx.get(c);    // this is a marker before the palette
  176.  
  177.  
  178.     for (int i=0; i<(3*256); i++) {
  179.         pcx.get (c);
  180.         pcxColors[i]= (c >> 2);
  181.     }
  182.  
  183.  
  184.     // set palette
  185.  
  186.     int offcol=FP_OFF(pcxColors);
  187.     int segcol=FP_SEG(pcxColors);
  188.     asm{
  189.         mov    dx,es
  190.         push    dx
  191.         mov    dx,segcol
  192.         mov    es,dx
  193.         mov    dx,offcol
  194.         mov    ah,0x10
  195.         mov    al,0x12
  196.         mov    bx,0
  197.         mov    cx,0x100
  198.         int    0x10
  199.         pop    dx
  200.         mov    es,dx
  201.     }
  202.  
  203.  
  204.     return 1;
  205. }
  206.  
  207.  
  208.