1 /*
2  * This software is copyrighted as noted below.  It may be freely copied,
3  * modified, and redistributed, provided that the copyright notices are
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is".  Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 
19 /*
20  * getx11.h - Declaration for image_information structure...  (tote a global)
21  *
22  * Author:	Martin R. Friedmann
23  * 		Dept of Electrical Engineering and Computer Science
24  *		University of Michigan
25  * Date:	Tue, Dec 10, 1989
26  * Copyright (c) 1989, University of Michigan
27  */
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <ctype.h>
35 #include <X11/X.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38 #include <X11/cursorfont.h>
39 #include "rle.h"
40 #ifdef X_SHARED_MEMORY
41 #include <sys/ipc.h>
42 #include <sys/shm.h>
43 #include <X11/extensions/XShm.h>
44 #ifndef USE_PROTOTYPES
45 extern void_star shmat();
46 #endif
47 #endif
48 
49 #define COUNT_OF(_array_)	( sizeof (_array_) / sizeof (_array_[0]) )
50 #define IMAGE_BORDERWIDTH	3
51 
52 typedef int Boolean;
53 typedef unsigned long Pixel;
54 
55 #define MALLOC_FAILURE 3
56 #define FILE_FAILURE 2
57 #define FATAL_FAILURE 1
58 #define SUCCESS 0
59 
60 #define VPRINTF if (verbose_flag) fprintf
61 #define DPRINTF if (debug_flag) fprintf
62 
63 #define SHIFT_MASK_PIXEL(r, g, b) \
64     ( (( ( (r) << cmap_i.red_shift  ) & cmap_i.red_mask )\
65        | ( ( (g) << cmap_i.green_shift) & cmap_i.green_mask )\
66        | ( ( (b) << cmap_i.blue_shift ) & cmap_i.blue_mask )) +\
67       cmap_i.pixel_base)
68 
69 #define SHIFT_MASK_PIXEL_32(r, g, b) \
70     (( (r) << cmap_i.red_shift  ) \
71      | ( (g) << cmap_i.green_shift) \
72      | ( (b) << cmap_i.blue_shift ))
73 
74 #define Min(x,y) (((x) < (y)) ? (x) : (y))
75 #define Max(x,y) (((x) > (y)) ? (x) : (y))
76 
77 typedef void VOID_FUNCTION();
78 typedef int array16[16];
79 
80 extern double		display_gamma;
81 extern int 		iflag;
82 
83 extern char 		*progname;
84 extern Display 		*dpy;
85 extern Window		root_window;
86 extern int		screen;
87 
88 extern Boolean		debug_flag;	/* set if debug mode -D */
89 extern Boolean		verbose_flag;	/* set if verbose mode -v */
90 extern int 		stingy_flag;
91 extern int              specified_levels;
92 #ifdef X_SHARED_MEMORY
93 /*
94  * use_shared_pixmaps is TRUE if the server supports shared images.
95  * no_shared_space is set to TRUE the first time shmget returns
96  * ENOSPC, so the error message is only printed once. We still
97  * try, though, as an 'r' or 'q' command could release segments.
98  */
99 extern Boolean		do_sharing;
100 extern Boolean		use_shared_pixmaps;
101 extern Boolean		no_shared_space;
102 #endif
103 
104 /* X11/NeWS server bug workaround. */
105 extern int no_color_ref_counts;
106 
107 /*
108  * Color map, gamma correction map, and lookup tables
109  */
110 
111 typedef struct _cmap_info_struct
112 {
113     int		red_shift;
114     int		green_shift;
115     int		blue_shift;
116     Pixel	red_mask;
117     Pixel	green_mask;
118     Pixel	blue_mask;
119     Pixel	pixel_base;
120 } cmap_info;
121 
122 typedef struct _image_info_struct
123 {
124     Window window, icn_window;	/* X windows and pixmaps */
125     Window pix_info_window;
126     Pixmap pixmap, icn_pixmap;
127     GC gc, icn_gc;			/* And GC's and XImages */
128     XImage *image, *icn_image;
129     Colormap colormap;
130     int visual_class;
131     Visual *dpy_visual;
132     int dpy_depth;
133     Boolean pixmap_failed;
134 
135     CONST_DECL char *filename;		/* file that Image came from.  */
136     CONST_DECL char *title;		/* title for this image...     */
137     int   img_num;			/* Number of image within file. */
138     FILE *fd;
139     unsigned char *scan_data;           /* a[img_h][img_w][img_clr_channels] */
140     int img_channels;			/* # of color channels in file       */
141     int dpy_channels;			/* # of channels we will display     */
142     VOID_FUNCTION *map_scanline;	/* map_scanline routine to use       */
143     VOID_FUNCTION *MAG_scanline;	/* MAG_scanline routine to use       */
144     float gamma;
145     float dpy_gamma;
146 
147     int x, y;				/* Original origin of image	     */
148     int xo, yo;				/* Origin of image in X11 window (-y)*/
149     int w, h;				/* width and height of image         */
150     int win_w, win_h;			/* width and height of window        */
151     int icn_w, icn_h;			/* width and height of icon          */
152     int pix_w, pix_h;			/* width and height of img->pixmap   */
153     int icn_factor;			/* divide factor from img -> icon    */
154 
155     int pan_x, pan_y, pan_w, pan_h;	/* image rect currently being viewed */
156     int mag_fact, save_mag_fact;	/* current magnification factor      */
157     Boolean mag_mode;			/* are we display magnified image?   */
158     int save_pan_x, save_pan_y;
159     int save_pan_w, save_pan_h;
160     int save_win_w, save_win_h;		/* wdth and hgt of window when saved */
161 
162     Boolean binary_img;			/* will we make it 2 color? (-W)     */
163     Boolean mono_img;			/* do we make it grey scale? (-w)    */
164     Boolean dither_img;			/* do we dither it? (-a)             */
165     Boolean rw_cmap;			/* is the colormap writable?         */
166     Boolean sep_colors;			/* is the visual True or DirectColor?*/
167     Boolean mono_color;			/* a one channel color image (mcut)  */
168     Boolean color_dpy;			/* False if we are FORCED to b/w     */
169 
170     rle_pixel **in_cmap;
171     int ncmap;				/* rle_hdr.ncmap 		     */
172     int cmlen;				/* Comment `color_map_length` in file*/
173 
174     int *modN;				/* dither arrays, all of them */
175     int *divN;
176     array16 *dm16;
177     cmap_info x_cmap;
178     Pixel *pixel_table;
179     Pixel white_pixel, black_pixel;
180     int lvls, lvls_squared;
181 #ifdef X_SHARED_MEMORY
182     XShmSegmentInfo shm_img;
183     XShmSegmentInfo shm_pix;
184 #endif
185 } image_information;
186 
187 /* pointer arithmetic... gack!  */
188 /* Returns Y'th row in our saved data array.  Works around problem with */
189 /* rle_getrow by adding 1 to y.  We waste the first line of this array  */
190 /* SAVED_RLE_ROW(img, -1) == img->scan_data, and is never used...       */
191 #define SAVED_RLE_ROW( img, y ) \
192     ((img)->scan_data + (((y) + 1) * (img)->w * (img)->dpy_channels))
193 
194 
195 #define duff8(counter, block) {\
196   while (counter >= 8) {\
197      { block; } \
198      { block; } \
199      { block; } \
200      { block; } \
201      { block; } \
202      { block; } \
203      { block; } \
204      { block; } \
205      counter -= 8;\
206   } \
207   switch (counter & 7) { \
208      case 7:    { block; } \
209      case 6:    { block; } \
210      case 5:    { block; } \
211      case 4:    { block; } \
212      case 3:    { block; } \
213      case 2:    { block; } \
214      case 1:    { block; } \
215      case 0:    counter = 0;\
216      }\
217 }
218 
219 #ifdef USE_PROTOTYPES
220 /* Prototypes.h contains prototypes for all global functions.  It is
221  * automatically generated using the cproto program.
222  */
223 #include "prototypes.h"
224 #else
225 /* This is also automatically generated by cproto. */
226 #include "fn_decls.h"
227 #endif
228