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.
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
;
60 /* the +4 is to give extra bytes at the end of the buffer,
61 * so that we can optimize image conversion for MMX(tm).. see convert.c
63 image
->data
= malloc(width
* height
* (alpha
? 4 : 3) + 4);
65 RErrorCode
= RERR_NOMEMORY
;
75 RRetainImage(RImage
*image
)
85 RReleaseImage(RImage
*image
)
91 if (image
->refCount
< 1) {
99 RCloneImage(RImage
*image
)
105 new_image
= RCreateImage(image
->width
, image
->height
, HAS_ALPHA(image
));
109 new_image
->background
= image
->background
;
110 memcpy(new_image
->data
, image
->data
,
111 image
->width
*image
->height
*(HAS_ALPHA(image
) ? 4 : 3));
118 RGetSubImage(RImage
*image
, int x
, int y
, unsigned width
, unsigned height
)
122 unsigned total_line_size
, line_size
;
125 assert(x
>=0 && y
>=0);
126 assert(x
<image
->width
&& y
<image
->height
);
127 assert(width
>0 && height
>0);
129 if (x
+width
> image
->width
)
130 width
= image
->width
-x
;
131 if (y
+height
> image
->height
)
132 height
= image
->height
-y
;
134 new_image
= RCreateImage(width
, height
, HAS_ALPHA(image
));
138 new_image
->background
= image
->background
;
140 total_line_size
= image
->width
* (HAS_ALPHA(image
) ? 4 : 3);
141 line_size
= width
* (HAS_ALPHA(image
) ? 4 : 3);
143 ofs
= x
*(HAS_ALPHA(image
) ? 4 : 3) + y
*total_line_size
;;
145 for (i
=0; i
<height
; i
++) {
146 memcpy(&new_image
->data
[i
*line_size
],
147 &image
->data
[i
*total_line_size
+ofs
], line_size
);
154 *----------------------------------------------------------------------
156 * Combines two equal sized images with alpha image. The second
157 * image will be placed on top of the first one.
158 *----------------------------------------------------------------------
161 RCombineImages(RImage
*image
, RImage
*src
)
163 assert(image
->width
== src
->width
);
164 assert(image
->height
== src
->height
);
166 if (!HAS_ALPHA(src
)) {
167 if (!HAS_ALPHA(image
)) {
168 memcpy(image
->data
, src
->data
, image
->height
*image
->width
*3);
171 unsigned char *d
, *s
;
175 for (y
= 0; y
< image
->height
; y
++) {
176 for (x
= 0; x
< image
->width
; x
++) {
193 if (!HAS_ALPHA(image
)) {
194 for (i
=0; i
<image
->height
*image
->width
; i
++) {
196 calpha
= 255 - alpha
;
197 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
198 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
199 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
203 for (i
=0; i
<image
->height
*image
->width
; i
++) {
205 calpha
= 255 - alpha
;
206 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
207 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
208 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; d
++; s
++;
219 RCombineImagesWithOpaqueness(RImage
*image
, RImage
*src
, int opaqueness
)
226 assert(image
->width
== src
->width
);
227 assert(image
->height
== src
->height
);
232 c_opaqueness
= 255 - opaqueness
;
234 #define OP opaqueness
235 #define COP c_opaqueness
237 if (!HAS_ALPHA(src
)) {
238 int dalpha
= HAS_ALPHA(image
);
239 for (i
=0; i
< image
->width
*image
->height
; i
++) {
240 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
241 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
242 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; d
++; s
++;
250 if (!HAS_ALPHA(image
)) {
251 for (i
=0; i
<image
->width
*image
->height
; i
++) {
252 tmp
= (*(s
+3) * opaqueness
)/256;
253 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
254 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
255 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
259 for (i
=0; i
<image
->width
*image
->height
; i
++) {
260 tmp
= (*(s
+3) * opaqueness
)/256;
261 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
262 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
263 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
274 calculateCombineArea(RImage
*des
, RImage
*src
, int *sx
, int *sy
,
275 int *swidth
, int *sheight
, int *dx
, int *dy
)
279 *swidth
= *swidth
+ *dx
;
283 if (*dx
+ *swidth
> des
->width
) {
284 *swidth
= des
->width
- *dx
;
289 *sheight
= *sheight
+ *dy
;
293 if (*dy
+ *sheight
> des
->height
) {
294 *sheight
= des
->height
- *dy
;
297 if (*sheight
> 0 && *swidth
> 0) {
303 RCombineArea(RImage
*image
, RImage
*src
, int sx
, int sy
, unsigned width
,
304 unsigned height
, int dx
, int dy
)
311 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
314 if (!HAS_ALPHA(src
)) {
315 if (!HAS_ALPHA(image
)) {
316 swi
= src
->width
* 3;
317 dwi
= image
->width
* 3;
319 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
320 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
322 for (y
=0; y
< height
; y
++) {
323 memcpy(d
, s
, width
*3);
328 swi
= (src
->width
- width
) * 3;
329 dwi
= (image
->width
- width
) * 4;
331 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 3;
332 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
334 for (y
=0; y
< height
; y
++) {
335 for (x
=0; x
< width
; x
++) {
346 int dalpha
= HAS_ALPHA(image
);
348 swi
= (src
->width
- width
) * 4;
349 s
= src
->data
+ (sy
*(int)src
->width
+ sx
) * 4;
351 dwi
= (image
->width
- width
) * 4;
352 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 4;
354 dwi
= (image
->width
- width
) * 3;
355 d
= image
->data
+ (dy
*(int)image
->width
+ dx
) * 3;
358 for (y
=0; y
< height
; y
++) {
359 for (x
=0; x
< width
; x
++) {
361 calpha
= 255 - alpha
;
362 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
363 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
364 *d
= (((int)*d
* calpha
) + ((int)*s
* alpha
))/256; s
++; d
++;
377 RCombineAreaWithOpaqueness(RImage
*image
, RImage
*src
, int sx
, int sy
,
378 unsigned width
, unsigned height
, int dx
, int dy
,
383 unsigned char *s
, *d
;
384 int dalpha
= HAS_ALPHA(image
);
385 int dch
= (dalpha
? 4 : 3);
387 if(!calculateCombineArea(image
, src
, &sx
, &sy
, &width
, &height
, &dx
, &dy
))
390 d
= image
->data
+ (dy
*image
->width
+ dx
) * dch
;
391 dwi
= (image
->width
- width
)*dch
;
393 c_opaqueness
= 255 - opaqueness
;
395 #define OP opaqueness
396 #define COP c_opaqueness
398 if (!HAS_ALPHA(src
)) {
400 s
= src
->data
+ (sy
*src
->width
+ sx
)*3;
401 swi
= (src
->width
- width
) * 3;
403 for (y
=0; y
< height
; y
++) {
404 for (x
=0; x
< width
; x
++) {
405 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
406 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
407 *d
= (((int)*d
*(int)COP
) + ((int)*s
*(int)OP
))/256; s
++; d
++;
416 s
= src
->data
+ (sy
*src
->width
+ sx
)*4;
417 swi
= (src
->width
- width
) * 4;
419 for (y
=0; y
< height
; y
++) {
420 for (x
=0; x
< width
; x
++) {
421 tmp
= (*(s
+3) * opaqueness
)/256;
422 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
423 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
424 *d
= (((int)*d
* (255-tmp
)) + ((int)*s
* tmp
))/256; d
++; s
++;
439 RCombineImageWithColor(RImage
*image
, RColor
*color
)
443 int alpha
, nalpha
, r
, g
, b
;
447 if (!HAS_ALPHA(image
)) {
448 /* Image has no alpha channel, so we consider it to be all 255.
449 * Thus there are no transparent parts to be filled. */
456 for (i
=0; i
< image
->width
*image
->height
; i
++) {
458 nalpha
= 255 - alpha
;
460 *d
= (((int)*d
* alpha
) + (r
* nalpha
))/256; d
++;
461 *d
= (((int)*d
* alpha
) + (g
* nalpha
))/256; d
++;
462 *d
= (((int)*d
* alpha
) + (b
* nalpha
))/256; d
++;
471 RMakeTiledImage(RImage
*tile
, unsigned width
, unsigned height
)
475 unsigned long tile_size
= tile
->width
* tile
->height
;
476 unsigned long tx
= 0;
478 unsigned char *s
, *d
;
480 if (width
== tile
->width
&& height
== tile
->height
)
481 image
= RCloneImage(tile
);
482 else if (width
<= tile
->width
&& height
<= tile
->height
)
483 image
= RGetSubImage(tile
, 0, 0, width
, height
);
485 int has_alpha
= HAS_ALPHA(tile
);
487 image
= RCreateImage(width
, height
, has_alpha
);
492 for (y
= 0; y
< height
; y
++) {
493 for (x
= 0; x
< width
; x
+= tile
->width
) {
495 w
= (width
- x
< tile
->width
) ? width
- x
: tile
->width
;
499 memcpy(d
, s
+tx
*4, w
);
502 memcpy(d
, s
+tx
*3, w
);
507 tx
= (tx
+ tile
->width
) % tile_size
;
515 RMakeCenteredImage(RImage
*image
, unsigned width
, unsigned height
, RColor
*color
)
517 int x
, y
, w
, h
, sx
, sy
;
520 tmp
= RCreateImage(width
, height
, False
);
525 RClearImage(tmp
, color
);
527 if (image
->height
< height
) {
532 sy
= (image
->height
- height
)/2;
536 if (image
->width
< width
) {
541 sx
= (image
->width
- width
)/2;
545 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);