Image.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 <SDL_surface.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. unsigned 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. unsigned lpSurface_; // Do not define as a void pointer, as it is 8 bytes in a 64bit build
  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. default:
  174. // Unknown format
  175. return false;
  176. }
  177. }
  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::SavePNG(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 PNG");
  482. return false;
  483. }
  484. if (data_)
  485. return stbi_write_png(fileName.CString(), width_, height_, components_, data_.Get(), 0) != 0;
  486. else
  487. return false;
  488. }
  489. bool Image::SaveTGA(const String& fileName)
  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 TGA");
  500. return false;
  501. }
  502. if (data_)
  503. return stbi_write_tga(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  504. else
  505. return false;
  506. }
  507. bool Image::SaveJPG(const String & fileName, int quality)
  508. {
  509. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  510. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  511. {
  512. LOGERROR("Access denied to " + fileName);
  513. return false;
  514. }
  515. if (IsCompressed())
  516. {
  517. LOGERROR("Can not save compressed image to JPG");
  518. return false;
  519. }
  520. if (data_)
  521. return jo_write_jpg(fileName.CString(), data_.Get(), width_, height_, components_, quality) != 0;
  522. else
  523. return false;
  524. }
  525. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  526. {
  527. unsigned dataSize = source.GetSize();
  528. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  529. source.Read(buffer.Get(), dataSize);
  530. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int *)&components, 0);
  531. }
  532. void Image::FreeImageData(unsigned char* pixelData)
  533. {
  534. if (!pixelData)
  535. return;
  536. stbi_image_free(pixelData);
  537. }
  538. SharedPtr<Image> Image::GetNextLevel() const
  539. {
  540. if (IsCompressed())
  541. {
  542. LOGERROR("Can not generate mip level from compressed data");
  543. return SharedPtr<Image>();
  544. }
  545. if (components_ < 1 || components_ > 4)
  546. {
  547. LOGERROR("Illegal number of image components for mip level generation");
  548. return SharedPtr<Image>();
  549. }
  550. int widthOut = width_ / 2;
  551. int heightOut = height_ / 2;
  552. if (widthOut < 1)
  553. widthOut = 1;
  554. if (heightOut < 1)
  555. heightOut = 1;
  556. SharedPtr<Image> mipImage(new Image(context_));
  557. mipImage->SetSize(widthOut, heightOut, components_);
  558. const unsigned char* pixelDataIn = data_.Get();
  559. unsigned char* pixelDataOut = mipImage->data_.Get();
  560. // 1D case
  561. if (height_ == 1 || width_ == 1)
  562. {
  563. // Loop using the larger dimension
  564. if (widthOut < heightOut)
  565. widthOut = heightOut;
  566. switch (components_)
  567. {
  568. case 1:
  569. for (int x = 0; x < widthOut; ++x)
  570. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+1]) >> 1;
  571. break;
  572. case 2:
  573. for (int x = 0; x < widthOut*2; x += 2)
  574. {
  575. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+2]) >> 1;
  576. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+3]) >> 1;
  577. }
  578. break;
  579. case 3:
  580. for (int x = 0; x < widthOut*3; x += 3)
  581. {
  582. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+3]) >> 1;
  583. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+4]) >> 1;
  584. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+5]) >> 1;
  585. }
  586. break;
  587. case 4:
  588. for (int x = 0; x < widthOut*4; x += 4)
  589. {
  590. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+4]) >> 1;
  591. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+5]) >> 1;
  592. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+6]) >> 1;
  593. pixelDataOut[x+3] = ((unsigned)pixelDataIn[x*2+3] + pixelDataIn[x*2+7]) >> 1;
  594. }
  595. break;
  596. }
  597. }
  598. // 2D case
  599. else
  600. {
  601. switch (components_)
  602. {
  603. case 1:
  604. for (int y = 0; y < heightOut; ++y)
  605. {
  606. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_];
  607. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_];
  608. unsigned char* out = &pixelDataOut[y*widthOut];
  609. for (int x = 0; x < widthOut; ++x)
  610. {
  611. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+1] + inLower[x*2] + inLower[x*2+1]) >> 2;
  612. }
  613. }
  614. break;
  615. case 2:
  616. for (int y = 0; y < heightOut; ++y)
  617. {
  618. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*2];
  619. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*2];
  620. unsigned char* out = &pixelDataOut[y*widthOut*2];
  621. for (int x = 0; x < widthOut*2; x += 2)
  622. {
  623. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+2] + inLower[x*2] + inLower[x*2+2]) >> 2;
  624. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+3] + inLower[x*2+1] + inLower[x*2+3]) >> 2;
  625. }
  626. }
  627. break;
  628. case 3:
  629. for (int y = 0; y < heightOut; ++y)
  630. {
  631. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*3];
  632. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*3];
  633. unsigned char* out = &pixelDataOut[y*widthOut*3];
  634. for (int x = 0; x < widthOut*3; x += 3)
  635. {
  636. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+3] + inLower[x*2] + inLower[x*2+3]) >> 2;
  637. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+4] + inLower[x*2+1] + inLower[x*2+4]) >> 2;
  638. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+5] + inLower[x*2+2] + inLower[x*2+5]) >> 2;
  639. }
  640. }
  641. break;
  642. case 4:
  643. for (int y = 0; y < heightOut; ++y)
  644. {
  645. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*4];
  646. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*4];
  647. unsigned char* out = &pixelDataOut[y*widthOut*4];
  648. for (int x = 0; x < widthOut*4; x += 4)
  649. {
  650. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+4] + inLower[x*2] + inLower[x*2+4]) >> 2;
  651. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+5] + inLower[x*2+1] + inLower[x*2+5]) >> 2;
  652. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+6] + inLower[x*2+2] + inLower[x*2+6]) >> 2;
  653. out[x+3] = ((unsigned)inUpper[x*2+3] + inUpper[x*2+7] + inLower[x*2+3] + inLower[x*2+7]) >> 2;
  654. }
  655. }
  656. break;
  657. }
  658. }
  659. return mipImage;
  660. }
  661. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  662. {
  663. CompressedLevel level;
  664. if (compressedFormat_ == CF_NONE)
  665. {
  666. LOGERROR("Image is not compressed");
  667. return level;
  668. }
  669. if (index >= numCompressedLevels_)
  670. {
  671. LOGERROR("Compressed image mip level out of bounds");
  672. return level;
  673. }
  674. level.format_ = compressedFormat_;
  675. level.width_ = width_;
  676. level.height_ = height_;
  677. if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  678. {
  679. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  680. unsigned i = 0;
  681. unsigned offset = 0;
  682. for (;;)
  683. {
  684. if (!level.width_)
  685. level.width_ = 1;
  686. if (!level.height_)
  687. level.height_ = 1;
  688. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  689. level.rows_ = ((level.height_ + 3) / 4);
  690. level.data_ = data_.Get() + offset;
  691. level.dataSize_ = level.rows_ * level.rowSize_;
  692. if (offset + level.dataSize_ > GetMemoryUse())
  693. {
  694. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  695. " Datasize: " + String(GetMemoryUse()));
  696. level.data_ = 0;
  697. return level;
  698. }
  699. if (i == index)
  700. return level;
  701. offset += level.dataSize_;
  702. level.width_ /= 2;
  703. level.height_ /= 2;
  704. ++i;
  705. }
  706. }
  707. else
  708. {
  709. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  710. unsigned i = 0;
  711. unsigned offset = 0;
  712. for (;;)
  713. {
  714. if (!level.width_)
  715. level.width_ = 1;
  716. if (!level.height_)
  717. level.height_ = 1;
  718. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  719. int dataHeight = Max(level.height_, 8);
  720. level.data_ = data_.Get() + offset;
  721. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  722. level.rows_ = dataHeight;
  723. level.rowSize_ = level.dataSize_ / level.rows_;
  724. if (offset + level.dataSize_ > GetMemoryUse())
  725. {
  726. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  727. " Datasize: " + String(GetMemoryUse()));
  728. level.data_ = 0;
  729. return level;
  730. }
  731. if (i == index)
  732. return level;
  733. offset += level.dataSize_;
  734. level.width_ /= 2;
  735. level.height_ /= 2;
  736. ++i;
  737. }
  738. }
  739. }
  740. SDL_Surface* Image::GetSDLSurface(const IntRect& rect) const
  741. {
  742. if (!data_)
  743. return 0;
  744. if (IsCompressed())
  745. {
  746. LOGERROR("Can not get SDL surface from compressed image " + GetName());
  747. return 0;
  748. }
  749. if (components_ < 3)
  750. {
  751. LOGERROR("Can not get SDL surface from image " + GetName() + " with less than 3 components");
  752. return 0;
  753. }
  754. IntRect imageRect = rect;
  755. // Use full image if illegal rect
  756. if (imageRect.left_ < 0 || imageRect.top_ < 0 || imageRect.right_ > width_ || imageRect.bottom_ > height_ ||
  757. imageRect.left_ >= imageRect.right_ || imageRect.top_ >= imageRect.bottom_)
  758. {
  759. imageRect.left_ = 0;
  760. imageRect.top_ = 0;
  761. imageRect.right_ = width_;
  762. imageRect.bottom_ = height_;
  763. }
  764. int imageWidth = width_;
  765. int width = imageRect.Width();
  766. int height = imageRect.Height();
  767. // Assume little-endian for all the supported platforms
  768. unsigned rMask = 0x000000ff;
  769. unsigned gMask = 0x0000ff00;
  770. unsigned bMask = 0x00ff0000;
  771. unsigned aMask = 0xff000000;
  772. SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, components_ * 8, rMask, gMask, bMask, aMask);
  773. if (surface)
  774. {
  775. SDL_LockSurface(surface);
  776. unsigned char* destination = reinterpret_cast<unsigned char*>(surface->pixels);
  777. unsigned char* source = data_ + components_ * (imageWidth * imageRect.top_ + imageRect.left_);
  778. for (int i = 0; i < height; ++i)
  779. {
  780. memcpy(destination, source, components_ * width);
  781. destination += surface->pitch;
  782. source += components_ * imageWidth;
  783. }
  784. SDL_UnlockSurface(surface);
  785. }
  786. else
  787. LOGERROR("Failed to create SDL surface from image " + GetName());
  788. return surface;
  789. }
  790. }