Code reformatted using kde-dev-scripts/astyle-kdelibs.

Use git blame -w 47df948 to show authorship as it was before this commit.
This commit is contained in:
David Faure
2013-12-18 09:54:11 +01:00
parent 47df9483fd
commit 02b5e97197
19 changed files with 950 additions and 731 deletions

View File

@@ -79,7 +79,6 @@ union Color4444 {
ushort u; ushort u;
}; };
static const uint FOURCC_DDS = MAKEFOURCC('D', 'D', 'S', ' '); static const uint FOURCC_DDS = MAKEFOURCC('D', 'D', 'S', ' ');
static const uint FOURCC_DXT1 = MAKEFOURCC('D', 'X', 'T', '1'); static const uint FOURCC_DXT1 = MAKEFOURCC('D', 'X', 'T', '1');
static const uint FOURCC_DXT2 = MAKEFOURCC('D', 'X', 'T', '2'); static const uint FOURCC_DXT2 = MAKEFOURCC('D', 'X', 'T', '2');
@@ -126,7 +125,6 @@ enum DDSType {
DDS_UNKNOWN DDS_UNKNOWN
}; };
struct DDSPixelFormat { struct DDSPixelFormat {
uint size; uint size;
uint flags; uint flags;
@@ -217,7 +215,6 @@ static bool IsValid(const DDSHeader & header)
return true; return true;
} }
// Get supported type. We currently support 10 different types. // Get supported type. We currently support 10 different types.
static DDSType GetType(const DDSHeader &header) static DDSType GetType(const DDSHeader &header)
{ {
@@ -380,13 +377,13 @@ static QDataStream & operator>> (QDataStream & s, Color565 & c)
return s >> c.u; return s >> c.u;
} }
struct BlockDXT { struct BlockDXT {
Color565 col0; Color565 col0;
Color565 col1; Color565 col1;
uchar row[4]; 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].r = (col0.c.r << 3) | (col0.c.r >> 2);
color_array[0].g = (col0.c.g << 2) | (col0.c.g >> 4); color_array[0].g = (col0.c.g << 2) | (col0.c.g >> 4);
color_array[0].b = (col0.c.b << 3) | (col0.c.b >> 2); color_array[0].b = (col0.c.b << 3) | (col0.c.b >> 2);
@@ -424,7 +421,6 @@ 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]; return s >> c.col0 >> c.col1 >> c.row[0] >> c.row[1] >> c.row[2] >> c.row[3];
@@ -444,7 +440,8 @@ struct BlockDXTAlphaLinear {
uchar alpha1; uchar alpha1;
uchar bits[6]; uchar bits[6];
void GetAlphas(uchar alpha_array[8]) { void GetAlphas(uchar alpha_array[8])
{
alpha_array[0] = alpha0; alpha_array[0] = alpha0;
alpha_array[1] = alpha1; 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. // Split 24 packed bits into 8 bytes, 3 bits at a time.
uint b = bits[0] | bits[1] << 8 | bits[2] << 16; uint b = bits[0] | bits[1] << 8 | bits[2] << 16;
bit_array[0] = uchar(b & 0x07); b >>= 3; bit_array[0] = uchar(b & 0x07); b >>= 3;
@@ -588,7 +586,9 @@ static bool LoadDXT3(QDataStream & s, const DDSHeader & header, QImage & img)
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); //UndoPremultiplyAlpha(img);
return true; return true;
} }
@@ -643,7 +643,9 @@ static bool LoadDXT5(QDataStream & s, const DDSHeader & header, QImage & img)
} }
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); //UndoPremultiplyAlpha(img);
return true; return true;
} }
@@ -751,8 +753,6 @@ static bool LoadATI2(QDataStream & s, const DDSHeader & header, QImage & img)
return true; 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. // Get an appropriate texture loader for the given type.
@@ -788,7 +788,6 @@ static TextureLoader GetTextureLoader(DDSType type)
}; };
} }
// Load a 2d texture. // Load a 2d texture.
static bool LoadTexture(QDataStream &s, const DDSHeader &header, QImage &img) static bool LoadTexture(QDataStream &s, const DDSHeader &header, QImage &img)
{ {
@@ -811,7 +810,6 @@ static bool LoadTexture(QDataStream & s, const DDSHeader & header, QImage & img)
return loader(s, header, img); return loader(s, header, img);
} }
static int FaceOffset(const DDSHeader &header) static int FaceOffset(const DDSHeader &header)
{ {
@@ -926,8 +924,6 @@ static bool LoadCubeMap(QDataStream & s, const DDSHeader & header, QImage & img)
return true; return true;
} }
DDSHandler::DDSHandler() DDSHandler::DDSHandler()
{ {
} }
@@ -994,8 +990,9 @@ bool DDSHandler::canRead(QIODevice *device)
qint64 readBytes = device->read(head, sizeof(head)); qint64 readBytes = device->read(head, sizeof(head));
if (readBytes != sizeof(head)) { if (readBytes != sizeof(head)) {
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -1003,8 +1000,9 @@ bool DDSHandler::canRead(QIODevice *device)
} }
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -1014,16 +1012,20 @@ bool DDSHandler::canRead(QIODevice *device)
QImageIOPlugin::Capabilities DDSPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities DDSPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "dds") if (format == "dds") {
return Capabilities(CanRead); return Capabilities(CanRead);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && DDSHandler::canRead(device)) if (device->isReadable() && DDSHandler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
}
return cap; return cap;
} }

View File

@@ -142,8 +142,9 @@ bool EPSHandler::read(QImage *image)
qint64 ps_offset, ps_size; qint64 ps_offset, ps_size;
// find start of PostScript code // find start of PostScript code
if (!seekToCodeStart(io, ps_offset, ps_size)) if (!seekToCodeStart(io, ps_offset, ps_size)) {
return false; return false;
}
qCDebug(EPSPLUGIN) << "Offset:" << ps_offset << "; size:" << ps_size; qCDebug(EPSPLUGIN) << "Offset:" << ps_offset << "; size:" << ps_size;
@@ -209,8 +210,9 @@ bool EPSHandler::read(QImage *image)
converter.write(intro); converter.write(intro);
io->reset(); io->reset();
if (ps_offset > 0) if (ps_offset > 0) {
io->seek(ps_offset); io->seek(ps_offset);
}
QByteArray buffer; QByteArray buffer;
buffer.resize(4096); buffer.resize(4096);
@@ -246,15 +248,15 @@ bool EPSHandler::read(QImage *image)
} }
} }
bool EPSHandler::write(const QImage &image) bool EPSHandler::write(const QImage &image)
{ {
QPrinter psOut(QPrinter::PrinterResolution); QPrinter psOut(QPrinter::PrinterResolution);
QPainter p; QPainter p;
QTemporaryFile tmpFile(QStringLiteral("XXXXXXXX.pdf")); QTemporaryFile tmpFile(QStringLiteral("XXXXXXXX.pdf"));
if (!tmpFile.open()) if (!tmpFile.open()) {
return false; return false;
}
psOut.setCreator(QStringLiteral("KDE EPS image plugin")); psOut.setCreator(QStringLiteral("KDE EPS image plugin"));
psOut.setOutputFileName(tmpFile.fileName()); psOut.setOutputFileName(tmpFile.fileName());
@@ -319,8 +321,9 @@ bool EPSHandler::canRead(QIODevice *device)
QByteArray head = device->readLine(64); QByteArray head = device->readLine(64);
int readBytes = head.size(); int readBytes = head.size();
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -330,18 +333,23 @@ bool EPSHandler::canRead(QIODevice *device)
QImageIOPlugin::Capabilities EPSPlugin::capabilities(QIODevice *device, const QByteArray &format) const 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); return Capabilities(CanRead | CanWrite);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && EPSHandler::canRead(device)) if (device->isReadable() && EPSHandler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
if (device->isWritable()) }
if (device->isWritable()) {
cap |= CanWrite; cap |= CanWrite;
}
return cap; return cap;
} }

View File

@@ -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 * KImageIO Routines to read (and perhaps in the future, write) images
@@ -38,7 +37,8 @@ class K_IStream: public Imf::IStream
{ {
public: public:
K_IStream(QIODevice *dev, const QByteArray &fileName): K_IStream(QIODevice *dev, const QByteArray &fileName):
IStream(fileName.data()), m_dev(dev) { IStream(fileName.data()), m_dev(dev)
{
} }
virtual bool read(char c[], int n); virtual bool read(char c[], int n);
@@ -57,8 +57,9 @@ bool K_IStream::read(char c[], int n)
return true; return true;
} else if (result == 0) { } else if (result == 0) {
throw Iex::InputExc("Unexpected end of file"); throw Iex::InputExc("Unexpected end of file");
} else // negative value { } else { // negative value {
Iex::throwErrnoExc("Error in read", result); Iex::throwErrnoExc("Error in read", result);
}
return false; return false;
} }
@@ -115,14 +116,18 @@ QRgb RgbaToQrgba(struct Imf::Rgba imagePixel)
// this value will be mapped to the display's // this value will be mapped to the display's
// maximum intensity). // maximum intensity).
// Response: kneeLow = 0.0 (2^0.0 => 1); kneeHigh = 5.0 (2^5 =>32) // 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<float>::log((r - 1.0) * 0.184874 + 1) / 0.184874; r = 1.0 + Imath::Math<float>::log((r - 1.0) * 0.184874 + 1) / 0.184874;
if (g > 1.0) }
if (g > 1.0) {
g = 1.0 + Imath::Math<float>::log((g - 1.0) * 0.184874 + 1) / 0.184874; g = 1.0 + Imath::Math<float>::log((g - 1.0) * 0.184874 + 1) / 0.184874;
if (b > 1.0) }
if (b > 1.0) {
b = 1.0 + Imath::Math<float>::log((b - 1.0) * 0.184874 + 1) / 0.184874; b = 1.0 + Imath::Math<float>::log((b - 1.0) * 0.184874 + 1) / 0.184874;
if (a > 1.0) }
if (a > 1.0) {
a = 1.0 + Imath::Math<float>::log((a - 1.0) * 0.184874 + 1) / 0.184874; a = 1.0 + Imath::Math<float>::log((a - 1.0) * 0.184874 + 1) / 0.184874;
}
// //
// 5) Gamma-correct the pixel values, assuming that the // 5) Gamma-correct the pixel values, assuming that the
// screen's gamma is 0.4545 (or 1/2.2). // 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); file.readPixels(dw.min.y, dw.max.y);
QImage image(width, height, QImage::Format_RGB32); QImage image(width, height, QImage::Format_RGB32);
if (image.isNull()) if (image.isNull()) {
return false; return false;
}
// somehow copy pixels into image // somehow copy pixels into image
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
@@ -194,7 +200,6 @@ bool EXRHandler::read(QImage *outImage)
} }
} }
bool EXRHandler::write(const QImage &image) bool EXRHandler::write(const QImage &image)
{ {
// TODO: stub // TODO: stub
@@ -202,7 +207,6 @@ bool EXRHandler::write(const QImage &image)
return false; return false;
} }
bool EXRHandler::canRead(QIODevice *device) bool EXRHandler::canRead(QIODevice *device)
{ {
if (!device) { if (!device) {
@@ -217,16 +221,20 @@ bool EXRHandler::canRead(QIODevice *device)
QImageIOPlugin::Capabilities EXRPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities EXRPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "exr") if (format == "exr") {
return Capabilities(CanRead); return Capabilities(CanRead);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && EXRHandler::canRead(device)) if (device->isReadable() && EXRHandler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
}
return cap; return cap;
} }

View File

@@ -200,28 +200,32 @@ static void RGBTOHSV(uchar& red, uchar& green, uchar& blue)
v = max; v = max;
if (max != 0) if (max != 0) {
s = ((max - min) * 255) / (double)max; s = ((max - min) * 255) / (double)max;
else } else {
s = 0; s = 0;
}
if (s == 0) if (s == 0) {
h = 0; h = 0;
else { } else {
int delta = max - min; int delta = max - min;
if (r == max) if (r == max) {
h = (g - b) / (double)delta; h = (g - b) / (double)delta;
else if (g == max) } else if (g == max) {
h = 2 + (b - r) / (double)delta; h = 2 + (b - r) / (double)delta;
else if (b == max) } else if (b == max) {
h = 4 + (r - g) / (double)delta; h = 4 + (r - g) / (double)delta;
}
h *= 42.5; h *= 42.5;
if (h < 0) if (h < 0) {
h += 255; h += 255;
if (h > 255) }
if (h > 255) {
h -= 255; h -= 255;
} }
}
red = (uchar)h; red = (uchar)h;
green = (uchar)s; green = (uchar)s;
@@ -319,25 +323,28 @@ static void RGBTOHLS(uchar& red, uchar& green, uchar& blue)
} else { } else {
int delta = max - min; int delta = max - min;
if (l < 128) if (l < 128) {
s = 255 * (double)delta / (double)(max + min); s = 255 * (double)delta / (double)(max + min);
else } else {
s = 255 * (double)delta / (double)(511 - max - min); s = 255 * (double)delta / (double)(511 - max - min);
}
if (r == max) if (r == max) {
h = (g - b) / (double)delta; h = (g - b) / (double)delta;
else if (g == max) } else if (g == max) {
h = 2 + (b - r) / (double)delta; h = 2 + (b - r) / (double)delta;
else } else {
h = 4 + (r - g) / (double)delta; h = 4 + (r - g) / (double)delta;
}
h *= 42.5; h *= 42.5;
if (h < 0) if (h < 0) {
h += 255; h += 255;
else if (h > 255) } else if (h > 255) {
h -= 255; h -= 255;
} }
}
red = (uchar)h; red = (uchar)h;
green = (uchar)l; green = (uchar)l;
@@ -355,19 +362,21 @@ static int HLSVALUE(double n1, double n2, double hue)
{ {
double value; double value;
if (hue > 255) if (hue > 255) {
hue -= 255; hue -= 255;
else if (hue < 0) } else if (hue < 0) {
hue += 255; hue += 255;
}
if (hue < 42.5) if (hue < 42.5) {
value = n1 + (n2 - n1) * (hue / 42.5); value = n1 + (n2 - n1) * (hue / 42.5);
else if (hue < 127.5) } else if (hue < 127.5) {
value = n2; value = n2;
else if (hue < 170) } else if (hue < 170) {
value = n1 + (n2 - n1) * ((170 - hue) / 42.5); value = n1 + (n2 - n1) * ((170 - hue) / 42.5);
else } else {
value = n1; value = n1;
}
return (int)(value * 255); return (int)(value * 255);
} }
@@ -391,10 +400,11 @@ static void HLSTORGB(uchar& hue, uchar& lightness, uchar& saturation)
} else { } else {
double m1, m2; double m1, m2;
if (l < 128) if (l < 128) {
m2 = (l * (255 + s)) / 65025.; m2 = (l * (255 + s)) / 65025.;
else } else {
m2 = (l + s - (l * s) / 255.) / 255.; m2 = (l + s - (l * s) / 255.) / 255.;
}
m1 = (l / 127.5) - m2; m1 = (l / 127.5) - m2;

View File

@@ -26,7 +26,9 @@ namespace // Private.
static inline uchar ClipToByte(float value) 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. //else if (value < 0.0f) return 0; // we know value is positive.
return uchar(value); return uchar(value);
} }
@@ -44,7 +46,9 @@ static bool Read_Old_Line(uchar * image, int width, QDataStream & s)
s >> image[2]; s >> image[2];
s >> image[3]; s >> image[3];
if (s.atEnd()) return false; if (s.atEnd()) {
return false;
}
if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1)) { if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1)) {
for (i = image[3] << rshift; i > 0; i--) { for (i = image[3] << rshift; i > 0; i--) {
@@ -63,7 +67,6 @@ static bool Read_Old_Line(uchar * image, int width, QDataStream & s)
return true; 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++) { for (int j = 0; j < width; j++) {
@@ -173,7 +176,6 @@ static bool LoadHDR(QDataStream & s, const int width, const int height, QImage &
} // namespace } // namespace
Q_DECL_EXPORT void kimgio_hdr_read(QImageIO *io) Q_DECL_EXPORT void kimgio_hdr_read(QImageIO *io)
{ {
int len; int len;
@@ -229,7 +231,6 @@ Q_DECL_EXPORT void kimgio_hdr_read(QImageIO * io)
io->setStatus(0); io->setStatus(0);
} }
Q_DECL_EXPORT void kimgio_hdr_write(QImageIO *) Q_DECL_EXPORT void kimgio_hdr_write(QImageIO *)
{ {
// intentionally not implemented (since writing low dynamic range data to a HDR file is nonsense.) // intentionally not implemented (since writing low dynamic range data to a HDR file is nonsense.)

View File

@@ -31,7 +31,6 @@
#define DEFAULT_RATE 0.10 #define DEFAULT_RATE 0.10
#define MAXCMPTS 256 #define MAXCMPTS 256
/************************* JasPer QIODevice stream ***********************/ /************************* JasPer QIODevice stream ***********************/
//unfortunately this is declared as static in JasPer libraries //unfortunately this is declared as static in JasPer libraries
@@ -136,11 +135,12 @@ static long qiodevice_seek(jas_stream_obj_t *obj, long offset, int origin)
if (newpos < 0) { if (newpos < 0) {
return -1; return -1;
} }
if (io->seek(newpos)) if (io->seek(newpos)) {
return newpos; return newpos;
else } else {
return -1; return -1;
} }
}
static int qiodevice_close(jas_stream_obj_t *) 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; jas_stream_t *stream;
if (!iodevice) return 0; if (!iodevice) {
return 0;
}
if (!(stream = jas_stream_create())) { if (!(stream = jas_stream_create())) {
return 0; return 0;
} }
@@ -186,7 +188,6 @@ typedef struct {
jas_image_t *altimage; jas_image_t *altimage;
} gs_t; } gs_t;
static jas_image_t * static jas_image_t *
read_image(QIODevice *io) read_image(QIODevice *io)
{ {
@@ -194,7 +195,9 @@ read_image(QIODevice* io)
in = jas_stream_qiodevice(io); 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); jas_stream_close(in);
@@ -207,11 +210,15 @@ static bool
convert_colorspace(gs_t &gs) convert_colorspace(gs_t &gs)
{ {
jas_cmprof_t *outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB); 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, gs.altimage = jas_image_chclrspc(gs.image, outprof,
JAS_CMXFORM_INTENT_PER); JAS_CMXFORM_INTENT_PER);
if (!gs.altimage) return false; if (!gs.altimage) {
return false;
}
return true; return true;
} // convert_colorspace } // convert_colorspace
@@ -219,7 +226,9 @@ convert_colorspace(gs_t& gs)
static bool 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; QImage qti;
if ((gs.cmptlut[0] = jas_image_getcmptbytype(gs.altimage, if ((gs.cmptlut[0] = jas_image_getcmptbytype(gs.altimage,
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 || JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 ||
@@ -238,8 +247,9 @@ render_view(gs_t& gs, QImage* outImage)
const int height = jas_image_cmptheight(gs.altimage, cmptlut[0]); const int height = jas_image_cmptheight(gs.altimage, cmptlut[0]);
for (int i = 1; i < 3; ++i) { for (int i = 1; i < 3; ++i) {
if (jas_image_cmptwidth(gs.altimage, cmptlut[i]) != width || 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; return false;
}
} // for } // for
jas_matrix_t *cmptmatrix[3]; jas_matrix_t *cmptmatrix[3];
@@ -274,8 +284,11 @@ render_view(gs_t& gs, QImage* outImage)
// it to use the complete value range. // it to use the complete value range.
v[k] <<= 8 - prec[k]; v[k] <<= 8 - prec[k];
if (v[k] < 0) v[k] = 0; if (v[k] < 0) {
else if (v[k] > 255) v[k] = 255; v[k] = 0;
} else if (v[k] > 255) {
v[k] = 255;
}
++buf[k]; ++buf[k];
} // for k } // for k
@@ -293,7 +306,6 @@ render_view(gs_t& gs, QImage* outImage)
return true; return true;
} // render_view } // render_view
static jas_image_t * static jas_image_t *
create_image(const QImage &qi) create_image(const QImage &qi)
{ {
@@ -323,7 +335,6 @@ create_image(const QImage& qi)
return ji; return ji;
} // create_image } // create_image
static bool static bool
write_components(jas_image_t *ji, const QImage &qi) write_components(jas_image_t *ji, const QImage &qi)
{ {
@@ -331,26 +342,31 @@ write_components(jas_image_t* ji, const QImage& qi)
const unsigned width = qi.width(); const unsigned width = qi.width();
jas_matrix_t *m = jas_matrix_create(height, width); jas_matrix_t *m = jas_matrix_create(height, width);
if (!m) return false; if (!m) {
return false;
}
jas_image_setclrspc(ji, JAS_CLRSPC_SRGB); jas_image_setclrspc(ji, JAS_CLRSPC_SRGB);
jas_image_setcmpttype(ji, 0, JAS_IMAGE_CT_RGB_R); jas_image_setcmpttype(ji, 0, JAS_IMAGE_CT_RGB_R);
for (uint y = 0; y < height; ++y) 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_matrix_set(m, y, x, qRed(qi.pixel(x, y)));
}
jas_image_writecmpt(ji, 0, 0, 0, width, height, m); jas_image_writecmpt(ji, 0, 0, 0, width, height, m);
jas_image_setcmpttype(ji, 1, JAS_IMAGE_CT_RGB_G); jas_image_setcmpttype(ji, 1, JAS_IMAGE_CT_RGB_G);
for (uint y = 0; y < height; ++y) 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_matrix_set(m, y, x, qGreen(qi.pixel(x, y)));
}
jas_image_writecmpt(ji, 1, 0, 0, width, height, m); jas_image_writecmpt(ji, 1, 0, 0, width, height, m);
jas_image_setcmpttype(ji, 2, JAS_IMAGE_CT_RGB_B); jas_image_setcmpttype(ji, 2, JAS_IMAGE_CT_RGB_B);
for (uint y = 0; y < height; ++y) 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_matrix_set(m, y, x, qBlue(qi.pixel(x, y)));
}
jas_image_writecmpt(ji, 2, 0, 0, width, height, m); jas_image_writecmpt(ji, 2, 0, 0, width, height, m);
jas_matrix_destroy(m); jas_matrix_destroy(m);
@@ -364,7 +380,9 @@ write_image(const QImage &image, QIODevice* io, int quality)
stream = jas_stream_qiodevice(io); stream = jas_stream_qiodevice(io);
// by here, a jas_stream_t is open // 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) { if (!ji) {
@@ -390,7 +408,9 @@ write_image(const QImage &image, QIODevice* io, int quality)
jas_image_destroy(ji); jas_image_destroy(ji);
jas_stream_close(stream); jas_stream_close(stream);
if (i != 0) return false; if (i != 0) {
return false;
}
return true; return true;
} }
@@ -425,17 +445,27 @@ bool JP2Handler::canRead(QIODevice *device)
bool JP2Handler::read(QImage *image) bool JP2Handler::read(QImage *image)
{ {
if (!canRead()) return false; if (!canRead()) {
return false;
}
gs_t gs; 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); render_view(gs, image);
if (gs.image) jas_image_destroy(gs.image); if (gs.image) {
if (gs.altimage) jas_image_destroy(gs.altimage); jas_image_destroy(gs.image);
}
if (gs.altimage) {
jas_image_destroy(gs.altimage);
}
return true; return true;
} }
@@ -452,31 +482,38 @@ bool JP2Handler::supportsOption(ImageOption option) const
QVariant JP2Handler::option(ImageOption option) const QVariant JP2Handler::option(ImageOption option) const
{ {
if (option == Quality) if (option == Quality) {
return quality; return quality;
}
return QVariant(); return QVariant();
} }
void JP2Handler::setOption(ImageOption option, const QVariant &value) void JP2Handler::setOption(ImageOption option, const QVariant &value)
{ {
if (option == Quality) if (option == Quality) {
quality = qBound(-1, value.toInt(), 100); quality = qBound(-1, value.toInt(), 100);
} }
}
QImageIOPlugin::Capabilities JP2Plugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities JP2Plugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "jp2") if (format == "jp2") {
return Capabilities(CanRead | CanWrite); return Capabilities(CanRead | CanWrite);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && JP2Handler::canRead(device)) if (device->isReadable() && JP2Handler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
if (device->isWritable()) }
if (device->isWritable()) {
cap |= CanWrite; cap |= CanWrite;
}
return cap; return cap;
} }

View File

@@ -21,7 +21,8 @@ public:
quint8 g; quint8 g;
quint8 b; quint8 b;
static RGB from(const QRgb &color) { static RGB from(const QRgb &color)
{
RGB c; RGB c;
c.r = qRed(color); c.r = qRed(color);
c.g = qGreen(color); c.g = qGreen(color);
@@ -34,14 +35,16 @@ public:
class Palette class Palette
{ {
public: public:
void setColor(int i, const QRgb color) { void setColor(int i, const QRgb color)
{
RGB &c = rgb[ i ]; RGB &c = rgb[ i ];
c.r = qRed(color); c.r = qRed(color);
c.g = qGreen(color); c.g = qGreen(color);
c.b = qBlue(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); return qRgb(rgb[ i ].r, rgb[ i ].g, rgb[ i ].b);
} }
@@ -53,13 +56,16 @@ class PCXHEADER
public: public:
PCXHEADER(); PCXHEADER();
inline int width() const { inline int width() const
{
return (XMax - XMin) + 1; return (XMax - XMin) + 1;
} }
inline int height() const { inline int height() const
{
return (YMax - YMin) + 1; return (YMax - YMin) + 1;
} }
inline bool isCompressed() const { inline bool isCompressed() const
{
return (Encoding == 1); return (Encoding == 1);
} }
@@ -111,8 +117,9 @@ static QDataStream &operator>>(QDataStream &s, RGB &rgb)
static QDataStream &operator>>(QDataStream &s, Palette &pal) 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 ]; s >> pal.rgb[ i ];
}
return s; return s;
} }
@@ -151,8 +158,9 @@ static QDataStream &operator>>(QDataStream &s, PCXHEADER &ph)
// Skip the rest of the header // Skip the rest of the header
quint8 byte; quint8 byte;
while (s.device()->pos() < 128) while (s.device()->pos() < 128) {
s >> byte; s >> byte;
}
return s; return s;
} }
@@ -166,8 +174,9 @@ static QDataStream &operator<<(QDataStream &s, const RGB &rgb)
static QDataStream &operator<<(QDataStream &s, const Palette &pal) 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 ]; s << pal.rgb[ i ];
}
return s; return s;
} }
@@ -189,8 +198,9 @@ static QDataStream &operator<<(QDataStream &s, const PCXHEADER &ph)
s << ph.VScreenSize; s << ph.VScreenSize;
quint8 byte = 0; quint8 byte = 0;
for (int i = 0; i < 54; ++i) for (int i = 0; i < 54; ++i) {
s << byte; s << byte;
}
return s; return s;
} }
@@ -219,9 +229,10 @@ static void readLine(QDataStream &s, QByteArray &buf, const PCXHEADER &header)
count = byte - 0xc0; count = byte - 0xc0;
s >> byte; s >> byte;
} }
while (count-- && i < size) while (count-- && i < size) {
buf[ i++ ] = byte; buf[ i++ ] = byte;
} }
}
} else { } else {
// Image is not compressed (possible?) // Image is not compressed (possible?)
while (i < size) { while (i < size) {
@@ -247,9 +258,10 @@ static void readImage1(QImage &img, QDataStream &s, const PCXHEADER &header)
readLine(s, buf, header); readLine(s, buf, header);
uchar *p = img.scanLine(y); uchar *p = img.scanLine(y);
unsigned int bpl = qMin((quint16)((header.width() + 7) / 8), header.BytesPerLine); 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]; p[ x ] = buf[x];
} }
}
// Set the color palette // Set the color palette
img.setColor(0, qRgb(0, 0, 0)); img.setColor(0, qRgb(0, 0, 0));
@@ -276,19 +288,22 @@ static void readImage4(QImage &img, QDataStream &s, const PCXHEADER &header)
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
quint32 offset = i * header.BytesPerLine; quint32 offset = i * header.BytesPerLine;
for (int x = 0; x < header.width(); ++x) 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); pixbuf[ x ] = (int)(pixbuf[ x ]) + (1 << i);
} }
}
uchar *p = img.scanLine(y); 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 ]; p[ x ] = pixbuf[ x ];
} }
}
// Read the palette // Read the palette
for (int i = 0; i < 16; ++i) for (int i = 0; i < 16; ++i) {
img.setColor(i, header.ColorMap.color(i)); img.setColor(i, header.ColorMap.color(i));
} }
}
static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header) static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
{ {
@@ -307,9 +322,10 @@ static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
uchar *p = img.scanLine(y); uchar *p = img.scanLine(y);
unsigned int bpl = qMin(header.BytesPerLine, (quint16)header.width()); 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 ]; p[ x ] = buf[ x ];
} }
}
quint8 flag; quint8 flag;
s >> flag; s >> flag;
@@ -344,10 +360,11 @@ static void readImage24(QImage &img, QDataStream &s, const PCXHEADER &header)
readLine(s, b_buf, header); readLine(s, b_buf, header);
uint *p = (uint *)img.scanLine(y); 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 ]); p[ x ] = qRgb(r_buf[ x ], g_buf[ x ], b_buf[ x ]);
} }
} }
}
static void writeLine(QDataStream &s, QByteArray &buf) static void writeLine(QDataStream &s, QByteArray &buf)
{ {
@@ -392,8 +409,9 @@ static void writeImage1(QImage &img, QDataStream &s, PCXHEADER &header)
quint8 *p = img.scanLine(y); quint8 *p = img.scanLine(y);
// Invert as QImage uses reverse palette for monochrome images? // 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 ]; buf[ i ] = ~p[ i ];
}
writeLine(s, buf); writeLine(s, buf);
} }
@@ -405,32 +423,37 @@ static void writeImage4(QImage &img, QDataStream &s, PCXHEADER &header)
header.NPlanes = 4; header.NPlanes = 4;
header.BytesPerLine = header.width() / 8; 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)); header.ColorMap.setColor(i, img.color(i));
}
s << header; s << header;
QByteArray buf[ 4 ]; QByteArray buf[ 4 ];
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i) {
buf[ i ].resize(header.BytesPerLine); buf[ i ].resize(header.BytesPerLine);
}
for (int y = 0; y < header.height(); ++y) { for (int y = 0; y < header.height(); ++y) {
quint8 *p = img.scanLine(y); quint8 *p = img.scanLine(y);
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i) {
buf[ i ].fill(0); buf[ i ].fill(0);
}
for (int x = 0; x < header.width(); ++x) { for (int x = 0; x < header.width(); ++x) {
for (int i = 0; i < 4; ++i) 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); 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 ]); writeLine(s, buf[ i ]);
} }
} }
}
static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header) static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
{ {
@@ -445,8 +468,9 @@ static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
for (int y = 0; y < header.height(); ++y) { for (int y = 0; y < header.height(); ++y) {
quint8 *p = img.scanLine(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 ]; buf[ i ] = p[ i ];
}
writeLine(s, buf); writeLine(s, buf);
} }
@@ -456,9 +480,10 @@ static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
s << byte; s << byte;
// Write palette // Write palette
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i) {
s << RGB::from(img.color(i)); s << RGB::from(img.color(i));
} }
}
static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header) static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
{ {
@@ -488,7 +513,6 @@ static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
} }
} }
PCXHandler::PCXHandler() PCXHandler::PCXHandler()
{ {
} }
@@ -613,8 +637,9 @@ bool PCXHandler::canRead(QIODevice *device)
qint64 readBytes = device->read(head, sizeof(head)); qint64 readBytes = device->read(head, sizeof(head));
if (readBytes != sizeof(head)) { if (readBytes != sizeof(head)) {
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -622,8 +647,9 @@ bool PCXHandler::canRead(QIODevice *device)
} }
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -633,18 +659,23 @@ bool PCXHandler::canRead(QIODevice *device)
QImageIOPlugin::Capabilities PCXPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities PCXPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "pcx") if (format == "pcx") {
return Capabilities(CanRead | CanWrite); return Capabilities(CanRead | CanWrite);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && PCXHandler::canRead(device)) if (device->isReadable() && PCXHandler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
if (device->isWritable()) }
if (device->isWritable()) {
cap |= CanWrite; cap |= CanWrite;
}
return cap; return cap;
} }

View File

@@ -77,12 +77,15 @@ bool SoftimagePICHandler::supportsOption(ImageOption option) const
QImageIOPlugin::Capabilities SoftimagePICPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities SoftimagePICPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "pic") if (format == "pic") {
return Capabilities(CanRead | CanWrite); return Capabilities(CanRead | CanWrite);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && SoftimagePICHandler::canRead(device)) { if (device->isReadable() && SoftimagePICHandler::canRead(device)) {

View File

@@ -90,7 +90,6 @@ typedef struct {
#define HEADER_SIZE sizeof(PICHeader) #define HEADER_SIZE sizeof(PICHeader)
#define CHANNEL_SIZE sizeof(PICChannel) #define CHANNEL_SIZE sizeof(PICChannel)
/** /**
* Reads the PIC header and checks that it is OK * Reads the PIC header and checks that it is OK
* @param dev The QT device to read from * @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 /// Pic write handler for Qt / KDE
void pic_write(QIODevice *dev, const QImage *img); void pic_write(QIODevice *dev, const QImage *img);
#endif//__PIC_RW_H__ #endif//__PIC_RW_H__

View File

@@ -73,8 +73,9 @@ static bool seekBy(QDataStream& s, unsigned int bytes)
unsigned int num = qMin(bytes, (unsigned int)sizeof(buf)); unsigned int num = qMin(bytes, (unsigned int)sizeof(buf));
unsigned int l = num; unsigned int l = num;
s.readRawData(buf, l); s.readRawData(buf, l);
if (l != num) if (l != num) {
return false; return false;
}
bytes -= num; bytes -= num;
} }
return true; return true;
@@ -159,8 +160,9 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img)
if (compression) { if (compression) {
// Skip row lengths. // 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; return false;
}
// Read RLE data. // Read RLE data.
for (uint channel = 0; channel < channel_num; channel++) { for (uint channel = 0; channel < channel_num; channel++) {
@@ -170,8 +172,9 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img)
uint count = 0; uint count = 0;
while (count < pixel_count) { while (count < pixel_count) {
uchar c; uchar c;
if (s.atEnd()) if (s.atEnd()) {
return false; return false;
}
s >> c; s >> c;
uint len = c; uint len = c;
@@ -179,8 +182,9 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img)
// Copy next len+1 bytes literally. // Copy next len+1 bytes literally.
len++; len++;
count += len; count += len;
if (count > pixel_count) if (count > pixel_count) {
return false; return false;
}
while (len != 0) { while (len != 0) {
s >> *ptr; s >> *ptr;
@@ -193,8 +197,9 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img)
len ^= 0xFF; len ^= 0xFF;
len += 2; len += 2;
count += len; count += len;
if (s.atEnd() || count > pixel_count) if (s.atEnd() || count > pixel_count) {
return false; return false;
}
uchar val; uchar val;
s >> val; s >> val;
while (len != 0) { while (len != 0) {
@@ -231,7 +236,6 @@ static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img)
} // Private } // Private
PSDHandler::PSDHandler() PSDHandler::PSDHandler()
{ {
} }
@@ -288,8 +292,9 @@ bool PSDHandler::canRead(QIODevice *device)
qint64 readBytes = device->read(head, sizeof(head)); qint64 readBytes = device->read(head, sizeof(head));
if (readBytes != sizeof(head)) { if (readBytes != sizeof(head)) {
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -297,8 +302,9 @@ bool PSDHandler::canRead(QIODevice *device)
} }
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -308,16 +314,20 @@ bool PSDHandler::canRead(QIODevice *device)
QImageIOPlugin::Capabilities PSDPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities PSDPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "psd") if (format == "psd") {
return Capabilities(CanRead); return Capabilities(CanRead);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && PSDHandler::canRead(device)) if (device->isReadable() && PSDHandler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
}
return cap; return cap;
} }

View File

@@ -278,16 +278,20 @@ bool RASHandler::read(QImage *outImage)
QImageIOPlugin::Capabilities RASPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities RASPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "ras") if (format == "ras") {
return Capabilities(CanRead); return Capabilities(CanRead);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && RASHandler::canRead(device)) if (device->isReadable() && RASHandler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
}
return cap; return cap;
} }

View File

@@ -7,7 +7,6 @@
// published by the Free Software Foundation; either version 2 of the // published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version. // License, or (at your option) any later version.
/* this code supports: /* this code supports:
* reading: * reading:
* everything, except images with 1 dimension or images with * everything, except images with 1 dimension or images with
@@ -34,13 +33,16 @@ class RLEData : public QVector<uchar>
{ {
public: public:
RLEData() {} RLEData() {}
RLEData(const uchar *d, uint l, uint o) : _offset(o) { RLEData(const uchar *d, uint l, uint o) : _offset(o)
for (uint i = 0; i < l; i++) {
for (uint i = 0; i < l; i++) {
append(d[i]); append(d[i]);
} }
}
bool operator<(const RLEData &) const; bool operator<(const RLEData &) const;
void write(QDataStream &s); void write(QDataStream &s);
uint offset() const { uint offset() const
{
return _offset; return _offset;
} }
@@ -48,14 +50,14 @@ private:
uint _offset; uint _offset;
}; };
class RLEMap : public QMap<RLEData, uint> class RLEMap : public QMap<RLEData, uint>
{ {
public: public:
RLEMap() : _counter(0), _offset(0) {} RLEMap() : _counter(0), _offset(0) {}
uint insert(const uchar *d, uint l); uint insert(const uchar *d, uint l);
QVector<const RLEData *> vector(); QVector<const RLEData *> vector();
void setBaseOffset(uint o) { void setBaseOffset(uint o)
{
_offset = o; _offset = o;
} }
@@ -64,7 +66,6 @@ private:
uint _offset; uint _offset;
}; };
class SGIImage class SGIImage
{ {
public: public:
@@ -117,24 +118,22 @@ SGIImage::SGIImage(QIODevice *io) :
_stream.setDevice(_dev); _stream.setDevice(_dev);
} }
SGIImage::~SGIImage() SGIImage::~SGIImage()
{ {
delete[] _starttab; delete[] _starttab;
delete[] _lengthtab; delete[] _lengthtab;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
bool SGIImage::getRow(uchar *dest) bool SGIImage::getRow(uchar *dest)
{ {
int n, i; int n, i;
if (!_rle) { if (!_rle) {
for (i = 0; i < _xsize; i++) { for (i = 0; i < _xsize; i++) {
if (_pos >= _data.end()) if (_pos >= _data.end()) {
return false; return false;
}
dest[i] = uchar(*_pos); dest[i] = uchar(*_pos);
_pos += _bpc; _pos += _bpc;
} }
@@ -142,11 +141,13 @@ bool SGIImage::getRow(uchar *dest)
} }
for (i = 0; i < _xsize;) { for (i = 0; i < _xsize;) {
if (_bpc == 2) if (_bpc == 2) {
_pos++; _pos++;
}
n = *_pos & 0x7f; n = *_pos & 0x7f;
if (!n) if (!n) {
break; break;
}
if (*_pos++ & 0x80) { if (*_pos++ & 0x80) {
for (; i < _xsize && n--; i++) { for (; i < _xsize && n--; i++) {
@@ -154,8 +155,9 @@ bool SGIImage::getRow(uchar *dest)
_pos += _bpc; _pos += _bpc;
} }
} else { } else {
for (; i < _xsize && n--; i++) for (; i < _xsize && n--; i++) {
*dest++ = *_pos; *dest++ = *_pos;
}
_pos += _bpc; _pos += _bpc;
} }
@@ -163,7 +165,6 @@ bool SGIImage::getRow(uchar *dest)
return i == _xsize; return i == _xsize;
} }
bool SGIImage::readData(QImage &img) bool SGIImage::readData(QImage &img)
{ {
QRgb *c; QRgb *c;
@@ -172,61 +173,75 @@ bool SGIImage::readData(QImage& img)
uchar *line = (uchar *)lguard.data(); uchar *line = (uchar *)lguard.data();
unsigned x, y; unsigned x, y;
if (!_rle) if (!_rle) {
_pos = _data.begin(); _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; return true;
}
if (_zsize != 2) { if (_zsize != 2) {
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
if (_rle) if (_rle) {
_pos = _data.begin() + *start++; _pos = _data.begin() + *start++;
if (!getRow(line)) }
if (!getRow(line)) {
return false; return false;
}
c = (QRgb *)img.scanLine(_ysize - y - 1); 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]); *c = qRgb(qRed(*c), line[x], line[x]);
} }
}
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
if (_rle) if (_rle) {
_pos = _data.begin() + *start++; _pos = _data.begin() + *start++;
if (!getRow(line)) }
if (!getRow(line)) {
return false; return false;
}
c = (QRgb *)img.scanLine(_ysize - y - 1); 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]); *c = qRgb(qRed(*c), qGreen(*c), line[x]);
} }
}
if (_zsize == 3) if (_zsize == 3) {
return true; return true;
} }
}
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
if (_rle) if (_rle) {
_pos = _data.begin() + *start++; _pos = _data.begin() + *start++;
if (!getRow(line)) }
if (!getRow(line)) {
return false; return false;
}
c = (QRgb *)img.scanLine(_ysize - y - 1); 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]); *c = qRgba(qRed(*c), qGreen(*c), qBlue(*c), line[x]);
} }
}
return true; return true;
} }
bool SGIImage::readImage(QImage &img) bool SGIImage::readImage(QImage &img)
{ {
qint8 u8; qint8 u8;
@@ -237,14 +252,16 @@ bool SGIImage::readImage(QImage& img)
// magic // magic
_stream >> u16; _stream >> u16;
if (u16 != 0x01da) if (u16 != 0x01da) {
return false; return false;
}
// verbatim/rle // verbatim/rle
_stream >> _rle; _stream >> _rle;
// qDebug() << (_rle ? "RLE" : "verbatim"); // qDebug() << (_rle ? "RLE" : "verbatim");
if (_rle > 1) if (_rle > 1) {
return false; return false;
}
// bytes per channel // bytes per channel
_stream >> _bpc; _stream >> _bpc;
@@ -253,14 +270,16 @@ bool SGIImage::readImage(QImage& img)
; ;
else if (_bpc == 2) { else if (_bpc == 2) {
// qDebug() << "dropping least significant byte"; // qDebug() << "dropping least significant byte";
} else } else {
return false; return false;
}
// number of dimensions // number of dimensions
_stream >> _dim; _stream >> _dim;
// qDebug() << "dimensions: " << _dim; // qDebug() << "dimensions: " << _dim;
if (_dim < 1 || _dim > 3) if (_dim < 1 || _dim > 3) {
return false; return false;
}
_stream >> _xsize >> _ysize >> _zsize >> _pixmin >> _pixmax >> u32; _stream >> _xsize >> _ysize >> _zsize >> _pixmin >> _pixmax >> u32;
// qDebug() << "x: " << _xsize; // qDebug() << "x: " << _xsize;
@@ -273,27 +292,30 @@ bool SGIImage::readImage(QImage& img)
_stream >> _colormap; _stream >> _colormap;
// qDebug() << "colormap: " << _colormap; // qDebug() << "colormap: " << _colormap;
if (_colormap != NORMAL) if (_colormap != NORMAL) {
return false; // only NORMAL supported return false; // only NORMAL supported
}
for (int i = 0; i < 404; i++) for (int i = 0; i < 404; i++) {
_stream >> u8; _stream >> u8;
}
if (_dim == 1) { if (_dim == 1) {
// qDebug() << "1-dimensional images aren't supported yet"; // qDebug() << "1-dimensional images aren't supported yet";
return false; return false;
} }
if (_stream.atEnd()) if (_stream.atEnd()) {
return false; return false;
}
_numrows = _ysize * _zsize; _numrows = _ysize * _zsize;
img = QImage(_xsize, _ysize, QImage::Format_RGB32); img = QImage(_xsize, _ysize, QImage::Format_RGB32);
if (_zsize == 2 || _zsize == 4) if (_zsize == 2 || _zsize == 4) {
img = img.convertToFormat(QImage::Format_ARGB32); img = img.convertToFormat(QImage::Format_ARGB32);
else if (_zsize > 4) { } else if (_zsize > 4) {
// qDebug() << "using first 4 of " << _zsize << " channels"; // qDebug() << "using first 4 of " << _zsize << " channels";
} }
@@ -306,9 +328,10 @@ bool SGIImage::readImage(QImage& img)
} }
_lengthtab = new quint32[_numrows]; _lengthtab = new quint32[_numrows];
for (l = 0; l < _numrows; l++) for (l = 0; l < _numrows; l++) {
_stream >> _lengthtab[l]; _stream >> _lengthtab[l];
} }
}
_data = _dev->readAll(); _data = _dev->readAll();
@@ -329,16 +352,14 @@ bool SGIImage::readImage(QImage& img)
return true; 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); s << at(i);
} }
}
bool RLEData::operator<(const RLEData &b) const bool RLEData::operator<(const RLEData &b) const
{ {
@@ -346,67 +367,72 @@ bool RLEData::operator<(const RLEData& b) const
for (int i = 0; i < qMin(size(), b.size()); i++) { for (int i = 0; i < qMin(size(), b.size()); i++) {
ac = at(i); ac = at(i);
bc = b[i]; bc = b[i];
if (ac != bc) if (ac != bc) {
return ac < bc; return ac < bc;
} }
}
return size() < b.size(); return size() < b.size();
} }
uint RLEMap::insert(const uchar *d, uint l) uint RLEMap::insert(const uchar *d, uint l)
{ {
RLEData data = RLEData(d, l, _offset); RLEData data = RLEData(d, l, _offset);
Iterator it = find(data); Iterator it = find(data);
if (it != end()) if (it != end()) {
return it.value(); return it.value();
}
_offset += l; _offset += l;
return QMap<RLEData, uint>::insert(data, _counter++).value(); return QMap<RLEData, uint>::insert(data, _counter++).value();
} }
QVector<const RLEData *> RLEMap::vector() QVector<const RLEData *> RLEMap::vector()
{ {
QVector<const RLEData *> v(size()); QVector<const RLEData *> v(size());
for (Iterator it = begin(); it != end(); ++it) for (Iterator it = begin(); it != end(); ++it) {
v.replace(it.value(), &it.key()); v.replace(it.value(), &it.key());
}
return v; return v;
} }
uchar SGIImage::intensity(uchar c) uchar SGIImage::intensity(uchar c)
{ {
if (c < _pixmin) if (c < _pixmin) {
_pixmin = c; _pixmin = c;
if (c > _pixmax) }
if (c > _pixmax) {
_pixmax = c; _pixmax = c;
}
return c; return c;
} }
uint SGIImage::compact(uchar *d, uchar *s) uint SGIImage::compact(uchar *d, uchar *s)
{ {
uchar *dest = d, *src = s, patt, *t, *end = s + _xsize; uchar *dest = d, *src = s, patt, *t, *end = s + _xsize;
int i, n; int i, n;
while (src < end) { 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++; n++;
}
while (n) { while (n) {
i = n > 126 ? 126 : n; i = n > 126 ? 126 : n;
n -= i; n -= i;
*dest++ = 0x80 | i; *dest++ = 0x80 | i;
while (i--) while (i--) {
*dest++ = *src++; *dest++ = *src++;
} }
}
if (src == end) if (src == end) {
break; break;
}
patt = *src++; patt = *src++;
for (n = 1; src < end && *src == patt; src++) for (n = 1; src < end && *src == patt; src++) {
n++; n++;
}
while (n) { while (n) {
i = n > 126 ? 126 : n; i = n > 126 ? 126 : n;
@@ -419,7 +445,6 @@ uint SGIImage::compact(uchar *d, uchar *s)
return dest - d; return dest - d;
} }
bool SGIImage::scanData(const QImage &img) bool SGIImage::scanData(const QImage &img)
{ {
quint32 *start = _starttab; quint32 *start = _starttab;
@@ -433,40 +458,46 @@ bool SGIImage::scanData(const QImage& img)
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1)); c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++) for (x = 0; x < _xsize; x++) {
buf[x] = intensity(qRed(*c++)); buf[x] = intensity(qRed(*c++));
}
len = compact(line, buf); len = compact(line, buf);
*start++ = _rlemap.insert(line, len); *start++ = _rlemap.insert(line, len);
} }
if (_zsize == 1) if (_zsize == 1) {
return true; return true;
}
if (_zsize != 2) { if (_zsize != 2) {
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1)); c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++) for (x = 0; x < _xsize; x++) {
buf[x] = intensity(qGreen(*c++)); buf[x] = intensity(qGreen(*c++));
}
len = compact(line, buf); len = compact(line, buf);
*start++ = _rlemap.insert(line, len); *start++ = _rlemap.insert(line, len);
} }
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1)); c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++) for (x = 0; x < _xsize; x++) {
buf[x] = intensity(qBlue(*c++)); buf[x] = intensity(qBlue(*c++));
}
len = compact(line, buf); len = compact(line, buf);
*start++ = _rlemap.insert(line, len); *start++ = _rlemap.insert(line, len);
} }
if (_zsize == 3) if (_zsize == 3) {
return true; return true;
} }
}
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1)); c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++) for (x = 0; x < _xsize; x++) {
buf[x] = intensity(qAlpha(*c++)); buf[x] = intensity(qAlpha(*c++));
}
len = compact(line, buf); len = compact(line, buf);
*start++ = _rlemap.insert(line, len); *start++ = _rlemap.insert(line, len);
} }
@@ -474,7 +505,6 @@ bool SGIImage::scanData(const QImage& img)
return true; return true;
} }
void SGIImage::writeHeader() void SGIImage::writeHeader()
{ {
_stream << quint16(0x01da); _stream << quint16(0x01da);
@@ -483,15 +513,16 @@ void SGIImage::writeHeader()
_stream << _pixmin << _pixmax; _stream << _pixmin << _pixmax;
_stream << quint32(0); _stream << quint32(0);
for (int i = 0; i < 80; i++) for (int i = 0; i < 80; i++) {
_imagename[i] = '\0'; _imagename[i] = '\0';
}
_stream.writeRawData(_imagename, 80); _stream.writeRawData(_imagename, 80);
_stream << _colormap; _stream << _colormap;
for (int i = 0; i < 404; i++) for (int i = 0; i < 404; i++) {
_stream << quint8(0); _stream << quint8(0);
} }
}
void SGIImage::writeRle() void SGIImage::writeRle()
{ {
@@ -501,18 +532,20 @@ void SGIImage::writeRle()
uint i; uint i;
// write start table // write start table
for (i = 0; i < _numrows; i++) for (i = 0; i < _numrows; i++) {
_stream << quint32(_rlevector[_starttab[i]]->offset()); _stream << quint32(_rlevector[_starttab[i]]->offset());
// write length table
for (i = 0; i < _numrows; i++)
_stream << quint32(_rlevector[_starttab[i]]->size());
// write data
for (i = 0; (int)i < _rlevector.size(); i++)
const_cast<RLEData*>(_rlevector[i])->write(_stream);
} }
// write length table
for (i = 0; i < _numrows; i++) {
_stream << quint32(_rlevector[_starttab[i]]->size());
}
// write data
for (i = 0; (int)i < _rlevector.size(); i++) {
const_cast<RLEData *>(_rlevector[i])->write(_stream);
}
}
void SGIImage::writeVerbatim(const QImage &img) void SGIImage::writeVerbatim(const QImage &img)
{ {
@@ -525,49 +558,56 @@ void SGIImage::writeVerbatim(const QImage& img)
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1)); c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++) for (x = 0; x < _xsize; x++) {
_stream << quint8(qRed(*c++)); _stream << quint8(qRed(*c++));
} }
}
if (_zsize == 1) if (_zsize == 1) {
return; return;
}
if (_zsize != 2) { if (_zsize != 2) {
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1)); c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++) for (x = 0; x < _xsize; x++) {
_stream << quint8(qGreen(*c++)); _stream << quint8(qGreen(*c++));
} }
}
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1)); c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++) for (x = 0; x < _xsize; x++) {
_stream << quint8(qBlue(*c++)); _stream << quint8(qBlue(*c++));
} }
}
if (_zsize == 3) if (_zsize == 3) {
return; return;
} }
}
for (y = 0; y < _ysize; y++) { for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1)); c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++) for (x = 0; x < _xsize; x++) {
_stream << quint8(qAlpha(*c++)); _stream << quint8(qAlpha(*c++));
} }
} }
}
bool SGIImage::writeImage(const QImage &image) bool SGIImage::writeImage(const QImage &image)
{ {
// qDebug() << "writing "; // TODO add filename // qDebug() << "writing "; // TODO add filename
QImage img = image; QImage img = image;
if (img.allGray()) if (img.allGray()) {
_dim = 2, _zsize = 1; _dim = 2, _zsize = 1;
else } else {
_dim = 3, _zsize = 3; _dim = 3, _zsize = 3;
}
if (img.format() == QImage::Format_ARGB32) if (img.format() == QImage::Format_ARGB32) {
_dim = 3, _zsize++; _dim = 3, _zsize++;
}
img = img.convertToFormat(QImage::Format_RGB32); img = img.convertToFormat(QImage::Format_RGB32);
if (img.isNull()) { if (img.isNull()) {
@@ -594,8 +634,9 @@ bool SGIImage::writeImage(const QImage& image)
long verbatim_size = _numrows * _xsize; long verbatim_size = _numrows * _xsize;
long rle_size = _numrows * 2 * sizeof(quint32); 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(); rle_size += _rlevector[i]->size();
}
// qDebug() << "minimum intensity: " << _pixmin; // qDebug() << "minimum intensity: " << _pixmin;
// qDebug() << "maximum intensity: " << _pixmax; // qDebug() << "maximum intensity: " << _pixmax;
@@ -603,22 +644,20 @@ bool SGIImage::writeImage(const QImage& image)
// qDebug() << "total savings: " << (verbatim_size - rle_size) << " bytes"; // qDebug() << "total savings: " << (verbatim_size - rle_size) << " bytes";
// qDebug() << "compression: " << (rle_size * 100.0 / verbatim_size) << '%'; // qDebug() << "compression: " << (rle_size * 100.0 / verbatim_size) << '%';
if (verbatim_size <= rle_size) if (verbatim_size <= rle_size) {
writeVerbatim(img); writeVerbatim(img);
else } else {
writeRle(); writeRle();
}
return true; return true;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
RGBHandler::RGBHandler() RGBHandler::RGBHandler()
{ {
} }
bool RGBHandler::canRead() const bool RGBHandler::canRead() const
{ {
if (canRead(device())) { if (canRead(device())) {
@@ -628,21 +667,18 @@ bool RGBHandler::canRead() const
return false; return false;
} }
bool RGBHandler::read(QImage *outImage) bool RGBHandler::read(QImage *outImage)
{ {
SGIImage sgi(device()); SGIImage sgi(device());
return sgi.readImage(*outImage); return sgi.readImage(*outImage);
} }
bool RGBHandler::write(const QImage &image) bool RGBHandler::write(const QImage &image)
{ {
SGIImage sgi(device()); SGIImage sgi(device());
return sgi.writeImage(image); return sgi.writeImage(image);
} }
bool RGBHandler::canRead(QIODevice *device) bool RGBHandler::canRead(QIODevice *device)
{ {
if (!device) { if (!device) {
@@ -655,8 +691,9 @@ bool RGBHandler::canRead(QIODevice *device)
int readBytes = head.size(); int readBytes = head.size();
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
@@ -668,28 +705,30 @@ bool RGBHandler::canRead(QIODevice *device)
return data.contains(regexp); return data.contains(regexp);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
QImageIOPlugin::Capabilities RGBPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities RGBPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "rgb" || format == "rgba" || if (format == "rgb" || format == "rgba" ||
format == "bw" || format == "sgi") format == "bw" || format == "sgi") {
return Capabilities(CanRead | CanWrite); return Capabilities(CanRead | CanWrite);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
Capabilities cap;
if (device->isReadable() && RGBHandler::canRead(device))
cap |= CanRead;
if (device->isWritable())
cap |= CanWrite;
return cap;
} }
Capabilities cap;
if (device->isReadable() && RGBHandler::canRead(device)) {
cap |= CanRead;
}
if (device->isWritable()) {
cap |= CanWrite;
}
return cap;
}
QImageIOHandler *RGBPlugin::create(QIODevice *device, const QByteArray &format) const QImageIOHandler *RGBPlugin::create(QIODevice *device, const QByteArray &format) const
{ {

View File

@@ -140,7 +140,8 @@ struct TgaHeaderInfo {
bool rgb; bool rgb;
bool grey; 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) { switch (tga.image_type) {
case TGA_TYPE_RLE_INDEXED: case TGA_TYPE_RLE_INDEXED:
rle = true; rle = true;
@@ -170,8 +171,6 @@ struct TgaHeaderInfo {
} }
}; };
static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img) static bool LoadTGA(QDataStream &s, const TgaHeader &tga, QImage &img)
{ {
// Create image. // Create image.
@@ -299,7 +298,6 @@ static bool LoadTGA(QDataStream & s, const TgaHeader & tga, QImage &img)
} // namespace } // namespace
TGAHandler::TGAHandler() TGAHandler::TGAHandler()
{ {
} }
@@ -320,7 +318,6 @@ bool TGAHandler::read(QImage *outImage)
QDataStream s(device()); QDataStream s(device());
s.setByteOrder(QDataStream::LittleEndian); s.setByteOrder(QDataStream::LittleEndian);
// Read image header. // Read image header.
TgaHeader tga; TgaHeader tga;
s >> tga; s >> tga;
@@ -338,7 +335,6 @@ bool TGAHandler::read(QImage *outImage)
return false; return false;
} }
QImage img; QImage img;
bool result = LoadTGA(s, tga, img); bool result = LoadTGA(s, tga, img);
@@ -347,7 +343,6 @@ bool TGAHandler::read(QImage *outImage)
return false; return false;
} }
*outImage = img; *outImage = img;
return true; return true;
} }
@@ -359,8 +354,9 @@ bool TGAHandler::write(const QImage &image)
const QImage &img = image; const QImage &img = image;
const bool hasAlpha = (img.format() == QImage::Format_ARGB32); 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]; s << targaMagic[i];
}
// write header // write header
s << quint16(img.width()); // width s << quint16(img.width()); // width
@@ -374,9 +370,10 @@ bool TGAHandler::write(const QImage &image)
s << quint8(qBlue(color)); s << quint8(qBlue(color));
s << quint8(qGreen(color)); s << quint8(qGreen(color));
s << quint8(qRed(color)); s << quint8(qRed(color));
if (hasAlpha) if (hasAlpha) {
s << quint8(qAlpha(color)); s << quint8(qAlpha(color));
} }
}
return true; return true;
} }
@@ -413,18 +410,23 @@ bool TGAHandler::canRead(QIODevice *device)
QImageIOPlugin::Capabilities TGAPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities TGAPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "tga") if (format == "tga") {
return Capabilities(CanRead | CanWrite); return Capabilities(CanRead | CanWrite);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && TGAHandler::canRead(device)) if (device->isReadable() && TGAHandler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
if (device->isWritable()) }
if (device->isWritable()) {
cap |= CanWrite; cap |= CanWrite;
}
return cap; return cap;
} }

File diff suppressed because it is too large Load Diff

View File

@@ -18,7 +18,6 @@
static const int b_255_3[] = {0, 85, 170, 255}, // index*255/3 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 rg_255_7[] = {0, 36, 72, 109, 145, 182, 218, 255}; // index *255/7
XVHandler::XVHandler() XVHandler::XVHandler()
{ {
} }
@@ -43,40 +42,47 @@ bool XVHandler::read(QImage *retImage)
// magic number must be "P7 332" // magic number must be "P7 332"
iodev->readLine(str, BUFSIZE); iodev->readLine(str, BUFSIZE);
if (strncmp(str, "P7 332", 6)) if (strncmp(str, "P7 332", 6)) {
return false; return false;
}
// next line #XVVERSION // next line #XVVERSION
iodev->readLine(str, BUFSIZE); iodev->readLine(str, BUFSIZE);
if (strncmp(str, "#XVVERSION", 10)) if (strncmp(str, "#XVVERSION", 10)) {
return false; return false;
}
// now it gets interesting, #BUILTIN means we are out. // now it gets interesting, #BUILTIN means we are out.
// if IMGINFO comes, we are happy! // if IMGINFO comes, we are happy!
iodev->readLine(str, BUFSIZE); iodev->readLine(str, BUFSIZE);
if (strncmp(str, "#IMGINFO:", 9)) if (strncmp(str, "#IMGINFO:", 9)) {
return false; return false;
}
// after this an #END_OF_COMMENTS signals everything to be ok! // after this an #END_OF_COMMENTS signals everything to be ok!
iodev->readLine(str, BUFSIZE); iodev->readLine(str, BUFSIZE);
if (strncmp(str, "#END_OF", 7)) if (strncmp(str, "#END_OF", 7)) {
return false; return false;
}
// now a last line with width, height, maxval which is // now a last line with width, height, maxval which is
// supposed to be 255 // supposed to be 255
iodev->readLine(str, BUFSIZE); iodev->readLine(str, BUFSIZE);
sscanf(str, "%d %d %d", &x, &y, &maxval); sscanf(str, "%d %d %d", &x, &y, &maxval);
if (maxval != 255) if (maxval != 255) {
return false; return false;
}
int blocksize = x * y; int blocksize = x * y;
if (x < 0 || y < 0 || blocksize < x || blocksize < y) if (x < 0 || y < 0 || blocksize < x || blocksize < y) {
return false; return false;
}
// now follows a binary block of x*y bytes. // now follows a binary block of x*y bytes.
char *block = (char *) malloc(blocksize); char *block = (char *) malloc(blocksize);
if (!block) if (!block) {
return false; return false;
}
if (iodev->read(block, blocksize) != blocksize) { if (iodev->read(block, blocksize) != blocksize) {
free(block); free(block);
@@ -139,10 +145,10 @@ bool XVHandler::write(const QImage &image)
sprintf(str, "%i %i 255\n", w, h); sprintf(str, "%i %i 255\n", w, h);
f.write(str, strlen(str)); f.write(str, strlen(str));
QImage tmpImage(image); QImage tmpImage(image);
if (image.depth() == 1) if (image.depth() == 1) {
tmpImage = image.convertToFormat(QImage::Format_Indexed8, Qt::AutoColor); tmpImage = image.convertToFormat(QImage::Format_Indexed8, Qt::AutoColor);
}
uchar *buffer = new uchar[ w ]; uchar *buffer = new uchar[ w ];
@@ -185,8 +191,9 @@ bool XVHandler::canRead(QIODevice *device)
qint64 readBytes = device->read(head, sizeof(head)); qint64 readBytes = device->read(head, sizeof(head));
if (readBytes != sizeof(head)) { if (readBytes != sizeof(head)) {
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -194,8 +201,9 @@ bool XVHandler::canRead(QIODevice *device)
} }
if (device->isSequential()) { if (device->isSequential()) {
while (readBytes > 0) while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]); device->ungetChar(head[readBytes-- - 1]);
}
} else { } else {
device->seek(oldPos); device->seek(oldPos);
} }
@@ -205,18 +213,23 @@ bool XVHandler::canRead(QIODevice *device)
QImageIOPlugin::Capabilities XVPlugin::capabilities(QIODevice *device, const QByteArray &format) const QImageIOPlugin::Capabilities XVPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{ {
if (format == "xv") if (format == "xv") {
return Capabilities(CanRead | CanWrite); return Capabilities(CanRead | CanWrite);
if (!format.isEmpty()) }
if (!format.isEmpty()) {
return 0; return 0;
if (!device->isOpen()) }
if (!device->isOpen()) {
return 0; return 0;
}
Capabilities cap; Capabilities cap;
if (device->isReadable() && XVHandler::canRead(device)) if (device->isReadable() && XVHandler::canRead(device)) {
cap |= CanRead; cap |= CanRead;
if (device->isWritable()) }
if (device->isWritable()) {
cap |= CanWrite; cap |= CanWrite;
}
return cap; return cap;
} }