~ubuntu-branches/ubuntu/jaunty/kaa-imlib2/jaunty

« back to all changes in this revision

Viewing changes to src/rawformats.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeremie Corbier
  • Date: 2007-05-29 12:48:02 UTC
  • Revision ID: james.westby@ubuntu.com-20070529124802-7rqvjajce01xpyly
Tags: upstream-0.2.1
ImportĀ upstreamĀ versionĀ 0.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ----------------------------------------------------------------------------
 
3
 * Imlib2 wrapper for Python
 
4
 * ----------------------------------------------------------------------------
 
5
 * $Id: rawformats.c 2604 2007-03-28 09:21:52Z dmeyer $
 
6
 *
 
7
 * ----------------------------------------------------------------------------
 
8
 * kaa.imlib2 - An imlib2 wrapper for Python
 
9
 * Copyright (C) 2004-2006 Jason Tackaberry <tack@urandom.ca>
 
10
 *
 
11
 * First Edition: Jason Tackaberry <tack@urandom.ca>
 
12
 * Maintainer:    Jason Tackaberry <tack@urandom.ca>
 
13
 *
 
14
 * Please see the file AUTHORS for a complete list of authors.
 
15
 *
 
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.
 
19
 *
 
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.
 
24
 *
 
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
 
28
 * 02110-1301 USA
 
29
 *
 
30
 * ----------------------------------------------------------------------------
 
31
 */
 
32
 
 
33
#define X_DISPLAY_MISSING
 
34
#include <Imlib2.h>
 
35
#include <string.h>
 
36
#include <stdlib.h>
 
37
 
 
38
unsigned int get_format_bpp(char *format)
 
39
{
 
40
    if (strstr(format, "24"))
 
41
        return 3;
 
42
    else if (strstr(format, "32"))
 
43
        return 4;
 
44
    else
 
45
        return strlen(format);
 
46
}
 
47
 
 
48
unsigned int get_raw_bytes_size(char *format)
 
49
{
 
50
    unsigned int w = imlib_image_get_width();
 
51
    unsigned int h = imlib_image_get_height();
 
52
 
 
53
    return w * h * get_format_bpp(format);
 
54
}
 
55
 
 
56
 
 
57
 
 
58
unsigned char* convert_raw_rgba_bytes(char *from_format, char *to_format,
 
59
                                      unsigned char *from_buf,
 
60
                                      unsigned char *to_buf,
 
61
                                      int w, int h)
 
62
{
 
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);
 
67
 
 
68
    if (to_buf == 0)
 
69
        to_buf = (unsigned char *)malloc(w*h*to_bpp);
 
70
 
 
71
#define LOOP_START \
 
72
    for (from_ptr = from_buf, to_ptr = to_buf; from_ptr < from_buf + \
 
73
           w*h*from_bpp; from_ptr += from_bpp)
 
74
 
 
75
 
 
76
    // FIXME: pointless code duplication follows.
 
77
 
 
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
 
80
     * MMX.
 
81
     */
 
82
    if (!strcmp(from_format, "BGRA") && !strcmp(to_format, "RGB")) {
 
83
        LOOP_START {
 
84
            *(to_ptr++) = *(from_ptr + 2); *(to_ptr++) = *(from_ptr + 1);
 
85
            *(to_ptr++) = *(from_ptr + 0);
 
86
        }
 
87
    return to_buf;
 
88
    }
 
89
    if (!strcmp(from_format, "BGRA") && !strcmp(to_format, "RGBA")) {
 
90
        LOOP_START {
 
91
            *(to_ptr++) = *(from_ptr + 2); *(to_ptr++) = *(from_ptr + 1);
 
92
            *(to_ptr++) = *(from_ptr + 0); *(to_ptr++) = *(from_ptr + 3);
 
93
        }
 
94
    return to_buf;
 
95
    }
 
96
 
 
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;
 
101
 
 
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;
 
107
    }
 
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;
 
113
    }
 
114
 
 
115
    LOOP_START {
 
116
        *(to_ptr + tr) = *(from_ptr + fr);
 
117
        *(to_ptr + tg) = *(from_ptr + fg);
 
118
        *(to_ptr + tb) = *(from_ptr + fb);
 
119
        if (to_bpp == 4)
 
120
            *(to_ptr + ta) = (from_bpp==4)?*(from_ptr + fa):255;
 
121
 
 
122
        to_ptr += to_bpp;
 
123
    }
 
124
    return to_buf;
 
125
}
 
126
 
 
127
 
 
128
unsigned char* get_raw_bytes(char *format, unsigned char *dstbuf)
 
129
{
 
130
    unsigned int w, h, bufsize;
 
131
    unsigned char *srcbuf;
 
132
 
 
133
    w = imlib_image_get_width(),
 
134
    h = imlib_image_get_height(),
 
135
    bufsize = get_raw_bytes_size(format);
 
136
 
 
137
    imlib_image_set_has_alpha(1);
 
138
    srcbuf = (unsigned char *)imlib_image_get_data_for_reading_only();
 
139
    if (dstbuf == 0)
 
140
        dstbuf = (unsigned char *)malloc(bufsize);
 
141
 
 
142
    if (!strcmp(format, "BGRA"))
 
143
        memcpy(dstbuf, srcbuf, bufsize);
 
144
    else
 
145
        dstbuf = convert_raw_rgba_bytes("BGRA", format, srcbuf, dstbuf, w, h);
 
146
    return dstbuf;
 
147
}