Image.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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_, depth_, 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. depth_(0),
  183. components_(0)
  184. {
  185. }
  186. Image::~Image()
  187. {
  188. }
  189. void Image::RegisterObject(Context* context)
  190. {
  191. context->RegisterFactory<Image>();
  192. }
  193. bool Image::Load(Deserializer& source)
  194. {
  195. // Check for DDS, KTX or PVR compressed format
  196. String fileID = source.ReadFileID();
  197. if (fileID == "DDS ")
  198. {
  199. // DDS compressed format
  200. DDSurfaceDesc2 ddsd;
  201. source.Read(&ddsd, sizeof(ddsd));
  202. switch (ddsd.ddpfPixelFormat_.dwFourCC_)
  203. {
  204. case FOURCC_DXT1:
  205. compressedFormat_ = CF_DXT1;
  206. components_ = 3;
  207. break;
  208. case FOURCC_DXT3:
  209. compressedFormat_ = CF_DXT3;
  210. components_ = 4;
  211. break;
  212. case FOURCC_DXT5:
  213. compressedFormat_ = CF_DXT5;
  214. components_ = 4;
  215. break;
  216. default:
  217. LOGERROR("Unsupported DDS format");
  218. return false;
  219. }
  220. unsigned dataSize = source.GetSize() - source.GetPosition();
  221. data_ = new unsigned char[dataSize];
  222. width_ = ddsd.dwWidth_;
  223. height_ = ddsd.dwHeight_;
  224. depth_ = ddsd.dwDepth_;
  225. numCompressedLevels_ = ddsd.dwMipMapCount_;
  226. if (!numCompressedLevels_)
  227. numCompressedLevels_ = 1;
  228. SetMemoryUse(dataSize);
  229. source.Read(data_.Get(), dataSize);
  230. }
  231. else if (fileID == "\253KTX")
  232. {
  233. source.Seek(12);
  234. unsigned endianness = source.ReadUInt();
  235. unsigned type = source.ReadUInt();
  236. /* unsigned typeSize = */ source.ReadUInt();
  237. unsigned format = source.ReadUInt();
  238. unsigned internalFormat = source.ReadUInt();
  239. /* unsigned baseInternalFormat = */ source.ReadUInt();
  240. unsigned width = source.ReadUInt();
  241. unsigned height = source.ReadUInt();
  242. unsigned depth = source.ReadUInt();
  243. /* unsigned arrayElements = */ source.ReadUInt();
  244. unsigned faces = source.ReadUInt();
  245. unsigned mipmaps = source.ReadUInt();
  246. unsigned keyValueBytes = source.ReadUInt();
  247. if (endianness != 0x04030201)
  248. {
  249. LOGERROR("Big-endian KTX files not supported");
  250. return false;
  251. }
  252. if (type != 0 || format != 0)
  253. {
  254. LOGERROR("Uncompressed KTX files not supported");
  255. return false;
  256. }
  257. if (faces > 1 || depth > 1)
  258. {
  259. LOGERROR("3D or cube KTX files not supported");
  260. return false;
  261. }
  262. if (mipmaps == 0)
  263. {
  264. LOGERROR("KTX files without explicitly specified mipmap count not supported");
  265. return false;
  266. }
  267. compressedFormat_ = CF_NONE;
  268. switch (internalFormat)
  269. {
  270. case 0x83f1:
  271. compressedFormat_ = CF_DXT1;
  272. components_ = 4;
  273. break;
  274. case 0x83f2:
  275. compressedFormat_ = CF_DXT3;
  276. components_ = 4;
  277. break;
  278. case 0x83f3:
  279. compressedFormat_ = CF_DXT5;
  280. components_ = 4;
  281. break;
  282. case 0x8d64:
  283. compressedFormat_ = CF_ETC1;
  284. components_ = 3;
  285. break;
  286. case 0x8c00:
  287. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  288. components_ = 3;
  289. break;
  290. case 0x8c01:
  291. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  292. components_ = 3;
  293. break;
  294. case 0x8c02:
  295. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  296. components_ = 4;
  297. break;
  298. case 0x8c03:
  299. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  300. components_ = 4;
  301. break;
  302. }
  303. if (compressedFormat_ == CF_NONE)
  304. {
  305. LOGERROR("Unsupported texture format in KTX file");
  306. return false;
  307. }
  308. source.Seek(source.GetPosition() + keyValueBytes);
  309. unsigned dataSize = source.GetSize() - source.GetPosition() - mipmaps * sizeof(unsigned);
  310. data_ = new unsigned char[dataSize];
  311. width_ = width;
  312. height_ = height;
  313. numCompressedLevels_ = mipmaps;
  314. unsigned dataOffset = 0;
  315. for (unsigned i = 0; i < mipmaps; ++i)
  316. {
  317. unsigned levelSize = source.ReadUInt();
  318. if (levelSize + dataOffset > dataSize)
  319. {
  320. LOGERROR("KTX mipmap level data size exceeds file size");
  321. return false;
  322. }
  323. source.Read(&data_[dataOffset], levelSize);
  324. dataOffset += levelSize;
  325. if (source.GetPosition() & 3)
  326. source.Seek((source.GetPosition() + 3) & 0xfffffffc);
  327. }
  328. SetMemoryUse(dataSize);
  329. }
  330. else if (fileID == "PVR\3")
  331. {
  332. /* unsigned flags = */ source.ReadUInt();
  333. unsigned pixelFormatLo = source.ReadUInt();
  334. /* unsigned pixelFormatHi = */ source.ReadUInt();
  335. /* unsigned colourSpace = */ source.ReadUInt();
  336. /* unsigned channelType = */ source.ReadUInt();
  337. unsigned height = source.ReadUInt();
  338. unsigned width = source.ReadUInt();
  339. unsigned depth = source.ReadUInt();
  340. /* unsigned numSurfaces = */ source.ReadUInt();
  341. unsigned numFaces = source.ReadUInt();
  342. unsigned mipmapCount = source.ReadUInt();
  343. unsigned metaDataSize = source.ReadUInt();
  344. if (depth > 1 || numFaces > 1)
  345. {
  346. LOGERROR("3D or cube PVR files not supported");
  347. return false;
  348. }
  349. if (mipmapCount == 0)
  350. {
  351. LOGERROR("PVR files without explicitly specified mipmap count not supported");
  352. return false;
  353. }
  354. compressedFormat_ = CF_NONE;
  355. switch (pixelFormatLo)
  356. {
  357. case 0:
  358. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  359. components_ = 3;
  360. break;
  361. case 1:
  362. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  363. components_ = 4;
  364. break;
  365. case 2:
  366. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  367. components_ = 3;
  368. break;
  369. case 3:
  370. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  371. components_ = 4;
  372. break;
  373. case 6:
  374. compressedFormat_ = CF_ETC1;
  375. components_ = 3;
  376. break;
  377. case 7:
  378. compressedFormat_ = CF_DXT1;
  379. components_ = 4;
  380. break;
  381. case 9:
  382. compressedFormat_ = CF_DXT3;
  383. components_ = 4;
  384. break;
  385. case 11:
  386. compressedFormat_ = CF_DXT5;
  387. components_ = 4;
  388. break;
  389. }
  390. if (compressedFormat_ == CF_NONE)
  391. {
  392. LOGERROR("Unsupported texture format in PVR file");
  393. return false;
  394. }
  395. source.Seek(source.GetPosition() + metaDataSize);
  396. unsigned dataSize = source.GetSize() - source.GetPosition();
  397. data_ = new unsigned char[dataSize];
  398. width_ = width;
  399. height_ = height;
  400. numCompressedLevels_ = mipmapCount;
  401. source.Read(data_.Get(), dataSize);
  402. SetMemoryUse(dataSize);
  403. }
  404. else
  405. {
  406. // Not DDS, KTX or PVR, use STBImage to load other image formats as uncompressed
  407. source.Seek(0);
  408. int width, height;
  409. unsigned components;
  410. unsigned char* pixelData = GetImageData(source, width, height, components);
  411. if (!pixelData)
  412. {
  413. LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  414. return false;
  415. }
  416. SetSize(width, height, components);
  417. SetData(pixelData);
  418. FreeImageData(pixelData);
  419. }
  420. return true;
  421. }
  422. void Image::SetSize(int width, int height, unsigned components)
  423. {
  424. SetSize(width, height, 1, components);
  425. }
  426. void Image::SetSize(int width, int height, int depth, unsigned components)
  427. {
  428. if (width == width_ && height == height_ && depth == depth_ && components == components_)
  429. return;
  430. if (width <= 0 || height <= 0 || depth <= 0)
  431. return;
  432. data_ = new unsigned char[width * height * depth * components];
  433. width_ = width;
  434. height_ = height;
  435. depth_ = depth;
  436. components_ = components;
  437. compressedFormat_ = CF_NONE;
  438. numCompressedLevels_ = 0;
  439. SetMemoryUse(width * height * depth * components);
  440. }
  441. void Image::SetData(const unsigned char* pixelData)
  442. {
  443. memcpy(data_.Get(), pixelData, width_ * height_ * depth_ * components_);
  444. }
  445. bool Image::LoadColorLUT(Deserializer& source)
  446. {
  447. String fileID = source.ReadFileID();
  448. if (fileID == "DDS " || fileID == "\253KTX" || fileID == "PVR\3")
  449. {
  450. LOGERROR("Invalid image format, can not load image");
  451. return false;
  452. }
  453. source.Seek(0);
  454. int width, height;
  455. unsigned components;
  456. unsigned char* pixelDataIn = GetImageData(source, width, height, components);
  457. if (!pixelDataIn)
  458. {
  459. LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  460. return false;
  461. }
  462. if (components != 3)
  463. {
  464. LOGERROR("Invalid image format, can not load image");
  465. return false;
  466. }
  467. SetSize(COLOR_LUT_SIZE, COLOR_LUT_SIZE, COLOR_LUT_SIZE, components);
  468. SetMemoryUse(width_ * height_ * depth_ * components);
  469. unsigned char* pixelDataOut = GetData();
  470. for (int z = 0; z < depth_; ++z)
  471. {
  472. for (int y = 0; y < height_; ++y)
  473. {
  474. unsigned char* in = &pixelDataIn[z*width_*3 + y*width*3];
  475. unsigned char* out = &pixelDataOut[z*width_*height_*3 + y*width_*3];
  476. for (int x = 0; x < width_*3; x += 3)
  477. {
  478. out[x] = in[x];
  479. out[x+1] = in[x+1];
  480. out[x+2] = in[x+2];
  481. }
  482. }
  483. }
  484. FreeImageData(pixelDataIn);
  485. return true;
  486. }
  487. void Image::FlipVertical()
  488. {
  489. if (!data_)
  490. return;
  491. if (IsCompressed())
  492. {
  493. LOGERROR("Can not flip a compressed image");
  494. return;
  495. }
  496. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  497. unsigned rowSize = width_ * components_;
  498. for (int y = 0; y < height_; ++y)
  499. memcpy(&newData[(height_ - y - 1) * rowSize], &data_[y * rowSize], rowSize);
  500. data_ = newData;
  501. }
  502. bool Image::SaveBMP(const String& fileName)
  503. {
  504. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  505. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  506. {
  507. LOGERROR("Access denied to " + fileName);
  508. return false;
  509. }
  510. if (IsCompressed())
  511. {
  512. LOGERROR("Can not save compressed image to BMP");
  513. return false;
  514. }
  515. if (data_)
  516. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  517. else
  518. return false;
  519. }
  520. bool Image::SavePNG(const String& fileName)
  521. {
  522. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  523. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  524. {
  525. LOGERROR("Access denied to " + fileName);
  526. return false;
  527. }
  528. if (IsCompressed())
  529. {
  530. LOGERROR("Can not save compressed image to PNG");
  531. return false;
  532. }
  533. if (data_)
  534. return stbi_write_png(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get(), 0) != 0;
  535. else
  536. return false;
  537. }
  538. bool Image::SaveTGA(const String& fileName)
  539. {
  540. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  541. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  542. {
  543. LOGERROR("Access denied to " + fileName);
  544. return false;
  545. }
  546. if (IsCompressed())
  547. {
  548. LOGERROR("Can not save compressed image to TGA");
  549. return false;
  550. }
  551. if (data_)
  552. return stbi_write_tga(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get()) != 0;
  553. else
  554. return false;
  555. }
  556. bool Image::SaveJPG(const String & fileName, int quality)
  557. {
  558. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  559. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  560. {
  561. LOGERROR("Access denied to " + fileName);
  562. return false;
  563. }
  564. if (IsCompressed())
  565. {
  566. LOGERROR("Can not save compressed image to JPG");
  567. return false;
  568. }
  569. if (data_)
  570. return jo_write_jpg(GetNativePath(fileName).CString(), data_.Get(), width_, height_, components_, quality) != 0;
  571. else
  572. return false;
  573. }
  574. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  575. {
  576. unsigned dataSize = source.GetSize();
  577. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  578. source.Read(buffer.Get(), dataSize);
  579. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int *)&components, 0);
  580. }
  581. void Image::FreeImageData(unsigned char* pixelData)
  582. {
  583. if (!pixelData)
  584. return;
  585. stbi_image_free(pixelData);
  586. }
  587. SharedPtr<Image> Image::GetNextLevel() const
  588. {
  589. if (IsCompressed())
  590. {
  591. LOGERROR("Can not generate mip level from compressed data");
  592. return SharedPtr<Image>();
  593. }
  594. if (components_ < 1 || components_ > 4)
  595. {
  596. LOGERROR("Illegal number of image components for mip level generation");
  597. return SharedPtr<Image>();
  598. }
  599. int widthOut = width_ / 2;
  600. int heightOut = height_ / 2;
  601. int depthOut = depth_ / 2;
  602. if (widthOut < 1)
  603. widthOut = 1;
  604. if (heightOut < 1)
  605. heightOut = 1;
  606. if (depthOut < 1)
  607. depthOut = 1;
  608. SharedPtr<Image> mipImage(new Image(context_));
  609. if (depth_ > 1)
  610. mipImage->SetSize(widthOut, heightOut, depthOut, components_);
  611. else
  612. mipImage->SetSize(widthOut, heightOut, components_);
  613. const unsigned char* pixelDataIn = data_.Get();
  614. unsigned char* pixelDataOut = mipImage->data_.Get();
  615. // 1D case
  616. if (depth_ == 1 && (height_ == 1 || width_ == 1))
  617. {
  618. // Loop using the larger dimension
  619. if (widthOut < heightOut)
  620. widthOut = heightOut;
  621. switch (components_)
  622. {
  623. case 1:
  624. for (int x = 0; x < widthOut; ++x)
  625. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+1]) >> 1;
  626. break;
  627. case 2:
  628. for (int x = 0; x < widthOut*2; x += 2)
  629. {
  630. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+2]) >> 1;
  631. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+3]) >> 1;
  632. }
  633. break;
  634. case 3:
  635. for (int x = 0; x < widthOut*3; x += 3)
  636. {
  637. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+3]) >> 1;
  638. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+4]) >> 1;
  639. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+5]) >> 1;
  640. }
  641. break;
  642. case 4:
  643. for (int x = 0; x < widthOut*4; x += 4)
  644. {
  645. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+4]) >> 1;
  646. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+5]) >> 1;
  647. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+6]) >> 1;
  648. pixelDataOut[x+3] = ((unsigned)pixelDataIn[x*2+3] + pixelDataIn[x*2+7]) >> 1;
  649. }
  650. break;
  651. }
  652. }
  653. // 2D case
  654. else if (depth_ == 1)
  655. {
  656. switch (components_)
  657. {
  658. case 1:
  659. for (int y = 0; y < heightOut; ++y)
  660. {
  661. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_];
  662. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_];
  663. unsigned char* out = &pixelDataOut[y*widthOut];
  664. for (int x = 0; x < widthOut; ++x)
  665. {
  666. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+1] + inLower[x*2] + inLower[x*2+1]) >> 2;
  667. }
  668. }
  669. break;
  670. case 2:
  671. for (int y = 0; y < heightOut; ++y)
  672. {
  673. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*2];
  674. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*2];
  675. unsigned char* out = &pixelDataOut[y*widthOut*2];
  676. for (int x = 0; x < widthOut*2; x += 2)
  677. {
  678. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+2] + inLower[x*2] + inLower[x*2+2]) >> 2;
  679. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+3] + inLower[x*2+1] + inLower[x*2+3]) >> 2;
  680. }
  681. }
  682. break;
  683. case 3:
  684. for (int y = 0; y < heightOut; ++y)
  685. {
  686. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*3];
  687. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*3];
  688. unsigned char* out = &pixelDataOut[y*widthOut*3];
  689. for (int x = 0; x < widthOut*3; x += 3)
  690. {
  691. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+3] + inLower[x*2] + inLower[x*2+3]) >> 2;
  692. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+4] + inLower[x*2+1] + inLower[x*2+4]) >> 2;
  693. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+5] + inLower[x*2+2] + inLower[x*2+5]) >> 2;
  694. }
  695. }
  696. break;
  697. case 4:
  698. for (int y = 0; y < heightOut; ++y)
  699. {
  700. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*4];
  701. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*4];
  702. unsigned char* out = &pixelDataOut[y*widthOut*4];
  703. for (int x = 0; x < widthOut*4; x += 4)
  704. {
  705. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+4] + inLower[x*2] + inLower[x*2+4]) >> 2;
  706. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+5] + inLower[x*2+1] + inLower[x*2+5]) >> 2;
  707. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+6] + inLower[x*2+2] + inLower[x*2+6]) >> 2;
  708. out[x+3] = ((unsigned)inUpper[x*2+3] + inUpper[x*2+7] + inLower[x*2+3] + inLower[x*2+7]) >> 2;
  709. }
  710. }
  711. break;
  712. }
  713. }
  714. // 3D case
  715. else
  716. {
  717. switch (components_)
  718. {
  719. case 1:
  720. for (int z = 0; z < depthOut; ++z)
  721. {
  722. const unsigned char* inOuter = &pixelDataIn[(z*2)*width_*height_];
  723. const unsigned char* inInner = &pixelDataIn[(z*2+1)*width_*height_];
  724. for (int y = 0; y < heightOut; ++y)
  725. {
  726. const unsigned char* inOuterUpper = &inOuter[(y*2)*width_];
  727. const unsigned char* inOuterLower = &inOuter[(y*2+1)*width_];
  728. const unsigned char* inInnerUpper = &inInner[(y*2)*width_];
  729. const unsigned char* inInnerLower = &inInner[(y*2+1)*width_];
  730. unsigned char* out = &pixelDataOut[z*widthOut*heightOut + y*widthOut];
  731. for (int x = 0; x < widthOut; ++x)
  732. {
  733. out[x] = ((unsigned)inOuterUpper[x*2] + inOuterUpper[x*2+1] + inOuterLower[x*2] + inOuterLower[x*2+1] +
  734. inInnerUpper[x*2] + inInnerUpper[x*2+1] + inInnerLower[x*2] + inInnerLower[x*2+1]) >> 3;
  735. }
  736. }
  737. }
  738. break;
  739. case 2:
  740. for (int z = 0; z < depthOut; ++z)
  741. {
  742. const unsigned char* inOuter = &pixelDataIn[(z*2)*width_*height_*2];
  743. const unsigned char* inInner = &pixelDataIn[(z*2+1)*width_*height_*2];
  744. for (int y = 0; y < heightOut; ++y)
  745. {
  746. const unsigned char* inOuterUpper = &inOuter[(y*2)*width_*2];
  747. const unsigned char* inOuterLower = &inOuter[(y*2+1)*width_*2];
  748. const unsigned char* inInnerUpper = &inInner[(y*2)*width_*2];
  749. const unsigned char* inInnerLower = &inInner[(y*2+1)*width_*2];
  750. unsigned char* out = &pixelDataOut[z*widthOut*heightOut*2 + y*widthOut*2];
  751. for (int x = 0; x < widthOut*2; x += 2)
  752. {
  753. out[x] = ((unsigned)inOuterUpper[x*2] + inOuterUpper[x*2+2] + inOuterLower[x*2] + inOuterLower[x*2+2] +
  754. inInnerUpper[x*2] + inInnerUpper[x*2+2] + inInnerLower[x*2] + inInnerLower[x*2+2]) >> 3;
  755. out[x+1] = ((unsigned)inOuterUpper[x*2+1] + inOuterUpper[x*2+3] + inOuterLower[x*2+1] + inOuterLower[x*2+3] +
  756. inInnerUpper[x*2+1] + inInnerUpper[x*2+3] + inInnerLower[x*2+1] + inInnerLower[x*2+3]) >> 3;
  757. }
  758. }
  759. }
  760. break;
  761. case 3:
  762. for (int z = 0; z < depthOut; ++z)
  763. {
  764. const unsigned char* inOuter = &pixelDataIn[(z*2)*width_*height_*3];
  765. const unsigned char* inInner = &pixelDataIn[(z*2+1)*width_*height_*3];
  766. for (int y = 0; y < heightOut; ++y)
  767. {
  768. const unsigned char* inOuterUpper = &inOuter[(y*2)*width_*3];
  769. const unsigned char* inOuterLower = &inOuter[(y*2+1)*width_*3];
  770. const unsigned char* inInnerUpper = &inInner[(y*2)*width_*3];
  771. const unsigned char* inInnerLower = &inInner[(y*2+1)*width_*3];
  772. unsigned char* out = &pixelDataOut[z*widthOut*heightOut*3 + y*widthOut*3];
  773. for (int x = 0; x < widthOut*3; x += 3)
  774. {
  775. out[x] = ((unsigned)inOuterUpper[x*2] + inOuterUpper[x*2+3] + inOuterLower[x*2] + inOuterLower[x*2+3] +
  776. inInnerUpper[x*2] + inInnerUpper[x*2+3] + inInnerLower[x*2] + inInnerLower[x*2+3]) >> 3;
  777. out[x+1] = ((unsigned)inOuterUpper[x*2+1] + inOuterUpper[x*2+4] + inOuterLower[x*2+1] + inOuterLower[x*2+4] +
  778. inInnerUpper[x*2+1] + inInnerUpper[x*2+4] + inInnerLower[x*2+1] + inInnerLower[x*2+4]) >> 3;
  779. out[x+2] = ((unsigned)inOuterUpper[x*2+2] + inOuterUpper[x*2+5] + inOuterLower[x*2+2] + inOuterLower[x*2+5] +
  780. inInnerUpper[x*2+2] + inInnerUpper[x*2+5] + inInnerLower[x*2+2] + inInnerLower[x*2+5]) >> 3;
  781. }
  782. }
  783. }
  784. break;
  785. case 4:
  786. for (int z = 0; z < depthOut; ++z)
  787. {
  788. const unsigned char* inOuter = &pixelDataIn[(z*2)*width_*height_*4];
  789. const unsigned char* inInner = &pixelDataIn[(z*2+1)*width_*height_*4];
  790. for (int y = 0; y < heightOut; ++y)
  791. {
  792. const unsigned char* inOuterUpper = &inOuter[(y*2)*width_*4];
  793. const unsigned char* inOuterLower = &inOuter[(y*2+1)*width_*4];
  794. const unsigned char* inInnerUpper = &inInner[(y*2)*width_*4];
  795. const unsigned char* inInnerLower = &inInner[(y*2+1)*width_*4];
  796. unsigned char* out = &pixelDataOut[z*widthOut*heightOut*4 + y*widthOut*4];
  797. for (int x = 0; x < widthOut*4; x += 4)
  798. {
  799. out[x] = ((unsigned)inOuterUpper[x*2] + inOuterUpper[x*2+4] + inOuterLower[x*2] + inOuterLower[x*2+4] +
  800. inInnerUpper[x*2] + inInnerUpper[x*2+4] + inInnerLower[x*2] + inInnerLower[x*2+4]) >> 3;
  801. out[x+1] = ((unsigned)inOuterUpper[x*2+1] + inOuterUpper[x*2+5] + inOuterLower[x*2+1] + inOuterLower[x*2+5] +
  802. inInnerUpper[x*2+1] + inInnerUpper[x*2+5] + inInnerLower[x*2+1] + inInnerLower[x*2+5]) >> 3;
  803. out[x+2] = ((unsigned)inOuterUpper[x*2+2] + inOuterUpper[x*2+6] + inOuterLower[x*2+2] + inOuterLower[x*2+6] +
  804. inInnerUpper[x*2+2] + inInnerUpper[x*2+6] + inInnerLower[x*2+2] + inInnerLower[x*2+6]) >> 3;
  805. }
  806. }
  807. }
  808. break;
  809. }
  810. }
  811. return mipImage;
  812. }
  813. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  814. {
  815. CompressedLevel level;
  816. if (compressedFormat_ == CF_NONE)
  817. {
  818. LOGERROR("Image is not compressed");
  819. return level;
  820. }
  821. if (index >= numCompressedLevels_)
  822. {
  823. LOGERROR("Compressed image mip level out of bounds");
  824. return level;
  825. }
  826. level.format_ = compressedFormat_;
  827. level.width_ = width_;
  828. level.height_ = height_;
  829. level.depth_ = depth_;
  830. if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  831. {
  832. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  833. unsigned i = 0;
  834. unsigned offset = 0;
  835. for (;;)
  836. {
  837. if (!level.width_)
  838. level.width_ = 1;
  839. if (!level.height_)
  840. level.height_ = 1;
  841. if (!level.depth_)
  842. level.depth_ = 1;
  843. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  844. level.rows_ = ((level.height_ + 3) / 4);
  845. level.data_ = data_.Get() + offset;
  846. level.dataSize_ = level.depth_ * level.rows_ * level.rowSize_;
  847. if (offset + level.dataSize_ > GetMemoryUse())
  848. {
  849. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  850. " Datasize: " + String(GetMemoryUse()));
  851. level.data_ = 0;
  852. return level;
  853. }
  854. if (i == index)
  855. return level;
  856. offset += level.dataSize_;
  857. level.width_ /= 2;
  858. level.height_ /= 2;
  859. level.depth_ /= 2;
  860. ++i;
  861. }
  862. }
  863. else
  864. {
  865. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  866. unsigned i = 0;
  867. unsigned offset = 0;
  868. for (;;)
  869. {
  870. if (!level.width_)
  871. level.width_ = 1;
  872. if (!level.height_)
  873. level.height_ = 1;
  874. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  875. int dataHeight = Max(level.height_, 8);
  876. level.data_ = data_.Get() + offset;
  877. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  878. level.rows_ = dataHeight;
  879. level.rowSize_ = level.dataSize_ / level.rows_;
  880. if (offset + level.dataSize_ > GetMemoryUse())
  881. {
  882. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  883. " Datasize: " + String(GetMemoryUse()));
  884. level.data_ = 0;
  885. return level;
  886. }
  887. if (i == index)
  888. return level;
  889. offset += level.dataSize_;
  890. level.width_ /= 2;
  891. level.height_ /= 2;
  892. ++i;
  893. }
  894. }
  895. }
  896. SDL_Surface* Image::GetSDLSurface(const IntRect& rect) const
  897. {
  898. if (!data_)
  899. return 0;
  900. if (IsCompressed())
  901. {
  902. LOGERROR("Can not get SDL surface from compressed image " + GetName());
  903. return 0;
  904. }
  905. if (components_ < 3)
  906. {
  907. LOGERROR("Can not get SDL surface from image " + GetName() + " with less than 3 components");
  908. return 0;
  909. }
  910. IntRect imageRect = rect;
  911. // Use full image if illegal rect
  912. if (imageRect.left_ < 0 || imageRect.top_ < 0 || imageRect.right_ > width_ || imageRect.bottom_ > height_ ||
  913. imageRect.left_ >= imageRect.right_ || imageRect.top_ >= imageRect.bottom_)
  914. {
  915. imageRect.left_ = 0;
  916. imageRect.top_ = 0;
  917. imageRect.right_ = width_;
  918. imageRect.bottom_ = height_;
  919. }
  920. int imageWidth = width_;
  921. int width = imageRect.Width();
  922. int height = imageRect.Height();
  923. // Assume little-endian for all the supported platforms
  924. unsigned rMask = 0x000000ff;
  925. unsigned gMask = 0x0000ff00;
  926. unsigned bMask = 0x00ff0000;
  927. unsigned aMask = 0xff000000;
  928. SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, components_ * 8, rMask, gMask, bMask, aMask);
  929. if (surface)
  930. {
  931. SDL_LockSurface(surface);
  932. unsigned char* destination = reinterpret_cast<unsigned char*>(surface->pixels);
  933. unsigned char* source = data_ + components_ * (imageWidth * imageRect.top_ + imageRect.left_);
  934. for (int i = 0; i < height; ++i)
  935. {
  936. memcpy(destination, source, components_ * width);
  937. destination += surface->pitch;
  938. source += components_ * imageWidth;
  939. }
  940. SDL_UnlockSurface(surface);
  941. }
  942. else
  943. LOGERROR("Failed to create SDL surface from image " + GetName());
  944. return surface;
  945. }
  946. }