image_xpm.c

来自「CS架构的多平台的GUI系统」· C语言 代码 · 共 437 行 · 第 1/2 页

C
437
字号
                     {"Orange", 255, 135, 0},                     {"OrangeRed", 255, 69, 0},                     {"Orchid", 239, 132, 239},                     {"PaleGoldenrod", 238, 232, 170},                     {"PaleGreen", 115, 222, 120},                     {"PaleTurquoise", 175, 238, 238},                     {"PaleVioletRed", 219, 112, 147},                     {"PapayaWhip", 255, 239, 213},                     {"PeachPuff", 255, 218, 185},                     {"peru", 205, 133, 63},                     {"Pink", 255, 181, 197},                     {"Plum", 197, 72, 155},                     {"PowderBlue", 176, 224, 230},                     {"purple", 160, 32, 240},                     {"Red", 255, 0, 0},                     {"RosyBrown", 188, 143, 143},                     {"RoyalBlue", 65, 105, 225},                     {"SaddleBrown", 139, 69, 19},                     {"Salmon", 233, 150, 122},                     {"SandyBrown", 244, 164, 96},                     {"SeaGreen", 82, 149, 132},                     {"seashell", 255, 245, 238},                     {"Sienna", 150, 82, 45},                     {"SkyBlue", 114, 159, 255},                     {"SlateBlue", 126, 136, 171},                     {"SlateGray", 112, 128, 144},                     {"snow", 255, 250, 250},                     {"SpringGreen", 65, 172, 65},                     {"SteelBlue", 84, 112, 170},                     {"Tan", 222, 184, 135},                     {"Thistle", 216, 191, 216},                     {"tomato", 255, 99, 71},                     {"Transparent", 0, 0, 1},                     {"Turquoise", 25, 204, 223},                     {"Violet", 156, 62, 206},                     {"VioletRed", 243, 62, 150},                     {"Wheat", 245, 222, 179},                     {"White", 255, 255, 255},                     {"WhiteSmoke", 245, 245, 245},                     {"Yellow", 255, 255, 0},                     {"YellowGreen", 50, 216, 56}};#define find_sign(a) \	i = 0;\	for (j = 0; j < 10; j++) {\		if ((colors_pp >= (j + 1)) && (buf_tmp[j] != ' ')) {\			k = (strchr(a, buf_tmp[j]) - a);\			for (l = j; l > 0; l--) {\				k *= 92;\			}\			i += k;\		}\	}int s_image_xpm_memcmp (char *ptr0, char *ptr1, int n){	while (n--) {		if (*ptr0 != *ptr1) {			return 1;		}		ptr0++;		ptr1++;	}	return 0;}int s_image_xpm_is (char *file){	FILE *fp;	int ret = -1;	char magic[9];	if ((fp = fopen(file, "r")) == NULL) {		debugf(DCLI | DFAT, "Coult not open file (%s) for reading");	}	fread(magic, 1, sizeof(magic), fp);	ret = memcmp(magic, "/* XPM */", 9);	fclose(fp);	return ret;}int s_image_xpm (char *file, s_image_t *img){	int i;	int j;	int k;	int l;	int m;	int x;	int y;	char *buf;	char *buf_tmp;	int colors = 0;	int colors_pp = 0;	char color_hex[10];	unsigned int *rgba_tmp = NULL;	struct xpm_rgb_s {		char sign[10];		unsigned char a;		unsigned char r;		unsigned char g;		unsigned char b;	} *rgb = NULL;	char sign_str0[] = " .+@#$%&*=-;>,')!~{]^/(_:<[}|1234567890abcdefg"                           "hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`";	char sign_str1[] = " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"	                   "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";	FILE *fp;	buf = (char *) s_malloc(sizeof(char) * BUFFSIZE + 1);	buf_tmp = buf;	if ((fp = fopen(file, "r")) == NULL) {		debugf(DCLI | DFAT, "Coult not open file (%s) for reading");	}	while (!feof(fp)) {		fgets(buf, BUFFSIZE, fp);		if (strncmp(buf, "/*", 2) == 0) {			continue;		}		if (buf[0] != '"') {			continue;		}		sscanf(buf + 1, "%d %d %d %d", &(img->w), &(img->h), &colors, &colors_pp);		rgb = (struct xpm_rgb_s *) s_calloc(1, colors * sizeof(struct xpm_rgb_s));		img->rgba = (unsigned int *) s_calloc(1, img->w * img->h * sizeof(unsigned int));		rgba_tmp = img->rgba;		break;	}	if ((rgb == NULL) || (rgba_tmp == NULL)) {		debugf(DCLI | DFAT, "Not enough memory");	}        for (i = 0; i < colors; i++) {		fgets(buf, BUFFSIZE, fp);		if (strncmp(buf, "/*", 2) == 0) {			continue;		}		for (j = 0; j < colors_pp; j++) {			rgb[i].sign[j] = buf[j + 1];		}		rgb[i].sign[j] = '\0';		if ((buf_tmp = strstr(buf, "c #")) != NULL) {			sscanf(buf_tmp, "c #%s", color_hex);			rgb[i].a = 0;			rgb[i].r = s_image_hex2int(color_hex);			rgb[i].g = s_image_hex2int(color_hex + 2);			rgb[i].b = s_image_hex2int(color_hex + 4);		} else if ((buf_tmp = strstr(buf, "c ")) != NULL) {			char rgbname[30];			sscanf(buf_tmp, "c %s", rgbname);			rgbname[strlen(rgbname) - 2] = 0;			if (strcasecmp(rgbname, "none") == 0) {				goto color_none;			}			for (k = 0; k < 234; k++) {				if (strcasecmp(rgbname, rgbRecord[k].name) == 0) {					rgb[i].a = 0;					rgb[i].r = rgbRecord[k].r;					rgb[i].g = rgbRecord[k].g;					rgb[i].b = rgbRecord[k].b;					break;				}			}		} else {color_none:		rgb[i].a = 255;			rgb[i].r = 0;			rgb[i].g = 0;			rgb[i].b = 0;		}	}	for (y = 0; y < img->h; y++) {		fgets(buf, BUFFSIZE, fp);		if (strncmp(buf, "/*", 2) == 0) {			continue;		}		buf_tmp = buf + 1;		for (x = 0; x < img->w; x++, buf_tmp += colors_pp) {			find_sign(sign_str0);			m = i;			find_sign(sign_str1);			i = (m < i) ? m : i;			for (; i < colors; i++) {				if (s_image_xpm_memcmp(buf_tmp, rgb[i].sign, colors_pp) == 0) {					*rgba_tmp |= (rgb[i].r << 0x18);					*rgba_tmp |= (rgb[i].g << 0x10);					*rgba_tmp |= (rgb[i].b << 0x08);					*rgba_tmp |= (rgb[i].a << 0x00);					rgba_tmp++;					break;				}			}		}	}	s_free(rgb);	s_free(buf);	fclose(fp);		return 0;}#elseint s_image_xpm_is (char *file){	return -1;}int s_image_xpm (char *file, s_image_t *img){	return -1;}#endif

⌨️ 快捷键说明

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