3
0

PixelOperation.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/std/smart_ptr/make_shared.h>
  9. #include <Processing/ImageObjectImpl.h>
  10. #include <Processing/ImageConvert.h>
  11. #include <Processing/PixelFormatInfo.h>
  12. #include <Converters/PixelOperation.h>
  13. ///////////////////////////////////////////////////////////////////////////////////
  14. //functions for maintaining alpha coverage.
  15. namespace ImageProcessingAtom
  16. {
  17. //convertion: all data type supported by pixel channel <=> float
  18. float U8ToF32(uint8 in)
  19. {
  20. return in / 255.f;
  21. }
  22. uint8 F32ToU8(float in)
  23. {
  24. return static_cast<uint8>(round(AZ::GetClamp<float>(in, 0.f, 1.f) * 255.f));
  25. }
  26. float U16ToF32(uint16 in)
  27. {
  28. return in / 65535.f;
  29. }
  30. uint16 F32ToU16(float in)
  31. {
  32. return static_cast<uint16>(round(AZ::GetClamp<float>(in, 0.f, 1.f) * 65535.f));
  33. }
  34. float HalfToF32(SHalf in)
  35. {
  36. return in;
  37. }
  38. SHalf F32ToHalf(float in)
  39. {
  40. return SHalf(in);
  41. }
  42. //stucture for RGBE pixel format
  43. struct RgbE
  44. {
  45. static const int RGB9E5_EXPONENT_BITS = 5;
  46. static const int RGB9E5_MANTISSA_BITS = 9;
  47. static const int RGB9E5_EXP_BIAS = 15;
  48. static const int RGB9E5_MAX_VALID_BIASED_EXP = 31;
  49. static const int MAX_RGB9E5_EXP = (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS);
  50. static const int RGB9E5_MANTISSA_VALUES = (1 << RGB9E5_MANTISSA_BITS);
  51. static const int MAX_RGB9E5_MANTISSA = (RGB9E5_MANTISSA_VALUES - 1);
  52. static float MAX_RGB9E5;
  53. unsigned int r : 9;
  54. unsigned int g : 9;
  55. unsigned int b : 9;
  56. unsigned int e : 5;
  57. static int log2(float x)
  58. {
  59. int bitfield = *((int*)(&x));
  60. bitfield &= ~0x80000000;
  61. return ((bitfield >> 23) - 127);
  62. }
  63. void GetRGBF(float& outR, float& outG, float& outB) const
  64. {
  65. int exponent = e - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
  66. float scale = powf(2.0f, static_cast<float>(exponent));
  67. outR = r * scale;
  68. outG = g * scale;
  69. outB = b * scale;
  70. }
  71. void SetRGBF(const float& inR, const float& inG, const float& inB)
  72. {
  73. float rf = AZStd::GetMax(0.0f, AZStd::GetMin(inR, MAX_RGB9E5));
  74. float gf = AZStd::GetMax(0.0f, AZStd::GetMin(inG, MAX_RGB9E5));
  75. float bf = AZStd::GetMax(0.0f, AZStd::GetMin(inB, MAX_RGB9E5));
  76. float mf = AZStd::GetMax(rf, AZStd::GetMax(gf, bf));
  77. e = AZStd::GetMax(0, log2(mf) + (RGB9E5_EXP_BIAS + 1));
  78. int exponent = e - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
  79. float scale = powf(2.0f, static_cast<float>(exponent));
  80. r = AZStd::GetMin(511, (int)floorf(rf / scale + 0.5f));
  81. g = AZStd::GetMin(511, (int)floorf(gf / scale + 0.5f));
  82. b = AZStd::GetMin(511, (int)floorf(bf / scale + 0.5f));
  83. }
  84. };
  85. float RgbE::MAX_RGB9E5 = (((float)MAX_RGB9E5_MANTISSA) / RGB9E5_MANTISSA_VALUES * (1 << MAX_RGB9E5_EXP));
  86. //ePixelFormat_R8G8B8A8
  87. class PixelOperationR8G8B8A8
  88. : public IPixelOperation
  89. {
  90. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  91. {
  92. const uint8* data = buf;
  93. r = U8ToF32(data[0]);
  94. g = U8ToF32(data[1]);
  95. b = U8ToF32(data[2]);
  96. a = U8ToF32(data[3]);
  97. }
  98. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
  99. {
  100. uint8* data = buf;
  101. data[0] = F32ToU8(r);
  102. data[1] = F32ToU8(g);
  103. data[2] = F32ToU8(b);
  104. data[3] = F32ToU8(a);
  105. }
  106. };
  107. //ePixelFormat_R8G8B8X8
  108. class PixelOperationR8G8B8X8
  109. : public IPixelOperation
  110. {
  111. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  112. {
  113. const uint8* data = buf;
  114. r = U8ToF32(data[0]);
  115. g = U8ToF32(data[1]);
  116. b = U8ToF32(data[2]);
  117. a = 1.f;
  118. }
  119. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, [[maybe_unused]] const float& a) override
  120. {
  121. uint8* data = buf;
  122. data[0] = F32ToU8(r);
  123. data[1] = F32ToU8(g);
  124. data[2] = F32ToU8(b);
  125. data[3] = 0xff;
  126. }
  127. };
  128. //ePixelFormat_B8G8R8A8
  129. class PixelOperationB8G8R8A8
  130. : public IPixelOperation
  131. {
  132. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  133. {
  134. const uint8* data = buf;
  135. r = U8ToF32(data[2]);
  136. g = U8ToF32(data[1]);
  137. b = U8ToF32(data[0]);
  138. a = U8ToF32(data[3]);
  139. }
  140. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
  141. {
  142. uint8* data = buf;
  143. data[0] = F32ToU8(b);
  144. data[1] = F32ToU8(g);
  145. data[2] = F32ToU8(r);
  146. data[3] = F32ToU8(a);
  147. }
  148. };
  149. //ePixelFormat_R8G8B8
  150. class PixelOperationR8G8B8
  151. : public IPixelOperation
  152. {
  153. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  154. {
  155. const uint8* data = buf;
  156. r = U8ToF32(data[0]);
  157. g = U8ToF32(data[1]);
  158. b = U8ToF32(data[2]);
  159. a = 1.0f;
  160. }
  161. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, [[maybe_unused]] const float& a) override
  162. {
  163. uint8* data = buf;
  164. data[0] = F32ToU8(r);
  165. data[1] = F32ToU8(g);
  166. data[2] = F32ToU8(b);
  167. }
  168. };
  169. //ePixelFormat_B8G8R8
  170. class PixelOperationB8G8R8
  171. : public IPixelOperation
  172. {
  173. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  174. {
  175. const uint8* data = buf;
  176. r = U8ToF32(data[2]);
  177. g = U8ToF32(data[1]);
  178. b = U8ToF32(data[0]);
  179. a = 1.0f;
  180. }
  181. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, [[maybe_unused]] const float& a) override
  182. {
  183. uint8* data = buf;
  184. data[0] = F32ToU8(b);
  185. data[1] = F32ToU8(g);
  186. data[2] = F32ToU8(r);
  187. }
  188. };
  189. //ePixelFormat_R8G8
  190. class PixelOperationR8G8
  191. : public IPixelOperation
  192. {
  193. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  194. {
  195. const uint8* data = buf;
  196. r = U8ToF32(data[0]);
  197. g = U8ToF32(data[1]);
  198. b = 0.f;
  199. a = 1.f;
  200. }
  201. void SetRGBA(uint8* buf, const float& r, const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
  202. {
  203. uint8* data = buf;
  204. data[0] = F32ToU8(r);
  205. data[1] = F32ToU8(g);
  206. }
  207. };
  208. //ePixelFormat_R8
  209. class PixelOperationR8
  210. : public IPixelOperation
  211. {
  212. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  213. {
  214. const uint8* data = buf;
  215. r = U8ToF32(data[0]);
  216. g = r;
  217. b = r;
  218. a = 1.f;
  219. }
  220. void SetRGBA(uint8* buf, const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
  221. {
  222. uint8* data = buf;
  223. data[0] = F32ToU8(r);
  224. }
  225. };
  226. //ePixelFormat_A8
  227. class PixelOperationA8
  228. : public IPixelOperation
  229. {
  230. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  231. {
  232. const uint8* data = buf;
  233. a = U8ToF32(data[0]);
  234. //save alpha information to rgb too. useful for preview.
  235. r = a;
  236. g = a;
  237. b = a;
  238. }
  239. void SetRGBA(uint8* buf, [[maybe_unused]] const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, const float& a) override
  240. {
  241. uint8* data = buf;
  242. data[0] = F32ToU8(a);
  243. }
  244. };
  245. //ePixelFormat_R16G16B16A16
  246. class PixelOperationR16G16B16A16
  247. : public IPixelOperation
  248. {
  249. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  250. {
  251. const uint16* data = (uint16*)(buf);
  252. r = U16ToF32(data[0]);
  253. g = U16ToF32(data[1]);
  254. b = U16ToF32(data[2]);
  255. a = U16ToF32(data[3]);
  256. }
  257. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
  258. {
  259. uint16* data = (uint16*)(buf);
  260. data[0] = F32ToU16(r);
  261. data[1] = F32ToU16(g);
  262. data[2] = F32ToU16(b);
  263. data[3] = F32ToU16(a);
  264. }
  265. };
  266. //ePixelFormat_R16G16
  267. class PixelOperationR16G16
  268. : public IPixelOperation
  269. {
  270. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  271. {
  272. const uint16* data = (uint16*)(buf);
  273. r = U16ToF32(data[0]);
  274. g = U16ToF32(data[1]);
  275. b = 0.f;
  276. a = 1.f;
  277. }
  278. void SetRGBA(uint8* buf, const float& r, const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
  279. {
  280. uint16* data = (uint16*)(buf);
  281. data[0] = F32ToU16(r);
  282. data[1] = F32ToU16(g);
  283. }
  284. };
  285. //ePixelFormat_R16
  286. class PixelOperationR16
  287. : public IPixelOperation
  288. {
  289. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  290. {
  291. const uint16* data = (uint16*)(buf);
  292. r = U16ToF32(data[0]);
  293. g = r;
  294. b = r;
  295. a = 1.f;
  296. }
  297. void SetRGBA(uint8* buf, const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
  298. {
  299. uint16* data = (uint16*)(buf);
  300. data[0] = F32ToU16(r);
  301. }
  302. };
  303. //ePixelFormat_R9G9B9E5
  304. class PixelOperationR9G9B9E5
  305. : public IPixelOperation
  306. {
  307. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  308. {
  309. const RgbE* data = (RgbE*)(buf);
  310. data->GetRGBF(r, g, b);
  311. a = 1.f;
  312. }
  313. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, [[maybe_unused]] const float& a) override
  314. {
  315. RgbE* data = (RgbE*)(buf);
  316. data->SetRGBF(r, g, b);
  317. }
  318. };
  319. //ePixelFormat_R32G32B32A32F
  320. class PixelOperationR32G32B32A32F
  321. : public IPixelOperation
  322. {
  323. public:
  324. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  325. {
  326. const float* data = (float*)(buf);
  327. r = data[0];
  328. g = data[1];
  329. b = data[2];
  330. a = data[3];
  331. }
  332. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
  333. {
  334. float* data = (float*)(buf);
  335. data[0] = r;
  336. data[1] = g;
  337. data[2] = b;
  338. data[3] = a;
  339. }
  340. };
  341. //ePixelFormat_R32G32F
  342. class PixelOperationR32G32F
  343. : public IPixelOperation
  344. {
  345. public:
  346. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  347. {
  348. const float* data = (float*)(buf);
  349. r = data[0];
  350. g = data[1];
  351. b = 0.f;
  352. a = 1.f;
  353. }
  354. void SetRGBA(uint8* buf, const float& r, const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
  355. {
  356. float* data = (float*)(buf);
  357. data[0] = r;
  358. data[1] = g;
  359. }
  360. };
  361. //ePixelFormat_R32F
  362. class PixelOperationR32F
  363. : public IPixelOperation
  364. {
  365. public:
  366. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  367. {
  368. const float* data = (float*)(buf);
  369. r = data[0];
  370. g = r;
  371. b = r;
  372. a = 1.f;
  373. }
  374. void SetRGBA(uint8* buf, const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
  375. {
  376. float* data = (float*)(buf);
  377. data[0] = r;
  378. }
  379. };
  380. //ePixelFormat_R16G16B16A16F
  381. class PixelOperationR16G16B16A16F
  382. : public IPixelOperation
  383. {
  384. public:
  385. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  386. {
  387. const SHalf* data = (SHalf*)(buf);
  388. r = data[0];
  389. g = data[1];
  390. b = data[2];
  391. a = data[3];
  392. }
  393. void SetRGBA(uint8* buf, const float& r, const float& g, const float& b, const float& a) override
  394. {
  395. SHalf* data = (SHalf*)(buf);
  396. data[0] = SHalf(r);
  397. data[1] = SHalf(g);
  398. data[2] = SHalf(b);
  399. data[3] = SHalf(a);
  400. }
  401. };
  402. //ePixelFormat_R16G16F
  403. class PixelOperationR16G16F
  404. : public IPixelOperation
  405. {
  406. public:
  407. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  408. {
  409. const SHalf* data = (SHalf*)(buf);
  410. r = data[0];
  411. g = data[1];
  412. b = 0.f;
  413. a = 1.f;
  414. }
  415. void SetRGBA(uint8* buf, const float& r, const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
  416. {
  417. SHalf* data = (SHalf*)(buf);
  418. data[0] = SHalf(r);
  419. data[1] = SHalf(g);
  420. }
  421. };
  422. //ePixelFormat_R16F
  423. class PixelOperationR16F
  424. : public IPixelOperation
  425. {
  426. public:
  427. void GetRGBA(const uint8* buf, float& r, float& g, float& b, float& a) override
  428. {
  429. const SHalf* data = (SHalf*)(buf);
  430. r = data[0];
  431. g = r;
  432. b = r;
  433. a = 1.f;
  434. }
  435. void SetRGBA(uint8* buf, const float& r, [[maybe_unused]] const float& g, [[maybe_unused]] const float& b, [[maybe_unused]] const float& a) override
  436. {
  437. SHalf* data = (SHalf*)(buf);
  438. data[0] = SHalf(r);
  439. }
  440. };
  441. IPixelOperationPtr CreatePixelOperation(EPixelFormat pixelFmt)
  442. {
  443. switch (pixelFmt)
  444. {
  445. case ePixelFormat_R8G8B8A8:
  446. return AZStd::make_shared<PixelOperationR8G8B8A8>();
  447. case ePixelFormat_R8G8B8X8:
  448. return AZStd::make_shared<PixelOperationR8G8B8X8>();
  449. case ePixelFormat_B8G8R8A8:
  450. return AZStd::make_shared<PixelOperationB8G8R8A8>();
  451. case ePixelFormat_B8G8R8:
  452. return AZStd::make_shared<PixelOperationB8G8R8>();
  453. case ePixelFormat_R8G8B8:
  454. return AZStd::make_shared<PixelOperationR8G8B8>();
  455. case ePixelFormat_R8G8:
  456. return AZStd::make_shared<PixelOperationR8G8>();
  457. case ePixelFormat_R8:
  458. return AZStd::make_shared<PixelOperationR8>();
  459. case ePixelFormat_A8:
  460. return AZStd::make_shared<PixelOperationA8>();
  461. case ePixelFormat_R16G16B16A16:
  462. return AZStd::make_shared<PixelOperationR16G16B16A16>();
  463. case ePixelFormat_R16G16:
  464. return AZStd::make_shared<PixelOperationR16G16>();
  465. case ePixelFormat_R16:
  466. return AZStd::make_shared<PixelOperationR16>();
  467. case ePixelFormat_R9G9B9E5:
  468. return AZStd::make_shared<PixelOperationR9G9B9E5>();
  469. case ePixelFormat_R32G32B32A32F:
  470. return AZStd::make_shared<PixelOperationR32G32B32A32F>();
  471. case ePixelFormat_R32G32F:
  472. return AZStd::make_shared<PixelOperationR32G32F>();
  473. case ePixelFormat_R32F:
  474. return AZStd::make_shared<PixelOperationR32F>();
  475. case ePixelFormat_R16G16B16A16F:
  476. return AZStd::make_shared<PixelOperationR16G16B16A16F>();
  477. case ePixelFormat_R16G16F:
  478. return AZStd::make_shared<PixelOperationR16G16F>();
  479. case ePixelFormat_R16F:
  480. return AZStd::make_shared<PixelOperationR16F>();
  481. default:
  482. AZ_Assert(false, "This function should be only called for uncompressed pixel format");
  483. break;
  484. }
  485. return nullptr;
  486. }
  487. } // namespace ImageProcessingAtom