diff --git a/TESTS/TEST_COLOR_MAP/main.cpp b/TESTS/TEST_COLOR_MAP/main.cpp index 77f7a579d432cfa97344fc1057dcba25de1b66bb..d85e4acf3b11cfd0311f70b3090dfb78e2c952de 100644 --- a/TESTS/TEST_COLOR_MAP/main.cpp +++ b/TESTS/TEST_COLOR_MAP/main.cpp @@ -106,6 +106,44 @@ bool testWriteColorMapPng3(){ return b; } +///Test the write of a png file +/** @return true on success, false otherwise +*/ +bool testWriteColorMapPng4(){ + std::string fileName("outputColorMap4.png"); + bool b(true); + + PColorMap colorMap; +// 0,#000000,0.2,#00FF00,0.21,#FFFF00,0.4,#FF0000,0.6,#FFFFFF + colorMap.addColor(0.0, 0, 0, 0, 0); + colorMap.addColor(5.0, "00FF00FF"); + colorMap.addColor(10.0, "00FF0099"); + colorMap.addColor(20.0, "FFFF0055"); + colorMap.addColor(30.0, "FF0000AA"); + colorMap.addColor(40.0, "FFFFFFFF"); + + size_t width(640lu), height(480lu); + PImagePng image; + b &= image.createImage(width, height, PImagePng::RGBA); + + float * matValue = new float[width*height]; + + image.fill(18, 234, 142); + for(size_t j(0lu); j < height; ++j){ + for(size_t i(0lu); i < width; ++i){ + float value(((j*width + i)/1000lu)%42lu); + matValue[j*width + i] = value; + } + } + + image.setColor(matValue, height, width, colorMap); + + b &= image.write(fileName); + + delete [] matValue; + return b; +} + ///The the basic functions of a color map /** @return true on success, false otherwise */ @@ -133,6 +171,7 @@ int main(int argc, char** argv){ b &= testWriteColorMapPng(); b &= testWriteColorMapPng2(); b &= testWriteColorMapPng3(); + b &= testWriteColorMapPng4(); b &= testBaseColorMap(); return b - 1; } diff --git a/src/PImagePng.h b/src/PImagePng.h index eb278c45211c8024b1cf5cc0ac6e84d7df1cb530..d4a38b2bd814e630f0eb6072733a1d770bb6975e 100644 --- a/src/PImagePng.h +++ b/src/PImagePng.h @@ -41,10 +41,14 @@ class PImagePng{ void setColor(size_t idxWidth, size_t idxHeight, color_t red, color_t green, color_t blue); void setColor(size_t idxWidth, size_t idxHeight, color_t red, color_t green, color_t blue, color_t alpha); + template + void setColor(const T * matValue, size_t nbRow, size_t nbCol, const PColorMap & colorMap); + void getColor(size_t idxWidth, size_t idxHeight, color_t & red, color_t & green, color_t & blue); void getColor(size_t idxWidth, size_t idxHeight, color_t & red, color_t & green, color_t & blue, color_t & alpha); + unsigned int getWidth() const; unsigned int getHeight() const; NbColorByte getNbBytePerPixel() const; @@ -66,7 +70,7 @@ class PImagePng{ NbColorByte p_nbBytePerPixel; }; - +#include "PImagePng_impl.h" #endif diff --git a/src/PImagePng_impl.h b/src/PImagePng_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..93c6f778ae94ea5522d8b2915a5187b5dab70e18 --- /dev/null +++ b/src/PImagePng_impl.h @@ -0,0 +1,36 @@ +/*************************************** + Auteur : Pierre Aubert + Mail : aubertp7@gmail.com + Licence : CeCILL-C +****************************************/ + +#ifndef __PIMAGEPNG_IMPL_H__ +#define __PIMAGEPNG_IMPL_H__ + +#include +#include "PImagePng.h" + +///Set the color of the image by respect to a given matrix of values and the colorMap +/** @param matValue : matrix of value to be used to colorize the PImagePng + * @param nbRow : number of rows of the matrix + * @param nbCol : number of columns of the matrix + * @param colorMap : PColorMap to be used to convert values in color +*/ +template +void PImagePng::setColor(const T * matValue, size_t nbRow, size_t nbCol, const PColorMap & colorMap){ + size_t nbValueRow(std::min(nbRow, (size_t)p_image.height)); + size_t nbValueCol(std::min(nbCol, (size_t)p_image.width)); + + for(size_t i(0lu); i < nbValueRow; ++i){ + for(size_t j(0lu); j < nbValueCol; ++j){ + float value(matValue[i*nbCol + j]); + color_t red(0), green(0), blue(0), alpha(0); + colorMap.interpolate(red, green, blue, alpha, value); + setColor(j, i, red, green, blue, alpha); + } + } + +} + +#endif +