123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263 |
- #include "pch.h"
- #include "ImageHDR.hpp"
- #include "MT_exposure.hpp"
- #include "MT_contrast.hpp"
- #include "MT_histogram_regularization.hpp"
- #include "MT_lightnessMask.hpp"
- #include "MT_saturation.hpp"
- #include "MT_colorEditor.hpp"
- #include "Conversion.hpp"
- #include "YCurve.hpp"
- #include "Utils.hpp"
- #include <cmath>
- #include <thread>
- #include <iostream>
- #include <vector>
- #include <ctime>
- #include <cstdlib>
- /* Member methods*/
- ImageHDR::ImageHDR(float* d, unsigned int w, unsigned int h)
- {
- width = w;
- height = h;
- this->min_intensity = d[0];
- this->max_intensity = d[0];
- data = new float[width*height*3];
- for (unsigned int i = 0; i < width*height*3; i++) {
- data[i]=d[i];
- if (this->min_intensity > data[i])
- this->min_intensity = data[i];
- if (this->max_intensity < data[i])
- this->max_intensity = data[i];
- }
- linear = true;
- colorspace = Colorspace::RGB;
- }
- void ImageHDR::display_pixel(unsigned int i) const
- {
- if (linear)
- std::cout << "LINEAIRE - ";
- else
- std::cout << "NON LINEAIRE - ";
- std::cout << "Pixel : ( " << data[i * 3] << " " << data[i * 3 + 1] << " " << data[i * 3 + 2] << " )" << std::endl;
- }
- void ImageHDR::display_pixel(unsigned int i, unsigned int j) const
- {
- display_pixel(j * width + i);
- }
- /****************************************/
- /**************** LINEAR ****************/
- /****************************************/
- void ImageHDR::linear_to_non_linear()
- {
- float* non_linear = Conversion::linear_to_non_linear(data, width * height * 3);
- delete[](data);
- data = non_linear;
- }
- void ImageHDR::non_linear_to_linear()
- {
- float* linear = Conversion::non_linear_to_linear(data, width * height * 3);
- delete[](data);
- data = linear;
- }
- /****************************************/
- /*************** EXPOSURE ***************/
- /****************************************/
- #ifdef _MT_
- void* exposure_MT(void* arg)
- {
- MT_exposure* a = (MT_exposure*)arg;
- float* data = a->data;
- for (unsigned int i = 0; i < a->length; i++)
- data[i] *= a->coeff;
- return arg;
- }
- void ImageHDR::exposure(const float ev)
- {
- float coeff = powf(2, ev);
- if (!linear)
- {
- non_linear_to_linear();
- linear = true;
- }
- std::thread tab_t[_MT_];
- MT_exposure tab_a[_MT_];
- unsigned int id;
- unsigned int tab_length = width * height * 3;
- unsigned int block_size = tab_length / _MT_;
- for (id = 0; id < _MT_; id++) {
- tab_a[id].data = data + (id * block_size);
- tab_a[id].length = block_size;
- tab_a[id].coeff = coeff;
-
- if (id == (_MT_ - 1))
- tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
- tab_t[id] = std::thread(exposure_MT, (void*)(tab_a + id));
- }
- for (id = 0; id < _MT_; id++) {
- tab_t[id].join();
- }
- }
- #else
- void ImageHDR::exposure(const float ev)
- {
- float coef = powf(2, ev);
- if (!linear)
- {
- non_linear_to_linear();
- linear = true;
- }
- for (unsigned int i = 0; i < width * height * 3; i++)
- data[i] *= coef;
- }
- #endif
- /****************************************/
- /*************** CONTRAST ***************/
- /****************************************/
- #ifdef _MT_
- void* contrast_MT(void* arg)
- {
- MT_contrast* a = (MT_contrast*)arg;
- float* data = a->data;
- for (unsigned int i = 0; i < a->length; i++)
- data[i] = a->coeff * (data[i] - 0.5f) + 0.5f;
- return arg;
- }
- void ImageHDR::contrast(const float c)
- {
- float max_contrast_factor = 2.0f, scaling_factor = 1.0f, contrast_value = c;
- if (linear)
- {
- linear_to_non_linear();
- linear = false;
- }
- if (contrast_value != 0.0f)
- {
- contrast_value = contrast_value / 100.0f;
- if (contrast_value > 0.0f)
- {
- scaling_factor = 1 * (1 - contrast_value) + max_contrast_factor * contrast_value;
- }
- else
- {
- contrast_value = -contrast_value;
- scaling_factor = 1 * (1 - contrast_value) + max_contrast_factor * contrast_value;
- scaling_factor = 1 / scaling_factor;
- }
- }
- std::thread tab_t[_MT_];
- MT_contrast tab_a[_MT_];
- unsigned int id;
- unsigned int tab_length = width * height * 3;
- unsigned int block_size = tab_length / _MT_;
- for (id = 0; id < _MT_; id++)
- {
- tab_a[id].data = data + (id * block_size);
- tab_a[id].length = block_size;
- tab_a[id].coeff = scaling_factor;
- if (id == (_MT_ - 1))
- tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
- tab_t[id] = std::thread(contrast_MT, (void*)(tab_a + id));
- }
- for (id = 0; id < _MT_; id++) {
- tab_t[id].join();
- }
- }
- #else
- void ImageHDR::contrast(const float c)
- {
- float max_contrast_factor = 2.0f, scaling_factor = 1.0f, contrast_value = c;
- if (linear)
- {
- linear_to_non_linear();
- linear = false;
- }
- if (contrast_value != 0.0f)
- {
- contrast_value = contrast_value / 100.0f;
- if (contrast_value > 0.0f)
- {
- scaling_factor = 1 * (1 - contrast_value) + max_contrast_factor * contrast_value;
- }
- else
- {
- contrast_value = -contrast_value;
- scaling_factor = 1 * (1 - contrast_value) + max_contrast_factor * contrast_value;
- scaling_factor = 1 / scaling_factor;
- }
- }
- for (unsigned int i = 0; i < width * height * 3; i++)
- data[i] = scaling_factor * (data[i] - 0.5f) + 0.5f;
- }
- #endif
- /****************************************/
- /**************** YCURVE ****************/
- /****************************************/
- #ifdef _MT_
- void* histogram_regularization_MT(void* arg)
- {
- MT_histogram_regularization* a = (MT_histogram_regularization*)arg;
- float* data = a->data;
- float* colorDataY = a->colorDataY;
- float* colorDataFY = a->colorDataFY;
- for (unsigned int i = 0; i < a->length; i++)
- {
- data[i * 3] = data[i * 3] * colorDataFY[i] / colorDataY[i];
- data[i * 3 + 1] = data[i * 3 + 1] * colorDataFY[i] / colorDataY[i];
- data[i * 3 + 2] = data[i * 3 + 2] * colorDataFY[i] / colorDataY[i];
- }
- return arg;
- }
- void ImageHDR::ycurve_histogram_regularization(float* colorDataY, float* colorDataFY)
- {
- float yMin = colorDataY[0];
- unsigned int i = 1;
- while (yMin == 0)
- yMin = colorDataY[i++];
- for (unsigned int i = 0; i < width * height; i++)
- if (colorDataY[i] != 0 && colorDataY[i] < yMin)
- yMin = colorDataY[i];
- for (unsigned int i = 0; i < width * height; i++)
- if (colorDataY[i] == 0)
- colorDataY[i] = yMin;
- std::thread tab_t[_MT_];
- MT_histogram_regularization tab_a[_MT_];
- unsigned int id;
- unsigned int tab_length = width * height;
- unsigned int block_size = tab_length / _MT_;
- for (id = 0; id < _MT_; id++) {
- tab_a[id].data = data + (id * block_size * 3);
- tab_a[id].length = block_size;
- tab_a[id].colorDataY = colorDataY + (id * block_size);
- tab_a[id].colorDataFY = colorDataFY + (id * block_size);
- if (id == (_MT_ - 1))
- tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
- tab_t[id] = std::thread(histogram_regularization_MT, (void*)(tab_a + id));
- }
- for (id = 0; id < _MT_; id++) {
- tab_t[id].join();
- }
- }
- void ImageHDR::yCurve(float s, float b, float m, float w, float h)
- {
- if (linear)
- {
- linear_to_non_linear();
- linear = false;
- }
- float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
- float yMax = colorDataY[0];
- for (unsigned int i = 0; i < width * height; i++)
- if (yMax < colorDataY[i])
- yMax = colorDataY[i];
- yMax = yMax * 100;
- YCurve yc(s, b, m, w, h, yMax);
- Eigen::MatrixXf* points = yc.evalpts(100);
- Eigen::RowVectorXf y = (*points).col(0) / 100;
- Eigen::RowVectorXf fy = (*points).col(1) / 100;
- delete(points);
- // TODO - try to optimize ?!
- // The index of the search method in utils.cpp could be calculated or determined ?
- float* colorDataFY = Utils::interp(colorDataY, width * height, y, fy);
- ycurve_histogram_regularization(colorDataY, colorDataFY);
-
- delete[](colorDataY);
- delete[](colorDataFY);
- }
- #else
- void ImageHDR::ycurve_histogram_regularization(float* colorDataY, float* colorDataFY)
- {
- float yMin = colorDataY[0];
- unsigned int i = 1;
- while (yMin == 0)
- yMin = colorDataY[i++];
- for (unsigned int i = 0; i < width * height; i++)
- if (colorDataY[i] != 0 && colorDataY[i] < yMin)
- yMin = colorDataY[i];
- for (unsigned int i = 0; i < width * height; i++)
- if (colorDataY[i] == 0)
- colorDataY[i] = yMin;
- for (unsigned int i = 0; i < width * height; i++)
- {
- data[i * 3] = data[i * 3] * colorDataFY[i] / colorDataY[i];
- data[i * 3 + 1] = data[i * 3 + 1] * colorDataFY[i] / colorDataY[i];
- data[i * 3 + 2] = data[i * 3 + 2] * colorDataFY[i] / colorDataY[i];
- }
- }
- void ImageHDR::yCurve(float s, float b, float m, float w, float h)
- {
- if (linear)
- {
- linear_to_non_linear();
- linear = false;
- }
- float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
- float yMax = colorDataY[0];
- for (unsigned int i = 0; i < width * height; i++)
- if (yMax < colorDataY[i])
- yMax = colorDataY[i];
- yMax = yMax * 100;
- YCurve yc(s, b, m, w, h, yMax);
- Eigen::MatrixXf* points = yc.evalpts(100);
- Eigen::RowVectorXf y = (*points).col(0) / 100;
- Eigen::RowVectorXf fy = (*points).col(1) / 100;
- delete(points);
- float* colorDataFY = Utils::interp(colorDataY, width * height, y, fy);
- ycurve_histogram_regularization(colorDataY, colorDataFY);
- delete[](colorDataY);
- delete[](colorDataFY);
- }
- #endif
- /****************************************/
- /************** LIGHTNESSMASK ***********/
- /****************************************/
- #ifdef _MT_
- void* lightness_MT(void* arg)
- {
- MT_lightnessMask* a = (MT_lightnessMask*)arg;
- float* data = a->data;
- float* colorDataY = a->colorDataY;
- bool* mask = a->mask;
- float rangeMask[5][2] =
- {
- {0.0f, 0.2f},
- {0.2f, 0.4f},
- {0.4f, 0.6f},
- {0.6f, 0.8f},
- {0.8f, 1.0f}
- };
- unsigned int maskColor[5][3] =
- {
- {0, 0, 1},
- {0, 1, 1},
- {0, 1, 0},
- {1, 1, 0},
- {1, 0, 0}
- };
- for (unsigned int i = 0; i < a->length; i++)
- {
- for (unsigned int j = 0; j < 5; j++)
- if (mask[j])
- if (colorDataY[i] >= rangeMask[j][0] && colorDataY[i] <= rangeMask[j][1])
- {
- data[i * 3] = (float)(maskColor[j][0]);
- data[i * 3 + 1] = (float)(maskColor[j][1]);
- data[i * 3 + 2] = (float)(maskColor[j][2]);
- }
- }
- return arg;
- }
- void ImageHDR::lightnessMask(bool s, bool b, bool m, bool w, bool h)
- {
- bool mask[5] = { s, b, m, w, h };
- if (linear)
- {
- linear_to_non_linear();
- linear = false;
- }
- float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
- std::thread tab_t[_MT_];
- MT_lightnessMask tab_a[_MT_];
- unsigned int id;
- unsigned int tab_length = width * height;
- unsigned int block_size = tab_length / _MT_;
- for (id = 0; id < _MT_; id++) {
- tab_a[id].data = data + (id * block_size * 3);
- tab_a[id].length = block_size;
- tab_a[id].colorDataY = colorDataY + (id * block_size);
- tab_a[id].mask = mask;
- if (id == (_MT_ - 1))
- tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
- tab_t[id] = std::thread(lightness_MT, (void*)(tab_a + id));
- }
- for (id = 0; id < _MT_; id++) {
- tab_t[id].join();
- }
- delete[](colorDataY);
- }
- #else
- void ImageHDR::lightnessMask(bool s, bool b, bool m, bool w, bool h)
- {
- bool mask[5] = { s, b, m, w, h };
- float rangeMask[5][2] =
- {
- {0.0f, 0.2f},
- {0.2f, 0.4f},
- {0.4f, 0.6f},
- {0.6f, 0.8f},
- {0.8f, 1.0f}
- };
- unsigned int maskColor[5][3] =
- {
- {0, 0, 1},
- {0, 1, 1},
- {0, 1, 0},
- {1, 1, 0},
- {1, 0, 0}
- };
- if (linear)
- {
- linear_to_non_linear();
- linear = false;
- }
- float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
- for (unsigned int i = 0; i < width * height; i++)
- {
- for (unsigned int j = 0; j < 5; j++)
- if (mask[j])
- if (colorDataY[i] >= rangeMask[j][0] && colorDataY[i] <= rangeMask[j][1])
- {
- data[i * 3] = (float)(maskColor[j][0]);
- data[i * 3 + 1] = (float)(maskColor[j][1]);
- data[i * 3 + 2] = (float)(maskColor[j][2]);
- }
- }
- delete[](colorDataY);
- }
- #endif
- /****************************************/
- /************** SATURATION **************/
- /****************************************/
- #ifdef _MT_
- void* saturation_MT(void* arg)
- {
- MT_saturation* a = (MT_saturation*)arg;
- float* dataLab = a->dataLab;
- for (unsigned int i = 0; i < a->length; i++)
- {
- float a_of_Lab = dataLab[i * 3 + 1];
- float b_of_Lab = dataLab[i * 3 + 2];
- dataLab[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a_of_Lab, b_of_Lab);
- // Application de la saturation
- dataLab[i * 3 + 1] = powf(dataLab[i * 3 + 1] / 100.0f, a->gamma) * 100.0f;
- dataLab[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a_of_Lab, b_of_Lab);
- }
- return arg;
- }
- void ImageHDR::saturation(float s)
- {
- float gamma = 1.0f / ((s / 25.0f) + 1.0f);
- if (s < 0)
- gamma = (-s / 25.0f) + 1.0f;
- if (!linear)
- {
- non_linear_to_linear();
- linear = false;
- }
- float* dataLab = Conversion::sRGB_to_Lab(data, width * height);
- std::thread tab_t[_MT_];
- MT_saturation tab_a[_MT_];
- unsigned int id;
- unsigned int tab_length = width * height;
- unsigned int block_size = tab_length / _MT_;
- for (id = 0; id < _MT_; id++) {
- tab_a[id].dataLab = dataLab + (id * block_size * 3);
- tab_a[id].length = block_size;
- tab_a[id].gamma = gamma;
- if (id == (_MT_ - 1))
- tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
- tab_t[id] = std::thread(saturation_MT, (void*)(tab_a + id));
- }
- for (id = 0; id < _MT_; id++) {
- tab_t[id].join();
- }
- delete[](data);
- data = dataLab;
- linear = false;
- colorspace = Colorspace::LCH;
- }
- #else
- void ImageHDR::saturation(float s)
- {
- float gamma = 1.0f / ((s / 25.0f) + 1.0f);
- if (s < 0)
- gamma = (-s / 25.0f) + 1.0f;
- if (!linear)
- {
- non_linear_to_linear();
- linear = false;
- }
- float* dataLab = Conversion::sRGB_to_Lab(data, width * height);
- for (unsigned int i = 0; i < width * height; i++)
- {
- float a = dataLab[i * 3 + 1];
- float b = dataLab[i * 3 + 2];
- dataLab[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
- // Application de la saturation
- dataLab[i * 3 + 1] = powf(dataLab[i * 3 + 1] / 100.0f, gamma) * 100.0f;
- dataLab[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
- }
- delete[](data);
- data = dataLab;
- linear = false;
- colorspace = Colorspace::LCH;
- }
- #endif
- /*************************************/
- /************ COLOREDITOR ************/
- /*************************************/
- #ifdef _MT_
- void* colorEditor_MT(void* arg)
- {
- MT_colorEditor* a = (MT_colorEditor*)arg;
- float* data = a->data;
- unsigned int length = a->length;
- unsigned int colorspace = a->colorspace;
- bool linear = a->linear;
- float lMin = a->lMin, lMax = a->lMax;
- float cMin = a->cMin, cMax = a->cMax;
- float hMin = a->hMin, hMax = a->hMax;
- float tolerance = a->tolerance;
- float edit_hue = a->edit_hue;
- float edit_exposure = a->edit_exposure;
- float edit_contrast = a->edit_contrast;
- float edit_saturation = a->edit_saturation;
- float hueTolerance = tolerance * 360.0f;
- float chromaTolerance = tolerance * 100.0f;
- float lightTolerance = tolerance * 100.0f;
- bool mask = a->mask;
- float* dataLCH = NULL;
- float* minMask = NULL;
- float* compMask = NULL;
- // not the default parameter
- if (!(lMin == 0.0f && lMax == 100.0f
- && cMin == 0.0f && cMax == 100.0f
- && hMin == 0.0f && hMax == 360.0f
- && tolerance == 0.1f
- && edit_hue == 0.0f
- && edit_exposure == 0.0f
- && edit_contrast == 0.0f
- && edit_saturation == 0.0f
- && mask == false))
- {
- if (colorspace == Colorspace::RGB)
- {
- if (!linear)
- {
- data = Conversion::non_linear_to_linear(data, length * 3);
- linear = true;
- }
- dataLCH = Conversion::sRGB_to_Lab(data, length);
- for (unsigned int i = 0; i < length; i++)
- {
- float a = dataLCH[i * 3 + 1];
- float b = dataLCH[i * 3 + 2];
- dataLCH[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
- dataLCH[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
- }
- }
- else
- dataLCH = data;
- float* lChannel = new float[length];
- float* cChannel = new float[length];
- float* hChannel = new float[length];
- for (unsigned int i = 0; i < length; i++)
- {
- lChannel[i] = dataLCH[i * 3];
- cChannel[i] = dataLCH[i * 3 + 1];
- hChannel[i] = dataLCH[i * 3 + 2];
- }
- // Récupération du max du canal L et C
- float lMaxChannel = dataLCH[0];
- float cMaxChannel = dataLCH[1];
- for (unsigned int i = 1; i < length; i++)
- {
- if (dataLCH[i * 3] > lMaxChannel)
- lMaxChannel = dataLCH[i * 3];
- if (dataLCH[i * 3 + 1] > cMaxChannel)
- cMaxChannel = dataLCH[i * 3 + 1];
- }
- if (lMaxChannel < 100.0f)
- lMaxChannel = 100.0f;
- if (cMaxChannel < 100.0f)
- cMaxChannel = 100.0f;
- lMax = lMax * lMaxChannel / 100.0f;
- cMax = cMax * cMaxChannel / 100.0f;
- float* lightnessMask = Utils::NPlinearWeightMask(lChannel, length, lMin, lMax, lightTolerance);
- float* chromaMask = Utils::NPlinearWeightMask(cChannel, length, cMin, cMax, chromaTolerance);
- float* hueMask = Utils::NPlinearWeightMask(hChannel, length, hMin, hMax, hueTolerance);
- minMask = new float[length];
- compMask = new float[length];
- for (unsigned int i = 0; i < length; i++)
- {
- minMask[i] = lightnessMask[i];
- if (chromaMask[i] < minMask[i])
- minMask[i] = chromaMask[i];
- if (hueMask[i] < minMask[i])
- minMask[i] = hueMask[i];
- compMask[i] = 1.0f - minMask[i];
- }
- delete[](lightnessMask);
- delete[](chromaMask);
- delete[](hueMask);
- float hueShift = edit_hue;
- for (unsigned int i = 0; i < length; i++) {
- float oldValue = hChannel[i];
- hChannel[i] = oldValue + hueShift;
- while (hChannel[i] < 0.0f)
- hChannel[i] += 360.0f;
- while (hChannel[i] >= 360.0f)
- hChannel[i] -= 360.0f;
- hChannel[i] = hChannel[i] * minMask[i] + oldValue * compMask[i];
- }
- float saturation = edit_saturation;
- float gamma = 1.0f / ((saturation / 25.0f) + 1.0f);
- if (saturation < 0)
- gamma = (-saturation / 25.0f) + 1.0f;
- for (unsigned int i = 0; i < length; i++) {
- cChannel[i] = powf(cChannel[i] / 100.0f, gamma) * 100 * minMask[i] + cChannel[i] * compMask[i];
- }
- float* colorLCH = new float[length * 3];
- for (unsigned int i = 0; i < length; i++)
- {
- colorLCH[i * 3] = lChannel[i];
- colorLCH[i * 3 + 1] = cChannel[i];
- colorLCH[i * 3 + 2] = hChannel[i];
- }
- delete[](lChannel);
- delete[](cChannel);
- delete[](hChannel);
- float ev = edit_exposure;
- float* colorRGB = NULL;
- if (ev != 0)
- {
- colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
- float* colorRGBev = new float[length * 3];
- float coeff = powf(2, ev);
- for (unsigned int i = 0; i < length; i++)
- {
- colorRGBev[i * 3] = colorRGB[i * 3] * coeff * minMask[i];
- colorRGBev[i * 3 + 1] = colorRGB[i * 3 + 1] * coeff * minMask[i];
- colorRGBev[i * 3 + 2] = colorRGB[i * 3 + 2] * coeff * minMask[i];
- colorRGB[i * 3] = colorRGB[i * 3] * compMask[i] + colorRGBev[i * 3];
- colorRGB[i * 3 + 1] = colorRGB[i * 3 + 1] * compMask[i] + colorRGBev[i * 3 + 1];
- colorRGB[i * 3 + 2] = colorRGB[i * 3 + 2] * compMask[i] + colorRGBev[i * 3 + 2];
- }
- delete[](colorRGBev);
- }
- if (edit_contrast != 0)
- {
- float contrast = edit_contrast / 100.0f;
- float maxContrastFactor = 2.0f;
- float scalingFactor = (1.0f - contrast) + maxContrastFactor * contrast;
- if (contrast < 0.0f)
- {
- contrast = -contrast;
- scalingFactor = 1.0f / scalingFactor;
- }
- float pivot = powf(2, ev) * (lMin + lMax) / 2.0f / 100.0f;
- if (colorRGB == NULL)
- colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
-
- float* colorRGB2 = Conversion::linear_to_non_linear(colorRGB, length * 3);
- delete[](colorRGB);
- colorRGB=colorRGB2;
- float* colorRGBcon = new float[length * 3];
-
- for (unsigned int i = 0; i < length; i++)
- {
- colorRGBcon[i * 3] = (colorRGB[i * 3] - pivot) * scalingFactor + pivot;
- colorRGBcon[i * 3 + 1] = (colorRGB[i * 3 + 1] - pivot) * scalingFactor + pivot;
- colorRGBcon[i * 3 + 2] = (colorRGB[i * 3 + 2] - pivot) * scalingFactor + pivot;
- colorRGB[i * 3] = colorRGBcon[i * 3] * minMask[i] + colorRGB[i * 3] * compMask[i];
- colorRGB[i * 3 + 1] = colorRGBcon[i * 3 + 1] * minMask[i] + colorRGB[i * 3 + 1] * compMask[i];
- colorRGB[i * 3 + 2] = colorRGBcon[i * 3 + 2] * minMask[i] + colorRGB[i * 3 + 2] * compMask[i];
- }
- delete[](colorRGBcon);
- colorRGB2 = Conversion::non_linear_to_linear(colorRGB, length * 3);
- delete[](colorRGB);
- colorRGB=colorRGB2;
- }
- if (colorRGB == NULL)
- colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
- for (unsigned int i = 0; i < length * 3; i++)
- {
- data[i] = colorRGB[i];
- }
- delete[](colorRGB);
- delete[](colorLCH);
- colorspace = Colorspace::RGB;
- linear = true;
- }
- else
- {
- //TODO - To test for memory leak ?
- if (colorspace == Colorspace::LCH)
- {
- float* colorRGB = Conversion::LCH_to_sRGB(data, length);
- for (unsigned int i = 0; i < length * 3; i++)
- {
- data[i] = colorRGB[i];
- }
- delete[](colorRGB);
- colorspace = Colorspace::RGB;
- linear = true;
- }
- }
- if (mask)
- {
- for (unsigned int i = 0; i < length; i++)
- {
- data[i * 3] = minMask[i];
- data[i * 3 + 1] = minMask[i];
- data[i * 3 + 2] = minMask[i];
- }
- colorspace = Colorspace::RGB;
- linear = false;
- }
- delete[](minMask);
- delete[](compMask);
- return arg;
- }
- void ImageHDR::colorEditor(float* selection_lightness, float* selection_chroma, float* selection_hue, float tolerance, float edit_hue, float edit_exposure, float edit_contrast, float edit_saturation, bool mask)
- {
- float lMin = selection_lightness[0], lMax = selection_lightness[1];
- float cMin = selection_chroma[0], cMax = selection_chroma[1];
- float hMin = selection_hue[0], hMax = selection_hue[1];
- std::thread tab_t[_MT_];
- MT_colorEditor tab_a[_MT_];
- unsigned int id;
- unsigned int length = width * height;
- unsigned int block_size = length / _MT_;
- for (id = 0; id < _MT_; id++) {
- tab_a[id].data = data + (id * block_size * 3);
- tab_a[id].length = block_size;
- tab_a[id].colorspace = colorspace;
- tab_a[id].linear = linear;
- tab_a[id].lMin = lMin;
- tab_a[id].lMax = lMax;
- tab_a[id].cMin = cMin;
- tab_a[id].cMax = cMax;
- tab_a[id].hMin = hMin;
- tab_a[id].hMax = hMax;
- tab_a[id].tolerance = tolerance;
- tab_a[id].edit_hue = edit_hue;
- tab_a[id].edit_exposure = edit_exposure;
- tab_a[id].edit_contrast = edit_contrast;
- tab_a[id].edit_saturation = edit_saturation;
- tab_a[id].mask = mask;
- if (id == (_MT_ - 1))
- tab_a[id].length = length - ((_MT_ - 1) * block_size);
- tab_t[id] = std::thread(colorEditor_MT, (void*)(tab_a + id));
- }
- for (id = 0; id < _MT_; id++) {
- tab_t[id].join();
- }
- colorspace = Colorspace::RGB;
- linear = true;
- }
- #else
- void ImageHDR::colorEditor(float* selection_lightness, float* selection_chroma, float* selection_hue, float tolerance, float edit_hue, float edit_exposure, float edit_contrast, float edit_saturation, bool mask)
- {
- float lMin = selection_lightness[0], lMax = selection_lightness[1];
- float cMin = selection_chroma[0], cMax = selection_chroma[1];
- float hMin = selection_hue[0], hMax = selection_hue[1];
- float hueTolerance = tolerance * 360.0f;
- float chromaTolerance = tolerance * 100.0f;
- float lightTolerance = tolerance * 100.0f;
- float* dataLCH = NULL;
- float* minMask = NULL;
- float* compMask = NULL;
- // not the default parameter
- if (!(selection_lightness[0] == 0.0f && selection_lightness[1] == 100.0f
- && selection_chroma[0] == 0.0f && selection_chroma[1] == 100.0f
- && selection_hue[0] == 0.0f && selection_hue[1] == 360.0f
- && tolerance == 0.1f
- && edit_hue == 0.0f
- && edit_exposure == 0.0f
- && edit_contrast == 0.0f
- && edit_saturation == 0.0f
- && mask == false))
- {
- if (colorspace == Colorspace::RGB)
- {
- if (!linear)
- {
- non_linear_to_linear();
- linear = true;
- }
- dataLCH = Conversion::sRGB_to_Lab(data, width * height);
- for (unsigned int i = 0; i < width * height; i++)
- {
- float a = dataLCH[i * 3 + 1];
- float b = dataLCH[i * 3 + 2];
- dataLCH[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
- dataLCH[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
- }
- }
- else
- dataLCH = data;
- float* lChannel = new float[width * height];
- float* cChannel = new float[width * height];
- float* hChannel = new float[width * height];
- for (unsigned int i = 0; i < width * height; i++)
- {
- lChannel[i] = dataLCH[i * 3];
- cChannel[i] = dataLCH[i * 3 + 1];
- hChannel[i] = dataLCH[i * 3 + 2];
- }
- // Récupération du max du canal L et C
- float lMaxChannel = dataLCH[0];
- float cMaxChannel = dataLCH[1];
- for (unsigned int i = 1; i < width * height; i++)
- {
- if (dataLCH[i * 3] > lMaxChannel)
- lMaxChannel = dataLCH[i * 3];
- if (dataLCH[i * 3 + 1] > cMaxChannel)
- cMaxChannel = dataLCH[i * 3 + 1];
- }
- if (lMaxChannel < 100.0f)
- lMaxChannel = 100.0f;
- if (cMaxChannel < 100.0f)
- cMaxChannel = 100.0f;
- lMax = lMax * lMaxChannel / 100.0f;
- cMax = cMax * cMaxChannel / 100.0f;
- float* lightnessMask = Utils::NPlinearWeightMask(lChannel, width * height, lMin, lMax, lightTolerance);
- float* chromaMask = Utils::NPlinearWeightMask(cChannel, width * height, cMin, cMax, chromaTolerance);
- float* hueMask = Utils::NPlinearWeightMask(hChannel, width * height, hMin, hMax, hueTolerance);
- minMask = new float[width * height];
- compMask = new float[width * height];
- for (unsigned int i = 0; i < width * height; i++)
- {
- minMask[i] = lightnessMask[i];
- if (chromaMask[i] < minMask[i])
- minMask[i] = chromaMask[i];
- if (hueMask[i] < minMask[i])
- minMask[i] = hueMask[i];
- compMask[i] = 1.0f - minMask[i];
- }
- delete[](lightnessMask);
- delete[](chromaMask);
- delete[](hueMask);
- float hueShift = edit_hue;
- for (unsigned int i = 0; i < width * height; i++) {
- float oldValue = hChannel[i];
- hChannel[i] = oldValue + hueShift;
- while (hChannel[i] < 0.0f)
- hChannel[i] += 360.0f;
- while (hChannel[i] >= 360.0f)
- hChannel[i] -= 360.0f;
- hChannel[i] = hChannel[i] * minMask[i] + oldValue * compMask[i];
- }
- float saturation = edit_saturation;
- float gamma = 1.0f / ((saturation / 25.0f) + 1.0f);
- if (saturation < 0)
- gamma = (-saturation / 25.0f) + 1.0f;
- for (unsigned int i = 0; i < width * height; i++) {
- cChannel[i] = powf(cChannel[i] / 100.0f, gamma) * 100 * minMask[i] + cChannel[i] * compMask[i];
- }
- float* colorLCH = new float[width * height * 3];
- for (unsigned int i = 0; i < width * height; i++)
- {
- colorLCH[i * 3] = lChannel[i];
- colorLCH[i * 3 + 1] = cChannel[i];
- colorLCH[i * 3 + 2] = hChannel[i];
- }
- delete[](lChannel);
- delete[](cChannel);
- delete[](hChannel);
- float ev = edit_exposure;
- float* colorRGB = NULL;
- if (ev != 0)
- {
- colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
- float* colorRGBev = new float[width * height * 3];
- float coeff = powf(2, ev);
- for (unsigned int i = 0; i < width * height; i++)
- {
- colorRGBev[i * 3] = colorRGB[i * 3] * coeff * minMask[i];
- colorRGBev[i * 3 + 1] = colorRGB[i * 3 + 1] * coeff * minMask[i];
- colorRGBev[i * 3 + 2] = colorRGB[i * 3 + 2] * coeff * minMask[i];
- colorRGB[i * 3] = colorRGB[i * 3] * compMask[i] + colorRGBev[i * 3];
- colorRGB[i * 3 + 1] = colorRGB[i * 3 + 1] * compMask[i] + colorRGBev[i * 3 + 1];
- colorRGB[i * 3 + 2] = colorRGB[i * 3 + 2] * compMask[i] + colorRGBev[i * 3 + 2];
- }
- delete[](colorRGBev);
- }
- if (edit_contrast != 0)
- {
- float contrast = edit_contrast / 100.0f;
- float maxContrastFactor = 2.0f;
- float scalingFactor = (1.0f - contrast) + maxContrastFactor * contrast;
- if (contrast < 0.0f)
- {
- contrast = -contrast;
- scalingFactor = 1.0f / scalingFactor;
- }
- float pivot = powf(2, ev) * (lMin + lMax) / 2.0f / 100.0f;
- if (colorRGB == NULL)
- colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
-
- float* colorRGB2 = Conversion::linear_to_non_linear(colorRGB, width * height * 3);
- delete[](colorRGB);
- colorRGB=colorRGB2;
- float* colorRGBcon = new float[width * height * 3];
- for (unsigned int i = 0; i < width * height; i++)
- {
- colorRGBcon[i * 3] = (colorRGB[i * 3] - pivot) * scalingFactor + pivot;
- colorRGBcon[i * 3 + 1] = (colorRGB[i * 3 + 1] - pivot) * scalingFactor + pivot;
- colorRGBcon[i * 3 + 2] = (colorRGB[i * 3 + 2] - pivot) * scalingFactor + pivot;
- colorRGB[i * 3] = colorRGBcon[i * 3] * minMask[i] + colorRGB[i * 3] * compMask[i];
- colorRGB[i * 3 + 1] = colorRGBcon[i * 3 + 1] * minMask[i] + colorRGB[i * 3 + 1] * compMask[i];
- colorRGB[i * 3 + 2] = colorRGBcon[i * 3 + 2] * minMask[i] + colorRGB[i * 3 + 2] * compMask[i];
- }
- delete[](colorRGBcon);
- colorRGB2 = Conversion::non_linear_to_linear(colorRGB, width * height * 3);
- delete[](colorRGB);
- colorRGB=colorRGB2;
- }
- if (colorRGB == NULL)
- colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
-
- for (unsigned int i = 0; i < width*height * 3; i++)
- {
- data[i] = colorRGB[i];
- }
- delete[](colorRGB);
- delete[](colorLCH);
- colorspace = Colorspace::RGB;
- linear = true;
- }
- else
- {
- if (colorspace == Colorspace::LCH)
- {
- float* colorRGB = Conversion::LCH_to_sRGB(data, width * height);
-
- for (unsigned int i = 0; i < width*height * 3; i++)
- {
- data[i] = colorRGB[i];
- }
- delete[](colorRGB);
- colorspace = Colorspace::RGB;
- linear = true;
- }
- }
- if (mask)
- {
- for (unsigned int i = 0; i < width * height; i++)
- {
- data[i * 3] = minMask[i];
- data[i * 3 + 1] = minMask[i];
- data[i * 3 + 2] = minMask[i];
- }
- colorspace = Colorspace::RGB;
- linear = false;
- }
- delete[](minMask);
- delete[](compMask);
- }
- #endif
- /* Private methods */
- Eigen::VectorXf ImageHDR::to_EigenVector() const
- {
- Eigen::VectorXf v(width * height * 3);
- for (unsigned int i = 0; i < width; i++)
- v(i) = data[i];
- return v;
- }
|