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