Image.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Decompress.h"
  25. #include "File.h"
  26. #include "FileSystem.h"
  27. #include "Log.h"
  28. #include <cstring>
  29. #include <stb_image.h>
  30. #include <stb_image_write.h>
  31. #include <jo_jpeg.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. unsigned 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. unsigned lpSurface_; // Do not define as a void pointer, as it is 8 bytes in a 64bit build
  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. default:
  173. // Unknown format
  174. return false;
  175. }
  176. }
  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::FlipVertical()
  434. {
  435. if (!data_)
  436. return;
  437. if (IsCompressed())
  438. {
  439. LOGERROR("Can not flip a compressed image");
  440. return;
  441. }
  442. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  443. unsigned rowSize = width_ * components_;
  444. for (int y = 0; y < height_; ++y)
  445. memcpy(&newData[(height_ - y - 1) * rowSize], &data_[y * rowSize], rowSize);
  446. data_ = newData;
  447. }
  448. void Image::SetData(const unsigned char* pixelData)
  449. {
  450. memcpy(data_.Get(), pixelData, width_ * height_ * components_);
  451. }
  452. bool Image::SaveBMP(const String& fileName)
  453. {
  454. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  455. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  456. {
  457. LOGERROR("Access denied to " + fileName);
  458. return false;
  459. }
  460. if (IsCompressed())
  461. {
  462. LOGERROR("Can not save compressed image to BMP");
  463. return false;
  464. }
  465. if (data_)
  466. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  467. else
  468. return false;
  469. }
  470. bool Image::SavePNG(const String& fileName)
  471. {
  472. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  473. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  474. {
  475. LOGERROR("Access denied to " + fileName);
  476. return false;
  477. }
  478. if (IsCompressed())
  479. {
  480. LOGERROR("Can not save compressed image to PNG");
  481. return false;
  482. }
  483. if (data_)
  484. return stbi_write_png(fileName.CString(), width_, height_, components_, data_.Get(), 0) != 0;
  485. else
  486. return false;
  487. }
  488. bool Image::SaveTGA(const String& fileName)
  489. {
  490. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  491. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  492. {
  493. LOGERROR("Access denied to " + fileName);
  494. return false;
  495. }
  496. if (IsCompressed())
  497. {
  498. LOGERROR("Can not save compressed image to TGA");
  499. return false;
  500. }
  501. if (data_)
  502. return stbi_write_tga(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  503. else
  504. return false;
  505. }
  506. bool Image::SaveJPG(const String & fileName, int quality)
  507. {
  508. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  509. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  510. {
  511. LOGERROR("Access denied to " + fileName);
  512. return false;
  513. }
  514. if (IsCompressed())
  515. {
  516. LOGERROR("Can not save compressed image to JPG");
  517. return false;
  518. }
  519. if (data_)
  520. return jo_write_jpg(fileName.CString(), data_.Get(), width_, height_, components_, quality) != 0;
  521. else
  522. return false;
  523. }
  524. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  525. {
  526. unsigned dataSize = source.GetSize();
  527. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  528. source.Read(buffer.Get(), dataSize);
  529. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int *)&components, 0);
  530. }
  531. void Image::FreeImageData(unsigned char* pixelData)
  532. {
  533. if (!pixelData)
  534. return;
  535. stbi_image_free(pixelData);
  536. }
  537. SharedPtr<Image> Image::GetNextLevel() const
  538. {
  539. if (IsCompressed())
  540. {
  541. LOGERROR("Can not generate mip level from compressed data");
  542. return SharedPtr<Image>();
  543. }
  544. if (components_ < 1 || components_ > 4)
  545. {
  546. LOGERROR("Illegal number of image components for mip level generation");
  547. return SharedPtr<Image>();
  548. }
  549. int widthOut = width_ / 2;
  550. int heightOut = height_ / 2;
  551. if (widthOut < 1)
  552. widthOut = 1;
  553. if (heightOut < 1)
  554. heightOut = 1;
  555. SharedPtr<Image> mipImage(new Image(context_));
  556. mipImage->SetSize(widthOut, heightOut, components_);
  557. const unsigned char* pixelDataIn = data_.Get();
  558. unsigned char* pixelDataOut = mipImage->data_.Get();
  559. // 1D case
  560. if (height_ == 1 || width_ == 1)
  561. {
  562. // Loop using the larger dimension
  563. if (widthOut < heightOut)
  564. widthOut = heightOut;
  565. switch (components_)
  566. {
  567. case 1:
  568. for (int x = 0; x < widthOut; ++x)
  569. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+1]) >> 1;
  570. break;
  571. case 2:
  572. for (int x = 0; x < widthOut*2; x += 2)
  573. {
  574. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+2]) >> 1;
  575. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+3]) >> 1;
  576. }
  577. break;
  578. case 3:
  579. for (int x = 0; x < widthOut*3; x += 3)
  580. {
  581. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+3]) >> 1;
  582. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+4]) >> 1;
  583. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+5]) >> 1;
  584. }
  585. break;
  586. case 4:
  587. for (int x = 0; x < widthOut*4; x += 4)
  588. {
  589. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+4]) >> 1;
  590. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+5]) >> 1;
  591. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+6]) >> 1;
  592. pixelDataOut[x+3] = ((unsigned)pixelDataIn[x*2+3] + pixelDataIn[x*2+7]) >> 1;
  593. }
  594. break;
  595. }
  596. }
  597. // 2D case
  598. else
  599. {
  600. switch (components_)
  601. {
  602. case 1:
  603. for (int y = 0; y < heightOut; ++y)
  604. {
  605. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_];
  606. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_];
  607. unsigned char* out = &pixelDataOut[y*widthOut];
  608. for (int x = 0; x < widthOut; ++x)
  609. {
  610. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+1] + inLower[x*2] + inLower[x*2+1]) >> 2;
  611. }
  612. }
  613. break;
  614. case 2:
  615. for (int y = 0; y < heightOut; ++y)
  616. {
  617. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*2];
  618. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*2];
  619. unsigned char* out = &pixelDataOut[y*widthOut*2];
  620. for (int x = 0; x < widthOut*2; x += 2)
  621. {
  622. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+2] + inLower[x*2] + inLower[x*2+2]) >> 2;
  623. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+3] + inLower[x*2+1] + inLower[x*2+3]) >> 2;
  624. }
  625. }
  626. break;
  627. case 3:
  628. for (int y = 0; y < heightOut; ++y)
  629. {
  630. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*3];
  631. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*3];
  632. unsigned char* out = &pixelDataOut[y*widthOut*3];
  633. for (int x = 0; x < widthOut*3; x += 3)
  634. {
  635. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+3] + inLower[x*2] + inLower[x*2+3]) >> 2;
  636. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+4] + inLower[x*2+1] + inLower[x*2+4]) >> 2;
  637. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+5] + inLower[x*2+2] + inLower[x*2+5]) >> 2;
  638. }
  639. }
  640. break;
  641. case 4:
  642. for (int y = 0; y < heightOut; ++y)
  643. {
  644. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*4];
  645. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*4];
  646. unsigned char* out = &pixelDataOut[y*widthOut*4];
  647. for (int x = 0; x < widthOut*4; x += 4)
  648. {
  649. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+4] + inLower[x*2] + inLower[x*2+4]) >> 2;
  650. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+5] + inLower[x*2+1] + inLower[x*2+5]) >> 2;
  651. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+6] + inLower[x*2+2] + inLower[x*2+6]) >> 2;
  652. out[x+3] = ((unsigned)inUpper[x*2+3] + inUpper[x*2+7] + inLower[x*2+3] + inLower[x*2+7]) >> 2;
  653. }
  654. }
  655. break;
  656. }
  657. }
  658. return mipImage;
  659. }
  660. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  661. {
  662. CompressedLevel level;
  663. if (compressedFormat_ == CF_NONE)
  664. {
  665. LOGERROR("Image is not compressed");
  666. return level;
  667. }
  668. if (index >= numCompressedLevels_)
  669. {
  670. LOGERROR("Compressed image mip level out of bounds");
  671. return level;
  672. }
  673. level.format_ = compressedFormat_;
  674. level.width_ = width_;
  675. level.height_ = height_;
  676. if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  677. {
  678. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  679. unsigned i = 0;
  680. unsigned offset = 0;
  681. for (;;)
  682. {
  683. if (!level.width_)
  684. level.width_ = 1;
  685. if (!level.height_)
  686. level.height_ = 1;
  687. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  688. level.rows_ = ((level.height_ + 3) / 4);
  689. level.data_ = data_.Get() + offset;
  690. level.dataSize_ = level.rows_ * level.rowSize_;
  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. else
  707. {
  708. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  709. unsigned i = 0;
  710. unsigned offset = 0;
  711. for (;;)
  712. {
  713. if (!level.width_)
  714. level.width_ = 1;
  715. if (!level.height_)
  716. level.height_ = 1;
  717. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  718. int dataHeight = Max(level.height_, 8);
  719. level.data_ = data_.Get() + offset;
  720. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  721. level.rows_ = dataHeight;
  722. level.rowSize_ = level.dataSize_ / level.rows_;
  723. if (offset + level.dataSize_ > GetMemoryUse())
  724. {
  725. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  726. " Datasize: " + String(GetMemoryUse()));
  727. level.data_ = 0;
  728. return level;
  729. }
  730. if (i == index)
  731. return level;
  732. offset += level.dataSize_;
  733. level.width_ /= 2;
  734. level.height_ /= 2;
  735. ++i;
  736. }
  737. }
  738. }
  739. }