?? gdk-pixbuf-xlibrgb.c
字號(hào):
/* this is the colorcube suitable for dithering */static voidxlib_rgb_make_colorcube_d (unsigned long *pixels, int nr, int ng, int nb){ int r, g, b; int i; colorcube_d = malloc(sizeof(unsigned char) * 512); memset(colorcube_d, 0, (sizeof(unsigned char) * 512)); for (i = 0; i < 512; i++) { r = MIN (nr - 1, i >> 6); g = MIN (ng - 1, (i >> 3) & 7); b = MIN (nb - 1, i & 7); colorcube_d[i] = pixels[(r * ng + g) * nb + b]; }}/* Try installing a color cube of the specified size. Make the colorcube and return TRUE on success */static intxlib_rgb_try_colormap (int nr, int ng, int nb){ int r, g, b; int ri, gi, bi; int r0, g0, b0; Colormap cmap; XVisualInfo *visual; XColor *colors = NULL; XColor color; unsigned long pixels[256]; unsigned long junk[256]; int i; int d2; unsigned int colors_needed; int idx; int best[256]; if (nr * ng * nb < xlib_rgb_min_colors) return FALSE; if (image_info->cmap_alloced) { cmap = image_info->cmap; visual = image_info->x_visual_info; } else { cmap = image_info->default_colormap; visual = image_info->x_visual_info; } colors_needed = nr * ng * nb; for (i = 0; i < 256; i++) { best[i] = 192; pixels[i] = 256; }#ifndef GAMMA if (!xlib_rgb_install_cmap) { /* go out and get the colors for this colormap. */ colors = malloc(sizeof(XColor) * visual->colormap_size); for (i=0; i < visual->colormap_size; i++){ colors[i].pixel = i; } XQueryColors (image_info->display, cmap, colors, visual->colormap_size); /* find color cube colors that are already present */ for (i = 0; i < MIN (256, visual->colormap_size); i++) { r = colors[i].red >> 8; g = colors[i].green >> 8; b = colors[i].blue >> 8; ri = (r * (nr - 1) + 128) >> 8; gi = (g * (ng - 1) + 128) >> 8; bi = (b * (nb - 1) + 128) >> 8; r0 = ri * 255 / (nr - 1); g0 = gi * 255 / (ng - 1); b0 = bi * 255 / (nb - 1); idx = ((ri * nr) + gi) * nb + bi; d2 = (r - r0) * (r - r0) + (g - g0) * (g - g0) + (b - b0) * (b - b0); if (d2 < best[idx]) { if (pixels[idx] < 256) XFreeColors(image_info->display, cmap, pixels + idx, 1, 0); else colors_needed--; color.pixel = colors[i].pixel; color.red = colors[i].red; color.green = colors[i].green; color.blue = colors[i].blue; color.flags = 0; if (!XAllocColor(image_info->display, cmap, &color)) return xlib_rgb_cmap_fail ("error allocating system color\n", cmap, pixels); pixels[idx] = color.pixel; /* which is almost certainly i */ best[idx] = d2; } } }#endif if (colors_needed) { if (!XAllocColorCells(image_info->display, cmap, 0, NULL, 0, junk, colors_needed)) { char tmp_str[80]; sprintf (tmp_str, "%d %d %d colormap failed (in XAllocColorCells)\n", nr, ng, nb); return xlib_rgb_cmap_fail (tmp_str, cmap, pixels); } XFreeColors(image_info->display, cmap, junk, (int)colors_needed, 0); } for (r = 0, i = 0; r < nr; r++) for (g = 0; g < ng; g++) for (b = 0; b < nb; b++, i++) { if (pixels[i] == 256) { color.red = r * 65535 / (nr - 1); color.green = g * 65535 / (ng - 1); color.blue = b * 65535 / (nb - 1);#ifdef GAMMA color.red = 65535 * pow (color.red / 65535.0, 0.5); color.green = 65535 * pow (color.green / 65535.0, 0.5); color.blue = 65535 * pow (color.blue / 65535.0, 0.5);#endif /* This should be a raw XAllocColor call */ if (!XAllocColor(image_info->display, cmap, &color)) { char tmp_str[80]; sprintf (tmp_str, "%d %d %d colormap failed\n", nr, ng, nb); return xlib_rgb_cmap_fail (tmp_str, cmap, pixels); } pixels[i] = color.pixel; }#ifdef VERBOSE printf ("%d: %lx\n", i, pixels[i]);#endif } image_info->nred_shades = nr; image_info->ngreen_shades = ng; image_info->nblue_shades = nb; xlib_rgb_make_colorcube (pixels, nr, ng, nb); xlib_rgb_make_colorcube_d (pixels, nr, ng, nb); if (colors) free(colors); return TRUE;}/* Return TRUE on success. */static Boolxlib_rgb_do_colormaps (void){ static const int sizes[][3] = { /* { 6, 7, 6 }, */ { 6, 6, 6 }, { 6, 6, 5 }, { 6, 6, 4 }, { 5, 5, 5 }, { 5, 5, 4 }, { 4, 4, 4 }, { 4, 4, 3 }, { 3, 3, 3 }, { 2, 2, 2 } }; static const int n_sizes = sizeof(sizes) / (3 * sizeof(int)); int i; for (i = 0; i < n_sizes; i++) if (xlib_rgb_try_colormap (sizes[i][0], sizes[i][1], sizes[i][2])) return TRUE; return FALSE;}/* Make a 2 x 2 x 2 colorcube */static voidxlib_rgb_colorcube_222 (void){ int i; XColor color; Colormap cmap; if (image_info->cmap_alloced) cmap = image_info->cmap; else cmap = image_info->default_colormap; colorcube_d = malloc(sizeof(unsigned char) * 512); for (i = 0; i < 8; i++) { color.red = ((i & 4) >> 2) * 65535; color.green = ((i & 2) >> 1) * 65535; color.blue = (i & 1) * 65535; XAllocColor (image_info->display, cmap, &color); colorcube_d[((i & 4) << 4) | ((i & 2) << 2) | (i & 1)] = color.pixel; }}/** * xlib_rgb_set_verbose: * @verbose: %True to be verbose * * Enables/disables debug spew. **/voidxlib_rgb_set_verbose (Bool verbose){ xlib_rgb_verbose = verbose;}/** * xlib_rgb_set_install: * @install: %True to install a colormap * * Sets whether we install an RGB colormap. **/voidxlib_rgb_set_install (Bool install){ xlib_rgb_install_cmap = install;}/** * xlib_rgb_set_min_colors: * @min_colors: minimum colors to use * * Sets the minimum number of colors in the color cube. **/voidxlib_rgb_set_min_colors (int min_colors){ xlib_rgb_min_colors = min_colors;}/* Return a "score" based on the following criteria (in hex): x000 is the quality - 1 is 1bpp, 2 is 4bpp, 4 is 8bpp,
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -