Image.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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. SDL_Surface* Image::GetSDLSurface(const IntRect& rect) const
  1060. {
  1061. if (!data_)
  1062. return 0;
  1063. if (IsCompressed())
  1064. {
  1065. LOGERROR("Can not get SDL surface from compressed image " + GetName());
  1066. return 0;
  1067. }
  1068. if (components_ < 3)
  1069. {
  1070. LOGERROR("Can not get SDL surface from image " + GetName() + " with less than 3 components");
  1071. return 0;
  1072. }
  1073. IntRect imageRect = rect;
  1074. // Use full image if illegal rect
  1075. if (imageRect.left_ < 0 || imageRect.top_ < 0 || imageRect.right_ > width_ || imageRect.bottom_ > height_ ||
  1076. imageRect.left_ >= imageRect.right_ || imageRect.top_ >= imageRect.bottom_)
  1077. {
  1078. imageRect.left_ = 0;
  1079. imageRect.top_ = 0;
  1080. imageRect.right_ = width_;
  1081. imageRect.bottom_ = height_;
  1082. }
  1083. int imageWidth = width_;
  1084. int width = imageRect.Width();
  1085. int height = imageRect.Height();
  1086. // Assume little-endian for all the supported platforms
  1087. unsigned rMask = 0x000000ff;
  1088. unsigned gMask = 0x0000ff00;
  1089. unsigned bMask = 0x00ff0000;
  1090. unsigned aMask = 0xff000000;
  1091. SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, components_ * 8, rMask, gMask, bMask, aMask);
  1092. if (surface)
  1093. {
  1094. SDL_LockSurface(surface);
  1095. unsigned char* destination = reinterpret_cast<unsigned char*>(surface->pixels);
  1096. unsigned char* source = data_ + components_ * (imageWidth * imageRect.top_ + imageRect.left_);
  1097. for (int i = 0; i < height; ++i)
  1098. {
  1099. memcpy(destination, source, components_ * width);
  1100. destination += surface->pitch;
  1101. source += components_ * imageWidth;
  1102. }
  1103. SDL_UnlockSurface(surface);
  1104. }
  1105. else
  1106. LOGERROR("Failed to create SDL surface from image " + GetName());
  1107. return surface;
  1108. }
  1109. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  1110. {
  1111. unsigned dataSize = source.GetSize();
  1112. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  1113. source.Read(buffer.Get(), dataSize);
  1114. return stbi_load_from_memory(buffer.Get(), dataSize, &width, &height, (int *)&components, 0);
  1115. }
  1116. void Image::FreeImageData(unsigned char* pixelData)
  1117. {
  1118. if (!pixelData)
  1119. return;
  1120. stbi_image_free(pixelData);
  1121. }
  1122. }