- removed hermeslib dependancy
[wmaker-crm.git] / wrlib / raster.c
blob590d8ce8102bf41b3ec62df01228cf831a48f46e
1 /* raster.c - main and other misc stuff
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997-2002 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <config.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
28 #include "wraster.h"
30 #include <assert.h>
33 char *WRasterLibVersion="0.9";
35 int RErrorCode=RERR_NONE;
38 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
41 RImage*
42 RCreateImage(unsigned width, unsigned height, int alpha)
44 RImage *image=NULL;
46 assert(width>0 && height>0);
48 image = malloc(sizeof(RImage));
49 if (!image) {
50 RErrorCode = RERR_NOMEMORY;
51 return NULL;
54 memset(image, 0, sizeof(RImage));
55 image->width = width;
56 image->height = height;
57 image->format = alpha ? RRGBAFormat : RRGBFormat;
58 image->refCount = 1;
60 /* the +4 is to give extra bytes at the end of the buffer,
61 * so that we can optimize image conversion for MMX(tm).. see convert.c
63 image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
64 if (!image->data) {
65 RErrorCode = RERR_NOMEMORY;
66 free(image);
67 image = NULL;
70 return image;
74 RImage*
75 RRetainImage(RImage *image)
77 if (image)
78 image->refCount++;
80 return image;
84 void
85 RReleaseImage(RImage *image)
87 assert(image!=NULL);
89 image->refCount--;
91 if (image->refCount < 1) {
92 free(image->data);
93 free(image);
98 RImage*
99 RCloneImage(RImage *image)
101 RImage *new_image;
103 assert(image!=NULL);
105 new_image = RCreateImage(image->width, image->height, HAS_ALPHA(image));
106 if (!new_image)
107 return NULL;
109 new_image->background = image->background;
110 memcpy(new_image->data, image->data,
111 image->width*image->height*(HAS_ALPHA(image) ? 4 : 3));
113 return new_image;
117 RImage*
118 RGetSubImage(RImage *image, int x, int y, unsigned width, unsigned height)
120 int i, ofs;
121 RImage *new_image;
122 unsigned total_line_size, line_size;
124 assert(image!=NULL);
125 assert(x>=0 && y>=0);
126 assert(x<image->width && y<image->height);
127 assert(width>0 && height>0);
129 if (x+width > image->width)
130 width = image->width-x;
131 if (y+height > image->height)
132 height = image->height-y;
134 new_image = RCreateImage(width, height, HAS_ALPHA(image));
136 if (!new_image)
137 return NULL;
138 new_image->background = image->background;
140 total_line_size = image->width * (HAS_ALPHA(image) ? 4 : 3);
141 line_size = width * (HAS_ALPHA(image) ? 4 : 3);
143 ofs = x*(HAS_ALPHA(image) ? 4 : 3) + y*total_line_size;;
145 for (i=0; i<height; i++) {
146 memcpy(&new_image->data[i*line_size],
147 &image->data[i*total_line_size+ofs], line_size);
149 return new_image;
154 *----------------------------------------------------------------------
155 * RCombineImages-
156 * Combines two equal sized images with alpha image. The second
157 * image will be placed on top of the first one.
158 *----------------------------------------------------------------------
160 void
161 RCombineImages(RImage *image, RImage *src)
163 assert(image->width == src->width);
164 assert(image->height == src->height);
166 if (!HAS_ALPHA(src)) {
167 if (!HAS_ALPHA(image)) {
168 memcpy(image->data, src->data, image->height*image->width*3);
169 } else {
170 int x, y;
171 unsigned char *d, *s;
173 d = image->data;
174 s = src->data;
175 for (y = 0; y < image->height; y++) {
176 for (x = 0; x < image->width; x++) {
177 *d++ = *s++;
178 *d++ = *s++;
179 *d++ = *s++;
180 d++;
184 } else {
185 register int i;
186 unsigned char *d;
187 unsigned char *s;
188 int alpha, calpha;
190 d = image->data;
191 s = src->data;
193 if (!HAS_ALPHA(image)) {
194 for (i=0; i<image->height*image->width; i++) {
195 alpha = *(s+3);
196 calpha = 255 - alpha;
197 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
198 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
199 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
200 s++;
202 } else {
203 for (i=0; i<image->height*image->width; i++) {
204 alpha = *(s+3);
205 calpha = 255 - alpha;
206 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
207 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
208 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
209 *d++ |= *s++;
218 void
219 RCombineImagesWithOpaqueness(RImage *image, RImage *src, int opaqueness)
221 register int i;
222 unsigned char *d;
223 unsigned char *s;
224 int c_opaqueness;
226 assert(image->width == src->width);
227 assert(image->height == src->height);
229 d = image->data;
230 s = src->data;
232 c_opaqueness = 255 - opaqueness;
234 #define OP opaqueness
235 #define COP c_opaqueness
237 if (!HAS_ALPHA(src)) {
238 int dalpha = HAS_ALPHA(image);
239 for (i=0; i < image->width*image->height; i++) {
240 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; d++; s++;
241 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; d++; s++;
242 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; d++; s++;
243 if (dalpha) {
244 d++;
247 } else {
248 int tmp;
250 if (!HAS_ALPHA(image)) {
251 for (i=0; i<image->width*image->height; i++) {
252 tmp = (*(s+3) * opaqueness)/256;
253 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
254 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
255 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
256 s++;
258 } else {
259 for (i=0; i<image->width*image->height; i++) {
260 tmp = (*(s+3) * opaqueness)/256;
261 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
262 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
263 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
264 *d |= tmp;
265 d++; s++;
269 #undef OP
270 #undef COP
274 calculateCombineArea(RImage *des, RImage *src, int *sx, int *sy,
275 int *swidth, int *sheight, int *dx, int *dy)
277 if (*dx < 0) {
278 *sx = -*dx;
279 *swidth = *swidth + *dx;
280 *dx = 0;
283 if (*dx + *swidth > des->width) {
284 *swidth = des->width - *dx;
287 if (*dy < 0) {
288 *sy = -*dy;
289 *sheight = *sheight + *dy;
290 *dy = 0;
293 if (*dy + *sheight > des->height) {
294 *sheight = des->height - *dy;
297 if (*sheight > 0 && *swidth > 0) {
298 return True;
299 } else return False;
302 void
303 RCombineArea(RImage *image, RImage *src, int sx, int sy, unsigned width,
304 unsigned height, int dx, int dy)
306 int x, y, dwi, swi;
307 unsigned char *d;
308 unsigned char *s;
309 int alpha, calpha;
311 if(!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
312 return;
314 if (!HAS_ALPHA(src)) {
315 if (!HAS_ALPHA(image)) {
316 swi = src->width * 3;
317 dwi = image->width * 3;
319 s = src->data + (sy*(int)src->width + sx) * 3;
320 d = image->data + (dy*(int)image->width + dx) * 3;
322 for (y=0; y < height; y++) {
323 memcpy(d, s, width*3);
324 d += dwi;
325 s += swi;
327 } else {
328 swi = (src->width - width) * 3;
329 dwi = (image->width - width) * 4;
331 s = src->data + (sy*(int)src->width + sx) * 3;
332 d = image->data + (dy*(int)image->width + dx) * 4;
334 for (y=0; y < height; y++) {
335 for (x=0; x < width; x++) {
336 *d++ = *s++;
337 *d++ = *s++;
338 *d++ = *s++;
339 d++;
341 d += dwi;
342 s += swi;
345 } else {
346 int dalpha = HAS_ALPHA(image);
348 swi = (src->width - width) * 4;
349 s = src->data + (sy*(int)src->width + sx) * 4;
350 if (dalpha) {
351 dwi = (image->width - width) * 4;
352 d = image->data + (dy*(int)image->width + dx) * 4;
353 } else {
354 dwi = (image->width - width) * 3;
355 d = image->data + (dy*(int)image->width + dx) * 3;
358 for (y=0; y < height; y++) {
359 for (x=0; x < width; x++) {
360 alpha = *(s+3);
361 calpha = 255 - alpha;
362 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; s++; d++;
363 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; s++; d++;
364 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; s++; d++;
365 s++;
366 if (dalpha)
367 d++;
369 d += dwi;
370 s += swi;
376 void
377 RCombineAreaWithOpaqueness(RImage *image, RImage *src, int sx, int sy,
378 unsigned width, unsigned height, int dx, int dy,
379 int opaqueness)
381 int x, y, dwi, swi;
382 int c_opaqueness;
383 unsigned char *s, *d;
384 int dalpha = HAS_ALPHA(image);
385 int dch = (dalpha ? 4 : 3);
387 if(!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
388 return;
390 d = image->data + (dy*image->width + dx) * dch;
391 dwi = (image->width - width)*dch;
393 c_opaqueness = 255 - opaqueness;
395 #define OP opaqueness
396 #define COP c_opaqueness
398 if (!HAS_ALPHA(src)) {
400 s = src->data + (sy*src->width + sx)*3;
401 swi = (src->width - width) * 3;
403 for (y=0; y < height; y++) {
404 for (x=0; x < width; x++) {
405 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; s++; d++;
406 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; s++; d++;
407 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; s++; d++;
408 if (dalpha)
409 d++;
411 d += dwi; s += swi;
413 } else {
414 int tmp;
416 s = src->data + (sy*src->width + sx)*4;
417 swi = (src->width - width) * 4;
419 for (y=0; y < height; y++) {
420 for (x=0; x < width; x++) {
421 tmp = (*(s+3) * opaqueness)/256;
422 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
423 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
424 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
425 s++;
426 if (dalpha)
427 d++;
429 d += dwi; s += swi;
432 #undef OP
433 #undef COP
438 void
439 RCombineImageWithColor(RImage *image, RColor *color)
441 register int i;
442 unsigned char *d;
443 int alpha, nalpha, r, g, b;
445 d = image->data;
447 if (!HAS_ALPHA(image)) {
448 /* Image has no alpha channel, so we consider it to be all 255.
449 * Thus there are no transparent parts to be filled. */
450 return;
452 r = color->red;
453 g = color->green;
454 b = color->blue;
456 for (i=0; i < image->width*image->height; i++) {
457 alpha = *(d+3);
458 nalpha = 255 - alpha;
460 *d = (((int)*d * alpha) + (r * nalpha))/256; d++;
461 *d = (((int)*d * alpha) + (g * nalpha))/256; d++;
462 *d = (((int)*d * alpha) + (b * nalpha))/256; d++;
463 d++;
470 RImage*
471 RMakeTiledImage(RImage *tile, unsigned width, unsigned height)
473 int x, y;
474 unsigned w;
475 unsigned long tile_size = tile->width * tile->height;
476 unsigned long tx = 0;
477 RImage *image;
478 unsigned char *s, *d;
480 if (width == tile->width && height == tile->height)
481 image = RCloneImage(tile);
482 else if (width <= tile->width && height <= tile->height)
483 image = RGetSubImage(tile, 0, 0, width, height);
484 else {
485 int has_alpha = HAS_ALPHA(tile);
487 image = RCreateImage(width, height, has_alpha);
489 d = image->data;
490 s = tile->data;
492 for (y = 0; y < height; y++) {
493 for (x = 0; x < width; x += tile->width) {
495 w = (width - x < tile->width) ? width - x : tile->width;
497 if (has_alpha) {
498 w *= 4;
499 memcpy(d, s+tx*4, w);
500 } else {
501 w *= 3;
502 memcpy(d, s+tx*3, w);
504 d += w;
507 tx = (tx + tile->width) % tile_size;
510 return image;
514 RImage*
515 RMakeCenteredImage(RImage *image, unsigned width, unsigned height, RColor *color)
517 int x, y, w, h, sx, sy;
518 RImage *tmp;
520 tmp = RCreateImage(width, height, False);
521 if (!tmp) {
522 return NULL;
525 RClearImage(tmp, color);
527 if (image->height < height) {
528 h = image->height;
529 y = (height - h)/2;
530 sy = 0;
531 } else {
532 sy = (image->height - height)/2;
533 y = 0;
534 h = height;
536 if (image->width < width) {
537 w = image->width;
538 x = (width - w)/2;
539 sx = 0;
540 } else {
541 sx = (image->width - width)/2;
542 x = 0;
543 w = width;
545 RCombineArea(tmp, image, sx, sy, w, h, x, y);
547 return tmp;