wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / misc.c
blob4a7c06573b6bc36c33449d83030b96b1d3c17cc0
1 /*
2 * Raster graphics library
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
21 #include <config.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <X11/Xlib.h>
28 #include "wraster.h"
30 void RBevelImage(RImage * image, int bevel_type)
32 RColor color;
33 RColor cdelta;
34 int w, h;
36 if (image->width < 3 || image->height < 3)
37 return;
39 w = image->width;
40 h = image->height;
41 if (bevel_type > 0) { /* raised */
42 /* top */
43 cdelta.alpha = 0;
44 cdelta.red = cdelta.green = cdelta.blue = 80;
45 ROperateLine(image, RAddOperation, 0, 0, w - 1, 0, &cdelta);
46 if (bevel_type == RBEV_RAISED3 && w > 3)
47 ROperateLine(image, RAddOperation, 1, 1, w - 3, 1, &cdelta);
49 /* left */
50 ROperateLine(image, RAddOperation, 0, 1, 0, h - 1, &cdelta);
51 if (bevel_type == RBEV_RAISED3 && h > 3)
52 ROperateLine(image, RAddOperation, 1, 2, 1, h - 3, &cdelta);
54 /* bottom */
55 color.alpha = 255;
56 color.red = color.green = color.blue = 0;
57 cdelta.red = cdelta.green = cdelta.blue = 40;
58 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
59 ROperateLine(image, RSubtractOperation, 0, h - 2, w - 3, h - 2, &cdelta);
60 RDrawLine(image, 0, h - 1, w - 1, h - 1, &color);
61 } else {
62 ROperateLine(image, RSubtractOperation, 0, h - 1, w - 1, h - 1, &cdelta);
65 /* right */
66 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
67 ROperateLine(image, RSubtractOperation, w - 2, 0, w - 2, h - 2, &cdelta);
68 RDrawLine(image, w - 1, 0, w - 1, h - 2, &color);
69 } else {
70 ROperateLine(image, RSubtractOperation, w - 1, 0, w - 1, h - 2, &cdelta);
72 } else { /* sunken */
73 cdelta.alpha = 0;
74 cdelta.red = cdelta.green = cdelta.blue = 40;
75 ROperateLine(image, RSubtractOperation, 0, 0, w - 1, 0, &cdelta); /* top */
76 ROperateLine(image, RSubtractOperation, 0, 1, 0, h - 1, &cdelta); /* left */
77 cdelta.red = cdelta.green = cdelta.blue = 80;
78 ROperateLine(image, RAddOperation, 0, h - 1, w - 1, h - 1, &cdelta); /* bottom */
79 ROperateLine(image, RAddOperation, w - 1, 0, w - 1, h - 2, &cdelta); /* right */
83 void RFillImage(RImage * image, const RColor * color)
85 unsigned char *d = image->data;
86 unsigned lineSize;
87 int i;
89 if (image->format == RRGBAFormat) {
90 for (i = 0; i < image->width; i++) {
91 *d++ = color->red;
92 *d++ = color->green;
93 *d++ = color->blue;
94 *d++ = color->alpha;
96 lineSize = image->width * 4;
97 for (i = 1; i < image->height; i++, d += lineSize) {
98 memcpy(d, image->data, lineSize);
100 } else {
101 for (i = 0; i < image->width; i++) {
102 *d++ = color->red;
103 *d++ = color->green;
104 *d++ = color->blue;
106 lineSize = image->width * 3;
107 for (i = 1; i < image->height; i++, d += lineSize) {
108 memcpy(d, image->data, lineSize);
113 void RClearImage(RImage * image, const RColor * color)
115 unsigned char *d = image->data;
116 unsigned lineSize;
117 int i;
119 if (color->alpha == 255) {
120 if (image->format == RRGBAFormat) {
121 for (i = 0; i < image->width; i++) {
122 *d++ = color->red;
123 *d++ = color->green;
124 *d++ = color->blue;
125 *d++ = 0xff;
127 lineSize = image->width * 4;
128 for (i = 1; i < image->height; i++, d += lineSize) {
129 memcpy(d, image->data, lineSize);
131 } else {
132 for (i = 0; i < image->width; i++) {
133 *d++ = color->red;
134 *d++ = color->green;
135 *d++ = color->blue;
137 lineSize = image->width * 3;
138 for (i = 1; i < image->height; i++, d += lineSize) {
139 memcpy(d, image->data, lineSize);
142 } else {
143 int bytes = image->width * image->height;
144 int alpha, nalpha, r, g, b, s;
146 alpha = color->alpha;
147 r = color->red * alpha;
148 g = color->green * alpha;
149 b = color->blue * alpha;
150 nalpha = 255 - alpha;
152 s = (image->format == RRGBAFormat) ? 4 : 3;
154 for (i = 0; i < bytes; i++, d += s) {
155 d[0] = (((int)d[0] * nalpha) + r)/256;
156 d[1] = (((int)d[1] * nalpha) + g)/256;
157 d[2] = (((int)d[2] * nalpha) + b)/256;
162 static inline unsigned char clip(int c)
164 if (c > 255)
165 c = 255;
166 return (unsigned char)c;
169 void RLightImage(RImage *image, const RColor *color)
171 unsigned char *d = image->data;
172 unsigned char *dd;
173 int alpha, r, g, b, s;
175 s = (image->format == RRGBAFormat) ? 4 : 3;
176 dd = d + s*image->width*image->height;
178 r = color->red;
179 g = color->green;
180 b = color->blue;
182 alpha = color->alpha;
184 if (r == 0 && g == 0 && b == 0) {
185 for (; d < dd; d += s) {
186 d[0] = clip(((int)d[0] * alpha)/128);
187 d[1] = clip(((int)d[1] * alpha)/128);
188 d[2] = clip(((int)d[2] * alpha)/128);
190 } else {
191 for (; d < dd; d += s) {
192 d[0] = clip((((int)d[0] * alpha) + r)/128);
193 d[1] = clip((((int)d[1] * alpha) + g)/128);
194 d[2] = clip((((int)d[2] * alpha) + b)/128);
199 const char *RMessageForError(int errorCode)
201 switch (errorCode) {
202 case RERR_NONE:
203 return "no error";
205 case RERR_OPEN:
206 return "could not open file";
208 case RERR_READ:
209 return "error reading from file";
211 case RERR_WRITE:
212 return "error writing to file";
214 case RERR_NOMEMORY:
215 return "out of memory";
217 case RERR_NOCOLOR:
218 return "out of color cells";
220 case RERR_BADIMAGEFILE:
221 return "invalid or corrupted image file";
223 case RERR_BADFORMAT:
224 return "the image format in the file is not supported and can't be loaded";
226 case RERR_BADINDEX:
227 return "image file does not contain requested image index";
229 case RERR_BADVISUALID:
230 return "request for an invalid visual ID";
232 case RERR_STDCMAPFAIL:
233 return "failed to create standard colormap";
235 case RERR_XERROR:
236 return "internal X error";
238 default:
239 case RERR_INTERNAL:
240 return "internal error";