Image.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. //
  2. // Copyright (c) 2008-2014 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 "Profiler.h"
  29. #include <cstring>
  30. #include <stb_image.h>
  31. #include <stb_image_write.h>
  32. #include <jo_jpeg.h>
  33. #include <SDL_surface.h>
  34. #include "DebugNew.h"
  35. #ifndef MAKEFOURCC
  36. #define MAKEFOURCC(ch0, ch1, ch2, ch3) ((unsigned)(ch0) | ((unsigned)(ch1) << 8) | ((unsigned)(ch2) << 16) | ((unsigned)(ch3) << 24))
  37. #endif
  38. #define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1'))
  39. #define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2'))
  40. #define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3'))
  41. #define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4'))
  42. #define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5'))
  43. namespace Urho3D
  44. {
  45. struct DDColorKey
  46. {
  47. unsigned dwColorSpaceLowValue_;
  48. unsigned dwColorSpaceHighValue_;
  49. };
  50. struct DDPixelFormat
  51. {
  52. unsigned dwSize_;
  53. unsigned dwFlags_;
  54. unsigned dwFourCC_;
  55. union
  56. {
  57. unsigned dwRGBBitCount_;
  58. unsigned dwYUVBitCount_;
  59. unsigned dwZBufferBitDepth_;
  60. unsigned dwAlphaBitDepth_;
  61. unsigned dwLuminanceBitCount_;
  62. unsigned dwBumpBitCount_;
  63. unsigned dwPrivateFormatBitCount_;
  64. };
  65. union
  66. {
  67. unsigned dwRBitMask_;
  68. unsigned dwYBitMask_;
  69. unsigned dwStencilBitDepth_;
  70. unsigned dwLuminanceBitMask_;
  71. unsigned dwBumpDuBitMask_;
  72. unsigned dwOperations_;
  73. };
  74. union
  75. {
  76. unsigned dwGBitMask_;
  77. unsigned dwUBitMask_;
  78. unsigned dwZBitMask_;
  79. unsigned dwBumpDvBitMask_;
  80. struct
  81. {
  82. unsigned short wFlipMSTypes_;
  83. unsigned short wBltMSTypes_;
  84. } multiSampleCaps_;
  85. };
  86. union
  87. {
  88. unsigned dwBBitMask_;
  89. unsigned dwVBitMask_;
  90. unsigned dwStencilBitMask_;
  91. unsigned dwBumpLuminanceBitMask_;
  92. };
  93. union
  94. {
  95. unsigned dwRGBAlphaBitMask_;
  96. unsigned dwYUVAlphaBitMask_;
  97. unsigned dwLuminanceAlphaBitMask_;
  98. unsigned dwRGBZBitMask_;
  99. unsigned dwYUVZBitMask_;
  100. };
  101. };
  102. struct DDSCaps2
  103. {
  104. unsigned dwCaps_;
  105. unsigned dwCaps2_;
  106. unsigned dwCaps3_;
  107. union
  108. {
  109. unsigned dwCaps4_;
  110. unsigned dwVolumeDepth_;
  111. };
  112. };
  113. struct DDSurfaceDesc2
  114. {
  115. unsigned dwSize_;
  116. unsigned dwFlags_;
  117. unsigned dwHeight_;
  118. unsigned dwWidth_;
  119. union
  120. {
  121. unsigned lPitch_;
  122. unsigned dwLinearSize_;
  123. };
  124. union
  125. {
  126. unsigned dwBackBufferCount_;
  127. unsigned dwDepth_;
  128. };
  129. union
  130. {
  131. unsigned dwMipMapCount_;
  132. unsigned dwRefreshRate_;
  133. unsigned dwSrcVBHandle_;
  134. };
  135. unsigned dwAlphaBitDepth_;
  136. unsigned dwReserved_;
  137. unsigned lpSurface_; // Do not define as a void pointer, as it is 8 bytes in a 64bit build
  138. union
  139. {
  140. DDColorKey ddckCKDestOverlay_;
  141. unsigned dwEmptyFaceColor_;
  142. };
  143. DDColorKey ddckCKDestBlt_;
  144. DDColorKey ddckCKSrcOverlay_;
  145. DDColorKey ddckCKSrcBlt_;
  146. union
  147. {
  148. DDPixelFormat ddpfPixelFormat_;
  149. unsigned dwFVF_;
  150. };
  151. DDSCaps2 ddsCaps_;
  152. unsigned dwTextureStage_;
  153. };
  154. bool CompressedLevel::Decompress(unsigned char* dest)
  155. {
  156. if (!data_)
  157. return false;
  158. switch (format_)
  159. {
  160. case CF_DXT1:
  161. case CF_DXT3:
  162. case CF_DXT5:
  163. DecompressImageDXT(dest, data_, width_, height_, depth_, format_);
  164. return true;
  165. case CF_ETC1:
  166. DecompressImageETC(dest, data_, width_, height_);
  167. return true;
  168. case CF_PVRTC_RGB_2BPP:
  169. case CF_PVRTC_RGBA_2BPP:
  170. case CF_PVRTC_RGB_4BPP:
  171. case CF_PVRTC_RGBA_4BPP:
  172. DecompressImagePVRTC(dest, data_, width_, height_, format_);
  173. return true;
  174. default:
  175. // Unknown format
  176. return false;
  177. }
  178. }
  179. Image::Image(Context* context) :
  180. Resource(context),
  181. width_(0),
  182. height_(0),
  183. depth_(0),
  184. components_(0)
  185. {
  186. }
  187. Image::~Image()
  188. {
  189. }
  190. void Image::RegisterObject(Context* context)
  191. {
  192. context->RegisterFactory<Image>();
  193. }
  194. bool Image::BeginLoad(Deserializer& source)
  195. {
  196. // Check for DDS, KTX or PVR compressed format
  197. String fileID = source.ReadFileID();
  198. if (fileID == "DDS ")
  199. {
  200. // DDS compressed format
  201. DDSurfaceDesc2 ddsd;
  202. source.Read(&ddsd, sizeof(ddsd));
  203. switch (ddsd.ddpfPixelFormat_.dwFourCC_)
  204. {
  205. case FOURCC_DXT1:
  206. compressedFormat_ = CF_DXT1;
  207. components_ = 3;
  208. break;
  209. case FOURCC_DXT3:
  210. compressedFormat_ = CF_DXT3;
  211. components_ = 4;
  212. break;
  213. case FOURCC_DXT5:
  214. compressedFormat_ = CF_DXT5;
  215. components_ = 4;
  216. break;
  217. default:
  218. LOGERROR("Unsupported DDS format");
  219. return false;
  220. }
  221. unsigned dataSize = source.GetSize() - source.GetPosition();
  222. data_ = new unsigned char[dataSize];
  223. width_ = ddsd.dwWidth_;
  224. height_ = ddsd.dwHeight_;
  225. depth_ = ddsd.dwDepth_;
  226. numCompressedLevels_ = ddsd.dwMipMapCount_;
  227. if (!numCompressedLevels_)
  228. numCompressedLevels_ = 1;
  229. SetMemoryUse(dataSize);
  230. source.Read(data_.Get(), dataSize);
  231. }
  232. else if (fileID == "\253KTX")
  233. {
  234. source.Seek(12);
  235. unsigned endianness = source.ReadUInt();
  236. unsigned type = source.ReadUInt();
  237. /* unsigned typeSize = */ source.ReadUInt();
  238. unsigned format = source.ReadUInt();
  239. unsigned internalFormat = source.ReadUInt();
  240. /* unsigned baseInternalFormat = */ source.ReadUInt();
  241. unsigned width = source.ReadUInt();
  242. unsigned height = source.ReadUInt();
  243. unsigned depth = source.ReadUInt();
  244. /* unsigned arrayElements = */ source.ReadUInt();
  245. unsigned faces = source.ReadUInt();
  246. unsigned mipmaps = source.ReadUInt();
  247. unsigned keyValueBytes = source.ReadUInt();
  248. if (endianness != 0x04030201)
  249. {
  250. LOGERROR("Big-endian KTX files not supported");
  251. return false;
  252. }
  253. if (type != 0 || format != 0)
  254. {
  255. LOGERROR("Uncompressed KTX files not supported");
  256. return false;
  257. }
  258. if (faces > 1 || depth > 1)
  259. {
  260. LOGERROR("3D or cube KTX files not supported");
  261. return false;
  262. }
  263. if (mipmaps == 0)
  264. {
  265. LOGERROR("KTX files without explicitly specified mipmap count not supported");
  266. return false;
  267. }
  268. compressedFormat_ = CF_NONE;
  269. switch (internalFormat)
  270. {
  271. case 0x83f1:
  272. compressedFormat_ = CF_DXT1;
  273. components_ = 4;
  274. break;
  275. case 0x83f2:
  276. compressedFormat_ = CF_DXT3;
  277. components_ = 4;
  278. break;
  279. case 0x83f3:
  280. compressedFormat_ = CF_DXT5;
  281. components_ = 4;
  282. break;
  283. case 0x8d64:
  284. compressedFormat_ = CF_ETC1;
  285. components_ = 3;
  286. break;
  287. case 0x8c00:
  288. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  289. components_ = 3;
  290. break;
  291. case 0x8c01:
  292. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  293. components_ = 3;
  294. break;
  295. case 0x8c02:
  296. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  297. components_ = 4;
  298. break;
  299. case 0x8c03:
  300. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  301. components_ = 4;
  302. break;
  303. }
  304. if (compressedFormat_ == CF_NONE)
  305. {
  306. LOGERROR("Unsupported texture format in KTX file");
  307. return false;
  308. }
  309. source.Seek(source.GetPosition() + keyValueBytes);
  310. unsigned dataSize = source.GetSize() - source.GetPosition() - mipmaps * sizeof(unsigned);
  311. data_ = new unsigned char[dataSize];
  312. width_ = width;
  313. height_ = height;
  314. numCompressedLevels_ = mipmaps;
  315. unsigned dataOffset = 0;
  316. for (unsigned i = 0; i < mipmaps; ++i)
  317. {
  318. unsigned levelSize = source.ReadUInt();
  319. if (levelSize + dataOffset > dataSize)
  320. {
  321. LOGERROR("KTX mipmap level data size exceeds file size");
  322. return false;
  323. }
  324. source.Read(&data_[dataOffset], levelSize);
  325. dataOffset += levelSize;
  326. if (source.GetPosition() & 3)
  327. source.Seek((source.GetPosition() + 3) & 0xfffffffc);
  328. }
  329. SetMemoryUse(dataSize);
  330. }
  331. else if (fileID == "PVR\3")
  332. {
  333. /* unsigned flags = */ source.ReadUInt();
  334. unsigned pixelFormatLo = source.ReadUInt();
  335. /* unsigned pixelFormatHi = */ source.ReadUInt();
  336. /* unsigned colourSpace = */ source.ReadUInt();
  337. /* unsigned channelType = */ source.ReadUInt();
  338. unsigned height = source.ReadUInt();
  339. unsigned width = source.ReadUInt();
  340. unsigned depth = source.ReadUInt();
  341. /* unsigned numSurfaces = */ source.ReadUInt();
  342. unsigned numFaces = source.ReadUInt();
  343. unsigned mipmapCount = source.ReadUInt();
  344. unsigned metaDataSize = source.ReadUInt();
  345. if (depth > 1 || numFaces > 1)
  346. {
  347. LOGERROR("3D or cube PVR files not supported");
  348. return false;
  349. }
  350. if (mipmapCount == 0)
  351. {
  352. LOGERROR("PVR files without explicitly specified mipmap count not supported");
  353. return false;
  354. }
  355. compressedFormat_ = CF_NONE;
  356. switch (pixelFormatLo)
  357. {
  358. case 0:
  359. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  360. components_ = 3;
  361. break;
  362. case 1:
  363. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  364. components_ = 4;
  365. break;
  366. case 2:
  367. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  368. components_ = 3;
  369. break;
  370. case 3:
  371. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  372. components_ = 4;
  373. break;
  374. case 6:
  375. compressedFormat_ = CF_ETC1;
  376. components_ = 3;
  377. break;
  378. case 7:
  379. compressedFormat_ = CF_DXT1;
  380. components_ = 4;
  381. break;
  382. case 9:
  383. compressedFormat_ = CF_DXT3;
  384. components_ = 4;
  385. break;
  386. case 11:
  387. compressedFormat_ = CF_DXT5;
  388. components_ = 4;
  389. break;
  390. }
  391. if (compressedFormat_ == CF_NONE)
  392. {
  393. LOGERROR("Unsupported texture format in PVR file");
  394. return false;
  395. }
  396. source.Seek(source.GetPosition() + metaDataSize);
  397. unsigned dataSize = source.GetSize() - source.GetPosition();
  398. data_ = new unsigned char[dataSize];
  399. width_ = width;
  400. height_ = height;
  401. numCompressedLevels_ = mipmapCount;
  402. source.Read(data_.Get(), dataSize);
  403. SetMemoryUse(dataSize);
  404. }
  405. else
  406. {
  407. // Not DDS, KTX or PVR, use STBImage to load other image formats as uncompressed
  408. source.Seek(0);
  409. int width, height;
  410. unsigned components;
  411. unsigned char* pixelData = GetImageData(source, width, height, components);
  412. if (!pixelData)
  413. {
  414. LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  415. return false;
  416. }
  417. SetSize(width, height, components);
  418. SetData(pixelData);
  419. FreeImageData(pixelData);
  420. }
  421. return true;
  422. }
  423. bool Image::SetSize(int width, int height, unsigned components)
  424. {
  425. return SetSize(width, height, 1, components);
  426. }
  427. bool Image::SetSize(int width, int height, int depth, unsigned components)
  428. {
  429. if (width == width_ && height == height_ && depth == depth_ && components == components_)
  430. return true;
  431. if (width <= 0 || height <= 0 || depth <= 0)
  432. return false;
  433. if (components > 4)
  434. {
  435. LOGERROR("More than 4 color components are not supported");
  436. return false;
  437. }
  438. data_ = new unsigned char[width * height * depth * components];
  439. width_ = width;
  440. height_ = height;
  441. depth_ = depth;
  442. components_ = components;
  443. compressedFormat_ = CF_NONE;
  444. numCompressedLevels_ = 0;
  445. nextLevel_.Reset();
  446. SetMemoryUse(width * height * depth * components);
  447. return true;
  448. }
  449. void Image::SetPixel(int x, int y, const Color& color)
  450. {
  451. SetPixelInt(x, y, 0, color.ToUInt());
  452. }
  453. void Image::SetPixel(int x, int y, int z, const Color& color)
  454. {
  455. SetPixelInt(x, y, z, color.ToUInt());
  456. }
  457. void Image::SetPixelInt(int x, int y, unsigned uintColor)
  458. {
  459. SetPixelInt(x, y, 0, uintColor);
  460. }
  461. void Image::SetPixelInt(int x, int y, int z, unsigned uintColor)
  462. {
  463. if (!data_ || x < 0 || x >= width_ || y < 0 || y >= height_ || z < 0 || z >= depth_ || IsCompressed())
  464. return;
  465. unsigned char* dest = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  466. unsigned char* src = (unsigned char*)&uintColor;
  467. switch (components_)
  468. {
  469. case 4:
  470. dest[3] = src[3];
  471. // Fall through
  472. case 3:
  473. dest[2] = src[2];
  474. // Fall through
  475. case 2:
  476. dest[1]= src[1];
  477. // Fall through
  478. default:
  479. dest[0] = src[0];
  480. break;
  481. }
  482. }
  483. void Image::SetData(const unsigned char* pixelData)
  484. {
  485. if (!data_)
  486. return;
  487. if (IsCompressed())
  488. {
  489. LOGERROR("Can not set new pixel data for a compressed image");
  490. return;
  491. }
  492. memcpy(data_.Get(), pixelData, width_ * height_ * depth_ * components_);
  493. nextLevel_.Reset();
  494. }
  495. bool Image::LoadColorLUT(Deserializer& source)
  496. {
  497. String fileID = source.ReadFileID();
  498. if (fileID == "DDS " || fileID == "\253KTX" || fileID == "PVR\3")
  499. {
  500. LOGERROR("Invalid image format, can not load image");
  501. return false;
  502. }
  503. source.Seek(0);
  504. int width, height;
  505. unsigned components;
  506. unsigned char* pixelDataIn = GetImageData(source, width, height, components);
  507. if (!pixelDataIn)
  508. {
  509. LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  510. return false;
  511. }
  512. if (components != 3)
  513. {
  514. LOGERROR("Invalid image format, can not load image");
  515. return false;
  516. }
  517. SetSize(COLOR_LUT_SIZE, COLOR_LUT_SIZE, COLOR_LUT_SIZE, components);
  518. SetMemoryUse(width_ * height_ * depth_ * components);
  519. unsigned char* pixelDataOut = GetData();
  520. for (int z = 0; z < depth_; ++z)
  521. {
  522. for (int y = 0; y < height_; ++y)
  523. {
  524. unsigned char* in = &pixelDataIn[z * width_ * 3 + y * width * 3];
  525. unsigned char* out = &pixelDataOut[z * width_ * height_ * 3 + y * width_ * 3];
  526. for (int x = 0; x < width_ * 3; x += 3)
  527. {
  528. out[x] = in[x];
  529. out[x+1] = in[x + 1];
  530. out[x+2] = in[x + 2];
  531. }
  532. }
  533. }
  534. FreeImageData(pixelDataIn);
  535. return true;
  536. }
  537. void Image::FlipVertical()
  538. {
  539. if (!data_)
  540. return;
  541. if (IsCompressed())
  542. {
  543. LOGERROR("FlipVertical not supported for compressed images");
  544. return;
  545. }
  546. if (depth_ > 1)
  547. {
  548. LOGERROR("FlipVertical not supported for 3D images");
  549. return;
  550. }
  551. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  552. unsigned rowSize = width_ * components_;
  553. for (int y = 0; y < height_; ++y)
  554. memcpy(&newData[(height_ - y - 1) * rowSize], &data_[y * rowSize], rowSize);
  555. data_ = newData;
  556. }
  557. bool Image::Resize(int width, int height)
  558. {
  559. PROFILE(ResizeImage);
  560. if (IsCompressed())
  561. {
  562. LOGERROR("Resize not supported for compressed images");
  563. return false;
  564. }
  565. if (depth_ > 1)
  566. {
  567. LOGERROR("Resize not supported for 3D images");
  568. return false;
  569. }
  570. if (!data_ || width <= 0 || height <= 0)
  571. return false;
  572. /// \todo Reducing image size does not sample all needed pixels
  573. SharedArrayPtr<unsigned char> newData(new unsigned char[width * height * components_]);
  574. for (int y = 0; y < height; ++y)
  575. {
  576. for (int x = 0; x < width; ++x)
  577. {
  578. // Calculate float coordinates between 0 - 1 for resampling
  579. float xF = (width_ > 1) ? (float)x / (float)(width - 1) : 0.0f;
  580. float yF = (height_ > 1) ? (float)y / (float)(height - 1) : 0.0f;
  581. unsigned uintColor = GetPixelBilinear(xF, yF).ToUInt();
  582. unsigned char* dest = newData + (y * width + x) * components_;
  583. unsigned char* src = (unsigned char*)&uintColor;
  584. switch (components_)
  585. {
  586. case 4:
  587. dest[3] = src[3];
  588. // Fall through
  589. case 3:
  590. dest[2] = src[2];
  591. // Fall through
  592. case 2:
  593. dest[1] = src[1];
  594. // Fall through
  595. default:
  596. dest[0] = src[0];
  597. break;
  598. }
  599. }
  600. }
  601. width_ = width;
  602. height_ = height;
  603. data_ = newData;
  604. SetMemoryUse(width * height * depth_ * components_);
  605. return true;
  606. }
  607. void Image::Clear(const Color& color)
  608. {
  609. ClearInt(color.ToUInt());
  610. }
  611. void Image::ClearInt(unsigned uintColor)
  612. {
  613. PROFILE(ClearImage);
  614. if (!data_)
  615. return;
  616. if (IsCompressed())
  617. {
  618. LOGERROR("Clear not supported for compressed images");
  619. return;
  620. }
  621. unsigned char* src = (unsigned char*)&uintColor;
  622. for (unsigned i = 0; i < width_ * height_ * depth_ * components_; ++i)
  623. data_[i] = src[i % components_];
  624. }
  625. bool Image::SaveBMP(const String& fileName) const
  626. {
  627. PROFILE(SaveImageBMP);
  628. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  629. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  630. {
  631. LOGERROR("Access denied to " + fileName);
  632. return false;
  633. }
  634. if (IsCompressed())
  635. {
  636. LOGERROR("Can not save compressed image to BMP");
  637. return false;
  638. }
  639. if (data_)
  640. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  641. else
  642. return false;
  643. }
  644. bool Image::SavePNG(const String& fileName) const
  645. {
  646. PROFILE(SaveImagePNG);
  647. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  648. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  649. {
  650. LOGERROR("Access denied to " + fileName);
  651. return false;
  652. }
  653. if (IsCompressed())
  654. {
  655. LOGERROR("Can not save compressed image to PNG");
  656. return false;
  657. }
  658. if (data_)
  659. return stbi_write_png(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get(), 0) != 0;
  660. else
  661. return false;
  662. }
  663. bool Image::SaveTGA(const String& fileName) const
  664. {
  665. PROFILE(SaveImageTGA);
  666. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  667. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  668. {
  669. LOGERROR("Access denied to " + fileName);
  670. return false;
  671. }
  672. if (IsCompressed())
  673. {
  674. LOGERROR("Can not save compressed image to TGA");
  675. return false;
  676. }
  677. if (data_)
  678. return stbi_write_tga(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get()) != 0;
  679. else
  680. return false;
  681. }
  682. bool Image::SaveJPG(const String & fileName, int quality) const
  683. {
  684. PROFILE(SaveImageJPG);
  685. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  686. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  687. {
  688. LOGERROR("Access denied to " + fileName);
  689. return false;
  690. }
  691. if (IsCompressed())
  692. {
  693. LOGERROR("Can not save compressed image to JPG");
  694. return false;
  695. }
  696. if (data_)
  697. return jo_write_jpg(GetNativePath(fileName).CString(), data_.Get(), width_, height_, components_, quality) != 0;
  698. else
  699. return false;
  700. }
  701. Color Image::GetPixel(int x, int y) const
  702. {
  703. return GetPixel(x, y, 0);
  704. }
  705. Color Image::GetPixel(int x, int y, int z) const
  706. {
  707. if (!data_ || z < 0 || z >= depth_ || IsCompressed())
  708. return Color::BLACK;
  709. x = Clamp(x, 0, width_ - 1);
  710. y = Clamp(y, 0, height_ - 1);
  711. unsigned char* src = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  712. Color ret;
  713. switch (components_)
  714. {
  715. case 4:
  716. ret.a_ = (float)src[3] / 255.0f;
  717. // Fall through
  718. case 3:
  719. ret.b_ = (float)src[2] / 255.0f;
  720. // Fall through
  721. case 2:
  722. ret.g_ = (float)src[1] / 255.0f;
  723. ret.r_ = (float)src[0] / 255.0f;
  724. break;
  725. default:
  726. ret.r_ = ret.g_ = ret.b_ = (float)src[0] / 255.0f;
  727. break;
  728. }
  729. return ret;
  730. }
  731. unsigned Image::GetPixelInt(int x, int y) const
  732. {
  733. return GetPixelInt(x, y, 0);
  734. }
  735. unsigned Image::GetPixelInt(int x, int y, int z) const
  736. {
  737. if (!data_ || z < 0 || z >= depth_ || IsCompressed())
  738. return 0xff000000;
  739. x = Clamp(x, 0, width_ - 1);
  740. y = Clamp(y, 0, height_ - 1);
  741. unsigned char* src = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  742. unsigned ret = 0;
  743. if (components_ < 4)
  744. ret |= 0xff000000;
  745. switch (components_)
  746. {
  747. case 4:
  748. ret |= (unsigned)src[3] << 24;
  749. // Fall through
  750. case 3:
  751. ret |= (unsigned)src[2] << 16;
  752. // Fall through
  753. case 2:
  754. ret |= (unsigned)src[1] << 8;
  755. ret |= (unsigned)src[0];
  756. break;
  757. default:
  758. ret |= (unsigned)src[0] << 16;
  759. ret |= (unsigned)src[0] << 8;
  760. ret |= (unsigned)src[0];
  761. break;
  762. }
  763. return ret;
  764. }
  765. Color Image::GetPixelBilinear(float x, float y) const
  766. {
  767. x = Clamp(x * width_ - 0.5f, 0.0f, (float)(width_ - 1));
  768. y = Clamp(y * height_ - 0.5f, 0.0f, (float)(height_ - 1));
  769. int xI = (int)x;
  770. int yI = (int)y;
  771. float xF = x - floorf(x);
  772. float yF = y - floorf(y);
  773. Color topColor = GetPixel(xI, yI).Lerp(GetPixel(xI + 1, yI), xF);
  774. Color bottomColor = GetPixel(xI, yI + 1).Lerp(GetPixel(xI + 1, yI + 1), xF);
  775. return topColor.Lerp(bottomColor, yF);
  776. }
  777. Color Image::GetPixelTrilinear(float x, float y, float z) const
  778. {
  779. if (depth_ < 2)
  780. return GetPixelBilinear(x, y);
  781. x = Clamp(x * width_ - 0.5f, 0.0f, (float)(width_ - 1));
  782. y = Clamp(y * height_ - 0.5f, 0.0f, (float)(height_ - 1));
  783. z = Clamp(z * depth_ - 0.5f, 0.0f, (float)(depth_ - 1));
  784. int xI = (int)x;
  785. int yI = (int)y;
  786. int zI = (int)z;
  787. if (zI == depth_ - 1)
  788. return GetPixelBilinear(x, y);
  789. float xF = x - floorf(x);
  790. float yF = y - floorf(y);
  791. float zF = z - floorf(z);
  792. Color topColorNear = GetPixel(xI, yI, zI).Lerp(GetPixel(xI + 1, yI, zI), xF);
  793. Color bottomColorNear = GetPixel(xI, yI + 1, zI).Lerp(GetPixel(xI + 1, yI + 1, zI), xF);
  794. Color colorNear = topColorNear.Lerp(bottomColorNear, yF);
  795. Color topColorFar = GetPixel(xI, yI, zI + 1).Lerp(GetPixel(xI + 1, yI, zI + 1), xF);
  796. Color bottomColorFar = GetPixel(xI, yI + 1, zI + 1).Lerp(GetPixel(xI + 1, yI + 1, zI + 1), xF);
  797. Color colorFar = topColorNear.Lerp(bottomColorNear, yF);
  798. return colorNear.Lerp(colorFar, zF);
  799. }
  800. SharedPtr<Image> Image::GetNextLevel() const
  801. {
  802. if (IsCompressed())
  803. {
  804. LOGERROR("Can not generate mip level from compressed data");
  805. return SharedPtr<Image>();
  806. }
  807. if (components_ < 1 || components_ > 4)
  808. {
  809. LOGERROR("Illegal number of image components for mip level generation");
  810. return SharedPtr<Image>();
  811. }
  812. if (nextLevel_)
  813. return nextLevel_;
  814. PROFILE(CalculateImageMipLevel);
  815. int widthOut = width_ / 2;
  816. int heightOut = height_ / 2;
  817. int depthOut = depth_ / 2;
  818. if (widthOut < 1)
  819. widthOut = 1;
  820. if (heightOut < 1)
  821. heightOut = 1;
  822. if (depthOut < 1)
  823. depthOut = 1;
  824. SharedPtr<Image> mipImage(new Image(context_));
  825. if (depth_ > 1)
  826. mipImage->SetSize(widthOut, heightOut, depthOut, components_);
  827. else
  828. mipImage->SetSize(widthOut, heightOut, components_);
  829. const unsigned char* pixelDataIn = data_.Get();
  830. unsigned char* pixelDataOut = mipImage->data_.Get();
  831. // 1D case
  832. if (depth_ == 1 && (height_ == 1 || width_ == 1))
  833. {
  834. // Loop using the larger dimension
  835. if (widthOut < heightOut)
  836. widthOut = heightOut;
  837. switch (components_)
  838. {
  839. case 1:
  840. for (int x = 0; x < widthOut; ++x)
  841. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+1]) >> 1;
  842. break;
  843. case 2:
  844. for (int x = 0; x < widthOut*2; x += 2)
  845. {
  846. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+2]) >> 1;
  847. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+3]) >> 1;
  848. }
  849. break;
  850. case 3:
  851. for (int x = 0; x < widthOut*3; x += 3)
  852. {
  853. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+3]) >> 1;
  854. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+4]) >> 1;
  855. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+5]) >> 1;
  856. }
  857. break;
  858. case 4:
  859. for (int x = 0; x < widthOut*4; x += 4)
  860. {
  861. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+4]) >> 1;
  862. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+5]) >> 1;
  863. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+6]) >> 1;
  864. pixelDataOut[x+3] = ((unsigned)pixelDataIn[x*2+3] + pixelDataIn[x*2+7]) >> 1;
  865. }
  866. break;
  867. }
  868. }
  869. // 2D case
  870. else if (depth_ == 1)
  871. {
  872. switch (components_)
  873. {
  874. case 1:
  875. for (int y = 0; y < heightOut; ++y)
  876. {
  877. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_];
  878. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_];
  879. unsigned char* out = &pixelDataOut[y*widthOut];
  880. for (int x = 0; x < widthOut; ++x)
  881. {
  882. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+1] + inLower[x*2] + inLower[x*2+1]) >> 2;
  883. }
  884. }
  885. break;
  886. case 2:
  887. for (int y = 0; y < heightOut; ++y)
  888. {
  889. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*2];
  890. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*2];
  891. unsigned char* out = &pixelDataOut[y*widthOut*2];
  892. for (int x = 0; x < widthOut*2; x += 2)
  893. {
  894. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+2] + inLower[x*2] + inLower[x*2+2]) >> 2;
  895. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+3] + inLower[x*2+1] + inLower[x*2+3]) >> 2;
  896. }
  897. }
  898. break;
  899. case 3:
  900. for (int y = 0; y < heightOut; ++y)
  901. {
  902. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*3];
  903. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*3];
  904. unsigned char* out = &pixelDataOut[y*widthOut*3];
  905. for (int x = 0; x < widthOut*3; x += 3)
  906. {
  907. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+3] + inLower[x*2] + inLower[x*2+3]) >> 2;
  908. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+4] + inLower[x*2+1] + inLower[x*2+4]) >> 2;
  909. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+5] + inLower[x*2+2] + inLower[x*2+5]) >> 2;
  910. }
  911. }
  912. break;
  913. case 4:
  914. for (int y = 0; y < heightOut; ++y)
  915. {
  916. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*4];
  917. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*4];
  918. unsigned char* out = &pixelDataOut[y*widthOut*4];
  919. for (int x = 0; x < widthOut*4; x += 4)
  920. {
  921. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+4] + inLower[x*2] + inLower[x*2+4]) >> 2;
  922. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+5] + inLower[x*2+1] + inLower[x*2+5]) >> 2;
  923. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+6] + inLower[x*2+2] + inLower[x*2+6]) >> 2;
  924. out[x+3] = ((unsigned)inUpper[x*2+3] + inUpper[x*2+7] + inLower[x*2+3] + inLower[x*2+7]) >> 2;
  925. }
  926. }
  927. break;
  928. }
  929. }
  930. // 3D case
  931. else
  932. {
  933. switch (components_)
  934. {
  935. case 1:
  936. for (int z = 0; z < depthOut; ++z)
  937. {
  938. const unsigned char* inOuter = &pixelDataIn[(z*2)*width_*height_];
  939. const unsigned char* inInner = &pixelDataIn[(z*2+1)*width_*height_];
  940. for (int y = 0; y < heightOut; ++y)
  941. {
  942. const unsigned char* inOuterUpper = &inOuter[(y*2)*width_];
  943. const unsigned char* inOuterLower = &inOuter[(y*2+1)*width_];
  944. const unsigned char* inInnerUpper = &inInner[(y*2)*width_];
  945. const unsigned char* inInnerLower = &inInner[(y*2+1)*width_];
  946. unsigned char* out = &pixelDataOut[z*widthOut*heightOut + y*widthOut];
  947. for (int x = 0; x < widthOut; ++x)
  948. {
  949. out[x] = ((unsigned)inOuterUpper[x*2] + inOuterUpper[x*2+1] + inOuterLower[x*2] + inOuterLower[x*2+1] +
  950. inInnerUpper[x*2] + inInnerUpper[x*2+1] + inInnerLower[x*2] + inInnerLower[x*2+1]) >> 3;
  951. }
  952. }
  953. }
  954. break;
  955. case 2:
  956. for (int z = 0; z < depthOut; ++z)
  957. {
  958. const unsigned char* inOuter = &pixelDataIn[(z*2)*width_*height_*2];
  959. const unsigned char* inInner = &pixelDataIn[(z*2+1)*width_*height_*2];
  960. for (int y = 0; y < heightOut; ++y)
  961. {
  962. const unsigned char* inOuterUpper = &inOuter[(y*2)*width_*2];
  963. const unsigned char* inOuterLower = &inOuter[(y*2+1)*width_*2];
  964. const unsigned char* inInnerUpper = &inInner[(y*2)*width_*2];
  965. const unsigned char* inInnerLower = &inInner[(y*2+1)*width_*2];
  966. unsigned char* out = &pixelDataOut[z*widthOut*heightOut*2 + y*widthOut*2];
  967. for (int x = 0; x < widthOut*2; x += 2)
  968. {
  969. out[x] = ((unsigned)inOuterUpper[x*2] + inOuterUpper[x*2+2] + inOuterLower[x*2] + inOuterLower[x*2+2] +
  970. inInnerUpper[x*2] + inInnerUpper[x*2+2] + inInnerLower[x*2] + inInnerLower[x*2+2]) >> 3;
  971. out[x+1] = ((unsigned)inOuterUpper[x*2+1] + inOuterUpper[x*2+3] + inOuterLower[x*2+1] + inOuterLower[x*2+3] +
  972. inInnerUpper[x*2+1] + inInnerUpper[x*2+3] + inInnerLower[x*2+1] + inInnerLower[x*2+3]) >> 3;
  973. }
  974. }
  975. }
  976. break;
  977. case 3:
  978. for (int z = 0; z < depthOut; ++z)
  979. {
  980. const unsigned char* inOuter = &pixelDataIn[(z*2)*width_*height_*3];
  981. const unsigned char* inInner = &pixelDataIn[(z*2+1)*width_*height_*3];
  982. for (int y = 0; y < heightOut; ++y)
  983. {
  984. const unsigned char* inOuterUpper = &inOuter[(y*2)*width_*3];
  985. const unsigned char* inOuterLower = &inOuter[(y*2+1)*width_*3];
  986. const unsigned char* inInnerUpper = &inInner[(y*2)*width_*3];
  987. const unsigned char* inInnerLower = &inInner[(y*2+1)*width_*3];
  988. unsigned char* out = &pixelDataOut[z*widthOut*heightOut*3 + y*widthOut*3];
  989. for (int x = 0; x < widthOut*3; x += 3)
  990. {
  991. out[x] = ((unsigned)inOuterUpper[x*2] + inOuterUpper[x*2+3] + inOuterLower[x*2] + inOuterLower[x*2+3] +
  992. inInnerUpper[x*2] + inInnerUpper[x*2+3] + inInnerLower[x*2] + inInnerLower[x*2+3]) >> 3;
  993. out[x+1] = ((unsigned)inOuterUpper[x*2+1] + inOuterUpper[x*2+4] + inOuterLower[x*2+1] + inOuterLower[x*2+4] +
  994. inInnerUpper[x*2+1] + inInnerUpper[x*2+4] + inInnerLower[x*2+1] + inInnerLower[x*2+4]) >> 3;
  995. out[x+2] = ((unsigned)inOuterUpper[x*2+2] + inOuterUpper[x*2+5] + inOuterLower[x*2+2] + inOuterLower[x*2+5] +
  996. inInnerUpper[x*2+2] + inInnerUpper[x*2+5] + inInnerLower[x*2+2] + inInnerLower[x*2+5]) >> 3;
  997. }
  998. }
  999. }
  1000. break;
  1001. case 4:
  1002. for (int z = 0; z < depthOut; ++z)
  1003. {
  1004. const unsigned char* inOuter = &pixelDataIn[(z*2)*width_*height_*4];
  1005. const unsigned char* inInner = &pixelDataIn[(z*2+1)*width_*height_*4];
  1006. for (int y = 0; y < heightOut; ++y)
  1007. {
  1008. const unsigned char* inOuterUpper = &inOuter[(y*2)*width_*4];
  1009. const unsigned char* inOuterLower = &inOuter[(y*2+1)*width_*4];
  1010. const unsigned char* inInnerUpper = &inInner[(y*2)*width_*4];
  1011. const unsigned char* inInnerLower = &inInner[(y*2+1)*width_*4];
  1012. unsigned char* out = &pixelDataOut[z*widthOut*heightOut*4 + y*widthOut*4];
  1013. for (int x = 0; x < widthOut*4; x += 4)
  1014. {
  1015. out[x] = ((unsigned)inOuterUpper[x*2] + inOuterUpper[x*2+4] + inOuterLower[x*2] + inOuterLower[x*2+4] +
  1016. inInnerUpper[x*2] + inInnerUpper[x*2+4] + inInnerLower[x*2] + inInnerLower[x*2+4]) >> 3;
  1017. out[x+1] = ((unsigned)inOuterUpper[x*2+1] + inOuterUpper[x*2+5] + inOuterLower[x*2+1] + inOuterLower[x*2+5] +
  1018. inInnerUpper[x*2+1] + inInnerUpper[x*2+5] + inInnerLower[x*2+1] + inInnerLower[x*2+5]) >> 3;
  1019. out[x+2] = ((unsigned)inOuterUpper[x*2+2] + inOuterUpper[x*2+6] + inOuterLower[x*2+2] + inOuterLower[x*2+6] +
  1020. inInnerUpper[x*2+2] + inInnerUpper[x*2+6] + inInnerLower[x*2+2] + inInnerLower[x*2+6]) >> 3;
  1021. }
  1022. }
  1023. }
  1024. break;
  1025. }
  1026. }
  1027. return mipImage;
  1028. }
  1029. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  1030. {
  1031. CompressedLevel level;
  1032. if (compressedFormat_ == CF_NONE)
  1033. {
  1034. LOGERROR("Image is not compressed");
  1035. return level;
  1036. }
  1037. if (index >= numCompressedLevels_)
  1038. {
  1039. LOGERROR("Compressed image mip level out of bounds");
  1040. return level;
  1041. }
  1042. level.format_ = compressedFormat_;
  1043. level.width_ = width_;
  1044. level.height_ = height_;
  1045. level.depth_ = depth_;
  1046. if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  1047. {
  1048. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  1049. unsigned i = 0;
  1050. unsigned offset = 0;
  1051. for (;;)
  1052. {
  1053. if (!level.width_)
  1054. level.width_ = 1;
  1055. if (!level.height_)
  1056. level.height_ = 1;
  1057. if (!level.depth_)
  1058. level.depth_ = 1;
  1059. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  1060. level.rows_ = ((level.height_ + 3) / 4);
  1061. level.data_ = data_.Get() + offset;
  1062. level.dataSize_ = level.depth_ * level.rows_ * level.rowSize_;
  1063. if (offset + level.dataSize_ > GetMemoryUse())
  1064. {
  1065. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1066. " Datasize: " + String(GetMemoryUse()));
  1067. level.data_ = 0;
  1068. return level;
  1069. }
  1070. if (i == index)
  1071. return level;
  1072. offset += level.dataSize_;
  1073. level.width_ /= 2;
  1074. level.height_ /= 2;
  1075. level.depth_ /= 2;
  1076. ++i;
  1077. }
  1078. }
  1079. else
  1080. {
  1081. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  1082. unsigned i = 0;
  1083. unsigned offset = 0;
  1084. for (;;)
  1085. {
  1086. if (!level.width_)
  1087. level.width_ = 1;
  1088. if (!level.height_)
  1089. level.height_ = 1;
  1090. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  1091. int dataHeight = Max(level.height_, 8);
  1092. level.data_ = data_.Get() + offset;
  1093. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  1094. level.rows_ = dataHeight;
  1095. level.rowSize_ = level.dataSize_ / level.rows_;
  1096. if (offset + level.dataSize_ > GetMemoryUse())
  1097. {
  1098. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1099. " Datasize: " + String(GetMemoryUse()));
  1100. level.data_ = 0;
  1101. return level;
  1102. }
  1103. if (i == index)
  1104. return level;
  1105. offset += level.dataSize_;
  1106. level.width_ /= 2;
  1107. level.height_ /= 2;
  1108. ++i;
  1109. }
  1110. }
  1111. }
  1112. Image* Image::GetSubimage(const IntRect& rect) const
  1113. {
  1114. if (!data_)
  1115. return 0;
  1116. if (IsCompressed())
  1117. {
  1118. LOGERROR("Can not get subimage from compressed image " + GetName());
  1119. return 0;
  1120. }
  1121. if (rect.left_ < 0 || rect.top_ < 0 || rect.right_ >= width_ || rect.bottom_ >= height_)
  1122. {
  1123. LOGERROR("Can not get subimage from image " + GetName() + " with invalid region");
  1124. return 0;
  1125. }
  1126. int x = rect.left_;
  1127. int y = rect.top_;
  1128. int width = rect.Width();
  1129. int height = rect.Height();
  1130. Image* image = new Image(context_);
  1131. image->SetSize(width, height, components_);
  1132. unsigned char* dest = image->GetData();
  1133. unsigned char* source = data_.Get() + (y * width_ + x) * components_;
  1134. for (int i = 0; i < height; ++i)
  1135. {
  1136. memcpy(dest, source, width * components_);
  1137. dest += width * components_;
  1138. source += width_ * components_;
  1139. }
  1140. return image;
  1141. }
  1142. SDL_Surface* Image::GetSDLSurface(const IntRect& rect) const
  1143. {
  1144. if (!data_)
  1145. return 0;
  1146. if (IsCompressed())
  1147. {
  1148. LOGERROR("Can not get SDL surface from compressed image " + GetName());
  1149. return 0;
  1150. }
  1151. if (components_ < 3)
  1152. {
  1153. LOGERROR("Can not get SDL surface from image " + GetName() + " with less than 3 components");
  1154. return 0;
  1155. }
  1156. IntRect imageRect = rect;
  1157. // Use full image if illegal rect
  1158. if (imageRect.left_ < 0 || imageRect.top_ < 0 || imageRect.right_ > width_ || imageRect.bottom_ > height_ ||
  1159. imageRect.left_ >= imageRect.right_ || imageRect.top_ >= imageRect.bottom_)
  1160. {
  1161. imageRect.left_ = 0;
  1162. imageRect.top_ = 0;
  1163. imageRect.right_ = width_;
  1164. imageRect.bottom_ = height_;
  1165. }
  1166. int imageWidth = width_;
  1167. int width = imageRect.Width();
  1168. int height = imageRect.Height();
  1169. // Assume little-endian for all the supported platforms
  1170. unsigned rMask = 0x000000ff;
  1171. unsigned gMask = 0x0000ff00;
  1172. unsigned bMask = 0x00ff0000;
  1173. unsigned aMask = 0xff000000;
  1174. SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, components_ * 8, rMask, gMask, bMask, aMask);
  1175. if (surface)
  1176. {
  1177. SDL_LockSurface(surface);
  1178. unsigned char* destination = reinterpret_cast<unsigned char*>(surface->pixels);
  1179. unsigned char* source = data_ + components_ * (imageWidth * imageRect.top_ + imageRect.left_);
  1180. for (int i = 0; i < height; ++i)
  1181. {
  1182. memcpy(destination, source, components_ * width);
  1183. destination += surface->pitch;
  1184. source += components_ * imageWidth;
  1185. }
  1186. SDL_UnlockSurface(surface);
  1187. }
  1188. else
  1189. LOGERROR("Failed to create SDL surface from image " + GetName());
  1190. return surface;
  1191. }
  1192. void Image::PrecalculateLevels()
  1193. {
  1194. if (!data_ || IsCompressed())
  1195. return;
  1196. PROFILE(PrecalculateImageMipLevels);
  1197. nextLevel_.Reset();
  1198. if (width_ > 1 || height_ > 1)
  1199. {
  1200. SharedPtr<Image> current = GetNextLevel();
  1201. nextLevel_ = current;
  1202. while (current && (current->width_ > 1 || current->height_ > 1))
  1203. {
  1204. current->nextLevel_ = current->GetNextLevel();
  1205. current = current->nextLevel_;
  1206. }
  1207. }
  1208. }
  1209. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  1210. {
  1211. unsigned dataSize = source.GetSize();
  1212. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  1213. source.Read(buffer.Get(), dataSize);
  1214. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int *)&components, 0);
  1215. }
  1216. void Image::FreeImageData(unsigned char* pixelData)
  1217. {
  1218. if (!pixelData)
  1219. return;
  1220. stbi_image_free(pixelData);
  1221. }
  1222. }