Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / misc.c
Commit [+]AuthorDateLineData
6830b057 dan2004-10-12 21:28:27 +00001/*
4153e2fd dan2003-01-16 23:30:45 +00002 * Raster graphics library
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1997-2003 Alfredo K. Kojima
9d2e6ef9 scottc1998-09-29 22:36:29 +00005 *
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.
6830b057 dan2004-10-12 21:28:27 +000010 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000011 * 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.
6830b057 dan2004-10-12 21:28:27 +000015 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000016 * 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 */
9d2e6ef9 scottc1998-09-29 22:36:29 +000020#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
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020029void RBevelImage(RImage * image, int bevel_type)
9d2e6ef9 scottc1998-09-29 22:36:29 +000030{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020031 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 }
9d2e6ef9 scottc1998-09-29 22:36:29 +000080}
81
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020082void RFillImage(RImage * image, RColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +000083{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020084 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 }
150432ff kojima2000-03-09 22:22:41 +0000110}
111
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200112void RClearImage(RImage * image, RColor * color)
150432ff kojima2000-03-09 22:22:41 +0000113{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200114 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 }
9af1c6c4 dan1998-10-21 14:43:47 +0000163}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000164
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200165const char *RMessageForError(int errorCode)
9af1c6c4 dan1998-10-21 14:43:47 +0000166{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200167 switch (errorCode) {
168 case RERR_NONE:
169 return "no error";
33b8c225 kojima1999-11-16 00:13:21 +0000170
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200171 case RERR_OPEN:
172 return "could not open file";
33b8c225 kojima1999-11-16 00:13:21 +0000173
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200174 case RERR_READ:
175 return "error reading from file";
33b8c225 kojima1999-11-16 00:13:21 +0000176
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200177 case RERR_WRITE:
178 return "error writing to file";
33b8c225 kojima1999-11-16 00:13:21 +0000179
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200180 case RERR_NOMEMORY:
181 return "out of memory";
33b8c225 kojima1999-11-16 00:13:21 +0000182
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200183 case RERR_NOCOLOR:
184 return "out of color cells";
33b8c225 kojima1999-11-16 00:13:21 +0000185
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200186 case RERR_BADIMAGEFILE:
187 return "invalid or corrupted image file";
33b8c225 kojima1999-11-16 00:13:21 +0000188
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200189 case RERR_BADFORMAT:
190 return "the image format in the file is not supported and can't be loaded";
33b8c225 kojima1999-11-16 00:13:21 +0000191
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200192 case RERR_BADINDEX:
193 return "image file does not contain requested image index";
33b8c225 kojima1999-11-16 00:13:21 +0000194
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200195 case RERR_BADVISUALID:
196 return "request for an invalid visual ID";
33b8c225 kojima1999-11-16 00:13:21 +0000197
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200198 case RERR_STDCMAPFAIL:
199 return "failed to create standard colormap";
33b8c225 kojima1999-11-16 00:13:21 +0000200
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200201 case RERR_XERROR:
202 return "internal X error";
33b8c225 kojima1999-11-16 00:13:21 +0000203
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200204 default:
205 case RERR_INTERNAL:
206 return "internal error";
207 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000208}