Only cache pixmaps for docked icons
[wmaker-crm.git] / wrlib / raster.c
blob188dd5164d6c0e6690af809e22338415dc4c1719
1 /* raster.c - main and other misc stuff
3 * Raster graphics library
5 * Copyright (c) 1997-2003 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>
32 char *WRasterLibVersion = "0.9";
34 int RErrorCode = RERR_NONE;
36 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
38 #define MAX_WIDTH 20000
39 #define MAX_HEIGHT 20000
40 /* 20000^2*4 < 2G */
42 RImage *RCreateImage(unsigned width, unsigned height, int alpha)
44 RImage *image = NULL;
46 assert(width > 0 && height > 0);
48 if (width > MAX_WIDTH || height > MAX_HEIGHT) {
49 RErrorCode = RERR_NOMEMORY;
50 return NULL;
53 image = malloc(sizeof(RImage));
54 if (!image) {
55 RErrorCode = RERR_NOMEMORY;
56 return NULL;
59 memset(image, 0, sizeof(RImage));
60 image->width = width;
61 image->height = height;
62 image->format = alpha ? RRGBAFormat : RRGBFormat;
63 image->refCount = 1;
65 /* the +4 is to give extra bytes at the end of the buffer,
66 * so that we can optimize image conversion for MMX(tm).. see convert.c
68 image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
69 if (!image->data) {
70 RErrorCode = RERR_NOMEMORY;
71 free(image);
72 image = NULL;
75 return image;
79 RImage *RRetainImage(RImage * image)
81 if (image)
82 image->refCount++;
84 return image;
87 void RReleaseImage(RImage * image)
89 assert(image != NULL);
91 image->refCount--;
93 if (image->refCount < 1) {
94 free(image->data);
95 free(image);
99 RImage *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, image->width * image->height * (HAS_ALPHA(image) ? 4 : 3));
112 return new_image;
115 RImage *RGetSubImage(RImage * image, int x, int y, unsigned width, unsigned height)
117 int i, ofs;
118 RImage *new_image;
119 unsigned total_line_size, line_size;
121 assert(image != NULL);
122 assert(x >= 0 && y >= 0);
123 assert(x < image->width && y < image->height);
124 assert(width > 0 && height > 0);
126 if (x + width > image->width)
127 width = image->width - x;
128 if (y + height > image->height)
129 height = image->height - y;
131 new_image = RCreateImage(width, height, HAS_ALPHA(image));
133 if (!new_image)
134 return NULL;
135 new_image->background = image->background;
137 total_line_size = image->width * (HAS_ALPHA(image) ? 4 : 3);
138 line_size = width * (HAS_ALPHA(image) ? 4 : 3);
140 ofs = x * (HAS_ALPHA(image) ? 4 : 3) + y * total_line_size;;
142 for (i = 0; i < height; i++) {
143 memcpy(&new_image->data[i * line_size], &image->data[i * total_line_size + ofs], line_size);
145 return new_image;
149 *----------------------------------------------------------------------
150 * RCombineImages-
151 * Combines two equal sized images with alpha image. The second
152 * image will be placed on top of the first one.
153 *----------------------------------------------------------------------
155 void RCombineImages(RImage * image, RImage * src)
157 assert(image->width == src->width);
158 assert(image->height == src->height);
160 if (!HAS_ALPHA(src)) {
161 if (!HAS_ALPHA(image)) {
162 memcpy(image->data, src->data, image->height * image->width * 3);
163 } else {
164 int x, y;
165 unsigned char *d, *s;
167 d = image->data;
168 s = src->data;
169 for (y = 0; y < image->height; y++) {
170 for (x = 0; x < image->width; x++) {
171 *d++ = *s++;
172 *d++ = *s++;
173 *d++ = *s++;
174 d++;
178 } else {
179 register int i;
180 unsigned char *d;
181 unsigned char *s;
182 int alpha, calpha;
184 d = image->data;
185 s = src->data;
187 if (!HAS_ALPHA(image)) {
188 for (i = 0; i < image->height * image->width; i++) {
189 alpha = *(s + 3);
190 calpha = 255 - alpha;
191 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
192 d++;
193 s++;
194 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
195 d++;
196 s++;
197 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
198 d++;
199 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;
207 d++;
208 s++;
209 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
210 d++;
211 s++;
212 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
213 d++;
214 s++;
215 *d++ |= *s++;
221 void RCombineImagesWithOpaqueness(RImage * image, RImage * src, int opaqueness)
223 register int i;
224 unsigned char *d;
225 unsigned char *s;
226 int c_opaqueness;
228 assert(image->width == src->width);
229 assert(image->height == src->height);
231 d = image->data;
232 s = src->data;
234 c_opaqueness = 255 - opaqueness;
236 #define OP opaqueness
237 #define COP c_opaqueness
239 if (!HAS_ALPHA(src)) {
240 int dalpha = HAS_ALPHA(image);
241 for (i = 0; i < image->width * image->height; i++) {
242 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
243 d++;
244 s++;
245 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
246 d++;
247 s++;
248 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
249 d++;
250 s++;
251 if (dalpha) {
252 d++;
255 } else {
256 int tmp;
258 if (!HAS_ALPHA(image)) {
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;
262 d++;
263 s++;
264 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
265 d++;
266 s++;
267 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
268 d++;
269 s++;
270 s++;
272 } else {
273 for (i = 0; i < image->width * image->height; i++) {
274 tmp = (*(s + 3) * opaqueness) / 256;
275 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
276 d++;
277 s++;
278 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
279 d++;
280 s++;
281 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
282 d++;
283 s++;
284 *d |= tmp;
285 d++;
286 s++;
290 #undef OP
291 #undef COP
294 static int calculateCombineArea(RImage *des, int *sx, int *sy, unsigned int *swidth,
295 unsigned int *sheight, int *dx, int *dy)
297 int width = (int)*swidth, height = (int)*sheight;
299 if (*dx < 0) {
300 *sx = -*dx;
301 width = width + *dx;
302 *dx = 0;
305 if (*dx + width > des->width) {
306 width = des->width - *dx;
309 if (*dy < 0) {
310 *sy = -*dy;
311 height = height + *dy;
312 *dy = 0;
315 if (*dy + height > des->height) {
316 height = des->height - *dy;
319 if (height > 0 && width > 0) {
320 *swidth = width;
321 *sheight = height;
322 return True;
325 return False;
328 void RCombineArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
330 int x, y, dwi, swi;
331 unsigned char *d;
332 unsigned char *s;
333 int alpha, calpha;
335 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
336 return;
338 if (!HAS_ALPHA(src)) {
339 if (!HAS_ALPHA(image)) {
340 swi = src->width * 3;
341 dwi = image->width * 3;
343 s = src->data + (sy * (int)src->width + sx) * 3;
344 d = image->data + (dy * (int)image->width + dx) * 3;
346 for (y = 0; y < height; y++) {
347 memcpy(d, s, width * 3);
348 d += dwi;
349 s += swi;
351 } else {
352 swi = (src->width - width) * 3;
353 dwi = (image->width - width) * 4;
355 s = src->data + (sy * (int)src->width + sx) * 3;
356 d = image->data + (dy * (int)image->width + dx) * 4;
358 for (y = 0; y < height; y++) {
359 for (x = 0; x < width; x++) {
360 *d++ = *s++;
361 *d++ = *s++;
362 *d++ = *s++;
363 d++;
365 d += dwi;
366 s += swi;
369 } else {
370 int dalpha = HAS_ALPHA(image);
372 swi = (src->width - width) * 4;
373 s = src->data + (sy * (int)src->width + sx) * 4;
374 if (dalpha) {
375 dwi = (image->width - width) * 4;
376 d = image->data + (dy * (int)image->width + dx) * 4;
377 } else {
378 dwi = (image->width - width) * 3;
379 d = image->data + (dy * (int)image->width + dx) * 3;
382 for (y = 0; y < height; y++) {
383 for (x = 0; x < width; x++) {
384 alpha = *(s + 3);
385 calpha = 255 - alpha;
386 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
387 s++;
388 d++;
389 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
390 s++;
391 d++;
392 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
393 s++;
394 d++;
395 s++;
396 if (dalpha)
397 d++;
399 d += dwi;
400 s += swi;
405 void RCopyArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
407 int x, y, dwi, swi;
408 unsigned char *d;
409 unsigned char *s;
411 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
412 return;
414 if (!HAS_ALPHA(src)) {
415 if (!HAS_ALPHA(image)) {
416 swi = src->width * 3;
417 dwi = image->width * 3;
419 s = src->data + (sy * (int)src->width + sx) * 3;
420 d = image->data + (dy * (int)image->width + dx) * 3;
422 for (y = 0; y < height; y++) {
423 memcpy(d, s, width * 3);
424 d += dwi;
425 s += swi;
427 } else {
428 swi = (src->width - width) * 3;
429 dwi = (image->width - width) * 4;
431 s = src->data + (sy * (int)src->width + sx) * 3;
432 d = image->data + (dy * (int)image->width + dx) * 4;
434 for (y = 0; y < height; y++) {
435 for (x = 0; x < width; x++) {
436 *d++ = *s++;
437 *d++ = *s++;
438 *d++ = *s++;
439 d++;
441 d += dwi;
442 s += swi;
445 } else {
446 int dalpha = HAS_ALPHA(image);
448 swi = src->width * 4;
449 s = src->data + (sy * (int)src->width + sx) * 4;
450 if (dalpha) {
451 dwi = image->width * 4;
452 d = image->data + (dy * (int)image->width + dx) * 4;
453 } else {
454 dwi = image->width * 3;
455 d = image->data + (dy * (int)image->width + dx) * 3;
458 if (dalpha) {
459 for (y = 0; y < height; y++) {
460 memcpy(d, s, width * 4);
461 d += dwi;
462 s += swi;
464 } else {
465 for (y = 0; y < height; y++) {
466 for (x = 0; x < width; x++) {
467 *d++ = *s++;
468 *d++ = *s++;
469 *d++ = *s++;
470 s++;
472 d += dwi;
473 s += swi;
479 void
480 RCombineAreaWithOpaqueness(RImage * image, RImage * src, int sx, int sy,
481 unsigned width, unsigned height, int dx, int dy, int opaqueness)
483 int x, y, dwi, swi;
484 int c_opaqueness;
485 unsigned char *s, *d;
486 int dalpha = HAS_ALPHA(image);
487 int dch = (dalpha ? 4 : 3);
489 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
490 return;
492 d = image->data + (dy * image->width + dx) * dch;
493 dwi = (image->width - width) * dch;
495 c_opaqueness = 255 - opaqueness;
497 #define OP opaqueness
498 #define COP c_opaqueness
500 if (!HAS_ALPHA(src)) {
502 s = src->data + (sy * src->width + sx) * 3;
503 swi = (src->width - width) * 3;
505 for (y = 0; y < height; y++) {
506 for (x = 0; x < width; x++) {
507 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
508 s++;
509 d++;
510 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
511 s++;
512 d++;
513 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
514 s++;
515 d++;
516 if (dalpha)
517 d++;
519 d += dwi;
520 s += swi;
522 } else {
523 int tmp;
525 s = src->data + (sy * src->width + sx) * 4;
526 swi = (src->width - width) * 4;
528 for (y = 0; y < height; y++) {
529 for (x = 0; x < width; x++) {
530 tmp = (*(s + 3) * opaqueness) / 256;
531 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
532 d++;
533 s++;
534 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
535 d++;
536 s++;
537 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
538 d++;
539 s++;
540 s++;
541 if (dalpha)
542 d++;
544 d += dwi;
545 s += swi;
548 #undef OP
549 #undef COP
552 void RCombineImageWithColor(RImage * image, RColor * color)
554 register int i;
555 unsigned char *d;
556 int alpha, nalpha, r, g, b;
558 d = image->data;
560 if (!HAS_ALPHA(image)) {
561 /* Image has no alpha channel, so we consider it to be all 255.
562 * Thus there are no transparent parts to be filled. */
563 return;
565 r = color->red;
566 g = color->green;
567 b = color->blue;
569 for (i = 0; i < image->width * image->height; i++) {
570 alpha = *(d + 3);
571 nalpha = 255 - alpha;
573 *d = (((int)*d * alpha) + (r * nalpha)) / 256;
574 d++;
575 *d = (((int)*d * alpha) + (g * nalpha)) / 256;
576 d++;
577 *d = (((int)*d * alpha) + (b * nalpha)) / 256;
578 d++;
579 d++;
583 RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
585 int x, y;
586 unsigned w;
587 unsigned long tile_size = tile->width * tile->height;
588 unsigned long tx = 0;
589 RImage *image;
590 unsigned char *s, *d;
592 if (width == tile->width && height == tile->height)
593 image = RCloneImage(tile);
594 else if (width <= tile->width && height <= tile->height)
595 image = RGetSubImage(tile, 0, 0, width, height);
596 else {
597 int has_alpha = HAS_ALPHA(tile);
599 image = RCreateImage(width, height, has_alpha);
601 d = image->data;
602 s = tile->data;
604 for (y = 0; y < height; y++) {
605 for (x = 0; x < width; x += tile->width) {
607 w = (width - x < tile->width) ? width - x : tile->width;
609 if (has_alpha) {
610 w *= 4;
611 memcpy(d, s + tx * 4, w);
612 } else {
613 w *= 3;
614 memcpy(d, s + tx * 3, w);
616 d += w;
619 tx = (tx + tile->width) % tile_size;
622 return image;
625 RImage *RMakeCenteredImage(RImage * image, unsigned width, unsigned height, RColor * color)
627 int x, y, w, h, sx, sy;
628 RImage *tmp;
630 tmp = RCreateImage(width, height, False);
631 if (!tmp) {
632 return NULL;
635 RClearImage(tmp, color);
637 if (image->height < height) {
638 h = image->height;
639 y = (height - h) / 2;
640 sy = 0;
641 } else {
642 sy = (image->height - height) / 2;
643 y = 0;
644 h = height;
646 if (image->width < width) {
647 w = image->width;
648 x = (width - w) / 2;
649 sx = 0;
650 } else {
651 sx = (image->width - width) / 2;
652 x = 0;
653 w = width;
655 RCombineArea(tmp, image, sx, sy, w, h, x, y);
657 return tmp;