qimageioloader.cpp 5.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
    Large image load library -- QImageIO decoder

    Copyright (C) 2007-2009 Allan Sandfeld Jensen <sandfeld@kde.org>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include <QBuffer>
#include <QByteArray>
#include <QStringList>
#include <QImageReader>

#include "qimageioloader.h"
#include "imageloader.h"
#include "kservice.h"
#include "kservicetypetrader.h"
#include "kdebug.h"
35
#include "imagemanager.h"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

namespace khtmlImLoad {

class QImageIOLoader: public ImageLoader
{
    QByteArray array;
    QImage image;
public:
    QImageIOLoader()
    {
    }

    ~QImageIOLoader()
    {
    }

    virtual int processData(uchar* data, int length)
    {
        //Collect data in the buffer
        int pos = array.size();
        array.resize(array.size() + length);
        memcpy(array.data() + pos, data, length);
        return length;
    }

    virtual int processEOF()
    {
        QBuffer buffer(&array);
        buffer.open(QIODevice::ReadOnly);

        QByteArray qformat = QImageReader::imageFormat(&buffer);
        QImageReader reader(&buffer, qformat);

        if (!reader.canRead()) {
            return Error;
        }

        QSize size = reader.size();
74 75 76 77 78 79
        if (size.isValid()) {
            if (ImageManager::isAcceptableSize(size.width(), size.height()))
                notifyImageInfo(size.width(), size.height());
            else
                return Error;
        }
80 81 82 83 84

        if (!reader.read(&image)) {
            return Error;
        }

85 86 87 88 89 90 91
        if (!size.isValid()) {
            // Might be too late by now..
            if (ImageManager::isAcceptableSize(image.width(), image.height()))
                notifyImageInfo(image.width(), image.height());
            else
                return Error;
        }
92

93
        ImageFormat format;
94 95 96 97 98
        if (!imageFormat(image, format)) {
            return Error;
        }
        notifyAppendFrame(image.width(), image.height(), format);

99
        notifyQImage(1, &image);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

        return Done;
    }
    bool imageFormat(QImage &image, ImageFormat &format) {
        switch(image.format()) {
        case QImage::Format_RGB32:
            format.type  = ImageFormat::Image_RGB_32;
            break;
        case QImage::Format_ARGB32:
            format.type  = ImageFormat::Image_ARGB_32_DontPremult;
            break;
        case QImage::Format_ARGB32_Premultiplied:
            format.type  = ImageFormat::Image_ARGB_32;
            break;
        case QImage::Format_Indexed8:
            format.type  = ImageFormat::Image_Palette_8;
            format.palette = image.colorTable();
            break;
        case QImage::Format_Mono:
        case QImage::Format_MonoLSB:
120 121 122 123
            image = image.convertToFormat(QImage::Format_Indexed8);
            format.type  = ImageFormat::Image_Palette_8;
            format.palette = image.colorTable();
            break;
124 125 126 127 128 129 130 131 132
        case QImage::Format_Invalid:
        default:
            // unsupported formats
            return false;
        }
        return true;
    }
};

133
static const char* const positiveList[] = {
Wolfgang Bauer's avatar
Wolfgang Bauer committed
134
    "BMP", "TIFF", "JP2", "PNM", "EXR", "XBM", "XPM", "ICO", "SVG", "SVGZ", 0
135 136
};

137 138 139 140 141 142 143 144 145
bool isSupportedFormat(QString format) {
    QStringList pList;
    for(int i=0; positiveList[i]; i++) {
        pList.append(QString::fromLatin1(positiveList[i]));
    }

    return pList.contains(format, Qt::CaseInsensitive);
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159
static QStringList s_formats;

ImageLoaderProvider::Type QImageIOLoaderProvider::type()
{
    return Foreign;
}

const QStringList& QImageIOLoaderProvider::mimeTypes()
{
    if (!s_formats.isEmpty()) return s_formats;

//     QList<QByteArray> formats = QImageIOReader::supportedFormats();
    KService::List services = KServiceTypeTrader::self()->query("QImageIOPlugins");

Albert Astals Cid's avatar
Albert Astals Cid committed
160
    foreach(const KService::Ptr &service, services) {
161 162 163
        QStringList formats = service->property("X-KDE-ImageFormat").toStringList();
        QString mimetype = service->property("X-KDE-MimeType").toString();
        bool positive = false;
Albert Astals Cid's avatar
Albert Astals Cid committed
164
        foreach(const QString &format, formats) {
165
            if (isSupportedFormat(format)) {
166 167 168 169 170 171 172
                positive = true;
                break;
            }
        }
        if (!positive) continue;
        if (!mimetype.isEmpty()) {
            s_formats.append(mimetype);
173
            kDebug(399) << "QImageIO - Format supported: " << mimetype << endl;
174 175 176 177 178 179 180 181 182 183 184 185
        }
    }
    return s_formats;
}

ImageLoader* QImageIOLoaderProvider::loaderFor(const QByteArray& prefix)
{
    QByteArray pref = prefix;
    QBuffer prefixBuffer(&pref);
    prefixBuffer.open(QIODevice::ReadOnly);
    QByteArray format = QImageReader::imageFormat(&prefixBuffer);
    prefixBuffer.close();
186
    if (format.isEmpty() || !isSupportedFormat(format))
187 188
        return 0;
    else
189
        kDebug(399) << "QImageIO - Format guessed: " << format << endl;
190 191 192 193 194

    return new QImageIOLoader;
}

} // namespace