Update Serbian translation from master branch
[wmaker-crm.git] / wrlib / raster.c
blob0f8fdd113a52ad02cd9052850751b13e1e687ec8
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>
30 #include "wraster.h"
31 #include "wr_i18n.h"
33 #include <assert.h>
35 char *WRasterLibVersion = "0.9";
37 int RErrorCode = RERR_NONE;
39 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
41 #define MAX_WIDTH 20000
42 #define MAX_HEIGHT 20000
43 /* 20000^2*4 < 2G */
45 RImage *RCreateImage(unsigned width, unsigned height, int alpha)
47 RImage *image = NULL;
49 assert(width > 0 && height > 0);
51 if (width > MAX_WIDTH || height > MAX_HEIGHT) {
52 RErrorCode = RERR_NOMEMORY;
53 return NULL;
56 image = malloc(sizeof(RImage));
57 if (!image) {
58 RErrorCode = RERR_NOMEMORY;
59 return NULL;
62 memset(image, 0, sizeof(RImage));
63 image->width = width;
64 image->height = height;
65 image->format = alpha ? RRGBAFormat : RRGBFormat;
66 image->refCount = 1;
68 /* the +4 is to give extra bytes at the end of the buffer,
69 * so that we can optimize image conversion for MMX(tm).. see convert.c
71 image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
72 if (!image->data) {
73 RErrorCode = RERR_NOMEMORY;
74 free(image);
75 image = NULL;
78 return image;
82 RImage *RRetainImage(RImage * image)
84 if (image)
85 image->refCount++;
87 return image;
90 void RReleaseImage(RImage * image)
92 assert(image != NULL);
94 image->refCount--;
96 if (image->refCount < 1) {
97 free(image->data);
98 free(image);
102 RImage *RCloneImage(RImage * image)
104 RImage *new_image;
106 assert(image != NULL);
108 new_image = RCreateImage(image->width, image->height, HAS_ALPHA(image));
109 if (!new_image)
110 return NULL;
112 new_image->background = image->background;
113 memcpy(new_image->data, image->data, image->width * image->height * (HAS_ALPHA(image) ? 4 : 3));
115 return new_image;
118 RImage *RGetSubImage(RImage * image, int x, int y, unsigned width, unsigned height)
120 int i, ofs;
121 RImage *new_image;
122 unsigned total_line_size, line_size;
124 assert(image != NULL);
125 assert(x >= 0 && y >= 0);
126 assert(x < image->width && y < image->height);
127 assert(width > 0 && height > 0);
129 if (x + width > image->width)
130 width = image->width - x;
131 if (y + height > image->height)
132 height = image->height - y;
134 new_image = RCreateImage(width, height, HAS_ALPHA(image));
136 if (!new_image)
137 return NULL;
138 new_image->background = image->background;
140 total_line_size = image->width * (HAS_ALPHA(image) ? 4 : 3);
141 line_size = width * (HAS_ALPHA(image) ? 4 : 3);
143 ofs = x * (HAS_ALPHA(image) ? 4 : 3) + y * total_line_size;;
145 for (i = 0; i < height; i++) {
146 memcpy(&new_image->data[i * line_size], &image->data[i * total_line_size + ofs], line_size);
148 return new_image;
152 *----------------------------------------------------------------------
153 * RCombineImages-
154 * Combines two equal sized images with alpha image. The second
155 * image will be placed on top of the first one.
156 *----------------------------------------------------------------------
158 void RCombineImages(RImage * image, RImage * src)
160 assert(image->width == src->width);
161 assert(image->height == src->height);
163 if (!HAS_ALPHA(src)) {
164 if (!HAS_ALPHA(image)) {
165 memcpy(image->data, src->data, image->height * image->width * 3);
166 } else {
167 int x, y;
168 unsigned char *d, *s;
170 d = image->data;
171 s = src->data;
172 for (y = 0; y < image->height; y++) {
173 for (x = 0; x < image->width; x++) {
174 *d++ = *s++;
175 *d++ = *s++;
176 *d++ = *s++;
177 *d++ = 255;
181 } else {
182 register int i;
183 unsigned char *d;
184 unsigned char *s;
185 int alpha, calpha;
187 d = image->data;
188 s = src->data;
190 if (!HAS_ALPHA(image)) {
191 for (i = 0; i < image->height * image->width; i++) {
192 alpha = *(s + 3);
193 calpha = 255 - alpha;
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 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
201 d++;
202 s++;
203 s++;
205 } else {
206 RCombineAlpha(d, s, 1, image->width, image->height, 0, 0, 255);
211 void RCombineImagesWithOpaqueness(RImage * image, RImage * src, int opaqueness)
213 register int i;
214 unsigned char *d;
215 unsigned char *s;
216 int c_opaqueness;
218 assert(image->width == src->width);
219 assert(image->height == src->height);
221 d = image->data;
222 s = src->data;
224 c_opaqueness = 255 - opaqueness;
226 #define OP opaqueness
227 #define COP c_opaqueness
229 if (!HAS_ALPHA(src)) {
230 if (!HAS_ALPHA(image)) {
231 for (i = 0; i < image->width * image->height; i++) {
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++;
238 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
239 d++;
240 s++;
242 } else {
243 RCombineAlpha(d, s, 0, image->width, image->height, 0, 0, OP);
245 } else {
246 int tmp;
248 if (!HAS_ALPHA(image)) {
249 for (i = 0; i < image->width * image->height; i++) {
250 tmp = (*(s + 3) * opaqueness) / 256;
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 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
258 d++;
259 s++;
260 s++;
262 } else {
263 RCombineAlpha(d, s, 1, image->width, image->height, 0, 0, opaqueness);
266 #undef OP
267 #undef COP
270 static int calculateCombineArea(RImage *des, int *sx, int *sy, unsigned int *swidth,
271 unsigned int *sheight, int *dx, int *dy)
273 int width = (int)*swidth, height = (int)*sheight;
275 if (*dx < 0) {
276 *sx = -*dx;
277 width = width + *dx;
278 *dx = 0;
281 if (*dx + width > des->width) {
282 width = des->width - *dx;
285 if (*dy < 0) {
286 *sy = -*dy;
287 height = height + *dy;
288 *dy = 0;
291 if (*dy + height > des->height) {
292 height = des->height - *dy;
295 if (height > 0 && width > 0) {
296 *swidth = width;
297 *sheight = height;
298 return True;
301 return False;
304 void RCombineArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
306 int x, y, dwi, swi;
307 unsigned char *d;
308 unsigned char *s;
309 int alpha, calpha;
311 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
312 return;
314 if (!HAS_ALPHA(src)) {
315 if (!HAS_ALPHA(image)) {
316 swi = src->width * 3;
317 dwi = image->width * 3;
319 s = src->data + (sy * (int)src->width + sx) * 3;
320 d = image->data + (dy * (int)image->width + dx) * 3;
322 for (y = 0; y < height; y++) {
323 memcpy(d, s, width * 3);
324 d += dwi;
325 s += swi;
327 } else {
328 swi = (src->width - width) * 3;
329 dwi = (image->width - width) * 4;
331 s = src->data + (sy * (int)src->width + sx) * 3;
332 d = image->data + (dy * (int)image->width + dx) * 4;
334 for (y = 0; y < height; y++) {
335 for (x = 0; x < width; x++) {
336 *d++ = *s++;
337 *d++ = *s++;
338 *d++ = *s++;
339 *d++ = 255;
341 d += dwi;
342 s += swi;
345 } else {
346 int dalpha = HAS_ALPHA(image);
348 swi = (src->width - width) * 4;
349 s = src->data + (sy * (int)src->width + sx) * 4;
350 if (dalpha) {
351 dwi = (image->width - width) * 4;
352 d = image->data + (dy * (int)image->width + dx) * 4;
353 } else {
354 dwi = (image->width - width) * 3;
355 d = image->data + (dy * (int)image->width + dx) * 3;
358 if (!dalpha) {
359 for (y = 0; y < height; y++) {
360 for (x = 0; x < width; x++) {
361 alpha = *(s + 3);
362 calpha = 255 - alpha;
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 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
370 s++;
371 d++;
372 s++;
374 d += dwi;
375 s += swi;
377 } else {
378 RCombineAlpha(d, s, 1, width, height, dwi, swi, 255);
383 void RCopyArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
385 int x, y, dwi, swi;
386 unsigned char *d;
387 unsigned char *s;
389 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
390 return;
392 if (!HAS_ALPHA(src)) {
393 if (!HAS_ALPHA(image)) {
394 swi = src->width * 3;
395 dwi = image->width * 3;
397 s = src->data + (sy * (int)src->width + sx) * 3;
398 d = image->data + (dy * (int)image->width + dx) * 3;
400 for (y = 0; y < height; y++) {
401 memcpy(d, s, width * 3);
402 d += dwi;
403 s += swi;
405 } else {
406 swi = (src->width - width) * 3;
407 dwi = (image->width - width) * 4;
409 s = src->data + (sy * (int)src->width + sx) * 3;
410 d = image->data + (dy * (int)image->width + dx) * 4;
412 for (y = 0; y < height; y++) {
413 for (x = 0; x < width; x++) {
414 *d++ = *s++;
415 *d++ = *s++;
416 *d++ = *s++;
417 d++;
419 d += dwi;
420 s += swi;
423 } else {
424 int dalpha = HAS_ALPHA(image);
426 swi = src->width * 4;
427 s = src->data + (sy * (int)src->width + sx) * 4;
428 if (dalpha) {
429 dwi = image->width * 4;
430 d = image->data + (dy * (int)image->width + dx) * 4;
431 } else {
432 dwi = image->width * 3;
433 d = image->data + (dy * (int)image->width + dx) * 3;
436 if (dalpha) {
437 for (y = 0; y < height; y++) {
438 memcpy(d, s, width * 4);
439 d += dwi;
440 s += swi;
442 } else {
443 for (y = 0; y < height; y++) {
444 for (x = 0; x < width; x++) {
445 *d++ = *s++;
446 *d++ = *s++;
447 *d++ = *s++;
448 s++;
450 d += dwi;
451 s += swi;
457 void
458 RCombineAreaWithOpaqueness(RImage * image, RImage * src, int sx, int sy,
459 unsigned width, unsigned height, int dx, int dy, int opaqueness)
461 int x, y, dwi, swi;
462 int c_opaqueness;
463 unsigned char *s, *d;
464 int dalpha = HAS_ALPHA(image);
465 int dch = (dalpha ? 4 : 3);
467 if (!calculateCombineArea(image, &sx, &sy, &width, &height, &dx, &dy))
468 return;
470 d = image->data + (dy * image->width + dx) * dch;
471 dwi = (image->width - width) * dch;
473 c_opaqueness = 255 - opaqueness;
475 #define OP opaqueness
476 #define COP c_opaqueness
478 if (!HAS_ALPHA(src)) {
480 s = src->data + (sy * src->width + sx) * 3;
481 swi = (src->width - width) * 3;
483 if (!dalpha) {
484 for (y = 0; y < height; y++) {
485 for (x = 0; x < width; x++) {
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++;
492 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
493 s++;
494 d++;
496 d += dwi;
497 s += swi;
499 } else {
500 RCombineAlpha(d, s, 0, width, height, dwi, swi, OP);
502 } else {
503 int tmp;
505 s = src->data + (sy * src->width + sx) * 4;
506 swi = (src->width - width) * 4;
508 if (!dalpha) {
509 for (y = 0; y < height; y++) {
510 for (x = 0; x < width; x++) {
511 tmp = (*(s + 3) * opaqueness) / 256;
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 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
519 d++;
520 s++;
521 s++;
523 d += dwi;
524 s += swi;
526 } else {
527 RCombineAlpha(d, s, 1, width, height, dwi, swi, OP);
530 #undef OP
531 #undef COP
534 void RCombineImageWithColor(RImage * image, const RColor * color)
536 register int i;
537 unsigned char *d;
538 int alpha, nalpha, r, g, b;
540 d = image->data;
542 if (!HAS_ALPHA(image)) {
543 /* Image has no alpha channel, so we consider it to be all 255.
544 * Thus there are no transparent parts to be filled. */
545 return;
547 r = color->red;
548 g = color->green;
549 b = color->blue;
551 for (i = 0; i < image->width * image->height; i++) {
552 alpha = *(d + 3);
553 nalpha = 255 - alpha;
555 *d = (((int)*d * alpha) + (r * nalpha)) / 256;
556 d++;
557 *d = (((int)*d * alpha) + (g * nalpha)) / 256;
558 d++;
559 *d = (((int)*d * alpha) + (b * nalpha)) / 256;
560 d++;
561 d++;
565 RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
567 int x, y;
568 unsigned w;
569 unsigned long tile_size = tile->width * tile->height;
570 unsigned long tx = 0;
571 RImage *image;
572 unsigned char *s, *d;
574 if (width == tile->width && height == tile->height)
575 image = RCloneImage(tile);
576 else if (width <= tile->width && height <= tile->height)
577 image = RGetSubImage(tile, 0, 0, width, height);
578 else {
579 int has_alpha = HAS_ALPHA(tile);
581 image = RCreateImage(width, height, has_alpha);
583 d = image->data;
584 s = tile->data;
586 for (y = 0; y < height; y++) {
587 for (x = 0; x < width; x += tile->width) {
589 w = (width - x < tile->width) ? width - x : tile->width;
591 if (has_alpha) {
592 w *= 4;
593 memcpy(d, s + tx * 4, w);
594 } else {
595 w *= 3;
596 memcpy(d, s + tx * 3, w);
598 d += w;
601 tx = (tx + tile->width) % tile_size;
604 return image;
607 RImage *RMakeCenteredImage(RImage * image, unsigned width, unsigned height, const RColor * color)
609 int x, y, w, h, sx, sy;
610 RImage *tmp;
612 tmp = RCreateImage(width, height, HAS_ALPHA(image));
613 if (!tmp) {
614 return NULL;
617 RFillImage(tmp, color);
619 if (image->height < height) {
620 h = image->height;
621 y = (height - h) / 2;
622 sy = 0;
623 } else {
624 sy = (image->height - height) / 2;
625 y = 0;
626 h = height;
628 if (image->width < width) {
629 w = image->width;
630 x = (width - w) / 2;
631 sx = 0;
632 } else {
633 sx = (image->width - width) / 2;
634 x = 0;
635 w = width;
637 RCombineArea(tmp, image, sx, sy, w, h, x, y);
639 return tmp;