/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % % % X X % % X X % % X % % X X % % X X % % % % X11 Utility Routines for ImageMagick. % % % % % % Software Design % % John Cristy % % July 1992 % % % % % % Copyright 1997 E. I. du Pont de Nemours and Company % % % % Permission to use, copy, modify, distribute, and sell this software and % % its documentation for any purpose is hereby granted without fee, % % provided that the above Copyright notice appear in all copies and that % % both that Copyright notice and this permission notice appear in % % supporting documentation, and that the name of E. I. du Pont de Nemours % % and Company not be used in advertising or publicity pertaining to % % distribution of the software without specific, written prior % % permission. E. I. du Pont de Nemours and Company makes no representations % % about the suitability of this software for any purpose. It is provided % % "as is" without express or implied warranty. % % % % E. I. du Pont de Nemours and Company disclaims all warranties with regard % % to this software, including all implied warranties of merchantability % % and fitness, in no event shall E. I. du Pont de Nemours and Company be % % liable for any special, indirect or consequential damages or any % % damages whatsoever resulting from loss of use, data or profits, whether % % in an action of contract, negligence or other tortious action, arising % % out of or in connection with the use or performance of this software. % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % */ /* Include declarations. */ #include "magick.h" #include "Colorlist.h" /* State declarations. */ #define ControlState 0x0001 #define DefaultState 0x0000 #define ExitState 0x0002 /* Global declarations. */ static unsigned int xerror_alert = False; /* Function prototypes. */ static void XMakeImageLSBFirst(const XResourceInfo *,const XWindowInfo *,Image *, XImage *,XImage *), XMakeImageMSBFirst(const XResourceInfo *,const XWindowInfo *,Image *, XImage *,XImage *); /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % I s T r u e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function IsTrue returns True if the boolean is "true", "on", "yes" or "1". % % The format of the IsTrue routine is: % % option=IsTrue(boolean) % % A description of each parameter follows: % % o option: either True or False depending on the boolean parameter. % % o boolean: Specifies a pointer to a character array. % % */ Export unsigned int IsTrue(const char *boolean) { if (boolean == (char *) NULL) return(False); if (Latin1Compare(boolean,"true") == 0) return(True); if (Latin1Compare(boolean,"on") == 0) return(True); if (Latin1Compare(boolean,"yes") == 0) return(True); if (Latin1Compare(boolean,"1") == 0) return(True); return(False); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % L a t i n 1 C o m p a r e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function Latin1Compare compares two null terminated Latin-1 strings, % ignoring case differences, and returns an integer greater than, equal % to, or less than 0, according to whether first is lexicographically % greater than, equal to, or less than second. The two strings are % assumed to be encoded using ISO 8859-1. % % The format of the Latin1Compare routine is: % % Latin1Compare(first,second) % % A description of each parameter follows: % % o first: A pointer to the string to convert to Latin1 string. % % o second: A pointer to the string to convert to Latin1 string. % % */ Export int Latin1Compare(const char *first,const char *second) { register unsigned char *p, *q; if (first == second) return(0); if (first == (char *) NULL) return(-1); if (second == (char *) NULL) return(1); p=(unsigned char *) first; q=(unsigned char *) second; while ((*p != '\0') && (*q != '\0')) { register unsigned char c, d; c=(*p); d=(*q); if (c != d) { /* Try lowercasing and try again. */ if ((c >= XK_A) && (c <= XK_Z)) c+=(XK_a-XK_A); else if ((c >= XK_Agrave) && (c <= XK_Odiaeresis)) c+=(XK_agrave-XK_Agrave); else if ((c >= XK_Ooblique) && (c <= XK_Thorn)) c+=(XK_oslash-XK_Ooblique); if ((d >= XK_A) && (d <= XK_Z)) d+=(XK_a-XK_A); else if ((d >= XK_Agrave) && (d <= XK_Odiaeresis)) d+=(XK_agrave-XK_Agrave); else if ((d >= XK_Ooblique) && (d <= XK_Thorn)) d+=(XK_oslash-XK_Ooblique); if (c != d) return(((int) c)-((int) d)); } p++; q++; } return(((int) *p)-((int) *q)); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % L a t i n 1 U p p e r % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function Latin1Upper copies a null terminated string from source to % destination (including the null), changing all Latin-1 lowercase letters % to uppercase. The string is assumed to be encoded using ISO 8859-1. % % The format of the Latin1Upper routine is: % % Latin1Upper(string) % % A description of each parameter follows: % % o string: A pointer to the string to convert to upper-case Latin1. % % */ static void Latin1Upper(char *string) { unsigned char c; if (string == (char *) NULL) return; c=(*string); while (c != '\0') { if ((c >= XK_a) && (c <= XK_z)) *string=c-(XK_a-XK_A); else if ((c >= XK_agrave) && (c <= XK_odiaeresis)) *string=c-(XK_agrave-XK_Agrave); else if ((c >= XK_oslash) && (c <= XK_thorn)) *string=c-(XK_oslash-XK_Ooblique); string++; c=(*string); } } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X A n n o t a t e I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XAnnotateImage annotates the image with text. % % The format of the XAnnotateImage routine is: % % status=XAnnotateImage(display,pixel_info,annotate_info,image) % % A description of each parameter follows: % % o status: Function XAnnotateImage returns True if the image is % successfully annotated with text. False is returned is there is a % memory shortage. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o pixel_info: Specifies a pointer to a XPixelInfo structure. % % o annotate_info: Specifies a pointer to a XAnnotateInfo structure. % % o image: Specifies a pointer to a Image structure; returned from % ReadImage. % % */ Export unsigned int XAnnotateImage(Display *display, const XPixelInfo *pixel_info,const XAnnotateInfo *annotate_info,Image *image) { GC annotate_context; Image *annotate_image; int x, y; Pixmap annotate_pixmap; register int i; register RunlengthPacket *p; unsigned int depth, height, matte, width; Window root_window; XGCValues context_values; XImage *annotate_ximage; /* Initialize annotated image. */ assert(display != (Display *) NULL); assert(pixel_info != (XPixelInfo *) NULL); assert(annotate_info != (XAnnotateInfo *) NULL); assert(image != (Image *) NULL); if (!UncompressImage(image)) return(False); /* Initialize annotated pixmap. */ root_window=XRootWindow(display,XDefaultScreen(display)); depth=XDefaultDepth(display,XDefaultScreen(display)); annotate_pixmap=XCreatePixmap(display,root_window,annotate_info->width, annotate_info->height,(int) depth); if (annotate_pixmap == (Pixmap) NULL) return(False); /* Initialize graphics info. */ context_values.background=0; context_values.foreground=(unsigned long) (~0); context_values.font=annotate_info->font_info->fid; annotate_context=XCreateGC(display,root_window,GCBackground | GCFont | GCForeground,&context_values); if (annotate_context == (GC) NULL) return(False); /* Draw text to pixmap. */ XDrawImageString(display,annotate_pixmap,annotate_context,0, (int) annotate_info->font_info->ascent,annotate_info->text, Extent(annotate_info->text)); XFreeGC(display,annotate_context); /* Initialize annotated X image. */ annotate_ximage=XGetImage(display,annotate_pixmap,0,0,annotate_info->width, annotate_info->height,AllPlanes,ZPixmap); if (annotate_ximage == (XImage *) NULL) return(False); XFreePixmap(display,annotate_pixmap); /* Initialize annotated image. */ annotate_image=AllocateImage((ImageInfo *) NULL); if (annotate_image == (Image *) NULL) return(False); annotate_image->columns=annotate_info->width; annotate_image->rows=annotate_info->height; annotate_image->packets=annotate_image->columns*annotate_image->rows; annotate_image->pixels=(RunlengthPacket *) malloc((unsigned int) image->packets*sizeof(RunlengthPacket)); if (annotate_image->pixels == (RunlengthPacket *) NULL) { DestroyImage(annotate_image); return(False); } /* Transfer annotated X image to image. */ (void) XParseGeometry(annotate_info->geometry,&x,&y,&width,&height); p=image->pixels+y*image->columns+x; annotate_image->background_color.red=p->red; annotate_image->background_color.green=p->green; annotate_image->background_color.blue=p->blue; annotate_image->background_color.index=Transparent; annotate_image->matte=True; p=annotate_image->pixels; for (y=0; y < annotate_image->rows; y++) for (x=0; x < annotate_image->columns; x++) { p->index=(unsigned short) XGetPixel(annotate_ximage,x,y); if (p->index == 0) { /* Set this pixel to the background color. */ p->red=XDownScale(pixel_info->box_color.red); p->green=XDownScale(pixel_info->box_color.green); p->blue=XDownScale(pixel_info->box_color.blue); p->index=Opaque; if (annotate_info->stencil == ForegroundStencil) { p->red=annotate_image->background_color.red; p->green=annotate_image->background_color.green; p->blue=annotate_image->background_color.blue; p->index=Transparent; } } else { /* Set this pixel to the pen color. */ p->red=XDownScale(pixel_info->pen_color.red); p->green=XDownScale(pixel_info->pen_color.green); p->blue=XDownScale(pixel_info->pen_color.blue); p->index=Opaque; if (annotate_info->stencil == BackgroundStencil) { p->red=annotate_image->background_color.red; p->green=annotate_image->background_color.green; p->blue=annotate_image->background_color.blue; p->index=Transparent; } } p->length=0; p++; } XDestroyImage(annotate_ximage); /* Determine annotate geometry. */ (void) XParseGeometry(annotate_info->geometry,&x,&y,&width,&height); if ((width != annotate_image->columns) || (height != annotate_image->rows)) { char image_geometry[MaxTextExtent]; /* Scale image. */ (void) sprintf(image_geometry,"%ux%u",width,height); TransformImage(&annotate_image,(char *) NULL,image_geometry); } if (annotate_info->degrees != 0.0) { double normalized_degrees; Image *rotated_image; int rotations; /* Rotate image. */ rotated_image= RotateImage(annotate_image,annotate_info->degrees,False,True); if (rotated_image == (Image *) NULL) return(False); DestroyImage(annotate_image); annotate_image=rotated_image; /* Annotation is relative to the degree of rotation. */ normalized_degrees=annotate_info->degrees; while (normalized_degrees < -45.0) normalized_degrees+=360.0; for (rotations=0; normalized_degrees > 45.0; rotations++) normalized_degrees-=90.0; switch (rotations % 4) { default: case 0: break; case 1: { /* Rotate 90 degrees. */ x-=annotate_image->columns >> 1; y+=annotate_image->columns >> 1; break; } case 2: { /* Rotate 180 degrees. */ x-=annotate_image->columns; break; } case 3: { /* Rotate 270 degrees. */ x-=annotate_image->columns >> 1; y-=annotate_image->rows-(annotate_image->columns >> 1); break; } } } /* Composite text onto the image. */ p=annotate_image->pixels; for (i=0; i < annotate_image->packets; i++) { if (p->index != Transparent) p->index=Opaque; p++; } matte=image->matte; CompositeImage(image,OverCompositeOp,annotate_image,x,y); image->matte=matte; DestroyImage(annotate_image); return(True); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X B e s t F o n t % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XBestFont returns the "best" font. "Best" is defined as a font % specified in the X resource database or a font such that the text width % displayed with the font does not exceed the specified maximum width. % % The format of the XBestFont routine is: % % font=XBestFont(display,resource_info,text_font) % % A description of each parameter follows: % % o font: XBestFont returns a pointer to a XFontStruct structure. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o resource_info: Specifies a pointer to a X11 XResourceInfo structure. % % o text_font: True is font should be mono-spaced (typewriter style). % % */ static char **FontToList(char *font) { char **fontlist; register char *p, *q; register int i; unsigned int fonts; if (font == (char *) NULL) return((char **) NULL); /* Convert string to an ASCII list. */ fonts=1; for (p=font; *p != '\0'; p++) if (*p == ':') fonts++; fontlist=(char **) malloc((fonts+1)*sizeof(char *)); if (fontlist == (char **) NULL) { Warning("Unable to convert font","Memory allocation failed"); return((char **) NULL); } p=font; for (i=0; i < fonts; i++) { for (q=p; *q != '\0'; q++) if (*q == ':') break; fontlist[i]=(char *) malloc((q-p+1)*sizeof(char)); if (fontlist[i] == (char *) NULL) { Warning("Unable to convert font","Memory allocation failed"); return((char **) NULL); } (void) strncpy(fontlist[i],p,q-p); fontlist[i][q-p]='\0'; p=q+1; } fontlist[i]=(char *) NULL; return(fontlist); } Export XFontStruct *XBestFont(Display *display, const XResourceInfo *resource_info,const unsigned int text_font) { static char *Fonts[]= { "-adobe-helvetica-medium-r-normal-*-14-*", "-*-helvetica-medium-r-*-*-14-*", "-*-lucida-medium-r-*-*-14-*", "8x13", "6x13", "fixed", "variable", (char *) NULL }, *TextFonts[]= { "-adobe-courier-medium-r-*-*-14-*", "-*-fixed-medium-r-normal-*-14-*", "-*-fixed-medium-r-*-*-14-*", "8x13", "6x13", "fixed", (char *) NULL }; char *font_name, **p; XFontStruct *font_info; font_info=(XFontStruct *) NULL; font_name=resource_info->font; if (text_font) font_name=resource_info->text_font; if (font_name != (char *) NULL) { char **fontlist; register int i; /* Load preferred font specified in the X resource database. */ fontlist=FontToList(font_name); if (fontlist != (char **) NULL) { for (i=0; fontlist[i] != (char *) NULL; i++) { if (font_info == (XFontStruct *) NULL) font_info=XLoadQueryFont(display,fontlist[i]); free((char *) fontlist[i]); } free((char *) fontlist); } if (font_info == (XFontStruct *) NULL) Warning("Unable to load font",font_name); } /* Load fonts from list of fonts until one is found. */ p=Fonts; if (text_font) p=TextFonts; while (*p != (char *) NULL) { if (font_info != (XFontStruct *) NULL) break; font_info=XLoadQueryFont(display,*p); p++; } return(font_info); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X B e s t I c o n S i z e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XBestIconSize returns the "best" icon size. "Best" is defined as % an icon size that maintains the aspect ratio of the image. If the window % manager has preferred icon sizes, one of the preferred sizes is used. % % The format of the XBestIconSize routine is: % % XBestIconSize(display,window,image) % % A description of each parameter follows: % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o image: Specifies a pointer to a Image structure; returned from % ReadImage. % % */ Export void XBestIconSize(Display *display,XWindowInfo *window,Image *image) { #define MaxIconSize 96 int i, number_sizes; unsigned int height, icon_height, icon_width, width; unsigned long scale_factor; Window root_window; XIconSize *icon_size, *size_list; /* Determine if the window manager has specified preferred icon sizes. */ assert(display != (Display *) NULL); assert(window != (XWindowInfo *) NULL); assert(image != (Image *) NULL); window->width=MaxIconSize; window->height=MaxIconSize; icon_size=(XIconSize *) NULL; number_sizes=0; root_window=XRootWindow(display,window->screen); if (XGetIconSizes(display,root_window,&size_list,&number_sizes) != 0) if ((number_sizes > 0) && (size_list != (XIconSize *) NULL)) icon_size=size_list; if (icon_size == (XIconSize *) NULL) { /* Window manager does not restrict icon size. */ icon_size=XAllocIconSize(); if (icon_size == (XIconSize *) NULL) { Warning("Unable to choose best icon size","Memory allocation failed"); return; } icon_size->min_width=1; icon_size->max_width=MaxIconSize; icon_size->min_height=1; icon_size->max_height=MaxIconSize; icon_size->width_inc=1; icon_size->height_inc=1; } /* Determine aspect ratio of image. */ width=image->columns; height=image->rows; if (window->crop_geometry) (void) XParseGeometry(window->crop_geometry,&i,&i,&width,&height); /* Look for an icon size that maintains the aspect ratio of image. */ scale_factor=UpShift(icon_size->max_width)/width; if (scale_factor > (UpShift(icon_size->max_height)/height)) scale_factor=UpShift(icon_size->max_height)/height; icon_width=icon_size->min_width; while (icon_width < icon_size->max_width) { if (icon_width >= (DownShift(width*scale_factor))) break; icon_width+=icon_size->width_inc; } icon_height=icon_size->min_height; while (icon_height < icon_size->max_height) { if (icon_height >= (DownShift(height*scale_factor))) break; icon_height+=icon_size->height_inc; } XFree((void *) icon_size); window->width=icon_width; window->height=icon_height; } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X B e s t P i x e l % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XBestPixel returns a pixel from an array of pixels that is closest % to the requested color. If the color array is NULL, the colors are obtained % from the X server. % % The format of the XBestPixel routine is: % % pixel=XBestPixel(display,colormap,colors,number_colors,color) % % A description of each parameter follows: % % o pixel: XBestPixel returns the pixel value closest to the requested % color. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o colormap: Specifies the ID of the X server colormap. % % o colors: Specifies an array of XColor structures. % % o number_colors: Specifies the number of XColor structures in the % color definition array. % % o color: Specifies the desired RGB value to find in the colors array. % % */ Export void XBestPixel(Display *display,const Colormap colormap,XColor *colors, unsigned int number_colors,XColor *color) { double distance_squared, min_distance; int distance, query_server, status; register int i, j; /* Find closest representation for the requested RGB color. */ assert(display != (Display *) NULL); assert(color != (XColor *) NULL); status=XAllocColor(display,colormap,color); if (status != 0) return; query_server=colors == (XColor *) NULL; if (query_server) { /* Read X server colormap. */ colors=(XColor *) malloc(number_colors*sizeof(XColor)); if (colors == (XColor *) NULL) Error("Unable to read X server colormap","Memory allocation failed"); for (i=0; i < number_colors; i++) colors[i].pixel=(unsigned long) i; if (number_colors > 256) number_colors=256; XQueryColors(display,colormap,colors,number_colors); } min_distance=3.0*65536.0*65536.0; color->pixel=colors[0].pixel; j=0; for (i=0; i < number_colors; i++) { distance=(int) colors[i].red-(int) color->red; distance_squared=(unsigned int) (distance*distance); distance=(int) colors[i].green-(int) color->green; distance_squared+=(unsigned int) (distance*distance); distance=(int) colors[i].blue-(int) color->blue; distance_squared+=(unsigned int) (distance*distance); if (distance_squared < min_distance) { min_distance=distance_squared; color->pixel=colors[i].pixel; j=i; } } (void) XAllocColor(display,colormap,&colors[j]); if (query_server) free((char *) colors); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X B e s t V i s u a l I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XBestVisualInfo returns visual information for a visual that is % the "best" the server supports. "Best" is defined as: % % 1. Restrict the visual list to those supported by the default screen. % % 2. If a visual type is specified, restrict the visual list to those of % that type. % % 3. If a map type is specified, choose the visual that matches the id % specified by the Standard Colormap. % % 4 From the list of visuals, choose one that can display the most % simultaneous colors. If more than one visual can display the same % number of simultaneous colors, one is choosen based on a rank. % % The format of the XBestVisualInfo routine is: % % visual_info=XBestVisualInfo(display,map_info,resource_info) % % A description of each parameter follows: % % o visual_info: XBestVisualInfo returns a pointer to a X11 XVisualInfo % structure. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o map_info: If map_type is specified, this structure is initialized % with info from the Standard Colormap. % % o resource_info: Specifies a pointer to a X11 XResourceInfo structure. % % */ Export XVisualInfo *XBestVisualInfo(Display *display, XStandardColormap *map_info,XResourceInfo *resource_info) { #define MaxStandardColormaps 7 #define XVisualColormapSize(visual_info) Min( \ (visual_info->class == TrueColor) || (visual_info->class == DirectColor) ? \ visual_info->red_mask | visual_info->green_mask | visual_info->blue_mask : \ visual_info->colormap_size,1 << visual_info->depth) char *map_type, *visual_type; register int i; static int number_visuals; static XVisualInfo visual_template; unsigned int visual_mask; XVisualInfo *visual_info, *visual_list; /* Restrict visual search by screen number. */ assert(display != (Display *) NULL); assert(map_info != (XStandardColormap *) NULL); assert(resource_info != (XResourceInfo *) NULL); map_type=resource_info->map_type; visual_type=resource_info->visual_type; visual_mask=VisualScreenMask; visual_template.screen=XDefaultScreen(display); if (visual_type != (char *) NULL) { /* Restrict visual search by class or visual id. */ if (Latin1Compare("staticgray",visual_type) == 0) { visual_mask|=VisualClassMask; visual_template.class=StaticGray; } else if (Latin1Compare("grayscale",visual_type) == 0) { visual_mask|=VisualClassMask; visual_template.class=GrayScale; } else if (Latin1Compare("staticcolor",visual_type) == 0) { visual_mask|=VisualClassMask; visual_template.class=StaticColor; } else if (Latin1Compare("pseudocolor",visual_type) == 0) { visual_mask|=VisualClassMask; visual_template.class=PseudoColor; } else if (Latin1Compare("truecolor",visual_type) == 0) { visual_mask|=VisualClassMask; visual_template.class=TrueColor; } else if (Latin1Compare("directcolor",visual_type) == 0) { visual_mask|=VisualClassMask; visual_template.class=DirectColor; } else if (Latin1Compare("default",visual_type) == 0) { visual_mask|=VisualIDMask; visual_template.visualid=XVisualIDFromVisual( XDefaultVisual(display,XDefaultScreen(display))); } else if (isdigit(*visual_type)) { visual_mask|=VisualIDMask; visual_template.visualid= strtol(visual_type,(char **) NULL,0); } else Warning("Invalid visual specifier",visual_type); } /* Get all visuals that meet our criteria so far. */ number_visuals=0; visual_list=XGetVisualInfo(display,visual_mask,&visual_template, &number_visuals); visual_mask=VisualScreenMask | VisualIDMask; if ((number_visuals == 0) || (visual_list == (XVisualInfo *) NULL)) { /* Failed to get visual; try using the default visual. */ Warning("Unable to get visual",visual_type); visual_template.visualid= XVisualIDFromVisual(XDefaultVisual(display,XDefaultScreen(display))); visual_list=XGetVisualInfo(display,visual_mask,&visual_template, &number_visuals); if ((number_visuals == 0) || (visual_list == (XVisualInfo *) NULL)) return((XVisualInfo *) NULL); Warning("Using default visual",XVisualClassName(visual_list->class)); } resource_info->color_recovery=False; if ((map_info != (XStandardColormap *) NULL) && (map_type != (char *) NULL)) { Atom map_property; char map_name[MaxTextExtent]; int j, number_maps, status; Window root_window; XStandardColormap *map_list; /* Choose a visual associated with a standard colormap. */ root_window=XRootWindow(display,XDefaultScreen(display)); status=0; if (strcmp(map_type,"list") != 0) { /* User specified Standard Colormap. */ (void) sprintf((char *) map_name,"RGB_%s_MAP",map_type); Latin1Upper(map_name); map_property=XInternAtom(display,(char *) map_name,True); if (map_property == (Atom) NULL) Error("Unable to get Standard Colormap",map_type); status=XGetRGBColormaps(display,root_window,&map_list,&number_maps, map_property); } else { static char *colormap[]= { "_HP_RGB_SMOOTH_MAP_LIST", "RGB_BEST_MAP", "RGB_DEFAULT_MAP", "RGB_GRAY_MAP", "RGB_RED_MAP", "RGB_GREEN_MAP", "RGB_BLUE_MAP", }; /* Choose a standard colormap from a list. */ for (i=0; i < MaxStandardColormaps; i++) { map_property=XInternAtom(display,colormap[i],True); if (map_property == (Atom) NULL) continue; status=XGetRGBColormaps(display,root_window,&map_list,&number_maps, map_property); if (status != 0) break; } resource_info->color_recovery=(i == 0); /* _HP_RGB_SMOOTH_MAP_LIST */ } if (status == 0) Error("Unable to get Standard Colormap",map_type); /* Search all Standard Colormaps and visuals for ids that match. */ *map_info=map_list[0]; #ifndef PRE_R4_ICCCM visual_template.visualid=XVisualIDFromVisual(visual_list[0].visual); for (i=0; i < number_maps; i++) for (j=0; j < number_visuals; j++) if (map_list[i].visualid == XVisualIDFromVisual(visual_list[j].visual)) { *map_info=map_list[i]; visual_template.visualid= XVisualIDFromVisual(visual_list[j].visual); break; } if (map_info->visualid != visual_template.visualid) Error("Unable to match visual to Standard Colormap",map_type); #endif if (map_info->colormap == (Colormap) NULL) Error("Standard Colormap is not initialized",map_type); XFree((void *) map_list); } else { static unsigned int rank[]= { StaticGray, GrayScale, StaticColor, DirectColor, TrueColor, PseudoColor }; XVisualInfo *p; /* Pick one visual that displays the most simultaneous colors. */ visual_info=visual_list; p=visual_list; for (i=1; i < number_visuals; i++) { p++; if (XVisualColormapSize(p) > XVisualColormapSize(visual_info)) visual_info=p; else if (XVisualColormapSize(p) == XVisualColormapSize(visual_info)) if (rank[p->class] > rank[visual_info->class]) visual_info=p; } visual_template.visualid=XVisualIDFromVisual(visual_info->visual); } XFree((void *) visual_list); /* Retrieve only one visual by its screen & id number. */ visual_info=XGetVisualInfo(display,visual_mask,&visual_template, &number_visuals); if ((number_visuals == 0) || (visual_info == (XVisualInfo *) NULL)) return((XVisualInfo *) NULL); return(visual_info); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X C h e c k R e f r e s h W i n d o w s % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XCheckRefreshWindows checks the X server for exposure events for % a particular window and updates the area associated withe exposure event. % % The format of the XCheckRefreshWindows routine is: % % XCheckRefreshWindows(display,windows) % % A description of each parameter follows: % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o windows: Specifies a pointer to a XWindows structure. % % */ Export void XCheckRefreshWindows(Display *display,XWindows *windows) { XEvent event; assert(display != (Display *) NULL); assert(windows != (XWindows *) NULL); XDelay(display,SuspendTime); while (XCheckTypedWindowEvent(display,windows->command.id,Expose,&event)) (void) XCommandWidget(display,windows,(char **) NULL,&event); while (XCheckTypedWindowEvent(display,windows->image.id,Expose,&event)) XRefreshWindow(display,&windows->image,&event); XDelay(display,SuspendTime << 1); while (XCheckTypedWindowEvent(display,windows->command.id,Expose,&event)) (void) XCommandWidget(display,windows,(char **) NULL,&event); while (XCheckTypedWindowEvent(display,windows->image.id,Expose,&event)) XRefreshWindow(display,&windows->image,&event); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X C l i e n t M e s s a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XClientMessage sends a message to a window with XSendEvent. The % message is initialized with a particular protocol type and atom. % % The format of the XClientMessage function is: % % XClientMessage(display,window,protocol,message,timestamp) % % A description of each parameter follows: % % o display: Specifies a pointer to the Display structure; returned from % XOpenDisplay. % % o window: Specifies a pointer to a Window structure. % % o protocol: Specifies an atom value. % % o message: Specifies an atom value which is the message to send. % % o timestamp: Specifies a value of type Time. % % */ Export void XClientMessage(Display *display,const Window window, const Atom protocol,const Atom message,const Time timestamp) { XClientMessageEvent client_event; assert(display != (Display *) NULL); client_event.type=ClientMessage; client_event.window=window; client_event.message_type=protocol; client_event.format=32; client_event.data.l[0]=(long) message; client_event.data.l[1]=(long) timestamp; XSendEvent(display,window,False,NoEventMask,(XEvent *) &client_event); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X C l i e n t W i n d o w % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XClientWindow finds a window, at or below the specified window, % which has a WM_STATE property. If such a window is found, it is returned, % otherwise the argument window is returned. % % The format of the XClientWindow function is: % % client_window=XClientWindow(display,target_window) % % A description of each parameter follows: % % o client_window: XClientWindow returns a window, at or below the specified % window, which has a WM_STATE property otherwise the argument % target_window is returned. % % o display: Specifies a pointer to the Display structure; returned from % XOpenDisplay. % % o target_window: Specifies the window to find a WM_STATE property. % % */ Window XClientWindow(Display *display,Window target_window) { Atom state, type; int format, status; unsigned char *data; unsigned long after, number_items; Window client_window; assert(display != (Display *) NULL); state=XInternAtom(display,"WM_STATE",True); if (state == (Atom) NULL) return(target_window); type=(Atom) NULL; status=XGetWindowProperty(display,target_window,state,0L,0L,False, (Atom) AnyPropertyType,&type,&format,&number_items,&after,&data); if ((status == Success) && (type != (Atom) NULL)) return(target_window); client_window=XWindowByProperty(display,target_window,state); if (client_window == (Window) NULL) return(target_window); return(client_window); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X C o n s t r a i n W i n d o w P o s i t i o n % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XConstrainWindowPosition assures a window is positioned witin the % X server boundaries. % % The format of the XConstrainWindowPosition routine is: % % XConstrainWindowPosition(display,window_info) % % A description of each parameter follows: % % o display: Specifies a pointer to the Display structure; returned from % XOpenDisplay. % % o window_info: Specifies a pointer to a XWindowInfo structure. % % */ Export void XConstrainWindowPosition(Display *display,XWindowInfo *window_info) { unsigned int limit; assert(display != (Display *) NULL); assert(window_info != (XWindowInfo *) NULL); limit=XDisplayWidth(display,window_info->screen)-window_info->width; if (window_info->x < 0) window_info->x=0; else if (window_info->x > limit) window_info->x=limit; limit=XDisplayHeight(display,window_info->screen)-window_info->height; if (window_info->y < 0) window_info->y=0; else if (window_info->y > limit) window_info->y=limit; } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X D e l a y % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XDelay suspends program execution for the number of milliseconds % specified. % % The format of the Delay routine is: % % XDelay(display,milliseconds) % % A description of each parameter follows: % % o display: Specifies a pointer to the Display structure; returned from % XOpenDisplay. % % o milliseconds: Specifies the number of milliseconds to delay before % returning. % % */ Export void XDelay(Display *display,const unsigned long milliseconds) { assert(display != (Display *) NULL); (void) XFlush(display); if (milliseconds == 0) return; #if !defined(vms) && !defined(macintosh) && !defined(WIN32) #ifdef sysv { #include (void) poll((struct pollfd *) NULL,0,(int) milliseconds); } #else { struct timeval timer; timer.tv_sec=(int) milliseconds/1000; timer.tv_usec=(int) (milliseconds % 1000)*1000; (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer); } #endif #endif #if defined(vms) { float timer; timer=milliseconds/1000.0; lib$wait(&timer); } #endif #if defined(WIN32) Sleep(milliseconds); #endif } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X D e s t r o y W i n d o w C o l o r s % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XDestroyWindowColors frees X11 color resources previously saved on % a window by XRetainWindowColors or programs like xsetroot. % % The format of the XDestroyWindowColors routine is: % % XDestroyWindowColors(display,window) % % A description of each parameter follows: % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o window: Specifies a pointer to a Window structure. % % */ Export void XDestroyWindowColors(Display *display,Window window) { Atom property, type; int format, status; unsigned char *data; unsigned long after, length; /* If there are previous resources on the root window, destroy them. */ assert(display != (Display *) NULL); property=XInternAtom(display,"_XSETROOT_ID",False); if (property == (Atom) NULL) { Warning("Unable to create X property","_XSETROOT_ID"); return; } status=XGetWindowProperty(display,window,property,0L,1L,True, (Atom) AnyPropertyType,&type,&format,&length,&after,&data); if (status != Success) return; if ((type == XA_PIXMAP) && (format == 32) && (length == 1) && (after == 0)) { XKillClient(display,(XID) (*((Pixmap *) data))); XDeleteProperty(display,window,property); } if (type != None) XFree((void *) data); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X D i s p l a y I m a g e I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XDisplayImageInfo displays information about an X image. % % The format of the XDisplayImageInfo routine is: % % XDisplayImageInfo(display,resource_info,windows,undo_image,image) % % A description of each parameter follows: % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o resource_info: Specifies a pointer to a X11 XResourceInfo structure. % % o windows: Specifies a pointer to a XWindows structure. % % o undo_image: Specifies a pointer to a Image structure; returned from % ReadImage. % % o image: Specifies a pointer to a Image structure; returned from % ReadImage. % % */ Export void XDisplayImageInfo(Display *display, const XResourceInfo *resource_info,XWindows *windows,Image *undo_image, Image *image) { char *text, **textlist, title[MaxTextExtent]; int length; Image *p; register int i; unsigned int bytes, count, levels; /* Display information about the image in the Text View widget. */ assert(display != (Display *) NULL); assert(resource_info != (XResourceInfo *) NULL); assert(windows != (XWindows *) NULL); assert(image != (Image *) NULL); length=50*MaxTextExtent; if (image->directory != (char *) NULL) length+=Extent(image->directory); if (image->comments != (char *) NULL) length+=Extent(image->comments); text=(char *) malloc(length*sizeof(char)); if (text == (char *) NULL) { XNoticeWidget(display,windows,"Unable to display image info:", "Memory allocation failed"); return; } *text='\0'; /* Display info about the X server. */ (void) sprintf(title," Image Info: %s",image->filename); if (resource_info->gamma_correct) if (resource_info->display_gamma != (char *) NULL) (void) sprintf(text,"%sDisplay\n gamma: %s\n\n",text, resource_info->display_gamma); /* Display info about the X image. */ (void) sprintf(text,"%sX\n visual: %s\n",text, XVisualClassName(windows->image.class)); (void) sprintf(text,"%s depth: %d\n",text,windows->image.ximage->depth); if (windows->image.visual_info->colormap_size != 0) (void) sprintf(text,"%s colormap size: %d\n",text, windows->image.visual_info->colormap_size); if (resource_info->colormap== SharedColormap) (void) strcat(text," colormap type: Shared\n"); else (void) strcat(text," colormap type: Private\n"); (void) sprintf(text,"%s geometry: %dx%d\n",text, windows->image.ximage->width,windows->image.ximage->height); if (windows->image.crop_geometry != (char *) NULL) (void) sprintf(text,"%s crop geometry: %s\n",text, windows->image.crop_geometry); if (windows->image.pixmap == (Pixmap) NULL) (void) strcat(text," type: X Image\n"); else (void) strcat(text," type: Pixmap\n"); if (windows->image.shared_memory) (void) strcat(text," shared memory: True\n"); else (void) strcat(text," shared memory: False\n"); (void) strcat(text,"\n"); /* Display info about the undo image cache. */ bytes=0; for (levels=0; undo_image != (Image *) NULL; levels++) { bytes+=undo_image->list->packets*sizeof(RunlengthPacket); undo_image=undo_image->previous; } (void) sprintf(text,"%sUndo Edit Cache\n levels: %u\n",text,levels); (void) sprintf(text,"%s bytes: %umb\n",text,(bytes+(1 << 19)) >> 20); (void) sprintf(text,"%s limit: %umb\n\n",text,resource_info->undo_cache); /* Display info about the image. */ (void) sprintf(text,"%sImage\n file: %s\n",text,image->filename); if (image->class == DirectClass) (void) strcat(text," class: DirectClass\n"); else (void) strcat(text," class: PseudoClass\n"); if (image->class == PseudoClass) { ColorPacket *p; register int i; register XColorlist *q; /* Display image colormap. */ if (image->total_colors <= image->colors) (void) sprintf(text,"%s colors: %u\n",text,image->colors); else (void) sprintf(text,"%s colors: %lu=>%u\n",text,image->total_colors, image->colors); p=image->colormap; for (i=0; i < image->colors; i++) { (void) sprintf(text,"%s %d: (%3d,%3d,%3d) #%02x%02x%02x", text,i,p->red,p->green,p->blue,(unsigned int) p->red, (unsigned int) p->green,(unsigned int) p->blue); for (q=Colorlist; q->name != (char *) NULL; q++) if ((DownScale(p->red) == q->red) && (DownScale(p->green) == q->green) && (DownScale(p->blue) == q->blue)) (void) sprintf(text,"%s %s",text,q->name); (void) strcat(text,"\n"); p++; } } if (image->signature != (char *) NULL) (void) sprintf(text,"%s signature: %s\n",text,image->signature); if (image->matte) (void) strcat(text," matte: True\n"); else (void) strcat(text," matte: False\n"); if (image->gamma != 0.0) (void) sprintf(text,"%s gamma: %f\n",text,image->gamma); if (image->packets < (image->columns*image->rows)) (void) sprintf(text,"%s runlength packets: %u of %u\n",text,image->packets, image->columns*image->rows); (void) sprintf(text,"%s geometry: %ux%u\n",text, image->columns,image->rows); if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0)) { /* Display image resolution. */ (void) sprintf(text,"%s resolution: %gx%g",text, image->x_resolution,image->y_resolution); if (image->units == UndefinedResolution) (void) sprintf(text,"%s pixels\n",text); else if (image->units == PixelsPerInchResolution) (void) sprintf(text,"%s pixels/inch\n",text); else if (image->units == PixelsPerCentimeterResolution) (void) sprintf(text,"%s pixels/centimeter\n",text); else (void) sprintf(text,"%s\n",text); } (void) sprintf(text,"%s depth: %u\n",text,image->depth); if (image->filesize != 0) if (image->filesize >= (1 << 24)) (void) sprintf(text,"%s filesize: %ldmb\n",text, image->filesize/1024/1024); else if (image->filesize >= (1 << 14)) (void) sprintf(text,"%s filesize: %ldkb\n",text,image->filesize/1024); else (void) sprintf(text,"%s filesize: %ldb\n",text,image->filesize); if (image->interlace) (void) strcat(text," interlaced: True\n"); else (void) strcat(text," interlaced: False\n"); if (image->page != (char *) NULL) (void) sprintf(text,"%s page geometry: %s\n",text,image->page); if (image->dispose) (void) sprintf(text,"%s dispose method: %d\n",text,image->dispose); if (image->delay) (void) sprintf(text,"%s delay: %d\n",text,image->delay); if (image->iterations != 1) (void) sprintf(text,"%s iterations: %d\n",text,image->iterations); (void) sprintf(text,"%s format: %s\n",text,image->magick); p=image; while (p->previous != (Image *) NULL) p=p->previous; for (count=1; p->next != (Image *) NULL; count++) p=p->next; if (count > 1) (void) sprintf(text,"%s scene: %u of %u\n",text,image->scene,count); else if (image->scene != 0) (void) sprintf(text,"%s scene: %u\n",text,image->scene); if (image->label != (char *) NULL) (void) sprintf(text,"%s label: %s\n",text,image->label); if (image->comments != (char *) NULL) { /* Display image comment. */ (void) sprintf(text,"%s comments:\n",text); textlist=StringToList(image->comments); if (textlist != (char **) NULL) { for (i=0; textlist[i] != (char *) NULL; i++) { (void) sprintf(text,"%s %s\n",text,textlist[i]); free(textlist[i]); } free((char *) textlist); } } if (image->montage != (char *) NULL) (void) sprintf(text,"%s montage: %s\n",text,image->montage); if (image->directory != (char *) NULL) { /* Display image directory. */ (void) sprintf(text,"%s directory:\n",text); textlist=StringToList(image->directory); if (textlist != (char **) NULL) { for (i=0; textlist[i] != (char *) NULL; i++) { (void) sprintf(text,"%s %s\n",text,textlist[i]); free(textlist[i]); } free((char *) textlist); } } textlist=StringToList(text); if (textlist != (char **) NULL) { register int i; XTextViewWidget(display,resource_info,windows,True,title,textlist); for (i=0; textlist[i] != (char *) NULL; i++) free((char *) textlist[i]); free((char *) textlist); } free((char *) text); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % X D i t h e r I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XDitherImage dithers the reference image as required by the HP % Color Recovery algorithm. The color values are quantized to 3 bits of red % and green, and 2 bits of blue (3/3/2) and can be used as indices into a % 8-bit X standard colormap. % % The format of the XDitherImage routine is: % % XDitherImage(image,ximage) % % A description of each parameter follows: % % o image: Specifies a pointer to a Image structure; returned from % ReadImage. % % o ximage: Specifies a pointer to a XImage structure; returned from % XCreateImage. % % */ static void XDitherImage(Image *image,XImage *ximage) { static short int dither_red[2][16]= { {-16, 4, -1, 11,-14, 6, -3, 9,-15, 5, -2, 10,-13, 7, -4, 8}, { 15, -5, 0,-12, 13, -7, 2,-10, 14, -6, 1,-11, 12, -8, 3, -9} }, dither_green[2][16]= { { 11,-15, 7, -3, 8,-14, 4, -2, 10,-16, 6, -4, 9,-13, 5, -1}, {-12, 14, -8, 2, -9, 13, -5, 1,-11, 15, -7, 3,-10, 12, -6, 0} }, dither_blue[2][16]= { { -3, 9,-13, 7, -1, 11,-15, 5, -4, 8,-14, 6, -2, 10,-16, 4}, { 2,-10, 12, -8, 0,-12, 14, -6, 3, -9, 13, -7, 1,-11, 15, -5} }; ColorPacket color; int value, y; register char *q; register int i, j, x; register RunlengthPacket *p; unsigned int scanline_pad; register unsigned long pixel; unsigned short *blue_map[2][16], *green_map[2][16], *red_map[2][16]; if (!UncompressImage(image)) return; /* Allocate and initialize dither maps. */ for (i=0; i < 2; i++) for (j=0; j < 16; j++) { red_map[i][j]=(unsigned short *) malloc(256*sizeof(unsigned short)); green_map[i][j]=(unsigned short *) malloc(256*sizeof(unsigned short)); blue_map[i][j]=(unsigned short *) malloc(256*sizeof(unsigned short)); if ((red_map[i][j] == (unsigned short *) NULL) || (green_map[i][j] == (unsigned short *) NULL) || (blue_map[i][j] == (unsigned short *) NULL)) { Warning("Unable to dither image","Memory allocation failed"); return; } } /* Initialize dither tables. */ for (i=0; i < 2; i++) for (j=0; j < 16; j++) for (x=0; x < 256; x++) { value=x-16; if (x < 48) value=x/2+8; value+=dither_red[i][j]; red_map[i][j][x]=(unsigned short) ((value < 0) ? 0 : (value > MaxRGB) ? MaxRGB : value); value=x-16; if (x < 48) value=x/2+8; value+=dither_green[i][j]; green_map[i][j][x]=(unsigned short) ((value < 0) ? 0 : (value > MaxRGB) ? MaxRGB : value); value=x-32; if (x < 112) value=x/2+24; value+=(dither_blue[i][j] << 1); blue_map[i][j][x]=(unsigned short) ((value < 0) ? 0 : (value > MaxRGB) ? MaxRGB : value); } /* Dither image. */ scanline_pad=ximage->bytes_per_line- ((ximage->width*ximage->bits_per_pixel) >> 3); i=0; j=0; p=image->pixels; q=ximage->data; for (y=0; y < image->rows; y++) { for (x=0; x < image->columns; x++) { color.red=red_map[i][j][p->red]; color.green=green_map[i][j][p->green]; color.blue=blue_map[i][j][p->blue]; pixel=(unsigned long) ((color.red & 0xe0) | ((color.green & 0xe0) >> 3) | ((color.blue & 0xc0) >> 6)); *q++=(unsigned char) pixel; p++; j++; if (j == 16) j=0; } q+=scanline_pad; i++; if (i == 2) i=0; } /* Free allocated memory. */ for (i=0; i < 2; i++) for (j=0; j < 16; j++) { free((char *) green_map[i][j]); free((char *) blue_map[i][j]); free((char *) red_map[i][j]); } } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X D r a w I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XDrawImage draws a line on the image. % % The format of the XDrawImage routine is: % % status=XDrawImage(display,pixel_info,draw_info,image) % % A description of each parameter follows: % % o status: Function XDrawImage returns True if the image is % successfully drawd with text. False is returned is there is a % memory shortage. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o pixel_info: Specifies a pointer to a XPixelInfo structure. % % o draw_info: Specifies a pointer to a XDrawInfo structure. % % o image: Specifies a pointer to a Image structure; returned from % ReadImage. % % */ Export unsigned int XDrawImage(Display *display,const XPixelInfo *pixel_info, const XDrawInfo *draw_info,Image *image) { GC draw_context; Image *draw_image; int x, y; Pixmap draw_pixmap; register int i; register RunlengthPacket *p, *q; unsigned int depth, height, matte, width; Window root_window; XGCValues context_values; XImage *draw_ximage; /* Initialize drawd image. */ assert(display != (Display *) NULL); assert(pixel_info != (XPixelInfo *) NULL); assert(draw_info != (XDrawInfo *) NULL); assert(image != (Image *) NULL); if (!UncompressImage(image)) return(False); /* Initialize drawd pixmap. */ root_window=XRootWindow(display,XDefaultScreen(display)); depth=XDefaultDepth(display,XDefaultScreen(display)); draw_pixmap=XCreatePixmap(display,root_window,draw_info->width, draw_info->height,(int) depth); if (draw_pixmap == (Pixmap) NULL) return(False); /* Initialize graphics info. */ context_values.background=(unsigned long) (~0); context_values.foreground=0; context_values.line_width=draw_info->line_width; draw_context=XCreateGC(display,root_window,GCBackground | GCForeground | GCLineWidth,&context_values); if (draw_context == (GC) NULL) return(False); /* Clear pixmap. */ XFillRectangle(display,draw_pixmap,draw_context,0,0,draw_info->width, draw_info->height); /* Draw line to pixmap. */ XSetBackground(display,draw_context,0); XSetForeground(display,draw_context,(unsigned long) (~0)); XSetFillStyle(display,draw_context,FillOpaqueStippled); XSetStipple(display,draw_context,draw_info->stipple); switch (draw_info->primitive) { case PointPrimitive: default: { XDrawLines(display,draw_pixmap,draw_context,draw_info->coordinate_info, draw_info->number_coordinates,CoordModeOrigin); break; } case LinePrimitive: { XDrawLine(display,draw_pixmap,draw_context,draw_info->line_info.x1, draw_info->line_info.y1,draw_info->line_info.x2, draw_info->line_info.y2); break; } case RectanglePrimitive: { XDrawRectangle(display,draw_pixmap,draw_context, draw_info->rectangle_info.x,draw_info->rectangle_info.y, draw_info->rectangle_info.width,draw_info->rectangle_info.height); break; } case FillRectanglePrimitive: { XFillRectangle(display,draw_pixmap,draw_context, draw_info->rectangle_info.x,draw_info->rectangle_info.y, draw_info->rectangle_info.width,draw_info->rectangle_info.height); break; } case EllipsePrimitive: { XDrawArc(display,draw_pixmap,draw_context, draw_info->rectangle_info.x,draw_info->rectangle_info.y, draw_info->rectangle_info.width,draw_info->rectangle_info.height, 0,360*64); break; } case FillEllipsePrimitive: { XFillArc(display,draw_pixmap,draw_context, draw_info->rectangle_info.x,draw_info->rectangle_info.y, draw_info->rectangle_info.width,draw_info->rectangle_info.height, 0,360*64); break; } case PolygonPrimitive: { XPoint *coordinate_info; coordinate_info=draw_info->coordinate_info; XDrawLines(display,draw_pixmap,draw_context,coordinate_info, draw_info->number_coordinates,CoordModeOrigin); XDrawLine(display,draw_pixmap,draw_context, coordinate_info[draw_info->number_coordinates-1].x, coordinate_info[draw_info->number_coordinates-1].y, coordinate_info[0].x,coordinate_info[0].y); break; } case FillPolygonPrimitive: { XFillPolygon(display,draw_pixmap,draw_context,draw_info->coordinate_info, draw_info->number_coordinates,Complex,CoordModeOrigin); break; } } XFreeGC(display,draw_context); /* Initialize X image. */ draw_ximage=XGetImage(display,draw_pixmap,0,0,draw_info->width, draw_info->height,AllPlanes,ZPixmap); if (draw_ximage == (XImage *) NULL) return(False); XFreePixmap(display,draw_pixmap); /* Initialize draw image. */ draw_image=AllocateImage((ImageInfo *) NULL); if (draw_image == (Image *) NULL) return(False); draw_image->columns=draw_info->width; draw_image->rows=draw_info->height; draw_image->packets=draw_image->columns*draw_image->rows; draw_image->pixels=(RunlengthPacket *) malloc((unsigned int) image->packets*sizeof(RunlengthPacket)); if (draw_image->pixels == (RunlengthPacket *) NULL) { DestroyImage(draw_image); return(False); } /* Transfer drawn X image to image. */ (void) XParseGeometry(draw_info->geometry,&x,&y,&width,&height); q=image->pixels+y*image->columns+x; draw_image->background_color.red=q->red; draw_image->background_color.green=q->green; draw_image->background_color.blue=q->blue; draw_image->background_color.index=0; draw_image->matte=True; p=draw_image->pixels; for (y=0; y < draw_image->rows; y++) for (x=0; x < draw_image->columns; x++) { p->index=(unsigned short) XGetPixel(draw_ximage,x,y); if (p->index == 0) { /* Set this pixel to the background color. */ p->red=draw_image->background_color.red; p->green=draw_image->background_color.green; p->blue=draw_image->background_color.blue; p->index=draw_info->stencil == OpaqueStencil ? Transparent : Opaque; } else { /* Set this pixel to the pen color. */ p->red=XDownScale(pixel_info->pen_color.red); p->green=XDownScale(pixel_info->pen_color.green); p->blue=XDownScale(pixel_info->pen_color.blue); p->index=draw_info->stencil == OpaqueStencil ? Opaque : Transparent; } p->length=0; p++; } XDestroyImage(draw_ximage); /* Determine draw geometry. */ (void) XParseGeometry(draw_info->geometry,&x,&y,&width,&height); if ((width != draw_image->columns) || (height != draw_image->rows)) { char image_geometry[MaxTextExtent]; /* Scale image. */ (void) sprintf(image_geometry,"%ux%u",width,height); TransformImage(&draw_image,(char *) NULL,image_geometry); } if (draw_info->degrees != 0.0) { double normalized_degrees; Image *rotated_image; int rotations; /* Rotate image. */ rotated_image=RotateImage(draw_image,draw_info->degrees,False,True); if (rotated_image == (Image *) NULL) return(False); DestroyImage(draw_image); draw_image=rotated_image; /* Annotation is relative to the degree of rotation. */ normalized_degrees=draw_info->degrees; while (normalized_degrees < -45.0) normalized_degrees+=360.0; for (rotations=0; normalized_degrees > 45.0; rotations++) normalized_degrees-=90.0; switch (rotations % 4) { default: case 0: break; case 1: { /* Rotate 90 degrees. */ x-=draw_image->columns >> 1; y+=draw_image->columns >> 1; break; } case 2: { /* Rotate 180 degrees. */ x-=draw_image->columns; break; } case 3: { /* Rotate 270 degrees. */ x-=draw_image->columns >> 1; y-=draw_image->rows-(draw_image->columns >> 1); break; } } } /* Composite text onto the image. */ p=draw_image->pixels; for (i=0; i < draw_image->packets; i++) { if (p->index != Transparent) p->index=Opaque; p++; } if (draw_info->stencil == TransparentStencil) CompositeImage(image,MatteReplaceCompositeOp,draw_image,x,y); else { matte=image->matte; CompositeImage(image,OverCompositeOp,draw_image,x,y); image->matte=matte; } DestroyImage(draw_image); return(True); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X E r r o r % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XError ignores BadWindow errors for XQueryTree and % XGetWindowAttributes, and ignores BadDrawable errors for XGetGeometry, and % ignores BadValue errors for XQueryColor. It returns False in those cases. % Otherwise it returns True. % % The format of the XError function is: % % XError(display,error) % % A description of each parameter follows: % % o display: Specifies a pointer to the Display structure; returned from % XOpenDisplay. % % o error: Specifies the error event. % % */ Export int XError(Display *display,XErrorEvent *error) { assert(display != (Display *) NULL); assert(error != (XErrorEvent *) NULL); xerror_alert=True; switch (error->request_code) { case X_GetGeometry: { if (error->error_code == BadDrawable) return(False); break; } case X_GetWindowAttributes: case X_QueryTree: { if (error->error_code == BadWindow) return(False); break; } case X_QueryColors: { if (error->error_code == BadValue) return(False); break; } } return(True); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X F r e e R e s o u r c e s % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XFreeResources frees X11 resources. % % The format of the XFreeResources routine is: % % XFreeResources(display,visual_info,map_info,pixel_info,font_info, % resource_info,window_info) % % A description of each parameter follows: % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o visual_info: Specifies a pointer to a X11 XVisualInfo structure; % returned from XGetVisualInfo. % % o map_info: If map_type is specified, this structure is initialized % with info from the Standard Colormap. % % o pixel_info: Specifies a pointer to a XPixelInfo structure. % % o font_info: Specifies a pointer to a XFontStruct structure. % % o resource_info: Specifies a pointer to a X11 XResourceInfo structure. % % o window_info: Specifies a pointer to a X11 XWindowInfo structure. % % */ void XFreeResources(Display *display,XVisualInfo *visual_info, XStandardColormap *map_info,XPixelInfo *pixel_info,XFontStruct *font_info, XResourceInfo *resource_info,XWindowInfo *window_info) { assert(display != (Display *) NULL); assert(resource_info != (XResourceInfo *) NULL); if (window_info != (XWindowInfo *) NULL) { /* Free X image. */ if (window_info->ximage != (XImage *) NULL) XDestroyImage(window_info->ximage); if (window_info->id != (Window) NULL) { /* Free destroy window and free cursors. */ if (window_info->id != XRootWindow(display,visual_info->screen)) XDestroyWindow(display,window_info->id); if (window_info->annotate_context != (GC) NULL) XFreeGC(display,window_info->annotate_context); if (window_info->highlight_context != (GC) NULL) XFreeGC(display,window_info->highlight_context); if (window_info->widget_context != (GC) NULL) XFreeGC(display,window_info->widget_context); XFreeCursor(display,window_info->cursor); XFreeCursor(display,window_info->busy_cursor); } } /* Free font. */ if (font_info != (XFontStruct *) NULL) XFreeFont(display,font_info); if (map_info != (XStandardColormap *) NULL) { /* Free X Standard Colormap. */ if (resource_info->map_type == (char *) NULL) XFreeStandardColormap(display,visual_info,map_info,pixel_info); XFree((void *) map_info); } /* Free X visual info. */ if (visual_info != (XVisualInfo *) NULL) XFree((void *) visual_info); if (resource_info->close_server) XCloseDisplay(display); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X F r e e S t a n d a r d C o l o r m a p % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XFreeStandardColormap frees an X11 colormap. % % The format of the XFreeStandardColormap routine is: % % XFreeStandardColormap(display,visual_info,map_info,pixel_info) % % A description of each parameter follows: % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o visual_info: Specifies a pointer to a X11 XVisualInfo structure; % returned from XGetVisualInfo. % % o map_info: If map_type is specified, this structure is initialized % with info from the Standard Colormap. % % o pixel_info: Specifies a pointer to a XPixelInfo structure. % % */ Export void XFreeStandardColormap(Display *display, const XVisualInfo *visual_info,XStandardColormap *map_info, XPixelInfo *pixel_info) { /* Free colormap. */ assert(display != (Display *) NULL); assert(visual_info != (XVisualInfo *) NULL); assert(map_info != (XStandardColormap *) NULL); XFlush(display); if (map_info->colormap != (Colormap) NULL) if (map_info->colormap != XDefaultColormap(display,visual_info->screen)) XFreeColormap(display,map_info->colormap); else if (pixel_info != (XPixelInfo *) NULL) if ((visual_info->class != TrueColor) && (visual_info->class != DirectColor)) XFreeColors(display,map_info->colormap,pixel_info->pixels, (int) pixel_info->colors,0); map_info->colormap=(Colormap) NULL; if (pixel_info != (XPixelInfo *) NULL) { if (pixel_info->gamma_map != (XColor *) NULL) free((char *) pixel_info->gamma_map); pixel_info->gamma_map=(XColor *) NULL; if (pixel_info->pixels != (unsigned long *) NULL) free((char *) pixel_info->pixels); pixel_info->pixels=(unsigned long *) NULL; } } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t A n n o t a t e I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetAnnotateInfo initializes the AnnotateInfo structure. % % The format of the GetAnnotateInfo routine is: % % XGetAnnotateInfo(annotate_info) % % A description of each parameter follows: % % o annotate_info: Specifies a pointer to a XAnnotateInfo structure. % % */ Export void XGetAnnotateInfo(XAnnotateInfo *annotate_info) { /* Initialize annotate structure. */ assert(annotate_info != (XAnnotateInfo *) NULL); annotate_info->x=0; annotate_info->y=0; annotate_info->width=0; annotate_info->height=0; annotate_info->stencil=ForegroundStencil; annotate_info->degrees=0.0; annotate_info->font_info=(XFontStruct *) NULL; annotate_info->text=(char *) NULL; *annotate_info->geometry='\0'; annotate_info->previous=(XAnnotateInfo *) NULL; annotate_info->next=(XAnnotateInfo *) NULL; } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t M a p I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetMapInfo initializes the XStandardColormap structure. % % The format of the XStandardColormap routine is: % % XGetMapInfo(visual_info,colormap,map_info) % % A description of each parameter follows: % % o colormap: Specifies the ID of the X server colormap. % % o visual_info: Specifies a pointer to a X11 XVisualInfo structure; % returned from XGetVisualInfo. % % o map_info: Specifies a pointer to a X11 XStandardColormap structure. % % */ void XGetMapInfo(const XVisualInfo *visual_info,const Colormap colormap, XStandardColormap *map_info) { /* Initialize map info. */ assert(visual_info != (XVisualInfo *) NULL); assert(map_info != (XStandardColormap *) NULL); map_info->colormap=colormap; map_info->red_max=visual_info->red_mask; map_info->red_mult=map_info->red_max != 0 ? 1 : 0; if (map_info->red_max != 0) while ((map_info->red_max & 0x01) == 0) { map_info->red_max>>=1; map_info->red_mult<<=1; } map_info->green_max=visual_info->green_mask; map_info->green_mult=map_info->green_max != 0 ? 1 : 0; if (map_info->green_max != 0) while ((map_info->green_max & 0x01) == 0) { map_info->green_max>>=1; map_info->green_mult<<=1; } map_info->blue_max=visual_info->blue_mask; map_info->blue_mult=map_info->blue_max != 0 ? 1 : 0; if (map_info->blue_max != 0) while ((map_info->blue_max & 0x01) == 0) { map_info->blue_max>>=1; map_info->blue_mult<<=1; } map_info->base_pixel=0; } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t M o n t a g e I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetMontageInfo initializes the MontageInfo structure. % % The format of the GetMontageInfo routine is: % % XGetMontageInfo(montage_info) % % A description of each parameter follows: % % o montage_info: Specifies a pointer to a MontageInfo structure. % % */ Export void XGetMontageInfo(XMontageInfo *montage_info) { assert(montage_info != (XMontageInfo *) NULL); *montage_info->filename='\0'; montage_info->shadow=False; montage_info->compose=ReplaceCompositeOp; montage_info->pointsize=atoi(DefaultPointSize); montage_info->frame=(char *) NULL; montage_info->tile=(char *) malloc(MaxTextExtent*sizeof(char)); if (montage_info->tile != (char *) NULL) (void) strcpy(montage_info->tile,DefaultTilePageGeometry); montage_info->texture=(char *) NULL; montage_info->filter=MitchellFilter; } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t P i x e l I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetPixelInfo initializes the PixelInfo structure. % % The format of the XGetPixelInfo routine is: % % XGetPixelInfo(display,visual_info,map_info,resource_info,image, % pixel_info) % % A description of each parameter follows: % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o visual_info: Specifies a pointer to a X11 XVisualInfo structure; % returned from XGetVisualInfo. % % o map_info: If map_type is specified, this structure is initialized % with info from the Standard Colormap. % % o resource_info: Specifies a pointer to a X11 XResourceInfo structure. % % o image: Specifies a pointer to a Image structure; returned from % ReadImage. % % o pixel_info: Specifies a pointer to a XPixelInfo structure. % % */ Export void XGetPixelInfo(Display *display,const XVisualInfo *visual_info, const XStandardColormap *map_info,const XResourceInfo *resource_info, Image *image,XPixelInfo *pixel_info) { static char *PenColors[MaxNumberPens]= { Pen1Color, Pen2Color, Pen3Color, Pen4Color, Pen5Color, Pen6Color, Pen7Color, Pen8Color, Pen9Color, Pen0Color, Pen0Color }; Colormap colormap; int status; register int i; unsigned int packets; /* Initialize pixel info. */ assert(display != (Display *) NULL); assert(visual_info != (XVisualInfo *) NULL); assert(map_info != (XStandardColormap *) NULL); assert(resource_info != (XResourceInfo *) NULL); assert(pixel_info != (XPixelInfo *) NULL); pixel_info->colors=0; if (image != (Image *) NULL) if (image->class == PseudoClass) pixel_info->colors=image->colors; packets=Max(pixel_info->colors,visual_info->colormap_size)+MaxNumberPens; if (pixel_info->pixels != (unsigned long *) NULL) free((char *) pixel_info->pixels); pixel_info->pixels=(unsigned long *) malloc(packets*sizeof(unsigned long)); if (pixel_info->pixels == (unsigned long *) NULL) Error("Unable to get pixel info","Memory allocation failed"); /* Set foreground color. */ colormap=map_info->colormap; (void) XParseColor(display,colormap,ForegroundColor, &pixel_info->foreground_color); status=XParseColor(display,colormap,resource_info->foreground_color, &pixel_info->foreground_color); if (status == 0) Warning("Color is not known to X server",resource_info->foreground_color); pixel_info->foreground_color.pixel= XStandardPixel(map_info,pixel_info->foreground_color,16); pixel_info->foreground_color.flags=DoRed | DoGreen | DoBlue; /* Set background color. */ (void) XParseColor(display,colormap,BackgroundColor, &pixel_info->background_color); status=XParseColor(display,colormap,resource_info->background_color, &pixel_info->background_color); if (status == 0) Warning("Color is not known to X server",resource_info->background_color); pixel_info->background_color.pixel= XStandardPixel(map_info,pixel_info->background_color,16); pixel_info->background_color.flags=DoRed | DoGreen | DoBlue; /* Set border color. */ (void) XParseColor(display,colormap,BorderColor,&pixel_info->border_color); status=XParseColor(display,colormap,resource_info->border_color, &pixel_info->border_color); if (status == 0) Warning("Color is not known to X server",resource_info->border_color); pixel_info->border_color.pixel= XStandardPixel(map_info,pixel_info->border_color,16); pixel_info->border_color.flags=DoRed | DoGreen | DoBlue; /* Set matte color. */ pixel_info->matte_color=pixel_info->background_color; if (resource_info->matte_color != (char *) NULL) { /* Matte color is specified as a X resource or command line argument. */ status=XParseColor(display,colormap,resource_info->matte_color, &pixel_info->matte_color); if (status == 0) Warning("Color is not known to X server",resource_info->matte_color); pixel_info->matte_color.pixel= XStandardPixel(map_info,pixel_info->matte_color,16); pixel_info->matte_color.flags=DoRed | DoGreen | DoBlue; } /* Set highlight color. */ pixel_info->highlight_color.red=(unsigned int) (pixel_info->matte_color.red* HighlightModulate+(MaxRGB-HighlightModulate)*65535L)/MaxRGB; pixel_info->highlight_color.green=(unsigned int) (pixel_info->matte_color.green*HighlightModulate+ (MaxRGB-HighlightModulate)*65535L)/MaxRGB; pixel_info->highlight_color.blue=(unsigned int) (pixel_info->matte_color.blue* HighlightModulate+(MaxRGB-HighlightModulate)*65535L)/MaxRGB; pixel_info->highlight_color.pixel= XStandardPixel(map_info,pixel_info->highlight_color,16); pixel_info->highlight_color.flags=DoRed | DoGreen | DoBlue; /* Set shadow color. */ pixel_info->shadow_color.red=(unsigned int) (pixel_info->matte_color.red*ShadowModulate)/MaxRGB; pixel_info->shadow_color.green=(unsigned int) (pixel_info->matte_color.green*ShadowModulate)/MaxRGB; pixel_info->shadow_color.blue=(unsigned int) (pixel_info->matte_color.blue*ShadowModulate)/MaxRGB; pixel_info->shadow_color.pixel= XStandardPixel(map_info,pixel_info->shadow_color,16); pixel_info->shadow_color.flags=DoRed | DoGreen | DoBlue; /* Set depth color. */ pixel_info->depth_color.red=(unsigned int) (pixel_info->matte_color.red*DepthModulate)/MaxRGB; pixel_info->depth_color.green=(unsigned int) (pixel_info->matte_color.green*DepthModulate)/MaxRGB; pixel_info->depth_color.blue=(unsigned int) (pixel_info->matte_color.blue*DepthModulate)/MaxRGB; pixel_info->depth_color.pixel= XStandardPixel(map_info,pixel_info->depth_color,16); pixel_info->depth_color.flags=DoRed | DoGreen | DoBlue; /* Set trough color. */ pixel_info->trough_color.red=(unsigned int) (pixel_info->matte_color.red*TroughModulate)/MaxRGB; pixel_info->trough_color.green=(unsigned int) (pixel_info->matte_color.green*TroughModulate)/MaxRGB; pixel_info->trough_color.blue=(unsigned int) (pixel_info->matte_color.blue*TroughModulate)/MaxRGB; pixel_info->trough_color.pixel= XStandardPixel(map_info,pixel_info->trough_color,16); pixel_info->trough_color.flags=DoRed | DoGreen | DoBlue; /* Set pen color. */ for (i=0; i < MaxNumberPens; i++) { (void) XParseColor(display,colormap,PenColors[i], &pixel_info->pen_colors[i]); status=XParseColor(display,colormap,resource_info->pen_colors[i], &pixel_info->pen_colors[i]); if (status == 0) Warning("Color is not known to X server",resource_info->pen_colors[i]); pixel_info->pen_colors[i].pixel= XStandardPixel(map_info,pixel_info->pen_colors[i],16); pixel_info->pen_colors[i].flags=DoRed | DoGreen | DoBlue; } pixel_info->box_color=pixel_info->background_color; pixel_info->pen_color=pixel_info->foreground_color; pixel_info->box_index=0; pixel_info->pen_index=1; /* Initialize gamma map to linear brightness. */ if (pixel_info->gamma_map != (XColor *) NULL) free((char *) pixel_info->gamma_map); pixel_info->gamma_map=(XColor *) malloc((MaxRGB+1)*sizeof(XColor)); if (pixel_info->gamma_map == (XColor *) NULL) Error("Unable to get pixel info","Memory allocation failed"); for (i=0; i <= MaxRGB; i++) { pixel_info->gamma_map[i].red=i; pixel_info->gamma_map[i].green=i; pixel_info->gamma_map[i].blue=i; } if (image != (Image *) NULL) { if (resource_info->gamma_correct && (image->gamma != 0.0)) { double blue_gamma, green_gamma, red_gamma; int count; /* Initialize map relative to display and image gamma. */ red_gamma=1.0; green_gamma=1.0; blue_gamma=1.0; count=sscanf(resource_info->display_gamma,"%lf,%lf,%lf", &red_gamma,&green_gamma,&blue_gamma); if (count == 1) { green_gamma=red_gamma; blue_gamma=red_gamma; } red_gamma*=image->gamma; green_gamma*=image->gamma; blue_gamma*=image->gamma; for (i=0; i <= MaxRGB; i++) { pixel_info->gamma_map[i].red=(short unsigned int) ((pow((double) i/MaxRGB,1.0/red_gamma)*MaxRGB)+0.5); pixel_info->gamma_map[i].green=(short unsigned int) ((pow((double) i/MaxRGB,1.0/green_gamma)*MaxRGB)+0.5); pixel_info->gamma_map[i].blue=(short unsigned int) ((pow((double) i/MaxRGB,1.0/blue_gamma)*MaxRGB)+0.5); } } if (image->class == PseudoClass) { register XColor *gamma_map; /* Initialize pixel array for images of type PseudoClass. */ gamma_map=pixel_info->gamma_map; for (i=0; i < image->colors; i++) pixel_info->pixels[i]= XGammaPixel(map_info,gamma_map,image->colormap+i,QuantumDepth); for (i=0; i < MaxNumberPens; i++) pixel_info->pixels[image->colors+i]=pixel_info->pen_colors[i].pixel; pixel_info->colors+=MaxNumberPens; } } } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t R e s o u r c e C l a s s % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetResourceClass queries the X server for the specified resource % name or class. If the resource name or class is not defined in the % database, the supplied default value is returned. % % The format of the XGetResourceClass routine is: % % value=XGetResourceClass(database,client_name,keyword,resource_default) % % A description of each parameter follows: % % o value: Function XGetResourceClass returns the resource value associated % with the name or class. If none is found, the supplied default value is % returned. % % o database: Specifies a resource database; returned from % XrmGetStringDatabase. % % o client_name: Specifies the application name used to retrieve resource % info from the X server database. % % o keyword: Specifies the keyword of the value being retrieved. % % o resource_default: Specifies the default value to return if the query % fails to find the specified keyword/class. % % */ Export char *XGetResourceClass(XrmDatabase database,const char *client_name, const char *keyword,char *resource_default) { char resource_class[MaxTextExtent], resource_name[MaxTextExtent]; int status; static char *resource_type; XrmValue resource_value; if (database == (XrmDatabase) NULL) return(resource_default); *resource_name='\0'; *resource_class='\0'; if (keyword != (char *) NULL) { unsigned char c, k; /* Initialize resource keyword and class. */ (void) sprintf(resource_name,"%s.%s",client_name,keyword); c=(*client_name); if ((c >= XK_a) && (c <= XK_z)) c-=(XK_a-XK_A); else if ((c >= XK_agrave) && (c <= XK_odiaeresis)) c-=(XK_agrave-XK_Agrave); else if ((c >= XK_oslash) && (c <= XK_thorn)) c-=(XK_oslash-XK_Ooblique); k=(*keyword); if ((k >= XK_a) && (k <= XK_z)) k-=(XK_a-XK_A); else if ((k >= XK_agrave) && (k <= XK_odiaeresis)) k-=(XK_agrave-XK_Agrave); else if ((k >= XK_oslash) && (k <= XK_thorn)) k-=(XK_oslash-XK_Ooblique); (void) sprintf(resource_class,"%c%s.%c%s",c,client_name+1,k,keyword+1); } status=XrmGetResource(database,resource_name,resource_class,&resource_type, &resource_value); if (status == False) return(resource_default); return(resource_value.addr); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t R e s o u r c e D a t a b a s e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetResourceDatabase creates a new resource database and % initializes it. % % The format of the XGetResourceDatabase routine is: % % database=XGetResourceDatabase(display,client_name) % % A description of each parameter follows: % % o database: Function XGetResourceDatabase returns the database after % it is initialized. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o client_name: Specifies the application name used to retrieve resource % info from the X server database. % % */ Export XrmDatabase XGetResourceDatabase(Display *display,char *client_name) { char filename[MaxTextExtent]; register char *p; unsigned char c; XrmDatabase resource_database, server_database; if (display == (Display *) NULL) return((XrmDatabase) NULL); assert(client_name != (char *) NULL); /* Initialize resource database. */ XrmInitialize(); XGetDefault(display,client_name,"dummy"); resource_database=XrmGetDatabase(display); /* Combine application database. */ if (client_name != (char *) NULL) { /* Get basename of client. */ p=client_name+(Extent(client_name)-1); while ((p > client_name) && (*p != '/')) p--; if (*p == '/') client_name=p+1; } c=(*client_name); if ((c >= XK_a) && (c <= XK_z)) c-=(XK_a-XK_A); else if ((c >= XK_agrave) && (c <= XK_odiaeresis)) c-=(XK_agrave-XK_Agrave); else if ((c >= XK_oslash) && (c <= XK_thorn)) c-=(XK_oslash-XK_Ooblique); (void) sprintf(filename,"%s%c%s",ApplicationDefaults,c,client_name+1); XrmCombineFileDatabase(filename,&resource_database,False); if (XResourceManagerString(display) != (char *) NULL) { /* Combine server database. */ server_database=XrmGetStringDatabase(XResourceManagerString(display)); XrmCombineDatabase(server_database,&resource_database,False); } /* Merge user preferences database. */ (void) sprintf(filename,"%s%src",PreferencesDefaults,client_name); ExpandFilename(filename); XrmCombineFileDatabase(filename,&resource_database,False); return(resource_database); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t R e s o u r c e I n f o % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetResourceInfo initializes the ResourceInfo structure. % % The format of the XGetResourceInfo routine is: % % XGetResourceInfo(database,client_name,resource_info) % % A description of each parameter follows: % % o database: Specifies a resource database; returned from % XrmGetStringDatabase. % % o client_name: Specifies the application name used to retrieve % resource info from the X server database. % % o resource_info: Specifies a pointer to a X11 XResourceInfo structure. % % */ Export void XGetResourceInfo(XrmDatabase database,char *client_name, XResourceInfo *resource_info) { char *resource_value; /* Initialize resource info fields. */ assert(resource_info != (XResourceInfo *) NULL); resource_info->resource_database=database; resource_info->close_server=True; resource_info->client_name=client_name; resource_value=XGetResourceClass(database,client_name,"backdrop","False"); resource_info->backdrop=IsTrue(resource_value); resource_info->background_color=XGetResourceInstance(database,client_name, "background",BackgroundColor); resource_info->border_color=XGetResourceInstance(database,client_name, "borderColor",BorderColor); resource_value=XGetResourceClass(database,client_name,"borderWidth","2"); resource_info->border_width=atoi(resource_value); resource_info->browse_command=XGetResourceClass(database,client_name, "browseCommand",BrowseCommand); resource_value=XGetResourceClass(database,client_name,"colormap","shared"); resource_info->colormap=UndefinedColormap; if (Latin1Compare("private",resource_value) == 0) resource_info->colormap=PrivateColormap; if (Latin1Compare("shared",resource_value) == 0) resource_info->colormap=SharedColormap; if (resource_info->colormap == UndefinedColormap) Warning("Unrecognized colormap type",resource_value); resource_value= XGetResourceClass(database,client_name,"colorRecovery","False"); resource_info->color_recovery=IsTrue(resource_value); resource_value=XGetResourceClass(database,client_name,"colorspace","rgb"); resource_info->colorspace=UndefinedColorspace; if (Latin1Compare("gray",resource_value) == 0) resource_info->colorspace=GRAYColorspace; if (Latin1Compare("rgb",resource_value) == 0) resource_info->colorspace=RGBColorspace; if (Latin1Compare("ohta",resource_value) == 0) resource_info->colorspace=OHTAColorspace; if (Latin1Compare("xyz",resource_value) == 0) resource_info->colorspace=XYZColorspace; if (Latin1Compare("yiq",resource_value) == 0) resource_info->colorspace=YIQColorspace; if (Latin1Compare("yuv",resource_value) == 0) resource_info->colorspace=YUVColorspace; if (resource_info->colorspace == UndefinedColorspace) Warning("Unrecognized colorspace type",resource_value); resource_value=XGetResourceClass(database,client_name,"confirmExit","False"); resource_info->confirm_exit=IsTrue(resource_value); resource_value=XGetResourceClass(database,client_name,"debug","False"); resource_info->debug=IsTrue(resource_value); resource_value=XGetResourceClass(database,client_name,"delay","0"); resource_info->delay=atoi(resource_value); resource_info->display_gamma=XGetResourceClass(database,client_name, "displayGamma",DefaultDisplayGamma); resource_value= XGetResourceClass(database,client_name,"displayWarnings","True"); resource_info->display_warnings=IsTrue(resource_value); resource_value=XGetResourceClass(database,client_name,"dither","True"); resource_info->dither=IsTrue(resource_value); resource_info->editor_command=XGetResourceClass(database,client_name, "editorCommand",EditorCommand); resource_info->font= XGetResourceClass(database,client_name,"font",(char *) NULL); resource_info->font=XGetResourceClass(database,client_name,"fontList", resource_info->font); resource_info->font_name[0]= XGetResourceClass(database,client_name,"font1","fixed"); resource_info->font_name[1]= XGetResourceClass(database,client_name,"font2","variable"); resource_info->font_name[2]= XGetResourceClass(database,client_name,"font3","5x8"); resource_info->font_name[3]= XGetResourceClass(database,client_name,"font4","6x10"); resource_info->font_name[4]= XGetResourceClass(database,client_name,"font5","7x13bold"); resource_info->font_name[5]= XGetResourceClass(database,client_name,"font6","8x13bold"); resource_info->font_name[6]= XGetResourceClass(database,client_name,"font7","9x15bold"); resource_info->font_name[7]= XGetResourceClass(database,client_name,"font8","10x20"); resource_info->font_name[8]= XGetResourceClass(database,client_name,"font9","12x24"); resource_info->font_name[9]= XGetResourceClass(database,client_name,"font0","fixed"); resource_info->font_name[10]= XGetResourceClass(database,client_name,"font0","fixed"); resource_info->foreground_color=XGetResourceInstance(database,client_name, "foreground",ForegroundColor); resource_value=XGetResourceClass(database,client_name,"gammaCorrect","True"); resource_info->gamma_correct=IsTrue(resource_value); resource_value=XGetResourceClass(database,client_name,"gravity","South"); resource_info->gravity=(-1); if (Latin1Compare("Forget",resource_value) == 0) resource_info->gravity=ForgetGravity; if (Latin1Compare("NorthWest",resource_value) == 0) resource_info->gravity=NorthWestGravity; if (Latin1Compare("North",resource_value) == 0) resource_info->gravity=NorthGravity; if (Latin1Compare("NorthEast",resource_value) == 0) resource_info->gravity=NorthEastGravity; if (Latin1Compare("West",resource_value) == 0) resource_info->gravity=WestGravity; if (Latin1Compare("Center",resource_value) == 0) resource_info->gravity=CenterGravity; if (Latin1Compare("East",resource_value) == 0) resource_info->gravity=EastGravity; if (Latin1Compare("SouthWest",resource_value) == 0) resource_info->gravity=SouthWestGravity; if (Latin1Compare("South",resource_value) == 0) resource_info->gravity=SouthGravity; if (Latin1Compare("SouthEast",resource_value) == 0) resource_info->gravity=SouthEastGravity; if (Latin1Compare("Static",resource_value) == 0) resource_info->gravity=StaticGravity; if (resource_info->gravity == (-1)) { Warning("Unrecognized gravity type",resource_value); resource_info->gravity=CenterGravity; } (void) getcwd(resource_info->home_directory,MaxTextExtent-1); resource_info->icon_geometry=XGetResourceClass(database,client_name, "iconGeometry",(char *) NULL); resource_value=XGetResourceClass(database,client_name,"iconic","False"); resource_info->iconic=IsTrue(resource_value); resource_value=XGetResourceClass(database,client_name,"immutable","False"); resource_info->immutable=IsTrue(resource_value); resource_info->image_geometry=XGetResourceClass(database,client_name, "geometry",(char *) NULL); resource_info->launch_command=XGetResourceClass(database,client_name, "launchCommand",LauncherCommand); resource_value=XGetResourceClass(database,client_name,"magnify","3"); resource_info->magnify=atoi(resource_value); resource_info->map_type= XGetResourceClass(database,client_name,"map",(char *) NULL); resource_info->matte_color=XGetResourceInstance(database,client_name, "mattecolor",(char *) NULL); resource_value=XGetResourceClass(database,client_name,"monochrome","False"); resource_info->monochrome=IsTrue(resource_value); resource_info->name= XGetResourceClass(database,client_name,"name",(char *) NULL); resource_value=XGetResourceClass(database,client_name,"colors","0"); resource_info->number_colors=atoi(resource_value); resource_info->pen_colors[0]= XGetResourceClass(database,client_name,"pen1","black"); resource_info->pen_colors[1]= XGetResourceClass(database,client_name,"pen2","blue"); resource_info->pen_colors[2]= XGetResourceClass(database,client_name,"pen3","cyan"); resource_info->pen_colors[3]= XGetResourceClass(database,client_name,"pen4","green"); resource_info->pen_colors[4]= XGetResourceClass(database,client_name,"pen5","gray"); resource_info->pen_colors[5]= XGetResourceClass(database,client_name,"pen6","red"); resource_info->pen_colors[6]= XGetResourceClass(database,client_name,"pen7","magenta"); resource_info->pen_colors[7]= XGetResourceClass(database,client_name,"pen8","yellow"); resource_info->pen_colors[8]= XGetResourceClass(database,client_name,"pen9","white"); resource_info->pen_colors[9]= XGetResourceClass(database,client_name,"pen0","gray"); resource_info->pen_colors[10]= XGetResourceClass(database,client_name,"pen0","gray"); resource_info->print_command=XGetResourceClass(database,client_name, "printCommand",PrinterCommand); resource_value=XGetResourceClass(database,client_name,"quantum","1"); resource_info->quantum=atoi(resource_value); resource_info->server_name= XGetResourceClass(database,client_name,"serverName",(char *) NULL); resource_info->text_font= XGetResourceClass(database,client_name,"font",(char *) NULL); resource_info->text_font=XGetResourceClass(database,client_name, "textFontList",resource_info->text_font); resource_info->title= XGetResourceClass(database,client_name,"title",(char *) NULL); resource_value=XGetResourceClass(database,client_name,"treeDepth","0"); resource_info->tree_depth=atoi(resource_value); resource_value=XGetResourceClass(database,client_name,"undoCache",UndoCache); resource_info->undo_cache=atoi(resource_value); resource_value=XGetResourceClass(database,client_name,"update","False"); resource_info->update=IsTrue(resource_value); resource_value=XGetResourceClass(database,client_name,"usePixmap","False"); resource_info->use_pixmap=IsTrue(resource_value); resource_value= XGetResourceClass(database,client_name,"sharedMemory","True"); resource_info->use_shared_memory=IsTrue(resource_value); resource_info->visual_type= XGetResourceClass(database,client_name,"visual",(char *) NULL); resource_info->window_group=XGetResourceClass(database,client_name, "windowGroup",(char *) NULL); resource_info->window_id= XGetResourceClass(database,client_name,"window",(char *) NULL); resource_info->write_filename=XGetResourceClass(database,client_name, "writeFilename",(char *) NULL); /* Handle side-effects. */ if (resource_info->monochrome) { resource_info->number_colors=2; resource_info->tree_depth=8; resource_info->dither=True; resource_info->colorspace=GRAYColorspace; } if (resource_info->colorspace == GRAYColorspace) { resource_info->number_colors=256; resource_info->tree_depth=8; } } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t R e s o u r c e I n s t a n c e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetResourceInstance queries the X server for the specified % resource name. If the resource name is not defined in the database, the % supplied default value is returned. % % The format of the XGetResourceInstance routine is: % % value=XGetResourceInstance(database,client_name,keyword,resource_default) % % A description of each parameter follows: % % o value: Function XGetResourceInstance returns the resource value % associated with the name or class. If none is found, the supplied % default value is returned. % % o database: Specifies a resource database; returned from % XrmGetStringDatabase. % % o client_name: Specifies the application name used to retrieve % resource info from the X server database. % % o keyword: Specifies the keyword of the value being retrieved. % % o resource_default: Specifies the default value to return if the query % fails to find the specified keyword/class. % % */ Export char *XGetResourceInstance(XrmDatabase database,const char *client_name, const char *keyword,char *resource_default) { char *resource_type, resource_name[MaxTextExtent]; int status; XrmValue resource_value; if (database == (XrmDatabase) NULL) return(resource_default); *resource_name='\0'; if (keyword != (char *) NULL) (void) sprintf(resource_name,"%s.%s",client_name,keyword); status=XrmGetResource(database,resource_name,"ImageMagick",&resource_type, &resource_value); if (status == False) return(resource_default); return(resource_value.addr); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t S u b w i n d o w % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetSubwindow returns the subwindow of a window choosen the % user with the pointer and a button press. % % The format of the XGetSubwindow routine is: % % subwindow=XGetSubwindow(display,window,x,y) % % A description of each parameter follows: % % o subwindow: Function XGetSubwindow returns NULL if no subwindow is found % otherwise the subwindow is returned. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o window: Specifies a pointer to a Window. % % o x: the x coordinate of the pointer relative to the origin of the % window. % % o y: the y coordinate of the pointer relative to the origin of the % window. % % */ Window XGetSubwindow(Display *display,Window window,int x,int y) { Window source_window, target_window; int status, x_offset, y_offset; assert(display != (Display *) NULL); source_window=XRootWindow(display,XDefaultScreen(display)); if (window == (Window) NULL) return(source_window); target_window=window; for ( ; ; ) { status=XTranslateCoordinates(display,source_window,window,x,y, &x_offset,&y_offset,&target_window); if (status != True) break; if (target_window == (Window) NULL) break; source_window=window; window=target_window; x=x_offset; y=y_offset; } if (target_window == (Window) NULL) target_window=window; return(target_window); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t W i n d o w C o l o r % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetWindowColor returns the color of a pixel interactively choosen % from the X server. % % The format of the XGetWindowColor routine is: % % status=XGetWindowColor(display,name) % % A description of each parameter follows: % % o status: Function XGetWindowColor returns True if the color is obtained % from the X server. False is returned if any errors occurs. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o name: The name of of the color if found in the X Color Database is % returned in this character string. % % */ unsigned int XGetWindowColor(Display *display,char *name) { FILE *database; int x, y; RectangleInfo crop_info; unsigned int status; Window child, client_window, root_window, target_window; XColor color; XImage *ximage; XWindowAttributes window_attributes; /* Choose a pixel from the X server. */ assert(display != (Display *) NULL); assert(name != (char *) NULL); target_window=XSelectWindow(display,&crop_info); root_window=XRootWindow(display,XDefaultScreen(display)); client_window=target_window; if (target_window != root_window) { unsigned int d; /* Get client window. */ status=XGetGeometry(display,target_window,&root_window,&x,&x,&d,&d,&d,&d); if (status != 0) { client_window=XClientWindow(display,target_window); target_window=client_window; } } /* Verify window is viewable. */ status=XGetWindowAttributes(display,target_window,&window_attributes); if ((status == False) || (window_attributes.map_state != IsViewable)) return(False); /* Get window X image. */ XTranslateCoordinates(display,root_window,target_window,crop_info.x, crop_info.y,&x,&y,&child); ximage=XGetImage(display,target_window,x,y,1,1,AllPlanes,ZPixmap); if (ximage == (XImage *) NULL) return(False); color.pixel=XGetPixel(ximage,0,0); XDestroyImage(ximage); /* Query X server for pixel color. */ XQueryColor(display,window_attributes.colormap,&color); (void) sprintf(name,"#%02x%02x%02x",XDownScale(color.red), XDownScale(color.green),XDownScale(color.blue)); database=fopen(RGBColorDatabase,"r"); if (database != (FILE *) NULL) { char colorname[MaxTextExtent], text[MaxTextExtent]; int blue, count, green, red; /* Match color against the X color database. */ while (fgets(text,MaxTextExtent,database) != (char *) NULL) { count=sscanf(text,"%d %d %d %[^\n]\n",&red,&green,&blue,colorname); if (count != 4) continue; if ((red == XDownScale(color.red)) && (green == XDownScale(color.green)) && (blue == XDownScale(color.blue))) { (void) strcpy(name,colorname); break; } } (void) fclose(database); } return(True); } /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % X G e t W i n d o w I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Function XGetWindowImage reads an image from the target X window and returns % it. XGetWindowImage optionally descends the window hierarchy and overlays % the target image with each subwindow image. % % The format of the XGetWindowImage routine is: % % image=XGetWindowImage(display,window,borders,level) % % A description of each parameter follows: % % o image: Function XGetWindowImage returns a MIFF image if it can be % successfully read from the X window. A null image is returned if % any errors occurs. % % o display: Specifies a connection to an X server; returned from % XOpenDisplay. % % o window: Specifies the window to obtain the image from. % % o borders: Specifies whether borders pixels are to be saved with % the image. % % o level: Specifies an unsigned integer representing the level of % decent in the window hierarchy. This value must be zero or one on % the initial call to XGetWindowImage. A value of zero returns after % one call. A value of one causes the function to descend the window % hierarchy and overlays the target image with each subwindow image. % % */ Image *XGetWindowImage(Display *display,const Window window, const unsigned int borders,const unsigned int level) { #define LoadImageText " Loading image... " typedef struct _ColormapList { Colormap colormap; XColor *colors; struct _ColormapList *next; } ColormapList; GC annotate_context; Image *image; int display_height, display_width, number_colors, status, x_offset, y_offset; RectangleInfo crop_info; register int i, x, y; register RunlengthPacket *p; register unsigned long pixel; static ColormapList *colormap_list = (ColormapList *) NULL; Window child, root_window; XColor *colors; XGCValues context_values; XImage *ximage; XWindowAttributes window_attributes; /* Verify window is viewable. */ assert(display != (Display *) NULL); status=XGetWindowAttributes(display,window,&window_attributes); if ((status == False) || (window_attributes.map_state != IsViewable)) return((Image *) NULL); /* Cropping rectangle is relative to root window. */ root_window=XRootWindow(display,XDefaultScreen(display)); XTranslateCoordinates(display,window,root_window,0,0,&x_offset,&y_offset, &child); crop_info.x=x_offset; crop_info.y=y_offset; crop_info.width=window_attributes.width; crop_info.height=window_attributes.height; if (borders) { /* Include border in image. */ crop_info.x-=window_attributes.border_width; crop_info.y-=window_attributes.border_width; crop_info.width+=window_attributes.border_width << 1; crop_info.height+=window_attributes.border_width << 1; } /* Crop to root window. */ if (crop_info.x < 0) { if ((crop_info.x+(int) crop_info.width) < 0) return((Image *) NULL); crop_info.width+=crop_info.x; crop_info.x=0; } if (crop_info.y < 0) { if ((crop_info.y+(int) crop_info.height) < 0) return((Image *) NULL); crop_info.height+=crop_info.y; crop_info.y=0; } display_width=XDisplayWidth(display,XDefaultScreen(display)); if ((crop_info.x+(int) crop_info.width) > display_width) { if (crop_info.x >= display_width) return((Image *) NULL); crop_info.width=display_width-crop_info.x; } display_height=XDisplayHeight(display,XDefaultScreen(display)); if ((crop_info.y+(int) crop_info.height) > display_height) { if (crop_info.y >= display_height) return((Image *) NULL); crop_info.height=display_height-crop_info.y; } crop_info.x-=x_offset; crop_info.y-=y_offset; /* Alert user we are about to get an X region by flashing a border. */ context_values.background=XBlackPixel(display,XDefaultScreen(display)); context_values.foreground=XWhitePixel(display,XDefaultScreen(display)); context_values.function=GXinvert; context_values.plane_mask= context_values.background ^ context_values.foreground; context_values.subwindow_mode=IncludeInferiors; annotate_context=XCreateGC(display,window,GCBackground | GCForeground | GCFunction | GCPlaneMask | GCSubwindowMode,&context_values); if (annotate_context != (GC) NULL) { XHighlightRectangle(display,window,annotate_context,&crop_info); XDelay(display,(unsigned long) (SuspendTime << 2)); XHighlightRectangle(display,window,annotate_context,&crop_info); } /* Get window X image. */ ximage=XGetImage(display,window,crop_info.x,crop_info.y,crop_info.width, crop_info.height,AllPlanes,ZPixmap); if (ximage == (XImage *) NULL) return((Image *) NULL); number_colors=0; colors=(XColor *) NULL; if (window_attributes.colormap != (Colormap) NULL) { ColormapList *p; /* Search colormap list for window colormap. */ number_colors=window_attributes.visual->map_entries; for (p=colormap_list; p != (ColormapList *) NULL; p=p->next) if (p->colormap == window_attributes.colormap) break; if (p == (ColormapList *) NULL) { /* Get the window colormap. */ colors=(XColor *) malloc(number_colors*sizeof(XColor)); if (colors == (XColor *) NULL) { XDestroyImage(ximage); return((Image *) NULL); } if ((window_attributes.visual->class != DirectColor) && (window_attributes.visual->class != TrueColor)) for (i=0; i < number_colors; i++) { colors[i].pixel=i; colors[i].pad=0; } else { unsigned long blue, blue_bit, green, green_bit, red, red_bit; /* DirectColor or TrueColor visual. */ red=0; green=0; blue=0; red_bit=window_attributes.visual->red_mask & (~(window_attributes.visual->red_mask)+1); green_bit=window_attributes.visual->green_mask & (~(window_attributes.visual->green_mask)+1); blue_bit=window_attributes.visual->blue_mask & (~(window_attributes.visual->blue_mask)+1); for (i=0; i < number_colors; i++) { colors[i].pixel=red | green | blue; colors[i].pad=0; red+=red_bit; if (red > window_attributes.visual->red_mask) red=0; green+=green_bit; if (green > window_attributes.visual->green_mask) green=0; blue+=blue_bit; if (blue > window_attributes.visual->blue_mask) blue=0; } } XQueryColors(display,window_attributes.colormap,colors, (int) number_colors); /* Append colormap to colormap list. */ p=(ColormapList *) malloc(sizeof(ColormapList)); p->colormap=window_attributes.colormap; p->colors=colors; p->next=colormap_list; colormap_list=p; } colors=p->colors; } /* Allocate image structure. */ image=AllocateImage((ImageInfo *) NULL); if (image == (Image *) NULL) { XDestroyImage(ximage); return((Image *) NULL); } /* Convert X image to MIFF format. */ if ((window_attributes.visual->class != TrueColor) && (window_attributes.visual->class != DirectColor)) image->class=PseudoClass; image->columns=ximage->width; image->rows=ximage->height; image->packets=ima