2
* ----------------------------------------------------------------------------
3
* Imlib2 wrapper for Python
4
* ----------------------------------------------------------------------------
5
* $Id: rawformats.c 2604 2007-03-28 09:21:52Z dmeyer $
7
* ----------------------------------------------------------------------------
8
* kaa.imlib2 - An imlib2 wrapper for Python
9
* Copyright (C) 2004-2006 Jason Tackaberry <tack@urandom.ca>
11
* First Edition: Jason Tackaberry <tack@urandom.ca>
12
* Maintainer: Jason Tackaberry <tack@urandom.ca>
14
* Please see the file AUTHORS for a complete list of authors.
16
* This library is free software; you can redistribute it and/or modify
17
* it under the terms of the GNU Lesser General Public License version
18
* 2.1 as published by the Free Software Foundation.
20
* This library is distributed in the hope that it will be useful, but
21
* WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
* Lesser General Public License for more details.
25
* You should have received a copy of the GNU Lesser General Public
26
* License along with this library; if not, write to the Free Software
27
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30
* ----------------------------------------------------------------------------
33
#define X_DISPLAY_MISSING
38
unsigned int get_format_bpp(char *format)
40
if (strstr(format, "24"))
42
else if (strstr(format, "32"))
45
return strlen(format);
48
unsigned int get_raw_bytes_size(char *format)
50
unsigned int w = imlib_image_get_width();
51
unsigned int h = imlib_image_get_height();
53
return w * h * get_format_bpp(format);
58
unsigned char* convert_raw_rgba_bytes(char *from_format, char *to_format,
59
unsigned char *from_buf,
60
unsigned char *to_buf,
63
int from_bpp, to_bpp, i;
64
unsigned char fr, fb, fg, fa, tr, tb, tg, ta, *from_ptr, *to_ptr;
65
from_bpp = get_format_bpp(from_format);
66
to_bpp = get_format_bpp(to_format);
69
to_buf = (unsigned char *)malloc(w*h*to_bpp);
72
for (from_ptr = from_buf, to_ptr = to_buf; from_ptr < from_buf + \
73
w*h*from_bpp; from_ptr += from_bpp)
76
// FIXME: pointless code duplication follows.
78
/* Hard code the common cases of BGRA -> RGB/A. This is pretty much
79
* as fast as memcpy. I don't think it gets much faster without
82
if (!strcmp(from_format, "BGRA") && !strcmp(to_format, "RGB")) {
84
*(to_ptr++) = *(from_ptr + 2); *(to_ptr++) = *(from_ptr + 1);
85
*(to_ptr++) = *(from_ptr + 0);
89
if (!strcmp(from_format, "BGRA") && !strcmp(to_format, "RGBA")) {
91
*(to_ptr++) = *(from_ptr + 2); *(to_ptr++) = *(from_ptr + 1);
92
*(to_ptr++) = *(from_ptr + 0); *(to_ptr++) = *(from_ptr + 3);
97
// Initialize these values to shut the compiler up during -Wall. We
98
// don't bother checking the validity of to_format and from_format
99
// because the python wrapper ensures they're valid.
100
tr = tg = tb = ta = fr = fg = fb = fa = 0;
102
for (i = 0; i < to_bpp; i ++) {
103
if (to_format[i] == 'R') tr = i;
104
else if (to_format[i] == 'G') tg = i;
105
else if (to_format[i] == 'B') tb = i;
106
else if (to_format[i] == 'A') ta = i;
108
for (i = 0; i < from_bpp; i ++) {
109
if (from_format[i] == 'R') fr = i;
110
else if (from_format[i] == 'G') fg = i;
111
else if (from_format[i] == 'B') fb = i;
112
else if (from_format[i] == 'A') fa = i;
116
*(to_ptr + tr) = *(from_ptr + fr);
117
*(to_ptr + tg) = *(from_ptr + fg);
118
*(to_ptr + tb) = *(from_ptr + fb);
120
*(to_ptr + ta) = (from_bpp==4)?*(from_ptr + fa):255;
128
unsigned char* get_raw_bytes(char *format, unsigned char *dstbuf)
130
unsigned int w, h, bufsize;
131
unsigned char *srcbuf;
133
w = imlib_image_get_width(),
134
h = imlib_image_get_height(),
135
bufsize = get_raw_bytes_size(format);
137
imlib_image_set_has_alpha(1);
138
srcbuf = (unsigned char *)imlib_image_get_data_for_reading_only();
140
dstbuf = (unsigned char *)malloc(bufsize);
142
if (!strcmp(format, "BGRA"))
143
memcpy(dstbuf, srcbuf, bufsize);
145
dstbuf = convert_raw_rgba_bytes("BGRA", format, srcbuf, dstbuf, w, h);