ResLib.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "Common.h"
  2. #include "BFApp.h"
  3. #include "img/PSDReader.h"
  4. #include "gfx/RenderDevice.h"
  5. #include "gfx/Texture.h"
  6. #include "util/PerfTimer.h"
  7. #include "util/TLSingleton.h"
  8. #include "img/JPEGData.h"
  9. #include "img/TGAData.h"
  10. #include "img/PNGData.h"
  11. #include "img/PVRData.h"
  12. #include "img/BFIData.h"
  13. #pragma warning(disable:4190)
  14. USING_NS_BF;
  15. static TLSingleton<String> gResLib_TLStrReturn;
  16. BF_EXPORT PSDReader* BF_CALLTYPE Res_OpenPSD(const char* fileName)
  17. {
  18. //gPerfManager->StartRecording();
  19. PSDReader* aPSDReader = new PSDReader();
  20. if (!aPSDReader->Init(fileName))
  21. {
  22. delete aPSDReader;
  23. return NULL;
  24. }
  25. return aPSDReader;
  26. }
  27. BF_EXPORT void BF_CALLTYPE Res_DeletePSDReader(PSDReader* pSDReader)
  28. {
  29. delete pSDReader;
  30. gPerfManager->StopRecording();
  31. gPerfManager->DbgPrint();
  32. }
  33. BF_EXPORT TextureSegment* BF_CALLTYPE Res_PSD_GetLayerTexture(PSDReader* pSDReader, int layerIdx, int* ofsX, int* ofsY)
  34. {
  35. Texture* texture = pSDReader->LoadLayerTexture(layerIdx, ofsX, ofsY);
  36. if (texture == NULL)
  37. return NULL;
  38. TextureSegment* textureSegment = new TextureSegment();
  39. textureSegment->InitFromTexture(texture);
  40. return textureSegment;
  41. }
  42. BF_EXPORT TextureSegment* BF_CALLTYPE Res_PSD_GetMergedLayerTexture(PSDReader* pSDReader, int* layerIndices, int count, int* ofsX, int* ofsY)
  43. {
  44. std::vector<int> aLayerIndices;
  45. aLayerIndices.insert(aLayerIndices.begin(), layerIndices, layerIndices + count);
  46. Texture* texture = pSDReader->LoadMergedLayerTexture(aLayerIndices, ofsX, ofsY);
  47. if (texture == NULL)
  48. return NULL;
  49. TextureSegment* textureSegment = new TextureSegment();
  50. textureSegment->InitFromTexture(texture);
  51. return textureSegment;
  52. }
  53. BF_EXPORT int BF_CALLTYPE Res_PSD_GetLayerCount(PSDReader* pSDReader)
  54. {
  55. return (int) pSDReader->mPSDLayerInfoVector.size();
  56. }
  57. BF_EXPORT PSDLayerInfo* BF_CALLTYPE Res_PSD_GetLayerInfo(PSDReader* pSDReader, int layerIdx)
  58. {
  59. return pSDReader->mPSDLayerInfoVector[layerIdx];
  60. }
  61. BF_EXPORT void BF_CALLTYPE Res_PSDLayer_GetSize(PSDLayerInfo* layerInfo, int* x, int* y, int* width, int* height)
  62. {
  63. *x = layerInfo->mX;
  64. *y = layerInfo->mY;
  65. *width = layerInfo->mWidth;
  66. *height = layerInfo->mHeight;
  67. }
  68. BF_EXPORT int BF_CALLTYPE Res_PSDLayer_GetLayerId(PSDLayerInfo* layerInfo)
  69. {
  70. return layerInfo->mLayerId;
  71. }
  72. BF_EXPORT const char* BF_CALLTYPE Res_PSDLayer_GetName(PSDLayerInfo* layerInfo)
  73. {
  74. return layerInfo->mName.c_str();
  75. }
  76. BF_EXPORT int BF_CALLTYPE Res_PSDLayer_IsVisible(PSDLayerInfo* layerInfo)
  77. {
  78. return layerInfo->mVisible ? 1 : 0;
  79. }
  80. ///
  81. BF_EXPORT uint32* BF_CALLTYPE Res_LoadImage(char* inFileName, int& width, int& height)
  82. {
  83. String fileName = inFileName;
  84. int dotPos = (int)fileName.LastIndexOf('.');
  85. String ext;
  86. if (dotPos != -1)
  87. ext = fileName.Substring(dotPos);
  88. ImageData* imageData = NULL;
  89. bool handled = false;
  90. bool failed = false;
  91. if (fileName == "!white")
  92. {
  93. imageData = new ImageData();
  94. imageData->CreateNew(1, 1, true);
  95. imageData->mBits[0] = 0xFFFFFFFF;
  96. handled = true;
  97. }
  98. else if (ext == ".tga")
  99. imageData = new TGAData();
  100. else if (ext == ".png")
  101. imageData = new PNGData();
  102. else if (ext == ".jpg")
  103. imageData = new JPEGData();
  104. else if (ext == ".pvr")
  105. imageData = new PVRData();
  106. else
  107. {
  108. return NULL; // Unknown format
  109. }
  110. if (!imageData->LoadFromFile(fileName))
  111. {
  112. imageData->Deref();
  113. return NULL;
  114. }
  115. uint32* bits = imageData->mBits;
  116. imageData->mBits = NULL;
  117. width = imageData->mWidth;
  118. height = imageData->mHeight;
  119. imageData->Deref();
  120. return bits;
  121. }
  122. BF_EXPORT void BF_CALLTYPE Res_FreeImageBits(uint32* bits)
  123. {
  124. delete bits;
  125. }
  126. BF_EXPORT StringView BF_CALLTYPE Res_JPEGCompress(uint32* bits, int width, int height, int quality)
  127. {
  128. String& outString = *gResLib_TLStrReturn.Get();
  129. JPEGData jpegData;
  130. jpegData.mBits = bits;
  131. jpegData.mWidth = width;
  132. jpegData.mHeight = height;
  133. jpegData.Compress(quality);
  134. jpegData.mBits = NULL;
  135. outString.Clear();
  136. outString.Insert(0, (char*)jpegData.mSrcData, jpegData.mSrcDataLen);
  137. return outString;
  138. }
  139. BF_EXPORT bool BF_CALLTYPE Res_WritePNG(uint32* bits, int width, int height, const char* filePath)
  140. {
  141. String& outString = *gResLib_TLStrReturn.Get();
  142. PNGData pngData;
  143. pngData.mBits = bits;
  144. pngData.mWidth = width;
  145. pngData.mHeight = height;
  146. bool result = pngData.WriteToFile(filePath);
  147. pngData.mBits = NULL;
  148. outString.Clear();
  149. outString.Insert(0, (char*)pngData.mSrcData, pngData.mSrcDataLen);
  150. return result;
  151. }