Partially support _NET_WM_STRUT_PARTIAL.
[wmaker-crm.git] / wrlib / misc.c
blob7117d817749db62b08276b53b397ffc596783f27
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <config.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <X11/Xlib.h>
27 #include "wraster.h"
29 void RBevelImage(RImage * image, int bevel_type)
31 RColor color;
32 RColor cdelta;
33 int w, h;
35 if (image->width < 3 || image->height < 3)
36 return;
38 w = image->width;
39 h = image->height;
40 if (bevel_type > 0) { /* raised */
41 /* top */
42 cdelta.alpha = 0;
43 cdelta.red = cdelta.green = cdelta.blue = 80;
44 ROperateLine(image, RAddOperation, 0, 0, w - 1, 0, &cdelta);
45 if (bevel_type == RBEV_RAISED3 && w > 3)
46 ROperateLine(image, RAddOperation, 1, 1, w - 3, 1, &cdelta);
48 /* left */
49 ROperateLine(image, RAddOperation, 0, 1, 0, h - 1, &cdelta);
50 if (bevel_type == RBEV_RAISED3 && h > 3)
51 ROperateLine(image, RAddOperation, 1, 2, 1, h - 3, &cdelta);
53 /* bottom */
54 color.alpha = 255;
55 color.red = color.green = color.blue = 0;
56 cdelta.red = cdelta.green = cdelta.blue = 40;
57 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
58 ROperateLine(image, RSubtractOperation, 0, h - 2, w - 3, h - 2, &cdelta);
59 RDrawLine(image, 0, h - 1, w - 1, h - 1, &color);
60 } else {
61 ROperateLine(image, RSubtractOperation, 0, h - 1, w - 1, h - 1, &cdelta);
64 /* right */
65 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
66 ROperateLine(image, RSubtractOperation, w - 2, 0, w - 2, h - 2, &cdelta);
67 RDrawLine(image, w - 1, 0, w - 1, h - 2, &color);
68 } else {
69 ROperateLine(image, RSubtractOperation, w - 1, 0, w - 1, h - 2, &cdelta);
71 } else { /* sunken */
72 cdelta.alpha = 0;
73 cdelta.red = cdelta.green = cdelta.blue = 40;
74 ROperateLine(image, RSubtractOperation, 0, 0, w - 1, 0, &cdelta); /* top */
75 ROperateLine(image, RSubtractOperation, 0, 1, 0, h - 1, &cdelta); /* left */
76 cdelta.red = cdelta.green = cdelta.blue = 80;
77 ROperateLine(image, RAddOperation, 0, h - 1, w - 1, h - 1, &cdelta); /* bottom */
78 ROperateLine(image, RAddOperation, w - 1, 0, w - 1, h - 2, &cdelta); /* right */
82 void RFillImage(RImage * image, RColor * color)
84 unsigned char *d = image->data;
85 unsigned lineSize;
86 int i;
88 if (image->format == RRGBAFormat) {
89 for (i = 0; i < image->width; i++) {
90 *d++ = color->red;
91 *d++ = color->green;
92 *d++ = color->blue;
93 *d++ = color->alpha;
95 lineSize = image->width * 4;
96 for (i = 1; i < image->height; i++, d += lineSize) {
97 memcpy(d, image->data, lineSize);
99 } else {
100 for (i = 0; i < image->width; i++) {
101 *d++ = color->red;
102 *d++ = color->green;
103 *d++ = color->blue;
105 lineSize = image->width * 3;
106 for (i = 1; i < image->height; i++, d += lineSize) {
107 memcpy(d, image->data, lineSize);
112 void RClearImage(RImage * image, RColor * color)
114 unsigned char *d = image->data;
115 unsigned lineSize;
116 int i;
118 if (color->alpha == 255) {
119 if (image->format == RRGBAFormat) {
120 for (i = 0; i < image->width; i++) {
121 *d++ = color->red;
122 *d++ = color->green;
123 *d++ = color->blue;
124 *d++ = 0xff;
126 lineSize = image->width * 4;
127 for (i = 1; i < image->height; i++, d += lineSize) {
128 memcpy(d, image->data, lineSize);
130 } else {
131 for (i = 0; i < image->width; i++) {
132 *d++ = color->red;
133 *d++ = color->green;
134 *d++ = color->blue;
136 lineSize = image->width * 3;
137 for (i = 1; i < image->height; i++, d += lineSize) {
138 memcpy(d, image->data, lineSize);
141 } else {
142 int bytes = image->width * image->height;
143 int alpha, nalpha, r, g, b, s;
145 alpha = color->alpha;
146 r = color->red * alpha;
147 g = color->green * alpha;
148 b = color->blue * alpha;
149 nalpha = 255 - alpha;
151 s = (image->format == RRGBAFormat) ? 4 : 3;
153 for (i = 0; i < bytes; i++, d += s) {
154 d[0] = (((int)d[0] * nalpha) + r)/256;
155 d[1] = (((int)d[1] * nalpha) + g)/256;
156 d[2] = (((int)d[2] * nalpha) + b)/256;
161 static __inline__ unsigned char clip(int c)
163 if (c > 255)
164 c = 255;
165 return (unsigned char)c;
168 void RLightImage(RImage *image, RColor *color)
170 unsigned char *d = image->data;
171 unsigned char *dd;
172 int alpha, r, g, b, s;
174 s = (image->format == RRGBAFormat) ? 4 : 3;
175 dd = d + s*image->width*image->height;
177 r = color->red;
178 g = color->green;
179 b = color->blue;
181 alpha = color->alpha;
183 if (r == 0 && g == 0 && b == 0) {
184 for (; d < dd; d += s) {
185 d[0] = clip(((int)d[0] * alpha)/128);
186 d[1] = clip(((int)d[1] * alpha)/128);
187 d[2] = clip(((int)d[2] * alpha)/128);
189 } else {
190 for (; d < dd; d += s) {
191 d[0] = clip((((int)d[0] * alpha) + r)/128);
192 d[1] = clip((((int)d[1] * alpha) + g)/128);
193 d[2] = clip((((int)d[2] * alpha) + b)/128);
198 const char *RMessageForError(int errorCode)
200 switch (errorCode) {
201 case RERR_NONE:
202 return "no error";
204 case RERR_OPEN:
205 return "could not open file";
207 case RERR_READ:
208 return "error reading from file";
210 case RERR_WRITE:
211 return "error writing to file";
213 case RERR_NOMEMORY:
214 return "out of memory";
216 case RERR_NOCOLOR:
217 return "out of color cells";
219 case RERR_BADIMAGEFILE:
220 return "invalid or corrupted image file";
222 case RERR_BADFORMAT:
223 return "the image format in the file is not supported and can't be loaded";
225 case RERR_BADINDEX:
226 return "image file does not contain requested image index";
228 case RERR_BADVISUALID:
229 return "request for an invalid visual ID";
231 case RERR_STDCMAPFAIL:
232 return "failed to create standard colormap";
234 case RERR_XERROR:
235 return "internal X error";
237 default:
238 case RERR_INTERNAL:
239 return "internal error";