Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / raster.c
blobe236085870d06f3fe6322158a5c4cca7c219b2f1
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
295 calculateCombineArea(RImage * des, RImage * src, int *sx, int *sy,
296 unsigned int *swidth, unsigned int *sheight, int *dx, int *dy)
298 int width = (int)*swidth, height = (int)*sheight;
300 if (*dx < 0) {
301 *sx = -*dx;
302 width = width + *dx;
303 *dx = 0;
306 if (*dx + width > des->width) {
307 width = des->width - *dx;
310 if (*dy < 0) {
311 *sy = -*dy;
312 height = height + *dy;
313 *dy = 0;
316 if (*dy + height > des->height) {
317 height = des->height - *dy;
320 if (height > 0 && width > 0) {
321 *swidth = width;
322 *sheight = height;
323 return True;
326 return False;
329 void RCombineArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
331 int x, y, dwi, swi;
332 unsigned char *d;
333 unsigned char *s;
334 int alpha, calpha;
336 if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
337 return;
339 if (!HAS_ALPHA(src)) {
340 if (!HAS_ALPHA(image)) {
341 swi = src->width * 3;
342 dwi = image->width * 3;
344 s = src->data + (sy * (int)src->width + sx) * 3;
345 d = image->data + (dy * (int)image->width + dx) * 3;
347 for (y = 0; y < height; y++) {
348 memcpy(d, s, width * 3);
349 d += dwi;
350 s += swi;
352 } else {
353 swi = (src->width - width) * 3;
354 dwi = (image->width - width) * 4;
356 s = src->data + (sy * (int)src->width + sx) * 3;
357 d = image->data + (dy * (int)image->width + dx) * 4;
359 for (y = 0; y < height; y++) {
360 for (x = 0; x < width; x++) {
361 *d++ = *s++;
362 *d++ = *s++;
363 *d++ = *s++;
364 d++;
366 d += dwi;
367 s += swi;
370 } else {
371 int dalpha = HAS_ALPHA(image);
373 swi = (src->width - width) * 4;
374 s = src->data + (sy * (int)src->width + sx) * 4;
375 if (dalpha) {
376 dwi = (image->width - width) * 4;
377 d = image->data + (dy * (int)image->width + dx) * 4;
378 } else {
379 dwi = (image->width - width) * 3;
380 d = image->data + (dy * (int)image->width + dx) * 3;
383 for (y = 0; y < height; y++) {
384 for (x = 0; x < width; x++) {
385 alpha = *(s + 3);
386 calpha = 255 - alpha;
387 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
388 s++;
389 d++;
390 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
391 s++;
392 d++;
393 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
394 s++;
395 d++;
396 s++;
397 if (dalpha)
398 d++;
400 d += dwi;
401 s += swi;
406 void RCopyArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
408 int x, y, dwi, swi;
409 unsigned char *d;
410 unsigned char *s;
412 if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
413 return;
415 if (!HAS_ALPHA(src)) {
416 if (!HAS_ALPHA(image)) {
417 swi = src->width * 3;
418 dwi = image->width * 3;
420 s = src->data + (sy * (int)src->width + sx) * 3;
421 d = image->data + (dy * (int)image->width + dx) * 3;
423 for (y = 0; y < height; y++) {
424 memcpy(d, s, width * 3);
425 d += dwi;
426 s += swi;
428 } else {
429 swi = (src->width - width) * 3;
430 dwi = (image->width - width) * 4;
432 s = src->data + (sy * (int)src->width + sx) * 3;
433 d = image->data + (dy * (int)image->width + dx) * 4;
435 for (y = 0; y < height; y++) {
436 for (x = 0; x < width; x++) {
437 *d++ = *s++;
438 *d++ = *s++;
439 *d++ = *s++;
440 d++;
442 d += dwi;
443 s += swi;
446 } else {
447 int dalpha = HAS_ALPHA(image);
449 swi = src->width * 4;
450 s = src->data + (sy * (int)src->width + sx) * 4;
451 if (dalpha) {
452 dwi = image->width * 4;
453 d = image->data + (dy * (int)image->width + dx) * 4;
454 } else {
455 dwi = image->width * 3;
456 d = image->data + (dy * (int)image->width + dx) * 3;
459 if (dalpha) {
460 for (y = 0; y < height; y++) {
461 memcpy(d, s, width * 4);
462 d += dwi;
463 s += swi;
465 } else {
466 for (y = 0; y < height; y++) {
467 for (x = 0; x < width; x++) {
468 *d++ = *s++;
469 *d++ = *s++;
470 *d++ = *s++;
471 s++;
473 d += dwi;
474 s += swi;
480 void
481 RCombineAreaWithOpaqueness(RImage * image, RImage * src, int sx, int sy,
482 unsigned width, unsigned height, int dx, int dy, int opaqueness)
484 int x, y, dwi, swi;
485 int c_opaqueness;
486 unsigned char *s, *d;
487 int dalpha = HAS_ALPHA(image);
488 int dch = (dalpha ? 4 : 3);
490 if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
491 return;
493 d = image->data + (dy * image->width + dx) * dch;
494 dwi = (image->width - width) * dch;
496 c_opaqueness = 255 - opaqueness;
498 #define OP opaqueness
499 #define COP c_opaqueness
501 if (!HAS_ALPHA(src)) {
503 s = src->data + (sy * src->width + sx) * 3;
504 swi = (src->width - width) * 3;
506 for (y = 0; y < height; y++) {
507 for (x = 0; x < width; x++) {
508 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
509 s++;
510 d++;
511 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
512 s++;
513 d++;
514 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
515 s++;
516 d++;
517 if (dalpha)
518 d++;
520 d += dwi;
521 s += swi;
523 } else {
524 int tmp;
526 s = src->data + (sy * src->width + sx) * 4;
527 swi = (src->width - width) * 4;
529 for (y = 0; y < height; y++) {
530 for (x = 0; x < width; x++) {
531 tmp = (*(s + 3) * opaqueness) / 256;
532 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
533 d++;
534 s++;
535 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
536 d++;
537 s++;
538 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
539 d++;
540 s++;
541 s++;
542 if (dalpha)
543 d++;
545 d += dwi;
546 s += swi;
549 #undef OP
550 #undef COP
553 void RCombineImageWithColor(RImage * image, RColor * color)
555 register int i;
556 unsigned char *d;
557 int alpha, nalpha, r, g, b;
559 d = image->data;
561 if (!HAS_ALPHA(image)) {
562 /* Image has no alpha channel, so we consider it to be all 255.
563 * Thus there are no transparent parts to be filled. */
564 return;
566 r = color->red;
567 g = color->green;
568 b = color->blue;
570 for (i = 0; i < image->width * image->height; i++) {
571 alpha = *(d + 3);
572 nalpha = 255 - alpha;
574 *d = (((int)*d * alpha) + (r * nalpha)) / 256;
575 d++;
576 *d = (((int)*d * alpha) + (g * nalpha)) / 256;
577 d++;
578 *d = (((int)*d * alpha) + (b * nalpha)) / 256;
579 d++;
580 d++;
584 RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
586 int x, y;
587 unsigned w;
588 unsigned long tile_size = tile->width * tile->height;
589 unsigned long tx = 0;
590 RImage *image;
591 unsigned char *s, *d;
593 if (width == tile->width && height == tile->height)
594 image = RCloneImage(tile);
595 else if (width <= tile->width && height <= tile->height)
596 image = RGetSubImage(tile, 0, 0, width, height);
597 else {
598 int has_alpha = HAS_ALPHA(tile);
600 image = RCreateImage(width, height, has_alpha);
602 d = image->data;
603 s = tile->data;
605 for (y = 0; y < height; y++) {
606 for (x = 0; x < width; x += tile->width) {
608 w = (width - x < tile->width) ? width - x : tile->width;
610 if (has_alpha) {
611 w *= 4;
612 memcpy(d, s + tx * 4, w);
613 } else {
614 w *= 3;
615 memcpy(d, s + tx * 3, w);
617 d += w;
620 tx = (tx + tile->width) % tile_size;
623 return image;
626 RImage *RMakeCenteredImage(RImage * image, unsigned width, unsigned height, RColor * color)
628 int x, y, w, h, sx, sy;
629 RImage *tmp;
631 tmp = RCreateImage(width, height, False);
632 if (!tmp) {
633 return NULL;
636 RClearImage(tmp, color);
638 if (image->height < height) {
639 h = image->height;
640 y = (height - h) / 2;
641 sy = 0;
642 } else {
643 sy = (image->height - height) / 2;
644 y = 0;
645 h = height;
647 if (image->width < width) {
648 w = image->width;
649 x = (width - w) / 2;
650 sx = 0;
651 } else {
652 sx = (image->width - width) / 2;
653 x = 0;
654 w = width;
656 RCombineArea(tmp, image, sx, sy, w, h, x, y);
658 return tmp;