summaryrefslogtreecommitdiffstats
path: root/Tools/AnvilStats/ImageComposingCallback.cpp
blob: eb43ad49f2ae50775bad6a00a496a462ada46937 (plain) (blame)
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

// ImageComposingCallback.cpp

// Implements the cImageComposingCallback class that implements a subset of cCallback for composing per-region images

#include "Globals.h"
#include "ImageComposingCallback.h"





cImageComposingCallback::cImageComposingCallback(const AString & a_FileNamePrefix) :
	m_FileNamePrefix(a_FileNamePrefix),
	m_CurrentRegionX(INVALID_REGION_COORD),
	m_CurrentRegionZ(INVALID_REGION_COORD),
	m_ImageData(new int[32 * 16 * 32 * 16])
{
}





cImageComposingCallback::~cImageComposingCallback()
{
	delete[] m_ImageData;
}





bool cImageComposingCallback::OnNewRegion(int a_RegionX, int a_RegionZ)
{
	ASSERT(m_CurrentRegionX == INVALID_REGION_COORD);
	ASSERT(m_CurrentRegionZ == INVALID_REGION_COORD);  // Has any previous region been finished properly?
	
	m_CurrentRegionX = a_RegionX;
	m_CurrentRegionZ = a_RegionZ;
	OnEraseImage();
	
	return CALLBACK_CONTINUE;
}





void cImageComposingCallback::OnRegionFinished(int a_RegionX, int a_RegionZ)
{
	ASSERT(m_CurrentRegionX != INVALID_REGION_COORD);
	ASSERT(m_CurrentRegionZ != INVALID_REGION_COORD);  // Has a region been started properly?
	ASSERT(m_CurrentRegionX == a_RegionX);
	ASSERT(m_CurrentRegionZ == a_RegionZ);  // Is it the same region that has been started?
	
	AString FileName = GetFileName(a_RegionX, a_RegionZ);
	if (!FileName.empty())
	{
		OnBeforeImageSaved(a_RegionX, a_RegionZ, FileName);
		SaveImage(FileName);
		OnAfterImageSaved(a_RegionX, a_RegionZ, FileName);
	}
	
	m_CurrentRegionX = INVALID_REGION_COORD;
	m_CurrentRegionZ = INVALID_REGION_COORD;
}





AString cImageComposingCallback::GetFileName(int a_RegionX, int a_RegionZ)
{
	return Printf("%s.%d.%d.bmp", m_FileNamePrefix.c_str(), a_RegionX, a_RegionZ);
}





void cImageComposingCallback::OnEraseImage(void)
{
	// By default erase the image to black:
	EraseImage(0);
}





void cImageComposingCallback::EraseImage(int a_Color)
{
	for (int i = 0; i < PIXEL_COUNT; i++)
	{
		m_ImageData[i] = a_Color;
	}
}





void cImageComposingCallback::EraseChunk(int a_Color, int a_RelChunkX, int a_RelChunkZ)
{
	int Base = a_RelChunkZ * IMAGE_HEIGHT + a_RelChunkX * 16;
	for (int v = 0; v < 16; v++)
	{
		int BaseV = Base + v * IMAGE_HEIGHT;
		for (int u = 0; u < 16; u++)
		{
			m_ImageData[BaseV + u] = a_Color;
		}
	}  // for y
}





void cImageComposingCallback::SetPixel(int a_RelU, int a_RelV, int a_Color)
{
	ASSERT((a_RelU >= 0) && (a_RelU < IMAGE_WIDTH));
	ASSERT((a_RelV >= 0) && (a_RelV < IMAGE_HEIGHT));
	
	m_ImageData[a_RelU + IMAGE_WIDTH * a_RelV] = a_Color;
}





int cImageComposingCallback::GetPixel(int a_RelU, int a_RelV)
{
	if ((a_RelU < 0) || (a_RelU >= IMAGE_WIDTH) || (a_RelV < 0) || (a_RelV >= IMAGE_HEIGHT))
	{
		// Outside the image data
		return -1;
	}
	
	return m_ImageData[a_RelU + IMAGE_WIDTH * a_RelV];
}






void cImageComposingCallback::SetPixelURow(int a_RelUStart, int a_RelV, int a_CountU, int * a_Pixels)
{
	ASSERT((a_RelUStart >= 0) && (a_RelUStart + a_CountU <= IMAGE_WIDTH));
	ASSERT((a_RelV >= 0) && (a_RelV < IMAGE_HEIGHT));
	ASSERT(a_Pixels != NULL);

	int Base = a_RelUStart + a_RelV * IMAGE_WIDTH;
	for (int u = 0; u < a_CountU; u++)
	{
		m_ImageData[Base + u] = a_Pixels[u];
	}
}





int cImageComposingCallback::ShadeColor(int a_Color, int a_Shade)
{
	if (a_Shade < 64)
	{
		return MixColor(0, a_Color, a_Shade * 4);
	}
	return MixColor(a_Color, 0xffffff, (a_Shade - 64) * 4);
}





int cImageComposingCallback::MixColor(int a_Src, int a_Dest, int a_Amount)
{
	int r = a_Src & 0xff;
	int g = (a_Src >> 8) & 0xff;
	int b = (a_Src >> 16) & 0xff;
	int rd = a_Dest & 0xff;
	int gd = (a_Dest >> 8) & 0xff;
	int bd = (a_Dest >> 16) & 0xff;
	int nr = r + (rd - r) * a_Amount / 256;
	int ng = g + (gd - g) * a_Amount / 256;
	int nb = b + (bd - b) * a_Amount / 256;
	return nr | (ng << 8) | (nb << 16);
}




void cImageComposingCallback::SaveImage(const AString & a_FileName)
{
	cFile f(a_FileName, cFile::fmWrite);
	if (!f.IsOpen())
	{
		return;
	}

	// Header for BMP files (is the same for the same-size files)
	static const unsigned char BMPHeader[] =
	{
		0x42, 0x4D, 0x36, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
		0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x13, 0x0B, 0x00, 0x00, 0x13, 0x0B, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	} ;

	f.Write(BMPHeader, sizeof(BMPHeader));
	f.Write(m_ImageData, PIXEL_COUNT * 4);
}