-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegion.cpp
More file actions
735 lines (631 loc) · 20.1 KB
/
Copy pathRegion.cpp
File metadata and controls
735 lines (631 loc) · 20.1 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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#include <windows.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#define PRINTF
//#define PRINTF printf
// ランデータの安全な追加
inline void SerializeRun(std::vector<WORD>& out, WORD start, WORD length)
{
PRINTF("Run: start=%d, length=%d\n", start, length);
out.push_back(start);
out.push_back(length);
}
BOOL SerializeRegion(std::vector<WORD>& out, HRGN hRgn)
{
RECT rc;
if (GetRgnBox(hRgn, &rc) == NULLREGION)
return FALSE;
INT x = rc.left, y = rc.top;
INT cx = rc.right - rc.left, cy = rc.bottom - rc.top;
if (cx <= 0 || cy <= 0 || cx > 300 || cy > 300)
return FALSE;
if (x < 0 || y < 0 || x >= 300 || y >= 300)
return FALSE;
BOOL wide = (cx >= cy);
BYTE signature = wide ? 'H' : 'V';
// ヘッダー情報
out.push_back((WORD)signature);
out.push_back((WORD)x);
out.push_back((WORD)y);
out.push_back((WORD)cx);
out.push_back((WORD)cy);
if (wide)
{
// 水平スキャン:各行を処理
for (INT y0 = y; y0 < y + cy; ++y0)
{
INT runStart = -1; // 現在のランの開始位置
for (INT x0 = x; x0 < x + cx; ++x0)
{
BOOL inside = PtInRegion(hRgn, x0, y0);
if (inside)
{
if (runStart == -1) // 新しいランの開始
{
runStart = x0;
}
}
else
{
if (runStart != -1) // ランの終了
{
SerializeRun(out, runStart - x, x0 - runStart);
runStart = -1;
}
}
}
// 行の終端でランが続いている場合
if (runStart != -1)
{
SerializeRun(out, runStart - x, (x + cx) - runStart);
}
// 行の終わりマーカー
out.push_back(0xFFFF);
}
}
else
{
// 垂直スキャン:各列を処理
for (INT x0 = x; x0 < x + cx; ++x0)
{
INT runStart = -1; // 現在のランの開始位置
for (INT y0 = y; y0 < y + cy; ++y0)
{
BOOL inside = PtInRegion(hRgn, x0, y0);
if (inside)
{
if (runStart == -1) // 新しいランの開始
{
runStart = y0;
}
}
else
{
if (runStart != -1) // ランの終了
{
SerializeRun(out, runStart - y, y0 - runStart);
runStart = -1;
}
}
}
// 列の終端でランが続いている場合
if (runStart != -1)
{
SerializeRun(out, runStart - y, (y + cy) - runStart);
}
// 列の終わりマーカー
out.push_back(0xFFFF);
}
}
return TRUE;
}
HRGN DeserializeRegion(const WORD *pw, size_t data_size)
{
if (data_size < 5)
{
PRINTF("Error: Insufficient data data_size\n");
return NULL;
}
WORD signature = pw[0];
BOOL isHorizontal = (signature == 'H');
size_t offset = 1;
if (signature != 'H' && signature != 'V')
{
PRINTF("Error: Invalid signature\n");
return NULL;
}
// 座標とサイズの読み取り
INT x = pw[offset], y = pw[offset + 1];
INT cx = pw[offset + 2], cy = pw[offset + 3];
offset += 4;
PRINTF("Deserializing: signature=%c, bounds=(%d,%d,%dx%d)\n",
signature, x, y, cx, cy);
if (cx <= 0 || cy <= 0 || cx > 300 || cy > 300)
{
PRINTF("Error: Invalid region size\n");
return NULL;
}
if (x < 0 || y < 0 || x >= 300 || y >= 300)
{
PRINTF("Error: Invalid region\n");
return NULL;
}
// RGNDATA構造体の準備
RGNDATAHEADER header = { sizeof(header) };
header.iType = RDH_RECTANGLES;
SetRect(&header.rcBound, x, y, x + cx, y + cy);
std::vector<RECT> rects;
if (isHorizontal)
{
INT yCurrent = y;
while (offset < data_size && yCurrent < y + cy)
{
// 各行のランデータを処理
while (offset + 1 < data_size)
{
WORD value = pw[offset];
// 行終了マーカーのチェック
if (value == 0xFFFF)
{
offset++;
break;
}
// ランの開始位置とレングスを取得
if (offset + 1 >= data_size)
{
PRINTF("Error: Unexpected end of data\n");
return NULL;
}
DWORD runStart = pw[offset];
DWORD runLength = pw[offset + 1];
offset += 2;
PRINTF("H-Run: start=%d, length=%d at y=%d\n", runStart, runLength, yCurrent);
// 境界チェック
if (runStart + runLength > (DWORD)cx)
{
PRINTF("Warning: Run exceeds horizontal bounds\n");
runLength = cx - runStart;
}
if (runLength > 0)
{
RECT r = {
x + (INT)runStart,
yCurrent,
x + (INT)runStart + (INT)runLength,
yCurrent + 1
};
rects.push_back(r);
}
}
yCurrent++;
}
}
else
{
INT xCurrent = x;
while (offset < data_size && xCurrent < x + cx)
{
// 各列のランデータを処理
while (offset + 1 < data_size)
{
WORD value = pw[offset];
// 列終了マーカーのチェック
if (value == 0xFFFF)
{
offset++;
break;
}
// ランの開始位置とレングスを取得
if (offset + 1 >= data_size)
{
PRINTF("Error: Unexpected end of data\n");
return NULL;
}
DWORD runStart = pw[offset];
DWORD runLength = pw[offset + 1];
offset += 2;
PRINTF("V-Run: start=%d, length=%d at x=%d\n", runStart, runLength, xCurrent);
// 境界チェック
if (runStart + runLength > (DWORD)cy)
{
PRINTF("Warning: Run exceeds vertical bounds\n");
runLength = cy - runStart;
}
if (runLength > 0)
{
RECT r = {
xCurrent,
y + (INT)runStart,
xCurrent + 1,
y + (INT)runStart + (INT)runLength
};
rects.push_back(r);
}
}
xCurrent++;
}
}
if (rects.empty())
{
PRINTF("Warning: No rectangles generated\n");
return NULL;
}
// RECTを元にリージョンを作成
header.nCount = (DWORD)rects.size();
size_t dataSize = sizeof(RGNDATAHEADER) + rects.size() * sizeof(RECT);
RGNDATA* rgndata = (RGNDATA*)malloc(dataSize);
if (!rgndata)
{
PRINTF("Error: Memory allocation failed\n");
return NULL;
}
memcpy(&rgndata->rdh, &header, sizeof(RGNDATAHEADER));
memcpy(rgndata->Buffer, &rects[0], rects.size() * sizeof(RECT));
HRGN hRgn = ExtCreateRegion(NULL, (DWORD)dataSize, rgndata);
free(rgndata);
PRINTF("Created region with %u rectangles\n", (UINT)rects.size());
return hRgn;
}
// ランデータの安全な追加 (254版)
inline void SerializeRun254(std::vector<BYTE>& out, BYTE start, BYTE length)
{
PRINTF("Run: start=%d, length=%d\n", start, length);
out.push_back(start);
out.push_back(length);
}
BOOL SerializeRegion254(std::vector<BYTE>& out, HRGN hRgn)
{
RECT rc;
if (GetRgnBox(hRgn, &rc) == NULLREGION)
return FALSE;
INT x = rc.left, y = rc.top;
INT cx = rc.right - rc.left, cy = rc.bottom - rc.top;
if (cx <= 0 || cy <= 0 || cx > 254 || cy > 254)
return FALSE;
if (x < 0 || y < 0 || x >= 254 || y >= 254)
return FALSE;
BOOL wide = (cx >= cy);
BYTE signature = wide ? 'h' : 'v';
// ヘッダー情報
out.push_back((BYTE)signature);
out.push_back((BYTE)x);
out.push_back((BYTE)y);
out.push_back((BYTE)cx);
out.push_back((BYTE)cy);
if (wide)
{
// 水平スキャン:各行を処理
for (INT y0 = y; y0 < y + cy; ++y0)
{
INT runStart = -1; // 現在のランの開始位置
for (INT x0 = x; x0 < x + cx; ++x0)
{
BOOL inside = PtInRegion(hRgn, x0, y0);
if (inside)
{
if (runStart == -1) // 新しいランの開始
{
runStart = x0;
}
}
else
{
if (runStart != -1) // ランの終了
{
SerializeRun254(out, runStart - x, x0 - runStart);
runStart = -1;
}
}
}
// 行の終端でランが続いている場合
if (runStart != -1)
{
SerializeRun254(out, runStart - x, (x + cx) - runStart);
}
// 行の終わりマーカー
out.push_back(0xFF);
}
}
else
{
// 垂直スキャン:各列を処理
for (INT x0 = x; x0 < x + cx; ++x0)
{
INT runStart = -1; // 現在のランの開始位置
for (INT y0 = y; y0 < y + cy; ++y0)
{
BOOL inside = PtInRegion(hRgn, x0, y0);
if (inside)
{
if (runStart == -1) // 新しいランの開始
{
runStart = y0;
}
}
else
{
if (runStart != -1) // ランの終了
{
SerializeRun254(out, runStart - y, y0 - runStart);
runStart = -1;
}
}
}
// 列の終端でランが続いている場合
if (runStart != -1)
{
SerializeRun254(out, runStart - y, (y + cy) - runStart);
}
// 列の終わりマーカー
out.push_back(0xFF);
}
}
return TRUE;
}
HRGN DeserializeRegion254(const BYTE *pb, size_t data_size)
{
if (data_size < 5)
{
PRINTF("Error: Insufficient data size\n");
return NULL;
}
BYTE signature = pb[0];
BOOL isHorizontal = (signature == 'h');
size_t offset = 1;
if (signature != 'h' && signature != 'v')
{
PRINTF("Error: Invalid signature\n");
return NULL;
}
// 座標とサイズの読み取り
INT x = pb[offset], y = pb[offset + 1];
INT cx = pb[offset + 2], cy = pb[offset + 3];
offset += 4;
PRINTF("Deserializing: signature=%c, bounds=(%d,%d,%dx%d)\n",
signature, x, y, cx, cy);
if (cx <= 0 || cy <= 0 || cx > 254 || cy > 254)
{
PRINTF("Error: Invalid region size\n");
return NULL;
}
if (x < 0 || y < 0 || x >= 254 || y >= 254)
{
PRINTF("Error: Invalid region\n");
return NULL;
}
// RGNDATA構造体の準備
RGNDATAHEADER header = { sizeof(header) };
header.iType = RDH_RECTANGLES;
SetRect(&header.rcBound, x, y, x + cx, y + cy);
std::vector<RECT> rects;
if (isHorizontal)
{
INT yCurrent = y;
while (offset < data_size && yCurrent < y + cy)
{
// 各行のランデータを処理
while (offset + 1 < data_size)
{
BYTE value = pb[offset];
// 行終了マーカーのチェック
if (value == 0xFF)
{
offset++;
break;
}
// ランの開始位置とレングスを取得
if (offset + 1 >= data_size)
{
PRINTF("Error: Unexpected end of data\n");
return NULL;
}
DWORD runStart = pb[offset];
DWORD runLength = pb[offset + 1];
offset += 2;
PRINTF("H-Run: start=%d, length=%d at y=%d\n", runStart, runLength, yCurrent);
// 境界チェック
if (runStart + runLength > (DWORD)cx)
{
PRINTF("Warning: Run exceeds horizontal bounds\n");
runLength = cx - runStart;
}
if (runLength > 0)
{
RECT r = {
x + (INT)runStart,
yCurrent,
x + (INT)runStart + (INT)runLength,
yCurrent + 1
};
rects.push_back(r);
}
}
yCurrent++;
}
}
else
{
INT xCurrent = x;
while (offset < data_size && xCurrent < x + cx)
{
// 各列のランデータを処理
while (offset + 1 < data_size)
{
BYTE value = pb[offset];
// 列終了マーカーのチェック
if (value == 0xFF)
{
offset++;
break;
}
// ランの開始位置とレングスを取得
if (offset + 1 >= data_size)
{
PRINTF("Error: Unexpected end of data\n");
return NULL;
}
DWORD runStart = pb[offset];
DWORD runLength = pb[offset + 1];
offset += 2;
PRINTF("V-Run: start=%d, length=%d at x=%d\n", runStart, runLength, xCurrent);
// 境界チェック
if (runStart + runLength > (DWORD)cy)
{
PRINTF("Warning: Run exceeds vertical bounds\n");
runLength = cy - runStart;
}
if (runLength > 0)
{
RECT r = {
xCurrent,
y + (INT)runStart,
xCurrent + 1,
y + (INT)runStart + (INT)runLength
};
rects.push_back(r);
}
}
xCurrent++;
}
}
if (rects.empty())
{
PRINTF("Warning: No rectangles generated\n");
return NULL;
}
// RECTを元にリージョンを作成
header.nCount = (DWORD)rects.size();
size_t dataSize = sizeof(RGNDATAHEADER) + rects.size() * sizeof(RECT);
RGNDATA* rgndata = (RGNDATA*)malloc(dataSize);
if (!rgndata)
{
PRINTF("Error: Memory allocation failed\n");
return NULL;
}
memcpy(&rgndata->rdh, &header, sizeof(RGNDATAHEADER));
memcpy(rgndata->Buffer, &rects[0], rects.size() * sizeof(RECT));
HRGN hRgn = ExtCreateRegion(NULL, (DWORD)dataSize, rgndata);
free(rgndata);
PRINTF("Created region with %u rectangles\n", (UINT)rects.size());
return hRgn;
}
typedef struct tagBITMAPINFOEX
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
} BITMAPINFOEX, FAR * LPBITMAPINFOEX;
HBITMAP LoadBitmapFromFile(LPCTSTR pszFileName)
{
return (HBITMAP)LoadImage(NULL, pszFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}
BOOL SaveBitmapToFile(LPCTSTR pszFileName, HBITMAP hbm)
{
BOOL f;
DWORD dwError;
BITMAPFILEHEADER bf;
BITMAPINFOEX bi;
BITMAPINFOHEADER *pbmih;
DWORD cb;
DWORD cColors, cbColors;
HDC hDC;
HANDLE hFile;
LPVOID pBits;
BITMAP bm;
if (!GetObject(hbm, sizeof(BITMAP), &bm))
return FALSE;
pbmih = &bi.bmiHeader;
ZeroMemory(pbmih, sizeof(BITMAPINFOHEADER));
pbmih->biSize = sizeof(BITMAPINFOHEADER);
pbmih->biWidth = bm.bmWidth;
pbmih->biHeight = bm.bmHeight;
pbmih->biPlanes = 1;
pbmih->biBitCount = bm.bmBitsPixel;
pbmih->biCompression = BI_RGB;
pbmih->biSizeImage = bm.bmWidthBytes * bm.bmHeight;
if (bm.bmBitsPixel < 16)
cColors = 1 << bm.bmBitsPixel;
else
cColors = 0;
cbColors = cColors * sizeof(RGBQUAD);
bf.bfType = 0x4d42;
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
cb = sizeof(BITMAPFILEHEADER) + pbmih->biSize + cbColors;
bf.bfOffBits = cb;
bf.bfSize = cb + pbmih->biSizeImage;
pBits = HeapAlloc(GetProcessHeap(), 0, pbmih->biSizeImage);
if (pBits == NULL)
return FALSE;
f = FALSE;
hDC = GetDC(NULL);
if (hDC != NULL)
{
if (GetDIBits(hDC, hbm, 0, bm.bmHeight, pBits, (BITMAPINFO*)&bi,
DIB_RGB_COLORS))
{
hFile = CreateFile(pszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_WRITE_THROUGH, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
f = WriteFile(hFile, &bf, sizeof(BITMAPFILEHEADER), &cb, NULL) &&
WriteFile(hFile, &bi, sizeof(BITMAPINFOHEADER), &cb, NULL) &&
WriteFile(hFile, bi.bmiColors, cbColors, &cb, NULL) &&
WriteFile(hFile, pBits, pbmih->biSizeImage, &cb, NULL);
if (!f)
dwError = GetLastError();
CloseHandle(hFile);
if (!f)
DeleteFile(pszFileName);
}
else
dwError = GetLastError();
}
else
dwError = GetLastError();
ReleaseDC(NULL, hDC);
}
else
dwError = GetLastError();
HeapFree(GetProcessHeap(), 0, pBits);
SetLastError(dwError);
return f;
}
HBITMAP CreateBitmapFromRegionGeneric(HRGN hRgn, INT cxy)
{
HDC hdc = CreateCompatibleDC(NULL);
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = cxy;
bmi.bmiHeader.biHeight = cxy;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
HBITMAP hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
HGDIOBJ hbmOld = SelectObject(hdc, hbm);
RECT rc = { 0, 0, cxy, cxy };
FillRect(hdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));
FillRgn(hdc, hRgn, (HBRUSH)GetStockObject(BLACK_BRUSH));
SelectObject(hdc, hbmOld);
DeleteDC(hdc);
return hbm;
}
HBITMAP CreateBitmapFromRegion(HRGN hRgn)
{
return CreateBitmapFromRegionGeneric(hRgn, 300);
}
HBITMAP CreateBitmapFromRegion254(HRGN hRgn)
{
return CreateBitmapFromRegionGeneric(hRgn, 254);
}
HRGN CreateRegionFromBitmapGeneric(HBITMAP hbm, INT cxy)
{
HRGN hRgn1 = CreateRectRgn(0, 0, 0, 0);
HDC hdc = CreateCompatibleDC(NULL);
HGDIOBJ hbmOld = SelectObject(hdc, hbm);
for (INT y = 0; y < cxy; y++)
{
for (INT x = 0; x < cxy; x++)
{
if (!GetPixel(hdc, x, y))
{
HRGN hRgn2 = CreateRectRgn(x, y, x + 1, y + 1);
CombineRgn(hRgn1, hRgn1, hRgn2, RGN_OR);
DeleteObject(hRgn2);
}
}
}
SelectObject(hdc, hbmOld);
DeleteDC(hdc);
return hRgn1;
}
HRGN CreateRegionFromBitmap(HBITMAP hbm)
{
return CreateRegionFromBitmapGeneric(hbm, 300);
}
HRGN CreateRegionFromBitmap254(HBITMAP hbm)
{
return CreateRegionFromBitmapGeneric(hbm, 254);
}