home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / uray / code / display.c next >
C/C++ Source or Header  |  1994-06-20  |  9KB  |  281 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1988, David B. Wecker            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  * This file is part of DBW_uRAY                    *
  7.  *                                    *
  8.  * DBW_uRAY is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts        *
  10.  * responsibility to anyone for the consequences of using it or for    *
  11.  * whether it serves any particular purpose or works at all, unless    *
  12.  * he says so in writing. Refer to the DBW_uRAY General Public        *
  13.  * License for full details.                        *
  14.  *                                    *
  15.  * Everyone is granted permission to copy, modify and redistribute    *
  16.  * DBW_uRAY, but only under the conditions described in the        *
  17.  * DBW_uRAY General Public License. A copy of this license is        *
  18.  * supposed to have been given to you along with DBW_uRAY so you    *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named LICENSE. Among other things, the copyright notice and this    *
  21.  * notice must be preserved on all copies.                *
  22.  ************************************************************************
  23.  *                                    *
  24.  * Authors:                                *
  25.  *    DBW - David B. Wecker                        *
  26.  *                                    *
  27.  * Versions:                                *
  28.  *    V1.0 881023 DBW    - First released version            *
  29.  *    V1.1 881110 DBW - Fixed scan coherence code            *
  30.  *    V1.2 881125 DBW - Removed ALL scan coherence code (useless)    *
  31.  *              added "fat" extent boxes            *
  32.  *    V1.3 881203 DBW - Fixed single precision TOLerances        *
  33.  *                                    *
  34.  ************************************************************************/
  35.  
  36. #define VERSION "uRAY v1.0 881029 (C) 1988 D. Wecker - all rights reserved\n"
  37.  
  38. /************************************************************************/
  39. /************* program to display IFF/ILBM files on an AMIGA ************/
  40. /************************************************************************/
  41.  
  42. #include <stdio.h>
  43. #include <intuition/intuitionbase.h>
  44. #include <functions.h>
  45.  
  46. #define BPP        4            /* Bits per pixel */
  47. #define DEPTH        6            /* HAM screen depth */
  48.  
  49. #define MAXGRAY        (1 << BPP)
  50. #define    ABS(x)        ((x) < 0 ? -(x) : (x))
  51. #define MIN(a,b)    ((a) < (b) ? (a) : (b))
  52. #define MAX(a,b)    ((a) > (b) ? (a) : (b))
  53.  
  54. /* chunk sizes */
  55. long    FORMsize,BODYsize,CAMGsize,CMAPsize,BMHDsize;
  56.  
  57. FILE            *fp;
  58. short            bytcnt,val;
  59. unsigned char        chr,buf[128],*planes[DEPTH];
  60. char            cod,str[10],colors[256][3];
  61. unsigned long        class;
  62. unsigned short        code;
  63. unsigned char        docompress,depth;
  64. short            ham,lace,hires;
  65. short            width,height,top,left,maxbyte;
  66. long            modes;
  67.  
  68. /*********************** 68000 I/O routines ******************************/
  69.  
  70. void wfil(fil,cnt,val)        /* write a number to a file (68000 order) */
  71. FILE    *fil;
  72. short    cnt;
  73. char    val[];
  74.     {
  75. #ifdef MCH_AMIGA
  76.     fwrite(val,cnt,1,fil);
  77. #else
  78.     while (cnt--) fwrite(&val[cnt],1,1,fil);
  79. #endif
  80.     }
  81.  
  82. short rfil(fil,cnt,val)        /* read a number from a file (68000 order) */
  83. FILE    *fil;
  84. short    cnt;
  85. char    val[];
  86.     {
  87.  
  88. #ifdef MCH_AMIGA
  89.     if (fread(val,cnt,1,fil) != 1)             return(-1);
  90. #else
  91.     while (cnt--) if (fread(&val[cnt],1,1,fil) != 1) return(-1);
  92. #endif
  93.  
  94.     return(0);
  95.     }
  96.  
  97. /************************* structure defs *********************************/
  98.  
  99. struct    GfxBase        *GfxBase;
  100. struct    IntuitionBase    *IntuitionBase;
  101. struct    RastPort    *rp;
  102. struct    ViewPort    *vp;
  103. struct    Screen        *screen;
  104. struct    NewScreen    ns = {
  105.     0L,0L,352L,464L,6L,
  106.     0,1,HAM|LACE,
  107.     CUSTOMSCREEN,NULL,
  108.     (UBYTE *)"uRAY Display",
  109.     NULL,NULL };
  110.  
  111.  
  112. /* leave program and return all resources */
  113. error(lev,msg)
  114. short   lev;
  115. char *msg;
  116.     {
  117.     switch (lev) {
  118.  
  119.     case 5:
  120.     CloseScreen(screen);
  121.     RemakeDisplay();
  122.  
  123.     case 4:
  124.     CloseLibrary(IntuitionBase);
  125.  
  126.     case 3:
  127.     CloseLibrary(GfxBase);
  128.  
  129.     case 2:
  130.  
  131.     case 1:
  132.     if (fp != NULL) fclose(fp);
  133.  
  134.     case 0:
  135.     if (msg) {
  136.         fprintf(stderr,"display: %s\n",msg);
  137.         exit(2);
  138.         }
  139.     }
  140.     exit(0);
  141.     }
  142.  
  143. main(argc,argv)
  144. int    argc;
  145. char    **argv;
  146.     {
  147.     char    f1[40],f2[40],fname[80];
  148.     short   i,j,k,modulo;
  149.  
  150.     printf(VERSION);
  151.     printf("\n\t(Type <return> to end display)\n\n");
  152.  
  153.  
  154.     if (argc < 2)   strcpy(fname,"uray");
  155.     else        strcpy(fname,argv[1]);
  156.     strcat(fname,".ilbm");
  157.     fp  = fopen(fname, "r");
  158.     if (fp == NULL) error(1,"Error opening input file");
  159.  
  160.     /* here is where we read the ILBM file in */
  161.     if (fread(str,1,4,fp) != 4 || strncmp(str,"FORM",4) != 0) error(2,"no FORM");
  162.     if (rfil(fp,4,&FORMsize)) error(2,"no FORM length");
  163.     if (fread(str,1,4,fp) != 4 || strncmp(str,"ILBM",4) != 0) error(2,"no ILBM");
  164.  
  165.     if (fread(str,1,4,fp) != 4 || strncmp(str,"BMHD",4) != 0) error(2,"no BMHD");
  166.     if (rfil(fp,4,&BMHDsize) || BMHDsize != 20L) error(2,"bad BMHD length");
  167.     if (rfil(fp,2,&width))            error(2,"bad BMHD");
  168.     if (rfil(fp,2,&height))            error(2,"bad BMHD");
  169.     if (rfil(fp,2,&top))            error(2,"bad BMHD");
  170.     if (rfil(fp,2,&left))            error(2,"bad BMHD");
  171.     if (rfil(fp,1,&depth))            error(2,"bad BMHD");
  172.     if (rfil(fp,1,buf))                error(2,"bad BMHD");
  173.     if (rfil(fp,1,&docompress))            error(2,"bad BMHD");
  174.     if (rfil(fp,9,buf))                error(2,"bad BMHD");
  175.     maxbyte = width >> 3;
  176.     if (docompress != 0 && docompress != 1)
  177.     error(2,"Unknown compression technique");
  178.     if (depth < 1 || depth > 6) error(2,"Bad depth!!");
  179.  
  180.     if (fread(str,1,4,fp) != 4 || strncmp(str,"CAMG",4) != 0) error(2,"no CAMG");
  181.     if (rfil(fp,4,&CAMGsize) || CAMGsize != 4L)    error(2,"bad CAMG length");
  182.     if (rfil(fp,4,&modes)) error(2,"can't read in CAMG");
  183.     if (modes & HIRES)        hires   = 1;
  184.     else            hires   = 0;
  185.     if (modes & HAM)        ham        = 1;
  186.     else            ham        = 0;
  187.     if (modes & LACE)        lace    = 1;
  188.     else            lace    = 0;
  189.     ns.ViewModes = modes;
  190.     ns.Depth     = (long)depth;
  191.     ns.TopEdge     = (long)top;
  192.     if (height > 464) ns.Height     = (long)height;
  193.     if (width  > 352) ns.Width     = (long)width;
  194.     if (width  < 352) modulo = (352 >> 3) - maxbyte;
  195.     else          modulo = 0;
  196.  
  197.     /* set up to display the image */
  198.     GfxBase    = (struct GfxBase *)OpenLibrary("graphics.library",0L);
  199.     if (GfxBase == NULL) error(2,"No graphics library");
  200.     IntuitionBase = 
  201.     (struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  202.     if (IntuitionBase == NULL) error(3,"No intuition library");
  203.     screen    = (struct Screen *)OpenScreen(&ns);
  204.     if (screen == NULL) error(4,"Can't open screen");
  205.  
  206.     vp        = &screen->ViewPort;
  207.     rp        = &screen->RastPort;
  208.  
  209.     if (lace && height > 400)        vp->DyOffset += (400 - height) >> 1;
  210.     if (lace == 0 && height > 200)  vp->DyOffset += (200 - height) >> 1;
  211.     if (hires && width > 640)        vp->DxOffset += (640 - width)  >> 1;
  212.     if (hires == 0 && width > 320)  vp->DxOffset += (320 - width)  >> 1;
  213.  
  214.     RemakeDisplay();
  215.  
  216.     if (fread(str,1,4,fp) != 4 || strncmp(str,"CMAP",4) != 0) error(5,"no CMAP");
  217.     if (rfil(fp,4,&CMAPsize)) error(5,"bad CMAP length");
  218.     CMAPsize /= 3;
  219.     for (i = 0; i < CMAPsize; i++) {
  220.     if (fread(str,1,3,fp) != 3) error(5,"can't read in color table");
  221.     colors[i][0] = (str[0] >> 4) & 0xF;
  222.     colors[i][1] = (str[1] >> 4) & 0xF;
  223.     colors[i][2] = (str[2] >> 4) & 0xF;
  224.     if (i < 16 || ham == 0)
  225.         SetRGB4(vp, (long)i,
  226.             (long)colors[i][0],
  227.             (long)colors[i][1],
  228.             (long)colors[i][2]);
  229.     }
  230.  
  231.     if (fread(str,1,4,fp) != 4 || strncmp(str,"BODY",4) != 0) error(5,"no BODY");
  232.     if (rfil(fp,4,&BODYsize)) error(5,"bad BODY length");
  233.  
  234.  
  235.     for (i = 0; i < depth; i++) {
  236.     planes[i] = (unsigned char *)rp->BitMap->Planes[i];
  237.     }
  238.  
  239.     for (i = 0; i < height; i++) {
  240.     for (j = 0; j < depth; j++) {
  241.  
  242.         if (docompress) {
  243.         bytcnt  = 0;
  244.  
  245.         while (bytcnt < maxbyte) {
  246.             if (fread(&cod,1,1,fp) != 1) goto DONE;
  247.             val = (short)cod;
  248.  
  249.             if (val >= 0) {
  250.             val++;
  251.             if (fread(planes[j],1,val,fp) != val) goto DONE;
  252.             }
  253.             else if (val > -128) {
  254.             val = (-val) + 1;
  255.             if (fread(&chr,1,1,fp) != 1) goto DONE;
  256.             for (k = 0; k < val; k++) planes[j][k] = chr;
  257.             }
  258.             else val = 0;
  259.  
  260.             bytcnt        += val;
  261.             planes[j]        += val;
  262.             if (bytcnt > maxbyte) error(5,"Bad run/dump count");
  263.             }
  264.         }
  265.         else {
  266.         if (fread(planes[j],1,maxbyte,fp) != maxbyte) goto DONE;
  267.         planes[j] += maxbyte;
  268.         }
  269.         planes[j] += modulo;    /* take care of undersized image */
  270.         }
  271.     }
  272.     DONE:
  273.     fclose(fp);
  274.     RemakeDisplay();
  275.     
  276.  
  277.     /* wait here until we get a close window message */
  278.     getchar();
  279.     error(5,NULL);
  280.     }
  281.