home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / bomb.tar.gz / bomb.tar / bomb / xli-image.h < prev    next >
C/C++ Source or Header  |  1997-04-25  |  8KB  |  244 lines

  1. /* image.h: (renamed xli-image.h by spot)
  2.  *
  3.  * portable image type declarations
  4.  *
  5.  * jim frost 10.02.89
  6.  *
  7.  * Copyright 1989 Jim Frost.  See included file README for complete
  8.  * copyright information.
  9.  */
  10.  
  11. #include <stdio.h>
  12.  
  13. /* ANSI-C stuff
  14.  */
  15. #if defined(__STDC__)
  16.  
  17. #if !defined(_ArgProto)
  18. #define _ArgProto(ARGS) ARGS
  19. #endif
  20.  
  21. #include <stdlib.h>
  22.  
  23. #else /* !__STDC__ */
  24.  
  25. #if !defined(const) /* "const" is an ANSI thing */
  26. #define const
  27. #endif
  28. #if !defined(_ArgProto)
  29. #define _ArgProto(ARGS) ()
  30. #endif
  31.  
  32. #endif /* !__STDC__ */
  33.  
  34. /* handle strings stuff that varies between BSD and ANSI/SYSV
  35.  */
  36. #if defined(IS_BSD) && !defined(__STDC__)
  37. #include <strings.h>
  38. #if !defined(strchr) && !defined(index)
  39. #define strchr index
  40. #endif
  41. #if !defined(strrchr) && !defined(rindex)
  42. #define strrchr rindex
  43. #endif
  44. #if !defined(memcpy) && !defined(bcopy)
  45. #define memcpy(D,S,L) bcopy((char *)(S),(char *)(D),(L))
  46. #endif
  47. #if !defined(memset) && !defined(bzero)
  48. /* #define memset(D,V,L) bzero(D,L) */
  49. #endif
  50. #else /* !IS_BSD || __STDC__ */
  51. #include <string.h>
  52. #if !defined(index) && !defined(strchr)
  53. #define index strchr
  54. #endif
  55. #if !defined(rindex) && !defined(strrchr)
  56. #define rindex strrchr
  57. #endif
  58. #if !defined(bcopy) && !defined(memcpy)
  59. #define bcopy(S,D,L) memcpy((void *)(D),(void *)(S),(L))
  60. #endif
  61. #if !defined(bzero) && !defined(memset)
  62. #define bzero(D,L) memset((void *)(D),0,(L))
  63. #endif
  64. #endif /* !IS_BSD || __STDC__ */
  65.  
  66. #ifdef VMS
  67. #define R_OK 4
  68. #define NO_UNCOMPRESS
  69. #endif
  70.  
  71.  
  72. typedef unsigned short Intensity; /* what X thinks an RGB intensity is */
  73. typedef unsigned char  byte;      /* byte type */
  74.  
  75. /* filter/extension pair for user-defined filters
  76.  */
  77. struct filter {
  78.   char          *extension; /* extension to match */
  79.   char          *filter;    /* filter to invoke */
  80.   struct filter *next;
  81. };
  82.  
  83. struct cache {
  84.   int           len;
  85.   char          buf[BUFSIZ];
  86.   struct cache *next;
  87. };
  88.  
  89. typedef struct {
  90.   unsigned int  type;     /* ZIO file type */
  91.   unsigned int  nocache;  /* true if caching has been disabled */
  92.   FILE         *stream;   /* file input stream */
  93.   char         *filename; /* filename */
  94.   struct cache *data;     /* data cache */
  95.   struct cache *dataptr;  /* ptr to current cache block */
  96.   int           bufptr;   /* ptr within current cache block */
  97. } ZFILE;
  98.  
  99. #define ZSTANDARD 0 /* standard file */
  100. #define ZPIPE     1 /* file is a pipe (ie uncompress) */
  101. #define ZSTDIN    2 /* file is stdin */
  102.  
  103. typedef struct rgbmap {
  104.   unsigned int  size;       /* size of RGB map */
  105.   unsigned int  used;       /* number of colors used in RGB map */
  106.   unsigned int  compressed; /* image uses colormap fully */
  107.   Intensity    *red;        /* color values in X style */
  108.   Intensity    *green;
  109.   Intensity    *blue;
  110. } RGBMap;
  111.  
  112. /* image structure
  113.  */
  114.  
  115. typedef struct {
  116.   char         *title;  /* name of image */
  117.   unsigned int  type;   /* type of image */
  118.   RGBMap        rgb;    /* RGB map of image if IRGB type */
  119.   unsigned int  width;  /* width of image in pixels */
  120.   unsigned int  height; /* height of image in pixels */
  121.   unsigned int  depth;  /* depth of image in bits if IRGB type */
  122.   unsigned int  pixlen; /* length of pixel if IRGB type */
  123.   float        gamma;    /* gamma of display the image is adjusted for */
  124.   byte         *data;   /* data rounded to full byte for each row */
  125. } xli_Image;
  126.  
  127. #define IBAD    0 /* invalid image (used when freeing) */
  128. #define IBITMAP 1 /* image is a bitmap */
  129. #define IRGB    2 /* image is RGB */
  130. #define ITRUE   3 /* image is true color */
  131.  
  132. #define BITMAPP(IMAGE) ((IMAGE)->type == IBITMAP)
  133. #define RGBP(IMAGE)    ((IMAGE)->type == IRGB)
  134. #define TRUEP(IMAGE)   ((IMAGE)->type == ITRUE)
  135.  
  136. #define TRUE_RED(PIXVAL)   (((PIXVAL) & 0xff0000) >> 16)
  137. #define TRUE_GREEN(PIXVAL) (((PIXVAL) & 0xff00) >> 8)
  138. #define TRUE_BLUE(PIXVAL)  ((PIXVAL) & 0xff)
  139. #define RGB_TO_TRUE(R,G,B) \
  140.   (((unsigned int)((R) & 0xff00) << 8) | ((unsigned int)(G) & 0xff00) | \
  141.    ((unsigned int)(B) >> 8))
  142.  
  143. #ifdef NO_INLINE
  144. /* only inline 1-byte transfers.  this is provided for systems whose C
  145.  * compilers can't hash the complexity of the inlined functions.
  146.  */
  147.  
  148. #define memToVal(PTR,LEN)    ((LEN) == 1 ? (unsigned long)(*(PTR)) : \
  149.                   doMemToVal(PTR,LEN))
  150. #define memToValLSB(PTR,LEN) ((LEN) == 1 ? (unsigned long)(*(PTR)) : \
  151.                   doMemToValLSB(PTR,LEN))
  152. #define valToMem(VAL,PTR,LEN)    ((LEN) == 1 ? \
  153.                   (unsigned long)(*(PTR) = (byte)(VAL)) : \
  154.                   doValToMem(VAL,PTR,LEN))
  155. #define valToMemLSB(VAL,PTR,LEN) ((LEN) == 1 ? \
  156.                   (unsigned long)(*(PTR) = (byte)(VAL)) : \
  157.                   (int)doValToMemLSB(VAL,PTR,LEN))
  158.  
  159. #else /* !NO_INLINE */
  160. /* inline these functions for speed.  these only work for {len : 1,2,3,4}.
  161.  */
  162.  
  163. #define memToVal(PTR,LEN) \
  164.   ((LEN) == 1 ? ((unsigned long)(*((byte *)PTR))) : \
  165.    ((LEN) == 3 ? ((unsigned long) \
  166.           (*(byte *)(PTR) << 16) | \
  167.           (*((byte *)(PTR) + 1) << 8) | \
  168.           (*((byte *)(PTR) + 2))) : \
  169.     ((LEN) == 2 ? ((unsigned long) \
  170.            (*(byte *)(PTR) << 8) | \
  171.            (*((byte *)(PTR) + 1))) : \
  172.      ((unsigned long)((*(byte *)(PTR) << 24) | \
  173.               (*((byte *)(PTR) + 1) << 16) | \
  174.               (*((byte *)(PTR) + 2) << 8) | \
  175.               (*((byte *)(PTR) + 3)))))))
  176.  
  177. #define memToValLSB(PTR,LEN) \
  178.   ((LEN) == 1 ? ((unsigned long)(*(byte *)(PTR))) : \
  179.    ((LEN) == 3 ? ((unsigned long) \
  180.           (*(byte *)(PTR)) | \
  181.           (*((byte *)(PTR) + 1) << 8) | \
  182.           (*((byte *)(PTR) + 2) << 16)) : \
  183.     ((LEN) == 2 ? ((unsigned long) \
  184.            (*(byte *)(PTR)) | (*((byte *)(PTR) + 1) << 8)) : \
  185.      ((unsigned long)((*(byte *)(PTR)) | \
  186.               (*((byte *)(PTR) + 1) << 8) | \
  187.               (*((byte *)(PTR) + 2) << 16) | \
  188.               (*((byte *)(PTR) + 3) << 24))))))
  189.  
  190. #define valToMem(VAL,PTR,LEN) \
  191.   ((LEN) == 1 ? (*(byte *)(PTR) = ((unsigned int)(VAL) & 0xff)) : \
  192.    ((LEN) == 3 ? (((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff0000) >> 16), \
  193.           ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  194.           ((*((byte *)(PTR) + 2)) = ((unsigned int)(VAL) & 0xff))) : \
  195.     ((LEN) == 2 ? (((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  196.            ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff))) : \
  197.      (((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff000000) >> 24), \
  198.       ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff0000) >> 16), \
  199.       ((*((byte *)(PTR) + 2)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  200.       ((*((byte *)(PTR) + 3)) = ((unsigned int)(VAL) & 0xff))))))
  201.  
  202. #define valToMemLSB(VAL,PTR,LEN) \
  203.   ((LEN) == 1 ? (*(byte *)(PTR) = ((unsigned int)(VAL) & 0xff)) : \
  204.    ((LEN) == 3 ? (((*(byte *)(PTR) + 2) = ((unsigned int)(VAL) & 0xff0000) >> 16), \
  205.           ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  206.           ((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff))) : \
  207.     ((LEN) == 2 ? (((*((byte *)(PTR) + 1) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  208.             ((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff)))) : \
  209.      (((*((byte *)(PTR) + 3)) = ((unsigned int)(VAL) & 0xff000000) >> 24), \
  210.       ((*((byte *)(PTR) + 2)) = ((unsigned int)(VAL) & 0xff0000) >> 16), \
  211.       ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  212.       ((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff))))))
  213. #endif /* !NO_INLINE */
  214.  
  215. /* zio.c */
  216. ZFILE *zopen _ArgProto((char *name));
  217. int    zread _ArgProto((ZFILE *zf, byte *buf, unsigned int len));
  218. int    zgetc _ArgProto((ZFILE *zf));
  219. char  *zgets _ArgProto((byte *buf, unsigned int size, ZFILE *zf));
  220. void   zclose _ArgProto((ZFILE *zf));
  221. void   znocache _ArgProto((ZFILE *zf));
  222. void   zreset _ArgProto((char *filename));
  223.  
  224. #define lmalloc malloc
  225. #define lfree free
  226. #define dupString strdup
  227.  
  228. /* this returns the (approximate) intensity of an RGB triple
  229.  */
  230.  
  231. #define colorIntensity(R,G,B) \
  232.   (RedIntensity[(R) >> 8] + GreenIntensity[(G) >> 8] + BlueIntensity[(B) >> 8])
  233.  
  234. extern unsigned short RedIntensity[];
  235. extern unsigned short GreenIntensity[];
  236. extern unsigned short BlueIntensity[];
  237.  
  238. #ifdef DEBUG
  239. extern int _Xdebug;
  240. #define debug(ARGS) if (_Xdebug) printf ARGS
  241. #else /* !DEBUG */
  242. #define debug(ARGS)
  243. #endif /* !DEBUG */
  244.