ImageHDR.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. std::cout << data[0] << std::endl;
  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. }