Image.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "File.h"
  26. #include "FileSystem.h"
  27. #include "Image.h"
  28. #include "Log.h"
  29. #include "StringUtils.h"
  30. #include <cstring>
  31. #include <ddraw.h>
  32. #include <stb_image.h>
  33. #include <stb_image_write.h>
  34. #include "DebugNew.h"
  35. OBJECTTYPESTATIC(Image);
  36. Image::Image(Context* context) :
  37. Resource(context),
  38. width_(0),
  39. height_(0),
  40. components_(0)
  41. {
  42. }
  43. Image::~Image()
  44. {
  45. }
  46. void Image::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<Image>();
  49. }
  50. bool Image::Load(Deserializer& source)
  51. {
  52. // Check for DDS compressed format
  53. if (source.ReadID() != "DDS ")
  54. {
  55. // Not DDS, use STBImage to load other image formats as uncompressed
  56. source.Seek(0);
  57. int width, height;
  58. unsigned components;
  59. unsigned char* pixelData = GetImageData(source, width, height, components);
  60. if (!pixelData)
  61. {
  62. LOGERROR("Could not load image: " + String(stbi_failure_reason()));
  63. return false;
  64. }
  65. SetSize(width, height, components);
  66. SetData(pixelData);
  67. FreeImageData(pixelData);
  68. }
  69. else
  70. {
  71. // DDS compressed format
  72. DDSURFACEDESC2 ddsd;
  73. source.Read(&ddsd, sizeof(ddsd));
  74. switch (ddsd.ddpfPixelFormat.dwFourCC)
  75. {
  76. case FOURCC_DXT1:
  77. compressedFormat_ = CF_DXT1;
  78. components_ = 3;
  79. break;
  80. case FOURCC_DXT3:
  81. compressedFormat_ = CF_DXT3;
  82. components_ = 4;
  83. break;
  84. case FOURCC_DXT5:
  85. compressedFormat_ = CF_DXT5;
  86. components_ = 4;
  87. break;
  88. default:
  89. LOGERROR("Unsupported DDS format");
  90. return false;
  91. }
  92. unsigned dataSize = source.GetSize() - source.GetPosition();
  93. data_ = new unsigned char[dataSize];
  94. width_ = ddsd.dwWidth;
  95. height_ = ddsd.dwHeight;
  96. numCompressedLevels_ = ddsd.dwMipMapCount;
  97. if (!numCompressedLevels_)
  98. numCompressedLevels_ = 1;
  99. SetMemoryUse(dataSize);
  100. source.Read(data_.GetPtr(), dataSize);
  101. }
  102. return true;
  103. }
  104. void Image::SetSize(unsigned width, unsigned height, unsigned components)
  105. {
  106. if ((width == width_) && (height == height_) && (components == components_))
  107. return;
  108. if ((width <= 0) || (height <= 0))
  109. return;
  110. data_ = new unsigned char[width * height * components];
  111. width_ = width;
  112. height_ = height;
  113. components_ = components;
  114. compressedFormat_ = CF_NONE;
  115. numCompressedLevels_ = 0;
  116. SetMemoryUse(width * height * components);
  117. }
  118. void Image::SetData(const unsigned char* pixelData)
  119. {
  120. memcpy(data_.GetPtr(), pixelData, width_ * height_ * components_);
  121. }
  122. bool Image::SaveBMP(const String& fileName)
  123. {
  124. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  125. if ((fileSystem) && (!fileSystem->CheckAccess(GetPath(fileName))))
  126. {
  127. LOGERROR("Access denied to " + fileName);
  128. return false;
  129. }
  130. if (IsCompressed())
  131. {
  132. LOGERROR("Can not save compressed image to BMP");
  133. return false;
  134. }
  135. if (data_)
  136. return stbi_write_bmp(fileName.CString(), width_, height_, components_, data_.GetPtr()) != 0;
  137. else
  138. return false;
  139. }
  140. bool Image::SaveTGA(const String& fileName)
  141. {
  142. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  143. if ((fileSystem) && (!fileSystem->CheckAccess(GetPath(fileName))))
  144. {
  145. LOGERROR("Access denied to " + fileName);
  146. return false;
  147. }
  148. if (IsCompressed())
  149. {
  150. LOGERROR("Can not save compressed image to TGA");
  151. return false;
  152. }
  153. if (data_)
  154. return stbi_write_tga(fileName.CString(), width_, height_, components_, data_.GetPtr()) != 0;
  155. else
  156. return false;
  157. }
  158. unsigned char* Image::GetImageData(Deserializer& source, int& width, int& height, unsigned& components)
  159. {
  160. unsigned dataSize = source.GetSize();
  161. SharedArrayPtr<unsigned char> buffer(new unsigned char[dataSize]);
  162. source.Read(buffer.GetPtr(), dataSize);
  163. return stbi_load_from_memory(buffer.GetPtr(), dataSize, &width, &height, (int *)&components, 0);
  164. }
  165. void Image::FreeImageData(unsigned char* pixelData)
  166. {
  167. if (!pixelData)
  168. return;
  169. stbi_image_free(pixelData);
  170. }
  171. SharedPtr<Image> Image::GetNextLevel() const
  172. {
  173. if (IsCompressed())
  174. {
  175. LOGERROR("Can not generate mip level from compressed data");
  176. return SharedPtr<Image>();
  177. }
  178. if ((components_ < 1) || (components_ > 4))
  179. {
  180. LOGERROR("Illegal number of image components for mip level generation");
  181. return SharedPtr<Image>();
  182. }
  183. int widthOut = width_ / 2;
  184. int heightOut = height_ / 2;
  185. if (widthOut < 1)
  186. widthOut = 1;
  187. if (heightOut < 1)
  188. heightOut = 1;
  189. SharedPtr<Image> mipImage(new Image(context_));
  190. mipImage->SetSize(widthOut, heightOut, components_);
  191. const unsigned char* pixelDataIn = data_.GetPtr();
  192. unsigned char* pixelDataOut = mipImage->data_.GetPtr();
  193. // 1D case
  194. if ((height_ == 1) || (width_ == 1))
  195. {
  196. // Loop using the larger dimension
  197. if (widthOut < heightOut)
  198. widthOut = heightOut;
  199. switch (components_)
  200. {
  201. case 1:
  202. for (int x = 0; x < widthOut; ++x)
  203. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+1]) >> 1;
  204. break;
  205. case 2:
  206. for (int x = 0; x < widthOut*2; x += 2)
  207. {
  208. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+2]) >> 1;
  209. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+3]) >> 1;
  210. }
  211. break;
  212. case 3:
  213. for (int x = 0; x < widthOut*3; x += 3)
  214. {
  215. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+3]) >> 1;
  216. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+4]) >> 1;
  217. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+5]) >> 1;
  218. }
  219. break;
  220. case 4:
  221. for (int x = 0; x < widthOut*4; x += 4)
  222. {
  223. pixelDataOut[x] = ((unsigned)pixelDataIn[x*2] + pixelDataIn[x*2+4]) >> 1;
  224. pixelDataOut[x+1] = ((unsigned)pixelDataIn[x*2+1] + pixelDataIn[x*2+5]) >> 1;
  225. pixelDataOut[x+2] = ((unsigned)pixelDataIn[x*2+2] + pixelDataIn[x*2+6]) >> 1;
  226. pixelDataOut[x+3] = ((unsigned)pixelDataIn[x*2+3] + pixelDataIn[x*2+7]) >> 1;
  227. }
  228. break;
  229. }
  230. }
  231. // 2D case
  232. else
  233. {
  234. switch (components_)
  235. {
  236. case 1:
  237. for (int y = 0; y < heightOut; ++y)
  238. {
  239. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_];
  240. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_];
  241. unsigned char* out = &pixelDataOut[y*widthOut];
  242. for (int x = 0; x < widthOut; ++x)
  243. {
  244. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+1] + inLower[x*2] + inLower[x*2+1]) >> 2;
  245. }
  246. }
  247. break;
  248. case 2:
  249. for (int y = 0; y < heightOut; ++y)
  250. {
  251. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*2];
  252. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*2];
  253. unsigned char* out = &pixelDataOut[y*widthOut*2];
  254. for (int x = 0; x < widthOut*2; x += 2)
  255. {
  256. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+2] + inLower[x*2] + inLower[x*2+2]) >> 2;
  257. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+3] + inLower[x*2+1] + inLower[x*2+3]) >> 2;
  258. }
  259. }
  260. break;
  261. case 3:
  262. for (int y = 0; y < heightOut; ++y)
  263. {
  264. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*3];
  265. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*3];
  266. unsigned char* out = &pixelDataOut[y*widthOut*3];
  267. for (int x = 0; x < widthOut*3; x += 3)
  268. {
  269. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+3] + inLower[x*2] + inLower[x*2+3]) >> 2;
  270. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+4] + inLower[x*2+1] + inLower[x*2+4]) >> 2;
  271. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+5] + inLower[x*2+2] + inLower[x*2+5]) >> 2;
  272. }
  273. }
  274. break;
  275. case 4:
  276. for (int y = 0; y < heightOut; ++y)
  277. {
  278. const unsigned char* inUpper = &pixelDataIn[(y*2)*width_*4];
  279. const unsigned char* inLower = &pixelDataIn[(y*2+1)*width_*4];
  280. unsigned char* out = &pixelDataOut[y*widthOut*4];
  281. for (int x = 0; x < widthOut*4; x += 4)
  282. {
  283. out[x] = ((unsigned)inUpper[x*2] + inUpper[x*2+4] + inLower[x*2] + inLower[x*2+4]) >> 2;
  284. out[x+1] = ((unsigned)inUpper[x*2+1] + inUpper[x*2+5] + inLower[x*2+1] + inLower[x*2+5]) >> 2;
  285. out[x+2] = ((unsigned)inUpper[x*2+2] + inUpper[x*2+6] + inLower[x*2+2] + inLower[x*2+6]) >> 2;
  286. out[x+3] = ((unsigned)inUpper[x*2+3] + inUpper[x*2+7] + inLower[x*2+3] + inLower[x*2+7]) >> 2;
  287. }
  288. }
  289. break;
  290. }
  291. }
  292. return mipImage;
  293. }
  294. CompressedLevel Image::GetCompressedLevel(unsigned index) const
  295. {
  296. CompressedLevel level;
  297. if (compressedFormat_ == CF_NONE)
  298. {
  299. LOGERROR("Image is not compressed");
  300. return level;
  301. }
  302. if (index >= numCompressedLevels_)
  303. {
  304. LOGERROR("Compressed image mip level out of bounds");
  305. return level;
  306. }
  307. level.width_ = width_;
  308. level.height_ = height_;
  309. level.blockSize_ = (compressedFormat_ == CF_DXT1) ? 8 : 16;
  310. unsigned i = 0;
  311. unsigned offset = 0;
  312. for (;;)
  313. {
  314. if (!level.width_)
  315. level.width_ = 1;
  316. if (!level.height_)
  317. level.height_ = 1;
  318. level.rowSize_ = ((level.width_ + 3) / 4) * level.blockSize_;
  319. level.rows_ = ((level.height_ + 3) / 4);
  320. level.data_ = data_.GetPtr() + offset;
  321. level.dataSize_ = level.rows_ * level.rowSize_;
  322. if (offset + level.dataSize_ > GetMemoryUse())
  323. {
  324. LOGERROR("Compressed level is outside image data. Offset: " + String(offset) + " Size: " + String(level.dataSize_) +
  325. " Datasize: " + String(GetMemoryUse()));
  326. level.data_ = 0;
  327. return level;
  328. }
  329. if (i == index)
  330. return level;
  331. offset += level.dataSize_;
  332. level.width_ /= 2;
  333. level.height_ /= 2;
  334. ++i;
  335. }
  336. }