home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / basq / source / poweru / pcx2bmp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-12  |  2.9 KB  |  129 lines

  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.     #include <process.h>
  4.     #include "cache.h"
  5.  
  6. void main(int parnum,char *pars[],char *env[])
  7.     {
  8.     int        horres,
  9.             verres,
  10.             hfilein,
  11.             hfileout,
  12.             decompcik;
  13.     char        buf,
  14.             picbuf[768],
  15.             decomps;
  16.     long        lfilein,
  17.             deflength;
  18.  
  19.     printf("PCX2BMP v1.0 by Psycho/TSI (C) '92\n\n");
  20.     if(parnum!=3)
  21.         {
  22.         printf("Too ");
  23.         if(parnum<3) printf("few ");
  24.         else printf("much ");
  25.         printf("parameters!!\n");
  26.         printf("USAGE: PCX2BMP <pcx file> <output file>\n\n");
  27.         exit(EXIT_FAILURE);
  28.         }
  29.     if((hfilein=open(pars[1],O_RDONLY|O_BINARY))==-1||
  30.         (hfileout=open(pars[2],O_WRONLY|O_BINARY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE))==-1)
  31.         {
  32.         printf("Error in file operations!!\n");
  33.         printf("USAGE: PCX2BMP <pcx file> <output file>\n\n");
  34.         exit(EXIT_FAILURE);
  35.         }
  36.     printf("Reading picture datas...\n");
  37.     if(lseek(hfilein,3,SEEK_SET)!=3||
  38.         read(hfilein,&buf,1)!=1||
  39.         lseek(hfilein,8,SEEK_SET)!=8||
  40.         read(hfilein,&horres,2)!=2||
  41.         read(hfilein,&verres,2)!=2||
  42.         lseek(hfilein,128,SEEK_SET)!=128)
  43.         {
  44.         printf("Read error!!\n\n");
  45.         exit(EXIT_FAILURE);
  46.         }
  47.     if(buf!=8)
  48.         {
  49.         printf("This picture is not 256 color mapped!!\n\n");
  50.         exit(EXIT_FAILURE);
  51.         }
  52.     horres++;
  53.     verres++;
  54.     lfilein=(long) horres*(long) verres;
  55.     printf("Creating cache memory...\n");
  56.     readablecache    cachein(hfilein);
  57.     writeablecache cacheout(hfileout);
  58.  
  59.     if(cachein.status==BADCACHE||cacheout.status==BADCACHE)
  60.         {
  61.         printf("Cache error!!\n\n");
  62.         exit(EXIT_FAILURE);
  63.         }
  64.     printf("The size of picture %i*%i\n",horres,verres);
  65.     printf("Decompressing picture...\n");
  66.     deflength=lfilein;
  67.     while(deflength)
  68.         {
  69.         buf=cachein.readitem();
  70.         if(cachein.status==BADCACHE)
  71.             {
  72.             printf("Read error!!\n\n");
  73.             exit(EXIT_FAILURE);
  74.             }
  75.         if((buf&128)&&(buf&64))
  76.             {
  77.             decomps=buf-192;
  78.             buf=cachein.readitem();
  79.             if(cachein.status==BADCACHE)
  80.                 {
  81.                 printf("Read error!!\n\n");
  82.                 exit(EXIT_FAILURE);
  83.                 }
  84.             for(decompcik=0;decompcik<decomps;decompcik++)
  85.                 {
  86.                 cacheout.writeitem(buf);
  87.                 if(cacheout.status==BADCACHE)
  88.                     {
  89.                     printf("Write error!!\n\n");
  90.                     exit(EXIT_FAILURE);
  91.                     }
  92.                 deflength--;
  93.                 }
  94.             }
  95.         else    {
  96.             deflength--;
  97.             cacheout.writeitem(buf);
  98.             if(cacheout.status==BADCACHE)
  99.                 {
  100.                 printf("Write error!!\n\n");
  101.                 exit(EXIT_FAILURE);
  102.                 }
  103.             }
  104.         }
  105.  
  106.     cacheout.~writeablecache();
  107.     cachein.~readablecache();
  108.  
  109.     printf("Reading color palette...\n");
  110.     if(lseek(hfilein,-768,SEEK_END)==-1L||
  111.         read(hfilein,picbuf,768)!=768)
  112.         {
  113.         printf("Read error!!\n\n");
  114.         exit(EXIT_FAILURE);
  115.         }
  116.     printf("Converting colors to 6bit...\n");
  117.     for(decompcik=0;decompcik<768;decompcik++)
  118.         {
  119.         picbuf[decompcik]/=4;
  120.         }
  121.     if(write(hfileout,picbuf,768)!=768)
  122.         {
  123.         printf("Write error!!\n\n");
  124.         exit(EXIT_FAILURE);
  125.         }
  126.     printf("Converting has successful.\n\n");
  127.     exit(EXIT_SUCCESS);
  128.     }
  129.