1 /* raster.c - main and other misc stuff
3 * Raster graphics library
5 * Copyright (c) 1997-2000 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.
33 char *WRasterLibVersion
="0.9";
35 int RErrorCode
=RERR_NONE
;
38 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
42 RCreateImage(unsigned width
, unsigned height
, int alpha
)
46 assert(width
>0 && height
>0);
48 image
= malloc(sizeof(RImage
));
50 RErrorCode
= RERR_NOMEMORY
;
54 memset(image
, 0, sizeof(RImage
));
56 image
->height
= height
;
57 image
->format
= alpha
? RRGBAFormat
: RRGBFormat
;
59 /* the +4 is to give extra bytes at the end of the buffer,
60 * so that we can optimize image conversion for MMX(tm).. see convert.c
62 image
->data
= malloc(width
* height
* (alpha
? 4 : 3) + 4);
64 RErrorCode
= RERR_NOMEMORY
;
75 RCloneImage(RImage
*image
)
81 new_image
= RCreateImage(image
->width
, image
->height
, HAS_ALPHA(image
));
85 new_image
->background
= image
->background
;
86 memcpy(new_image
->data
, image
->data
,
87 image
->width
*image
->height
*(HAS_ALPHA(image
) ? 4 : 3));
94 RGetSubImage(RImage
*image
, int x
, int y
, unsigned width
, unsigned height
)
98 unsigned total_line_size
, line_size
;
101 assert(x
>=0 && y
>=0);
102 assert(x
<image
->width
&& y
<image
->height
);
103 assert(width
>0 && height
>0);
105 if (x
+width
> image
->width
)
106 width
= image
->width
-x
;
107 if (y
+height
> image
->height
)
108 height
= image
->height
-y
;
110 new_image
= RCreateImage(width
, height
, HAS_ALPHA(image
));
114 new_image
->background
= image
->background
;
116 total_line_size
= image
->width
* (HAS_ALPHA(image
) ? 4 : 3);
117 line_size
= width
* (HAS_ALPHA(image
) ? 4 : 3);
119 ofs
= x
*(HAS_ALPHA(image
) ? 4 : 3) + y
*total_line_size
;;
121 for (i
=0; i
<height
; i
++) {
122 memcpy(&new_image
->data
[i
*line_size
],
123 &image
->data
[i
*total_line_size
+ofs
], line_size
);
130 RDestroyImage(RImage
*image
)
140 *----------------------------------------------------------------------
142 * Combines two equal sized images with alpha image. The second
143 * image will be placed on top of the first one.
144 *----------------------------------------------------------------------
147 RCombineImages(RImage
*image
, RImage
*src
)
149 assert(image
->width
== src
->width
);
150 assert(image
->height
== src
->height
);
152 if (!HAS_ALPHA(src
)) {
153 if (!HAS_ALPHA(image
)) {
154 memcpy(image
->data
, src
->data
, image
->height
*image
->width
*3);
157 unsigned char *d
, *s
;
161 for (y
= 0; y
< image
->height
; y
++) {
162 for (x
= 0; x
< image
->width
; x
++) {
179 if (!HAS_ALPHA(image
)) {
180 for (i
=0; i
<image
->height
*image
->width
; i
++) {
182 calpha
= 255 - alpha
;
183 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
184 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
185 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
189 for (i
=0; i
<image
->height
*image
->width
; i
++) {
191 calpha
= 255 - alpha
;
192 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
193 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
194 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
205 RCombineImagesWithOpaqueness(RImage
*image
, RImage
*src
, int opaqueness
)
212 assert(image
->width
== src
->width
);
213 assert(image
->height
== src
->height
);
218 c_opaqueness
= 255 - opaqueness
;
220 #define OP opaqueness
221 #define COP c_opaqueness
223 if (!HAS_ALPHA(src
)) {
224 int dalpha
= HAS_ALPHA(image
);
225 for (i
=0; i
< image
->width
*image
->height
; i
++) {
226 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
227 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
228 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
236 if (!HAS_ALPHA(image
)) {
237 for (i
=0; i
<image
->width
*image
->height
; i
++) {
238 tmp
= (*(s
+3) * opaqueness
)/256;
239 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
240 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
241 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
245 for (i
=0; i
<image
->width
*image
->height
; i
++) {
246 tmp
= (*(s
+3) * opaqueness
)/256;
247 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
248 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
249 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
260 calculateCombineArea(RImage
*des
, RImage
*src
, int *sx
, int *sy
,
261 int *swidth
, int *sheight
, int *dx
, int *dy
)
265 *swidth
= *swidth
+ *dx
;
269 if (*dx
+ *swidth
> des
->width
) {
270 *swidth
= des
->width
- *dx
;
275 *sheight
= *sheight
+ *dy
;
279 if (*dy
+ *sheight
> des
->height
) {
280 *sheight
= des
->height
- *dy
;
283 if (*sheight
> 0 && *swidth
> 0) {
289 RCombineArea(RImage
*image
, RImage
*src
, int sx
, int sy
, unsigned width
,
290 unsigned height
, int dx
, int dy
)
297 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
300 if (!HAS_ALPHA(src
)) {
301 if (!HAS_ALPHA(image
)) {
302 swi
= src
->width
* 3;
303 dwi
= image
->width
* 3;
305 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
306 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
308 for (y
=0; y
< height
; y
++) {
309 memcpy(d
, s
, width
*3);
314 swi
= (src
->width
- width
) * 3;
315 dwi
= (image
->width
- width
) * 4;
317 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
318 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
320 for (y
=0; y
< height
; y
++) {
321 for (x
=0; x
< width
; x
++) {
332 int dalpha
= HAS_ALPHA(image
);
334 swi
= (src
->width
- width
) * 4;
335 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 4;
337 dwi
= (image
->width
- width
) * 4;
338 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
340 dwi
= (image
->width
- width
) * 3;
341 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
344 for (y
=0; y
< height
; y
++) {
345 for (x
=0; x
< width
; x
++) {
347 calpha
= 255 - alpha
;
348 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
349 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
350 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
363 RCombineAreaWithOpaqueness(RImage
*image
, RImage
*src
, int sx
, int sy
,
364 unsigned width
, unsigned height
, int dx
, int dy
,
369 unsigned char *s
, *d
;
370 int dalpha
= HAS_ALPHA(image
);
371 int dch
= (dalpha
? 4 : 3);
373 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
376 d
= image
->data
+ (dy
*image
->width
+ dx
) * dch
;
377 dwi
= (image
->width
- width
)*dch
;
379 c_opaqueness
= 255 - opaqueness
;
381 #define OP opaqueness
382 #define COP c_opaqueness
384 if (!HAS_ALPHA(src
)) {
386 s
= src
->data
+ sy
*src
->width
*3;
387 swi
= (src
->width
- width
) * 3;
389 for (y
=0; y
< height
; y
++) {
390 for (x
=0; x
< width
; x
++) {
391 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
392 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
393 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
402 s
= src
->data
+ sy
*src
->width
*4;
403 swi
= (src
->width
- width
) * 4;
405 for (y
=0; y
< height
; y
++) {
406 for (x
=0; x
< width
; x
++) {
407 tmp
= (*(s
+3) * opaqueness
)/256;
408 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
409 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
410 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
425 RCombineImageWithColor(RImage
*image
, RColor
*color
)
429 int alpha
, nalpha
, r
, g
, b
;
433 if (!HAS_ALPHA(image
)) {
434 /* Image has no alpha channel, so we consider it to be all 255.
435 * Thus there are no transparent parts to be filled. */
442 for (i
=0; i
< image
->width
*image
->height
; i
++) {
444 nalpha
= 255 - alpha
;
446 *d
= (((int)*d
* alpha
) + (r
* nalpha
))/256; d
++;
447 *d
= (((int)*d
* alpha
) + (g
* nalpha
))/256; d
++;
448 *d
= (((int)*d
* alpha
) + (b
* nalpha
))/256; d
++;
457 RMakeTiledImage(RImage
*tile
, unsigned width
, unsigned height
)
461 unsigned long tile_size
= tile
->width
* tile
->height
;
462 unsigned long tx
= 0;
464 unsigned char *s
, *d
;
466 if (width
== tile
->width
&& height
== tile
->height
)
467 image
= RCloneImage(tile
);
468 else if (width
<= tile
->width
&& height
<= tile
->height
)
469 image
= RGetSubImage(tile
, 0, 0, width
, height
);
471 int has_alpha
= HAS_ALPHA(tile
);
473 image
= RCreateImage(width
, height
, has_alpha
);
478 for (y
= 0; y
< height
; y
++) {
479 for (x
= 0; x
< width
; x
+= tile
->width
) {
481 w
= (width
- x
< tile
->width
) ? width
- x
: tile
->width
;
485 memcpy(d
, s
+tx
*4, w
);
488 memcpy(d
, s
+tx
*3, w
);
493 tx
= (tx
+ tile
->width
) % tile_size
;
501 RMakeCenteredImage(RImage
*image
, unsigned width
, unsigned height
, RColor
*color
)
503 int x
, y
, w
, h
, sx
, sy
;
506 tmp
= RCreateImage(width
, height
, False
);
511 RClearImage(tmp
, color
);
513 if (image
->height
< height
) {
518 sy
= (image
->height
- height
)/2;
522 if (image
->width
< width
) {
527 sx
= (image
->width
- width
)/2;
531 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);