1 /* raster.c - main and other misc stuff
3 * Raster graphics library
5 * Copyright (c) 1997-2002 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.
34 char *WRasterLibVersion
="0.9";
36 int RErrorCode
=RERR_NONE
;
39 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
43 RCreateImage(unsigned width
, unsigned height
, int alpha
)
47 assert(width
>0 && height
>0);
49 /* check for too large images (cap on INT_MAX just to be sure :P) */
50 if (width
> (INT_MAX
/4)/height
+4) {
51 RErrorCode
= RERR_NOMEMORY
;
55 image
= malloc(sizeof(RImage
));
57 RErrorCode
= RERR_NOMEMORY
;
61 memset(image
, 0, sizeof(RImage
));
63 image
->height
= height
;
64 image
->format
= alpha
? RRGBAFormat
: RRGBFormat
;
67 /* the +4 is to give extra bytes at the end of the buffer,
68 * so that we can optimize image conversion for MMX(tm).. see convert.c
70 image
->data
= malloc(width
* height
* (alpha
? 4 : 3) + 4);
72 RErrorCode
= RERR_NOMEMORY
;
82 RRetainImage(RImage
*image
)
92 RReleaseImage(RImage
*image
)
98 if (image
->refCount
< 1) {
106 RCloneImage(RImage
*image
)
112 new_image
= RCreateImage(image
->width
, image
->height
, HAS_ALPHA(image
));
116 new_image
->background
= image
->background
;
117 memcpy(new_image
->data
, image
->data
,
118 image
->width
*image
->height
*(HAS_ALPHA(image
) ? 4 : 3));
125 RGetSubImage(RImage
*image
, int x
, int y
, unsigned width
, unsigned height
)
129 unsigned total_line_size
, line_size
;
132 assert(x
>=0 && y
>=0);
133 assert(x
<image
->width
&& y
<image
->height
);
134 assert(width
>0 && height
>0);
136 if (x
+width
> image
->width
)
137 width
= image
->width
-x
;
138 if (y
+height
> image
->height
)
139 height
= image
->height
-y
;
141 new_image
= RCreateImage(width
, height
, HAS_ALPHA(image
));
145 new_image
->background
= image
->background
;
147 total_line_size
= image
->width
* (HAS_ALPHA(image
) ? 4 : 3);
148 line_size
= width
* (HAS_ALPHA(image
) ? 4 : 3);
150 ofs
= x
*(HAS_ALPHA(image
) ? 4 : 3) + y
*total_line_size
;;
152 for (i
=0; i
<height
; i
++) {
153 memcpy(&new_image
->data
[i
*line_size
],
154 &image
->data
[i
*total_line_size
+ofs
], line_size
);
161 *----------------------------------------------------------------------
163 * Combines two equal sized images with alpha image. The second
164 * image will be placed on top of the first one.
165 *----------------------------------------------------------------------
168 RCombineImages(RImage
*image
, RImage
*src
)
170 assert(image
->width
== src
->width
);
171 assert(image
->height
== src
->height
);
173 if (!HAS_ALPHA(src
)) {
174 if (!HAS_ALPHA(image
)) {
175 memcpy(image
->data
, src
->data
, image
->height
*image
->width
*3);
178 unsigned char *d
, *s
;
182 for (y
= 0; y
< image
->height
; y
++) {
183 for (x
= 0; x
< image
->width
; x
++) {
200 if (!HAS_ALPHA(image
)) {
201 for (i
=0; i
<image
->height
*image
->width
; i
++) {
203 calpha
= 255 - alpha
;
204 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
205 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
206 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
210 for (i
=0; i
<image
->height
*image
->width
; i
++) {
212 calpha
= 255 - alpha
;
213 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
214 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
215 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
226 RCombineImagesWithOpaqueness(RImage
*image
, RImage
*src
, int opaqueness
)
233 assert(image
->width
== src
->width
);
234 assert(image
->height
== src
->height
);
239 c_opaqueness
= 255 - opaqueness
;
241 #define OP opaqueness
242 #define COP c_opaqueness
244 if (!HAS_ALPHA(src
)) {
245 int dalpha
= HAS_ALPHA(image
);
246 for (i
=0; i
< image
->width
*image
->height
; i
++) {
247 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
248 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
249 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
257 if (!HAS_ALPHA(image
)) {
258 for (i
=0; i
<image
->width
*image
->height
; i
++) {
259 tmp
= (*(s
+3) * opaqueness
)/256;
260 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
261 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
262 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
266 for (i
=0; i
<image
->width
*image
->height
; i
++) {
267 tmp
= (*(s
+3) * opaqueness
)/256;
268 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
269 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
270 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
281 calculateCombineArea(RImage
*des
, RImage
*src
, int *sx
, int *sy
,
282 int *swidth
, int *sheight
, int *dx
, int *dy
)
286 *swidth
= *swidth
+ *dx
;
290 if (*dx
+ *swidth
> des
->width
) {
291 *swidth
= des
->width
- *dx
;
296 *sheight
= *sheight
+ *dy
;
300 if (*dy
+ *sheight
> des
->height
) {
301 *sheight
= des
->height
- *dy
;
304 if (*sheight
> 0 && *swidth
> 0) {
310 RCombineArea(RImage
*image
, RImage
*src
, int sx
, int sy
, unsigned width
,
311 unsigned height
, int dx
, int dy
)
318 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
321 if (!HAS_ALPHA(src
)) {
322 if (!HAS_ALPHA(image
)) {
323 swi
= src
->width
* 3;
324 dwi
= image
->width
* 3;
326 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
327 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
329 for (y
=0; y
< height
; y
++) {
330 memcpy(d
, s
, width
*3);
335 swi
= (src
->width
- width
) * 3;
336 dwi
= (image
->width
- width
) * 4;
338 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
339 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
341 for (y
=0; y
< height
; y
++) {
342 for (x
=0; x
< width
; x
++) {
353 int dalpha
= HAS_ALPHA(image
);
355 swi
= (src
->width
- width
) * 4;
356 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 4;
358 dwi
= (image
->width
- width
) * 4;
359 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
361 dwi
= (image
->width
- width
) * 3;
362 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
365 for (y
=0; y
< height
; y
++) {
366 for (x
=0; x
< width
; x
++) {
368 calpha
= 255 - alpha
;
369 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
370 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
371 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
384 RCombineAreaWithOpaqueness(RImage
*image
, RImage
*src
, int sx
, int sy
,
385 unsigned width
, unsigned height
, int dx
, int dy
,
390 unsigned char *s
, *d
;
391 int dalpha
= HAS_ALPHA(image
);
392 int dch
= (dalpha
? 4 : 3);
394 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
397 d
= image
->data
+ (dy
*image
->width
+ dx
) * dch
;
398 dwi
= (image
->width
- width
)*dch
;
400 c_opaqueness
= 255 - opaqueness
;
402 #define OP opaqueness
403 #define COP c_opaqueness
405 if (!HAS_ALPHA(src
)) {
407 s
= src
->data
+ (sy
*src
->width
+ sx
)*3;
408 swi
= (src
->width
- width
) * 3;
410 for (y
=0; y
< height
; y
++) {
411 for (x
=0; x
< width
; x
++) {
412 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
413 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
414 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
423 s
= src
->data
+ (sy
*src
->width
+ sx
)*4;
424 swi
= (src
->width
- width
) * 4;
426 for (y
=0; y
< height
; y
++) {
427 for (x
=0; x
< width
; x
++) {
428 tmp
= (*(s
+3) * opaqueness
)/256;
429 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
430 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
431 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
446 RCombineImageWithColor(RImage
*image
, RColor
*color
)
450 int alpha
, nalpha
, r
, g
, b
;
454 if (!HAS_ALPHA(image
)) {
455 /* Image has no alpha channel, so we consider it to be all 255.
456 * Thus there are no transparent parts to be filled. */
463 for (i
=0; i
< image
->width
*image
->height
; i
++) {
465 nalpha
= 255 - alpha
;
467 *d
= (((int)*d
* alpha
) + (r
* nalpha
))/256; d
++;
468 *d
= (((int)*d
* alpha
) + (g
* nalpha
))/256; d
++;
469 *d
= (((int)*d
* alpha
) + (b
* nalpha
))/256; d
++;
478 RMakeTiledImage(RImage
*tile
, unsigned width
, unsigned height
)
482 unsigned long tile_size
= tile
->width
* tile
->height
;
483 unsigned long tx
= 0;
485 unsigned char *s
, *d
;
487 if (width
== tile
->width
&& height
== tile
->height
)
488 image
= RCloneImage(tile
);
489 else if (width
<= tile
->width
&& height
<= tile
->height
)
490 image
= RGetSubImage(tile
, 0, 0, width
, height
);
492 int has_alpha
= HAS_ALPHA(tile
);
494 image
= RCreateImage(width
, height
, has_alpha
);
499 for (y
= 0; y
< height
; y
++) {
500 for (x
= 0; x
< width
; x
+= tile
->width
) {
502 w
= (width
- x
< tile
->width
) ? width
- x
: tile
->width
;
506 memcpy(d
, s
+tx
*4, w
);
509 memcpy(d
, s
+tx
*3, w
);
514 tx
= (tx
+ tile
->width
) % tile_size
;
522 RMakeCenteredImage(RImage
*image
, unsigned width
, unsigned height
, RColor
*color
)
524 int x
, y
, w
, h
, sx
, sy
;
527 tmp
= RCreateImage(width
, height
, False
);
532 RClearImage(tmp
, color
);
534 if (image
->height
< height
) {
539 sy
= (image
->height
- height
)/2;
543 if (image
->width
< width
) {
548 sx
= (image
->width
- width
)/2;
552 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);