All pastes #1463761 Raw Edit

Someone

public cpp v1 · immutable
#1463761 ·published 2009-06-17 18:33 UTC
rendered paste body
/*  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB.  If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */#include "gimpbrushcreator.h"#include <QImage>#include <QString>#include <QFile>#include <kdebug.h>extern "C"{    KDE_EXPORT ThumbCreator *new_creator()    {        return new GimpBrushCreator;    }}GimpBrushCreator::GimpBrushCreator(){}bool GimpBrushCreator::create(const QString &path, int width, int height, QImage &img){    Q_UNUSED(width)    Q_UNUSED(height)    QFile file(path);    file.open(QIODevice::ReadOnly);    quint32 headerSize;    quint32 version;    quint32 w;    quint32 h;    quint32 colorDepth;    quint32 magic;    quint32 spacing;    QString brushName;    QDataStream in(&file);    in >> headerSize >> version >> w >> h >> colorDepth >> magic >> spacing;    if ((magic != 0x47494D50))    {        kDebug() << "No GBR file!";        file.close();        return false;    }    if (version != 2)    {        kDebug() << "Unknown GBR version!";        file.close();        return false;    }    // read the brush name    unsigned int nameLength = headerSize - 28;    char* brushName_c       = new char[nameLength];    in.readRawData(brushName_c, nameLength);    brushName = QString(brushName_c);    kDebug() << "GBR brush name: " << brushName;    // read the image data    unsigned int dataLength = w * h * colorDepth;    char* data              = new char[dataLength];    in.readRawData(data, dataLength);    // close file    file.close();    // generate thumbnail    bool success = true;    QImage::Format imageFormat;    if (colorDepth == 1)    {        imageFormat = QImage::Format_RGB32;    }    else    {        imageFormat = QImage::Format_ARGB32;    }    QImage thumbnail(w, h, imageFormat);    quint32 k = headerSize;    if (colorDepth == 1 && data)    {        // Grayscale        for (quint32 y = 0; y < h; y++)        {            for (quint32 x = 0; x < w; x++, k++)            {                qint32 val = 255 - static_cast<uchar> (data[k]);                thumbnail.setPixel(x, y, qRgb(val, val, val));            }        }    }    else if (colorDepth == 4 && data)    {        // RGBA        for (quint32 y = 0; y < h; y++)        {            for (quint32 x = 0; x < w; x++, k += 4)            {                thumbnail.setPixel(x, y, qRgba(data[k], data[k + 1], data[k + 2], data[k + 3]));            }        }    }    else    {        success = false;    }    if (!success || !thumbnail.save(QString("thumbnail.png")))    {        kDebug() << "Failed generating GBR thumbnail...";        success = false;    }    else    {        // load image data into reference        img = thumbnail;    }//    // debugging//    kDebug() << "headerSize" << m_headerSize;//    kDebug() << "version" << m_version;//    kDebug() << "width" << m_width;//    kDebug() << "height" << m_height;//    kqDebug() << "colorDepth" << m_colorDepth;//    kDebug() << "spacing" << m_spacing;//    kDebug() << "brushName" << m_brushName;//    kDebug() << "image data" << (data ? "yes" : "no");    // cleanup    delete[] brushName_c;    delete[] data;    return success;}ThumbCreator::Flags GimpBrushCreator::flags() const{    return None;}