Undim switchpanel icons when selecting windows directly.
[wmaker-crm.git] / wrlib / raster.c
blobe2a2d997eee18330a0488e7a11932b7a2efe4f66
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., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <X11/Xlib.h>
29 #include "wraster.h"
31 #include <assert.h>
33 char *WRasterLibVersion = "0.9";
35 int RErrorCode = RERR_NONE;
37 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
39 #define MAX_WIDTH 20000
40 #define MAX_HEIGHT 20000
41 /* 20000^2*4 < 2G */
43 RImage *RCreateImage(unsigned width, unsigned height, int alpha)
45 RImage *image = NULL;
47 assert(width > 0 && height > 0);
49 if (width > MAX_WIDTH || height > MAX_HEIGHT) {
50 RErrorCode = RERR_NOMEMORY;
51 return NULL;
54 image = malloc(sizeof(RImage));
55 if (!image) {
56 RErrorCode = RERR_NOMEMORY;
57 return NULL;
60 memset(image, 0, sizeof(RImage));
61 image->width = width;
62 image->height = height;
63 image->format = alpha ? RRGBAFormat : RRGBFormat;
64 image->refCount = 1;
66 /* the +4 is to give extra bytes at the end of the buffer,
67 * so that we can optimize image conversion for MMX(tm).. see convert.c
69 image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
70 if (!image->data) {
71 RErrorCode = RERR_NOMEMORY;
72 free(image);
73 image = NULL;
76 return image;
80 RImage *RRetainImage(RImage * image)
82 if (image)
83 image->refCount++;
85 return image;
88 void RReleaseImage(RImage * image)
90 assert(image != NULL);
92 image->refCount--;
94 if (image->refCount < 1) {
95 free(image->data);
96 free(image);
100 RImage *RCloneImage(RImage * image)
102 RImage *new_image;
104 assert(image != NULL);
106 new_image = RCreateImage(image->width, image->height, HAS_ALPHA(image));
107 if (!new_image)
108 return NULL;
110 new_image->background = image->background;
111 memcpy(new_image->data, image->data, image->width * image->height * (HAS_ALPHA(image) ? 4 : 3));
113 return new_image;
116 RImage *RGetSubImage(RImage * image, int x, int y, unsigned width, unsigned height)
118 int i, ofs;
119 RImage *new_image;
120 unsigned total_line_size, line_size;
122 assert(image != NULL);
123 assert(x >= 0 && y >= 0);
124 assert(x < image->width && y < image->height);
125 assert(width > 0 && height > 0);
127 if (x + width > image->width)
128 width = image->width - x;
129 if (y + height > image->height)
130 height = image->height - y;
132 new_image = RCreateImage(width, height, HAS_ALPHA(image));
134 if (!new_image)
135 return NULL;
136 new_image->background = image->background;
138 total_line_size = image->width * (HAS_ALPHA(image) ? 4 : 3);
139 line_size = width * (HAS_ALPHA(image) ? 4 : 3);
141 ofs = x * (HAS_ALPHA(image) ? 4 : 3) + y * total_line_size;;
143 for (i = 0; i < height; i++) {
144 memcpy(&new_image->data[i * line_size], &image->data[i * total_line_size + ofs], line_size);
146 return new_image;
150 *----------------------------------------------------------------------
151 * RCombineImages-
152 * Combines two equal sized images with alpha image. The second
153 * image will be placed on top of the first one.
154 *----------------------------------------------------------------------
156 void RCombineImages(RImage * image, RImage * src)
158 assert(image->width == src->width);
159 assert(image->height == src->height);
161 if (!HAS_ALPHA(src)) {
162 if (!HAS_ALPHA(image)) {
163 memcpy(image->data, src->data, image->height * image->width * 3);
164 } else {
165 int x, y;
166 unsigned char *d, *s;
168 d = image->data;
169 s = src->data;
170 for (y = 0; y < image->height; y++) {
171 for (x = 0; x < image->width; x++) {
172 *d++ = *s++;
173 *d++ = *s++;
174 *d++ = *s++;
175 *d++ = 255;
179 } else {
180 register int i;
181 unsigned char *d;
182 unsigned char *s;
183 int alpha, calpha;
185 d = image->data;
186 s = src->data;
188 if (!HAS_ALPHA(image)) {
189 for (i = 0; i < image->height * image->width; i++) {
190 alpha = *(s + 3);
191 calpha = 255 - alpha;
192 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
193 d++;
194 s++;
195 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
196 d++;
197 s++;
198 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
199 d++;
200 s++;
201 s++;
203 } else {
204 RCombineAlpha(d, s, 1, image->width, image->height, 0, 0, 255);
209 void RCombineImagesWithOpaqueness(RImage * image, RImage * src, int opaqueness)
211 register int i;
212 unsigned char *d;
213 unsigned char *s;
214 int c_opaqueness;
216 assert(image->width == src->width);
217 assert(image->height == src->height);
219 d = image->data;
220 s = src->data;
222 c_opaqueness = 255 - opaqueness;
224 #define OP opaqueness
225 #define COP c_opaqueness
227 if (!HAS_ALPHA(src)) {
228 if (!HAS_ALPHA(image)) {
229 for (i = 0; i < image->width * image->height; i++) {
230 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
231 d++;
232 s++;
233 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
234 d++;
235 s++;
236 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
237 d++;
238 s++;
240 } else {
241 RCombineAlpha(d, s, 0, image->width, image->height, 0, 0, OP);
243 } else {
244 int tmp;
246 if (!HAS_ALPHA(image)) {
247 for (i = 0; i < image->width * image->height; i++) {
248 tmp = (*(s + 3) * opaqueness) / 256;
249 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
250 d++;
251 s++;
252 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
253 d++;
254 s++;
255 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
256 d++;
257 s++;
258 s++;
260 } else {
261 RCombineAlpha(d, s, 1, image->width, image->height, 0, 0, opaqueness);
264 #undef OP
265 #undef COP
268 static int calculateCombineArea(RImage *des, int *sx, int *sy, unsigned int *swidth,
269 unsigned int *sheight, int *dx, int *dy)
271 int width = (int)*swidth, height = (int)*sheight;
273 if (*dx < 0) {
274 *sx = -*dx;
275 width = width + *dx;
276 *dx = 0;
279 if (*dx + width > des->width) {
280 width = des->width - *dx;
283 if (*dy < 0) {
284 *sy = -*dy;
285 height = height + *dy;
286 *dy = 0;
289 if (*dy + height > des->height) {
290 height = des->height - *dy;
293 if (height > 0 && width > 0) {
294 *swidth = width;
295 *sheight = height;
296 return True;
299 return False;
302 void RCombineArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
304 int x, y, dwi, swi;
305 unsigned char *d;
306 unsigned char *s;
307 int alpha, calpha;
309 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
310 return;
312 if (!HAS_ALPHA(src)) {
313 if (!HAS_ALPHA(image)) {
314 swi = src->width * 3;
315 dwi = image->width * 3;
317 s = src->data + (sy * (int)src->width + sx) * 3;
318 d = image->data + (dy * (int)image->width + dx) * 3;
320 for (y = 0; y < height; y++) {
321 memcpy(d, s, width * 3);
322 d += dwi;
323 s += swi;
325 } else {
326 swi = (src->width - width) * 3;
327 dwi = (image->width - width) * 4;
329 s = src->data + (sy * (int)src->width + sx) * 3;
330 d = image->data + (dy * (int)image->width + dx) * 4;
332 for (y = 0; y < height; y++) {
333 for (x = 0; x < width; x++) {
334 *d++ = *s++;
335 *d++ = *s++;
336 *d++ = *s++;
337 *d++ = 255;
339 d += dwi;
340 s += swi;
343 } else {
344 int dalpha = HAS_ALPHA(image);
346 swi = (src->width - width) * 4;
347 s = src->data + (sy * (int)src->width + sx) * 4;
348 if (dalpha) {
349 dwi = (image->width - width) * 4;
350 d = image->data + (dy * (int)image->width + dx) * 4;
351 } else {
352 dwi = (image->width - width) * 3;
353 d = image->data + (dy * (int)image->width + dx) * 3;
356 if (!dalpha) {
357 for (y = 0; y < height; y++) {
358 for (x = 0; x < width; x++) {
359 alpha = *(s + 3);
360 calpha = 255 - alpha;
361 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
362 s++;
363 d++;
364 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
365 s++;
366 d++;
367 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
368 s++;
369 d++;
370 s++;
372 d += dwi;
373 s += swi;
375 } else {
376 RCombineAlpha(d, s, 1, width, height, dwi, swi, 255);
381 void RCopyArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
383 int x, y, dwi, swi;
384 unsigned char *d;
385 unsigned char *s;
387 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
388 return;
390 if (!HAS_ALPHA(src)) {
391 if (!HAS_ALPHA(image)) {
392 swi = src->width * 3;
393 dwi = image->width * 3;
395 s = src->data + (sy * (int)src->width + sx) * 3;
396 d = image->data + (dy * (int)image->width + dx) * 3;
398 for (y = 0; y < height; y++) {
399 memcpy(d, s, width * 3);
400 d += dwi;
401 s += swi;
403 } else {
404 swi = (src->width - width) * 3;
405 dwi = (image->width - width) * 4;
407 s = src->data + (sy * (int)src->width + sx) * 3;
408 d = image->data + (dy * (int)image->width + dx) * 4;
410 for (y = 0; y < height; y++) {
411 for (x = 0; x < width; x++) {
412 *d++ = *s++;
413 *d++ = *s++;
414 *d++ = *s++;
415 d++;
417 d += dwi;
418 s += swi;
421 } else {
422 int dalpha = HAS_ALPHA(image);
424 swi = src->width * 4;
425 s = src->data + (sy * (int)src->width + sx) * 4;
426 if (dalpha) {
427 dwi = image->width * 4;
428 d = image->data + (dy * (int)image->width + dx) * 4;
429 } else {
430 dwi = image->width * 3;
431 d = image->data + (dy * (int)image->width + dx) * 3;
434 if (dalpha) {
435 for (y = 0; y < height; y++) {
436 memcpy(d, s, width * 4);
437 d += dwi;
438 s += swi;
440 } else {
441 for (y = 0; y < height; y++) {
442 for (x = 0; x < width; x++) {
443 *d++ = *s++;
444 *d++ = *s++;
445 *d++ = *s++;
446 s++;
448 d += dwi;
449 s += swi;
455 void
456 RCombineAreaWithOpaqueness(RImage * image, RImage * src, int sx, int sy,
457 unsigned width, unsigned height, int dx, int dy, int opaqueness)
459 int x, y, dwi, swi;
460 int c_opaqueness;
461 unsigned char *s, *d;
462 int dalpha = HAS_ALPHA(image);
463 int dch = (dalpha ? 4 : 3);
465 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
466 return;
468 d = image->data + (dy * image->width + dx) * dch;
469 dwi = (image->width - width) * dch;
471 c_opaqueness = 255 - opaqueness;
473 #define OP opaqueness
474 #define COP c_opaqueness
476 if (!HAS_ALPHA(src)) {
478 s = src->data + (sy * src->width + sx) * 3;
479 swi = (src->width - width) * 3;
481 if (!dalpha) {
482 for (y = 0; y < height; y++) {
483 for (x = 0; x < width; x++) {
484 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
485 s++;
486 d++;
487 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
488 s++;
489 d++;
490 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
491 s++;
492 d++;
494 d += dwi;
495 s += swi;
497 } else {
498 RCombineAlpha(d, s, 0, width, height, dwi, swi, OP);
500 } else {
501 int tmp;
503 s = src->data + (sy * src->width + sx) * 4;
504 swi = (src->width - width) * 4;
506 if (!dalpha) {
507 for (y = 0; y < height; y++) {
508 for (x = 0; x < width; x++) {
509 tmp = (*(s + 3) * opaqueness) / 256;
510 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
511 d++;
512 s++;
513 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
514 d++;
515 s++;
516 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
517 d++;
518 s++;
519 s++;
521 d += dwi;
522 s += swi;
524 } else {
525 RCombineAlpha(d, s, 1, width, height, dwi, swi, OP);
528 #undef OP
529 #undef COP
532 void RCombineImageWithColor(RImage * image, const RColor * color)
534 register int i;
535 unsigned char *d;
536 int alpha, nalpha, r, g, b;
538 d = image->data;
540 if (!HAS_ALPHA(image)) {
541 /* Image has no alpha channel, so we consider it to be all 255.
542 * Thus there are no transparent parts to be filled. */
543 return;
545 r = color->red;
546 g = color->green;
547 b = color->blue;
549 for (i = 0; i < image->width * image->height; i++) {
550 alpha = *(d + 3);
551 nalpha = 255 - alpha;
553 *d = (((int)*d * alpha) + (r * nalpha)) / 256;
554 d++;
555 *d = (((int)*d * alpha) + (g * nalpha)) / 256;
556 d++;
557 *d = (((int)*d * alpha) + (b * nalpha)) / 256;
558 d++;
559 d++;
563 RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
565 int x, y;
566 unsigned w;
567 unsigned long tile_size = tile->width * tile->height;
568 unsigned long tx = 0;
569 RImage *image;
570 unsigned char *s, *d;
572 if (width == tile->width && height == tile->height)
573 image = RCloneImage(tile);
574 else if (width <= tile->width && height <= tile->height)
575 image = RGetSubImage(tile, 0, 0, width, height);
576 else {
577 int has_alpha = HAS_ALPHA(tile);
579 image = RCreateImage(width, height, has_alpha);
581 d = image->data;
582 s = tile->data;
584 for (y = 0; y < height; y++) {
585 for (x = 0; x < width; x += tile->width) {
587 w = (width - x < tile->width) ? width - x : tile->width;
589 if (has_alpha) {
590 w *= 4;
591 memcpy(d, s + tx * 4, w);
592 } else {
593 w *= 3;
594 memcpy(d, s + tx * 3, w);
596 d += w;
599 tx = (tx + tile->width) % tile_size;
602 return image;
605 RImage *RMakeCenteredImage(RImage * image, unsigned width, unsigned height, const RColor * color)
607 int x, y, w, h, sx, sy;
608 RImage *tmp;
610 tmp = RCreateImage(width, height, HAS_ALPHA(image));
611 if (!tmp) {
612 return NULL;
615 RFillImage(tmp, color);
617 if (image->height < height) {
618 h = image->height;
619 y = (height - h) / 2;
620 sy = 0;
621 } else {
622 sy = (image->height - height) / 2;
623 y = 0;
624 h = height;
626 if (image->width < width) {
627 w = image->width;
628 x = (width - w) / 2;
629 sx = 0;
630 } else {
631 sx = (image->width - width) / 2;
632 x = 0;
633 w = width;
635 RCombineArea(tmp, image, sx, sy, w, h, x, y);
637 return tmp;