home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1997 #3 / amigamamagazinepolishissue03-1 / polski_aminet / michal_brzozowski / showpcx / showpcx.c < prev    next >
C/C++ Source or Header  |  1996-01-14  |  3KB  |  140 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <proto/exec.h>
  5. #include <proto/intuition.h>
  6. #include "display.h"
  7.  
  8. char *name;
  9.  
  10. char *bufor;
  11.  
  12. static int   GetByte(FILE *fp);
  13. static int   GetWord(FILE *fp);
  14. static void  readImage(FILE *fp, unsigned char *buf, int BytesPerLine, int Height);
  15. /*static void  showImage(unsigned char *buf, int Width, int Height);*/
  16.  
  17. static int GetByte(fp)
  18. FILE *fp;
  19. {
  20.    int c;
  21.  
  22.    if((c=getc(fp))==EOF)
  23.       assert(c!=EOF);
  24.    return c;
  25. }
  26.  
  27. static int GetWord(fp)
  28. FILE *fp;
  29. {
  30.    int c;
  31.  
  32.    c=GetByte(fp);
  33.    c|=(GetByte(fp)<<8);
  34.    return c;
  35. }
  36.  
  37. static void  readImage(fp, buf, BytesPerLine, Height)
  38. FILE *fp;
  39. unsigned char *buf;
  40. int BytesPerLine;
  41. int Height;
  42. {
  43.    int           c;
  44.    int           nbytes;
  45.    int           count;
  46.  
  47.    nbytes=BytesPerLine*Height;
  48.  
  49.    while(nbytes>0)
  50.    {
  51.       c=GetByte(fp);
  52.       if((c&0xc0)!=0xc0)
  53.       {
  54.          *buf++=c;
  55.          --nbytes;
  56.          continue;
  57.       }
  58.       count=c&0x3f;
  59.       c=GetByte(fp);
  60.       assert(count<=nbytes);
  61.  
  62.       nbytes-=count;
  63.       while(--count>=0)
  64.          *buf++=c;
  65.    }
  66. }
  67.  
  68. void main(int argc, char *argv[])
  69. {
  70.    register int  i;
  71.    FILE          *ifp;
  72.    int           Version;
  73.    int           Xmin, Xmax, Ymin, Ymax;
  74.    int           Width, Height;
  75.    int           Planes, BitsPerPixel, BytesPerLine, tmp;
  76.    unsigned char red[256], green[256], blue[256], gray[256];
  77.    unsigned char *data;
  78.  
  79.    name=argv[1];
  80.  
  81.    bufor=(unsigned char *)malloc(16384);
  82.  
  83.    if(ifp=fopen(name, "rb"))
  84.    {
  85.       if(bufor) setvbuf(ifp, bufor, _IOFBF, 16384);
  86.       if(GetByte(ifp)!=0x0a)
  87.          assert(0);
  88.       Version=GetByte(ifp);
  89.       tmp=GetByte(ifp);
  90.       assert(tmp==1);
  91.       BitsPerPixel=GetByte(ifp);
  92.       assert(BitsPerPixel==8);
  93.  
  94.       Xmin=GetWord(ifp);
  95.       Ymin=GetWord(ifp);
  96.       Xmax=GetWord(ifp);
  97.       Ymax=GetWord(ifp);
  98.  
  99.       Width=(Xmax-Xmin)+1;
  100.       Height=(Ymax-Ymin)+1;
  101.  
  102.       (void) GetWord(ifp);
  103.       (void) GetWord(ifp);
  104.  
  105.       for(i=0;i<16;i++)
  106.       {
  107.          red[i]=GetByte(ifp);
  108.          green[i]=GetByte(ifp);
  109.          blue[i]=GetByte(ifp);
  110.       }
  111.  
  112.       (void) GetByte(ifp);
  113.       Planes=GetByte(ifp);
  114.       BytesPerLine=GetWord(ifp);
  115.       (void) GetWord(ifp);
  116.  
  117.       fseek(ifp, (long)128, 0);
  118.  
  119.       data=(unsigned char *)malloc(BytesPerLine*Height);
  120.       
  121.       if(data)
  122.       {
  123.          readImage(ifp, data, BytesPerLine, Height);
  124.    
  125.          (void) GetByte(ifp);
  126.          for(i=0;i<256;i++)
  127.          {
  128.             red[i]=GetByte(ifp);
  129.             green[i]=GetByte(ifp);
  130.             blue[i]=GetByte(ifp);
  131.             gray[i]=(299*red[i]+587*green[i]+114*blue[i])/16000;
  132.          }
  133.       }
  134.       fclose(ifp);
  135.       if(data) showImage(data, gray, BytesPerLine-1, Height);
  136.       if(data) free(data);
  137.       if(bufor) free(bufor);
  138.    }
  139. }
  140.