home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pcx_c.zip / PCX.C
Text File  |  1994-07-17  |  6KB  |  257 lines

  1.  
  2. /***********************************************************************/
  3. /* from PCX.H */
  4.  
  5.  
  6.  /*****************************************************************************/
  7.  /*                                                                           */
  8.  /* PCX File Data Structures                                                  */
  9.  /*                                                                           */
  10.  /*****************************************************************************/
  11.  
  12.  #define PCXHEADERIDENTIFIER    0x0A
  13.  #define PCXTRAILERIDENTIFIER   0x0C
  14.  
  15.  typedef struct _PCXCOLORMAP { /* pcxc */
  16.      BYTE    bRed;
  17.      BYTE    bGreen;
  18.      BYTE    bBlue;
  19.  } PCXCOLORMAP;
  20.  typedef PCXCOLORMAP FAR *PPCXCOLORMAP;
  21.  
  22.  typedef struct _PCXHEADER { /* pcxh */
  23.      BYTE         bHeaderID;                      /* x'0A'                    */
  24.      BYTE         bVersion;                       /* 0 = 2.5                  */
  25.                                                   /* 2 = 2.8 w/palette data   */
  26.                                                   /* 3 = 2.8 w/o palette data */
  27.                                                   /* 5 = 3.0 w/palette data   */
  28.      BYTE         bEncodingMode;                  /* 1 = RLE                  */
  29.      BYTE         bNumBitsPerPelPerPlane;         /* Bits per pel per plane   */
  30.      USHORT       usXTopLeft;                     /* Cutout window coords     */
  31.      USHORT       usYTopLeft;
  32.      USHORT       usXBottomRight;
  33.      USHORT       usYBottomRight;
  34.      USHORT       usHorzResolution;
  35.      USHORT       usVertResolution;
  36.      PCXCOLORMAP  apcxcPalette16[16];             /* 16-color RGB palette     */
  37.      BYTE         bIgnored;
  38.      BYTE         bNumPlanes;                     /* Normally 1, 3, 4         */
  39.                                                   /* 0 if 2.5 compatible      */
  40.      USHORT       usBytesPerLine;                 /* Bytes per scan line      */
  41.      USHORT       usPaletteInfo;                  /* 1 = Color/BW             */
  42.                                                   /* 2 = Grayscale            */
  43.      BYTE         abIgnored[58];
  44.  } PCXHEADER;
  45.  typedef PCXHEADER FAR *PPCXHEADER;
  46.  
  47.  typedef struct _PCXTRAILER { /* pcxt */
  48.      BYTE         bTrailerID;                     /* x'0C'                    */
  49.      PCXCOLORMAP  apcxcPalette256[256];           /* 256-color RGB palette    */
  50.  } PCXTRAILER;
  51.  typedef PCXTRAILER FAR *PPCXTRAILER;
  52.  
  53.  
  54.  
  55.  
  56. /***********************************************************************/
  57. /* from PCX.H */
  58.  
  59.  
  60.  
  61.  
  62.  
  63. /* declared in .c file */
  64.  
  65. PCXHEADER pcxheader;
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /* this is how process_file is called */
  73.  
  74.    process_file( "apcxfile.pcx" );
  75.  
  76.  
  77.  
  78.  
  79.  
  80. /* the message that processes a new PCX file */
  81.  
  82.  
  83. case WM_USER+1:
  84.  
  85.        if(NEW_FILE == 1)
  86.        {
  87.            NEW_FILE = 0;
  88.  
  89.            rect.xLeft = s_xtl-1;
  90.            rect.yTop = s_ytl+1;
  91.            rect.yBottom = swp_hs.y + swp_hs.cy + border_width;
  92.            rect.xRight = swp_vs.x - border_width;
  93.  
  94.            WinFillRect(hpsWin, &rect, CLR_WHITE);      /* clears client window */
  95.        }
  96.        pt.x = s_xtl;
  97.        pt.y = s_ytl;
  98.  
  99.        bm_ptr = start_point;
  100.        stp_pt = s_ybr;
  101.  
  102.        x = display_bits( hpsWin, bm_ptr, pt, stp_pt );
  103.  
  104.        break;
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. /* this function displays the image to the screen */
  113.  
  114.  
  115.  
  116.  
  117. INT display_bits( HPS hps, CHAR *bitmap_ptr, POINTL pt, LONG stp_pt )
  118. {
  119.  
  120. LONG x;
  121. APIRET rc;
  122.  
  123.    for( x=0 ; pt.y >= stp_pt ; pt.y-- )
  124.    {
  125.        GpiMove( hps, &pt );
  126.        rc = GpiImage(hps, 0L, &sizl, nbytes, bitmap_ptr+x+spo );     /* draws the image */
  127.        if( rc != GPI_OK )
  128.            return(-1);
  129.        x = x + byte_line_length;
  130.    } /* end for */
  131.  
  132.    return(x);
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. /* this function reads a PCX file into memory */
  141. /* it should be updated to use newer memory stuff */
  142.  
  143.  
  144.  
  145.  
  146.  
  147. void process_file ( CHAR *cptr )
  148. {
  149.  
  150. ULONG allocflgs = PAG_WRITE | PAG_READ;
  151. APIRET apirc;
  152.  
  153. PVOID *temp_point;
  154.  
  155.  
  156.    fh= fopen(cptr, "rb");
  157.    fread( (void *)&pcxheader, sizeof(pcxheader), 1, fh);
  158.    total_bytes = pcxheader.usBytesPerLine * ( pcxheader.usYBottomRight + 1 );
  159.  
  160.    page_length =  pcxheader.usYBottomRight + 1;
  161.    line_length =  pcxheader.usXBottomRight + 1;
  162.    byte_line_length = pcxheader.usBytesPerLine;
  163.  
  164.  
  165. /*
  166.    if( MEM_ALLOCATED )
  167.    {
  168.        free( start_point );
  169.        MEM_ALLOCATED = 0;
  170.    }
  171.  else
  172.    {
  173.        first_line = start_point = malloc( sizeof(CHAR)* total_bytes );
  174.  
  175.        MEM_ALLOCATED = 1;
  176.    }
  177. */
  178.  
  179.    if( MEM_ALLOCATED )
  180.        DosFreeMem( first_line );
  181.  
  182.    apirc = DosAllocMem( (PPVOID)&start_point, sizeof(CHAR)*total_bytes, fALLOC );
  183.  
  184.    MEM_ALLOCATED = 1;
  185.  
  186.    first_line = start_point;
  187.  
  188.    x=0;
  189.  
  190.    spo=0;
  191.  
  192.    while( (frc = fread( (void *)&buffer, 1, 1, fh)) == 1 )
  193.    {
  194.        if(x >= total_bytes)
  195.            break;
  196.  
  197.        if( (buffer & 0XC0) == 0XC0 )
  198.        {
  199.            runlength = buffer & 0X3F;
  200.            fread( (void *)&buffer, 1, 1, fh);
  201.  
  202.            buffer = buffer ^ 0XFF;
  203.  
  204.            for( y=0 ; y < runlength ; y++, x++ )
  205.            {
  206.               *(start_point + x ) = buffer;
  207.            }
  208.        }
  209.      else
  210.        {
  211.            buffer = buffer ^ 0XFF;
  212.            *(start_point + x ) = buffer;
  213.            x++;
  214.        }
  215.  
  216.    }
  217.  
  218.    x=0;
  219.    fclose( fh);
  220.  
  221. }
  222. /* end of process file */
  223. ree( start_point );
  224.        MEM_ALLOCATED = 0;
  225.    }
  226.  else
  227.    {
  228.        first_line = start_point = malloc( sizeof(CHAR)* total_bytes );
  229.  
  230.        MEM_ALLOCATED = 1;
  231.    }
  232. */
  233.  
  234.    if( MEM_ALLOCATED )
  235.        DosFreeMem( first_line );
  236.  
  237.    apirc = DosAllocMem( (PPVOID)&start_point, sizeof(CHAR)*total_bytes, fALLOC );
  238.  
  239.    MEM_ALLOCATED = 1;
  240.  
  241.    first_line = start_point;
  242.  
  243.    x=0;
  244.  
  245.    spo=0;
  246.  
  247.    while( (frc = fread( (void *)&buffer, 1, 1, fh)) == 1 )
  248.    {
  249.        if(x >= total_bytes)
  250.            break;
  251.  
  252.        if( (buffer & 0XC0) == 0XC0 )
  253.        {
  254.            runlength = buffer & 0X3F;
  255.            fread( (void *)&buffer, 1, 1, fh);
  256.  
  257.            buffer = buffer ^ 0XFF;