ImfRgbaYca.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_NAMESPACE;
  47. using namespace std;
  48. #include "ImfNamespace.h"
  49. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  50. namespace RgbaYca {
  51. V3f
  52. computeYw (const Chromaticities &cr)
  53. {
  54. M44f m = RGBtoXYZ (cr, 1);
  55. return V3f (m[0][1], m[1][1], m[2][1]) / (m[0][1] + m[1][1] + m[2][1]);
  56. }
  57. void
  58. RGBAtoYCA (const V3f &yw,
  59. int n,
  60. bool aIsValid,
  61. const Rgba rgbaIn[/*n*/],
  62. Rgba ycaOut[/*n*/])
  63. {
  64. for (int i = 0; i < n; ++i)
  65. {
  66. Rgba in = rgbaIn[i];
  67. Rgba &out = ycaOut[i];
  68. //
  69. // Conversion to YCA and subsequent chroma subsampling
  70. // work only if R, G and B are finite and non-negative.
  71. //
  72. if (!in.r.isFinite() || in.r < 0)
  73. in.r = 0;
  74. if (!in.g.isFinite() || in.g < 0)
  75. in.g = 0;
  76. if (!in.b.isFinite() || in.b < 0)
  77. in.b = 0;
  78. if (in.r == in.g && in.g == in.b)
  79. {
  80. //
  81. // Special case -- R, G and B are equal. To avoid rounding
  82. // errors, we explicitly set the output luminance channel
  83. // to G, and the chroma channels to 0.
  84. //
  85. // The special cases here and in YCAtoRGBA() ensure that
  86. // converting black-and white images from RGBA to YCA and
  87. // back is lossless.
  88. //
  89. out.r = 0;
  90. out.g = in.g;
  91. out.b = 0;
  92. }
  93. else
  94. {
  95. out.g = in.r * yw.x + in.g * yw.y + in.b * yw.z;
  96. float Y = out.g;
  97. if (abs (in.r - Y) < HALF_MAX * Y)
  98. out.r = (in.r - Y) / Y;
  99. else
  100. out.r = 0;
  101. if (abs (in.b - Y) < HALF_MAX * Y)
  102. out.b = (in.b - Y) / Y;
  103. else
  104. out.b = 0;
  105. }
  106. if (aIsValid)
  107. out.a = in.a;
  108. else
  109. out.a = 1;
  110. }
  111. }
  112. void
  113. decimateChromaHoriz (int n,
  114. const Rgba ycaIn[/*n+N-1*/],
  115. Rgba ycaOut[/*n*/])
  116. {
  117. #ifdef DEBUG
  118. assert (ycaIn != ycaOut);
  119. #endif
  120. int begin = N2;
  121. int end = begin + n;
  122. for (int i = begin, j = 0; i < end; ++i, ++j)
  123. {
  124. if ((j & 1) == 0)
  125. {
  126. ycaOut[j].r = ycaIn[i - 13].r * 0.001064f +
  127. ycaIn[i - 11].r * -0.003771f +
  128. ycaIn[i - 9].r * 0.009801f +
  129. ycaIn[i - 7].r * -0.021586f +
  130. ycaIn[i - 5].r * 0.043978f +
  131. ycaIn[i - 3].r * -0.093067f +
  132. ycaIn[i - 1].r * 0.313659f +
  133. ycaIn[i ].r * 0.499846f +
  134. ycaIn[i + 1].r * 0.313659f +
  135. ycaIn[i + 3].r * -0.093067f +
  136. ycaIn[i + 5].r * 0.043978f +
  137. ycaIn[i + 7].r * -0.021586f +
  138. ycaIn[i + 9].r * 0.009801f +
  139. ycaIn[i + 11].r * -0.003771f +
  140. ycaIn[i + 13].r * 0.001064f;
  141. ycaOut[j].b = ycaIn[i - 13].b * 0.001064f +
  142. ycaIn[i - 11].b * -0.003771f +
  143. ycaIn[i - 9].b * 0.009801f +
  144. ycaIn[i - 7].b * -0.021586f +
  145. ycaIn[i - 5].b * 0.043978f +
  146. ycaIn[i - 3].b * -0.093067f +
  147. ycaIn[i - 1].b * 0.313659f +
  148. ycaIn[i ].b * 0.499846f +
  149. ycaIn[i + 1].b * 0.313659f +
  150. ycaIn[i + 3].b * -0.093067f +
  151. ycaIn[i + 5].b * 0.043978f +
  152. ycaIn[i + 7].b * -0.021586f +
  153. ycaIn[i + 9].b * 0.009801f +
  154. ycaIn[i + 11].b * -0.003771f +
  155. ycaIn[i + 13].b * 0.001064f;
  156. }
  157. ycaOut[j].g = ycaIn[i].g;
  158. ycaOut[j].a = ycaIn[i].a;
  159. }
  160. }
  161. void
  162. decimateChromaVert (int n,
  163. const Rgba * const ycaIn[N],
  164. Rgba ycaOut[/*n*/])
  165. {
  166. for (int i = 0; i < n; ++i)
  167. {
  168. if ((i & 1) == 0)
  169. {
  170. ycaOut[i].r = ycaIn[ 0][i].r * 0.001064f +
  171. ycaIn[ 2][i].r * -0.003771f +
  172. ycaIn[ 4][i].r * 0.009801f +
  173. ycaIn[ 6][i].r * -0.021586f +
  174. ycaIn[ 8][i].r * 0.043978f +
  175. ycaIn[10][i].r * -0.093067f +
  176. ycaIn[12][i].r * 0.313659f +
  177. ycaIn[13][i].r * 0.499846f +
  178. ycaIn[14][i].r * 0.313659f +
  179. ycaIn[16][i].r * -0.093067f +
  180. ycaIn[18][i].r * 0.043978f +
  181. ycaIn[20][i].r * -0.021586f +
  182. ycaIn[22][i].r * 0.009801f +
  183. ycaIn[24][i].r * -0.003771f +
  184. ycaIn[26][i].r * 0.001064f;
  185. ycaOut[i].b = ycaIn[ 0][i].b * 0.001064f +
  186. ycaIn[ 2][i].b * -0.003771f +
  187. ycaIn[ 4][i].b * 0.009801f +
  188. ycaIn[ 6][i].b * -0.021586f +
  189. ycaIn[ 8][i].b * 0.043978f +
  190. ycaIn[10][i].b * -0.093067f +
  191. ycaIn[12][i].b * 0.313659f +
  192. ycaIn[13][i].b * 0.499846f +
  193. ycaIn[14][i].b * 0.313659f +
  194. ycaIn[16][i].b * -0.093067f +
  195. ycaIn[18][i].b * 0.043978f +
  196. ycaIn[20][i].b * -0.021586f +
  197. ycaIn[22][i].b * 0.009801f +
  198. ycaIn[24][i].b * -0.003771f +
  199. ycaIn[26][i].b * 0.001064f;
  200. }
  201. ycaOut[i].g = ycaIn[13][i].g;
  202. ycaOut[i].a = ycaIn[13][i].a;
  203. }
  204. }
  205. void
  206. roundYCA (int n,
  207. unsigned int roundY,
  208. unsigned int roundC,
  209. const Rgba ycaIn[/*n*/],
  210. Rgba ycaOut[/*n*/])
  211. {
  212. for (int i = 0; i < n; ++i)
  213. {
  214. ycaOut[i].g = ycaIn[i].g.round (roundY);
  215. ycaOut[i].a = ycaIn[i].a;
  216. if ((i & 1) == 0)
  217. {
  218. ycaOut[i].r = ycaIn[i].r.round (roundC);
  219. ycaOut[i].b = ycaIn[i].b.round (roundC);
  220. }
  221. }
  222. }
  223. void
  224. reconstructChromaHoriz (int n,
  225. const Rgba ycaIn[/*n+N-1*/],
  226. Rgba ycaOut[/*n*/])
  227. {
  228. #ifdef DEBUG
  229. assert (ycaIn != ycaOut);
  230. #endif
  231. int begin = N2;
  232. int end = begin + n;
  233. for (int i = begin, j = 0; i < end; ++i, ++j)
  234. {
  235. if (j & 1)
  236. {
  237. ycaOut[j].r = ycaIn[i - 13].r * 0.002128f +
  238. ycaIn[i - 11].r * -0.007540f +
  239. ycaIn[i - 9].r * 0.019597f +
  240. ycaIn[i - 7].r * -0.043159f +
  241. ycaIn[i - 5].r * 0.087929f +
  242. ycaIn[i - 3].r * -0.186077f +
  243. ycaIn[i - 1].r * 0.627123f +
  244. ycaIn[i + 1].r * 0.627123f +
  245. ycaIn[i + 3].r * -0.186077f +
  246. ycaIn[i + 5].r * 0.087929f +
  247. ycaIn[i + 7].r * -0.043159f +
  248. ycaIn[i + 9].r * 0.019597f +
  249. ycaIn[i + 11].r * -0.007540f +
  250. ycaIn[i + 13].r * 0.002128f;
  251. ycaOut[j].b = ycaIn[i - 13].b * 0.002128f +
  252. ycaIn[i - 11].b * -0.007540f +
  253. ycaIn[i - 9].b * 0.019597f +
  254. ycaIn[i - 7].b * -0.043159f +
  255. ycaIn[i - 5].b * 0.087929f +
  256. ycaIn[i - 3].b * -0.186077f +
  257. ycaIn[i - 1].b * 0.627123f +
  258. ycaIn[i + 1].b * 0.627123f +
  259. ycaIn[i + 3].b * -0.186077f +
  260. ycaIn[i + 5].b * 0.087929f +
  261. ycaIn[i + 7].b * -0.043159f +
  262. ycaIn[i + 9].b * 0.019597f +
  263. ycaIn[i + 11].b * -0.007540f +
  264. ycaIn[i + 13].b * 0.002128f;
  265. }
  266. else
  267. {
  268. ycaOut[j].r = ycaIn[i].r;
  269. ycaOut[j].b = ycaIn[i].b;
  270. }
  271. ycaOut[j].g = ycaIn[i].g;
  272. ycaOut[j].a = ycaIn[i].a;
  273. }
  274. }
  275. void
  276. reconstructChromaVert (int n,
  277. const Rgba * const ycaIn[N],
  278. Rgba ycaOut[/*n*/])
  279. {
  280. for (int i = 0; i < n; ++i)
  281. {
  282. ycaOut[i].r = ycaIn[ 0][i].r * 0.002128f +
  283. ycaIn[ 2][i].r * -0.007540f +
  284. ycaIn[ 4][i].r * 0.019597f +
  285. ycaIn[ 6][i].r * -0.043159f +
  286. ycaIn[ 8][i].r * 0.087929f +
  287. ycaIn[10][i].r * -0.186077f +
  288. ycaIn[12][i].r * 0.627123f +
  289. ycaIn[14][i].r * 0.627123f +
  290. ycaIn[16][i].r * -0.186077f +
  291. ycaIn[18][i].r * 0.087929f +
  292. ycaIn[20][i].r * -0.043159f +
  293. ycaIn[22][i].r * 0.019597f +
  294. ycaIn[24][i].r * -0.007540f +
  295. ycaIn[26][i].r * 0.002128f;
  296. ycaOut[i].b = ycaIn[ 0][i].b * 0.002128f +
  297. ycaIn[ 2][i].b * -0.007540f +
  298. ycaIn[ 4][i].b * 0.019597f +
  299. ycaIn[ 6][i].b * -0.043159f +
  300. ycaIn[ 8][i].b * 0.087929f +
  301. ycaIn[10][i].b * -0.186077f +
  302. ycaIn[12][i].b * 0.627123f +
  303. ycaIn[14][i].b * 0.627123f +
  304. ycaIn[16][i].b * -0.186077f +
  305. ycaIn[18][i].b * 0.087929f +
  306. ycaIn[20][i].b * -0.043159f +
  307. ycaIn[22][i].b * 0.019597f +
  308. ycaIn[24][i].b * -0.007540f +
  309. ycaIn[26][i].b * 0.002128f;
  310. ycaOut[i].g = ycaIn[13][i].g;
  311. ycaOut[i].a = ycaIn[13][i].a;
  312. }
  313. }
  314. void
  315. YCAtoRGBA (const IMATH_NAMESPACE::V3f &yw,
  316. int n,
  317. const Rgba ycaIn[/*n*/],
  318. Rgba rgbaOut[/*n*/])
  319. {
  320. for (int i = 0; i < n; ++i)
  321. {
  322. const Rgba &in = ycaIn[i];
  323. Rgba &out = rgbaOut[i];
  324. if (in.r == 0 && in.b == 0)
  325. {
  326. //
  327. // Special case -- both chroma channels are 0. To avoid
  328. // rounding errors, we explicitly set the output R, G and B
  329. // channels equal to the input luminance.
  330. //
  331. // The special cases here and in RGBAtoYCA() ensure that
  332. // converting black-and white images from RGBA to YCA and
  333. // back is lossless.
  334. //
  335. out.r = in.g;
  336. out.g = in.g;
  337. out.b = in.g;
  338. out.a = in.a;
  339. }
  340. else
  341. {
  342. float Y = in.g;
  343. float r = (in.r + 1) * Y;
  344. float b = (in.b + 1) * Y;
  345. float g = (Y - r * yw.x - b * yw.z) / yw.y;
  346. out.r = r;
  347. out.g = g;
  348. out.b = b;
  349. out.a = in.a;
  350. }
  351. }
  352. }
  353. namespace {
  354. inline float
  355. saturation (const Rgba &in)
  356. {
  357. float rgbMax = max (in.r, max (in.g, in.b));
  358. float rgbMin = min (in.r, min (in.g, in.b));
  359. if (rgbMax > 0)
  360. return 1 - rgbMin / rgbMax;
  361. else
  362. return 0;
  363. }
  364. void
  365. desaturate (const Rgba &in, float f, const V3f &yw, Rgba &out)
  366. {
  367. float rgbMax = max (in.r, max (in.g, in.b));
  368. out.r = max (float (rgbMax - (rgbMax - in.r) * f), 0.0f);
  369. out.g = max (float (rgbMax - (rgbMax - in.g) * f), 0.0f);
  370. out.b = max (float (rgbMax - (rgbMax - in.b) * f), 0.0f);
  371. out.a = in.a;
  372. float Yin = in.r * yw.x + in.g * yw.y + in.b * yw.z;
  373. float Yout = out.r * yw.x + out.g * yw.y + out.b * yw.z;
  374. if (Yout > 0)
  375. {
  376. out.r *= Yin / Yout;
  377. out.g *= Yin / Yout;
  378. out.b *= Yin / Yout;
  379. }
  380. }
  381. } // namespace
  382. void
  383. fixSaturation (const IMATH_NAMESPACE::V3f &yw,
  384. int n,
  385. const Rgba * const rgbaIn[3],
  386. Rgba rgbaOut[/*n*/])
  387. {
  388. float neighborA2 = saturation (rgbaIn[0][0]);
  389. float neighborA1 = neighborA2;
  390. float neighborB2 = saturation (rgbaIn[2][0]);
  391. float neighborB1 = neighborB2;
  392. for (int i = 0; i < n; ++i)
  393. {
  394. float neighborA0 = neighborA1;
  395. neighborA1 = neighborA2;
  396. float neighborB0 = neighborB1;
  397. neighborB1 = neighborB2;
  398. if (i < n - 1)
  399. {
  400. neighborA2 = saturation (rgbaIn[0][i + 1]);
  401. neighborB2 = saturation (rgbaIn[2][i + 1]);
  402. }
  403. //
  404. // A0 A1 A2
  405. // rgbaOut[i]
  406. // B0 B1 B2
  407. //
  408. float sMean = min (1.0f, 0.25f * (neighborA0 + neighborA2 +
  409. neighborB0 + neighborB2));
  410. const Rgba &in = rgbaIn[1][i];
  411. Rgba &out = rgbaOut[i];
  412. float s = saturation (in);
  413. if (s > sMean)
  414. {
  415. float sMax = min (1.0f, 1 - (1 - sMean) * 0.25f);
  416. if (s > sMax)
  417. {
  418. desaturate (in, sMax / s, yw, out);
  419. continue;
  420. }
  421. }
  422. out = in;
  423. }
  424. }
  425. } // namespace RgbaYca
  426. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT