EtcImage.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #pragma once
  17. //#include "Etc.h"
  18. #include "EtcColorFloatRGBA.h"
  19. #include "EtcBlock4x4EncodingBits.h"
  20. #include "EtcErrorMetric.h"
  21. namespace Etc
  22. {
  23. class Block4x4;
  24. class EncoderSpec;
  25. class SortedBlockList;
  26. class Image
  27. {
  28. public:
  29. //the differnt warning and errors that can come up during encoding
  30. enum EncodingStatus
  31. {
  32. SUCCESS = 0,
  33. //
  34. WARNING_THRESHOLD = 1 << 0,
  35. //
  36. WARNING_EFFORT_OUT_OF_RANGE = 1 << 1,
  37. WARNING_JOBS_OUT_OF_RANGE = 1 << 2,
  38. WARNING_SOME_NON_OPAQUE_PIXELS = 1 << 3,//just for opaque formats, etc1, rgb8, r11, rg11
  39. WARNING_ALL_OPAQUE_PIXELS = 1 << 4,
  40. WARNING_ALL_TRANSPARENT_PIXELS = 1 << 5,
  41. WARNING_SOME_TRANSLUCENT_PIXELS = 1 << 6,//just for rgb8A1
  42. WARNING_SOME_RGBA_NOT_0_TO_1 = 1 << 7,
  43. WARNING_SOME_BLUE_VALUES_ARE_NOT_ZERO = 1 << 8,
  44. WARNING_SOME_GREEN_VALUES_ARE_NOT_ZERO = 1 << 9,
  45. //
  46. ERROR_THRESHOLD = 1 << 16,
  47. //
  48. ERROR_UNKNOWN_FORMAT = 1 << 17,
  49. ERROR_UNKNOWN_ERROR_METRIC = 1 << 18,
  50. ERROR_ZERO_WIDTH_OR_HEIGHT = 1 << 19,
  51. //
  52. };
  53. enum class Format
  54. {
  55. UNKNOWN,
  56. //
  57. ETC1,
  58. //
  59. // ETC2 formats
  60. RGB8,
  61. SRGB8,
  62. RGBA8,
  63. SRGBA8,
  64. R11,
  65. SIGNED_R11,
  66. RG11,
  67. SIGNED_RG11,
  68. RGB8A1,
  69. SRGB8A1,
  70. //
  71. FORMATS,
  72. //
  73. DEFAULT = SRGB8
  74. };
  75. // constructor using source image
  76. Image(float *a_pafSourceRGBA, unsigned int a_uiSourceWidth,
  77. unsigned int a_uiSourceHeight,
  78. ErrorMetric a_errormetric);
  79. // constructor using encoding bits
  80. Image(Format a_format,
  81. unsigned int a_uiSourceWidth, unsigned int a_uiSourceHeight,
  82. unsigned char *a_paucEncidingBits, unsigned int a_uiEncodingBitsBytes,
  83. Image *a_pimageSource,
  84. ErrorMetric a_errormetric);
  85. ~Image(void);
  86. EncodingStatus Encode(Format a_format, ErrorMetric a_errormetric, float a_fEffort,
  87. unsigned int a_uiJobs, unsigned int a_uiMaxJobs);
  88. inline void AddToEncodingStatus(EncodingStatus a_encStatus)
  89. {
  90. m_encodingStatus = (EncodingStatus)((unsigned int)m_encodingStatus | (unsigned int)a_encStatus);
  91. }
  92. inline unsigned int GetSourceWidth(void)
  93. {
  94. return m_uiSourceWidth;
  95. }
  96. inline unsigned int GetSourceHeight(void)
  97. {
  98. return m_uiSourceHeight;
  99. }
  100. inline unsigned int GetExtendedWidth(void)
  101. {
  102. return m_uiExtendedWidth;
  103. }
  104. inline unsigned int GetExtendedHeight(void)
  105. {
  106. return m_uiExtendedHeight;
  107. }
  108. inline unsigned int GetNumberOfBlocks()
  109. {
  110. return m_uiBlockColumns * m_uiBlockRows;
  111. }
  112. inline Block4x4 * GetBlocks()
  113. {
  114. return m_pablock;
  115. }
  116. inline unsigned char * GetEncodingBits(void)
  117. {
  118. return m_paucEncodingBits;
  119. }
  120. inline unsigned int GetEncodingBitsBytes(void)
  121. {
  122. return m_uiEncodingBitsBytes;
  123. }
  124. inline int GetEncodingTimeMs(void)
  125. {
  126. return m_iEncodeTime_ms;
  127. }
  128. float GetError(void);
  129. inline ColorFloatRGBA * GetSourcePixel(unsigned int a_uiH, unsigned int a_uiV)
  130. {
  131. if (a_uiH >= m_uiSourceWidth || a_uiV >= m_uiSourceHeight)
  132. {
  133. return nullptr;
  134. }
  135. return &m_pafrgbaSource[a_uiV*m_uiSourceWidth + a_uiH];
  136. }
  137. inline Format GetFormat(void)
  138. {
  139. return m_format;
  140. }
  141. static Block4x4EncodingBits::Format DetermineEncodingBitsFormat(Format a_format);
  142. inline static unsigned short CalcExtendedDimension(unsigned short a_ushOriginalDimension)
  143. {
  144. return (unsigned short)((a_ushOriginalDimension + 3) & ~3);
  145. }
  146. inline ErrorMetric GetErrorMetric(void)
  147. {
  148. return m_errormetric;
  149. }
  150. static const char * EncodingFormatToString(Image::Format a_format);
  151. const char * EncodingFormatToString(void);
  152. //used to get basic information about the image data
  153. int m_iNumOpaquePixels;
  154. int m_iNumTranslucentPixels;
  155. int m_iNumTransparentPixels;
  156. ColorFloatRGBA m_numColorValues;
  157. ColorFloatRGBA m_numOutOfRangeValues;
  158. bool m_bVerboseOutput;
  159. private:
  160. //add a warning or error to check for while encoding
  161. inline void TrackEncodingWarning(EncodingStatus a_encStatus)
  162. {
  163. m_warningsToCapture = (EncodingStatus)((unsigned int)m_warningsToCapture | (unsigned int)a_encStatus);
  164. }
  165. //report the warning if it is something we care about for this encoding
  166. inline void AddToEncodingStatusIfSignfigant(EncodingStatus a_encStatus)
  167. {
  168. if ((EncodingStatus)((unsigned int)m_warningsToCapture & (unsigned int)a_encStatus) == a_encStatus)
  169. {
  170. AddToEncodingStatus(a_encStatus);
  171. }
  172. }
  173. Image(void);
  174. void FindEncodingWarningTypesForCurFormat();
  175. void FindAndSetEncodingWarnings();
  176. void InitBlocksAndBlockSorter(void);
  177. void RunFirstPass(unsigned int a_uiMultithreadingOffset,
  178. unsigned int a_uiMultithreadingStride);
  179. void SetEncodingBits(unsigned int a_uiMultithreadingOffset,
  180. unsigned int a_uiMultithreadingStride);
  181. unsigned int IterateThroughWorstBlocks(unsigned int a_uiMaxBlocks,
  182. unsigned int a_uiMultithreadingOffset,
  183. unsigned int a_uiMultithreadingStride);
  184. // inputs
  185. ColorFloatRGBA *m_pafrgbaSource;
  186. unsigned int m_uiSourceWidth;
  187. unsigned int m_uiSourceHeight;
  188. unsigned int m_uiExtendedWidth;
  189. unsigned int m_uiExtendedHeight;
  190. unsigned int m_uiBlockColumns;
  191. unsigned int m_uiBlockRows;
  192. // intermediate data
  193. Block4x4 *m_pablock;
  194. // encoding
  195. Format m_format;
  196. Block4x4EncodingBits::Format m_encodingbitsformat;
  197. unsigned int m_uiEncodingBitsBytes; // for entire image
  198. unsigned char *m_paucEncodingBits;
  199. ErrorMetric m_errormetric;
  200. float m_fEffort;
  201. // stats
  202. int m_iEncodeTime_ms;
  203. SortedBlockList *m_psortedblocklist;
  204. //this will hold any warning or errors that happen during encoding
  205. EncodingStatus m_encodingStatus;
  206. //these will be the warnings we are tracking
  207. EncodingStatus m_warningsToCapture;
  208. };
  209. } // namespace Etc