static void makeXPixelInfo(XPixelInfo *info, XImage *img, Display *disp, Colormap cmap) { if ( img->depth <= 8 ) { XColor *cdata = info->cinfo; int entries = 1<<img->depth; int i; for(i=0; i<entries; i++) cdata[i].pixel = i; if ( !cmap ) cmap = DefaultColormap(disp, DefaultScreen(disp)); XQueryColors(disp, cmap, cdata, entries); } else { info->cinfo = 0; info->r_shift = shift_for_mask(img->red_mask); info->g_shift = shift_for_mask(img->green_mask); info->b_shift = shift_for_mask(img->blue_mask); info->r_fill = 16 - mask_width(img->red_mask); info->g_fill = 16 - mask_width(img->green_mask); info->b_fill = 16 - mask_width(img->blue_mask); } }
static void fill_xft_data (struct widget_xft_data *data, Widget widget, XftFont *font) { Pixel bg, fg; XColor colors[2]; data->widget = widget; data->xft_font = font; XtVaGetValues (widget, XtNbackground, &bg, XtNforeground, &fg, NULL); colors[0].pixel = data->xft_fg.pixel = fg; colors[1].pixel = data->xft_bg.pixel = bg; XQueryColors (XtDisplay (widget), DefaultColormapOfScreen (XtScreen (widget)), colors, 2); data->xft_fg.color.alpha = 0xFFFF; data->xft_fg.color.red = colors[0].red; data->xft_fg.color.green = colors[0].green; data->xft_fg.color.blue = colors[0].blue; data->xft_bg.color.alpha = 0xFFFF; data->xft_bg.color.red = colors[1].red; data->xft_bg.color.green = colors[1].green; data->xft_bg.color.blue = colors[1].blue; data->p = None; data->xft_draw = 0; data->p_width = data->p_height = 0; }
/* needs to be called after a workspace is added or removed or after application colors changed etc. */ void BroadcastWorkSpacesInfo() { int a,b; UDEWorkspaceExchange *workspace_exchange; workspace_exchange = MyCalloc(TheScreen.desktop.WorkSpaces, sizeof(UDEWorkspaceExchange)); if(workspace_exchange) { for(a=0;a<TheScreen.desktop.WorkSpaces;a++) { for(b=0;b<UDE_MAXCOLORS;b++) XQueryColors(disp, TheScreen.colormap, TheScreen.Colors[a], UDE_MAXCOLORS); strncpy(workspace_exchange[a].name, TheScreen.WorkSpace[a], 32); memcpy(workspace_exchange[a].WorkspaceColors, TheScreen.Colors[a], sizeof(UDEColors)); } XChangeProperty(disp, TheScreen.root, TheScreen.UDE_WORKSPACES_PROPERTY, TheScreen.UDE_WORKSPACES_PROPERTY, 8, PropModeReplace, (unsigned char *) workspace_exchange, sizeof(UDEWorkspaceExchange)* TheScreen.desktop.WorkSpaces); free(workspace_exchange); } else { fprintf(TheScreen.errout,"UWM: coldn't allocate memory to broadcast workspace color information.\n"); } SetResourceDB(); }
/* ** BeginWait/EndWait ** ** Display/Remove a watch cursor over topCursorWidget and its descendents */ void BeginWait(Widget topCursorWidget) { Display *display = XtDisplay(topCursorWidget); Pixmap pixmap; Pixmap maskPixmap; XColor xcolors[2]; static Cursor waitCursor = 0; /* if the watch cursor hasn't been created yet, create it */ if (!waitCursor) { pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display), (char *)watch_bits, watch_width, watch_height); maskPixmap = XCreateBitmapFromData(display, DefaultRootWindow(display), (char *)watch_mask_bits, watch_width, watch_height); xcolors[0].pixel = BlackPixelOfScreen(DefaultScreenOfDisplay(display)); xcolors[1].pixel = WhitePixelOfScreen(DefaultScreenOfDisplay(display)); XQueryColors(display, DefaultColormapOfScreen( DefaultScreenOfDisplay(display)), xcolors, 2); waitCursor = XCreatePixmapCursor(display, pixmap, maskPixmap, &xcolors[0], &xcolors[1], watch_x_hot, watch_y_hot); XFreePixmap(display, pixmap); XFreePixmap(display, maskPixmap); } /* display the cursor */ XDefineCursor(display, XtWindow(topCursorWidget), waitCursor); }
void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc) { #if !wxUSE_NANOX int llp; int screen = DefaultScreen(d); int num_colors = DisplayCells(d,screen); XColor *color_defs = new XColor[num_colors]; #if defined(__INTEL_COMPILER) && 1 /* VDM auto patch */ # pragma ivdep # pragma swp # pragma unroll # pragma prefetch # if 0 # pragma simd noassert # endif #endif /* VDM auto patch */ for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp; XQueryColors(d,cmp,color_defs,num_colors); wxHSV hsv_defs, hsv; wxXColorToHSV(&hsv,xc); int diff, min_diff = 0, pixel = 0; #if defined(__INTEL_COMPILER) && 1 /* VDM auto patch */ # pragma ivdep # pragma swp # pragma unroll # pragma prefetch # if 0 # pragma simd noassert # endif #endif /* VDM auto patch */ for(llp = 0;llp < num_colors;llp++) { wxXColorToHSV(&hsv_defs,&color_defs[llp]); diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) + wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) + wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v)); if (llp == 0) min_diff = diff; if (min_diff > diff) { min_diff = diff; pixel = llp; } if (min_diff == 0) break; } xc -> red = color_defs[pixel].red; xc -> green = color_defs[pixel].green; xc -> blue = color_defs[pixel].blue; xc -> flags = DoRed | DoGreen | DoBlue; /* FIXME, TODO if (!XAllocColor(d,cmp,xc)) cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n"; */ delete[] color_defs; #endif }
Cursor _Delay::hourglass_cursor() { if (hourglass_cache != 0) return hourglass_cache; Display *display = XtDisplay(widget); #if SMALL_HOURGLASS_CURSOR || LARGE_HORGLASS_CURSOR Screen *screen = XtScreen(widget); Window rootWindow = RootWindowOfScreen(screen); unsigned char *cursor_bits = time16_bits; unsigned char *cursor_mask_bits = time16m_bits; unsigned int cursor_width = time16_width; unsigned int cursor_height = time16_height; unsigned int cursor_x_hot = time16_x_hot; unsigned int cursor_y_hot = time16_y_hot; #if LARGE_HOURGLASS_CURSOR // Fetch cursor shape unsigned int width, height; XQueryBestCursor(display, rootWindow, 32, 32, &width, &height); Boolean largeCursors = (width >= 32 && height >= 32); if (largeCursors) { cursor_bits = time32_bits; cursor_mask_bits = time32m_bits; cursor_width = time32_width; cursor_height = time32_height; cursor_x_hot = time32_x_hot; cursor_y_hot = time32_y_hot; } #endif Pixmap cursor_pixmap = XCreateBitmapFromData(display, rootWindow, (char *)cursor_bits, cursor_width, cursor_height); Pixmap cursor_mask_pixmap = XCreateBitmapFromData(display, rootWindow, (char *)cursor_mask_bits, cursor_width, cursor_height); XColor cursor_colors[2]; cursor_colors[0].pixel = BlackPixelOfScreen(screen); cursor_colors[1].pixel = WhitePixelOfScreen(screen); XQueryColors(display, DefaultColormapOfScreen(screen), cursor_colors, 2); hourglass_cache = XCreatePixmapCursor(display, cursor_pixmap, cursor_mask_pixmap, cursor_colors, cursor_colors + 1, cursor_x_hot, cursor_y_hot); #else // Watch cursor hourglass_cache = XCreateFontCursor(display, XC_watch); #endif return hourglass_cache; }
static void xftfont_get_colors (struct frame *f, struct face *face, GC gc, struct xftface_info *xftface_info, XftColor *fg, XftColor *bg) { if (xftface_info && face->gc == gc) { *fg = xftface_info->xft_fg; if (bg) *bg = xftface_info->xft_bg; } else { XGCValues xgcv; bool fg_done = 0, bg_done = 0; block_input (); XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground | GCBackground, &xgcv); if (xftface_info) { if (xgcv.foreground == face->foreground) *fg = xftface_info->xft_fg, fg_done = 1; else if (xgcv.foreground == face->background) *fg = xftface_info->xft_bg, fg_done = 1; if (! bg) bg_done = 1; else if (xgcv.background == face->background) *bg = xftface_info->xft_bg, bg_done = 1; else if (xgcv.background == face->foreground) *bg = xftface_info->xft_fg, bg_done = 1; } if (! (fg_done & bg_done)) { XColor colors[2]; colors[0].pixel = fg->pixel = xgcv.foreground; if (bg) colors[1].pixel = bg->pixel = xgcv.background; XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, bg ? 2 : 1); fg->color.alpha = 0xFFFF; fg->color.red = colors[0].red; fg->color.green = colors[0].green; fg->color.blue = colors[0].blue; if (bg) { bg->color.alpha = 0xFFFF; bg->color.red = colors[1].red; bg->color.green = colors[1].green; bg->color.blue = colors[1].blue; } } unblock_input (); } }
void NxmColorBlinkSet ( int color_index, int type ) /************************************************************************ * NxmColorBlinkSet * * * * NxmColorBlinkSet ( color_index, type ) * * * * Input parameters: * * color_index int * * type int * ** * * T. Piper/SAIC 07/04 Replaced ratio with COLR_SCAL * ***********************************************************************/ { XColor xcolor[2]; int icolr, iret, red, green, blue; /*---------------------------------------------------------------------*/ switch(type) { case 0: if ( LOCK ) return; LOCK = 1; blink[color_index] = BLINK_ON; xcolor[0].pixel = NXMcolrEditPixels[color_index]; xcolor[0].flags = DoRed | DoBlue | DoGreen; xcolor[1].pixel = NXMcolrEditPixels[0]; xcolor[1].flags = DoRed | DoBlue | DoGreen; XQueryColors(gemdisplay, gemmap, xcolor, 2 ); _blinkData.on_color = xcolor[0]; _blinkData.off_color = xcolor[1]; (_blinkData.off_color).pixel = (_blinkData.on_color).pixel; _blinkData.color_index = color_index; _blinkData.timer_id = XtAppAddTimeOut(NXMapp, 1L, (XtTimerCallbackProc)NxmBlinkTimeout, (XtPointer)NULL ); break; case 1: if ( _blinkData.timer_id != (XtIntervalId)0) XtRemoveTimeOut( _blinkData.timer_id ); _blinkData.timer_id = 0; icolr = _blinkData.color_index; red = _blinkData.on_color.red/COLR_SCAL; green = _blinkData.on_color.green/COLR_SCAL; blue = _blinkData.on_color.blue/COLR_SCAL; gscrgb( &icolr, &red, &green, &blue, &iret ); LOCK = 0; break; } }
static long search_near_color(Display *disp, int r, int g, int b) { double d, mind; int scr; static XColor *xc = NULL; static int xc_size = 0; long i, k; scr = DefaultScreen(disp); if(depth == 1) /* black or white */ { d = (double)r * r + (double)g * g + (double)b * b; if(d > 3.0 * 32768.0 * 32768.0) return (long)WhitePixel(disp, scr); return (long)BlackPixel(disp, scr); } if(xc_size == 0) { xc_size = DisplayCells(disp, scr); if(xc_size > 256) return 0; /* Colormap size is too large */ xc = (XColor *)safe_malloc(sizeof(XColor) * xc_size); for(i = 0; i < xc_size; i++) xc[i].pixel = i; } XQueryColors(disp, DefaultColormap(disp, scr), xc, xc_size); mind = calc_color_diff(r, g, b, xc[0].red, xc[0].green, xc[0].blue); k = 0; for(i = 1; i < xc_size; i++) { d = calc_color_diff(r, g, b, xc[i].red, xc[i].green, xc[i].blue); if(d < mind) { mind = d; k = i; if(mind == 0.0) break; } } #ifdef DEBUG fprintf(stderr, "color [%04x %04x %04x]->[%04x %04x %04x] (d^2=%f, k=%d)\n", r, g, b, xc[k].red, xc[k].green, xc[k].blue, mind, (int)k); #endif return k; }
/* Busca o RGB mais proximo na tabela de cores */ static unsigned long nearest_rgb(XColor* xc1) { static int nearest_try = 0; int pos = 0, i; unsigned long min_dist = ULONG_MAX, this_dist; int dr, dg, db; XColor* xc2; for (i=0; i<iupmot_color.num_colors; i++) { xc2 = &(iupmot_color.color_table[i]); dr = (xc1->red - xc2->red) / 850; /* 0.30 / 255 */ dg = (xc1->green - xc2->green) / 432; /* 0.59 / 255 */ db = (xc1->blue - xc2->blue) / 2318; /* 0.11 / 255 */ this_dist = dr*dr + dg*dg + db*db; if (this_dist < min_dist) { min_dist = this_dist; pos = i; } } /* verifico se a cor ainda esta alocada */ /* Try to allocate the closest match color. This should only fail if the cell is read/write. Otherwise, we're incrementing the cell's reference count. (comentario extraido da biblioteca Mesa) */ if (!XAllocColor(iupmot_auxdisplay, iupmot_color.colormap, &(iupmot_color.color_table[pos]))) { /* nao esta, preciso atualizar a tabela e procurar novamente */ /* isto acontece porque a cor encontrada pode ter sido de uma aplicacao que nao existe mais */ /* uma vez atualizada, o problema nao ocorrera' na nova procura */ /* ou a celula e' read write */ if (nearest_try == 1) { nearest_try = 0; return iupmot_color.color_table[pos].pixel; } XQueryColors(iupmot_display, iupmot_color.colormap, iupmot_color.color_table, iupmot_color.num_colors); nearest_try = 1; /* garante que so' vai tentar isso uma vez */ return nearest_rgb(xc1); } return iupmot_color.color_table[pos].pixel; }
Cursor MotifUI::InitHourGlassCursor() { unsigned int width; unsigned int height; unsigned int xHotspot; unsigned int yHotspot; Pixmap pixmap; Pixmap maskPixmap; XColor xcolors[2]; char * bits; char * maskBits; Cursor cursor = None; if (XQueryBestCursor(display, root, 32, 32, &width, &height)) { if ((width >= 32) && (height >= 32)) { width = time32_width; height = time32_height; bits = (char *)time32_bits; maskBits = (char *)time32m_bits; xHotspot = time32_x_hot; yHotspot = time32_y_hot; } else { width = time16_width; height = time16_height; bits = (char *)time16_bits; maskBits = (char *)time16m_bits; xHotspot = time16_x_hot; yHotspot = time16_y_hot; } pixmap = XCreateBitmapFromData(display, root, bits, width, height); maskPixmap = XCreateBitmapFromData(display, root, maskBits, width, height); xcolors[0].pixel = black; xcolors[1].pixel = white; XQueryColors(display, DefaultColormapOfScreen(DefaultScreenOfDisplay (display)), xcolors, 2); cursor = XCreatePixmapCursor(display, pixmap, maskPixmap, &(xcolors[0]), &(xcolors[1]), xHotspot, yHotspot); XFreePixmap(display, pixmap); XFreePixmap(display, maskPixmap); } return cursor; }
/* A replacement for XAllocColor (originally by Brian Paul). This function should never fail to allocate a color. When XAllocColor fails, we return the nearest matching color. If we have to allocate many colors this function isn't a great solution; the XQueryColors() could be done just once. */ static void noFaultXAllocColor(Display * dpy, Colormap cmap, int cmapSize, XColor * color) { XColor *ctable, subColor; int i, bestmatch; double mindist; /* 3*2^16^2 exceeds 32-bit long int precision. */ for (;;) { /* First try just using XAllocColor. */ if (XAllocColor(dpy, cmap, color)) { return; } /* Retrieve color table entries. */ /* XXX alloca canidate. */ ctable = (XColor *) malloc(cmapSize * sizeof(XColor)); for (i = 0; i < cmapSize; i++) ctable[i].pixel = i; XQueryColors(dpy, cmap, ctable, cmapSize); /* Find best match. */ bestmatch = -1; mindist = 0.0; for (i = 0; i < cmapSize; i++) { double dr = (double) color->red - (double) ctable[i].red; double dg = (double) color->green - (double) ctable[i].green; double db = (double) color->blue - (double) ctable[i].blue; double dist = dr * dr + dg * dg + db * db; if (bestmatch < 0 || dist < mindist) { bestmatch = i; mindist = dist; } } /* Return result. */ subColor.red = ctable[bestmatch].red; subColor.green = ctable[bestmatch].green; subColor.blue = ctable[bestmatch].blue; free(ctable); if (XAllocColor(dpy, cmap, &subColor)) { *color = subColor; return; } /* Extremely unlikely, but possibly color was deallocated and reallocated by someone else before we could XAllocColor the color cell we located. If so, loop again... */ } }
void _DtTermPrimRecolorPointer(Widget w) { DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w; XColor colordefs[2]; /* 0 is foreground, 1 is background */ Display *dpy = XtDisplay(w); colordefs[0].pixel = tw->term.pointerColor; colordefs[1].pixel = tw->term.pointerColorBackground; XQueryColors (dpy, DefaultColormap (dpy, DefaultScreen (dpy)), colordefs, 2); XRecolorCursor (dpy, tw->term.pointerShape, colordefs, colordefs+1); return; }
Cursor HelpAgent::create_help_cursor(Widget parent) { Display *display = XtDisplay(parent); Screen *retScr = XtScreen(parent); int screen = XScreenNumberOfScreen(retScr); char *bits; char *maskBits; unsigned int width; unsigned int height; unsigned int xHotspot; unsigned int yHotspot; Pixmap pixmap; Pixmap maskPixmap; XColor xcolors[2]; Cursor cursor; width = onitem32_width; height = onitem32_height; bits = (char *) onitem32_bits; maskBits = (char *) onitem32_m_bits; xHotspot = onitem32_x_hot; yHotspot = onitem32_y_hot; pixmap = XCreateBitmapFromData (display, RootWindowOfScreen(XtScreen(parent)), bits, width, height); maskPixmap = XCreateBitmapFromData (display, RootWindowOfScreen(XtScreen(parent)), maskBits, width, height); xcolors[0].pixel = BlackPixelOfScreen(ScreenOfDisplay(display, screen)); xcolors[1].pixel = WhitePixelOfScreen(ScreenOfDisplay(display, screen)); XQueryColors (display, DefaultColormapOfScreen(ScreenOfDisplay(display, screen)), xcolors, 2); cursor = XCreatePixmapCursor (display, pixmap, maskPixmap, &(xcolors[0]), &(xcolors[1]), xHotspot, yHotspot); XFreePixmap (display, pixmap); XFreePixmap (display, maskPixmap); return cursor; }
void SetCursorColor( Widget widget, Cursor cursor, unsigned long foreground) { XColor colors[2]; Arg args[2]; Colormap cmap; colors[0].pixel = foreground; XtSetArg(args[0], XtNbackground, &(colors[1].pixel)); XtSetArg(args[1], XtNcolormap, &cmap); XtGetValues(widget, args, (Cardinal) 2); XQueryColors(XtDisplay(widget), cmap, colors, 2); XRecolorCursor(XtDisplay(widget), cursor, &colors[0], &colors[1]); }
unsigned long XbrGfxBestColour(Display *display, Colormap defmap, XColor *color, int num_colors) { static XColor *colormap = NULL; int i, best_match = 0, best_diff = 65535 * 3, colorval; /* NULL parameter passed to indicate we can free our mem */ if(color == NULL) { if(colormap) free(colormap); colormap = NULL; return(0); } /* If we have not yet read in the colormap do it */ if(!colormap) { if((colormap = (XColor *) malloc(sizeof(XColor) * num_colors)) == NULL) return(0); for(i = 0; i < num_colors; i++) colormap[i].pixel = i; XQueryColors(display, defmap, colormap, num_colors); } /* Try and get a good match */ for(i = 0; i < num_colors; i++) { colorval = abs(color->red - colormap[i].red) + abs(color->green - colormap[i].green) + abs(color->blue - colormap[i].blue); /* Is it a perfect match */ if(colorval == 0) { best_match = i; break; } /* Is it better than the best one so far */ if(colorval < best_diff) { best_diff = colorval; best_match = i; } } return(colormap[best_match].pixel); }
/*ARGSUSED*/ Boolean XmuCvtStringToColorCursor(Display *dpy, XrmValuePtr args, Cardinal *num_args, XrmValuePtr fromVal, XrmValuePtr toVal, XtPointer *converter_data) { Cursor cursor; Screen *screen; Pixel fg, bg; Colormap c_map; XColor colors[2]; Cardinal number; XrmValue ret_val; if (*num_args != 4) { XtAppWarningMsg(XtDisplayToApplicationContext(dpy), "wrongParameters","cvtStringToColorCursor","XmuError", "String to color cursor conversion needs four arguments", (String *)NULL, (Cardinal *)NULL); return False; } screen = *((Screen **) args[0].addr); fg = *((Pixel *) args[1].addr); bg = *((Pixel *) args[2].addr); c_map = *((Colormap *) args[3].addr); number = 1; XmuCvtStringToCursor(args, &number, fromVal, &ret_val); cursor = *((Cursor *) ret_val.addr); if (cursor == None || (fg == BlackPixelOfScreen(screen) && bg == WhitePixelOfScreen(screen))) new_done(Cursor, cursor); colors[0].pixel = fg; colors[1].pixel = bg; XQueryColors (dpy, c_map, colors, 2); XRecolorCursor(dpy, cursor, colors, colors + 1); new_done(Cursor, cursor); }
Colormap CreateColorMap(Widget main) { Colormap cmap,def_cmap; XColor Colors[256]; int i,ncolors; unsigned long pixels[256],planes[1]; /* Find out how many colors are available on the default colormap */ cmap = DefaultColormap (dpy, DefaultScreen (dpy)); ncolors = 256; while(ncolors>1) { if (XAllocColorCells(dpy,cmap,0,planes,0,pixels,ncolors)) break; ncolors--; } if(ncolors>1) XFreeColors(dpy,cmap,pixels,ncolors,0); /* If there are not enough colors available, create a new colormap */ if(ncolors < 5) { ncolors = 5; def_cmap = DefaultColormap (dpy, DefaultScreen(dpy)); for(i=0; i<200; i++) { Colors[i].pixel = i; Colors[i].flags = DoRed|DoGreen|DoBlue; } XQueryColors(dpy,def_cmap,Colors,200); cmap = XCreateColormap(dpy,DefaultRootWindow(dpy), DefaultVisual(dpy,DefaultScreen(dpy)), AllocNone); XAllocColorCells(dpy,cmap,1,planes,0,pixels,200); //XStoreColors(dpy,cmap,Colors,200); } /* Map the colors we will use */ memset(color,0,sizeof(unsigned long)*256); for(i=0; i<5; i++) MapColor(cmap,i,Color[i][0],Color[i][1],Color[i][2]); /* Return the colormap */ return(cmap); }
Bool XmuDistinguishablePixels(Display * dpy, Colormap cmap, unsigned long *pixels, int count) { XColor *defs; int i, j; Bool ret; for (i = 0; i < count - 1; i++) for (j = i + 1; j < count; j++) if (pixels[i] == pixels[j]) return False; defs = (XColor *) malloc(count * sizeof(XColor)); if (!defs) return False; for (i = 0; i < count; i++) defs[i].pixel = pixels[i]; XQueryColors(dpy, cmap, defs, count); ret = XmuDistinguishableColors(defs, count); free((char *) defs); return ret; }
XilLookup XILWindowAssistor::create_cmap( Colormap *cmap, int type, XilIndexList *ilist, XilLookup yuvtorgb, XilLookup colorcube ) { unsigned long tmp_colors[256], pixels[256], mask; XColor cdefs[256]; int top_colors = 2; /* colormap indices 255 and 254 */ int i, t; Xil_unsigned8 *data; XilLookup lut; int cmapsize; Display *display = Tk_Display(window_->tkwin()); Window window = Tk_WindowId(window_->tkwin()); int screen = Tk_ScreenNumber(window_->tkwin()); //fprintf(stderr,"XILWindowAssistor::"__FUNCTION__"\n"); /* Get colormap size */ switch(type) { case FT_JPEG: case FT_H261: cmapsize = xil_lookup_get_num_entries(yuvtorgb); break; case FT_CELLB: default: /* hmm */ xil_cis_get_attribute(cis_,"DECOMPRESSOR_MAX_CMAP_SIZE", (void**)&cmapsize); break; } /* Create an X colormap for the cis.*/ *cmap = XCreateColormap(display, window, DefaultVisual(display,screen), AllocNone); /* Allocate X Colormap cells * * If we do not need the entire colormap, allocate `cmapsize' * entries just below the top of the colormap. Here's how: * Temporarily allocate some entries at the front of the cmap. * Don't allocate the top_colors (the top two color indices * are often used by other applications). * Allocate the needed entries in the next cmap section * Free the temporary entries. * This allows the X-Window manager to use them and reduces the * chances of of your window colormap flashing. */ if (cmapsize < 256-top_colors) { if (!XAllocColorCells(display, *cmap, 0, &mask, 0, tmp_colors, 256 - cmapsize - top_colors)) { fprintf(stderr, " XAllocColorCells for cmap_create failed(1)\n"); } } if (!XAllocColorCells(display, *cmap, 0, &mask, 0, pixels, cmapsize)) { fprintf(stderr, " XAllocColorCells for cmap_create failed(2)\n"); } /* The remaining code assumes that the values returned in pixels[0] through * pixels[cmapsize-1] are a contiguous range. */ /* Free the unused colors in the front */ if (cmapsize < 256-top_colors) XFreeColors(display, *cmap, tmp_colors, 256 - cmapsize - top_colors, 0); #if 0 if (type == CELLB) { /* Initialize the XilIndexList to use when setting the RDWR_INDICES * attribute. In this example, we make all of the indices writable. */ if ( (ilist->pixels = (Xil_unsigned32 *) malloc(sizeof(Xil_unsigned32) * cmapsize) ) == NULL ) { fprintf(stderr, " out of memory for ilist->pixels create\n"); } ilist->ncolors = cmapsize; /* Copy the color cells returned by XAllocColorCells into the ilist */ for (i = 0; i < cmapsize; i++) ilist->pixels[i] = (Xil_unsigned32) pixels[i]; } #else UNUSED(ilist); #endif /* Allocate memory to hold colormap data. */ if ( (data = (Xil_unsigned8 *) malloc(sizeof(Xil_unsigned8) * cmapsize * 3) ) == NULL ) { fprintf(stderr, "xilcis_color: out of memory for cmap data create\n"); } /* Get the entries for the colormap. The method depends on the compression * type. For CELL, get the entries in the current default colormap. * For JPEG, get the entries from the standard lookup table * yuv_to_rgb. */ switch(type_) { #if 0 /* case FT_CELLB:*/ /* Get the current values in the colormap */ for (i = 0; i < cmapsize; i++) cdefs[i].pixel = i + pixels[0]; XQueryColors(display, DefaultColormap(display, screen), cdefs, cmapsize); /* Convert the values read from the colormap to an array that can * be read by xil_lookup_create. Note that the colormap values are * are stored in the XilLookup in BGR order. */ for (i = 0, j = 0; i < cmapsize; i++, j += 3) { data[j] = cdefs[i].blue >> 8; data[j + 1] = cdefs[i].green >> 8; data[j + 2] = cdefs[i].red >> 8; } lut = xil_lookup_create(xil_, XIL_BYTE, XIL_BYTE, 3, cmapsize, (int)pixels[0], data); break; #endif case FT_JPEG: case FT_H261: case FT_CELLB: xil_lookup_get_values(yuvtorgb, xil_lookup_get_offset(yuvtorgb), cmapsize, data); xil_lookup_set_offset(colorcube, (unsigned int)pixels[0]); for (i = 0, t = 0; i < cmapsize; i++, t += 3) { cdefs[i].pixel = pixels[i]; cdefs[i].flags = DoRed | DoGreen | DoBlue; cdefs[i].blue = data[t] << 8; cdefs[i].green = data[t+1] << 8; cdefs[i].red = data[t+2] << 8; } XStoreColors(display, *cmap, cdefs, cmapsize); lut = yuvtorgb; break; } Tk_SetWindowColormap(window_->tkwin(),*cmap); free(data); return(lut); }
Widget CpCreateCartesianPlot(DisplayInfo *displayInfo, DlCartesianPlot *dlCartesianPlot, MedmCartesianPlot *pcp) { Arg args[47]; int nargs; XColor xColors[2]; char rgb[2][16], string[24]; int usedHeight, usedCharWidth, bestSize, preferredHeight; char *headerStrings[2]; int k, iPrec; XcVType minF, maxF, tickF; Widget w; int validTraces = pcp ? pcp->nTraces : 0; /* Set widget args from the dlCartesianPlot structure */ nargs = 0; XtSetArg(args[nargs],XmNx,(Position)dlCartesianPlot->object.x); nargs++; XtSetArg(args[nargs],XmNy,(Position)dlCartesianPlot->object.y); nargs++; XtSetArg(args[nargs],XmNwidth,(Dimension)dlCartesianPlot->object.width); nargs++; XtSetArg(args[nargs],XmNborderWidth,0); nargs++; XtSetArg(args[nargs],XmNheight,(Dimension)dlCartesianPlot->object.height); nargs++; XtSetArg(args[nargs],XmNhighlightThickness,0); nargs++; /* JPT uses strings for color names */ xColors[0].pixel = displayInfo->colormap[dlCartesianPlot->plotcom.clr]; xColors[1].pixel = displayInfo->colormap[dlCartesianPlot->plotcom.bclr]; XQueryColors(display,cmap,xColors,2); sprintf(rgb[0],"#%2.2x%2.2x%2.2x",xColors[0].red>>8, xColors[0].green>>8, xColors[0].blue>>8); sprintf(rgb[1],"#%2.2x%2.2x%2.2x",xColors[1].red>>8, xColors[1].green>>8, xColors[1].blue>>8); XtSetArg(args[nargs],XtNplotGraphForegroundColor,rgb[0]); nargs++; XtSetArg(args[nargs],XtNplotGraphBackgroundColor,rgb[1]); nargs++; XtSetArg(args[nargs],XtNplotForegroundColor,rgb[0]); nargs++; XtSetArg(args[nargs],XtNplotBackgroundColor,rgb[1]); nargs++; preferredHeight = MIN(dlCartesianPlot->object.width, dlCartesianPlot->object.height)/TITLE_SCALE_FACTOR; bestSize = dmGetBestFontWithInfo(fontTable,MAX_FONTS,NULL, preferredHeight,0,&usedHeight,&usedCharWidth,FALSE); XtSetArg(args[nargs],XtNplotHeaderFont,fontTable[bestSize]->fid); nargs++; if (strlen(dlCartesianPlot->plotcom.title) > 0) { headerStrings[0] = dlCartesianPlot->plotcom.title; } else { headerStrings[0] = NULL; } headerStrings[1] = NULL; XtSetArg(args[nargs],XtNplotHeaderStrings,headerStrings); nargs++; if (strlen(dlCartesianPlot->plotcom.xlabel) > 0) { XtSetArg(args[nargs],XtNplotXTitle,dlCartesianPlot->plotcom.xlabel); nargs++; } else { XtSetArg(args[nargs],XtNplotXTitle,NULL); nargs++; } if (strlen(dlCartesianPlot->plotcom.ylabel) > 0) { XtSetArg(args[nargs],XtNplotYTitle,dlCartesianPlot->plotcom.ylabel); nargs++; } else { XtSetArg(args[nargs],XtNplotYTitle,NULL); nargs++; } preferredHeight = MIN(dlCartesianPlot->object.width, dlCartesianPlot->object.height)/AXES_SCALE_FACTOR; bestSize = dmGetBestFontWithInfo(fontTable,MAX_FONTS,NULL, preferredHeight,0,&usedHeight,&usedCharWidth,FALSE); XtSetArg(args[nargs],XtNplotAxisFont,fontTable[bestSize]->fid); nargs++; switch (dlCartesianPlot->style) { case POINT_PLOT: case LINE_PLOT: XtSetArg(args[nargs],XtNplotType,PLOT_PLOT); nargs++; if (validTraces > 1) { XtSetArg(args[nargs],XtNplotType2,PLOT_PLOT); nargs++; } break; case FILL_UNDER_PLOT: XtSetArg(args[nargs],XtNplotType,PLOT_AREA); nargs++; if (validTraces > 1) { XtSetArg(args[nargs],XtNplotType2,PLOT_AREA); nargs++; } break; } if (validTraces > 1) { XtSetArg(args[nargs],XtNplotY2AxisShow,TRUE); nargs++; } /* X Axis Style */ switch (dlCartesianPlot->axis[X_AXIS_ELEMENT].axisStyle) { case LINEAR_AXIS: XtSetArg(args[nargs],XtNplotXAnnotationMethod,PLOT_ANNO_VALUES); nargs++; XtSetArg(args[nargs],XtNplotXAxisLogarithmic,False); nargs++; break; case LOG10_AXIS: XtSetArg(args[nargs],XtNplotXAnnotationMethod,PLOT_ANNO_VALUES); nargs++; XtSetArg(args[nargs],XtNplotXAxisLogarithmic,True); nargs++; break; case TIME_AXIS: { XtSetArg(args[nargs],XtNplotXAxisLogarithmic,False); nargs++; XtSetArg(args[nargs],XtNplotXAnnotationMethod,PLOT_ANNO_TIME_LABELS); nargs++; XtSetArg(args[nargs],XtNplotTimeBase,time900101); nargs++; XtSetArg(args[nargs],XtNplotTimeUnit,PLOT_TMUNIT_SECONDS); nargs++; XtSetArg(args[nargs],XtNplotTimeFormatUseDefault,False); nargs++; XtSetArg(args[nargs],XtNplotTimeFormat, cpTimeFormatString[dlCartesianPlot->axis[0].timeFormat -FIRST_CP_TIME_FORMAT]); nargs++; if(pcp) pcp->timeScale = True; } break; default: medmPrintf(1,"\nCpCreateRunTimeCartesianPlot: Unknown X axis style\n"); break; } /* X Axis Range */ switch (dlCartesianPlot->axis[X_AXIS_ELEMENT].rangeStyle) { case CHANNEL_RANGE: /* handle as default until connected */ case AUTO_SCALE_RANGE: XtSetArg(args[nargs],XtNplotXNumUseDefault,True); nargs++; XtSetArg(args[nargs],XtNplotXTickUseDefault,True); nargs++; XtSetArg(args[nargs],XtNplotXPrecisionUseDefault,True); nargs++; break; case USER_SPECIFIED_RANGE: minF.fval = dlCartesianPlot->axis[X_AXIS_ELEMENT].minRange; maxF.fval = dlCartesianPlot->axis[X_AXIS_ELEMENT].maxRange; tickF.fval = (float)((maxF.fval - minF.fval)/4.0); XtSetArg(args[nargs],XtNplotXMin,minF.lval); nargs++; XtSetArg(args[nargs],XtNplotXMax,maxF.lval); nargs++; XtSetArg(args[nargs],XtNplotXTick,tickF.lval); nargs++; XtSetArg(args[nargs],XtNplotXNum,tickF.lval); nargs++; sprintf(string,"%f",tickF.fval); k = strlen(string)-1; while (string[k] == '0') k--; /* strip off trailing zeroes */ iPrec = k; while (string[k] != '.' && k >= 0) k--; iPrec = iPrec - k; XtSetArg(args[nargs],XtNplotXPrecision,iPrec); nargs++; break; default: medmPrintf(1,"\nCpCreateRunTimeCartesianPlot: Unknown X range style\n"); break; } /* Y1 Axis Style */ switch (dlCartesianPlot->axis[Y1_AXIS_ELEMENT].axisStyle) { case LINEAR_AXIS: break; case LOG10_AXIS: XtSetArg(args[nargs],XtNplotYAxisLogarithmic,True); nargs++; break; default: medmPrintf(1,"\nCpCreateRunTimeCartesianPlot: Unknown Y1 axis style\n"); break; } /* Y1 Axis Range */ switch (dlCartesianPlot->axis[Y1_AXIS_ELEMENT].rangeStyle) { case CHANNEL_RANGE: /* handle as default until connected */ case AUTO_SCALE_RANGE: XtSetArg(args[nargs],XtNplotYNumUseDefault,True); nargs++; XtSetArg(args[nargs],XtNplotYTickUseDefault,True); nargs++; XtSetArg(args[nargs],XtNplotYPrecisionUseDefault,True); nargs++; break; case USER_SPECIFIED_RANGE: minF.fval = dlCartesianPlot->axis[Y1_AXIS_ELEMENT].minRange; maxF.fval = dlCartesianPlot->axis[Y1_AXIS_ELEMENT].maxRange; tickF.fval = (float)((maxF.fval - minF.fval)/4.0); XtSetArg(args[nargs],XtNplotYMin,minF.lval); nargs++; XtSetArg(args[nargs],XtNplotYMax,maxF.lval); nargs++; XtSetArg(args[nargs],XtNplotYTick,tickF.lval); nargs++; XtSetArg(args[nargs],XtNplotYNum,tickF.lval); nargs++; sprintf(string,"%f",tickF.fval); k = strlen(string)-1; while (string[k] == '0') k--; /* strip off trailing zeroes */ iPrec = k; while (string[k] != '.' && k >= 0) k--; iPrec = iPrec - k; XtSetArg(args[nargs],XtNplotYPrecision,iPrec); nargs++; break; default: medmPrintf(1,"\nCpCreateRunTimeCartesianPlot: Unknown Y1 range style\n"); break; } /* Y2 Axis Style */ switch (dlCartesianPlot->axis[Y2_AXIS_ELEMENT].axisStyle) { case LINEAR_AXIS: break; case LOG10_AXIS: XtSetArg(args[nargs],XtNplotY2AxisLogarithmic,True); nargs++; break; default: medmPrintf(1, "\nCpCreateRunTimeCartesianPlot: Unknown Y2 axis style\n"); break; } /* Y2 Axis Range */ switch (dlCartesianPlot->axis[Y2_AXIS_ELEMENT].rangeStyle) { case CHANNEL_RANGE: /* handle as default until connected */ case AUTO_SCALE_RANGE: XtSetArg(args[nargs],XtNplotY2NumUseDefault,True); nargs++; XtSetArg(args[nargs],XtNplotY2TickUseDefault,True); nargs++; XtSetArg(args[nargs],XtNplotY2PrecisionUseDefault,True); nargs++; break; case USER_SPECIFIED_RANGE: minF.fval = dlCartesianPlot->axis[Y2_AXIS_ELEMENT].minRange; maxF.fval = dlCartesianPlot->axis[Y2_AXIS_ELEMENT].maxRange; tickF.fval = (float)((maxF.fval - minF.fval)/4.0); XtSetArg(args[nargs],XtNplotY2Min,minF.lval); nargs++; XtSetArg(args[nargs],XtNplotY2Max,maxF.lval); nargs++; XtSetArg(args[nargs],XtNplotY2Tick,tickF.lval); nargs++; XtSetArg(args[nargs],XtNplotY2Num,tickF.lval); nargs++; sprintf(string,"%f",tickF.fval); k = strlen(string)-1; while (string[k] == '0') k--; /* strip off trailing zeroes */ iPrec = k; while (string[k] != '.' && k >= 0) k--; iPrec = iPrec - k; XtSetArg(args[nargs],XtNplotY2Precision,iPrec); nargs++; break; default: medmPrintf(1, "\nCpCreateRunTimeCartesianPlot: Unknown Y2 range style\n"); break; } /* Don't show outlines in filled plots */ XtSetArg(args[nargs], XtNplotGraphShowOutlines, False); nargs++; /* Set miscellaneous args */ XtSetArg(args[nargs],XmNtraversalOn,False); nargs++; XtSetArg(args[nargs], XtNplotDoubleBuffer, True); nargs++; /* Add pointer to CartesianPlot struct as userData to widget */ XtSetArg(args[nargs], XmNuserData, (XtPointer)pcp); nargs++; /* Create the widget */ w = XtCreateWidget("cartesianPlot", atPlotterWidgetClass, displayInfo->drawingArea, args, nargs); return(w); }
void Xvars::unstretch_image(int dpyNum, XImage* dest,XImage* src, Pixel* pixels,int pixelsNum) { assert(src->width == 2 * dest->width && src->height == 2 * dest->height); // Don't use color info at all, just subsample the pixels. // // Do this if 1) useAveraging is turned off, // 2) there is no interesting pixel information, i.e. masks. if (!useAveraging || pixels == NULL) { Xvars_unstretch_subsample(dest,src); return; } // First get list of RGB values for the available pixels. // colors[] will be indexed parallel to pixels[]. XColor* colors = new XColor[pixelsNum]; assert(colors); int n; for (n = 0; n < pixelsNum; n++) { colors[n].pixel = pixels[n]; } XQueryColors(dpy[dpyNum],cmap[dpyNum],colors,pixelsNum); // Create hashtable to map from pixels to RGB values. // Safe to put pointers to colors[], since that array is guaranteed to // live as long as the HashTable. IDictionary* pixel2RGB = HashTable_factory(); for (n = 0; n < pixelsNum; n++) { pixel2RGB->put((void*)colors[n].pixel,(void*)&colors[n]); } // Real simple, sample every other pixel. // We could make it look much better with a better algorithm, e.g. // average every block of four pixels. Pos destPos; for (destPos.y = 0; destPos.y < dest->height; destPos.y++) { for (destPos.x = 0; destPos.x < dest->width; destPos.x++) { // Use int not XColor, since members of XColor are short. int r = 0, g = 0, b = 0; // Search four pixels of src. Pos srcPos; for (srcPos.y = 2 * destPos.y; srcPos.y < 2 * (destPos.y + 1); srcPos.y++) { for (srcPos.x = 2 * destPos.x; srcPos.x < 2 * (destPos.x + 1); srcPos.x++) { unsigned long pix = XGetPixel(src,srcPos.x,srcPos.y); XColor* color = (XColor*)pixel2RGB->get((void*)pix); // The XImage, src, shouldn't have any pixels that aren't in the // list of pixels passed in. if (color) { r += color->red; g += color->green; b += color->blue; } else { if (pix != 0) { std::cerr << "Xvars::unstretch_image() found pixel " << pix << " that is not in the pixel list." << std::endl; } } } // x } // y // Divide by 4, since we are averaging 4 pixels in the source. XColor destColor; destColor.red = (r >> 2); destColor.green = (g >> 2); destColor.blue = (b >> 2); // Find the color in colors closest to the RGB values in destColor. int destIndex = color_match(&destColor,colors,pixelsNum); Pixel destPixel = colors[destIndex].pixel; // Put the pixel in the dest image. XPutPixel(dest,destPos.x,destPos.y,destPixel); } } delete pixel2RGB; delete [] colors; }
/* translate a colorset spec into a colorset structure */ void parse_colorset(int n, char *line) { int i; int w; int h; int tmp; int percent; colorset_t *cs; char *optstring; char *args; char *option; char *tmp_str; char *fg = NULL; char *bg = NULL; char *hi = NULL; char *sh = NULL; char *fgsh = NULL; char *tint = NULL; char *fg_tint = NULL; char *bg_tint = NULL; char *icon_tint = NULL; Bool have_pixels_changed = False; Bool has_icon_pixels_changed = False; Bool has_fg_changed = False; Bool has_bg_changed = False; Bool has_sh_changed = False; Bool has_hi_changed = False; Bool has_fgsh_changed = False; Bool has_fg_alpha_changed = False; Bool has_tint_changed = False; Bool has_fg_tint_changed = False; Bool has_bg_tint_changed = False; Bool has_icon_tint_changed = False; Bool has_pixmap_changed = False; Bool has_shape_changed = False; Bool has_image_alpha_changed = False; Bool pixmap_is_a_bitmap = False; Bool do_reload_pixmap = False; Bool is_server_grabbed = False; XColor color; XGCValues xgcv; static char *name = "parse_colorset"; Window win = Scr.NoFocusWin; static GC gc = None; /* initialize statics */ if (gc == None) { gc = fvwmlib_XCreateGC(dpy, win, 0, &xgcv); } /* make sure it exists and has sensible contents */ alloc_colorset(n); cs = &Colorset[n]; /*** Parse the options ***/ while (line && *line) { /* Read next option specification delimited by a comma or \0. */ line = GetQuotedString( line, &optstring, ",", NULL, NULL, NULL); if (!optstring) break; args = GetNextToken(optstring, &option); if (!option) { free(optstring); break; } switch((i = GetTokenIndex(option, csetopts, 0, NULL))) { case 0: /* Foreground */ case 1: /* Fore */ case 2: /* fg */ get_simple_color( args, &fg, cs, FG_SUPPLIED, FG_CONTRAST, "contrast"); has_fg_changed = True; break; case 3: /* Background */ case 4: /* Back */ case 5: /* bg */ get_simple_color( args, &bg, cs, BG_SUPPLIED, BG_AVERAGE, "average"); has_bg_changed = True; break; case 6: /* Hilight */ case 7: /* Hilite */ case 8: /* hi */ get_simple_color(args, &hi, cs, HI_SUPPLIED, 0, NULL); has_hi_changed = True; break; case 9: /* Shadow */ case 10: /* Shade */ case 11: /* sh */ get_simple_color(args, &sh, cs, SH_SUPPLIED, 0, NULL); has_sh_changed = True; break; case 12: /* fgsh */ get_simple_color( args, &fgsh, cs, FGSH_SUPPLIED, 0,NULL); has_fgsh_changed = True; break; case 13: /* fg_alpha */ case 14: /* fgAlpha */ if (GetIntegerArguments(args, NULL, &tmp, 1)) { if (tmp > 100) tmp = 100; else if (tmp < 0) tmp = 0; } else { tmp = 100; } if (tmp != cs->fg_alpha_percent) { cs->fg_alpha_percent = tmp; has_fg_alpha_changed = True; } break; case 15: /* TiledPixmap */ case 16: /* Pixmap */ case 17: /* AspectPixmap */ has_pixmap_changed = True; free_colorset_background(cs, True); tmp_str = PeekToken(args, &args); if (tmp_str) { CopyString(&cs->pixmap_args, tmp_str); do_reload_pixmap = True; cs->gradient_type = 0; /* set the flags */ if (csetopts[i][0] == 'T') { cs->pixmap_type = PIXMAP_TILED; } else if (csetopts[i][0] == 'A') { cs->pixmap_type = PIXMAP_STRETCH_ASPECT; } else { cs->pixmap_type = PIXMAP_STRETCH; } } /* the pixmap is build later */ break; case 18: /* Shape */ case 19: /* TiledShape */ case 20: /* AspectShape */ parse_shape(win, cs, i, args, &has_shape_changed); break; case 21: /* Plain */ has_pixmap_changed = True; free_colorset_background(cs, True); break; case 22: /* NoShape */ has_shape_changed = True; if (cs->shape_mask) { add_to_junk(cs->shape_mask); cs->shape_mask = None; } break; case 23: /* Transparent */ /* This is only allowable when the root depth == fvwm * visual depth otherwise bad match errors happen, * it may be even more restrictive but my tests (on * exceed 6.2) show that only == depth is necessary */ if (Pdepth != DefaultDepth(dpy, (DefaultScreen(dpy)))) { fvwm_msg( ERR, name, "can't do Transparent " "when root_depth!=fvwm_depth"); break; } has_pixmap_changed = True; free_colorset_background(cs, True); cs->pixmap = ParentRelative; cs->pixmap_type = PIXMAP_STRETCH; break; case 24: /* RootTransparent */ if (Pdepth != DefaultDepth(dpy, (DefaultScreen(dpy)))) { fvwm_msg( ERR, name, "can't do RootTransparent " "when root_depth!=fvwm_depth"); break; } free_colorset_background(cs, True); has_pixmap_changed = True; cs->pixmap_type = PIXMAP_ROOT_PIXMAP_PURE; do_reload_pixmap = True; tmp_str = PeekToken(args, &args); if (StrEquals(tmp_str, "buffer")) { cs->allows_buffered_transparency = True; } else { cs->allows_buffered_transparency = False; } cs->is_maybe_root_transparent = True; break; case 25: /* Tint */ case 26: /* PixmapTint */ case 27: /* ImageTint */ case 28: /* TintMask */ parse_simple_tint( cs, args, &tint, TINT_SUPPLIED, &has_tint_changed, &percent, "tint"); if (has_tint_changed) { cs->tint_percent = percent; } break; case 29: /* NoTint */ has_tint_changed = True; cs->tint_percent = 0; cs->color_flags &= ~TINT_SUPPLIED; break; case 30: /* fgTint */ parse_simple_tint( cs, args, &fg_tint, FG_TINT_SUPPLIED, &has_fg_tint_changed, &percent, "fgTint"); if (has_fg_tint_changed) { cs->fg_tint_percent = percent; } break; case 31: /* bgTint */ parse_simple_tint( cs, args, &bg_tint, BG_TINT_SUPPLIED, &has_bg_tint_changed, &percent, "bgTint"); if (has_bg_tint_changed) { cs->bg_tint_percent = percent; } break; case 32: /* dither */ if (cs->pixmap_args || cs->gradient_args) { has_pixmap_changed = True; do_reload_pixmap = True; } cs->dither = True; break; case 33: /* nodither */ if (cs->pixmap_args || cs->gradient_args) { has_pixmap_changed = True; do_reload_pixmap = True; } cs->dither = False; break; case 34: /* Alpha */ case 35: /* PixmapAlpha */ case 36: /* ImageAlpha */ if (GetIntegerArguments(args, NULL, &tmp, 1)) { if (tmp > 100) tmp = 100; else if (tmp < 0) tmp = 0; } else { tmp = 100; } if (tmp != cs->image_alpha_percent) { has_image_alpha_changed = True; cs->image_alpha_percent = tmp; } break; /* dither icon is not dynamic (yet) maybe a bad opt: default * to False ? */ case 37: /* ditherIcon */ cs->do_dither_icon = True; break; case 38: /* DoNotDitherIcon */ cs->do_dither_icon = False; break; case 39: /* IconTint */ parse_simple_tint( cs, args, &icon_tint, ICON_TINT_SUPPLIED, &has_icon_tint_changed, &percent, "IconTint"); if (has_icon_tint_changed) { cs->icon_tint_percent = percent; has_icon_pixels_changed = True; } break; case 40: /* NoIconTint */ has_icon_tint_changed = True; if (cs->icon_tint_percent != 0) { has_icon_pixels_changed = True; } cs->icon_tint_percent = 0; break; case 41: /* IconAlpha */ if (GetIntegerArguments(args, NULL, &tmp, 1)) { if (tmp > 100) tmp = 100; else if (tmp < 0) tmp = 0; } else { tmp = 100; } if (tmp != cs->icon_alpha_percent) { has_icon_pixels_changed = True; cs->icon_alpha_percent = tmp; } break; default: /* test for ?Gradient */ if (option[0] && StrEquals(&option[1], "Gradient")) { cs->gradient_type = toupper(option[0]); if (!IsGradientTypeSupported(cs->gradient_type)) break; has_pixmap_changed = True; free_colorset_background(cs, True); CopyString(&cs->gradient_args, args); do_reload_pixmap = True; if (cs->gradient_type == V_GRADIENT) { cs->pixmap_type = PIXMAP_STRETCH_Y; } else if (cs->gradient_type == H_GRADIENT) cs->pixmap_type = PIXMAP_STRETCH_X; else cs->pixmap_type = PIXMAP_STRETCH; } else { fvwm_msg( WARN, name, "bad colorset pixmap " "specifier %s %s", option, line); } break; } /* switch */ if (option) { free(option); option = NULL; } free(optstring); optstring = NULL; } /* while (line && *line) */ /* * ---------- change the "pixmap" tint colour ---------- */ if (has_tint_changed) { /* user specified colour */ if (tint != NULL) { Pixel old_tint = cs->tint; PictureFreeColors(dpy, Pcmap, &cs->tint, 1, 0, True); cs->tint = GetColor(tint); if (old_tint != cs->tint) { have_pixels_changed = True; } } else if (tint == NULL) { /* default */ Pixel old_tint = cs->tint; PictureFreeColors(dpy, Pcmap, &cs->tint, 1, 0, True); cs->tint = GetColor(black); if (old_tint != cs->tint) { have_pixels_changed = True; } } } /* * reload the gradient if the tint or the alpha have changed. * Do this too if we need to recompute the bg average and the * gradient is tinted (perforemence issue). */ if ((has_tint_changed || has_image_alpha_changed || (has_bg_changed && (cs->color_flags & BG_AVERAGE) && cs->tint_percent > 0)) && cs->gradient_args) { do_reload_pixmap = True; } /* * reset the pixmap if the tint or the alpha has changed */ if (!do_reload_pixmap && (has_tint_changed || has_image_alpha_changed || (has_bg_changed && cs->alpha_pixmap != None))) { if (cs->pixmap_type == PIXMAP_ROOT_PIXMAP_PURE || cs->pixmap_type == PIXMAP_ROOT_PIXMAP_TRAN) { do_reload_pixmap = True; } else if (cs->picture != NULL && cs->pixmap) { XSetClipMask(dpy, gc, cs->picture->mask); reset_cs_pixmap(cs, gc); XSetClipMask(dpy, gc, None); has_pixmap_changed = True; } } /* * (re)build the pixmap or the gradient */ if (do_reload_pixmap) { free_colorset_background(cs, False); has_pixmap_changed = True; if (cs->pixmap_type == PIXMAP_ROOT_PIXMAP_PURE || cs->pixmap_type == PIXMAP_ROOT_PIXMAP_TRAN) { cs->pixmap_type = 0; if (root_pic.pixmap) { cs->pixmap = root_pic.pixmap; cs->width = root_pic.width; cs->height = root_pic.height; cs->pixmap_type = PIXMAP_ROOT_PIXMAP_PURE; #if 0 fprintf(stderr,"Cset %i LoadRoot 0x%lx\n", n, cs->pixmap); #endif } } else if (cs->pixmap_args) { parse_pixmap(win, gc, cs, &pixmap_is_a_bitmap); } else if (cs->gradient_args) { cs->pixmap = CreateGradientPixmapFromString( dpy, win, gc, cs->gradient_type, cs->gradient_args, &w, &h, &cs->pixels, &cs->nalloc_pixels, cs->dither); cs->width = w; cs->height = h; } has_pixmap_changed = True; } if (cs->picture != NULL && cs->picture->depth != Pdepth) { pixmap_is_a_bitmap = True; } /* * ---------- change the background colour ---------- */ if (has_bg_changed || (has_pixmap_changed && (cs->color_flags & BG_AVERAGE) && cs->pixmap != None && cs->pixmap != ParentRelative && !pixmap_is_a_bitmap)) { Bool do_set_default_background = False; Pixmap average_pix = None; if (cs->color_flags & BG_AVERAGE) { if (cs->picture != NULL && cs->picture->picture != None) { average_pix = cs->picture->picture; } else if (cs->pixmap != ParentRelative) { average_pix = cs->pixmap; } if (average_pix == root_pic.pixmap) { int w; int h; XID dummy; MyXGrabServer(dpy); is_server_grabbed = True; if (!XGetGeometry( dpy, average_pix, &dummy, (int *)&dummy, (int *)&dummy, (unsigned int *)&w, (unsigned int *)&h, (unsigned int *)&dummy, (unsigned int *)&dummy)) { average_pix = None; } else { if (w != cs->width || h != cs->height) { average_pix = None; } } if (average_pix == None) { MyXUngrabServer(dpy); is_server_grabbed = False; } } } /* note: no average for bitmap */ if ((cs->color_flags & BG_AVERAGE) && average_pix) { /* calculate average background color */ XColor *colors; XImage *image; XImage *mask_image = None; unsigned int i, j, k = 0; unsigned long red = 0, blue = 0, green = 0; unsigned long tred, tblue, tgreen; double dred = 0.0, dblue = 0.0, dgreen = 0.0; has_bg_changed = True; /* create an array to store all the pixmap colors in */ /* Note: this may allocate a lot of memory: * cs->width * cs->height * 12 and then the rest of the * procedure can take a lot of times */ colors = (XColor *)safemalloc( cs->width * cs->height * sizeof(XColor)); /* get the pixmap and mask into an image */ image = XGetImage( dpy, average_pix, 0, 0, cs->width, cs->height, AllPlanes, ZPixmap); if (cs->mask != None) { mask_image = XGetImage( dpy, cs->mask, 0, 0, cs->width, cs->height, AllPlanes, ZPixmap); } if (is_server_grabbed == True) { MyXUngrabServer(dpy); } if (image != None && mask_image != None) { /* only fetch the pixels that are not masked * out */ for (i = 0; i < cs->width; i++) { for (j = 0; j < cs->height; j++) { if ( cs->mask == None || XGetPixel( mask_image, i, j) == 0) { colors[k++].pixel = XGetPixel( image, i, j); } } } } if (image != None) { XDestroyImage(image); } if (mask_image != None) { XDestroyImage(mask_image); } if (k == 0) { do_set_default_background = True; } else { /* look them all up, XQueryColors() can't * handle more than 256 */ for (i = 0; i < k; i += 256) { XQueryColors( dpy, Pcmap, &colors[i], min(k - i, 256)); } /* calculate average, add overflows in a double * .red is short, red is long */ for (i = 0; i < k; i++) { tred = red; red += colors[i].red; if (red < tred) { dred += (double)tred; red = colors[i].red; } tgreen = green; green += colors[i].green; if (green < tgreen) { dgreen += (double)tgreen; green = colors[i].green; } tblue = blue; blue += colors[i].blue; if (blue < tblue) { dblue += (double)tblue; blue = colors[i].blue; } } dred += red; dgreen += green; dblue += blue; /* get it */ color.red = dred / k; color.green = dgreen / k; color.blue = dblue / k; { Pixel old_bg = cs->bg; PictureFreeColors( dpy, Pcmap, &cs->bg, 1, 0, True); PictureAllocColor( dpy, Pcmap, &color, True); cs->bg = color.pixel; if (old_bg != cs->bg) { have_pixels_changed = True; } } } free(colors); } /* average */ else if ((cs->color_flags & BG_SUPPLIED) && bg != NULL) { /* user specified colour */ Pixel old_bg = cs->bg; PictureFreeColors(dpy, Pcmap, &cs->bg, 1, 0, True); cs->bg = GetColor(bg); if (old_bg != cs->bg) { have_pixels_changed = True; } } /* user specified */ else if (bg == NULL && has_bg_changed) { /* default */ do_set_default_background = True; } /* default */ if (do_set_default_background) { Pixel old_bg = cs->bg; PictureFreeColors(dpy, Pcmap, &cs->bg, 1, 0, True); cs->bg = GetColor(white); if (old_bg != cs->bg) { have_pixels_changed = True; } has_bg_changed = True; } if (has_bg_changed) { /* save the bg color for tinting */ cs->bg_saved = cs->bg; } } /* has_bg_changed */ /* * ---------- setup the bg tint colour ---------- */ if (has_bg_tint_changed && cs->bg_tint_percent > 0 && bg_tint != NULL) { PictureFreeColors(dpy, Pcmap, &cs->bg_tint, 1, 0, True); cs->bg_tint = GetColor(bg_tint); } /* * ---------- tint the bg colour ---------- */ if (has_bg_tint_changed || (has_bg_changed && cs->bg_tint_percent > 0)) { if (cs->bg_tint_percent == 0) { Pixel old_bg = cs->bg; PictureFreeColors(dpy, Pcmap, &cs->bg, 1, 0, True); cs->bg = cs->bg_saved; if (old_bg != cs->bg) { have_pixels_changed = True; has_bg_changed = True; } } else { Pixel old_bg = cs->bg; PictureFreeColors(dpy, Pcmap, &cs->bg, 1, 0, True); cs->bg = GetTintedPixel( cs->bg_saved, cs->bg_tint, cs->bg_tint_percent); if (old_bg != cs->bg) { have_pixels_changed = True; has_bg_changed = True; } } } /* * ---------- setup the fg tint colour ---------- */ if (has_fg_tint_changed && cs->fg_tint_percent > 0 && fg_tint != NULL) { PictureFreeColors(dpy, Pcmap, &cs->fg_tint, 1, 0, True); cs->fg_tint = GetColor(fg_tint); } /* * ---------- change the foreground colour ---------- */ if (has_fg_changed || (has_bg_changed && (cs->color_flags & FG_CONTRAST))) { if (cs->color_flags & FG_CONTRAST) { Pixel old_fg = cs->fg; /* calculate contrasting foreground color */ color.pixel = cs->bg; XQueryColor(dpy, Pcmap, &color); color.red = (color.red > 32767) ? 0 : 65535; color.green = (color.green > 32767) ? 0 : 65535; color.blue = (color.blue > 32767) ? 0 : 65535; PictureFreeColors(dpy, Pcmap, &cs->fg, 1, 0, True); PictureAllocColor(dpy, Pcmap, &color, True); cs->fg = color.pixel; if (old_fg != cs->fg) { have_pixels_changed = True; has_fg_changed = 1; } } /* contrast */ else if ((cs->color_flags & FG_SUPPLIED) && fg != NULL) { /* user specified colour */ Pixel old_fg = cs->fg; PictureFreeColors(dpy, Pcmap, &cs->fg, 1, 0, True); cs->fg = GetColor(fg); if (old_fg != cs->fg) { have_pixels_changed = True; has_fg_changed = 1; } } /* user specified */ else if (fg == NULL) { /* default */ Pixel old_fg = cs->fg; PictureFreeColors(dpy, Pcmap, &cs->fg, 1, 0, True); cs->fg = GetColor(black); if (old_fg != cs->fg) { have_pixels_changed = True; has_fg_changed = 1; } } /* save the fg color for tinting */ cs->fg_saved = cs->fg; } /* has_fg_changed */ /* * ---------- tint the foreground colour ---------- */ if (has_fg_tint_changed || (has_fg_changed && cs->fg_tint_percent > 0)) { if (cs->fg_tint_percent == 0) { Pixel old_fg = cs->fg; PictureFreeColors(dpy, Pcmap, &cs->fg, 1, 0, True); cs->fg = cs->fg_saved; if (old_fg != cs->fg) { have_pixels_changed = True; has_fg_changed = 1; } } else { Pixel old_fg = cs->fg; PictureFreeColors(dpy, Pcmap, &cs->fg, 1, 0, True); cs->fg = GetTintedPixel( cs->fg_saved, cs->fg_tint, cs->fg_tint_percent); if (old_fg != cs->fg) { have_pixels_changed = True; has_fg_changed = 1; } } } /* * ---------- change the hilight colour ---------- */ if (has_hi_changed || (has_bg_changed && !(cs->color_flags & HI_SUPPLIED))) { has_hi_changed = 1; if ((cs->color_flags & HI_SUPPLIED) && hi != NULL) { /* user specified colour */ Pixel old_hilite = cs->hilite; PictureFreeColors(dpy, Pcmap, &cs->hilite, 1, 0, True); cs->hilite = GetColor(hi); if (old_hilite != cs->hilite) { have_pixels_changed = True; } } /* user specified */ else if (hi == NULL) { Pixel old_hilite = cs->hilite; PictureFreeColors(dpy, Pcmap, &cs->hilite, 1, 0, True); cs->hilite = GetHilite(cs->bg); if (old_hilite != cs->hilite) { have_pixels_changed = True; } } } /* has_hi_changed */ /* * ---------- change the shadow colour ---------- */ if (has_sh_changed || (has_bg_changed && !(cs->color_flags & SH_SUPPLIED))) { has_sh_changed = 1; if ((cs->color_flags & SH_SUPPLIED) && sh != NULL) { /* user specified colour */ Pixel old_shadow = cs->shadow; PictureFreeColors(dpy, Pcmap, &cs->shadow, 1, 0, True); cs->shadow = GetColor(sh); if (old_shadow != cs->shadow) { have_pixels_changed = True; } } /* user specified */ else if (sh == NULL) { Pixel old_shadow = cs->shadow; PictureFreeColors(dpy, Pcmap, &cs->shadow, 1, 0, True); cs->shadow = GetShadow(cs->bg); if (old_shadow != cs->shadow) { have_pixels_changed = True; } } } /* has_sh_changed */ /* * ---------- change the shadow foreground colour ---------- */ if (has_fgsh_changed || ((has_fg_changed || has_bg_changed) && !(cs->color_flags & FGSH_SUPPLIED))) { has_fgsh_changed = 1; if ((cs->color_flags & FGSH_SUPPLIED) && fgsh != NULL) { /* user specified colour */ Pixel old_fgsh = cs->fgsh; PictureFreeColors(dpy, Pcmap, &cs->fgsh, 1, 0, True); cs->fgsh = GetColor(fgsh); if (old_fgsh != cs->fgsh) { have_pixels_changed = True; } } /* user specified */ else if (fgsh == NULL) { Pixel old_fgsh = cs->fgsh; PictureFreeColors(dpy, Pcmap, &cs->fgsh, 1, 0, True); cs->fgsh = GetForeShadow(cs->fg, cs->bg); if (old_fgsh != cs->fgsh) { have_pixels_changed = True; } } } /* has_fgsh_changed */ /* * ------- the pixmap is a bitmap: create here cs->pixmap ------- */ if (cs->picture != None && pixmap_is_a_bitmap && (has_pixmap_changed || has_bg_changed)) { cs->pixmap = XCreatePixmap( dpy, win, cs->width, cs->height, Pdepth); XSetBackground(dpy, gc, cs->bg); XSetForeground(dpy, gc, cs->fg); reset_cs_pixmap(cs, gc); } /* * ------- change the masked out parts of the background pixmap ------- */ if (cs->pixmap != None && cs->pixmap != ParentRelative && (!CSETS_IS_TRANSPARENT_ROOT(cs)|| cs->allows_buffered_transparency) && (cs->mask != None || cs->alpha_pixmap != None || cs->image_alpha_percent < 100 || cs->tint_percent > 0) && (has_pixmap_changed || has_bg_changed || has_image_alpha_changed || has_tint_changed)) { /* Now that we know the background colour we can update the * pixmap background. */ FvwmRenderAttributes fra; Pixmap temp, mask, alpha; memset(&fra, 0, sizeof(fra)); temp = XCreatePixmap(dpy, win, cs->width, cs->height, Pdepth); if (cs->picture != NULL) { mask = cs->picture->mask; alpha = cs->picture->alpha; } else { mask = None; alpha = None; } XSetForeground(dpy, gc, cs->bg); XFillRectangle( dpy, temp, gc, 0, 0, cs->width, cs->height); fra.mask = FRAM_HAVE_ADDED_ALPHA | FRAM_HAVE_TINT; fra.added_alpha_percent = cs->image_alpha_percent; fra.tint = cs->tint; fra.tint_percent = cs->tint_percent; PGraphicsRenderPixmaps( dpy, win, cs->pixmap, mask, alpha, Pdepth, &fra, temp, gc, Scr.MonoGC, Scr.AlphaGC, 0, 0, cs->width, cs->height, 0, 0, cs->width, cs->height, False); if (cs->pixmap != root_pic.pixmap) { add_to_junk(cs->pixmap); } cs->pixmap = temp; has_pixmap_changed = True; if (CSETS_IS_TRANSPARENT_ROOT(cs)) { cs->pixmap_type = PIXMAP_ROOT_PIXMAP_TRAN; } } /* has_pixmap_changed */ /* * ---------- change the icon tint colour ---------- */ if (has_icon_tint_changed) { /* user specified colour */ if (icon_tint != NULL) { Pixel old_tint = cs->icon_tint; PictureFreeColors( dpy, Pcmap, &cs->icon_tint, 1, 0, True); cs->icon_tint = GetColor(icon_tint); if (old_tint != cs->icon_tint) { has_icon_pixels_changed = True; } } else { /* default */ Pixel old_tint = cs->icon_tint; PictureFreeColors( dpy, Pcmap, &cs->icon_tint, 1, 0, True); cs->icon_tint = GetColor(black); if (old_tint != cs->icon_tint) { has_icon_pixels_changed = True; } } } /* * ---------- send new colorset to fvwm and clean up ---------- */ /* make sure the server has this to avoid races */ XSync(dpy, False); /* inform modules of the change */ if (have_pixels_changed || has_pixmap_changed || has_shape_changed || has_fg_alpha_changed || has_icon_pixels_changed) { BroadcastColorset(n); } if (fg) { free(fg); } if (bg) { free(bg); } if (hi) { free(hi); } if (sh) { free(sh); } if (fgsh) { free(fgsh); } if (tint) { free(tint); } if (fg_tint) { free(fg_tint); } if (bg_tint) { free(bg_tint); } if (icon_tint) { free(icon_tint); } return; }
/*************************************<->************************************* * * Cursor _DtHelpGetHourGlassCursor () * * * Description: * ----------- * Builds and returns the appropriate Hourglass cursor * * * Inputs: * ------ * dpy = display * * Outputs: * ------- * Return = cursor. * * Comments: * -------- * None. (None doesn't count as a comment) * *************************************<->***********************************/ Cursor _DtHelpGetHourGlassCursor( Display *dpy ) { char *bits; char *maskBits; unsigned int width; unsigned int height; unsigned int xHotspot; unsigned int yHotspot; Pixmap pixmap; Pixmap maskPixmap; XColor xcolors[2]; unsigned int cWidth; unsigned int cHeight; int useLargeCursors = 0; static Cursor waitCursor=0; if (waitCursor != 0) return(waitCursor); if (XQueryBestCursor (dpy, DefaultRootWindow(dpy), 32, 32, &cWidth, &cHeight)) { if ((cWidth >= 32) && (cHeight >= 32)) { useLargeCursors = 1; } } if (useLargeCursors) { width = time32_width; height = time32_height; bits = (char *) time32_bits; maskBits = (char *) time32m_bits; xHotspot = time32_x_hot; yHotspot = time32_y_hot; } else { width = time16_width; height = time16_height; bits = (char *) time16_bits; maskBits = (char *) time16m_bits; xHotspot = time16_x_hot; yHotspot = time16_y_hot; } pixmap = XCreateBitmapFromData (dpy, DefaultRootWindow(dpy), bits, width, height); maskPixmap = XCreateBitmapFromData (dpy, DefaultRootWindow(dpy), maskBits, width, height); xcolors[0].pixel = BlackPixelOfScreen(DefaultScreenOfDisplay(dpy)); xcolors[1].pixel = WhitePixelOfScreen(DefaultScreenOfDisplay(dpy)); XQueryColors (dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay (dpy)), xcolors, 2); waitCursor = XCreatePixmapCursor (dpy, pixmap, maskPixmap, &(xcolors[0]), &(xcolors[1]), xHotspot, yHotspot); XFreePixmap (dpy, pixmap); XFreePixmap (dpy, maskPixmap); return (waitCursor); }
static void _find_color(Widget w, XColor *target, char *dontuse) { Colormap cm; XColor cell_def[256]; double cur_dist; int cur_red_dist_index=0; int cur_green_dist_index=0; int cur_blue_dist_index=0; double cur_red_dist; double cur_green_dist; double cur_blue_dist; double dist; double red_dist; double green_dist; double blue_dist; int i; XColor tmp = {0,0,0,0,0,0}; int screen; int ncells; Display *d = XtDisplay(w); XWindowAttributes win_att; int depth; Pixel pix; unsigned long red_mask=0; unsigned long green_mask=0; unsigned long blue_mask=0; unsigned long red_mult=0; unsigned long green_mult=0; unsigned long blue_mult=0; screen = XScreenNumberOfScreen(XtScreen(w)); XtVaGetValues(w, XmNdepth, &depth, NULL); if(depth == 8) ncells = 255; else if(depth == 12) ncells = 16; else ncells = 256; if(XtIsRealized(w)) { XGetWindowAttributes(d, XtWindow(w), &win_att); if(win_att.colormap != DefaultColormap(XtDisplay(w), screen)) { cm = win_att.colormap; red_mask = win_att.visual->red_mask; green_mask = win_att.visual->green_mask; blue_mask = win_att.visual->blue_mask; red_mult = red_mask & (~red_mask+1); green_mult = green_mask & (~green_mask+1); blue_mult = blue_mask & (~blue_mask+1); } else { cm = DefaultColormap(XtDisplay(w), screen); } } else { cm = DefaultColormap(XtDisplay(w), screen); } for (i = 0; i < ncells; i++) { switch(depth) { case 8: cell_def[i].pixel = i; break; case 12: pix = i; cell_def[i].pixel = (pix * red_mult) + (pix * green_mult) + (pix * blue_mult); break; default: pix = i; cell_def[i].pixel = (pix * red_mult) | (pix * green_mult) | (pix * blue_mult); break; } cell_def[i].flags = DoRed | DoGreen | DoBlue; } XQueryColors(d, cm, cell_def, ncells); cur_dist = sqrt((double)((double)65535*(double)65535*(double)65535)); cur_red_dist = cur_green_dist = cur_blue_dist = 65535.0*65535.0; for (i = 0; i < ncells; i++) { red_dist = (double)(target->red - cell_def[i].red) * (double)(target->red - cell_def[i].red); green_dist = (double)(target->green - cell_def[i].green) * (double)(target->green - cell_def[i].green); blue_dist = (double)(target->blue - cell_def[i].blue) * (double)(target->blue - cell_def[i].blue); switch(depth) { Boolean ok2use; case 8: dist = sqrt(red_dist + green_dist + blue_dist); if (!dontuse) ok2use = True; else ok2use = (dontuse[cell_def[i].pixel] == 0); if ((dist < cur_dist) && (ok2use)) { cur_dist = dist; tmp.red = cell_def[i].red; tmp.green = cell_def[i].green; tmp.blue = cell_def[i].blue; tmp.pixel = cell_def[i].pixel; } break; default: if(red_dist < cur_red_dist) { cur_red_dist = red_dist; cur_red_dist_index = i; } if(green_dist < cur_green_dist) { cur_green_dist = green_dist; cur_green_dist_index = i; } if(blue_dist < cur_blue_dist) { cur_blue_dist = blue_dist; cur_blue_dist_index = i; } break; } } switch(depth) { case 8: target->red = tmp.red; target->green = tmp.green; target->blue = tmp.blue; target->pixel = tmp.pixel; target->flags = tmp.flags; target->pad = tmp.pad; break; default: pix = (cell_def[cur_red_dist_index].pixel & red_mask) | (cell_def[cur_green_dist_index].pixel & green_mask) | (cell_def[cur_blue_dist_index].pixel & blue_mask); target->pixel = pix; target->red = cell_def[cur_red_dist_index].red; target->green = cell_def[cur_green_dist_index].green; target->blue = cell_def[cur_blue_dist_index].blue; break; } }
i_img * imss_x11(unsigned long display_ul, int window_id, int left, int top, int right, int bottom) { Display *display = (Display *)display_ul; int own_display = 0; /* non-zero if we connect */ XImage *image; XWindowAttributes attr; i_img *result; i_color *line, *cp; int x, y; XColor *colors; XErrorHandler old_handler; int width, height; i_clear_error(); /* we don't want the default noisy error handling */ old_handler = XSetErrorHandler(my_handler); if (!display) { display = XOpenDisplay(NULL); ++own_display; if (!display) { XSetErrorHandler(old_handler); i_push_error(0, "No display supplied and cannot connect"); return NULL; } } if (!window_id) { int screen = DefaultScreen(display); window_id = RootWindow(display, screen); } if (!XGetWindowAttributes(display, window_id, &attr)) { XSetErrorHandler(old_handler); if (own_display) XCloseDisplay(display); i_push_error(0, "Cannot XGetWindowAttributes"); return NULL; } /* adjust negative/zero values to window size */ if (left < 0) left += attr.width; if (top < 0) top += attr.height; if (right <= 0) right += attr.width; if (bottom <= 0) bottom += attr.height; /* clamp */ if (left < 0) left = 0; if (right > attr.width) right = attr.width; if (top < 0) top = 0; if (bottom > attr.height) bottom = attr.height; /* validate */ if (right <= left || bottom <= top) { XSetErrorHandler(old_handler); if (own_display) XCloseDisplay(display); i_push_error(0, "image would be empty"); return NULL; } width = right - left; height = bottom - top; image = XGetImage(display, window_id, left, top, width, height, -1, ZPixmap); if (!image) { XSetErrorHandler(old_handler); if (own_display) XCloseDisplay(display); i_push_error(0, "Cannot XGetImage"); return NULL; } result = i_img_8_new(width, height, 3); line = mymalloc(sizeof(i_color) * width); colors = mymalloc(sizeof(XColor) * width); for (y = 0; y < height; ++y) { cp = line; /* XQueryColors seems to be a round-trip, so do one big request instead of one per pixel */ for (x = 0; x < width; ++x) { colors[x].pixel = XGetPixel(image, x, y); } XQueryColors(display, attr.colormap, colors, width); for (x = 0; x < width; ++x) { cp->rgb.r = colors[x].red >> 8; cp->rgb.g = colors[x].green >> 8; cp->rgb.b = colors[x].blue >> 8; ++cp; } i_plin(result, 0, width, y, line); } myfree(line); myfree(colors); XDestroyImage(image); XSetErrorHandler(old_handler); if (own_display) XCloseDisplay(display); i_tags_setn(&result->tags, "ss_window_width", attr.width); i_tags_setn(&result->tags, "ss_window_height", attr.height); i_tags_set(&result->tags, "ss_type", "X11", 3); i_tags_setn(&result->tags, "ss_left", left); i_tags_setn(&result->tags, "ss_top", top); return result; }
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % % % R e a d D P S I m a g e % % % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % ReadDPSImage() reads a Adobe Postscript image file and returns it. It % allocates the memory necessary for the new Image structure and returns a % pointer to the new image. % % The format of the ReadDPSImage method is: % % Image *ReadDPSImage(const ImageInfo *image_info,ExceptionInfo *exception) % % A description of each parameter follows: % % o image_info: the image info. % % o exception: return any errors or warnings in this structure. % */ static Image *ReadDPSImage(const ImageInfo *image_info,ExceptionInfo *exception) { const char *client_name; Display *display; float pixels_per_point; Image *image; int sans, status; Pixmap pixmap; register ssize_t i; register Quantum *q; register size_t pixel; Screen *screen; ssize_t x, y; XColor *colors; XImage *dps_image; XRectangle page, bits_per_pixel; XResourceInfo resource_info; XrmDatabase resource_database; XStandardColormap *map_info; XVisualInfo *visual_info; /* Open X server connection. */ assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); assert(exception->signature == MagickSignature); display=XOpenDisplay(image_info->server_name); if (display == (Display *) NULL) return((Image *) NULL); /* Set our forgiving exception handler. */ (void) XSetErrorHandler(XError); /* Open image file. */ image=AcquireImage(image_info,exception); status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); if (status == MagickFalse) return((Image *) NULL); /* Get user defaults from X resource database. */ client_name=GetClientName(); resource_database=XGetResourceDatabase(display,client_name); XGetResourceInfo(image_info,resource_database,client_name,&resource_info); /* Allocate standard colormap. */ map_info=XAllocStandardColormap(); visual_info=(XVisualInfo *) NULL; if (map_info == (XStandardColormap *) NULL) ThrowReaderException(ResourceLimitError,"UnableToCreateStandardColormap") else { /* Initialize visual info. */ (void) CloneString(&resource_info.visual_type,"default"); visual_info=XBestVisualInfo(display,map_info,&resource_info); map_info->colormap=(Colormap) NULL; } if ((map_info == (XStandardColormap *) NULL) || (visual_info == (XVisualInfo *) NULL)) { image=DestroyImage(image); XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL, (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL); return((Image *) NULL); } /* Create a pixmap the appropriate size for the image. */ screen=ScreenOfDisplay(display,visual_info->screen); pixels_per_point=XDPSPixelsPerPoint(screen); if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0)) pixels_per_point=MagickMin(image->resolution.x,image->resolution.y)/ DefaultResolution; status=XDPSCreatePixmapForEPSF((DPSContext) NULL,screen, GetBlobFileHandle(image),visual_info->depth,pixels_per_point,&pixmap, &bits_per_pixel,&page); if ((status == dps_status_failure) || (status == dps_status_no_extension)) { image=DestroyImage(image); XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL, (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL); return((Image *) NULL); } /* Rasterize the file into the pixmap. */ status=XDPSImageFileIntoDrawable((DPSContext) NULL,screen,pixmap, GetBlobFileHandle(image),(int) bits_per_pixel.height,visual_info->depth, &page,-page.x,-page.y,pixels_per_point,MagickTrue,MagickFalse,MagickTrue, &sans); if (status != dps_status_success) { image=DestroyImage(image); XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL, (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL); return((Image *) NULL); } /* Initialize DPS X image. */ dps_image=XGetImage(display,pixmap,0,0,bits_per_pixel.width, bits_per_pixel.height,AllPlanes,ZPixmap); (void) XFreePixmap(display,pixmap); if (dps_image == (XImage *) NULL) { image=DestroyImage(image); XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL, (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL); return((Image *) NULL); } /* Get the colormap colors. */ colors=(XColor *) AcquireQuantumMemory(visual_info->colormap_size, sizeof(*colors)); if (colors == (XColor *) NULL) { image=DestroyImage(image); XDestroyImage(dps_image); XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL, (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL); return((Image *) NULL); } if ((visual_info->klass != DirectColor) && (visual_info->klass != TrueColor)) for (i=0; i < visual_info->colormap_size; i++) { colors[i].pixel=(size_t) i; colors[i].pad=0; } else { size_t blue, blue_bit, green, green_bit, red, red_bit; /* DirectColor or TrueColor visual. */ red=0; green=0; blue=0; red_bit=visual_info->red_mask & (~(visual_info->red_mask)+1); green_bit=visual_info->green_mask & (~(visual_info->green_mask)+1); blue_bit=visual_info->blue_mask & (~(visual_info->blue_mask)+1); for (i=0; i < visual_info->colormap_size; i++) { colors[i].pixel=red | green | blue; colors[i].pad=0; red+=red_bit; if (red > visual_info->red_mask) red=0; green+=green_bit; if (green > visual_info->green_mask) green=0; blue+=blue_bit; if (blue > visual_info->blue_mask) blue=0; } } (void) XQueryColors(display,XDefaultColormap(display,visual_info->screen), colors,visual_info->colormap_size); /* Convert X image to MIFF format. */ if ((visual_info->klass != TrueColor) && (visual_info->klass != DirectColor)) image->storage_class=PseudoClass; image->columns=(size_t) dps_image->width; image->rows=(size_t) dps_image->height; if (image_info->ping != MagickFalse) { (void) CloseBlob(image); return(GetFirstImageInList(image)); } status=SetImageExtent(image,image->columns,image->rows,exception); if (status == MagickFalse) return(DestroyImageList(image)); switch (image->storage_class) { case DirectClass: default: { register size_t color, index; size_t blue_mask, blue_shift, green_mask, green_shift, red_mask, red_shift; /* Determine shift and mask for red, green, and blue. */ red_mask=visual_info->red_mask; red_shift=0; while ((red_mask != 0) && ((red_mask & 0x01) == 0)) { red_mask>>=1; red_shift++; } green_mask=visual_info->green_mask; green_shift=0; while ((green_mask != 0) && ((green_mask & 0x01) == 0)) { green_mask>>=1; green_shift++; } blue_mask=visual_info->blue_mask; blue_shift=0; while ((blue_mask != 0) && ((blue_mask & 0x01) == 0)) { blue_mask>>=1; blue_shift++; } /* Convert X image to DirectClass packets. */ if ((visual_info->colormap_size > 0) && (visual_info->klass == DirectColor)) for (y=0; y < (ssize_t) image->rows; y++) { q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); if (q == (Quantum *) NULL) break; for (x=0; x < (ssize_t) image->columns; x++) { pixel=XGetPixel(dps_image,x,y); index=(pixel >> red_shift) & red_mask; SetPixelRed(image,ScaleShortToQuantum(colors[index].red),q); index=(pixel >> green_shift) & green_mask; SetPixelGreen(image,ScaleShortToQuantum(colors[index].green),q); index=(pixel >> blue_shift) & blue_mask; SetPixelBlue(image,ScaleShortToQuantum(colors[index].blue),q); q+=GetPixelChannels(image); } if (SyncAuthenticPixels(image,exception) == MagickFalse) break; if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse) break; } else for (y=0; y < (ssize_t) image->rows; y++) { q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); if (q == (Quantum *) NULL) break; for (x=0; x < (ssize_t) image->columns; x++) { pixel=XGetPixel(dps_image,x,y); color=(pixel >> red_shift) & red_mask; color=(color*65535L)/red_mask; SetPixelRed(image,ScaleShortToQuantum((unsigned short) color),q); color=(pixel >> green_shift) & green_mask; color=(color*65535L)/green_mask; SetPixelGreen(image,ScaleShortToQuantum((unsigned short) color),q); color=(pixel >> blue_shift) & blue_mask; color=(color*65535L)/blue_mask; SetPixelBlue(image,ScaleShortToQuantum((unsigned short) color),q); q+=GetPixelChannels(image); } if (SyncAuthenticPixels(image,exception) == MagickFalse) break; if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse) break; } break; } case PseudoClass: { /* Create colormap. */ if (AcquireImageColormap(image,(size_t) visual_info->colormap_size,exception) == MagickFalse) { image=DestroyImage(image); colors=(XColor *) RelinquishMagickMemory(colors); XDestroyImage(dps_image); XFreeResources(display,visual_info,map_info,(XPixelInfo *) NULL, (XFontStruct *) NULL,&resource_info,(XWindowInfo *) NULL); return((Image *) NULL); } for (i=0; i < (ssize_t) image->colors; i++) { image->colormap[colors[i].pixel].red=ScaleShortToQuantum(colors[i].red); image->colormap[colors[i].pixel].green= ScaleShortToQuantum(colors[i].green); image->colormap[colors[i].pixel].blue= ScaleShortToQuantum(colors[i].blue); } /* Convert X image to PseudoClass packets. */ for (y=0; y < (ssize_t) image->rows; y++) { q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); if (q == (Quantum *) NULL) break; for (x=0; x < (ssize_t) image->columns; x++) { SetPixelIndex(image,(unsigned short) XGetPixel(dps_image,x,y),q); q+=GetPixelChannels(image); } if (SyncAuthenticPixels(image,exception) == MagickFalse) break; if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse) break; } break; } }
void xqcmps ( int *icbank, int *ncolr, int *ired, int *igreen, int *iblue, int *iret ) /************************************************************************ * xqcmps * * * * xqcmps ( icbank, ncolr, ired, igreen, iblue, iret ) * * * * This subroutine returns RGB values for the specified color bank. * * * * Input parameters: * * *icbank int Color bank ID * * * * Output parameters: * * *ncolr int number of colors in the bank * * *ired int Red color component * * *igreen int Green color component * * *iblue int Blue color component * * *iret int Return code * * 0 = successful * * -1 = bank ID out of range * * -2 = color bank is not allocated * ** * * Log: * * C. Lin/EAI 5/96 * * T. Piper/GSC 3/01 Fixed IRIX6 compiler warnings * * S. Chiswell 8/02 Changed pixel range check for 8 bit * * R. Tian/SAIC 3/03 Added check for color bank * * T. Piper/SAIC 07/04 Minor clean-up * ***********************************************************************/ { int ii, ncolors, cbank; int xdpth; XColor xcolors[MAXCOLORS]; /*---------------------------------------------------------------------*/ *iret = 0; cbank = *icbank; if (cbank > ColorBanks.nbank - 1) { *iret = -1; return; } xdpth = DefaultDepth ( (XtPointer)gemdisplay, DefaultScreen((XtPointer)gemdisplay) ); ncolors = ColorBanks.banks[cbank]; if ( ncolors <= 0 || ncolors > MAXCOLORS ) { *iret = -2; return; } /* * Check that the color bank has already been allocated */ if ( (*icbank == GraphCid && !GColorIsInitialized ) || (*icbank > GraphCid && !allocflag[*icbank]) ) { *iret = G_NCBALOC; return; } for ( ii = 0; ii < ncolors; ii++) { xcolors[ii].pixel = ColorBanks.colrs[cbank][ii]; if ( ( xdpth == 8 ) && ( xcolors[ii].pixel > (Pixel)255 ) ) { *iret = -2; return; } xcolors[ii].flags = DoRed | DoGreen | DoBlue; } XQueryColors(gemdisplay, gemmap, xcolors, ncolors); for ( ii = 0; ii < ncolors; ii++) { ired[ii] = xcolors[ii].red / COLR_SCAL; igreen[ii] = xcolors[ii].green / COLR_SCAL; iblue[ii] = xcolors[ii].blue / COLR_SCAL; } *ncolr = ncolors; }
ImageDraw::operator Image() const { GuiLock __; XImage *xim = XGetImage(Xdisplay, dw, 0, 0, max(size.cx, 1), max(size.cy, 1), AllPlanes, ZPixmap); if(!xim) return Null; Visual *v = DefaultVisual(Xdisplay, Xscreenno); RasterFormat fmt; RGBA palette[256]; switch(xim->depth) { case 15: case 16: if(xim->byte_order == LSBFirst) fmt.Set16le(v->red_mask, v->green_mask, v->blue_mask); else fmt.Set16be(v->red_mask, v->green_mask, v->blue_mask); break; case 8: { int n = min(v->map_entries, 256); XColor colors[256]; for(int i = 0; i < 256; i++) { colors[i].pixel = i; colors[i].flags = DoRed|DoGreen|DoBlue; } XQueryColors(Xdisplay, Xcolormap, colors, n); XColor *s = colors; XColor *e = s + n; while(s < e) { RGBA& t = palette[s->pixel]; t.r = s->red >> 8; t.g = s->green >> 8; t.b = s->blue >> 8; t.a = 255; s++; } fmt.Set8(); break; } default: if(xim->bits_per_pixel == 32) if(xim->byte_order == LSBFirst) fmt.Set32le(v->red_mask, v->green_mask, v->blue_mask); else fmt.Set32be(v->red_mask, v->green_mask, v->blue_mask); else if(xim->byte_order == LSBFirst) fmt.Set24le(v->red_mask, v->green_mask, v->blue_mask); else fmt.Set24be(v->red_mask, v->green_mask, v->blue_mask); break; } ImageBuffer ib(size); const byte *s = (const byte *)xim->data; RGBA *t = ib; for(int y = 0; y < size.cy; y++) { fmt.Read(t, s, size.cx, palette); s += xim->bytes_per_line; t += size.cx; } XDestroyImage(xim); if(has_alpha) { xim = XGetImage(Xdisplay, alpha.dw, 0, 0, max(size.cx, 1), max(size.cy, 1), AllPlanes, ZPixmap); if(xim) { const byte *s = (const byte *)xim->data; t = ib; Buffer<RGBA> line(size.cx); for(int y = 0; y < size.cy; y++) { fmt.Read(line, s, size.cx, palette); for(int x = 0; x < size.cx; x++) (t++)->a = line[x].r; s += xim->bytes_per_line; } XDestroyImage(xim); } } Premultiply(ib); return ib; }
static XColor *allocatePseudoColor(RContext * ctx) { XColor *colors; XColor avcolors[256]; int avncolors; int i, ncolors, r, g, b; int retries; int cpc = ctx->attribs->colors_per_channel; ncolors = cpc * cpc * cpc; if (ncolors > (1 << ctx->depth)) { /* reduce colormap size */ cpc = ctx->attribs->colors_per_channel = 1 << ((int) ctx->depth / 3); ncolors = cpc * cpc * cpc; } assert(cpc >= 2 && ncolors <= (1 << ctx->depth)); colors = malloc(sizeof(XColor) * ncolors); if (!colors) { RErrorCode = RERR_NOMEMORY; return NULL; } i = 0; if ((ctx->attribs->flags & RC_GammaCorrection) && ctx->attribs->rgamma > 0 && ctx->attribs->ggamma > 0 && ctx->attribs->bgamma > 0) { double rg, gg, bg; double tmp; /* do gamma correction */ rg = 1.0 / ctx->attribs->rgamma; gg = 1.0 / ctx->attribs->ggamma; bg = 1.0 / ctx->attribs->bgamma; for (r = 0; r < cpc; r++) { for (g = 0; g < cpc; g++) { for (b = 0; b < cpc; b++) { colors[i].red = (r * 0xffff) / (cpc - 1); colors[i].green = (g * 0xffff) / (cpc - 1); colors[i].blue = (b * 0xffff) / (cpc - 1); colors[i].flags = DoRed | DoGreen | DoBlue; tmp = (double) colors[i].red / 65536.0; colors[i].red = (unsigned short) (65536.0 * pow(tmp, rg)); tmp = (double) colors[i].green / 65536.0; colors[i].green = (unsigned short) (65536.0 * pow(tmp, gg)); tmp = (double) colors[i].blue / 65536.0; colors[i].blue = (unsigned short) (65536.0 * pow(tmp, bg)); i++; } } } } else { for (r = 0; r < cpc; r++) { for (g = 0; g < cpc; g++) { for (b = 0; b < cpc; b++) { colors[i].red = (r * 0xffff) / (cpc - 1); colors[i].green = (g * 0xffff) / (cpc - 1); colors[i].blue = (b * 0xffff) / (cpc - 1); colors[i].flags = DoRed | DoGreen | DoBlue; i++; } } } } /* try to allocate the colors */ for (i = 0; i < ncolors; i++) { if (!XAllocColor(ctx->dpy, ctx->cmap, &(colors[i]))) { colors[i].flags = 0; /* failed */ } else { colors[i].flags = DoRed | DoGreen | DoBlue; } } /* try to allocate close values for the colors that couldn't * be allocated before */ avncolors = (1 << ctx->depth > 256 ? 256 : 1 << ctx->depth); for (i = 0; i < avncolors; i++) avcolors[i].pixel = i; XQueryColors(ctx->dpy, ctx->cmap, avcolors, avncolors); for (i = 0; i < ncolors; i++) { if (colors[i].flags == 0) { int j; unsigned long cdiff = 0xffffffff, diff; unsigned long closest = 0; retries = 2; while (retries--) { /* find closest color */ for (j = 0; j < avncolors; j++) { r = (colors[i].red - avcolors[i].red) >> 8; g = (colors[i].green - avcolors[i].green) >> 8; b = (colors[i].blue - avcolors[i].blue) >> 8; diff = r * r + g * g + b * b; if (diff < cdiff) { cdiff = diff; closest = j; } } /* allocate closest color found */ colors[i].red = avcolors[closest].red; colors[i].green = avcolors[closest].green; colors[i].blue = avcolors[closest].blue; if (XAllocColor(ctx->dpy, ctx->cmap, &colors[i])) { colors[i].flags = DoRed | DoGreen | DoBlue; break; /* succeeded, don't need to retry */ } #ifdef DEBUG printf("close color allocation failed. Retrying...\n"); #endif } } }