ImageHDR.cpp 31 KB

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