Image.cpp 41 KB

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