Update Serbian translation from master branch
[wmaker-crm.git] / wrlib / misc.c
blob922c73d6ecbdce14210b89b9dfad6d4a59533d22
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"
31 #include "wr_i18n.h"
34 void RBevelImage(RImage * image, int bevel_type)
36 RColor color;
37 RColor cdelta;
38 int w, h;
40 if (image->width < 3 || image->height < 3)
41 return;
43 w = image->width;
44 h = image->height;
45 if (bevel_type > 0) { /* raised */
46 /* top */
47 cdelta.alpha = 0;
48 cdelta.red = cdelta.green = cdelta.blue = 80;
49 ROperateLine(image, RAddOperation, 0, 0, w - 1, 0, &cdelta);
50 if (bevel_type == RBEV_RAISED3 && w > 3)
51 ROperateLine(image, RAddOperation, 1, 1, w - 3, 1, &cdelta);
53 /* left */
54 ROperateLine(image, RAddOperation, 0, 1, 0, h - 1, &cdelta);
55 if (bevel_type == RBEV_RAISED3 && h > 3)
56 ROperateLine(image, RAddOperation, 1, 2, 1, h - 3, &cdelta);
58 /* bottom */
59 color.alpha = 255;
60 color.red = color.green = color.blue = 0;
61 cdelta.red = cdelta.green = cdelta.blue = 40;
62 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
63 ROperateLine(image, RSubtractOperation, 0, h - 2, w - 3, h - 2, &cdelta);
64 RDrawLine(image, 0, h - 1, w - 1, h - 1, &color);
65 } else {
66 ROperateLine(image, RSubtractOperation, 0, h - 1, w - 1, h - 1, &cdelta);
69 /* right */
70 if (bevel_type == RBEV_RAISED2 || bevel_type == RBEV_RAISED3) {
71 ROperateLine(image, RSubtractOperation, w - 2, 0, w - 2, h - 2, &cdelta);
72 RDrawLine(image, w - 1, 0, w - 1, h - 2, &color);
73 } else {
74 ROperateLine(image, RSubtractOperation, w - 1, 0, w - 1, h - 2, &cdelta);
76 } else { /* sunken */
77 cdelta.alpha = 0;
78 cdelta.red = cdelta.green = cdelta.blue = 40;
79 ROperateLine(image, RSubtractOperation, 0, 0, w - 1, 0, &cdelta); /* top */
80 ROperateLine(image, RSubtractOperation, 0, 1, 0, h - 1, &cdelta); /* left */
81 cdelta.red = cdelta.green = cdelta.blue = 80;
82 ROperateLine(image, RAddOperation, 0, h - 1, w - 1, h - 1, &cdelta); /* bottom */
83 ROperateLine(image, RAddOperation, w - 1, 0, w - 1, h - 2, &cdelta); /* right */
87 void RFillImage(RImage * image, const RColor * color)
89 unsigned char *d = image->data;
90 unsigned lineSize;
91 int i;
93 if (image->format == RRGBAFormat) {
94 for (i = 0; i < image->width; i++) {
95 *d++ = color->red;
96 *d++ = color->green;
97 *d++ = color->blue;
98 *d++ = color->alpha;
100 lineSize = image->width * 4;
101 for (i = 1; i < image->height; i++, d += lineSize) {
102 memcpy(d, image->data, lineSize);
104 } else {
105 for (i = 0; i < image->width; i++) {
106 *d++ = color->red;
107 *d++ = color->green;
108 *d++ = color->blue;
110 lineSize = image->width * 3;
111 for (i = 1; i < image->height; i++, d += lineSize) {
112 memcpy(d, image->data, lineSize);
117 void RClearImage(RImage * image, const RColor * color)
119 unsigned char *d = image->data;
120 unsigned lineSize;
121 int i;
123 if (color->alpha == 255) {
124 if (image->format == RRGBAFormat) {
125 for (i = 0; i < image->width; i++) {
126 *d++ = color->red;
127 *d++ = color->green;
128 *d++ = color->blue;
129 *d++ = 0xff;
131 lineSize = image->width * 4;
132 for (i = 1; i < image->height; i++, d += lineSize) {
133 memcpy(d, image->data, lineSize);
135 } else {
136 for (i = 0; i < image->width; i++) {
137 *d++ = color->red;
138 *d++ = color->green;
139 *d++ = color->blue;
141 lineSize = image->width * 3;
142 for (i = 1; i < image->height; i++, d += lineSize) {
143 memcpy(d, image->data, lineSize);
146 } else {
147 int bytes = image->width * image->height;
148 int alpha, nalpha, r, g, b, s;
150 alpha = color->alpha;
151 r = color->red * alpha;
152 g = color->green * alpha;
153 b = color->blue * alpha;
154 nalpha = 255 - alpha;
156 s = (image->format == RRGBAFormat) ? 4 : 3;
158 for (i = 0; i < bytes; i++, d += s) {
159 d[0] = (((int)d[0] * nalpha) + r)/256;
160 d[1] = (((int)d[1] * nalpha) + g)/256;
161 d[2] = (((int)d[2] * nalpha) + b)/256;
166 static inline unsigned char clip(int c)
168 if (c > 255)
169 c = 255;
170 return (unsigned char)c;
173 void RLightImage(RImage *image, const RColor *color)
175 unsigned char *d = image->data;
176 unsigned char *dd;
177 int alpha, r, g, b, s;
179 s = (image->format == RRGBAFormat) ? 4 : 3;
180 dd = d + s*image->width*image->height;
182 r = color->red;
183 g = color->green;
184 b = color->blue;
186 alpha = color->alpha;
188 if (r == 0 && g == 0 && b == 0) {
189 for (; d < dd; d += s) {
190 d[0] = clip(((int)d[0] * alpha)/128);
191 d[1] = clip(((int)d[1] * alpha)/128);
192 d[2] = clip(((int)d[2] * alpha)/128);
194 } else {
195 for (; d < dd; d += s) {
196 d[0] = clip((((int)d[0] * alpha) + r)/128);
197 d[1] = clip((((int)d[1] * alpha) + g)/128);
198 d[2] = clip((((int)d[2] * alpha) + b)/128);
203 const char *RMessageForError(int errorCode)
205 switch (errorCode) {
206 case RERR_NONE:
207 return _("no error");
209 case RERR_OPEN:
210 return _("could not open file");
212 case RERR_READ:
213 return _("error reading from file");
215 case RERR_WRITE:
216 return _("error writing to file");
218 case RERR_NOMEMORY:
219 return _("out of memory");
221 case RERR_NOCOLOR:
222 return _("out of color cells");
224 case RERR_BADIMAGEFILE:
225 return _("invalid or corrupted image file");
227 case RERR_BADFORMAT:
228 return _("image format is not supported");
230 case RERR_BADINDEX:
231 return _("file does not contain requested image index");
233 case RERR_BADVISUALID:
234 return _("request for an invalid Visual ID");
236 case RERR_STDCMAPFAIL:
237 return _("failed to create X standard colormap");
239 case RERR_XERROR:
240 return _("internal X error");
242 default:
243 case RERR_INTERNAL:
244 return _("internal error");
248 #ifdef I18N
250 * Setup internationalization on startup
252 * For historical reason, the WRaster library does not have a function that
253 * user is supposed to call to initialise the library. Because we need to do
254 * some stuff now, we rely on the compiler attribute to tell this function
255 * has to be called automatically when library is loaded.
257 void WLIB_CONSTRUCTOR(RStartup) (void)
259 const char *locale_path;
261 locale_path = getenv("NLSPATH");
262 if (locale_path == NULL)
263 locale_path = LOCALEDIR;
265 bindtextdomain("WRaster", locale_path);
267 #endif
270 * cleaning third-party libs at shutdown
272 void RShutdown(void)
274 #ifdef USE_MAGICK
275 RReleaseMagick();
276 #endif
277 RReleaseCache();
278 r_destroy_conversion_tables();