1 /* raster.c - main and other misc stuff
3 * Raster graphics library
5 * Copyright (c) 1997 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.
35 char *WRasterLibVersion
="0.2";
37 char RErrorString
[MAXERRLEN
];
42 RCreateImage(unsigned width
, unsigned height
, int alpha
)
47 assert(width
>0 && height
>0);
49 image
= malloc(sizeof(RImage
));
51 sprintf(RErrorString
, "out of memory");
55 memset(image
, 0, sizeof(RImage
));
57 image
->height
= height
;
58 for (i
=0; i
<3+(alpha
?1:0); i
++) {
59 image
->data
[i
] = malloc(width
*height
);
72 sprintf(RErrorString
, "out of memory");
79 RCloneImage(RImage
*image
)
85 new_image
= RCreateImage(image
->width
, image
->height
, image
->data
[3]!=NULL
);
89 new_image
->background
= image
->background
;
90 memcpy(new_image
->data
[0], image
->data
[0], image
->width
*image
->height
);
91 memcpy(new_image
->data
[1], image
->data
[1], image
->width
*image
->height
);
92 memcpy(new_image
->data
[2], image
->data
[2], image
->width
*image
->height
);
94 memcpy(new_image
->data
[3], image
->data
[3], image
->width
*image
->height
);
101 RGetSubImage(RImage
*image
, int x
, int y
, unsigned width
, unsigned height
)
105 unsigned char *sr
, *sg
, *sb
, *sa
;
106 unsigned char *dr
, *dg
, *db
, *da
;
109 assert(x
>=0 && y
>=0);
110 assert(x
<image
->width
&& y
<image
->height
);
111 assert(width
>0 && height
>0);
113 if (x
+width
> image
->width
)
114 width
= image
->width
-x
;
115 if (y
+height
> image
->height
)
116 height
= image
->height
-y
;
118 new_image
= RCreateImage(width
, height
, image
->data
[3]!=NULL
);
121 new_image
->background
= image
->background
;
122 ofs
= image
->width
*y
+x
;
124 sr
= image
->data
[0]+ofs
;
125 sg
= image
->data
[1]+ofs
;
126 sb
= image
->data
[2]+ofs
;
127 sa
= image
->data
[3]+ofs
;
129 dr
= new_image
->data
[0];
130 dg
= new_image
->data
[1];
131 db
= new_image
->data
[2];
132 da
= new_image
->data
[3];
134 for (i
=0; i
<height
; i
++) {
135 memcpy(dr
, sr
, width
);
136 memcpy(dg
, sg
, width
);
137 memcpy(db
, sb
, width
);
145 memcpy(da
, sa
, width
);
155 RDestroyImage(RImage
*image
)
161 for (i
=0; i
<4; i
++) {
163 free(image
->data
[i
]);
170 *----------------------------------------------------------------------
172 * Combines two equal sized images with alpha image. The second
173 * image will be placed on top of the first one.
174 *----------------------------------------------------------------------
177 RCombineImages(RImage
*image
, RImage
*src
)
180 unsigned char *dr
, *dg
, *db
, *da
;
181 unsigned char *sr
, *sg
, *sb
, *sa
;
185 assert(image
->width
== src
->width
);
186 assert(image
->height
== src
->height
);
199 memcpy(dr
, sr
, image
->height
*image
->width
);
200 memcpy(dg
, sg
, image
->height
*image
->width
);
201 memcpy(db
, sb
, image
->height
*image
->width
);
203 for (i
=0; i
<image
->height
*image
->width
; i
++) {
206 *dr
= (((int)*dr
* calpha
) + ((int)*sr
* alpha
))/256;
207 *dg
= (((int)*dg
* calpha
) + ((int)*sg
* alpha
))/256;
208 *db
= (((int)*db
* calpha
) + ((int)*sb
* alpha
))/256;
223 RCombineImagesWithOpaqueness(RImage
*image
, RImage
*src
, int opaqueness
)
226 unsigned char *dr
, *dg
, *db
;
227 unsigned char *sr
, *sg
, *sb
, *sa
;
231 assert(image
->width
== src
->width
);
232 assert(image
->height
== src
->height
);
243 c_opaqueness
= 255 - opaqueness
;
244 #define OP opaqueness
246 #define COP c_opaqueness
247 for (i
=0; i
<image
->width
*image
->height
; i
++) {
248 *dr
= (((int)*dr
*(int)COP
) + ((int)*sr
*(int)OP
))/256;
249 *dg
= (((int)*dg
*(int)COP
) + ((int)*sg
*(int)OP
))/256;
250 *db
= (((int)*db
*(int)COP
) + ((int)*sb
*(int)OP
))/256;
258 for (i
=0; i
<image
->width
*image
->height
; i
++) {
259 tmp
= (*sa
* opaqueness
)/256;
260 *dr
= (((int)*dr
*(int)(255-tmp
)) + ((int)*sr
*(int)tmp
))/256;
261 *dg
= (((int)*dg
*(int)(255-tmp
)) + ((int)*sg
*(int)tmp
))/256;
262 *db
= (((int)*db
*(int)(255-tmp
)) + ((int)*sb
*(int)tmp
))/256;
273 RCombineArea(RImage
*image
, RImage
*src
, int sx
, int sy
, unsigned width
,
274 unsigned height
, int dx
, int dy
)
277 unsigned char *dr
, *dg
, *db
;
278 unsigned char *sr
, *sg
, *sb
, *sa
;
282 assert(dy
< image
->height
);
283 assert(dx
< image
->width
);
285 assert(sy
+ height
<= src
->height
);
286 assert(sx
+ width
<= src
->width
);
288 dr
= image
->data
[0] + dy
*(int)image
->width
+ dx
;
289 dg
= image
->data
[1] + dy
*(int)image
->width
+ dx
;
290 db
= image
->data
[2] + dy
*(int)image
->width
+ dx
;
292 sr
= src
->data
[0] + sy
*(int)src
->width
+ sx
;
293 sg
= src
->data
[1] + sy
*(int)src
->width
+ sx
;
294 sb
= src
->data
[2] + sy
*(int)src
->width
+ sx
;
295 sa
= src
->data
[3] + sy
*(int)src
->width
+ sx
;
297 swi
= src
->width
- width
;
298 dwi
= image
->width
- width
;
300 if (height
> image
->height
- dy
)
301 height
= image
->height
- dy
;
304 for (y
=sy
; y
<height
+sy
; y
++) {
305 for (x
=sx
; x
<width
+sx
; x
++) {
310 dr
+= dwi
; dg
+= dwi
; db
+= dwi
;
311 sr
+= swi
; sg
+= swi
; sb
+= swi
;
314 for (y
=sy
; y
<height
+sy
; y
++) {
315 for (x
=sx
; x
<width
+sx
; x
++) {
317 calpha
= 255 - alpha
;
318 *dr
= (((int)*dr
* calpha
) + ((int)*sr
* alpha
))/256;
319 *dg
= (((int)*dg
* calpha
) + ((int)*sg
* alpha
))/256;
320 *db
= (((int)*db
* calpha
) + ((int)*sb
* alpha
))/256;
325 dr
+= dwi
; dg
+= dwi
; db
+= dwi
;
326 sr
+= swi
; sg
+= swi
; sb
+= swi
;
334 RCombineAreaWithOpaqueness(RImage
*image
, RImage
*src
, int sx
, int sy
,
335 unsigned width
, unsigned height
, int dx
, int dy
,
340 unsigned char *dr
, *dg
, *db
;
341 unsigned char *sr
, *sg
, *sb
, *sa
;
343 assert(dy
<= image
->height
);
344 assert(dx
<= image
->width
);
346 assert(sy
<= height
);
349 dr
= image
->data
[0] + dy
*image
->width
+ dx
;
350 dg
= image
->data
[1] + dy
*image
->width
+ dx
;
351 db
= image
->data
[2] + dy
*image
->width
+ dx
;
353 sr
= src
->data
[0] + sy
*src
->width
;
354 sg
= src
->data
[1] + sy
*src
->width
;
355 sb
= src
->data
[2] + sy
*src
->width
;
356 sa
= src
->data
[3] + sy
*src
->width
;
358 swi
= src
->width
- width
;
359 dwi
= image
->width
- width
;
365 if (height
> image
->height
- dy
)
366 height
= image
->height
- dy
;
368 c_opaqueness
= 255 - opaqueness
;
369 #define OP opaqueness
371 #define COP c_opaqueness
372 for (y
=0; y
<height
; y
++) {
373 for (x
=0; x
<width
; x
++) {
374 *dr
= (((int)*dr
*(int)COP
) + ((int)*sr
*(int)OP
))/256;
375 *dg
= (((int)*dg
*(int)COP
) + ((int)*sg
*(int)OP
))/256;
376 *db
= (((int)*db
*(int)COP
) + ((int)*sb
*(int)OP
))/256;
380 dr
+= dwi
; dg
+= dwi
; db
+= dwi
;
381 sr
+= swi
; sg
+= swi
; sb
+= swi
;
387 for (y
=0; y
<height
; y
++) {
388 for (x
=0; x
<width
; x
++) {
389 tmp
= (*sa
* opaqueness
)/256;
390 *dr
= (((int)*dr
*(int)(255-tmp
)) + ((int)*sr
*(int)tmp
))/256;
391 *dg
= (((int)*dg
*(int)(255-tmp
)) + ((int)*sg
*(int)tmp
))/256;
392 *db
= (((int)*db
*(int)(255-tmp
)) + ((int)*sb
*(int)tmp
))/256;
397 dr
+= dwi
; dg
+= dwi
; db
+= dwi
;
398 sr
+= swi
; sg
+= swi
; sb
+= swi
;
409 RCombineImageWithColor(RImage
*image
, RColor
*color
)
412 unsigned char *dr
, *dg
, *db
, *da
;
413 int alpha
, nalpha
, r
, g
, b
;
421 /* Image has no alpha channel, so we consider it to be all 255 */
428 for (i
=0; i
<image
->width
*image
->height
; i
++) {
430 nalpha
= 255 - alpha
;
432 *dr
= (((int)*dr
* alpha
) + (r
* nalpha
))/256;
433 *dg
= (((int)*dg
* alpha
) + (g
* nalpha
))/256;
434 *db
= (((int)*db
* alpha
) + (b
* nalpha
))/256;
435 dr
++; dg
++; db
++; da
++;
441 RMakeTiledImage(RImage
*tile
, unsigned width
, unsigned height
)
445 unsigned long tile_size
= tile
->width
* tile
->height
;
446 unsigned long tx
= 0;
447 int have_alpha
= (tile
->data
[3]!=NULL
);
449 unsigned char *sr
, *sg
, *sb
, *sa
;
450 unsigned char *dr
, *dg
, *db
, *da
;
452 if (width
== tile
->width
&& height
== tile
->height
)
453 image
= RCloneImage(tile
);
454 else if (width
<= tile
->width
&& height
<= tile
->height
)
455 image
= RGetSubImage(tile
, 0, 0, width
, height
);
458 image
= RCreateImage(width
, height
, have_alpha
);
470 for (y
= 0; y
< height
; y
++) {
471 for (x
= 0; x
< width
; x
+= tile
->width
) {
473 w
= (width
- x
< tile
->width
) ? width
- x
: tile
->width
;
475 memcpy(dr
, sr
+tx
, w
);
476 memcpy(dg
, sg
+tx
, w
);
477 memcpy(db
, sb
+tx
, w
);
479 memcpy(da
, sa
+tx
, w
);
488 tx
= (tx
+ tile
->width
) % tile_size
;
496 RMakeCenteredImage(RImage
*image
, unsigned width
, unsigned height
, RColor
*color
)
498 int x
, y
, w
, h
, sx
, sy
;
501 tmp
= RCreateImage(width
, height
, False
);
506 RClearImage(tmp
, color
);
508 if (image
->height
< height
) {
513 sy
= (image
->height
- height
)/2;
517 if (image
->width
< width
) {
522 sx
= (image
->width
- width
)/2;
526 RCombineArea(tmp
, image
, sx
, sy
, w
, h
, x
, y
);