home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <process.h>
- #include "cache.h"
-
- void main(int parnum,char *pars[],char *env[])
- {
- int horres,
- verres,
- hfilein,
- hfileout,
- decompcik;
- char buf,
- picbuf[768],
- decomps;
- long lfilein,
- deflength;
-
- printf("PCX2BMP v1.0 by Psycho/TSI (C) '92\n\n");
- if(parnum!=3)
- {
- printf("Too ");
- if(parnum<3) printf("few ");
- else printf("much ");
- printf("parameters!!\n");
- printf("USAGE: PCX2BMP <pcx file> <output file>\n\n");
- exit(EXIT_FAILURE);
- }
- if((hfilein=open(pars[1],O_RDONLY|O_BINARY))==-1||
- (hfileout=open(pars[2],O_WRONLY|O_BINARY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE))==-1)
- {
- printf("Error in file operations!!\n");
- printf("USAGE: PCX2BMP <pcx file> <output file>\n\n");
- exit(EXIT_FAILURE);
- }
- printf("Reading picture datas...\n");
- if(lseek(hfilein,3,SEEK_SET)!=3||
- read(hfilein,&buf,1)!=1||
- lseek(hfilein,8,SEEK_SET)!=8||
- read(hfilein,&horres,2)!=2||
- read(hfilein,&verres,2)!=2||
- lseek(hfilein,128,SEEK_SET)!=128)
- {
- printf("Read error!!\n\n");
- exit(EXIT_FAILURE);
- }
- if(buf!=8)
- {
- printf("This picture is not 256 color mapped!!\n\n");
- exit(EXIT_FAILURE);
- }
- horres++;
- verres++;
- lfilein=(long) horres*(long) verres;
- printf("Creating cache memory...\n");
- readablecache cachein(hfilein);
- writeablecache cacheout(hfileout);
-
- if(cachein.status==BADCACHE||cacheout.status==BADCACHE)
- {
- printf("Cache error!!\n\n");
- exit(EXIT_FAILURE);
- }
- printf("The size of picture %i*%i\n",horres,verres);
- printf("Decompressing picture...\n");
- deflength=lfilein;
- while(deflength)
- {
- buf=cachein.readitem();
- if(cachein.status==BADCACHE)
- {
- printf("Read error!!\n\n");
- exit(EXIT_FAILURE);
- }
- if((buf&128)&&(buf&64))
- {
- decomps=buf-192;
- buf=cachein.readitem();
- if(cachein.status==BADCACHE)
- {
- printf("Read error!!\n\n");
- exit(EXIT_FAILURE);
- }
- for(decompcik=0;decompcik<decomps;decompcik++)
- {
- cacheout.writeitem(buf);
- if(cacheout.status==BADCACHE)
- {
- printf("Write error!!\n\n");
- exit(EXIT_FAILURE);
- }
- deflength--;
- }
- }
- else {
- deflength--;
- cacheout.writeitem(buf);
- if(cacheout.status==BADCACHE)
- {
- printf("Write error!!\n\n");
- exit(EXIT_FAILURE);
- }
- }
- }
-
- cacheout.~writeablecache();
- cachein.~readablecache();
-
- printf("Reading color palette...\n");
- if(lseek(hfilein,-768,SEEK_END)==-1L||
- read(hfilein,picbuf,768)!=768)
- {
- printf("Read error!!\n\n");
- exit(EXIT_FAILURE);
- }
- printf("Converting colors to 6bit...\n");
- for(decompcik=0;decompcik<768;decompcik++)
- {
- picbuf[decompcik]/=4;
- }
- if(write(hfileout,picbuf,768)!=768)
- {
- printf("Write error!!\n\n");
- exit(EXIT_FAILURE);
- }
- printf("Converting has successful.\n\n");
- exit(EXIT_SUCCESS);
- }
-