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
)
48 assert(width
>0 && height
>0);
50 /* detect overflow (gr33tz to ruda :D) */
53 if (bla1
/height
!= width
|| bla2
/4 != bla1
|| bla2
> INT_MAX
-32) {
54 RErrorCode
= RERR_NOMEMORY
;
58 image
= malloc(sizeof(RImage
));
60 RErrorCode
= RERR_NOMEMORY
;
64 memset(image
, 0, sizeof(RImage
));
66 image
->height
= height
;
67 image
->format
= alpha
? RRGBAFormat
: RRGBFormat
;
70 /* the +4 is to give extra bytes at the end of the buffer,
71 * so that we can optimize image conversion for MMX(tm).. see convert.c
73 image
->data
= malloc(width
* height
* (alpha
? 4 : 3) + 4);
75 RErrorCode
= RERR_NOMEMORY
;
85 RRetainImage(RImage
*image
)
95 RReleaseImage(RImage
*image
)
101 if (image
->refCount
< 1) {
109 RCloneImage(RImage
*image
)
115 new_image
= RCreateImage(image
->width
, image
->height
, HAS_ALPHA(image
));
119 new_image
->background
= image
->background
;
120 memcpy(new_image
->data
, image
->data
,
121 image
->width
*image
->height
*(HAS_ALPHA(image
) ? 4 : 3));
128 RGetSubImage(RImage
*image
, int x
, int y
, unsigned width
, unsigned height
)
132 unsigned total_line_size
, line_size
;
135 assert(x
>=0 && y
>=0);
136 assert(x
<image
->width
&& y
<image
->height
);
137 assert(width
>0 && height
>0);
139 if (x
+width
> image
->width
)
140 width
= image
->width
-x
;
141 if (y
+height
> image
->height
)
142 height
= image
->height
-y
;
144 new_image
= RCreateImage(width
, height
, HAS_ALPHA(image
));
148 new_image
->background
= image
->background
;
150 total_line_size
= image
->width
* (HAS_ALPHA(image
) ? 4 : 3);
151 line_size
= width
* (HAS_ALPHA(image
) ? 4 : 3);
153 ofs
= x
*(HAS_ALPHA(image
) ? 4 : 3) + y
*total_line_size
;;
155 for (i
=0; i
<height
; i
++) {
156 memcpy(&new_image
->data
[i
*line_size
],
157 &image
->data
[i
*total_line_size
+ofs
], line_size
);
164 *----------------------------------------------------------------------
166 * Combines two equal sized images with alpha image. The second
167 * image will be placed on top of the first one.
168 *----------------------------------------------------------------------
171 RCombineImages(RImage
*image
, RImage
*src
)
173 assert(image
->width
== src
->width
);
174 assert(image
->height
== src
->height
);
176 if (!HAS_ALPHA(src
)) {
177 if (!HAS_ALPHA(image
)) {
178 memcpy(image
->data
, src
->data
, image
->height
*image
->width
*3);
181 unsigned char *d
, *s
;
185 for (y
= 0; y
< image
->height
; y
++) {
186 for (x
= 0; x
< image
->width
; x
++) {
203 if (!HAS_ALPHA(image
)) {
204 for (i
=0; i
<image
->height
*image
->width
; i
++) {
206 calpha
= 255 - alpha
;
207 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
208 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
209 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
213 for (i
=0; i
<image
->height
*image
->width
; i
++) {
215 calpha
= 255 - alpha
;
216 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
217 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
218 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
229 RCombineImagesWithOpaqueness(RImage
*image
, RImage
*src
, int opaqueness
)
236 assert(image
->width
== src
->width
);
237 assert(image
->height
== src
->height
);
242 c_opaqueness
= 255 - opaqueness
;
244 #define OP opaqueness
245 #define COP c_opaqueness
247 if (!HAS_ALPHA(src
)) {
248 int dalpha
= HAS_ALPHA(image
);
249 for (i
=0; i
< image
->width
*image
->height
; i
++) {
250 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
251 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
252 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
260 if (!HAS_ALPHA(image
)) {
261 for (i
=0; i
<image
->width
*image
->height
; i
++) {
262 tmp
= (*(s
+3) * opaqueness
)/256;
263 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
264 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
265 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
269 for (i
=0; i
<image
->width
*image
->height
; i
++) {
270 tmp
= (*(s
+3) * opaqueness
)/256;
271 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
272 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
273 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
284 calculateCombineArea(RImage
*des
, RImage
*src
, int *sx
, int *sy
,
285 int *swidth
, int *sheight
, int *dx
, int *dy
)
289 *swidth
= *swidth
+ *dx
;
293 if (*dx
+ *swidth
> des
->width
) {
294 *swidth
= des
->width
- *dx
;
299 *sheight
= *sheight
+ *dy
;
303 if (*dy
+ *sheight
> des
->height
) {
304 *sheight
= des
->height
- *dy
;
307 if (*sheight
> 0 && *swidth
> 0) {
313 RCombineArea(RImage
*image
, RImage
*src
, int sx
, int sy
, unsigned width
,
314 unsigned height
, int dx
, int dy
)
321 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
324 if (!HAS_ALPHA(src
)) {
325 if (!HAS_ALPHA(image
)) {
326 swi
= src
->width
* 3;
327 dwi
= image
->width
* 3;
329 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
330 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
332 for (y
=0; y
< height
; y
++) {
333 memcpy(d
, s
, width
*3);
338 swi
= (src
->width
- width
) * 3;
339 dwi
= (image
->width
- width
) * 4;
341 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
342 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
344 for (y
=0; y
< height
; y
++) {
345 for (x
=0; x
< width
; x
++) {
356 int dalpha
= HAS_ALPHA(image
);
358 swi
= (src
->width
- width
) * 4;
359 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 4;
361 dwi
= (image
->width
- width
) * 4;
362 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
364 dwi
= (image
->width
- width
) * 3;
365 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
368 for (y
=0; y
< height
; y
++) {
369 for (x
=0; x
< width
; x
++) {
371 calpha
= 255 - alpha
;
372 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
373 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
374 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
387 RCombineAreaWithOpaqueness(RImage
*image
, RImage
*src
, int sx
, int sy
,
388 unsigned width
, unsigned height
, int dx
, int dy
,
393 unsigned char *s
, *d
;
394 int dalpha
= HAS_ALPHA(image
);
395 int dch
= (dalpha
? 4 : 3);
397 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
400 d
= image
->data
+ (dy
*image
->width
+ dx
) * dch
;
401 dwi
= (image
->width
- width
)*dch
;
403 c_opaqueness
= 255 - opaqueness
;
405 #define OP opaqueness
406 #define COP c_opaqueness
408 if (!HAS_ALPHA(src
)) {
410 s
= src
->data
+ (sy
*src
->width
+ sx
)*3;
411 swi
= (src
->width
- width
) * 3;
413 for (y
=0; y
< height
; y
++) {
414 for (x
=0; x
< width
; x
++) {
415 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
416 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
417 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
426 s
= src
->data
+ (sy
*src
->width
+ sx
)*4;
427 swi
= (src
->width
- width
) * 4;
429 for (y
=0; y
< height
; y
++) {
430 for (x
=0; x
< width
; x
++) {
431 tmp
= (*(s
+3) * opaqueness
)/256;
432 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
433 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
434 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
449 RCombineImageWithColor(RImage
*image
, RColor
*color
)
453 int alpha
, nalpha
, r
, g
, b
;
457 if (!HAS_ALPHA(image
)) {
458 /* Image has no alpha channel, so we consider it to be all 255.
459 * Thus there are no transparent parts to be filled. */
466 for (i
=0; i
< image
->width
*image
->height
; i
++) {
468 nalpha
= 255 - alpha
;
470 *d
= (((int)*d
* alpha
) + (r
* nalpha
))/256; d
++;
471 *d
= (((int)*d
* alpha
) + (g
* nalpha
))/256; d
++;
472 *d
= (((int)*d
* alpha
) + (b
* nalpha
))/256; d
++;
481 RMakeTiledImage(RImage
*tile
, unsigned width
, unsigned height
)
485 unsigned long tile_size
= tile
->width
* tile
->height
;
486 unsigned long tx
= 0;
488 unsigned char *s
, *d
;
490 if (width
== tile
->width
&& height
== tile
->height
)
491 image
= RCloneImage(tile
);
492 else if (width
<= tile
->width
&& height
<= tile
->height
)
493 image
= RGetSubImage(tile
, 0, 0, width
, height
);
495 int has_alpha
= HAS_ALPHA(tile
);
497 image
= RCreateImage(width
, height
, has_alpha
);
502 for (y
= 0; y
< height
; y
++) {
503 for (x
= 0; x
< width
; x
+= tile
->width
) {
505 w
= (width
- x
< tile
->width
) ? width
- x
: tile
->width
;
509 memcpy(d
, s
+tx
*4, w
);
512 memcpy(d
, s
+tx
*3, w
);
517 tx
= (tx
+ tile
->width
) % tile_size
;
525 RMakeCenteredImage(RImage
*image
, unsigned width
, unsigned height
, RColor
*color
)
527 int x
, y
, w
, h
, sx
, sy
;
530 tmp
= RCreateImage(width
, height
, False
);
535 RClearImage(tmp
, color
);
537 if (image
->height
< height
) {
542 sy
= (image
->height
- height
)/2;
546 if (image
->width
< width
) {
551 sx
= (image
->width
- width
)/2;
555 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);