Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / raster.c
1 /* raster.c - main and other misc stuff
2  *
3  * Raster graphics library
4  *
5  * Copyright (c) 1997-2003 Alfredo K. Kojima
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
28 #include "wraster.h"
29
30 #include <assert.h>
31
32 char *WRasterLibVersion = "0.9";
33
34 int RErrorCode = RERR_NONE;
35
36 #define HAS_ALPHA(I)    ((I)->format == RRGBAFormat)
37
38 #define MAX_WIDTH 20000
39 #define MAX_HEIGHT 20000
40 /* 20000^2*4 < 2G */
41
42 RImage *RCreateImage(unsigned width, unsigned height, int alpha)
43 {
44         RImage *image = NULL;
45
46         assert(width > 0 && height > 0);
47
48         if (width > MAX_WIDTH || height > MAX_HEIGHT) {
49                 RErrorCode = RERR_NOMEMORY;
50                 return NULL;
51         }
52
53         image = malloc(sizeof(RImage));
54         if (!image) {
55                 RErrorCode = RERR_NOMEMORY;
56                 return NULL;
57         }
58
59         memset(image, 0, sizeof(RImage));
60         image->width = width;
61         image->height = height;
62         image->format = alpha ? RRGBAFormat : RRGBFormat;
63         image->refCount = 1;
64
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
67          */
68         image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
69         if (!image->data) {
70                 RErrorCode = RERR_NOMEMORY;
71                 free(image);
72                 image = NULL;
73         }
74
75         return image;
76
77 }
78
79 RImage *RRetainImage(RImage * image)
80 {
81         if (image)
82                 image->refCount++;
83
84         return image;
85 }
86
87 void RReleaseImage(RImage * image)
88 {
89         assert(image != NULL);
90
91         image->refCount--;
92
93         if (image->refCount < 1) {
94                 free(image->data);
95                 free(image);
96         }
97 }
98
99 RImage *RCloneImage(RImage * image)
100 {
101         RImage *new_image;
102
103         assert(image != NULL);
104
105         new_image = RCreateImage(image->width, image->height, HAS_ALPHA(image));
106         if (!new_image)
107                 return NULL;
108
109         new_image->background = image->background;
110         memcpy(new_image->data, image->data, image->width * image->height * (HAS_ALPHA(image) ? 4 : 3));
111
112         return new_image;
113 }
114
115 RImage *RGetSubImage(RImage * image, int x, int y, unsigned width, unsigned height)
116 {
117         int i, ofs;
118         RImage *new_image;
119         unsigned total_line_size, line_size;
120
121         assert(image != NULL);
122         assert(x >= 0 && y >= 0);
123         assert(x < image->width && y < image->height);
124         assert(width > 0 && height > 0);
125
126         if (x + width > image->width)
127                 width = image->width - x;
128         if (y + height > image->height)
129                 height = image->height - y;
130
131         new_image = RCreateImage(width, height, HAS_ALPHA(image));
132
133         if (!new_image)
134                 return NULL;
135         new_image->background = image->background;
136
137         total_line_size = image->width * (HAS_ALPHA(image) ? 4 : 3);
138         line_size = width * (HAS_ALPHA(image) ? 4 : 3);
139
140         ofs = x * (HAS_ALPHA(image) ? 4 : 3) + y * total_line_size;;
141
142         for (i = 0; i < height; i++) {
143                 memcpy(&new_image->data[i * line_size], &image->data[i * total_line_size + ofs], line_size);
144         }
145         return new_image;
146 }
147
148 /*
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  *----------------------------------------------------------------------
154  */
155 void RCombineImages(RImage * image, RImage * src)
156 {
157         assert(image->width == src->width);
158         assert(image->height == src->height);
159
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;
166
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++;
175                                 }
176                         }
177                 }
178         } else {
179                 register int i;
180                 unsigned char *d;
181                 unsigned char *s;
182                 int alpha, calpha;
183
184                 d = image->data;
185                 s = src->data;
186
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++;
201                         }
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++;
216                         }
217                 }
218         }
219 }
220
221 void RCombineImagesWithOpaqueness(RImage * image, RImage * src, int opaqueness)
222 {
223         register int i;
224         unsigned char *d;
225         unsigned char *s;
226         int c_opaqueness;
227
228         assert(image->width == src->width);
229         assert(image->height == src->height);
230
231         d = image->data;
232         s = src->data;
233
234         c_opaqueness = 255 - opaqueness;
235
236 #define OP opaqueness
237 #define COP c_opaqueness
238
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++;
253                         }
254                 }
255         } else {
256                 int tmp;
257
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++;
271                         }
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++;
287                         }
288                 }
289         }
290 #undef OP
291 #undef COP
292 }
293
294 int
295 calculateCombineArea(RImage * des, RImage * src, int *sx, int *sy,
296                      unsigned int *swidth, unsigned int *sheight, int *dx, int *dy)
297 {
298         int width = (int)*swidth, height = (int)*sheight;
299
300         if (*dx < 0) {
301                 *sx = -*dx;
302                 width = width + *dx;
303                 *dx = 0;
304         }
305
306         if (*dx + width > des->width) {
307                 width = des->width - *dx;
308         }
309
310         if (*dy < 0) {
311                 *sy = -*dy;
312                 height = height + *dy;
313                 *dy = 0;
314         }
315
316         if (*dy + height > des->height) {
317                 height = des->height - *dy;
318         }
319
320         if (height > 0 && width > 0) {
321                 *swidth = width;
322                 *sheight = height;
323                 return True;
324         }
325
326         return False;
327 }
328
329 void RCombineArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
330 {
331         int x, y, dwi, swi;
332         unsigned char *d;
333         unsigned char *s;
334         int alpha, calpha;
335
336         if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
337                 return;
338
339         if (!HAS_ALPHA(src)) {
340                 if (!HAS_ALPHA(image)) {
341                         swi = src->width * 3;
342                         dwi = image->width * 3;
343
344                         s = src->data + (sy * (int)src->width + sx) * 3;
345                         d = image->data + (dy * (int)image->width + dx) * 3;
346
347                         for (y = 0; y < height; y++) {
348                                 memcpy(d, s, width * 3);
349                                 d += dwi;
350                                 s += swi;
351                         }
352                 } else {
353                         swi = (src->width - width) * 3;
354                         dwi = (image->width - width) * 4;
355
356                         s = src->data + (sy * (int)src->width + sx) * 3;
357                         d = image->data + (dy * (int)image->width + dx) * 4;
358
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++;
365                                 }
366                                 d += dwi;
367                                 s += swi;
368                         }
369                 }
370         } else {
371                 int dalpha = HAS_ALPHA(image);
372
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;
381                 }
382
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++;
399                         }
400                         d += dwi;
401                         s += swi;
402                 }
403         }
404 }
405
406 void RCopyArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
407 {
408         int x, y, dwi, swi;
409         unsigned char *d;
410         unsigned char *s;
411
412         if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
413                 return;
414
415         if (!HAS_ALPHA(src)) {
416                 if (!HAS_ALPHA(image)) {
417                         swi = src->width * 3;
418                         dwi = image->width * 3;
419
420                         s = src->data + (sy * (int)src->width + sx) * 3;
421                         d = image->data + (dy * (int)image->width + dx) * 3;
422
423                         for (y = 0; y < height; y++) {
424                                 memcpy(d, s, width * 3);
425                                 d += dwi;
426                                 s += swi;
427                         }
428                 } else {
429                         swi = (src->width - width) * 3;
430                         dwi = (image->width - width) * 4;
431
432                         s = src->data + (sy * (int)src->width + sx) * 3;
433                         d = image->data + (dy * (int)image->width + dx) * 4;
434
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++;
441                                 }
442                                 d += dwi;
443                                 s += swi;
444                         }
445                 }
446         } else {
447                 int dalpha = HAS_ALPHA(image);
448
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;
457                 }
458
459                 if (dalpha) {
460                         for (y = 0; y < height; y++) {
461                                 memcpy(d, s, width * 4);
462                                 d += dwi;
463                                 s += swi;
464                         }
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++;
472                                 }
473                                 d += dwi;
474                                 s += swi;
475                         }
476                 }
477         }
478 }
479
480 void
481 RCombineAreaWithOpaqueness(RImage * image, RImage * src, int sx, int sy,
482                            unsigned width, unsigned height, int dx, int dy, int opaqueness)
483 {
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);
489
490         if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
491                 return;
492
493         d = image->data + (dy * image->width + dx) * dch;
494         dwi = (image->width - width) * dch;
495
496         c_opaqueness = 255 - opaqueness;
497
498 #define OP opaqueness
499 #define COP c_opaqueness
500
501         if (!HAS_ALPHA(src)) {
502
503                 s = src->data + (sy * src->width + sx) * 3;
504                 swi = (src->width - width) * 3;
505
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++;
519                         }
520                         d += dwi;
521                         s += swi;
522                 }
523         } else {
524                 int tmp;
525
526                 s = src->data + (sy * src->width + sx) * 4;
527                 swi = (src->width - width) * 4;
528
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++;
544                         }
545                         d += dwi;
546                         s += swi;
547                 }
548         }
549 #undef OP
550 #undef COP
551 }
552
553 void RCombineImageWithColor(RImage * image, RColor * color)
554 {
555         register int i;
556         unsigned char *d;
557         int alpha, nalpha, r, g, b;
558
559         d = image->data;
560
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;
565         }
566         r = color->red;
567         g = color->green;
568         b = color->blue;
569
570         for (i = 0; i < image->width * image->height; i++) {
571                 alpha = *(d + 3);
572                 nalpha = 255 - alpha;
573
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++;
581         }
582 }
583
584 RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
585 {
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;
592
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);
599
600                 image = RCreateImage(width, height, has_alpha);
601
602                 d = image->data;
603                 s = tile->data;
604
605                 for (y = 0; y < height; y++) {
606                         for (x = 0; x < width; x += tile->width) {
607
608                                 w = (width - x < tile->width) ? width - x : tile->width;
609
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);
616                                 }
617                                 d += w;
618                         }
619
620                         tx = (tx + tile->width) % tile_size;
621                 }
622         }
623         return image;
624 }
625
626 RImage *RMakeCenteredImage(RImage * image, unsigned width, unsigned height, RColor * color)
627 {
628         int x, y, w, h, sx, sy;
629         RImage *tmp;
630
631         tmp = RCreateImage(width, height, False);
632         if (!tmp) {
633                 return NULL;
634         }
635
636         RClearImage(tmp, color);
637
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;
646         }
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;
655         }
656         RCombineArea(tmp, image, sx, sy, w, h, x, y);
657
658         return tmp;
659 }