Image.cpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. //
  2. // Copyright (c) 2008-2018 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 <SDL/SDL_surface.h>
  30. #define STB_IMAGE_IMPLEMENTATION
  31. #include <STB/stb_image.h>
  32. #define STB_IMAGE_WRITE_IMPLEMENTATION
  33. #include <STB/stb_image_write.h>
  34. #ifdef URHO3D_WEBP
  35. #include <webp/decode.h>
  36. #include <webp/encode.h>
  37. #include <webp/mux.h>
  38. #endif
  39. #include "../DebugNew.h"
  40. #ifndef MAKEFOURCC
  41. #define MAKEFOURCC(ch0, ch1, ch2, ch3) ((unsigned)(ch0) | ((unsigned)(ch1) << 8) | ((unsigned)(ch2) << 16) | ((unsigned)(ch3) << 24))
  42. #endif
  43. #define FOURCC_DXT1 (MAKEFOURCC('D','X','T','1'))
  44. #define FOURCC_DXT2 (MAKEFOURCC('D','X','T','2'))
  45. #define FOURCC_DXT3 (MAKEFOURCC('D','X','T','3'))
  46. #define FOURCC_DXT4 (MAKEFOURCC('D','X','T','4'))
  47. #define FOURCC_DXT5 (MAKEFOURCC('D','X','T','5'))
  48. #define FOURCC_DX10 (MAKEFOURCC('D','X','1','0'))
  49. static const unsigned DDSCAPS_COMPLEX = 0x00000008U;
  50. static const unsigned DDSCAPS_TEXTURE = 0x00001000U;
  51. static const unsigned DDSCAPS_MIPMAP = 0x00400000U;
  52. static const unsigned DDSCAPS2_VOLUME = 0x00200000U;
  53. static const unsigned DDSCAPS2_CUBEMAP = 0x00000200U;
  54. static const unsigned DDSCAPS2_CUBEMAP_POSITIVEX = 0x00000400U;
  55. static const unsigned DDSCAPS2_CUBEMAP_NEGATIVEX = 0x00000800U;
  56. static const unsigned DDSCAPS2_CUBEMAP_POSITIVEY = 0x00001000U;
  57. static const unsigned DDSCAPS2_CUBEMAP_NEGATIVEY = 0x00002000U;
  58. static const unsigned DDSCAPS2_CUBEMAP_POSITIVEZ = 0x00004000U;
  59. static const unsigned DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x00008000U;
  60. static const unsigned DDSCAPS2_CUBEMAP_ALL_FACES = 0x0000FC00U;
  61. // DX10 flags
  62. static const unsigned DDS_DIMENSION_TEXTURE1D = 2;
  63. static const unsigned DDS_DIMENSION_TEXTURE2D = 3;
  64. static const unsigned DDS_DIMENSION_TEXTURE3D = 4;
  65. static const unsigned DDS_RESOURCE_MISC_TEXTURECUBE = 0x4;
  66. static const unsigned DDS_DXGI_FORMAT_R8G8B8A8_UNORM = 28;
  67. static const unsigned DDS_DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 26;
  68. static const unsigned DDS_DXGI_FORMAT_BC1_UNORM = 71;
  69. static const unsigned DDS_DXGI_FORMAT_BC1_UNORM_SRGB = 72;
  70. static const unsigned DDS_DXGI_FORMAT_BC2_UNORM = 74;
  71. static const unsigned DDS_DXGI_FORMAT_BC2_UNORM_SRGB = 75;
  72. static const unsigned DDS_DXGI_FORMAT_BC3_UNORM = 77;
  73. static const unsigned DDS_DXGI_FORMAT_BC3_UNORM_SRGB = 78;
  74. namespace Urho3D
  75. {
  76. /// DirectDraw color key definition.
  77. struct DDColorKey
  78. {
  79. unsigned dwColorSpaceLowValue_;
  80. unsigned dwColorSpaceHighValue_;
  81. };
  82. /// DirectDraw pixel format definition.
  83. struct DDPixelFormat
  84. {
  85. unsigned dwSize_;
  86. unsigned dwFlags_;
  87. unsigned dwFourCC_;
  88. union
  89. {
  90. unsigned dwRGBBitCount_;
  91. unsigned dwYUVBitCount_;
  92. unsigned dwZBufferBitDepth_;
  93. unsigned dwAlphaBitDepth_;
  94. unsigned dwLuminanceBitCount_;
  95. unsigned dwBumpBitCount_;
  96. unsigned dwPrivateFormatBitCount_;
  97. };
  98. union
  99. {
  100. unsigned dwRBitMask_;
  101. unsigned dwYBitMask_;
  102. unsigned dwStencilBitDepth_;
  103. unsigned dwLuminanceBitMask_;
  104. unsigned dwBumpDuBitMask_;
  105. unsigned dwOperations_;
  106. };
  107. union
  108. {
  109. unsigned dwGBitMask_;
  110. unsigned dwUBitMask_;
  111. unsigned dwZBitMask_;
  112. unsigned dwBumpDvBitMask_;
  113. struct
  114. {
  115. unsigned short wFlipMSTypes_;
  116. unsigned short wBltMSTypes_;
  117. } multiSampleCaps_;
  118. };
  119. union
  120. {
  121. unsigned dwBBitMask_;
  122. unsigned dwVBitMask_;
  123. unsigned dwStencilBitMask_;
  124. unsigned dwBumpLuminanceBitMask_;
  125. };
  126. union
  127. {
  128. unsigned dwRGBAlphaBitMask_;
  129. unsigned dwYUVAlphaBitMask_;
  130. unsigned dwLuminanceAlphaBitMask_;
  131. unsigned dwRGBZBitMask_;
  132. unsigned dwYUVZBitMask_;
  133. };
  134. };
  135. /// DirectDraw surface capabilities.
  136. struct DDSCaps2
  137. {
  138. unsigned dwCaps_;
  139. unsigned dwCaps2_;
  140. unsigned dwCaps3_;
  141. union
  142. {
  143. unsigned dwCaps4_;
  144. unsigned dwVolumeDepth_;
  145. };
  146. };
  147. struct DDSHeader10
  148. {
  149. unsigned dxgiFormat;
  150. unsigned resourceDimension;
  151. unsigned miscFlag;
  152. unsigned arraySize;
  153. unsigned reserved;
  154. };
  155. /// DirectDraw surface description.
  156. struct DDSurfaceDesc2
  157. {
  158. unsigned dwSize_;
  159. unsigned dwFlags_;
  160. unsigned dwHeight_;
  161. unsigned dwWidth_;
  162. union
  163. {
  164. unsigned lPitch_;
  165. unsigned dwLinearSize_;
  166. };
  167. union
  168. {
  169. unsigned dwBackBufferCount_;
  170. unsigned dwDepth_;
  171. };
  172. union
  173. {
  174. unsigned dwMipMapCount_;
  175. unsigned dwRefreshRate_;
  176. unsigned dwSrcVBHandle_;
  177. };
  178. unsigned dwAlphaBitDepth_;
  179. unsigned dwReserved_;
  180. unsigned lpSurface_; // Do not define as a void pointer, as it is 8 bytes in a 64bit build
  181. union
  182. {
  183. DDColorKey ddckCKDestOverlay_;
  184. unsigned dwEmptyFaceColor_;
  185. };
  186. DDColorKey ddckCKDestBlt_;
  187. DDColorKey ddckCKSrcOverlay_;
  188. DDColorKey ddckCKSrcBlt_;
  189. union
  190. {
  191. DDPixelFormat ddpfPixelFormat_;
  192. unsigned dwFVF_;
  193. };
  194. DDSCaps2 ddsCaps_;
  195. unsigned dwTextureStage_;
  196. };
  197. bool CompressedLevel::Decompress(unsigned char* dest)
  198. {
  199. if (!data_)
  200. return false;
  201. switch (format_)
  202. {
  203. case CF_DXT1:
  204. case CF_DXT3:
  205. case CF_DXT5:
  206. DecompressImageDXT(dest, data_, width_, height_, depth_, format_);
  207. return true;
  208. case CF_ETC1:
  209. DecompressImageETC(dest, data_, width_, height_);
  210. return true;
  211. case CF_PVRTC_RGB_2BPP:
  212. case CF_PVRTC_RGBA_2BPP:
  213. case CF_PVRTC_RGB_4BPP:
  214. case CF_PVRTC_RGBA_4BPP:
  215. DecompressImagePVRTC(dest, data_, width_, height_, format_);
  216. return true;
  217. default:
  218. // Unknown format
  219. return false;
  220. }
  221. }
  222. Image::Image(Context* context) :
  223. Resource(context)
  224. {
  225. }
  226. Image::~Image() = default;
  227. void Image::RegisterObject(Context* context)
  228. {
  229. context->RegisterFactory<Image>();
  230. }
  231. bool Image::BeginLoad(Deserializer& source)
  232. {
  233. // Check for DDS, KTX or PVR compressed format
  234. String fileID = source.ReadFileID();
  235. if (fileID == "DDS ")
  236. {
  237. // DDS compressed format
  238. DDSurfaceDesc2 ddsd; // NOLINT(hicpp-member-init)
  239. source.Read(&ddsd, sizeof(ddsd));
  240. // DDS DX10+
  241. const bool hasDXGI = ddsd.ddpfPixelFormat_.dwFourCC_ == FOURCC_DX10;
  242. DDSHeader10 dxgiHeader; // NOLINT(hicpp-member-init)
  243. if (hasDXGI)
  244. source.Read(&dxgiHeader, sizeof(dxgiHeader));
  245. unsigned fourCC = ddsd.ddpfPixelFormat_.dwFourCC_;
  246. // If the DXGI header is available then remap formats and check sRGB
  247. if (hasDXGI)
  248. {
  249. switch (dxgiHeader.dxgiFormat)
  250. {
  251. case DDS_DXGI_FORMAT_BC1_UNORM:
  252. case DDS_DXGI_FORMAT_BC1_UNORM_SRGB:
  253. fourCC = FOURCC_DXT1;
  254. break;
  255. case DDS_DXGI_FORMAT_BC2_UNORM:
  256. case DDS_DXGI_FORMAT_BC2_UNORM_SRGB:
  257. fourCC = FOURCC_DXT3;
  258. break;
  259. case DDS_DXGI_FORMAT_BC3_UNORM:
  260. case DDS_DXGI_FORMAT_BC3_UNORM_SRGB:
  261. fourCC = FOURCC_DXT5;
  262. break;
  263. case DDS_DXGI_FORMAT_R8G8B8A8_UNORM:
  264. case DDS_DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
  265. fourCC = 0;
  266. break;
  267. default:
  268. URHO3D_LOGERROR("Unrecognized DDS DXGI image format");
  269. return false;
  270. }
  271. // Check the internal sRGB formats
  272. if (dxgiHeader.dxgiFormat == DDS_DXGI_FORMAT_BC1_UNORM_SRGB ||
  273. dxgiHeader.dxgiFormat == DDS_DXGI_FORMAT_BC2_UNORM_SRGB ||
  274. dxgiHeader.dxgiFormat == DDS_DXGI_FORMAT_BC3_UNORM_SRGB ||
  275. dxgiHeader.dxgiFormat == DDS_DXGI_FORMAT_R8G8B8A8_UNORM_SRGB)
  276. {
  277. sRGB_ = true;
  278. }
  279. }
  280. switch (fourCC)
  281. {
  282. case FOURCC_DXT1:
  283. compressedFormat_ = CF_DXT1;
  284. components_ = 3;
  285. break;
  286. case FOURCC_DXT3:
  287. compressedFormat_ = CF_DXT3;
  288. components_ = 4;
  289. break;
  290. case FOURCC_DXT5:
  291. compressedFormat_ = CF_DXT5;
  292. components_ = 4;
  293. break;
  294. case 0:
  295. if (ddsd.ddpfPixelFormat_.dwRGBBitCount_ != 32 && ddsd.ddpfPixelFormat_.dwRGBBitCount_ != 24 &&
  296. ddsd.ddpfPixelFormat_.dwRGBBitCount_ != 16)
  297. {
  298. URHO3D_LOGERROR("Unsupported DDS pixel byte size");
  299. return false;
  300. }
  301. compressedFormat_ = CF_RGBA;
  302. components_ = 4;
  303. break;
  304. default:
  305. URHO3D_LOGERROR("Unrecognized DDS image format");
  306. return false;
  307. }
  308. // Is it a cube map or texture array? If so determine the size of the image chain.
  309. cubemap_ = (ddsd.ddsCaps_.dwCaps2_ & DDSCAPS2_CUBEMAP_ALL_FACES) != 0 || (hasDXGI && (dxgiHeader.miscFlag & DDS_RESOURCE_MISC_TEXTURECUBE) != 0);
  310. unsigned imageChainCount = 1;
  311. if (cubemap_)
  312. imageChainCount = 6;
  313. else if (hasDXGI && dxgiHeader.arraySize > 1)
  314. {
  315. imageChainCount = dxgiHeader.arraySize;
  316. array_ = true;
  317. }
  318. // Calculate the size of the data
  319. unsigned dataSize = 0;
  320. if (compressedFormat_ != CF_RGBA)
  321. {
  322. const unsigned blockSize = compressedFormat_ == CF_DXT1 ? 8 : 16; //DXT1/BC1 is 8 bytes, DXT3/BC2 and DXT5/BC3 are 16 bytes
  323. // Add 3 to ensure valid block: ie 2x2 fits uses a whole 4x4 block
  324. unsigned blocksWide = (ddsd.dwWidth_ + 3) / 4;
  325. unsigned blocksHeight = (ddsd.dwHeight_ + 3) / 4;
  326. dataSize = blocksWide * blocksHeight * blockSize;
  327. // Calculate mip data size
  328. unsigned x = ddsd.dwWidth_ / 2;
  329. unsigned y = ddsd.dwHeight_ / 2;
  330. unsigned z = ddsd.dwDepth_ / 2;
  331. for (unsigned level = ddsd.dwMipMapCount_; level > 1; x /= 2, y /= 2, z /= 2, --level)
  332. {
  333. blocksWide = (Max(x, 1U) + 3) / 4;
  334. blocksHeight = (Max(y, 1U) + 3) / 4;
  335. dataSize += blockSize * blocksWide * blocksHeight * Max(z, 1U);
  336. }
  337. }
  338. else
  339. {
  340. dataSize = (ddsd.ddpfPixelFormat_.dwRGBBitCount_ / 8) * ddsd.dwWidth_ * ddsd.dwHeight_ * Max(ddsd.dwDepth_, 1U);
  341. // Calculate mip data size
  342. unsigned x = ddsd.dwWidth_ / 2;
  343. unsigned y = ddsd.dwHeight_ / 2;
  344. unsigned z = ddsd.dwDepth_ / 2;
  345. for (unsigned level = ddsd.dwMipMapCount_; level > 1; x /= 2, y /= 2, z /= 2, --level)
  346. dataSize += (ddsd.ddpfPixelFormat_.dwRGBBitCount_ / 8) * Max(x, 1U) * Max(y, 1U) * Max(z, 1U);
  347. }
  348. // Do not use a shared ptr here, in case nothing is refcounting the image outside this function.
  349. // A raw pointer is fine as the image chain (if needed) uses shared ptr's properly
  350. Image* currentImage = this;
  351. for (unsigned faceIndex = 0; faceIndex < imageChainCount; ++faceIndex)
  352. {
  353. currentImage->data_ = new unsigned char[dataSize];
  354. currentImage->cubemap_ = cubemap_;
  355. currentImage->array_ = array_;
  356. currentImage->components_ = components_;
  357. currentImage->compressedFormat_ = compressedFormat_;
  358. currentImage->width_ = ddsd.dwWidth_;
  359. currentImage->height_ = ddsd.dwHeight_;
  360. currentImage->depth_ = ddsd.dwDepth_;
  361. currentImage->numCompressedLevels_ = ddsd.dwMipMapCount_;
  362. if (!currentImage->numCompressedLevels_)
  363. currentImage->numCompressedLevels_ = 1;
  364. // Memory use needs to be exact per image as it's used for verifying the data size in GetCompressedLevel()
  365. // even though it would be more proper for the first image to report the size of all siblings combined
  366. currentImage->SetMemoryUse(dataSize);
  367. source.Read(currentImage->data_.Get(), dataSize);
  368. if (faceIndex < imageChainCount - 1)
  369. {
  370. // Build the image chain
  371. SharedPtr<Image> nextImage(new Image(context_));
  372. currentImage->nextSibling_ = nextImage;
  373. currentImage = nextImage;
  374. }
  375. }
  376. // If uncompressed DDS, convert the data to 8bit RGBA as the texture classes can not currently use eg. RGB565 format
  377. if (compressedFormat_ == CF_RGBA)
  378. {
  379. URHO3D_PROFILE(ConvertDDSToRGBA);
  380. currentImage = this;
  381. while (currentImage)
  382. {
  383. unsigned sourcePixelByteSize = ddsd.ddpfPixelFormat_.dwRGBBitCount_ >> 3;
  384. unsigned numPixels = dataSize / sourcePixelByteSize;
  385. #define ADJUSTSHIFT(mask, l, r) \
  386. if ((mask) >= 0x100) \
  387. { \
  388. while (((mask) >> (r)) >= 0x100) \
  389. ++(r); \
  390. } \
  391. else if ((mask) && (mask) < 0x80) \
  392. { \
  393. while (((mask) << (l)) < 0x80) \
  394. ++(l); \
  395. }
  396. unsigned rShiftL = 0, gShiftL = 0, bShiftL = 0, aShiftL = 0;
  397. unsigned rShiftR = 0, gShiftR = 0, bShiftR = 0, aShiftR = 0;
  398. unsigned rMask = ddsd.ddpfPixelFormat_.dwRBitMask_;
  399. unsigned gMask = ddsd.ddpfPixelFormat_.dwGBitMask_;
  400. unsigned bMask = ddsd.ddpfPixelFormat_.dwBBitMask_;
  401. unsigned aMask = ddsd.ddpfPixelFormat_.dwRGBAlphaBitMask_;
  402. ADJUSTSHIFT(rMask, rShiftL, rShiftR)
  403. ADJUSTSHIFT(gMask, gShiftL, gShiftR)
  404. ADJUSTSHIFT(bMask, bShiftL, bShiftR)
  405. ADJUSTSHIFT(aMask, aShiftL, aShiftR)
  406. SharedArrayPtr<unsigned char> rgbaData(new unsigned char[numPixels * 4]);
  407. switch (sourcePixelByteSize)
  408. {
  409. case 4:
  410. {
  411. auto* src = (unsigned*)currentImage->data_.Get();
  412. unsigned char* dest = rgbaData.Get();
  413. while (numPixels--)
  414. {
  415. unsigned pixels = *src++;
  416. *dest++ = ((pixels & rMask) << rShiftL) >> rShiftR;
  417. *dest++ = ((pixels & gMask) << gShiftL) >> gShiftR;
  418. *dest++ = ((pixels & bMask) << bShiftL) >> bShiftR;
  419. *dest++ = ((pixels & aMask) << aShiftL) >> aShiftR;
  420. }
  421. }
  422. break;
  423. case 3:
  424. {
  425. unsigned char* src = currentImage->data_.Get();
  426. unsigned char* dest = rgbaData.Get();
  427. while (numPixels--)
  428. {
  429. unsigned pixels = src[0] | (src[1] << 8) | (src[2] << 16);
  430. src += 3;
  431. *dest++ = ((pixels & rMask) << rShiftL) >> rShiftR;
  432. *dest++ = ((pixels & gMask) << gShiftL) >> gShiftR;
  433. *dest++ = ((pixels & bMask) << bShiftL) >> bShiftR;
  434. *dest++ = ((pixels & aMask) << aShiftL) >> aShiftR;
  435. }
  436. }
  437. break;
  438. default:
  439. {
  440. auto* src = (unsigned short*)currentImage->data_.Get();
  441. unsigned char* dest = rgbaData.Get();
  442. while (numPixels--)
  443. {
  444. unsigned short pixels = *src++;
  445. *dest++ = ((pixels & rMask) << rShiftL) >> rShiftR;
  446. *dest++ = ((pixels & gMask) << gShiftL) >> gShiftR;
  447. *dest++ = ((pixels & bMask) << bShiftL) >> bShiftR;
  448. *dest++ = ((pixels & aMask) << aShiftL) >> aShiftR;
  449. }
  450. }
  451. break;
  452. }
  453. // Replace with converted data
  454. currentImage->data_ = rgbaData;
  455. currentImage->SetMemoryUse(numPixels * 4);
  456. currentImage = currentImage->GetNextSibling();
  457. }
  458. }
  459. }
  460. else if (fileID == "\253KTX")
  461. {
  462. source.Seek(12);
  463. unsigned endianness = source.ReadUInt();
  464. unsigned type = source.ReadUInt();
  465. /* unsigned typeSize = */ source.ReadUInt();
  466. unsigned format = source.ReadUInt();
  467. unsigned internalFormat = source.ReadUInt();
  468. /* unsigned baseInternalFormat = */ source.ReadUInt();
  469. unsigned width = source.ReadUInt();
  470. unsigned height = source.ReadUInt();
  471. unsigned depth = source.ReadUInt();
  472. /* unsigned arrayElements = */ source.ReadUInt();
  473. unsigned faces = source.ReadUInt();
  474. unsigned mipmaps = source.ReadUInt();
  475. unsigned keyValueBytes = source.ReadUInt();
  476. if (endianness != 0x04030201)
  477. {
  478. URHO3D_LOGERROR("Big-endian KTX files not supported");
  479. return false;
  480. }
  481. if (type != 0 || format != 0)
  482. {
  483. URHO3D_LOGERROR("Uncompressed KTX files not supported");
  484. return false;
  485. }
  486. if (faces > 1 || depth > 1)
  487. {
  488. URHO3D_LOGERROR("3D or cube KTX files not supported");
  489. return false;
  490. }
  491. if (mipmaps == 0)
  492. {
  493. URHO3D_LOGERROR("KTX files without explicitly specified mipmap count not supported");
  494. return false;
  495. }
  496. switch (internalFormat)
  497. {
  498. case 0x83f1:
  499. compressedFormat_ = CF_DXT1;
  500. components_ = 4;
  501. break;
  502. case 0x83f2:
  503. compressedFormat_ = CF_DXT3;
  504. components_ = 4;
  505. break;
  506. case 0x83f3:
  507. compressedFormat_ = CF_DXT5;
  508. components_ = 4;
  509. break;
  510. case 0x8d64:
  511. compressedFormat_ = CF_ETC1;
  512. components_ = 3;
  513. break;
  514. case 0x8c00:
  515. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  516. components_ = 3;
  517. break;
  518. case 0x8c01:
  519. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  520. components_ = 3;
  521. break;
  522. case 0x8c02:
  523. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  524. components_ = 4;
  525. break;
  526. case 0x8c03:
  527. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  528. components_ = 4;
  529. break;
  530. default:
  531. compressedFormat_ = CF_NONE;
  532. break;
  533. }
  534. if (compressedFormat_ == CF_NONE)
  535. {
  536. URHO3D_LOGERROR("Unsupported texture format in KTX file");
  537. return false;
  538. }
  539. source.Seek(source.GetPosition() + keyValueBytes);
  540. auto dataSize = (unsigned)(source.GetSize() - source.GetPosition() - mipmaps * sizeof(unsigned));
  541. data_ = new unsigned char[dataSize];
  542. width_ = width;
  543. height_ = height;
  544. numCompressedLevels_ = mipmaps;
  545. unsigned dataOffset = 0;
  546. for (unsigned i = 0; i < mipmaps; ++i)
  547. {
  548. unsigned levelSize = source.ReadUInt();
  549. if (levelSize + dataOffset > dataSize)
  550. {
  551. URHO3D_LOGERROR("KTX mipmap level data size exceeds file size");
  552. return false;
  553. }
  554. source.Read(&data_[dataOffset], levelSize);
  555. dataOffset += levelSize;
  556. if (source.GetPosition() & 3)
  557. source.Seek((source.GetPosition() + 3) & 0xfffffffc);
  558. }
  559. SetMemoryUse(dataSize);
  560. }
  561. else if (fileID == "PVR\3")
  562. {
  563. /* unsigned flags = */ source.ReadUInt();
  564. unsigned pixelFormatLo = source.ReadUInt();
  565. /* unsigned pixelFormatHi = */ source.ReadUInt();
  566. /* unsigned colourSpace = */ source.ReadUInt();
  567. /* unsigned channelType = */ source.ReadUInt();
  568. unsigned height = source.ReadUInt();
  569. unsigned width = source.ReadUInt();
  570. unsigned depth = source.ReadUInt();
  571. /* unsigned numSurfaces = */ source.ReadUInt();
  572. unsigned numFaces = source.ReadUInt();
  573. unsigned mipmapCount = source.ReadUInt();
  574. unsigned metaDataSize = source.ReadUInt();
  575. if (depth > 1 || numFaces > 1)
  576. {
  577. URHO3D_LOGERROR("3D or cube PVR files not supported");
  578. return false;
  579. }
  580. if (mipmapCount == 0)
  581. {
  582. URHO3D_LOGERROR("PVR files without explicitly specified mipmap count not supported");
  583. return false;
  584. }
  585. switch (pixelFormatLo)
  586. {
  587. case 0:
  588. compressedFormat_ = CF_PVRTC_RGB_2BPP;
  589. components_ = 3;
  590. break;
  591. case 1:
  592. compressedFormat_ = CF_PVRTC_RGBA_2BPP;
  593. components_ = 4;
  594. break;
  595. case 2:
  596. compressedFormat_ = CF_PVRTC_RGB_4BPP;
  597. components_ = 3;
  598. break;
  599. case 3:
  600. compressedFormat_ = CF_PVRTC_RGBA_4BPP;
  601. components_ = 4;
  602. break;
  603. case 6:
  604. compressedFormat_ = CF_ETC1;
  605. components_ = 3;
  606. break;
  607. case 7:
  608. compressedFormat_ = CF_DXT1;
  609. components_ = 4;
  610. break;
  611. case 9:
  612. compressedFormat_ = CF_DXT3;
  613. components_ = 4;
  614. break;
  615. case 11:
  616. compressedFormat_ = CF_DXT5;
  617. components_ = 4;
  618. break;
  619. default:
  620. compressedFormat_ = CF_NONE;
  621. break;
  622. }
  623. if (compressedFormat_ == CF_NONE)
  624. {
  625. URHO3D_LOGERROR("Unsupported texture format in PVR file");
  626. return false;
  627. }
  628. source.Seek(source.GetPosition() + metaDataSize);
  629. unsigned dataSize = source.GetSize() - source.GetPosition();
  630. data_ = new unsigned char[dataSize];
  631. width_ = width;
  632. height_ = height;
  633. numCompressedLevels_ = mipmapCount;
  634. source.Read(data_.Get(), dataSize);
  635. SetMemoryUse(dataSize);
  636. }
  637. #ifdef URHO3D_WEBP
  638. else if (fileID == "RIFF")
  639. {
  640. // WebP: https://developers.google.com/speed/webp/docs/api
  641. // RIFF layout is:
  642. // Offset tag
  643. // 0...3 "RIFF" 4-byte tag
  644. // 4...7 size of image data (including metadata) starting at offset 8
  645. // 8...11 "WEBP" our form-type signature
  646. const uint8_t TAG_SIZE(4);
  647. source.Seek(8);
  648. uint8_t fourCC[TAG_SIZE];
  649. memset(&fourCC, 0, sizeof(uint8_t) * TAG_SIZE);
  650. unsigned bytesRead(source.Read(&fourCC, TAG_SIZE));
  651. if (bytesRead != TAG_SIZE)
  652. {
  653. // Truncated.
  654. URHO3D_LOGERROR("Truncated RIFF data");
  655. return false;
  656. }
  657. const uint8_t WEBP[TAG_SIZE] = {'W', 'E', 'B', 'P'};
  658. if (memcmp(fourCC, WEBP, TAG_SIZE) != 0)
  659. {
  660. // VP8_STATUS_BITSTREAM_ERROR
  661. URHO3D_LOGERROR("Invalid header");
  662. return false;
  663. }
  664. // Read the file to buffer.
  665. size_t dataSize(source.GetSize());
  666. SharedArrayPtr<uint8_t> data(new uint8_t[dataSize]);
  667. memset(data.Get(), 0, sizeof(uint8_t) * dataSize);
  668. source.Seek(0);
  669. source.Read(data.Get(), dataSize);
  670. WebPBitstreamFeatures features;
  671. if (WebPGetFeatures(data.Get(), dataSize, &features) != VP8_STATUS_OK)
  672. {
  673. URHO3D_LOGERROR("Error reading WebP image: " + source.GetName());
  674. return false;
  675. }
  676. size_t imgSize = (size_t)features.width * features.height * (features.has_alpha ? 4 : 3);
  677. SharedArrayPtr<uint8_t> pixelData(new uint8_t[imgSize]);
  678. bool decodeError(false);
  679. if (features.has_alpha)
  680. {
  681. decodeError = WebPDecodeRGBAInto(data.Get(), dataSize, pixelData.Get(), imgSize, 4 * features.width) == nullptr;
  682. }
  683. else
  684. {
  685. decodeError = WebPDecodeRGBInto(data.Get(), dataSize, pixelData.Get(), imgSize, 3 * features.width) == nullptr;
  686. }
  687. if (decodeError)
  688. {
  689. URHO3D_LOGERROR("Error decoding WebP image:" + source.GetName());
  690. return false;
  691. }
  692. SetSize(features.width, features.height, features.has_alpha ? 4 : 3);
  693. SetData(pixelData);
  694. }
  695. #endif
  696. else
  697. {
  698. // Not DDS, KTX or PVR, use STBImage to load other image formats as uncompressed
  699. source.Seek(0);
  700. int width, height;
  701. unsigned components;
  702. unsigned char* pixelData = GetImageData(source, width, height, components);
  703. if (!pixelData)
  704. {
  705. URHO3D_LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  706. return false;
  707. }
  708. SetSize(width, height, components);
  709. SetData(pixelData);
  710. FreeImageData(pixelData);
  711. }
  712. return true;
  713. }
  714. bool Image::Save(Serializer& dest) const
  715. {
  716. URHO3D_PROFILE(SaveImage);
  717. if (IsCompressed())
  718. {
  719. URHO3D_LOGERROR("Can not save compressed image " + GetName());
  720. return false;
  721. }
  722. if (!data_)
  723. {
  724. URHO3D_LOGERROR("Can not save zero-sized image " + GetName());
  725. return false;
  726. }
  727. int len;
  728. unsigned char* png = stbi_write_png_to_mem(data_.Get(), 0, width_, height_, components_, &len);
  729. bool success = dest.Write(png, (unsigned)len) == (unsigned)len;
  730. free(png); // NOLINT(hicpp-no-malloc)
  731. return success;
  732. }
  733. bool Image::SaveFile(const String& fileName) const
  734. {
  735. if (fileName.EndsWith(".dds", false))
  736. return SaveDDS(fileName);
  737. else if (fileName.EndsWith(".bmp", false))
  738. return SaveBMP(fileName);
  739. else if (fileName.EndsWith(".jpg", false) || fileName.EndsWith(".jpeg", false))
  740. return SaveJPG(fileName, 100);
  741. else if (fileName.EndsWith(".tga", false))
  742. return SaveTGA(fileName);
  743. #ifdef URHO3D_WEBP
  744. else if (fileName.EndsWith(".webp", false))
  745. return SaveWEBP(fileName, 100.0f);
  746. #endif
  747. else
  748. return SavePNG(fileName);
  749. }
  750. bool Image::SetSize(int width, int height, unsigned components)
  751. {
  752. return SetSize(width, height, 1, components);
  753. }
  754. bool Image::SetSize(int width, int height, int depth, unsigned components)
  755. {
  756. if (width == width_ && height == height_ && depth == depth_ && components == components_)
  757. return true;
  758. if (width <= 0 || height <= 0 || depth <= 0)
  759. return false;
  760. if (components > 4)
  761. {
  762. URHO3D_LOGERROR("More than 4 color components are not supported");
  763. return false;
  764. }
  765. data_ = new unsigned char[width * height * depth * components];
  766. width_ = width;
  767. height_ = height;
  768. depth_ = depth;
  769. components_ = components;
  770. compressedFormat_ = CF_NONE;
  771. numCompressedLevels_ = 0;
  772. nextLevel_.Reset();
  773. SetMemoryUse(width * height * depth * components);
  774. return true;
  775. }
  776. void Image::SetPixel(int x, int y, const Color& color)
  777. {
  778. SetPixelInt(x, y, 0, color.ToUInt());
  779. }
  780. void Image::SetPixel(int x, int y, int z, const Color& color)
  781. {
  782. SetPixelInt(x, y, z, color.ToUInt());
  783. }
  784. void Image::SetPixelInt(int x, int y, unsigned uintColor)
  785. {
  786. SetPixelInt(x, y, 0, uintColor);
  787. }
  788. void Image::SetPixelInt(int x, int y, int z, unsigned uintColor)
  789. {
  790. if (!data_ || x < 0 || x >= width_ || y < 0 || y >= height_ || z < 0 || z >= depth_ || IsCompressed())
  791. return;
  792. unsigned char* dest = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  793. auto* src = (unsigned char*)&uintColor;
  794. switch (components_)
  795. {
  796. case 4:
  797. dest[3] = src[3];
  798. // Fall through
  799. case 3:
  800. dest[2] = src[2];
  801. // Fall through
  802. case 2:
  803. dest[1] = src[1];
  804. // Fall through
  805. default:
  806. dest[0] = src[0];
  807. break;
  808. }
  809. }
  810. void Image::SetData(const unsigned char* pixelData)
  811. {
  812. if (!data_)
  813. return;
  814. if (IsCompressed())
  815. {
  816. URHO3D_LOGERROR("Can not set new pixel data for a compressed image");
  817. return;
  818. }
  819. auto size = (size_t)width_ * height_ * depth_ * components_;
  820. if (pixelData)
  821. memcpy(data_.Get(), pixelData, size);
  822. else
  823. memset(data_.Get(), 0, size);
  824. nextLevel_.Reset();
  825. }
  826. bool Image::LoadColorLUT(Deserializer& source)
  827. {
  828. String fileID = source.ReadFileID();
  829. if (fileID == "DDS " || fileID == "\253KTX" || fileID == "PVR\3")
  830. {
  831. URHO3D_LOGERROR("Invalid image format, can not load image");
  832. return false;
  833. }
  834. source.Seek(0);
  835. int width, height;
  836. unsigned components;
  837. unsigned char* pixelDataIn = GetImageData(source, width, height, components);
  838. if (!pixelDataIn)
  839. {
  840. URHO3D_LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  841. return false;
  842. }
  843. if (components != 3)
  844. {
  845. URHO3D_LOGERROR("Invalid image format, can not load image");
  846. return false;
  847. }
  848. SetSize(COLOR_LUT_SIZE, COLOR_LUT_SIZE, COLOR_LUT_SIZE, components);
  849. SetMemoryUse(width_ * height_ * depth_ * components);
  850. unsigned char* pixelDataOut = GetData();
  851. for (int z = 0; z < depth_; ++z)
  852. {
  853. for (int y = 0; y < height_; ++y)
  854. {
  855. unsigned char* in = &pixelDataIn[z * width_ * 3 + y * width * 3];
  856. unsigned char* out = &pixelDataOut[z * width_ * height_ * 3 + y * width_ * 3];
  857. for (int x = 0; x < width_ * 3; x += 3)
  858. {
  859. out[x] = in[x];
  860. out[x + 1] = in[x + 1];
  861. out[x + 2] = in[x + 2];
  862. }
  863. }
  864. }
  865. FreeImageData(pixelDataIn);
  866. return true;
  867. }
  868. bool Image::FlipHorizontal()
  869. {
  870. if (!data_)
  871. return false;
  872. if (depth_ > 1)
  873. {
  874. URHO3D_LOGERROR("FlipHorizontal not supported for 3D images");
  875. return false;
  876. }
  877. if (!IsCompressed())
  878. {
  879. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  880. unsigned rowSize = width_ * components_;
  881. for (int y = 0; y < height_; ++y)
  882. {
  883. for (int x = 0; x < width_; ++x)
  884. {
  885. for (unsigned c = 0; c < components_; ++c)
  886. newData[y * rowSize + x * components_ + c] = data_[y * rowSize + (width_ - x - 1) * components_ + c];
  887. }
  888. }
  889. data_ = newData;
  890. }
  891. else
  892. {
  893. if (compressedFormat_ > CF_DXT5)
  894. {
  895. URHO3D_LOGERROR("FlipHorizontal not yet implemented for other compressed formats than RGBA & DXT1,3,5");
  896. return false;
  897. }
  898. // Memory use = combined size of the compressed mip levels
  899. SharedArrayPtr<unsigned char> newData(new unsigned char[GetMemoryUse()]);
  900. unsigned dataOffset = 0;
  901. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  902. {
  903. CompressedLevel level = GetCompressedLevel(i);
  904. if (!level.data_)
  905. {
  906. URHO3D_LOGERROR("Got compressed level with no data, aborting horizontal flip");
  907. return false;
  908. }
  909. for (unsigned y = 0; y < level.rows_; ++y)
  910. {
  911. for (unsigned x = 0; x < level.rowSize_; x += level.blockSize_)
  912. {
  913. unsigned char* src = level.data_ + y * level.rowSize_ + (level.rowSize_ - level.blockSize_ - x);
  914. unsigned char* dest = newData.Get() + y * level.rowSize_ + x;
  915. FlipBlockHorizontal(dest, src, compressedFormat_);
  916. }
  917. }
  918. dataOffset += level.dataSize_;
  919. }
  920. data_ = newData;
  921. }
  922. return true;
  923. }
  924. bool Image::FlipVertical()
  925. {
  926. if (!data_)
  927. return false;
  928. if (depth_ > 1)
  929. {
  930. URHO3D_LOGERROR("FlipVertical not supported for 3D images");
  931. return false;
  932. }
  933. if (!IsCompressed())
  934. {
  935. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  936. unsigned rowSize = width_ * components_;
  937. for (int y = 0; y < height_; ++y)
  938. memcpy(&newData[(height_ - y - 1) * rowSize], &data_[y * rowSize], rowSize);
  939. data_ = newData;
  940. }
  941. else
  942. {
  943. if (compressedFormat_ > CF_DXT5)
  944. {
  945. URHO3D_LOGERROR("FlipVertical not yet implemented for other compressed formats than DXT1,3,5");
  946. return false;
  947. }
  948. // Memory use = combined size of the compressed mip levels
  949. SharedArrayPtr<unsigned char> newData(new unsigned char[GetMemoryUse()]);
  950. unsigned dataOffset = 0;
  951. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  952. {
  953. CompressedLevel level = GetCompressedLevel(i);
  954. if (!level.data_)
  955. {
  956. URHO3D_LOGERROR("Got compressed level with no data, aborting vertical flip");
  957. return false;
  958. }
  959. for (unsigned y = 0; y < level.rows_; ++y)
  960. {
  961. unsigned char* src = level.data_ + y * level.rowSize_;
  962. unsigned char* dest = newData.Get() + dataOffset + (level.rows_ - y - 1) * level.rowSize_;
  963. for (unsigned x = 0; x < level.rowSize_; x += level.blockSize_)
  964. FlipBlockVertical(dest + x, src + x, compressedFormat_);
  965. }
  966. dataOffset += level.dataSize_;
  967. }
  968. data_ = newData;
  969. }
  970. return true;
  971. }
  972. bool Image::Resize(int width, int height)
  973. {
  974. URHO3D_PROFILE(ResizeImage);
  975. if (IsCompressed())
  976. {
  977. URHO3D_LOGERROR("Resize not supported for compressed images");
  978. return false;
  979. }
  980. if (depth_ > 1)
  981. {
  982. URHO3D_LOGERROR("Resize not supported for 3D images");
  983. return false;
  984. }
  985. if (!data_ || width <= 0 || height <= 0)
  986. return false;
  987. /// \todo Reducing image size does not sample all needed pixels
  988. SharedArrayPtr<unsigned char> newData(new unsigned char[width * height * components_]);
  989. for (int y = 0; y < height; ++y)
  990. {
  991. for (int x = 0; x < width; ++x)
  992. {
  993. // Calculate float coordinates between 0 - 1 for resampling
  994. float xF = (width_ > 1) ? (float)x / (float)(width - 1) : 0.0f;
  995. float yF = (height_ > 1) ? (float)y / (float)(height - 1) : 0.0f;
  996. unsigned uintColor = GetPixelBilinear(xF, yF).ToUInt();
  997. unsigned char* dest = newData + (y * width + x) * components_;
  998. auto* src = (unsigned char*)&uintColor;
  999. switch (components_)
  1000. {
  1001. case 4:
  1002. dest[3] = src[3];
  1003. // Fall through
  1004. case 3:
  1005. dest[2] = src[2];
  1006. // Fall through
  1007. case 2:
  1008. dest[1] = src[1];
  1009. // Fall through
  1010. default:
  1011. dest[0] = src[0];
  1012. break;
  1013. }
  1014. }
  1015. }
  1016. width_ = width;
  1017. height_ = height;
  1018. data_ = newData;
  1019. SetMemoryUse(width * height * depth_ * components_);
  1020. return true;
  1021. }
  1022. void Image::Clear(const Color& color)
  1023. {
  1024. ClearInt(color.ToUInt());
  1025. }
  1026. void Image::ClearInt(unsigned uintColor)
  1027. {
  1028. URHO3D_PROFILE(ClearImage);
  1029. if (!data_)
  1030. return;
  1031. if (IsCompressed())
  1032. {
  1033. URHO3D_LOGERROR("Clear not supported for compressed images");
  1034. return;
  1035. }
  1036. if (components_ == 4)
  1037. {
  1038. unsigned color = uintColor;
  1039. auto* data = (unsigned*)GetData();
  1040. auto* data_end = (unsigned*)(GetData() + width_ * height_ * depth_ * components_);
  1041. for (; data < data_end; ++data)
  1042. *data = color;
  1043. }
  1044. else
  1045. {
  1046. auto* src = (unsigned char*)&uintColor;
  1047. for (unsigned i = 0; i < width_ * height_ * depth_ * components_; ++i)
  1048. data_[i] = src[i % components_];
  1049. }
  1050. }
  1051. bool Image::SaveBMP(const String& fileName) const
  1052. {
  1053. URHO3D_PROFILE(SaveImageBMP);
  1054. auto* fileSystem = GetSubsystem<FileSystem>();
  1055. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  1056. {
  1057. URHO3D_LOGERROR("Access denied to " + fileName);
  1058. return false;
  1059. }
  1060. if (IsCompressed())
  1061. {
  1062. URHO3D_LOGERROR("Can not save compressed image to BMP");
  1063. return false;
  1064. }
  1065. if (data_)
  1066. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  1067. else
  1068. return false;
  1069. }
  1070. bool Image::SavePNG(const String& fileName) const
  1071. {
  1072. URHO3D_PROFILE(SaveImagePNG);
  1073. File outFile(context_, fileName, FILE_WRITE);
  1074. if (outFile.IsOpen())
  1075. return Image::Save(outFile); // Save uses PNG format
  1076. else
  1077. return false;
  1078. }
  1079. bool Image::SaveTGA(const String& fileName) const
  1080. {
  1081. URHO3D_PROFILE(SaveImageTGA);
  1082. auto* fileSystem = GetSubsystem<FileSystem>();
  1083. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  1084. {
  1085. URHO3D_LOGERROR("Access denied to " + fileName);
  1086. return false;
  1087. }
  1088. if (IsCompressed())
  1089. {
  1090. URHO3D_LOGERROR("Can not save compressed image to TGA");
  1091. return false;
  1092. }
  1093. if (data_)
  1094. return stbi_write_tga(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get()) != 0;
  1095. else
  1096. return false;
  1097. }
  1098. bool Image::SaveJPG(const String& fileName, int quality) const
  1099. {
  1100. URHO3D_PROFILE(SaveImageJPG);
  1101. auto* fileSystem = GetSubsystem<FileSystem>();
  1102. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  1103. {
  1104. URHO3D_LOGERROR("Access denied to " + fileName);
  1105. return false;
  1106. }
  1107. if (IsCompressed())
  1108. {
  1109. URHO3D_LOGERROR("Can not save compressed image to JPG");
  1110. return false;
  1111. }
  1112. if (data_)
  1113. return stbi_write_jpg(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get(), quality) != 0;
  1114. else
  1115. return false;
  1116. }
  1117. bool Image::SaveDDS(const String& fileName) const
  1118. {
  1119. URHO3D_PROFILE(SaveImageDDS);
  1120. File outFile(context_, fileName, FILE_WRITE);
  1121. if (!outFile.IsOpen())
  1122. {
  1123. URHO3D_LOGERROR("Access denied to " + fileName);
  1124. return false;
  1125. }
  1126. if (IsCompressed())
  1127. {
  1128. URHO3D_LOGERROR("Can not save compressed image to DDS");
  1129. return false;
  1130. }
  1131. if (components_ != 4)
  1132. {
  1133. URHO3D_LOGERRORF("Can not save image with %u components to DDS", components_);
  1134. return false;
  1135. }
  1136. // Write image
  1137. PODVector<const Image*> levels;
  1138. GetLevels(levels);
  1139. outFile.WriteFileID("DDS ");
  1140. DDSurfaceDesc2 ddsd; // NOLINT(hicpp-member-init)
  1141. memset(&ddsd, 0, sizeof(ddsd));
  1142. ddsd.dwSize_ = sizeof(ddsd);
  1143. ddsd.dwFlags_ = 0x00000001l /*DDSD_CAPS*/
  1144. | 0x00000002l /*DDSD_HEIGHT*/ | 0x00000004l /*DDSD_WIDTH*/ | 0x00020000l /*DDSD_MIPMAPCOUNT*/ | 0x00001000l /*DDSD_PIXELFORMAT*/;
  1145. ddsd.dwWidth_ = width_;
  1146. ddsd.dwHeight_ = height_;
  1147. ddsd.dwMipMapCount_ = levels.Size();
  1148. ddsd.ddpfPixelFormat_.dwFlags_ = 0x00000040l /*DDPF_RGB*/ | 0x00000001l /*DDPF_ALPHAPIXELS*/;
  1149. ddsd.ddpfPixelFormat_.dwSize_ = sizeof(ddsd.ddpfPixelFormat_);
  1150. ddsd.ddpfPixelFormat_.dwRGBBitCount_ = 32;
  1151. ddsd.ddpfPixelFormat_.dwRBitMask_ = 0x000000ff;
  1152. ddsd.ddpfPixelFormat_.dwGBitMask_ = 0x0000ff00;
  1153. ddsd.ddpfPixelFormat_.dwBBitMask_ = 0x00ff0000;
  1154. ddsd.ddpfPixelFormat_.dwRGBAlphaBitMask_ = 0xff000000;
  1155. outFile.Write(&ddsd, sizeof(ddsd));
  1156. for (unsigned i = 0; i < levels.Size(); ++i)
  1157. outFile.Write(levels[i]->GetData(), levels[i]->GetWidth() * levels[i]->GetHeight() * 4);
  1158. return true;
  1159. }
  1160. bool Image::SaveWEBP(const String& fileName, float compression /* = 0.0f */) const
  1161. {
  1162. #ifdef URHO3D_WEBP
  1163. URHO3D_PROFILE(SaveImageWEBP);
  1164. auto* fileSystem(GetSubsystem<FileSystem>());
  1165. File outFile(context_, fileName, FILE_WRITE);
  1166. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  1167. {
  1168. URHO3D_LOGERROR("Access denied to " + fileName);
  1169. return false;
  1170. }
  1171. if (IsCompressed())
  1172. {
  1173. URHO3D_LOGERROR("Can not save compressed image to WebP");
  1174. return false;
  1175. }
  1176. if (height_ > WEBP_MAX_DIMENSION || width_ > WEBP_MAX_DIMENSION)
  1177. {
  1178. URHO3D_LOGERROR("Maximum dimension supported by WebP is " + String(WEBP_MAX_DIMENSION));
  1179. return false;
  1180. }
  1181. if (components_ != 4 && components_ != 3)
  1182. {
  1183. URHO3D_LOGERRORF("Can not save image with %u components to WebP, which requires 3 or 4; Try ConvertToRGBA first?", components_);
  1184. return false;
  1185. }
  1186. if (!data_)
  1187. {
  1188. URHO3D_LOGERROR("No image data to save");
  1189. return false;
  1190. }
  1191. WebPPicture pic;
  1192. WebPConfig config;
  1193. WebPMemoryWriter wrt;
  1194. int importResult(0);
  1195. size_t encodeResult(0);
  1196. if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, compression) || !WebPPictureInit(&pic))
  1197. {
  1198. URHO3D_LOGERROR("WebP initialization failed; check installation");
  1199. return false;
  1200. }
  1201. config.lossless = 1;
  1202. config.exact = 1; // Preserve RGB values under transparency, as they may be wanted.
  1203. pic.use_argb = 1;
  1204. pic.width = width_;
  1205. pic.height = height_;
  1206. pic.writer = WebPMemoryWrite;
  1207. pic.custom_ptr = &wrt;
  1208. WebPMemoryWriterInit(&wrt);
  1209. if (components_ == 4)
  1210. importResult = WebPPictureImportRGBA(&pic, data_.Get(), components_ * width_);
  1211. else if (components_ == 3)
  1212. importResult = WebPPictureImportRGB(&pic, data_.Get(), components_ * width_);
  1213. if (!importResult)
  1214. {
  1215. URHO3D_LOGERROR("WebP import of image data failed (truncated RGBA/RGB data or memory error?)");
  1216. WebPPictureFree(&pic);
  1217. WebPMemoryWriterClear(&wrt);
  1218. return false;
  1219. }
  1220. encodeResult = WebPEncode(&config, &pic);
  1221. // Check only general failure. WebPEncode() sets pic.error_code with specific error.
  1222. if (!encodeResult)
  1223. {
  1224. URHO3D_LOGERRORF("WebP encoding failed (memory error?). WebPEncodingError = %d", pic.error_code);
  1225. WebPPictureFree(&pic);
  1226. WebPMemoryWriterClear(&wrt);
  1227. return false;
  1228. }
  1229. WebPPictureFree(&pic);
  1230. outFile.Write(wrt.mem, wrt.size);
  1231. WebPMemoryWriterClear(&wrt);
  1232. return true;
  1233. #else
  1234. URHO3D_LOGERROR("Cannot save in WEBP format, support not compiled in");
  1235. return false;
  1236. #endif
  1237. }
  1238. Color Image::GetPixel(int x, int y) const
  1239. {
  1240. return GetPixel(x, y, 0);
  1241. }
  1242. Color Image::GetPixel(int x, int y, int z) const
  1243. {
  1244. if (!data_ || z < 0 || z >= depth_ || IsCompressed())
  1245. return Color::BLACK;
  1246. x = Clamp(x, 0, width_ - 1);
  1247. y = Clamp(y, 0, height_ - 1);
  1248. unsigned char* src = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  1249. Color ret;
  1250. switch (components_)
  1251. {
  1252. case 4:
  1253. ret.a_ = (float)src[3] / 255.0f;
  1254. // Fall through
  1255. case 3:
  1256. ret.b_ = (float)src[2] / 255.0f;
  1257. // Fall through
  1258. case 2:
  1259. ret.g_ = (float)src[1] / 255.0f;
  1260. ret.r_ = (float)src[0] / 255.0f;
  1261. break;
  1262. default:
  1263. ret.r_ = ret.g_ = ret.b_ = (float)src[0] / 255.0f;
  1264. break;
  1265. }
  1266. return ret;
  1267. }
  1268. unsigned Image::GetPixelInt(int x, int y) const
  1269. {
  1270. return GetPixelInt(x, y, 0);
  1271. }
  1272. unsigned Image::GetPixelInt(int x, int y, int z) const
  1273. {
  1274. if (!data_ || z < 0 || z >= depth_ || IsCompressed())
  1275. return 0xff000000;
  1276. x = Clamp(x, 0, width_ - 1);
  1277. y = Clamp(y, 0, height_ - 1);
  1278. unsigned char* src = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  1279. unsigned ret = 0;
  1280. if (components_ < 4)
  1281. ret |= 0xff000000;
  1282. switch (components_)
  1283. {
  1284. case 4:
  1285. ret |= (unsigned)src[3] << 24;
  1286. // Fall through
  1287. case 3:
  1288. ret |= (unsigned)src[2] << 16;
  1289. // Fall through
  1290. case 2:
  1291. ret |= (unsigned)src[1] << 8;
  1292. ret |= (unsigned)src[0];
  1293. break;
  1294. default:
  1295. ret |= (unsigned)src[0] << 16;
  1296. ret |= (unsigned)src[0] << 8;
  1297. ret |= (unsigned)src[0];
  1298. break;
  1299. }
  1300. return ret;
  1301. }
  1302. Color Image::GetPixelBilinear(float x, float y) const
  1303. {
  1304. x = Clamp(x * width_ - 0.5f, 0.0f, (float)(width_ - 1));
  1305. y = Clamp(y * height_ - 0.5f, 0.0f, (float)(height_ - 1));
  1306. auto xI = (int)x;
  1307. auto yI = (int)y;
  1308. float xF = Fract(x);
  1309. float yF = Fract(y);
  1310. Color topColor = GetPixel(xI, yI).Lerp(GetPixel(xI + 1, yI), xF);
  1311. Color bottomColor = GetPixel(xI, yI + 1).Lerp(GetPixel(xI + 1, yI + 1), xF);
  1312. return topColor.Lerp(bottomColor, yF);
  1313. }
  1314. Color Image::GetPixelTrilinear(float x, float y, float z) const
  1315. {
  1316. if (depth_ < 2)
  1317. return GetPixelBilinear(x, y);
  1318. x = Clamp(x * width_ - 0.5f, 0.0f, (float)(width_ - 1));
  1319. y = Clamp(y * height_ - 0.5f, 0.0f, (float)(height_ - 1));
  1320. z = Clamp(z * depth_ - 0.5f, 0.0f, (float)(depth_ - 1));
  1321. auto xI = (int)x;
  1322. auto yI = (int)y;
  1323. auto zI = (int)z;
  1324. if (zI == depth_ - 1)
  1325. return GetPixelBilinear(x, y);
  1326. float xF = Fract(x);
  1327. float yF = Fract(y);
  1328. float zF = Fract(z);
  1329. Color topColorNear = GetPixel(xI, yI, zI).Lerp(GetPixel(xI + 1, yI, zI), xF);
  1330. Color bottomColorNear = GetPixel(xI, yI + 1, zI).Lerp(GetPixel(xI + 1, yI + 1, zI), xF);
  1331. Color colorNear = topColorNear.Lerp(bottomColorNear, yF);
  1332. Color topColorFar = GetPixel(xI, yI, zI + 1).Lerp(GetPixel(xI + 1, yI, zI + 1), xF);
  1333. Color bottomColorFar = GetPixel(xI, yI + 1, zI + 1).Lerp(GetPixel(xI + 1, yI + 1, zI + 1), xF);
  1334. Color colorFar = topColorFar.Lerp(bottomColorFar, yF);
  1335. return colorNear.Lerp(colorFar, zF);
  1336. }
  1337. SharedPtr<Image> Image::GetNextLevel() const
  1338. {
  1339. if (IsCompressed())
  1340. {
  1341. URHO3D_LOGERROR("Can not generate mip level from compressed data");
  1342. return SharedPtr<Image>();
  1343. }
  1344. if (components_ < 1 || components_ > 4)
  1345. {
  1346. URHO3D_LOGERROR("Illegal number of image components for mip level generation");
  1347. return SharedPtr<Image>();
  1348. }
  1349. if (nextLevel_)
  1350. return nextLevel_;
  1351. URHO3D_PROFILE(CalculateImageMipLevel);
  1352. int widthOut = width_ / 2;
  1353. int heightOut = height_ / 2;
  1354. int depthOut = depth_ / 2;
  1355. if (widthOut < 1)
  1356. widthOut = 1;
  1357. if (heightOut < 1)
  1358. heightOut = 1;
  1359. if (depthOut < 1)
  1360. depthOut = 1;
  1361. SharedPtr<Image> mipImage(new Image(context_));
  1362. if (depth_ > 1)
  1363. mipImage->SetSize(widthOut, heightOut, depthOut, components_);
  1364. else
  1365. mipImage->SetSize(widthOut, heightOut, components_);
  1366. const unsigned char* pixelDataIn = data_.Get();
  1367. unsigned char* pixelDataOut = mipImage->data_.Get();
  1368. // 1D case
  1369. if (depth_ == 1 && (height_ == 1 || width_ == 1))
  1370. {
  1371. // Loop using the larger dimension
  1372. if (widthOut < heightOut)
  1373. widthOut = heightOut;
  1374. switch (components_)
  1375. {
  1376. case 1:
  1377. for (int x = 0; x < widthOut; ++x)
  1378. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 1]) >> 1);
  1379. break;
  1380. case 2:
  1381. for (int x = 0; x < widthOut * 2; x += 2)
  1382. {
  1383. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 2]) >> 1);
  1384. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 3]) >> 1);
  1385. }
  1386. break;
  1387. case 3:
  1388. for (int x = 0; x < widthOut * 3; x += 3)
  1389. {
  1390. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 3]) >> 1);
  1391. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 4]) >> 1);
  1392. pixelDataOut[x + 2] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 2] + pixelDataIn[x * 2 + 5]) >> 1);
  1393. }
  1394. break;
  1395. case 4:
  1396. for (int x = 0; x < widthOut * 4; x += 4)
  1397. {
  1398. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 4]) >> 1);
  1399. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 5]) >> 1);
  1400. pixelDataOut[x + 2] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 2] + pixelDataIn[x * 2 + 6]) >> 1);
  1401. pixelDataOut[x + 3] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 3] + pixelDataIn[x * 2 + 7]) >> 1);
  1402. }
  1403. break;
  1404. default:
  1405. assert(false); // Should never reach here
  1406. break;
  1407. }
  1408. }
  1409. // 2D case
  1410. else if (depth_ == 1)
  1411. {
  1412. switch (components_)
  1413. {
  1414. case 1:
  1415. for (int y = 0; y < heightOut; ++y)
  1416. {
  1417. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_];
  1418. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_];
  1419. unsigned char* out = &pixelDataOut[y * widthOut];
  1420. for (int x = 0; x < widthOut; ++x)
  1421. {
  1422. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 1] +
  1423. inLower[x * 2] + inLower[x * 2 + 1]) >> 2);
  1424. }
  1425. }
  1426. break;
  1427. case 2:
  1428. for (int y = 0; y < heightOut; ++y)
  1429. {
  1430. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 2];
  1431. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 2];
  1432. unsigned char* out = &pixelDataOut[y * widthOut * 2];
  1433. for (int x = 0; x < widthOut * 2; x += 2)
  1434. {
  1435. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 2] +
  1436. inLower[x * 2] + inLower[x * 2 + 2]) >> 2);
  1437. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 3] +
  1438. inLower[x * 2 + 1] + inLower[x * 2 + 3]) >> 2);
  1439. }
  1440. }
  1441. break;
  1442. case 3:
  1443. for (int y = 0; y < heightOut; ++y)
  1444. {
  1445. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 3];
  1446. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 3];
  1447. unsigned char* out = &pixelDataOut[y * widthOut * 3];
  1448. for (int x = 0; x < widthOut * 3; x += 3)
  1449. {
  1450. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 3] +
  1451. inLower[x * 2] + inLower[x * 2 + 3]) >> 2);
  1452. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 4] +
  1453. inLower[x * 2 + 1] + inLower[x * 2 + 4]) >> 2);
  1454. out[x + 2] = (unsigned char)(((unsigned)inUpper[x * 2 + 2] + inUpper[x * 2 + 5] +
  1455. inLower[x * 2 + 2] + inLower[x * 2 + 5]) >> 2);
  1456. }
  1457. }
  1458. break;
  1459. case 4:
  1460. for (int y = 0; y < heightOut; ++y)
  1461. {
  1462. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 4];
  1463. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 4];
  1464. unsigned char* out = &pixelDataOut[y * widthOut * 4];
  1465. for (int x = 0; x < widthOut * 4; x += 4)
  1466. {
  1467. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 4] +
  1468. inLower[x * 2] + inLower[x * 2 + 4]) >> 2);
  1469. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 5] +
  1470. inLower[x * 2 + 1] + inLower[x * 2 + 5]) >> 2);
  1471. out[x + 2] = (unsigned char)(((unsigned)inUpper[x * 2 + 2] + inUpper[x * 2 + 6] +
  1472. inLower[x * 2 + 2] + inLower[x * 2 + 6]) >> 2);
  1473. out[x + 3] = (unsigned char)(((unsigned)inUpper[x * 2 + 3] + inUpper[x * 2 + 7] +
  1474. inLower[x * 2 + 3] + inLower[x * 2 + 7]) >> 2);
  1475. }
  1476. }
  1477. break;
  1478. default:
  1479. assert(false); // Should never reach here
  1480. break;
  1481. }
  1482. }
  1483. // 3D case
  1484. else
  1485. {
  1486. switch (components_)
  1487. {
  1488. case 1:
  1489. for (int z = 0; z < depthOut; ++z)
  1490. {
  1491. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_];
  1492. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_];
  1493. for (int y = 0; y < heightOut; ++y)
  1494. {
  1495. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_];
  1496. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_];
  1497. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_];
  1498. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_];
  1499. unsigned char* out = &pixelDataOut[z * widthOut * heightOut + y * widthOut];
  1500. for (int x = 0; x < widthOut; ++x)
  1501. {
  1502. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 1] +
  1503. inOuterLower[x * 2] + inOuterLower[x * 2 + 1] +
  1504. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 1] +
  1505. inInnerLower[x * 2] + inInnerLower[x * 2 + 1]) >> 3);
  1506. }
  1507. }
  1508. }
  1509. break;
  1510. case 2:
  1511. for (int z = 0; z < depthOut; ++z)
  1512. {
  1513. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 2];
  1514. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 2];
  1515. for (int y = 0; y < heightOut; ++y)
  1516. {
  1517. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 2];
  1518. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 2];
  1519. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 2];
  1520. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 2];
  1521. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 2 + y * widthOut * 2];
  1522. for (int x = 0; x < widthOut * 2; x += 2)
  1523. {
  1524. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 2] +
  1525. inOuterLower[x * 2] + inOuterLower[x * 2 + 2] +
  1526. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 2] +
  1527. inInnerLower[x * 2] + inInnerLower[x * 2 + 2]) >> 3);
  1528. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 3] +
  1529. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 3] +
  1530. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 3] +
  1531. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 3]) >> 3);
  1532. }
  1533. }
  1534. }
  1535. break;
  1536. case 3:
  1537. for (int z = 0; z < depthOut; ++z)
  1538. {
  1539. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 3];
  1540. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 3];
  1541. for (int y = 0; y < heightOut; ++y)
  1542. {
  1543. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 3];
  1544. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 3];
  1545. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 3];
  1546. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 3];
  1547. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 3 + y * widthOut * 3];
  1548. for (int x = 0; x < widthOut * 3; x += 3)
  1549. {
  1550. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 3] +
  1551. inOuterLower[x * 2] + inOuterLower[x * 2 + 3] +
  1552. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 3] +
  1553. inInnerLower[x * 2] + inInnerLower[x * 2 + 3]) >> 3);
  1554. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 4] +
  1555. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 4] +
  1556. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 4] +
  1557. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 4]) >> 3);
  1558. out[x + 2] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 2] + inOuterUpper[x * 2 + 5] +
  1559. inOuterLower[x * 2 + 2] + inOuterLower[x * 2 + 5] +
  1560. inInnerUpper[x * 2 + 2] + inInnerUpper[x * 2 + 5] +
  1561. inInnerLower[x * 2 + 2] + inInnerLower[x * 2 + 5]) >> 3);
  1562. }
  1563. }
  1564. }
  1565. break;
  1566. case 4:
  1567. for (int z = 0; z < depthOut; ++z)
  1568. {
  1569. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 4];
  1570. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 4];
  1571. for (int y = 0; y < heightOut; ++y)
  1572. {
  1573. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 4];
  1574. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 4];
  1575. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 4];
  1576. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 4];
  1577. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 4 + y * widthOut * 4];
  1578. for (int x = 0; x < widthOut * 4; x += 4)
  1579. {
  1580. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 4] +
  1581. inOuterLower[x * 2] + inOuterLower[x * 2 + 4] +
  1582. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 4] +
  1583. inInnerLower[x * 2] + inInnerLower[x * 2 + 4]) >> 3);
  1584. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 5] +
  1585. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 5] +
  1586. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 5] +
  1587. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 5]) >> 3);
  1588. out[x + 2] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 2] + inOuterUpper[x * 2 + 6] +
  1589. inOuterLower[x * 2 + 2] + inOuterLower[x * 2 + 6] +
  1590. inInnerUpper[x * 2 + 2] + inInnerUpper[x * 2 + 6] +
  1591. inInnerLower[x * 2 + 2] + inInnerLower[x * 2 + 6]) >> 3);
  1592. }
  1593. }
  1594. }
  1595. break;
  1596. default:
  1597. assert(false); // Should never reach here
  1598. break;
  1599. }
  1600. }
  1601. return mipImage;
  1602. }
  1603. SharedPtr<Image> Image::ConvertToRGBA() const
  1604. {
  1605. if (IsCompressed())
  1606. {
  1607. URHO3D_LOGERROR("Can not convert compressed image to RGBA");
  1608. return SharedPtr<Image>();
  1609. }
  1610. if (components_ < 1 || components_ > 4)
  1611. {
  1612. URHO3D_LOGERROR("Illegal number of image components for conversion to RGBA");
  1613. return SharedPtr<Image>();
  1614. }
  1615. if (!data_)
  1616. {
  1617. URHO3D_LOGERROR("Can not convert image without data to RGBA");
  1618. return SharedPtr<Image>();
  1619. }
  1620. // Already RGBA?
  1621. if (components_ == 4)
  1622. return SharedPtr<Image>(const_cast<Image*>(this));
  1623. SharedPtr<Image> ret(new Image(context_));
  1624. ret->SetSize(width_, height_, depth_, 4);
  1625. const unsigned char* src = data_;
  1626. unsigned char* dest = ret->GetData();
  1627. switch (components_)
  1628. {
  1629. case 1:
  1630. for (unsigned i = 0; i < static_cast<unsigned>(width_ * height_ * depth_); ++i)
  1631. {
  1632. unsigned char pixel = *src++;
  1633. *dest++ = pixel;
  1634. *dest++ = pixel;
  1635. *dest++ = pixel;
  1636. *dest++ = 255;
  1637. }
  1638. break;
  1639. case 2:
  1640. for (unsigned i = 0; i < static_cast<unsigned>(width_ * height_ * depth_); ++i)
  1641. {
  1642. unsigned char pixel = *src++;
  1643. *dest++ = pixel;
  1644. *dest++ = pixel;
  1645. *dest++ = pixel;
  1646. *dest++ = *src++;
  1647. }
  1648. break;
  1649. case 3:
  1650. for (unsigned i = 0; i < static_cast<unsigned>(width_ * height_ * depth_); ++i)
  1651. {
  1652. *dest++ = *src++;
  1653. *dest++ = *src++;
  1654. *dest++ = *src++;
  1655. *dest++ = 255;
  1656. }
  1657. break;
  1658. default:
  1659. assert(false); // Should never reach nere
  1660. break;
  1661. }
  1662. return ret;
  1663. }
  1664. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  1665. {
  1666. CompressedLevel level;
  1667. if (compressedFormat_ == CF_NONE)
  1668. {
  1669. URHO3D_LOGERROR("Image is not compressed");
  1670. return level;
  1671. }
  1672. if (index >= numCompressedLevels_)
  1673. {
  1674. URHO3D_LOGERROR("Compressed image mip level out of bounds");
  1675. return level;
  1676. }
  1677. level.format_ = compressedFormat_;
  1678. level.width_ = width_;
  1679. level.height_ = height_;
  1680. level.depth_ = depth_;
  1681. if (compressedFormat_ == CF_RGBA)
  1682. {
  1683. level.blockSize_ = 4;
  1684. unsigned i = 0;
  1685. unsigned offset = 0;
  1686. for (;;)
  1687. {
  1688. if (!level.width_)
  1689. level.width_ = 1;
  1690. if (!level.height_)
  1691. level.height_ = 1;
  1692. if (!level.depth_)
  1693. level.depth_ = 1;
  1694. level.rowSize_ = level.width_ * level.blockSize_;
  1695. level.rows_ = (unsigned)level.height_;
  1696. level.data_ = data_.Get() + offset;
  1697. level.dataSize_ = level.depth_ * level.rows_ * level.rowSize_;
  1698. if (offset + level.dataSize_ > GetMemoryUse())
  1699. {
  1700. URHO3D_LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1701. " Datasize: " + String(GetMemoryUse()));
  1702. level.data_ = nullptr;
  1703. return level;
  1704. }
  1705. if (i == index)
  1706. return level;
  1707. offset += level.dataSize_;
  1708. level.width_ /= 2;
  1709. level.height_ /= 2;
  1710. level.depth_ /= 2;
  1711. ++i;
  1712. }
  1713. }
  1714. else if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  1715. {
  1716. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  1717. unsigned i = 0;
  1718. unsigned offset = 0;
  1719. for (;;)
  1720. {
  1721. if (!level.width_)
  1722. level.width_ = 1;
  1723. if (!level.height_)
  1724. level.height_ = 1;
  1725. if (!level.depth_)
  1726. level.depth_ = 1;
  1727. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  1728. level.rows_ = (unsigned)((level.height_ + 3) / 4);
  1729. level.data_ = data_.Get() + offset;
  1730. level.dataSize_ = level.depth_ * level.rows_ * level.rowSize_;
  1731. if (offset + level.dataSize_ > GetMemoryUse())
  1732. {
  1733. URHO3D_LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1734. " Datasize: " + String(GetMemoryUse()));
  1735. level.data_ = nullptr;
  1736. return level;
  1737. }
  1738. if (i == index)
  1739. return level;
  1740. offset += level.dataSize_;
  1741. level.width_ /= 2;
  1742. level.height_ /= 2;
  1743. level.depth_ /= 2;
  1744. ++i;
  1745. }
  1746. }
  1747. else
  1748. {
  1749. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  1750. unsigned i = 0;
  1751. unsigned offset = 0;
  1752. for (;;)
  1753. {
  1754. if (!level.width_)
  1755. level.width_ = 1;
  1756. if (!level.height_)
  1757. level.height_ = 1;
  1758. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  1759. int dataHeight = Max(level.height_, 8);
  1760. level.data_ = data_.Get() + offset;
  1761. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  1762. level.rows_ = (unsigned)dataHeight;
  1763. level.rowSize_ = level.dataSize_ / level.rows_;
  1764. if (offset + level.dataSize_ > GetMemoryUse())
  1765. {
  1766. URHO3D_LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1767. " Datasize: " + String(GetMemoryUse()));
  1768. level.data_ = nullptr;
  1769. return level;
  1770. }
  1771. if (i == index)
  1772. return level;
  1773. offset += level.dataSize_;
  1774. level.width_ /= 2;
  1775. level.height_ /= 2;
  1776. ++i;
  1777. }
  1778. }
  1779. }
  1780. Image* Image::GetSubimage(const IntRect& rect) const
  1781. {
  1782. if (!data_)
  1783. return nullptr;
  1784. if (depth_ > 1)
  1785. {
  1786. URHO3D_LOGERROR("Subimage not supported for 3D images");
  1787. return nullptr;
  1788. }
  1789. if (rect.left_ < 0 || rect.top_ < 0 || rect.right_ > width_ || rect.bottom_ > height_ || !rect.Width() || !rect.Height())
  1790. {
  1791. URHO3D_LOGERROR("Can not get subimage from image " + GetName() + " with invalid region");
  1792. return nullptr;
  1793. }
  1794. if (!IsCompressed())
  1795. {
  1796. int x = rect.left_;
  1797. int y = rect.top_;
  1798. int width = rect.Width();
  1799. int height = rect.Height();
  1800. auto* image = new Image(context_);
  1801. image->SetSize(width, height, components_);
  1802. unsigned char* dest = image->GetData();
  1803. unsigned char* src = data_.Get() + (y * width_ + x) * components_;
  1804. for (int i = 0; i < height; ++i)
  1805. {
  1806. memcpy(dest, src, (size_t)width * components_);
  1807. dest += width * components_;
  1808. src += width_ * components_;
  1809. }
  1810. return image;
  1811. }
  1812. else
  1813. {
  1814. // Pad the region to be a multiple of block size
  1815. IntRect paddedRect = rect;
  1816. paddedRect.left_ = (rect.left_ / 4) * 4;
  1817. paddedRect.top_ = (rect.top_ / 4) * 4;
  1818. paddedRect.right_ = (rect.right_ / 4) * 4;
  1819. paddedRect.bottom_ = (rect.bottom_ / 4) * 4;
  1820. IntRect currentRect = paddedRect;
  1821. PODVector<unsigned char> subimageData;
  1822. unsigned subimageLevels = 0;
  1823. // Save as many mips as possible until the next mip would cross a block boundary
  1824. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  1825. {
  1826. CompressedLevel level = GetCompressedLevel(i);
  1827. if (!level.data_)
  1828. break;
  1829. // Mips are stored continuously
  1830. unsigned destStartOffset = subimageData.Size();
  1831. unsigned destRowSize = currentRect.Width() / 4 * level.blockSize_;
  1832. unsigned destSize = currentRect.Height() / 4 * destRowSize;
  1833. if (!destSize)
  1834. break;
  1835. subimageData.Resize(destStartOffset + destSize);
  1836. unsigned char* dest = &subimageData[destStartOffset];
  1837. for (int y = currentRect.top_; y < currentRect.bottom_; y += 4)
  1838. {
  1839. unsigned char* src = level.data_ + level.rowSize_ * (y / 4) + currentRect.left_ / 4 * level.blockSize_;
  1840. memcpy(dest, src, destRowSize);
  1841. dest += destRowSize;
  1842. }
  1843. ++subimageLevels;
  1844. if ((currentRect.left_ & 4) || (currentRect.right_ & 4) || (currentRect.top_ & 4) || (currentRect.bottom_ & 4))
  1845. break;
  1846. else
  1847. {
  1848. currentRect.left_ /= 2;
  1849. currentRect.right_ /= 2;
  1850. currentRect.top_ /= 2;
  1851. currentRect.bottom_ /= 2;
  1852. }
  1853. }
  1854. if (!subimageLevels)
  1855. {
  1856. URHO3D_LOGERROR("Subimage region from compressed image " + GetName() + " did not produce any data");
  1857. return nullptr;
  1858. }
  1859. auto* image = new Image(context_);
  1860. image->width_ = paddedRect.Width();
  1861. image->height_ = paddedRect.Height();
  1862. image->depth_ = 1;
  1863. image->compressedFormat_ = compressedFormat_;
  1864. image->numCompressedLevels_ = subimageLevels;
  1865. image->components_ = components_;
  1866. image->data_ = new unsigned char[subimageData.Size()];
  1867. memcpy(image->data_.Get(), &subimageData[0], subimageData.Size());
  1868. image->SetMemoryUse(subimageData.Size());
  1869. return image;
  1870. }
  1871. }
  1872. SDL_Surface* Image::GetSDLSurface(const IntRect& rect) const
  1873. {
  1874. if (!data_)
  1875. return nullptr;
  1876. if (depth_ > 1)
  1877. {
  1878. URHO3D_LOGERROR("Can not get SDL surface from 3D image");
  1879. return nullptr;
  1880. }
  1881. if (IsCompressed())
  1882. {
  1883. URHO3D_LOGERROR("Can not get SDL surface from compressed image " + GetName());
  1884. return nullptr;
  1885. }
  1886. if (components_ < 3)
  1887. {
  1888. URHO3D_LOGERROR("Can not get SDL surface from image " + GetName() + " with less than 3 components");
  1889. return nullptr;
  1890. }
  1891. IntRect imageRect = rect;
  1892. // Use full image if illegal rect
  1893. if (imageRect.left_ < 0 || imageRect.top_ < 0 || imageRect.right_ > width_ || imageRect.bottom_ > height_ ||
  1894. imageRect.left_ >= imageRect.right_ || imageRect.top_ >= imageRect.bottom_)
  1895. {
  1896. imageRect.left_ = 0;
  1897. imageRect.top_ = 0;
  1898. imageRect.right_ = width_;
  1899. imageRect.bottom_ = height_;
  1900. }
  1901. int imageWidth = width_;
  1902. int width = imageRect.Width();
  1903. int height = imageRect.Height();
  1904. // Assume little-endian for all the supported platforms
  1905. unsigned rMask = 0x000000ff;
  1906. unsigned gMask = 0x0000ff00;
  1907. unsigned bMask = 0x00ff0000;
  1908. unsigned aMask = 0xff000000;
  1909. SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, components_ * 8, rMask, gMask, bMask, aMask);
  1910. if (surface)
  1911. {
  1912. SDL_LockSurface(surface);
  1913. auto* destination = reinterpret_cast<unsigned char*>(surface->pixels);
  1914. unsigned char* source = data_ + components_ * (imageWidth * imageRect.top_ + imageRect.left_);
  1915. for (int i = 0; i < height; ++i)
  1916. {
  1917. memcpy(destination, source, (size_t)components_ * width);
  1918. destination += surface->pitch;
  1919. source += components_ * imageWidth;
  1920. }
  1921. SDL_UnlockSurface(surface);
  1922. }
  1923. else
  1924. URHO3D_LOGERROR("Failed to create SDL surface from image " + GetName());
  1925. return surface;
  1926. }
  1927. void Image::PrecalculateLevels()
  1928. {
  1929. if (!data_ || IsCompressed())
  1930. return;
  1931. URHO3D_PROFILE(PrecalculateImageMipLevels);
  1932. nextLevel_.Reset();
  1933. if (width_ > 1 || height_ > 1)
  1934. {
  1935. SharedPtr<Image> current = GetNextLevel();
  1936. nextLevel_ = current;
  1937. while (current && (current->width_ > 1 || current->height_ > 1))
  1938. {
  1939. current->nextLevel_ = current->GetNextLevel();
  1940. current = current->nextLevel_;
  1941. }
  1942. }
  1943. }
  1944. void Image::CleanupLevels()
  1945. {
  1946. nextLevel_.Reset();
  1947. }
  1948. void Image::GetLevels(PODVector<Image*>& levels)
  1949. {
  1950. levels.Clear();
  1951. Image* image = this;
  1952. while (image)
  1953. {
  1954. levels.Push(image);
  1955. image = image->nextLevel_;
  1956. }
  1957. }
  1958. void Image::GetLevels(PODVector<const Image*>& levels) const
  1959. {
  1960. levels.Clear();
  1961. const Image* image = this;
  1962. while (image)
  1963. {
  1964. levels.Push(image);
  1965. image = image->nextLevel_;
  1966. }
  1967. }
  1968. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  1969. {
  1970. unsigned dataSize = source.GetSize();
  1971. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  1972. source.Read(buffer.Get(), dataSize);
  1973. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int*)&components, 0);
  1974. }
  1975. void Image::FreeImageData(unsigned char* pixelData)
  1976. {
  1977. if (!pixelData)
  1978. return;
  1979. stbi_image_free(pixelData);
  1980. }
  1981. bool Image::HasAlphaChannel() const
  1982. {
  1983. return components_ > 3;
  1984. }
  1985. // Author: Josh Engebretson (AtomicGameEngine)
  1986. bool Image::SetSubimage(const Image* image, const IntRect& rect)
  1987. {
  1988. if (!data_)
  1989. return false;
  1990. if (depth_ > 1 || IsCompressed())
  1991. {
  1992. URHO3D_LOGERROR("Image::SetSubimage is not supported for compressed or 3D images");
  1993. return false;
  1994. }
  1995. if (components_ != image->components_)
  1996. {
  1997. URHO3D_LOGERROR("Can not set subimage in image " + GetName() + " with different number of components");
  1998. return false;
  1999. }
  2000. if (rect.left_ < 0 || rect.top_ < 0 || rect.right_ > width_ || rect.bottom_ > height_ || !rect.Width() || !rect.Height())
  2001. {
  2002. URHO3D_LOGERROR("Can not set subimage in image " + GetName() + " with invalid region");
  2003. return false;
  2004. }
  2005. const int destWidth = rect.Width();
  2006. const int destHeight = rect.Height();
  2007. if (destWidth == image->GetWidth() && destHeight == image->GetHeight())
  2008. {
  2009. unsigned char* src = image->GetData();
  2010. unsigned char* dest = data_.Get() + (rect.top_ * width_ + rect.left_) * components_;
  2011. for (int i = 0; i < destHeight; ++i)
  2012. {
  2013. memcpy(dest, src, (size_t)destWidth * components_);
  2014. src += destWidth * image->components_;
  2015. dest += width_ * components_;
  2016. }
  2017. }
  2018. else
  2019. {
  2020. unsigned char* dest = data_.Get() + (rect.top_ * width_ + rect.left_) * components_;
  2021. for (int y = 0; y < destHeight; ++y)
  2022. {
  2023. for (int x = 0; x < destWidth; ++x)
  2024. {
  2025. // Calculate float coordinates between 0 - 1 for resampling
  2026. const float xF = (image->width_ > 1) ? static_cast<float>(x) / (destWidth - 1) : 0.0f;
  2027. const float yF = (image->height_ > 1) ? static_cast<float>(y) / (destHeight - 1) : 0.0f;
  2028. const unsigned uintColor = image->GetPixelBilinear(xF, yF).ToUInt();
  2029. memcpy(dest, reinterpret_cast<const unsigned char*>(&uintColor), components_);
  2030. dest += components_;
  2031. }
  2032. dest += (width_ - destWidth) * components_;
  2033. }
  2034. }
  2035. return true;
  2036. }
  2037. }