Partially support _NET_WM_STRUT_PARTIAL.
[wmaker-crm.git] / wrlib / raster.c
blobf01db848e2b441a2d57bf8513b408b67805db29d
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++ = 255;
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 RCombineAlpha(d, s, 1, image->width, image->height, 0, 0, 255);
208 void RCombineImagesWithOpaqueness(RImage * image, RImage * src, int opaqueness)
210 register int i;
211 unsigned char *d;
212 unsigned char *s;
213 int c_opaqueness;
215 assert(image->width == src->width);
216 assert(image->height == src->height);
218 d = image->data;
219 s = src->data;
221 c_opaqueness = 255 - opaqueness;
223 #define OP opaqueness
224 #define COP c_opaqueness
226 if (!HAS_ALPHA(src)) {
227 if (!HAS_ALPHA(image)) {
228 for (i = 0; i < image->width * image->height; i++) {
229 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
230 d++;
231 s++;
232 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
233 d++;
234 s++;
235 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
236 d++;
237 s++;
239 } else {
240 RCombineAlpha(d, s, 0, image->width, image->height, 0, 0, OP);
242 } else {
243 int tmp;
245 if (!HAS_ALPHA(image)) {
246 for (i = 0; i < image->width * image->height; i++) {
247 tmp = (*(s + 3) * opaqueness) / 256;
248 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
249 d++;
250 s++;
251 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
252 d++;
253 s++;
254 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
255 d++;
256 s++;
257 s++;
259 } else {
260 RCombineAlpha(d, s, 1, image->width, image->height, 0, 0, opaqueness);
263 #undef OP
264 #undef COP
267 static int calculateCombineArea(RImage *des, int *sx, int *sy, unsigned int *swidth,
268 unsigned int *sheight, int *dx, int *dy)
270 int width = (int)*swidth, height = (int)*sheight;
272 if (*dx < 0) {
273 *sx = -*dx;
274 width = width + *dx;
275 *dx = 0;
278 if (*dx + width > des->width) {
279 width = des->width - *dx;
282 if (*dy < 0) {
283 *sy = -*dy;
284 height = height + *dy;
285 *dy = 0;
288 if (*dy + height > des->height) {
289 height = des->height - *dy;
292 if (height > 0 && width > 0) {
293 *swidth = width;
294 *sheight = height;
295 return True;
298 return False;
301 void RCombineArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
303 int x, y, dwi, swi;
304 unsigned char *d;
305 unsigned char *s;
306 int alpha, calpha;
308 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
309 return;
311 if (!HAS_ALPHA(src)) {
312 if (!HAS_ALPHA(image)) {
313 swi = src->width * 3;
314 dwi = image->width * 3;
316 s = src->data + (sy * (int)src->width + sx) * 3;
317 d = image->data + (dy * (int)image->width + dx) * 3;
319 for (y = 0; y < height; y++) {
320 memcpy(d, s, width * 3);
321 d += dwi;
322 s += swi;
324 } else {
325 swi = (src->width - width) * 3;
326 dwi = (image->width - width) * 4;
328 s = src->data + (sy * (int)src->width + sx) * 3;
329 d = image->data + (dy * (int)image->width + dx) * 4;
331 for (y = 0; y < height; y++) {
332 for (x = 0; x < width; x++) {
333 *d++ = *s++;
334 *d++ = *s++;
335 *d++ = *s++;
336 *d++ = 255;
338 d += dwi;
339 s += swi;
342 } else {
343 int dalpha = HAS_ALPHA(image);
345 swi = (src->width - width) * 4;
346 s = src->data + (sy * (int)src->width + sx) * 4;
347 if (dalpha) {
348 dwi = (image->width - width) * 4;
349 d = image->data + (dy * (int)image->width + dx) * 4;
350 } else {
351 dwi = (image->width - width) * 3;
352 d = image->data + (dy * (int)image->width + dx) * 3;
355 if (!dalpha) {
356 for (y = 0; y < height; y++) {
357 for (x = 0; x < width; x++) {
358 alpha = *(s + 3);
359 calpha = 255 - alpha;
360 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
361 s++;
362 d++;
363 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
364 s++;
365 d++;
366 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
367 s++;
368 d++;
369 s++;
371 d += dwi;
372 s += swi;
374 } else {
375 RCombineAlpha(d, s, 1, width, height, dwi, swi, 255);
380 void RCopyArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
382 int x, y, dwi, swi;
383 unsigned char *d;
384 unsigned char *s;
386 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
387 return;
389 if (!HAS_ALPHA(src)) {
390 if (!HAS_ALPHA(image)) {
391 swi = src->width * 3;
392 dwi = image->width * 3;
394 s = src->data + (sy * (int)src->width + sx) * 3;
395 d = image->data + (dy * (int)image->width + dx) * 3;
397 for (y = 0; y < height; y++) {
398 memcpy(d, s, width * 3);
399 d += dwi;
400 s += swi;
402 } else {
403 swi = (src->width - width) * 3;
404 dwi = (image->width - width) * 4;
406 s = src->data + (sy * (int)src->width + sx) * 3;
407 d = image->data + (dy * (int)image->width + dx) * 4;
409 for (y = 0; y < height; y++) {
410 for (x = 0; x < width; x++) {
411 *d++ = *s++;
412 *d++ = *s++;
413 *d++ = *s++;
414 d++;
416 d += dwi;
417 s += swi;
420 } else {
421 int dalpha = HAS_ALPHA(image);
423 swi = src->width * 4;
424 s = src->data + (sy * (int)src->width + sx) * 4;
425 if (dalpha) {
426 dwi = image->width * 4;
427 d = image->data + (dy * (int)image->width + dx) * 4;
428 } else {
429 dwi = image->width * 3;
430 d = image->data + (dy * (int)image->width + dx) * 3;
433 if (dalpha) {
434 for (y = 0; y < height; y++) {
435 memcpy(d, s, width * 4);
436 d += dwi;
437 s += swi;
439 } else {
440 for (y = 0; y < height; y++) {
441 for (x = 0; x < width; x++) {
442 *d++ = *s++;
443 *d++ = *s++;
444 *d++ = *s++;
445 s++;
447 d += dwi;
448 s += swi;
454 void
455 RCombineAreaWithOpaqueness(RImage * image, RImage * src, int sx, int sy,
456 unsigned width, unsigned height, int dx, int dy, int opaqueness)
458 int x, y, dwi, swi;
459 int c_opaqueness;
460 unsigned char *s, *d;
461 int dalpha = HAS_ALPHA(image);
462 int dch = (dalpha ? 4 : 3);
464 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
465 return;
467 d = image->data + (dy * image->width + dx) * dch;
468 dwi = (image->width - width) * dch;
470 c_opaqueness = 255 - opaqueness;
472 #define OP opaqueness
473 #define COP c_opaqueness
475 if (!HAS_ALPHA(src)) {
477 s = src->data + (sy * src->width + sx) * 3;
478 swi = (src->width - width) * 3;
480 if (!dalpha) {
481 for (y = 0; y < height; y++) {
482 for (x = 0; x < width; x++) {
483 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
484 s++;
485 d++;
486 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
487 s++;
488 d++;
489 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
490 s++;
491 d++;
493 d += dwi;
494 s += swi;
496 } else {
497 RCombineAlpha(d, s, 0, width, height, dwi, swi, OP);
499 } else {
500 int tmp;
502 s = src->data + (sy * src->width + sx) * 4;
503 swi = (src->width - width) * 4;
505 if (!dalpha) {
506 for (y = 0; y < height; y++) {
507 for (x = 0; x < width; x++) {
508 tmp = (*(s + 3) * opaqueness) / 256;
509 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
510 d++;
511 s++;
512 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
513 d++;
514 s++;
515 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
516 d++;
517 s++;
518 s++;
520 d += dwi;
521 s += swi;
523 } else {
524 RCombineAlpha(d, s, 1, width, height, dwi, swi, OP);
527 #undef OP
528 #undef COP
531 void RCombineImageWithColor(RImage * image, RColor * color)
533 register int i;
534 unsigned char *d;
535 int alpha, nalpha, r, g, b;
537 d = image->data;
539 if (!HAS_ALPHA(image)) {
540 /* Image has no alpha channel, so we consider it to be all 255.
541 * Thus there are no transparent parts to be filled. */
542 return;
544 r = color->red;
545 g = color->green;
546 b = color->blue;
548 for (i = 0; i < image->width * image->height; i++) {
549 alpha = *(d + 3);
550 nalpha = 255 - alpha;
552 *d = (((int)*d * alpha) + (r * nalpha)) / 256;
553 d++;
554 *d = (((int)*d * alpha) + (g * nalpha)) / 256;
555 d++;
556 *d = (((int)*d * alpha) + (b * nalpha)) / 256;
557 d++;
558 d++;
562 RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
564 int x, y;
565 unsigned w;
566 unsigned long tile_size = tile->width * tile->height;
567 unsigned long tx = 0;
568 RImage *image;
569 unsigned char *s, *d;
571 if (width == tile->width && height == tile->height)
572 image = RCloneImage(tile);
573 else if (width <= tile->width && height <= tile->height)
574 image = RGetSubImage(tile, 0, 0, width, height);
575 else {
576 int has_alpha = HAS_ALPHA(tile);
578 image = RCreateImage(width, height, has_alpha);
580 d = image->data;
581 s = tile->data;
583 for (y = 0; y < height; y++) {
584 for (x = 0; x < width; x += tile->width) {
586 w = (width - x < tile->width) ? width - x : tile->width;
588 if (has_alpha) {
589 w *= 4;
590 memcpy(d, s + tx * 4, w);
591 } else {
592 w *= 3;
593 memcpy(d, s + tx * 3, w);
595 d += w;
598 tx = (tx + tile->width) % tile_size;
601 return image;
604 RImage *RMakeCenteredImage(RImage * image, unsigned width, unsigned height, RColor * color)
606 int x, y, w, h, sx, sy;
607 RImage *tmp;
609 tmp = RCreateImage(width, height, HAS_ALPHA(image));
610 if (!tmp) {
611 return NULL;
614 RFillImage(tmp, color);
616 if (image->height < height) {
617 h = image->height;
618 y = (height - h) / 2;
619 sy = 0;
620 } else {
621 sy = (image->height - height) / 2;
622 y = 0;
623 h = height;
625 if (image->width < width) {
626 w = image->width;
627 x = (width - w) / 2;
628 sx = 0;
629 } else {
630 sx = (image->width - width) / 2;
631 x = 0;
632 w = width;
634 RCombineArea(tmp, image, sx, sy, w, h, x, y);
636 return tmp;