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) */
52 if (bla1
/ height
!= width
) {
53 RErrorCode
= RERR_NOMEMORY
;
59 RErrorCode
= RERR_NOMEMORY
;
63 if (bla2
> INT_MAX
- 4) {
64 RErrorCode
= RERR_NOMEMORY
;
68 image
= malloc(sizeof(RImage
));
70 RErrorCode
= RERR_NOMEMORY
;
74 memset(image
, 0, sizeof(RImage
));
76 image
->height
= height
;
77 image
->format
= alpha
? RRGBAFormat
: RRGBFormat
;
80 /* the +4 is to give extra bytes at the end of the buffer,
81 * so that we can optimize image conversion for MMX(tm).. see convert.c
83 image
->data
= malloc(width
* height
* (alpha
? 4 : 3) + 4);
85 RErrorCode
= RERR_NOMEMORY
;
96 RRetainImage(RImage
*image
)
106 RReleaseImage(RImage
*image
)
112 if (image
->refCount
< 1) {
120 RCloneImage(RImage
*image
)
126 new_image
= RCreateImage(image
->width
, image
->height
, HAS_ALPHA(image
));
130 new_image
->background
= image
->background
;
131 memcpy(new_image
->data
, image
->data
,
132 image
->width
*image
->height
*(HAS_ALPHA(image
) ? 4 : 3));
139 RGetSubImage(RImage
*image
, int x
, int y
, unsigned width
, unsigned height
)
143 unsigned total_line_size
, line_size
;
146 assert(x
>=0 && y
>=0);
147 assert(x
<image
->width
&& y
<image
->height
);
148 assert(width
>0 && height
>0);
150 if (x
+width
> image
->width
)
151 width
= image
->width
-x
;
152 if (y
+height
> image
->height
)
153 height
= image
->height
-y
;
155 new_image
= RCreateImage(width
, height
, HAS_ALPHA(image
));
159 new_image
->background
= image
->background
;
161 total_line_size
= image
->width
* (HAS_ALPHA(image
) ? 4 : 3);
162 line_size
= width
* (HAS_ALPHA(image
) ? 4 : 3);
164 ofs
= x
*(HAS_ALPHA(image
) ? 4 : 3) + y
*total_line_size
;;
166 for (i
=0; i
<height
; i
++) {
167 memcpy(&new_image
->data
[i
*line_size
],
168 &image
->data
[i
*total_line_size
+ofs
], line_size
);
175 *----------------------------------------------------------------------
177 * Combines two equal sized images with alpha image. The second
178 * image will be placed on top of the first one.
179 *----------------------------------------------------------------------
182 RCombineImages(RImage
*image
, RImage
*src
)
184 assert(image
->width
== src
->width
);
185 assert(image
->height
== src
->height
);
187 if (!HAS_ALPHA(src
)) {
188 if (!HAS_ALPHA(image
)) {
189 memcpy(image
->data
, src
->data
, image
->height
*image
->width
*3);
192 unsigned char *d
, *s
;
196 for (y
= 0; y
< image
->height
; y
++) {
197 for (x
= 0; x
< image
->width
; x
++) {
214 if (!HAS_ALPHA(image
)) {
215 for (i
=0; i
<image
->height
*image
->width
; i
++) {
217 calpha
= 255 - alpha
;
218 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
219 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
220 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
224 for (i
=0; i
<image
->height
*image
->width
; i
++) {
226 calpha
= 255 - alpha
;
227 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
228 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
229 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
240 RCombineImagesWithOpaqueness(RImage
*image
, RImage
*src
, int opaqueness
)
247 assert(image
->width
== src
->width
);
248 assert(image
->height
== src
->height
);
253 c_opaqueness
= 255 - opaqueness
;
255 #define OP opaqueness
256 #define COP c_opaqueness
258 if (!HAS_ALPHA(src
)) {
259 int dalpha
= HAS_ALPHA(image
);
260 for (i
=0; i
< image
->width
*image
->height
; i
++) {
261 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
262 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
263 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
271 if (!HAS_ALPHA(image
)) {
272 for (i
=0; i
<image
->width
*image
->height
; i
++) {
273 tmp
= (*(s
+3) * opaqueness
)/256;
274 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
275 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
276 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
280 for (i
=0; i
<image
->width
*image
->height
; i
++) {
281 tmp
= (*(s
+3) * opaqueness
)/256;
282 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
283 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
284 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
295 calculateCombineArea(RImage
*des
, RImage
*src
, int *sx
, int *sy
,
296 int *swidth
, int *sheight
, int *dx
, int *dy
)
300 *swidth
= *swidth
+ *dx
;
304 if (*dx
+ *swidth
> des
->width
) {
305 *swidth
= des
->width
- *dx
;
310 *sheight
= *sheight
+ *dy
;
314 if (*dy
+ *sheight
> des
->height
) {
315 *sheight
= des
->height
- *dy
;
318 if (*sheight
> 0 && *swidth
> 0) {
324 RCombineArea(RImage
*image
, RImage
*src
, int sx
, int sy
, unsigned width
,
325 unsigned height
, int dx
, int dy
)
332 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
335 if (!HAS_ALPHA(src
)) {
336 if (!HAS_ALPHA(image
)) {
337 swi
= src
->width
* 3;
338 dwi
= image
->width
* 3;
340 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
341 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
343 for (y
=0; y
< height
; y
++) {
344 memcpy(d
, s
, width
*3);
349 swi
= (src
->width
- width
) * 3;
350 dwi
= (image
->width
- width
) * 4;
352 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
353 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
355 for (y
=0; y
< height
; y
++) {
356 for (x
=0; x
< width
; x
++) {
367 int dalpha
= HAS_ALPHA(image
);
369 swi
= (src
->width
- width
) * 4;
370 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 4;
372 dwi
= (image
->width
- width
) * 4;
373 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
375 dwi
= (image
->width
- width
) * 3;
376 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
379 for (y
=0; y
< height
; y
++) {
380 for (x
=0; x
< width
; x
++) {
382 calpha
= 255 - alpha
;
383 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
384 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
385 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
398 RCombineAreaWithOpaqueness(RImage
*image
, RImage
*src
, int sx
, int sy
,
399 unsigned width
, unsigned height
, int dx
, int dy
,
404 unsigned char *s
, *d
;
405 int dalpha
= HAS_ALPHA(image
);
406 int dch
= (dalpha
? 4 : 3);
408 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
411 d
= image
->data
+ (dy
*image
->width
+ dx
) * dch
;
412 dwi
= (image
->width
- width
)*dch
;
414 c_opaqueness
= 255 - opaqueness
;
416 #define OP opaqueness
417 #define COP c_opaqueness
419 if (!HAS_ALPHA(src
)) {
421 s
= src
->data
+ (sy
*src
->width
+ sx
)*3;
422 swi
= (src
->width
- width
) * 3;
424 for (y
=0; y
< height
; y
++) {
425 for (x
=0; x
< width
; x
++) {
426 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
427 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
428 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
437 s
= src
->data
+ (sy
*src
->width
+ sx
)*4;
438 swi
= (src
->width
- width
) * 4;
440 for (y
=0; y
< height
; y
++) {
441 for (x
=0; x
< width
; x
++) {
442 tmp
= (*(s
+3) * opaqueness
)/256;
443 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
444 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
445 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
460 RCombineImageWithColor(RImage
*image
, RColor
*color
)
464 int alpha
, nalpha
, r
, g
, b
;
468 if (!HAS_ALPHA(image
)) {
469 /* Image has no alpha channel, so we consider it to be all 255.
470 * Thus there are no transparent parts to be filled. */
477 for (i
=0; i
< image
->width
*image
->height
; i
++) {
479 nalpha
= 255 - alpha
;
481 *d
= (((int)*d
* alpha
) + (r
* nalpha
))/256; d
++;
482 *d
= (((int)*d
* alpha
) + (g
* nalpha
))/256; d
++;
483 *d
= (((int)*d
* alpha
) + (b
* nalpha
))/256; d
++;
492 RMakeTiledImage(RImage
*tile
, unsigned width
, unsigned height
)
496 unsigned long tile_size
= tile
->width
* tile
->height
;
497 unsigned long tx
= 0;
499 unsigned char *s
, *d
;
501 if (width
== tile
->width
&& height
== tile
->height
)
502 image
= RCloneImage(tile
);
503 else if (width
<= tile
->width
&& height
<= tile
->height
)
504 image
= RGetSubImage(tile
, 0, 0, width
, height
);
506 int has_alpha
= HAS_ALPHA(tile
);
508 image
= RCreateImage(width
, height
, has_alpha
);
513 for (y
= 0; y
< height
; y
++) {
514 for (x
= 0; x
< width
; x
+= tile
->width
) {
516 w
= (width
- x
< tile
->width
) ? width
- x
: tile
->width
;
520 memcpy(d
, s
+tx
*4, w
);
523 memcpy(d
, s
+tx
*3, w
);
528 tx
= (tx
+ tile
->width
) % tile_size
;
536 RMakeCenteredImage(RImage
*image
, unsigned width
, unsigned height
, RColor
*color
)
538 int x
, y
, w
, h
, sx
, sy
;
541 tmp
= RCreateImage(width
, height
, False
);
546 RClearImage(tmp
, color
);
548 if (image
->height
< height
) {
553 sy
= (image
->height
- height
)/2;
557 if (image
->width
< width
) {
562 sx
= (image
->width
- width
)/2;
566 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);