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

  1.     #include <malloc.h>
  2.     #include <stdio.h>
  3.     #include <stdlib.h>
  4.     #include <process.h>
  5.     #include <io.h>
  6.     #include <fcntl.h>
  7.     #include <sys/stat.h>
  8.  
  9. void main(int parnum,char *pars[],char *env[])
  10.     {
  11.     int        bufcount,
  12.             npkx,
  13.             npky,
  14.             npvx,
  15.             npvy,
  16.             scypcik,
  17.             scxpcik,
  18.             horres,
  19.             verres,
  20.             hfilein,
  21.             hfileout,
  22.             decompcik;
  23.     char        buf,
  24.             decomps;
  25.     char        *picbuf;
  26.     long        lfilein,
  27.             deflength;
  28.  
  29.     printf("PICUT v1.0ß by Psycho/TSI (C) '92\n\n");
  30.         if(parnum!=3)
  31.             {
  32.         printf("Too ");
  33.         if(parnum<3) printf("few ");
  34.         else printf("much ");
  35.         printf("parameters!!\n");
  36.         printf("USAGE: PICUT <pcx file> <output file>\n\n");
  37.         exit(EXIT_FAILURE);
  38.         }
  39.     if((hfilein=open(pars[1],O_RDONLY|O_BINARY))==-1||
  40.         (hfileout=open(pars[2],O_WRONLY|O_CREAT|O_BINARY|O_TRUNC,S_IREAD|S_IWRITE))==-1)
  41.         {
  42.         printf("Error in file operations!!\n");
  43.         printf("USAGE: PICUT <pcx file> <output file>\n\n");
  44.         exit(EXIT_FAILURE);
  45.         }
  46.     printf("Reading picture datas...\n");
  47.     if(lseek(hfilein,3,SEEK_SET)!=3||
  48.         read(hfilein,&buf,1)!=1||
  49.         lseek(hfilein,8,SEEK_SET)!=8||
  50.         read(hfilein,&horres,2)!=2||
  51.         read(hfilein,&verres,2)!=2||
  52.         lseek(hfilein,128,SEEK_SET)!=128)
  53.         {
  54.         printf("Read error!!\n\n");
  55.         exit(EXIT_FAILURE);
  56.         }
  57.     if(buf!=8)
  58.         {
  59.         printf("This picture is not 256 color mapped!!\n\n");
  60.         exit(EXIT_FAILURE);
  61.         }
  62.     horres++;
  63.     verres++;
  64.     if(horres!=320||verres!=200)
  65.         {
  66.         printf("The picture size is not 320x200!!\n\n");
  67.         exit(EXIT_FAILURE);
  68.         }
  69.     lfilein=64000L;
  70.     printf("Allocating memory...\n");
  71.     if((picbuf=new char[lfilein])==NULL)
  72.         {
  73.         printf("Not enough memory!!\n\n");
  74.         exit(EXIT_FAILURE);
  75.         }
  76.     printf("Decompressing picture...\n");
  77.     deflength=lfilein;
  78.     bufcount=0;
  79.     while(deflength)
  80.         {
  81.         if(read(hfilein,&buf,1)!=1)
  82.             {
  83.             printf("Read error!!\n\n");
  84.             exit(EXIT_FAILURE);
  85.             }
  86.             if((buf&128)&&(buf&64))
  87.                     {
  88.             decomps=buf-192;
  89.             read(hfilein,&buf,1);
  90.             for(decompcik=0;decompcik<decomps;decompcik++)
  91.                 {
  92.                 picbuf[bufcount]=buf;
  93.                 bufcount++;
  94.                 deflength--;
  95.                 }
  96.             }
  97.         else    {
  98.             deflength--;
  99.             picbuf[bufcount]=buf;
  100.             bufcount++;
  101.             }
  102.         }
  103.     npkx=319;
  104.     npky=199;
  105.     npvx=
  106.     npvy=0;
  107.     printf("Cutting picture...\n");
  108.     for(scypcik=0;scypcik<200;scypcik++)
  109.         {
  110.             for(scxpcik=0;scxpcik<320;scxpcik++)
  111.             {
  112.             if(picbuf[scypcik*320+scxpcik])
  113.                 {
  114.                 if(scxpcik<=npkx) npkx=scxpcik;
  115.                 if(scxpcik>=npvx) npvx=scxpcik;
  116.                 if(scypcik<=npky) npky=scypcik;
  117.                 if(scypcik>=npvy) npvy=scypcik;
  118.                 }
  119.             }
  120.         }
  121.     printf("New size:\nx:%i y:%i\n",(npvx-npkx)+1,(npvy-npky)+1);
  122.     printf("Writing new image...\n");
  123.     for(scypcik=npky;scypcik<=npvy;scypcik++)
  124.         {
  125.         if(write(hfileout,&picbuf[scypcik*320+npkx],(npvx-npkx)+1)==-1)
  126.             {
  127.             printf("Error in writing!!\n\n");
  128.             exit(EXIT_FAILURE);
  129.             }
  130.         }
  131.        printf("Cutting has successful!!\n\n");
  132.        exit(EXIT_SUCCESS);
  133.        }
  134.