Image.cpp 24 KB

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