1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #ifndef _MCW_DISPLAY_HEADER_
8 #define _MCW_DISPLAY_HEADER_
9 
10 #include <math.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <X11/X.h>
16 #include <X11/Intrinsic.h>
17 #include <X11/Shell.h>
18 #include <X11/cursorfont.h>
19 
20 #include <Xm/Xm.h>
21 #include <Xm/MwmUtil.h>
22 #include <Xm/DialogS.h>
23 #include <Xm/PushB.h>
24 
25 #include "mrilib.h"
26 
27 #include "afni_environ.h"
28 
29 /*** Macros ***/
30 
31 #ifndef MAX
32 #   define MAX(a,b) (((a)<(b)) ? (b) : (a))
33 #   define MIN(a,b) (((a)>(b)) ? (b) : (a))
34 #endif
35 
36 #ifndef myXtFree
37 # define myXtFree(xp) (XtFree((char *)(xp)) , (xp)=NULL)
38 #endif
39 
40 #ifndef myXtNew
41 # define myXtNew(type) ((type *) XtCalloc(1,(unsigned) sizeof(type)))
42 #endif
43 
44 /* these macros are to produce RGB intensities (unsigned shorts) */
45 
46 #define CLIP_INTEN(i)    (((i)<256) ? (256) : ((i)>65280) ? (65280) : (i))
47 #define BYTE_TO_INTEN(b) (CLIP_INTEN((b)<<8))
48 #define INTEN_TO_BYTE(i) ((i)>>8)
49 
50 #define BRIGHTNESS(r,g,b)   (0.299*(r)+0.587*(g)+0.114*(b))
51 #define XCOL_BRIGHTNESS(xc) BRIGHTNESS((xc).red,(xc).green,(xc).blue)
52 
53 #define XCOL_REDNESS(xc)    (0.299*(xc).red   - MAX(0.587*(xc).green,0.114*(xc).blue ))
54 #define XCOL_GREENNESS(xc)  (0.587*(xc).green - MAX(0.299*(xc).red  ,0.114*(xc).blue ))
55 #define XCOL_BLUENESS(xc)   (0.114*(xc).blue  - MAX(0.299*(xc).red  ,0.587*(xc).green))
56 #define XCOL_YELLOWNESS(xc) (0.299*(xc).red+0.587*(xc).green-0.114*(xc).blue)
57 
58 /* given x in [0..wx-1], map proportionally to [0..wn-1] */
59 
60 #if 1
61 #define MAP_XY(x,wx,wn) (((wn)*(x)+(wn)/2)/(wx))
62 #else
63 #define MAP_XY(x,wx,wn) (((wn)*(x))/(wx))
64 #endif
65 
66 /* 07 Aug 1998:
67    Macro to produce TrueColor pixel values from
68    an RGB triple, by appropriately shifting and masking.
69    This can only be used if dc->visual_class == TrueColor! */
70 
71 #define RGB_TO_TCINT(dc,r,g,b)                                       \
72  ( ((((dc)->visual_redshift  <0)                                     \
73       ? ((r)<<(-(dc)->visual_redshift)  )                            \
74       : ((r)>>  (dc)->visual_redshift)  ) & (dc)->visual_redmask  )  \
75   |                                                                  \
76    ((((dc)->visual_greenshift<0)                                     \
77       ? ((g)<<(-(dc)->visual_greenshift))                            \
78       : ((g)>>  (dc)->visual_greenshift)) & (dc)->visual_greenmask)  \
79   |                                                                  \
80    ((((dc)->visual_blueshift <0)                                     \
81       ? ((b)<<(-(dc)->visual_blueshift) )                            \
82       : ((b)>>  (dc)->visual_blueshift) ) & (dc)->visual_bluemask ) )
83 
84 /*--- 11 Feb 1999: stuff for mapping colors to pixels ------------------*/
85 
86 typedef struct {
87    int classKRH ;    /* type of colormap: PseudoColor and TrueColor are OK */
88    int depth ;
89 
90    int ncolors , nblack,nwhite ;  /* This stuff for PseudoColor */
91    byte *rr , *gg , *bb ;
92 
93    unsigned long rrmask , ggmask , bbmask ;   /* This stuff for TrueColor */
94    int           rrshift, ggshift, bbshift;
95    Pixel         whpix ;
96 } DC_colordef ;
97 
98 #define FREE_DC_colordef(cd)                                     \
99   do{ if( (cd) != NULL ){                                        \
100          if( (cd)->rr != NULL ){                                 \
101             free((cd)->rr) ; free((cd)->gg) ; free((cd)->bb) ; } \
102          free((cd)) ; (cd) = NULL ; } } while(0)
103 
104 /***---------------------------- typedefs ----------------------------***/
105 
106 #define MAX_COLORS 256
107 #undef  NSBUF
108 #define NSBUF 128    /* Place here because SUMA needs that baby too */
109 
110 #if 0
111 #define NPANE_BIG    256    /* 30 Jan 2003: # colors in "big" mode , ZSS. Jan 06, Up from 128, Bigger, immer.*/
112 #else
113 extern int npane_big ;
114 #define NPANE_BIG     npane_big
115 #define NPANE_BIGGEST 2048  /* don't let npane_big be bigger than this! */
116 #endif
117 
118 #define NPANE_BIG1   (NPANE_BIG-1)
119 /* to add a big colormap: update NBIGMAP_INIT and BIGMAP_NAMES,
120  * and initialize that bigmap index in NJ_bigmaps_init */
121 #define NBIGMAP_INIT 9      /* # of initial colorscales */
122 #define NBIG_GAP     (NPANE_BIG/32)
123 #define NBIG_MBOT    (NPANE_BIG/2-NBIG_GAP)
124 #define NBIG_MTOP    (NPANE_BIG/2+NBIG_GAP)
125 #define AJJ_RED        0.0
126 #define AJJ_YEL       60.0
127 #define AJJ_GRN      120.0
128 #define AJJ_CYN      180.0
129 #define AJJ_BLU      240.0
130 #define AJJ_PUR      300.0
131 static char BIGMAP_NAMES[][32] = {
132    "Spectrum:red_to_blue", "Spectrum:red_to_blue+gap",
133    "Spectrum:yellow_to_cyan", "Spectrum:yellow_to_cyan+gap",
134    "Spectrum:yellow_to_red", "Color_circle_AJJ",
135    "Color_circle_ZSS", "Reds_and_Blues", "Reds_and_Blues_w_Green"
136    "\0" };
137 int NJ_bigmaps_init(int bigmap_num, char ***bigmap_namep, rgbyte ***bigmapp);
138 
139 /** Dec 1997: split overlay stuff into a separate struct **/
140 
141 typedef struct {
142    int    ncol_ov ;              /* number of overlay colors defined */
143    XColor xcol_ov[MAX_COLORS] ;  /* definitions of overlay colors */
144    Pixel  pix_ov[MAX_COLORS]  ;  /* Pixels for overlay */
145    char  *name_ov[MAX_COLORS] ;  /* names of overlay colors */
146    char  *label_ov[MAX_COLORS] ; /* labels for overlay colors */
147 
148    Pixel  pixov_brightest,pixov_darkest,pixov_reddest,pixov_greenest,pixov_bluest,pixov_yellowest;
149    int    ov_brightest   ,   ov_darkest,   ov_reddest,   ov_greenest,   ov_bluest,   ov_yellowest;
150 
151    float  bright_ov[MAX_COLORS] ; /* brightness of overlay colors [20 Dec 1999] */
152 
153    byte r_ov[MAX_COLORS] ;  /* 06 Mar 2001 */
154    byte g_ov[MAX_COLORS] ;
155    byte b_ov[MAX_COLORS] ;
156 } MCW_DCOV ;
157 
158 #define DCOV_REDBYTE(dc,i)   ((dc)->ovc->r_ov[i])
159 #define DCOV_GREENBYTE(dc,i) ((dc)->ovc->g_ov[i])
160 #define DCOV_BLUEBYTE(dc,i)  ((dc)->ovc->b_ov[i])
161 
162 #define DCOV_BRIGHTNESS(dc,i) ((dc)->ovc->bright_ov[i])
163 
164 typedef struct {
165       XtAppContext appcontext ;    /* X and Xt stuff */
166       Display     *display ;
167       Screen      *screen ;
168       int          screen_num ;
169       Visual      *visual ;
170       Colormap     colormap , default_colormap ;
171       GC           myGC , origGC ;
172       int          planes ;
173       int          depth ;
174 
175       VisualID      visual_id ;     /* 07 Aug 1998: added visual_* stuff */
176       XVisualInfo  *visual_info ;
177       unsigned long visual_redmask  , visual_greenmask  , visual_bluemask ;
178       int           visual_redshift , visual_greenshift , visual_blueshift ;
179       int           visual_class ;
180 
181       int          width , height ;       /* of the screen */
182 
183       int          ncol_im ;              /* # colors we use */
184       double       gamma , gamma_init ;   /* gamma factor */
185       int          use_xcol_im ;          /* color in use? */
186 
187       XColor       xgry_im[MAX_COLORS] ,  /* for images */
188                    xcol_im[MAX_COLORS]  ;
189 
190       Pixel        pix_im[MAX_COLORS] ;
191       int          pix_im_ready ;         /* 22 Aug 1998 */
192       int          byper , bypad ;        /* 23 Aug 1998 */
193 
194       MCW_DCOV    *ovc ;                  /* Dec 1997 */
195 
196       int          xint_im[MAX_COLORS] ;  /* intensity levels for xgry_im */
197 
198       XFontStruct  *myFontStruct ;
199 
200       Widget       parent_widget ;
201 
202       XtPointer    parent , aux ;
203 
204       DC_colordef  *cdef ;    /* 11 Feb 1999 */
205 
206       int does_backingstore ; /* 27 Feb 2001 */
207       int does_saveunders ;
208 
209       byte r_im[MAX_COLORS] ; /* 06 Mar 2001 */
210       byte g_im[MAX_COLORS] ;
211       byte b_im[MAX_COLORS] ;
212 #if 0
213       byte gray_im[MAX_COLORS] ;
214 #endif
215 } MCW_DC ;
216 
217 extern MCW_DC *first_dc ;                     /* 26 Jun 2003 */
218 
219 #define DC_REDBYTE(dc,i)     ((dc)->r_im[i])  /* 06 Mar 2001 */
220 #define DC_GREENBYTE(dc,i)   ((dc)->g_im[i])
221 #define DC_BLUEBYTE(dc,i)    ((dc)->b_im[i])
222 
223 #if 0
224 #define DC_GRAYBYTE(dc,i)    ((dc)->gray_im[i])
225 #endif
226 
227 /* text fonts to look for if the defaults fail */
228 
229 static char *tfont_hopefuls[] = {
230                 "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1"   ,
231                 "-misc-fixed-bold-r-normal--13-100-100-100-c-70-iso8859-1"    ,
232                 "lucidasanstypewriter-bold-10" ,
233                 "9x15bold" , "8x13bold" , "7x14bold" , "6x13bold" ,
234                 "fixed" ,                 /* this is the font of last resort */
235               NULL } ;
236 
237 /*** Macro for text widths ***/
238 
239 #define DC_text_width(dc,str) XTextWidth((dc)->myFontStruct,(str),strlen((str)))
240 
241 extern int DC_char_height ( MCW_DC * , char ) ;  /* 18 Apr 2011 */
242 extern int DC_char_width  ( MCW_DC * , char ) ;
243 extern int_pair DC_char_adscent( MCW_DC * , char ) ;
244 
245 /*** prototypes ***/
246 
247 #ifdef  __cplusplus
248 extern "C" {
249 #endif
250 
251 extern void DC_yokify( Widget , MCW_DC * ) ; /* 14 Sep 1998 */
252 
253 extern MCW_DC * MCW_new_DC( Widget, int, int, char * c[], char * l[], double, int ) ;
254 
255 extern void DC_init_im_gry( MCW_DC * ) ;
256 extern void DC_init_im_col( MCW_DC * ) ;
257 extern void DC_init_ov_col( MCW_DC * ) ;
258 
259 #if 0
260 extern Pixel RGB_byte_to_color( MCW_DC *, int,int,int ) ;
261 extern Pixel Name_to_color( MCW_DC * , char * ) ;
262 #endif
263 
264 extern int DC_add_overlay_color( MCW_DC * , char * , char * ) ;
265 extern int DC_find_overlay_color( MCW_DC * , char * ) ;
266 extern int DC_find_closest_overlay_color( MCW_DC * , char * ) ;
267 
268 extern void load_tmp_colors( int , XColor c[] ) ;
269 
270 extern void DC_palette_rotate( MCW_DC * , int ) ;
271 extern void DC_palette_swap( MCW_DC * ) ;
272 
273 extern void DC_palette_bright( MCW_DC * , int ) ;
274 extern void DC_palette_squeeze( MCW_DC * , int ) ;
275 
276 extern void DC_palette_restore( MCW_DC * , double ) ;
277 
278 extern void DC_gray_change( MCW_DC * , int ) ;
279 extern void DC_color_bright( MCW_DC * , int ) ;
280 
281 extern void DC_gray_contrast( MCW_DC * , int ) ;
282 extern void DC_color_squeeze( MCW_DC * , int ) ;
283 
284 extern void DC_gray_conbrio( MCW_DC * , int ) ;  /* 23 Oct 2003 */
285 
286 extern void DC_palette_setgray( MCW_DC * ) ;
287 extern void DC_palette_setcolor( MCW_DC * ) ;
288 
289 extern RwcBoolean MCW_check_iconsize( int,int,MCW_DC * ) ;
290 
291 extern XColor * DCpix_to_XColor( MCW_DC * , Pixel , int ) ;
292 
293 extern void DC_fg_color( MCW_DC * , int ) ;
294 extern void DC_bg_color( MCW_DC * , int ) ;
295 extern void DC_fg_colortext( MCW_DC * , char * ) ;
296 extern void DC_linewidth( MCW_DC * , int ) ;
297 extern void DC_fg_colorpix( MCW_DC * , Pixel ) ;
298 
299 extern void DC_linestyle( MCW_DC * , int ) ;
300 #define DC_solid_line(ddcc)  DC_linestyle((ddcc),LineSolid)
301 #define DC_dashed_line(ddcc) DC_linestyle((ddcc),LineOnOffDash)
302 
303 extern void OVC_mostest( MCW_DCOV * ) ;
304 
305 extern void DC_set_image_colors( MCW_DC * ) ;  /* 22 Aug 1998 */
306 
307 extern void reload_DC_colordef( MCW_DC * ) ;   /* 11 Feb 1999 */
308 extern Pixel DC_rgb_to_pixel( MCW_DC *, byte,byte,byte ) ;
309 extern void DC_pixel_to_rgb( MCW_DC *, Pixel, byte *,byte *,byte * ) ;
310 
311 extern Pixel DC_rgb_to_ovpix( MCW_DC *, byte,byte,byte ) ; /* 20 Dec 1999 */
312 extern void DC_rgb_to_ovrgb( MCW_DC *, int,int *,int,byte *, byte *, byte *) ;
313 
314 extern int DC_parse_color( MCW_DC *, char *, float *,float *,float *) ; /* 21 Sep 2001 */
315 
316 extern rgbyte DC_spectrum_AJJ( double, double ) ;
317 extern rgbyte DC_spectrum_ZSS( double, double ) ;
318 
319 extern void show_motif_version_string(void) ;   /* 4 Mar 2009 [rickr] */
320 extern int  source_is_lesstif        (void) ;
321 extern int  using_lesstif_is_defined (void) ;
322 
323 #ifdef  __cplusplus
324 }
325 #endif
326 
327 #endif /* _MCW_DISPLAY_HEADER_ */
328