File: xview.cpp

package info (click to toggle)
kde4libs 4%3A4.14.2-5%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 82,428 kB
  • ctags: 99,415
  • sloc: cpp: 761,864; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (259 lines) | stat: -rw-r--r-- 6,366 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* QImageIO Routines to read/write XV images.
* copyright (c) 1998 Torben Weis <weis@kde.org>
* copyright (c) 1999 Oliver Eiden <o.eiden@pop.ruhr.de>
*
* This library is distributed under the conditions of the GNU LGPL.
*/

#include "xview.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <QtGui/QImage>

#define BUFSIZE 1024

static const int b_255_3[]= {0,85,170,255},  // index*255/3
           rg_255_7[]={0,36,72,109,145,182,218,255}; // index *255/7


XVHandler::XVHandler()
{
}

bool XVHandler::canRead() const
{
    if (canRead(device())) {
        setFormat("xv");
        return true;
    }
    return false;
}

bool XVHandler::read(QImage *retImage)
{
    int x=-1;
    int y=-1;
    int maxval=-1;
    QIODevice *iodev = device();

    char str[ BUFSIZE ];

    // magic number must be "P7 332"
    iodev->readLine( str, BUFSIZE );
    if (strncmp(str,"P7 332",6))
        return false;

    // next line #XVVERSION
    iodev->readLine( str, BUFSIZE );
    if (strncmp(str, "#XVVERSION", 10))
        return false;

    // now it gets interesting, #BUILTIN means we are out.
    // if IMGINFO comes, we are happy!
    iodev->readLine( str, BUFSIZE );
    if (strncmp(str, "#IMGINFO:", 9))
        return false;

    // after this an #END_OF_COMMENTS signals everything to be ok!
    iodev->readLine( str, BUFSIZE );
    if (strncmp(str, "#END_OF", 7))
        return false;

    // now a last line with width, height, maxval which is
    // supposed to be 255
    iodev->readLine( str, BUFSIZE );
    sscanf(str, "%d %d %d", &x, &y, &maxval);

    if (maxval != 255)
        return false;
    int blocksize = x*y;
    if(x < 0 || y < 0 || blocksize < x || blocksize < y)
        return false;

    // now follows a binary block of x*y bytes.
    char *block = (char*) malloc(blocksize);
    if(!block)
        return false;

    if (iodev->read(block, blocksize) != blocksize )
    {
	free(block);
        return false;
    }

    // Create the image
    QImage image( x, y, QImage::Format_Indexed8 );
    int numColors;
    numColors = qMin( maxval + 1, 0 );
    numColors = qMax( 0, maxval + 1 );
    image.setNumColors( numColors );

    // how do the color handling? they are absolute 24bpp
    // or at least can be calculated as such.
    int r,g,b;

    for ( int j = 0; j < 256; j++ )
    {
        r =  rg_255_7[((j >> 5) & 0x07)];
        g =  rg_255_7[((j >> 2) & 0x07)];
        b =  b_255_3[((j >> 0) & 0x03)];
        image.setColor( j, qRgb( r, g, b ) );
    }

    for ( int py = 0; py < y; py++ )
    {
        uchar *data = image.scanLine( py );
        memcpy( data, block + py * x, x );
    }

    *retImage = image;

    free(block);
    return true;
}

bool XVHandler::write(const QImage &image)
{
    QIODevice& f = *( device() );

    // Removed "f.open(...)" and "f.close()" (tanghus)

    int w = image.width(), h = image.height();

    char str[ 1024 ];

    // magic number must be "P7 332"
    f.write( "P7 332\n", 7 );

    // next line #XVVERSION
    f.write( "#XVVERSION:\n", 12 );

    // now it gets interesting, #BUILTIN means we are out.
    // if IMGINFO comes, we are happy!
    f.write( "#IMGINFO:\n", 10 );

    // after this an #END_OF_COMMENTS signals everything to be ok!
    f.write( "#END_OF_COMMENTS:\n", 18 );

    // now a last line with width, height, maxval which is supposed to be 255
    sprintf( str, "%i %i 255\n", w, h );
    f.write( str, strlen( str ) );


    QImage tmpImage( image );
    if ( image.depth() == 1 )
        tmpImage = image.convertToFormat( QImage::Format_Indexed8, Qt::AutoColor );

    uchar* buffer = new uchar[ w ];

    for ( int py = 0; py < h; py++ )
    {
        const uchar *data = tmpImage.scanLine( py );
        for ( int px = 0; px < w; px++ )
        {
            int r, g, b;
            if ( tmpImage.depth() == 32 )
            {
                const QRgb *data32 = (QRgb*) data;
                r = qRed( *data32 ) >> 5;
                g = qGreen( *data32 ) >> 5;
                b = qBlue( *data32 ) >> 6;
                data += sizeof( QRgb );
            }
            else
            {
                QRgb color = tmpImage.color( *data );
                r = qRed( color ) >> 5;
                g = qGreen( color ) >> 5;
                b = qBlue( color ) >> 6;
                data++;
            }
            buffer[ px ] = ( r << 5 ) | ( g << 2 ) | b;
        }
        f.write( (const char*)buffer, w );
    }
    delete[] buffer;

    return true;
}

QByteArray XVHandler::name() const
{
    return "xv";
}

bool XVHandler::canRead(QIODevice *device)
{
     if (!device) {
        qWarning("XVHandler::canRead() called with no device");
        return false;
    }

    qint64 oldPos = device->pos();

    char head[6];
    qint64 readBytes = device->read(head, sizeof(head));
    if (readBytes != sizeof(head)) {
        if (device->isSequential()) {
            while (readBytes > 0)
                device->ungetChar(head[readBytes-- - 1]);
        } else {
            device->seek(oldPos);
        }
        return false;
    }

    if (device->isSequential()) {
        while (readBytes > 0)
            device->ungetChar(head[readBytes-- - 1]);
    } else {
        device->seek(oldPos);
    }

    return qstrncmp(head, "P7 332", 6) == 0;
}


class XVPlugin : public QImageIOPlugin
{
public:
    QStringList keys() const;
    Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
    QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};

QStringList XVPlugin::keys() const
{
    return QStringList() << "xv";
}

QImageIOPlugin::Capabilities XVPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
    if (format == "xv")
        return Capabilities(CanRead | CanWrite);
    if (!format.isEmpty())
        return 0;
    if (!device->isOpen())
        return 0;

    Capabilities cap;
    if (device->isReadable() && XVHandler::canRead(device))
        cap |= CanRead;
    if (device->isWritable())
        cap |= CanWrite;
    return cap;
}

QImageIOHandler *XVPlugin::create(QIODevice *device, const QByteArray &format) const
{
    QImageIOHandler *handler = new XVHandler;
    handler->setDevice(device);
    handler->setFormat(format);
    return handler;
}

Q_EXPORT_STATIC_PLUGIN(XVPlugin)
Q_EXPORT_PLUGIN2(xv, XVPlugin)