12 #include <QtGui/QImage>
30 for (
int i=0; i<16; ++i )
38 quint8 m, ver, enc, bpp;
39 s >> m >> ver >> enc >> bpp;
44 quint16 xmin, ymin, xmax, ymax;
45 s >> xmin >> ymin >> xmax >> ymax;
56 s >> colorMap >> res >> np;
64 quint16 hscreensize, vscreensize;
70 while ( s.device()->pos() < 128 )
78 s << rgb.
r << rgb.
g << rgb.
b;
85 for (
int i=0; i<16; ++i )
108 for (
int i=0; i<54; ++i )
117 QByteArray dummy( 128, 0 );
119 QDataStream s( &dummy, QIODevice::ReadOnly );
126 quint32 size = buf.size();
141 while ( count-- && i < size )
160 img = QImage( header.
width(), header.
height(), QImage::Format_Mono );
161 img.setNumColors( 2 );
163 for (
int y=0; y<header.
height(); ++y )
172 uchar *p = img.scanLine( y );
174 for (
unsigned int x=0; x< bpl; ++x )
179 img.setColor( 0, qRgb( 0, 0, 0 ) );
180 img.setColor( 1, qRgb( 255, 255, 255 ) );
186 QByteArray pixbuf( header.
width(), 0 );
188 img = QImage( header.
width(), header.
height(), QImage::Format_Indexed8 );
189 img.setNumColors( 16 );
191 for (
int y=0; y<header.
height(); ++y )
202 for (
int i=0; i<4; i++ )
205 for (
int x=0; x<header.
width(); ++x )
206 if ( buf[ offset + ( x/8 ) ] & ( 128 >> ( x%8 ) ) )
207 pixbuf[ x ] = (
int)(pixbuf[ x ]) + ( 1 << i );
210 uchar *p = img.scanLine( y );
211 for (
int x=0; x<header.
width(); ++x )
212 p[ x ] = pixbuf[ x ];
216 for (
int i=0; i<16; ++i )
224 img = QImage( header.
width(), header.
height(), QImage::Format_Indexed8 );
225 img.setNumColors( 256 );
227 for (
int y=0; y<header.
height(); ++y )
237 uchar *p = img.scanLine( y );
239 for (
unsigned int x=0; x<bpl; ++x )
245 kDebug( 399 ) <<
"Palette Flag: " << flag;
247 if ( flag == 12 && ( header.
Version == 5 || header.
Version == 2 ) )
251 for (
int i=0; i<256; ++i )
254 img.setColor( i, qRgb( r, g, b ) );
265 img = QImage( header.
width(), header.
height(), QImage::Format_RGB32 );
267 for (
int y=0; y<header.
height(); ++y )
279 uint *p = (
uint * )img.scanLine( y );
280 for (
int x=0; x<header.
width(); ++x )
281 p[ x ] = qRgb( r_buf[ x ], g_buf[ x ], b_buf[ x ] );
285 static void writeLine( QDataStream &s, QByteArray &buf )
288 quint32 size = buf.size();
297 while ( ( i < size ) && ( byte == buf[ i ] ) && ( count < 63 ) )
305 if ( count > 1 || data >= 0xc0 )
317 img = img.convertToFormat( QImage::Format_Mono );
325 QByteArray buf( header.BytesPerLine, 0 );
327 for (
int y=0; y<header.height(); ++y )
329 quint8 *p = img.scanLine( y );
332 for (
int i=0; i<header.BytesPerLine; ++i )
345 for (
int i=0; i<16; ++i )
352 for (
int i=0; i<4; ++i )
353 buf[ i ].resize( header.BytesPerLine );
355 for (
int y=0; y<header.height(); ++y )
357 quint8 *p = img.scanLine( y );
359 for (
int i=0; i<4; ++i )
362 for (
int x=0; x<header.width(); ++x )
364 for (
int i=0; i<4; ++i )
365 if ( *( p+x ) & ( 1 << i ) )
366 buf[ i ][ x/8 ] = (int)(buf[ i ][ x/8 ])| 1 << ( 7-x%8 );
369 for (
int i=0; i<4; ++i )
382 QByteArray buf( header.BytesPerLine, 0 );
384 for (
int y=0; y<header.height(); ++y )
386 quint8 *p = img.scanLine( y );
388 for (
int i=0; i<header.BytesPerLine; ++i )
399 for (
int i=0; i<256; ++i )
411 QByteArray r_buf( header.width(), 0 );
412 QByteArray g_buf( header.width(), 0 );
413 QByteArray b_buf( header.width(), 0 );
415 for (
int y=0; y<header.height(); ++y )
417 uint *p = (
uint * )img.scanLine( y );
419 for (
int x=0; x<header.width(); ++x )
422 r_buf[ x ] = qRed( rgb );
423 g_buf[ x ] = qGreen( rgb );
424 b_buf[ x ] = qBlue( rgb );
450 QDataStream s( device() );
451 s.setByteOrder( QDataStream::LittleEndian );
453 if ( s.device()->size() < 128 )
462 if ( header.Manufacturer != 10 || s.atEnd())
467 int w = header.
width();
468 int h = header.height();
470 kDebug( 399 ) <<
"Manufacturer: " << header.Manufacturer;
471 kDebug( 399 ) <<
"Version: " << header.Version;
472 kDebug( 399 ) <<
"Encoding: " << header.Encoding;
473 kDebug( 399 ) <<
"Bpp: " << header.Bpp;
474 kDebug( 399 ) <<
"Width: " << w;
475 kDebug( 399 ) <<
"Height: " << h;
476 kDebug( 399 ) <<
"Window: " << header.XMin <<
"," << header.XMax <<
","
477 << header.YMin <<
"," << header.YMax << endl;
478 kDebug( 399 ) <<
"BytesPerLine: " << header.BytesPerLine;
479 kDebug( 399 ) <<
"NPlanes: " << header.NPlanes;
483 if ( header.Bpp == 1 && header.NPlanes == 1 )
487 else if ( header.Bpp == 1 && header.NPlanes == 4 )
491 else if ( header.Bpp == 8 && header.NPlanes == 1 )
495 else if ( header.Bpp == 8 && header.NPlanes == 3 )
500 kDebug( 399 ) <<
"Image Bytes: " << img.numBytes();
501 kDebug( 399 ) <<
"Image Bytes Per Line: " << img.bytesPerLine();
502 kDebug( 399 ) <<
"Image Depth: " << img.depth();
517 QDataStream s( device() );
518 s.setByteOrder( QDataStream::LittleEndian );
523 int h = img.height();
525 kDebug( 399 ) <<
"Width: " << w;
526 kDebug( 399 ) <<
"Height: " << h;
527 kDebug( 399 ) <<
"Depth: " << img.depth();
528 kDebug( 399 ) <<
"BytesPerLine: " << img.bytesPerLine();
529 kDebug( 399 ) <<
"Num Colors: " << img.numColors();
545 if ( img.depth() == 1 )
549 else if ( img.depth() == 8 && img.numColors() <= 16 )
553 else if ( img.depth() == 8 )
557 else if ( img.depth() == 32 )
573 qWarning(
"PCXHandler::canRead() called with no device");
577 qint64 oldPos = device->pos();
580 qint64 readBytes = device->read(head,
sizeof(head));
581 if (readBytes !=
sizeof(head)) {
582 if (device->isSequential()) {
583 while (readBytes > 0)
584 device->ungetChar(head[readBytes-- - 1]);
586 device->seek(oldPos);
591 if (device->isSequential()) {
592 while (readBytes > 0)
593 device->ungetChar(head[readBytes-- - 1]);
595 device->seek(oldPos);
598 return qstrncmp(head,
"\012", 1) == 0;
601 class PCXPlugin :
public QImageIOPlugin
604 QStringList keys()
const;
605 Capabilities capabilities(QIODevice *device,
const QByteArray &format)
const;
606 QImageIOHandler *create(QIODevice *device,
const QByteArray &format = QByteArray())
const;
609 QStringList PCXPlugin::keys()
const
611 return QStringList() <<
"pcx" <<
"PCX";
614 QImageIOPlugin::Capabilities PCXPlugin::capabilities(QIODevice *device,
const QByteArray &format)
const
616 if (format ==
"pcx" || format ==
"PCX")
617 return Capabilities(CanRead | CanWrite);
618 if (!format.isEmpty())
620 if (!device->isOpen())
626 if (device->isWritable())
631 QImageIOHandler *PCXPlugin::create(QIODevice *device,
const QByteArray &format)
const
634 handler->setDevice(device);
635 handler->setFormat(format);
639 Q_EXPORT_STATIC_PLUGIN(PCXPlugin)
640 Q_EXPORT_PLUGIN2(pcx, PCXPlugin)
static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
static void readImage24(QImage &img, QDataStream &s, const PCXHEADER &header)
static void writeLine(QDataStream &s, QByteArray &buf)
void setColor(int i, const QRgb color)
static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
static QDataStream & operator>>(QDataStream &s, RGB &rgb)
static void readImage1(QImage &img, QDataStream &s, const PCXHEADER &header)
static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
static void writeImage1(QImage &img, QDataStream &s, PCXHEADER &header)
static QDataStream & operator<<(QDataStream &s, const RGB &rgb)
static void readLine(QDataStream &s, QByteArray &buf, const PCXHEADER &header)
bool write(const QImage &image)
static void readImage4(QImage &img, QDataStream &s, const PCXHEADER &header)
static void writeImage4(QImage &img, QDataStream &s, PCXHEADER &header)
static RGB from(const QRgb &color)