home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / image.h < prev    next >
C/C++ Source or Header  |  1993-10-28  |  11KB  |  334 lines

  1. /* image.h:
  2.  *
  3.  * portable image type declarations
  4.  *
  5.  * jim frost 10.02.89
  6.  *
  7.  * Copyright 1989 Jim Frost.  See included file "copyright.h" for complete
  8.  * copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12.  
  13. #include <stdio.h>
  14.  
  15. /* ANSI-C stuff
  16.  */
  17. #if defined(__STDC__)
  18.  
  19. #if !defined(_ArgProto)
  20. #define _ArgProto(ARGS) ARGS
  21. #endif
  22.  
  23. #include <stdlib.h>
  24.  
  25. #else /* !__STDC__ */
  26.  
  27. #if !defined(const) /* "const" is an ANSI thing */
  28. #define const
  29. #endif
  30. #if !defined(_ArgProto)
  31. #define _ArgProto(ARGS) ()
  32. #endif
  33.  
  34. #endif /* !__STDC__ */
  35.  
  36. /* handle strings stuff that varies between BSD and ANSI/SYSV
  37.  */
  38. #if defined(IS_BSD) && !defined(__STDC__)
  39. #include <strings.h>
  40. #if !defined(strchr) && !defined(index)
  41. #define strchr index
  42. #endif
  43. #if !defined(strrchr) && !defined(rindex)
  44. #define strrchr rindex
  45. #endif
  46. #if !defined(memcpy) && !defined(bcopy)
  47. #define memcpy(D,S,L) bcopy((char *)(S),(char *)(D),(L))
  48. #endif
  49. #if !defined(memset) && !defined(bzero)
  50. /* #define memset(D,V,L) bzero(D,L) */
  51. #endif
  52. #else /* !IS_BSD || __STDC__ */
  53. #include <string.h>
  54. #if !defined(index) && !defined(strchr)
  55. #define index strchr
  56. #endif
  57. #if !defined(rindex) && !defined(strrchr)
  58. #define rindex strrchr
  59. #endif
  60. #if !defined(bcopy) && !defined(memcpy)
  61. #define bcopy(S,D,L) memcpy((void *)(D),(void *)(S),(L))
  62. #endif
  63. #if !defined(bzero) && !defined(memset)
  64. #define bzero(D,L) memset((void *)(D),0,(L))
  65. #endif
  66. #endif /* !IS_BSD || __STDC__ */
  67.  
  68. #ifdef VMS
  69. #define R_OK 4
  70. #define NO_UNCOMPRESS
  71. #endif
  72.  
  73. typedef unsigned long  Pixel;     /* what X thinks a pixel is */
  74. typedef unsigned short Intensity; /* what X thinks an RGB intensity is */
  75. typedef unsigned char  byte;      /* byte type */
  76.  
  77. /* filter/extension pair for user-defined filters
  78.  */
  79. struct filter {
  80.   char          *extension; /* extension to match */
  81.   char          *filter;    /* filter to invoke */
  82.   struct filter *next;
  83. };
  84.  
  85. struct cache {
  86.   int           len;
  87.   char          buf[BUFSIZ];
  88.   struct cache *next;
  89. };
  90.  
  91. typedef struct {
  92.   unsigned int  type;     /* ZIO file type */
  93.   unsigned int  nocache;  /* true if caching has been disabled */
  94.   FILE         *stream;   /* file input stream */
  95.   char         *filename; /* filename */
  96.   struct cache *data;     /* data cache */
  97.   struct cache *dataptr;  /* ptr to current cache block */
  98.   int           bufptr;   /* ptr within current cache block */
  99. } ZFILE;
  100.  
  101. #define ZSTANDARD 0 /* standard file */
  102. #define ZPIPE     1 /* file is a pipe (ie uncompress) */
  103. #define ZSTDIN    2 /* file is stdin */
  104.  
  105. typedef struct rgbmap {
  106.   unsigned int  size;       /* size of RGB map */
  107.   unsigned int  used;       /* number of colors used in RGB map */
  108.   unsigned int  compressed; /* image uses colormap fully */
  109.   Intensity    *red;        /* color values in X style */
  110.   Intensity    *green;
  111.   Intensity    *blue;
  112. } RGBMap;
  113.  
  114. /* image structure
  115.  */
  116.  
  117. typedef struct {
  118.   char         *title;  /* name of image */
  119.   unsigned int  type;   /* type of image */
  120.   RGBMap        rgb;    /* RGB map of image if IRGB type */
  121.   unsigned int  width;  /* width of image in pixels */
  122.   unsigned int  height; /* height of image in pixels */
  123.   unsigned int  depth;  /* depth of image in bits if IRGB type */
  124.   unsigned int  pixlen; /* length of pixel if IRGB type */
  125.   float        gamma;    /* gamma of display the image is adjusted for */
  126.   byte         *data;   /* data rounded to full byte for each row */
  127. } Image;
  128.  
  129. #define IBAD    0 /* invalid image (used when freeing) */
  130. #define IBITMAP 1 /* image is a bitmap */
  131. #define IRGB    2 /* image is RGB */
  132. #define ITRUE   3 /* image is true color */
  133.  
  134. #define BITMAPP(IMAGE) ((IMAGE)->type == IBITMAP)
  135. #define RGBP(IMAGE)    ((IMAGE)->type == IRGB)
  136. #define TRUEP(IMAGE)   ((IMAGE)->type == ITRUE)
  137.  
  138. #define TRUE_RED(PIXVAL)   (((PIXVAL) & 0xff0000) >> 16)
  139. #define TRUE_GREEN(PIXVAL) (((PIXVAL) & 0xff00) >> 8)
  140. #define TRUE_BLUE(PIXVAL)  ((PIXVAL) & 0xff)
  141. #define RGB_TO_TRUE(R,G,B) \
  142.   (((unsigned int)((R) & 0xff00) << 8) | ((unsigned int)(G) & 0xff00) | \
  143.    ((unsigned int)(B) >> 8))
  144.  
  145. #ifdef NO_INLINE
  146. /* only inline 1-byte transfers.  this is provided for systems whose C
  147.  * compilers can't hash the complexity of the inlined functions.
  148.  */
  149.  
  150. #define memToVal(PTR,LEN)    ((LEN) == 1 ? (unsigned long)(*(PTR)) : \
  151.                   doMemToVal(PTR,LEN))
  152. #define memToValLSB(PTR,LEN) ((LEN) == 1 ? (unsigned long)(*(PTR)) : \
  153.                   doMemToValLSB(PTR,LEN))
  154. #define valToMem(VAL,PTR,LEN)    ((LEN) == 1 ? \
  155.                   (unsigned long)(*(PTR) = (byte)(VAL)) : \
  156.                   doValToMem(VAL,PTR,LEN))
  157. #define valToMemLSB(VAL,PTR,LEN) ((LEN) == 1 ? \
  158.                   (unsigned long)(*(PTR) = (byte)(VAL)) : \
  159.                   (int)doValToMemLSB(VAL,PTR,LEN))
  160.  
  161. #else /* !NO_INLINE */
  162. /* inline these functions for speed.  these only work for {len : 1,2,3,4}.
  163.  */
  164.  
  165. #define memToVal(PTR,LEN) \
  166.   ((LEN) == 1 ? ((unsigned long)(*((byte *)PTR))) : \
  167.    ((LEN) == 3 ? ((unsigned long) \
  168.           (*(byte *)(PTR) << 16) | \
  169.           (*((byte *)(PTR) + 1) << 8) | \
  170.           (*((byte *)(PTR) + 2))) : \
  171.     ((LEN) == 2 ? ((unsigned long) \
  172.            (*(byte *)(PTR) << 8) | \
  173.            (*((byte *)(PTR) + 1))) : \
  174.      ((unsigned long)((*(byte *)(PTR) << 24) | \
  175.               (*((byte *)(PTR) + 1) << 16) | \
  176.               (*((byte *)(PTR) + 2) << 8) | \
  177.               (*((byte *)(PTR) + 3)))))))
  178.  
  179. #define memToValLSB(PTR,LEN) \
  180.   ((LEN) == 1 ? ((unsigned long)(*(byte *)(PTR))) : \
  181.    ((LEN) == 3 ? ((unsigned long) \
  182.           (*(byte *)(PTR)) | \
  183.           (*((byte *)(PTR) + 1) << 8) | \
  184.           (*((byte *)(PTR) + 2) << 16)) : \
  185.     ((LEN) == 2 ? ((unsigned long) \
  186.            (*(byte *)(PTR)) | (*((byte *)(PTR) + 1) << 8)) : \
  187.      ((unsigned long)((*(byte *)(PTR)) | \
  188.               (*((byte *)(PTR) + 1) << 8) | \
  189.               (*((byte *)(PTR) + 2) << 16) | \
  190.               (*((byte *)(PTR) + 3) << 24))))))
  191.  
  192. #define valToMem(VAL,PTR,LEN) \
  193.   ((LEN) == 1 ? (*(byte *)(PTR) = ((unsigned int)(VAL) & 0xff)) : \
  194.    ((LEN) == 3 ? (((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff0000) >> 16), \
  195.           ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  196.           ((*((byte *)(PTR) + 2)) = ((unsigned int)(VAL) & 0xff))) : \
  197.     ((LEN) == 2 ? (((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  198.            ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff))) : \
  199.      (((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff000000) >> 24), \
  200.       ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff0000) >> 16), \
  201.       ((*((byte *)(PTR) + 2)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  202.       ((*((byte *)(PTR) + 3)) = ((unsigned int)(VAL) & 0xff))))))
  203.  
  204. #define valToMemLSB(VAL,PTR,LEN) \
  205.   ((LEN) == 1 ? (*(byte *)(PTR) = ((unsigned int)(VAL) & 0xff)) : \
  206.    ((LEN) == 3 ? (((*(byte *)(PTR) + 2) = ((unsigned int)(VAL) & 0xff0000) >> 16), \
  207.           ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  208.           ((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff))) : \
  209.     ((LEN) == 2 ? (((*((byte *)(PTR) + 1) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  210.             ((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff)))) : \
  211.      (((*((byte *)(PTR) + 3)) = ((unsigned int)(VAL) & 0xff000000) >> 24), \
  212.       ((*((byte *)(PTR) + 2)) = ((unsigned int)(VAL) & 0xff0000) >> 16), \
  213.       ((*((byte *)(PTR) + 1)) = ((unsigned int)(VAL) & 0xff00) >> 8), \
  214.       ((*(byte *)(PTR)) = ((unsigned int)(VAL) & 0xff))))))
  215. #endif /* !NO_INLINE */
  216.  
  217. /* SUPPRESS 558 */
  218.  
  219. /* function declarations
  220.  */
  221. /* clip.c */
  222. Image *clip _ArgProto((Image *image, unsigned int x, unsigned int y,
  223.         unsigned int width, unsigned int height,
  224.         unsigned int verbose));
  225.  
  226. /* bright.c */
  227. void brighten _ArgProto((Image *image, unsigned int percent, unsigned int verbose));
  228. void gammacorrect _ArgProto((Image *image, double disp_gam, unsigned int verbose));
  229. void gray _ArgProto((Image *image, unsigned int verbose));
  230. Image *normalize _ArgProto((Image *image, unsigned int verbose));
  231.  
  232. /* compress.c */
  233. void compress _ArgProto((Image *image, unsigned int verbose));
  234.  
  235. /* dither.c */
  236. Image *dither _ArgProto((Image *image, unsigned int verbose));
  237.  
  238. /* fill.c */
  239. void fill _ArgProto((Image *image, unsigned int fx, unsigned int fy,
  240.           unsigned int fw, unsigned int fh, Pixel pixval));
  241.  
  242. /* halftone.c */
  243. Image *halftone _ArgProto((Image *image, unsigned int verbose));
  244.  
  245. /* imagetypes.c */
  246. void   goodImage _ArgProto((Image *image, char *where));
  247.  
  248. /* merge.c */
  249. Image *merge _ArgProto((Image *dest, Image *src, int atx, int aty,
  250.          unsigned int verbose));
  251. Image *tile _ArgProto((Image *image, int x, int y,
  252.         unsigned int width, unsigned int height,
  253.         unsigned int verbose));
  254.  
  255. /* misc.c */
  256. void memoryExhausted _ArgProto((void));
  257. char *tail _ArgProto((char *));
  258. void usage _ArgProto((void));
  259. int errorHandler();
  260. char *findstr _ArgProto((char *, char *));
  261.  
  262. /* new.c */
  263. extern unsigned long DepthToColorsTable[];
  264. unsigned long colorsToDepth();
  265. char  *dupString _ArgProto((char *s));
  266. Image *newBitImage _ArgProto((unsigned int width, unsigned int height));
  267. Image *newRGBImage _ArgProto((unsigned int width, unsigned int height,
  268.                unsigned int depth));
  269. Image *newTrueImage _ArgProto((unsigned int width, unsigned int height));
  270. void   freeImage _ArgProto((Image *image));
  271. void   freeImageData _ArgProto((Image *image));
  272. void   newRGBMapData _ArgProto((RGBMap *rgb, unsigned int  size));
  273. void   freeRGBMapData _ArgProto((RGBMap *rgb));
  274. byte  *lcalloc _ArgProto((unsigned int size));
  275. byte  *lmalloc _ArgProto((unsigned int size));
  276. void   lfree _ArgProto((byte *area));
  277.  
  278. #define depthToColors(n) DepthToColorsTable[((n) < 32 ? (n) : 32)]
  279.  
  280. /* reduce.c */
  281. Image *reduce _ArgProto((Image *image, unsigned int n, unsigned int verbose));
  282. Image *expand _ArgProto((Image *image));
  283. Image *flatten _ArgProto((Image *image));
  284.  
  285. /* rotate.c */
  286. Image *rotate _ArgProto((Image *image, unsigned int degrees, unsigned int verbose));
  287.  
  288. /* smooth.c */
  289. Image *smooth _ArgProto((Image *image, int iterations, unsigned int verbose));
  290.  
  291. /* undither.c */
  292. Image *undither _ArgProto((Image *oimage, unsigned int  verbose));
  293.  
  294. /* doMemToVal and doMemToValLSB used to be void type but some compilers
  295.  * (particularly the 4.1.1 SunOS compiler) couldn't handle the
  296.  * (void)(thing= value) conversion used in the macros.
  297.  */
  298. /* value.c */
  299. unsigned long doMemToVal _ArgProto((byte *p, unsigned int len));
  300. unsigned long doValToMem _ArgProto((unsigned long val, byte *p, unsigned int len));
  301. unsigned long doMemToValLSB _ArgProto((byte *p, unsigned int len));
  302. unsigned long doValToMemLSB _ArgProto((unsigned long val, byte *p, unsigned int len));
  303. void          flipBits _ArgProto((byte *p, unsigned int len));
  304.  
  305. /* zio.c */
  306. ZFILE *zopen _ArgProto((char *name));
  307. int    zread _ArgProto((ZFILE *zf, byte *buf, unsigned int len));
  308. int    zgetc _ArgProto((ZFILE *zf));
  309. char  *zgets _ArgProto((byte *buf, unsigned int size, ZFILE *zf));
  310. void   zclose _ArgProto((ZFILE *zf));
  311. void   znocache _ArgProto((ZFILE *zf));
  312. void   zreset _ArgProto((char *filename));
  313.  
  314. /* zoom.c */
  315. Image *zoom _ArgProto((Image *image, unsigned int x, unsigned int y,
  316.                unsigned int verbose));
  317.  
  318. /* this returns the (approximate) intensity of an RGB triple
  319.  */
  320.  
  321. #define colorIntensity(R,G,B) \
  322.   (RedIntensity[(R) >> 8] + GreenIntensity[(G) >> 8] + BlueIntensity[(B) >> 8])
  323.  
  324. extern unsigned short RedIntensity[];
  325. extern unsigned short GreenIntensity[];
  326. extern unsigned short BlueIntensity[];
  327.  
  328. #ifdef DEBUG
  329. extern int _Xdebug;
  330. #define debug(ARGS) if (_Xdebug) printf ARGS
  331. #else /* !DEBUG */
  332. #define debug(ARGS)
  333. #endif /* !DEBUG */
  334.