Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / misc.c
1 /*
2 * Raster graphics library
3 *
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 *
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.
10 *
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.
15 *
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.
19 */
20 #include <config.h>
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <X11/Xlib.h>
26
27 #include "wraster.h"
28
29 void RBevelImage(RImage * image, int bevel_type)
30 {
31 RColor color;
32 RColor cdelta;
33 int w, h;
34
35 if (image->width < 3 || image->height < 3)
36 return;
37
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);
47
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);
52
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);
62 }
63
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);
70 }
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 */
79 }
80 }
81
82 void RFillImage(RImage * image, RColor * color)
83 {
84 unsigned char *d = image->data;
85 unsigned lineSize;
86 int i;
87
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;
94 }
95 lineSize = image->width * 4;
96 for (i = 1; i < image->height; i++, d += lineSize) {
97 memcpy(d, image->data, lineSize);
98 }
99 } else {
100 for (i = 0; i < image->width; i++) {
101 *d++ = color->red;
102 *d++ = color->green;
103 *d++ = color->blue;
104 }
105 lineSize = image->width * 3;
106 for (i = 1; i < image->height; i++, d += lineSize) {
107 memcpy(d, image->data, lineSize);
108 }
109 }
110 }
111
112 void RClearImage(RImage * image, RColor * color)
113 {
114 unsigned char *d = image->data;
115 unsigned lineSize;
116 int i;
117
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;
125 }
126 lineSize = image->width * 4;
127 for (i = 1; i < image->height; i++, d += lineSize) {
128 memcpy(d, image->data, lineSize);
129 }
130 } else {
131 for (i = 0; i < image->width; i++) {
132 *d++ = color->red;
133 *d++ = color->green;
134 *d++ = color->blue;
135 }
136 lineSize = image->width * 3;
137 for (i = 1; i < image->height; i++, d += lineSize) {
138 memcpy(d, image->data, lineSize);
139 }
140 }
141 } else {
142 int bytes = image->width * image->height;
143 int alpha, nalpha, r, g, b;
144
145 alpha = color->alpha;
146 r = color->red * alpha;
147 g = color->green * alpha;
148 b = color->blue * alpha;
149 nalpha = 255 - alpha;
150
151 for (i = 0; i < bytes; i++) {
152 *d = (((int)*d * nalpha) + r) / 256;
153 d++;
154 *d = (((int)*d * nalpha) + g) / 256;
155 d++;
156 *d = (((int)*d * nalpha) + b) / 256;
157 d++;
158 if (image->format == RRGBAFormat) {
159 d++;
160 }
161 }
162 }
163 }
164
165 const char *RMessageForError(int errorCode)
166 {
167 switch (errorCode) {
168 case RERR_NONE:
169 return "no error";
170
171 case RERR_OPEN:
172 return "could not open file";
173
174 case RERR_READ:
175 return "error reading from file";
176
177 case RERR_WRITE:
178 return "error writing to file";
179
180 case RERR_NOMEMORY:
181 return "out of memory";
182
183 case RERR_NOCOLOR:
184 return "out of color cells";
185
186 case RERR_BADIMAGEFILE:
187 return "invalid or corrupted image file";
188
189 case RERR_BADFORMAT:
190 return "the image format in the file is not supported and can't be loaded";
191
192 case RERR_BADINDEX:
193 return "image file does not contain requested image index";
194
195 case RERR_BADVISUALID:
196 return "request for an invalid visual ID";
197
198 case RERR_STDCMAPFAIL:
199 return "failed to create standard colormap";
200
201 case RERR_XERROR:
202 return "internal X error";
203
204 default:
205 case RERR_INTERNAL:
206 return "internal error";
207 }
208 }