~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/image/qxbmhandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the painting module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "private/qxbmhandler_p.h"
 
30
 
 
31
#include <qimage.h>
 
32
#include <qiodevice.h>
 
33
#include <qvariant.h>
 
34
 
 
35
#include <stdio.h>
 
36
#include <ctype.h>
 
37
 
 
38
 
 
39
/*****************************************************************************
 
40
  X bitmap image read/write functions
 
41
 *****************************************************************************/
 
42
 
 
43
static inline int hex2byte(register char *p)
 
44
{
 
45
    return ((isdigit((uchar) *p) ? *p - '0' : toupper((uchar) *p) - 'A' + 10) << 4) |
 
46
           (isdigit((uchar) *(p+1)) ? *(p+1) - '0' : toupper((uchar) *(p+1)) - 'A' + 10);
 
47
}
 
48
 
 
49
static bool read_xbm_image(QIODevice *device, QImage *outImage)
 
50
{
 
51
    const int buflen = 300;
 
52
    char buf[buflen + 1];
 
53
    QRegExp r1(QLatin1String("^#define[ \t]+[a-zA-Z0-9._]+[ \t]+"));
 
54
    QRegExp r2(QLatin1String("[0-9]+"));
 
55
    int w = -1, h = -1;
 
56
    QImage image;
 
57
 
 
58
    qint64 readBytes = 0;
 
59
 
 
60
    // "#define .._width <num>"
 
61
    readBytes = device->readLine(buf, buflen);
 
62
    if (readBytes == -1)
 
63
        return false;
 
64
    buf[readBytes] = '\0';
 
65
 
 
66
    // skip initial comment, if any
 
67
    while (buf[0] != '#' && (readBytes = device->readLine( buf, buflen )) > 0);
 
68
 
 
69
    if (readBytes == -1)
 
70
        return false;
 
71
    buf[readBytes] = '\0';
 
72
    QString sbuf;
 
73
    sbuf = QString::fromLatin1(buf);
 
74
 
 
75
    if (r1.indexIn(sbuf) == 0 &&
 
76
         r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength())
 
77
        w = QString(&buf[r1.matchedLength()]).toInt();
 
78
 
 
79
    // "#define .._height <num>"
 
80
    readBytes = device->readLine(buf, buflen);
 
81
    if (readBytes == -1)
 
82
        return false;
 
83
    buf[readBytes] = '\0';
 
84
 
 
85
    sbuf = QString::fromLatin1(buf);
 
86
 
 
87
    if (r1.indexIn(sbuf) == 0 &&
 
88
         r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength())
 
89
        h = QString(&buf[r1.matchedLength()]).toInt();
 
90
 
 
91
    // format error
 
92
    if (w <= 0 || w > 32767 || h <= 0 || h > 32767)
 
93
        return false;
 
94
 
 
95
    // scan for database
 
96
    for (;;) {
 
97
        if ((readBytes = device->readLine(buf, buflen)) <= 0) {
 
98
            // end of file
 
99
            return false;
 
100
        }
 
101
 
 
102
        buf[readBytes] = '\0';
 
103
        if (QByteArray::fromRawData(buf, readBytes).contains("0x"))
 
104
            break;
 
105
    }
 
106
 
 
107
    image = QImage(w, h, QImage::Format_MonoLSB);
 
108
    if (image.isNull())
 
109
        return false;
 
110
 
 
111
    image.setNumColors(2);
 
112
    image.setColor(0, qRgb(255,255,255));        // white
 
113
    image.setColor(1, qRgb(0,0,0));                // black
 
114
 
 
115
    int           x = 0, y = 0;
 
116
    uchar *b = image.scanLine(0);
 
117
    char  *p = buf + QByteArray::fromRawData(buf, readBytes).indexOf("0x");
 
118
    w = (w+7)/8;                                // byte width
 
119
 
 
120
    while (y < h) {                                // for all encoded bytes...
 
121
        if (p) {                                // p = "0x.."
 
122
            *b++ = hex2byte(p+2);
 
123
            p += 2;
 
124
            if (++x == w && ++y < h) {
 
125
                b = image.scanLine(y);
 
126
                x = 0;
 
127
            }
 
128
            p = strstr(p, "0x");
 
129
        } else {                                // read another line
 
130
            if ((readBytes = device->readLine(buf,buflen)) <= 0)        // EOF ==> truncated image
 
131
                break;
 
132
            p = buf + QByteArray::fromRawData(buf, readBytes).indexOf("0x");
 
133
        }
 
134
    }
 
135
 
 
136
    *outImage = image;
 
137
    return true;
 
138
}
 
139
 
 
140
static bool write_xbm_image(const QImage &sourceImage, QIODevice *device, const QString &fileName)
 
141
{
 
142
    QImage image = sourceImage;
 
143
    int        w = image.width();
 
144
    int        h = image.height();
 
145
    int        i;
 
146
    QString    s = fileName; // get file base name
 
147
    char *buf = new char[s.length() + 100];
 
148
 
 
149
    sprintf(buf, "#define %s_width %d\n", s.toAscii().data(), w);
 
150
    device->write(buf, qstrlen(buf));
 
151
    sprintf(buf, "#define %s_height %d\n", s.toAscii().data(), h);
 
152
    device->write(buf, qstrlen(buf));
 
153
    sprintf(buf, "static char %s_bits[] = {\n ", s.toAscii().data());
 
154
    device->write(buf, qstrlen(buf));
 
155
 
 
156
    if (image.format() != QImage::Format_MonoLSB)
 
157
        image = image.convertToFormat(QImage::Format_MonoLSB);
 
158
 
 
159
    bool invert = qGray(image.color(0)) < qGray(image.color(1));
 
160
    char hexrep[16];
 
161
    for (i=0; i<10; i++)
 
162
        hexrep[i] = '0' + i;
 
163
    for (i=10; i<16; i++)
 
164
        hexrep[i] = 'a' -10 + i;
 
165
    if (invert) {
 
166
        char t;
 
167
        for (i=0; i<8; i++) {
 
168
            t = hexrep[15-i];
 
169
            hexrep[15-i] = hexrep[i];
 
170
            hexrep[i] = t;
 
171
        }
 
172
    }
 
173
    int bcnt = 0;
 
174
    register char *p = buf;
 
175
    int bpl = (w+7)/8;
 
176
    for (int y = 0; y < h; ++y) {
 
177
        uchar *b = image.scanLine(y);
 
178
        for (i = 0; i < bpl; ++i) {
 
179
            *p++ = '0'; *p++ = 'x';
 
180
            *p++ = hexrep[*b >> 4];
 
181
            *p++ = hexrep[*b++ & 0xf];
 
182
 
 
183
            if (i < bpl - 1 || y < h - 1) {
 
184
                *p++ = ',';
 
185
                if (++bcnt > 14) {
 
186
                    *p++ = '\n';
 
187
                    *p++ = ' ';
 
188
                    *p   = '\0';
 
189
                    if ((int)qstrlen(buf) != device->write(buf, qstrlen(buf))) {
 
190
                        delete [] buf;
 
191
                        return false;
 
192
                    }
 
193
                    p = buf;
 
194
                    bcnt = 0;
 
195
                }
 
196
            }
 
197
        }
 
198
    }
 
199
    strcpy(p, " };\n");
 
200
    if ((int)qstrlen(buf) != device->write(buf, qstrlen(buf))) {
 
201
        delete [] buf;
 
202
        return false;
 
203
    }
 
204
 
 
205
    delete [] buf;
 
206
    return true;
 
207
}
 
208
 
 
209
bool QXbmHandler::canRead() const
 
210
{
 
211
    return canRead(device());
 
212
}
 
213
 
 
214
bool QXbmHandler::canRead(QIODevice *device)
 
215
{
 
216
    QImage image;
 
217
 
 
218
    // it's impossible to tell whether we can load an XBM or not when
 
219
    // it's from a sequential device, as the only way to do it is to
 
220
    // attempt to parse the whole image.
 
221
    if (device->isSequential())
 
222
        return false;
 
223
 
 
224
    qint64 oldPos = device->pos();
 
225
    bool success = read_xbm_image(device, &image);
 
226
    device->seek(oldPos);
 
227
 
 
228
    return success;
 
229
}
 
230
 
 
231
bool QXbmHandler::read(QImage *image)
 
232
{
 
233
    return read_xbm_image(device(), image);
 
234
}
 
235
 
 
236
bool QXbmHandler::write(const QImage &image)
 
237
{
 
238
    return write_xbm_image(image, device(), fileName);
 
239
}
 
240
 
 
241
bool QXbmHandler::supportsOption(ImageOption option) const
 
242
{
 
243
    return option == Name;
 
244
}
 
245
 
 
246
QVariant QXbmHandler::option(ImageOption option) const
 
247
{
 
248
    return option == Name ? fileName : QString();
 
249
}
 
250
 
 
251
void QXbmHandler::setOption(ImageOption option, const QVariant &value)
 
252
{
 
253
    if (option == Name)
 
254
        fileName = value.toString();
 
255
}
 
256
 
 
257
QByteArray QXbmHandler::name() const
 
258
{
 
259
    return "xbm";
 
260
}