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.
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
42 RImage *RCreateImage(unsigned width, unsigned height, int alpha)
46 assert(width > 0 && height > 0);
48 if (width > MAX_WIDTH || height > MAX_HEIGHT) {
49 RErrorCode = RERR_NOMEMORY;
53 image = malloc(sizeof(RImage));
55 RErrorCode = RERR_NOMEMORY;
59 memset(image, 0, sizeof(RImage));
61 image->height = height;
62 image->format = alpha ? RRGBAFormat : RRGBFormat;
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);
70 RErrorCode = RERR_NOMEMORY;
79 RImage *RRetainImage(RImage * image)
87 void RReleaseImage(RImage * image)
89 assert(image != NULL);
93 if (image->refCount < 1) {
99 RImage *RCloneImage(RImage * image)
103 assert(image != NULL);
105 new_image = RCreateImage(image->width, image->height, HAS_ALPHA(image));
109 new_image->background = image->background;
110 memcpy(new_image->data, image->data, image->width * image->height * (HAS_ALPHA(image) ? 4 : 3));
115 RImage *RGetSubImage(RImage * image, int x, int y, unsigned width, unsigned height)
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));
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);
149 *----------------------------------------------------------------------
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);
165 unsigned char *d, *s;
169 for (y = 0; y < image->height; y++) {
170 for (x = 0; x < image->width; x++) {
187 if (!HAS_ALPHA(image)) {
188 for (i = 0; i < image->height * image->width; i++) {
190 calpha = 255 - alpha;
191 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
194 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
197 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
203 for (i = 0; i < image->height * image->width; i++) {
205 calpha = 255 - alpha;
206 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
209 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
212 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
221 void RCombineImagesWithOpaqueness(RImage * image, RImage * src, int opaqueness)
228 assert(image->width == src->width);
229 assert(image->height == src->height);
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;
245 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
248 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
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;
264 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
267 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
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;
278 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
281 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
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;
306 if (*dx + width > des->width) {
307 width = des->width - *dx;
312 height = height + *dy;
316 if (*dy + height > des->height) {
317 height = des->height - *dy;
320 if (height > 0 && width > 0) {
329 void RCombineArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
336 if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
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);
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++) {
371 int dalpha = HAS_ALPHA(image);
373 swi = (src->width - width) * 4;
374 s = src->data + (sy * (int)src->width + sx) * 4;
376 dwi = (image->width - width) * 4;
377 d = image->data + (dy * (int)image->width + dx) * 4;
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++) {
386 calpha = 255 - alpha;
387 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
390 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
393 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
406 void RCopyArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
412 if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
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);
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++) {
447 int dalpha = HAS_ALPHA(image);
449 swi = src->width * 4;
450 s = src->data + (sy * (int)src->width + sx) * 4;
452 dwi = image->width * 4;
453 d = image->data + (dy * (int)image->width + dx) * 4;
455 dwi = image->width * 3;
456 d = image->data + (dy * (int)image->width + dx) * 3;
460 for (y = 0; y < height; y++) {
461 memcpy(d, s, width * 4);
466 for (y = 0; y < height; y++) {
467 for (x = 0; x < width; x++) {
481 RCombineAreaWithOpaqueness(RImage * image, RImage * src, int sx, int sy,
482 unsigned width, unsigned height, int dx, int dy, int 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))
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;
511 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
514 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
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;
535 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
538 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
553 void RCombineImageWithColor(RImage * image, RColor * color)
557 int alpha, nalpha, r, g, b;
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. */
570 for (i = 0; i < image->width * image->height; i++) {
572 nalpha = 255 - alpha;
574 *d = (((int)*d * alpha) + (r * nalpha)) / 256;
576 *d = (((int)*d * alpha) + (g * nalpha)) / 256;
578 *d = (((int)*d * alpha) + (b * nalpha)) / 256;
584 RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
588 unsigned long tile_size = tile->width * tile->height;
589 unsigned long tx = 0;
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);
598 int has_alpha = HAS_ALPHA(tile);
600 image = RCreateImage(width, height, has_alpha);
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;
612 memcpy(d, s + tx * 4, w);
615 memcpy(d, s + tx * 3, w);
620 tx = (tx + tile->width) % tile_size;
626 RImage *RMakeCenteredImage(RImage * image, unsigned width, unsigned height, RColor * color)
628 int x, y, w, h, sx, sy;
631 tmp = RCreateImage(width, height, False);
636 RClearImage(tmp, color);
638 if (image->height < height) {
640 y = (height - h) / 2;
643 sy = (image->height - height) / 2;
647 if (image->width < width) {
652 sx = (image->width - width) / 2;
656 RCombineArea(tmp, image, sx, sy, w, h, x, y);