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,
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
45 RImage
*RCreateImage(unsigned width
, unsigned height
, int alpha
)
49 assert(width
> 0 && height
> 0);
51 if (width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
52 RErrorCode
= RERR_NOMEMORY
;
56 image
= malloc(sizeof(RImage
));
58 RErrorCode
= RERR_NOMEMORY
;
62 memset(image
, 0, sizeof(RImage
));
64 image
->height
= height
;
65 image
->format
= alpha
? RRGBAFormat
: RRGBFormat
;
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);
73 RErrorCode
= RERR_NOMEMORY
;
82 RImage
*RRetainImage(RImage
* image
)
90 void RReleaseImage(RImage
* image
)
92 assert(image
!= NULL
);
96 if (image
->refCount
< 1) {
102 RImage
*RCloneImage(RImage
* image
)
106 assert(image
!= NULL
);
108 new_image
= RCreateImage(image
->width
, image
->height
, HAS_ALPHA(image
));
112 new_image
->background
= image
->background
;
113 memcpy(new_image
->data
, image
->data
, image
->width
* image
->height
* (HAS_ALPHA(image
) ? 4 : 3));
118 RImage
*RGetSubImage(RImage
* image
, int x
, int y
, unsigned width
, unsigned height
)
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
));
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
);
152 *----------------------------------------------------------------------
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);
168 unsigned char *d
, *s
;
172 for (y
= 0; y
< image
->height
; y
++) {
173 for (x
= 0; x
< image
->width
; x
++) {
190 if (!HAS_ALPHA(image
)) {
191 for (i
= 0; i
< image
->height
* image
->width
; i
++) {
193 calpha
= 255 - alpha
;
194 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
197 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
200 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
206 RCombineAlpha(d
, s
, 1, image
->width
, image
->height
, 0, 0, 255);
211 void RCombineImagesWithOpaqueness(RImage
* image
, RImage
* src
, int opaqueness
)
218 assert(image
->width
== src
->width
);
219 assert(image
->height
== src
->height
);
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;
235 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
238 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
243 RCombineAlpha(d
, s
, 0, image
->width
, image
->height
, 0, 0, OP
);
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;
254 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
257 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
263 RCombineAlpha(d
, s
, 1, image
->width
, image
->height
, 0, 0, opaqueness
);
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
;
281 if (*dx
+ width
> des
->width
) {
282 width
= des
->width
- *dx
;
287 height
= height
+ *dy
;
291 if (*dy
+ height
> des
->height
) {
292 height
= des
->height
- *dy
;
295 if (height
> 0 && width
> 0) {
304 void RCombineArea(RImage
* image
, RImage
* src
, int sx
, int sy
, unsigned width
, unsigned height
, int dx
, int dy
)
311 if (!calculateCombineArea(image
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
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);
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
++) {
346 int dalpha
= HAS_ALPHA(image
);
348 swi
= (src
->width
- width
) * 4;
349 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 4;
351 dwi
= (image
->width
- width
) * 4;
352 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 4;
354 dwi
= (image
->width
- width
) * 3;
355 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 3;
359 for (y
= 0; y
< height
; y
++) {
360 for (x
= 0; x
< width
; x
++) {
362 calpha
= 255 - alpha
;
363 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
366 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
369 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
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
)
389 if (!calculateCombineArea(image
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
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);
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
++) {
424 int dalpha
= HAS_ALPHA(image
);
426 swi
= src
->width
* 4;
427 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 4;
429 dwi
= image
->width
* 4;
430 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 4;
432 dwi
= image
->width
* 3;
433 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 3;
437 for (y
= 0; y
< height
; y
++) {
438 memcpy(d
, s
, width
* 4);
443 for (y
= 0; y
< height
; y
++) {
444 for (x
= 0; x
< width
; x
++) {
458 RCombineAreaWithOpaqueness(RImage
* image
, RImage
* src
, int sx
, int sy
,
459 unsigned width
, unsigned height
, int dx
, int dy
, int 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
))
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;
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;
489 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
492 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
500 RCombineAlpha(d
, s
, 0, width
, height
, dwi
, swi
, OP
);
505 s
= src
->data
+ (sy
* src
->width
+ sx
) * 4;
506 swi
= (src
->width
- width
) * 4;
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;
515 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
518 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
527 RCombineAlpha(d
, s
, 1, width
, height
, dwi
, swi
, OP
);
534 void RCombineImageWithColor(RImage
* image
, const RColor
* color
)
538 int alpha
, nalpha
, r
, g
, b
;
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. */
551 for (i
= 0; i
< image
->width
* image
->height
; i
++) {
553 nalpha
= 255 - alpha
;
555 *d
= (((int)*d
* alpha
) + (r
* nalpha
)) / 256;
557 *d
= (((int)*d
* alpha
) + (g
* nalpha
)) / 256;
559 *d
= (((int)*d
* alpha
) + (b
* nalpha
)) / 256;
565 RImage
*RMakeTiledImage(RImage
* tile
, unsigned width
, unsigned height
)
569 unsigned long tile_size
= tile
->width
* tile
->height
;
570 unsigned long tx
= 0;
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
);
579 int has_alpha
= HAS_ALPHA(tile
);
581 image
= RCreateImage(width
, height
, has_alpha
);
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
;
593 memcpy(d
, s
+ tx
* 4, w
);
596 memcpy(d
, s
+ tx
* 3, w
);
601 tx
= (tx
+ tile
->width
) % tile_size
;
607 RImage
*RMakeCenteredImage(RImage
* image
, unsigned width
, unsigned height
, const RColor
* color
)
609 int x
, y
, w
, h
, sx
, sy
;
612 tmp
= RCreateImage(width
, height
, HAS_ALPHA(image
));
617 RFillImage(tmp
, color
);
619 if (image
->height
< height
) {
621 y
= (height
- h
) / 2;
624 sy
= (image
->height
- height
) / 2;
628 if (image
->width
< width
) {
633 sx
= (image
->width
- width
) / 2;
637 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);