Image.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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);
  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. memcpy(data_.Get(), pixelData, (size_t)width_ * height_ * depth_ * components_);
  820. nextLevel_.Reset();
  821. }
  822. bool Image::LoadColorLUT(Deserializer& source)
  823. {
  824. String fileID = source.ReadFileID();
  825. if (fileID == "DDS " || fileID == "\253KTX" || fileID == "PVR\3")
  826. {
  827. URHO3D_LOGERROR("Invalid image format, can not load image");
  828. return false;
  829. }
  830. source.Seek(0);
  831. int width, height;
  832. unsigned components;
  833. unsigned char* pixelDataIn = GetImageData(source, width, height, components);
  834. if (!pixelDataIn)
  835. {
  836. URHO3D_LOGERROR("Could not load image " + source.GetName() + ": " + String(stbi_failure_reason()));
  837. return false;
  838. }
  839. if (components != 3)
  840. {
  841. URHO3D_LOGERROR("Invalid image format, can not load image");
  842. return false;
  843. }
  844. SetSize(COLOR_LUT_SIZE, COLOR_LUT_SIZE, COLOR_LUT_SIZE, components);
  845. SetMemoryUse(width_ * height_ * depth_ * components);
  846. unsigned char* pixelDataOut = GetData();
  847. for (int z = 0; z < depth_; ++z)
  848. {
  849. for (int y = 0; y < height_; ++y)
  850. {
  851. unsigned char* in = &pixelDataIn[z * width_ * 3 + y * width * 3];
  852. unsigned char* out = &pixelDataOut[z * width_ * height_ * 3 + y * width_ * 3];
  853. for (int x = 0; x < width_ * 3; x += 3)
  854. {
  855. out[x] = in[x];
  856. out[x + 1] = in[x + 1];
  857. out[x + 2] = in[x + 2];
  858. }
  859. }
  860. }
  861. FreeImageData(pixelDataIn);
  862. return true;
  863. }
  864. bool Image::FlipHorizontal()
  865. {
  866. if (!data_)
  867. return false;
  868. if (depth_ > 1)
  869. {
  870. URHO3D_LOGERROR("FlipHorizontal not supported for 3D images");
  871. return false;
  872. }
  873. if (!IsCompressed())
  874. {
  875. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  876. unsigned rowSize = width_ * components_;
  877. for (int y = 0; y < height_; ++y)
  878. {
  879. for (int x = 0; x < width_; ++x)
  880. {
  881. for (unsigned c = 0; c < components_; ++c)
  882. newData[y * rowSize + x * components_ + c] = data_[y * rowSize + (width_ - x - 1) * components_ + c];
  883. }
  884. }
  885. data_ = newData;
  886. }
  887. else
  888. {
  889. if (compressedFormat_ > CF_DXT5)
  890. {
  891. URHO3D_LOGERROR("FlipHorizontal not yet implemented for other compressed formats than RGBA & DXT1,3,5");
  892. return false;
  893. }
  894. // Memory use = combined size of the compressed mip levels
  895. SharedArrayPtr<unsigned char> newData(new unsigned char[GetMemoryUse()]);
  896. unsigned dataOffset = 0;
  897. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  898. {
  899. CompressedLevel level = GetCompressedLevel(i);
  900. if (!level.data_)
  901. {
  902. URHO3D_LOGERROR("Got compressed level with no data, aborting horizontal flip");
  903. return false;
  904. }
  905. for (unsigned y = 0; y < level.rows_; ++y)
  906. {
  907. for (unsigned x = 0; x < level.rowSize_; x += level.blockSize_)
  908. {
  909. unsigned char* src = level.data_ + y * level.rowSize_ + (level.rowSize_ - level.blockSize_ - x);
  910. unsigned char* dest = newData.Get() + y * level.rowSize_ + x;
  911. FlipBlockHorizontal(dest, src, compressedFormat_);
  912. }
  913. }
  914. dataOffset += level.dataSize_;
  915. }
  916. data_ = newData;
  917. }
  918. return true;
  919. }
  920. bool Image::FlipVertical()
  921. {
  922. if (!data_)
  923. return false;
  924. if (depth_ > 1)
  925. {
  926. URHO3D_LOGERROR("FlipVertical not supported for 3D images");
  927. return false;
  928. }
  929. if (!IsCompressed())
  930. {
  931. SharedArrayPtr<unsigned char> newData(new unsigned char[width_ * height_ * components_]);
  932. unsigned rowSize = width_ * components_;
  933. for (int y = 0; y < height_; ++y)
  934. memcpy(&newData[(height_ - y - 1) * rowSize], &data_[y * rowSize], rowSize);
  935. data_ = newData;
  936. }
  937. else
  938. {
  939. if (compressedFormat_ > CF_DXT5)
  940. {
  941. URHO3D_LOGERROR("FlipVertical not yet implemented for other compressed formats than DXT1,3,5");
  942. return false;
  943. }
  944. // Memory use = combined size of the compressed mip levels
  945. SharedArrayPtr<unsigned char> newData(new unsigned char[GetMemoryUse()]);
  946. unsigned dataOffset = 0;
  947. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  948. {
  949. CompressedLevel level = GetCompressedLevel(i);
  950. if (!level.data_)
  951. {
  952. URHO3D_LOGERROR("Got compressed level with no data, aborting vertical flip");
  953. return false;
  954. }
  955. for (unsigned y = 0; y < level.rows_; ++y)
  956. {
  957. unsigned char* src = level.data_ + y * level.rowSize_;
  958. unsigned char* dest = newData.Get() + dataOffset + (level.rows_ - y - 1) * level.rowSize_;
  959. for (unsigned x = 0; x < level.rowSize_; x += level.blockSize_)
  960. FlipBlockVertical(dest + x, src + x, compressedFormat_);
  961. }
  962. dataOffset += level.dataSize_;
  963. }
  964. data_ = newData;
  965. }
  966. return true;
  967. }
  968. bool Image::Resize(int width, int height)
  969. {
  970. URHO3D_PROFILE(ResizeImage);
  971. if (IsCompressed())
  972. {
  973. URHO3D_LOGERROR("Resize not supported for compressed images");
  974. return false;
  975. }
  976. if (depth_ > 1)
  977. {
  978. URHO3D_LOGERROR("Resize not supported for 3D images");
  979. return false;
  980. }
  981. if (!data_ || width <= 0 || height <= 0)
  982. return false;
  983. /// \todo Reducing image size does not sample all needed pixels
  984. SharedArrayPtr<unsigned char> newData(new unsigned char[width * height * components_]);
  985. for (int y = 0; y < height; ++y)
  986. {
  987. for (int x = 0; x < width; ++x)
  988. {
  989. // Calculate float coordinates between 0 - 1 for resampling
  990. float xF = (width_ > 1) ? (float)x / (float)(width - 1) : 0.0f;
  991. float yF = (height_ > 1) ? (float)y / (float)(height - 1) : 0.0f;
  992. unsigned uintColor = GetPixelBilinear(xF, yF).ToUInt();
  993. unsigned char* dest = newData + (y * width + x) * components_;
  994. auto* src = (unsigned char*)&uintColor;
  995. switch (components_)
  996. {
  997. case 4:
  998. dest[3] = src[3];
  999. // Fall through
  1000. case 3:
  1001. dest[2] = src[2];
  1002. // Fall through
  1003. case 2:
  1004. dest[1] = src[1];
  1005. // Fall through
  1006. default:
  1007. dest[0] = src[0];
  1008. break;
  1009. }
  1010. }
  1011. }
  1012. width_ = width;
  1013. height_ = height;
  1014. data_ = newData;
  1015. SetMemoryUse(width * height * depth_ * components_);
  1016. return true;
  1017. }
  1018. void Image::Clear(const Color& color)
  1019. {
  1020. ClearInt(color.ToUInt());
  1021. }
  1022. void Image::ClearInt(unsigned uintColor)
  1023. {
  1024. URHO3D_PROFILE(ClearImage);
  1025. if (!data_)
  1026. return;
  1027. if (IsCompressed())
  1028. {
  1029. URHO3D_LOGERROR("Clear not supported for compressed images");
  1030. return;
  1031. }
  1032. if (components_ == 4)
  1033. {
  1034. unsigned color = uintColor;
  1035. auto* data = (unsigned*)GetData();
  1036. auto* data_end = (unsigned*)(GetData() + width_ * height_ * depth_ * components_);
  1037. for (; data < data_end; ++data)
  1038. *data = color;
  1039. }
  1040. else
  1041. {
  1042. auto* src = (unsigned char*)&uintColor;
  1043. for (unsigned i = 0; i < width_ * height_ * depth_ * components_; ++i)
  1044. data_[i] = src[i % components_];
  1045. }
  1046. }
  1047. bool Image::SaveBMP(const String& fileName) const
  1048. {
  1049. URHO3D_PROFILE(SaveImageBMP);
  1050. auto* fileSystem = GetSubsystem<FileSystem>();
  1051. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  1052. {
  1053. URHO3D_LOGERROR("Access denied to " + fileName);
  1054. return false;
  1055. }
  1056. if (IsCompressed())
  1057. {
  1058. URHO3D_LOGERROR("Can not save compressed image to BMP");
  1059. return false;
  1060. }
  1061. if (data_)
  1062. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.Get()) != 0;
  1063. else
  1064. return false;
  1065. }
  1066. bool Image::SavePNG(const String& fileName) const
  1067. {
  1068. URHO3D_PROFILE(SaveImagePNG);
  1069. File outFile(context_, fileName, FILE_WRITE);
  1070. if (outFile.IsOpen())
  1071. return Image::Save(outFile); // Save uses PNG format
  1072. else
  1073. return false;
  1074. }
  1075. bool Image::SaveTGA(const String& fileName) const
  1076. {
  1077. URHO3D_PROFILE(SaveImageTGA);
  1078. auto* fileSystem = GetSubsystem<FileSystem>();
  1079. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  1080. {
  1081. URHO3D_LOGERROR("Access denied to " + fileName);
  1082. return false;
  1083. }
  1084. if (IsCompressed())
  1085. {
  1086. URHO3D_LOGERROR("Can not save compressed image to TGA");
  1087. return false;
  1088. }
  1089. if (data_)
  1090. return stbi_write_tga(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get()) != 0;
  1091. else
  1092. return false;
  1093. }
  1094. bool Image::SaveJPG(const String& fileName, int quality) const
  1095. {
  1096. URHO3D_PROFILE(SaveImageJPG);
  1097. auto* fileSystem = GetSubsystem<FileSystem>();
  1098. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  1099. {
  1100. URHO3D_LOGERROR("Access denied to " + fileName);
  1101. return false;
  1102. }
  1103. if (IsCompressed())
  1104. {
  1105. URHO3D_LOGERROR("Can not save compressed image to JPG");
  1106. return false;
  1107. }
  1108. if (data_)
  1109. return stbi_write_jpg(GetNativePath(fileName).CString(), width_, height_, components_, data_.Get(), quality) != 0;
  1110. else
  1111. return false;
  1112. }
  1113. bool Image::SaveDDS(const String& fileName) const
  1114. {
  1115. URHO3D_PROFILE(SaveImageDDS);
  1116. File outFile(context_, fileName, FILE_WRITE);
  1117. if (!outFile.IsOpen())
  1118. {
  1119. URHO3D_LOGERROR("Access denied to " + fileName);
  1120. return false;
  1121. }
  1122. if (IsCompressed())
  1123. {
  1124. URHO3D_LOGERROR("Can not save compressed image to DDS");
  1125. return false;
  1126. }
  1127. if (components_ != 4)
  1128. {
  1129. URHO3D_LOGERRORF("Can not save image with %u components to DDS", components_);
  1130. return false;
  1131. }
  1132. // Write image
  1133. PODVector<const Image*> levels;
  1134. GetLevels(levels);
  1135. outFile.WriteFileID("DDS ");
  1136. DDSurfaceDesc2 ddsd; // NOLINT(hicpp-member-init)
  1137. memset(&ddsd, 0, sizeof(ddsd));
  1138. ddsd.dwSize_ = sizeof(ddsd);
  1139. ddsd.dwFlags_ = 0x00000001l /*DDSD_CAPS*/
  1140. | 0x00000002l /*DDSD_HEIGHT*/ | 0x00000004l /*DDSD_WIDTH*/ | 0x00020000l /*DDSD_MIPMAPCOUNT*/ | 0x00001000l /*DDSD_PIXELFORMAT*/;
  1141. ddsd.dwWidth_ = width_;
  1142. ddsd.dwHeight_ = height_;
  1143. ddsd.dwMipMapCount_ = levels.Size();
  1144. ddsd.ddpfPixelFormat_.dwFlags_ = 0x00000040l /*DDPF_RGB*/ | 0x00000001l /*DDPF_ALPHAPIXELS*/;
  1145. ddsd.ddpfPixelFormat_.dwSize_ = sizeof(ddsd.ddpfPixelFormat_);
  1146. ddsd.ddpfPixelFormat_.dwRGBBitCount_ = 32;
  1147. ddsd.ddpfPixelFormat_.dwRBitMask_ = 0x000000ff;
  1148. ddsd.ddpfPixelFormat_.dwGBitMask_ = 0x0000ff00;
  1149. ddsd.ddpfPixelFormat_.dwBBitMask_ = 0x00ff0000;
  1150. ddsd.ddpfPixelFormat_.dwRGBAlphaBitMask_ = 0xff000000;
  1151. outFile.Write(&ddsd, sizeof(ddsd));
  1152. for (unsigned i = 0; i < levels.Size(); ++i)
  1153. outFile.Write(levels[i]->GetData(), levels[i]->GetWidth() * levels[i]->GetHeight() * 4);
  1154. return true;
  1155. }
  1156. bool Image::SaveWEBP(const String& fileName, float compression /* = 0.0f */) const
  1157. {
  1158. #ifdef URHO3D_WEBP
  1159. URHO3D_PROFILE(SaveImageWEBP);
  1160. auto* fileSystem(GetSubsystem<FileSystem>());
  1161. File outFile(context_, fileName, FILE_WRITE);
  1162. if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName)))
  1163. {
  1164. URHO3D_LOGERROR("Access denied to " + fileName);
  1165. return false;
  1166. }
  1167. if (IsCompressed())
  1168. {
  1169. URHO3D_LOGERROR("Can not save compressed image to WebP");
  1170. return false;
  1171. }
  1172. if (height_ > WEBP_MAX_DIMENSION || width_ > WEBP_MAX_DIMENSION)
  1173. {
  1174. URHO3D_LOGERROR("Maximum dimension supported by WebP is " + String(WEBP_MAX_DIMENSION));
  1175. return false;
  1176. }
  1177. if (components_ != 4 && components_ != 3)
  1178. {
  1179. URHO3D_LOGERRORF("Can not save image with %u components to WebP, which requires 3 or 4; Try ConvertToRGBA first?", components_);
  1180. return false;
  1181. }
  1182. if (!data_)
  1183. {
  1184. URHO3D_LOGERROR("No image data to save");
  1185. return false;
  1186. }
  1187. WebPPicture pic;
  1188. WebPConfig config;
  1189. WebPMemoryWriter wrt;
  1190. int importResult(0);
  1191. size_t encodeResult(0);
  1192. if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, compression) || !WebPPictureInit(&pic))
  1193. {
  1194. URHO3D_LOGERROR("WebP initialization failed; check installation");
  1195. return false;
  1196. }
  1197. config.lossless = 1;
  1198. config.exact = 1; // Preserve RGB values under transparency, as they may be wanted.
  1199. pic.use_argb = 1;
  1200. pic.width = width_;
  1201. pic.height = height_;
  1202. pic.writer = WebPMemoryWrite;
  1203. pic.custom_ptr = &wrt;
  1204. WebPMemoryWriterInit(&wrt);
  1205. if (components_ == 4)
  1206. importResult = WebPPictureImportRGBA(&pic, data_.Get(), components_ * width_);
  1207. else if (components_ == 3)
  1208. importResult = WebPPictureImportRGB(&pic, data_.Get(), components_ * width_);
  1209. if (!importResult)
  1210. {
  1211. URHO3D_LOGERROR("WebP import of image data failed (truncated RGBA/RGB data or memory error?)");
  1212. WebPPictureFree(&pic);
  1213. WebPMemoryWriterClear(&wrt);
  1214. return false;
  1215. }
  1216. encodeResult = WebPEncode(&config, &pic);
  1217. // Check only general failure. WebPEncode() sets pic.error_code with specific error.
  1218. if (!encodeResult)
  1219. {
  1220. URHO3D_LOGERRORF("WebP encoding failed (memory error?). WebPEncodingError = %d", pic.error_code);
  1221. WebPPictureFree(&pic);
  1222. WebPMemoryWriterClear(&wrt);
  1223. return false;
  1224. }
  1225. WebPPictureFree(&pic);
  1226. outFile.Write(wrt.mem, wrt.size);
  1227. WebPMemoryWriterClear(&wrt);
  1228. return true;
  1229. #else
  1230. URHO3D_LOGERROR("Cannot save in WEBP format, support not compiled in");
  1231. return false;
  1232. #endif
  1233. }
  1234. Color Image::GetPixel(int x, int y) const
  1235. {
  1236. return GetPixel(x, y, 0);
  1237. }
  1238. Color Image::GetPixel(int x, int y, int z) const
  1239. {
  1240. if (!data_ || z < 0 || z >= depth_ || IsCompressed())
  1241. return Color::BLACK;
  1242. x = Clamp(x, 0, width_ - 1);
  1243. y = Clamp(y, 0, height_ - 1);
  1244. unsigned char* src = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  1245. Color ret;
  1246. switch (components_)
  1247. {
  1248. case 4:
  1249. ret.a_ = (float)src[3] / 255.0f;
  1250. // Fall through
  1251. case 3:
  1252. ret.b_ = (float)src[2] / 255.0f;
  1253. // Fall through
  1254. case 2:
  1255. ret.g_ = (float)src[1] / 255.0f;
  1256. ret.r_ = (float)src[0] / 255.0f;
  1257. break;
  1258. default:
  1259. ret.r_ = ret.g_ = ret.b_ = (float)src[0] / 255.0f;
  1260. break;
  1261. }
  1262. return ret;
  1263. }
  1264. unsigned Image::GetPixelInt(int x, int y) const
  1265. {
  1266. return GetPixelInt(x, y, 0);
  1267. }
  1268. unsigned Image::GetPixelInt(int x, int y, int z) const
  1269. {
  1270. if (!data_ || z < 0 || z >= depth_ || IsCompressed())
  1271. return 0xff000000;
  1272. x = Clamp(x, 0, width_ - 1);
  1273. y = Clamp(y, 0, height_ - 1);
  1274. unsigned char* src = data_ + (z * width_ * height_ + y * width_ + x) * components_;
  1275. unsigned ret = 0;
  1276. if (components_ < 4)
  1277. ret |= 0xff000000;
  1278. switch (components_)
  1279. {
  1280. case 4:
  1281. ret |= (unsigned)src[3] << 24;
  1282. // Fall through
  1283. case 3:
  1284. ret |= (unsigned)src[2] << 16;
  1285. // Fall through
  1286. case 2:
  1287. ret |= (unsigned)src[1] << 8;
  1288. ret |= (unsigned)src[0];
  1289. break;
  1290. default:
  1291. ret |= (unsigned)src[0] << 16;
  1292. ret |= (unsigned)src[0] << 8;
  1293. ret |= (unsigned)src[0];
  1294. break;
  1295. }
  1296. return ret;
  1297. }
  1298. Color Image::GetPixelBilinear(float x, float y) const
  1299. {
  1300. x = Clamp(x * width_ - 0.5f, 0.0f, (float)(width_ - 1));
  1301. y = Clamp(y * height_ - 0.5f, 0.0f, (float)(height_ - 1));
  1302. auto xI = (int)x;
  1303. auto yI = (int)y;
  1304. float xF = Fract(x);
  1305. float yF = Fract(y);
  1306. Color topColor = GetPixel(xI, yI).Lerp(GetPixel(xI + 1, yI), xF);
  1307. Color bottomColor = GetPixel(xI, yI + 1).Lerp(GetPixel(xI + 1, yI + 1), xF);
  1308. return topColor.Lerp(bottomColor, yF);
  1309. }
  1310. Color Image::GetPixelTrilinear(float x, float y, float z) const
  1311. {
  1312. if (depth_ < 2)
  1313. return GetPixelBilinear(x, y);
  1314. x = Clamp(x * width_ - 0.5f, 0.0f, (float)(width_ - 1));
  1315. y = Clamp(y * height_ - 0.5f, 0.0f, (float)(height_ - 1));
  1316. z = Clamp(z * depth_ - 0.5f, 0.0f, (float)(depth_ - 1));
  1317. auto xI = (int)x;
  1318. auto yI = (int)y;
  1319. auto zI = (int)z;
  1320. if (zI == depth_ - 1)
  1321. return GetPixelBilinear(x, y);
  1322. float xF = Fract(x);
  1323. float yF = Fract(y);
  1324. float zF = Fract(z);
  1325. Color topColorNear = GetPixel(xI, yI, zI).Lerp(GetPixel(xI + 1, yI, zI), xF);
  1326. Color bottomColorNear = GetPixel(xI, yI + 1, zI).Lerp(GetPixel(xI + 1, yI + 1, zI), xF);
  1327. Color colorNear = topColorNear.Lerp(bottomColorNear, yF);
  1328. Color topColorFar = GetPixel(xI, yI, zI + 1).Lerp(GetPixel(xI + 1, yI, zI + 1), xF);
  1329. Color bottomColorFar = GetPixel(xI, yI + 1, zI + 1).Lerp(GetPixel(xI + 1, yI + 1, zI + 1), xF);
  1330. Color colorFar = topColorFar.Lerp(bottomColorFar, yF);
  1331. return colorNear.Lerp(colorFar, zF);
  1332. }
  1333. SharedPtr<Image> Image::GetNextLevel() const
  1334. {
  1335. if (IsCompressed())
  1336. {
  1337. URHO3D_LOGERROR("Can not generate mip level from compressed data");
  1338. return SharedPtr<Image>();
  1339. }
  1340. if (components_ < 1 || components_ > 4)
  1341. {
  1342. URHO3D_LOGERROR("Illegal number of image components for mip level generation");
  1343. return SharedPtr<Image>();
  1344. }
  1345. if (nextLevel_)
  1346. return nextLevel_;
  1347. URHO3D_PROFILE(CalculateImageMipLevel);
  1348. int widthOut = width_ / 2;
  1349. int heightOut = height_ / 2;
  1350. int depthOut = depth_ / 2;
  1351. if (widthOut < 1)
  1352. widthOut = 1;
  1353. if (heightOut < 1)
  1354. heightOut = 1;
  1355. if (depthOut < 1)
  1356. depthOut = 1;
  1357. SharedPtr<Image> mipImage(new Image(context_));
  1358. if (depth_ > 1)
  1359. mipImage->SetSize(widthOut, heightOut, depthOut, components_);
  1360. else
  1361. mipImage->SetSize(widthOut, heightOut, components_);
  1362. const unsigned char* pixelDataIn = data_.Get();
  1363. unsigned char* pixelDataOut = mipImage->data_.Get();
  1364. // 1D case
  1365. if (depth_ == 1 && (height_ == 1 || width_ == 1))
  1366. {
  1367. // Loop using the larger dimension
  1368. if (widthOut < heightOut)
  1369. widthOut = heightOut;
  1370. switch (components_)
  1371. {
  1372. case 1:
  1373. for (int x = 0; x < widthOut; ++x)
  1374. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 1]) >> 1);
  1375. break;
  1376. case 2:
  1377. for (int x = 0; x < widthOut * 2; x += 2)
  1378. {
  1379. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 2]) >> 1);
  1380. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 3]) >> 1);
  1381. }
  1382. break;
  1383. case 3:
  1384. for (int x = 0; x < widthOut * 3; x += 3)
  1385. {
  1386. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 3]) >> 1);
  1387. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 4]) >> 1);
  1388. pixelDataOut[x + 2] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 2] + pixelDataIn[x * 2 + 5]) >> 1);
  1389. }
  1390. break;
  1391. case 4:
  1392. for (int x = 0; x < widthOut * 4; x += 4)
  1393. {
  1394. pixelDataOut[x] = (unsigned char)(((unsigned)pixelDataIn[x * 2] + pixelDataIn[x * 2 + 4]) >> 1);
  1395. pixelDataOut[x + 1] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 1] + pixelDataIn[x * 2 + 5]) >> 1);
  1396. pixelDataOut[x + 2] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 2] + pixelDataIn[x * 2 + 6]) >> 1);
  1397. pixelDataOut[x + 3] = (unsigned char)(((unsigned)pixelDataIn[x * 2 + 3] + pixelDataIn[x * 2 + 7]) >> 1);
  1398. }
  1399. break;
  1400. default:
  1401. assert(false); // Should never reach here
  1402. break;
  1403. }
  1404. }
  1405. // 2D case
  1406. else if (depth_ == 1)
  1407. {
  1408. switch (components_)
  1409. {
  1410. case 1:
  1411. for (int y = 0; y < heightOut; ++y)
  1412. {
  1413. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_];
  1414. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_];
  1415. unsigned char* out = &pixelDataOut[y * widthOut];
  1416. for (int x = 0; x < widthOut; ++x)
  1417. {
  1418. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 1] +
  1419. inLower[x * 2] + inLower[x * 2 + 1]) >> 2);
  1420. }
  1421. }
  1422. break;
  1423. case 2:
  1424. for (int y = 0; y < heightOut; ++y)
  1425. {
  1426. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 2];
  1427. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 2];
  1428. unsigned char* out = &pixelDataOut[y * widthOut * 2];
  1429. for (int x = 0; x < widthOut * 2; x += 2)
  1430. {
  1431. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 2] +
  1432. inLower[x * 2] + inLower[x * 2 + 2]) >> 2);
  1433. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 3] +
  1434. inLower[x * 2 + 1] + inLower[x * 2 + 3]) >> 2);
  1435. }
  1436. }
  1437. break;
  1438. case 3:
  1439. for (int y = 0; y < heightOut; ++y)
  1440. {
  1441. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 3];
  1442. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 3];
  1443. unsigned char* out = &pixelDataOut[y * widthOut * 3];
  1444. for (int x = 0; x < widthOut * 3; x += 3)
  1445. {
  1446. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 3] +
  1447. inLower[x * 2] + inLower[x * 2 + 3]) >> 2);
  1448. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 4] +
  1449. inLower[x * 2 + 1] + inLower[x * 2 + 4]) >> 2);
  1450. out[x + 2] = (unsigned char)(((unsigned)inUpper[x * 2 + 2] + inUpper[x * 2 + 5] +
  1451. inLower[x * 2 + 2] + inLower[x * 2 + 5]) >> 2);
  1452. }
  1453. }
  1454. break;
  1455. case 4:
  1456. for (int y = 0; y < heightOut; ++y)
  1457. {
  1458. const unsigned char* inUpper = &pixelDataIn[(y * 2) * width_ * 4];
  1459. const unsigned char* inLower = &pixelDataIn[(y * 2 + 1) * width_ * 4];
  1460. unsigned char* out = &pixelDataOut[y * widthOut * 4];
  1461. for (int x = 0; x < widthOut * 4; x += 4)
  1462. {
  1463. out[x] = (unsigned char)(((unsigned)inUpper[x * 2] + inUpper[x * 2 + 4] +
  1464. inLower[x * 2] + inLower[x * 2 + 4]) >> 2);
  1465. out[x + 1] = (unsigned char)(((unsigned)inUpper[x * 2 + 1] + inUpper[x * 2 + 5] +
  1466. inLower[x * 2 + 1] + inLower[x * 2 + 5]) >> 2);
  1467. out[x + 2] = (unsigned char)(((unsigned)inUpper[x * 2 + 2] + inUpper[x * 2 + 6] +
  1468. inLower[x * 2 + 2] + inLower[x * 2 + 6]) >> 2);
  1469. out[x + 3] = (unsigned char)(((unsigned)inUpper[x * 2 + 3] + inUpper[x * 2 + 7] +
  1470. inLower[x * 2 + 3] + inLower[x * 2 + 7]) >> 2);
  1471. }
  1472. }
  1473. break;
  1474. default:
  1475. assert(false); // Should never reach here
  1476. break;
  1477. }
  1478. }
  1479. // 3D case
  1480. else
  1481. {
  1482. switch (components_)
  1483. {
  1484. case 1:
  1485. for (int z = 0; z < depthOut; ++z)
  1486. {
  1487. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_];
  1488. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_];
  1489. for (int y = 0; y < heightOut; ++y)
  1490. {
  1491. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_];
  1492. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_];
  1493. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_];
  1494. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_];
  1495. unsigned char* out = &pixelDataOut[z * widthOut * heightOut + y * widthOut];
  1496. for (int x = 0; x < widthOut; ++x)
  1497. {
  1498. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 1] +
  1499. inOuterLower[x * 2] + inOuterLower[x * 2 + 1] +
  1500. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 1] +
  1501. inInnerLower[x * 2] + inInnerLower[x * 2 + 1]) >> 3);
  1502. }
  1503. }
  1504. }
  1505. break;
  1506. case 2:
  1507. for (int z = 0; z < depthOut; ++z)
  1508. {
  1509. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 2];
  1510. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 2];
  1511. for (int y = 0; y < heightOut; ++y)
  1512. {
  1513. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 2];
  1514. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 2];
  1515. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 2];
  1516. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 2];
  1517. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 2 + y * widthOut * 2];
  1518. for (int x = 0; x < widthOut * 2; x += 2)
  1519. {
  1520. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 2] +
  1521. inOuterLower[x * 2] + inOuterLower[x * 2 + 2] +
  1522. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 2] +
  1523. inInnerLower[x * 2] + inInnerLower[x * 2 + 2]) >> 3);
  1524. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 3] +
  1525. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 3] +
  1526. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 3] +
  1527. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 3]) >> 3);
  1528. }
  1529. }
  1530. }
  1531. break;
  1532. case 3:
  1533. for (int z = 0; z < depthOut; ++z)
  1534. {
  1535. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 3];
  1536. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 3];
  1537. for (int y = 0; y < heightOut; ++y)
  1538. {
  1539. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 3];
  1540. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 3];
  1541. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 3];
  1542. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 3];
  1543. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 3 + y * widthOut * 3];
  1544. for (int x = 0; x < widthOut * 3; x += 3)
  1545. {
  1546. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 3] +
  1547. inOuterLower[x * 2] + inOuterLower[x * 2 + 3] +
  1548. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 3] +
  1549. inInnerLower[x * 2] + inInnerLower[x * 2 + 3]) >> 3);
  1550. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 4] +
  1551. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 4] +
  1552. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 4] +
  1553. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 4]) >> 3);
  1554. out[x + 2] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 2] + inOuterUpper[x * 2 + 5] +
  1555. inOuterLower[x * 2 + 2] + inOuterLower[x * 2 + 5] +
  1556. inInnerUpper[x * 2 + 2] + inInnerUpper[x * 2 + 5] +
  1557. inInnerLower[x * 2 + 2] + inInnerLower[x * 2 + 5]) >> 3);
  1558. }
  1559. }
  1560. }
  1561. break;
  1562. case 4:
  1563. for (int z = 0; z < depthOut; ++z)
  1564. {
  1565. const unsigned char* inOuter = &pixelDataIn[(z * 2) * width_ * height_ * 4];
  1566. const unsigned char* inInner = &pixelDataIn[(z * 2 + 1) * width_ * height_ * 4];
  1567. for (int y = 0; y < heightOut; ++y)
  1568. {
  1569. const unsigned char* inOuterUpper = &inOuter[(y * 2) * width_ * 4];
  1570. const unsigned char* inOuterLower = &inOuter[(y * 2 + 1) * width_ * 4];
  1571. const unsigned char* inInnerUpper = &inInner[(y * 2) * width_ * 4];
  1572. const unsigned char* inInnerLower = &inInner[(y * 2 + 1) * width_ * 4];
  1573. unsigned char* out = &pixelDataOut[z * widthOut * heightOut * 4 + y * widthOut * 4];
  1574. for (int x = 0; x < widthOut * 4; x += 4)
  1575. {
  1576. out[x] = (unsigned char)(((unsigned)inOuterUpper[x * 2] + inOuterUpper[x * 2 + 4] +
  1577. inOuterLower[x * 2] + inOuterLower[x * 2 + 4] +
  1578. inInnerUpper[x * 2] + inInnerUpper[x * 2 + 4] +
  1579. inInnerLower[x * 2] + inInnerLower[x * 2 + 4]) >> 3);
  1580. out[x + 1] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 1] + inOuterUpper[x * 2 + 5] +
  1581. inOuterLower[x * 2 + 1] + inOuterLower[x * 2 + 5] +
  1582. inInnerUpper[x * 2 + 1] + inInnerUpper[x * 2 + 5] +
  1583. inInnerLower[x * 2 + 1] + inInnerLower[x * 2 + 5]) >> 3);
  1584. out[x + 2] = (unsigned char)(((unsigned)inOuterUpper[x * 2 + 2] + inOuterUpper[x * 2 + 6] +
  1585. inOuterLower[x * 2 + 2] + inOuterLower[x * 2 + 6] +
  1586. inInnerUpper[x * 2 + 2] + inInnerUpper[x * 2 + 6] +
  1587. inInnerLower[x * 2 + 2] + inInnerLower[x * 2 + 6]) >> 3);
  1588. }
  1589. }
  1590. }
  1591. break;
  1592. default:
  1593. assert(false); // Should never reach here
  1594. break;
  1595. }
  1596. }
  1597. return mipImage;
  1598. }
  1599. SharedPtr<Image> Image::ConvertToRGBA() const
  1600. {
  1601. if (IsCompressed())
  1602. {
  1603. URHO3D_LOGERROR("Can not convert compressed image to RGBA");
  1604. return SharedPtr<Image>();
  1605. }
  1606. if (components_ < 1 || components_ > 4)
  1607. {
  1608. URHO3D_LOGERROR("Illegal number of image components for conversion to RGBA");
  1609. return SharedPtr<Image>();
  1610. }
  1611. if (!data_)
  1612. {
  1613. URHO3D_LOGERROR("Can not convert image without data to RGBA");
  1614. return SharedPtr<Image>();
  1615. }
  1616. // Already RGBA?
  1617. if (components_ == 4)
  1618. return SharedPtr<Image>(const_cast<Image*>(this));
  1619. SharedPtr<Image> ret(new Image(context_));
  1620. ret->SetSize(width_, height_, depth_, 4);
  1621. const unsigned char* src = data_;
  1622. unsigned char* dest = ret->GetData();
  1623. switch (components_)
  1624. {
  1625. case 1:
  1626. for (unsigned i = 0; i < static_cast<unsigned>(width_ * height_ * depth_); ++i)
  1627. {
  1628. unsigned char pixel = *src++;
  1629. *dest++ = pixel;
  1630. *dest++ = pixel;
  1631. *dest++ = pixel;
  1632. *dest++ = 255;
  1633. }
  1634. break;
  1635. case 2:
  1636. for (unsigned i = 0; i < static_cast<unsigned>(width_ * height_ * depth_); ++i)
  1637. {
  1638. unsigned char pixel = *src++;
  1639. *dest++ = pixel;
  1640. *dest++ = pixel;
  1641. *dest++ = pixel;
  1642. *dest++ = *src++;
  1643. }
  1644. break;
  1645. case 3:
  1646. for (unsigned i = 0; i < static_cast<unsigned>(width_ * height_ * depth_); ++i)
  1647. {
  1648. *dest++ = *src++;
  1649. *dest++ = *src++;
  1650. *dest++ = *src++;
  1651. *dest++ = 255;
  1652. }
  1653. break;
  1654. default:
  1655. assert(false); // Should never reach nere
  1656. break;
  1657. }
  1658. return ret;
  1659. }
  1660. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  1661. {
  1662. CompressedLevel level;
  1663. if (compressedFormat_ == CF_NONE)
  1664. {
  1665. URHO3D_LOGERROR("Image is not compressed");
  1666. return level;
  1667. }
  1668. if (index >= numCompressedLevels_)
  1669. {
  1670. URHO3D_LOGERROR("Compressed image mip level out of bounds");
  1671. return level;
  1672. }
  1673. level.format_ = compressedFormat_;
  1674. level.width_ = width_;
  1675. level.height_ = height_;
  1676. level.depth_ = depth_;
  1677. if (compressedFormat_ == CF_RGBA)
  1678. {
  1679. level.blockSize_ = 4;
  1680. unsigned i = 0;
  1681. unsigned offset = 0;
  1682. for (;;)
  1683. {
  1684. if (!level.width_)
  1685. level.width_ = 1;
  1686. if (!level.height_)
  1687. level.height_ = 1;
  1688. if (!level.depth_)
  1689. level.depth_ = 1;
  1690. level.rowSize_ = level.width_ * level.blockSize_;
  1691. level.rows_ = (unsigned)level.height_;
  1692. level.data_ = data_.Get() + offset;
  1693. level.dataSize_ = level.depth_ * level.rows_ * level.rowSize_;
  1694. if (offset + level.dataSize_ > GetMemoryUse())
  1695. {
  1696. URHO3D_LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1697. " Datasize: " + String(GetMemoryUse()));
  1698. level.data_ = nullptr;
  1699. return level;
  1700. }
  1701. if (i == index)
  1702. return level;
  1703. offset += level.dataSize_;
  1704. level.width_ /= 2;
  1705. level.height_ /= 2;
  1706. level.depth_ /= 2;
  1707. ++i;
  1708. }
  1709. }
  1710. else if (compressedFormat_ < CF_PVRTC_RGB_2BPP)
  1711. {
  1712. level.blockSize_ = (compressedFormat_ == CF_DXT1 || compressedFormat_ == CF_ETC1) ? 8 : 16;
  1713. unsigned i = 0;
  1714. unsigned offset = 0;
  1715. for (;;)
  1716. {
  1717. if (!level.width_)
  1718. level.width_ = 1;
  1719. if (!level.height_)
  1720. level.height_ = 1;
  1721. if (!level.depth_)
  1722. level.depth_ = 1;
  1723. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  1724. level.rows_ = (unsigned)((level.height_ + 3) / 4);
  1725. level.data_ = data_.Get() + offset;
  1726. level.dataSize_ = level.depth_ * level.rows_ * level.rowSize_;
  1727. if (offset + level.dataSize_ > GetMemoryUse())
  1728. {
  1729. URHO3D_LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1730. " Datasize: " + String(GetMemoryUse()));
  1731. level.data_ = nullptr;
  1732. return level;
  1733. }
  1734. if (i == index)
  1735. return level;
  1736. offset += level.dataSize_;
  1737. level.width_ /= 2;
  1738. level.height_ /= 2;
  1739. level.depth_ /= 2;
  1740. ++i;
  1741. }
  1742. }
  1743. else
  1744. {
  1745. level.blockSize_ = compressedFormat_ < CF_PVRTC_RGB_4BPP ? 2 : 4;
  1746. unsigned i = 0;
  1747. unsigned offset = 0;
  1748. for (;;)
  1749. {
  1750. if (!level.width_)
  1751. level.width_ = 1;
  1752. if (!level.height_)
  1753. level.height_ = 1;
  1754. int dataWidth = Max(level.width_, level.blockSize_ == 2 ? 16 : 8);
  1755. int dataHeight = Max(level.height_, 8);
  1756. level.data_ = data_.Get() + offset;
  1757. level.dataSize_ = (dataWidth * dataHeight * level.blockSize_ + 7) >> 3;
  1758. level.rows_ = (unsigned)dataHeight;
  1759. level.rowSize_ = level.dataSize_ / level.rows_;
  1760. if (offset + level.dataSize_ > GetMemoryUse())
  1761. {
  1762. URHO3D_LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  1763. " Datasize: " + String(GetMemoryUse()));
  1764. level.data_ = nullptr;
  1765. return level;
  1766. }
  1767. if (i == index)
  1768. return level;
  1769. offset += level.dataSize_;
  1770. level.width_ /= 2;
  1771. level.height_ /= 2;
  1772. ++i;
  1773. }
  1774. }
  1775. }
  1776. Image* Image::GetSubimage(const IntRect& rect) const
  1777. {
  1778. if (!data_)
  1779. return nullptr;
  1780. if (depth_ > 1)
  1781. {
  1782. URHO3D_LOGERROR("Subimage not supported for 3D images");
  1783. return nullptr;
  1784. }
  1785. if (rect.left_ < 0 || rect.top_ < 0 || rect.right_ > width_ || rect.bottom_ > height_ || !rect.Width() || !rect.Height())
  1786. {
  1787. URHO3D_LOGERROR("Can not get subimage from image " + GetName() + " with invalid region");
  1788. return nullptr;
  1789. }
  1790. if (!IsCompressed())
  1791. {
  1792. int x = rect.left_;
  1793. int y = rect.top_;
  1794. int width = rect.Width();
  1795. int height = rect.Height();
  1796. auto* image = new Image(context_);
  1797. image->SetSize(width, height, components_);
  1798. unsigned char* dest = image->GetData();
  1799. unsigned char* src = data_.Get() + (y * width_ + x) * components_;
  1800. for (int i = 0; i < height; ++i)
  1801. {
  1802. memcpy(dest, src, (size_t)width * components_);
  1803. dest += width * components_;
  1804. src += width_ * components_;
  1805. }
  1806. return image;
  1807. }
  1808. else
  1809. {
  1810. // Pad the region to be a multiple of block size
  1811. IntRect paddedRect = rect;
  1812. paddedRect.left_ = (rect.left_ / 4) * 4;
  1813. paddedRect.top_ = (rect.top_ / 4) * 4;
  1814. paddedRect.right_ = (rect.right_ / 4) * 4;
  1815. paddedRect.bottom_ = (rect.bottom_ / 4) * 4;
  1816. IntRect currentRect = paddedRect;
  1817. PODVector<unsigned char> subimageData;
  1818. unsigned subimageLevels = 0;
  1819. // Save as many mips as possible until the next mip would cross a block boundary
  1820. for (unsigned i = 0; i < numCompressedLevels_; ++i)
  1821. {
  1822. CompressedLevel level = GetCompressedLevel(i);
  1823. if (!level.data_)
  1824. break;
  1825. // Mips are stored continuously
  1826. unsigned destStartOffset = subimageData.Size();
  1827. unsigned destRowSize = currentRect.Width() / 4 * level.blockSize_;
  1828. unsigned destSize = currentRect.Height() / 4 * destRowSize;
  1829. if (!destSize)
  1830. break;
  1831. subimageData.Resize(destStartOffset + destSize);
  1832. unsigned char* dest = &subimageData[destStartOffset];
  1833. for (int y = currentRect.top_; y < currentRect.bottom_; y += 4)
  1834. {
  1835. unsigned char* src = level.data_ + level.rowSize_ * (y / 4) + currentRect.left_ / 4 * level.blockSize_;
  1836. memcpy(dest, src, destRowSize);
  1837. dest += destRowSize;
  1838. }
  1839. ++subimageLevels;
  1840. if ((currentRect.left_ & 4) || (currentRect.right_ & 4) || (currentRect.top_ & 4) || (currentRect.bottom_ & 4))
  1841. break;
  1842. else
  1843. {
  1844. currentRect.left_ /= 2;
  1845. currentRect.right_ /= 2;
  1846. currentRect.top_ /= 2;
  1847. currentRect.bottom_ /= 2;
  1848. }
  1849. }
  1850. if (!subimageLevels)
  1851. {
  1852. URHO3D_LOGERROR("Subimage region from compressed image " + GetName() + " did not produce any data");
  1853. return nullptr;
  1854. }
  1855. auto* image = new Image(context_);
  1856. image->width_ = paddedRect.Width();
  1857. image->height_ = paddedRect.Height();
  1858. image->depth_ = 1;
  1859. image->compressedFormat_ = compressedFormat_;
  1860. image->numCompressedLevels_ = subimageLevels;
  1861. image->components_ = components_;
  1862. image->data_ = new unsigned char[subimageData.Size()];
  1863. memcpy(image->data_.Get(), &subimageData[0], subimageData.Size());
  1864. image->SetMemoryUse(subimageData.Size());
  1865. return image;
  1866. }
  1867. }
  1868. SDL_Surface* Image::GetSDLSurface(const IntRect& rect) const
  1869. {
  1870. if (!data_)
  1871. return nullptr;
  1872. if (depth_ > 1)
  1873. {
  1874. URHO3D_LOGERROR("Can not get SDL surface from 3D image");
  1875. return nullptr;
  1876. }
  1877. if (IsCompressed())
  1878. {
  1879. URHO3D_LOGERROR("Can not get SDL surface from compressed image " + GetName());
  1880. return nullptr;
  1881. }
  1882. if (components_ < 3)
  1883. {
  1884. URHO3D_LOGERROR("Can not get SDL surface from image " + GetName() + " with less than 3 components");
  1885. return nullptr;
  1886. }
  1887. IntRect imageRect = rect;
  1888. // Use full image if illegal rect
  1889. if (imageRect.left_ < 0 || imageRect.top_ < 0 || imageRect.right_ > width_ || imageRect.bottom_ > height_ ||
  1890. imageRect.left_ >= imageRect.right_ || imageRect.top_ >= imageRect.bottom_)
  1891. {
  1892. imageRect.left_ = 0;
  1893. imageRect.top_ = 0;
  1894. imageRect.right_ = width_;
  1895. imageRect.bottom_ = height_;
  1896. }
  1897. int imageWidth = width_;
  1898. int width = imageRect.Width();
  1899. int height = imageRect.Height();
  1900. // Assume little-endian for all the supported platforms
  1901. unsigned rMask = 0x000000ff;
  1902. unsigned gMask = 0x0000ff00;
  1903. unsigned bMask = 0x00ff0000;
  1904. unsigned aMask = 0xff000000;
  1905. SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, components_ * 8, rMask, gMask, bMask, aMask);
  1906. if (surface)
  1907. {
  1908. SDL_LockSurface(surface);
  1909. auto* destination = reinterpret_cast<unsigned char*>(surface->pixels);
  1910. unsigned char* source = data_ + components_ * (imageWidth * imageRect.top_ + imageRect.left_);
  1911. for (int i = 0; i < height; ++i)
  1912. {
  1913. memcpy(destination, source, (size_t)components_ * width);
  1914. destination += surface->pitch;
  1915. source += components_ * imageWidth;
  1916. }
  1917. SDL_UnlockSurface(surface);
  1918. }
  1919. else
  1920. URHO3D_LOGERROR("Failed to create SDL surface from image " + GetName());
  1921. return surface;
  1922. }
  1923. void Image::PrecalculateLevels()
  1924. {
  1925. if (!data_ || IsCompressed())
  1926. return;
  1927. URHO3D_PROFILE(PrecalculateImageMipLevels);
  1928. nextLevel_.Reset();
  1929. if (width_ > 1 || height_ > 1)
  1930. {
  1931. SharedPtr<Image> current = GetNextLevel();
  1932. nextLevel_ = current;
  1933. while (current && (current->width_ > 1 || current->height_ > 1))
  1934. {
  1935. current->nextLevel_ = current->GetNextLevel();
  1936. current = current->nextLevel_;
  1937. }
  1938. }
  1939. }
  1940. void Image::CleanupLevels()
  1941. {
  1942. nextLevel_.Reset();
  1943. }
  1944. void Image::GetLevels(PODVector<Image*>& levels)
  1945. {
  1946. levels.Clear();
  1947. Image* image = this;
  1948. while (image)
  1949. {
  1950. levels.Push(image);
  1951. image = image->nextLevel_;
  1952. }
  1953. }
  1954. void Image::GetLevels(PODVector<const Image*>& levels) const
  1955. {
  1956. levels.Clear();
  1957. const Image* image = this;
  1958. while (image)
  1959. {
  1960. levels.Push(image);
  1961. image = image->nextLevel_;
  1962. }
  1963. }
  1964. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  1965. {
  1966. unsigned dataSize = source.GetSize();
  1967. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  1968. source.Read(buffer.Get(), dataSize);
  1969. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int*)&components, 0);
  1970. }
  1971. void Image::FreeImageData(unsigned char* pixelData)
  1972. {
  1973. if (!pixelData)
  1974. return;
  1975. stbi_image_free(pixelData);
  1976. }
  1977. bool Image::HasAlphaChannel() const
  1978. {
  1979. return components_ > 3;
  1980. }
  1981. // Author: Josh Engebretson (AtomicGameEngine)
  1982. bool Image::SetSubimage(const Image* image, const IntRect& rect)
  1983. {
  1984. if (!data_)
  1985. return false;
  1986. if (depth_ > 1 || IsCompressed())
  1987. {
  1988. URHO3D_LOGERROR("Image::SetSubimage is not supported for compressed or 3D images");
  1989. return false;
  1990. }
  1991. if (components_ != image->components_)
  1992. {
  1993. URHO3D_LOGERROR("Can not set subimage in image " + GetName() + " with different number of components");
  1994. return false;
  1995. }
  1996. if (rect.left_ < 0 || rect.top_ < 0 || rect.right_ > width_ || rect.bottom_ > height_ || !rect.Width() || !rect.Height())
  1997. {
  1998. URHO3D_LOGERROR("Can not set subimage in image " + GetName() + " with invalid region");
  1999. return false;
  2000. }
  2001. const int destWidth = rect.Width();
  2002. const int destHeight = rect.Height();
  2003. if (destWidth == image->GetWidth() && destHeight == image->GetHeight())
  2004. {
  2005. unsigned char* src = image->GetData();
  2006. unsigned char* dest = data_.Get() + (rect.top_ * width_ + rect.left_) * components_;
  2007. for (int i = 0; i < destHeight; ++i)
  2008. {
  2009. memcpy(dest, src, (size_t)destWidth * components_);
  2010. src += destWidth * image->components_;
  2011. dest += width_ * components_;
  2012. }
  2013. }
  2014. else
  2015. {
  2016. unsigned char* dest = data_.Get() + (rect.top_ * width_ + rect.left_) * components_;
  2017. for (int y = 0; y < destHeight; ++y)
  2018. {
  2019. for (int x = 0; x < destWidth; ++x)
  2020. {
  2021. // Calculate float coordinates between 0 - 1 for resampling
  2022. const float xF = (image->width_ > 1) ? static_cast<float>(x) / (destWidth - 1) : 0.0f;
  2023. const float yF = (image->height_ > 1) ? static_cast<float>(y) / (destHeight - 1) : 0.0f;
  2024. const unsigned uintColor = image->GetPixelBilinear(xF, yF).ToUInt();
  2025. memcpy(dest, reinterpret_cast<const unsigned char*>(&uintColor), components_);
  2026. dest += components_;
  2027. }
  2028. dest += (width_ - destWidth) * components_;
  2029. }
  2030. }
  2031. return true;
  2032. }
  2033. }