Image.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Context.h"
  24. #include "../Core/Profiler.h"
  25. #include "../IO/File.h"
  26. #include "../IO/FileSystem.h"
  27. #include "../IO/Log.h"
  28. #include "../Resource/Decompress.h"
  29. #include <JO/jo_jpeg.h>
  30. #include <SDL/include/SDL_surface.h>
  31. #include <STB/stb_image.h>
  32. #include <STB/stb_image_write.h>
  33. #include "../DebugNew.h"
  34. extern "C" unsigned char* stbi_write_png_to_mem(unsigned char* pixels, int stride_bytes, int x, int y, int n, int* out_len);
  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 Atomic
  44. {
  45. /// DirectDraw color key definition.
  46. struct DDColorKey
  47. {
  48. unsigned dwColorSpaceLowValue_;
  49. unsigned dwColorSpaceHighValue_;
  50. };
  51. /// DirectDraw pixel format definition.
  52. struct DDPixelFormat
  53. {
  54. unsigned dwSize_;
  55. unsigned dwFlags_;
  56. unsigned dwFourCC_;
  57. union
  58. {
  59. unsigned dwRGBBitCount_;
  60. unsigned dwYUVBitCount_;
  61. unsigned dwZBufferBitDepth_;
  62. unsigned dwAlphaBitDepth_;
  63. unsigned dwLuminanceBitCount_;
  64. unsigned dwBumpBitCount_;
  65. unsigned dwPrivateFormatBitCount_;
  66. };
  67. union
  68. {
  69. unsigned dwRBitMask_;
  70. unsigned dwYBitMask_;
  71. unsigned dwStencilBitDepth_;
  72. unsigned dwLuminanceBitMask_;
  73. unsigned dwBumpDuBitMask_;
  74. unsigned dwOperations_;
  75. };
  76. union
  77. {
  78. unsigned dwGBitMask_;
  79. unsigned dwUBitMask_;
  80. unsigned dwZBitMask_;
  81. unsigned dwBumpDvBitMask_;
  82. struct
  83. {
  84. unsigned short wFlipMSTypes_;
  85. unsigned short wBltMSTypes_;
  86. } multiSampleCaps_;
  87. };
  88. union
  89. {
  90. unsigned dwBBitMask_;
  91. unsigned dwVBitMask_;
  92. unsigned dwStencilBitMask_;
  93. unsigned dwBumpLuminanceBitMask_;
  94. };
  95. union
  96. {
  97. unsigned dwRGBAlphaBitMask_;
  98. unsigned dwYUVAlphaBitMask_;
  99. unsigned dwLuminanceAlphaBitMask_;
  100. unsigned dwRGBZBitMask_;
  101. unsigned dwYUVZBitMask_;
  102. };
  103. };
  104. /// DirectDraw surface capabilities.
  105. struct DDSCaps2
  106. {
  107. unsigned dwCaps_;
  108. unsigned dwCaps2_;
  109. unsigned dwCaps3_;
  110. union
  111. {
  112. unsigned dwCaps4_;
  113. unsigned dwVolumeDepth_;
  114. };
  115. };
  116. /// DirectDraw surface description.
  117. struct DDSurfaceDesc2
  118. {
  119. unsigned dwSize_;
  120. unsigned dwFlags_;
  121. unsigned dwHeight_;
  122. unsigned dwWidth_;
  123. union
  124. {
  125. unsigned lPitch_;
  126. unsigned dwLinearSize_;
  127. };
  128. union
  129. {
  130. unsigned dwBackBufferCount_;
  131. unsigned dwDepth_;
  132. };
  133. union
  134. {
  135. unsigned dwMipMapCount_;
  136. unsigned dwRefreshRate_;
  137. unsigned dwSrcVBHandle_;
  138. };
  139. unsigned dwAlphaBitDepth_;
  140. unsigned dwReserved_;
  141. unsigned lpSurface_; // Do not define as a void pointer, as it is 8 bytes in a 64bit build
  142. union
  143. {
  144. DDColorKey ddckCKDestOverlay_;
  145. unsigned dwEmptyFaceColor_;
  146. };
  147. DDColorKey ddckCKDestBlt_;
  148. DDColorKey ddckCKSrcOverlay_;
  149. DDColorKey ddckCKSrcBlt_;
  150. union
  151. {
  152. DDPixelFormat ddpfPixelFormat_;
  153. unsigned dwFVF_;
  154. };
  155. DDSCaps2 ddsCaps_;
  156. unsigned dwTextureStage_;
  157. };
  158. bool CompressedLevel::Decompress(unsigned char* dest)
  159. {
  160. if (!data_)
  161. return false;
  162. switch (format_)
  163. {
  164. case CF_DXT1:
  165. case CF_DXT3:
  166. case CF_DXT5:
  167. DecompressImageDXT(dest, data_, width_, height_, depth_, format_);
  168. return true;
  169. case CF_ETC1:
  170. DecompressImageETC(dest, data_, width_, height_);
  171. return true;
  172. case CF_PVRTC_RGB_2BPP:
  173. case CF_PVRTC_RGBA_2BPP:
  174. case CF_PVRTC_RGB_4BPP:
  175. case CF_PVRTC_RGBA_4BPP:
  176. DecompressImagePVRTC(dest, data_, width_, height_, format_);
  177. return true;
  178. default:
  179. // Unknown format
  180. return false;
  181. }
  182. }
  183. Image::Image(Context* context) :
  184. Resource(context),
  185. width_(0),
  186. height_(0),
  187. depth_(0),
  188. components_(0)
  189. {
  190. }
  191. Image::~Image()
  192. {
  193. }
  194. void Image::RegisterObject(Context* context)
  195. {
  196. context->RegisterFactory<Image>();
  197. }
  198. bool Image::BeginLoad(Deserializer& source)
  199. {
  200. // Check for DDS, KTX or PVR compressed format
  201. String fileID = source.ReadFileID();
  202. if (fileID == "DDS ")
  203. {
  204. // DDS compressed format
  205. DDSurfaceDesc2 ddsd;
  206. source.Read(&ddsd, sizeof(ddsd));
  207. switch (ddsd.ddpfPixelFormat_.dwFourCC_)
  208. {
  209. case FOURCC_DXT1:
  210. compressedFormat_ = CF_DXT1;
  211. components_ = 3;
  212. break;
  213. case FOURCC_DXT3:
  214. compressedFormat_ = CF_DXT3;
  215. components_ = 4;
  216. break;
  217. case FOURCC_DXT5:
  218. compressedFormat_ = CF_DXT5;
  219. components_ = 4;
  220. break;
  221. case 0:
  222. if (ddsd.ddpfPixelFormat_.dwRGBBitCount_ != 32 && ddsd.ddpfPixelFormat_.dwRGBBitCount_ != 24 &&
  223. ddsd.ddpfPixelFormat_.dwRGBBitCount_ != 16)
  224. {
  225. LOGERROR("Unsupported DDS pixel byte size");
  226. return false;
  227. }
  228. compressedFormat_ = CF_RGBA;
  229. components_ = 4;
  230. break;
  231. default:
  232. LOGERROR("Unrecognized DDS image format");
  233. return false;
  234. }
  235. unsigned dataSize = source.GetSize() - source.GetPosition();
  236. data_ = new unsigned char[dataSize];
  237. width_ = ddsd.dwWidth_;
  238. height_ = ddsd.dwHeight_;
  239. depth_ = ddsd.dwDepth_;
  240. numCompressedLevels_ = ddsd.dwMipMapCount_;
  241. if (!numCompressedLevels_)
  242. numCompressedLevels_ = 1;
  243. SetMemoryUse(dataSize);
  244. source.Read(data_.Get(), dataSize);
  245. // If uncompressed DDS, convert the data to 8bit RGBA as the texture classes can not currently use eg. RGB565 format
  246. if (compressedFormat_ == CF_RGBA)
  247. {
  248. PROFILE(ConvertDDSToRGBA);
  249. unsigned sourcePixelByteSize = ddsd.ddpfPixelFormat_.dwRGBBitCount_ >> 3;
  250. unsigned numPixels = dataSize / sourcePixelByteSize;
  251. #define ADJUSTSHIFT(mask, l, r) \
  252. if (mask && mask >= 0x100) \
  253. { \
  254. while ((mask >> r) >= 0x100) \
  255. ++r; \
  256. } \
  257. else if (mask && mask < 0x80) \
  258. { \
  259. while ((mask << l) < 0x80) \
  260. ++l; \
  261. }
  262. unsigned rShiftL = 0, gShiftL = 0, bShiftL = 0, aShiftL = 0;
  263. unsigned rShiftR = 0, gShiftR = 0, bShiftR = 0, aShiftR = 0;
  264. unsigned rMask = ddsd.ddpfPixelFormat_.dwRBitMask_;
  265. unsigned gMask = ddsd.ddpfPixelFormat_.dwGBitMask_;
  266. unsigned bMask = ddsd.ddpfPixelFormat_.dwBBitMask_;
  267. unsigned aMask = ddsd.ddpfPixelFormat_.dwRGBAlphaBitMask_;
  268. ADJUSTSHIFT(rMask, rShiftL, rShiftR)
  269. ADJUSTSHIFT(gMask, gShiftL, gShiftR)
  270. ADJUSTSHIFT(bMask, bShiftL, bShiftR)
  271. ADJUSTSHIFT(aMask, aShiftL, aShiftR)
  272. SharedArrayPtr<unsigned char> rgbaData(new unsigned char[numPixels * 4]);
  273. SetMemoryUse(numPixels * 4);
  274. switch (sourcePixelByteSize)
  275. {
  276. case 4:
  277. {
  278. unsigned* src = (unsigned*)data_.Get();
  279. unsigned char* dest = rgbaData.Get();
  280. while (numPixels--)
  281. {
  282. unsigned pixels = *src++;
  283. *dest++ = (unsigned char)(((pixels & rMask) << rShiftL) >> rShiftR);
  284. *dest++ = (unsigned char)(((pixels & gMask) << gShiftL) >> gShiftR);
  285. *dest++ = (unsigned char)(((pixels & bMask) << bShiftL) >> bShiftR);
  286. *dest++ = (unsigned char)(((pixels & aMask) << aShiftL) >> aShiftR);
  287. }
  288. }
  289. break;
  290. case 3:
  291. {
  292. unsigned char* src = data_.Get();
  293. unsigned char* dest = rgbaData.Get();
  294. while (numPixels--)
  295. {
  296. unsigned pixels = src[0] | (src[1] << 8) | (src[2] << 16);
  297. src += 3;
  298. *dest++ = (unsigned char)(((pixels & rMask) << rShiftL) >> rShiftR);
  299. *dest++ = (unsigned char)(((pixels & gMask) << gShiftL) >> gShiftR);
  300. *dest++ = (unsigned char)(((pixels & bMask) << bShiftL) >> bShiftR);
  301. *dest++ = (unsigned char)(((pixels & aMask) << aShiftL) >> aShiftR);
  302. }
  303. }
  304. break;
  305. default:
  306. {
  307. unsigned short* src = (unsigned short*)data_.Get();
  308. unsigned char* dest = rgbaData.Get();
  309. while (numPixels--)
  310. {
  311. unsigned short pixels = *src++;
  312. *dest++ = (unsigned char)(((pixels & rMask) << rShiftL) >> rShiftR);
  313. *dest++ = (unsigned char)(((pixels & gMask) << gShiftL) >> gShiftR);
  314. *dest++ = (unsigned char)(((pixels & bMask) << bShiftL) >> bShiftR);
  315. *dest++ = (unsigned char)(((pixels & aMask) << aShiftL) >> aShiftR);
  316. }
  317. }
  318. break;
  319. }
  320. // Replace with converted data
  321. data_ = rgbaData;
  322. }
  323. }
  324. else if (fileID == "\253KTX")
  325. {
  326. source.Seek(12);
  327. unsigned endianness = source.ReadUInt();
  328. unsigned type = source.ReadUInt();
  329. /* unsigned typeSize = */ source.ReadUInt();
  330. unsigned format = source.ReadUInt();
  331. unsigned internalFormat = source.ReadUInt();
  332. /* unsigned baseInternalFormat = */ source.ReadUInt();
  333. unsigned width = source.ReadUInt();
  334. unsigned height = source.ReadUInt();
  335. unsigned depth = source.ReadUInt();
  336. /* unsigned arrayElements = */ source.ReadUInt();
  337. unsigned faces = source.ReadUInt();
  338. unsigned mipmaps = source.ReadUInt();
  339. unsigned keyValueBytes = source.ReadUInt();
  340. if (endianness != 0x04030201)
  341. {
  342. LOGERROR("Big-endian KTX files not supported");
  343. return false;
  344. }
  345. if (type != 0 || format != 0)
  346. {
  347. LOGERROR("Uncompressed KTX files not supported");
  348. return false;
  349. }
  350. if (faces > 1 || depth > 1)
  351. {
  352. LOGERROR("3D or cube KTX files not supported");
  353. return false;
  354. }
  355. if (mipmaps == 0)
  356. {
  357. LOGERROR("KTX files without explicitly specified mipmap count not supported");
  358. return false;
  359. }
  360. switch (internalFormat)
  361. {
  362. case 0x83f1:
  363. compressedFormat_ = CF_DXT1;
  364. components_ = 4;
  365. break;
  366. case 0x83f2:
  367. compressedFormat_ = CF_DXT3;
  368. components_ = 4;
  369. break;
  370. case 0x83f3:
  371. compressedFormat_ = CF_DXT5;
  372. components_ = 4;
  373. break;
  374. case 0x8d64:
  375. compressedFormat_ = CF_ETC1;
  376. components_ = 3;
  377. break;
  378. case 0x8c00:
  379. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  380. components_ = 3;
  381. break;
  382. case 0x8c01:
  383. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  384. components_ = 3;
  385. break;
  386. case 0x8c02:
  387. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  388. components_ = 4;
  389. break;
  390. case 0x8c03:
  391. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  392. components_ = 4;
  393. break;
  394. default:
  395. compressedFormat_ = CF_NONE;
  396. break;
  397. }
  398. if (compressedFormat_ == CF_NONE)
  399. {
  400. LOGERROR("Unsupported texture format in KTX file");
  401. return false;
  402. }
  403. source.Seek(source.GetPosition() + keyValueBytes);
  404. unsigned dataSize = (unsigned)(source.GetSize() - source.GetPosition() - mipmaps * sizeof(unsigned));
  405. data_ = new unsigned char[dataSize];
  406. width_ = width;
  407. height_ = height;
  408. numCompressedLevels_ = mipmaps;
  409. unsigned dataOffset = 0;
  410. for (unsigned i = 0; i < mipmaps; ++i)
  411. {
  412. unsigned levelSize = source.ReadUInt();
  413. if (levelSize + dataOffset > dataSize)
  414. {
  415. LOGERROR("KTX mipmap level data size exceeds file size");
  416. return false;
  417. }
  418. source.Read(&data_[dataOffset], levelSize);
  419. dataOffset += levelSize;
  420. if (source.GetPosition() & 3)
  421. source.Seek((source.GetPosition() + 3) & 0xfffffffc);
  422. }
  423. SetMemoryUse(dataSize);
  424. }
  425. else if (fileID == "PVR\3")
  426. {
  427. /* unsigned flags = */ source.ReadUInt();
  428. unsigned pixelFormatLo = source.ReadUInt();
  429. /* unsigned pixelFormatHi = */ source.ReadUInt();
  430. /* unsigned colourSpace = */ source.ReadUInt();
  431. /* unsigned channelType = */ source.ReadUInt();
  432. unsigned height = source.ReadUInt();
  433. unsigned width = source.ReadUInt();
  434. unsigned depth = source.ReadUInt();
  435. /* unsigned numSurfaces = */ source.ReadUInt();
  436. unsigned numFaces = source.ReadUInt();
  437. unsigned mipmapCount = source.ReadUInt();
  438. unsigned metaDataSize = source.ReadUInt();
  439. if (depth > 1 || numFaces > 1)
  440. {
  441. LOGERROR("3D or cube PVR files not supported");
  442. return false;
  443. }
  444. if (mipmapCount == 0)
  445. {
  446. LOGERROR("PVR files without explicitly specified mipmap count not supported");
  447. return false;
  448. }
  449. switch (pixelFormatLo)
  450. {
  451. case 0:
  452. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  453. components_ = 3;
  454. break;
  455. case 1:
  456. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  457. components_ = 4;
  458. break;
  459. case 2:
  460. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  461. components_ = 3;
  462. break;
  463. case 3:
  464. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  465. components_ = 4;
  466. break;
  467. case 6:
  468. compressedFormat_ = CF_ETC1;
  469. components_ = 3;
  470. break;
  471. case 7:
  472. compressedFormat_ = CF_DXT1;
  473. components_ = 4;
  474. break;
  475. case 9:
  476. compressedFormat_ = CF_DXT3;
  477. components_ = 4;
  478. break;
  479. case 11:
  480. compressedFormat_ = CF_DXT5;
  481. components_ = 4;
  482. break;
  483. default:
  484. compressedFormat_ = CF_NONE;
  485. break;
  486. }
  487. if (compressedFormat_ == CF_NONE)
  488. {
  489. LOGERROR("Unsupported texture format in PVR file");
  490. return false;
  491. }
  492. source.Seek(source.GetPosition() + metaDataSize);
  493. unsigned dataSize = source.GetSize() - source.GetPosition();
  494. data_ = new unsigned char[dataSize];
  495. width_ = width;
  496. height_ = height;
  497. numCompressedLevels_ = mipmapCount;
  498. source.Read(data_.Get(), dataSize);
  499. SetMemoryUse(dataSize);
  500. }
  501. else
  502. {
  503. // Not DDS, KTX or PVR, use STBImage to load other image formats as uncompressed
  504. source.Seek(0);
  505. int width, height;
  506. unsigned components;
  507. unsigned char* pixelData = GetImageData(source, width, height, components);
  508. if (!pixelData)
  509. {
  510. LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  511. return false;
  512. }
  513. SetSize(width, height, components);
  514. SetData(pixelData);
  515. FreeImageData(pixelData);
  516. }
  517. return true;
  518. }
  519. bool Image::Save(Serializer& dest) const
  520. {
  521. PROFILE(SaveImage);
  522. if (IsCompressed())
  523. {
  524. LOGERROR("Can not save compressed image " + GetName());
  525. return false;
  526. }
  527. if (!data_)
  528. {
  529. LOGERROR("Can not save zero-sized image " + GetName());
  530. return false;
  531. }
  532. int len;
  533. unsigned char* png = stbi_write_png_to_mem(data_.Get(), 0, width_, height_, components_, &len);
  534. bool success = dest.Write(png, (unsigned)len) == (unsigned)len;
  535. free(png);
  536. return success;
  537. }
  538. bool Image::SetSize(int width, int height, unsigned components)
  539. {
  540. return SetSize(width, height, 1, components);
  541. }
  542. bool Image::SetSize(int width, int height, int depth, unsigned components)
  543. {
  544. if (width == width_ && height == height_ && depth == depth_ && components == components_)
  545. return true;
  546. if (width <= 0 || height <= 0 || depth <= 0)
  547. return false;
  548. if (components > 4)
  549. {
  550. LOGERROR("More than 4 color components are not supported");
  551. return false;
  552. }
  553. data_ = new unsigned char[width * height * depth * components];
  554. width_ = width;
  555. height_ = height;
  556. depth_ = depth;
  557. components_ = components;
  558. compressedFormat_ = CF_NONE;
  559. numCompressedLevels_ = 0;
  560. nextLevel_.Reset();
  561. SetMemoryUse(width * height * depth * components);
  562. return true;
  563. }
  564. void Image::SetPixel(int x, int y, const Color& color)
  565. {
  566. SetPixelInt(x, y, 0, color.ToUInt());
  567. }
  568. void Image::SetPixel(int x, int y, int z, const Color& color)
  569. {
  570. SetPixelInt(x, y, z, color.ToUInt());
  571. }
  572. void Image::SetPixelInt(int x, int y, unsigned uintColor)
  573. {
  574. SetPixelInt(x, y, 0, uintColor);
  575. }
  576. void Image::SetPixelInt(int x, int y, int z, unsigned uintColor)
  577. {
  578. if (!data_ || x < 0 || x >= width_ || y < 0 || y >= height_ || z < 0 || z >= depth_ || IsCompressed())
  579. return;
  580. unsigned char* dest = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  581. unsigned char* src = (unsigned char*)&uintColor;
  582. switch (components_)
  583. {
  584. case 4:
  585. dest[3] = src[3];
  586. // Fall through
  587. case 3:
  588. dest[2] = src[2];
  589. // Fall through
  590. case 2:
  591. dest[1] = src[1];
  592. // Fall through
  593. default:
  594. dest[0] = src[0];
  595. break;
  596. }
  597. }
  598. void Image::SetData(const unsigned char* pixelData)
  599. {
  600. if (!data_)
  601. return;
  602. if (IsCompressed())
  603. {
  604. LOGERROR("Can not set new pixel data for a compressed image");
  605. return;
  606. }
  607. memcpy(data_.Get(), pixelData, width_ * height_ * depth_ * components_);
  608. nextLevel_.Reset();
  609. }
  610. bool Image::LoadColorLUT(Deserializer& source)
  611. {
  612. String fileID = source.ReadFileID();
  613. if (fileID == "DDS " || fileID == "\253KTX" || fileID == "PVR\3")
  614. {
  615. LOGERROR("Invalid image format, can not load image");
  616. return false;
  617. }
  618. source.Seek(0);
  619. int width, height;
  620. unsigned components;
  621. unsigned char* pixelDataIn = GetImageData(source, width, height, components);
  622. if (!pixelDataIn)
  623. {
  624. LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  625. return false;
  626. }
  627. if (components != 3)
  628. {
  629. LOGERROR("Invalid image format, can not load image");
  630. return false;
  631. }
  632. SetSize(COLOR_LUT_SIZE, COLOR_LUT_SIZE, COLOR_LUT_SIZE, components);
  633. SetMemoryUse(width_ * height_ * depth_ * components);
  634. unsigned char* pixelDataOut = GetData();
  635. for (int z = 0; z < depth_; ++z)
  636. {
  637. for (int y = 0; y < height_; ++y)
  638. {
  639. unsigned char* in = &pixelDataIn[z * width_ * 3 + y * width * 3];
  640. unsigned char* out = &pixelDataOut[z * width_ * height_ * 3 + y * width_ * 3];
  641. for (int x = 0; x < width_ * 3; x += 3)
  642. {
  643. out[x] = in[x];
  644. out[x + 1] = in[x + 1];
  645. out[x + 2] = in[x + 2];
  646. }
  647. }
  648. }
  649. FreeImageData(pixelDataIn);
  650. return true;
  651. }
  652. bool Image::FlipHorizontal()
  653. {
  654. if (!data_)
  655. return false;
  656. if (depth_ > 1)
  657. {
  658. LOGERROR("FlipHorizontal not supported for 3D images");
  659. return false;
  660. }
  661. if (!IsCompressed())
  662. {
  663. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  664. unsigned rowSize = width_ * components_;
  665. for (int y = 0; y < height_; ++y)
  666. {
  667. for (int x = 0; x < width_; ++x)
  668. {
  669. for (unsigned c = 0; c < components_; ++c)
  670. newData[y * rowSize + x * components_ + c] = data_[y * rowSize + (width_ - x - 1) * components_ + c];
  671. }
  672. }
  673. data_ = newData;
  674. }
  675. else
  676. {
  677. if (compressedFormat_ > CF_DXT5)
  678. {
  679. LOGERROR("FlipHorizontal not yet implemented for other compressed formats than RGBA & DXT1,3,5");
  680. return false;
  681. }
  682. // Memory use = combined size of the compressed mip levels
  683. SharedArrayPtr<unsigned char> newData(new unsigned char[GetMemoryUse()]);
  684. unsigned dataOffset = 0;
  685. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  686. {
  687. CompressedLevel level = GetCompressedLevel(i);
  688. if (!level.data_)
  689. {
  690. LOGERROR("Got compressed level with no data, aborting horizontal flip");
  691. return false;
  692. }
  693. for (unsigned y = 0; y < level.rows_; ++y)
  694. {
  695. for (unsigned x = 0; x < level.rowSize_; x += level.blockSize_)
  696. {
  697. unsigned char* src = level.data_ + y * level.rowSize_ + (level.rowSize_ - level.blockSize_ - x);
  698. unsigned char* dest = newData.Get() + y * level.rowSize_ + x;
  699. FlipBlockHorizontal(dest, src, compressedFormat_);
  700. }
  701. }
  702. dataOffset += level.dataSize_;
  703. }
  704. data_ = newData;
  705. }
  706. return true;
  707. }
  708. bool Image::FlipVertical()
  709. {
  710. if (!data_)
  711. return false;
  712. if (depth_ > 1)
  713. {
  714. LOGERROR("FlipVertical not supported for 3D images");
  715. return false;
  716. }
  717. if (!IsCompressed())
  718. {
  719. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  720. unsigned rowSize = width_ * components_;
  721. for (int y = 0; y < height_; ++y)
  722. memcpy(&newData[(height_ - y - 1) * rowSize], &data_[y * rowSize], rowSize);
  723. data_ = newData;
  724. }
  725. else
  726. {
  727. if (compressedFormat_ > CF_DXT5)
  728. {
  729. LOGERROR("FlipVertical not yet implemented for other compressed formats than DXT1,3,5");
  730. return false;
  731. }
  732. // Memory use = combined size of the compressed mip levels
  733. SharedArrayPtr<unsigned char> newData(new unsigned char[GetMemoryUse()]);
  734. unsigned dataOffset = 0;
  735. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  736. {
  737. CompressedLevel level = GetCompressedLevel(i);
  738. if (!level.data_)
  739. {
  740. LOGERROR("Got compressed level with no data, aborting vertical flip");
  741. return false;
  742. }
  743. for (unsigned y = 0; y < level.rows_; ++y)
  744. {
  745. unsigned char* src = level.data_ + y * level.rowSize_;
  746. unsigned char* dest = newData.Get() + dataOffset + (level.rows_ - y - 1) * level.rowSize_;
  747. for (unsigned x = 0; x < level.rowSize_; x += level.blockSize_)
  748. FlipBlockVertical(dest + x, src + x, compressedFormat_);
  749. }
  750. dataOffset += level.dataSize_;
  751. }
  752. data_ = newData;
  753. }
  754. return true;
  755. }
  756. bool Image::Resize(int width, int height)
  757. {
  758. PROFILE(ResizeImage);
  759. if (IsCompressed())
  760. {
  761. LOGERROR("Resize not supported for compressed images");
  762. return false;
  763. }
  764. if (depth_ > 1)
  765. {
  766. LOGERROR("Resize not supported for 3D images");
  767. return false;
  768. }
  769. if (!data_ || width <= 0 || height <= 0)
  770. return false;
  771. /// \todo Reducing image size does not sample all needed pixels
  772. SharedArrayPtr<unsigned char> newData(new unsigned char[width * height * components_]);
  773. for (int y = 0; y < height; ++y)
  774. {
  775. for (int x = 0; x < width; ++x)
  776. {
  777. // Calculate float coordinates between 0 - 1 for resampling
  778. float xF = (width_ > 1) ? (float)x / (float)(width - 1) : 0.0f;
  779. float yF = (height_ > 1) ? (float)y / (float)(height - 1) : 0.0f;
  780. unsigned uintColor = GetPixelBilinear(xF, yF).ToUInt();
  781. unsigned char* dest = newData + (y * width + x) * components_;
  782. unsigned char* src = (unsigned char*)&uintColor;
  783. switch (components_)
  784. {
  785. case 4:
  786. dest[3] = src[3];
  787. // Fall through
  788. case 3:
  789. dest[2] = src[2];
  790. // Fall through
  791. case 2:
  792. dest[1] = src[1];
  793. // Fall through
  794. default:
  795. dest[0] = src[0];
  796. break;
  797. }
  798. }
  799. }
  800. width_ = width;
  801. height_ = height;
  802. data_ = newData;
  803. SetMemoryUse(width * height * depth_ * components_);
  804. return true;
  805. }
  806. void Image::Clear(const Color& color)
  807. {
  808. ClearInt(color.ToUInt());
  809. }
  810. void Image::ClearInt(unsigned uintColor)
  811. {
  812. PROFILE(ClearImage);
  813. if (!data_)
  814. return;
  815. if (IsCompressed())
  816. {
  817. LOGERROR("Clear not supported for compressed images");
  818. return;
  819. }
  820. unsigned char* src = (unsigned char*)&uintColor;
  821. for (unsigned i = 0; i < width_ * height_ * depth_ * components_; ++i)
  822. data_[i] = src[i % components_];
  823. }
  824. bool Image::SaveBMP(const String& fileName) const
  825. {
  826. PROFILE(SaveImageBMP);
  827. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  828. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  829. {
  830. LOGERROR("Access denied to " + fileName);
  831. return false;
  832. }
  833. if (IsCompressed())
  834. {
  835. LOGERROR("Can not save compressed image to BMP");
  836. return false;
  837. }
  838. if (data_)
  839. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  840. else
  841. return false;
  842. }
  843. bool Image::SavePNG(const String& fileName) const
  844. {
  845. PROFILE(SaveImagePNG);
  846. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  847. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  848. {
  849. LOGERROR("Access denied to " + fileName);
  850. return false;
  851. }
  852. if (IsCompressed())
  853. {
  854. LOGERROR("Can not save compressed image to PNG");
  855. return false;
  856. }
  857. if (data_)
  858. return stbi_write_png(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get(), 0) != 0;
  859. else
  860. return false;
  861. }
  862. bool Image::SaveTGA(const String& fileName) const
  863. {
  864. PROFILE(SaveImageTGA);
  865. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  866. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  867. {
  868. LOGERROR("Access denied to " + fileName);
  869. return false;
  870. }
  871. if (IsCompressed())
  872. {
  873. LOGERROR("Can not save compressed image to TGA");
  874. return false;
  875. }
  876. if (data_)
  877. return stbi_write_tga(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get()) != 0;
  878. else
  879. return false;
  880. }
  881. bool Image::SaveJPG(const String& fileName, int quality) const
  882. {
  883. PROFILE(SaveImageJPG);
  884. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  885. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  886. {
  887. LOGERROR("Access denied to " + fileName);
  888. return false;
  889. }
  890. if (IsCompressed())
  891. {
  892. LOGERROR("Can not save compressed image to JPG");
  893. return false;
  894. }
  895. if (data_)
  896. return jo_write_jpg(GetNativePath(fileName).CString(), data_.Get(), width_, height_, components_, quality) != 0;
  897. else
  898. return false;
  899. }
  900. Color Image::GetPixel(int x, int y) const
  901. {
  902. return GetPixel(x, y, 0);
  903. }
  904. Color Image::GetPixel(int x, int y, int z) const
  905. {
  906. if (!data_ || z < 0 || z >= depth_ || IsCompressed())
  907. return Color::BLACK;
  908. x = Clamp(x, 0, width_ - 1);
  909. y = Clamp(y, 0, height_ - 1);
  910. unsigned char* src = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  911. Color ret;
  912. switch (components_)
  913. {
  914. case 4:
  915. ret.a_ = (float)src[3] / 255.0f;
  916. // Fall through
  917. case 3:
  918. ret.b_ = (float)src[2] / 255.0f;
  919. // Fall through
  920. case 2:
  921. ret.g_ = (float)src[1] / 255.0f;
  922. ret.r_ = (float)src[0] / 255.0f;
  923. break;
  924. default:
  925. ret.r_ = ret.g_ = ret.b_ = (float)src[0] / 255.0f;
  926. break;
  927. }
  928. return ret;
  929. }
  930. unsigned Image::GetPixelInt(int x, int y) const
  931. {
  932. return GetPixelInt(x, y, 0);
  933. }
  934. unsigned Image::GetPixelInt(int x, int y, int z) const
  935. {
  936. if (!data_ || z < 0 || z >= depth_ || IsCompressed())
  937. return 0xff000000;
  938. x = Clamp(x, 0, width_ - 1);
  939. y = Clamp(y, 0, height_ - 1);
  940. unsigned char* src = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  941. unsigned ret = 0;
  942. if (components_ < 4)
  943. ret |= 0xff000000;
  944. switch (components_)
  945. {
  946. case 4:
  947. ret |= (unsigned)src[3] << 24;
  948. // Fall through
  949. case 3:
  950. ret |= (unsigned)src[2] << 16;
  951. // Fall through
  952. case 2:
  953. ret |= (unsigned)src[1] << 8;
  954. ret |= (unsigned)src[0];
  955. break;
  956. default:
  957. ret |= (unsigned)src[0] << 16;
  958. ret |= (unsigned)src[0] << 8;
  959. ret |= (unsigned)src[0];
  960. break;
  961. }
  962. return ret;
  963. }
  964. Color Image::GetPixelBilinear(float x, float y) const
  965. {
  966. x = Clamp(x * width_ - 0.5f, 0.0f, (float)(width_ - 1));
  967. y = Clamp(y * height_ - 0.5f, 0.0f, (float)(height_ - 1));
  968. int xI = (int)x;
  969. int yI = (int)y;
  970. float xF = x - floorf(x);
  971. float yF = y - floorf(y);
  972. Color topColor = GetPixel(xI, yI).Lerp(GetPixel(xI + 1, yI), xF);
  973. Color bottomColor = GetPixel(xI, yI + 1).Lerp(GetPixel(xI + 1, yI + 1), xF);
  974. return topColor.Lerp(bottomColor, yF);
  975. }
  976. Color Image::GetPixelTrilinear(float x, float y, float z) const
  977. {
  978. if (depth_ < 2)
  979. return GetPixelBilinear(x, y);
  980. x = Clamp(x * width_ - 0.5f, 0.0f, (float)(width_ - 1));
  981. y = Clamp(y * height_ - 0.5f, 0.0f, (float)(height_ - 1));
  982. z = Clamp(z * depth_ - 0.5f, 0.0f, (float)(depth_ - 1));
  983. int xI = (int)x;
  984. int yI = (int)y;
  985. int zI = (int)z;
  986. if (zI == depth_ - 1)
  987. return GetPixelBilinear(x, y);
  988. float xF = x - floorf(x);
  989. float yF = y - floorf(y);
  990. float zF = z - floorf(z);
  991. Color topColorNear = GetPixel(xI, yI, zI).Lerp(GetPixel(xI + 1, yI, zI), xF);
  992. Color bottomColorNear = GetPixel(xI, yI + 1, zI).Lerp(GetPixel(xI + 1, yI + 1, zI), xF);
  993. Color colorNear = topColorNear.Lerp(bottomColorNear, yF);
  994. Color topColorFar = GetPixel(xI, yI, zI + 1).Lerp(GetPixel(xI + 1, yI, zI + 1), xF);
  995. Color bottomColorFar = GetPixel(xI, yI + 1, zI + 1).Lerp(GetPixel(xI + 1, yI + 1, zI + 1), xF);
  996. Color colorFar = topColorFar.Lerp(bottomColorFar, yF);
  997. return colorNear.Lerp(colorFar, zF);
  998. }
  999. SharedPtr<Image> Image::GetNextLevel() const
  1000. {
  1001. if (IsCompressed())
  1002. {
  1003. LOGERROR("Can not generate mip level from compressed data");
  1004. return SharedPtr<Image>();
  1005. }
  1006. if (components_ < 1 || components_ > 4)
  1007. {
  1008. LOGERROR("Illegal number of image components for mip level generation");
  1009. return SharedPtr<Image>();
  1010. }
  1011. if (nextLevel_)
  1012. return nextLevel_;
  1013. PROFILE(CalculateImageMipLevel);
  1014. int widthOut = width_ / 2;
  1015. int heightOut = height_ / 2;
  1016. int depthOut = depth_ / 2;
  1017. if (widthOut < 1)
  1018. widthOut = 1;
  1019. if (heightOut < 1)
  1020. heightOut = 1;
  1021. if (depthOut < 1)
  1022. depthOut = 1;
  1023. SharedPtr<Image> mipImage(new Image(context_));
  1024. if (depth_ > 1)
  1025. mipImage->SetSize(widthOut, heightOut, depthOut, components_);
  1026. else
  1027. mipImage->SetSize(widthOut, heightOut, components_);
  1028. const unsigned char* pixelDataIn = data_.Get();
  1029. unsigned char* pixelDataOut = mipImage->data_.Get();
  1030. // 1D case
  1031. if (depth_ == 1 && (height_ == 1 || width_ == 1))
  1032. {
  1033. // Loop using the larger dimension
  1034. if (widthOut < heightOut)
  1035. widthOut = heightOut;
  1036. switch (components_)
  1037. {
  1038. case 1:
  1039. for (int x = 0; x < widthOut; ++x)
  1040. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 1]) >> 1);
  1041. break;
  1042. case 2:
  1043. for (int x = 0; x < widthOut * 2; x += 2)
  1044. {
  1045. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 2]) >> 1);
  1046. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 3]) >> 1);
  1047. }
  1048. break;
  1049. case 3:
  1050. for (int x = 0; x < widthOut * 3; x += 3)
  1051. {
  1052. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 3]) >> 1);
  1053. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 4]) >> 1);
  1054. pixelDataOut[x + 2] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 2] + pixelDataIn[x * 2 + 5]) >> 1);
  1055. }
  1056. break;
  1057. case 4:
  1058. for (int x = 0; x < widthOut * 4; x += 4)
  1059. {
  1060. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 4]) >> 1);
  1061. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 5]) >> 1);
  1062. pixelDataOut[x + 2] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 2] + pixelDataIn[x * 2 + 6]) >> 1);
  1063. pixelDataOut[x + 3] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 3] + pixelDataIn[x * 2 + 7]) >> 1);
  1064. }
  1065. break;
  1066. default:
  1067. assert(false); // Should never reach here
  1068. break;
  1069. }
  1070. }
  1071. // 2D case
  1072. else if (depth_ == 1)
  1073. {
  1074. switch (components_)
  1075. {
  1076. case 1:
  1077. for (int y = 0; y < heightOut; ++y)
  1078. {
  1079. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_];
  1080. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_];
  1081. unsigned char* out = &pixelDataOut[y * widthOut];
  1082. for (int x = 0; x < widthOut; ++x)
  1083. {
  1084. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 1] +
  1085. inLower[x * 2] + inLower[x * 2 + 1]) >> 2);
  1086. }
  1087. }
  1088. break;
  1089. case 2:
  1090. for (int y = 0; y < heightOut; ++y)
  1091. {
  1092. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 2];
  1093. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 2];
  1094. unsigned char* out = &pixelDataOut[y * widthOut * 2];
  1095. for (int x = 0; x < widthOut * 2; x += 2)
  1096. {
  1097. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 2] +
  1098. inLower[x * 2] + inLower[x * 2 + 2]) >> 2);
  1099. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 3] +
  1100. inLower[x * 2 + 1] + inLower[x * 2 + 3]) >> 2);
  1101. }
  1102. }
  1103. break;
  1104. case 3:
  1105. for (int y = 0; y < heightOut; ++y)
  1106. {
  1107. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 3];
  1108. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 3];
  1109. unsigned char* out = &pixelDataOut[y * widthOut * 3];
  1110. for (int x = 0; x < widthOut * 3; x += 3)
  1111. {
  1112. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 3] +
  1113. inLower[x * 2] + inLower[x * 2 + 3]) >> 2);
  1114. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 4] +
  1115. inLower[x * 2 + 1] + inLower[x * 2 + 4]) >> 2);
  1116. out[x + 2] = (unsigned char)(((unsigned)inUpper[x * 2 + 2] + inUpper[x * 2 + 5] +
  1117. inLower[x * 2 + 2] + inLower[x * 2 + 5]) >> 2);
  1118. }
  1119. }
  1120. break;
  1121. case 4:
  1122. for (int y = 0; y < heightOut; ++y)
  1123. {
  1124. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 4];
  1125. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 4];
  1126. unsigned char* out = &pixelDataOut[y * widthOut * 4];
  1127. for (int x = 0; x < widthOut * 4; x += 4)
  1128. {
  1129. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 4] +
  1130. inLower[x * 2] + inLower[x * 2 + 4]) >> 2);
  1131. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 5] +
  1132. inLower[x * 2 + 1] + inLower[x * 2 + 5]) >> 2);
  1133. out[x + 2] = (unsigned char)(((unsigned)inUpper[x * 2 + 2] + inUpper[x * 2 + 6] +
  1134. inLower[x * 2 + 2] + inLower[x * 2 + 6]) >> 2);
  1135. out[x + 3] = (unsigned char)(((unsigned)inUpper[x * 2 + 3] + inUpper[x * 2 + 7] +
  1136. inLower[x * 2 + 3] + inLower[x * 2 + 7]) >> 2);
  1137. }
  1138. }
  1139. break;
  1140. default:
  1141. assert(false); // Should never reach here
  1142. break;
  1143. }
  1144. }
  1145. // 3D case
  1146. else
  1147. {
  1148. switch (components_)
  1149. {
  1150. case 1:
  1151. for (int z = 0; z < depthOut; ++z)
  1152. {
  1153. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_];
  1154. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_];
  1155. for (int y = 0; y < heightOut; ++y)
  1156. {
  1157. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_];
  1158. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_];
  1159. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_];
  1160. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_];
  1161. unsigned char* out = &pixelDataOut[z * widthOut * heightOut + y * widthOut];
  1162. for (int x = 0; x < widthOut; ++x)
  1163. {
  1164. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 1] +
  1165. inOuterLower[x * 2] + inOuterLower[x * 2 + 1] +
  1166. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 1] +
  1167. inInnerLower[x * 2] + inInnerLower[x * 2 + 1]) >> 3);
  1168. }
  1169. }
  1170. }
  1171. break;
  1172. case 2:
  1173. for (int z = 0; z < depthOut; ++z)
  1174. {
  1175. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 2];
  1176. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 2];
  1177. for (int y = 0; y < heightOut; ++y)
  1178. {
  1179. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 2];
  1180. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 2];
  1181. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 2];
  1182. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 2];
  1183. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 2 + y * widthOut * 2];
  1184. for (int x = 0; x < widthOut * 2; x += 2)
  1185. {
  1186. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 2] +
  1187. inOuterLower[x * 2] + inOuterLower[x * 2 + 2] +
  1188. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 2] +
  1189. inInnerLower[x * 2] + inInnerLower[x * 2 + 2]) >> 3);
  1190. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 3] +
  1191. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 3] +
  1192. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 3] +
  1193. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 3]) >> 3);
  1194. }
  1195. }
  1196. }
  1197. break;
  1198. case 3:
  1199. for (int z = 0; z < depthOut; ++z)
  1200. {
  1201. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 3];
  1202. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 3];
  1203. for (int y = 0; y < heightOut; ++y)
  1204. {
  1205. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 3];
  1206. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 3];
  1207. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 3];
  1208. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 3];
  1209. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 3 + y * widthOut * 3];
  1210. for (int x = 0; x < widthOut * 3; x += 3)
  1211. {
  1212. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 3] +
  1213. inOuterLower[x * 2] + inOuterLower[x * 2 + 3] +
  1214. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 3] +
  1215. inInnerLower[x * 2] + inInnerLower[x * 2 + 3]) >> 3);
  1216. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 4] +
  1217. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 4] +
  1218. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 4] +
  1219. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 4]) >> 3);
  1220. out[x + 2] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 2] + inOuterUpper[x * 2 + 5] +
  1221. inOuterLower[x * 2 + 2] + inOuterLower[x * 2 + 5] +
  1222. inInnerUpper[x * 2 + 2] + inInnerUpper[x * 2 + 5] +
  1223. inInnerLower[x * 2 + 2] + inInnerLower[x * 2 + 5]) >> 3);
  1224. }
  1225. }
  1226. }
  1227. break;
  1228. case 4:
  1229. for (int z = 0; z < depthOut; ++z)
  1230. {
  1231. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 4];
  1232. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 4];
  1233. for (int y = 0; y < heightOut; ++y)
  1234. {
  1235. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 4];
  1236. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 4];
  1237. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 4];
  1238. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 4];
  1239. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 4 + y * widthOut * 4];
  1240. for (int x = 0; x < widthOut * 4; x += 4)
  1241. {
  1242. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 4] +
  1243. inOuterLower[x * 2] + inOuterLower[x * 2 + 4] +
  1244. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 4] +
  1245. inInnerLower[x * 2] + inInnerLower[x * 2 + 4]) >> 3);
  1246. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 5] +
  1247. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 5] +
  1248. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 5] +
  1249. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 5]) >> 3);
  1250. out[x + 2] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 2] + inOuterUpper[x * 2 + 6] +
  1251. inOuterLower[x * 2 + 2] + inOuterLower[x * 2 + 6] +
  1252. inInnerUpper[x * 2 + 2] + inInnerUpper[x * 2 + 6] +
  1253. inInnerLower[x * 2 + 2] + inInnerLower[x * 2 + 6]) >> 3);
  1254. }
  1255. }
  1256. }
  1257. break;
  1258. default:
  1259. assert(false); // Should never reach here
  1260. break;
  1261. }
  1262. }
  1263. return mipImage;
  1264. }
  1265. SharedPtr<Image> Image::ConvertToRGBA() const
  1266. {
  1267. if (IsCompressed())
  1268. {
  1269. LOGERROR("Can not convert compressed image to RGBA");
  1270. return SharedPtr<Image>();
  1271. }
  1272. if (components_ < 1 || components_ > 4)
  1273. {
  1274. LOGERROR("Illegal number of image components for conversion to RGBA");
  1275. return SharedPtr<Image>();
  1276. }
  1277. if (!data_)
  1278. {
  1279. LOGERROR("Can not convert image without data to RGBA");
  1280. return SharedPtr<Image>();
  1281. }
  1282. // Already RGBA?
  1283. if (components_ == 4)
  1284. return SharedPtr<Image>(const_cast<Image*>(this));
  1285. SharedPtr<Image> ret(new Image(context_));
  1286. ret->SetSize(width_, height_, depth_, 4);
  1287. const unsigned char* src = data_;
  1288. unsigned char* dest = ret->GetData();
  1289. switch (components_)
  1290. {
  1291. case 1:
  1292. for (unsigned i = 0; i < width_ * height_ * depth_; ++i)
  1293. {
  1294. unsigned char pixel = *src++;
  1295. *dest++ = pixel;
  1296. *dest++ = pixel;
  1297. *dest++ = pixel;
  1298. *dest++ = 255;
  1299. }
  1300. break;
  1301. case 2:
  1302. for (unsigned i = 0; i < width_ * height_ * depth_; ++i)
  1303. {
  1304. unsigned char pixel = *src++;
  1305. *dest++ = pixel;
  1306. *dest++ = pixel;
  1307. *dest++ = pixel;
  1308. *dest++ = *src++;
  1309. }
  1310. break;
  1311. case 3:
  1312. for (unsigned i = 0; i < width_ * height_ * depth_; ++i)
  1313. {
  1314. *dest++ = *src++;
  1315. *dest++ = *src++;
  1316. *dest++ = *src++;
  1317. *dest++ = 255;
  1318. }
  1319. break;
  1320. default:
  1321. assert(false); // Should never reach nere
  1322. break;
  1323. }
  1324. return ret;
  1325. }
  1326. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  1327. {
  1328. CompressedLevel level;
  1329. if (compressedFormat_ == CF_NONE)
  1330. {
  1331. LOGERROR("Image is not compressed");
  1332. return level;
  1333. }
  1334. if (index >= numCompressedLevels_)
  1335. {
  1336. LOGERROR("Compressed image mip level out of bounds");
  1337. return level;
  1338. }
  1339. level.format_ = compressedFormat_;
  1340. level.width_ = width_;
  1341. level.height_ = height_;
  1342. level.depth_ = depth_;
  1343. if (compressedFormat_ == CF_RGBA)
  1344. {
  1345. level.blockSize_ = 4;
  1346. unsigned i = 0;
  1347. unsigned offset = 0;
  1348. for (;;)
  1349. {
  1350. if (!level.width_)
  1351. level.width_ = 1;
  1352. if (!level.height_)
  1353. level.height_ = 1;
  1354. if (!level.depth_)
  1355. level.depth_ = 1;
  1356. level.rowSize_ = level.width_ * level.blockSize_;
  1357. level.rows_ = (unsigned)level.height_;
  1358. level.data_ = data_.Get() + offset;
  1359. level.dataSize_ = level.depth_ * level.rows_ * level.rowSize_;
  1360. if (offset + level.dataSize_ > GetMemoryUse())
  1361. {
  1362. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1363. " Datasize: " + String(GetMemoryUse()));
  1364. level.data_ = 0;
  1365. return level;
  1366. }
  1367. if (i == index)
  1368. return level;
  1369. offset += level.dataSize_;
  1370. level.width_ /= 2;
  1371. level.height_ /= 2;
  1372. level.depth_ /= 2;
  1373. ++i;
  1374. }
  1375. }
  1376. else if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  1377. {
  1378. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  1379. unsigned i = 0;
  1380. unsigned offset = 0;
  1381. for (;;)
  1382. {
  1383. if (!level.width_)
  1384. level.width_ = 1;
  1385. if (!level.height_)
  1386. level.height_ = 1;
  1387. if (!level.depth_)
  1388. level.depth_ = 1;
  1389. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  1390. level.rows_ = (unsigned)((level.height_ + 3) / 4);
  1391. level.data_ = data_.Get() + offset;
  1392. level.dataSize_ = level.depth_ * level.rows_ * level.rowSize_;
  1393. if (offset + level.dataSize_ > GetMemoryUse())
  1394. {
  1395. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1396. " Datasize: " + String(GetMemoryUse()));
  1397. level.data_ = 0;
  1398. return level;
  1399. }
  1400. if (i == index)
  1401. return level;
  1402. offset += level.dataSize_;
  1403. level.width_ /= 2;
  1404. level.height_ /= 2;
  1405. level.depth_ /= 2;
  1406. ++i;
  1407. }
  1408. }
  1409. else
  1410. {
  1411. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  1412. unsigned i = 0;
  1413. unsigned offset = 0;
  1414. for (;;)
  1415. {
  1416. if (!level.width_)
  1417. level.width_ = 1;
  1418. if (!level.height_)
  1419. level.height_ = 1;
  1420. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  1421. int dataHeight = Max(level.height_, 8);
  1422. level.data_ = data_.Get() + offset;
  1423. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  1424. level.rows_ = (unsigned)dataHeight;
  1425. level.rowSize_ = level.dataSize_ / level.rows_;
  1426. if (offset + level.dataSize_ > GetMemoryUse())
  1427. {
  1428. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1429. " Datasize: " + String(GetMemoryUse()));
  1430. level.data_ = 0;
  1431. return level;
  1432. }
  1433. if (i == index)
  1434. return level;
  1435. offset += level.dataSize_;
  1436. level.width_ /= 2;
  1437. level.height_ /= 2;
  1438. ++i;
  1439. }
  1440. }
  1441. }
  1442. Image* Image::GetSubimage(const IntRect& rect) const
  1443. {
  1444. if (!data_)
  1445. return 0;
  1446. if (depth_ > 1)
  1447. {
  1448. LOGERROR("Subimage not supported for 3D images");
  1449. return 0;
  1450. }
  1451. if (rect.left_ < 0 || rect.top_ < 0 || rect.right_ > width_ || rect.bottom_ > height_ || !rect.Width() || !rect.Height())
  1452. {
  1453. LOGERROR("Can not get subimage from image " + GetName() + " with invalid region");
  1454. return 0;
  1455. }
  1456. if (!IsCompressed())
  1457. {
  1458. int x = rect.left_;
  1459. int y = rect.top_;
  1460. int width = rect.Width();
  1461. int height = rect.Height();
  1462. Image* image = new Image(context_);
  1463. image->SetSize(width, height, components_);
  1464. unsigned char* dest = image->GetData();
  1465. unsigned char* src = data_.Get() + (y * width_ + x) * components_;
  1466. for (int i = 0; i < height; ++i)
  1467. {
  1468. memcpy(dest, src, width * components_);
  1469. dest += width * components_;
  1470. src += width_ * components_;
  1471. }
  1472. return image;
  1473. }
  1474. else
  1475. {
  1476. // Pad the region to be a multiple of block size
  1477. IntRect paddedRect = rect;
  1478. paddedRect.left_ = (rect.left_ / 4) * 4;
  1479. paddedRect.top_ = (rect.top_ / 4) * 4;
  1480. paddedRect.right_ = (rect.right_ / 4) * 4;
  1481. paddedRect.bottom_ = (rect.bottom_ / 4) * 4;
  1482. IntRect currentRect = paddedRect;
  1483. PODVector<unsigned char> subimageData;
  1484. unsigned subimageLevels = 0;
  1485. // Save as many mips as possible until the next mip would cross a block boundary
  1486. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  1487. {
  1488. CompressedLevel level = GetCompressedLevel(i);
  1489. if (!level.data_)
  1490. break;
  1491. // Mips are stored continuously
  1492. unsigned destStartOffset = subimageData.Size();
  1493. unsigned destRowSize = currentRect.Width() / 4 * level.blockSize_;
  1494. unsigned destSize = currentRect.Height() / 4 * destRowSize;
  1495. if (!destSize)
  1496. break;
  1497. subimageData.Resize(destStartOffset + destSize);
  1498. unsigned char* dest = &subimageData[destStartOffset];
  1499. for (int y = currentRect.top_; y < currentRect.bottom_; y += 4)
  1500. {
  1501. unsigned char* src = level.data_ + level.rowSize_ * (y / 4) + currentRect.left_ / 4 * level.blockSize_;
  1502. memcpy(dest, src, destRowSize);
  1503. dest += destRowSize;
  1504. }
  1505. ++subimageLevels;
  1506. if ((currentRect.left_ & 4) || (currentRect.right_ & 4) || (currentRect.top_ & 4) || (currentRect.bottom_ & 4))
  1507. break;
  1508. else
  1509. {
  1510. currentRect.left_ /= 2;
  1511. currentRect.right_ /= 2;
  1512. currentRect.top_ /= 2;
  1513. currentRect.bottom_ /= 2;
  1514. }
  1515. }
  1516. if (!subimageLevels)
  1517. {
  1518. LOGERROR("Subimage region from compressed image " + GetName() + " did not produce any data");
  1519. return 0;
  1520. }
  1521. Image* image = new Image(context_);
  1522. image->width_ = paddedRect.Width();
  1523. image->height_ = paddedRect.Height();
  1524. image->depth_ = 1;
  1525. image->compressedFormat_ = compressedFormat_;
  1526. image->numCompressedLevels_ = subimageLevels;
  1527. image->components_ = components_;
  1528. image->data_ = new unsigned char[subimageData.Size()];
  1529. memcpy(image->data_.Get(), &subimageData[0], subimageData.Size());
  1530. image->SetMemoryUse(subimageData.Size());
  1531. return image;
  1532. }
  1533. }
  1534. SDL_Surface* Image::GetSDLSurface(const IntRect& rect) const
  1535. {
  1536. if (!data_)
  1537. return 0;
  1538. if (depth_ > 1)
  1539. {
  1540. LOGERROR("Can not get SDL surface from 3D image");
  1541. return 0;
  1542. }
  1543. if (IsCompressed())
  1544. {
  1545. LOGERROR("Can not get SDL surface from compressed image " + GetName());
  1546. return 0;
  1547. }
  1548. if (components_ < 3)
  1549. {
  1550. LOGERROR("Can not get SDL surface from image " + GetName() + " with less than 3 components");
  1551. return 0;
  1552. }
  1553. IntRect imageRect = rect;
  1554. // Use full image if illegal rect
  1555. if (imageRect.left_ < 0 || imageRect.top_ < 0 || imageRect.right_ > width_ || imageRect.bottom_ > height_ ||
  1556. imageRect.left_ >= imageRect.right_ || imageRect.top_ >= imageRect.bottom_)
  1557. {
  1558. imageRect.left_ = 0;
  1559. imageRect.top_ = 0;
  1560. imageRect.right_ = width_;
  1561. imageRect.bottom_ = height_;
  1562. }
  1563. int imageWidth = width_;
  1564. int width = imageRect.Width();
  1565. int height = imageRect.Height();
  1566. // Assume little-endian for all the supported platforms
  1567. unsigned rMask = 0x000000ff;
  1568. unsigned gMask = 0x0000ff00;
  1569. unsigned bMask = 0x00ff0000;
  1570. unsigned aMask = 0xff000000;
  1571. SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, components_ * 8, rMask, gMask, bMask, aMask);
  1572. if (surface)
  1573. {
  1574. SDL_LockSurface(surface);
  1575. unsigned char* destination = reinterpret_cast<unsigned char*>(surface->pixels);
  1576. unsigned char* source = data_ + components_ * (imageWidth * imageRect.top_ + imageRect.left_);
  1577. for (int i = 0; i < height; ++i)
  1578. {
  1579. memcpy(destination, source, components_ * width);
  1580. destination += surface->pitch;
  1581. source += components_ * imageWidth;
  1582. }
  1583. SDL_UnlockSurface(surface);
  1584. }
  1585. else
  1586. LOGERROR("Failed to create SDL surface from image " + GetName());
  1587. return surface;
  1588. }
  1589. void Image::PrecalculateLevels()
  1590. {
  1591. if (!data_ || IsCompressed())
  1592. return;
  1593. PROFILE(PrecalculateImageMipLevels);
  1594. nextLevel_.Reset();
  1595. if (width_ > 1 || height_ > 1)
  1596. {
  1597. SharedPtr<Image> current = GetNextLevel();
  1598. nextLevel_ = current;
  1599. while (current && (current->width_ > 1 || current->height_ > 1))
  1600. {
  1601. current->nextLevel_ = current->GetNextLevel();
  1602. current = current->nextLevel_;
  1603. }
  1604. }
  1605. }
  1606. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  1607. {
  1608. unsigned dataSize = source.GetSize();
  1609. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  1610. source.Read(buffer.Get(), dataSize);
  1611. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int*)&components, 0);
  1612. }
  1613. void Image::FreeImageData(unsigned char* pixelData)
  1614. {
  1615. if (!pixelData)
  1616. return;
  1617. stbi_image_free(pixelData);
  1618. }
  1619. }