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,
33 char *WRasterLibVersion
= "0.9";
35 int RErrorCode
= RERR_NONE
;
37 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
39 #define MAX_WIDTH 20000
40 #define MAX_HEIGHT 20000
43 RImage
*RCreateImage(unsigned width
, unsigned height
, int alpha
)
47 assert(width
> 0 && height
> 0);
49 if (width
> MAX_WIDTH
|| height
> MAX_HEIGHT
) {
50 RErrorCode
= RERR_NOMEMORY
;
54 image
= malloc(sizeof(RImage
));
56 RErrorCode
= RERR_NOMEMORY
;
60 memset(image
, 0, sizeof(RImage
));
62 image
->height
= height
;
63 image
->format
= alpha
? RRGBAFormat
: RRGBFormat
;
66 /* the +4 is to give extra bytes at the end of the buffer,
67 * so that we can optimize image conversion for MMX(tm).. see convert.c
69 image
->data
= malloc(width
* height
* (alpha
? 4 : 3) + 4);
71 RErrorCode
= RERR_NOMEMORY
;
80 RImage
*RRetainImage(RImage
* image
)
88 void RReleaseImage(RImage
* image
)
90 assert(image
!= NULL
);
94 if (image
->refCount
< 1) {
100 RImage
*RCloneImage(RImage
* image
)
104 assert(image
!= NULL
);
106 new_image
= RCreateImage(image
->width
, image
->height
, HAS_ALPHA(image
));
110 new_image
->background
= image
->background
;
111 memcpy(new_image
->data
, image
->data
, image
->width
* image
->height
* (HAS_ALPHA(image
) ? 4 : 3));
116 RImage
*RGetSubImage(RImage
* image
, int x
, int y
, unsigned width
, unsigned height
)
120 unsigned total_line_size
, line_size
;
122 assert(image
!= NULL
);
123 assert(x
>= 0 && y
>= 0);
124 assert(x
< image
->width
&& y
< image
->height
);
125 assert(width
> 0 && height
> 0);
127 if (x
+ width
> image
->width
)
128 width
= image
->width
- x
;
129 if (y
+ height
> image
->height
)
130 height
= image
->height
- y
;
132 new_image
= RCreateImage(width
, height
, HAS_ALPHA(image
));
136 new_image
->background
= image
->background
;
138 total_line_size
= image
->width
* (HAS_ALPHA(image
) ? 4 : 3);
139 line_size
= width
* (HAS_ALPHA(image
) ? 4 : 3);
141 ofs
= x
* (HAS_ALPHA(image
) ? 4 : 3) + y
* total_line_size
;;
143 for (i
= 0; i
< height
; i
++) {
144 memcpy(&new_image
->data
[i
* line_size
], &image
->data
[i
* total_line_size
+ ofs
], line_size
);
150 *----------------------------------------------------------------------
152 * Combines two equal sized images with alpha image. The second
153 * image will be placed on top of the first one.
154 *----------------------------------------------------------------------
156 void RCombineImages(RImage
* image
, RImage
* src
)
158 assert(image
->width
== src
->width
);
159 assert(image
->height
== src
->height
);
161 if (!HAS_ALPHA(src
)) {
162 if (!HAS_ALPHA(image
)) {
163 memcpy(image
->data
, src
->data
, image
->height
* image
->width
* 3);
166 unsigned char *d
, *s
;
170 for (y
= 0; y
< image
->height
; y
++) {
171 for (x
= 0; x
< image
->width
; x
++) {
188 if (!HAS_ALPHA(image
)) {
189 for (i
= 0; i
< image
->height
* image
->width
; i
++) {
191 calpha
= 255 - alpha
;
192 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
195 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
198 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
204 RCombineAlpha(d
, s
, 1, image
->width
, image
->height
, 0, 0, 255);
209 void RCombineImagesWithOpaqueness(RImage
* image
, RImage
* src
, int opaqueness
)
216 assert(image
->width
== src
->width
);
217 assert(image
->height
== src
->height
);
222 c_opaqueness
= 255 - opaqueness
;
224 #define OP opaqueness
225 #define COP c_opaqueness
227 if (!HAS_ALPHA(src
)) {
228 if (!HAS_ALPHA(image
)) {
229 for (i
= 0; i
< image
->width
* image
->height
; i
++) {
230 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
233 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
236 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
241 RCombineAlpha(d
, s
, 0, image
->width
, image
->height
, 0, 0, OP
);
246 if (!HAS_ALPHA(image
)) {
247 for (i
= 0; i
< image
->width
* image
->height
; i
++) {
248 tmp
= (*(s
+ 3) * opaqueness
) / 256;
249 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
252 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
255 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
261 RCombineAlpha(d
, s
, 1, image
->width
, image
->height
, 0, 0, opaqueness
);
268 static int calculateCombineArea(RImage
*des
, int *sx
, int *sy
, unsigned int *swidth
,
269 unsigned int *sheight
, int *dx
, int *dy
)
271 int width
= (int)*swidth
, height
= (int)*sheight
;
279 if (*dx
+ width
> des
->width
) {
280 width
= des
->width
- *dx
;
285 height
= height
+ *dy
;
289 if (*dy
+ height
> des
->height
) {
290 height
= des
->height
- *dy
;
293 if (height
> 0 && width
> 0) {
302 void RCombineArea(RImage
* image
, RImage
* src
, int sx
, int sy
, unsigned width
, unsigned height
, int dx
, int dy
)
309 if (!calculateCombineArea(image
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
312 if (!HAS_ALPHA(src
)) {
313 if (!HAS_ALPHA(image
)) {
314 swi
= src
->width
* 3;
315 dwi
= image
->width
* 3;
317 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 3;
318 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 3;
320 for (y
= 0; y
< height
; y
++) {
321 memcpy(d
, s
, width
* 3);
326 swi
= (src
->width
- width
) * 3;
327 dwi
= (image
->width
- width
) * 4;
329 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 3;
330 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 4;
332 for (y
= 0; y
< height
; y
++) {
333 for (x
= 0; x
< width
; x
++) {
344 int dalpha
= HAS_ALPHA(image
);
346 swi
= (src
->width
- width
) * 4;
347 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 4;
349 dwi
= (image
->width
- width
) * 4;
350 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 4;
352 dwi
= (image
->width
- width
) * 3;
353 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 3;
357 for (y
= 0; y
< height
; y
++) {
358 for (x
= 0; x
< width
; x
++) {
360 calpha
= 255 - alpha
;
361 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
364 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
367 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
)) / 256;
376 RCombineAlpha(d
, s
, 1, width
, height
, dwi
, swi
, 255);
381 void RCopyArea(RImage
* image
, RImage
* src
, int sx
, int sy
, unsigned width
, unsigned height
, int dx
, int dy
)
387 if (!calculateCombineArea(image
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
390 if (!HAS_ALPHA(src
)) {
391 if (!HAS_ALPHA(image
)) {
392 swi
= src
->width
* 3;
393 dwi
= image
->width
* 3;
395 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 3;
396 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 3;
398 for (y
= 0; y
< height
; y
++) {
399 memcpy(d
, s
, width
* 3);
404 swi
= (src
->width
- width
) * 3;
405 dwi
= (image
->width
- width
) * 4;
407 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 3;
408 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 4;
410 for (y
= 0; y
< height
; y
++) {
411 for (x
= 0; x
< width
; x
++) {
422 int dalpha
= HAS_ALPHA(image
);
424 swi
= src
->width
* 4;
425 s
= src
->data
+ (sy
* (int)src
->width
+ sx
) * 4;
427 dwi
= image
->width
* 4;
428 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 4;
430 dwi
= image
->width
* 3;
431 d
= image
->data
+ (dy
* (int)image
->width
+ dx
) * 3;
435 for (y
= 0; y
< height
; y
++) {
436 memcpy(d
, s
, width
* 4);
441 for (y
= 0; y
< height
; y
++) {
442 for (x
= 0; x
< width
; x
++) {
456 RCombineAreaWithOpaqueness(RImage
* image
, RImage
* src
, int sx
, int sy
,
457 unsigned width
, unsigned height
, int dx
, int dy
, int opaqueness
)
461 unsigned char *s
, *d
;
462 int dalpha
= HAS_ALPHA(image
);
463 int dch
= (dalpha
? 4 : 3);
465 if (!calculateCombineArea(image
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
468 d
= image
->data
+ (dy
* image
->width
+ dx
) * dch
;
469 dwi
= (image
->width
- width
) * dch
;
471 c_opaqueness
= 255 - opaqueness
;
473 #define OP opaqueness
474 #define COP c_opaqueness
476 if (!HAS_ALPHA(src
)) {
478 s
= src
->data
+ (sy
* src
->width
+ sx
) * 3;
479 swi
= (src
->width
- width
) * 3;
482 for (y
= 0; y
< height
; y
++) {
483 for (x
= 0; x
< width
; x
++) {
484 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
487 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
490 *d
= (((int)*d
* (int)COP
) + ((int)*s
* (int)OP
)) / 256;
498 RCombineAlpha(d
, s
, 0, width
, height
, dwi
, swi
, OP
);
503 s
= src
->data
+ (sy
* src
->width
+ sx
) * 4;
504 swi
= (src
->width
- width
) * 4;
507 for (y
= 0; y
< height
; y
++) {
508 for (x
= 0; x
< width
; x
++) {
509 tmp
= (*(s
+ 3) * opaqueness
) / 256;
510 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
513 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
516 *d
= (((int)*d
* (255 - tmp
)) + ((int)*s
* tmp
)) / 256;
525 RCombineAlpha(d
, s
, 1, width
, height
, dwi
, swi
, OP
);
532 void RCombineImageWithColor(RImage
* image
, const RColor
* color
)
536 int alpha
, nalpha
, r
, g
, b
;
540 if (!HAS_ALPHA(image
)) {
541 /* Image has no alpha channel, so we consider it to be all 255.
542 * Thus there are no transparent parts to be filled. */
549 for (i
= 0; i
< image
->width
* image
->height
; i
++) {
551 nalpha
= 255 - alpha
;
553 *d
= (((int)*d
* alpha
) + (r
* nalpha
)) / 256;
555 *d
= (((int)*d
* alpha
) + (g
* nalpha
)) / 256;
557 *d
= (((int)*d
* alpha
) + (b
* nalpha
)) / 256;
563 RImage
*RMakeTiledImage(RImage
* tile
, unsigned width
, unsigned height
)
567 unsigned long tile_size
= tile
->width
* tile
->height
;
568 unsigned long tx
= 0;
570 unsigned char *s
, *d
;
572 if (width
== tile
->width
&& height
== tile
->height
)
573 image
= RCloneImage(tile
);
574 else if (width
<= tile
->width
&& height
<= tile
->height
)
575 image
= RGetSubImage(tile
, 0, 0, width
, height
);
577 int has_alpha
= HAS_ALPHA(tile
);
579 image
= RCreateImage(width
, height
, has_alpha
);
584 for (y
= 0; y
< height
; y
++) {
585 for (x
= 0; x
< width
; x
+= tile
->width
) {
587 w
= (width
- x
< tile
->width
) ? width
- x
: tile
->width
;
591 memcpy(d
, s
+ tx
* 4, w
);
594 memcpy(d
, s
+ tx
* 3, w
);
599 tx
= (tx
+ tile
->width
) % tile_size
;
605 RImage
*RMakeCenteredImage(RImage
* image
, unsigned width
, unsigned height
, const RColor
* color
)
607 int x
, y
, w
, h
, sx
, sy
;
610 tmp
= RCreateImage(width
, height
, HAS_ALPHA(image
));
615 RFillImage(tmp
, color
);
617 if (image
->height
< height
) {
619 y
= (height
- h
) / 2;
622 sy
= (image
->height
- height
) / 2;
626 if (image
->width
< width
) {
631 sx
= (image
->width
- width
) / 2;
635 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);