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 RCombineAlpha(d
, s
, 1, image
->width
, image
->height
, 0, 0, 255);
208 void RCombineImagesWithOpaqueness(RImage
* image
, RImage
* src
, int opaqueness
)
215 assert(image
->width
== src
->width
);
216 assert(image
->height
== src
->height
);
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;
232 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
235 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
240 RCombineAlpha(d
, s
, 0, image
->width
, image
->height
, 0, 0, OP
);
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;
251 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
254 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
260 RCombineAlpha(d
, s
, 1, image
->width
, image
->height
, 0, 0, opaqueness
);
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
;
278 if (*dx
+ width
> des
->width
) {
279 width
= des
->width
- *dx
;
284 height
= height
+ *dy
;
288 if (*dy
+ height
> des
->height
) {
289 height
= des
->height
- *dy
;
292 if (height
> 0 && width
> 0) {
301 void RCombineArea(RImage
* image
, RImage
* src
, int sx
, int sy
, unsigned width
, unsigned height
, int dx
, int dy
)
308 if (!calculateCombineArea(image
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
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);
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
++) {
343 int dalpha
= HAS_ALPHA(image
);
345 swi
= (src
->width
- width
) * 4;
346 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 4;
348 dwi
= (image
->width
- width
) * 4;
349 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 4;
351 dwi
= (image
->width
- width
) * 3;
352 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 3;
356 for (y
= 0; y
< height
; y
++) {
357 for (x
= 0; x
< width
; x
++) {
359 calpha
= 255 - alpha
;
360 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
363 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
366 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
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
)
386 if (!calculateCombineArea(image
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
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);
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
++) {
421 int dalpha
= HAS_ALPHA(image
);
423 swi
= src
->width
* 4;
424 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 4;
426 dwi
= image
->width
* 4;
427 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 4;
429 dwi
= image
->width
* 3;
430 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 3;
434 for (y
= 0; y
< height
; y
++) {
435 memcpy(d
, s
, width
* 4);
440 for (y
= 0; y
< height
; y
++) {
441 for (x
= 0; x
< width
; x
++) {
455 RCombineAreaWithOpaqueness(RImage
* image
, RImage
* src
, int sx
, int sy
,
456 unsigned width
, unsigned height
, int dx
, int dy
, int 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
))
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;
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;
486 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
489 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
497 RCombineAlpha(d
, s
, 0, width
, height
, dwi
, swi
, OP
);
502 s
= src
->data
+ (sy
* src
->width
+ sx
) * 4;
503 swi
= (src
->width
- width
) * 4;
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;
512 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
515 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
524 RCombineAlpha(d
, s
, 1, width
, height
, dwi
, swi
, OP
);
531 void RCombineImageWithColor(RImage
* image
, RColor
* color
)
535 int alpha
, nalpha
, r
, g
, b
;
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. */
548 for (i
= 0; i
< image
->width
* image
->height
; i
++) {
550 nalpha
= 255 - alpha
;
552 *d
= (((int)*d
* alpha
) + (r
* nalpha
)) / 256;
554 *d
= (((int)*d
* alpha
) + (g
* nalpha
)) / 256;
556 *d
= (((int)*d
* alpha
) + (b
* nalpha
)) / 256;
562 RImage
*RMakeTiledImage(RImage
* tile
, unsigned width
, unsigned height
)
566 unsigned long tile_size
= tile
->width
* tile
->height
;
567 unsigned long tx
= 0;
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
);
576 int has_alpha
= HAS_ALPHA(tile
);
578 image
= RCreateImage(width
, height
, has_alpha
);
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
;
590 memcpy(d
, s
+ tx
* 4, w
);
593 memcpy(d
, s
+ tx
* 3, w
);
598 tx
= (tx
+ tile
->width
) % tile_size
;
604 RImage
*RMakeCenteredImage(RImage
* image
, unsigned width
, unsigned height
, RColor
* color
)
606 int x
, y
, w
, h
, sx
, sy
;
609 tmp
= RCreateImage(width
, height
, HAS_ALPHA(image
));
614 RFillImage(tmp
, color
);
616 if (image
->height
< height
) {
618 y
= (height
- h
) / 2;
621 sy
= (image
->height
- height
) / 2;
625 if (image
->width
< width
) {
630 sx
= (image
->width
- width
) / 2;
634 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);