- Fixed crashing bug in menu.c
[wmaker-crm.git] / wrlib / raster.c
blobe0b30505dc781d829145e90fed172a4106b9fa97
1 /* raster.c - main and other misc stuff
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997-2003 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.
22 #include <config.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
28 #include "wraster.h"
30 #include <assert.h>
33 char *WRasterLibVersion="0.9";
35 int RErrorCode=RERR_NONE;
38 #define HAS_ALPHA(I) ((I)->format == RRGBAFormat)
41 #define MAX_WIDTH 20000
42 #define MAX_HEIGHT 20000
43 /* 20000^2*4 < 2G */
46 RImage*
47 RCreateImage(unsigned width, unsigned height, int alpha)
49 RImage *image=NULL;
51 assert(width>0 && height>0);
53 if (width > MAX_WIDTH || height > MAX_HEIGHT) {
54 RErrorCode = RERR_NOMEMORY;
55 return NULL;
58 image = malloc(sizeof(RImage));
59 if (!image) {
60 RErrorCode = RERR_NOMEMORY;
61 return NULL;
64 memset(image, 0, sizeof(RImage));
65 image->width = width;
66 image->height = height;
67 image->format = alpha ? RRGBAFormat : RRGBFormat;
68 image->refCount = 1;
70 /* the +4 is to give extra bytes at the end of the buffer,
71 * so that we can optimize image conversion for MMX(tm).. see convert.c
73 image->data = malloc(width * height * (alpha ? 4 : 3) + 4);
74 if (!image->data) {
75 RErrorCode = RERR_NOMEMORY;
76 free(image);
77 image = NULL;
80 return image;
85 RImage*
86 RRetainImage(RImage *image)
88 if (image)
89 image->refCount++;
91 return image;
95 void
96 RReleaseImage(RImage *image)
98 assert(image!=NULL);
100 image->refCount--;
102 if (image->refCount < 1) {
103 free(image->data);
104 free(image);
109 RImage*
110 RCloneImage(RImage *image)
112 RImage *new_image;
114 assert(image!=NULL);
116 new_image = RCreateImage(image->width, image->height, HAS_ALPHA(image));
117 if (!new_image)
118 return NULL;
120 new_image->background = image->background;
121 memcpy(new_image->data, image->data,
122 image->width*image->height*(HAS_ALPHA(image) ? 4 : 3));
124 return new_image;
128 RImage*
129 RGetSubImage(RImage *image, int x, int y, unsigned width, unsigned height)
131 int i, ofs;
132 RImage *new_image;
133 unsigned total_line_size, line_size;
135 assert(image!=NULL);
136 assert(x>=0 && y>=0);
137 assert(x<image->width && y<image->height);
138 assert(width>0 && height>0);
140 if (x+width > image->width)
141 width = image->width-x;
142 if (y+height > image->height)
143 height = image->height-y;
145 new_image = RCreateImage(width, height, HAS_ALPHA(image));
147 if (!new_image)
148 return NULL;
149 new_image->background = image->background;
151 total_line_size = image->width * (HAS_ALPHA(image) ? 4 : 3);
152 line_size = width * (HAS_ALPHA(image) ? 4 : 3);
154 ofs = x*(HAS_ALPHA(image) ? 4 : 3) + y*total_line_size;;
156 for (i=0; i<height; i++) {
157 memcpy(&new_image->data[i*line_size],
158 &image->data[i*total_line_size+ofs], line_size);
160 return new_image;
165 *----------------------------------------------------------------------
166 * RCombineImages-
167 * Combines two equal sized images with alpha image. The second
168 * image will be placed on top of the first one.
169 *----------------------------------------------------------------------
171 void
172 RCombineImages(RImage *image, RImage *src)
174 assert(image->width == src->width);
175 assert(image->height == src->height);
177 if (!HAS_ALPHA(src)) {
178 if (!HAS_ALPHA(image)) {
179 memcpy(image->data, src->data, image->height*image->width*3);
180 } else {
181 int x, y;
182 unsigned char *d, *s;
184 d = image->data;
185 s = src->data;
186 for (y = 0; y < image->height; y++) {
187 for (x = 0; x < image->width; x++) {
188 *d++ = *s++;
189 *d++ = *s++;
190 *d++ = *s++;
191 d++;
195 } else {
196 register int i;
197 unsigned char *d;
198 unsigned char *s;
199 int alpha, calpha;
201 d = image->data;
202 s = src->data;
204 if (!HAS_ALPHA(image)) {
205 for (i=0; i<image->height*image->width; i++) {
206 alpha = *(s+3);
207 calpha = 255 - alpha;
208 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
209 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
210 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
211 s++;
213 } else {
214 for (i=0; i<image->height*image->width; i++) {
215 alpha = *(s+3);
216 calpha = 255 - alpha;
217 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
218 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
219 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; d++; s++;
220 *d++ |= *s++;
229 void
230 RCombineImagesWithOpaqueness(RImage *image, RImage *src, int opaqueness)
232 register int i;
233 unsigned char *d;
234 unsigned char *s;
235 int c_opaqueness;
237 assert(image->width == src->width);
238 assert(image->height == src->height);
240 d = image->data;
241 s = src->data;
243 c_opaqueness = 255 - opaqueness;
245 #define OP opaqueness
246 #define COP c_opaqueness
248 if (!HAS_ALPHA(src)) {
249 int dalpha = HAS_ALPHA(image);
250 for (i=0; i < image->width*image->height; i++) {
251 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; d++; s++;
252 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; d++; s++;
253 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; d++; s++;
254 if (dalpha) {
255 d++;
258 } else {
259 int tmp;
261 if (!HAS_ALPHA(image)) {
262 for (i=0; i<image->width*image->height; i++) {
263 tmp = (*(s+3) * opaqueness)/256;
264 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
265 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
266 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
267 s++;
269 } else {
270 for (i=0; i<image->width*image->height; i++) {
271 tmp = (*(s+3) * opaqueness)/256;
272 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
273 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
274 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
275 *d |= tmp;
276 d++; s++;
280 #undef OP
281 #undef COP
285 calculateCombineArea(RImage *des, RImage *src, int *sx, int *sy,
286 int *swidth, int *sheight, int *dx, int *dy)
288 if (*dx < 0) {
289 *sx = -*dx;
290 *swidth = *swidth + *dx;
291 *dx = 0;
294 if (*dx + *swidth > des->width) {
295 *swidth = des->width - *dx;
298 if (*dy < 0) {
299 *sy = -*dy;
300 *sheight = *sheight + *dy;
301 *dy = 0;
304 if (*dy + *sheight > des->height) {
305 *sheight = des->height - *dy;
308 if (*sheight > 0 && *swidth > 0) {
309 return True;
310 } else return False;
313 void
314 RCombineArea(RImage *image, RImage *src, int sx, int sy, unsigned width,
315 unsigned height, int dx, int dy)
317 int x, y, dwi, swi;
318 unsigned char *d;
319 unsigned char *s;
320 int alpha, calpha;
322 if(!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
323 return;
325 if (!HAS_ALPHA(src)) {
326 if (!HAS_ALPHA(image)) {
327 swi = src->width * 3;
328 dwi = image->width * 3;
330 s = src->data + (sy*(int)src->width + sx) * 3;
331 d = image->data + (dy*(int)image->width + dx) * 3;
333 for (y=0; y < height; y++) {
334 memcpy(d, s, width*3);
335 d += dwi;
336 s += swi;
338 } else {
339 swi = (src->width - width) * 3;
340 dwi = (image->width - width) * 4;
342 s = src->data + (sy*(int)src->width + sx) * 3;
343 d = image->data + (dy*(int)image->width + dx) * 4;
345 for (y=0; y < height; y++) {
346 for (x=0; x < width; x++) {
347 *d++ = *s++;
348 *d++ = *s++;
349 *d++ = *s++;
350 d++;
352 d += dwi;
353 s += swi;
356 } else {
357 int dalpha = HAS_ALPHA(image);
359 swi = (src->width - width) * 4;
360 s = src->data + (sy*(int)src->width + sx) * 4;
361 if (dalpha) {
362 dwi = (image->width - width) * 4;
363 d = image->data + (dy*(int)image->width + dx) * 4;
364 } else {
365 dwi = (image->width - width) * 3;
366 d = image->data + (dy*(int)image->width + dx) * 3;
369 for (y=0; y < height; y++) {
370 for (x=0; x < width; x++) {
371 alpha = *(s+3);
372 calpha = 255 - alpha;
373 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; s++; d++;
374 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; s++; d++;
375 *d = (((int)*d * calpha) + ((int)*s * alpha))/256; s++; d++;
376 s++;
377 if (dalpha)
378 d++;
380 d += dwi;
381 s += swi;
387 void
388 RCombineAreaWithOpaqueness(RImage *image, RImage *src, int sx, int sy,
389 unsigned width, unsigned height, int dx, int dy,
390 int opaqueness)
392 int x, y, dwi, swi;
393 int c_opaqueness;
394 unsigned char *s, *d;
395 int dalpha = HAS_ALPHA(image);
396 int dch = (dalpha ? 4 : 3);
398 if(!calculateCombineArea(image, src, &sx, &sy, &width, &height, &dx, &dy))
399 return;
401 d = image->data + (dy*image->width + dx) * dch;
402 dwi = (image->width - width)*dch;
404 c_opaqueness = 255 - opaqueness;
406 #define OP opaqueness
407 #define COP c_opaqueness
409 if (!HAS_ALPHA(src)) {
411 s = src->data + (sy*src->width + sx)*3;
412 swi = (src->width - width) * 3;
414 for (y=0; y < height; y++) {
415 for (x=0; x < width; x++) {
416 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; s++; d++;
417 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; s++; d++;
418 *d = (((int)*d *(int)COP) + ((int)*s *(int)OP))/256; s++; d++;
419 if (dalpha)
420 d++;
422 d += dwi; s += swi;
424 } else {
425 int tmp;
427 s = src->data + (sy*src->width + sx)*4;
428 swi = (src->width - width) * 4;
430 for (y=0; y < height; y++) {
431 for (x=0; x < width; x++) {
432 tmp = (*(s+3) * opaqueness)/256;
433 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
434 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
435 *d = (((int)*d * (255-tmp)) + ((int)*s * tmp))/256; d++; s++;
436 s++;
437 if (dalpha)
438 d++;
440 d += dwi; s += swi;
443 #undef OP
444 #undef COP
449 void
450 RCombineImageWithColor(RImage *image, RColor *color)
452 register int i;
453 unsigned char *d;
454 int alpha, nalpha, r, g, b;
456 d = image->data;
458 if (!HAS_ALPHA(image)) {
459 /* Image has no alpha channel, so we consider it to be all 255.
460 * Thus there are no transparent parts to be filled. */
461 return;
463 r = color->red;
464 g = color->green;
465 b = color->blue;
467 for (i=0; i < image->width*image->height; i++) {
468 alpha = *(d+3);
469 nalpha = 255 - alpha;
471 *d = (((int)*d * alpha) + (r * nalpha))/256; d++;
472 *d = (((int)*d * alpha) + (g * nalpha))/256; d++;
473 *d = (((int)*d * alpha) + (b * nalpha))/256; d++;
474 d++;
481 RImage*
482 RMakeTiledImage(RImage *tile, unsigned width, unsigned height)
484 int x, y;
485 unsigned w;
486 unsigned long tile_size = tile->width * tile->height;
487 unsigned long tx = 0;
488 RImage *image;
489 unsigned char *s, *d;
491 if (width == tile->width && height == tile->height)
492 image = RCloneImage(tile);
493 else if (width <= tile->width && height <= tile->height)
494 image = RGetSubImage(tile, 0, 0, width, height);
495 else {
496 int has_alpha = HAS_ALPHA(tile);
498 image = RCreateImage(width, height, has_alpha);
500 d = image->data;
501 s = tile->data;
503 for (y = 0; y < height; y++) {
504 for (x = 0; x < width; x += tile->width) {
506 w = (width - x < tile->width) ? width - x : tile->width;
508 if (has_alpha) {
509 w *= 4;
510 memcpy(d, s+tx*4, w);
511 } else {
512 w *= 3;
513 memcpy(d, s+tx*3, w);
515 d += w;
518 tx = (tx + tile->width) % tile_size;
521 return image;
525 RImage*
526 RMakeCenteredImage(RImage *image, unsigned width, unsigned height, RColor *color)
528 int x, y, w, h, sx, sy;
529 RImage *tmp;
531 tmp = RCreateImage(width, height, False);
532 if (!tmp) {
533 return NULL;
536 RClearImage(tmp, color);
538 if (image->height < height) {
539 h = image->height;
540 y = (height - h)/2;
541 sy = 0;
542 } else {
543 sy = (image->height - height)/2;
544 y = 0;
545 h = height;
547 if (image->width < width) {
548 w = image->width;
549 x = (width - w)/2;
550 sx = 0;
551 } else {
552 sx = (image->width - width)/2;
553 x = 0;
554 w = width;
556 RCombineArea(tmp, image, sx, sy, w, h, x, y);
558 return tmp;