Image.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Decompress.h"
  26. #include "File.h"
  27. #include "FileSystem.h"
  28. #include "Log.h"
  29. #include <cstring>
  30. #include <stb_image.h>
  31. #include <stb_image_write.h>
  32. #include "DebugNew.h"
  33. #ifndef MAKEFOURCC
  34. #define MAKEFOURCC(ch0, ch1, ch2, ch3) ((unsigned)(ch0) | ((unsigned)(ch1) << 8) | ((unsigned)(ch2) << 16) | ((unsigned)(ch3) << 24))
  35. #endif
  36. #define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1'))
  37. #define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2'))
  38. #define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3'))
  39. #define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4'))
  40. #define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5'))
  41. namespace Urho3D
  42. {
  43. struct DDColorKey
  44. {
  45. unsigned dwColorSpaceLowValue_;
  46. unsigned dwColorSpaceHighValue_;
  47. };
  48. struct DDPixelFormat
  49. {
  50. unsigned dwSize_;
  51. unsigned dwFlags_;
  52. unsigned dwFourCC_;
  53. union
  54. {
  55. unsigned dwRGBBitCount_;
  56. unsigned dwYUVBitCount_;
  57. unsigned dwZBufferBitDepth_;
  58. unsigned dwAlphaBitDepth_;
  59. unsigned dwLuminanceBitCount_;
  60. unsigned dwBumpBitCount_;
  61. unsigned dwPrivateFormatBitCount_;
  62. };
  63. union
  64. {
  65. unsigned dwRBitMask_;
  66. unsigned dwYBitMask_;
  67. unsigned dwStencilBitDepth_;
  68. unsigned dwLuminanceBitMask_;
  69. unsigned dwBumpDuBitMask_;
  70. unsigned dwOperations_;
  71. };
  72. union
  73. {
  74. unsigned dwGBitMask_;
  75. unsigned dwUBitMask_;
  76. unsigned dwZBitMask_;
  77. unsigned dwBumpDvBitMask_;
  78. struct
  79. {
  80. unsigned short wFlipMSTypes_;
  81. unsigned short wBltMSTypes_;
  82. } multiSampleCaps_;
  83. };
  84. union
  85. {
  86. unsigned dwBBitMask_;
  87. unsigned dwVBitMask_;
  88. unsigned dwStencilBitMask_;
  89. unsigned dwBumpLuminanceBitMask_;
  90. };
  91. union
  92. {
  93. unsigned dwRGBAlphaBitMask_;
  94. unsigned dwYUVAlphaBitMask_;
  95. unsigned dwLuminanceAlphaBitMask_;
  96. unsigned dwRGBZBitMask_;
  97. unsigned dwYUVZBitMask_;
  98. };
  99. };
  100. struct DDSCaps2
  101. {
  102. unsigned dwCaps_;
  103. unsigned dwCaps2_;
  104. unsigned dwCaps3_;
  105. union
  106. {
  107. unsigned dwCaps4_;
  108. unsigned dwVolumeDepth_;
  109. };
  110. };
  111. struct DDSurfaceDesc2
  112. {
  113. unsigned dwSize_;
  114. unsigned dwFlags_;
  115. unsigned dwHeight_;
  116. unsigned dwWidth_;
  117. union
  118. {
  119. long lPitch_;
  120. unsigned dwLinearSize_;
  121. };
  122. union
  123. {
  124. unsigned dwBackBufferCount_;
  125. unsigned dwDepth_;
  126. };
  127. union
  128. {
  129. unsigned dwMipMapCount_;
  130. unsigned dwRefreshRate_;
  131. unsigned dwSrcVBHandle_;
  132. };
  133. unsigned dwAlphaBitDepth_;
  134. unsigned dwReserved_;
  135. void* lpSurface_;
  136. union
  137. {
  138. DDColorKey ddckCKDestOverlay_;
  139. unsigned dwEmptyFaceColor_;
  140. };
  141. DDColorKey ddckCKDestBlt_;
  142. DDColorKey ddckCKSrcOverlay_;
  143. DDColorKey ddckCKSrcBlt_;
  144. union
  145. {
  146. DDPixelFormat ddpfPixelFormat_;
  147. unsigned dwFVF_;
  148. };
  149. DDSCaps2 ddsCaps_;
  150. unsigned dwTextureStage_;
  151. };
  152. bool CompressedLevel::Decompress(unsigned char* dest)
  153. {
  154. if (!data_)
  155. return false;
  156. switch (format_)
  157. {
  158. case CF_DXT1:
  159. case CF_DXT3:
  160. case CF_DXT5:
  161. DecompressImageDXT(dest, data_, width_, height_, format_);
  162. return true;
  163. case CF_ETC1:
  164. DecompressImageETC(dest, data_, width_, height_);
  165. return true;
  166. case CF_PVRTC_RGB_2BPP:
  167. case CF_PVRTC_RGBA_2BPP:
  168. case CF_PVRTC_RGB_4BPP:
  169. case CF_PVRTC_RGBA_4BPP:
  170. DecompressImagePVRTC(dest, data_, width_, height_, format_);
  171. return true;
  172. }
  173. // Unknown format
  174. return false;
  175. }
  176. OBJECTTYPESTATIC(Image);
  177. Image::Image(Context* context) :
  178. Resource(context),
  179. width_(0),
  180. height_(0),
  181. components_(0)
  182. {
  183. }
  184. Image::~Image()
  185. {
  186. }
  187. void Image::RegisterObject(Context* context)
  188. {
  189. context->RegisterFactory<Image>();
  190. }
  191. bool Image::Load(Deserializer& source)
  192. {
  193. // Check for DDS, KTX or PVR compressed format
  194. String fileID = source.ReadFileID();
  195. if (fileID == "DDS ")
  196. {
  197. // DDS compressed format
  198. DDSurfaceDesc2 ddsd;
  199. source.Read(&ddsd, sizeof(ddsd));
  200. switch (ddsd.ddpfPixelFormat_.dwFourCC_)
  201. {
  202. case FOURCC_DXT1:
  203. compressedFormat_ = CF_DXT1;
  204. components_ = 3;
  205. break;
  206. case FOURCC_DXT3:
  207. compressedFormat_ = CF_DXT3;
  208. components_ = 4;
  209. break;
  210. case FOURCC_DXT5:
  211. compressedFormat_ = CF_DXT5;
  212. components_ = 4;
  213. break;
  214. default:
  215. LOGERROR("Unsupported DDS format");
  216. return false;
  217. }
  218. unsigned dataSize = source.GetSize() - source.GetPosition();
  219. data_ = new unsigned char[dataSize];
  220. width_ = ddsd.dwWidth_;
  221. height_ = ddsd.dwHeight_;
  222. numCompressedLevels_ = ddsd.dwMipMapCount_;
  223. if (!numCompressedLevels_)
  224. numCompressedLevels_ = 1;
  225. SetMemoryUse(dataSize);
  226. source.Read(data_.Get(), dataSize);
  227. }
  228. else if (fileID == "\253KTX")
  229. {
  230. source.Seek(12);
  231. unsigned endianness = source.ReadUInt();
  232. unsigned type = source.ReadUInt();
  233. unsigned typeSize = source.ReadUInt();
  234. unsigned format = source.ReadUInt();
  235. unsigned internalFormat = source.ReadUInt();
  236. unsigned baseInternalFormat = source.ReadUInt();
  237. unsigned width = source.ReadUInt();
  238. unsigned height = source.ReadUInt();
  239. unsigned depth = source.ReadUInt();
  240. unsigned arrayElements = source.ReadUInt();
  241. unsigned faces = source.ReadUInt();
  242. unsigned mipmaps = source.ReadUInt();
  243. unsigned keyValueBytes = source.ReadUInt();
  244. if (endianness != 0x04030201)
  245. {
  246. LOGERROR("Big-endian KTX files not supported");
  247. return false;
  248. }
  249. if (type != 0 || format != 0)
  250. {
  251. LOGERROR("Uncompressed KTX files not supported");
  252. return false;
  253. }
  254. if (faces > 1 || depth > 1)
  255. {
  256. LOGERROR("3D or cube KTX files not supported");
  257. return false;
  258. }
  259. if (mipmaps == 0)
  260. {
  261. LOGERROR("KTX files without explicitly specified mipmap count not supported");
  262. return false;
  263. }
  264. compressedFormat_ = CF_NONE;
  265. switch (internalFormat)
  266. {
  267. case 0x83f1:
  268. compressedFormat_ = CF_DXT1;
  269. components_ = 4;
  270. break;
  271. case 0x83f2:
  272. compressedFormat_ = CF_DXT3;
  273. components_ = 4;
  274. break;
  275. case 0x83f3:
  276. compressedFormat_ = CF_DXT5;
  277. components_ = 4;
  278. break;
  279. case 0x8d64:
  280. compressedFormat_ = CF_ETC1;
  281. components_ = 3;
  282. break;
  283. case 0x8c00:
  284. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  285. components_ = 3;
  286. break;
  287. case 0x8c01:
  288. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  289. components_ = 3;
  290. break;
  291. case 0x8c02:
  292. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  293. components_ = 4;
  294. break;
  295. case 0x8c03:
  296. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  297. components_ = 4;
  298. break;
  299. }
  300. if (compressedFormat_ == CF_NONE)
  301. {
  302. LOGERROR("Unsupported texture format in KTX file");
  303. return false;
  304. }
  305. source.Seek(source.GetPosition() + keyValueBytes);
  306. unsigned dataSize = source.GetSize() - source.GetPosition() - mipmaps * sizeof(unsigned);
  307. data_ = new unsigned char[dataSize];
  308. width_ = width;
  309. height_ = height;
  310. numCompressedLevels_ = mipmaps;
  311. unsigned dataOffset = 0;
  312. for (unsigned i = 0; i < mipmaps; ++i)
  313. {
  314. unsigned levelSize = source.ReadUInt();
  315. if (levelSize + dataOffset > dataSize)
  316. {
  317. LOGERROR("KTX mipmap level data size exceeds file size");
  318. return false;
  319. }
  320. source.Read(&data_[dataOffset], levelSize);
  321. dataOffset += levelSize;
  322. if (source.GetPosition() & 3)
  323. source.Seek((source.GetPosition() + 3) & 0xfffffffc);
  324. }
  325. SetMemoryUse(dataSize);
  326. }
  327. else if (fileID == "PVR\3")
  328. {
  329. unsigned flags = source.ReadUInt();
  330. unsigned pixelFormatLo = source.ReadUInt();
  331. unsigned pixelFormatHi = source.ReadUInt();
  332. unsigned colourSpace = source.ReadUInt();
  333. unsigned channelType = source.ReadUInt();
  334. unsigned height = source.ReadUInt();
  335. unsigned width = source.ReadUInt();
  336. unsigned depth = source.ReadUInt();
  337. unsigned numSurfaces = source.ReadUInt();
  338. unsigned numFaces = source.ReadUInt();
  339. unsigned mipmapCount = source.ReadUInt();
  340. unsigned metaDataSize = source.ReadUInt();
  341. if (depth > 1 || numFaces > 1)
  342. {
  343. LOGERROR("3D or cube PVR files not supported");
  344. return false;
  345. }
  346. if (mipmapCount == 0)
  347. {
  348. LOGERROR("PVR files without explicitly specified mipmap count not supported");
  349. return false;
  350. }
  351. compressedFormat_ = CF_NONE;
  352. switch (pixelFormatLo)
  353. {
  354. case 0:
  355. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  356. components_ = 3;
  357. break;
  358. case 1:
  359. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  360. components_ = 4;
  361. break;
  362. case 2:
  363. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  364. components_ = 3;
  365. break;
  366. case 3:
  367. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  368. components_ = 4;
  369. break;
  370. case 6:
  371. compressedFormat_ = CF_ETC1;
  372. components_ = 3;
  373. break;
  374. case 7:
  375. compressedFormat_ = CF_DXT1;
  376. components_ = 4;
  377. break;
  378. case 9:
  379. compressedFormat_ = CF_DXT3;
  380. components_ = 4;
  381. break;
  382. case 11:
  383. compressedFormat_ = CF_DXT5;
  384. components_ = 4;
  385. break;
  386. }
  387. if (compressedFormat_ == CF_NONE)
  388. {
  389. LOGERROR("Unsupported texture format in PVR file");
  390. return false;
  391. }
  392. source.Seek(source.GetPosition() + metaDataSize);
  393. unsigned dataSize = source.GetSize() - source.GetPosition();
  394. data_ = new unsigned char[dataSize];
  395. width_ = width;
  396. height_ = height;
  397. numCompressedLevels_ = mipmapCount;
  398. source.Read(data_.Get(), dataSize);
  399. SetMemoryUse(dataSize);
  400. }
  401. else
  402. {
  403. // Not DDS, KTX or PVR, use STBImage to load other image formats as uncompressed
  404. source.Seek(0);
  405. int width, height;
  406. unsigned components;
  407. unsigned char* pixelData = GetImageData(source, width, height, components);
  408. if (!pixelData)
  409. {
  410. LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  411. return false;
  412. }
  413. SetSize(width, height, components);
  414. SetData(pixelData);
  415. FreeImageData(pixelData);
  416. }
  417. return true;
  418. }
  419. void Image::SetSize(int width, int height, unsigned components)
  420. {
  421. if (width == width_ && height == height_ && components == components_)
  422. return;
  423. if (width <= 0 || height <= 0)
  424. return;
  425. data_ = new unsigned char[width * height * components];
  426. width_ = width;
  427. height_ = height;
  428. components_ = components;
  429. compressedFormat_ = CF_NONE;
  430. numCompressedLevels_ = 0;
  431. SetMemoryUse(width * height * components);
  432. }
  433. void Image::SetData(const unsigned char* pixelData)
  434. {
  435. memcpy(data_.Get(), pixelData, width_ * height_ * components_);
  436. }
  437. bool Image::SaveBMP(const String& fileName)
  438. {
  439. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  440. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  441. {
  442. LOGERROR("Access denied to " + fileName);
  443. return false;
  444. }
  445. if (IsCompressed())
  446. {
  447. LOGERROR("Can not save compressed image to BMP");
  448. return false;
  449. }
  450. if (data_)
  451. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  452. else
  453. return false;
  454. }
  455. bool Image::SaveTGA(const String& fileName)
  456. {
  457. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  458. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  459. {
  460. LOGERROR("Access denied to " + fileName);
  461. return false;
  462. }
  463. if (IsCompressed())
  464. {
  465. LOGERROR("Can not save compressed image to TGA");
  466. return false;
  467. }
  468. if (data_)
  469. return stbi_write_tga(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  470. else
  471. return false;
  472. }
  473. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  474. {
  475. unsigned dataSize = source.GetSize();
  476. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  477. source.Read(buffer.Get(), dataSize);
  478. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int *)&components, 0);
  479. }
  480. void Image::FreeImageData(unsigned char* pixelData)
  481. {
  482. if (!pixelData)
  483. return;
  484. stbi_image_free(pixelData);
  485. }
  486. SharedPtr<Image> Image::GetNextLevel() const
  487. {
  488. if (IsCompressed())
  489. {
  490. LOGERROR("Can not generate mip level from compressed data");
  491. return SharedPtr<Image>();
  492. }
  493. if (components_ < 1 || components_ > 4)
  494. {
  495. LOGERROR("Illegal number of image components for mip level generation");
  496. return SharedPtr<Image>();
  497. }
  498. int widthOut = width_ / 2;
  499. int heightOut = height_ / 2;
  500. if (widthOut < 1)
  501. widthOut = 1;
  502. if (heightOut < 1)
  503. heightOut = 1;
  504. SharedPtr<Image> mipImage(new Image(context_));
  505. mipImage->SetSize(widthOut, heightOut, components_);
  506. const unsigned char* pixelDataIn = data_.Get();
  507. unsigned char* pixelDataOut = mipImage->data_.Get();
  508. // 1D case
  509. if (height_ == 1 || width_ == 1)
  510. {
  511. // Loop using the larger dimension
  512. if (widthOut < heightOut)
  513. widthOut = heightOut;
  514. switch (components_)
  515. {
  516. case 1:
  517. for (int x = 0; x < widthOut; ++x)
  518. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+1]) >> 1;
  519. break;
  520. case 2:
  521. for (int x = 0; x < widthOut*2; x += 2)
  522. {
  523. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+2]) >> 1;
  524. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+3]) >> 1;
  525. }
  526. break;
  527. case 3:
  528. for (int x = 0; x < widthOut*3; x += 3)
  529. {
  530. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+3]) >> 1;
  531. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+4]) >> 1;
  532. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+5]) >> 1;
  533. }
  534. break;
  535. case 4:
  536. for (int x = 0; x < widthOut*4; x += 4)
  537. {
  538. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+4]) >> 1;
  539. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+5]) >> 1;
  540. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+6]) >> 1;
  541. pixelDataOut[x+3] = ((unsigned)pixelDataIn[x*2+3] + pixelDataIn[x*2+7]) >> 1;
  542. }
  543. break;
  544. }
  545. }
  546. // 2D case
  547. else
  548. {
  549. switch (components_)
  550. {
  551. case 1:
  552. for (int y = 0; y < heightOut; ++y)
  553. {
  554. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_];
  555. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_];
  556. unsigned char* out = &pixelDataOut[y*widthOut];
  557. for (int x = 0; x < widthOut; ++x)
  558. {
  559. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+1] + inLower[x*2] + inLower[x*2+1]) >> 2;
  560. }
  561. }
  562. break;
  563. case 2:
  564. for (int y = 0; y < heightOut; ++y)
  565. {
  566. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*2];
  567. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*2];
  568. unsigned char* out = &pixelDataOut[y*widthOut*2];
  569. for (int x = 0; x < widthOut*2; x += 2)
  570. {
  571. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+2] + inLower[x*2] + inLower[x*2+2]) >> 2;
  572. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+3] + inLower[x*2+1] + inLower[x*2+3]) >> 2;
  573. }
  574. }
  575. break;
  576. case 3:
  577. for (int y = 0; y < heightOut; ++y)
  578. {
  579. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*3];
  580. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*3];
  581. unsigned char* out = &pixelDataOut[y*widthOut*3];
  582. for (int x = 0; x < widthOut*3; x += 3)
  583. {
  584. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+3] + inLower[x*2] + inLower[x*2+3]) >> 2;
  585. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+4] + inLower[x*2+1] + inLower[x*2+4]) >> 2;
  586. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+5] + inLower[x*2+2] + inLower[x*2+5]) >> 2;
  587. }
  588. }
  589. break;
  590. case 4:
  591. for (int y = 0; y < heightOut; ++y)
  592. {
  593. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*4];
  594. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*4];
  595. unsigned char* out = &pixelDataOut[y*widthOut*4];
  596. for (int x = 0; x < widthOut*4; x += 4)
  597. {
  598. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+4] + inLower[x*2] + inLower[x*2+4]) >> 2;
  599. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+5] + inLower[x*2+1] + inLower[x*2+5]) >> 2;
  600. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+6] + inLower[x*2+2] + inLower[x*2+6]) >> 2;
  601. out[x+3] = ((unsigned)inUpper[x*2+3] + inUpper[x*2+7] + inLower[x*2+3] + inLower[x*2+7]) >> 2;
  602. }
  603. }
  604. break;
  605. }
  606. }
  607. return mipImage;
  608. }
  609. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  610. {
  611. CompressedLevel level;
  612. if (compressedFormat_ == CF_NONE)
  613. {
  614. LOGERROR("Image is not compressed");
  615. return level;
  616. }
  617. if (index >= numCompressedLevels_)
  618. {
  619. LOGERROR("Compressed image mip level out of bounds");
  620. return level;
  621. }
  622. level.format_ = compressedFormat_;
  623. level.width_ = width_;
  624. level.height_ = height_;
  625. if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  626. {
  627. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  628. unsigned i = 0;
  629. unsigned offset = 0;
  630. for (;;)
  631. {
  632. if (!level.width_)
  633. level.width_ = 1;
  634. if (!level.height_)
  635. level.height_ = 1;
  636. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  637. level.rows_ = ((level.height_ + 3) / 4);
  638. level.data_ = data_.Get() + offset;
  639. level.dataSize_ = level.rows_ * level.rowSize_;
  640. if (offset + level.dataSize_ > GetMemoryUse())
  641. {
  642. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  643. " Datasize: " + String(GetMemoryUse()));
  644. level.data_ = 0;
  645. return level;
  646. }
  647. if (i == index)
  648. return level;
  649. offset += level.dataSize_;
  650. level.width_ /= 2;
  651. level.height_ /= 2;
  652. ++i;
  653. }
  654. }
  655. else
  656. {
  657. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  658. unsigned i = 0;
  659. unsigned offset = 0;
  660. for (;;)
  661. {
  662. if (!level.width_)
  663. level.width_ = 1;
  664. if (!level.height_)
  665. level.height_ = 1;
  666. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  667. int dataHeight = Max(level.height_, 8);
  668. level.data_ = data_.Get() + offset;
  669. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  670. level.rows_ = dataHeight;
  671. level.rowSize_ = level.dataSize_ / level.rows_;
  672. if (offset + level.dataSize_ > GetMemoryUse())
  673. {
  674. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  675. " Datasize: " + String(GetMemoryUse()));
  676. level.data_ = 0;
  677. return level;
  678. }
  679. if (i == index)
  680. return level;
  681. offset += level.dataSize_;
  682. level.width_ /= 2;
  683. level.height_ /= 2;
  684. ++i;
  685. }
  686. }
  687. }
  688. }