home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 2.ddi / OWLSRC.EXE / CSCAPE / SOURCE / PCPMAPIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  7.5 KB  |  291 lines

  1. /*
  2.     pcpmapio.c
  3.  
  4.     % Routines for PC pixel map i/o interface
  5.  
  6.     7/05/89  by Ted.
  7.  
  8.     OWL 1.1
  9.     Copyright (c) 1989 by Oakland Group, Inc.
  10.     ALL RIGHTS RESERVED.
  11.  
  12.     Revision History:
  13.     -----------------
  14.     02/23/88 ted:    Added copy case.
  15. */
  16.  
  17. #include "pcpriv.h"
  18. #include "pmlsreq.h"
  19. #include "pcxdecl.h"
  20.  
  21. #define MAXPLANES    24    /*    max number of planes supported in image format */
  22.  
  23. OSTATIC pcxunpack_func(pc_unpackpcx);
  24. OSTATIC pcxpack_func(pc_packpcx);
  25. OSTATIC void DIGPRIV setobuf(_arg3(pmap_type pmap, byte *obuf[], byte nplanes));
  26. OSTATIC boolean DIGPRIV pixnot0(_arg6(pmap_type pmap, byte **obuf, byte nplanes, opcoord x, opcoord y, byte bmask));
  27. /* -------------------------------------------------------------------------- */
  28.  
  29. int pc_PmapIoReq(msg, indata, outdata)
  30.     dig_pcmsg msg;
  31.     VOID *indata;
  32.     VOID *outdata;
  33. {
  34.     pmaplsreq_struct *pmlsr;
  35.     pmap_type pmap;
  36.     bmap_type bmap;
  37.  
  38.     switch (msg) {
  39.     case PC_LOADPMAP:
  40.     {        
  41.         ocolmap_type cmap = NULL;
  42.  
  43.         pmlsr = (pmaplsreq_struct *)indata;
  44.  
  45.         dig_loadpcx(&pmlsr->frw, &pmlsr->pmap, &cmap, pc_unpackpcx);
  46.         pc_squeezecmap(pmlsr->pmap, cmap, pmlsr->crange);
  47.  
  48.         ocolmap_Close(cmap);
  49.         break;
  50.     }
  51.     case PC_SAVEPMAP:
  52.  
  53.         pmlsr = (pmaplsreq_struct *)indata;
  54.  
  55.         *((boolean *) outdata) =
  56.             dig_savepcx(&pmlsr->frw, pmlsr->pmap, pmlsr->crange, pc_packpcx);
  57.         break;
  58.  
  59.     case PC_BMAPTOPMAP:
  60.     {
  61.         /* Copy / translate an image from the bit map passed in indata */
  62.         /* into the pixel map passed in outdata. */
  63.     }
  64.     break;
  65.     case PC_PMAPTOBMAP:
  66.     {
  67.         /* Copy / translate an image from the pixel map passed in indata */
  68.         /* into the bit map passed in outdata. */
  69.         byte bmask;
  70.         unsigned bmbyte;
  71.         odim x, y;
  72.         byte *obuf[MAXPLANES];
  73.         odim bwidth;
  74.         byte nplanes;
  75.         byte iplane;
  76.  
  77.         pmap = (pmap_type) indata;
  78.         bmap = (bmap_type) outdata;
  79.  
  80.         if (pmap == NULL || bmap == NULL) {
  81.             return(FALSE);
  82.         }
  83.         bwidth = pcpmap_bytewidth(pmap);
  84.         nplanes = pcpmap_nplanes(pmap);
  85.         setobuf(pmap, obuf, nplanes);
  86.  
  87.         bmbyte = 0;
  88.         for (y = 0; y < bmap->height; y++) {
  89.             bmask = 0x80;
  90.             for (x = 0; ; x++) {
  91.                 if (pixnot0(pmap, obuf, nplanes, x, y, bmask)) {
  92.                     bmap->array[bmbyte] |= bmask;
  93.                 }
  94.                 else {
  95.                     bmap->array[bmbyte] &= ~bmask;
  96.                 }
  97.                 if (x >= bmap->width) {
  98.                     break;
  99.                 }
  100.                 if ((bmask >>= 1) == 0) {
  101.                     bmask = 0x80;
  102.                     bmbyte++;
  103.                 }
  104.             }
  105.             for (iplane = 0; iplane < nplanes; iplane++) {
  106.                 obuf[iplane] += bwidth;
  107.             }
  108.             bmbyte++;
  109.         }
  110.         break;
  111.     }
  112.     }
  113.     return(TRUE);
  114. }
  115. /* -------------------------------------------------------------------------- */
  116.  
  117. static boolean DIGPRIV pc_unpackpcx(frw, pcxhdr, pmap)
  118.     struct frw_struct *frw;
  119.     pcxhdr_struct *pcxhdr;
  120.     pmap_type pmap;
  121. {
  122.     byte *obuf[MAXPLANES];
  123.     unsigned height;
  124.     unsigned linebytes, pclinebytes;
  125.     int iline, iplane;
  126.     byte **planes;
  127.     int nplanes;
  128.     int pcnplanes;
  129.     unsigned ocount;
  130.  
  131.     nplanes = pcxhdr->nplanes;
  132.     pcnplanes = pcpmap_nplanes(pmap);
  133.     height = pmap_GetHeight(pmap);
  134.     linebytes = pclinebytes = pcpmap_bytewidth(pmap);
  135.     /* Don't read more bytes per line than the file has. */
  136.     if (linebytes > pcxhdr->linebytes) {
  137.         linebytes = pcxhdr->linebytes;
  138.     }
  139.     /* Initialize the obuf array of image plane buffers pointers */
  140.     if (pcnplanes == 1) {
  141.         /* Set all planes to point to same buffer so it will get overwritten */
  142.         /* by the pcx file planes, to suck all the bytes out of the file */
  143.         for (iplane = 0; iplane < nplanes; iplane++) {
  144.             obuf[iplane] = pcpmap_pixbuf(pmap);
  145.         }
  146.     }
  147.     else {
  148.         planes = pcpmap_planes(pmap);
  149.         for (iplane = 0; iplane < nplanes && iplane < pcnplanes; iplane++) {
  150.             obuf[iplane] = planes[iplane];
  151.         }
  152.         /* If pcxhdr has fewer planes than pmap, clear extra pmap planes to 0 */
  153.         for ( ; iplane < pcnplanes; iplane++) {
  154.             memset(planes[iplane], 0, (unsigned)pcpmap_GetPlaneSize(pmap));
  155.         }
  156.     }
  157.     /* Decode the image */
  158.     for (iline = 0 ; iline < height; iline++) {        /* For every line */
  159.         /* For each plane within a line */
  160.         ocount = 0;
  161.         for (iplane = 0; iplane < nplanes; iplane++) {
  162.             /* Load a line of the plane buffer for the current plane */
  163.             if (!pcx_readline(frw, obuf[iplane], linebytes, &ocount)) {
  164.                 return(FALSE);
  165.             }
  166.             obuf[iplane] += pclinebytes;
  167.         }        /* end of plane loop */
  168.     }        /* end of lines loop */
  169.  
  170.     /* Take care of cases where the linebytes counts don't match */
  171.     if (pclinebytes > pcxhdr->linebytes) {
  172.         /* Pmap wider - fill with 0's */
  173.         for (iplane = 0; iplane < pcnplanes; iplane++) {
  174.             obuf[iplane] -= pclinebytes * height - linebytes; /* Rewind obuf's */
  175.         }
  176.         /* Blank out leftover bytes on every plane on every line */
  177.         linebytes = pclinebytes - linebytes;    /* # of extra bytes per line */
  178.         for (iline = 0 ; iline < height; iline++) {        /* For every line */
  179.             for (iplane = 0; iplane < pcnplanes; iplane++) {
  180.                 memset(obuf[iplane], 0, linebytes);
  181.                 obuf[iplane] += pclinebytes;
  182.             }
  183.         }
  184.     }
  185.     else if (pclinebytes < pcxhdr->linebytes) {
  186.         /* File wider - suck up extra bytes */
  187.         /* Round linebytes up to even #; pcx_readline did. */
  188.         linebytes += linebytes&1;
  189.         ocount = (pcxhdr->linebytes - linebytes) * height;
  190.         for ( ; ocount > 0; ocount--) {
  191.             if (frw_readb(frw) & 0xFF00) return(FALSE);
  192.         }
  193.     }
  194.     /* Take care of case where the pcx file has leftover lines */
  195.     if (height < pcxhdr->ylines) {
  196.         unsigned long lcount;
  197.  
  198.         lcount = (unsigned long) pcxhdr->linebytes *
  199.                     (unsigned long)(pcxhdr->ylines - height);
  200.         for ( ; lcount > 0; lcount--) {
  201.             if (frw_readb(frw) & 0xFF00) return(FALSE);
  202.         }
  203.     }
  204.     return(TRUE);
  205. }
  206. /* -------------------------------------------------------------------------- */
  207.  
  208. static boolean DIGPRIV pc_packpcx(frw, pmap)
  209.     struct frw_struct *frw;
  210.     pmap_type pmap;
  211. {
  212.     unsigned height;
  213.     unsigned linebytes;
  214.     int iline, iplane;
  215.     byte nplanes;
  216.     byte *obuf[MAXPLANES];
  217.  
  218.     nplanes = disp_GetInfo()->nplanes;
  219.     height = pmap_GetHeight(pmap);
  220.  
  221.     /* Note: pmap linebytes must equal pcxhdr->linebytes or pcxhdr->linebytes-1 */
  222.     linebytes = pcpmap_bytewidth(pmap);
  223.  
  224.     /* Initialize the obuf array of image plane buffers pointers */
  225.     setobuf(pmap, obuf, nplanes);
  226.  
  227.     /* Encode the image */
  228.     for (iline = 0 ; iline < height; iline++) {        /* For every line */
  229.  
  230.         /* For each plane within a line */
  231.         for (iplane = 0; iplane < nplanes; iplane++) {
  232.             /* Save a line of the plane buffer for the current plane */
  233.             if (!pcx_writeline(frw, obuf[iplane], linebytes)) {
  234.                 return(FALSE);
  235.             }
  236.             obuf[iplane] += linebytes;
  237.         }        /* end of plane loop */
  238.     }        /* end of lines loop */
  239.     return(TRUE);
  240. }
  241. /* -------------------------------------------------------------------------- */
  242.  
  243. static boolean DIGPRIV pixnot0(pmap, obuf, nplanes, x, y, bmask)
  244.     pmap_type pmap;
  245.     byte **obuf;
  246.     byte nplanes;
  247.     opcoord x;
  248.     opcoord y;
  249.     byte bmask;
  250. {
  251.     int iplane;
  252.     unsigned planebyte;
  253.  
  254.     if (y < pmap_GetHeight(pmap) && x < pmap_GetWidth(pmap)) {
  255.         
  256.         if (pcpmap_pixbits(pmap) == 8) {
  257.             bmask = 0xFF;
  258.             planebyte = x;
  259.         }
  260.         else planebyte = x >> 3;
  261.         for (iplane = 0; iplane < nplanes; iplane++) {
  262.             if ((obuf[iplane][planebyte] & bmask) != 0) {
  263.                 return(TRUE);
  264.             }
  265.         }
  266.     }
  267.     return(FALSE);
  268. }
  269. /* -------------------------------------------------------------------------- */
  270.  
  271. static void DIGPRIV setobuf(pmap, obuf, nplanes)
  272.     pmap_type pmap;
  273.     byte *obuf[];
  274.     byte nplanes;
  275. {
  276.     byte **planes;
  277.     int iplane;
  278.  
  279.     if (nplanes <= 1) {
  280.         obuf[0] = pcpmap_pixbuf(pmap);
  281.     }
  282.     else {
  283.         planes = pcpmap_planes(pmap);
  284.         for (iplane = 0; iplane < nplanes; iplane++) {
  285.             obuf[iplane] = planes[iplane];
  286.         }
  287.     }
  288. }
  289. /* -------------------------------------------------------------------------- */
  290.  
  291.