Image.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. 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. default:
  173. // Unknown format
  174. return false;
  175. }
  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::FlipVertical()
  435. {
  436. if (!data_)
  437. return;
  438. if (IsCompressed())
  439. {
  440. LOGERROR("Can not flip a compressed image");
  441. return;
  442. }
  443. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  444. unsigned rowSize = width_ * components_;
  445. for (int y = 0; y < height_; ++y)
  446. memcpy(&newData[(height_ - y - 1) * rowSize], &data_[y * rowSize], rowSize);
  447. data_ = newData;
  448. }
  449. void Image::SetData(const unsigned char* pixelData)
  450. {
  451. memcpy(data_.Get(), pixelData, width_ * height_ * components_);
  452. }
  453. bool Image::SaveBMP(const String& fileName)
  454. {
  455. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  456. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  457. {
  458. LOGERROR("Access denied to " + fileName);
  459. return false;
  460. }
  461. if (IsCompressed())
  462. {
  463. LOGERROR("Can not save compressed image to BMP");
  464. return false;
  465. }
  466. if (data_)
  467. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  468. else
  469. return false;
  470. }
  471. bool Image::SaveTGA(const String& fileName)
  472. {
  473. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  474. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  475. {
  476. LOGERROR("Access denied to " + fileName);
  477. return false;
  478. }
  479. if (IsCompressed())
  480. {
  481. LOGERROR("Can not save compressed image to TGA");
  482. return false;
  483. }
  484. if (data_)
  485. return stbi_write_tga(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  486. else
  487. return false;
  488. }
  489. bool Image::SaveJPG(const String & fileName, int quality)
  490. {
  491. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  492. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  493. {
  494. LOGERROR("Access denied to " + fileName);
  495. return false;
  496. }
  497. if (IsCompressed())
  498. {
  499. LOGERROR("Can not save compressed image to JPG");
  500. return false;
  501. }
  502. if (data_)
  503. return jo_write_jpg(fileName.CString(), data_.Get(), width_, height_, components_, quality) != 0;
  504. else
  505. return false;
  506. }
  507. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  508. {
  509. unsigned dataSize = source.GetSize();
  510. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  511. source.Read(buffer.Get(), dataSize);
  512. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int *)&components, 0);
  513. }
  514. void Image::FreeImageData(unsigned char* pixelData)
  515. {
  516. if (!pixelData)
  517. return;
  518. stbi_image_free(pixelData);
  519. }
  520. SharedPtr<Image> Image::GetNextLevel() const
  521. {
  522. if (IsCompressed())
  523. {
  524. LOGERROR("Can not generate mip level from compressed data");
  525. return SharedPtr<Image>();
  526. }
  527. if (components_ < 1 || components_ > 4)
  528. {
  529. LOGERROR("Illegal number of image components for mip level generation");
  530. return SharedPtr<Image>();
  531. }
  532. int widthOut = width_ / 2;
  533. int heightOut = height_ / 2;
  534. if (widthOut < 1)
  535. widthOut = 1;
  536. if (heightOut < 1)
  537. heightOut = 1;
  538. SharedPtr<Image> mipImage(new Image(context_));
  539. mipImage->SetSize(widthOut, heightOut, components_);
  540. const unsigned char* pixelDataIn = data_.Get();
  541. unsigned char* pixelDataOut = mipImage->data_.Get();
  542. // 1D case
  543. if (height_ == 1 || width_ == 1)
  544. {
  545. // Loop using the larger dimension
  546. if (widthOut < heightOut)
  547. widthOut = heightOut;
  548. switch (components_)
  549. {
  550. case 1:
  551. for (int x = 0; x < widthOut; ++x)
  552. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+1]) >> 1;
  553. break;
  554. case 2:
  555. for (int x = 0; x < widthOut*2; x += 2)
  556. {
  557. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+2]) >> 1;
  558. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+3]) >> 1;
  559. }
  560. break;
  561. case 3:
  562. for (int x = 0; x < widthOut*3; x += 3)
  563. {
  564. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+3]) >> 1;
  565. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+4]) >> 1;
  566. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+5]) >> 1;
  567. }
  568. break;
  569. case 4:
  570. for (int x = 0; x < widthOut*4; x += 4)
  571. {
  572. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+4]) >> 1;
  573. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+5]) >> 1;
  574. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+6]) >> 1;
  575. pixelDataOut[x+3] = ((unsigned)pixelDataIn[x*2+3] + pixelDataIn[x*2+7]) >> 1;
  576. }
  577. break;
  578. }
  579. }
  580. // 2D case
  581. else
  582. {
  583. switch (components_)
  584. {
  585. case 1:
  586. for (int y = 0; y < heightOut; ++y)
  587. {
  588. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_];
  589. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_];
  590. unsigned char* out = &pixelDataOut[y*widthOut];
  591. for (int x = 0; x < widthOut; ++x)
  592. {
  593. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+1] + inLower[x*2] + inLower[x*2+1]) >> 2;
  594. }
  595. }
  596. break;
  597. case 2:
  598. for (int y = 0; y < heightOut; ++y)
  599. {
  600. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*2];
  601. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*2];
  602. unsigned char* out = &pixelDataOut[y*widthOut*2];
  603. for (int x = 0; x < widthOut*2; x += 2)
  604. {
  605. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+2] + inLower[x*2] + inLower[x*2+2]) >> 2;
  606. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+3] + inLower[x*2+1] + inLower[x*2+3]) >> 2;
  607. }
  608. }
  609. break;
  610. case 3:
  611. for (int y = 0; y < heightOut; ++y)
  612. {
  613. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*3];
  614. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*3];
  615. unsigned char* out = &pixelDataOut[y*widthOut*3];
  616. for (int x = 0; x < widthOut*3; x += 3)
  617. {
  618. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+3] + inLower[x*2] + inLower[x*2+3]) >> 2;
  619. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+4] + inLower[x*2+1] + inLower[x*2+4]) >> 2;
  620. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+5] + inLower[x*2+2] + inLower[x*2+5]) >> 2;
  621. }
  622. }
  623. break;
  624. case 4:
  625. for (int y = 0; y < heightOut; ++y)
  626. {
  627. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*4];
  628. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*4];
  629. unsigned char* out = &pixelDataOut[y*widthOut*4];
  630. for (int x = 0; x < widthOut*4; x += 4)
  631. {
  632. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+4] + inLower[x*2] + inLower[x*2+4]) >> 2;
  633. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+5] + inLower[x*2+1] + inLower[x*2+5]) >> 2;
  634. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+6] + inLower[x*2+2] + inLower[x*2+6]) >> 2;
  635. out[x+3] = ((unsigned)inUpper[x*2+3] + inUpper[x*2+7] + inLower[x*2+3] + inLower[x*2+7]) >> 2;
  636. }
  637. }
  638. break;
  639. }
  640. }
  641. return mipImage;
  642. }
  643. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  644. {
  645. CompressedLevel level;
  646. if (compressedFormat_ == CF_NONE)
  647. {
  648. LOGERROR("Image is not compressed");
  649. return level;
  650. }
  651. if (index >= numCompressedLevels_)
  652. {
  653. LOGERROR("Compressed image mip level out of bounds");
  654. return level;
  655. }
  656. level.format_ = compressedFormat_;
  657. level.width_ = width_;
  658. level.height_ = height_;
  659. if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  660. {
  661. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  662. unsigned i = 0;
  663. unsigned offset = 0;
  664. for (;;)
  665. {
  666. if (!level.width_)
  667. level.width_ = 1;
  668. if (!level.height_)
  669. level.height_ = 1;
  670. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  671. level.rows_ = ((level.height_ + 3) / 4);
  672. level.data_ = data_.Get() + offset;
  673. level.dataSize_ = level.rows_ * level.rowSize_;
  674. if (offset + level.dataSize_ > GetMemoryUse())
  675. {
  676. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  677. " Datasize: " + String(GetMemoryUse()));
  678. level.data_ = 0;
  679. return level;
  680. }
  681. if (i == index)
  682. return level;
  683. offset += level.dataSize_;
  684. level.width_ /= 2;
  685. level.height_ /= 2;
  686. ++i;
  687. }
  688. }
  689. else
  690. {
  691. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  692. unsigned i = 0;
  693. unsigned offset = 0;
  694. for (;;)
  695. {
  696. if (!level.width_)
  697. level.width_ = 1;
  698. if (!level.height_)
  699. level.height_ = 1;
  700. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  701. int dataHeight = Max(level.height_, 8);
  702. level.data_ = data_.Get() + offset;
  703. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  704. level.rows_ = dataHeight;
  705. level.rowSize_ = level.dataSize_ / level.rows_;
  706. if (offset + level.dataSize_ > GetMemoryUse())
  707. {
  708. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  709. " Datasize: " + String(GetMemoryUse()));
  710. level.data_ = 0;
  711. return level;
  712. }
  713. if (i == index)
  714. return level;
  715. offset += level.dataSize_;
  716. level.width_ /= 2;
  717. level.height_ /= 2;
  718. ++i;
  719. }
  720. }
  721. }
  722. }