EtcBlock4x4Encoding_RG11.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright 2015 The Etc2Comp Authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. EtcBlock4x4Encoding_RG11.cpp
  18. Block4x4Encoding_RG11 is the encoder to use when targetting file format RG11 and SRG11 (signed RG11).
  19. */
  20. #include "EtcConfig.h"
  21. #include "EtcBlock4x4Encoding_RG11.h"
  22. #include "EtcBlock4x4EncodingBits.h"
  23. #include "EtcBlock4x4.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <float.h>
  28. #include <limits>
  29. namespace Etc
  30. {
  31. // ----------------------------------------------------------------------------------------------------
  32. //
  33. Block4x4Encoding_RG11::Block4x4Encoding_RG11(void)
  34. {
  35. m_pencodingbitsRG11 = nullptr;
  36. }
  37. Block4x4Encoding_RG11::~Block4x4Encoding_RG11(void) {}
  38. // ----------------------------------------------------------------------------------------------------
  39. // initialization prior to encoding
  40. // a_pblockParent points to the block associated with this encoding
  41. // a_errormetric is used to choose the best encoding
  42. // a_pafrgbaSource points to a 4x4 block subset of the source image
  43. // a_paucEncodingBits points to the final encoding bits
  44. //
  45. void Block4x4Encoding_RG11::InitFromSource(Block4x4 *a_pblockParent,
  46. ColorFloatRGBA *a_pafrgbaSource,
  47. unsigned char *a_paucEncodingBits, ErrorMetric a_errormetric)
  48. {
  49. Block4x4Encoding::Init(a_pblockParent, a_pafrgbaSource,a_errormetric);
  50. m_pencodingbitsRG11 = (Block4x4EncodingBits_RG11 *)a_paucEncodingBits;
  51. }
  52. // ----------------------------------------------------------------------------------------------------
  53. // initialization from the encoding bits of a previous encoding
  54. // a_pblockParent points to the block associated with this encoding
  55. // a_errormetric is used to choose the best encoding
  56. // a_pafrgbaSource points to a 4x4 block subset of the source image
  57. // a_paucEncodingBits points to the final encoding bits of a previous encoding
  58. //
  59. void Block4x4Encoding_RG11::InitFromEncodingBits(Block4x4 *a_pblockParent,
  60. unsigned char *a_paucEncodingBits,
  61. ColorFloatRGBA *a_pafrgbaSource,
  62. ErrorMetric a_errormetric)
  63. {
  64. m_pencodingbitsRG11 = (Block4x4EncodingBits_RG11 *)a_paucEncodingBits;
  65. // init RGB portion
  66. Block4x4Encoding_RGB8::InitFromEncodingBits(a_pblockParent,
  67. (unsigned char *)m_pencodingbitsRG11,
  68. a_pafrgbaSource,
  69. a_errormetric);
  70. m_fError = 0.0f;
  71. {
  72. m_mode = MODE_RG11;
  73. if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
  74. {
  75. m_fRedBase = (float)(signed char)m_pencodingbitsRG11->data.baseR;
  76. m_fGrnBase = (float)(signed char)m_pencodingbitsRG11->data.baseG;
  77. }
  78. else
  79. {
  80. m_fRedBase = (float)(unsigned char)m_pencodingbitsRG11->data.baseR;
  81. m_fGrnBase = (float)(unsigned char)m_pencodingbitsRG11->data.baseG;
  82. }
  83. m_fRedMultiplier = (float)m_pencodingbitsRG11->data.multiplierR;
  84. m_fGrnMultiplier = (float)m_pencodingbitsRG11->data.multiplierG;
  85. m_uiRedModifierTableIndex = m_pencodingbitsRG11->data.tableIndexR;
  86. m_uiGrnModifierTableIndex = m_pencodingbitsRG11->data.tableIndexG;
  87. unsigned long long int ulliSelectorBitsR = 0;
  88. ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR0 << 40;
  89. ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR1 << 32;
  90. ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR2 << 24;
  91. ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR3 << 16;
  92. ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR4 << 8;
  93. ulliSelectorBitsR |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsR5;
  94. unsigned long long int ulliSelectorBitsG = 0;
  95. ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG0 << 40;
  96. ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG1 << 32;
  97. ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG2 << 24;
  98. ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG3 << 16;
  99. ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG4 << 8;
  100. ulliSelectorBitsG |= (unsigned long long int)m_pencodingbitsRG11->data.selectorsG5;
  101. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  102. {
  103. unsigned int uiShift = 45 - (3 * uiPixel);
  104. m_auiRedSelectors[uiPixel] = (ulliSelectorBitsR >> uiShift) & (SELECTORS - 1);
  105. m_auiGrnSelectors[uiPixel] = (ulliSelectorBitsG >> uiShift) & (SELECTORS - 1);
  106. }
  107. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  108. {
  109. float fRedDecodedData = 0.0f;
  110. float fGrnDecodedData = 0.0f;
  111. if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
  112. {
  113. fRedDecodedData = DecodePixelRed(m_fRedBase, m_fRedMultiplier, m_uiRedModifierTableIndex, m_auiRedSelectors[uiPixel]);
  114. fGrnDecodedData = DecodePixelRed(m_fGrnBase, m_fGrnMultiplier, m_uiGrnModifierTableIndex, m_auiGrnSelectors[uiPixel]);
  115. }
  116. else if (a_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
  117. {
  118. fRedDecodedData = DecodePixelRed(m_fRedBase + 128, m_fRedMultiplier, m_uiRedModifierTableIndex, m_auiRedSelectors[uiPixel]);
  119. fGrnDecodedData = DecodePixelRed(m_fGrnBase + 128, m_fGrnMultiplier, m_uiGrnModifierTableIndex, m_auiGrnSelectors[uiPixel]);
  120. }
  121. else
  122. {
  123. assert(0);
  124. }
  125. m_afrgbaDecodedColors[uiPixel] = ColorFloatRGBA(fRedDecodedData, fGrnDecodedData, 0.0f, 1.0f);
  126. }
  127. }
  128. CalcBlockError();
  129. }
  130. // ----------------------------------------------------------------------------------------------------
  131. // perform a single encoding iteration
  132. // replace the encoding if a better encoding was found
  133. // subsequent iterations generally take longer for each iteration
  134. // set m_boolDone if encoding is perfect or encoding is finished based on a_fEffort
  135. //
  136. void Block4x4Encoding_RG11::PerformIteration(float a_fEffort)
  137. {
  138. assert(!m_boolDone);
  139. switch (m_uiEncodingIterations)
  140. {
  141. case 0:
  142. m_fError = FLT_MAX;
  143. m_fGrnBlockError = FLT_MAX; // artificially high value
  144. m_fRedBlockError = FLT_MAX;
  145. CalculateR11(8, 0.0f, 0.0f);
  146. CalculateG11(8, 0.0f, 0.0f);
  147. m_fError = (m_fGrnBlockError + m_fRedBlockError);
  148. break;
  149. case 1:
  150. CalculateR11(8, 2.0f, 1.0f);
  151. CalculateG11(8, 2.0f, 1.0f);
  152. m_fError = (m_fGrnBlockError + m_fRedBlockError);
  153. if (a_fEffort <= 24.5f)
  154. {
  155. m_boolDone = true;
  156. }
  157. break;
  158. case 2:
  159. CalculateR11(8, 12.0f, 1.0f);
  160. CalculateG11(8, 12.0f, 1.0f);
  161. m_fError = (m_fGrnBlockError + m_fRedBlockError);
  162. if (a_fEffort <= 49.5f)
  163. {
  164. m_boolDone = true;
  165. }
  166. break;
  167. case 3:
  168. CalculateR11(7, 6.0f, 1.0f);
  169. CalculateG11(7, 6.0f, 1.0f);
  170. m_fError = (m_fGrnBlockError + m_fRedBlockError);
  171. break;
  172. case 4:
  173. CalculateR11(6, 3.0f, 1.0f);
  174. CalculateG11(6, 3.0f, 1.0f);
  175. m_fError = (m_fGrnBlockError + m_fRedBlockError);
  176. break;
  177. case 5:
  178. CalculateR11(5, 1.0f, 0.0f);
  179. CalculateG11(5, 1.0f, 0.0f);
  180. m_fError = (m_fGrnBlockError + m_fRedBlockError);
  181. m_boolDone = true;
  182. break;
  183. default:
  184. assert(0);
  185. break;
  186. }
  187. m_uiEncodingIterations++;
  188. SetDoneIfPerfect();
  189. }
  190. // ----------------------------------------------------------------------------------------------------
  191. // find the best combination of base color, multiplier and selectors
  192. //
  193. // a_uiSelectorsUsed limits the number of selector combinations to try
  194. // a_fBaseRadius limits the range of base colors to try
  195. // a_fMultiplierRadius limits the range of multipliers to try
  196. //
  197. void Block4x4Encoding_RG11::CalculateG11(unsigned int a_uiSelectorsUsed,
  198. float a_fBaseRadius, float a_fMultiplierRadius)
  199. {
  200. // maps from virtual (monotonic) selector to etc selector
  201. static const unsigned int auiVirtualSelectorMap[8] = { 3, 2, 1, 0, 4, 5, 6, 7 };
  202. // find min/max Grn
  203. float fMinGrn = 1.0f;
  204. float fMaxGrn = 0.0f;
  205. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  206. {
  207. // ignore border pixels
  208. float fAlpha = m_pafrgbaSource[uiPixel].fA;
  209. if (isnan(fAlpha))
  210. {
  211. continue;
  212. }
  213. float fGrn = m_pafrgbaSource[uiPixel].fG;
  214. if (fGrn < fMinGrn)
  215. {
  216. fMinGrn = fGrn;
  217. }
  218. if (fGrn > fMaxGrn)
  219. {
  220. fMaxGrn = fGrn;
  221. }
  222. }
  223. assert(fMinGrn <= fMaxGrn);
  224. float fGrnRange = (fMaxGrn - fMinGrn);
  225. // try each modifier table entry
  226. for (unsigned int uiTableEntry = 0; uiTableEntry < MODIFIER_TABLE_ENTRYS; uiTableEntry++)
  227. {
  228. for (unsigned int uiMinVirtualSelector = 0;
  229. uiMinVirtualSelector <= (8 - a_uiSelectorsUsed);
  230. uiMinVirtualSelector++)
  231. {
  232. unsigned int uiMaxVirtualSelector = uiMinVirtualSelector + a_uiSelectorsUsed - 1;
  233. unsigned int uiMinSelector = auiVirtualSelectorMap[uiMinVirtualSelector];
  234. unsigned int uiMaxSelector = auiVirtualSelectorMap[uiMaxVirtualSelector];
  235. float fTableEntryCenter = -s_aafModifierTable[uiTableEntry][uiMinSelector];
  236. float fTableEntryRange = s_aafModifierTable[uiTableEntry][uiMaxSelector] -
  237. s_aafModifierTable[uiTableEntry][uiMinSelector];
  238. float fCenterRatio = fTableEntryCenter / fTableEntryRange;
  239. float fCenter = fMinGrn + fCenterRatio*fGrnRange;
  240. fCenter = roundf(255.0f * fCenter) / 255.0f;
  241. float fMinBase = fCenter - (a_fBaseRadius / 255.0f);
  242. if (fMinBase < 0.0f)
  243. {
  244. fMinBase = 0.0f;
  245. }
  246. float fMaxBase = fCenter + (a_fBaseRadius / 255.0f);
  247. if (fMaxBase > 1.0f)
  248. {
  249. fMaxBase = 1.0f;
  250. }
  251. for (float fBase = fMinBase; fBase <= fMaxBase; fBase += (0.999999f / 255.0f))
  252. {
  253. float fRangeMultiplier = roundf(fGrnRange / fTableEntryRange);
  254. float fMinMultiplier = fRangeMultiplier - a_fMultiplierRadius;
  255. if (fMinMultiplier < 1.0f)
  256. {
  257. fMinMultiplier = 0.0f;
  258. }
  259. else if (fMinMultiplier > 15.0f)
  260. {
  261. fMinMultiplier = 15.0f;
  262. }
  263. float fMaxMultiplier = fRangeMultiplier + a_fMultiplierRadius;
  264. if (fMaxMultiplier < 1.0f)
  265. {
  266. fMaxMultiplier = 1.0f;
  267. }
  268. else if (fMaxMultiplier > 15.0f)
  269. {
  270. fMaxMultiplier = 15.0f;
  271. }
  272. for (float fMultiplier = fMinMultiplier; fMultiplier <= fMaxMultiplier; fMultiplier += 1.0f)
  273. {
  274. // find best selector for each pixel
  275. unsigned int auiBestSelectors[PIXELS];
  276. float afBestGrnError[PIXELS];
  277. float afBestPixelGrn[PIXELS];
  278. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  279. {
  280. float fBestPixelGrnError = FLT_MAX;
  281. for (unsigned int uiSelector = 0; uiSelector < SELECTORS; uiSelector++)
  282. {
  283. //DecodePixelRed is not red channel specific
  284. float fPixelGrn = DecodePixelRed(fBase * 255.0f, fMultiplier, uiTableEntry, uiSelector);
  285. ColorFloatRGBA frgba(m_pafrgbaSource[uiPixel].fR, fPixelGrn, 0.0f, 1.0f);
  286. float fPixelGrnError = CalcPixelError(frgba, 1.0f, m_pafrgbaSource[uiPixel]);
  287. if (fPixelGrnError < fBestPixelGrnError)
  288. {
  289. fBestPixelGrnError = fPixelGrnError;
  290. auiBestSelectors[uiPixel] = uiSelector;
  291. afBestGrnError[uiPixel] = fBestPixelGrnError;
  292. afBestPixelGrn[uiPixel] = fPixelGrn;
  293. }
  294. }
  295. }
  296. float fBlockError = 0.0f;
  297. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  298. {
  299. fBlockError += afBestGrnError[uiPixel];
  300. }
  301. if (fBlockError < m_fGrnBlockError)
  302. {
  303. m_fGrnBlockError = fBlockError;
  304. if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
  305. {
  306. m_fGrnBase = 255.0f * fBase;
  307. }
  308. else if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
  309. {
  310. m_fGrnBase = (fBase * 255) - 128;
  311. }
  312. else
  313. {
  314. assert(0);
  315. }
  316. m_fGrnMultiplier = fMultiplier;
  317. m_uiGrnModifierTableIndex = uiTableEntry;
  318. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  319. {
  320. m_auiGrnSelectors[uiPixel] = auiBestSelectors[uiPixel];
  321. m_afrgbaDecodedColors[uiPixel].fG = afBestPixelGrn[uiPixel];
  322. m_afDecodedAlphas[uiPixel] = 1.0f;
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. // ----------------------------------------------------------------------------------------------------
  331. // set the encoding bits based on encoding state
  332. //
  333. void Block4x4Encoding_RG11::SetEncodingBits(void)
  334. {
  335. unsigned long long int ulliSelectorBitsR = 0;
  336. unsigned long long int ulliSelectorBitsG = 0;
  337. for (unsigned int uiPixel = 0; uiPixel < PIXELS; uiPixel++)
  338. {
  339. unsigned int uiShift = 45 - (3 * uiPixel);
  340. ulliSelectorBitsR |= ((unsigned long long int)m_auiRedSelectors[uiPixel]) << uiShift;
  341. ulliSelectorBitsG |= ((unsigned long long int)m_auiGrnSelectors[uiPixel]) << uiShift;
  342. }
  343. if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
  344. {
  345. m_pencodingbitsRG11->data.baseR = (unsigned char)roundf(m_fRedBase);
  346. }
  347. else if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
  348. {
  349. m_pencodingbitsRG11->data.baseR = (signed char)roundf(m_fRedBase);
  350. }
  351. else
  352. {
  353. assert(0);
  354. }
  355. m_pencodingbitsRG11->data.tableIndexR = m_uiRedModifierTableIndex;
  356. m_pencodingbitsRG11->data.multiplierR = (unsigned char)roundf(m_fRedMultiplier);
  357. m_pencodingbitsRG11->data.selectorsR0 = ulliSelectorBitsR >> 40;
  358. m_pencodingbitsRG11->data.selectorsR1 = ulliSelectorBitsR >> 32;
  359. m_pencodingbitsRG11->data.selectorsR2 = ulliSelectorBitsR >> 24;
  360. m_pencodingbitsRG11->data.selectorsR3 = ulliSelectorBitsR >> 16;
  361. m_pencodingbitsRG11->data.selectorsR4 = ulliSelectorBitsR >> 8;
  362. m_pencodingbitsRG11->data.selectorsR5 = ulliSelectorBitsR;
  363. if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::RG11)
  364. {
  365. m_pencodingbitsRG11->data.baseG = (unsigned char)roundf(m_fGrnBase);
  366. }
  367. else if (m_pblockParent->GetImageSource()->GetFormat() == Image::Format::SIGNED_RG11)
  368. {
  369. m_pencodingbitsRG11->data.baseG = (signed char)roundf(m_fGrnBase);
  370. }
  371. else
  372. {
  373. assert(0);
  374. }
  375. m_pencodingbitsRG11->data.tableIndexG = m_uiGrnModifierTableIndex;
  376. m_pencodingbitsRG11->data.multiplierG = (unsigned char)roundf(m_fGrnMultiplier);
  377. m_pencodingbitsRG11->data.selectorsG0 = ulliSelectorBitsG >> 40;
  378. m_pencodingbitsRG11->data.selectorsG1 = ulliSelectorBitsG >> 32;
  379. m_pencodingbitsRG11->data.selectorsG2 = ulliSelectorBitsG >> 24;
  380. m_pencodingbitsRG11->data.selectorsG3 = ulliSelectorBitsG >> 16;
  381. m_pencodingbitsRG11->data.selectorsG4 = ulliSelectorBitsG >> 8;
  382. m_pencodingbitsRG11->data.selectorsG5 = ulliSelectorBitsG;
  383. }
  384. // ----------------------------------------------------------------------------------------------------
  385. //
  386. }