aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/BufferedImage.cpp
blob: 8777d307bd2f50b7a34adab8bcc522c8934fdada (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "stdafx.h"
#include "..\Minecraft.World\StringHelpers.h"
#include "Textures.h"
#include "..\Minecraft.World\ArrayWithLength.h"
#include "BufferedImage.h"

#ifdef _XBOX
typedef struct
{
	unsigned int   filesz;
	unsigned short creator1;
	unsigned short creator2;
	unsigned int	 bmp_offset;
	unsigned int	 header_sz;
	unsigned int	 width;
	unsigned int	 height;
	unsigned short nplanes;
	unsigned short bitspp;
	unsigned int	 compress_type;
	unsigned int	 bmp_bytesz;
	int			 hres;
	int			 vres;
	unsigned int	 ncolors;
	unsigned int	 nimpcolors;
} BITMAPINFOHEADER;
#endif

BufferedImage::BufferedImage(int width,int height,int type)
{
	data[0] = new int[width*height];

	for( int i = 1 ; i < 10; i++ )
	{
		data[i] = nullptr;
	}
	this->width = width;
	this->height = height;
}

void BufferedImage::ByteFlip4(unsigned int &data)
{
	data = ( data >> 24 ) |
		( ( data >> 8 ) & 0x0000ff00 ) |
		( ( data << 8 ) & 0x00ff0000 ) |
		( data << 24 );
}
// Loads a bitmap into a buffered image - only currently supports the 2 types of 32-bit image that we've made so far
// and determines which of these is which by the compression method. Compression method 3 is a 32-bit image with only
// 24-bits used (ie no alpha channel) whereas method 0 is a full 32-bit image with a valid alpha channel.
BufferedImage::BufferedImage(const wstring& File, bool filenameHasExtension /*=false*/, bool bTitleUpdateTexture /*=false*/, const wstring &drive /*=L""*/)
{
	HRESULT hr;
	wstring wDrive;
	wstring filePath;
	filePath = File;

	wDrive = drive;
	if(wDrive.empty())
	{
#ifdef _XBOX
		if(bTitleUpdateTexture)
		{
			// Make the content package point to to the UPDATE: drive is needed
#ifdef _TU_BUILD
			wDrive=L"UPDATE:\\";
#else

			wDrive=L"GAME:\\res\\TitleUpdate\\";
#endif
		}
		else
		{
			wDrive=L"GAME:\\";
		}
#else

#ifdef __PS3__

		char *pchUsrDir;
		if(app.GetBootedFromDiscPatch())
		{
			const char *pchTextureName=wstringtofilename(File);
			pchUsrDir = app.GetBDUsrDirPath(pchTextureName);
		}
		else
		{
			pchUsrDir=getUsrDirPath();
		}

		wstring wstr (pchUsrDir, pchUsrDir+strlen(pchUsrDir));

		if(bTitleUpdateTexture)
		{
			// Make the content package point to to the UPDATE: drive is needed
			wDrive= wstr + L"\\Common\\res\\TitleUpdate\\";
		}
		else
		{
			wDrive= wstr + L"/Common/";
		}
#elif __PSVITA__

		/*char *pchUsrDir=getUsrDirPath();

		wstring wstr (pchUsrDir, pchUsrDir+strlen(pchUsrDir));

		if(bTitleUpdateTexture)
		{
			// Make the content package point to to the UPDATE: drive is needed
			wDrive= wstr + L"\\Common\\res\\TitleUpdate\\";
		}
		else
		{
			wDrive= wstr + L"/Common/";
		}*/

		if(bTitleUpdateTexture)
		{
			// Make the content package point to to the UPDATE: drive is needed
			wDrive= L"Common\\res\\TitleUpdate\\";
		}
		else
		{
			wDrive= L"Common/";
		}
#else
		if(bTitleUpdateTexture)
		{
			// Make the content package point to to the UPDATE: drive is needed
			wDrive= L"Common\\res\\TitleUpdate\\";
		}
		else
		{
			wDrive= L"Common/";
		}
#endif

#endif
	}

	for( int l = 0 ; l < 10; l++ )
	{
		data[l] = nullptr;
	}

	for( int l = 0; l < 10; l++ )
	{
		wstring name;
		wstring mipMapPath = L"";
		if( l != 0 )
		{
			mipMapPath = L"MipMapLevel" + std::to_wstring(l+1);
		}
		if( filenameHasExtension )
		{
			name = wDrive + L"res" + filePath.substr(0,filePath.length());
		}
		else
		{
			name = wDrive + L"res" + filePath.substr(0,filePath.length()-4) + mipMapPath + L".png";
		}

		const char *pchTextureName=wstringtofilename(name);

#ifndef _CONTENT_PACKAGE
		app.DebugPrintf("\n--- Loading TEXTURE - %s\n\n",pchTextureName);
#endif

		D3DXIMAGE_INFO ImageInfo;
		ZeroMemory(&ImageInfo,sizeof(D3DXIMAGE_INFO));
		hr=RenderManager.LoadTextureData(pchTextureName,&ImageInfo,&data[l]);


		if(hr!=ERROR_SUCCESS)
		{
			// 4J - If we haven't loaded the non-mipmap version then exit the game
			if( l == 0 )
			{
				app.FatalLoadError();
			}
			return;
		}

		if( l == 0 )
		{
			width=ImageInfo.Width;
			height=ImageInfo.Height;
		}
	}
}

BufferedImage::BufferedImage(DLCPack *dlcPack, const wstring& File, bool filenameHasExtension /*= false*/ )
{
	HRESULT hr;
	wstring filePath = File;
	BYTE *pbData = nullptr;
	DWORD dwBytes = 0;

	for( int l = 0 ; l < 10; l++ )
	{
		data[l] = nullptr;
	}

	for( int l = 0; l < 10; l++ )
	{
		wstring name;
		wstring mipMapPath = L"";
		if( l != 0 )
		{
			mipMapPath = L"MipMapLevel" + std::to_wstring(l+1);
		}
		if( filenameHasExtension )
		{
			name = L"res" + filePath.substr(0,filePath.length());
		}
		else
		{
			name = L"res" + filePath.substr(0,filePath.length()-4) + mipMapPath + L".png";
		}

		if(!dlcPack->doesPackContainFile(DLCManager::e_DLCType_All, name))
		{
			// 4J - If we haven't loaded the non-mipmap version then exit the game
			if( l == 0 )
			{
				app.FatalLoadError();
			}
			return;
		}

		DLCFile *dlcFile = dlcPack->getFile(DLCManager::e_DLCType_All, name);
		pbData = dlcFile->getData(dwBytes);
		if(pbData == nullptr || dwBytes == 0)
		{
			// 4J - If we haven't loaded the non-mipmap version then exit the game
			if( l == 0 )
			{
				app.FatalLoadError();
			}
			return;
		}

		D3DXIMAGE_INFO ImageInfo;
		ZeroMemory(&ImageInfo,sizeof(D3DXIMAGE_INFO));
		hr=RenderManager.LoadTextureData(pbData,dwBytes,&ImageInfo,&data[l]);


		if(hr!=ERROR_SUCCESS)
		{
			// 4J - If we haven't loaded the non-mipmap version then exit the game
			if( l == 0 )
			{
				app.FatalLoadError();
			}
			return;
		}

		if( l == 0 )
		{
			width=ImageInfo.Width;
			height=ImageInfo.Height;
		}
	}
}


BufferedImage::BufferedImage(BYTE *pbData, DWORD dwBytes)
{
	int iCurrentByte=0;
	for( int l = 0 ; l < 10; l++ )
	{
		data[l] = nullptr;
	}

	D3DXIMAGE_INFO ImageInfo;
	ZeroMemory(&ImageInfo,sizeof(D3DXIMAGE_INFO));
	HRESULT hr=RenderManager.LoadTextureData(pbData,dwBytes,&ImageInfo,&data[0]);

	if(hr==ERROR_SUCCESS)
	{
		width=ImageInfo.Width;
		height=ImageInfo.Height;
	}
	else
	{
		app.FatalLoadError();
	}
}

BufferedImage::~BufferedImage()
{
	for(int i  = 0; i < 10; i++ )
	{
		delete[] data[i];
	}
}

int BufferedImage::getWidth()
{
	return width;
}

int BufferedImage::getHeight()
{
	return height;
}

void BufferedImage::getRGB(int startX, int startY, int w, int h, intArray out,int offset,int scansize, int level)
{
	int ww = width >> level;
	for( int y = 0; y < h; y++ )
	{
		for( int x = 0; x < w; x++ )
		{
			out[ y * scansize + offset + x] = data[level][ startX + x + ww * ( startY + y ) ];
		}
	}
}

int *BufferedImage::getData()
{
	return data[0];
}

int *BufferedImage::getData(int level)
{
	return data[level];
}

Graphics *BufferedImage::getGraphics()
{
	return nullptr;
}

//Returns the transparency. Returns either OPAQUE, BITMASK, or TRANSLUCENT.
//Specified by:
//getTransparency in interface Transparency
//Returns:
//the transparency of this BufferedImage.
int BufferedImage::getTransparency()
{
	// TODO - 4J Implement?
	return 0;
}

//Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.
//Parameters:
//x, y - the coordinates of the upper-left corner of the specified rectangular region
//w - the width of the specified rectangular region
//h - the height of the specified rectangular region
//Returns:
//a BufferedImage that is the subimage of this BufferedImage.
BufferedImage *BufferedImage::getSubimage(int x ,int y, int w, int h)
{
	// TODO - 4J Implement

	BufferedImage *img = new BufferedImage(w,h,0);
	intArray arrayWrapper(img->data[0], w*h);
	this->getRGB(x, y, w, h, arrayWrapper,0,w);

	int level = 1;
	while(getData(level) != nullptr)
	{
		int ww = w >> level;
		int hh = h >> level;
		int xx = x >> level;
		int yy = y >> level;
		img->data[level] = new int[ww*hh];
		intArray arrayWrapper(img->data[level], ww*hh);
		this->getRGB(xx, yy, ww, hh, arrayWrapper,0,ww,level);

		++level;
	}

	return img;
}


void BufferedImage::preMultiplyAlpha()
{
	int *curData = data[0];

	int cur = 0;
	int alpha = 0;
	int r = 0;
	int g = 0;
	int b = 0;

	int total = width * height;
	for(unsigned int i = 0; i < total; ++i)
	{
		cur = curData[i];
		alpha = (cur >> 24) & 0xff;
		r = ((cur >> 16) & 0xff) * static_cast<float>(alpha)/255;
		g = ((cur >> 8) & 0xff) * static_cast<float>(alpha)/255;
		b = (cur & 0xff) * static_cast<float>(alpha)/255;

		curData[i] = (r << 16) | (g << 8) | (b ) | (alpha << 24);
	}
}