/src/FreeImage/Source/OpenEXR/IlmImf/ImfRgbaYca.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 495 lines · 356 code · 70 blank · 69 comment · 39 complexity · 35212749ba5aaacf3f6eaec172162bf5 MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2004, Industrial Light & Magic, a division of Lucasfilm
  4. // Entertainment Company Ltd. Portions contributed and copyright held by
  5. // others as indicated. All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above
  12. // copyright notice, this list of conditions and the following
  13. // disclaimer.
  14. //
  15. // * Redistributions in binary form must reproduce the above
  16. // copyright notice, this list of conditions and the following
  17. // disclaimer in the documentation and/or other materials provided with
  18. // the distribution.
  19. //
  20. // * Neither the name of Industrial Light & Magic nor the names of
  21. // any other contributors to this software may be used to endorse or
  22. // promote products derived from this software without specific prior
  23. // written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. //
  37. //////////////////////////////////////////////////////////////////////////////
  38. //-----------------------------------------------------------------------------
  39. //
  40. // Conversion between RGBA and YCA data.
  41. //
  42. //-----------------------------------------------------------------------------
  43. #include <ImfRgbaYca.h>
  44. #include <assert.h>
  45. #include <algorithm>
  46. using namespace Imath;
  47. using namespace std;
  48. namespace Imf {
  49. namespace RgbaYca {
  50. V3f
  51. computeYw (const Chromaticities &cr)
  52. {
  53. M44f m = RGBtoXYZ (cr, 1);
  54. return V3f (m[0][1], m[1][1], m[2][1]) / (m[0][1] + m[1][1] + m[2][1]);
  55. }
  56. void
  57. RGBAtoYCA (const V3f &yw,
  58. int n,
  59. bool aIsValid,
  60. const Rgba rgbaIn[/*n*/],
  61. Rgba ycaOut[/*n*/])
  62. {
  63. for (int i = 0; i < n; ++i)
  64. {
  65. Rgba in = rgbaIn[i];
  66. Rgba &out = ycaOut[i];
  67. //
  68. // Conversion to YCA and subsequent chroma subsampling
  69. // work only if R, G and B are finite and non-negative.
  70. //
  71. if (!in.r.isFinite() || in.r < 0)
  72. in.r = 0;
  73. if (!in.g.isFinite() || in.g < 0)
  74. in.g = 0;
  75. if (!in.b.isFinite() || in.b < 0)
  76. in.b = 0;
  77. if (in.r == in.g && in.g == in.b)
  78. {
  79. //
  80. // Special case -- R, G and B are equal. To avoid rounding
  81. // errors, we explicitly set the output luminance channel
  82. // to G, and the chroma channels to 0.
  83. //
  84. // The special cases here and in YCAtoRGBA() ensure that
  85. // converting black-and white images from RGBA to YCA and
  86. // back is lossless.
  87. //
  88. out.r = 0;
  89. out.g = in.g;
  90. out.b = 0;
  91. }
  92. else
  93. {
  94. out.g = in.r * yw.x + in.g * yw.y + in.b * yw.z;
  95. float Y = out.g;
  96. if (abs (in.r - Y) < HALF_MAX * Y)
  97. out.r = (in.r - Y) / Y;
  98. else
  99. out.r = 0;
  100. if (abs (in.b - Y) < HALF_MAX * Y)
  101. out.b = (in.b - Y) / Y;
  102. else
  103. out.b = 0;
  104. }
  105. if (aIsValid)
  106. out.a = in.a;
  107. else
  108. out.a = 1;
  109. }
  110. }
  111. void
  112. decimateChromaHoriz (int n,
  113. const Rgba ycaIn[/*n+N-1*/],
  114. Rgba ycaOut[/*n*/])
  115. {
  116. #ifdef DEBUG
  117. assert (ycaIn != ycaOut);
  118. #endif
  119. int begin = N2;
  120. int end = begin + n;
  121. for (int i = begin, j = 0; i < end; ++i, ++j)
  122. {
  123. if ((j & 1) == 0)
  124. {
  125. ycaOut[j].r = ycaIn[i - 13].r * 0.001064f +
  126. ycaIn[i - 11].r * -0.003771f +
  127. ycaIn[i - 9].r * 0.009801f +
  128. ycaIn[i - 7].r * -0.021586f +
  129. ycaIn[i - 5].r * 0.043978f +
  130. ycaIn[i - 3].r * -0.093067f +
  131. ycaIn[i - 1].r * 0.313659f +
  132. ycaIn[i ].r * 0.499846f +
  133. ycaIn[i + 1].r * 0.313659f +
  134. ycaIn[i + 3].r * -0.093067f +
  135. ycaIn[i + 5].r * 0.043978f +
  136. ycaIn[i + 7].r * -0.021586f +
  137. ycaIn[i + 9].r * 0.009801f +
  138. ycaIn[i + 11].r * -0.003771f +
  139. ycaIn[i + 13].r * 0.001064f;
  140. ycaOut[j].b = ycaIn[i - 13].b * 0.001064f +
  141. ycaIn[i - 11].b * -0.003771f +
  142. ycaIn[i - 9].b * 0.009801f +
  143. ycaIn[i - 7].b * -0.021586f +
  144. ycaIn[i - 5].b * 0.043978f +
  145. ycaIn[i - 3].b * -0.093067f +
  146. ycaIn[i - 1].b * 0.313659f +
  147. ycaIn[i ].b * 0.499846f +
  148. ycaIn[i + 1].b * 0.313659f +
  149. ycaIn[i + 3].b * -0.093067f +
  150. ycaIn[i + 5].b * 0.043978f +
  151. ycaIn[i + 7].b * -0.021586f +
  152. ycaIn[i + 9].b * 0.009801f +
  153. ycaIn[i + 11].b * -0.003771f +
  154. ycaIn[i + 13].b * 0.001064f;
  155. }
  156. ycaOut[j].g = ycaIn[i].g;
  157. ycaOut[j].a = ycaIn[i].a;
  158. }
  159. }
  160. void
  161. decimateChromaVert (int n,
  162. const Rgba * const ycaIn[N],
  163. Rgba ycaOut[/*n*/])
  164. {
  165. for (int i = 0; i < n; ++i)
  166. {
  167. if ((i & 1) == 0)
  168. {
  169. ycaOut[i].r = ycaIn[ 0][i].r * 0.001064f +
  170. ycaIn[ 2][i].r * -0.003771f +
  171. ycaIn[ 4][i].r * 0.009801f +
  172. ycaIn[ 6][i].r * -0.021586f +
  173. ycaIn[ 8][i].r * 0.043978f +
  174. ycaIn[10][i].r * -0.093067f +
  175. ycaIn[12][i].r * 0.313659f +
  176. ycaIn[13][i].r * 0.499846f +
  177. ycaIn[14][i].r * 0.313659f +
  178. ycaIn[16][i].r * -0.093067f +
  179. ycaIn[18][i].r * 0.043978f +
  180. ycaIn[20][i].r * -0.021586f +
  181. ycaIn[22][i].r * 0.009801f +
  182. ycaIn[24][i].r * -0.003771f +
  183. ycaIn[26][i].r * 0.001064f;
  184. ycaOut[i].b = ycaIn[ 0][i].b * 0.001064f +
  185. ycaIn[ 2][i].b * -0.003771f +
  186. ycaIn[ 4][i].b * 0.009801f +
  187. ycaIn[ 6][i].b * -0.021586f +
  188. ycaIn[ 8][i].b * 0.043978f +
  189. ycaIn[10][i].b * -0.093067f +
  190. ycaIn[12][i].b * 0.313659f +
  191. ycaIn[13][i].b * 0.499846f +
  192. ycaIn[14][i].b * 0.313659f +
  193. ycaIn[16][i].b * -0.093067f +
  194. ycaIn[18][i].b * 0.043978f +
  195. ycaIn[20][i].b * -0.021586f +
  196. ycaIn[22][i].b * 0.009801f +
  197. ycaIn[24][i].b * -0.003771f +
  198. ycaIn[26][i].b * 0.001064f;
  199. }
  200. ycaOut[i].g = ycaIn[13][i].g;
  201. ycaOut[i].a = ycaIn[13][i].a;
  202. }
  203. }
  204. void
  205. roundYCA (int n,
  206. unsigned int roundY,
  207. unsigned int roundC,
  208. const Rgba ycaIn[/*n*/],
  209. Rgba ycaOut[/*n*/])
  210. {
  211. for (int i = 0; i < n; ++i)
  212. {
  213. ycaOut[i].g = ycaIn[i].g.round (roundY);
  214. ycaOut[i].a = ycaIn[i].a;
  215. if ((i & 1) == 0)
  216. {
  217. ycaOut[i].r = ycaIn[i].r.round (roundC);
  218. ycaOut[i].b = ycaIn[i].b.round (roundC);
  219. }
  220. }
  221. }
  222. void
  223. reconstructChromaHoriz (int n,
  224. const Rgba ycaIn[/*n+N-1*/],
  225. Rgba ycaOut[/*n*/])
  226. {
  227. #ifdef DEBUG
  228. assert (ycaIn != ycaOut);
  229. #endif
  230. int begin = N2;
  231. int end = begin + n;
  232. for (int i = begin, j = 0; i < end; ++i, ++j)
  233. {
  234. if (j & 1)
  235. {
  236. ycaOut[j].r = ycaIn[i - 13].r * 0.002128f +
  237. ycaIn[i - 11].r * -0.007540f +
  238. ycaIn[i - 9].r * 0.019597f +
  239. ycaIn[i - 7].r * -0.043159f +
  240. ycaIn[i - 5].r * 0.087929f +
  241. ycaIn[i - 3].r * -0.186077f +
  242. ycaIn[i - 1].r * 0.627123f +
  243. ycaIn[i + 1].r * 0.627123f +
  244. ycaIn[i + 3].r * -0.186077f +
  245. ycaIn[i + 5].r * 0.087929f +
  246. ycaIn[i + 7].r * -0.043159f +
  247. ycaIn[i + 9].r * 0.019597f +
  248. ycaIn[i + 11].r * -0.007540f +
  249. ycaIn[i + 13].r * 0.002128f;
  250. ycaOut[j].b = ycaIn[i - 13].b * 0.002128f +
  251. ycaIn[i - 11].b * -0.007540f +
  252. ycaIn[i - 9].b * 0.019597f +
  253. ycaIn[i - 7].b * -0.043159f +
  254. ycaIn[i - 5].b * 0.087929f +
  255. ycaIn[i - 3].b * -0.186077f +
  256. ycaIn[i - 1].b * 0.627123f +
  257. ycaIn[i + 1].b * 0.627123f +
  258. ycaIn[i + 3].b * -0.186077f +
  259. ycaIn[i + 5].b * 0.087929f +
  260. ycaIn[i + 7].b * -0.043159f +
  261. ycaIn[i + 9].b * 0.019597f +
  262. ycaIn[i + 11].b * -0.007540f +
  263. ycaIn[i + 13].b * 0.002128f;
  264. }
  265. else
  266. {
  267. ycaOut[j].r = ycaIn[i].r;
  268. ycaOut[j].b = ycaIn[i].b;
  269. }
  270. ycaOut[j].g = ycaIn[i].g;
  271. ycaOut[j].a = ycaIn[i].a;
  272. }
  273. }
  274. void
  275. reconstructChromaVert (int n,
  276. const Rgba * const ycaIn[N],
  277. Rgba ycaOut[/*n*/])
  278. {
  279. for (int i = 0; i < n; ++i)
  280. {
  281. ycaOut[i].r = ycaIn[ 0][i].r * 0.002128f +
  282. ycaIn[ 2][i].r * -0.007540f +
  283. ycaIn[ 4][i].r * 0.019597f +
  284. ycaIn[ 6][i].r * -0.043159f +
  285. ycaIn[ 8][i].r * 0.087929f +
  286. ycaIn[10][i].r * -0.186077f +
  287. ycaIn[12][i].r * 0.627123f +
  288. ycaIn[14][i].r * 0.627123f +
  289. ycaIn[16][i].r * -0.186077f +
  290. ycaIn[18][i].r * 0.087929f +
  291. ycaIn[20][i].r * -0.043159f +
  292. ycaIn[22][i].r * 0.019597f +
  293. ycaIn[24][i].r * -0.007540f +
  294. ycaIn[26][i].r * 0.002128f;
  295. ycaOut[i].b = ycaIn[ 0][i].b * 0.002128f +
  296. ycaIn[ 2][i].b * -0.007540f +
  297. ycaIn[ 4][i].b * 0.019597f +
  298. ycaIn[ 6][i].b * -0.043159f +
  299. ycaIn[ 8][i].b * 0.087929f +
  300. ycaIn[10][i].b * -0.186077f +
  301. ycaIn[12][i].b * 0.627123f +
  302. ycaIn[14][i].b * 0.627123f +
  303. ycaIn[16][i].b * -0.186077f +
  304. ycaIn[18][i].b * 0.087929f +
  305. ycaIn[20][i].b * -0.043159f +
  306. ycaIn[22][i].b * 0.019597f +
  307. ycaIn[24][i].b * -0.007540f +
  308. ycaIn[26][i].b * 0.002128f;
  309. ycaOut[i].g = ycaIn[13][i].g;
  310. ycaOut[i].a = ycaIn[13][i].a;
  311. }
  312. }
  313. void
  314. YCAtoRGBA (const Imath::V3f &yw,
  315. int n,
  316. const Rgba ycaIn[/*n*/],
  317. Rgba rgbaOut[/*n*/])
  318. {
  319. for (int i = 0; i < n; ++i)
  320. {
  321. const Rgba &in = ycaIn[i];
  322. Rgba &out = rgbaOut[i];
  323. if (in.r == 0 && in.b == 0)
  324. {
  325. //
  326. // Special case -- both chroma channels are 0. To avoid
  327. // rounding errors, we explicitly set the output R, G and B
  328. // channels equal to the input luminance.
  329. //
  330. // The special cases here and in RGBAtoYCA() ensure that
  331. // converting black-and white images from RGBA to YCA and
  332. // back is lossless.
  333. //
  334. out.r = in.g;
  335. out.g = in.g;
  336. out.b = in.g;
  337. out.a = in.a;
  338. }
  339. else
  340. {
  341. float Y = in.g;
  342. float r = (in.r + 1) * Y;
  343. float b = (in.b + 1) * Y;
  344. float g = (Y - r * yw.x - b * yw.z) / yw.y;
  345. out.r = r;
  346. out.g = g;
  347. out.b = b;
  348. out.a = in.a;
  349. }
  350. }
  351. }
  352. namespace {
  353. inline float
  354. saturation (const Rgba &in)
  355. {
  356. float rgbMax = max (in.r, max (in.g, in.b));
  357. float rgbMin = min (in.r, min (in.g, in.b));
  358. if (rgbMax > 0)
  359. return 1 - rgbMin / rgbMax;
  360. else
  361. return 0;
  362. }
  363. void
  364. desaturate (const Rgba &in, float f, const V3f &yw, Rgba &out)
  365. {
  366. float rgbMax = max (in.r, max (in.g, in.b));
  367. out.r = max (float (rgbMax - (rgbMax - in.r) * f), 0.0f);
  368. out.g = max (float (rgbMax - (rgbMax - in.g) * f), 0.0f);
  369. out.b = max (float (rgbMax - (rgbMax - in.b) * f), 0.0f);
  370. out.a = in.a;
  371. float Yin = in.r * yw.x + in.g * yw.y + in.b * yw.z;
  372. float Yout = out.r * yw.x + out.g * yw.y + out.b * yw.z;
  373. if (Yout > 0)
  374. {
  375. out.r *= Yin / Yout;
  376. out.g *= Yin / Yout;
  377. out.b *= Yin / Yout;
  378. }
  379. }
  380. } // namespace
  381. void
  382. fixSaturation (const Imath::V3f &yw,
  383. int n,
  384. const Rgba * const rgbaIn[3],
  385. Rgba rgbaOut[/*n*/])
  386. {
  387. float neighborA2 = saturation (rgbaIn[0][0]);
  388. float neighborA1 = neighborA2;
  389. float neighborB2 = saturation (rgbaIn[2][0]);
  390. float neighborB1 = neighborB2;
  391. for (int i = 0; i < n; ++i)
  392. {
  393. float neighborA0 = neighborA1;
  394. neighborA1 = neighborA2;
  395. float neighborB0 = neighborB1;
  396. neighborB1 = neighborB2;
  397. if (i < n - 1)
  398. {
  399. neighborA2 = saturation (rgbaIn[0][i + 1]);
  400. neighborB2 = saturation (rgbaIn[2][i + 1]);
  401. }
  402. //
  403. // A0 A1 A2
  404. // rgbaOut[i]
  405. // B0 B1 B2
  406. //
  407. float sMean = min (1.0f, 0.25f * (neighborA0 + neighborA2 +
  408. neighborB0 + neighborB2));
  409. const Rgba &in = rgbaIn[1][i];
  410. Rgba &out = rgbaOut[i];
  411. float s = saturation (in);
  412. if (s > sMean)
  413. {
  414. float sMax = min (1.0f, 1 - (1 - sMean) * 0.25f);
  415. if (s > sMax)
  416. {
  417. desaturate (in, sMax / s, yw, out);
  418. continue;
  419. }
  420. }
  421. out = in;
  422. }
  423. }
  424. } // namespace RgbaYca
  425. } // namespace Imf