EtcFile.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #ifdef _WIN32
  17. #define _CRT_SECURE_NO_WARNINGS (1)
  18. #endif
  19. #include "EtcConfig.h"
  20. #include "EtcFile.h"
  21. #include "EtcFileHeader.h"
  22. #include "EtcColor.h"
  23. #include "Etc.h"
  24. #include "EtcBlock4x4EncodingBits.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <stdlib.h>
  29. using namespace Etc;
  30. // ----------------------------------------------------------------------------------------------------
  31. //
  32. File::File(const char *a_pstrFilename, Format a_fileformat, Image::Format a_imageformat,
  33. unsigned char *a_paucEncodingBits, unsigned int a_uiEncodingBitsBytes,
  34. unsigned int a_uiSourceWidth, unsigned int a_uiSourceHeight,
  35. unsigned int a_uiExtendedWidth, unsigned int a_uiExtendedHeight)
  36. {
  37. if (a_pstrFilename == nullptr)
  38. {
  39. m_pstrFilename = const_cast<char *>("");
  40. }
  41. else
  42. {
  43. m_pstrFilename = new char[strlen(a_pstrFilename) + 1];
  44. strcpy(m_pstrFilename, a_pstrFilename);
  45. }
  46. m_fileformat = a_fileformat;
  47. if (m_fileformat == Format::INFER_FROM_FILE_EXTENSION)
  48. {
  49. // ***** TODO: add this later *****
  50. m_fileformat = Format::KTX;
  51. }
  52. m_imageformat = a_imageformat;
  53. m_uiNumMipmaps = 1;
  54. m_pMipmapImages = new RawImage[m_uiNumMipmaps];
  55. m_pMipmapImages[0].paucEncodingBits = std::shared_ptr<unsigned char>(a_paucEncodingBits, [](unsigned char *p) { delete[] p; } );
  56. m_pMipmapImages[0].uiEncodingBitsBytes = a_uiEncodingBitsBytes;
  57. m_pMipmapImages[0].uiExtendedWidth = a_uiExtendedWidth;
  58. m_pMipmapImages[0].uiExtendedHeight = a_uiExtendedHeight;
  59. m_uiSourceWidth = a_uiSourceWidth;
  60. m_uiSourceHeight = a_uiSourceHeight;
  61. switch (m_fileformat)
  62. {
  63. case Format::PKM:
  64. m_pheader = new FileHeader_Pkm(this);
  65. break;
  66. case Format::KTX:
  67. m_pheader = new FileHeader_Ktx(this);
  68. break;
  69. default:
  70. assert(0);
  71. break;
  72. }
  73. }
  74. // ----------------------------------------------------------------------------------------------------
  75. //
  76. File::File(const char *a_pstrFilename, Format a_fileformat, Image::Format a_imageformat,
  77. unsigned int a_uiNumMipmaps, RawImage *a_pMipmapImages,
  78. unsigned int a_uiSourceWidth, unsigned int a_uiSourceHeight)
  79. {
  80. if (a_pstrFilename == nullptr)
  81. {
  82. m_pstrFilename = const_cast<char *>("");
  83. }
  84. else
  85. {
  86. m_pstrFilename = new char[strlen(a_pstrFilename) + 1];
  87. strcpy(m_pstrFilename, a_pstrFilename);
  88. }
  89. m_fileformat = a_fileformat;
  90. if (m_fileformat == Format::INFER_FROM_FILE_EXTENSION)
  91. {
  92. // ***** TODO: add this later *****
  93. m_fileformat = Format::KTX;
  94. }
  95. m_imageformat = a_imageformat;
  96. m_uiNumMipmaps = a_uiNumMipmaps;
  97. m_pMipmapImages = new RawImage[m_uiNumMipmaps];
  98. for(unsigned int mip = 0; mip < m_uiNumMipmaps; mip++)
  99. {
  100. m_pMipmapImages[mip] = a_pMipmapImages[mip];
  101. }
  102. m_uiSourceWidth = a_uiSourceWidth;
  103. m_uiSourceHeight = a_uiSourceHeight;
  104. switch (m_fileformat)
  105. {
  106. case Format::PKM:
  107. m_pheader = new FileHeader_Pkm(this);
  108. break;
  109. case Format::KTX:
  110. m_pheader = new FileHeader_Ktx(this);
  111. break;
  112. default:
  113. assert(0);
  114. break;
  115. }
  116. }
  117. // ----------------------------------------------------------------------------------------------------
  118. //
  119. File::File(const char *a_pstrFilename, Format a_fileformat)
  120. {
  121. if (a_pstrFilename == nullptr)
  122. {
  123. return;
  124. }
  125. else
  126. {
  127. m_pstrFilename = new char[strlen(a_pstrFilename) + 1];
  128. strcpy(m_pstrFilename, a_pstrFilename);
  129. }
  130. m_fileformat = a_fileformat;
  131. if (m_fileformat == Format::INFER_FROM_FILE_EXTENSION)
  132. {
  133. // ***** TODO: add this later *****
  134. m_fileformat = Format::KTX;
  135. }
  136. FILE *pfile = fopen(m_pstrFilename, "rb");
  137. if (pfile == nullptr)
  138. {
  139. printf("ERROR: Couldn't open %s", m_pstrFilename);
  140. exit(1);
  141. }
  142. fseek(pfile, 0, SEEK_END);
  143. unsigned int fileSize = ftell(pfile);
  144. fseek(pfile, 0, SEEK_SET);
  145. size_t szResult;
  146. m_pheader = new FileHeader_Ktx(this);
  147. szResult = fread( ((FileHeader_Ktx*)m_pheader)->GetData(), 1, sizeof(FileHeader_Ktx::Data), pfile);
  148. assert(szResult > 0);
  149. m_uiNumMipmaps = 1;
  150. m_pMipmapImages = new RawImage[m_uiNumMipmaps];
  151. if (((FileHeader_Ktx*)m_pheader)->GetData()->m_u32BytesOfKeyValueData > 0)
  152. fseek(pfile, ((FileHeader_Ktx*)m_pheader)->GetData()->m_u32BytesOfKeyValueData, SEEK_CUR);
  153. szResult = fread(&m_pMipmapImages->uiEncodingBitsBytes, 1, sizeof(unsigned int), pfile);
  154. assert(szResult > 0);
  155. m_pMipmapImages->paucEncodingBits = std::shared_ptr<unsigned char>(new unsigned char[m_pMipmapImages->uiEncodingBitsBytes], [](unsigned char *p) { delete[] p; } );
  156. assert(ftell(pfile) + m_pMipmapImages->uiEncodingBitsBytes <= fileSize);
  157. szResult = fread(m_pMipmapImages->paucEncodingBits.get(), 1, m_pMipmapImages->uiEncodingBitsBytes, pfile);
  158. assert(szResult == m_pMipmapImages->uiEncodingBitsBytes);
  159. uint32_t uiInternalFormat = ((FileHeader_Ktx*)m_pheader)->GetData()->m_u32GlInternalFormat;
  160. uint32_t uiBaseInternalFormat = ((FileHeader_Ktx*)m_pheader)->GetData()->m_u32GlBaseInternalFormat;
  161. if (uiInternalFormat == (uint32_t)FileHeader_Ktx::InternalFormat::ETC1_RGB8 && uiBaseInternalFormat == (uint32_t)FileHeader_Ktx::BaseInternalFormat::ETC1_RGB8)
  162. {
  163. m_imageformat = Image::Format::ETC1;
  164. }
  165. else if (uiInternalFormat == (uint32_t)FileHeader_Ktx::InternalFormat::ETC2_RGB8 && uiBaseInternalFormat == (uint32_t)FileHeader_Ktx::BaseInternalFormat::ETC2_RGB8)
  166. {
  167. m_imageformat = Image::Format::RGB8;
  168. }
  169. else if (uiInternalFormat == (uint32_t)FileHeader_Ktx::InternalFormat::ETC2_RGB8A1 && uiBaseInternalFormat == (uint32_t)FileHeader_Ktx::BaseInternalFormat::ETC2_RGB8A1)
  170. {
  171. m_imageformat = Image::Format::RGB8A1;
  172. }
  173. else if (uiInternalFormat == (uint32_t)FileHeader_Ktx::InternalFormat::ETC2_RGBA8 && uiBaseInternalFormat == (uint32_t)FileHeader_Ktx::BaseInternalFormat::ETC2_RGBA8)
  174. {
  175. m_imageformat = Image::Format::RGBA8;
  176. }
  177. else if (uiInternalFormat == (uint32_t)FileHeader_Ktx::InternalFormat::ETC2_R11 && uiBaseInternalFormat == (uint32_t)FileHeader_Ktx::BaseInternalFormat::ETC2_R11)
  178. {
  179. m_imageformat = Image::Format::R11;
  180. }
  181. else if (uiInternalFormat == (uint32_t)FileHeader_Ktx::InternalFormat::ETC2_SIGNED_R11 && uiBaseInternalFormat == (uint32_t)FileHeader_Ktx::BaseInternalFormat::ETC2_R11)
  182. {
  183. m_imageformat = Image::Format::SIGNED_R11;
  184. }
  185. else if (uiInternalFormat == (uint32_t)FileHeader_Ktx::InternalFormat::ETC2_RG11 && uiBaseInternalFormat == (uint32_t)FileHeader_Ktx::BaseInternalFormat::ETC2_RG11)
  186. {
  187. m_imageformat = Image::Format::RG11;
  188. }
  189. else if (uiInternalFormat == (uint32_t)FileHeader_Ktx::InternalFormat::ETC2_SIGNED_RG11 && uiBaseInternalFormat == (uint32_t)FileHeader_Ktx::BaseInternalFormat::ETC2_RG11)
  190. {
  191. m_imageformat = Image::Format::SIGNED_RG11;
  192. }
  193. else
  194. {
  195. m_imageformat = Image::Format::UNKNOWN;
  196. }
  197. m_uiSourceWidth = ((FileHeader_Ktx*)m_pheader)->GetData()->m_u32PixelWidth;
  198. m_uiSourceHeight = ((FileHeader_Ktx*)m_pheader)->GetData()->m_u32PixelHeight;
  199. m_pMipmapImages->uiExtendedWidth = Image::CalcExtendedDimension((unsigned short)m_uiSourceWidth);
  200. m_pMipmapImages->uiExtendedHeight = Image::CalcExtendedDimension((unsigned short)m_uiSourceHeight);
  201. unsigned int uiBlocks = m_pMipmapImages->uiExtendedWidth * m_pMipmapImages->uiExtendedHeight / 16;
  202. Block4x4EncodingBits::Format encodingbitsformat = Image::DetermineEncodingBitsFormat(m_imageformat);
  203. unsigned int expectedbytes = uiBlocks * Block4x4EncodingBits::GetBytesPerBlock(encodingbitsformat);
  204. assert(expectedbytes == m_pMipmapImages->uiEncodingBitsBytes);
  205. fclose(pfile);
  206. }
  207. File::~File()
  208. {
  209. if (m_pMipmapImages != nullptr)
  210. {
  211. delete [] m_pMipmapImages;
  212. }
  213. if(m_pstrFilename != nullptr)
  214. {
  215. delete[] m_pstrFilename;
  216. m_pstrFilename = nullptr;
  217. }
  218. if (m_pheader != nullptr)
  219. {
  220. delete m_pheader;
  221. m_pheader = nullptr;
  222. }
  223. }
  224. void File::UseSingleBlock(int a_iPixelX, int a_iPixelY)
  225. {
  226. if (a_iPixelX <= -1 || a_iPixelY <= -1)
  227. return;
  228. if (a_iPixelX >(int) m_uiSourceWidth)
  229. {
  230. //if we are using a ktx thats the size of a single block or less
  231. //then make sure we use the 4x4 image as the single block
  232. if (m_uiSourceWidth <= 4)
  233. {
  234. a_iPixelX = 0;
  235. }
  236. else
  237. {
  238. printf("blockAtHV: H coordinate out of range, capped to image width\n");
  239. a_iPixelX = m_uiSourceWidth - 1;
  240. }
  241. }
  242. if (a_iPixelY >(int) m_uiSourceHeight)
  243. {
  244. //if we are using a ktx thats the size of a single block or less
  245. //then make sure we use the 4x4 image as the single block
  246. if (m_uiSourceHeight <= 4)
  247. {
  248. a_iPixelY= 0;
  249. }
  250. else
  251. {
  252. printf("blockAtHV: V coordinate out of range, capped to image height\n");
  253. a_iPixelY = m_uiSourceHeight - 1;
  254. }
  255. }
  256. unsigned int origWidth = m_uiSourceWidth;
  257. unsigned int origHeight = m_uiSourceHeight;
  258. m_uiSourceWidth = 4;
  259. m_uiSourceHeight = 4;
  260. Block4x4EncodingBits::Format encodingbitsformat = Image::DetermineEncodingBitsFormat(m_imageformat);
  261. unsigned int uiEncodingBitsBytesPerBlock = Block4x4EncodingBits::GetBytesPerBlock(encodingbitsformat);
  262. int numMipmaps = 1;
  263. RawImage* pMipmapImages = new RawImage[numMipmaps];
  264. pMipmapImages[0].uiExtendedWidth = Image::CalcExtendedDimension((unsigned short)m_uiSourceWidth);
  265. pMipmapImages[0].uiExtendedHeight = Image::CalcExtendedDimension((unsigned short)m_uiSourceHeight);
  266. pMipmapImages[0].uiEncodingBitsBytes = 0;
  267. pMipmapImages[0].paucEncodingBits = std::shared_ptr<unsigned char>(new unsigned char[uiEncodingBitsBytesPerBlock], [](unsigned char *p) { delete[] p; });
  268. //block position in pixels
  269. // remove the bottom 2 bits to get the block coordinates
  270. unsigned int iBlockPosX = (a_iPixelX & 0xFFFFFFFC);
  271. unsigned int iBlockPosY = (a_iPixelY & 0xFFFFFFFC);
  272. int numXBlocks = (origWidth / 4);
  273. int numYBlocks = (origHeight / 4);
  274. // block location
  275. //int iBlockX = (a_iPixelX % 4) == 0 ? a_iPixelX / 4.0f : (a_iPixelX / 4) + 1;
  276. //int iBlockY = (a_iPixelY % 4) == 0 ? a_iPixelY / 4.0f : (a_iPixelY / 4) + 1;
  277. //m_paucEncodingBits += ((iBlockY * numXBlocks) + iBlockX) * uiEncodingBitsBytesPerBlock;
  278. unsigned int num = numXBlocks*numYBlocks;
  279. unsigned int uiH = 0, uiV = 0;
  280. unsigned char* pEncodingBits = m_pMipmapImages[0].paucEncodingBits.get();
  281. for (unsigned int uiBlock = 0; uiBlock < num; uiBlock++)
  282. {
  283. if (uiH == iBlockPosX && uiV == iBlockPosY)
  284. {
  285. memcpy(pMipmapImages[0].paucEncodingBits.get(),pEncodingBits, uiEncodingBitsBytesPerBlock);
  286. break;
  287. }
  288. pEncodingBits += uiEncodingBitsBytesPerBlock;
  289. uiH += 4;
  290. if (uiH >= origWidth)
  291. {
  292. uiH = 0;
  293. uiV += 4;
  294. }
  295. }
  296. delete [] m_pMipmapImages;
  297. m_pMipmapImages = pMipmapImages;
  298. }
  299. // ----------------------------------------------------------------------------------------------------
  300. //
  301. void File::Write()
  302. {
  303. FILE *pfile = fopen(m_pstrFilename, "wb");
  304. if (pfile == nullptr)
  305. {
  306. printf("Error: couldn't open Etc file (%s)\n", m_pstrFilename);
  307. exit(1);
  308. }
  309. m_pheader->Write(pfile);
  310. for(unsigned int mip = 0; mip < m_uiNumMipmaps; mip++)
  311. {
  312. if(m_fileformat == Format::KTX)
  313. {
  314. // Write u32 image size
  315. uint32_t u32ImageSize = m_pMipmapImages[mip].uiEncodingBitsBytes;
  316. uint32_t szBytesWritten = fwrite(&u32ImageSize, 1, sizeof(u32ImageSize), pfile);
  317. assert(szBytesWritten == sizeof(u32ImageSize));
  318. }
  319. unsigned int iResult = (int)fwrite(m_pMipmapImages[mip].paucEncodingBits.get(), 1, m_pMipmapImages[mip].uiEncodingBitsBytes, pfile);
  320. if (iResult != m_pMipmapImages[mip].uiEncodingBitsBytes)
  321. {
  322. printf("Error: couldn't write Etc file (%s)\n", m_pstrFilename);
  323. exit(1);
  324. }
  325. }
  326. fclose(pfile);
  327. }
  328. // ----------------------------------------------------------------------------------------------------
  329. //