Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / raster.c
Commit [+]AuthorDateLineData
6830b057 dan2004-10-12 21:28:27 +00001/* raster.c - main and other misc stuff
2 *
4153e2fd dan2003-01-16 23:30:45 +00003 * Raster graphics library
6830b057 dan2004-10-12 21:28:27 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1997-2003 Alfredo K. Kojima
9d2e6ef9 scottc1998-09-29 22:36:29 +00006 *
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.
6830b057 dan2004-10-12 21:28:27 +000011 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000012 * 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.
6830b057 dan2004-10-12 21:28:27 +000016 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000017 * 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.
20 */
21
22#include <config.h>
23
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include <X11/Xlib.h>
28#include "wraster.h"
29
30#include <assert.h>
31
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020032char *WRasterLibVersion = "0.9";
9d2e6ef9 scottc1998-09-29 22:36:29 +000033
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020034int RErrorCode = RERR_NONE;
9d2e6ef9 scottc1998-09-29 22:36:29 +000035
a30475fc kojima2000-01-14 16:39:15 +000036#define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
37
d597c61c kojima2002-11-09 14:56:33 +000038#define MAX_WIDTH 20000
39#define MAX_HEIGHT 20000
40/* 20000^2*4 < 2G */
19f7fbc9 kojima2002-11-08 19:30:53 +000041
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020042RImage *RCreateImage(unsigned width, unsigned height, int alpha)
9d2e6ef9 scottc1998-09-29 22:36:29 +000043{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020044 RImage *image = NULL;
45
46 assert(width > 0 && height > 0);
47
48 if (width > MAX_WIDTH || height > MAX_HEIGHT) {
49 RErrorCode = RERR_NOMEMORY;
50 return NULL;
51 }
52
53 image = malloc(sizeof(RImage));
54 if (!image) {
55 RErrorCode = RERR_NOMEMORY;
56 return NULL;
57 }
58
59 memset(image, 0, sizeof(RImage));
60 image->width = width;
61 image->height = height;
62 image->format = alpha ? RRGBAFormat : RRGBFormat;
63 image->refCount = 1;
64
65 /* the +4 is to give extra bytes at the end of the buffer,
66 * so that we can optimize image conversion for MMX(tm).. see convert.c
67 */
68 image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
69 if (!image->data) {
70 RErrorCode = RERR_NOMEMORY;
71 free(image);
72 image = NULL;
73 }
74
75 return image;
55f3cb95 kojima2002-11-08 18:00:56 +000076
9d2e6ef9 scottc1998-09-29 22:36:29 +000077}
78
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020079RImage *RRetainImage(RImage * image)
36e46831 dan2001-04-21 07:12:21 +000080{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020081 if (image)
82 image->refCount++;
36e46831 dan2001-04-21 07:12:21 +000083
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020084 return image;
36e46831 dan2001-04-21 07:12:21 +000085}
86
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020087void RReleaseImage(RImage * image)
36e46831 dan2001-04-21 07:12:21 +000088{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020089 assert(image != NULL);
36e46831 dan2001-04-21 07:12:21 +000090
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020091 image->refCount--;
36e46831 dan2001-04-21 07:12:21 +000092
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020093 if (image->refCount < 1) {
94 free(image->data);
95 free(image);
96 }
36e46831 dan2001-04-21 07:12:21 +000097}
98
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020099RImage *RCloneImage(RImage * image)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000100{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200101 RImage *new_image;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000102
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200103 assert(image != NULL);
6830b057 dan2004-10-12 21:28:27 +0000104
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200105 new_image = RCreateImage(image->width, image->height, HAS_ALPHA(image));
106 if (!new_image)
107 return NULL;
6830b057 dan2004-10-12 21:28:27 +0000108
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200109 new_image->background = image->background;
110 memcpy(new_image->data, image->data, image->width * image->height * (HAS_ALPHA(image) ? 4 : 3));
9d2e6ef9 scottc1998-09-29 22:36:29 +0000111
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200112 return new_image;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000113}
114
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200115RImage *RGetSubImage(RImage * image, int x, int y, unsigned width, unsigned height)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000116{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200117 int i, ofs;
118 RImage *new_image;
119 unsigned total_line_size, line_size;
6830b057 dan2004-10-12 21:28:27 +0000120
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200121 assert(image != NULL);
122 assert(x >= 0 && y >= 0);
123 assert(x < image->width && y < image->height);
124 assert(width > 0 && height > 0);
6830b057 dan2004-10-12 21:28:27 +0000125
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200126 if (x + width > image->width)
127 width = image->width - x;
128 if (y + height > image->height)
129 height = image->height - y;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000130
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200131 new_image = RCreateImage(width, height, HAS_ALPHA(image));
a30475fc kojima2000-01-14 16:39:15 +0000132
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200133 if (!new_image)
134 return NULL;
135 new_image->background = image->background;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000136
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200137 total_line_size = image->width * (HAS_ALPHA(image) ? 4 : 3);
138 line_size = width * (HAS_ALPHA(image) ? 4 : 3);
a30475fc kojima2000-01-14 16:39:15 +0000139
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200140 ofs = x * (HAS_ALPHA(image) ? 4 : 3) + y * total_line_size;;
a30475fc kojima2000-01-14 16:39:15 +0000141
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200142 for (i = 0; i < height; i++) {
143 memcpy(&new_image->data[i * line_size], &image->data[i * total_line_size + ofs], line_size);
144 }
145 return new_image;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000146}
147
9d2e6ef9 scottc1998-09-29 22:36:29 +0000148/*
6830b057 dan2004-10-12 21:28:27 +0000149 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000150 * RCombineImages-
151 * Combines two equal sized images with alpha image. The second
152 * image will be placed on top of the first one.
6830b057 dan2004-10-12 21:28:27 +0000153 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000154 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200155void RCombineImages(RImage * image, RImage * src)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000156{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200157 assert(image->width == src->width);
158 assert(image->height == src->height);
159
160 if (!HAS_ALPHA(src)) {
161 if (!HAS_ALPHA(image)) {
162 memcpy(image->data, src->data, image->height * image->width * 3);
163 } else {
164 int x, y;
165 unsigned char *d, *s;
166
167 d = image->data;
168 s = src->data;
169 for (y = 0; y < image->height; y++) {
170 for (x = 0; x < image->width; x++) {
171 *d++ = *s++;
172 *d++ = *s++;
173 *d++ = *s++;
174 d++;
175 }
176 }
177 }
178 } else {
179 register int i;
180 unsigned char *d;
181 unsigned char *s;
182 int alpha, calpha;
183
184 d = image->data;
185 s = src->data;
186
187 if (!HAS_ALPHA(image)) {
188 for (i = 0; i < image->height * image->width; i++) {
189 alpha = *(s + 3);
190 calpha = 255 - alpha;
191 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
192 d++;
193 s++;
194 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
195 d++;
196 s++;
197 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
198 d++;
199 s++;
200 s++;
201 }
202 } else {
203 for (i = 0; i < image->height * image->width; i++) {
204 alpha = *(s + 3);
205 calpha = 255 - alpha;
206 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
207 d++;
208 s++;
209 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
210 d++;
211 s++;
212 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
213 d++;
214 s++;
215 *d++ |= *s++;
216 }
217 }
218 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000219}
220
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200221void RCombineImagesWithOpaqueness(RImage * image, RImage * src, int opaqueness)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000222{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200223 register int i;
224 unsigned char *d;
225 unsigned char *s;
226 int c_opaqueness;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000227
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200228 assert(image->width == src->width);
229 assert(image->height == src->height);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000230
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200231 d = image->data;
232 s = src->data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000233
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200234 c_opaqueness = 255 - opaqueness;
c720860e dan2001-02-26 00:43:44 +0000235
9d2e6ef9 scottc1998-09-29 22:36:29 +0000236#define OP opaqueness
c720860e dan2001-02-26 00:43:44 +0000237#define COP c_opaqueness
238
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200239 if (!HAS_ALPHA(src)) {
240 int dalpha = HAS_ALPHA(image);
241 for (i = 0; i < image->width * image->height; i++) {
242 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
243 d++;
244 s++;
245 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
246 d++;
247 s++;
248 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
249 d++;
250 s++;
251 if (dalpha) {
252 d++;
253 }
254 }
255 } else {
256 int tmp;
257
258 if (!HAS_ALPHA(image)) {
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;
262 d++;
263 s++;
264 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
265 d++;
266 s++;
267 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
268 d++;
269 s++;
270 s++;
271 }
272 } else {
273 for (i = 0; i < image->width * image->height; i++) {
274 tmp = (*(s + 3) * opaqueness) / 256;
275 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
276 d++;
277 s++;
278 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
279 d++;
280 s++;
281 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
282 d++;
283 s++;
284 *d |= tmp;
285 d++;
286 s++;
287 }
288 }
289 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000290#undef OP
c720860e dan2001-02-26 00:43:44 +0000291#undef COP
9d2e6ef9 scottc1998-09-29 22:36:29 +0000292}
293
83824dbc id2000-12-06 03:07:51 +0000294int
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200295calculateCombineArea(RImage * des, RImage * src, int *sx, int *sy,
296 unsigned int *swidth, unsigned int *sheight, int *dx, int *dy)
c720860e dan2001-02-26 00:43:44 +0000297{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200298 int width = (int)*swidth, height = (int)*sheight;
299
300 if (*dx < 0) {
301 *sx = -*dx;
302 width = width + *dx;
303 *dx = 0;
304 }
305
306 if (*dx + width > des->width) {
307 width = des->width - *dx;
308 }
309
310 if (*dy < 0) {
311 *sy = -*dy;
312 height = height + *dy;
313 *dy = 0;
314 }
315
316 if (*dy + height > des->height) {
317 height = des->height - *dy;
318 }
319
320 if (height > 0 && width > 0) {
321 *swidth = width;
322 *sheight = height;
323 return True;
324 }
325
326 return False;
83824dbc id2000-12-06 03:07:51 +0000327}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000328
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200329void RCombineArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000330{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200331 int x, y, dwi, swi;
332 unsigned char *d;
333 unsigned char *s;
334 int alpha, calpha;
335
336 if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
337 return;
338
339 if (!HAS_ALPHA(src)) {
340 if (!HAS_ALPHA(image)) {
341 swi = src->width * 3;
342 dwi = image->width * 3;
343
344 s = src->data + (sy * (int)src->width + sx) * 3;
345 d = image->data + (dy * (int)image->width + dx) * 3;
346
347 for (y = 0; y < height; y++) {
348 memcpy(d, s, width * 3);
349 d += dwi;
350 s += swi;
351 }
352 } else {
353 swi = (src->width - width) * 3;
354 dwi = (image->width - width) * 4;
355
356 s = src->data + (sy * (int)src->width + sx) * 3;
357 d = image->data + (dy * (int)image->width + dx) * 4;
358
359 for (y = 0; y < height; y++) {
360 for (x = 0; x < width; x++) {
361 *d++ = *s++;
362 *d++ = *s++;
363 *d++ = *s++;
364 d++;
365 }
366 d += dwi;
367 s += swi;
368 }
369 }
370 } else {
371 int dalpha = HAS_ALPHA(image);
372
373 swi = (src->width - width) * 4;
374 s = src->data + (sy * (int)src->width + sx) * 4;
375 if (dalpha) {
376 dwi = (image->width - width) * 4;
377 d = image->data + (dy * (int)image->width + dx) * 4;
378 } else {
379 dwi = (image->width - width) * 3;
380 d = image->data + (dy * (int)image->width + dx) * 3;
381 }
382
383 for (y = 0; y < height; y++) {
384 for (x = 0; x < width; x++) {
385 alpha = *(s + 3);
386 calpha = 255 - alpha;
387 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
388 s++;
389 d++;
390 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
391 s++;
392 d++;
393 *d = (((int)*d * calpha) + ((int)*s * alpha)) / 256;
394 s++;
395 d++;
396 s++;
397 if (dalpha)
398 d++;
399 }
400 d += dwi;
401 s += swi;
402 }
403 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000404}
405
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200406void RCopyArea(RImage * image, RImage * src, int sx, int sy, unsigned width, unsigned height, int dx, int dy)
363b6032 kojima2004-10-24 05:01:55 +0000407{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200408 int x, y, dwi, swi;
409 unsigned char *d;
410 unsigned char *s;
411
412 if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
413 return;
414
415 if (!HAS_ALPHA(src)) {
416 if (!HAS_ALPHA(image)) {
417 swi = src->width * 3;
418 dwi = image->width * 3;
419
420 s = src->data + (sy * (int)src->width + sx) * 3;
421 d = image->data + (dy * (int)image->width + dx) * 3;
422
423 for (y = 0; y < height; y++) {
424 memcpy(d, s, width * 3);
425 d += dwi;
426 s += swi;
427 }
428 } else {
429 swi = (src->width - width) * 3;
430 dwi = (image->width - width) * 4;
431
432 s = src->data + (sy * (int)src->width + sx) * 3;
433 d = image->data + (dy * (int)image->width + dx) * 4;
434
435 for (y = 0; y < height; y++) {
436 for (x = 0; x < width; x++) {
437 *d++ = *s++;
438 *d++ = *s++;
439 *d++ = *s++;
440 d++;
441 }
442 d += dwi;
443 s += swi;
444 }
445 }
446 } else {
447 int dalpha = HAS_ALPHA(image);
448
449 swi = src->width * 4;
450 s = src->data + (sy * (int)src->width + sx) * 4;
451 if (dalpha) {
452 dwi = image->width * 4;
453 d = image->data + (dy * (int)image->width + dx) * 4;
454 } else {
455 dwi = image->width * 3;
456 d = image->data + (dy * (int)image->width + dx) * 3;
457 }
458
459 if (dalpha) {
460 for (y = 0; y < height; y++) {
461 memcpy(d, s, width * 4);
462 d += dwi;
463 s += swi;
464 }
465 } else {
466 for (y = 0; y < height; y++) {
467 for (x = 0; x < width; x++) {
468 *d++ = *s++;
469 *d++ = *s++;
470 *d++ = *s++;
471 s++;
472 }
473 d += dwi;
474 s += swi;
475 }
476 }
477 }
363b6032 kojima2004-10-24 05:01:55 +0000478}
479
363b6032 kojima2004-10-24 05:01:55 +0000480void
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200481RCombineAreaWithOpaqueness(RImage * image, RImage * src, int sx, int sy,
482 unsigned width, unsigned height, int dx, int dy, int opaqueness)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000483{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200484 int x, y, dwi, swi;
485 int c_opaqueness;
486 unsigned char *s, *d;
487 int dalpha = HAS_ALPHA(image);
488 int dch = (dalpha ? 4 : 3);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000489
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200490 if (!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
491 return;
049a69a9 dan2000-01-15 02:00:24 +0000492
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200493 d = image->data + (dy * image->width + dx) * dch;
494 dwi = (image->width - width) * dch;
049a69a9 dan2000-01-15 02:00:24 +0000495
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200496 c_opaqueness = 255 - opaqueness;
c720860e dan2001-02-26 00:43:44 +0000497
9d2e6ef9 scottc1998-09-29 22:36:29 +0000498#define OP opaqueness
9d2e6ef9 scottc1998-09-29 22:36:29 +0000499#define COP c_opaqueness
a30475fc kojima2000-01-14 16:39:15 +0000500
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200501 if (!HAS_ALPHA(src)) {
502
503 s = src->data + (sy * src->width + sx) * 3;
504 swi = (src->width - width) * 3;
505
506 for (y = 0; y < height; y++) {
507 for (x = 0; x < width; x++) {
508 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
509 s++;
510 d++;
511 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
512 s++;
513 d++;
514 *d = (((int)*d * (int)COP) + ((int)*s * (int)OP)) / 256;
515 s++;
516 d++;
517 if (dalpha)
518 d++;
519 }
520 d += dwi;
521 s += swi;
522 }
523 } else {
524 int tmp;
525
526 s = src->data + (sy * src->width + sx) * 4;
527 swi = (src->width - width) * 4;
528
529 for (y = 0; y < height; y++) {
530 for (x = 0; x < width; x++) {
531 tmp = (*(s + 3) * opaqueness) / 256;
532 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
533 d++;
534 s++;
535 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
536 d++;
537 s++;
538 *d = (((int)*d * (255 - tmp)) + ((int)*s * tmp)) / 256;
539 d++;
540 s++;
541 s++;
542 if (dalpha)
543 d++;
544 }
545 d += dwi;
546 s += swi;
547 }
548 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000549#undef OP
c720860e dan2001-02-26 00:43:44 +0000550#undef COP
6830b057 dan2004-10-12 21:28:27 +0000551}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000552
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200553void RCombineImageWithColor(RImage * image, RColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000554{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200555 register int i;
556 unsigned char *d;
557 int alpha, nalpha, r, g, b;
558
559 d = image->data;
560
561 if (!HAS_ALPHA(image)) {
562 /* Image has no alpha channel, so we consider it to be all 255.
563 * Thus there are no transparent parts to be filled. */
564 return;
565 }
566 r = color->red;
567 g = color->green;
568 b = color->blue;
569
570 for (i = 0; i < image->width * image->height; i++) {
571 alpha = *(d + 3);
572 nalpha = 255 - alpha;
573
574 *d = (((int)*d * alpha) + (r * nalpha)) / 256;
575 d++;
576 *d = (((int)*d * alpha) + (g * nalpha)) / 256;
577 d++;
578 *d = (((int)*d * alpha) + (b * nalpha)) / 256;
579 d++;
580 d++;
581 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000582}
583
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200584RImage *RMakeTiledImage(RImage * tile, unsigned width, unsigned height)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000585{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200586 int x, y;
587 unsigned w;
588 unsigned long tile_size = tile->width * tile->height;
589 unsigned long tx = 0;
590 RImage *image;
591 unsigned char *s, *d;
592
593 if (width == tile->width && height == tile->height)
594 image = RCloneImage(tile);
595 else if (width <= tile->width && height <= tile->height)
596 image = RGetSubImage(tile, 0, 0, width, height);
597 else {
598 int has_alpha = HAS_ALPHA(tile);
599
600 image = RCreateImage(width, height, has_alpha);
601
602 d = image->data;
603 s = tile->data;
604
605 for (y = 0; y < height; y++) {
606 for (x = 0; x < width; x += tile->width) {
607
608 w = (width - x < tile->width) ? width - x : tile->width;
609
610 if (has_alpha) {
611 w *= 4;
612 memcpy(d, s + tx * 4, w);
613 } else {
614 w *= 3;
615 memcpy(d, s + tx * 3, w);
616 }
617 d += w;
618 }
619
620 tx = (tx + tile->width) % tile_size;
621 }
622 }
623 return image;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000624}
625
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200626RImage *RMakeCenteredImage(RImage * image, unsigned width, unsigned height, RColor * color)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000627{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200628 int x, y, w, h, sx, sy;
629 RImage *tmp;
630
631 tmp = RCreateImage(width, height, False);
632 if (!tmp) {
633 return NULL;
634 }
635
636 RClearImage(tmp, color);
637
638 if (image->height < height) {
639 h = image->height;
640 y = (height - h) / 2;
641 sy = 0;
642 } else {
643 sy = (image->height - height) / 2;
644 y = 0;
645 h = height;
646 }
647 if (image->width < width) {
648 w = image->width;
649 x = (width - w) / 2;
650 sx = 0;
651 } else {
652 sx = (image->width - width) / 2;
653 x = 0;
654 w = width;
655 }
656 RCombineArea(tmp, image, sx, sy, w, h, x, y);
657
658 return tmp;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000659}