⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdk-pixbuf-xlib-drawable.c

📁 linux下电话本所依赖的一些图形库
💻 C
📖 第 1 页 / 共 3 页
字号:
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- *//* GdkPixbuf library - convert X drawable information to RGB * * Copyright (C) 1999 Michael Zucchi * * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au> *          Cody Russell <bratsche@dfw.net> * 	    Federico Mena-Quintero <federico@gimp.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *//* Ported to Xlib by John Harper <john@dcs.warwick.ac.uk> */#include <config.h>#include <stdio.h>#include <string.h>#include <gdk-pixbuf/gdk-pixbuf-private.h>#include "gdk-pixbuf-xlib-private.h"#include <X11/Xlib.h>#include <X11/Xutil.h>#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)#define LITTLE#endif#define d(x)static guint32 mask_table[] = {	0x00000000, 0x00000001, 0x00000003, 0x00000007,	0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,	0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,	0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,	0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,	0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,	0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,	0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,	0xffffffff};/* color handling */typedef struct xlib_colormap_struct xlib_colormap;struct xlib_colormap_struct {	int size;	XColor *colors;	Visual *visual;	Colormap colormap;};static xlib_colormap *xlib_get_colormap (Colormap id, Visual *visual){	int i;	xlib_colormap *xc = g_new (xlib_colormap, 1);	xc->size = visual->map_entries;	xc->colors = g_new (XColor, xc->size);	xc->visual = visual;	xc->colormap = id;	for (i = 0; i < xc->size; i++) {		xc->colors[i].pixel = i;		xc->colors[i].flags = DoRed | DoGreen | DoBlue;	}	XQueryColors (gdk_pixbuf_dpy, xc->colormap, xc->colors, xc->size);	return xc;}static voidxlib_colormap_free (xlib_colormap *xc){	g_free (xc->colors);	g_free (xc);}/* from gdkvisual.c */static voidvisual_decompose_mask (gulong  mask,		       gint   *shift,		       gint   *prec){	*shift = 0;	*prec = 0;	while (!(mask & 0x1)) {		(*shift)++;		mask >>= 1;	}	while (mask & 0x1) {		(*prec)++;		mask >>= 1;	}}static gboolean x_error;static inthandle_x_error (Display *dpy, XErrorEvent *ev){	x_error = TRUE;	return 0;}static gbooleandrawable_is_pixmap (Drawable d){	/* copied from Imlib */	XErrorHandler errh;	XWindowAttributes wa;	gboolean is_pixmap;	errh = XSetErrorHandler (handle_x_error);	x_error = FALSE;	XGetWindowAttributes (gdk_pixbuf_dpy, d, &wa);	XSync (gdk_pixbuf_dpy, False);	is_pixmap = x_error;	XSetErrorHandler (errh);	return is_pixmap;}/*  convert 1 bits-pixel data  no alpha*/static voidrgb1 (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap){	int xx, yy;	int width, height;	int bpl;	guint8 *s;	register guint8 data;	guint8 *o;	guint8 *srow = image->data, *orow = pixels;	d (printf ("1 bits/pixel\n"));	/* convert upto 8 pixels/time */	/* its probably not worth trying to make this run very fast, who uses	   1 bit displays anymore? */	width = image->width;	height = image->height;	bpl = image->bytes_per_line;	for (yy = 0; yy < height; yy++) {		s = srow;		o = orow;		for (xx = 0; xx < width; xx ++) {			data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;			*o++ = colormap->colors[data].red;			*o++ = colormap->colors[data].green;			*o++ = colormap->colors[data].blue;		}		srow += bpl;		orow += rowstride;	}}/*  convert 1 bits/pixel data  with alpha*/static voidrgb1a (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap){	int xx, yy;	int width, height;	int bpl;	guint8 *s;	register guint8 data;	guint8 *o;	guint8 *srow = image->data, *orow = pixels;	guint32 remap[2];	d (printf ("1 bits/pixel\n"));	/* convert upto 8 pixels/time */	/* its probably not worth trying to make this run very fast, who uses	   1 bit displays anymore? */	width = image->width;	height = image->height;	bpl = image->bytes_per_line;	for (xx = 0; xx < 2; xx++) {#ifdef LITTLE		remap[xx] = 0xff000000			| colormap->colors[xx].blue << 16			| colormap->colors[xx].green << 8			| colormap->colors[xx].red;#else		remap[xx] = 0xff			| colormap->colors[xx].red << 24			| colormap->colors[xx].green << 16			| colormap->colors[xx].blue << 8;#endif	}	for (yy = 0; yy < height; yy++) {		s = srow;		o = orow;		for (xx = 0; xx < width; xx ++) {			data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;			*o++ = remap[data];		}		srow += bpl;		orow += rowstride;	}}/*  convert 8 bits/pixel data  no alpha*/static voidrgb8 (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap){	int xx, yy;	int width, height;	int bpl;	guint32 mask;	register guint32 data;	guint8 *srow = image->data, *orow = pixels;	register guint8 *s;	register guint8 *o;	width = image->width;	height = image->height;	bpl = image->bytes_per_line;	d (printf ("8 bit, no alpha output\n"));	mask = mask_table[image->depth];	for (yy = 0; yy < height; yy++) {		s = srow;		o = orow;		for (xx = 0; xx < width; xx++) {			data = *s++ & mask;			*o++ = colormap->colors[data].red;			*o++ = colormap->colors[data].green;			*o++ = colormap->colors[data].blue;		}		srow += bpl;		orow += rowstride;	}}/*  convert 8 bits/pixel data  with alpha*/static voidrgb8a (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap){	int xx, yy;	int width, height;	int bpl;	guint32 mask;	register guint32 data;	guint32 remap[256];	register guint8 *s;	/* read 2 pixels at once */	register guint32 *o;	guint8 *srow = image->data, *orow = pixels;	width = image->width;	height = image->height;	bpl = image->bytes_per_line;	d (printf ("8 bit, with alpha output\n"));	mask = mask_table[image->depth];	for (xx = 0; xx < colormap->size; xx++) {#ifdef LITTLE		remap[xx] = 0xff000000			| colormap->colors[xx].blue << 16			| colormap->colors[xx].green << 8			| colormap->colors[xx].red;#else		remap[xx] = 0xff			| colormap->colors[xx].red << 24			| colormap->colors[xx].green << 16			| colormap->colors[xx].blue << 8;#endif	}	for (yy = 0; yy < height; yy++) {		s = srow;		o = (guint32 *) orow;		for (xx = 0; xx < width; xx ++) {			data = *s++ & mask;			*o++ = remap[data];		}		srow += bpl;		orow += rowstride;	}}/*  convert 16 bits/pixel data  no alpha  data in lsb format*/static voidrgb565lsb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap){	int xx, yy;	int width, height;	int bpl;#ifdef LITTLE	register guint32 *s;	/* read 2 pixels at once */#else	register guint8 *s;	/* read 2 pixels at once */#endif	register guint16 *o;	guint8 *srow = image->data, *orow = pixels;	width = image->width;	height = image->height;	bpl = image->bytes_per_line;	for (yy = 0; yy < height; yy++) {#ifdef LITTLE		s = (guint32 *) srow;#else		s = srow;#endif		o = (guint16 *) orow;		for (xx = 1; xx < width; xx += 2) {			register guint32 data;#ifdef LITTLE			data = *s++;			*o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13				| (data & 0x7e0) << 5 | (data & 0x600) >> 1;			*o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2				| (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;			*o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25				| (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;#else			/* swap endianness first */			data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;			s += 4;			*o++ = (data & 0xf800) | (data & 0xe000) >> 5				| (data & 0x7e0) >> 3 | (data & 0x600) >> 9;			*o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6				| (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;			*o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17				| (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;#endif		}		/* check for last remaining pixel */		if (width & 1) {			register guint16 data;#ifdef LITTLE			data = *((short *) s);#else			data = *((short *) s);			data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);#endif			((char *) o)[0] = ((data >> 8) & 0xf8) | ((data >> 13) & 0x7);			((char *) o)[1] = ((data >> 3) & 0xfc) | ((data >> 9) & 0x3);			((char *) o)[2] = ((data << 3) & 0xf8) | ((data >> 2) & 0x7);		}		srow += bpl;		orow += rowstride;	}}/*  convert 16 bits/pixel data  no alpha  data in msb format*/static voidrgb565msb (XImage *image, guchar *pixels, int rowstride, xlib_colormap *colormap){	int xx, yy;	int width, height;	int bpl;#ifdef LITTLE	register guint8 *s;	/* need to swap data order */#else	register guint32 *s;	/* read 2 pixels at once */#endif	register guint16 *o;	guint8 *srow = image->data, *orow = pixels;	width = image->width;	height = image->height;	bpl = image->bytes_per_line;	for (yy = 0; yy < height; yy++) {#ifdef LITTLE		s = srow;#else		s = (guint32 *) srow;#endif		o = (guint16 *) orow;		for (xx = 1; xx < width; xx += 2) {			register guint32 data;#ifdef LITTLE			/* swap endianness first */			data = s[1] | s[0] << 8 | s[3] << 16 | s[2] << 24;			s += 4;			*o++ = (data & 0xf800) >> 8 | (data & 0xe000) >> 13				| (data & 0x7e0) << 5 | (data & 0x600) >> 1;			*o++ = (data & 0x1f) << 3 | (data & 0x1c) >> 2				| (data & 0xf8000000) >> 16 | (data & 0xe0000000) >> 21;			*o++ = (data & 0x7e00000) >> 19 | (data & 0x6000000) >> 25				| (data & 0x1f0000) >> 5 | (data & 0x1c0000) >> 10;#else			data = *s++;			*o++ = (data & 0xf800) | (data & 0xe000) >> 5				| (data & 0x7e0) >> 3 | (data & 0x600) >> 9;			*o++ = (data & 0x1f) << 11 | (data & 0x1c) << 6				| (data & 0xf8000000) >> 24 | (data & 0xe0000000) >> 29;			*o++ = (data & 0x7e00000) >> 11 | (data & 0x6000000) >> 17				| (data & 0x1f0000) >> 13 | (data & 0x1c0000) >> 18;#endif		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -