wrlib: Added clean-up of image cache in 'RShutdown'
[wmaker-crm.git] / wrlib / misc.c
blob53d5aaaee7d55115d41a967c1af52f053903437c
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"
32 void RBevelImage(RImage * image, int bevel_type)
34 RColor color;
35 RColor cdelta;
36 int w, h;
38 if (image->width < 3 || image->height < 3)
39 return;
41 w = image->width;
42 h = image->height;
43 if (bevel_type > 0) { /* raised */
44 /* top */
45 cdelta.alpha = 0;
46 cdelta.red = cdelta.green = cdelta.blue = 80;
47 ROperateLine(image, RAddOperation, 0, 0, w - 1, 0, &cdelta);
48 if (bevel_type == RBEV_RAISED3 && w > 3)
49 ROperateLine(image, RAddOperation, 1, 1, w - 3, 1, &cdelta);
51 /* left */
52 ROperateLine(image, RAddOperation, 0, 1, 0, h - 1, &cdelta);
53 if (bevel_type == RBEV_RAISED3 && h > 3)
54 ROperateLine(image, RAddOperation, 1, 2, 1, h - 3, &cdelta);
56 /* bottom */
57 color.alpha = 255;
58 color.red = color.green = color.blue = 0;
59 cdelta.red = cdelta.green = cdelta.blue = 40;
60 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
61 ROperateLine(image, RSubtractOperation, 0, h - 2, w - 3, h - 2, &cdelta);
62 RDrawLine(image, 0, h - 1, w - 1, h - 1, &color);
63 } else {
64 ROperateLine(image, RSubtractOperation, 0, h - 1, w - 1, h - 1, &cdelta);
67 /* right */
68 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
69 ROperateLine(image, RSubtractOperation, w - 2, 0, w - 2, h - 2, &cdelta);
70 RDrawLine(image, w - 1, 0, w - 1, h - 2, &color);
71 } else {
72 ROperateLine(image, RSubtractOperation, w - 1, 0, w - 1, h - 2, &cdelta);
74 } else { /* sunken */
75 cdelta.alpha = 0;
76 cdelta.red = cdelta.green = cdelta.blue = 40;
77 ROperateLine(image, RSubtractOperation, 0, 0, w - 1, 0, &cdelta); /* top */
78 ROperateLine(image, RSubtractOperation, 0, 1, 0, h - 1, &cdelta); /* left */
79 cdelta.red = cdelta.green = cdelta.blue = 80;
80 ROperateLine(image, RAddOperation, 0, h - 1, w - 1, h - 1, &cdelta); /* bottom */
81 ROperateLine(image, RAddOperation, w - 1, 0, w - 1, h - 2, &cdelta); /* right */
85 void RFillImage(RImage * image, const RColor * color)
87 unsigned char *d = image->data;
88 unsigned lineSize;
89 int i;
91 if (image->format == RRGBAFormat) {
92 for (i = 0; i < image->width; i++) {
93 *d++ = color->red;
94 *d++ = color->green;
95 *d++ = color->blue;
96 *d++ = color->alpha;
98 lineSize = image->width * 4;
99 for (i = 1; i < image->height; i++, d += lineSize) {
100 memcpy(d, image->data, lineSize);
102 } else {
103 for (i = 0; i < image->width; i++) {
104 *d++ = color->red;
105 *d++ = color->green;
106 *d++ = color->blue;
108 lineSize = image->width * 3;
109 for (i = 1; i < image->height; i++, d += lineSize) {
110 memcpy(d, image->data, lineSize);
115 void RClearImage(RImage * image, const RColor * color)
117 unsigned char *d = image->data;
118 unsigned lineSize;
119 int i;
121 if (color->alpha == 255) {
122 if (image->format == RRGBAFormat) {
123 for (i = 0; i < image->width; i++) {
124 *d++ = color->red;
125 *d++ = color->green;
126 *d++ = color->blue;
127 *d++ = 0xff;
129 lineSize = image->width * 4;
130 for (i = 1; i < image->height; i++, d += lineSize) {
131 memcpy(d, image->data, lineSize);
133 } else {
134 for (i = 0; i < image->width; i++) {
135 *d++ = color->red;
136 *d++ = color->green;
137 *d++ = color->blue;
139 lineSize = image->width * 3;
140 for (i = 1; i < image->height; i++, d += lineSize) {
141 memcpy(d, image->data, lineSize);
144 } else {
145 int bytes = image->width * image->height;
146 int alpha, nalpha, r, g, b, s;
148 alpha = color->alpha;
149 r = color->red * alpha;
150 g = color->green * alpha;
151 b = color->blue * alpha;
152 nalpha = 255 - alpha;
154 s = (image->format == RRGBAFormat) ? 4 : 3;
156 for (i = 0; i < bytes; i++, d += s) {
157 d[0] = (((int)d[0] * nalpha) + r)/256;
158 d[1] = (((int)d[1] * nalpha) + g)/256;
159 d[2] = (((int)d[2] * nalpha) + b)/256;
164 static inline unsigned char clip(int c)
166 if (c > 255)
167 c = 255;
168 return (unsigned char)c;
171 void RLightImage(RImage *image, const RColor *color)
173 unsigned char *d = image->data;
174 unsigned char *dd;
175 int alpha, r, g, b, s;
177 s = (image->format == RRGBAFormat) ? 4 : 3;
178 dd = d + s*image->width*image->height;
180 r = color->red;
181 g = color->green;
182 b = color->blue;
184 alpha = color->alpha;
186 if (r == 0 && g == 0 && b == 0) {
187 for (; d < dd; d += s) {
188 d[0] = clip(((int)d[0] * alpha)/128);
189 d[1] = clip(((int)d[1] * alpha)/128);
190 d[2] = clip(((int)d[2] * alpha)/128);
192 } else {
193 for (; d < dd; d += s) {
194 d[0] = clip((((int)d[0] * alpha) + r)/128);
195 d[1] = clip((((int)d[1] * alpha) + g)/128);
196 d[2] = clip((((int)d[2] * alpha) + b)/128);
201 const char *RMessageForError(int errorCode)
203 switch (errorCode) {
204 case RERR_NONE:
205 return "no error";
207 case RERR_OPEN:
208 return "could not open file";
210 case RERR_READ:
211 return "error reading from file";
213 case RERR_WRITE:
214 return "error writing to file";
216 case RERR_NOMEMORY:
217 return "out of memory";
219 case RERR_NOCOLOR:
220 return "out of color cells";
222 case RERR_BADIMAGEFILE:
223 return "invalid or corrupted image file";
225 case RERR_BADFORMAT:
226 return "the image format in the file is not supported and can't be loaded";
228 case RERR_BADINDEX:
229 return "image file does not contain requested image index";
231 case RERR_BADVISUALID:
232 return "request for an invalid visual ID";
234 case RERR_STDCMAPFAIL:
235 return "failed to create standard colormap";
237 case RERR_XERROR:
238 return "internal X error";
240 default:
241 case RERR_INTERNAL:
242 return "internal error";
247 * cleaning third-party libs at shutdown
249 void RShutdown(void)
251 #ifdef USE_MAGICK
252 RReleaseMagick();
253 #endif
254 RReleaseCache();