-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveDrawing.cpp
More file actions
620 lines (559 loc) · 24.4 KB
/
Copy pathSaveDrawing.cpp
File metadata and controls
620 lines (559 loc) · 24.4 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Daniele Terdina
#include <Windows.h>
#include <GdiPlus.h>
#include <GdiPlusFlat.h>
#include <vector>
#include <assert.h>
#include "MdvDecode.h"
using namespace Gdiplus;
using namespace Gdiplus::DllExports;
using namespace std;
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if (size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for (UINT j = 0; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
void SaveJpeg(HBITMAP hBmp, LPCWSTR lpszFilename, ULONG uQuality)
{
GpBitmap* pBitmap;
GdipCreateBitmapFromHBITMAP(hBmp, NULL, &pBitmap);
CLSID imageCLSID;
GetEncoderClsid(L"image/jpeg", &imageCLSID);
EncoderParameters encoderParams;
encoderParams.Count = 1;
encoderParams.Parameter[0].NumberOfValues = 1;
encoderParams.Parameter[0].Guid = EncoderQuality;
encoderParams.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParams.Parameter[0].Value = &uQuality;
GdipSaveImageToFile(pBitmap, lpszFilename, &imageCLSID, &encoderParams);
}
struct GdiplusInit {
GdiplusInit() {
GdiplusStartupInput inp;
GdiplusStartupOutput outp;
Status result = GdiplusStartup(&token_, &inp, &outp);
int i = 0;
}
~GdiplusInit() {
GdiplusShutdown(token_);
}
private:
ULONG_PTR token_;
};
static GdiplusInit initGraphics;
static void DrawErrorImpl(const vector<int>& fluxData, const vector<int>& alignment,
const vector<int>* bitValues, LPCWSTR path)
{
const int width = 4000;
const int height = 120;
const int waveHeight = 30;
Bitmap bmp(width, height, PixelFormat32bppARGB);
Graphics g(&bmp);
Pen blackPen(Color(255, 0, 0, 0), 3);
Pen lightGreyPen(Color(255, 200, 200, 200), 3);
SolidBrush whiteBrush(Color(255, 255, 255, 255));
SolidBrush bitBrush(Color(255, 0, 96, 200)); // decoded 0/1 labels
size_t start = 0;
int timeStart = 0;
if (!alignment.empty())
{
int time = 0;
while (start < fluxData.size() && time + fluxData[start] < alignment[0])
time += fluxData[start++];
for (int i = 0; i < 4 && start > 0; i++)
time -= fluxData[--start];
timeStart = time;
}
g.FillRectangle(&whiteBrush, 0, 0, width, height);
for (int t : alignment)
{
int x = (t - timeStart) / 6;
g.DrawLine(&lightGreyPen, x, 0, x, height);
}
// Overlay the decoded bit value (0 or 1) inside each cell — the interval
// [alignment[i], alignment[i+1]) is the cell whose result is bitValues[i].
if (bitValues && !bitValues->empty())
{
Gdiplus::Font font(L"Consolas", 14, FontStyleBold, UnitPixel);
size_t nCells = min(alignment.size() > 0 ? alignment.size() - 1 : 0, bitValues->size());
for (size_t i = 0; i < nCells; i++)
{
int x1 = (alignment[i] - timeStart) / 6;
int x2 = (alignment[i + 1] - timeStart) / 6;
int mid = (x1 + x2) / 2;
if (mid < 0 || mid > width) continue;
const WCHAR* s = (*bitValues)[i] ? L"1" : L"0";
g.DrawString(s, -1, &font, PointF((REAL)(mid - 4), 2), &bitBrush);
}
}
const int waveHigh = (height - waveHeight) / 2;
const int waveLow = height - waveHeight;
bool state = true;
int lastX = 0;
// Start `time` at the accumulated sample time of flux[start], not zero:
// the wave loop skips the pre-window flux, and `timeStart` holds where
// that skipped portion ended..
int time = timeStart;
for (size_t i = start; i < fluxData.size(); i++)
{
time += fluxData[i];
g.DrawLine(&blackPen, lastX, waveLow, lastX, waveHigh);
int y = state ? waveHigh : waveLow;
int x = (time - timeStart) / 6;
g.DrawLine(&blackPen, lastX, y, x, y);
if (x > width)
break;
lastX = x;
state = !state;
}
HBITMAP hBitmap;
bmp.GetHBITMAP(Color::Black, &hBitmap);
SaveJpeg(hBitmap, path, 80);
}
void DrawError(const vector<int>& fluxData, const vector<int>& alignment)
{
DrawErrorImpl(fluxData, alignment, nullptr, L"error.jpg");
}
void DrawErrorNamed(const vector<int>& fluxData, const vector<int>& alignment, const char* path)
{
WCHAR wPath[MAX_PATH];
size_t converted = 0;
mbstowcs_s(&converted, wPath, path, _TRUNCATE);
DrawErrorImpl(fluxData, alignment, nullptr, wPath);
}
void DrawErrorNamedBits(const vector<int>& fluxData, const vector<int>& alignment,
const vector<int>& bitValues, const char* path)
{
WCHAR wPath[MAX_PATH];
size_t converted = 0;
mbstowcs_s(&converted, wPath, path, _TRUNCATE);
DrawErrorImpl(fluxData, alignment, &bitValues, wPath);
}
void DrawAllBlocks(const vector<Block>& blocks, FileSystem *pFileSys, int firstBlock, const char* jpgPath, bool verbose)
{
const int width = 11000;
const int height = 450;
const int margin = 30;
const float VERT_SPACE = 1.8;
//Create a bitmap
Bitmap bmp(width, height, PixelFormat32bppARGB);
Graphics g(&bmp);
Pen blackPen(Color(255, 0, 0, 0), 3);
Pen lightGreyPen(Color(255, 200, 200, 200), 3);
Pen greenPen(Color(255, 16, 216, 16), 3);
SolidBrush whiteBrush(Color(255, 255, 255, 255));
SolidBrush blackSemitransparentBrush(Color(128, 0, 0, 0));
SolidBrush lightRedBrush(Color(255, 255, 128, 128));
SolidBrush lightGreenBrush(Color(255, 128, 255, 128));
// Erase background
g.FillRectangle(&whiteBrush, 0, 0, width, height);
g.DrawLine(&lightGreyPen, margin, margin, margin, height - margin);
// Estimate typical rev-to-rev block distance (median of hasNext deltas)
// so we can suppress spurious wraps that occur far too close in time to
// the previous one (e.g. when the physical tape junction sits mid-rev
// and a block just after junction has a lower order than one just
// before, followed shortly by a real rev-boundary block also with lower
// order — two apparent wraps per rev).
int medianRevDelta = 0;
{
vector<int> deltas;
deltas.reserve(64);
for (size_t i = 0; i < blocks.size() && deltas.size() < 64; i++)
if (blocks[i].hasNext)
deltas.push_back(blocks[blocks[i].nextLoopIndex].startTime - blocks[i].startTime);
if (!deltas.empty())
{
std::sort(deltas.begin(), deltas.end());
medianRevDelta = deltas[deltas.size() / 2];
}
}
int minWrapSpacing = medianRevDelta / 2; // must move >=half a rev forward
vector<int> distance;
distance.push_back(0);
int lastOrder = INT_MAX;
int sum = 0;
bool foundSector0 = false;
bool hasDistance = false;
int startTime = INT_MIN;
int dbgIndex = 0;
int lastWrapTime = INT_MIN;
int numWraps = 0;
bool firstIter = true;
for (const Block& b: blocks)
{
if (b.order < lastOrder)
{
bool tooSoon = (!firstIter && lastWrapTime != INT_MIN &&
(int64_t)(b.startTime - lastWrapTime) < minWrapSpacing);
if (tooSoon)
{
// Spurious wrap — a block came in out of monotonic order but
// we're still in the same physical rev. Ignore for row-count
// purposes but keep lastOrder in sync so we can still detect
// the real next boundary.
if (verbose)
printf("wrap suppressed at dbgId=%d (idx=%d): startTime=%d order=%d (prev order=%d) delta=%d < %d\n",
b.dbgId, dbgIndex, b.startTime, b.order, lastOrder,
b.startTime - lastWrapTime, minWrapSpacing);
}
else
{
// If the row we're leaving had no hasNext blocks (nothing
// contributed a distance for its transition), fall back to
// the raw wrap-to-wrap time delta so the distance array
// stays aligned with the wrap count. Without this, pass 2
// would apply the NEXT row's distance at the wrong wrap
// and render subsequent rows off-screen. Reference for the
// delta is the last real wrap, or the very first block if
// this is the first real wrap.
if (!firstIter && !hasDistance)
{
int prevRowStart = (lastWrapTime != INT_MIN) ? lastWrapTime : blocks.begin()->startTime;
int fallback = b.startTime - prevRowStart;
distance.push_back(fallback);
sum += fallback;
// Note: hasDistance stays false — a real hasNext block
// in this row can still push distance[++] on its own.
}
if (!firstIter)
{
numWraps++;
if (verbose)
{
int deltaFromLastWrap = (lastWrapTime == INT_MIN) ? 0 : b.startTime - lastWrapTime;
printf("wrap at dbgId=%d (idx=%d): startTime=%d masterId=%d order=%d (prev order=%d) delta=%d\n",
b.dbgId, dbgIndex, b.startTime, b.masterId, b.order, lastOrder, deltaFromLastWrap);
}
lastWrapTime = b.startTime; // only real wraps update lastWrapTime
}
firstIter = false;
hasDistance = false;
}
}
if (!foundSector0 && b.order == 0)
{
foundSector0 = true;
startTime = b.startTime - b.gapLen - sum;
}
if (!hasDistance && b.hasNext)
{
int d = blocks[b.nextLoopIndex].startTime - b.startTime;
distance.push_back(d);
sum += d;
hasDistance = true;
}
lastOrder = b.order;
dbgIndex++;
}
if (verbose)
printf("Pass 1 collected %zu distance entries for %d wraps (min spacing=%d)\n",
distance.size(), numWraps, minWrapSpacing);
// Pass 2 does distance[++rowId] once per wrap. If distance is short of
// numWraps entries, we'd read past the end and either crash or produce
// odd geometry. Rather than skipping the whole jpg, pad with the median
// of what we have so subsequent rows are just placed at typical spacing.
if ((int)distance.size() < numWraps + 1 && distance.size() > 1)
{
vector<int> sortedD(distance.begin() + 1, distance.end()); // skip the leading 0
std::sort(sortedD.begin(), sortedD.end());
int fill = sortedD.empty() ? 0 : sortedD[sortedD.size() / 2];
while ((int)distance.size() < numWraps + 1)
distance.push_back(fill);
if (verbose)
printf("Padded distance array to %zu entries (median=%d) for jpg rendering\n",
distance.size(), fill);
}
int maxDist = *std::max_element(distance.begin(), distance.end());
double xScale = (double)(width - margin * 2) / maxDist;
int numRows = distance.size();
double yScale = (double)(height - margin * 2) / (1.0 + (numRows - 1) * VERT_SPACE);
lastOrder = INT_MAX;
int rowId = -1;
int lastWrapTimeP2 = INT_MIN;
bool firstIterP2 = true;
Gdiplus::Rect r;
dbgIndex = 0;
for (const Block& b : blocks)
{
if (b.order < lastOrder)
{
// Wrap-suppression: skip if the previous REAL wrap was less than
// half a rev ago (spurious dip from a mid-rev tape junction).
// The very first iteration is the artificial "block 0 < INT_MAX"
// trigger — that one always applies (row 0 setup with distance[0]=0).
bool tooSoon = (!firstIterP2 && lastWrapTimeP2 != INT_MIN &&
(int64_t)(b.startTime - lastWrapTimeP2) < minWrapSpacing);
if (!tooSoon && rowId + 1 < (int)distance.size())
{
startTime += distance[++rowId];
r.Y = (int)(yScale * rowId * VERT_SPACE + 0.5) + margin;
r.Height = (int)(yScale + 0.5);
if (!firstIterP2)
lastWrapTimeP2 = b.startTime;
firstIterP2 = false;
}
}
lastOrder = b.order;
r.X = (int)(xScale * (b.startTime - startTime) + 0.5) + margin;
r.Width = (int)(xScale * (b.endTime - b.startTime) + 0.5);
if (b.hasNext)
{
const Block& b2 = blocks[b.nextLoopIndex];
int xTo = (int)(xScale * ((b2.startTime + b2.endTime) * 0.5 - startTime - distance[rowId + 1]) + 0.5) + margin;
int yTo = (int)(yScale * (rowId + 1) * VERT_SPACE + 0.5) + margin;
g.DrawLine((b.nextType == NT_STRONG) ? &greenPen : &lightGreyPen, r.X + r.Width / 2, r.Y + r.Height, xTo, yTo);
}
//if (pFileSys)
{
//bool isGood = pFileSys->IsGoodBlock(&b.data[0], b.data.size());
//assert(isGood == b.isGood);
SolidBrush* pBrush = (b.isGood && b.mergeQuality != MQ_BAD) ? &lightGreenBrush : &lightRedBrush;
if (!b.isGood || b.mergeQuality == MQ_PERFECT)
{
// Fill entire rectangle (bad sector, or all copies are good and match each other)
g.FillRectangle(pBrush, r);
}
else
{
// Fill triangle
Point triangle[3];
triangle[0].X = r.X;
triangle[0].Y = triangle[1].Y = r.Y + r.Height;
triangle[1].X = triangle[2].X = r.X + r.Width;
triangle[2].Y = r.Y;
g.FillPolygon(pBrush, triangle, 3);
}
if (b.sectorMapType == SMT_MARKED_BAD)
{
// Mark known bad sectors with an X
g.DrawLine(&blackPen, r.X, r.Y, r.X + r.Width, r.Y + r.Height);
g.DrawLine(&blackPen, r.X, r.Y + r.Height, r.X + r.Width, r.Y);
}
else if (b.sectorMapType == SMT_MAP || (b.sectorMapType == SMT_FILE && b.data.size() >= MIN_SECTOR_SIZE))
{
// Mark known used sectors with a dot
REAL x = r.X + r.Width * 0.5f - 4;
REAL y = r.Y + r.Height * 0.5f - 4;
REAL radius = min(r.Width, r.Height) * 0.25f;
g.FillEllipse(&blackSemitransparentBrush, x, y, radius, radius);
}
}
g.DrawRectangle(&blackPen, r);
dbgIndex++;
}
HBITMAP hBitmap;
bmp.GetHBITMAP(Color::Black, &hBitmap);
WCHAR wPath[MAX_PATH];
size_t converted = 0;
mbstowcs_s(&converted, wPath, jpgPath, _TRUNCATE);
SaveJpeg(hBitmap, wPath, 80);
}
// =============================================================================
// Phase 2 diagnostic layout
// =============================================================================
//
// Visualizes blockList right after MakeConnections (hash-based chain
// scaffolding) and BEFORE the Overlaps-based placement pass. Layout:
// Y = startTime / offset (rev index; ~7 rows for a 6.1-rev tape)
// X = startTime % offset (position within one revolution)
//
// Block fill colors reveal scaffolding quality:
// green = block is in a confirmed chain of length >= numLoops-1 (solid scaffold)
// yellow = block is in a shorter chain (2..numLoops-2) (partial)
// red = block is isolated (chain of length 1) (no scaffold)
// pale = block has a hash-match pointer but the connection was not confirmed
//
// Red numbers 1 or 2 inside the blocks mean that the corresponding track has some
// missing magnetic transitions
//
// Lines show the connections:
// solid green = confirmed unambiguous match (NT_STRONG)
// dashed green = confirmed loop=1 match (NT_OTHER)
// thin gray = hash-match, not confirmed
//
// For small blocks that look like sector headers, the sector number (data[1])
// is printed above the block.
//
void DrawPhase2Layout(const vector<Block>& blockList, int offset, int traceFreq, const char* jpgPath, bool verbose)
{
if (blockList.empty() || offset <= 0)
return;
// Estimate number of revs from the maximum startTime
int maxStart = 0;
for (const Block& b : blockList)
if (b.startTime > maxStart) maxStart = b.startTime;
int numRevs = maxStart / offset + 1;
if (numRevs < 1) numRevs = 1;
if (numRevs > 20) numRevs = 20; // sanity cap
// Compute chain length that each block belongs to
vector<int> chainLen(blockList.size(), 1);
vector<bool> isChainHead(blockList.size(), true);
for (size_t i = 0; i < blockList.size(); i++)
if (blockList[i].hasNext && blockList[i].nextLoopIndex >= 0)
isChainHead[blockList[i].nextLoopIndex] = false;
for (size_t i = 0; i < blockList.size(); i++)
{
if (!isChainHead[i]) continue;
int len = 1;
int cur = (int)i;
while (blockList[cur].hasNext && blockList[cur].nextLoopIndex >= 0)
{
cur = blockList[cur].nextLoopIndex;
len++;
}
// Walk again and assign length to every node in this chain
cur = (int)i;
chainLen[cur] = len;
while (blockList[cur].hasNext && blockList[cur].nextLoopIndex >= 0)
{
cur = blockList[cur].nextLoopIndex;
chainLen[cur] = len;
}
}
// Layout constants
const int margin = 40;
const int rowHeight = 60;
const int labelBand = 18; // strip above each row for sector-number labels
const int rowGap = 8;
const int height = margin * 2 + numRevs * (rowHeight + labelBand + rowGap);
const int width = 11000;
const double xScale = (double)(width - margin * 2) / offset;
Bitmap bmp(width, height, PixelFormat32bppARGB);
Graphics g(&bmp);
Pen blackPen(Color(255, 0, 0, 0), 2);
Pen thinGrayPen(Color(255, 180, 180, 180), 1);
Pen strongGreenPen(Color(255, 16, 160, 16), 2);
Pen otherGreenPen(Color(255, 16, 160, 16), 1);
otherGreenPen.SetDashStyle(DashStyleDash);
Pen lightGridPen(Color(255, 220, 220, 220), 1);
SolidBrush whiteBrush(Color(255, 255, 255, 255));
SolidBrush blackBrush(Color(255, 0, 0, 0));
SolidBrush solidGreenBrush(Color(255, 128, 220, 128));
SolidBrush yellowBrush(Color(255, 240, 220, 128));
SolidBrush redBrush(Color(255, 240, 128, 128));
SolidBrush paleBrush(Color(255, 220, 220, 220));
g.FillRectangle(&whiteBrush, 0, 0, width, height);
// Row baselines and labels
Gdiplus::Font labelFont(L"Consolas", 10, FontStyleRegular, UnitPixel);
for (int r = 0; r < numRevs; r++)
{
int y = margin + r * (rowHeight + labelBand + rowGap) + labelBand;
g.DrawLine(&lightGridPen, margin, y, width - margin, y);
WCHAR label[32];
swprintf_s(label, L"rev %d", r);
g.DrawString(label, -1, &labelFont, PointF((REAL)(margin - 34), (REAL)y), &blackBrush);
}
// Consider a chain "solid" if it covers at least (numRevs - 1) blocks —
// allows one dropped revolution.
const int solidLen = numRevs > 1 ? numRevs - 1 : numRevs;
Gdiplus::Font sectorFont(L"Consolas", 12, FontStyleBold, UnitPixel);
Gdiplus::Font gapFont(L"Consolas", 12, FontStyleBold, UnitPixel);
SolidBrush redTextBrush(Color(255, 220, 0, 0));
int drawnSectorLabels = 0;
for (size_t i = 0; i < blockList.size(); i++)
{
const Block& b = blockList[i];
int rev = b.startTime / offset;
if (rev < 0 || rev >= numRevs) continue;
int tInRev = b.startTime % offset;
int rectX = margin + (int)(xScale * tInRev + 0.5);
int rectW = (int)(xScale * (b.endTime - b.startTime) + 0.5);
if (rectW < 1) rectW = 1;
int rowTop = margin + rev * (rowHeight + labelBand + rowGap);
int rectY = rowTop + labelBand;
SolidBrush* pFill;
if (chainLen[i] >= solidLen) pFill = &solidGreenBrush;
else if (chainLen[i] >= 2) pFill = &yellowBrush;
else if (b.nextLoopIndex >= 0 || (i > 0 && blockList[i - 1].nextLoopIndex == (int)i))
pFill = &paleBrush;
else pFill = &redBrush;
g.FillRectangle(pFill, rectX, rectY, rectW, rowHeight);
g.DrawRectangle(&blackPen, rectX, rectY, rectW, rowHeight);
// Per-track dropout marker: red "1" in upper half if track1 has a
// >2×period gap in its raw flux, red "2" in lower half for track2.
// Set at decode time in DecodeBlocks; visible even when the block
// is otherwise good so we can spot borderline chunks.
if (b.track1HasGap)
g.DrawString(L"1", -1, &gapFont, PointF((REAL)(rectX + 6), (REAL)(rectY + 5)), &redTextBrush);
if (b.track2HasGap)
g.DrawString(L"2", -1, &gapFont, PointF((REAL)(rectX + 6), (REAL)(rectY + rowHeight / 2 + 5)), &redTextBrush);
// Label the block with its sector number iff it looks structurally
// like a real sector header:
// QDOS - 16 bytes starting with 0xFF, sector number at byte 1
// OPD - 14 bytes starting with 0xFF, sector number at byte 1
// ZX Spectrum - 15 bytes with byte 0's flag bit set, sector number at byte 1
// The size floor also excludes OPD's 4-byte "block header" (which
// holds fileNum/blockNum, not the sector number) so its byte 1 doesn't
// draw an overlapping bogus label next to the real sector-header one.
bool isQdosSectorHeader = b.data.size() == 16 && b.data[0] == 0xFF;
bool isOpdSectorHeader = b.data.size() == 14 && b.data[0] == 0xFF;
bool isZxSectorHeader = b.data.size() == 15 && (b.data[0] & 1) == 1;
if (isQdosSectorHeader || isOpdSectorHeader || isZxSectorHeader)
{
WCHAR num[8];
swprintf_s(num, L"%d", b.data[1]);
// Leader tick from label to block
g.DrawLine(&thinGrayPen, rectX + rectW / 2, rowTop + labelBand - 2, rectX + rectW / 2, rectY);
g.DrawString(num, -1, §orFont, PointF((REAL)(rectX - 6), (REAL)rowTop), &blackBrush);
drawnSectorLabels++;
}
}
// Connection lines: draw AFTER blocks so they sit on top of fills.
for (size_t i = 0; i < blockList.size(); i++)
{
const Block& b = blockList[i];
if (b.nextLoopIndex < 0 || b.nextLoopIndex >= (int)blockList.size()) continue;
const Block& n = blockList[b.nextLoopIndex];
int rev1 = b.startTime / offset;
int rev2 = n.startTime / offset;
if (rev1 < 0 || rev1 >= numRevs || rev2 < 0 || rev2 >= numRevs) continue;
int tIn1 = b.startTime % offset;
int tIn2 = n.startTime % offset;
int x1 = margin + (int)(xScale * (tIn1 + (b.endTime - b.startTime) / 2) + 0.5);
int y1 = margin + rev1 * (rowHeight + labelBand + rowGap) + labelBand + rowHeight;
int x2 = margin + (int)(xScale * (tIn2 + (n.endTime - n.startTime) / 2) + 0.5);
int y2 = margin + rev2 * (rowHeight + labelBand + rowGap) + labelBand;
Pen* pPen;
if (b.hasNext && b.nextType == NT_STRONG) pPen = &strongGreenPen;
else if (b.hasNext) pPen = &otherGreenPen;
else pPen = &thinGrayPen;
g.DrawLine(pPen, x1, y1, x2, y2);
}
// Legend
Gdiplus::Font legendFont(L"Consolas", 12, FontStyleRegular, UnitPixel);
WCHAR summary[256];
swprintf_s(summary,
L"Phase 2 layout: %d blocks offset=%d samples (%.2fs @ %d MHz) numRevs=%d sectorNums shown for %d blocks",
(int)blockList.size(), offset, (double)offset / traceFreq, traceFreq / 1000000,
numRevs, drawnSectorLabels);
g.DrawString(summary, -1, &legendFont, PointF(margin, 8), &blackBrush);
if (verbose)
printf("DrawPhase2Layout: numRevs=%d bitmap=%dx%d headerLabels=%d\n",
numRevs, width, height, drawnSectorLabels);
HBITMAP hBitmap;
bmp.GetHBITMAP(Color::Black, &hBitmap);
WCHAR wPath[MAX_PATH];
size_t converted = 0;
mbstowcs_s(&converted, wPath, jpgPath, _TRUNCATE);
SaveJpeg(hBitmap, wPath, 80);
}