ImageHDR.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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 (linear)
  147. {
  148. linear_to_non_linear();
  149. linear = false;
  150. }
  151. if (contrast_value != 0.0f)
  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. float yMax = colorDataY[0];
  267. for (unsigned int i = 0; i < width * height; i++)
  268. if (yMax < colorDataY[i])
  269. yMax = colorDataY[i];
  270. yMax = yMax * 100;
  271. YCurve yc(s, b, m, w, h, yMax);
  272. Eigen::MatrixXf* points = yc.evalpts(100);
  273. Eigen::RowVectorXf y = (*points).col(0) / 100;
  274. Eigen::RowVectorXf fy = (*points).col(1) / 100;
  275. delete(points);
  276. // TODO - try to optimize ?!
  277. // The index of the search method in utils.cpp could be calculated or determined ?
  278. float* colorDataFY = Utils::interp(colorDataY, width * height, y, fy);
  279. ycurve_histogram_regularization(colorDataY, colorDataFY);
  280. delete[](colorDataY);
  281. delete[](colorDataFY);
  282. }
  283. #else
  284. void ImageHDR::ycurve_histogram_regularization(float* colorDataY, float* colorDataFY)
  285. {
  286. float yMin = colorDataY[0];
  287. unsigned int i = 1;
  288. while (yMin == 0)
  289. yMin = colorDataY[i++];
  290. for (unsigned int i = 0; i < width * height; i++)
  291. if (colorDataY[i] != 0 && colorDataY[i] < yMin)
  292. yMin = colorDataY[i];
  293. for (unsigned int i = 0; i < width * height; i++)
  294. if (colorDataY[i] == 0)
  295. colorDataY[i] = yMin;
  296. for (unsigned int i = 0; i < width * height; i++)
  297. {
  298. data[i * 3] = data[i * 3] * colorDataFY[i] / colorDataY[i];
  299. data[i * 3 + 1] = data[i * 3 + 1] * colorDataFY[i] / colorDataY[i];
  300. data[i * 3 + 2] = data[i * 3 + 2] * colorDataFY[i] / colorDataY[i];
  301. }
  302. }
  303. void ImageHDR::yCurve(float s, float b, float m, float w, float h)
  304. {
  305. if (linear)
  306. {
  307. linear_to_non_linear();
  308. linear = false;
  309. }
  310. float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
  311. float yMax = colorDataY[0];
  312. for (unsigned int i = 0; i < width * height; i++)
  313. if (yMax < colorDataY[i])
  314. yMax = colorDataY[i];
  315. yMax = yMax * 100;
  316. YCurve yc(s, b, m, w, h, yMax);
  317. Eigen::MatrixXf* points = yc.evalpts(100);
  318. Eigen::RowVectorXf y = (*points).col(0) / 100;
  319. Eigen::RowVectorXf fy = (*points).col(1) / 100;
  320. delete(points);
  321. float* colorDataFY = Utils::interp(colorDataY, width * height, y, fy);
  322. ycurve_histogram_regularization(colorDataY, colorDataFY);
  323. delete[](colorDataY);
  324. delete[](colorDataFY);
  325. }
  326. #endif
  327. /****************************************/
  328. /************** LIGHTNESSMASK ***********/
  329. /****************************************/
  330. #ifdef _MT_
  331. void* lightness_MT(void* arg)
  332. {
  333. MT_lightnessMask* a = (MT_lightnessMask*)arg;
  334. float* data = a->data;
  335. float* colorDataY = a->colorDataY;
  336. bool* mask = a->mask;
  337. float rangeMask[5][2] =
  338. {
  339. {0.0f, 0.2f},
  340. {0.2f, 0.4f},
  341. {0.4f, 0.6f},
  342. {0.6f, 0.8f},
  343. {0.8f, 1.0f}
  344. };
  345. unsigned int maskColor[5][3] =
  346. {
  347. {0, 0, 1},
  348. {0, 1, 1},
  349. {0, 1, 0},
  350. {1, 1, 0},
  351. {1, 0, 0}
  352. };
  353. for (unsigned int i = 0; i < a->length; i++)
  354. {
  355. for (unsigned int j = 0; j < 5; j++)
  356. if (mask[j])
  357. if (colorDataY[i] >= rangeMask[j][0] && colorDataY[i] <= rangeMask[j][1])
  358. {
  359. data[i * 3] = (float)(maskColor[j][0]);
  360. data[i * 3 + 1] = (float)(maskColor[j][1]);
  361. data[i * 3 + 2] = (float)(maskColor[j][2]);
  362. }
  363. }
  364. return arg;
  365. }
  366. void ImageHDR::lightnessMask(bool s, bool b, bool m, bool w, bool h)
  367. {
  368. bool mask[5] = { s, b, m, w, h };
  369. if (linear)
  370. {
  371. linear_to_non_linear();
  372. linear = false;
  373. }
  374. float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
  375. std::thread tab_t[_MT_];
  376. MT_lightnessMask tab_a[_MT_];
  377. unsigned int id;
  378. unsigned int tab_length = width * height;
  379. unsigned int block_size = tab_length / _MT_;
  380. for (id = 0; id < _MT_; id++) {
  381. tab_a[id].data = data + (id * block_size * 3);
  382. tab_a[id].length = block_size;
  383. tab_a[id].colorDataY = colorDataY + (id * block_size);
  384. tab_a[id].mask = mask;
  385. if (id == (_MT_ - 1))
  386. tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
  387. tab_t[id] = std::thread(lightness_MT, (void*)(tab_a + id));
  388. }
  389. for (id = 0; id < _MT_; id++) {
  390. tab_t[id].join();
  391. }
  392. delete[](colorDataY);
  393. }
  394. #else
  395. void ImageHDR::lightnessMask(bool s, bool b, bool m, bool w, bool h)
  396. {
  397. bool mask[5] = { s, b, m, w, h };
  398. float rangeMask[5][2] =
  399. {
  400. {0.0f, 0.2f},
  401. {0.2f, 0.4f},
  402. {0.4f, 0.6f},
  403. {0.6f, 0.8f},
  404. {0.8f, 1.0f}
  405. };
  406. unsigned int maskColor[5][3] =
  407. {
  408. {0, 0, 1},
  409. {0, 1, 1},
  410. {0, 1, 0},
  411. {1, 1, 0},
  412. {1, 0, 0}
  413. };
  414. if (linear)
  415. {
  416. linear_to_non_linear();
  417. linear = false;
  418. }
  419. float* colorDataY = Conversion::sRGB_to_Y_of_XYZ(data, width * height);
  420. for (unsigned int i = 0; i < width * height; i++)
  421. {
  422. for (unsigned int j = 0; j < 5; j++)
  423. if (mask[j])
  424. if (colorDataY[i] >= rangeMask[j][0] && colorDataY[i] <= rangeMask[j][1])
  425. {
  426. data[i * 3] = (float)(maskColor[j][0]);
  427. data[i * 3 + 1] = (float)(maskColor[j][1]);
  428. data[i * 3 + 2] = (float)(maskColor[j][2]);
  429. }
  430. }
  431. delete[](colorDataY);
  432. }
  433. #endif
  434. /****************************************/
  435. /************** SATURATION **************/
  436. /****************************************/
  437. #ifdef _MT_
  438. void* saturation_MT(void* arg)
  439. {
  440. MT_saturation* a = (MT_saturation*)arg;
  441. float* dataLab = a->dataLab;
  442. for (unsigned int i = 0; i < a->length; i++)
  443. {
  444. float a_of_Lab = dataLab[i * 3 + 1];
  445. float b_of_Lab = dataLab[i * 3 + 2];
  446. dataLab[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a_of_Lab, b_of_Lab);
  447. // Application de la saturation
  448. dataLab[i * 3 + 1] = powf(dataLab[i * 3 + 1] / 100.0f, a->gamma) * 100.0f;
  449. dataLab[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a_of_Lab, b_of_Lab);
  450. }
  451. return arg;
  452. }
  453. void ImageHDR::saturation(float s)
  454. {
  455. float gamma = 1.0f / ((s / 25.0f) + 1.0f);
  456. if (s < 0)
  457. gamma = (-s / 25.0f) + 1.0f;
  458. if (!linear)
  459. {
  460. non_linear_to_linear();
  461. linear = false;
  462. }
  463. float* dataLab = Conversion::sRGB_to_Lab(data, width * height);
  464. std::thread tab_t[_MT_];
  465. MT_saturation tab_a[_MT_];
  466. unsigned int id;
  467. unsigned int tab_length = width * height;
  468. unsigned int block_size = tab_length / _MT_;
  469. for (id = 0; id < _MT_; id++) {
  470. tab_a[id].dataLab = dataLab + (id * block_size * 3);
  471. tab_a[id].length = block_size;
  472. tab_a[id].gamma = gamma;
  473. if (id == (_MT_ - 1))
  474. tab_a[id].length = tab_length - ((_MT_ - 1) * block_size);
  475. tab_t[id] = std::thread(saturation_MT, (void*)(tab_a + id));
  476. }
  477. for (id = 0; id < _MT_; id++) {
  478. tab_t[id].join();
  479. }
  480. delete[](data);
  481. data = dataLab;
  482. linear = false;
  483. colorspace = Colorspace::LCH;
  484. }
  485. #else
  486. void ImageHDR::saturation(float s)
  487. {
  488. float gamma = 1.0f / ((s / 25.0f) + 1.0f);
  489. if (s < 0)
  490. gamma = (-s / 25.0f) + 1.0f;
  491. if (!linear)
  492. {
  493. non_linear_to_linear();
  494. linear = false;
  495. }
  496. float* dataLab = Conversion::sRGB_to_Lab(data, width * height);
  497. for (unsigned int i = 0; i < width * height; i++)
  498. {
  499. float a = dataLab[i * 3 + 1];
  500. float b = dataLab[i * 3 + 2];
  501. dataLab[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
  502. // Application de la saturation
  503. dataLab[i * 3 + 1] = powf(dataLab[i * 3 + 1] / 100.0f, gamma) * 100.0f;
  504. dataLab[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
  505. }
  506. delete[](data);
  507. data = dataLab;
  508. linear = false;
  509. colorspace = Colorspace::LCH;
  510. }
  511. #endif
  512. /*************************************/
  513. /************ COLOREDITOR ************/
  514. /*************************************/
  515. #ifdef _MT_
  516. void* colorEditor_MT(void* arg)
  517. {
  518. MT_colorEditor* a = (MT_colorEditor*)arg;
  519. float* data = a->data;
  520. unsigned int length = a->length;
  521. unsigned int colorspace = a->colorspace;
  522. bool linear = a->linear;
  523. float lMin = a->lMin, lMax = a->lMax;
  524. float cMin = a->cMin, cMax = a->cMax;
  525. float hMin = a->hMin, hMax = a->hMax;
  526. float tolerance = a->tolerance;
  527. float edit_hue = a->edit_hue;
  528. float edit_exposure = a->edit_exposure;
  529. float edit_contrast = a->edit_contrast;
  530. float edit_saturation = a->edit_saturation;
  531. float hueTolerance = tolerance * 360.0f;
  532. float chromaTolerance = tolerance * 100.0f;
  533. float lightTolerance = tolerance * 100.0f;
  534. bool mask = a->mask;
  535. float* dataLCH = NULL;
  536. float* minMask = NULL;
  537. float* compMask = NULL;
  538. // not the default parameter
  539. if (!(lMin == 0.0f && lMax == 100.0f
  540. && cMin == 0.0f && cMax == 100.0f
  541. && hMin == 0.0f && hMax == 360.0f
  542. && tolerance == 0.1f
  543. && edit_hue == 0.0f
  544. && edit_exposure == 0.0f
  545. && edit_contrast == 0.0f
  546. && edit_saturation == 0.0f
  547. && mask == false))
  548. {
  549. if (colorspace == Colorspace::RGB)
  550. {
  551. if (!linear)
  552. {
  553. data = Conversion::non_linear_to_linear(data, length * 3);
  554. linear = true;
  555. }
  556. dataLCH = Conversion::sRGB_to_Lab(data, length);
  557. for (unsigned int i = 0; i < length; i++)
  558. {
  559. float a = dataLCH[i * 3 + 1];
  560. float b = dataLCH[i * 3 + 2];
  561. dataLCH[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
  562. dataLCH[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
  563. }
  564. }
  565. else
  566. dataLCH = data;
  567. float* lChannel = new float[length];
  568. float* cChannel = new float[length];
  569. float* hChannel = new float[length];
  570. for (unsigned int i = 0; i < length; i++)
  571. {
  572. lChannel[i] = dataLCH[i * 3];
  573. cChannel[i] = dataLCH[i * 3 + 1];
  574. hChannel[i] = dataLCH[i * 3 + 2];
  575. }
  576. // Récupération du max du canal L et C
  577. float lMaxChannel = dataLCH[0];
  578. float cMaxChannel = dataLCH[1];
  579. for (unsigned int i = 1; i < length; i++)
  580. {
  581. if (dataLCH[i * 3] > lMaxChannel)
  582. lMaxChannel = dataLCH[i * 3];
  583. if (dataLCH[i * 3 + 1] > cMaxChannel)
  584. cMaxChannel = dataLCH[i * 3 + 1];
  585. }
  586. if (lMaxChannel < 100.0f)
  587. lMaxChannel = 100.0f;
  588. if (cMaxChannel < 100.0f)
  589. cMaxChannel = 100.0f;
  590. lMax = lMax * lMaxChannel / 100.0f;
  591. cMax = cMax * cMaxChannel / 100.0f;
  592. float* lightnessMask = Utils::NPlinearWeightMask(lChannel, length, lMin, lMax, lightTolerance);
  593. float* chromaMask = Utils::NPlinearWeightMask(cChannel, length, cMin, cMax, chromaTolerance);
  594. float* hueMask = Utils::NPlinearWeightMask(hChannel, length, hMin, hMax, hueTolerance);
  595. minMask = new float[length];
  596. compMask = new float[length];
  597. for (unsigned int i = 0; i < length; i++)
  598. {
  599. minMask[i] = lightnessMask[i];
  600. if (chromaMask[i] < minMask[i])
  601. minMask[i] = chromaMask[i];
  602. if (hueMask[i] < minMask[i])
  603. minMask[i] = hueMask[i];
  604. compMask[i] = 1.0f - minMask[i];
  605. }
  606. delete[](lightnessMask);
  607. delete[](chromaMask);
  608. delete[](hueMask);
  609. float hueShift = edit_hue;
  610. for (unsigned int i = 0; i < length; i++) {
  611. float oldValue = hChannel[i];
  612. hChannel[i] = oldValue + hueShift;
  613. while (hChannel[i] < 0.0f)
  614. hChannel[i] += 360.0f;
  615. while (hChannel[i] >= 360.0f)
  616. hChannel[i] -= 360.0f;
  617. hChannel[i] = hChannel[i] * minMask[i] + oldValue * compMask[i];
  618. }
  619. float saturation = edit_saturation;
  620. float gamma = 1.0f / ((saturation / 25.0f) + 1.0f);
  621. if (saturation < 0)
  622. gamma = (-saturation / 25.0f) + 1.0f;
  623. for (unsigned int i = 0; i < length; i++) {
  624. cChannel[i] = powf(cChannel[i] / 100.0f, gamma) * 100 * minMask[i] + cChannel[i] * compMask[i];
  625. }
  626. float* colorLCH = new float[length * 3];
  627. for (unsigned int i = 0; i < length; i++)
  628. {
  629. colorLCH[i * 3] = lChannel[i];
  630. colorLCH[i * 3 + 1] = cChannel[i];
  631. colorLCH[i * 3 + 2] = hChannel[i];
  632. }
  633. delete[](lChannel);
  634. delete[](cChannel);
  635. delete[](hChannel);
  636. float ev = edit_exposure;
  637. float* colorRGB = NULL;
  638. if (ev != 0)
  639. {
  640. colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
  641. float* colorRGBev = new float[length * 3];
  642. float coeff = powf(2, ev);
  643. for (unsigned int i = 0; i < length; i++)
  644. {
  645. colorRGBev[i * 3] = colorRGB[i * 3] * coeff * minMask[i];
  646. colorRGBev[i * 3 + 1] = colorRGB[i * 3 + 1] * coeff * minMask[i];
  647. colorRGBev[i * 3 + 2] = colorRGB[i * 3 + 2] * coeff * minMask[i];
  648. colorRGB[i * 3] = colorRGB[i * 3] * compMask[i] + colorRGBev[i * 3];
  649. colorRGB[i * 3 + 1] = colorRGB[i * 3 + 1] * compMask[i] + colorRGBev[i * 3 + 1];
  650. colorRGB[i * 3 + 2] = colorRGB[i * 3 + 2] * compMask[i] + colorRGBev[i * 3 + 2];
  651. }
  652. delete[](colorRGBev);
  653. }
  654. if (edit_contrast != 0)
  655. {
  656. float contrast = edit_contrast / 100.0f;
  657. float maxContrastFactor = 2.0f;
  658. float scalingFactor = (1.0f - contrast) + maxContrastFactor * contrast;
  659. if (contrast < 0.0f)
  660. {
  661. contrast = -contrast;
  662. scalingFactor = 1.0f / scalingFactor;
  663. }
  664. float pivot = powf(2, ev) * (lMin + lMax) / 2.0f / 100.0f;
  665. if (colorRGB == NULL)
  666. colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
  667. float* colorRGB2 = Conversion::linear_to_non_linear(colorRGB, length * 3);
  668. delete[](colorRGB);
  669. colorRGB=colorRGB2;
  670. float* colorRGBcon = new float[length * 3];
  671. for (unsigned int i = 0; i < length; i++)
  672. {
  673. colorRGBcon[i * 3] = (colorRGB[i * 3] - pivot) * scalingFactor + pivot;
  674. colorRGBcon[i * 3 + 1] = (colorRGB[i * 3 + 1] - pivot) * scalingFactor + pivot;
  675. colorRGBcon[i * 3 + 2] = (colorRGB[i * 3 + 2] - pivot) * scalingFactor + pivot;
  676. colorRGB[i * 3] = colorRGBcon[i * 3] * minMask[i] + colorRGB[i * 3] * compMask[i];
  677. colorRGB[i * 3 + 1] = colorRGBcon[i * 3 + 1] * minMask[i] + colorRGB[i * 3 + 1] * compMask[i];
  678. colorRGB[i * 3 + 2] = colorRGBcon[i * 3 + 2] * minMask[i] + colorRGB[i * 3 + 2] * compMask[i];
  679. }
  680. delete[](colorRGBcon);
  681. colorRGB2 = Conversion::non_linear_to_linear(colorRGB, length * 3);
  682. delete[](colorRGB);
  683. colorRGB=colorRGB2;
  684. }
  685. if (colorRGB == NULL)
  686. colorRGB = Conversion::LCH_to_sRGB(colorLCH, length);
  687. for (unsigned int i = 0; i < length * 3; i++)
  688. {
  689. data[i] = colorRGB[i];
  690. }
  691. delete[](colorRGB);
  692. delete[](colorLCH);
  693. colorspace = Colorspace::RGB;
  694. linear = true;
  695. }
  696. else
  697. {
  698. //TODO - To test for memory leak ?
  699. if (colorspace == Colorspace::LCH)
  700. {
  701. float* colorRGB = Conversion::LCH_to_sRGB(data, length);
  702. for (unsigned int i = 0; i < length * 3; i++)
  703. {
  704. data[i] = colorRGB[i];
  705. }
  706. delete[](colorRGB);
  707. colorspace = Colorspace::RGB;
  708. linear = true;
  709. }
  710. }
  711. if (mask)
  712. {
  713. for (unsigned int i = 0; i < length; i++)
  714. {
  715. data[i * 3] = minMask[i];
  716. data[i * 3 + 1] = minMask[i];
  717. data[i * 3 + 2] = minMask[i];
  718. }
  719. colorspace = Colorspace::RGB;
  720. linear = false;
  721. }
  722. delete[](minMask);
  723. delete[](compMask);
  724. return arg;
  725. }
  726. 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)
  727. {
  728. float lMin = selection_lightness[0], lMax = selection_lightness[1];
  729. float cMin = selection_chroma[0], cMax = selection_chroma[1];
  730. float hMin = selection_hue[0], hMax = selection_hue[1];
  731. std::thread tab_t[_MT_];
  732. MT_colorEditor tab_a[_MT_];
  733. unsigned int id;
  734. unsigned int length = width * height;
  735. unsigned int block_size = length / _MT_;
  736. for (id = 0; id < _MT_; id++) {
  737. tab_a[id].data = data + (id * block_size * 3);
  738. tab_a[id].length = block_size;
  739. tab_a[id].colorspace = colorspace;
  740. tab_a[id].linear = linear;
  741. tab_a[id].lMin = lMin;
  742. tab_a[id].lMax = lMax;
  743. tab_a[id].cMin = cMin;
  744. tab_a[id].cMax = cMax;
  745. tab_a[id].hMin = hMin;
  746. tab_a[id].hMax = hMax;
  747. tab_a[id].tolerance = tolerance;
  748. tab_a[id].edit_hue = edit_hue;
  749. tab_a[id].edit_exposure = edit_exposure;
  750. tab_a[id].edit_contrast = edit_contrast;
  751. tab_a[id].edit_saturation = edit_saturation;
  752. tab_a[id].mask = mask;
  753. if (id == (_MT_ - 1))
  754. tab_a[id].length = length - ((_MT_ - 1) * block_size);
  755. tab_t[id] = std::thread(colorEditor_MT, (void*)(tab_a + id));
  756. }
  757. for (id = 0; id < _MT_; id++) {
  758. tab_t[id].join();
  759. }
  760. colorspace = Colorspace::RGB;
  761. linear = true;
  762. }
  763. #else
  764. 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)
  765. {
  766. float lMin = selection_lightness[0], lMax = selection_lightness[1];
  767. float cMin = selection_chroma[0], cMax = selection_chroma[1];
  768. float hMin = selection_hue[0], hMax = selection_hue[1];
  769. float hueTolerance = tolerance * 360.0f;
  770. float chromaTolerance = tolerance * 100.0f;
  771. float lightTolerance = tolerance * 100.0f;
  772. float* dataLCH = NULL;
  773. float* minMask = NULL;
  774. float* compMask = NULL;
  775. // not the default parameter
  776. if (!(selection_lightness[0] == 0.0f && selection_lightness[1] == 100.0f
  777. && selection_chroma[0] == 0.0f && selection_chroma[1] == 100.0f
  778. && selection_hue[0] == 0.0f && selection_hue[1] == 360.0f
  779. && tolerance == 0.1f
  780. && edit_hue == 0.0f
  781. && edit_exposure == 0.0f
  782. && edit_contrast == 0.0f
  783. && edit_saturation == 0.0f
  784. && mask == false))
  785. {
  786. if (colorspace == Colorspace::RGB)
  787. {
  788. if (!linear)
  789. {
  790. non_linear_to_linear();
  791. linear = true;
  792. }
  793. dataLCH = Conversion::sRGB_to_Lab(data, width * height);
  794. for (unsigned int i = 0; i < width * height; i++)
  795. {
  796. float a = dataLCH[i * 3 + 1];
  797. float b = dataLCH[i * 3 + 2];
  798. dataLCH[i * 3 + 1] = Conversion::Lab_to_C_of_LCH(a, b);
  799. dataLCH[i * 3 + 2] = Conversion::Lab_to_H_of_LCH(a, b);
  800. }
  801. }
  802. else
  803. dataLCH = data;
  804. float* lChannel = new float[width * height];
  805. float* cChannel = new float[width * height];
  806. float* hChannel = new float[width * height];
  807. for (unsigned int i = 0; i < width * height; i++)
  808. {
  809. lChannel[i] = dataLCH[i * 3];
  810. cChannel[i] = dataLCH[i * 3 + 1];
  811. hChannel[i] = dataLCH[i * 3 + 2];
  812. }
  813. // Récupération du max du canal L et C
  814. float lMaxChannel = dataLCH[0];
  815. float cMaxChannel = dataLCH[1];
  816. for (unsigned int i = 1; i < width * height; i++)
  817. {
  818. if (dataLCH[i * 3] > lMaxChannel)
  819. lMaxChannel = dataLCH[i * 3];
  820. if (dataLCH[i * 3 + 1] > cMaxChannel)
  821. cMaxChannel = dataLCH[i * 3 + 1];
  822. }
  823. if (lMaxChannel < 100.0f)
  824. lMaxChannel = 100.0f;
  825. if (cMaxChannel < 100.0f)
  826. cMaxChannel = 100.0f;
  827. lMax = lMax * lMaxChannel / 100.0f;
  828. cMax = cMax * cMaxChannel / 100.0f;
  829. float* lightnessMask = Utils::NPlinearWeightMask(lChannel, width * height, lMin, lMax, lightTolerance);
  830. float* chromaMask = Utils::NPlinearWeightMask(cChannel, width * height, cMin, cMax, chromaTolerance);
  831. float* hueMask = Utils::NPlinearWeightMask(hChannel, width * height, hMin, hMax, hueTolerance);
  832. minMask = new float[width * height];
  833. compMask = new float[width * height];
  834. for (unsigned int i = 0; i < width * height; i++)
  835. {
  836. minMask[i] = lightnessMask[i];
  837. if (chromaMask[i] < minMask[i])
  838. minMask[i] = chromaMask[i];
  839. if (hueMask[i] < minMask[i])
  840. minMask[i] = hueMask[i];
  841. compMask[i] = 1.0f - minMask[i];
  842. }
  843. delete[](lightnessMask);
  844. delete[](chromaMask);
  845. delete[](hueMask);
  846. float hueShift = edit_hue;
  847. for (unsigned int i = 0; i < width * height; i++) {
  848. float oldValue = hChannel[i];
  849. hChannel[i] = oldValue + hueShift;
  850. while (hChannel[i] < 0.0f)
  851. hChannel[i] += 360.0f;
  852. while (hChannel[i] >= 360.0f)
  853. hChannel[i] -= 360.0f;
  854. hChannel[i] = hChannel[i] * minMask[i] + oldValue * compMask[i];
  855. }
  856. float saturation = edit_saturation;
  857. float gamma = 1.0f / ((saturation / 25.0f) + 1.0f);
  858. if (saturation < 0)
  859. gamma = (-saturation / 25.0f) + 1.0f;
  860. for (unsigned int i = 0; i < width * height; i++) {
  861. cChannel[i] = powf(cChannel[i] / 100.0f, gamma) * 100 * minMask[i] + cChannel[i] * compMask[i];
  862. }
  863. float* colorLCH = new float[width * height * 3];
  864. for (unsigned int i = 0; i < width * height; i++)
  865. {
  866. colorLCH[i * 3] = lChannel[i];
  867. colorLCH[i * 3 + 1] = cChannel[i];
  868. colorLCH[i * 3 + 2] = hChannel[i];
  869. }
  870. delete[](lChannel);
  871. delete[](cChannel);
  872. delete[](hChannel);
  873. float ev = edit_exposure;
  874. float* colorRGB = NULL;
  875. if (ev != 0)
  876. {
  877. colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
  878. float* colorRGBev = new float[width * height * 3];
  879. float coeff = powf(2, ev);
  880. for (unsigned int i = 0; i < width * height; i++)
  881. {
  882. colorRGBev[i * 3] = colorRGB[i * 3] * coeff * minMask[i];
  883. colorRGBev[i * 3 + 1] = colorRGB[i * 3 + 1] * coeff * minMask[i];
  884. colorRGBev[i * 3 + 2] = colorRGB[i * 3 + 2] * coeff * minMask[i];
  885. colorRGB[i * 3] = colorRGB[i * 3] * compMask[i] + colorRGBev[i * 3];
  886. colorRGB[i * 3 + 1] = colorRGB[i * 3 + 1] * compMask[i] + colorRGBev[i * 3 + 1];
  887. colorRGB[i * 3 + 2] = colorRGB[i * 3 + 2] * compMask[i] + colorRGBev[i * 3 + 2];
  888. }
  889. delete[](colorRGBev);
  890. }
  891. if (edit_contrast != 0)
  892. {
  893. float contrast = edit_contrast / 100.0f;
  894. float maxContrastFactor = 2.0f;
  895. float scalingFactor = (1.0f - contrast) + maxContrastFactor * contrast;
  896. if (contrast < 0.0f)
  897. {
  898. contrast = -contrast;
  899. scalingFactor = 1.0f / scalingFactor;
  900. }
  901. float pivot = powf(2, ev) * (lMin + lMax) / 2.0f / 100.0f;
  902. if (colorRGB == NULL)
  903. colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
  904. float* colorRGB2 = Conversion::linear_to_non_linear(colorRGB, width * height * 3);
  905. delete[](colorRGB);
  906. colorRGB=colorRGB2;
  907. float* colorRGBcon = new float[width * height * 3];
  908. for (unsigned int i = 0; i < width * height; i++)
  909. {
  910. colorRGBcon[i * 3] = (colorRGB[i * 3] - pivot) * scalingFactor + pivot;
  911. colorRGBcon[i * 3 + 1] = (colorRGB[i * 3 + 1] - pivot) * scalingFactor + pivot;
  912. colorRGBcon[i * 3 + 2] = (colorRGB[i * 3 + 2] - pivot) * scalingFactor + pivot;
  913. colorRGB[i * 3] = colorRGBcon[i * 3] * minMask[i] + colorRGB[i * 3] * compMask[i];
  914. colorRGB[i * 3 + 1] = colorRGBcon[i * 3 + 1] * minMask[i] + colorRGB[i * 3 + 1] * compMask[i];
  915. colorRGB[i * 3 + 2] = colorRGBcon[i * 3 + 2] * minMask[i] + colorRGB[i * 3 + 2] * compMask[i];
  916. }
  917. delete[](colorRGBcon);
  918. colorRGB2 = Conversion::non_linear_to_linear(colorRGB, width * height * 3);
  919. delete[](colorRGB);
  920. colorRGB=colorRGB2;
  921. }
  922. if (colorRGB == NULL)
  923. colorRGB = Conversion::LCH_to_sRGB(colorLCH, width * height);
  924. for (unsigned int i = 0; i < width*height * 3; i++)
  925. {
  926. data[i] = colorRGB[i];
  927. }
  928. delete[](colorRGB);
  929. delete[](colorLCH);
  930. colorspace = Colorspace::RGB;
  931. linear = true;
  932. }
  933. else
  934. {
  935. if (colorspace == Colorspace::LCH)
  936. {
  937. float* colorRGB = Conversion::LCH_to_sRGB(data, width * height);
  938. for (unsigned int i = 0; i < width*height * 3; i++)
  939. {
  940. data[i] = colorRGB[i];
  941. }
  942. delete[](colorRGB);
  943. colorspace = Colorspace::RGB;
  944. linear = true;
  945. }
  946. }
  947. if (mask)
  948. {
  949. for (unsigned int i = 0; i < width * height; i++)
  950. {
  951. data[i * 3] = minMask[i];
  952. data[i * 3 + 1] = minMask[i];
  953. data[i * 3 + 2] = minMask[i];
  954. }
  955. colorspace = Colorspace::RGB;
  956. linear = false;
  957. }
  958. delete[](minMask);
  959. delete[](compMask);
  960. }
  961. #endif
  962. /* Private methods */
  963. Eigen::VectorXf ImageHDR::to_EigenVector() const
  964. {
  965. Eigen::VectorXf v(width * height * 3);
  966. for (unsigned int i = 0; i < width; i++)
  967. v(i) = data[i];
  968. return v;
  969. }