-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZxImage.cpp
316 lines (271 loc) · 7.83 KB
/
ZxImage.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
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
// ZxImage.cpp: implementation of the CZxImage class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "pngconv.h"
#include "ZxImage.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include <sstream>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CZxImage::CZxImage(POINT _size)
: m_nMaskFormat(ZXIMAGE_FORMAT_DEFAULT)
, m_bMaskInvert(false)
, m_ptSize(_size)
, m_nGreyThreshold(128)
, m_nMaskThreshold(128)
, m_imgByte(_size.y)
, m_imgMask(_size.y)
, m_bTextOutput(false)
, m_bReverse(false)
, m_bUpsideDown(false)
, m_bZigZag(false)
, m_bUseRGBMask(false)
, m_bByteInvert(false)
, m_bHexOutput(false)
{
// correct the X size so it's a multiple of 8
if (m_ptSize.x % 8)
{
m_ptSize.x += 8 - (m_ptSize.x % 8);
}
// set the x size so it's actually a bitmap size rather than pixel...
// basically, divide by 8
m_ptSize.x /= 8;
for (unsigned int i = 0; i < m_ptSize.y; ++i)
{
m_imgByte[i].resize(m_ptSize.x);
m_imgMask[i].resize(m_ptSize.x);
}
}
CZxImage::~CZxImage()
{
}
CZxImage::ProcessRGBAImage(CRGBAImage &_rgba)
{
// for now we'll iterate through our image,
// and get the associated pixels from the _rgba image.
for (int y_idx = 0; y_idx < m_ptSize.y; ++y_idx)
{
for (int x_idx = 0; x_idx < m_ptSize.x; ++x_idx)
{
// need to set each bit...
ucBYTE bitmap = 0x80;
for (int b_idx = 0; b_idx < 8; ++b_idx)
{
bool byteon = false;
bool maskon = true;
try
{
byteon = _rgba(y_idx, (8*x_idx) + b_idx).PixelIsOn(m_nGreyThreshold);
if (!m_bUseRGBMask)
maskon = _rgba(y_idx, (8*x_idx) + b_idx).PixelIsMasked(m_nMaskThreshold);
else
{
maskon = _rgba(y_idx, (8*x_idx) + b_idx).PixelIsMasked(m_rgbMaskColour);
if (maskon)
byteon = false;
}
}
catch (CRGBAImage::CExBoundsViolation)
{
}
if (byteon)
m_imgByte[y_idx][x_idx] |= bitmap;
if (maskon)
m_imgMask[y_idx][x_idx] |= bitmap;
bitmap >>= 1;
}
if (m_bMaskInvert)
m_imgMask[y_idx][x_idx] = 255 - m_imgMask[y_idx][x_idx];
if (m_bByteInvert)
m_imgByte[y_idx][x_idx] = 255 - m_imgByte[y_idx][x_idx];
}
}
}
void CZxImage::SetLeadText(std::string _ss)
{
m_sLeadText = _ss;
// if the string is not empty we may need to add a space to it... unless it's a tab or space already
if (!m_sLeadText.empty())
{
// first up, ensure the "\t" is converted to a tab character...
int pos = 0;
while (std::string::npos != (pos = m_sLeadText.find("\\t", pos)))
{
m_sLeadText = m_sLeadText.substr(0, pos) + std::string("\t") + m_sLeadText.substr(pos + 2);
}
char theval = m_sLeadText[m_sLeadText.size() - 1];
if (theval != ' ' && theval != '\t')
m_sLeadText += " ";
}
}
std::ostream& txtoutput(std::ostream& ost, const CZxImage& zxi)
{
// This method outputs the required manipulators for text output.
if (zxi.m_bHexOutput)
ost << zxi.m_sLeadTextByte << std::ios::hex;
return ost;
}
std::ostream& txtoutput(std::ostream& ost, const CZxImage& zxi, int i)
{
// This method outputs the required manipulators for text output.
if (zxi.m_bHexOutput)
{
ost << zxi.m_sLeadTextByte;
ost.width(2);
ost.fill('0');
ost.setf(std::ios_base::hex, std::ios_base::basefield);
ost << i;
}
else
ost << i;
return ost;
}
std::ostream& operator<< (std::ostream& ost, const CZxImage& zxi)
{
// this is the operator that actually streams the contents out according to various set options.
// it's a friend of the CZxImage class so it can get to all the private/protected stuff.
// it means that externally we can just do something like:
// std::cout << myZxImage;
// which is nice.
// for the moment we only need to care about the following options:
// m_nMaskFormat;
// ZXIMAGE_FORMAT_B = 0, // no mask
// ZXIMAGE_FORMAT_M = 1, // no byte
// ZXIMAGE_FORMAT_MB = 2, // alternate mask then byte
// ZXIMAGE_FORMAT_BM = 3, // alternate byte then mask
// ZXIMAGE_FORMAT_MMBB = 4, // alternate lines of mask then byte
// ZXIMAGE_FORMAT_BBMM = 5 // alternate lines of byte then mask
// m_bTextOutput;
ZXIMAGEFORMAT zxf = zxi.m_nMaskFormat;
int y_init = 0;
int y_delta = 1;
// correct y index for upside down output
if (zxi.m_bUpsideDown)
{
y_init = zxi.m_ptSize.y - 1;
y_delta = -1;
}
int y_idx = y_init;
int y_count = zxi.m_ptSize.y;
int y_line = 0;
// we need to know which line we're outputting, in case we're in zigzag mode
for (y_line = 0; y_line < y_count; ++y_line)
{
// so, we have a line, what do we do with it?
// these are used in the for loops
int x_count = zxi.m_ptSize.x;
int x_line = 0;
bool breverse = false;
// first, work out which direction we're moving in...
if (zxi.m_bReverse)
{
breverse = !breverse;
}
if (zxi.m_bZigZag && (y_line % 2))
{
breverse = !breverse;
}
// set up the parameters for stepping through the X axis
int x_init = 0;
int x_delta = 1;
// correct for reversing the direction
if (breverse)
{
x_init = zxi.m_ptSize.x - 1;
x_delta = -1;
}
int x_idx = x_init;
if (zxi.m_bTextOutput) ost << zxi.m_sLeadText;
for (x_line = 0; x_line < x_count; ++x_line)
{
switch (zxf)
{
case ZXIMAGE_FORMAT_B:
case ZXIMAGE_FORMAT_BBMM:
if (zxi.m_bTextOutput)
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgByte[y_idx][x_idx]));
else
ost << zxi.m_imgByte[y_idx][x_idx];
break;
case ZXIMAGE_FORMAT_M:
case ZXIMAGE_FORMAT_MMBB:
if (zxi.m_bTextOutput)
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgMask[y_idx][x_idx]));
else
ost << zxi.m_imgMask[y_idx][x_idx];
break;
case ZXIMAGE_FORMAT_BM:
if (zxi.m_bTextOutput)
{
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgByte[y_idx][x_idx]));
ost << ",";
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgMask[y_idx][x_idx]));
}
else
ost << zxi.m_imgByte[y_idx][x_idx] << zxi.m_imgMask[y_idx][x_idx];
break;
case ZXIMAGE_FORMAT_MB:
if (zxi.m_bTextOutput)
{
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgMask[y_idx][x_idx]));
ost << ",";
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgByte[y_idx][x_idx]));
}
else
ost << zxi.m_imgMask[y_idx][x_idx] << zxi.m_imgByte[y_idx][x_idx];
break;
default:
{
// whoops!
std::ostringstream except;
except << zxf;
throw CZxImage::CExUnknownMask(except.str());
}
break;
}
if (zxi.m_bTextOutput && (x_line + 1 < x_count)) ost << ",";
x_idx += x_delta;
}
if (zxi.m_bTextOutput) ost << std::endl;
// now some clean-up in case we were doing a line-by-line thing...
// we need to go through and print out the other line.
x_idx = x_init;
if (ZXIMAGE_FORMAT_BBMM == zxf || ZXIMAGE_FORMAT_MMBB == zxf)
{
x_idx = x_init;
if (zxi.m_bTextOutput) ost << zxi.m_sLeadText;
for (x_line = 0; x_line < x_count; ++x_line)
{
switch (zxf)
{
case ZXIMAGE_FORMAT_BBMM:
if (zxi.m_bTextOutput)
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgMask[y_idx][x_idx]));
else
ost << zxi.m_imgMask[y_idx][x_idx];
break;
case ZXIMAGE_FORMAT_MMBB:
if (zxi.m_bTextOutput)
txtoutput(ost, zxi, static_cast<int>(zxi.m_imgByte[y_idx][x_idx]));
else
ost << zxi.m_imgByte[y_idx][x_idx];
break;
default:
break;
}
if (zxi.m_bTextOutput && (x_line + 1 < x_count)) ost << ",";
x_idx += x_delta;
}
if (zxi.m_bTextOutput) ost << std::endl;
}
y_idx += y_delta;
}
return ost;
}