[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-kimageshop
Subject:    illuminants
From:       Thomas Zander <zander () kde ! org>
Date:       2008-05-15 21:00:07
Message-ID: 200805152300.08031.zander () kde ! org
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


As various people on #koffice observed the illuminants loading code would 
benefit from being turned into an xml loader so it becomes more stable 
and 'future proof'.

I did some work on this but am unable to finish it.  Maybe someone else can 
pick up where I left off?
I left loads of comments so it should not be too hard. ;)
-- 
Thomas Zander

["reader.pro" (text/plain)]

######################################################################
# Automatically generated by qmake (2.01a) Thu May 15 22:03:40 2008
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .
LIBS += -lQtXml

# Input
SOURCES += main.cpp

["test-ill.xml" (text/xml)]

<?xml version="1.0" encoding="UTF-8"?>
<illuminants>
    <profile name="foo">
        <transformation columns="2">
            <matrix-row>1.5,   1.0</matrix-row>
            <matrix-row>4.51, 1.5   </matrix-row>
            <matrix-row>8.0, 4.1</matrix-row>
        </transformation>
        <primaries rows="2">
<!-- Emanuell said columns is pre-defined here.  Which doesn't make sense to me since \
we have R,G & B; which in my  mind makes 3 pre-defined rows.
    So if he encoded    <row>R G B</row> <row>R G B</row>
    I strongly suggest to reverse that to <row>R R</row><row>G G</row><row>B B</row>
-->
            <matrix-row>1.1, 1, 8</matrix-row>
            <matrix-row>4.5, 1, 5</matrix-row>
        </primaries>
        <coefficient>1.84</coefficient>
        <coefficient>1.84</coefficient>
        <coefficient>1.84</coefficient>
    </profile>
</illuminants>


["main.cpp" (text/x-c++src)]

#include <QtCore>
#include <QtXml/QtXml>

static double* readMatrixRow(QDomElement &matrixRow, int columns)
{
    if (matrixRow.isNull())
        return 0;

    double *answer = new double[columns];
    QString val = matrixRow.text();
    QStringList items = val.replace(',',' ').trimmed().split(' ', \
QString::SkipEmptyParts);  for (int column = 0; column < columns; ++column) {
        answer[column] = items[column].toDouble();
//qDebug() << answer[column];
    }
    return answer;
}

int main(int c, char **v) {
    QCoreApplication app(c, v);
    if (c < 2) {
        QTextStream(stdout) << "Usage; loader FILE\n  FILE is an illumunant xml \
file\n";  return 1;
    }

    QFile file(app.arguments()[1]);
    if (! file.open(QIODevice::ReadOnly)) {
        QTextStream(stderr) << "Could not open file for reading; " << \
app.arguments()[1] << endl;  return 1;
    }


    // copied from kis_illuminant_profile.h
    struct ProfileData {
        ProfileData() : m_valid(false) {}
        int m_wl;
        double **m_T; // array of arrays? Why not a 2D array? Also, is it rows or \
columsn first?  double *m_red;
        double *m_green;
        double *m_blue;

        double *m_refvec;

        int nc;
        double *coeffs;

        QString m_illuminant;
        bool m_valid;
    };

    ////// tmp data
    QDomDocument doc;
    QString error;
    if (!doc.setContent(&file, false)) {
        QTextStream(stderr) << "XML Parse error\n";
        return 1;
    }
    QDomElement root = doc.documentElement();
    if (root.nodeName() != "illuminants") {
        QTextStream(stderr) << "No illuminants file\n";
        return 1;
    }

    QDomElement profile = root.firstChildElement("profile");
    while (!profile.isNull()) {
        ProfileData pd;
        pd.m_illuminant = profile.attribute("name", "unnamed");
        QDomElement transformation = profile.firstChildElement("transformation");
        QDomElement primaries = profile.firstChildElement("primaries");
        if (transformation.isNull() || primaries.isNull()) {
            QTextStream(stderr) << "Missing data in profile" << pd.m_illuminant << \
endl;  continue;
        }

        // transformation -------
        const int columns = transformation.attribute("columns", "1").toInt();
        pd.m_T = new double*[3]; // TODO not sure if we should allocate rows first or \
                columns first..
                                // if we should allocate columns first the above 3 \
                should be replaced by
                                // columns.  and so should the 3 in the next for-loop
        QDomElement row = transformation.firstChildElement("matrix-row");
        for(int i=0; i < 3; ++i) {
            pd.m_T[i] = readMatrixRow(row, columns);
            row = row.nextSiblingElement("matrix-row");
        }

        // TODO read primaries -------
        // see comment in xml file.

        // coefficient -------
        // lets read it twice (its a dom, so who cares) in order to first get the \
                count.
        // Its easy to count here and not having it in the xml-file means its better
        // guarded against inconsistent alterations (i.e. when an item is removed but
        // the count not updated)
        QDomElement coefficient = profile.firstChildElement("coefficient");
        int coeffsCount = 0;
        while(! coefficient.isNull()) {
            ++coeffsCount;
            coefficient = coefficient.nextSiblingElement("coefficient");
        }
        pd.coeffs = new double[coeffsCount];

        coefficient = profile.firstChildElement("coefficient");
        for (int i=0; i < coeffsCount; ++i) {
            pd.coeffs[i] = coefficient.text().toDouble();
//qDebug() << pd.coeffs[i];
            coefficient = coefficient.nextSiblingElement("coefficient");
        }
        pd.m_valid = true; // we could check more here, like the coeffsCount being \
                equal to the transformation columns etc.
        profile = profile.nextSiblingElement("profile"); // allow for another profile \
in the same XML  }

    return 0;
}


["signature.asc" (application/pgp-signature)]

_______________________________________________
kimageshop mailing list
kimageshop@kde.org
https://mail.kde.org/mailman/listinfo/kimageshop


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic