Commit 1d358bed authored by Emanuele Tamponi's avatar Emanuele Tamponi

Tests & fixes.

svn path=/trunk/koffice/; revision=752752
parent 98de8b37
......@@ -25,6 +25,7 @@
#include <QFile>
#include <QString>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
......
......@@ -33,7 +33,6 @@
#include <gsl/gsl_blas.h>
#include <gsl/gsl_cblas.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
......@@ -41,7 +40,7 @@
KisKS3ColorSpace::KisKS3ColorSpace(KoColorProfile *p)
: parent( "ks3colorspace",
"KS Color Space - 3 wavelenghts",
KoColorSpaceRegistry::instance()->rgb16("") )
KoColorSpaceRegistry::instance()->rgb16("") ), m_inverse(0), m_rgbvec(0), m_refvec(0)
{
if (!profileIsCompatible(p))
return;
......@@ -49,9 +48,6 @@ KisKS3ColorSpace::KisKS3ColorSpace(KoColorProfile *p)
m_profile = dynamic_cast<KisIlluminantProfile *>(p);
m_converter = ChannelConverter(m_profile->Swhite(), m_profile->Kblack());
m_permutation = gsl_permutation_alloc(3);
gsl_linalg_LU_decomp(m_profile->T(), m_permutation, &m_s);
m_rgbvec = gsl_vector_alloc(3);
m_refvec = gsl_vector_alloc(3);
......@@ -82,13 +78,29 @@ KisKS3ColorSpace::KisKS3ColorSpace(KoColorProfile *p)
addCompositeOp( new KoCompositeOpMultiply<KisKS3ColorSpaceTrait>(this) );
addCompositeOp( new KoCompositeOpDivide<KisKS3ColorSpaceTrait>(this) );
addCompositeOp( new KoCompositeOpBurn<KisKS3ColorSpaceTrait>(this) );
int s;
gsl_permutation *perm = gsl_permutation_alloc(3);
gsl_matrix *tmp = gsl_matrix_alloc(m_profile->T()->size1, m_profile->T()->size2);
m_inverse = gsl_matrix_alloc(tmp->size1, tmp->size2);
gsl_matrix_memcpy(tmp, m_profile->T());
gsl_linalg_LU_decomp(tmp, perm, &s);
gsl_linalg_LU_invert(tmp, perm, m_inverse);
gsl_permutation_free(perm);
gsl_matrix_free(tmp);
}
KisKS3ColorSpace::~KisKS3ColorSpace()
{
gsl_permutation_free(m_permutation);
gsl_vector_free(m_rgbvec);
gsl_vector_free(m_refvec);
if (m_rgbvec)
gsl_vector_free(m_rgbvec);
if (m_refvec)
gsl_vector_free(m_refvec);
if (m_inverse)
gsl_matrix_free(m_inverse);
}
bool KisKS3ColorSpace::operator==(const KoColorSpace& rhs) const
......@@ -116,6 +128,8 @@ bool KisKS3ColorSpace::profileIsCompatible(const KoColorProfile *profile) const
return true;
}
#define NORM 65535.0f
void KisKS3ColorSpace::fromRgbA16(const quint8 *srcU8, quint8 *dstU8, quint32 nPixels) const
{
// For each pixel we do this:
......@@ -127,13 +141,13 @@ void KisKS3ColorSpace::fromRgbA16(const quint8 *srcU8, quint8 *dstU8, quint32 nP
quint8 *dst = dstU8;
float c;
for (quint32 pixel = 0; pixel < nPixels; pixel++) {
for ( ; nPixels > 0; nPixels-- ) {
for (int i = 0; i < 3; i++) {
m_converter.sRGBToRGB((float)srcU16[2-i], c);
m_converter.sRGBToRGB((float)srcU16[2-i]/NORM, c);
gsl_vector_set(m_rgbvec, i, (double)c);
}
gsl_linalg_LU_solve(m_profile->T(), m_permutation, m_rgbvec, m_refvec);
gsl_blas_dgemv(CblasNoTrans, 1.0, m_inverse, m_rgbvec, 0.0, m_refvec);
for (int i = 0; i < 3; i++)
m_converter.reflectanceToKS((float)gsl_vector_get(m_refvec, i),
KisKS3ColorSpaceTrait::K(dst, i),
......@@ -152,7 +166,7 @@ void KisKS3ColorSpace::toRgbA16(const quint8 *srcU8, quint8 *dstU8, quint32 nPix
const quint8 *src = srcU8;
float c;
for (quint32 pixel = 0; pixel < nPixels; pixel++) {
for ( ; nPixels > 0; nPixels-- ) {
for (int i = 0; i < 3; i++) {
m_converter.KSToReflectance(KisKS3ColorSpaceTrait::K(src, i),
KisKS3ColorSpaceTrait::S(src, i), c);
......@@ -162,7 +176,7 @@ void KisKS3ColorSpace::toRgbA16(const quint8 *srcU8, quint8 *dstU8, quint32 nPix
for (int i = 0; i < 3; i++) {
m_converter.RGBTosRGB((float)gsl_vector_get(m_rgbvec, i), c);
dstU16[2-i] = (quint16)c;
dstU16[2-i] = (quint16)(c * NORM);
}
dstU16[3] = KoColorSpaceMaths<float,quint16>::scaleToA(KisKS3ColorSpaceTrait::alpha(src));
......
......@@ -24,7 +24,7 @@
#include "channel_converter.h"
#include <KoIncompleteColorSpace.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
class KisIlluminantProfile;
......@@ -47,6 +47,13 @@ class KisKS3ColorSpace : public KoIncompleteColorSpace<KisKS3ColorSpaceTrait>
const KoColorProfile *profile() const;
bool profileIsCompatible(const KoColorProfile *profile) const;
KoColorSpace* clone() const { return 0; }
KoID colorModelId() const { return KoID(); }
KoID colorDepthId() const { return KoID(); }
bool willDegrade(ColorSpaceIndependence) const { return true; }
void colorToXML(const quint8*, QDomDocument&, QDomElement&) const {}
void colorFromXML(quint8*, const QDomElement&) const {}
void fromRgbA16(const quint8 *srcU8, quint8 *dstU8, quint32 nPixels) const;
void toRgbA16(const quint8 *srcU8, quint8 *dstU8, quint32 nPixels) const;
......@@ -55,8 +62,7 @@ class KisKS3ColorSpace : public KoIncompleteColorSpace<KisKS3ColorSpaceTrait>
KisIlluminantProfile *m_profile;
ChannelConverter m_converter;
int m_s;
gsl_permutation *m_permutation;
gsl_matrix *m_inverse;
gsl_vector *m_rgbvec;
gsl_vector *m_refvec;
......
......@@ -19,17 +19,112 @@
#include <qtest_kde.h>
#include <cstring>
#include "kis_ks3_colorspace_test.h"
#include "kis_ks3_colorspace.h"
#include "kis_illuminant_profile.h"
void KisKS3ColorSpaceTest::testConstructor()
{
KisIlluminantProfile *p1 = new KisIlluminantProfile("D653Test.ill");
KisIlluminantProfile *p2 = new KisIlluminantProfile("D659Test.ill");
KisKS3ColorSpace *cs1 = new KisKS3ColorSpace(p1);
QVERIFY(cs1->profileIsCompatible(p2) == false);
delete cs1;
delete p2;
delete p1;
}
void KisKS3ColorSpaceTest::testFromRgbA16()
template<typename type, int n>
void print_vector(quint8 *v, const QString &text)
{
QString vstr;
qDebug() << text;
for (int i = 0; i < n; i++)
vstr += QString::number(reinterpret_cast<type*>(v)[i]) + " ";
qDebug() << vstr;
}
void KisKS3ColorSpaceTest::testToFromRgbA16()
{
KisIlluminantProfile *p = new KisIlluminantProfile("D653Test.ill");
KisKS3ColorSpace *cs = new KisKS3ColorSpace(p);
quint8 *rgb1;
quint8 *kas1 = new quint8[2*cs->pixelSize()];
quint8 *rgb2 = new quint8[2*8];
quint32 val = 65535;
quint16 blue[4] = { val, 0, 0, val };
quint16 green[4] = { 0, val, 0, val };
quint16 red[4] = { 0, 0, val, val };
quint16 yellow[4] = { 0, val, val, val };
quint16 bluegreen[8] = { val, 0, 0, val, 0, val, 0, val };
rgb1 = reinterpret_cast<quint8*>(blue);
cs->fromRgbA16(rgb1, kas1, 1);
cs->toRgbA16(kas1, rgb2, 1);
print_vector<quint16, 4>(rgb1, "BLUE:");
print_vector<float, 7>(kas1, "BLUE IN KS:");
print_vector<quint16, 4>(rgb2, "BLUE AGAIN:");
QVERIFY(rgb1[0] == rgb2[0]);
QVERIFY(rgb1[1] == rgb2[1]);
QVERIFY(rgb1[2] == rgb2[2]);
QVERIFY(rgb1[3] == rgb2[3]);
rgb1 = reinterpret_cast<quint8*>(green);
cs->fromRgbA16(rgb1, kas1, 1);
cs->toRgbA16(kas1, rgb2, 1);
print_vector<quint16, 4>(rgb1, "GREEN:");
print_vector<float, 7>(kas1, "GREEN IN KS:");
print_vector<quint16, 4>(rgb2, "GREEN AGAIN:");
QVERIFY(rgb1[0] == rgb2[0]);
QVERIFY(rgb1[1] == rgb2[1]);
QVERIFY(rgb1[2] == rgb2[2]);
QVERIFY(rgb1[3] == rgb2[3]);
rgb1 = reinterpret_cast<quint8*>(red);
cs->fromRgbA16(rgb1, kas1, 1);
cs->toRgbA16(kas1, rgb2, 1);
print_vector<quint16, 4>(rgb1, "RED:");
print_vector<float, 7>(kas1, "RED IN KS:");
print_vector<quint16, 4>(rgb2, "RED AGAIN:");
QVERIFY(rgb1[0] == rgb2[0]);
QVERIFY(rgb1[1] == rgb2[1]);
QVERIFY(rgb1[2] == rgb2[2]);
QVERIFY(rgb1[3] == rgb2[3]);
rgb1 = reinterpret_cast<quint8*>(yellow);
cs->fromRgbA16(rgb1, kas1, 1);
cs->toRgbA16(kas1, rgb2, 1);
print_vector<quint16, 4>(rgb1, "YELLOW:");
print_vector<float, 7>(kas1, "YELLOW IN KS:");
print_vector<quint16, 4>(rgb2, "YELLOW AGAIN:");
QVERIFY(rgb1[0] == rgb2[0]);
QVERIFY(rgb1[1] == rgb2[1]);
QVERIFY(rgb1[2] == rgb2[2]);
QVERIFY(rgb1[3] == rgb2[3]);
rgb1 = reinterpret_cast<quint8*>(bluegreen);
cs->fromRgbA16(rgb1, kas1, 2);
cs->toRgbA16(kas1, rgb2, 2);
print_vector<quint16, 8>(rgb1, "BLUE AND GREEN:");
print_vector<float, 14>(kas1, "BLUE AND GREEN IN KS:");
print_vector<quint16, 8>(rgb2, "BLUE AND GREEN AGAIN:");
QVERIFY(rgb1[0] == rgb2[0]);
QVERIFY(rgb1[1] == rgb2[1]);
QVERIFY(rgb1[2] == rgb2[2]);
QVERIFY(rgb1[3] == rgb2[3]);
QVERIFY(rgb1[4] == rgb2[4]);
QVERIFY(rgb1[5] == rgb2[5]);
QVERIFY(rgb1[6] == rgb2[6]);
QVERIFY(rgb1[7] == rgb2[7]);
}
void KisKS3ColorSpaceTest::testToRgbA16()
void KisKS3ColorSpaceTest::testMixing()
{
}
......
......@@ -29,8 +29,8 @@ class KisKS3ColorSpaceTest : public QObject
private slots:
void testConstructor();
void testFromRgbA16();
void testToRgbA16();
void testToFromRgbA16();
void testMixing();
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment