All pastes #1463681 Raw Edit

Mine

public cpp v1 · immutable
#1463681 ·published 2009-06-17 17:35 UTC
rendered paste body
#include <QtCore/QCoreApplication>#include <QFile>#include <QDataStream>#include <QDebug>#include <QImage>int main(int argc, char *argv[]){    QCoreApplication a(argc, argv);    QString fileName("fraktle3.gbr");    // ------------------------------------------------    QFile file(fileName);    file.open(QIODevice::ReadOnly);    quint32 headerSize;    quint32 version;    quint32 width;    quint32 height;    quint32 colorDepth;    quint32 magic;    quint32 spacing;    QString brushName;    char*   data;    QDataStream in(&file);    in >> headerSize            >> version            >> width            >> height            >> colorDepth            >> magic            >> spacing;    if ((magic != 0x47494D50))    {        qDebug() << "No GBR file!" << endl;        file.close();        exit(1);    }    if (version != 2)    {        qDebug() << "Unknown GBR version!" << endl;        file.close();        exit(1);    }    // read the brush name    unsigned int nameLength = headerSize-28;    char* brushName_c       = new char[nameLength];    in.readBytes(brushName_c, nameLength);    brushName = QString(brushName_c);    // read the image data    unsigned int dataLength = width*height*colorDepth;    data = new char[dataLength];    in.readBytes(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(width, height, imageFormat);    quint32 k = headerSize;    if (colorDepth == 1 && data)    {        // Grayscale        for (quint32 y = 0; y < height; y++)        {            for (quint32 x = 0; x < width; 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 < height; y++) {            for (quint32 x = 0; x < width; 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")))    {        qDebug() << "Failed saving the thumbnail..." << endl;        success = false;    }    // debugging    qDebug() << "headerSize" << headerSize << endl;    qDebug() << "version"    << version << endl;    qDebug() << "width"      << width << endl;    qDebug() << "height"     << height << endl;    qDebug() << "colorDepth" << colorDepth << endl;    qDebug() << "spacing"    << spacing << endl;    qDebug() << "brushName"  << brushName << endl;    qDebug() << "image data" << (data ? "yes" : "no") << endl;    // cleanup    delete brushName_c;    delete data;    return 0;}