ImageHDR.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. // This file is part of HDRip.
  2. //
  3. // HDRip is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // HDRip is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with HDRip. If not, see <https://www.gnu.org/licenses/>.
  15. //
  16. // HDRip project
  17. // Author : Rémi Synave
  18. // Contact : remi.synave@univ-littoral.fr
  19. #include "pch.h"
  20. #include "ImageHDR.hpp"
  21. #include "MT_exposure.hpp"
  22. #include "MT_contrast.hpp"
  23. #include "MT_histogram_regularization.hpp"
  24. #include "MT_lightnessMask.hpp"
  25. #include "MT_saturation.hpp"
  26. #include "MT_colorEditor.hpp"
  27. #include "Conversion.hpp"
  28. #include "YCurve.hpp"
  29. #include "Utils.hpp"
  30. #include <cmath>
  31. #include <thread>
  32. #include <iostream>
  33. #include <vector>
  34. #include <ctime>
  35. #include <cstdlib>
  36. /* Member methods*/
  37. ImageHDR::ImageHDR(float* d, unsigned int w, unsigned int h)
  38. {
  39. width = w;
  40. height = h;
  41. this->min_intensity = d[0];
  42. this->max_intensity = d[0];
  43. data = new float[width * height * 3];
  44. for (unsigned int i = 0; i < width * height * 3; i++) {
  45. data[i] = d[i];
  46. if (this->min_intensity > data[i])
  47. this->min_intensity = data[i];
  48. if (this->max_intensity < data[i])
  49. this->max_intensity = data[i];
  50. }
  51. linear = true;
  52. colorspace = Colorspace::RGB;
  53. }
  54. void ImageHDR::display_pixel(unsigned int i) const
  55. {
  56. if (linear)
  57. std::cout << "LINEAIRE - ";
  58. else
  59. std::cout << "NON LINEAIRE - ";
  60. std::cout << "Pixel : ( " << data[i * 3] << " " << data[i * 3 + 1] << " " << data[i * 3 + 2] << " )" << std::endl;
  61. }
  62. void ImageHDR::display_pixel(unsigned int i, unsigned int j) const
  63. {
  64. display_pixel(j * width + i);
  65. }
  66. /****************************************/
  67. /**************** LINEAR ****************/
  68. /****************************************/
  69. void ImageHDR::linear_to_non_linear()
  70. {
  71. float* non_linear = Conversion::linear_to_non_linear(data, width * height * 3);
  72. delete[](data);
  73. data = non_linear;
  74. }
  75. void ImageHDR::non_linear_to_linear()
  76. {
  77. float* linear = Conversion::non_linear_to_linear(data, width * height * 3);
  78. delete[](data);
  79. data = linear;
  80. }
  81. /****************************************/
  82. /*************** EXPOSURE ***************/
  83. /****************************************/
  84. #ifdef _MT_
  85. void* exposure_MT(void* arg)
  86. {
  87. MT_exposure* a = (MT_exposure*)arg;
  88. float* data = a->data;
  89. for (unsigned int i = 0; i < a->length; i++)
  90. data[i] *= a->coeff;
  91. return arg;
  92. }
  93. void ImageHDR::exposure(const float ev)
  94. {
  95. float coeff = powf(2, ev);
  96. if (!linear)
  97. {
  98. non_linear_to_linear();
  99. linear = true;
  100. }
  101. std::thread tab_t[_MT_];
  102. MT_exposure tab_a[_MT_];
  103. unsigned int id;
  104. unsigned int tab_length = width * height * 3;
  105. unsigned int block_size = tab_length / _MT_;
  106. for (id = 0; id < _MT_; id++) {
  107. tab_a[id].data = data + (id * block_size);
  108. tab_a[id].length = block_size;
  109. tab_a[id].coeff = coeff;
  110. if (id == (_MT_ - 1))
  111. tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
  112. tab_t[id] = std::thread(exposure_MT, (void*)(tab_a + id));
  113. }
  114. for (id = 0; id < _MT_; id++) {
  115. tab_t[id].join();
  116. }
  117. }
  118. #else
  119. void ImageHDR::exposure(const float ev)
  120. {
  121. float coef = powf(2, ev);
  122. if (!linear)
  123. {
  124. non_linear_to_linear();
  125. linear = true;
  126. }
  127. for (unsigned int i = 0; i < width * height * 3; i++)
  128. data[i] *= coef;
  129. }
  130. #endif
  131. /****************************************/
  132. /*************** CONTRAST ***************/
  133. /****************************************/
  134. #ifdef _MT_
  135. void* contrast_MT(void* arg)
  136. {
  137. MT_contrast* a = (MT_contrast*)arg;
  138. float* data = a->data;
  139. for (unsigned int i = 0; i < a->length; i++)
  140. data[i] = a->coeff * (data[i] - 0.5f) + 0.5f;
  141. return arg;
  142. }
  143. void ImageHDR::contrast(const float c)
  144. {
  145. float max_contrast_factor = 2.0f, scaling_factor = 1.0f, contrast_value = c;
  146. if (contrast_value != 0.0f)
  147. {
  148. if (linear)
  149. {
  150. linear_to_non_linear();
  151. linear = false;
  152. }
  153. contrast_value = contrast_value / 100.0f;
  154. if (contrast_value > 0.0f)
  155. {
  156. scaling_factor = 1 * (1 - contrast_value) + max_contrast_factor * contrast_value;
  157. }
  158. else
  159. {
  160. contrast_value = -contrast_value;
  161. scaling_factor = 1 * (1 - contrast_value) + max_contrast_factor * contrast_value;
  162. scaling_factor = 1 / scaling_factor;
  163. }
  164. }
  165. std::thread tab_t[_MT_];
  166. MT_contrast tab_a[_MT_];
  167. unsigned int id;
  168. unsigned int tab_length = width * height * 3;
  169. unsigned int block_size = tab_length / _MT_;
  170. for (id = 0; id < _MT_; id++)
  171. {
  172. tab_a[id].data = data + (id * block_size);
  173. tab_a[id].length = block_size;
  174. tab_a[id].coeff = scaling_factor;
  175. if (id == (_MT_ - 1))
  176. tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
  177. tab_t[id] = std::thread(contrast_MT, (void*)(tab_a + id));
  178. }
  179. for (id = 0; id < _MT_; id++) {
  180. tab_t[id].join();
  181. }
  182. }
  183. #else
  184. void ImageHDR::contrast(const float c)
  185. {
  186. float max_contrast_factor = 2.0f, scaling_factor = 1.0f, contrast_value = c;
  187. if (linear)
  188. {
  189. linear_to_non_linear();
  190. linear = false;
  191. }
  192. if (contrast_value != 0.0f)
  193. {
  194. contrast_value = contrast_value / 100.0f;
  195. if (contrast_value > 0.0f)
  196. {
  197. scaling_factor = 1 * (1 - contrast_value) + max_contrast_factor * contrast_value;
  198. }
  199. else
  200. {
  201. contrast_value = -contrast_value;
  202. scaling_factor = 1 * (1 - contrast_value) + max_contrast_factor * contrast_value;
  203. scaling_factor = 1 / scaling_factor;
  204. }
  205. }
  206. for (unsigned int i = 0; i < width * height * 3; i++)
  207. data[i] = scaling_factor * (data[i] - 0.5f) + 0.5f;
  208. }
  209. #endif
  210. /****************************************/
  211. /**************** YCURVE ****************/
  212. /****************************************/
  213. #ifdef _MT_
  214. void* histogram_regularization_MT(void* arg)
  215. {
  216. MT_histogram_regularization* a = (MT_histogram_regularization*)arg;
  217. float* data = a->data;
  218. float* colorDataY = a->colorDataY;
  219. float* colorDataFY = a->colorDataFY;
  220. for (unsigned int i = 0; i < a->length; i++)
  221. {
  222. data[i * 3] = data[i * 3] * colorDataFY[i] / colorDataY[i];
  223. data[i * 3 + 1] = data[i * 3 + 1] * colorDataFY[i] / colorDataY[i];
  224. data[i * 3 + 2] = data[i * 3 + 2] * colorDataFY[i] / colorDataY[i];
  225. }
  226. return arg;
  227. }
  228. void ImageHDR::ycurve_histogram_regularization(float* colorDataY, float* colorDataFY)
  229. {
  230. float yMin = colorDataY[0];
  231. unsigned int i = 1;
  232. while (yMin == 0)
  233. yMin = colorDataY[i++];
  234. for (unsigned int i = 0; i < width * height; i++)
  235. if (colorDataY[i] != 0 && colorDataY[i] < yMin)
  236. yMin = colorDataY[i];
  237. for (unsigned int i = 0; i < width * height; i++)
  238. if (colorDataY[i] == 0)
  239. colorDataY[i] = yMin;
  240. std::thread tab_t[_MT_];
  241. MT_histogram_regularization tab_a[_MT_];
  242. unsigned int id;
  243. unsigned int tab_length = width * height;
  244. unsigned int block_size = tab_length / _MT_;
  245. for (id = 0; id < _MT_; id++) {
  246. tab_a[id].data = data + (id * block_size * 3);
  247. tab_a[id].length = block_size;
  248. tab_a[id].colorDataY = colorDataY + (id * block_size);
  249. tab_a[id].colorDataFY = colorDataFY + (id * block_size);
  250. if (id == (_MT_ - 1))
  251. tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
  252. tab_t[id] = std::thread(histogram_regularization_MT, (void*)(tab_a + id));
  253. }
  254. for (id = 0; id < _MT_; id++) {
  255. tab_t[id].join();
  256. }
  257. }
  258. void ImageHDR::yCurve(float s, float b, float m, float w, float h)
  259. {
  260. if (linear)
  261. {
  262. linear_to_non_linear();
  263. linear = false;
  264. }
  265. float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
  266. YCurve yc(s, b, m, w, h, 200);
  267. Eigen::MatrixXf* points = yc.evalpts(100);
  268. Eigen::RowVectorXf y = (*points).col(0) / 100;
  269. Eigen::RowVectorXf fy = (*points).col(1) / 100;
  270. delete(points);
  271. // TODO - try to optimize ?!
  272. // The index of the search method in utils.cpp could be calculated or determined ?
  273. float* colorDataFY = Utils::interp(colorDataY, width * height, y, fy);
  274. ycurve_histogram_regularization(colorDataY, colorDataFY);
  275. delete[](colorDataY);
  276. delete[](colorDataFY);
  277. }
  278. #else
  279. void ImageHDR::ycurve_histogram_regularization(float* colorDataY, float* colorDataFY)
  280. {
  281. float yMin = colorDataY[0];
  282. unsigned int i = 1;
  283. while (yMin == 0)
  284. yMin = colorDataY[i++];
  285. for (unsigned int i = 0; i < width * height; i++)
  286. if (colorDataY[i] != 0 && colorDataY[i] < yMin)
  287. yMin = colorDataY[i];
  288. for (unsigned int i = 0; i < width * height; i++)
  289. if (colorDataY[i] == 0)
  290. colorDataY[i] = yMin;
  291. for (unsigned int i = 0; i < width * height; i++)
  292. {
  293. data[i * 3] = data[i * 3] * colorDataFY[i] / colorDataY[i];
  294. data[i * 3 + 1] = data[i * 3 + 1] * colorDataFY[i] / colorDataY[i];
  295. data[i * 3 + 2] = data[i * 3 + 2] * colorDataFY[i] / colorDataY[i];
  296. }
  297. }
  298. void ImageHDR::yCurve(float s, float b, float m, float w, float h)
  299. {
  300. if (linear)
  301. {
  302. linear_to_non_linear();
  303. linear = false;
  304. }
  305. float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
  306. YCurve yc(s, b, m, w, h, 200);
  307. Eigen::MatrixXf* points = yc.evalpts(100);
  308. Eigen::RowVectorXf y = (*points).col(0) / 100;
  309. Eigen::RowVectorXf fy = (*points).col(1) / 100;
  310. delete(points);
  311. float* colorDataFY = Utils::interp(colorDataY, width * height, y, fy);
  312. ycurve_histogram_regularization(colorDataY, colorDataFY);
  313. delete[](colorDataY);
  314. delete[](colorDataFY);
  315. }
  316. #endif
  317. /****************************************/
  318. /************** LIGHTNESSMASK ***********/
  319. /****************************************/
  320. #ifdef _MT_
  321. void* lightness_MT(void* arg)
  322. {
  323. MT_lightnessMask* a = (MT_lightnessMask*)arg;
  324. float* data = a->data;
  325. float* colorDataY = a->colorDataY;
  326. bool* mask = a->mask;
  327. float rangeMask[5][2] =
  328. {
  329. {0.0f, 0.2f},
  330. {0.2f, 0.4f},
  331. {0.4f, 0.6f},
  332. {0.6f, 0.8f},
  333. {0.8f, 1.0f}
  334. };
  335. unsigned int maskColor[5][3] =
  336. {
  337. {0, 0, 1},
  338. {0, 1, 1},
  339. {0, 1, 0},
  340. {1, 1, 0},
  341. {1, 0, 0}
  342. };
  343. for (unsigned int i = 0; i < a->length; i++)
  344. {
  345. for (unsigned int j = 0; j < 5; j++)
  346. if (mask[j])
  347. if (colorDataY[i] >= rangeMask[j][0] && colorDataY[i] <= rangeMask[j][1])
  348. {
  349. data[i * 3] = (float)(maskColor[j][0]);
  350. data[i * 3 + 1] = (float)(maskColor[j][1]);
  351. data[i * 3 + 2] = (float)(maskColor[j][2]);
  352. }
  353. }
  354. return arg;
  355. }
  356. void ImageHDR::lightnessMask(bool s, bool b, bool m, bool w, bool h)
  357. {
  358. bool mask[5] = { s, b, m, w, h };
  359. if (linear)
  360. {
  361. linear_to_non_linear();
  362. linear = false;
  363. }
  364. float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
  365. std::thread tab_t[_MT_];
  366. MT_lightnessMask tab_a[_MT_];
  367. unsigned int id;
  368. unsigned int tab_length = width * height;
  369. unsigned int block_size = tab_length / _MT_;
  370. for (id = 0; id < _MT_; id++) {
  371. tab_a[id].data = data + (id * block_size * 3);
  372. tab_a[id].length = block_size;
  373. tab_a[id].colorDataY = colorDataY + (id * block_size);
  374. tab_a[id].mask = mask;
  375. if (id == (_MT_ - 1))
  376. tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
  377. tab_t[id] = std::thread(lightness_MT, (void*)(tab_a + id));
  378. }
  379. for (id = 0; id < _MT_; id++) {
  380. tab_t[id].join();
  381. }
  382. delete[](colorDataY);
  383. }
  384. #else
  385. void ImageHDR::lightnessMask(bool s, bool b, bool m, bool w, bool h)
  386. {
  387. bool mask[5] = { s, b, m, w, h };
  388. float rangeMask[5][2] =
  389. {
  390. {0.0f, 0.2f},
  391. {0.2f, 0.4f},
  392. {0.4f, 0.6f},
  393. {0.6f, 0.8f},
  394. {0.8f, 1.0f}
  395. };
  396. unsigned int maskColor[5][3] =
  397. {
  398. {0, 0, 1},
  399. {0, 1, 1},
  400. {0, 1, 0},
  401. {1, 1, 0},
  402. {1, 0, 0}
  403. };
  404. if (linear)
  405. {
  406. linear_to_non_linear();
  407. linear = false;
  408. }
  409. float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
  410. for (unsigned int i = 0; i < width * height; i++)
  411. {
  412. for (unsigned int j = 0; j < 5; j++)
  413. if (mask[j])
  414. if (colorDataY[i] >= rangeMask[j][0] && colorDataY[i] <= rangeMask[j][1])
  415. {
  416. data[i * 3] = (float)(maskColor[j][0]);
  417. data[i * 3 + 1] = (float)(maskColor[j][1]);
  418. data[i * 3 + 2] = (float)(maskColor[j][2]);
  419. }
  420. }
  421. delete[](colorDataY);
  422. }
  423. #endif
  424. /****************************************/
  425. /************** SATURATION **************/
  426. /****************************************/
  427. #ifdef _MT_
  428. void* saturation_MT(void* arg)
  429. {
  430. MT_saturation* a = (MT_saturation*)arg;
  431. float* dataLab = a->dataLab;
  432. for (unsigned int i = 0; i < a->length; i++)
  433. {
  434. float a_of_Lab = dataLab[i * 3 + 1];
  435. float b_of_Lab = dataLab[i * 3 + 2];
  436. dataLab[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a_of_Lab, b_of_Lab);
  437. // Application de la saturation
  438. dataLab[i * 3 + 1] = powf(dataLab[i * 3 + 1] / 100.0f, a->gamma) * 100.0f;
  439. dataLab[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a_of_Lab, b_of_Lab);
  440. }
  441. return arg;
  442. }
  443. void ImageHDR::saturation(float s)
  444. {
  445. float gamma = 1.0f / ((s / 25.0f) + 1.0f);
  446. if (s < 0)
  447. gamma = (-s / 25.0f) + 1.0f;
  448. if (!linear)
  449. {
  450. non_linear_to_linear();
  451. linear = false;
  452. }
  453. float* dataLab = Conversion::sRGB_to_Lab(data, width * height);
  454. std::thread tab_t[_MT_];
  455. MT_saturation tab_a[_MT_];
  456. unsigned int id;
  457. unsigned int tab_length = width * height;
  458. unsigned int block_size = tab_length / _MT_;
  459. for (id = 0; id < _MT_; id++) {
  460. tab_a[id].dataLab = dataLab + (id * block_size * 3);
  461. tab_a[id].length = block_size;
  462. tab_a[id].gamma = gamma;
  463. if (id == (_MT_ - 1))
  464. tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
  465. tab_t[id] = std::thread(saturation_MT, (void*)(tab_a + id));
  466. }
  467. for (id = 0; id < _MT_; id++) {
  468. tab_t[id].join();
  469. }
  470. delete[](data);
  471. data = dataLab;
  472. linear = false;
  473. colorspace = Colorspace::LCH;
  474. }
  475. #else
  476. void ImageHDR::saturation(float s)
  477. {
  478. float gamma = 1.0f / ((s / 25.0f) + 1.0f);
  479. if (s < 0)
  480. gamma = (-s / 25.0f) + 1.0f;
  481. if (!linear)
  482. {
  483. non_linear_to_linear();
  484. linear = false;
  485. }
  486. float* dataLab = Conversion::sRGB_to_Lab(data, width * height);
  487. for (unsigned int i = 0; i < width * height; i++)
  488. {
  489. float a = dataLab[i * 3 + 1];
  490. float b = dataLab[i * 3 + 2];
  491. dataLab[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
  492. // Application de la saturation
  493. dataLab[i * 3 + 1] = powf(dataLab[i * 3 + 1] / 100.0f, gamma) * 100.0f;
  494. dataLab[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
  495. }
  496. delete[](data);
  497. data = dataLab;
  498. linear = false;
  499. colorspace = Colorspace::LCH;
  500. }
  501. #endif
  502. /*************************************/
  503. /************ COLOREDITOR ************/
  504. /*************************************/
  505. #ifdef _MT_
  506. void* colorEditor_MT(void* arg)
  507. {
  508. MT_colorEditor* a = (MT_colorEditor*)arg;
  509. float* data = a->data;
  510. unsigned int length = a->length;
  511. unsigned int colorspace = a->colorspace;
  512. bool linear = a->linear;
  513. float lMin = a->lMin, lMax = a->lMax;
  514. float cMin = a->cMin, cMax = a->cMax;
  515. float hMin = a->hMin, hMax = a->hMax;
  516. float tolerance = a->tolerance;
  517. float edit_hue = a->edit_hue;
  518. float edit_exposure = a->edit_exposure;
  519. float edit_contrast = a->edit_contrast;
  520. float edit_saturation = a->edit_saturation;
  521. float hueTolerance = tolerance * 360.0f;
  522. float chromaTolerance = tolerance * 100.0f;
  523. float lightTolerance = tolerance * 100.0f;
  524. bool mask = a->mask;
  525. float* dataLCH = NULL;
  526. float* minMask = NULL;
  527. float* compMask = NULL;
  528. // not the default parameter
  529. if (!(lMin == 0.0f && lMax == 100.0f
  530. && cMin == 0.0f && cMax == 100.0f
  531. && hMin == 0.0f && hMax == 360.0f
  532. && tolerance == 0.1f
  533. && edit_hue == 0.0f
  534. && edit_exposure == 0.0f
  535. && edit_contrast == 0.0f
  536. && edit_saturation == 0.0f
  537. && mask == false))
  538. {
  539. if (colorspace == Colorspace::RGB)
  540. {
  541. if (!linear)
  542. {
  543. data = Conversion::non_linear_to_linear(data, length * 3);
  544. linear = true;
  545. }
  546. dataLCH = Conversion::sRGB_to_Lab(data, length);
  547. for (unsigned int i = 0; i < length; i++)
  548. {
  549. float a = dataLCH[i * 3 + 1];
  550. float b = dataLCH[i * 3 + 2];
  551. dataLCH[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
  552. dataLCH[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
  553. }
  554. }
  555. else
  556. dataLCH = data;
  557. float* lChannel = new float[length];
  558. float* cChannel = new float[length];
  559. float* hChannel = new float[length];
  560. for (unsigned int i = 0; i < length; i++)
  561. {
  562. lChannel[i] = dataLCH[i * 3];
  563. cChannel[i] = dataLCH[i * 3 + 1];
  564. hChannel[i] = dataLCH[i * 3 + 2];
  565. }
  566. // Récupération du max du canal L et C
  567. float lMaxChannel = dataLCH[0];
  568. float cMaxChannel = dataLCH[1];
  569. for (unsigned int i = 1; i < length; i++)
  570. {
  571. if (dataLCH[i * 3] > lMaxChannel)
  572. lMaxChannel = dataLCH[i * 3];
  573. if (dataLCH[i * 3 + 1] > cMaxChannel)
  574. cMaxChannel = dataLCH[i * 3 + 1];
  575. }
  576. if (lMaxChannel < 100.0f)
  577. lMaxChannel = 100.0f;
  578. if (cMaxChannel < 100.0f)
  579. cMaxChannel = 100.0f;
  580. lMax = lMax * lMaxChannel / 100.0f;
  581. cMax = cMax * cMaxChannel / 100.0f;
  582. float* lightnessMask = Utils::NPlinearWeightMask(lChannel, length, lMin, lMax, lightTolerance);
  583. float* chromaMask = Utils::NPlinearWeightMask(cChannel, length, cMin, cMax, chromaTolerance);
  584. float* hueMask = Utils::NPlinearWeightMask(hChannel, length, hMin, hMax, hueTolerance);
  585. minMask = new float[length];
  586. compMask = new float[length];
  587. for (unsigned int i = 0; i < length; i++)
  588. {
  589. minMask[i] = lightnessMask[i];
  590. if (chromaMask[i] < minMask[i])
  591. minMask[i] = chromaMask[i];
  592. if (hueMask[i] < minMask[i])
  593. minMask[i] = hueMask[i];
  594. compMask[i] = 1.0f - minMask[i];
  595. }
  596. delete[](lightnessMask);
  597. delete[](chromaMask);
  598. delete[](hueMask);
  599. float hueShift = edit_hue;
  600. for (unsigned int i = 0; i < length; i++) {
  601. float oldValue = hChannel[i];
  602. hChannel[i] = oldValue + hueShift;
  603. while (hChannel[i] < 0.0f)
  604. hChannel[i] += 360.0f;
  605. while (hChannel[i] >= 360.0f)
  606. hChannel[i] -= 360.0f;
  607. hChannel[i] = hChannel[i] * minMask[i] + oldValue * compMask[i];
  608. }
  609. float saturation = edit_saturation;
  610. float gamma = 1.0f / ((saturation / 25.0f) + 1.0f);
  611. if (saturation < 0)
  612. gamma = (-saturation / 25.0f) + 1.0f;
  613. for (unsigned int i = 0; i < length; i++) {
  614. cChannel[i] = powf(cChannel[i] / 100.0f, gamma) * 100 * minMask[i] + cChannel[i] * compMask[i];
  615. }
  616. float* colorLCH = new float[length * 3];
  617. for (unsigned int i = 0; i < length; i++)
  618. {
  619. colorLCH[i * 3] = lChannel[i];
  620. colorLCH[i * 3 + 1] = cChannel[i];
  621. colorLCH[i * 3 + 2] = hChannel[i];
  622. }
  623. delete[](lChannel);
  624. delete[](cChannel);
  625. delete[](hChannel);
  626. float ev = edit_exposure;
  627. float* colorRGB = NULL;
  628. if (ev != 0)
  629. {
  630. colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
  631. float* colorRGBev = new float[length * 3];
  632. float coeff = powf(2, ev);
  633. for (unsigned int i = 0; i < length; i++)
  634. {
  635. colorRGBev[i * 3] = colorRGB[i * 3] * coeff * minMask[i];
  636. colorRGBev[i * 3 + 1] = colorRGB[i * 3 + 1] * coeff * minMask[i];
  637. colorRGBev[i * 3 + 2] = colorRGB[i * 3 + 2] * coeff * minMask[i];
  638. colorRGB[i * 3] = colorRGB[i * 3] * compMask[i] + colorRGBev[i * 3];
  639. colorRGB[i * 3 + 1] = colorRGB[i * 3 + 1] * compMask[i] + colorRGBev[i * 3 + 1];
  640. colorRGB[i * 3 + 2] = colorRGB[i * 3 + 2] * compMask[i] + colorRGBev[i * 3 + 2];
  641. }
  642. delete[](colorRGBev);
  643. }
  644. if (edit_contrast != 0)
  645. {
  646. float contrast = edit_contrast / 100.0f;
  647. float maxContrastFactor = 2.0f;
  648. float scalingFactor = (1.0f - contrast) + maxContrastFactor * contrast;
  649. if (contrast < 0.0f)
  650. {
  651. contrast = -contrast;
  652. scalingFactor = 1.0f / scalingFactor;
  653. }
  654. float pivot = powf(2, ev) * (lMin + lMax) / 2.0f / 100.0f;
  655. if (colorRGB == NULL)
  656. colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
  657. float* colorRGB2 = Conversion::linear_to_non_linear(colorRGB, length * 3);
  658. delete[](colorRGB);
  659. colorRGB = colorRGB2;
  660. float* colorRGBcon = new float[length * 3];
  661. for (unsigned int i = 0; i < length; i++)
  662. {
  663. colorRGBcon[i * 3] = (colorRGB[i * 3] - pivot) * scalingFactor + pivot;
  664. colorRGBcon[i * 3 + 1] = (colorRGB[i * 3 + 1] - pivot) * scalingFactor + pivot;
  665. colorRGBcon[i * 3 + 2] = (colorRGB[i * 3 + 2] - pivot) * scalingFactor + pivot;
  666. colorRGB[i * 3] = colorRGBcon[i * 3] * minMask[i] + colorRGB[i * 3] * compMask[i];
  667. colorRGB[i * 3 + 1] = colorRGBcon[i * 3 + 1] * minMask[i] + colorRGB[i * 3 + 1] * compMask[i];
  668. colorRGB[i * 3 + 2] = colorRGBcon[i * 3 + 2] * minMask[i] + colorRGB[i * 3 + 2] * compMask[i];
  669. }
  670. delete[](colorRGBcon);
  671. colorRGB2 = Conversion::non_linear_to_linear(colorRGB, length * 3);
  672. delete[](colorRGB);
  673. colorRGB = colorRGB2;
  674. }
  675. if (colorRGB == NULL)
  676. colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
  677. for (unsigned int i = 0; i < length * 3; i++)
  678. {
  679. data[i] = colorRGB[i];
  680. }
  681. delete[](colorRGB);
  682. delete[](colorLCH);
  683. colorspace = Colorspace::RGB;
  684. linear = true;
  685. }
  686. else
  687. {
  688. //TODO - To test for memory leak ?
  689. if (colorspace == Colorspace::LCH)
  690. {
  691. float* colorRGB = Conversion::LCH_to_sRGB(data, length);
  692. for (unsigned int i = 0; i < length * 3; i++)
  693. {
  694. data[i] = colorRGB[i];
  695. }
  696. delete[](colorRGB);
  697. colorspace = Colorspace::RGB;
  698. linear = true;
  699. }
  700. }
  701. if (mask)
  702. {
  703. for (unsigned int i = 0; i < length; i++)
  704. {
  705. data[i * 3] = minMask[i];
  706. data[i * 3 + 1] = minMask[i];
  707. data[i * 3 + 2] = minMask[i];
  708. }
  709. colorspace = Colorspace::RGB;
  710. linear = false;
  711. }
  712. delete[](minMask);
  713. delete[](compMask);
  714. return arg;
  715. }
  716. 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)
  717. {
  718. float lMin = selection_lightness[0], lMax = selection_lightness[1];
  719. float cMin = selection_chroma[0], cMax = selection_chroma[1];
  720. float hMin = selection_hue[0], hMax = selection_hue[1];
  721. std::thread tab_t[_MT_];
  722. MT_colorEditor tab_a[_MT_];
  723. unsigned int id;
  724. unsigned int length = width * height;
  725. unsigned int block_size = length / _MT_;
  726. for (id = 0; id < _MT_; id++) {
  727. tab_a[id].data = data + (id * block_size * 3);
  728. tab_a[id].length = block_size;
  729. tab_a[id].colorspace = colorspace;
  730. tab_a[id].linear = linear;
  731. tab_a[id].lMin = lMin;
  732. tab_a[id].lMax = lMax;
  733. tab_a[id].cMin = cMin;
  734. tab_a[id].cMax = cMax;
  735. tab_a[id].hMin = hMin;
  736. tab_a[id].hMax = hMax;
  737. tab_a[id].tolerance = tolerance;
  738. tab_a[id].edit_hue = edit_hue;
  739. tab_a[id].edit_exposure = edit_exposure;
  740. tab_a[id].edit_contrast = edit_contrast;
  741. tab_a[id].edit_saturation = edit_saturation;
  742. tab_a[id].mask = mask;
  743. if (id == (_MT_ - 1))
  744. tab_a[id].length = length - ((_MT_ - 1) * block_size);
  745. tab_t[id] = std::thread(colorEditor_MT, (void*)(tab_a + id));
  746. }
  747. for (id = 0; id < _MT_; id++) {
  748. tab_t[id].join();
  749. }
  750. colorspace = Colorspace::RGB;
  751. linear = true;
  752. }
  753. #else
  754. 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)
  755. {
  756. float lMin = selection_lightness[0], lMax = selection_lightness[1];
  757. float cMin = selection_chroma[0], cMax = selection_chroma[1];
  758. float hMin = selection_hue[0], hMax = selection_hue[1];
  759. float hueTolerance = tolerance * 360.0f;
  760. float chromaTolerance = tolerance * 100.0f;
  761. float lightTolerance = tolerance * 100.0f;
  762. float* dataLCH = NULL;
  763. float* minMask = NULL;
  764. float* compMask = NULL;
  765. // not the default parameter
  766. if (!(selection_lightness[0] == 0.0f && selection_lightness[1] == 100.0f
  767. && selection_chroma[0] == 0.0f && selection_chroma[1] == 100.0f
  768. && selection_hue[0] == 0.0f && selection_hue[1] == 360.0f
  769. && tolerance == 0.1f
  770. && edit_hue == 0.0f
  771. && edit_exposure == 0.0f
  772. && edit_contrast == 0.0f
  773. && edit_saturation == 0.0f
  774. && mask == false))
  775. {
  776. if (colorspace == Colorspace::RGB)
  777. {
  778. if (!linear)
  779. {
  780. non_linear_to_linear();
  781. linear = true;
  782. }
  783. dataLCH = Conversion::sRGB_to_Lab(data, width * height);
  784. for (unsigned int i = 0; i < width * height; i++)
  785. {
  786. float a = dataLCH[i * 3 + 1];
  787. float b = dataLCH[i * 3 + 2];
  788. dataLCH[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
  789. dataLCH[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
  790. }
  791. }
  792. else
  793. dataLCH = data;
  794. float* lChannel = new float[width * height];
  795. float* cChannel = new float[width * height];
  796. float* hChannel = new float[width * height];
  797. for (unsigned int i = 0; i < width * height; i++)
  798. {
  799. lChannel[i] = dataLCH[i * 3];
  800. cChannel[i] = dataLCH[i * 3 + 1];
  801. hChannel[i] = dataLCH[i * 3 + 2];
  802. }
  803. // Récupération du max du canal L et C
  804. float lMaxChannel = dataLCH[0];
  805. float cMaxChannel = dataLCH[1];
  806. for (unsigned int i = 1; i < width * height; i++)
  807. {
  808. if (dataLCH[i * 3] > lMaxChannel)
  809. lMaxChannel = dataLCH[i * 3];
  810. if (dataLCH[i * 3 + 1] > cMaxChannel)
  811. cMaxChannel = dataLCH[i * 3 + 1];
  812. }
  813. if (lMaxChannel < 100.0f)
  814. lMaxChannel = 100.0f;
  815. if (cMaxChannel < 100.0f)
  816. cMaxChannel = 100.0f;
  817. lMax = lMax * lMaxChannel / 100.0f;
  818. cMax = cMax * cMaxChannel / 100.0f;
  819. float* lightnessMask = Utils::NPlinearWeightMask(lChannel, width * height, lMin, lMax, lightTolerance);
  820. float* chromaMask = Utils::NPlinearWeightMask(cChannel, width * height, cMin, cMax, chromaTolerance);
  821. float* hueMask = Utils::NPlinearWeightMask(hChannel, width * height, hMin, hMax, hueTolerance);
  822. minMask = new float[width * height];
  823. compMask = new float[width * height];
  824. for (unsigned int i = 0; i < width * height; i++)
  825. {
  826. minMask[i] = lightnessMask[i];
  827. if (chromaMask[i] < minMask[i])
  828. minMask[i] = chromaMask[i];
  829. if (hueMask[i] < minMask[i])
  830. minMask[i] = hueMask[i];
  831. compMask[i] = 1.0f - minMask[i];
  832. }
  833. delete[](lightnessMask);
  834. delete[](chromaMask);
  835. delete[](hueMask);
  836. float hueShift = edit_hue;
  837. for (unsigned int i = 0; i < width * height; i++) {
  838. float oldValue = hChannel[i];
  839. hChannel[i] = oldValue + hueShift;
  840. while (hChannel[i] < 0.0f)
  841. hChannel[i] += 360.0f;
  842. while (hChannel[i] >= 360.0f)
  843. hChannel[i] -= 360.0f;
  844. hChannel[i] = hChannel[i] * minMask[i] + oldValue * compMask[i];
  845. }
  846. float saturation = edit_saturation;
  847. float gamma = 1.0f / ((saturation / 25.0f) + 1.0f);
  848. if (saturation < 0)
  849. gamma = (-saturation / 25.0f) + 1.0f;
  850. for (unsigned int i = 0; i < width * height; i++) {
  851. cChannel[i] = powf(cChannel[i] / 100.0f, gamma) * 100 * minMask[i] + cChannel[i] * compMask[i];
  852. }
  853. float* colorLCH = new float[width * height * 3];
  854. for (unsigned int i = 0; i < width * height; i++)
  855. {
  856. colorLCH[i * 3] = lChannel[i];
  857. colorLCH[i * 3 + 1] = cChannel[i];
  858. colorLCH[i * 3 + 2] = hChannel[i];
  859. }
  860. delete[](lChannel);
  861. delete[](cChannel);
  862. delete[](hChannel);
  863. float ev = edit_exposure;
  864. float* colorRGB = NULL;
  865. if (ev != 0)
  866. {
  867. colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
  868. float* colorRGBev = new float[width * height * 3];
  869. float coeff = powf(2, ev);
  870. for (unsigned int i = 0; i < width * height; i++)
  871. {
  872. colorRGBev[i * 3] = colorRGB[i * 3] * coeff * minMask[i];
  873. colorRGBev[i * 3 + 1] = colorRGB[i * 3 + 1] * coeff * minMask[i];
  874. colorRGBev[i * 3 + 2] = colorRGB[i * 3 + 2] * coeff * minMask[i];
  875. colorRGB[i * 3] = colorRGB[i * 3] * compMask[i] + colorRGBev[i * 3];
  876. colorRGB[i * 3 + 1] = colorRGB[i * 3 + 1] * compMask[i] + colorRGBev[i * 3 + 1];
  877. colorRGB[i * 3 + 2] = colorRGB[i * 3 + 2] * compMask[i] + colorRGBev[i * 3 + 2];
  878. }
  879. delete[](colorRGBev);
  880. }
  881. if (edit_contrast != 0)
  882. {
  883. float contrast = edit_contrast / 100.0f;
  884. float maxContrastFactor = 2.0f;
  885. float scalingFactor = (1.0f - contrast) + maxContrastFactor * contrast;
  886. if (contrast < 0.0f)
  887. {
  888. contrast = -contrast;
  889. scalingFactor = 1.0f / scalingFactor;
  890. }
  891. float pivot = powf(2, ev) * (lMin + lMax) / 2.0f / 100.0f;
  892. if (colorRGB == NULL)
  893. colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
  894. float* colorRGB2 = Conversion::linear_to_non_linear(colorRGB, width * height * 3);
  895. delete[](colorRGB);
  896. colorRGB = colorRGB2;
  897. float* colorRGBcon = new float[width * height * 3];
  898. for (unsigned int i = 0; i < width * height; i++)
  899. {
  900. colorRGBcon[i * 3] = (colorRGB[i * 3] - pivot) * scalingFactor + pivot;
  901. colorRGBcon[i * 3 + 1] = (colorRGB[i * 3 + 1] - pivot) * scalingFactor + pivot;
  902. colorRGBcon[i * 3 + 2] = (colorRGB[i * 3 + 2] - pivot) * scalingFactor + pivot;
  903. colorRGB[i * 3] = colorRGBcon[i * 3] * minMask[i] + colorRGB[i * 3] * compMask[i];
  904. colorRGB[i * 3 + 1] = colorRGBcon[i * 3 + 1] * minMask[i] + colorRGB[i * 3 + 1] * compMask[i];
  905. colorRGB[i * 3 + 2] = colorRGBcon[i * 3 + 2] * minMask[i] + colorRGB[i * 3 + 2] * compMask[i];
  906. }
  907. delete[](colorRGBcon);
  908. colorRGB2 = Conversion::non_linear_to_linear(colorRGB, width * height * 3);
  909. delete[](colorRGB);
  910. colorRGB = colorRGB2;
  911. }
  912. if (colorRGB == NULL)
  913. colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
  914. for (unsigned int i = 0; i < width * height * 3; i++)
  915. {
  916. data[i] = colorRGB[i];
  917. }
  918. delete[](colorRGB);
  919. delete[](colorLCH);
  920. colorspace = Colorspace::RGB;
  921. linear = true;
  922. }
  923. else
  924. {
  925. if (colorspace == Colorspace::LCH)
  926. {
  927. float* colorRGB = Conversion::LCH_to_sRGB(data, width * height);
  928. for (unsigned int i = 0; i < width * height * 3; i++)
  929. {
  930. data[i] = colorRGB[i];
  931. }
  932. delete[](colorRGB);
  933. colorspace = Colorspace::RGB;
  934. linear = true;
  935. }
  936. }
  937. if (mask)
  938. {
  939. for (unsigned int i = 0; i < width * height; i++)
  940. {
  941. data[i * 3] = minMask[i];
  942. data[i * 3 + 1] = minMask[i];
  943. data[i * 3 + 2] = minMask[i];
  944. }
  945. colorspace = Colorspace::RGB;
  946. linear = false;
  947. }
  948. delete[](minMask);
  949. delete[](compMask);
  950. }
  951. #endif
  952. /* Private methods */
  953. Eigen::VectorXf ImageHDR::to_EigenVector() const
  954. {
  955. Eigen::VectorXf v(width * height * 3);
  956. for (unsigned int i = 0; i < width; i++)
  957. v(i) = data[i];
  958. return v;
  959. }