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