WSL/SLF GitLab Repository

Commit f8e83d3b authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Standardizing RGB colors as interlaced in Gradients and PNGIO

parent 7f97c133
......@@ -395,7 +395,7 @@ void Gradient::getColor(const double& val, unsigned char& index) const
else index = static_cast<unsigned char>( (val-min)/delta*(double)nr_unique_cols ) + reserved_idx;
}
void Gradient::getPalette(std::vector<unsigned char> &r, std::vector<unsigned char> &g, std::vector<unsigned char> &b) const
void Gradient::getPalette(std::vector<unsigned char> &palette, size_t &nr_colors) const
{
if(model==NULL) {
throw UnknownValueException("Please set the color gradient before using it!", AT);
......@@ -404,37 +404,46 @@ void Gradient::getPalette(std::vector<unsigned char> &r, std::vector<unsigned ch
throw UnknownValueException("Please define the number of colors for indexed gradients!", AT);
}
r.clear(); g.clear(); b.clear();
palette.clear();
//transparent color
r.push_back(channel_max_color); g.push_back(channel_max_color); b.push_back(channel_max_color);
palette.push_back(channel_max_color); palette.push_back(channel_max_color); palette.push_back(channel_max_color);
//legend background color
r.push_back(channel_max_color-1); g.push_back(channel_max_color-1); b.push_back(channel_max_color-1);
palette.push_back(channel_max_color-1); palette.push_back(channel_max_color-1); palette.push_back(channel_max_color-1);
//legend text color
r.push_back(0); g.push_back(0); b.push_back(0);
palette.push_back(0); palette.push_back(0); palette.push_back(0);
double r_d, g_d, b_d;
//underflow data color
model->getColor(-0.1, r_d, g_d, b_d);
r.push_back( static_cast<unsigned char>(r_d*channel_max_color) );
g.push_back( static_cast<unsigned char>(g_d*channel_max_color) );
b.push_back( static_cast<unsigned char>(b_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(r_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(g_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(b_d*channel_max_color) );
//overflow data color
model->getColor(1.1, r_d, g_d, b_d);
r.push_back( static_cast<unsigned char>(r_d*channel_max_color) );
g.push_back( static_cast<unsigned char>(g_d*channel_max_color) );
b.push_back( static_cast<unsigned char>(b_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(r_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(g_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(b_d*channel_max_color) );
//all normal colors
for(unsigned char ii=0; ii<=nr_unique_cols; ii++) {
const double val_norm = (double)ii/(double)nr_unique_cols;
model->getColor(val_norm, r_d, g_d, b_d);
r.push_back( static_cast<unsigned char>(r_d*channel_max_color) );
g.push_back( static_cast<unsigned char>(g_d*channel_max_color) );
b.push_back( static_cast<unsigned char>(b_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(r_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(g_d*channel_max_color) );
palette.push_back( static_cast<unsigned char>(b_d*channel_max_color) );
}
const size_t nr_entries = palette.size();
if((nr_entries%3) != 0) {
std::stringstream ss;
ss << "Error when creating color palette: " << nr_entries << " data points ";
ss << "for 3 channels palette is impossible! (colors are interlaced)";
throw IOException(ss.str(), AT);
}
nr_colors = nr_entries/3;
}
//we assume that the vectors are sorted by X
......
......@@ -254,12 +254,11 @@ class Gradient {
/**
* @brief Get palette colors for the selected gradient
* When building an indexed image, one needs to first retrieve the palette using this method. Afterwards, getColor(val, index)
* will be called for each pixel in order to retrieve its palette index.
* @param r red components
* @param g green components
* @param b blue components
* will be called for each pixel in order to retrieve its palette index. The returned colors are interlaced (rgb).
* @param palette interlaced colors
* @param nr_colors number of colors in the palette
*/
void getPalette(std::vector<unsigned char> &r, std::vector<unsigned char> &g, std::vector<unsigned char> &b) const;
void getPalette(std::vector<unsigned char> &palette, size_t &nr_colors) const;
static const unsigned char channel_max_color; ///< nr of colors per channel of the generated gradients
private:
......
......@@ -432,14 +432,15 @@ void PNGIO::writeDataSection(const Grid2DObject &grid, const Array2D<double> &le
void PNGIO::setPalette(const Gradient &gradient, png_structp& png_ptr, png_infop& info_ptr, png_color *palette)
{
std::vector<unsigned char> r, g, b;
gradient.getPalette(r,g,b);
const size_t nr_colors = r.size();
std::vector<unsigned char> pal;
size_t nr_colors;
gradient.getPalette(pal, nr_colors);
palette = (png_color*)calloc(sizeof (png_color), nr_colors); //ie: three png_bytes, each being an unsigned char
for(size_t ii=0; ii<nr_colors; ii++) {
palette[ii].red = static_cast<png_byte>(r[ii]);
palette[ii].green = static_cast<png_byte>(g[ii]);
palette[ii].blue = static_cast<png_byte>(b[ii]);
const size_t interlace = ii*3; //colors from Gradient interlaced
palette[ii].red = static_cast<png_byte>(pal[interlace]);
palette[ii].green = static_cast<png_byte>(pal[interlace+1]);
palette[ii].blue = static_cast<png_byte>(pal[interlace+2]);
}
png_set_PLTE(png_ptr, info_ptr, palette, nr_colors);
}
......
Supports Markdown
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