util: clarify a bit of the code for parsing commands in wmgenmenu
[wmaker-crm.git] / wrlib / misc.c
blob615777e1ad8bc50cbc81a99f2363c4ecebb95053
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"
29 #include "imgformat.h"
30 #include "convert.h"
33 void RBevelImage(RImage * image, int bevel_type)
35 RColor color;
36 RColor cdelta;
37 int w, h;
39 if (image->width < 3 || image->height < 3)
40 return;
42 w = image->width;
43 h = image->height;
44 if (bevel_type > 0) { /* raised */
45 /* top */
46 cdelta.alpha = 0;
47 cdelta.red = cdelta.green = cdelta.blue = 80;
48 ROperateLine(image, RAddOperation, 0, 0, w - 1, 0, &cdelta);
49 if (bevel_type == RBEV_RAISED3 && w > 3)
50 ROperateLine(image, RAddOperation, 1, 1, w - 3, 1, &cdelta);
52 /* left */
53 ROperateLine(image, RAddOperation, 0, 1, 0, h - 1, &cdelta);
54 if (bevel_type == RBEV_RAISED3 && h > 3)
55 ROperateLine(image, RAddOperation, 1, 2, 1, h - 3, &cdelta);
57 /* bottom */
58 color.alpha = 255;
59 color.red = color.green = color.blue = 0;
60 cdelta.red = cdelta.green = cdelta.blue = 40;
61 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
62 ROperateLine(image, RSubtractOperation, 0, h - 2, w - 3, h - 2, &cdelta);
63 RDrawLine(image, 0, h - 1, w - 1, h - 1, &color);
64 } else {
65 ROperateLine(image, RSubtractOperation, 0, h - 1, w - 1, h - 1, &cdelta);
68 /* right */
69 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
70 ROperateLine(image, RSubtractOperation, w - 2, 0, w - 2, h - 2, &cdelta);
71 RDrawLine(image, w - 1, 0, w - 1, h - 2, &color);
72 } else {
73 ROperateLine(image, RSubtractOperation, w - 1, 0, w - 1, h - 2, &cdelta);
75 } else { /* sunken */
76 cdelta.alpha = 0;
77 cdelta.red = cdelta.green = cdelta.blue = 40;
78 ROperateLine(image, RSubtractOperation, 0, 0, w - 1, 0, &cdelta); /* top */
79 ROperateLine(image, RSubtractOperation, 0, 1, 0, h - 1, &cdelta); /* left */
80 cdelta.red = cdelta.green = cdelta.blue = 80;
81 ROperateLine(image, RAddOperation, 0, h - 1, w - 1, h - 1, &cdelta); /* bottom */
82 ROperateLine(image, RAddOperation, w - 1, 0, w - 1, h - 2, &cdelta); /* right */
86 void RFillImage(RImage * image, const RColor * color)
88 unsigned char *d = image->data;
89 unsigned lineSize;
90 int i;
92 if (image->format == RRGBAFormat) {
93 for (i = 0; i < image->width; i++) {
94 *d++ = color->red;
95 *d++ = color->green;
96 *d++ = color->blue;
97 *d++ = color->alpha;
99 lineSize = image->width * 4;
100 for (i = 1; i < image->height; i++, d += lineSize) {
101 memcpy(d, image->data, lineSize);
103 } else {
104 for (i = 0; i < image->width; i++) {
105 *d++ = color->red;
106 *d++ = color->green;
107 *d++ = color->blue;
109 lineSize = image->width * 3;
110 for (i = 1; i < image->height; i++, d += lineSize) {
111 memcpy(d, image->data, lineSize);
116 void RClearImage(RImage * image, const RColor * color)
118 unsigned char *d = image->data;
119 unsigned lineSize;
120 int i;
122 if (color->alpha == 255) {
123 if (image->format == RRGBAFormat) {
124 for (i = 0; i < image->width; i++) {
125 *d++ = color->red;
126 *d++ = color->green;
127 *d++ = color->blue;
128 *d++ = 0xff;
130 lineSize = image->width * 4;
131 for (i = 1; i < image->height; i++, d += lineSize) {
132 memcpy(d, image->data, lineSize);
134 } else {
135 for (i = 0; i < image->width; i++) {
136 *d++ = color->red;
137 *d++ = color->green;
138 *d++ = color->blue;
140 lineSize = image->width * 3;
141 for (i = 1; i < image->height; i++, d += lineSize) {
142 memcpy(d, image->data, lineSize);
145 } else {
146 int bytes = image->width * image->height;
147 int alpha, nalpha, r, g, b, s;
149 alpha = color->alpha;
150 r = color->red * alpha;
151 g = color->green * alpha;
152 b = color->blue * alpha;
153 nalpha = 255 - alpha;
155 s = (image->format == RRGBAFormat) ? 4 : 3;
157 for (i = 0; i < bytes; i++, d += s) {
158 d[0] = (((int)d[0] * nalpha) + r)/256;
159 d[1] = (((int)d[1] * nalpha) + g)/256;
160 d[2] = (((int)d[2] * nalpha) + b)/256;
165 static inline unsigned char clip(int c)
167 if (c > 255)
168 c = 255;
169 return (unsigned char)c;
172 void RLightImage(RImage *image, const RColor *color)
174 unsigned char *d = image->data;
175 unsigned char *dd;
176 int alpha, r, g, b, s;
178 s = (image->format == RRGBAFormat) ? 4 : 3;
179 dd = d + s*image->width*image->height;
181 r = color->red;
182 g = color->green;
183 b = color->blue;
185 alpha = color->alpha;
187 if (r == 0 && g == 0 && b == 0) {
188 for (; d < dd; d += s) {
189 d[0] = clip(((int)d[0] * alpha)/128);
190 d[1] = clip(((int)d[1] * alpha)/128);
191 d[2] = clip(((int)d[2] * alpha)/128);
193 } else {
194 for (; d < dd; d += s) {
195 d[0] = clip((((int)d[0] * alpha) + r)/128);
196 d[1] = clip((((int)d[1] * alpha) + g)/128);
197 d[2] = clip((((int)d[2] * alpha) + b)/128);
202 const char *RMessageForError(int errorCode)
204 switch (errorCode) {
205 case RERR_NONE:
206 return "no error";
208 case RERR_OPEN:
209 return "could not open file";
211 case RERR_READ:
212 return "error reading from file";
214 case RERR_WRITE:
215 return "error writing to file";
217 case RERR_NOMEMORY:
218 return "out of memory";
220 case RERR_NOCOLOR:
221 return "out of color cells";
223 case RERR_BADIMAGEFILE:
224 return "invalid or corrupted image file";
226 case RERR_BADFORMAT:
227 return "the image format in the file is not supported and can't be loaded";
229 case RERR_BADINDEX:
230 return "image file does not contain requested image index";
232 case RERR_BADVISUALID:
233 return "request for an invalid visual ID";
235 case RERR_STDCMAPFAIL:
236 return "failed to create standard colormap";
238 case RERR_XERROR:
239 return "internal X error";
241 default:
242 case RERR_INTERNAL:
243 return "internal error";
248 * cleaning third-party libs at shutdown
250 void RShutdown(void)
252 #ifdef USE_MAGICK
253 RReleaseMagick();
254 #endif
255 RReleaseCache();
256 r_destroy_conversion_tables();