diff --git a/src/imageformats/dds.cpp b/src/imageformats/dds.cpp index 19ddbe0..d845b99 100644 --- a/src/imageformats/dds.cpp +++ b/src/imageformats/dds.cpp @@ -79,7 +79,6 @@ union Color4444 { ushort u; }; - static const uint FOURCC_DDS = MAKEFOURCC('D', 'D', 'S', ' '); static const uint FOURCC_DXT1 = MAKEFOURCC('D', 'X', 'T', '1'); static const uint FOURCC_DXT2 = MAKEFOURCC('D', 'X', 'T', '2'); @@ -126,7 +125,6 @@ enum DDSType { DDS_UNKNOWN }; - struct DDSPixelFormat { uint size; uint flags; @@ -138,7 +136,7 @@ struct DDSPixelFormat { uint amask; }; -static QDataStream & operator>> (QDataStream & s, DDSPixelFormat & pf) +static QDataStream &operator>> (QDataStream &s, DDSPixelFormat &pf) { s >> pf.size; s >> pf.flags; @@ -158,7 +156,7 @@ struct DDSCaps { uint caps4; }; -static QDataStream & operator>> (QDataStream & s, DDSCaps & caps) +static QDataStream &operator>> (QDataStream &s, DDSCaps &caps) { s >> caps.caps1; s >> caps.caps2; @@ -181,7 +179,7 @@ struct DDSHeader { uint notused; }; -static QDataStream & operator>> (QDataStream & s, DDSHeader & header) +static QDataStream &operator>> (QDataStream &s, DDSHeader &header) { s >> header.size; s >> header.flags; @@ -199,7 +197,7 @@ static QDataStream & operator>> (QDataStream & s, DDSHeader & header) return s; } -static bool IsValid(const DDSHeader & header) +static bool IsValid(const DDSHeader &header) { if (header.size != 124) { return false; @@ -217,9 +215,8 @@ static bool IsValid(const DDSHeader & header) return true; } - // Get supported type. We currently support 10 different types. -static DDSType GetType(const DDSHeader & header) +static DDSType GetType(const DDSHeader &header) { if (header.pf.flags & DDPF_RGB) { if (header.pf.flags & DDPF_ALPHAPIXELS) { @@ -258,17 +255,17 @@ static DDSType GetType(const DDSHeader & header) return DDS_UNKNOWN; } -static bool HasAlpha(const DDSHeader & header) +static bool HasAlpha(const DDSHeader &header) { return header.pf.flags & DDPF_ALPHAPIXELS; } -static bool IsCubeMap(const DDSHeader & header) +static bool IsCubeMap(const DDSHeader &header) { return header.caps.caps2 & DDSCAPS2_CUBEMAP; } -static bool IsSupported(const DDSHeader & header) +static bool IsSupported(const DDSHeader &header) { if (header.caps.caps2 & DDSCAPS2_VOLUME) { return false; @@ -279,13 +276,13 @@ static bool IsSupported(const DDSHeader & header) return true; } -static bool LoadA8R8G8B8(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadA8R8G8B8(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; for (uint y = 0; y < h; y++) { - QRgb * scanline = (QRgb *) img.scanLine(y); + QRgb *scanline = (QRgb *) img.scanLine(y); for (uint x = 0; x < w; x++) { uchar r, g, b, a; s >> b >> g >> r >> a; @@ -296,13 +293,13 @@ static bool LoadA8R8G8B8(QDataStream & s, const DDSHeader & header, QImage & img return true; } -static bool LoadR8G8B8(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadR8G8B8(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; for (uint y = 0; y < h; y++) { - QRgb * scanline = (QRgb *) img.scanLine(y); + QRgb *scanline = (QRgb *) img.scanLine(y); for (uint x = 0; x < w; x++) { uchar r, g, b; s >> b >> g >> r; @@ -313,13 +310,13 @@ static bool LoadR8G8B8(QDataStream & s, const DDSHeader & header, QImage & img) return true; } -static bool LoadA1R5G5B5(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadA1R5G5B5(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; for (uint y = 0; y < h; y++) { - QRgb * scanline = (QRgb *) img.scanLine(y); + QRgb *scanline = (QRgb *) img.scanLine(y); for (uint x = 0; x < w; x++) { Color1555 color; s >> color.u; @@ -334,13 +331,13 @@ static bool LoadA1R5G5B5(QDataStream & s, const DDSHeader & header, QImage & img return true; } -static bool LoadA4R4G4B4(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadA4R4G4B4(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; for (uint y = 0; y < h; y++) { - QRgb * scanline = (QRgb *) img.scanLine(y); + QRgb *scanline = (QRgb *) img.scanLine(y); for (uint x = 0; x < w; x++) { Color4444 color; s >> color.u; @@ -355,13 +352,13 @@ static bool LoadA4R4G4B4(QDataStream & s, const DDSHeader & header, QImage & img return true; } -static bool LoadR5G6B5(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadR5G6B5(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; for (uint y = 0; y < h; y++) { - QRgb * scanline = (QRgb *) img.scanLine(y); + QRgb *scanline = (QRgb *) img.scanLine(y); for (uint x = 0; x < w; x++) { Color565 color; s >> color.u; @@ -375,18 +372,18 @@ static bool LoadR5G6B5(QDataStream & s, const DDSHeader & header, QImage & img) return true; } -static QDataStream & operator>> (QDataStream & s, Color565 & c) +static QDataStream &operator>> (QDataStream &s, Color565 &c) { return s >> c.u; } - struct BlockDXT { Color565 col0; Color565 col1; uchar row[4]; - void GetColors(Color8888 color_array[4]) { + void GetColors(Color8888 color_array[4]) + { color_array[0].r = (col0.c.r << 3) | (col0.c.r >> 2); color_array[0].g = (col0.c.g << 2) | (col0.c.g >> 4); color_array[0].b = (col0.c.b << 3) | (col0.c.b >> 2); @@ -424,8 +421,7 @@ struct BlockDXT { } }; - -static QDataStream & operator>> (QDataStream & s, BlockDXT & c) +static QDataStream &operator>> (QDataStream &s, BlockDXT &c) { return s >> c.col0 >> c.col1 >> c.row[0] >> c.row[1] >> c.row[2] >> c.row[3]; } @@ -434,7 +430,7 @@ struct BlockDXTAlphaExplicit { ushort row[4]; }; -static QDataStream & operator>> (QDataStream & s, BlockDXTAlphaExplicit & c) +static QDataStream &operator>> (QDataStream &s, BlockDXTAlphaExplicit &c) { return s >> c.row[0] >> c.row[1] >> c.row[2] >> c.row[3]; } @@ -444,7 +440,8 @@ struct BlockDXTAlphaLinear { uchar alpha1; uchar bits[6]; - void GetAlphas(uchar alpha_array[8]) { + void GetAlphas(uchar alpha_array[8]) + { alpha_array[0] = alpha0; alpha_array[1] = alpha1; @@ -472,7 +469,8 @@ struct BlockDXTAlphaLinear { } } - void GetBits(uchar bit_array[16]) { + void GetBits(uchar bit_array[16]) + { // Split 24 packed bits into 8 bytes, 3 bits at a time. uint b = bits[0] | bits[1] << 8 | bits[2] << 16; bit_array[0] = uchar(b & 0x07); b >>= 3; @@ -496,19 +494,19 @@ struct BlockDXTAlphaLinear { } }; -static QDataStream & operator>> (QDataStream & s, BlockDXTAlphaLinear & c) +static QDataStream &operator>> (QDataStream &s, BlockDXTAlphaLinear &c) { s >> c.alpha0 >> c.alpha1; return s >> c.bits[0] >> c.bits[1] >> c.bits[2] >> c.bits[3] >> c.bits[4] >> c.bits[5]; } -static bool LoadDXT1(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadDXT1(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; BlockDXT block; - QRgb * scanline[4]; + QRgb *scanline[4]; for (uint y = 0; y < h; y += 4) { for (uint j = 0; j < 4; j++) { @@ -541,14 +539,14 @@ static bool LoadDXT1(QDataStream & s, const DDSHeader & header, QImage & img) return true; } -static bool LoadDXT3(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadDXT3(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; BlockDXT block; BlockDXTAlphaExplicit alpha; - QRgb * scanline[4]; + QRgb *scanline[4]; for (uint y = 0; y < h; y += 4) { for (uint j = 0; j < 4; j++) { @@ -586,21 +584,23 @@ static bool LoadDXT3(QDataStream & s, const DDSHeader & header, QImage & img) return true; } -static bool LoadDXT2(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadDXT2(QDataStream &s, const DDSHeader &header, QImage &img) { - if (!LoadDXT3(s, header, img)) return false; + if (!LoadDXT3(s, header, img)) { + return false; + } //UndoPremultiplyAlpha(img); return true; } -static bool LoadDXT5(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadDXT5(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; BlockDXT block; BlockDXTAlphaLinear alpha; - QRgb * scanline[4]; + QRgb *scanline[4]; for (uint y = 0; y < h; y += 4) { for (uint j = 0; j < 4; j++) { @@ -641,21 +641,23 @@ static bool LoadDXT5(QDataStream & s, const DDSHeader & header, QImage & img) return true; } -static bool LoadDXT4(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadDXT4(QDataStream &s, const DDSHeader &header, QImage &img) { - if (!LoadDXT5(s, header, img)) return false; + if (!LoadDXT5(s, header, img)) { + return false; + } //UndoPremultiplyAlpha(img); return true; } -static bool LoadRXGB(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadRXGB(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; BlockDXT block; BlockDXTAlphaLinear alpha; - QRgb * scanline[4]; + QRgb *scanline[4]; for (uint y = 0; y < h; y += 4) { for (uint j = 0; j < 4; j++) { @@ -697,14 +699,14 @@ static bool LoadRXGB(QDataStream & s, const DDSHeader & header, QImage & img) return true; } -static bool LoadATI2(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadATI2(QDataStream &s, const DDSHeader &header, QImage &img) { const uint w = header.width; const uint h = header.height; BlockDXTAlphaLinear xblock; BlockDXTAlphaLinear yblock; - QRgb * scanline[4]; + QRgb *scanline[4]; for (uint y = 0; y < h; y += 4) { for (uint j = 0; j < 4; j++) { @@ -751,9 +753,7 @@ static bool LoadATI2(QDataStream & s, const DDSHeader & header, QImage & img) return true; } - - -typedef bool (* TextureLoader)(QDataStream & s, const DDSHeader & header, QImage & img); +typedef bool (* TextureLoader)(QDataStream &s, const DDSHeader &header, QImage &img); // Get an appropriate texture loader for the given type. static TextureLoader GetTextureLoader(DDSType type) @@ -788,9 +788,8 @@ static TextureLoader GetTextureLoader(DDSType type) }; } - // Load a 2d texture. -static bool LoadTexture(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadTexture(QDataStream &s, const DDSHeader &header, QImage &img) { // Create dst image. img = QImage(header.width, header.height, QImage::Format_RGB32); @@ -811,8 +810,7 @@ static bool LoadTexture(QDataStream & s, const DDSHeader & header, QImage & img) return loader(s, header, img); } - -static int FaceOffset(const DDSHeader & header) +static int FaceOffset(const DDSHeader &header) { DDSType type = GetType(header); @@ -858,7 +856,7 @@ static int face_flags[6] = { }; // Load unwrapped cube map. -static bool LoadCubeMap(QDataStream & s, const DDSHeader & header, QImage & img) +static bool LoadCubeMap(QDataStream &s, const DDSHeader &header, QImage &img) { // Create dst image. #if CUBE_LAYOUT == HORIZONTAL @@ -917,8 +915,8 @@ static bool LoadCubeMap(QDataStream & s, const DDSHeader & header, QImage & img) // Copy face on the image. for (uint y = 0; y < header.height; y++) { - QRgb * src = (QRgb *) face.scanLine(y); - QRgb * dst = (QRgb *) img.scanLine(y + offset_y) + offset_x; + QRgb *src = (QRgb *) face.scanLine(y); + QRgb *dst = (QRgb *) img.scanLine(y + offset_y) + offset_x; memcpy(dst, src, sizeof(QRgb) * header.width); } } @@ -926,8 +924,6 @@ static bool LoadCubeMap(QDataStream & s, const DDSHeader & header, QImage & img) return true; } - - DDSHandler::DDSHandler() { } @@ -994,8 +990,9 @@ bool DDSHandler::canRead(QIODevice *device) qint64 readBytes = device->read(head, sizeof(head)); if (readBytes != sizeof(head)) { if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -1003,8 +1000,9 @@ bool DDSHandler::canRead(QIODevice *device) } if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -1014,16 +1012,20 @@ bool DDSHandler::canRead(QIODevice *device) QImageIOPlugin::Capabilities DDSPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "dds") + if (format == "dds") { return Capabilities(CanRead); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && DDSHandler::canRead(device)) + if (device->isReadable() && DDSHandler::canRead(device)) { cap |= CanRead; + } return cap; } diff --git a/src/imageformats/eps.cpp b/src/imageformats/eps.cpp index af521d7..443f7cf 100644 --- a/src/imageformats/eps.cpp +++ b/src/imageformats/eps.cpp @@ -49,7 +49,7 @@ static bool seekToCodeStart(QIODevice *io, qint64 &ps_offset, qint64 &ps_size) return false; } ps_offset // Offset is in little endian - = qint64( ((unsigned char)buf[0]) + = qint64(((unsigned char)buf[0]) + ((unsigned char)buf[1] << 8) + ((unsigned char)buf[2] << 16) + ((unsigned char)buf[3] << 24)); @@ -58,11 +58,11 @@ static bool seekToCodeStart(QIODevice *io, qint64 &ps_offset, qint64 &ps_size) return false; } ps_size // Size is in little endian - = qint64( ((unsigned char)buf[0]) + = qint64(((unsigned char)buf[0]) + ((unsigned char)buf[1] << 8) + ((unsigned char)buf[2] << 16) + ((unsigned char)buf[3] << 24)); - qCDebug(EPSPLUGIN) << "Offset: " << ps_offset <<" Size: " << ps_size; + qCDebug(EPSPLUGIN) << "Offset: " << ps_offset << " Size: " << ps_size; if (!io->seek(ps_offset)) { // Get offset of PostScript code in the MS-DOS EPS file. qCDebug(EPSPLUGIN) << "cannot seek in MS-DOS EPS file"; return false; @@ -138,12 +138,13 @@ bool EPSHandler::read(QImage *image) dt.start(); #endif - QIODevice* io = device(); + QIODevice *io = device(); qint64 ps_offset, ps_size; // find start of PostScript code - if (!seekToCodeStart(io, ps_offset, ps_size)) + if (!seekToCodeStart(io, ps_offset, ps_size)) { return false; + } qCDebug(EPSPLUGIN) << "Offset:" << ps_offset << "; size:" << ps_size; @@ -202,15 +203,16 @@ bool EPSHandler::read(QImage *image) } QByteArray intro = "\n"; - intro += QByteArray::number(-qRound(x1*xScale)); + intro += QByteArray::number(-qRound(x1 * xScale)); intro += " "; - intro += QByteArray::number(-qRound(y1*yScale)); + intro += QByteArray::number(-qRound(y1 * yScale)); intro += " translate\n"; converter.write(intro); io->reset(); - if (ps_offset > 0) + if (ps_offset > 0) { io->seek(ps_offset); + } QByteArray buffer; buffer.resize(4096); @@ -246,15 +248,15 @@ bool EPSHandler::read(QImage *image) } } - bool EPSHandler::write(const QImage &image) { QPrinter psOut(QPrinter::PrinterResolution); QPainter p; QTemporaryFile tmpFile(QStringLiteral("XXXXXXXX.pdf")); - if (!tmpFile.open()) + if (!tmpFile.open()) { return false; + } psOut.setCreator(QStringLiteral("KDE EPS image plugin")); psOut.setOutputFileName(tmpFile.fileName()); @@ -319,8 +321,9 @@ bool EPSHandler::canRead(QIODevice *device) QByteArray head = device->readLine(64); int readBytes = head.size(); if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -330,18 +333,23 @@ bool EPSHandler::canRead(QIODevice *device) QImageIOPlugin::Capabilities EPSPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "eps" || format == "epsi" || format == "epsf") + if (format == "eps" || format == "epsi" || format == "epsf") { return Capabilities(CanRead | CanWrite); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && EPSHandler::canRead(device)) + if (device->isReadable() && EPSHandler::canRead(device)) { cap |= CanRead; - if (device->isWritable()) + } + if (device->isWritable()) { cap |= CanWrite; + } return cap; } diff --git a/src/imageformats/exr.cpp b/src/imageformats/exr.cpp index 5d8a502..f8c70b7 100644 --- a/src/imageformats/exr.cpp +++ b/src/imageformats/exr.cpp @@ -1,4 +1,3 @@ -// -*- C++;indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*- /** * KImageIO Routines to read (and perhaps in the future, write) images @@ -37,8 +36,9 @@ class K_IStream: public Imf::IStream { public: - K_IStream(QIODevice *dev, const QByteArray& fileName): - IStream(fileName.data()), m_dev(dev) { + K_IStream(QIODevice *dev, const QByteArray &fileName): + IStream(fileName.data()), m_dev(dev) + { } virtual bool read(char c[], int n); @@ -57,8 +57,9 @@ bool K_IStream::read(char c[], int n) return true; } else if (result == 0) { throw Iex::InputExc("Unexpected end of file"); - } else // negative value { + } else { // negative value { Iex::throwErrnoExc("Error in read", result); + } return false; } @@ -115,14 +116,18 @@ QRgb RgbaToQrgba(struct Imf::Rgba imagePixel) // this value will be mapped to the display's // maximum intensity). // Response: kneeLow = 0.0 (2^0.0 => 1); kneeHigh = 5.0 (2^5 =>32) - if (r > 1.0) + if (r > 1.0) { r = 1.0 + Imath::Math::log((r - 1.0) * 0.184874 + 1) / 0.184874; - if (g > 1.0) + } + if (g > 1.0) { g = 1.0 + Imath::Math::log((g - 1.0) * 0.184874 + 1) / 0.184874; - if (b > 1.0) + } + if (b > 1.0) { b = 1.0 + Imath::Math::log((b - 1.0) * 0.184874 + 1) / 0.184874; - if (a > 1.0) + } + if (a > 1.0) { a = 1.0 + Imath::Math::log((a - 1.0) * 0.184874 + 1) / 0.184874; + } // // 5) Gamma-correct the pixel values, assuming that the // screen's gamma is 0.4545 (or 1/2.2). @@ -174,8 +179,9 @@ bool EXRHandler::read(QImage *outImage) file.readPixels(dw.min.y, dw.max.y); QImage image(width, height, QImage::Format_RGB32); - if (image.isNull()) + if (image.isNull()) { return false; + } // somehow copy pixels into image for (int y = 0; y < height; y++) { @@ -194,7 +200,6 @@ bool EXRHandler::read(QImage *outImage) } } - bool EXRHandler::write(const QImage &image) { // TODO: stub @@ -202,7 +207,6 @@ bool EXRHandler::write(const QImage &image) return false; } - bool EXRHandler::canRead(QIODevice *device) { if (!device) { @@ -217,16 +221,20 @@ bool EXRHandler::canRead(QIODevice *device) QImageIOPlugin::Capabilities EXRPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "exr") + if (format == "exr") { return Capabilities(CanRead); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && EXRHandler::canRead(device)) + if (device->isReadable() && EXRHandler::canRead(device)) { cap |= CanRead; + } return cap; } diff --git a/src/imageformats/gimp.h b/src/imageformats/gimp.h index cb697b8..028653b 100644 --- a/src/imageformats/gimp.h +++ b/src/imageformats/gimp.h @@ -178,7 +178,7 @@ inline int INT_BLEND(int a, int b, int alpha) * \param green the green component (modified in place). * \param blue the blue component (modified in place). */ -static void RGBTOHSV(uchar& red, uchar& green, uchar& blue) +static void RGBTOHSV(uchar &red, uchar &green, uchar &blue) { int r, g, b; double h, s, v; @@ -200,27 +200,31 @@ static void RGBTOHSV(uchar& red, uchar& green, uchar& blue) v = max; - if (max != 0) + if (max != 0) { s = ((max - min) * 255) / (double)max; - else + } else { s = 0; + } - if (s == 0) + if (s == 0) { h = 0; - else { + } else { int delta = max - min; - if (r == max) + if (r == max) { h = (g - b) / (double)delta; - else if (g == max) + } else if (g == max) { h = 2 + (b - r) / (double)delta; - else if (b == max) + } else if (b == max) { h = 4 + (r - g) / (double)delta; + } h *= 42.5; - if (h < 0) + if (h < 0) { h += 255; - if (h > 255) + } + if (h > 255) { h -= 255; + } } red = (uchar)h; @@ -234,7 +238,7 @@ static void RGBTOHSV(uchar& red, uchar& green, uchar& blue) * \param saturation the saturation component (modified in place). * \param value the value component (modified in place). */ -static void HSVTORGB(uchar& hue, uchar& saturation, uchar& value) +static void HSVTORGB(uchar &hue, uchar &saturation, uchar &value) { if (saturation == 0) { hue = value; @@ -293,7 +297,7 @@ static void HSVTORGB(uchar& hue, uchar& saturation, uchar& value) * \param green the green component (modified in place). * \param blue the blue component (modified in place). */ -static void RGBTOHLS(uchar& red, uchar& green, uchar& blue) +static void RGBTOHLS(uchar &red, uchar &green, uchar &blue) { int r = red; int g = green; @@ -319,24 +323,27 @@ static void RGBTOHLS(uchar& red, uchar& green, uchar& blue) } else { int delta = max - min; - if (l < 128) + if (l < 128) { s = 255 * (double)delta / (double)(max + min); - else + } else { s = 255 * (double)delta / (double)(511 - max - min); + } - if (r == max) + if (r == max) { h = (g - b) / (double)delta; - else if (g == max) + } else if (g == max) { h = 2 + (b - r) / (double)delta; - else + } else { h = 4 + (r - g) / (double)delta; + } h *= 42.5; - if (h < 0) + if (h < 0) { h += 255; - else if (h > 255) + } else if (h > 255) { h -= 255; + } } red = (uchar)h; @@ -355,19 +362,21 @@ static int HLSVALUE(double n1, double n2, double hue) { double value; - if (hue > 255) + if (hue > 255) { hue -= 255; - else if (hue < 0) + } else if (hue < 0) { hue += 255; + } - if (hue < 42.5) + if (hue < 42.5) { value = n1 + (n2 - n1) * (hue / 42.5); - else if (hue < 127.5) + } else if (hue < 127.5) { value = n2; - else if (hue < 170) + } else if (hue < 170) { value = n1 + (n2 - n1) * ((170 - hue) / 42.5); - else + } else { value = n1; + } return (int)(value * 255); } @@ -378,7 +387,7 @@ static int HLSVALUE(double n1, double n2, double hue) * \param lightness the lightness component (modified in place). * \param saturation the saturation component (modified in place). */ -static void HLSTORGB(uchar& hue, uchar& lightness, uchar& saturation) +static void HLSTORGB(uchar &hue, uchar &lightness, uchar &saturation) { double h = hue; double l = lightness; @@ -391,10 +400,11 @@ static void HLSTORGB(uchar& hue, uchar& lightness, uchar& saturation) } else { double m1, m2; - if (l < 128) + if (l < 128) { m2 = (l * (255 + s)) / 65025.; - else + } else { m2 = (l + s - (l * s) / 255.) / 255.; + } m1 = (l / 127.5) - m2; diff --git a/src/imageformats/hdr.cpp b/src/imageformats/hdr.cpp index f59fc95..73097f2 100644 --- a/src/imageformats/hdr.cpp +++ b/src/imageformats/hdr.cpp @@ -26,14 +26,16 @@ namespace // Private. static inline uchar ClipToByte(float value) { - if (value > 255.0f) return 255; + if (value > 255.0f) { + return 255; + } //else if (value < 0.0f) return 0; // we know value is positive. return uchar(value); } // read an old style line from the hdr image file // if 'first' is true the first byte is already read -static bool Read_Old_Line(uchar * image, int width, QDataStream & s) +static bool Read_Old_Line(uchar *image, int width, QDataStream &s) { int rshift = 0; int i; @@ -44,7 +46,9 @@ static bool Read_Old_Line(uchar * image, int width, QDataStream & s) s >> image[2]; s >> image[3]; - if (s.atEnd()) return false; + if (s.atEnd()) { + return false; + } if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1)) { for (i = image[3] << rshift; i > 0; i--) { @@ -63,8 +67,7 @@ static bool Read_Old_Line(uchar * image, int width, QDataStream & s) return true; } - -static void RGBE_To_QRgbLine(uchar * image, QRgb * scanline, int width) +static void RGBE_To_QRgbLine(uchar *image, QRgb *scanline, int width) { for (int j = 0; j < width; j++) { // v = ldexp(1.0, int(image[3]) - 128); @@ -85,7 +88,7 @@ static void RGBE_To_QRgbLine(uchar * image, QRgb * scanline, int width) } // Load the HDR image. -static bool LoadHDR(QDataStream & s, const int width, const int height, QImage & img) +static bool LoadHDR(QDataStream &s, const int width, const int height, QImage &img) { uchar val, code; @@ -97,7 +100,7 @@ static bool LoadHDR(QDataStream & s, const int width, const int height, QImage & QMemArray image(width * 4); for (int cline = 0; cline < height; cline++) { - QRgb * scanline = (QRgb *) img.scanLine(cline); + QRgb *scanline = (QRgb *) img.scanLine(cline); // determine scanline type if ((width < MINELEN) || (MAXELEN < width)) { @@ -173,8 +176,7 @@ static bool LoadHDR(QDataStream & s, const int width, const int height, QImage & } // namespace - -Q_DECL_EXPORT void kimgio_hdr_read(QImageIO * io) +Q_DECL_EXPORT void kimgio_hdr_read(QImageIO *io) { int len; char line[MAXLINE]; @@ -229,7 +231,6 @@ Q_DECL_EXPORT void kimgio_hdr_read(QImageIO * io) io->setStatus(0); } - Q_DECL_EXPORT void kimgio_hdr_write(QImageIO *) { // intentionally not implemented (since writing low dynamic range data to a HDR file is nonsense.) diff --git a/src/imageformats/jp2.cpp b/src/imageformats/jp2.cpp index c0856dd..5be5063 100644 --- a/src/imageformats/jp2.cpp +++ b/src/imageformats/jp2.cpp @@ -31,7 +31,6 @@ #define DEFAULT_RATE 0.10 #define MAXCMPTS 256 - /************************* JasPer QIODevice stream ***********************/ //unfortunately this is declared as static in JasPer libraries @@ -39,7 +38,7 @@ static jas_stream_t *jas_stream_create() { jas_stream_t *stream; - if (!(stream = (jas_stream_t*)jas_malloc(sizeof(jas_stream_t)))) { + if (!(stream = (jas_stream_t *)jas_malloc(sizeof(jas_stream_t)))) { return 0; } stream->openmode_ = 0; @@ -71,7 +70,7 @@ static void jas_stream_initbuf(jas_stream_t *stream, int bufmode, char *buf, if (!buf) { /* The caller has not specified a buffer to employ, so allocate one. */ - if ((stream->bufbase_ = (unsigned char*)jas_malloc(JAS_STREAM_BUFSIZE + + if ((stream->bufbase_ = (unsigned char *)jas_malloc(JAS_STREAM_BUFSIZE + JAS_STREAM_MAXPUTBACK))) { stream->bufmode_ |= JAS_STREAM_FREEBUF; stream->bufsize_ = JAS_STREAM_BUFSIZE; @@ -105,19 +104,19 @@ static void jas_stream_initbuf(jas_stream_t *stream, int bufmode, char *buf, static int qiodevice_read(jas_stream_obj_t *obj, char *buf, int cnt) { - QIODevice *io = (QIODevice*) obj; + QIODevice *io = (QIODevice *) obj; return io->read(buf, cnt); } static int qiodevice_write(jas_stream_obj_t *obj, char *buf, int cnt) { - QIODevice *io = (QIODevice*) obj; + QIODevice *io = (QIODevice *) obj; return io->write(buf, cnt); } static long qiodevice_seek(jas_stream_obj_t *obj, long offset, int origin) { - QIODevice *io = (QIODevice*) obj; + QIODevice *io = (QIODevice *) obj; long newpos; switch (origin) { @@ -136,10 +135,11 @@ static long qiodevice_seek(jas_stream_obj_t *obj, long offset, int origin) if (newpos < 0) { return -1; } - if (io->seek(newpos)) + if (io->seek(newpos)) { return newpos; - else + } else { return -1; + } } static int qiodevice_close(jas_stream_obj_t *) @@ -158,7 +158,9 @@ static jas_stream_t *jas_stream_qiodevice(QIODevice *iodevice) { jas_stream_t *stream; - if (!iodevice) return 0; + if (!iodevice) { + return 0; + } if (!(stream = jas_stream_create())) { return 0; } @@ -179,24 +181,25 @@ static jas_stream_t *jas_stream_qiodevice(QIODevice *iodevice) /************************ End of JasPer QIODevice stream ****************/ typedef struct { - jas_image_t* image; + jas_image_t *image; int cmptlut[MAXCMPTS]; - jas_image_t* altimage; + jas_image_t *altimage; } gs_t; - -static jas_image_t* -read_image(QIODevice* io) +static jas_image_t * +read_image(QIODevice *io) { - jas_stream_t* in = 0; + jas_stream_t *in = 0; in = jas_stream_qiodevice(io); - if (!in) return 0; + if (!in) { + return 0; + } - jas_image_t* image = jas_image_decode(in, -1, 0); + jas_image_t *image = jas_image_decode(in, -1, 0); jas_stream_close(in); // image may be 0, but that's Ok @@ -204,22 +207,28 @@ read_image(QIODevice* io) } // read_image static bool -convert_colorspace(gs_t& gs) +convert_colorspace(gs_t &gs) { jas_cmprof_t *outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB); - if (!outprof) return false; + if (!outprof) { + return false; + } gs.altimage = jas_image_chclrspc(gs.image, outprof, JAS_CMXFORM_INTENT_PER); - if (!gs.altimage) return false; + if (!gs.altimage) { + return false; + } return true; } // convert_colorspace static bool -render_view(gs_t& gs, QImage* outImage) +render_view(gs_t &gs, QImage *outImage) { - if (!gs.altimage) return false; + if (!gs.altimage) { + return false; + } QImage qti; if ((gs.cmptlut[0] = jas_image_getcmptbytype(gs.altimage, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 || @@ -230,7 +239,7 @@ render_view(gs_t& gs, QImage* outImage) return false; } // if - const int* cmptlut = gs.cmptlut; + const int *cmptlut = gs.cmptlut; int v[3]; // check that all components have the same size. @@ -238,8 +247,9 @@ render_view(gs_t& gs, QImage* outImage) const int height = jas_image_cmptheight(gs.altimage, cmptlut[0]); for (int i = 1; i < 3; ++i) { if (jas_image_cmptwidth(gs.altimage, cmptlut[i]) != width || - jas_image_cmptheight(gs.altimage, cmptlut[i]) != height) + jas_image_cmptheight(gs.altimage, cmptlut[i]) != height) { return false; + } } // for jas_matrix_t *cmptmatrix[3]; @@ -258,7 +268,7 @@ render_view(gs_t& gs, QImage* outImage) if (qti.isNull()) { return false; } - uint32_t* data = (uint32_t*)qti.bits(); + uint32_t *data = (uint32_t *)qti.bits(); for (int y = 0; y < height; ++y) { for (int k = 0; k < 3; ++k) { @@ -274,8 +284,11 @@ render_view(gs_t& gs, QImage* outImage) // it to use the complete value range. v[k] <<= 8 - prec[k]; - if (v[k] < 0) v[k] = 0; - else if (v[k] > 255) v[k] = 255; + if (v[k] < 0) { + v[k] = 0; + } else if (v[k] > 255) { + v[k] = 255; + } ++buf[k]; } // for k @@ -293,12 +306,11 @@ render_view(gs_t& gs, QImage* outImage) return true; } // render_view - -static jas_image_t* -create_image(const QImage& qi) +static jas_image_t * +create_image(const QImage &qi) { // prepare the component parameters - jas_image_cmptparm_t* cmptparms = new jas_image_cmptparm_t[ 3 ]; + jas_image_cmptparm_t *cmptparms = new jas_image_cmptparm_t[ 3 ]; for (int i = 0; i < 3; ++i) { // x and y offset @@ -316,41 +328,45 @@ create_image(const QImage& qi) cmptparms[i].sgnd = false; } - jas_image_t* ji = jas_image_create(3 /* number components */, cmptparms, JAS_CLRSPC_UNKNOWN); + jas_image_t *ji = jas_image_create(3 /* number components */, cmptparms, JAS_CLRSPC_UNKNOWN); delete[] cmptparms; // returning 0 is ok return ji; } // create_image - static bool -write_components(jas_image_t* ji, const QImage& qi) +write_components(jas_image_t *ji, const QImage &qi) { const unsigned height = qi.height(); const unsigned width = qi.width(); - jas_matrix_t* m = jas_matrix_create(height, width); - if (!m) return false; + jas_matrix_t *m = jas_matrix_create(height, width); + if (!m) { + return false; + } jas_image_setclrspc(ji, JAS_CLRSPC_SRGB); jas_image_setcmpttype(ji, 0, JAS_IMAGE_CT_RGB_R); for (uint y = 0; y < height; ++y) - for (uint x = 0; x < width; ++x) + for (uint x = 0; x < width; ++x) { jas_matrix_set(m, y, x, qRed(qi.pixel(x, y))); + } jas_image_writecmpt(ji, 0, 0, 0, width, height, m); jas_image_setcmpttype(ji, 1, JAS_IMAGE_CT_RGB_G); for (uint y = 0; y < height; ++y) - for (uint x = 0; x < width; ++x) + for (uint x = 0; x < width; ++x) { jas_matrix_set(m, y, x, qGreen(qi.pixel(x, y))); + } jas_image_writecmpt(ji, 1, 0, 0, width, height, m); jas_image_setcmpttype(ji, 2, JAS_IMAGE_CT_RGB_B); for (uint y = 0; y < height; ++y) - for (uint x = 0; x < width; ++x) + for (uint x = 0; x < width; ++x) { jas_matrix_set(m, y, x, qBlue(qi.pixel(x, y))); + } jas_image_writecmpt(ji, 2, 0, 0, width, height, m); jas_matrix_destroy(m); @@ -358,15 +374,17 @@ write_components(jas_image_t* ji, const QImage& qi) } // write_components static bool -write_image(const QImage &image, QIODevice* io, int quality) +write_image(const QImage &image, QIODevice *io, int quality) { - jas_stream_t* stream = 0; + jas_stream_t *stream = 0; stream = jas_stream_qiodevice(io); // by here, a jas_stream_t is open - if (!stream) return false; + if (!stream) { + return false; + } - jas_image_t* ji = create_image(image); + jas_image_t *ji = create_image(image); if (!ji) { jas_stream_close(stream); return false; @@ -390,7 +408,9 @@ write_image(const QImage &image, QIODevice* io, int quality) jas_image_destroy(ji); jas_stream_close(stream); - if (i != 0) return false; + if (i != 0) { + return false; + } return true; } @@ -425,17 +445,27 @@ bool JP2Handler::canRead(QIODevice *device) bool JP2Handler::read(QImage *image) { - if (!canRead()) return false; + if (!canRead()) { + return false; + } gs_t gs; - if (!(gs.image = read_image(device()))) return false; + if (!(gs.image = read_image(device()))) { + return false; + } - if (!convert_colorspace(gs)) return false; + if (!convert_colorspace(gs)) { + return false; + } render_view(gs, image); - if (gs.image) jas_image_destroy(gs.image); - if (gs.altimage) jas_image_destroy(gs.altimage); + if (gs.image) { + jas_image_destroy(gs.image); + } + if (gs.altimage) { + jas_image_destroy(gs.altimage); + } return true; } @@ -452,31 +482,38 @@ bool JP2Handler::supportsOption(ImageOption option) const QVariant JP2Handler::option(ImageOption option) const { - if (option == Quality) + if (option == Quality) { return quality; + } return QVariant(); } void JP2Handler::setOption(ImageOption option, const QVariant &value) { - if (option == Quality) + if (option == Quality) { quality = qBound(-1, value.toInt(), 100); + } } QImageIOPlugin::Capabilities JP2Plugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "jp2") + if (format == "jp2") { return Capabilities(CanRead | CanWrite); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && JP2Handler::canRead(device)) + if (device->isReadable() && JP2Handler::canRead(device)) { cap |= CanRead; - if (device->isWritable()) + } + if (device->isWritable()) { cap |= CanWrite; + } return cap; } diff --git a/src/imageformats/pcx.cpp b/src/imageformats/pcx.cpp index e398388..e0af73b 100644 --- a/src/imageformats/pcx.cpp +++ b/src/imageformats/pcx.cpp @@ -21,7 +21,8 @@ public: quint8 g; quint8 b; - static RGB from(const QRgb &color) { + static RGB from(const QRgb &color) + { RGB c; c.r = qRed(color); c.g = qGreen(color); @@ -34,14 +35,16 @@ public: class Palette { public: - void setColor(int i, const QRgb color) { + void setColor(int i, const QRgb color) + { RGB &c = rgb[ i ]; c.r = qRed(color); c.g = qGreen(color); c.b = qBlue(color); } - QRgb color(int i) const { + QRgb color(int i) const + { return qRgb(rgb[ i ].r, rgb[ i ].g, rgb[ i ].b); } @@ -53,13 +56,16 @@ class PCXHEADER public: PCXHEADER(); - inline int width() const { + inline int width() const + { return (XMax - XMin) + 1; } - inline int height() const { + inline int height() const + { return (YMax - YMin) + 1; } - inline bool isCompressed() const { + inline bool isCompressed() const + { return (Encoding == 1); } @@ -111,8 +117,9 @@ static QDataStream &operator>>(QDataStream &s, RGB &rgb) static QDataStream &operator>>(QDataStream &s, Palette &pal) { - for (int i = 0; i < 16; ++i) + for (int i = 0; i < 16; ++i) { s >> pal.rgb[ i ]; + } return s; } @@ -151,8 +158,9 @@ static QDataStream &operator>>(QDataStream &s, PCXHEADER &ph) // Skip the rest of the header quint8 byte; - while (s.device()->pos() < 128) + while (s.device()->pos() < 128) { s >> byte; + } return s; } @@ -166,8 +174,9 @@ static QDataStream &operator<<(QDataStream &s, const RGB &rgb) static QDataStream &operator<<(QDataStream &s, const Palette &pal) { - for (int i = 0; i < 16; ++i) + for (int i = 0; i < 16; ++i) { s << pal.rgb[ i ]; + } return s; } @@ -189,8 +198,9 @@ static QDataStream &operator<<(QDataStream &s, const PCXHEADER &ph) s << ph.VScreenSize; quint8 byte = 0; - for (int i = 0; i < 54; ++i) + for (int i = 0; i < 54; ++i) { s << byte; + } return s; } @@ -219,8 +229,9 @@ static void readLine(QDataStream &s, QByteArray &buf, const PCXHEADER &header) count = byte - 0xc0; s >> byte; } - while (count-- && i < size) + while (count-- && i < size) { buf[ i++ ] = byte; + } } } else { // Image is not compressed (possible?) @@ -247,8 +258,9 @@ static void readImage1(QImage &img, QDataStream &s, const PCXHEADER &header) readLine(s, buf, header); uchar *p = img.scanLine(y); unsigned int bpl = qMin((quint16)((header.width() + 7) / 8), header.BytesPerLine); - for (unsigned int x = 0; x < bpl; ++x) + for (unsigned int x = 0; x < bpl; ++x) { p[ x ] = buf[x]; + } } // Set the color palette @@ -276,18 +288,21 @@ static void readImage4(QImage &img, QDataStream &s, const PCXHEADER &header) for (int i = 0; i < 4; i++) { quint32 offset = i * header.BytesPerLine; for (int x = 0; x < header.width(); ++x) - if (buf[ offset + (x / 8) ] & (128 >> (x % 8))) + if (buf[ offset + (x / 8) ] & (128 >> (x % 8))) { pixbuf[ x ] = (int)(pixbuf[ x ]) + (1 << i); + } } uchar *p = img.scanLine(y); - for (int x = 0; x < header.width(); ++x) + for (int x = 0; x < header.width(); ++x) { p[ x ] = pixbuf[ x ]; + } } // Read the palette - for (int i = 0; i < 16; ++i) + for (int i = 0; i < 16; ++i) { img.setColor(i, header.ColorMap.color(i)); + } } static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header) @@ -307,8 +322,9 @@ static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header) uchar *p = img.scanLine(y); unsigned int bpl = qMin(header.BytesPerLine, (quint16)header.width()); - for (unsigned int x = 0; x < bpl; ++x) + for (unsigned int x = 0; x < bpl; ++x) { p[ x ] = buf[ x ]; + } } quint8 flag; @@ -344,8 +360,9 @@ static void readImage24(QImage &img, QDataStream &s, const PCXHEADER &header) readLine(s, b_buf, header); uint *p = (uint *)img.scanLine(y); - for (int x = 0; x < header.width(); ++x) + for (int x = 0; x < header.width(); ++x) { p[ x ] = qRgb(r_buf[ x ], g_buf[ x ], b_buf[ x ]); + } } } @@ -392,8 +409,9 @@ static void writeImage1(QImage &img, QDataStream &s, PCXHEADER &header) quint8 *p = img.scanLine(y); // Invert as QImage uses reverse palette for monochrome images? - for (int i = 0; i < header.BytesPerLine; ++i) + for (int i = 0; i < header.BytesPerLine; ++i) { buf[ i ] = ~p[ i ]; + } writeLine(s, buf); } @@ -405,30 +423,35 @@ static void writeImage4(QImage &img, QDataStream &s, PCXHEADER &header) header.NPlanes = 4; header.BytesPerLine = header.width() / 8; - for (int i = 0; i < 16; ++i) + for (int i = 0; i < 16; ++i) { header.ColorMap.setColor(i, img.color(i)); + } s << header; QByteArray buf[ 4 ]; - for (int i = 0; i < 4; ++i) + for (int i = 0; i < 4; ++i) { buf[ i ].resize(header.BytesPerLine); + } for (int y = 0; y < header.height(); ++y) { quint8 *p = img.scanLine(y); - for (int i = 0; i < 4; ++i) + for (int i = 0; i < 4; ++i) { buf[ i ].fill(0); + } for (int x = 0; x < header.width(); ++x) { for (int i = 0; i < 4; ++i) - if (*(p + x) & (1 << i)) + if (*(p + x) & (1 << i)) { buf[ i ][ x / 8 ] = (int)(buf[ i ][ x / 8 ]) | 1 << (7 - x % 8); + } } - for (int i = 0; i < 4; ++i) + for (int i = 0; i < 4; ++i) { writeLine(s, buf[ i ]); + } } } @@ -445,8 +468,9 @@ static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header) for (int y = 0; y < header.height(); ++y) { quint8 *p = img.scanLine(y); - for (int i = 0; i < header.BytesPerLine; ++i) + for (int i = 0; i < header.BytesPerLine; ++i) { buf[ i ] = p[ i ]; + } writeLine(s, buf); } @@ -456,8 +480,9 @@ static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header) s << byte; // Write palette - for (int i = 0; i < 256; ++i) + for (int i = 0; i < 256; ++i) { s << RGB::from(img.color(i)); + } } static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header) @@ -488,7 +513,6 @@ static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header) } } - PCXHandler::PCXHandler() { } @@ -613,8 +637,9 @@ bool PCXHandler::canRead(QIODevice *device) qint64 readBytes = device->read(head, sizeof(head)); if (readBytes != sizeof(head)) { if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -622,8 +647,9 @@ bool PCXHandler::canRead(QIODevice *device) } if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -633,18 +659,23 @@ bool PCXHandler::canRead(QIODevice *device) QImageIOPlugin::Capabilities PCXPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "pcx") + if (format == "pcx") { return Capabilities(CanRead | CanWrite); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && PCXHandler::canRead(device)) + if (device->isReadable() && PCXHandler::canRead(device)) { cap |= CanRead; - if (device->isWritable()) + } + if (device->isWritable()) { cap |= CanWrite; + } return cap; } diff --git a/src/imageformats/pic.cpp b/src/imageformats/pic.cpp index 1cfb8eb..9d8a7ed 100644 --- a/src/imageformats/pic.cpp +++ b/src/imageformats/pic.cpp @@ -77,12 +77,15 @@ bool SoftimagePICHandler::supportsOption(ImageOption option) const QImageIOPlugin::Capabilities SoftimagePICPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "pic") + if (format == "pic") { return Capabilities(CanRead | CanWrite); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; if (device->isReadable() && SoftimagePICHandler::canRead(device)) { @@ -94,9 +97,9 @@ QImageIOPlugin::Capabilities SoftimagePICPlugin::capabilities(QIODevice *device, return cap; } -QImageIOHandler * SoftimagePICPlugin::create(QIODevice *device, const QByteArray &format) const +QImageIOHandler *SoftimagePICPlugin::create(QIODevice *device, const QByteArray &format) const { - QImageIOHandler * handler = new SoftimagePICHandler(); + QImageIOHandler *handler = new SoftimagePICHandler(); handler->setDevice(device); handler->setFormat(format); return handler; diff --git a/src/imageformats/pic.h b/src/imageformats/pic.h index 524e960..add0a35 100644 --- a/src/imageformats/pic.h +++ b/src/imageformats/pic.h @@ -27,7 +27,7 @@ class SoftimagePICHandler : public QImageIOHandler { public: virtual bool canRead() const; - virtual bool read(QImage * image); + virtual bool read(QImage *image); virtual bool write(const QImage &); virtual QVariant option(ImageOption option) const; diff --git a/src/imageformats/pic_read.cpp b/src/imageformats/pic_read.cpp index 1177b7a..4243218 100644 --- a/src/imageformats/pic_read.cpp +++ b/src/imageformats/pic_read.cpp @@ -42,9 +42,9 @@ bool picReadHeader(QIODevice *dev, PICHeader *hdr, bool peek) { int result = 0; if (peek) { - result = dev->peek((char*) hdr, HEADER_SIZE); + result = dev->peek((char *) hdr, HEADER_SIZE); } else { - result = dev->read((char*) hdr, HEADER_SIZE); + result = dev->read((char *) hdr, HEADER_SIZE); } hdr->magic = ntohl(hdr->magic); @@ -85,7 +85,7 @@ static bool readChannels(QIODevice *dev, PICChannel *channels, int &bpp) int c = 0; memset(channels, 0, sizeof(PICChannel) * 8); do { - int result = dev->read((char*) & channels[c], CHANNEL_SIZE); + int result = dev->read((char *) & channels[c], CHANNEL_SIZE); if (result != CHANNEL_SIZE) { return false; } else { @@ -164,7 +164,7 @@ static int decodeRLE(QIODevice *dev, void *row, unsigned max, unsigned bpp, unsi makeComponentMap(channels, component_map); - if (dev->read((char*) buf, 1) != 1) { + if (dev->read((char *) buf, 1) != 1) { return -1; } @@ -174,16 +174,16 @@ static int decodeRLE(QIODevice *dev, void *row, unsigned max, unsigned bpp, unsi if (len > max) { return -1; } - unsigned count = dev->read((char*) buf, bpp); + unsigned count = dev->read((char *) buf, bpp); if (count != bpp) { return -1; } for (unsigned i = 0; i < len; i++) { - pic2RGBA(buf, (unsigned char*)(ptr + i), component_map, bpp); + pic2RGBA(buf, (unsigned char *)(ptr + i), component_map, bpp); } } /* If the value is exactly 10000000, it means that it is more than 127 repetitions */ else if (buf[0] == 128) { - unsigned count = dev->read((char*) buf, bpp + 2); + unsigned count = dev->read((char *) buf, bpp + 2); if (count != bpp + 2) { return -1; } @@ -192,7 +192,7 @@ static int decodeRLE(QIODevice *dev, void *row, unsigned max, unsigned bpp, unsi return -1; } for (unsigned i = 0; i < len; i++) { - pic2RGBA(buf + 2, (unsigned char*)(ptr + i), component_map, bpp); + pic2RGBA(buf + 2, (unsigned char *)(ptr + i), component_map, bpp); } } /** No repetitions */ else { @@ -200,12 +200,12 @@ static int decodeRLE(QIODevice *dev, void *row, unsigned max, unsigned bpp, unsi if (len > max) { return -1; } - unsigned count = dev->read((char*) buf, len * bpp); + unsigned count = dev->read((char *) buf, len * bpp); if (count != len * bpp) { return -1; } for (unsigned i = 0; i < len; i++) { - pic2RGBA(buf + (i * bpp), (unsigned char*)(ptr + i), component_map, bpp); + pic2RGBA(buf + (i * bpp), (unsigned char *)(ptr + i), component_map, bpp); } } return len; @@ -236,14 +236,14 @@ static bool readRow(QIODevice *dev, unsigned *row, unsigned width, PICChannel *c } } else { unsigned char component_map[8]; - unsigned count = dev->read((char*) row, width * bpp); + unsigned count = dev->read((char *) row, width * bpp); if (count != width * bpp) { return false; } makeComponentMap(channels[c].channel, component_map); for (unsigned i = 0; i < width; i++) { - pic2RGBA(((unsigned char*) row) + (i * bpp), (unsigned char*)(row + i), component_map, bpp); + pic2RGBA(((unsigned char *) row) + (i * bpp), (unsigned char *)(row + i), component_map, bpp); } } } @@ -281,7 +281,7 @@ void pic_read(QIODevice *dev, QImage *result) QImage img(header.width, header.height, QImage::Format_ARGB32); for (int r = 0; r < header.height; r++) { - unsigned *row = (unsigned*) img.scanLine(r); + unsigned *row = (unsigned *) img.scanLine(r); std::fill(row, row + header.width, 0); if (!readRow(dev, row, header.width, channels)) { FAIL(); diff --git a/src/imageformats/pic_rw.h b/src/imageformats/pic_rw.h index 2c39d61..2cc9589 100644 --- a/src/imageformats/pic_rw.h +++ b/src/imageformats/pic_rw.h @@ -90,7 +90,6 @@ typedef struct { #define HEADER_SIZE sizeof(PICHeader) #define CHANNEL_SIZE sizeof(PICChannel) - /** * Reads the PIC header and checks that it is OK * @param dev The QT device to read from @@ -106,5 +105,4 @@ void pic_read(QIODevice *dev, QImage *img); /// Pic write handler for Qt / KDE void pic_write(QIODevice *dev, const QImage *img); - #endif//__PIC_RW_H__ diff --git a/src/imageformats/pic_write.cpp b/src/imageformats/pic_write.cpp index ca3fd30..99cd6a1 100644 --- a/src/imageformats/pic_write.cpp +++ b/src/imageformats/pic_write.cpp @@ -54,7 +54,7 @@ static bool writeHeader(QIODevice *dev, std::string msg, unsigned width, unsigne h.height = htons(height); h.ratio = 1.0f; h.fields = htons(BOTH); - count = dev->write((const char*) & h, sizeof(PICHeader)); + count = dev->write((const char *) & h, sizeof(PICHeader)); if (count != sizeof(PICHeader)) { return false; } @@ -66,7 +66,7 @@ static bool writeHeader(QIODevice *dev, std::string msg, unsigned width, unsigne if (alpha) { c.chained = 1; } - count = dev->write((const char*) & c, sizeof(PICChannel)); + count = dev->write((const char *) & c, sizeof(PICChannel)); if (count != sizeof(PICChannel)) { return false; } @@ -74,7 +74,7 @@ static bool writeHeader(QIODevice *dev, std::string msg, unsigned width, unsigne if (alpha) { c.channel = ALPHA; c.chained = 0; - count = dev->write((const char*) & c, sizeof(PICChannel)); + count = dev->write((const char *) & c, sizeof(PICChannel)); if (count != sizeof(PICChannel)) { return false; } @@ -123,7 +123,7 @@ static bool encodeRLE(const unsigned *image, unsigned char *output, bool rgb, un *out++ = count >> 8; *out++ = count & 0xFF; unsigned pixel = convertABGRtoRGBA(*image); - memcpy(out, ((char*) & pixel) + offset, channels); + memcpy(out, ((char *) & pixel) + offset, channels); out += channels; oConsumed = count; oProduced = out - output; @@ -131,7 +131,7 @@ static bool encodeRLE(const unsigned *image, unsigned char *output, bool rgb, un /* Sequece of < 128 identical pixels */ *out++ = (count + 127); unsigned pixel = convertABGRtoRGBA(*image); - memcpy(out, ((char*) & pixel) + offset, channels); + memcpy(out, ((char *) & pixel) + offset, channels); out += channels; oConsumed = count; oProduced = out - output; @@ -152,7 +152,7 @@ static bool encodeRLE(const unsigned *image, unsigned char *output, bool rgb, un in = image; for (unsigned c = 0; c < count; ++c) { unsigned pixel = convertABGRtoRGBA(*in); - memcpy(out, ((char*) & pixel) + offset, channels); + memcpy(out, ((char *) & pixel) + offset, channels); out += channels; in++; } @@ -200,7 +200,7 @@ static bool writeRow(QIODevice *dev, unsigned *row, unsigned width, bool alpha) } } - dev->write((const char*) buf, posOut); + dev->write((const char *) buf, posOut); delete[] buf; return true; } @@ -220,7 +220,7 @@ void pic_write(QIODevice *dev, const QImage *img) } for (int r = 0; r < img->height(); r++) { - unsigned *row = (unsigned*) img->scanLine(r); + unsigned *row = (unsigned *) img->scanLine(r); if (!writeRow(dev, row, img->width(), alpha)) { FAIL(); } diff --git a/src/imageformats/psd.cpp b/src/imageformats/psd.cpp index a7c4c15..438286c 100644 --- a/src/imageformats/psd.cpp +++ b/src/imageformats/psd.cpp @@ -52,7 +52,7 @@ struct PSDHeader { ushort color_mode; }; -static QDataStream & operator>> (QDataStream & s, PSDHeader & header) +static QDataStream &operator>> (QDataStream &s, PSDHeader &header) { s >> header.signature; s >> header.version; @@ -66,22 +66,23 @@ static QDataStream & operator>> (QDataStream & s, PSDHeader & header) s >> header.color_mode; return s; } -static bool seekBy(QDataStream& s, unsigned int bytes) +static bool seekBy(QDataStream &s, unsigned int bytes) { char buf[4096]; while (bytes) { unsigned int num = qMin(bytes, (unsigned int)sizeof(buf)); unsigned int l = num; s.readRawData(buf, l); - if (l != num) + if (l != num) { return false; + } bytes -= num; } return true; } // Check that the header is a valid PSD. -static bool IsValid(const PSDHeader & header) +static bool IsValid(const PSDHeader &header) { if (header.signature != 0x38425053) { // '8BPS' return false; @@ -90,7 +91,7 @@ static bool IsValid(const PSDHeader & header) } // Check that the header is supported. -static bool IsSupported(const PSDHeader & header) +static bool IsSupported(const PSDHeader &header) { if (header.version != 1) { return false; @@ -108,7 +109,7 @@ static bool IsSupported(const PSDHeader & header) } // Load the PSD image. -static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img) +static bool LoadPSD(QDataStream &s, const PSDHeader &header, QImage &img) { // Create dst image. img = QImage(header.width, header.height, QImage::Format_RGB32); @@ -159,19 +160,21 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img) if (compression) { // Skip row lengths. - if (!seekBy(s, header.height * header.channel_count * sizeof(ushort))) + if (!seekBy(s, header.height * header.channel_count * sizeof(ushort))) { return false; + } // Read RLE data. for (uint channel = 0; channel < channel_num; channel++) { - uchar * ptr = img.bits() + components[channel]; + uchar *ptr = img.bits() + components[channel]; uint count = 0; while (count < pixel_count) { uchar c; - if (s.atEnd()) + if (s.atEnd()) { return false; + } s >> c; uint len = c; @@ -179,8 +182,9 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img) // Copy next len+1 bytes literally. len++; count += len; - if (count > pixel_count) + if (count > pixel_count) { return false; + } while (len != 0) { s >> *ptr; @@ -193,8 +197,9 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img) len ^= 0xFF; len += 2; count += len; - if (s.atEnd() || count > pixel_count) + if (s.atEnd() || count > pixel_count) { return false; + } uchar val; s >> val; while (len != 0) { @@ -214,7 +219,7 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img) // Read the data by channel. for (uint channel = 0; channel < channel_num; channel++) { - uchar * ptr = img.bits() + components[channel]; + uchar *ptr = img.bits() + components[channel]; // Read the data. uint count = pixel_count; @@ -231,7 +236,6 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img) } // Private - PSDHandler::PSDHandler() { } @@ -288,8 +292,9 @@ bool PSDHandler::canRead(QIODevice *device) qint64 readBytes = device->read(head, sizeof(head)); if (readBytes != sizeof(head)) { if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -297,8 +302,9 @@ bool PSDHandler::canRead(QIODevice *device) } if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -308,16 +314,20 @@ bool PSDHandler::canRead(QIODevice *device) QImageIOPlugin::Capabilities PSDPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "psd") + if (format == "psd") { return Capabilities(CanRead); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && PSDHandler::canRead(device)) + if (device->isReadable() && PSDHandler::canRead(device)) { cap |= CanRead; + } return cap; } diff --git a/src/imageformats/ras.cpp b/src/imageformats/ras.cpp index 166cc7c..acc58a7 100644 --- a/src/imageformats/ras.cpp +++ b/src/imageformats/ras.cpp @@ -51,7 +51,7 @@ struct RasHeader { enum { SIZE = 32 }; // 8 fields of four bytes each }; -static QDataStream & operator>> (QDataStream & s, RasHeader & head) +static QDataStream &operator>> (QDataStream &s, RasHeader &head) { s >> head.MagicNumber; s >> head.Width; @@ -72,7 +72,7 @@ static QDataStream & operator>> (QDataStream & s, RasHeader & head) return s; } -static bool IsSupported(const RasHeader & head) +static bool IsSupported(const RasHeader &head) { // check magic number if (head.MagicNumber != rasMagicBigEndian) { @@ -99,7 +99,7 @@ static bool IsSupported(const RasHeader & head) return true; } -static bool LoadRAS(QDataStream & s, const RasHeader & ras, QImage &img) +static bool LoadRAS(QDataStream &s, const RasHeader &ras, QImage &img) { s.device()->seek(RasHeader::SIZE); // Read palette if needed. @@ -278,16 +278,20 @@ bool RASHandler::read(QImage *outImage) QImageIOPlugin::Capabilities RASPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "ras") + if (format == "ras") { return Capabilities(CanRead); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && RASHandler::canRead(device)) + if (device->isReadable() && RASHandler::canRead(device)) { cap |= CanRead; + } return cap; } diff --git a/src/imageformats/rgb.cpp b/src/imageformats/rgb.cpp index 1611f18..db39bfe 100644 --- a/src/imageformats/rgb.cpp +++ b/src/imageformats/rgb.cpp @@ -7,7 +7,6 @@ // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. - /* this code supports: * reading: * everything, except images with 1 dimension or images with @@ -34,13 +33,16 @@ class RLEData : public QVector { public: RLEData() {} - RLEData(const uchar *d, uint l, uint o) : _offset(o) { - for (uint i = 0; i < l; i++) + RLEData(const uchar *d, uint l, uint o) : _offset(o) + { + for (uint i = 0; i < l; i++) { append(d[i]); + } } - bool operator<(const RLEData&) const; - void write(QDataStream& s); - uint offset() const { + bool operator<(const RLEData &) const; + void write(QDataStream &s); + uint offset() const + { return _offset; } @@ -48,14 +50,14 @@ private: uint _offset; }; - class RLEMap : public QMap { public: RLEMap() : _counter(0), _offset(0) {} uint insert(const uchar *d, uint l); - QVector vector(); - void setBaseOffset(uint o) { + QVector vector(); + void setBaseOffset(uint o) + { _offset = o; } @@ -64,15 +66,14 @@ private: uint _offset; }; - class SGIImage { public: SGIImage(QIODevice *device); ~SGIImage(); - bool readImage(QImage&); - bool writeImage(const QImage&); + bool readImage(QImage &); + bool writeImage(const QImage &); private: enum { NORMAL, DITHERED, SCREEN, COLORMAP }; // colormap @@ -95,16 +96,16 @@ private: QByteArray _data; QByteArray::Iterator _pos; RLEMap _rlemap; - QVector _rlevector; + QVector _rlevector; uint _numrows; - bool readData(QImage&); + bool readData(QImage &); bool getRow(uchar *dest); void writeHeader(); void writeRle(); - void writeVerbatim(const QImage&); - bool scanData(const QImage&); + void writeVerbatim(const QImage &); + bool scanData(const QImage &); uint compact(uchar *, uchar *); uchar intensity(uchar); }; @@ -117,24 +118,22 @@ SGIImage::SGIImage(QIODevice *io) : _stream.setDevice(_dev); } - SGIImage::~SGIImage() { delete[] _starttab; delete[] _lengthtab; } - /////////////////////////////////////////////////////////////////////////////// - bool SGIImage::getRow(uchar *dest) { int n, i; if (!_rle) { for (i = 0; i < _xsize; i++) { - if (_pos >= _data.end()) + if (_pos >= _data.end()) { return false; + } dest[i] = uchar(*_pos); _pos += _bpc; } @@ -142,11 +141,13 @@ bool SGIImage::getRow(uchar *dest) } for (i = 0; i < _xsize;) { - if (_bpc == 2) + if (_bpc == 2) { _pos++; + } n = *_pos & 0x7f; - if (!n) + if (!n) { break; + } if (*_pos++ & 0x80) { for (; i < _xsize && n--; i++) { @@ -154,8 +155,9 @@ bool SGIImage::getRow(uchar *dest) _pos += _bpc; } } else { - for (; i < _xsize && n--; i++) + for (; i < _xsize && n--; i++) { *dest++ = *_pos; + } _pos += _bpc; } @@ -163,8 +165,7 @@ bool SGIImage::getRow(uchar *dest) return i == _xsize; } - -bool SGIImage::readData(QImage& img) +bool SGIImage::readData(QImage &img) { QRgb *c; quint32 *start = _starttab; @@ -172,62 +173,76 @@ bool SGIImage::readData(QImage& img) uchar *line = (uchar *)lguard.data(); unsigned x, y; - if (!_rle) + if (!_rle) { _pos = _data.begin(); - - for (y = 0; y < _ysize; y++) { - if (_rle) - _pos = _data.begin() + *start++; - if (!getRow(line)) - return false; - c = (QRgb *)img.scanLine(_ysize - y - 1); - for (x = 0; x < _xsize; x++, c++) - *c = qRgb(line[x], line[x], line[x]); } - if (_zsize == 1) + for (y = 0; y < _ysize; y++) { + if (_rle) { + _pos = _data.begin() + *start++; + } + if (!getRow(line)) { + return false; + } + c = (QRgb *)img.scanLine(_ysize - y - 1); + for (x = 0; x < _xsize; x++, c++) { + *c = qRgb(line[x], line[x], line[x]); + } + } + + if (_zsize == 1) { return true; + } if (_zsize != 2) { for (y = 0; y < _ysize; y++) { - if (_rle) + if (_rle) { _pos = _data.begin() + *start++; - if (!getRow(line)) + } + if (!getRow(line)) { return false; + } c = (QRgb *)img.scanLine(_ysize - y - 1); - for (x = 0; x < _xsize; x++, c++) + for (x = 0; x < _xsize; x++, c++) { *c = qRgb(qRed(*c), line[x], line[x]); + } } for (y = 0; y < _ysize; y++) { - if (_rle) + if (_rle) { _pos = _data.begin() + *start++; - if (!getRow(line)) + } + if (!getRow(line)) { return false; + } c = (QRgb *)img.scanLine(_ysize - y - 1); - for (x = 0; x < _xsize; x++, c++) + for (x = 0; x < _xsize; x++, c++) { *c = qRgb(qRed(*c), qGreen(*c), line[x]); + } } - if (_zsize == 3) + if (_zsize == 3) { return true; + } } for (y = 0; y < _ysize; y++) { - if (_rle) + if (_rle) { _pos = _data.begin() + *start++; - if (!getRow(line)) + } + if (!getRow(line)) { return false; + } c = (QRgb *)img.scanLine(_ysize - y - 1); - for (x = 0; x < _xsize; x++, c++) + for (x = 0; x < _xsize; x++, c++) { *c = qRgba(qRed(*c), qGreen(*c), qBlue(*c), line[x]); + } } return true; } - -bool SGIImage::readImage(QImage& img) +bool SGIImage::readImage(QImage &img) { qint8 u8; qint16 u16; @@ -237,14 +252,16 @@ bool SGIImage::readImage(QImage& img) // magic _stream >> u16; - if (u16 != 0x01da) + if (u16 != 0x01da) { return false; + } // verbatim/rle _stream >> _rle; // qDebug() << (_rle ? "RLE" : "verbatim"); - if (_rle > 1) + if (_rle > 1) { return false; + } // bytes per channel _stream >> _bpc; @@ -253,14 +270,16 @@ bool SGIImage::readImage(QImage& img) ; else if (_bpc == 2) { // qDebug() << "dropping least significant byte"; - } else + } else { return false; + } // number of dimensions _stream >> _dim; // qDebug() << "dimensions: " << _dim; - if (_dim < 1 || _dim > 3) + if (_dim < 1 || _dim > 3) { return false; + } _stream >> _xsize >> _ysize >> _zsize >> _pixmin >> _pixmax >> u32; // qDebug() << "x: " << _xsize; @@ -273,27 +292,30 @@ bool SGIImage::readImage(QImage& img) _stream >> _colormap; // qDebug() << "colormap: " << _colormap; - if (_colormap != NORMAL) - return false; // only NORMAL supported + if (_colormap != NORMAL) { + return false; // only NORMAL supported + } - for (int i = 0; i < 404; i++) + for (int i = 0; i < 404; i++) { _stream >> u8; + } if (_dim == 1) { // qDebug() << "1-dimensional images aren't supported yet"; return false; } - if (_stream.atEnd()) + if (_stream.atEnd()) { return false; + } _numrows = _ysize * _zsize; img = QImage(_xsize, _ysize, QImage::Format_RGB32); - if (_zsize == 2 || _zsize == 4) + if (_zsize == 2 || _zsize == 4) { img = img.convertToFormat(QImage::Format_ARGB32); - else if (_zsize > 4) { + } else if (_zsize > 4) { // qDebug() << "using first 4 of " << _zsize << " channels"; } @@ -306,8 +328,9 @@ bool SGIImage::readImage(QImage& img) } _lengthtab = new quint32[_numrows]; - for (l = 0; l < _numrows; l++) + for (l = 0; l < _numrows; l++) { _stream >> _lengthtab[l]; + } } _data = _dev->readAll(); @@ -329,84 +352,87 @@ bool SGIImage::readImage(QImage& img) return true; } - /////////////////////////////////////////////////////////////////////////////// - -void RLEData::write(QDataStream& s) +void RLEData::write(QDataStream &s) { - for (int i = 0; i < size(); i++) + for (int i = 0; i < size(); i++) { s << at(i); + } } - -bool RLEData::operator<(const RLEData& b) const +bool RLEData::operator<(const RLEData &b) const { uchar ac, bc; for (int i = 0; i < qMin(size(), b.size()); i++) { ac = at(i); bc = b[i]; - if (ac != bc) + if (ac != bc) { return ac < bc; + } } return size() < b.size(); } - uint RLEMap::insert(const uchar *d, uint l) { RLEData data = RLEData(d, l, _offset); Iterator it = find(data); - if (it != end()) + if (it != end()) { return it.value(); + } _offset += l; return QMap::insert(data, _counter++).value(); } - -QVector RLEMap::vector() +QVector RLEMap::vector() { - QVector v(size()); - for (Iterator it = begin(); it != end(); ++it) + QVector v(size()); + for (Iterator it = begin(); it != end(); ++it) { v.replace(it.value(), &it.key()); + } return v; } - uchar SGIImage::intensity(uchar c) { - if (c < _pixmin) + if (c < _pixmin) { _pixmin = c; - if (c > _pixmax) + } + if (c > _pixmax) { _pixmax = c; + } return c; } - uint SGIImage::compact(uchar *d, uchar *s) { uchar *dest = d, *src = s, patt, *t, *end = s + _xsize; int i, n; while (src < end) { - for (n = 0, t = src; t + 2 < end && !(*t == t[1] && *t == t[2]); t++) + for (n = 0, t = src; t + 2 < end && !(*t == t[1] && *t == t[2]); t++) { n++; + } while (n) { i = n > 126 ? 126 : n; n -= i; *dest++ = 0x80 | i; - while (i--) + while (i--) { *dest++ = *src++; + } } - if (src == end) + if (src == end) { break; + } patt = *src++; - for (n = 1; src < end && *src == patt; src++) + for (n = 1; src < end && *src == patt; src++) { n++; + } while (n) { i = n > 126 ? 126 : n; @@ -419,8 +445,7 @@ uint SGIImage::compact(uchar *d, uchar *s) return dest - d; } - -bool SGIImage::scanData(const QImage& img) +bool SGIImage::scanData(const QImage &img) { quint32 *start = _starttab; QByteArray lineguard(_xsize * 2, 0); @@ -433,40 +458,46 @@ bool SGIImage::scanData(const QImage& img) for (y = 0; y < _ysize; y++) { c = reinterpret_cast(img.scanLine(_ysize - y - 1)); - for (x = 0; x < _xsize; x++) + for (x = 0; x < _xsize; x++) { buf[x] = intensity(qRed(*c++)); + } len = compact(line, buf); *start++ = _rlemap.insert(line, len); } - if (_zsize == 1) + if (_zsize == 1) { return true; + } if (_zsize != 2) { for (y = 0; y < _ysize; y++) { c = reinterpret_cast(img.scanLine(_ysize - y - 1)); - for (x = 0; x < _xsize; x++) + for (x = 0; x < _xsize; x++) { buf[x] = intensity(qGreen(*c++)); + } len = compact(line, buf); *start++ = _rlemap.insert(line, len); } for (y = 0; y < _ysize; y++) { c = reinterpret_cast(img.scanLine(_ysize - y - 1)); - for (x = 0; x < _xsize; x++) + for (x = 0; x < _xsize; x++) { buf[x] = intensity(qBlue(*c++)); + } len = compact(line, buf); *start++ = _rlemap.insert(line, len); } - if (_zsize == 3) + if (_zsize == 3) { return true; + } } for (y = 0; y < _ysize; y++) { c = reinterpret_cast(img.scanLine(_ysize - y - 1)); - for (x = 0; x < _xsize; x++) + for (x = 0; x < _xsize; x++) { buf[x] = intensity(qAlpha(*c++)); + } len = compact(line, buf); *start++ = _rlemap.insert(line, len); } @@ -474,7 +505,6 @@ bool SGIImage::scanData(const QImage& img) return true; } - void SGIImage::writeHeader() { _stream << quint16(0x01da); @@ -483,16 +513,17 @@ void SGIImage::writeHeader() _stream << _pixmin << _pixmax; _stream << quint32(0); - for (int i = 0; i < 80; i++) + for (int i = 0; i < 80; i++) { _imagename[i] = '\0'; + } _stream.writeRawData(_imagename, 80); _stream << _colormap; - for (int i = 0; i < 404; i++) + for (int i = 0; i < 404; i++) { _stream << quint8(0); + } } - void SGIImage::writeRle() { _rle = 1; @@ -501,20 +532,22 @@ void SGIImage::writeRle() uint i; // write start table - for (i = 0; i < _numrows; i++) + for (i = 0; i < _numrows; i++) { _stream << quint32(_rlevector[_starttab[i]]->offset()); + } // write length table - for (i = 0; i < _numrows; i++) + for (i = 0; i < _numrows; i++) { _stream << quint32(_rlevector[_starttab[i]]->size()); + } // write data - for (i = 0; (int)i < _rlevector.size(); i++) - const_cast(_rlevector[i])->write(_stream); + for (i = 0; (int)i < _rlevector.size(); i++) { + const_cast(_rlevector[i])->write(_stream); + } } - -void SGIImage::writeVerbatim(const QImage& img) +void SGIImage::writeVerbatim(const QImage &img) { _rle = 0; // qDebug() << "writing verbatim data"; @@ -525,49 +558,56 @@ void SGIImage::writeVerbatim(const QImage& img) for (y = 0; y < _ysize; y++) { c = reinterpret_cast(img.scanLine(_ysize - y - 1)); - for (x = 0; x < _xsize; x++) + for (x = 0; x < _xsize; x++) { _stream << quint8(qRed(*c++)); + } } - if (_zsize == 1) + if (_zsize == 1) { return; + } if (_zsize != 2) { for (y = 0; y < _ysize; y++) { c = reinterpret_cast(img.scanLine(_ysize - y - 1)); - for (x = 0; x < _xsize; x++) + for (x = 0; x < _xsize; x++) { _stream << quint8(qGreen(*c++)); + } } for (y = 0; y < _ysize; y++) { c = reinterpret_cast(img.scanLine(_ysize - y - 1)); - for (x = 0; x < _xsize; x++) + for (x = 0; x < _xsize; x++) { _stream << quint8(qBlue(*c++)); + } } - if (_zsize == 3) + if (_zsize == 3) { return; + } } for (y = 0; y < _ysize; y++) { c = reinterpret_cast(img.scanLine(_ysize - y - 1)); - for (x = 0; x < _xsize; x++) + for (x = 0; x < _xsize; x++) { _stream << quint8(qAlpha(*c++)); + } } } - -bool SGIImage::writeImage(const QImage& image) +bool SGIImage::writeImage(const QImage &image) { // qDebug() << "writing "; // TODO add filename QImage img = image; - if (img.allGray()) + if (img.allGray()) { _dim = 2, _zsize = 1; - else + } else { _dim = 3, _zsize = 3; + } - if (img.format() == QImage::Format_ARGB32) + if (img.format() == QImage::Format_ARGB32) { _dim = 3, _zsize++; + } img = img.convertToFormat(QImage::Format_RGB32); if (img.isNull()) { @@ -594,8 +634,9 @@ bool SGIImage::writeImage(const QImage& image) long verbatim_size = _numrows * _xsize; long rle_size = _numrows * 2 * sizeof(quint32); - for (int i = 0; i < _rlevector.size(); i++) + for (int i = 0; i < _rlevector.size(); i++) { rle_size += _rlevector[i]->size(); + } // qDebug() << "minimum intensity: " << _pixmin; // qDebug() << "maximum intensity: " << _pixmax; @@ -603,22 +644,20 @@ bool SGIImage::writeImage(const QImage& image) // qDebug() << "total savings: " << (verbatim_size - rle_size) << " bytes"; // qDebug() << "compression: " << (rle_size * 100.0 / verbatim_size) << '%'; - if (verbatim_size <= rle_size) + if (verbatim_size <= rle_size) { writeVerbatim(img); - else + } else { writeRle(); + } return true; } - /////////////////////////////////////////////////////////////////////////////// - RGBHandler::RGBHandler() { } - bool RGBHandler::canRead() const { if (canRead(device())) { @@ -628,21 +667,18 @@ bool RGBHandler::canRead() const return false; } - bool RGBHandler::read(QImage *outImage) { SGIImage sgi(device()); return sgi.readImage(*outImage); } - bool RGBHandler::write(const QImage &image) { SGIImage sgi(device()); return sgi.writeImage(image); } - bool RGBHandler::canRead(QIODevice *device) { if (!device) { @@ -655,8 +691,9 @@ bool RGBHandler::canRead(QIODevice *device) int readBytes = head.size(); if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); @@ -668,29 +705,31 @@ bool RGBHandler::canRead(QIODevice *device) return data.contains(regexp); } - /////////////////////////////////////////////////////////////////////////////// - QImageIOPlugin::Capabilities RGBPlugin::capabilities(QIODevice *device, const QByteArray &format) const { if (format == "rgb" || format == "rgba" || - format == "bw" || format == "sgi") + format == "bw" || format == "sgi") { return Capabilities(CanRead | CanWrite); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && RGBHandler::canRead(device)) + if (device->isReadable() && RGBHandler::canRead(device)) { cap |= CanRead; - if (device->isWritable()) + } + if (device->isWritable()) { cap |= CanWrite; + } return cap; } - QImageIOHandler *RGBPlugin::create(QIODevice *device, const QByteArray &format) const { QImageIOHandler *handler = new RGBHandler; diff --git a/src/imageformats/tga.cpp b/src/imageformats/tga.cpp index d88ce27..b48445b 100644 --- a/src/imageformats/tga.cpp +++ b/src/imageformats/tga.cpp @@ -74,7 +74,7 @@ struct TgaHeader { enum { SIZE = 18 }; // const static int SIZE = 18; }; -static QDataStream & operator>> (QDataStream & s, TgaHeader & head) +static QDataStream &operator>> (QDataStream &s, TgaHeader &head) { s >> head.id_length; s >> head.colormap_type; @@ -94,7 +94,7 @@ static QDataStream & operator>> (QDataStream & s, TgaHeader & head) return s; } -static bool IsSupported(const TgaHeader & head) +static bool IsSupported(const TgaHeader &head) { if (head.image_type != TGA_TYPE_INDEXED && head.image_type != TGA_TYPE_RGB && @@ -140,25 +140,26 @@ struct TgaHeaderInfo { bool rgb; bool grey; - TgaHeaderInfo(const TgaHeader & tga) : rle(false), pal(false), rgb(false), grey(false) { + TgaHeaderInfo(const TgaHeader &tga) : rle(false), pal(false), rgb(false), grey(false) + { switch (tga.image_type) { case TGA_TYPE_RLE_INDEXED: rle = true; - // no break is intended! + // no break is intended! case TGA_TYPE_INDEXED: pal = true; break; case TGA_TYPE_RLE_RGB: rle = true; - // no break is intended! + // no break is intended! case TGA_TYPE_RGB: rgb = true; break; case TGA_TYPE_RLE_GREY: rle = true; - // no break is intended! + // no break is intended! case TGA_TYPE_GREY: grey = true; break; @@ -170,9 +171,7 @@ struct TgaHeaderInfo { } }; - - -static bool LoadTGA(QDataStream & s, const TgaHeader & tga, QImage &img) +static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img) { // Create image. img = QImage(tga.width, tga.height, QImage::Format_RGB32); @@ -202,11 +201,11 @@ static bool LoadTGA(QDataStream & s, const TgaHeader & tga, QImage &img) } // Allocate image. - uchar * const image = new uchar[size]; + uchar *const image = new uchar[size]; if (info.rle) { // Decode image. - char * dst = (char *)image; + char *dst = (char *)image; int num = size; while (num > 0) { @@ -250,10 +249,10 @@ static bool LoadTGA(QDataStream & s, const TgaHeader & tga, QImage &img) y_end = -1; } - uchar * src = image; + uchar *src = image; for (int y = y_start; y != y_end; y += y_step) { - QRgb * scanline = (QRgb *) img.scanLine(y); + QRgb *scanline = (QRgb *) img.scanLine(y); if (info.pal) { // Paletted. @@ -299,7 +298,6 @@ static bool LoadTGA(QDataStream & s, const TgaHeader & tga, QImage &img) } // namespace - TGAHandler::TGAHandler() { } @@ -320,7 +318,6 @@ bool TGAHandler::read(QImage *outImage) QDataStream s(device()); s.setByteOrder(QDataStream::LittleEndian); - // Read image header. TgaHeader tga; s >> tga; @@ -338,7 +335,6 @@ bool TGAHandler::read(QImage *outImage) return false; } - QImage img; bool result = LoadTGA(s, tga, img); @@ -347,7 +343,6 @@ bool TGAHandler::read(QImage *outImage) return false; } - *outImage = img; return true; } @@ -357,10 +352,11 @@ bool TGAHandler::write(const QImage &image) QDataStream s(device()); s.setByteOrder(QDataStream::LittleEndian); - const QImage& img = image; + const QImage &img = image; const bool hasAlpha = (img.format() == QImage::Format_ARGB32); - for (int i = 0; i < 12; i++) + for (int i = 0; i < 12; i++) { s << targaMagic[i]; + } // write header s << quint16(img.width()); // width @@ -374,8 +370,9 @@ bool TGAHandler::write(const QImage &image) s << quint8(qBlue(color)); s << quint8(qGreen(color)); s << quint8(qRed(color)); - if (hasAlpha) + if (hasAlpha) { s << quint8(qAlpha(color)); + } } return true; @@ -413,18 +410,23 @@ bool TGAHandler::canRead(QIODevice *device) QImageIOPlugin::Capabilities TGAPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "tga") + if (format == "tga") { return Capabilities(CanRead | CanWrite); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && TGAHandler::canRead(device)) + if (device->isReadable() && TGAHandler::canRead(device)) { cap |= CanRead; - if (device->isWritable()) + } + if (device->isWritable()) { cap |= CanWrite; + } return cap; } diff --git a/src/imageformats/xcf.cpp b/src/imageformats/xcf.cpp index f8eecaf..2bf2a9c 100644 --- a/src/imageformats/xcf.cpp +++ b/src/imageformats/xcf.cpp @@ -42,15 +42,12 @@ const float INCHESPERMETER = (100.0f / 2.54f); */ typedef QVector > Tiles; - - class XCFImageFormat { public: XCFImageFormat(); bool readXCF(QIODevice *device, QImage *image); - private: /*! * Each GIMP image is composed of one or more layers. A layer can @@ -66,7 +63,7 @@ private: quint32 width; //!< Width of the layer quint32 height; //!< Height of the layer qint32 type; //!< Type of the layer (GimpImageType) - char* name; //!< Name of the layer + char *name; //!< Name of the layer quint32 hierarchy_offset; //!< File position of Tile hierarchy quint32 mask_offset; //!< File position of mask image @@ -102,21 +99,21 @@ private: quint32 tattoo; //!< (unique identifier?) //! As each tile is read from the file, it is buffered here. - uchar tile[TILE_WIDTH * TILE_HEIGHT * sizeof(QRgb)]; + uchar tile[TILE_WIDTH *TILE_HEIGHT *sizeof(QRgb)]; //! The data from tile buffer is copied to the Tile by this //! method. Depending on the type of the tile (RGB, Grayscale, //! Indexed) and use (image or mask), the bytes in the buffer are //! copied in different ways. - void (*assignBytes)(Layer& layer, uint i, uint j); + void (*assignBytes)(Layer &layer, uint i, uint j); Layer(void) : name(0) {} - ~Layer(void) { + ~Layer(void) + { delete[] name; } }; - /*! * The in-memory representation of the XCF Image. It contains a few * metadata items, but is mostly a container for the layer information. @@ -145,7 +142,6 @@ private: XCFImage(void) : initialized(false) {} }; - //! In layer DISSOLVE mode, a random number is chosen to compare to a //! pixel's alpha. If the alpha is greater than the random number, the //! pixel is drawn. This table merely contains the random number seeds @@ -165,12 +161,12 @@ private: //! The bottom-most layer is copied into the final QImage by this //! routine. - typedef void (*PixelCopyOperation)(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); + typedef void (*PixelCopyOperation)(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); //! Higher layers are merged into the final QImage by this routine. - typedef void (*PixelMergeOperation)(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); + typedef void (*PixelMergeOperation)(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); //! Layer mode static data. typedef struct { @@ -181,69 +177,67 @@ private: //! LayerModeEffects. static const LayerModes layer_modes[]; - bool loadImageProperties(QDataStream& xcf_io, XCFImage& image); - bool loadProperty(QDataStream& xcf_io, PropType& type, QByteArray& bytes); - bool loadLayer(QDataStream& xcf_io, XCFImage& xcf_image); - bool loadLayerProperties(QDataStream& xcf_io, Layer& layer); - bool composeTiles(XCFImage& xcf_image); - void setGrayPalette(QImage& image); - void setPalette(XCFImage& xcf_image, QImage& image); - static void assignImageBytes(Layer& layer, uint i, uint j); - bool loadHierarchy(QDataStream& xcf_io, Layer& layer); - bool loadLevel(QDataStream& xcf_io, Layer& layer, qint32 bpp); - static void assignMaskBytes(Layer& layer, uint i, uint j); - bool loadMask(QDataStream& xcf_io, Layer& layer); - bool loadChannelProperties(QDataStream& xcf_io, Layer& layer); - bool initializeImage(XCFImage& xcf_image); - bool loadTileRLE(QDataStream& xcf_io, uchar* tile, int size, + bool loadImageProperties(QDataStream &xcf_io, XCFImage &image); + bool loadProperty(QDataStream &xcf_io, PropType &type, QByteArray &bytes); + bool loadLayer(QDataStream &xcf_io, XCFImage &xcf_image); + bool loadLayerProperties(QDataStream &xcf_io, Layer &layer); + bool composeTiles(XCFImage &xcf_image); + void setGrayPalette(QImage &image); + void setPalette(XCFImage &xcf_image, QImage &image); + static void assignImageBytes(Layer &layer, uint i, uint j); + bool loadHierarchy(QDataStream &xcf_io, Layer &layer); + bool loadLevel(QDataStream &xcf_io, Layer &layer, qint32 bpp); + static void assignMaskBytes(Layer &layer, uint i, uint j); + bool loadMask(QDataStream &xcf_io, Layer &layer); + bool loadChannelProperties(QDataStream &xcf_io, Layer &layer); + bool initializeImage(XCFImage &xcf_image); + bool loadTileRLE(QDataStream &xcf_io, uchar *tile, int size, int data_length, qint32 bpp); - static void copyLayerToImage(XCFImage& xcf_image); - static void copyRGBToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void copyGrayToGray(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void copyGrayToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void copyGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void copyIndexedToIndexed(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void copyIndexedAToIndexed(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void copyIndexedAToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); + static void copyLayerToImage(XCFImage &xcf_image); + static void copyRGBToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void copyGrayToGray(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void copyGrayToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void copyGrayAToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void copyIndexedToIndexed(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void copyIndexedAToIndexed(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void copyIndexedAToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); - static void mergeLayerIntoImage(XCFImage& xcf_image); - static void mergeRGBToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void mergeGrayToGray(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void mergeGrayAToGray(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void mergeGrayToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void mergeGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void mergeIndexedToIndexed(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void mergeIndexedAToIndexed(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); - static void mergeIndexedAToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n); + static void mergeLayerIntoImage(XCFImage &xcf_image); + static void mergeRGBToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void mergeGrayToGray(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void mergeGrayAToGray(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void mergeGrayToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void mergeGrayAToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void mergeIndexedToIndexed(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void mergeIndexedAToIndexed(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); + static void mergeIndexedAToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n); static void initializeRandomTable(); - static void dissolveRGBPixels(QImage& image, int x, int y); - static void dissolveAlphaPixels(QImage& image, int x, int y); + static void dissolveRGBPixels(QImage &image, int x, int y); + static void dissolveAlphaPixels(QImage &image, int x, int y); }; - int XCFImageFormat::random_table[RANDOM_TABLE_SIZE]; bool XCFImageFormat::random_table_initialized; QVector XCFImageFormat::grayTable; - const XCFImageFormat::LayerModes XCFImageFormat::layer_modes[] = { {true}, // NORMAL_MODE {true}, // DISSOLVE_MODE @@ -269,14 +263,12 @@ const XCFImageFormat::LayerModes XCFImageFormat::layer_modes[] = { {false}, // GRAIN_MERGE_MODE }; - //! Change a QRgb value's alpha only. -inline QRgb qRgba(const QRgb& rgb, int a) +inline QRgb qRgba(const QRgb &rgb, int a) { return ((a & 0xff) << 24 | (rgb & RGB_MASK)); } - /*! * The constructor for the XCF image loader. */ @@ -292,8 +284,9 @@ void XCFImageFormat::initializeRandomTable() // From GIMP "paint_funcs.c" v1.2 srand(RANDOM_SEED); - for (int i = 0; i < RANDOM_TABLE_SIZE; i++) + for (int i = 0; i < RANDOM_TABLE_SIZE; i++) { random_table[i] = rand(); + } for (int i = 0; i < RANDOM_TABLE_SIZE; i++) { int tmp; @@ -329,8 +322,9 @@ bool XCFImageFormat::readXCF(QIODevice *device, QImage *outImage) xcf_io >> xcf_image.width >> xcf_image.height >> xcf_image.type; // qDebug() << tag << " " << xcf_image.width << " " << xcf_image.height << " " << xcf_image.type; - if (!loadImageProperties(xcf_io, xcf_image)) + if (!loadImageProperties(xcf_io, xcf_image)) { return false; + } // The layers appear to be stored in top-to-bottom order. This is // the reverse of how a merged image must be computed. So, the layer @@ -345,8 +339,9 @@ bool XCFImageFormat::readXCF(QIODevice *device, QImage *outImage) xcf_io >> layer_offset; - if (layer_offset == 0) + if (layer_offset == 0) { break; + } layer_offsets.push(layer_offset); } @@ -363,8 +358,9 @@ bool XCFImageFormat::readXCF(QIODevice *device, QImage *outImage) xcf_io.device()->seek(layer_offset); - if (!loadLayer(xcf_io, xcf_image)) + if (!loadLayer(xcf_io, xcf_image)) { return false; + } } if (!xcf_image.initialized) { @@ -376,7 +372,6 @@ bool XCFImageFormat::readXCF(QIODevice *device, QImage *outImage) return true; } - /*! * An XCF file can contain an arbitrary number of properties associated * with the image (and layer and mask). @@ -384,7 +379,7 @@ bool XCFImageFormat::readXCF(QIODevice *device, QImage *outImage) * \param xcf_image XCF image data. * \return true if there were no I/O errors. */ -bool XCFImageFormat::loadImageProperties(QDataStream& xcf_io, XCFImage& xcf_image) +bool XCFImageFormat::loadImageProperties(QDataStream &xcf_io, XCFImage &xcf_image) { while (true) { PropType type; @@ -415,17 +410,18 @@ bool XCFImageFormat::loadImageProperties(QDataStream& xcf_io, XCFImage& xcf_imag case PROP_PARASITES: while (!property.atEnd()) { - char* tag; + char *tag; quint32 size; property.readBytes(tag, size); quint32 flags; - char* data = 0; + char *data = 0; property >> flags >> data; - if (tag && strncmp(tag, "gimp-comment", strlen("gimp-comment")) == 0) + if (tag && strncmp(tag, "gimp-comment", strlen("gimp-comment")) == 0) { xcf_image.image.setText(QStringLiteral("Comment"), QString::fromUtf8(data)); + } delete[] tag; delete[] data; @@ -444,8 +440,9 @@ bool XCFImageFormat::loadImageProperties(QDataStream& xcf_io, XCFImage& xcf_imag case PROP_COLORMAP: property >> xcf_image.num_colors; - if (xcf_image.num_colors < 0 || xcf_image.num_colors > 65535) + if (xcf_image.num_colors < 0 || xcf_image.num_colors > 65535) { return false; + } xcf_image.palette.reserve(xcf_image.num_colors); @@ -464,7 +461,6 @@ bool XCFImageFormat::loadImageProperties(QDataStream& xcf_io, XCFImage& xcf_imag } } - /*! * Read a single property from the image file. The property type is returned * in type and the data is returned in bytes. @@ -472,13 +468,13 @@ bool XCFImageFormat::loadImageProperties(QDataStream& xcf_io, XCFImage& xcf_imag * \param type returns with the property type. * \param bytes returns with the property data. * \return true if there were no IO errors. */ -bool XCFImageFormat::loadProperty(QDataStream& xcf_io, PropType& type, QByteArray& bytes) +bool XCFImageFormat::loadProperty(QDataStream &xcf_io, PropType &type, QByteArray &bytes) { quint32 foo; xcf_io >> foo; type = PropType(foo); // TODO urks - char* data = 0; + char *data = 0; quint32 size; // The colormap property size is not the correct number of bytes: @@ -490,8 +486,9 @@ bool XCFImageFormat::loadProperty(QDataStream& xcf_io, PropType& type, QByteArra quint32 ncolors; xcf_io >> ncolors; - if (size > 65535 || size < 4) + if (size > 65535 || size < 4) { return false; + } size = 3 * ncolors + 4; data = new char[size]; @@ -512,7 +509,7 @@ bool XCFImageFormat::loadProperty(QDataStream& xcf_io, PropType& type, QByteArra xcf_io >> size >> factor >> digits; for (int i = 0; i < 5; i++) { - char* unit_strings; + char *unit_strings; xcf_io >> unit_strings; @@ -527,21 +524,22 @@ bool XCFImageFormat::loadProperty(QDataStream& xcf_io, PropType& type, QByteArra size = 0; } else { xcf_io >> size; - if (size > 256000) + if (size > 256000) { return false; + } data = new char[size]; xcf_io.readRawData(data, size); } - if (size != 0 && data) + if (size != 0 && data) { bytes = QByteArray(data, size); + } delete [] data; return true; } - /*! * Load a layer from the XCF file. The data stream must be positioned at * the beginning of the layer data. @@ -550,15 +548,16 @@ bool XCFImageFormat::loadProperty(QDataStream& xcf_io, PropType& type, QByteArra * (if the image is indexed). * \return true if there were no I/O errors. */ -bool XCFImageFormat::loadLayer(QDataStream& xcf_io, XCFImage& xcf_image) +bool XCFImageFormat::loadLayer(QDataStream &xcf_io, XCFImage &xcf_image) { - Layer& layer(xcf_image.layer); + Layer &layer(xcf_image.layer); delete[] layer.name; xcf_io >> layer.width >> layer.height >> layer.type >> layer.name; - if (!loadLayerProperties(xcf_io, layer)) + if (!loadLayerProperties(xcf_io, layer)) { return false; + } #if 0 cout << "layer: \"" << layer.name << "\", size: " << layer.width << " x " << layer.height << ", type: " << layer.type << ", mode: " << layer.mode @@ -569,8 +568,9 @@ bool XCFImageFormat::loadLayer(QDataStream& xcf_io, XCFImage& xcf_image) // you export an image from the The GIMP it flattens (or merges) only // the visible layers into the output image. - if (layer.visible == 0) + if (layer.visible == 0) { return true; + } // If there are any more layers, merge them into the final QImage. @@ -579,8 +579,9 @@ bool XCFImageFormat::loadLayer(QDataStream& xcf_io, XCFImage& xcf_image) // Allocate the individual tile QImages based on the size and type // of this layer. - if (!composeTiles(xcf_image)) + if (!composeTiles(xcf_image)) { return false; + } xcf_io.device()->seek(layer.hierarchy_offset); // As tiles are loaded, they are copied into the layers tiles by @@ -589,14 +590,16 @@ bool XCFImageFormat::loadLayer(QDataStream& xcf_io, XCFImage& xcf_image) layer.assignBytes = assignImageBytes; - if (!loadHierarchy(xcf_io, layer)) + if (!loadHierarchy(xcf_io, layer)) { return false; + } if (layer.mask_offset != 0) { xcf_io.device()->seek(layer.mask_offset); - if (!loadMask(xcf_io, layer)) + if (!loadMask(xcf_io, layer)) { return false; + } } // Now we should have enough information to initialize the final @@ -604,17 +607,18 @@ bool XCFImageFormat::loadLayer(QDataStream& xcf_io, XCFImage& xcf_image) // of the QImage. if (!xcf_image.initialized) { - if (!initializeImage(xcf_image)) + if (!initializeImage(xcf_image)) { return false; + } copyLayerToImage(xcf_image); xcf_image.initialized = true; - } else + } else { mergeLayerIntoImage(xcf_image); + } return true; } - /*! * An XCF file can contain an arbitrary number of properties associated * with a layer. @@ -622,7 +626,7 @@ bool XCFImageFormat::loadLayer(QDataStream& xcf_io, XCFImage& xcf_image) * \param layer layer to collect the properties. * \return true if there were no I/O errors. */ -bool XCFImageFormat::loadLayerProperties(QDataStream& xcf_io, Layer& layer) +bool XCFImageFormat::loadLayerProperties(QDataStream &xcf_io, Layer &layer) { while (true) { PropType type; @@ -691,15 +695,14 @@ bool XCFImageFormat::loadLayerProperties(QDataStream& xcf_io, Layer& layer) } } - /*! * Compute the number of tiles in the current layer and allocate * QImage structures for each of them. * \param xcf_image contains the current layer. */ -bool XCFImageFormat::composeTiles(XCFImage& xcf_image) +bool XCFImageFormat::composeTiles(XCFImage &xcf_image) { - Layer& layer(xcf_image.layer); + Layer &layer(xcf_image.layer); layer.nrows = (layer.height + TILE_HEIGHT - 1) / TILE_HEIGHT; layer.ncols = (layer.width + TILE_WIDTH - 1) / TILE_WIDTH; @@ -710,25 +713,30 @@ bool XCFImageFormat::composeTiles(XCFImage& xcf_image) // SANITY CHECK: Catch corrupted XCF image file where the width or height // of a tile is reported are bogus. See Bug# 234030. - if (layer.width > 32767 || layer.height > 32767 || layer.width * layer.height > 16384 * 16384) + if (layer.width > 32767 || layer.height > 32767 || layer.width * layer.height > 16384 * 16384) { return false; + } layer.image_tiles.resize(layer.nrows); - if (layer.type == GRAYA_GIMAGE || layer.type == INDEXEDA_GIMAGE) + if (layer.type == GRAYA_GIMAGE || layer.type == INDEXEDA_GIMAGE) { layer.alpha_tiles.resize(layer.nrows); + } - if (layer.mask_offset != 0) + if (layer.mask_offset != 0) { layer.mask_tiles.resize(layer.nrows); + } for (uint j = 0; j < layer.nrows; j++) { layer.image_tiles[j].resize(layer.ncols); - if (layer.type == GRAYA_GIMAGE || layer.type == INDEXEDA_GIMAGE) + if (layer.type == GRAYA_GIMAGE || layer.type == INDEXEDA_GIMAGE) { layer.alpha_tiles[j].resize(layer.ncols); + } - if (layer.mask_offset != 0) + if (layer.mask_offset != 0) { layer.mask_tiles[j].resize(layer.ncols); + } } for (uint j = 0; j < layer.nrows; j++) { @@ -747,66 +755,75 @@ bool XCFImageFormat::composeTiles(XCFImage& xcf_image) case RGB_GIMAGE: layer.image_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_RGB32); layer.image_tiles[j][i].setColorCount(0); - if (layer.image_tiles[j][i].isNull()) + if (layer.image_tiles[j][i].isNull()) { return false; + } break; case RGBA_GIMAGE: layer.image_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_ARGB32); layer.image_tiles[j][i].setColorCount(0); - if (layer.image_tiles[j][i].isNull()) + if (layer.image_tiles[j][i].isNull()) { return false; + } break; case GRAY_GIMAGE: layer.image_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_Indexed8); layer.image_tiles[j][i].setColorCount(256); - if (layer.image_tiles[j][i].isNull()) + if (layer.image_tiles[j][i].isNull()) { return false; + } setGrayPalette(layer.image_tiles[j][i]); break; case GRAYA_GIMAGE: layer.image_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_Indexed8); layer.image_tiles[j][i].setColorCount(256); - if (layer.image_tiles[j][i].isNull()) + if (layer.image_tiles[j][i].isNull()) { return false; + } setGrayPalette(layer.image_tiles[j][i]); layer.alpha_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_Indexed8); layer.alpha_tiles[j][i].setColorCount(256); - if (layer.alpha_tiles[j][i].isNull()) + if (layer.alpha_tiles[j][i].isNull()) { return false; + } setGrayPalette(layer.alpha_tiles[j][i]); break; case INDEXED_GIMAGE: layer.image_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_Indexed8); layer.image_tiles[j][i].setColorCount(xcf_image.num_colors); - if (layer.image_tiles[j][i].isNull()) + if (layer.image_tiles[j][i].isNull()) { return false; + } setPalette(xcf_image, layer.image_tiles[j][i]); break; case INDEXEDA_GIMAGE: layer.image_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_Indexed8); layer.image_tiles[j][i].setColorCount(xcf_image.num_colors); - if (layer.image_tiles[j][i].isNull()) + if (layer.image_tiles[j][i].isNull()) { return false; + } setPalette(xcf_image, layer.image_tiles[j][i]); layer.alpha_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_Indexed8); layer.alpha_tiles[j][i].setColorCount(256); - if (layer.alpha_tiles[j][i].isNull()) + if (layer.alpha_tiles[j][i].isNull()) { return false; + } setGrayPalette(layer.alpha_tiles[j][i]); } if (layer.mask_offset != 0) { layer.mask_tiles[j][i] = QImage(tile_width, tile_height, QImage::Format_Indexed8); layer.mask_tiles[j][i].setColorCount(256); - if (layer.mask_tiles[j][i].isNull()) + if (layer.mask_tiles[j][i].isNull()) { return false; + } setGrayPalette(layer.mask_tiles[j][i]); } } @@ -814,39 +831,37 @@ bool XCFImageFormat::composeTiles(XCFImage& xcf_image) return true; } - /*! * Apply a grayscale palette to the QImage. Note that Qt does not distinguish * between grayscale and indexed images. A grayscale image is just * an indexed image with a 256-color, grayscale palette. * \param image image to set to a grayscale palette. */ -void XCFImageFormat::setGrayPalette(QImage& image) +void XCFImageFormat::setGrayPalette(QImage &image) { if (grayTable.isEmpty()) { grayTable.resize(256); - for (int i = 0; i < 256; i++) + for (int i = 0; i < 256; i++) { grayTable[i] = qRgb(i, i, i); + } } image.setColorTable(grayTable); } - /*! * Copy the indexed palette from the XCF image into the QImage. * \param xcf_image XCF image containing the palette read from the data stream. * \param image image to apply the palette to. */ -void XCFImageFormat::setPalette(XCFImage& xcf_image, QImage& image) +void XCFImageFormat::setPalette(XCFImage &xcf_image, QImage &image) { Q_ASSERT(xcf_image.num_colors == xcf_image.palette.size()); image.setColorTable(xcf_image.palette); } - /*! * Copy the bytes from the tile buffer into the image tile QImage, taking into * account all the myriad different modes. @@ -854,10 +869,10 @@ void XCFImageFormat::setPalette(XCFImage& xcf_image, QImage& image) * \param i column index of current tile. * \param j row index of current tile. */ -void XCFImageFormat::assignImageBytes(Layer& layer, uint i, uint j) +void XCFImageFormat::assignImageBytes(Layer &layer, uint i, uint j) { QImage &image = layer.image_tiles[j][i]; - uchar* tile = layer.tile; + uchar *tile = layer.tile; const int width = image.width(); const int height = image.height(); const int bytesPerLine = image.bytesPerLine(); @@ -906,8 +921,9 @@ void XCFImageFormat::assignImageBytes(Layer& layer, uint i, uint j) // are some cases where the image can contain larger indices // than there are colors in the palette. (A bug in The GIMP?) - if (tile[0] < image.colorCount()) + if (tile[0] < image.colorCount()) { *dataPtr = tile[0]; + } *alphaPtr = tile[1]; dataPtr += 1; @@ -919,7 +935,6 @@ void XCFImageFormat::assignImageBytes(Layer& layer, uint i, uint j) } } - /*! * The GIMP stores images in a "mipmap"-like hierarchy. As far as the QImage * is concerned, however, only the top level (i.e., the full resolution image) @@ -928,7 +943,7 @@ void XCFImageFormat::assignImageBytes(Layer& layer, uint i, uint j) * \param layer the layer to collect the image. * \return true if there were no I/O errors. */ -bool XCFImageFormat::loadHierarchy(QDataStream& xcf_io, Layer& layer) +bool XCFImageFormat::loadHierarchy(QDataStream &xcf_io, Layer &layer) { qint32 width; qint32 height; @@ -954,14 +969,14 @@ bool XCFImageFormat::loadHierarchy(QDataStream& xcf_io, Layer& layer) qint64 saved_pos = xcf_io.device()->pos(); xcf_io.device()->seek(offset); - if (!loadLevel(xcf_io, layer, bpp)) + if (!loadLevel(xcf_io, layer, bpp)) { return false; + } xcf_io.device()->seek(saved_pos); return true; } - /*! * Load one level of the image hierarchy (but only the top level is ever used). * \param xcf_io the data stream connected to the XCF image. @@ -970,7 +985,7 @@ bool XCFImageFormat::loadHierarchy(QDataStream& xcf_io, Layer& layer) * \return true if there were no I/O errors. * \sa loadTileRLE(). */ -bool XCFImageFormat::loadLevel(QDataStream& xcf_io, Layer& layer, qint32 bpp) +bool XCFImageFormat::loadLevel(QDataStream &xcf_io, Layer &layer, qint32 bpp) { qint32 width; qint32 height; @@ -978,8 +993,9 @@ bool XCFImageFormat::loadLevel(QDataStream& xcf_io, Layer& layer, qint32 bpp) xcf_io >> width >> height >> offset; - if (offset == 0) + if (offset == 0) { return true; + } for (uint j = 0; j < layer.nrows; j++) { for (uint i = 0; i < layer.ncols; i++) { @@ -995,14 +1011,16 @@ bool XCFImageFormat::loadLevel(QDataStream& xcf_io, Layer& layer, qint32 bpp) // Evidently, RLE can occasionally expand a tile instead of compressing it! - if (offset2 == 0) + if (offset2 == 0) { offset2 = offset + (uint)(TILE_WIDTH * TILE_HEIGHT * 4 * 1.5); + } xcf_io.device()->seek(offset); int size = layer.image_tiles[j][i].width() * layer.image_tiles[j][i].height(); - if (!loadTileRLE(xcf_io, layer.tile, size, offset2 - offset, bpp)) + if (!loadTileRLE(xcf_io, layer.tile, size, offset2 - offset, bpp)) { return false; + } // The bytes in the layer tile are juggled differently depending on // the target QImage. The caller has set layer.assignBytes to the @@ -1018,25 +1036,25 @@ bool XCFImageFormat::loadLevel(QDataStream& xcf_io, Layer& layer, qint32 bpp) return true; } - /*! * A layer can have a one channel image which is used as a mask. * \param xcf_io the data stream connected to the XCF image. * \param layer the layer to collect the mask image. * \return true if there were no I/O errors. */ -bool XCFImageFormat::loadMask(QDataStream& xcf_io, Layer& layer) +bool XCFImageFormat::loadMask(QDataStream &xcf_io, Layer &layer) { qint32 width; qint32 height; - char* name; + char *name; xcf_io >> width >> height >> name; delete name; - if (!loadChannelProperties(xcf_io, layer)) + if (!loadChannelProperties(xcf_io, layer)) { return false; + } quint32 hierarchy_offset; xcf_io >> hierarchy_offset; @@ -1044,13 +1062,13 @@ bool XCFImageFormat::loadMask(QDataStream& xcf_io, Layer& layer) xcf_io.device()->seek(hierarchy_offset); layer.assignBytes = assignMaskBytes; - if (!loadHierarchy(xcf_io, layer)) + if (!loadHierarchy(xcf_io, layer)) { return false; + } return true; } - /*! * This is the routine for which all the other code is simply * infrastructure. Read the image bytes out of the file and @@ -1074,14 +1092,14 @@ bool XCFImageFormat::loadMask(QDataStream& xcf_io, Layer& layer) * \return true if there were no I/O errors and no obvious corruption of * the RLE data. */ -bool XCFImageFormat::loadTileRLE(QDataStream& xcf_io, uchar* tile, int image_size, +bool XCFImageFormat::loadTileRLE(QDataStream &xcf_io, uchar *tile, int image_size, int data_length, qint32 bpp) { - uchar* data; + uchar *data; - uchar* xcfdata; - uchar* xcfodata; - uchar* xcfdatalimit; + uchar *xcfdata; + uchar *xcfodata; + uchar *xcfdatalimit; if (data_length < 0 || data_length > int(TILE_WIDTH * TILE_HEIGHT * 4 * 1.5)) { // qDebug() << "XCF: invalid tile data length" << data_length; @@ -1090,7 +1108,7 @@ bool XCFImageFormat::loadTileRLE(QDataStream& xcf_io, uchar* tile, int image_siz xcfdata = xcfodata = new uchar[data_length]; - xcf_io.readRawData((char*)xcfdata, data_length); + xcf_io.readRawData((char *)xcfdata, data_length); if (!xcf_io.device()->isOpen()) { delete[] xcfodata; @@ -1108,8 +1126,9 @@ bool XCFImageFormat::loadTileRLE(QDataStream& xcf_io, uchar* tile, int image_siz int size = image_size; while (size > 0) { - if (xcfdata > xcfdatalimit) + if (xcfdata > xcfdatalimit) { goto bogus_rle; + } uchar val = *xcfdata++; uint length = val; @@ -1117,8 +1136,9 @@ bool XCFImageFormat::loadTileRLE(QDataStream& xcf_io, uchar* tile, int image_siz if (length >= 128) { length = 255 - (length - 1); if (length == 128) { - if (xcfdata >= xcfdatalimit) + if (xcfdata >= xcfdatalimit) { goto bogus_rle; + } length = (*xcfdata << 8) + xcfdata[1]; @@ -1128,11 +1148,13 @@ bool XCFImageFormat::loadTileRLE(QDataStream& xcf_io, uchar* tile, int image_siz count += length; size -= length; - if (size < 0) + if (size < 0) { goto bogus_rle; + } - if (&xcfdata[length - 1] > xcfdatalimit) + if (&xcfdata[length - 1] > xcfdatalimit) { goto bogus_rle; + } while (length-- > 0) { *data = *xcfdata++; @@ -1141,8 +1163,9 @@ bool XCFImageFormat::loadTileRLE(QDataStream& xcf_io, uchar* tile, int image_siz } else { length += 1; if (length == 128) { - if (xcfdata >= xcfdatalimit) + if (xcfdata >= xcfdatalimit) { goto bogus_rle; + } length = (*xcfdata << 8) + xcfdata[1]; xcfdata += 2; @@ -1151,11 +1174,13 @@ bool XCFImageFormat::loadTileRLE(QDataStream& xcf_io, uchar* tile, int image_siz count += length; size -= length; - if (size < 0) + if (size < 0) { goto bogus_rle; + } - if (xcfdata > xcfdatalimit) + if (xcfdata > xcfdatalimit) { goto bogus_rle; + } val = *xcfdata++; @@ -1177,7 +1202,6 @@ bogus_rle: return false; } - /*! * An XCF file can contain an arbitrary number of properties associated * with a channel. Note that this routine only reads mask channel properties. @@ -1185,7 +1209,7 @@ bogus_rle: * \param layer layer containing the mask channel to collect the properties. * \return true if there were no I/O errors. */ -bool XCFImageFormat::loadChannelProperties(QDataStream& xcf_io, Layer& layer) +bool XCFImageFormat::loadChannelProperties(QDataStream &xcf_io, Layer &layer) { while (true) { PropType type; @@ -1231,17 +1255,16 @@ bool XCFImageFormat::loadChannelProperties(QDataStream& xcf_io, Layer& layer) } } - /*! * Copy the bytes from the tile buffer into the mask tile QImage. * \param layer layer containing the tile buffer and the mask tile matrix. * \param i column index of current tile. * \param j row index of current tile. */ -void XCFImageFormat::assignMaskBytes(Layer& layer, uint i, uint j) +void XCFImageFormat::assignMaskBytes(Layer &layer, uint i, uint j) { QImage &image = layer.mask_tiles[j][i]; - uchar* tile = layer.tile; + uchar *tile = layer.tile; const int width = image.width(); const int height = image.height(); const int bytesPerLine = image.bytesPerLine(); @@ -1256,7 +1279,6 @@ void XCFImageFormat::assignMaskBytes(Layer& layer, uint i, uint j) } } - /*! * Construct the QImage which will eventually be returned to the QImage * loader. @@ -1285,26 +1307,28 @@ void XCFImageFormat::assignMaskBytes(Layer& layer, uint i, uint j) * For indexed images, translucency is an all or nothing effect. * \param xcf_image contains image info and bottom-most layer. */ -bool XCFImageFormat::initializeImage(XCFImage& xcf_image) +bool XCFImageFormat::initializeImage(XCFImage &xcf_image) { // (Aliases to make the code look a little better.) - Layer& layer(xcf_image.layer); - QImage& image(xcf_image.image); + Layer &layer(xcf_image.layer); + QImage &image(xcf_image.image); switch (layer.type) { case RGB_GIMAGE: if (layer.opacity == OPAQUE_OPACITY) { image = QImage(xcf_image.width, xcf_image.height, QImage::Format_RGB32); - if (image.isNull()) + if (image.isNull()) { return false; + } image.fill(qRgb(255, 255, 255)); break; } // else, fall through to 32-bit representation case RGBA_GIMAGE: image = QImage(xcf_image.width, xcf_image.height, QImage::Format_ARGB32); - if (image.isNull()) + if (image.isNull()) { return false; + } image.fill(qRgba(255, 255, 255, 0)); break; @@ -1312,8 +1336,9 @@ bool XCFImageFormat::initializeImage(XCFImage& xcf_image) if (layer.opacity == OPAQUE_OPACITY) { image = QImage(xcf_image.width, xcf_image.height, QImage::Format_Indexed8); image.setColorCount(256); - if (image.isNull()) + if (image.isNull()) { return false; + } setGrayPalette(image); image.fill(255); break; @@ -1321,8 +1346,9 @@ bool XCFImageFormat::initializeImage(XCFImage& xcf_image) case GRAYA_GIMAGE: image = QImage(xcf_image.width, xcf_image.height, QImage::Format_ARGB32); - if (image.isNull()) + if (image.isNull()) { return false; + } image.fill(qRgba(255, 255, 255, 0)); break; @@ -1342,15 +1368,17 @@ bool XCFImageFormat::initializeImage(XCFImage& xcf_image) if (xcf_image.num_colors <= 2) { image = QImage(xcf_image.width, xcf_image.height, QImage::Format_MonoLSB); image.setColorCount(xcf_image.num_colors); - if (image.isNull()) + if (image.isNull()) { return false; + } image.fill(0); setPalette(xcf_image, image); } else if (xcf_image.num_colors <= 256) { image = QImage(xcf_image.width, xcf_image.height, QImage::Format_Indexed8); image.setColorCount(xcf_image.num_colors); - if (image.isNull()) + if (image.isNull()) { return false; + } image.fill(0); setPalette(xcf_image, image); } @@ -1366,22 +1394,25 @@ bool XCFImageFormat::initializeImage(XCFImage& xcf_image) image = QImage(xcf_image.width, xcf_image.height, QImage::Format_MonoLSB); image.setColorCount(xcf_image.num_colors); - if (image.isNull()) + if (image.isNull()) { return false; + } image.fill(0); setPalette(xcf_image, image); } else if (xcf_image.num_colors < 256) { // Plenty of room to add a transparent color xcf_image.num_colors++; xcf_image.palette.resize(xcf_image.num_colors); - for (int c = xcf_image.num_colors - 1; c >= 1; c--) + for (int c = xcf_image.num_colors - 1; c >= 1; c--) { xcf_image.palette[c] = xcf_image.palette[c - 1]; + } xcf_image.palette[0] = qRgba(255, 255, 255, 0); image = QImage(xcf_image.width, xcf_image.height, QImage::Format_Indexed8); image.setColorCount(xcf_image.num_colors); - if (image.isNull()) + if (image.isNull()) { return false; + } image.fill(0); setPalette(xcf_image, image); } else { @@ -1389,8 +1420,9 @@ bool XCFImageFormat::initializeImage(XCFImage& xcf_image) // true color. (There is no equivalent PNG representation output // from The GIMP as of v1.2.) image = QImage(xcf_image.width, xcf_image.height, QImage::Format_ARGB32); - if (image.isNull()) + if (image.isNull()) { return false; + } image.fill(qRgba(255, 255, 255, 0)); } break; @@ -1401,16 +1433,15 @@ bool XCFImageFormat::initializeImage(XCFImage& xcf_image) return true; } - /*! * Copy a layer into an image, taking account of the manifold modes. The * contents of the image are replaced. * \param xcf_image contains the layer and image to be replaced. */ -void XCFImageFormat::copyLayerToImage(XCFImage& xcf_image) +void XCFImageFormat::copyLayerToImage(XCFImage &xcf_image) { - Layer& layer(xcf_image.layer); - QImage& image(xcf_image.image); + Layer &layer(xcf_image.layer); + QImage &image(xcf_image.image); PixelCopyOperation copy = 0; switch (layer.type) { @@ -1419,10 +1450,11 @@ void XCFImageFormat::copyLayerToImage(XCFImage& xcf_image) copy = copyRGBToRGB; break; case GRAY_GIMAGE: - if (layer.opacity == OPAQUE_OPACITY) + if (layer.opacity == OPAQUE_OPACITY) { copy = copyGrayToGray; - else + } else { copy = copyGrayToRGB; + } break; case GRAYA_GIMAGE: copy = copyGrayAToRGB; @@ -1431,10 +1463,11 @@ void XCFImageFormat::copyLayerToImage(XCFImage& xcf_image) copy = copyIndexedToIndexed; break; case INDEXEDA_GIMAGE: - if (xcf_image.image.depth() <= 8) + if (xcf_image.image.depth() <= 8) { copy = copyIndexedAToIndexed; - else + } else { copy = copyIndexedAToRGB; + } } if (!copy) { @@ -1459,11 +1492,13 @@ void XCFImageFormat::copyLayerToImage(XCFImage& xcf_image) initializeRandomTable(); random_table_initialized = true; } - if (layer.type == RGBA_GIMAGE) + if (layer.type == RGBA_GIMAGE) { dissolveRGBPixels(layer.image_tiles[j][i], x, y); + } - else if (layer.type == GRAYA_GIMAGE) + else if (layer.type == GRAYA_GIMAGE) { dissolveAlphaPixels(layer.alpha_tiles[j][i], x, y); + } } // Shortcut for common case @@ -1481,8 +1516,9 @@ void XCFImageFormat::copyLayerToImage(XCFImage& xcf_image) int m = x + k + layer.x_offset; int n = y + l + layer.y_offset; - if (m < 0 || m >= image.width() || n < 0 || n >= image.height()) + if (m < 0 || m >= image.width() || n < 0 || n >= image.height()) { continue; + } (*copy)(layer, i, j, k, l, image, m, n); } @@ -1491,7 +1527,6 @@ void XCFImageFormat::copyLayerToImage(XCFImage& xcf_image) } } - /*! * Copy an RGB pixel from the layer to the RGB image. Straight-forward. * The only thing this has to take account of is the opacity of the @@ -1505,25 +1540,26 @@ void XCFImageFormat::copyLayerToImage(XCFImage& xcf_image) * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::copyRGBToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::copyRGBToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { QRgb src = layer.image_tiles[j][i].pixel(k, l); uchar src_a = layer.opacity; - if (layer.type == RGBA_GIMAGE) + if (layer.type == RGBA_GIMAGE) { src_a = INT_MULT(src_a, qAlpha(src)); + } // Apply the mask (if any) if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } image.setPixel(m, n, qRgba(src, src_a)); } - /*! * Copy a Gray pixel from the layer to the Gray image. Straight-forward. * \param layer source layer. @@ -1535,14 +1571,13 @@ void XCFImageFormat::copyRGBToRGB(Layer& layer, uint i, uint j, int k, int l, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::copyGrayToGray(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::copyGrayToGray(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { int src = layer.image_tiles[j][i].pixelIndex(k, l); image.setPixel(m, n, src); } - /*! * Copy a Gray pixel from the layer to an RGB image. Straight-forward. * The only thing this has to take account of is the opacity of the @@ -1556,15 +1591,14 @@ void XCFImageFormat::copyGrayToGray(Layer& layer, uint i, uint j, int k, int l, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::copyGrayToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::copyGrayToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { QRgb src = layer.image_tiles[j][i].pixel(k, l); uchar src_a = layer.opacity; image.setPixel(m, n, qRgba(src, src_a)); } - /*! * Copy a GrayA pixel from the layer to an RGB image. Straight-forward. * The only thing this has to take account of is the opacity of the @@ -1578,8 +1612,8 @@ void XCFImageFormat::copyGrayToRGB(Layer& layer, uint i, uint j, int k, int l, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::copyGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::copyGrayAToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { QRgb src = layer.image_tiles[j][i].pixel(k, l); uchar src_a = layer.alpha_tiles[j][i].pixelIndex(k, l); @@ -1588,13 +1622,13 @@ void XCFImageFormat::copyGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, // Apply the mask (if any) if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } image.setPixel(m, n, qRgba(src, src_a)); } - /*! * Copy an Indexed pixel from the layer to the Indexed image. Straight-forward. * \param layer source layer. @@ -1606,14 +1640,13 @@ void XCFImageFormat::copyGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::copyIndexedToIndexed(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::copyIndexedToIndexed(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { int src = layer.image_tiles[j][i].pixelIndex(k, l); image.setPixel(m, n, src); } - /*! * Copy an IndexedA pixel from the layer to the Indexed image. Straight-forward. * \param layer source layer. @@ -1625,8 +1658,8 @@ void XCFImageFormat::copyIndexedToIndexed(Layer& layer, uint i, uint j, int k, i * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::copyIndexedAToIndexed(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::copyIndexedAToIndexed(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { uchar src = layer.image_tiles[j][i].pixelIndex(k, l); uchar src_a = layer.alpha_tiles[j][i].pixelIndex(k, l); @@ -1634,18 +1667,19 @@ void XCFImageFormat::copyIndexedAToIndexed(Layer& layer, uint i, uint j, int k, if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } - if (src_a > 127) + if (src_a > 127) { src++; - else + } else { src = 0; + } image.setPixel(m, n, src); } - /*! * Copy an IndexedA pixel from the layer to an RGB image. Straight-forward. * The only thing this has to take account of is the opacity of the @@ -1659,8 +1693,8 @@ void XCFImageFormat::copyIndexedAToIndexed(Layer& layer, uint i, uint j, int k, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::copyIndexedAToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::copyIndexedAToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { QRgb src = layer.image_tiles[j][i].pixel(k, l); uchar src_a = layer.alpha_tiles[j][i].pixelIndex(k, l); @@ -1668,31 +1702,34 @@ void XCFImageFormat::copyIndexedAToRGB(Layer& layer, uint i, uint j, int k, int // Apply the mask (if any) if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } // This is what appears in the GIMP window - if (src_a <= 127) + if (src_a <= 127) { src_a = 0; - else + } else { src_a = OPAQUE_OPACITY; + } image.setPixel(m, n, qRgba(src, src_a)); } - /*! * Merge a layer into an image, taking account of the manifold modes. * \param xcf_image contains the layer and image to merge. */ -void XCFImageFormat::mergeLayerIntoImage(XCFImage& xcf_image) +void XCFImageFormat::mergeLayerIntoImage(XCFImage &xcf_image) { - Layer& layer(xcf_image.layer); - QImage& image(xcf_image.image); + Layer &layer(xcf_image.layer); + QImage &image(xcf_image.image); PixelMergeOperation merge = 0; - if (!layer.opacity) return; // don't bother doing anything + if (!layer.opacity) { + return; // don't bother doing anything + } switch (layer.type) { case RGB_GIMAGE: @@ -1700,25 +1737,28 @@ void XCFImageFormat::mergeLayerIntoImage(XCFImage& xcf_image) merge = mergeRGBToRGB; break; case GRAY_GIMAGE: - if (layer.opacity == OPAQUE_OPACITY) + if (layer.opacity == OPAQUE_OPACITY) { merge = mergeGrayToGray; - else + } else { merge = mergeGrayToRGB; + } break; case GRAYA_GIMAGE: - if (xcf_image.image.depth() <= 8) + if (xcf_image.image.depth() <= 8) { merge = mergeGrayAToGray; - else + } else { merge = mergeGrayAToRGB; + } break; case INDEXED_GIMAGE: merge = mergeIndexedToIndexed; break; case INDEXEDA_GIMAGE: - if (xcf_image.image.depth() <= 8) + if (xcf_image.image.depth() <= 8) { merge = mergeIndexedAToIndexed; - else + } else { merge = mergeIndexedAToRGB; + } } if (!merge) { @@ -1741,11 +1781,13 @@ void XCFImageFormat::mergeLayerIntoImage(XCFImage& xcf_image) initializeRandomTable(); random_table_initialized = true; } - if (layer.type == RGBA_GIMAGE) + if (layer.type == RGBA_GIMAGE) { dissolveRGBPixels(layer.image_tiles[j][i], x, y); + } - else if (layer.type == GRAYA_GIMAGE) + else if (layer.type == GRAYA_GIMAGE) { dissolveAlphaPixels(layer.alpha_tiles[j][i], x, y); + } } // Shortcut for common case @@ -1764,8 +1806,9 @@ void XCFImageFormat::mergeLayerIntoImage(XCFImage& xcf_image) int m = x + k + layer.x_offset; int n = y + l + layer.y_offset; - if (m < 0 || m >= image.width() || n < 0 || n >= image.height()) + if (m < 0 || m >= image.width() || n < 0 || n >= image.height()) { continue; + } (*merge)(layer, i, j, k, l, image, m, n); } @@ -1774,7 +1817,6 @@ void XCFImageFormat::mergeLayerIntoImage(XCFImage& xcf_image) } } - /*! * Merge an RGB pixel from the layer to the RGB image. Straight-forward. * The only thing this has to take account of is the opacity of the @@ -1788,8 +1830,8 @@ void XCFImageFormat::mergeLayerIntoImage(XCFImage& xcf_image) * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::mergeRGBToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::mergeRGBToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { QRgb src = layer.image_tiles[j][i].pixel(k, l); QRgb dst = image.pixel(m, n); @@ -1804,7 +1846,9 @@ void XCFImageFormat::mergeRGBToRGB(Layer& layer, uint i, uint j, int k, int l, uchar dst_b = qBlue(dst); uchar dst_a = qAlpha(dst); - if (!src_a) return; // nothing to merge + if (!src_a) { + return; // nothing to merge + } switch (layer.mode) { case MULTIPLY_MODE: { @@ -2080,8 +2124,9 @@ void XCFImageFormat::mergeRGBToRGB(Layer& layer, uint i, uint j, int k, int l, // Apply the mask (if any) if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } uchar new_r, new_g, new_b, new_a; new_a = dst_a + INT_MULT(OPAQUE_OPACITY - dst_a, src_a); @@ -2093,13 +2138,13 @@ void XCFImageFormat::mergeRGBToRGB(Layer& layer, uint i, uint j, int k, int l, new_g = (uchar)(src_ratio * src_g + dst_ratio * dst_g + EPSILON); new_b = (uchar)(src_ratio * src_b + dst_ratio * dst_b + EPSILON); - if (!layer_modes[layer.mode].affect_alpha) + if (!layer_modes[layer.mode].affect_alpha) { new_a = dst_a; + } image.setPixel(m, n, qRgba(new_r, new_g, new_b, new_a)); } - /*! * Merge a Gray pixel from the layer to the Gray image. Straight-forward. * \param layer source layer. @@ -2111,14 +2156,13 @@ void XCFImageFormat::mergeRGBToRGB(Layer& layer, uint i, uint j, int k, int l, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::mergeGrayToGray(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::mergeGrayToGray(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { int src = layer.image_tiles[j][i].pixelIndex(k, l); image.setPixel(m, n, src); } - /*! * Merge a GrayA pixel from the layer to the Gray image. Straight-forward. * \param layer source layer. @@ -2130,15 +2174,17 @@ void XCFImageFormat::mergeGrayToGray(Layer& layer, uint i, uint j, int k, int l, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::mergeGrayAToGray(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::mergeGrayAToGray(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { int src = qGray(layer.image_tiles[j][i].pixel(k, l)); int dst = image.pixelIndex(m, n); uchar src_a = layer.alpha_tiles[j][i].pixelIndex(k, l); - if (!src_a) return; // nothing to merge + if (!src_a) { + return; // nothing to merge + } switch (layer.mode) { case MULTIPLY_MODE: { @@ -2238,8 +2284,9 @@ void XCFImageFormat::mergeGrayAToGray(Layer& layer, uint i, uint j, int k, int l // Apply the mask (if any) if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } uchar new_a = OPAQUE_OPACITY; @@ -2251,7 +2298,6 @@ void XCFImageFormat::mergeGrayAToGray(Layer& layer, uint i, uint j, int k, int l image.setPixel(m, n, new_g); } - /*! * Merge a Gray pixel from the layer to an RGB image. Straight-forward. * The only thing this has to take account of is the opacity of the @@ -2265,15 +2311,14 @@ void XCFImageFormat::mergeGrayAToGray(Layer& layer, uint i, uint j, int k, int l * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::mergeGrayToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::mergeGrayToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { QRgb src = layer.image_tiles[j][i].pixel(k, l); uchar src_a = layer.opacity; image.setPixel(m, n, qRgba(src, src_a)); } - /*! * Merge a GrayA pixel from the layer to an RGB image. Straight-forward. * The only thing this has to take account of is the opacity of the @@ -2287,8 +2332,8 @@ void XCFImageFormat::mergeGrayToRGB(Layer& layer, uint i, uint j, int k, int l, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::mergeGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::mergeGrayAToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { int src = qGray(layer.image_tiles[j][i].pixel(k, l)); int dst = qGray(image.pixel(m, n)); @@ -2296,7 +2341,9 @@ void XCFImageFormat::mergeGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, uchar src_a = layer.alpha_tiles[j][i].pixelIndex(k, l); uchar dst_a = qAlpha(image.pixel(m, n)); - if (!src_a) return; // nothing to merge + if (!src_a) { + return; // nothing to merge + } switch (layer.mode) { case MULTIPLY_MODE: { @@ -2410,8 +2457,9 @@ void XCFImageFormat::mergeGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, // Apply the mask (if any) if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } uchar new_a = dst_a + INT_MULT(OPAQUE_OPACITY - dst_a, src_a); @@ -2420,13 +2468,13 @@ void XCFImageFormat::mergeGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, uchar new_g = (uchar)(src_ratio * src + dst_ratio * dst + EPSILON); - if (!layer_modes[layer.mode].affect_alpha) + if (!layer_modes[layer.mode].affect_alpha) { new_a = dst_a; + } image.setPixel(m, n, qRgba(new_g, new_g, new_g, new_a)); } - /*! * Merge an Indexed pixel from the layer to the Indexed image. Straight-forward. * \param layer source layer. @@ -2438,14 +2486,13 @@ void XCFImageFormat::mergeGrayAToRGB(Layer& layer, uint i, uint j, int k, int l, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::mergeIndexedToIndexed(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::mergeIndexedToIndexed(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { int src = layer.image_tiles[j][i].pixelIndex(k, l); image.setPixel(m, n, src); } - /*! * Merge an IndexedA pixel from the layer to the Indexed image. Straight-forward. * \param layer source layer. @@ -2457,8 +2504,8 @@ void XCFImageFormat::mergeIndexedToIndexed(Layer& layer, uint i, uint j, int k, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::mergeIndexedAToIndexed(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::mergeIndexedAToIndexed(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { uchar src = layer.image_tiles[j][i].pixelIndex(k, l); uchar src_a = layer.alpha_tiles[j][i].pixelIndex(k, l); @@ -2466,8 +2513,9 @@ void XCFImageFormat::mergeIndexedAToIndexed(Layer& layer, uint i, uint j, int k, if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } if (src_a > 127) { src++; @@ -2475,7 +2523,6 @@ void XCFImageFormat::mergeIndexedAToIndexed(Layer& layer, uint i, uint j, int k, } } - /*! * Merge an IndexedA pixel from the layer to an RGB image. Straight-forward. * The only thing this has to take account of is the opacity of the @@ -2489,8 +2536,8 @@ void XCFImageFormat::mergeIndexedAToIndexed(Layer& layer, uint i, uint j, int k, * \param m x pixel of destination image. * \param n y pixel of destination image. */ -void XCFImageFormat::mergeIndexedAToRGB(Layer& layer, uint i, uint j, int k, int l, - QImage& image, int m, int n) +void XCFImageFormat::mergeIndexedAToRGB(Layer &layer, uint i, uint j, int k, int l, + QImage &image, int m, int n) { QRgb src = layer.image_tiles[j][i].pixel(k, l); uchar src_a = layer.alpha_tiles[j][i].pixelIndex(k, l); @@ -2498,19 +2545,20 @@ void XCFImageFormat::mergeIndexedAToRGB(Layer& layer, uint i, uint j, int k, int // Apply the mask (if any) if (layer.apply_mask == 1 && layer.mask_tiles.size() > (int)j && - layer.mask_tiles[j].size() > (int)i) + layer.mask_tiles[j].size() > (int)i) { src_a = INT_MULT(src_a, layer.mask_tiles[j][i].pixelIndex(k, l)); + } // This is what appears in the GIMP window - if (src_a <= 127) + if (src_a <= 127) { src_a = 0; - else + } else { src_a = OPAQUE_OPACITY; + } image.setPixel(m, n, qRgba(src, src_a)); } - /*! * Dissolving pixels: pick a random number between 0 and 255. If the pixel's * alpha is less than that, make it transparent. @@ -2518,7 +2566,7 @@ void XCFImageFormat::mergeIndexedAToRGB(Layer& layer, uint i, uint j, int k, int * \param x the global x position of the tile. * \param y the global y position of the tile. */ -void XCFImageFormat::dissolveRGBPixels(QImage& image, int x, int y) +void XCFImageFormat::dissolveRGBPixels(QImage &image, int x, int y) { // The apparently spurious rand() calls are to wind the random // numbers up to the same point for each tile. @@ -2526,8 +2574,9 @@ void XCFImageFormat::dissolveRGBPixels(QImage& image, int x, int y) for (int l = 0; l < image.height(); l++) { srand(random_table[(l + y) % RANDOM_TABLE_SIZE]); - for (int k = 0; k < x; k++) + for (int k = 0; k < x; k++) { rand(); + } for (int k = 0; k < image.width(); k++) { int rand_val = rand() & 0xff; @@ -2540,7 +2589,6 @@ void XCFImageFormat::dissolveRGBPixels(QImage& image, int x, int y) } } - /*! * Dissolving pixels: pick a random number between 0 and 255. If the pixel's * alpha is less than that, make it transparent. This routine works for @@ -2550,7 +2598,7 @@ void XCFImageFormat::dissolveRGBPixels(QImage& image, int x, int y) * \param x the global x position of the tile. * \param y the global y position of the tile. */ -void XCFImageFormat::dissolveAlphaPixels(QImage& image, int x, int y) +void XCFImageFormat::dissolveAlphaPixels(QImage &image, int x, int y) { // The apparently spurious rand() calls are to wind the random // numbers up to the same point for each tile. @@ -2558,8 +2606,9 @@ void XCFImageFormat::dissolveAlphaPixels(QImage& image, int x, int y) for (int l = 0; l < image.height(); l++) { srand(random_table[(l + y) % RANDOM_TABLE_SIZE]); - for (int k = 0; k < x; k++) + for (int k = 0; k < x; k++) { rand(); + } for (int k = 0; k < image.width(); k++) { int rand_val = rand() & 0xff; @@ -2572,7 +2621,6 @@ void XCFImageFormat::dissolveAlphaPixels(QImage& image, int x, int y) } } - /////////////////////////////////////////////////////////////////////////////// XCFHandler::XCFHandler() @@ -2612,8 +2660,9 @@ bool XCFHandler::canRead(QIODevice *device) qint64 readBytes = device->read(head, sizeof(head)); if (readBytes != sizeof(head)) { if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -2621,8 +2670,9 @@ bool XCFHandler::canRead(QIODevice *device) } if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -2630,19 +2680,22 @@ bool XCFHandler::canRead(QIODevice *device) return qstrncmp(head, "gimp xcf", 8) == 0; } - QImageIOPlugin::Capabilities XCFPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "xcf") + if (format == "xcf") { return Capabilities(CanRead); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && XCFHandler::canRead(device)) + if (device->isReadable() && XCFHandler::canRead(device)) { cap |= CanRead; + } return cap; } diff --git a/src/imageformats/xview.cpp b/src/imageformats/xview.cpp index 2a641b9..af8539a 100644 --- a/src/imageformats/xview.cpp +++ b/src/imageformats/xview.cpp @@ -18,7 +18,6 @@ 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() { } @@ -43,40 +42,47 @@ bool XVHandler::read(QImage *retImage) // magic number must be "P7 332" iodev->readLine(str, BUFSIZE); - if (strncmp(str, "P7 332", 6)) + if (strncmp(str, "P7 332", 6)) { return false; + } // next line #XVVERSION iodev->readLine(str, BUFSIZE); - if (strncmp(str, "#XVVERSION", 10)) + 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)) + 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)) + 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) + if (maxval != 255) { return false; + } int blocksize = x * y; - if (x < 0 || y < 0 || blocksize < x || blocksize < 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) + char *block = (char *) malloc(blocksize); + if (!block) { return false; + } if (iodev->read(block, blocksize) != blocksize) { free(block); @@ -114,7 +120,7 @@ bool XVHandler::read(QImage *retImage) bool XVHandler::write(const QImage &image) { - QIODevice& f = *(device()); + QIODevice &f = *(device()); // Removed "f.open(...)" and "f.close()" (tanghus) @@ -139,19 +145,19 @@ bool XVHandler::write(const QImage &image) sprintf(str, "%i %i 255\n", w, h); f.write(str, strlen(str)); - QImage tmpImage(image); - if (image.depth() == 1) + if (image.depth() == 1) { tmpImage = image.convertToFormat(QImage::Format_Indexed8, Qt::AutoColor); + } - uchar* buffer = new uchar[ w ]; + 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; + const QRgb *data32 = (QRgb *) data; r = qRed(*data32) >> 5; g = qGreen(*data32) >> 5; b = qBlue(*data32) >> 6; @@ -165,7 +171,7 @@ bool XVHandler::write(const QImage &image) } buffer[ px ] = (r << 5) | (g << 2) | b; } - f.write((const char*)buffer, w); + f.write((const char *)buffer, w); } delete[] buffer; @@ -185,8 +191,9 @@ bool XVHandler::canRead(QIODevice *device) qint64 readBytes = device->read(head, sizeof(head)); if (readBytes != sizeof(head)) { if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -194,8 +201,9 @@ bool XVHandler::canRead(QIODevice *device) } if (device->isSequential()) { - while (readBytes > 0) + while (readBytes > 0) { device->ungetChar(head[readBytes-- - 1]); + } } else { device->seek(oldPos); } @@ -205,18 +213,23 @@ bool XVHandler::canRead(QIODevice *device) QImageIOPlugin::Capabilities XVPlugin::capabilities(QIODevice *device, const QByteArray &format) const { - if (format == "xv") + if (format == "xv") { return Capabilities(CanRead | CanWrite); - if (!format.isEmpty()) + } + if (!format.isEmpty()) { return 0; - if (!device->isOpen()) + } + if (!device->isOpen()) { return 0; + } Capabilities cap; - if (device->isReadable() && XVHandler::canRead(device)) + if (device->isReadable() && XVHandler::canRead(device)) { cap |= CanRead; - if (device->isWritable()) + } + if (device->isWritable()) { cap |= CanWrite; + } return cap; } diff --git a/tests/imageconverter.cpp b/tests/imageconverter.cpp index 151bceb..30428bc 100644 --- a/tests/imageconverter.cpp +++ b/tests/imageconverter.cpp @@ -28,7 +28,7 @@ #include #include -int main(int argc, char** argv) +int main(int argc, char **argv) { QCoreApplication app(argc, argv); QCoreApplication::addLibraryPath(QLatin1String(PLUGIN_DIR)); @@ -42,18 +42,18 @@ int main(int argc, char** argv) parser.addPositionalArgument(QLatin1String("in"), QLatin1String("input image file")); parser.addPositionalArgument(QLatin1String("out"), QLatin1String("output image file")); QCommandLineOption informat( - QStringList() << QLatin1String("i") << QLatin1String("informat"), - QLatin1String("Image format for input file"), - QLatin1String("format")); + QStringList() << QLatin1String("i") << QLatin1String("informat"), + QLatin1String("Image format for input file"), + QLatin1String("format")); parser.addOption(informat); QCommandLineOption outformat( - QStringList() << QLatin1String("o") << QLatin1String("outformat"), - QLatin1String("Image format for output file"), - QLatin1String("format")); + QStringList() << QLatin1String("o") << QLatin1String("outformat"), + QLatin1String("Image format for output file"), + QLatin1String("format")); parser.addOption(outformat); QCommandLineOption listformats( - QStringList() << QLatin1String("l") << QLatin1String("list"), - QLatin1String("List supported image formats")); + QStringList() << QLatin1String("l") << QLatin1String("list"), + QLatin1String("List supported image formats")); parser.addOption(listformats); parser.process(app);