diff --git a/ReferenceTests/Retro68/CMakeLists.txt b/ReferenceTests/Retro68/CMakeLists.txt index fc9a5d9..f0a4966 100644 --- a/ReferenceTests/Retro68/CMakeLists.txt +++ b/ReferenceTests/Retro68/CMakeLists.txt @@ -23,3 +23,4 @@ add_application(QuickDrawDrawingCapture QuickDrawDrawingCapture.c) add_application(PolygonCapture PolygonCapture.c) add_application(CopyBitsCapture CopyBitsCapture.c) add_application(TextCapture TextCapture.c TextCapture.r) +add_application(GWorldCapture GWorldCapture.c) diff --git a/ReferenceTests/Retro68/GWorld-macos-9.2.2-ppc.txt b/ReferenceTests/Retro68/GWorld-macos-9.2.2-ppc.txt new file mode 100644 index 0000000..55f429e --- /dev/null +++ b/ReferenceTests/Retro68/GWorld-macos-9.2.2-ppc.txt @@ -0,0 +1 @@ +main device -> depth 32 type 2 flags 0xBC21 depth 1, NewGWorld -> 0 error 0 world set depth 1, after NewGWorld -> port mine device main thePort same depth 1, port -> device 0 portVersion 0xC001 grafVars set chExtra 0 pnLocHFrac 32768 portRect (0,0,16,35) visRgn size 10 box (0,0,16,35) clipRgn size 10 box (-32767,-32767,32767,32767) bkPixPat type 0 data 0000000000000000 pnPixPat type 0 data FFFFFFFFFFFFFFFF fillPixPat type 0 data FFFFFFFFFFFFFFFF rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF depth 1, pen and text -> pnLoc (0,0) pnSize (1,1) pnMode 8 pnVis 0 txFont 0 txFace 0 txMode 1 txSize 0 spExtra 0 fgColor 1 bkColor 0 colrBit 0 patStretch 0 picSave nil rgnSave nil polySave nil grafProcs nil depth 1 -> rowBytes 0x8010 bounds (0,0,16,35) version 2 packType 0 packSize 0 hRes 4718592 vRes 4718592 pixelType 0 pixelSize 1 cmpCount 1 cmpSize 1 planeBytes 0 reserved 521519840 table set depth 1, color table -> flags 0x8000 size 1 seed depth [0] 0 FFFF,FFFF,FFFF [1] 0 0000,0000,0000 depth 1, device -> refNum 0 id 0 type 0 resPref 0 flags 0x4000 refCon 0 mode -1 next nil rect (0,0,16,35) gdPMap another depth 1 rowBytes 0x8010 base same table same depth 1, pixels -> pixmap the port's state 0x0 baseAddr not pixels lock 1 state 0x80 error 0 baseAddr pixels moved 0 aligned4 1 unlock state 0x0 allow 0x40 disallow 0x0 depth 2, NewGWorld -> 0 error 0 world set depth 2, after NewGWorld -> port mine device main thePort same depth 2, port -> device 0 portVersion 0xC001 grafVars set chExtra 0 pnLocHFrac 32768 portRect (0,0,16,35) visRgn size 10 box (0,0,16,35) clipRgn size 10 box (-32767,-32767,32767,32767) bkPixPat type 0 data 0000000000000000 pnPixPat type 0 data FFFFFFFFFFFFFFFF fillPixPat type 0 data FFFFFFFFFFFFFFFF rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF depth 2, pen and text -> pnLoc (0,0) pnSize (1,1) pnMode 8 pnVis 0 txFont 0 txFace 0 txMode 1 txSize 0 spExtra 0 fgColor 3 bkColor 0 colrBit 0 patStretch 0 picSave nil rgnSave nil polySave nil grafProcs nil depth 2 -> rowBytes 0x8010 bounds (0,0,16,35) version 2 packType 0 packSize 0 hRes 4718592 vRes 4718592 pixelType 0 pixelSize 2 cmpCount 1 cmpSize 2 planeBytes 0 reserved 521519812 table set depth 2, color table -> flags 0x8000 size 3 seed depth [0] 0 FFFF,FFFF,FFFF [1] 0 ACAC,ACAC,ACAC [2] 0 5555,5555,5555 [3] 0 0000,0000,0000 depth 2, device -> refNum 0 id 0 type 0 resPref 0 flags 0x4001 refCon 0 mode -1 next nil rect (0,0,16,35) gdPMap another depth 2 rowBytes 0x8010 base same table same depth 2, pixels -> pixmap the port's state 0x0 baseAddr not pixels lock 1 state 0x80 error 0 baseAddr pixels moved 0 aligned4 1 unlock state 0x0 allow 0x40 disallow 0x0 depth 4, NewGWorld -> 0 error 0 world set depth 4, after NewGWorld -> port mine device main thePort same depth 4, port -> device 0 portVersion 0xC001 grafVars set chExtra 0 pnLocHFrac 32768 portRect (0,0,16,35) visRgn size 10 box (0,0,16,35) clipRgn size 10 box (-32767,-32767,32767,32767) bkPixPat type 0 data 0000000000000000 pnPixPat type 0 data FFFFFFFFFFFFFFFF fillPixPat type 0 data FFFFFFFFFFFFFFFF rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF depth 4, pen and text -> pnLoc (0,0) pnSize (1,1) pnMode 8 pnVis 0 txFont 0 txFace 0 txMode 1 txSize 0 spExtra 0 fgColor 15 bkColor 0 colrBit 0 patStretch 0 picSave nil rgnSave nil polySave nil grafProcs nil depth 4 -> rowBytes 0x8020 bounds (0,0,16,35) version 2 packType 0 packSize 0 hRes 4718592 vRes 4718592 pixelType 0 pixelSize 4 cmpCount 1 cmpSize 4 planeBytes 0 reserved 521519816 table set depth 4, color table -> flags 0x8000 size 15 seed depth [0] 0 FFFF,FFFF,FFFF [1] 0 FC00,F37D,052F [2] 0 FFFF,648A,028C [3] 0 DD6B,08C2,06A2 [4] 0 F2D7,0856,84EC [5] 0 46E3,0000,A53E [6] 0 0000,0000,D400 [7] 0 0241,AB54,EAFF [8] 0 1F21,B793,1431 [9] 0 0000,64AF,11B0 [10] 0 5600,2C9D,0524 [11] 0 90D7,7160,3A34 [12] 0 C000,C000,C000 [13] 0 8000,8000,8000 [14] 0 4000,4000,4000 [15] 0 0000,0000,0000 depth 4, device -> refNum 0 id 0 type 0 resPref 0 flags 0x4001 refCon 0 mode -1 next nil rect (0,0,16,35) gdPMap another depth 4 rowBytes 0x8020 base same table same depth 4, pixels -> pixmap the port's state 0x0 baseAddr not pixels lock 1 state 0x80 error 0 baseAddr pixels moved 0 aligned4 1 unlock state 0x0 allow 0x40 disallow 0x0 depth 8, NewGWorld -> 0 error 0 world set depth 8, after NewGWorld -> port mine device main thePort same depth 8, port -> device 0 portVersion 0xC001 grafVars set chExtra 0 pnLocHFrac 32768 portRect (0,0,16,35) visRgn size 10 box (0,0,16,35) clipRgn size 10 box (-32767,-32767,32767,32767) bkPixPat type 0 data 0000000000000000 pnPixPat type 0 data FFFFFFFFFFFFFFFF fillPixPat type 0 data FFFFFFFFFFFFFFFF rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF depth 8, pen and text -> pnLoc (0,0) pnSize (1,1) pnMode 8 pnVis 0 txFont 0 txFace 0 txMode 1 txSize 0 spExtra 0 fgColor 255 bkColor 0 colrBit 0 patStretch 0 picSave nil rgnSave nil polySave nil grafProcs nil depth 8 -> rowBytes 0x8030 bounds (0,0,16,35) version 2 packType 0 packSize 0 hRes 4718592 vRes 4718592 pixelType 0 pixelSize 8 cmpCount 1 cmpSize 8 planeBytes 0 reserved 521519836 table set depth 8, color table -> flags 0x8000 size 255 seed depth [0] 0 FFFF,FFFF,FFFF [1] 0 FFFF,FFFF,CCCC [2] 0 FFFF,FFFF,9999 [3] 0 FFFF,FFFF,6666 [254] 0 1111,1111,1111 [255] 0 0000,0000,0000 depth 8, device -> refNum 0 id 0 type 0 resPref 0 flags 0x4001 refCon 0 mode -1 next nil rect (0,0,16,35) gdPMap another depth 8 rowBytes 0x8030 base same table same depth 8, pixels -> pixmap the port's state 0x0 baseAddr not pixels lock 1 state 0x80 error 0 baseAddr pixels moved 0 aligned4 1 unlock state 0x0 allow 0x40 disallow 0x0 depth 16, NewGWorld -> 0 error 0 world set depth 16, after NewGWorld -> port mine device main thePort same depth 16, port -> device 0 portVersion 0xC001 grafVars set chExtra 0 pnLocHFrac 32768 portRect (0,0,16,35) visRgn size 10 box (0,0,16,35) clipRgn size 10 box (-32767,-32767,32767,32767) bkPixPat type 0 data 0000000000000000 pnPixPat type 0 data FFFFFFFFFFFFFFFF fillPixPat type 0 data FFFFFFFFFFFFFFFF rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF depth 16, pen and text -> pnLoc (0,0) pnSize (1,1) pnMode 8 pnVis 0 txFont 0 txFace 0 txMode 1 txSize 0 spExtra 0 fgColor 0 bkColor 32767 colrBit 0 patStretch 0 picSave nil rgnSave nil polySave nil grafProcs nil depth 16 -> rowBytes 0x8050 bounds (0,0,16,35) version 2 packType 0 packSize 0 hRes 4718592 vRes 4718592 pixelType 16 pixelSize 16 cmpCount 3 cmpSize 5 planeBytes 0 reserved 521519792 table set depth 16, color table -> flags 0x0000 size 0 seed small [0] 0 0000,0000,0000 depth 16, device -> refNum 0 id 0 type 2 resPref 0 flags 0x4001 refCon 0 mode -1 next nil rect (0,0,16,35) gdPMap another depth 16 rowBytes 0x8050 base same table same depth 16, pixels -> pixmap the port's state 0x0 baseAddr not pixels lock 1 state 0x80 error 0 baseAddr pixels moved 0 aligned4 1 unlock state 0x0 allow 0x40 disallow 0x0 depth 32, NewGWorld -> 0 error 0 world set depth 32, after NewGWorld -> port mine device main thePort same depth 32, port -> device 0 portVersion 0xC001 grafVars set chExtra 0 pnLocHFrac 32768 portRect (0,0,16,35) visRgn size 10 box (0,0,16,35) clipRgn size 10 box (-32767,-32767,32767,32767) bkPixPat type 0 data 0000000000000000 pnPixPat type 0 data FFFFFFFFFFFFFFFF fillPixPat type 0 data FFFFFFFFFFFFFFFF rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF depth 32, pen and text -> pnLoc (0,0) pnSize (1,1) pnMode 8 pnVis 0 txFont 0 txFace 0 txMode 1 txSize 0 spExtra 0 fgColor 0 bkColor 16777215 colrBit 0 patStretch 0 picSave nil rgnSave nil polySave nil grafProcs nil depth 32 -> rowBytes 0x8090 bounds (0,0,16,35) version 2 packType 0 packSize 0 hRes 4718592 vRes 4718592 pixelType 16 pixelSize 32 cmpCount 3 cmpSize 8 planeBytes 0 reserved 521519848 table set depth 32, color table -> flags 0x0000 size 0 seed small [0] 0 0000,0000,0000 depth 32, device -> refNum 0 id 0 type 2 resPref 0 flags 0x4001 refCon 0 mode -1 next nil rect (0,0,16,35) gdPMap another depth 32 rowBytes 0x8090 base same table same depth 32, pixels -> pixmap the port's state 0x0 baseAddr not pixels lock 1 state 0x80 error 0 baseAddr pixels moved 0 aligned4 1 unlock state 0x0 allow 0x40 disallow 0x0 rowBytes at depth 1 -> 1:16 2:16 3:16 4:16 5:16 7:16 8:16 9:16 15:16 16:16 17:16 31:16 32:16 33:16 63:16 64:16 65:16 100:32 127:32 128:32 129:32 640:96 rowBytes at depth 2 -> 1:16 2:16 3:16 4:16 5:16 7:16 8:16 9:16 15:16 16:16 17:16 31:16 32:16 33:16 63:32 64:32 65:32 100:32 127:48 128:48 129:48 640:176 rowBytes at depth 4 -> 1:16 2:16 3:16 4:16 5:16 7:16 8:16 9:16 15:16 16:16 17:16 31:32 32:32 33:32 63:48 64:48 65:48 100:64 127:80 128:80 129:80 640:336 rowBytes at depth 8 -> 1:16 2:16 3:16 4:16 5:16 7:16 8:16 9:16 15:32 16:32 17:32 31:48 32:48 33:48 63:80 64:80 65:80 100:112 127:144 128:144 129:144 640:656 rowBytes at depth 16 -> 1:16 2:16 3:16 4:16 5:16 7:32 8:32 9:32 15:48 16:48 17:48 31:80 32:80 33:80 63:144 64:144 65:144 100:208 127:272 128:272 129:272 640:1296 rowBytes at depth 32 -> 1:16 2:16 3:16 4:32 5:32 7:32 8:48 9:48 15:64 16:80 17:80 31:128 32:144 33:144 63:256 64:272 65:272 100:416 127:512 128:528 129:528 640:2576 NewGWorld away from the origin -> 0 error 0 world set depth own 8 rowBytes 0x8030 bounds (10,20,26,53) device own state 0x0 away from the origin, port -> device 0 portVersion 0xC001 grafVars set chExtra 0 pnLocHFrac 32768 portRect (10,20,26,53) visRgn size 10 box (10,20,26,53) clipRgn size 10 box (-32767,-32767,32767,32767) bkPixPat type 0 data 0000000000000000 pnPixPat type 0 data FFFFFFFFFFFFFFFF fillPixPat type 0 data FFFFFFFFFFFFFFFF rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF away from the origin, pen and text -> pnLoc (0,0) pnSize (1,1) pnMode 8 pnVis 0 txFont 0 txFace 0 txMode 1 txSize 0 spExtra 0 fgColor 255 bkColor 0 colrBit 0 patStretch 0 picSave nil rgnSave nil polySave nil grafProcs nil away from the origin, device -> refNum 0 id 0 type 0 resPref 0 flags 0x4001 refCon 0 mode -1 next nil rect (10,20,26,53) gdPMap another depth 8 rowBytes 0x8030 base same table same NewGWorld at negative coordinates -> 0 error 0 world set depth own 8 rowBytes 0x8010 bounds (-5,-7,3,1) device own state 0x0 at negative coordinates, port -> device 0 portVersion 0xC001 grafVars set chExtra 0 pnLocHFrac 32768 portRect (-5,-7,3,1) visRgn size 10 box (-5,-7,3,1) clipRgn size 10 box (-32767,-32767,32767,32767) bkPixPat type 0 data 0000000000000000 pnPixPat type 0 data FFFFFFFFFFFFFFFF fillPixPat type 0 data FFFFFFFFFFFFFFFF rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF at negative coordinates, pen and text -> pnLoc (0,0) pnSize (1,1) pnMode 8 pnVis 0 txFont 0 txFace 0 txMode 1 txSize 0 spExtra 0 fgColor 255 bkColor 0 colrBit 0 patStretch 0 picSave nil rgnSave nil polySave nil grafProcs nil at negative coordinates, device -> refNum 0 id 0 type 0 resPref 0 flags 0x4001 refCon 0 mode -1 next nil rect (-5,-7,3,1) gdPMap another depth 8 rowBytes 0x8010 base same table same NewGWorld depth 0 -> 0 error 0 world set depth screen's 0 rowBytes 0x8030 bounds (0,0,4,8) device own state 0x0 NewGWorld pixPurge -> 0 error 0 world set depth own 8 rowBytes 0x8010 bounds (0,0,4,8) device own state 0x40 NewGWorld noNewDevice -> -50 error -50 world nil NewGWorld useTempMem -> 0 error 0 world set depth own 8 rowBytes 0x8010 bounds (0,0,4,8) device own state 0x0 NewGWorld keepLocal -> 0 error 0 world set depth own 8 rowBytes 0x8010 bounds (0,0,4,8) device own state 0x0 NewGWorld depth 3 -> -157 error -157 world nil NewGWorld depth 24 -> 0 error 0 world set depth screen's 32 rowBytes 0x8030 bounds (0,0,4,8) device own state 0x0 NewGWorld depth -1 -> -50 error -50 world nil NewGWorld empty bounds -> -50 error -50 world nil NewGWorld no width -> -50 error -50 world nil NewGWorld inverted bounds -> -50 error -50 world nil at first -> port mine device main thePort same SetGWorld of world, nil -> port world device world thePort same SetGWorld of mine, main -> port mine device main thePort same SetPort of world -> port world device main thePort same SetPort of mine -> port mine device main thePort same SetGWorld of world, main -> port world device world thePort same SetGWorld of mine, world's device -> port mine device world thePort same SetGWorld of mine, nil -> port mine device main thePort same SetGWorld of other, world's device (world here is other) -> port world device world thePort same draw 1, erased -> 00 00 00 00 draw 1, painted black -> 00 60 60 00 draw 1, inverted -> 00 60 5C 3C draw 1, ForeColor red -> fgColor 1 bkColor 0 error 0 rgbFg DD6B,08C2,06A2 rgbBk FFFF,FFFF,FFFF GetForeColor DD6B,08C2,06A2 GetBackColor FFFF,FFFF,FFFF draw 1, painted red -> F0 F0 5C 3C draw 1, RGBForeColor -> fgColor 1 bkColor 0 error 0 rgbFg 8000,4000,C000 rgbBk FFFF,FFFF,FFFF GetForeColor 8000,4000,C000 GetBackColor FFFF,FFFF,FFFF draw 1, painted RGB -> FF FF 5C 3C draw 1, pixel painted RGB -> GetPixel 1 GetCPixel 0000,0000,0000 draw 1, RGBBackColor -> fgColor 1 bkColor 0 error 0 rgbFg 8000,4000,C000 rgbBk 2000,C000,6000 GetForeColor 8000,4000,C000 GetBackColor 2000,C000,6000 draw 1, erased with RGB -> 00 00 00 00 draw 1, filled gray -> AA 55 AA 55 draw 1, pen xor -> 55 AA AA 55 draw 1, inverted colors -> 55 AA 55 AA draw 1, black and white again -> fgColor 1 bkColor 0 error 0 rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF GetForeColor 0000,0000,0000 GetBackColor FFFF,FFFF,FFFF draw 1, line -> D5 EA 5D AB draw 1, SetCPixel -> D5 EA 5D EB draw 1, pixel set -> GetPixel 1 GetCPixel 0000,0000,0000 draw 2, erased -> 0000 0000 0000 0000 draw 2, painted black -> 0000 3C00 3C00 0000 draw 2, inverted -> 0000 3C00 33F0 0FF0 draw 2, ForeColor red -> fgColor 2 bkColor 0 error 0 rgbFg DD6B,08C2,06A2 rgbBk FFFF,FFFF,FFFF GetForeColor DD6B,08C2,06A2 GetBackColor FFFF,FFFF,FFFF draw 2, painted red -> AA00 AA00 33F0 0FF0 draw 2, RGBForeColor -> fgColor 2 bkColor 0 error 0 rgbFg 8000,4000,C000 rgbBk FFFF,FFFF,FFFF GetForeColor 8000,4000,C000 GetBackColor FFFF,FFFF,FFFF draw 2, painted RGB -> AAAA AAAA 33F0 0FF0 draw 2, pixel painted RGB -> GetPixel 1 GetCPixel 5555,5555,5555 draw 2, RGBBackColor -> fgColor 2 bkColor 1 error 0 rgbFg 8000,4000,C000 rgbBk 2000,C000,6000 GetForeColor 8000,4000,C000 GetBackColor 2000,C000,6000 draw 2, erased with RGB -> 5555 5555 5555 5555 draw 2, filled gray -> 9999 6666 9999 6666 draw 2, pen xor -> 6666 9999 9999 6666 draw 2, inverted colors -> 6666 9999 6666 9999 draw 2, black and white again -> fgColor 3 bkColor 0 error 0 rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF GetForeColor 0000,0000,0000 GetBackColor FFFF,FFFF,FFFF draw 2, line -> E666 BD99 67F6 999F draw 2, SetCPixel -> E666 BD99 67F6 A99F draw 2, pixel set -> GetPixel 1 GetCPixel 5555,5555,5555 draw 4, erased -> 00000000 00000000 00000000 00000000 draw 4, painted black -> 00000000 0FF00000 0FF00000 00000000 draw 4, inverted -> 00000000 0FF00000 0F0FFF00 00FFFF00 draw 4, ForeColor red -> fgColor 3 bkColor 0 error 0 rgbFg DD6B,08C2,06A2 rgbBk FFFF,FFFF,FFFF GetForeColor DD6B,08C2,06A2 GetBackColor FFFF,FFFF,FFFF draw 4, painted red -> 33330000 33330000 0F0FFF00 00FFFF00 draw 4, RGBForeColor -> fgColor 13 bkColor 0 error 0 rgbFg 8000,4000,C000 rgbBk FFFF,FFFF,FFFF GetForeColor 8000,4000,C000 GetBackColor FFFF,FFFF,FFFF draw 4, painted RGB -> 3333DDDD 3333DDDD 0F0FFF00 00FFFF00 draw 4, pixel painted RGB -> GetPixel 1 GetCPixel 8000,8000,8000 draw 4, RGBBackColor -> fgColor 13 bkColor 8 error 0 rgbFg 8000,4000,C000 rgbBk 2000,C000,6000 GetForeColor 8000,4000,C000 GetBackColor 2000,C000,6000 draw 4, erased with RGB -> 88888888 88888888 88888888 88888888 draw 4, filled gray -> D8D8D8D8 8D8D8D8D D8D8D8D8 8D8D8D8D draw 4, pen xor -> 27272727 72727272 D8D8D8D8 8D8D8D8D draw 4, inverted colors -> 27272727 72727272 27272727 72727272 draw 4, black and white again -> fgColor 15 bkColor 0 error 0 rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF GetForeColor 0000,0000,0000 GetBackColor FFFF,FFFF,FFFF draw 4, line -> F7272727 7FF27272 272FFF27 727272FF draw 4, SetCPixel -> F7272727 7FF27272 272FFF27 757272FF draw 4, pixel set -> GetPixel 1 GetCPixel 46E3,0000,A53E draw 8, erased -> 0000000000000000 0000000000000000 0000000000000000 0000000000000000 draw 8, painted black -> 0000000000000000 00FFFF0000000000 00FFFF0000000000 0000000000000000 draw 8, inverted -> 0000000000000000 00FFFF0000000000 00FF00FFFFFF0000 0000FFFFFFFF0000 draw 8, ForeColor red -> fgColor 216 bkColor 0 error 0 rgbFg DD6B,08C2,06A2 rgbBk FFFF,FFFF,FFFF GetForeColor DD6B,08C2,06A2 GetBackColor FFFF,FFFF,FFFF draw 8, painted red -> D8D8D8D800000000 D8D8D8D800000000 00FF00FFFFFF0000 0000FFFFFFFF0000 draw 8, RGBForeColor -> fgColor 97 bkColor 0 error 0 rgbFg 8000,4000,C000 rgbBk FFFF,FFFF,FFFF GetForeColor 8000,4000,C000 GetBackColor FFFF,FFFF,FFFF draw 8, painted RGB -> D8D8D8D861616161 D8D8D8D861616161 00FF00FFFFFF0000 0000FFFFFFFF0000 draw 8, pixel painted RGB -> GetPixel 1 GetCPixel 9999,3333,CCCC draw 8, RGBBackColor -> fgColor 97 bkColor 153 error 0 rgbFg 8000,4000,C000 rgbBk 2000,C000,6000 GetForeColor 8000,4000,C000 GetBackColor 2000,C000,6000 draw 8, erased with RGB -> 9999999999999999 9999999999999999 9999999999999999 9999999999999999 draw 8, filled gray -> 6199619961996199 9961996199619961 6199619961996199 9961996199619961 draw 8, pen xor -> 9E669E669E669E66 669E669E669E669E 6199619961996199 9961996199619961 draw 8, inverted colors -> 9E669E669E669E66 669E669E669E669E 9E669E669E669E66 669E669E669E669E draw 8, black and white again -> fgColor 255 bkColor 0 error 0 rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF GetForeColor 0000,0000,0000 GetBackColor FFFF,FFFF,FFFF draw 8, line -> FF669E669E669E66 66FFFF9E669E669E 9E669EFFFFFF9E66 669E669E669EFFFF draw 8, SetCPixel -> FF669E669E669E66 66FFFF9E669E669E 9E669EFFFFFF9E66 66C8669E669EFFFF draw 8, pixel set -> GetPixel 1 GetCPixel 0000,6666,9999 draw 16, erased -> 7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF 7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF 7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF 7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF draw 16, painted black -> 7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF 7FFF000000007FFF7FFF7FFF7FFF7FFF 7FFF000000007FFF7FFF7FFF7FFF7FFF 7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF draw 16, inverted -> 7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF 7FFF000000007FFF7FFF7FFF7FFF7FFF 7FFF00007FFF0000000000007FFF7FFF 7FFF7FFF00000000000000007FFF7FFF draw 16, ForeColor red -> fgColor 27680 bkColor 32767 error 0 rgbFg DD6B,08C2,06A2 rgbBk FFFF,FFFF,FFFF GetForeColor DD6B,08C2,06A2 GetBackColor FFFF,FFFF,FFFF draw 16, painted red -> 6C206C206C206C207FFF7FFF7FFF7FFF 6C206C206C206C207FFF7FFF7FFF7FFF 7FFF00007FFF0000000000007FFF7FFF 7FFF7FFF00000000000000007FFF7FFF draw 16, RGBForeColor -> fgColor 16664 bkColor 32767 error 0 rgbFg 8000,4000,C000 rgbBk FFFF,FFFF,FFFF GetForeColor 8000,4000,C000 GetBackColor FFFF,FFFF,FFFF draw 16, painted RGB -> 6C206C206C206C204118411841184118 6C206C206C206C204118411841184118 7FFF00007FFF0000000000007FFF7FFF 7FFF7FFF00000000000000007FFF7FFF draw 16, pixel painted RGB -> GetPixel 0 GetCPixel 8421,4210,C631 draw 16, RGBBackColor -> fgColor 16664 bkColor 4876 error 0 rgbFg 8000,4000,C000 rgbBk 2000,C000,6000 GetForeColor 8000,4000,C000 GetBackColor 2000,C000,6000 draw 16, erased with RGB -> 130C130C130C130C130C130C130C130C 130C130C130C130C130C130C130C130C 130C130C130C130C130C130C130C130C 130C130C130C130C130C130C130C130C draw 16, filled gray -> 4118130C4118130C4118130C4118130C 130C4118130C4118130C4118130C4118 4118130C4118130C4118130C4118130C 130C4118130C4118130C4118130C4118 draw 16, pen xor -> 3EE76CF33EE76CF33EE76CF33EE76CF3 6CF33EE76CF33EE76CF33EE76CF33EE7 4118130C4118130C4118130C4118130C 130C4118130C4118130C4118130C4118 draw 16, inverted colors -> 3EE76CF33EE76CF33EE76CF33EE76CF3 6CF33EE76CF33EE76CF33EE76CF33EE7 3EE76CF33EE76CF33EE76CF33EE76CF3 6CF33EE76CF33EE76CF33EE76CF33EE7 draw 16, black and white again -> fgColor 0 bkColor 32767 error 0 rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF GetForeColor 0000,0000,0000 GetBackColor FFFF,FFFF,FFFF draw 16, line -> 00006CF33EE76CF33EE76CF33EE76CF3 6CF3000000003EE76CF33EE76CF33EE7 3EE76CF33EE70000000000003EE76CF3 6CF33EE76CF33EE76CF33EE700000000 draw 16, SetCPixel -> 00006CF33EE76CF33EE76CF33EE76CF3 6CF3000000003EE76CF33EE76CF33EE7 3EE76CF33EE70000000000003EE76CF3 6CF309536CF33EE76CF33EE700000000 draw 16, pixel set -> GetPixel 0 GetCPixel 1084,5294,9CE7 draw 32, erased -> 00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF draw 32, painted black -> 00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF000000000000000000FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF000000000000000000FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF draw 32, inverted -> 00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF000000000000000000FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF0000000000FFFFFF00000000000000000000000000FFFFFF00FFFFFF 00FFFFFF00FFFFFF0000000000000000000000000000000000FFFFFF00FFFFFF draw 32, ForeColor red -> fgColor 14485510 bkColor 16777215 error 0 rgbFg DD6B,08C2,06A2 rgbBk FFFF,FFFF,FFFF GetForeColor DD6B,08C2,06A2 GetBackColor FFFF,FFFF,FFFF draw 32, painted red -> 00DD080600DD080600DD080600DD080600FFFFFF00FFFFFF00FFFFFF00FFFFFF 00DD080600DD080600DD080600DD080600FFFFFF00FFFFFF00FFFFFF00FFFFFF 00FFFFFF0000000000FFFFFF00000000000000000000000000FFFFFF00FFFFFF 00FFFFFF00FFFFFF0000000000000000000000000000000000FFFFFF00FFFFFF draw 32, RGBForeColor -> fgColor 8405184 bkColor 16777215 error 0 rgbFg 8000,4000,C000 rgbBk FFFF,FFFF,FFFF GetForeColor 8000,4000,C000 GetBackColor FFFF,FFFF,FFFF draw 32, painted RGB -> 00DD080600DD080600DD080600DD0806008040C0008040C0008040C0008040C0 00DD080600DD080600DD080600DD0806008040C0008040C0008040C0008040C0 00FFFFFF0000000000FFFFFF00000000000000000000000000FFFFFF00FFFFFF 00FFFFFF00FFFFFF0000000000000000000000000000000000FFFFFF00FFFFFF draw 32, pixel painted RGB -> GetPixel 0 GetCPixel 8080,4040,C0C0 draw 32, RGBBackColor -> fgColor 8405184 bkColor 2146400 error 0 rgbFg 8000,4000,C000 rgbBk 2000,C000,6000 GetForeColor 8000,4000,C000 GetBackColor 2000,C000,6000 draw 32, erased with RGB -> 0020C0600020C0600020C0600020C0600020C0600020C0600020C0600020C060 0020C0600020C0600020C0600020C0600020C0600020C0600020C0600020C060 0020C0600020C0600020C0600020C0600020C0600020C0600020C0600020C060 0020C0600020C0600020C0600020C0600020C0600020C0600020C0600020C060 draw 32, filled gray -> 008040C00020C060008040C00020C060008040C00020C060008040C00020C060 0020C060008040C00020C060008040C00020C060008040C00020C060008040C0 008040C00020C060008040C00020C060008040C00020C060008040C00020C060 0020C060008040C00020C060008040C00020C060008040C00020C060008040C0 draw 32, pen xor -> 007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F 00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F 008040C00020C060008040C00020C060008040C00020C060008040C00020C060 0020C060008040C00020C060008040C00020C060008040C00020C060008040C0 draw 32, inverted colors -> 007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F 00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F 007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F 00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F draw 32, black and white again -> fgColor 0 bkColor 16777215 error 0 rgbFg 0000,0000,0000 rgbBk FFFF,FFFF,FFFF GetForeColor 0000,0000,0000 GetBackColor FFFF,FFFF,FFFF draw 32, line -> 0000000000DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F 00DF3F9F0000000000000000007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F 007FBF3F00DF3F9F007FBF3F000000000000000000000000007FBF3F00DF3F9F 00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F0000000000000000 draw 32, SetCPixel -> 0000000000DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F00DF3F9F 00DF3F9F0000000000000000007FBF3F00DF3F9F007FBF3F00DF3F9F007FBF3F 007FBF3F00DF3F9F007FBF3F000000000000000000000000007FBF3F00DF3F9F 00DF3F9F0012569A00DF3F9F007FBF3F00DF3F9F007FBF3F0000000000000000 draw 32, pixel set -> GetPixel 0 GetCPixel 1212,5656,9A9A stripes at 1 -> 7F 7F 7F 7F CopyBits 1 to a BitMap -> 7F 7F 7F 7F CopyBits 1 to a BitMap by portBits -> 7F 7F 7F 7F CopyBits 1 to 1 -> 7F 7F 7F 7F CopyBits 1 to 2 -> 3FFF 3FFF 3FFF 3FFF CopyBits 1 to 4 -> 0FFFFFFF 0FFFFFFF 0FFFFFFF 0FFFFFFF CopyBits 1 to 8 -> 00FFFFFFFFFFFFFF 00FFFFFFFFFFFFFF 00FFFFFFFFFFFFFF 00FFFFFFFFFFFFFF CopyBits 1 to 16 -> 7FFF0000000000000000000000000000 7FFF0000000000000000000000000000 7FFF0000000000000000000000000000 7FFF0000000000000000000000000000 CopyBits 1 to 32 -> 00FFFFFF00000000000000000000000000000000000000000000000000000000 00FFFFFF00000000000000000000000000000000000000000000000000000000 00FFFFFF00000000000000000000000000000000000000000000000000000000 00FFFFFF00000000000000000000000000000000000000000000000000000000 stripes at 2 -> 39F6 39F6 39F6 39F6 CopyBits 2 to a BitMap -> 6D 6D 6D 6D CopyBits 2 to a BitMap by portBits -> 6D 6D 6D 6D CopyBits 2 to 1 -> 6D 6D 6D 6D CopyBits 2 to 2 -> 39F6 39F6 39F6 39F6 CopyBits 2 to 4 -> 0FECFFCE 0FECFFCE 0FECFFCE 0FECFFCE CopyBits 2 to 8 -> 00FFFBF8FFFFF8FB 00FFFBF8FFFFF8FB 00FFFBF8FFFFF8FB 00FFFBF8FFFFF8FB CopyBits 2 to 16 -> 7FFF0000294A56B50000000056B5294A 7FFF0000294A56B50000000056B5294A 7FFF0000294A56B50000000056B5294A 7FFF0000294A56B50000000056B5294A CopyBits 2 to 32 -> 00FFFFFF000000000055555500ACACAC000000000000000000ACACAC00555555 00FFFFFF000000000055555500ACACAC000000000000000000ACACAC00555555 00FFFFFF000000000055555500ACACAC000000000000000000ACACAC00555555 00FFFFFF000000000055555500ACACAC000000000000000000ACACAC00555555 stripes at 4 -> 0F3861DD 0F3861DD 0F3861DD 0F3861DD CopyBits 4 to a BitMap -> 78 78 78 78 CopyBits 4 to a BitMap by portBits -> 78 78 78 78 CopyBits 4 to 1 -> 78 78 78 78 CopyBits 4 to 2 -> 3ACA 3ACA 3ACA 3ACA CopyBits 4 to 4 -> 0F3861DD 0F3861DD 0F3861DD 0F3861DD CopyBits 4 to 8 -> 00FFD8E3EC05F9F9 00FFD8E3EC05F9F9 00FFD8E3EC05F9F9 00FFD8E3EC05F9F9 CopyBits 4 to 16 -> 7FFF00006C200EC2001A7FC042104210 7FFF00006C200EC2001A7FC042104210 7FFF00006C200EC2001A7FC042104210 7FFF00006C200EC2001A7FC042104210 CopyBits 4 to 32 -> 00FFFFFF0000000000DD0806001FB714000000D400FCF3050080808000808080 00FFFFFF0000000000DD0806001FB714000000D400FCF3050080808000808080 00FFFFFF0000000000DD0806001FB714000000D400FCF3050080808000808080 00FFFFFF0000000000DD0806001FB714000000D400FCF3050080808000808080 stripes at 8 -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 CopyBits 8 to a BitMap -> 69 69 69 69 CopyBits 8 to a BitMap by portBits -> 69 69 69 69 CopyBits 8 to 1 -> 69 69 69 69 CopyBits 8 to 2 -> 39C6 39C6 39C6 39C6 CopyBits 8 to 4 -> 0F3861DD 0F3861DD 0F3861DD 0F3861DD CopyBits 8 to 8 -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 CopyBits 8 to 16 -> 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C CopyBits 8 to 32 -> 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 stripes at 16 -> 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C CopyBits 16 to a BitMap -> 69 69 69 69 CopyBits 16 to a BitMap by portBits -> 69 69 69 69 CopyBits 16 to 1 -> 69 69 69 69 CopyBits 16 to 2 -> 39C6 39C6 39C6 39C6 CopyBits 16 to 4 -> 0F3861DD 0F3861DD 0F3861DD 0F3861DD CopyBits 16 to 8 -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 CopyBits 16 to 16 -> 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C CopyBits 16 to 32 -> 00FFFFFF0000000000FF00000000FF00000000FF00FFFF00009C9C9C00636363 00FFFFFF0000000000FF00000000FF00000000FF00FFFF00009C9C9C00636363 00FFFFFF0000000000FF00000000FF00000000FF00FFFF00009C9C9C00636363 00FFFFFF0000000000FF00000000FF00000000FF00FFFF00009C9C9C00636363 stripes at 32 -> 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 CopyBits 32 to a BitMap -> 69 69 69 69 CopyBits 32 to a BitMap by portBits -> 69 69 69 69 CopyBits 32 to 1 -> 69 69 69 69 CopyBits 32 to 2 -> 39C6 39C6 39C6 39C6 CopyBits 32 to 4 -> 0F3861DD 0F3861DD 0F3861DD 0F3861DD CopyBits 32 to 8 -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 CopyBits 32 to 16 -> 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C 7FFF00007C0003E0001F7FE04E73318C CopyBits 32 to 32 -> 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 00FFFFFF0000000000FF00000000FF00000000FF00FFFF000099999900666666 CopyBits BitMap to 1 -> F0 CC AA 00 CopyBits BitMap to 1, red on blue -> 0F 33 55 FF CopyBits BitMap to 2 -> FF00 F0F0 CCCC 0000 CopyBits BitMap to 2, red on blue -> AAFF AFAF BBBB FFFF CopyBits BitMap to 4 -> FFFF0000 FF00FF00 F0F0F0F0 00000000 CopyBits BitMap to 4, red on blue -> 33336666 33663366 36363636 66666666 CopyBits BitMap to 8 -> FFFFFFFF00000000 FFFF0000FFFF0000 FF00FF00FF00FF00 0000000000000000 CopyBits BitMap to 8, red on blue -> 23232323D2D2D2D2 2323D2D22323D2D2 23D223D223D223D2 D2D2D2D2D2D2D2D2 CopyBits BitMap to 16 -> 00000000000000007FFF7FFF7FFF7FFF 000000007FFF7FFF000000007FFF7FFF 00007FFF00007FFF00007FFF00007FFF 7FFF7FFF7FFF7FFF7FFF7FFF7FFF7FFF CopyBits BitMap to 16, red on blue -> 7C007C007C007C00001F001F001F001F 7C007C00001F001F7C007C00001F001F 7C00001F7C00001F7C00001F7C00001F 001F001F001F001F001F001F001F001F CopyBits BitMap to 32 -> 0000000000000000000000000000000000FFFFFF00FFFFFF00FFFFFF00FFFFFF 000000000000000000FFFFFF00FFFFFF000000000000000000FFFFFF00FFFFFF 0000000000FFFFFF0000000000FFFFFF0000000000FFFFFF0000000000FFFFFF 00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF00FFFFFF CopyBits BitMap to 32, red on blue -> 00FF000000FF000000FF000000FF0000000000FF000000FF000000FF000000FF 00FF000000FF0000000000FF000000FF00FF000000FF0000000000FF000000FF 00FF0000000000FF00FF0000000000FF00FF0000000000FF00FF0000000000FF 000000FF000000FF000000FF000000FF000000FF000000FF000000FF000000FF UpdateGWorld with the same, no flags -> 0x0 error 0 world same pixmap same base same depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,4,8) portRect (0,0,4,8) visRgn size 10 box (0,0,4,8) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld with the same, no flags, pixels -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 UpdateGWorld offset, no flags -> 0x40000 error 0 world same pixmap same base same depth 8 rowBytes 0x8010 state 0x0 bounds (1,2,5,10) portRect (1,2,5,10) visRgn size 10 box (1,2,5,10) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld offset, no flags, pixels -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 UpdateGWorld larger, no flags -> 0x20140000 error 0 world new pixmap new base new depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,6,12) portRect (0,0,6,12) visRgn size 10 box (0,0,6,12) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld larger, no flags, pixels -> 000000000000004800000048 000100010001000000000000 000000000000000000000000 1F166D601000000000000030 00000001000000010000FFFF 000100000000000000000000 UpdateGWorld smaller, no flags -> 0x20180000 error 0 world new pixmap new base new depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld smaller, no flags, pixels -> 1F15C29480 0000000000 0008000100 UpdateGWorld deeper, no flags -> 0x1B0000 error 0 world new pixmap new base new depth 32 rowBytes 0x8020 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld deeper, no flags, pixels -> 000000000000CCCCCCCCFFFF1F15C22800000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 UpdateGWorld to depth 3, no flags -> 0xFFFFFF63 error -157 world same pixmap same base same depth 32 rowBytes 0x8020 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld to depth 3, no flags, pixels -> 000000000000CCCCCCCCFFFF1F15C22800000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 UpdateGWorld with the same, clipPix -> 0x0 error 0 world same pixmap same base same depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,4,8) portRect (0,0,4,8) visRgn size 10 box (0,0,4,8) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld with the same, clipPix, pixels -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 UpdateGWorld offset, clipPix -> 0x40000 error 0 world same pixmap same base same depth 8 rowBytes 0x8010 state 0x0 bounds (1,2,5,10) portRect (1,2,5,10) visRgn size 10 box (1,2,5,10) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld offset, clipPix, pixels -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 UpdateGWorld larger, clipPix -> 0x10140000 error 0 world new pixmap new base new depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,6,12) portRect (0,0,6,12) visRgn size 10 box (0,0,6,12) clipRgn size 10 box (-32768,32767,32766,32765) UpdateGWorld larger, clipPix, pixels -> 00FF23B9D205568100000020 00FF23B9D205568100050000 00FF23B9D205568100000020 00FF23B9D20556817FFF1A40 1F164DF010000000000004B0 1F15C2948020000000000003 UpdateGWorld smaller, clipPix -> 0x10180000 error 0 world new pixmap new base new depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32768,32767,32766,32765) UpdateGWorld smaller, clipPix, pixels -> 00FF23B9D2 00FF23B9D2 00FF23B9D2 UpdateGWorld deeper, clipPix -> 0x1B0000 error 0 world new pixmap new base new depth 32 rowBytes 0x8020 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32768,32767,32766,32765) UpdateGWorld deeper, clipPix, pixels -> 00FFFFFF0000000000FF00000000FF00000000FF 00FFFFFF0000000000FF00000000FF00000000FF 00FFFFFF0000000000FF00000000FF00000000FF UpdateGWorld to depth 3, clipPix -> 0xFFFFFF63 error -157 world same pixmap same base same depth 32 rowBytes 0x8020 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32768,32767,32766,32765) UpdateGWorld to depth 3, clipPix, pixels -> 00FFFFFF0000000000FF00000000FF00000000FF 00FFFFFF0000000000FF00000000FF00000000FF 00FFFFFF0000000000FF00000000FF00000000FF UpdateGWorld with the same, stretchPix -> 0x0 error 0 world same pixmap same base same depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,4,8) portRect (0,0,4,8) visRgn size 10 box (0,0,4,8) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld with the same, stretchPix, pixels -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 UpdateGWorld offset, stretchPix -> 0x40000 error 0 world same pixmap same base same depth 8 rowBytes 0x8010 state 0x0 bounds (1,2,5,10) portRect (1,2,5,10) visRgn size 10 box (1,2,5,10) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld offset, stretchPix, pixels -> 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 00FF23B9D2055681 UpdateGWorld larger, stretchPix -> 0x20140000 error 0 world new pixmap new base new depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,6,12) portRect (0,0,6,12) visRgn size 10 box (0,0,6,12) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld larger, stretchPix, pixels -> 0000FF2323B9D2D205565681 0000FF2323B9D2D205565681 0000FF2323B9D2D205565681 0000FF2323B9D2D205565681 0000FF2323B9D2D205565681 0000FF2323B9D2D205565681 UpdateGWorld smaller, stretchPix -> 0x20180000 error 0 world new pixmap new base new depth 8 rowBytes 0x8010 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld smaller, stretchPix, pixels -> 00FFD2D281 00FFD2D281 00FFD2D281 UpdateGWorld deeper, stretchPix -> 0x1B0000 error 0 world new pixmap new base new depth 32 rowBytes 0x8020 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld deeper, stretchPix, pixels -> 00FFFFFF00000000000000FF000000FF00666666 00FFFFFF00000000000000FF000000FF00666666 00FFFFFF00000000000000FF000000FF00666666 UpdateGWorld to depth 3, stretchPix -> 0xFFFFFF63 error -157 world same pixmap same base same depth 32 rowBytes 0x8020 state 0x0 bounds (0,0,3,5) portRect (0,0,3,5) visRgn size 10 box (0,0,3,5) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld to depth 3, stretchPix, pixels -> 00FFFFFF00000000000000FF000000FF00666666 00FFFFFF00000000000000FF000000FF00666666 00FFFFFF00000000000000FF000000FF00666666 UpdateGWorld of the current world -> 0x201B0000 error 0 world new pixmap new base new depth 16 rowBytes 0x8020 state 0x80 bounds (0,0,6,12) portRect (0,0,6,12) visRgn size 10 box (0,0,6,12) clipRgn size 10 box (-32767,-32767,32767,32767) UpdateGWorld of the current world, pixels -> 0000000000000000000000001F1625A00000000000000000 000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000080017FFF 1F166D6010000000000000401F1673B000FFD2D281100001 00FFD2D281000048000000480000000000FFD2D281080000 1F164DA010000000000000D01F1673B01F15C27C80200000 after UpdateGWorld of the current world -> port world device world thePort same OffscreenVersion -> 336 QDDone -> 1 1 PixMap32Bit -> 0 after DisposeGWorld of the current world -> port other device main thePort same done -> \ No newline at end of file diff --git a/ReferenceTests/Retro68/GWorldCapture.c b/ReferenceTests/Retro68/GWorldCapture.c new file mode 100644 index 0000000..83f12c9 --- /dev/null +++ b/ReferenceTests/Retro68/GWorldCapture.c @@ -0,0 +1,600 @@ +/* + * Captures graphics worlds: what NewGWorld gives at each depth (the color + * port, its pixel map, color table and device), the rows' widths, its flags + * and errors, GetGWorld and SetGWorld, locking, what drawing and the color + * calls leave in a world's pixels, CopyBits between depths, and UpdateGWorld. + * It writes "GWorld.txt" next to itself and refuses to run if that file is + * already there. The file is flushed after every line, so a call that crashes + * leaves the lines before it; the calls most likely to crash come last. + * + * Everything is drawn offscreen. Addresses are never printed, only whether two + * are the same. A pixel dump gives each row's bytes up to the last pixel. + */ +#include +#include +#include +#include +#include +#include +#include + +static short out; +static GrafPort port; +static unsigned char bits[4 * 16]; +static GDHandle screen; + +static char text[2000]; +static int used; + +static void add(const char *format, ...) +{ + va_list arguments; + va_start(arguments, format); + used += vsnprintf(text + used, sizeof text - used, format, arguments); + va_end(arguments); +} + +static void start(const char *format, ...) +{ + va_list arguments; + va_start(arguments, format); + used = vsnprintf(text, sizeof text, format, arguments); + va_end(arguments); + add(" ->"); +} + +static void end(void) +{ + long count = strlen(text); + FSWrite(out, &count, text); + count = 1; + FSWrite(out, &count, "\r"); + FlushVol(0, 0); +} + +static Rect rect(short top, short left, short bottom, short right) +{ + Rect r; + r.top = top; r.left = left; r.bottom = bottom; r.right = right; + return r; +} + +static RGBColor rgb(unsigned short red, unsigned short green, unsigned short blue) +{ + RGBColor c; + c.red = red; c.green = green; c.blue = blue; + return c; +} + +static void addRect(const char *name, const Rect *r) +{ + add(" %s (%d,%d,%d,%d)", name, r->top, r->left, r->bottom, r->right); +} + +static void addColor(const char *name, const RGBColor *c) +{ + add(" %s %04X,%04X,%04X", name, c->red, c->green, c->blue); +} + +static void addRegion(const char *name, RgnHandle r) +{ + if (!r) { add(" %s nil", name); return; } + add(" %s size %d", name, (int)(*r)->rgnSize); + addRect("box", &(*r)->rgnBBox); +} + +static void addPattern(const char *name, PixPatHandle p) +{ + int i; + if (!p) { add(" %s nil", name); return; } + add(" %s type %d data ", name, (*p)->patType); + for (i = 0; i < 8; i++) add("%02X", (*p)->pat1Data.pat[i]); +} + +/* The current port and device, against a world and the probe's own. */ +static void where(const char *label, GWorldPtr world) +{ + CGrafPtr current; + GDHandle device; + GetGWorld(¤t, &device); + start("%s", label); + add(" port %s", (GrafPtr)current == &port ? "mine" : world && current == (CGrafPtr)world ? "world" : "other"); + add(" device %s", device == screen ? "main" : world && device == GetGWorldDevice(world) ? "world" : "other"); + add(" thePort %s", qd.thePort == (GrafPtr)current ? "same" : "differs"); + end(); +} + +static void dumpPixMap(const char *label, PixMapHandle handle) +{ + PixMap *map = *handle; + CTabHandle table = map->pmTable; + start("%s", label); + add(" rowBytes 0x%04X", (unsigned short)map->rowBytes); + addRect("bounds", &map->bounds); + add(" version %d packType %d packSize %ld hRes %ld vRes %ld pixelType %d pixelSize %d cmpCount %d cmpSize %d" + " planeBytes %ld reserved %ld table %s", + map->pmVersion, map->packType, (long)map->packSize, (long)map->hRes, (long)map->vRes, map->pixelType, + map->pixelSize, map->cmpCount, map->cmpSize, (long)map->planeBytes, (long)map->pmReserved, + table ? "set" : "nil"); + end(); + if (table) { + int count = (*table)->ctSize + 1, i; + start("%s, color table", label); + add(" flags 0x%04X size %d seed %s", (*table)->ctFlags, (*table)->ctSize, + (*table)->ctSeed == map->pixelSize ? "depth" : (*table)->ctSeed < 1024 ? "small" : "large"); + for (i = 0; i < count && i < 256; i++) { + if (count > 16 && i > 3 && i < count - 2) continue; + add(" [%d] %d", i, (*table)->ctTable[i].value); + addColor("", &(*table)->ctTable[i].rgb); + } + end(); + } +} + +static void dumpPort(const char *label, GWorldPtr world) +{ + CGrafPtr p = (CGrafPtr)world; + start("%s, port", label); + add(" device %d portVersion 0x%04X grafVars %s chExtra %d pnLocHFrac %d", p->device, + (unsigned short)p->portVersion, p->grafVars ? "set" : "nil", p->chExtra, (unsigned short)p->pnLocHFrac); + addRect("portRect", &p->portRect); + addRegion("visRgn", p->visRgn); + addRegion("clipRgn", p->clipRgn); + addPattern("bkPixPat", p->bkPixPat); + addPattern("pnPixPat", p->pnPixPat); + addPattern("fillPixPat", p->fillPixPat); + addColor("rgbFg", &p->rgbFgColor); + addColor("rgbBk", &p->rgbBkColor); + end(); + start("%s, pen and text", label); + add(" pnLoc (%d,%d) pnSize (%d,%d) pnMode %d pnVis %d txFont %d txFace %d txMode %d txSize %d spExtra %ld" + " fgColor %ld bkColor %ld colrBit %d patStretch %d picSave %s rgnSave %s polySave %s grafProcs %s", + p->pnLoc.v, p->pnLoc.h, p->pnSize.v, p->pnSize.h, p->pnMode, p->pnVis, p->txFont, (int)p->txFace, + p->txMode, p->txSize, (long)p->spExtra, (long)p->fgColor, (long)p->bkColor, p->colrBit, p->patStretch, + p->picSave ? "set" : "nil", p->rgnSave ? "set" : "nil", p->polySave ? "set" : "nil", + p->grafProcs ? "set" : "nil"); + end(); +} + +static void dumpDevice(const char *label, GWorldPtr world) +{ + GDHandle handle = GetGWorldDevice(world); + PixMapHandle map = GetGWorldPixMap(world); + start("%s, device", label); + if (handle == screen) { add(" main"); end(); return; } + if (!handle) { add(" nil"); end(); return; } + { + GDevice *d = *handle; + add(" refNum %d id %d type %d resPref %d flags 0x%04X refCon %ld mode %ld next %s", d->gdRefNum, d->gdID, + d->gdType, d->gdResPref, (unsigned short)d->gdFlags, (long)d->gdRefCon, (long)d->gdMode, + d->gdNextGD ? "set" : "nil"); + addRect("rect", &d->gdRect); + add(" gdPMap %s", d->gdPMap == map ? "the world's" : "another"); + if (d->gdPMap) + add(" depth %d rowBytes 0x%04X base %s table %s", (*d->gdPMap)->pixelSize, + (unsigned short)(*d->gdPMap)->rowBytes, + (*d->gdPMap)->baseAddr == (*map)->baseAddr ? "same" : "differs", + (*d->gdPMap)->pmTable == (*map)->pmTable ? "same" : "differs"); + end(); + } +} + +static void locking(const char *label, GWorldPtr world) +{ + PixMapHandle map = GetGWorldPixMap(world); + Ptr before; + Boolean locked; + start("%s, pixels", label); + add(" pixmap %s", map == ((CGrafPtr)world)->portPixMap ? "the port's" : "another"); + add(" state 0x%lX", (long)GetPixelsState(map)); + before = GetPixBaseAddr(map); + add(" baseAddr %s", (*map)->baseAddr == before ? "pixels" : "not pixels"); + locked = LockPixels(map); + add(" lock %d state 0x%lX error %d", locked != 0, (long)GetPixelsState(map), QDError()); + add(" baseAddr %s", (*map)->baseAddr == GetPixBaseAddr(map) ? "pixels" : "not pixels"); + add(" moved %d aligned4 %d", GetPixBaseAddr(map) != before, ((unsigned long)GetPixBaseAddr(map) & 3) == 0); + UnlockPixels(map); + add(" unlock state 0x%lX", (long)GetPixelsState(map)); + AllowPurgePixels(map); + add(" allow 0x%lX", (long)GetPixelsState(map)); + NoPurgePixels(map); + add(" disallow 0x%lX", (long)GetPixelsState(map)); + end(); +} + +static const short depths[] = { 1, 2, 4, 8, 16, 32 }; + +static void creation(void) +{ + char label[80]; + int d; + for (d = 0; d < 6; d++) { + GWorldPtr world = 0; + Rect bounds = rect(0, 0, 16, 35); + QDErr result = NewGWorld(&world, depths[d], &bounds, 0, 0, 0); + sprintf(label, "depth %d", depths[d]); + start("%s, NewGWorld", label); + add(" %d error %d world %s", result, QDError(), world ? "set" : "nil"); + end(); + if (!world) continue; + sprintf(label, "depth %d, after NewGWorld", depths[d]); + where(label, world); + sprintf(label, "depth %d", depths[d]); + dumpPort(label, world); + dumpPixMap(label, GetGWorldPixMap(world)); + dumpDevice(label, world); + locking(label, world); + DisposeGWorld(world); + } +} + +static void widths(void) +{ + static const short list[] = { 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 100, 127, 128, 129, 640 }; + int d, i; + for (d = 0; d < 6; d++) { + start("rowBytes at depth %d", depths[d]); + for (i = 0; i < (int)(sizeof list / sizeof list[0]); i++) { + GWorldPtr world = 0; + Rect bounds = rect(0, 0, 2, list[i]); + if (NewGWorld(&world, depths[d], &bounds, 0, 0, 0) != noErr || !world) { add(" %d:error", list[i]); continue; } + add(" %d:%d", list[i], (*GetGWorldPixMap(world))->rowBytes & 0x3FFF); + DisposeGWorld(world); + } + end(); + } +} + +static void attempt(const char *label, short depth, Rect bounds, GWorldFlags flags, int detail) +{ + GWorldPtr world = 0; + QDErr result = NewGWorld(&world, depth, &bounds, 0, 0, flags); + start("NewGWorld %s", label); + add(" %d error %d world %s", result, QDError(), world ? "set" : "nil"); + if (world) { + PixMapHandle map = GetGWorldPixMap(world); + add(" depth %s %d rowBytes 0x%04X", (*map)->pixelSize == (*(*screen)->gdPMap)->pixelSize ? "screen's" : "own", + depth ? (*map)->pixelSize : 0, (unsigned short)(*map)->rowBytes); + addRect("bounds", &(*map)->bounds); + add(" device %s state 0x%lX", GetGWorldDevice(world) == screen ? "main" : "own", (long)GetPixelsState(map)); + } + end(); + if (world && detail) { + dumpPort(label, world); + dumpDevice(label, world); + } + if (world) DisposeGWorld(world); +} + +static void flagsAndErrors(void) +{ + Rect small = rect(0, 0, 4, 8); + attempt("away from the origin", 8, rect(10, 20, 26, 53), 0, 1); + attempt("at negative coordinates", 8, rect(-5, -7, 3, 1), 0, 1); + attempt("depth 0", 0, small, 0, 0); + attempt("pixPurge", 8, small, pixPurge, 0); + attempt("noNewDevice", 8, small, noNewDevice, 1); + attempt("useTempMem", 8, small, useTempMem, 0); + attempt("keepLocal", 8, small, keepLocal, 0); + attempt("depth 3", 3, small, 0, 0); + attempt("depth 24", 24, small, 0, 0); + attempt("depth -1", -1, small, 0, 0); + attempt("empty bounds", 8, rect(0, 0, 0, 0), 0, 0); + attempt("no width", 8, rect(0, 4, 4, 4), 0, 0); + attempt("inverted bounds", 8, rect(4, 8, 0, 0), 0, 0); +} + +static void switching(void) +{ + GWorldPtr world = 0, other = 0; + Rect bounds = rect(0, 0, 4, 8); + SetGWorld((CGrafPtr)&port, screen); + where("at first", 0); + NewGWorld(&world, 8, &bounds, 0, 0, 0); + NewGWorld(&other, 32, &bounds, 0, 0, 0); + if (!world || !other) return; + SetGWorld((CGrafPtr)world, 0); where("SetGWorld of world, nil", world); + SetGWorld((CGrafPtr)&port, screen); where("SetGWorld of mine, main", world); + SetPort((GrafPtr)world); where("SetPort of world", world); + SetPort(&port); where("SetPort of mine", world); + SetGWorld((CGrafPtr)world, screen); where("SetGWorld of world, main", world); + SetGWorld((CGrafPtr)&port, GetGWorldDevice(world)); where("SetGWorld of mine, world's device", world); + SetGWorld((CGrafPtr)&port, 0); where("SetGWorld of mine, nil", world); + SetGWorld((CGrafPtr)world, 0); + SetGWorld((CGrafPtr)other, GetGWorldDevice(world)); where("SetGWorld of other, world's device (world here is other)", other); + SetGWorld((CGrafPtr)&port, screen); + DisposeGWorld(other); + DisposeGWorld(world); +} + +/* The pixels of a world that has been locked. */ +static void dumpPixels(const char *label, GWorldPtr world) +{ + PixMapHandle map = GetGWorldPixMap(world); + const unsigned char *base = (const unsigned char *)GetPixBaseAddr(map); + int rowBytes = (*map)->rowBytes & 0x3FFF; + int height = (*map)->bounds.bottom - (*map)->bounds.top; + int length = (((*map)->bounds.right - (*map)->bounds.left) * (*map)->pixelSize + 7) / 8; + int row, i; + start("%s", label); + for (row = 0; row < height; row++) { + add(" "); + for (i = 0; i < length; i++) add("%02X", base[row * rowBytes + i]); + } + end(); +} + +static void colors(const char *label) +{ + CGrafPtr p = (CGrafPtr)qd.thePort; + RGBColor fore, back; + GetForeColor(&fore); + GetBackColor(&back); + start("%s", label); + add(" fgColor %ld bkColor %ld error %d", (long)p->fgColor, (long)p->bkColor, QDError()); + addColor("rgbFg", &p->rgbFgColor); + addColor("rgbBk", &p->rgbBkColor); + addColor("GetForeColor", &fore); + addColor("GetBackColor", &back); + end(); +} + +static void pixel(const char *label, short h, short v) +{ + RGBColor c; + GetCPixel(h, v, &c); + start("%s", label); + add(" GetPixel %d", GetPixel(h, v) != 0); + addColor("GetCPixel", &c); + end(); +} + +static void drawing(void) +{ + int d; + for (d = 0; d < 6; d++) { + GWorldPtr world = 0; + Rect all = rect(0, 0, 4, 8), r; + RGBColor color; + char label[80]; + short depth = depths[d]; + NewGWorld(&world, depth, &all, 0, 0, 0); + if (!world) continue; + SetGWorld((CGrafPtr)world, 0); + LockPixels(GetGWorldPixMap(world)); +#define SHOW(what) (sprintf(label, "draw %d, %s", depth, what), dumpPixels(label, world)) + EraseRect(&all); SHOW("erased"); + r = rect(1, 1, 3, 3); PaintRect(&r); SHOW("painted black"); + r = rect(2, 2, 4, 6); InvertRect(&r); SHOW("inverted"); + ForeColor(redColor); + sprintf(label, "draw %d, ForeColor red", depth); colors(label); + r = rect(0, 0, 2, 4); PaintRect(&r); SHOW("painted red"); + color = rgb(0x8000, 0x4000, 0xC000); RGBForeColor(&color); + sprintf(label, "draw %d, RGBForeColor", depth); colors(label); + r = rect(0, 4, 2, 8); PaintRect(&r); SHOW("painted RGB"); + sprintf(label, "draw %d, pixel painted RGB", depth); pixel(label, 5, 0); + color = rgb(0x2000, 0xC000, 0x6000); RGBBackColor(&color); + sprintf(label, "draw %d, RGBBackColor", depth); colors(label); + EraseRect(&all); SHOW("erased with RGB"); + FillRect(&all, &qd.gray); SHOW("filled gray"); + r = rect(0, 0, 2, 8); PenMode(patXor); PaintRect(&r); SHOW("pen xor"); + PenNormal(); + r = rect(2, 0, 4, 8); InvertRect(&r); SHOW("inverted colors"); + ForeColor(blackColor); BackColor(whiteColor); + sprintf(label, "draw %d, black and white again", depth); colors(label); + MoveTo(0, 0); LineTo(7, 3); SHOW("line"); + color = rgb(0x1234, 0x5678, 0x9ABC); SetCPixel(1, 3, &color); SHOW("SetCPixel"); + sprintf(label, "draw %d, pixel set", depth); pixel(label, 1, 3); +#undef SHOW + SetGWorld((CGrafPtr)&port, screen); + DisposeGWorld(world); + } +} + +static const unsigned short palette[8][3] = { + { 0xFFFF, 0xFFFF, 0xFFFF }, { 0x0000, 0x0000, 0x0000 }, { 0xFFFF, 0x0000, 0x0000 }, { 0x0000, 0xFFFF, 0x0000 }, + { 0x0000, 0x0000, 0xFFFF }, { 0xFFFF, 0xFFFF, 0x0000 }, { 0x9999, 0x9999, 0x9999 }, { 0x6666, 0x6666, 0x6666 }, +}; + +/* Eight columns of those colors in a world, which is left locked. */ +static GWorldPtr striped(short depth) +{ + GWorldPtr world = 0; + Rect all = rect(0, 0, 4, 8), r; + int i; + NewGWorld(&world, depth, &all, 0, 0, 0); + if (!world) return 0; + SetGWorld((CGrafPtr)world, 0); + LockPixels(GetGWorldPixMap(world)); + for (i = 0; i < 8; i++) { + RGBColor c = rgb(palette[i][0], palette[i][1], palette[i][2]); + RGBForeColor(&c); + r = rect(0, i, 4, i + 1); + PaintRect(&r); + } + ForeColor(blackColor); + SetGWorld((CGrafPtr)&port, screen); + return world; +} + +static void dumpBits(const char *label) +{ + int row; + start("%s", label); + for (row = 0; row < 4; row++) add(" %02X", bits[row * 4]); + end(); +} + +static void copying(void) +{ + static const short to[] = { 1, 2, 4, 8, 16, 32 }; + Rect all = rect(0, 0, 4, 8); + static unsigned char source[4 * 2] = { 0xF0, 0, 0xCC, 0, 0xAA, 0, 0x00, 0 }; + BitMap map; + int s, d; + char label[80]; + + map.baseAddr = (Ptr)source; + map.rowBytes = 2; + map.bounds = rect(0, 0, 4, 16); + + for (s = 0; s < 6; s++) { + GWorldPtr from = striped(to[s]); + if (!from) continue; + sprintf(label, "stripes at %d", to[s]); dumpPixels(label, from); + memset(bits, 0, sizeof bits); + SetPort(&port); + CopyBits((BitMap *)*GetGWorldPixMap(from), &port.portBits, &all, &all, srcCopy, 0); + sprintf(label, "CopyBits %d to a BitMap", to[s]); dumpBits(label); + memset(bits, 0, sizeof bits); + CopyBits(&((GrafPtr)from)->portBits, &port.portBits, &all, &all, srcCopy, 0); + sprintf(label, "CopyBits %d to a BitMap by portBits", to[s]); dumpBits(label); + for (d = 0; d < 6; d++) { + GWorldPtr into = 0; + NewGWorld(&into, to[d], &all, 0, 0, 0); + if (!into) continue; + SetGWorld((CGrafPtr)into, 0); + LockPixels(GetGWorldPixMap(into)); + EraseRect(&all); + CopyBits((BitMap *)*GetGWorldPixMap(from), (BitMap *)*GetGWorldPixMap(into), &all, &all, srcCopy, 0); + sprintf(label, "CopyBits %d to %d", to[s], to[d]); dumpPixels(label, into); + SetGWorld((CGrafPtr)&port, screen); + DisposeGWorld(into); + } + DisposeGWorld(from); + } + + for (d = 0; d < 6; d++) { + GWorldPtr into = 0; + RGBColor color; + NewGWorld(&into, to[d], &all, 0, 0, 0); + if (!into) continue; + SetGWorld((CGrafPtr)into, 0); + LockPixels(GetGWorldPixMap(into)); + EraseRect(&all); + CopyBits(&map, (BitMap *)*GetGWorldPixMap(into), &all, &all, srcCopy, 0); + sprintf(label, "CopyBits BitMap to %d", to[d]); dumpPixels(label, into); + color = rgb(0xFFFF, 0, 0); RGBForeColor(&color); + color = rgb(0, 0, 0xFFFF); RGBBackColor(&color); + CopyBits(&map, (BitMap *)*GetGWorldPixMap(into), &all, &all, srcCopy, 0); + sprintf(label, "CopyBits BitMap to %d, red on blue", to[d]); dumpPixels(label, into); + SetGWorld((CGrafPtr)&port, screen); + DisposeGWorld(into); + } +} + +static void updated(const char *label, GWorldPtr *world, short depth, Rect bounds, GWorldFlags flags) +{ + GWorldPtr before = *world; + PixMapHandle map = GetGWorldPixMap(*world); + Ptr base = GetPixBaseAddr(map); + GWorldFlags result; + char more[100]; + result = UpdateGWorld(world, depth, &bounds, 0, 0, flags); + start("UpdateGWorld %s", label); + add(" 0x%lX error %d", (unsigned long)result, QDError()); + if (*world) { + PixMapHandle now = GetGWorldPixMap(*world); + add(" world %s pixmap %s base %s", *world == before ? "same" : "new", now == map ? "same" : "new", + GetPixBaseAddr(now) == base ? "same" : "new"); + add(" depth %d rowBytes 0x%04X state 0x%lX", (*now)->pixelSize, (unsigned short)(*now)->rowBytes, + (long)GetPixelsState(now)); + addRect("bounds", &(*now)->bounds); + addRect("portRect", &((CGrafPtr)*world)->portRect); + addRegion("visRgn", ((CGrafPtr)*world)->visRgn); + addRegion("clipRgn", ((CGrafPtr)*world)->clipRgn); + } + end(); + if (*world) { + sprintf(more, "UpdateGWorld %s, pixels", label); + LockPixels(GetGWorldPixMap(*world)); + dumpPixels(more, *world); + UnlockPixels(GetGWorldPixMap(*world)); + } +} + +static void updating(void) +{ + static const struct { const char *name; GWorldFlags flags; } ways[] = { + { "no flags", 0 }, { "clipPix", clipPix }, { "stretchPix", stretchPix }, + }; + char label[100]; + int w; + for (w = 0; w < 3; w++) { + GWorldPtr world = striped(8); + if (!world) return; + UnlockPixels(GetGWorldPixMap(world)); + sprintf(label, "with the same, %s", ways[w].name); + updated(label, &world, 8, rect(0, 0, 4, 8), ways[w].flags); + sprintf(label, "offset, %s", ways[w].name); + updated(label, &world, 8, rect(1, 2, 5, 10), ways[w].flags); + sprintf(label, "larger, %s", ways[w].name); + updated(label, &world, 8, rect(0, 0, 6, 12), ways[w].flags); + sprintf(label, "smaller, %s", ways[w].name); + updated(label, &world, 8, rect(0, 0, 3, 5), ways[w].flags); + sprintf(label, "deeper, %s", ways[w].name); + updated(label, &world, 32, rect(0, 0, 3, 5), ways[w].flags); + sprintf(label, "to depth 3, %s", ways[w].name); + updated(label, &world, 3, rect(0, 0, 3, 5), ways[w].flags); + if (world) DisposeGWorld(world); + } + { + GWorldPtr world = striped(8); + if (!world) return; + SetGWorld((CGrafPtr)world, 0); + updated("of the current world", &world, 16, rect(0, 0, 6, 12), 0); + where("after UpdateGWorld of the current world", world); + SetGWorld((CGrafPtr)&port, screen); + if (world) DisposeGWorld(world); + } +} + +static void last(void) +{ + GWorldPtr world = 0; + Rect all = rect(0, 0, 4, 8); + NewGWorld(&world, 8, &all, 0, 0, 0); + if (!world) return; + start("OffscreenVersion"); add(" %ld", (long)OffscreenVersion()); end(); + start("QDDone"); add(" %d %d", QDDone((GrafPtr)world) != 0, QDDone(0) != 0); end(); + start("PixMap32Bit"); add(" %d", PixMap32Bit(GetGWorldPixMap(world)) != 0); end(); + SetGWorld((CGrafPtr)world, 0); + DisposeGWorld(world); + where("after DisposeGWorld of the current world", 0); +} + +int main(void) +{ + static const unsigned char name[] = "\pGWorld.txt"; + + InitGraf(&qd.thePort); + InitFonts(); + if (Create(name, 0, 'Grph', 'TEXT') != noErr) return 1; + if (FSOpen(name, 0, &out) != noErr) return 2; + OpenPort(&port); + port.portBits.baseAddr = (Ptr)bits; + port.portBits.rowBytes = 4; + port.portBits.bounds = rect(0, 0, 16, 32); + port.portRect = port.portBits.bounds; + RectRgn(port.visRgn, &port.portRect); + screen = GetMainDevice(); + + start("main device"); + add(" depth %d type %d flags 0x%04X", (*(*screen)->gdPMap)->pixelSize, (*screen)->gdType, + (unsigned short)(*screen)->gdFlags); + end(); + + creation(); + widths(); + flagsAndErrors(); + switching(); + drawing(); + copying(); + updating(); + last(); + + start("done"); end(); + FSClose(out); + FlushVol(0, 0); + return 0; +} diff --git a/ReferenceTests/Retro68/README.md b/ReferenceTests/Retro68/README.md index f7c7c5f..70fce6d 100644 --- a/ReferenceTests/Retro68/README.md +++ b/ReferenceTests/Retro68/README.md @@ -705,3 +705,39 @@ ran from the Finder on Mac OS 9.2.2 in UTM. It writes `Text.txt`, kept here as `TextCaptureTests` makes the probe's calls through libQuickDraw and compares every line. Three differ: a pixel at the right edge of bold condensed text, and a few pixels of the two pictures shrunk to 9 points. + +## GWorlds + +`GWorldCapture.c` records what `NewGWorld` makes at every depth (the port, +pixel map, color table and device), rowBytes for a range of widths, its flags +and errors, `SetGWorld` and `SetPort`, the `LockPixels` calls, drawing and the +color calls in a world, `CopyBits` between every pair of depths and +`UpdateGWorld`. It ran from the Finder on Mac OS 9.2.2 in UTM, with a screen 32 +bits deep. It writes `GWorld.txt`, kept here as `GWorld-macos-9.2.2-ppc.txt` +(CR line endings). + +- A row was its pixels rounded up to a long, four bytes more, rounded up to 16. + The pixel map's rowBytes had its high bit set. +- The port was a `CGrafPort`: portVersion `0xC001`, pnLocHFrac `0x8000`. An + indexed world's color table had the depth as its seed, flags `0x8000` and + zero entry values; a direct world's had a size of 0. +- A depth of 3 gave `cDepthErr` (-157); -1, empty bounds and `noNewDevice` + without a device gave `paramErr`. 24 made a world of the screen's depth, as 0 + did. +- `SetGWorld` of a world used the world's device, whatever was passed. + `SetPort` left the device alone. +- `UpdateGWorld` returned flags saying what changed; a new size with neither + `clipPix` nor `stretchPix` returned `stretchPix`. +- A color matched the table with each component cut to 4 bits, unless it was in + the table exactly. A table of grays only matched by luminance. +- Where the foreground and background colors differed but had the same pixel + value, the foreground drew as the inverse of that value. + +`GWorldCaptureTests` makes the probe's calls through libQuickDraw and compares +every line, leaving out what differs by design: the pixel map's reserved long, +its baseAddr before `LockPixels`, and whether `UpdateGWorld` made a new world +or pixel map. 15 lines differ, and the test lists them: the main device and +what depths 0 and 24 make, as the screen here is 1 bit deep; the pixels +`UpdateGWorld` leaves with no flags or stretches with `stretchPix`; and the +flags it returns for smaller bounds. + diff --git a/Sources/CQuickDraw/include/Quickdraw.h b/Sources/CQuickDraw/include/Quickdraw.h index 3829a85..08955c3 100644 --- a/Sources/CQuickDraw/include/Quickdraw.h +++ b/Sources/CQuickDraw/include/Quickdraw.h @@ -399,6 +399,42 @@ typedef PixPat* PixPatPtr; typedef PixPatPtr* PixPatHandle; +#if __SIZEOF_POINTER__ == 8 +struct CGrafPort { + INTEGER device; + PixMapHandle portPixMap; + INTEGER portVersion; + INTEGER chExtra; + INTEGER pnLocHFrac; + Handle grafVars; + Rect portRect; + RgnHandle visRgn; + RgnHandle clipRgn; + PixPatHandle bkPixPat; + RGBColor rgbFgColor; + RGBColor rgbBkColor; + Point pnLoc; + Point pnSize; + INTEGER pnMode; + PixPatHandle pnPixPat; + PixPatHandle fillPixPat; + INTEGER pnVis; + INTEGER txFont; + Style txFace; + Byte filler; + INTEGER txMode; + INTEGER txSize; + Fixed spExtra; + LONGINT fgColor; + LONGINT bkColor; + INTEGER colrBit; + INTEGER patStretch; + Handle picSave; + Handle rgnSave; + Handle polySave; + CQDProcsPtr grafProcs; +}; +#else struct CGrafPort { INTEGER device; PixMapHandle portPixMap; @@ -433,6 +469,7 @@ struct CGrafPort { Handle polySave; CQDProcsPtr grafProcs; }; +#endif typedef CGrafPort* CGrafPtr; @@ -838,12 +875,43 @@ typedef SProcRec* SProcPtr; typedef SProcPtr* SProcHndl; +struct SProcRec { + SProcHndl nxtSrch; + ProcPtr srchProc; +}; + typedef CProcRec* CProcPtr; typedef CProcPtr* CProcHndl; +struct CProcRec { + CProcHndl nxtComp; + ProcPtr compProc; +}; + typedef void (*DeviceLoopDrawingUPP)(INTEGER, INTEGER, GDHandle, LONGINT); +struct GDevice { + INTEGER gdRefNum; + INTEGER gdID; + INTEGER gdType; + ITabHandle gdITable; + INTEGER gdResPref; + SProcHndl gdSearchProc; + CProcHndl gdCompProc; + INTEGER gdFlags; + PixMapHandle gdPMap; + LONGINT gdRefCon; + GDHandle gdNextGD; + Rect gdRect; + LONGINT gdMode; + INTEGER gdCCBytes; + INTEGER gdCCDepth; + Handle gdCCXData; + Handle gdCCXMask; + LONGINT gdReserved; +}; + typedef uint32_t DeviceLoopFlags; enum { diff --git a/Sources/CQuickDraw/stubs.c b/Sources/CQuickDraw/stubs.c index 4653786..174b166 100644 --- a/Sources/CQuickDraw/stubs.c +++ b/Sources/CQuickDraw/stubs.c @@ -255,22 +255,6 @@ INTEGER StdTxMeas(INTEGER n, Ptr p, Point * nump, Point * denp, FontInfo * finfo return none; } -void GetCPixel(INTEGER h, INTEGER v, RGBColor * colorp) -{ - (void) h; - (void) v; - (void) colorp; - cquickdraw_unimplemented("GetCPixel"); -} - -void SetCPixel(INTEGER h, INTEGER v, RGBColor * colorp) -{ - (void) h; - (void) v; - (void) colorp; - cquickdraw_unimplemented("SetCPixel"); -} - void SeedCFill(BitMap * srcbp, BitMap * dstbp, const Rect * srcrp, const Rect * dstrp, INTEGER seedh, INTEGER seedv, ProcPtr matchprocp, LONGINT matchdata) { (void) srcbp; @@ -399,30 +383,6 @@ void CloseCPort(CGrafPtr p0) cquickdraw_unimplemented("CloseCPort"); } -void RGBForeColor(RGBColor * p0) -{ - (void) p0; - cquickdraw_unimplemented("RGBForeColor"); -} - -void RGBBackColor(RGBColor * p0) -{ - (void) p0; - cquickdraw_unimplemented("RGBBackColor"); -} - -void GetForeColor(RGBColor * p0) -{ - (void) p0; - cquickdraw_unimplemented("GetForeColor"); -} - -void GetBackColor(RGBColor * p0) -{ - (void) p0; - cquickdraw_unimplemented("GetBackColor"); -} - void PenPixPat(PixPatHandle p0) { (void) p0; @@ -518,39 +478,12 @@ void SetDeviceAttribute(GDHandle p0, INTEGER p1, Boolean p2) cquickdraw_unimplemented("SetDeviceAttribute"); } -void SetGDevice(GDHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("SetGDevice"); -} - void DisposeGDevice(GDHandle p0) { (void) p0; cquickdraw_unimplemented("DisposeGDevice"); } -GDHandle GetGDevice(void) -{ - cquickdraw_unimplemented("GetGDevice"); - static GDHandle none; - return none; -} - -GDHandle GetDeviceList(void) -{ - cquickdraw_unimplemented("GetDeviceList"); - static GDHandle none; - return none; -} - -GDHandle GetMainDevice(void) -{ - cquickdraw_unimplemented("GetMainDevice"); - static GDHandle none; - return none; -} - GDHandle GetMaxDevice(const Rect * p0) { (void) p0; @@ -559,14 +492,6 @@ GDHandle GetMaxDevice(const Rect * p0) return none; } -GDHandle GetNextDevice(GDHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("GetNextDevice"); - static GDHandle none; - return none; -} - void DeviceLoop(RgnHandle p0, DeviceLoopDrawingUPP p1, LONGINT p2, DeviceLoopFlags p3) { (void) p0; @@ -1105,101 +1030,6 @@ void GetCWMgrPort(CGrafPtr * p0) cquickdraw_unimplemented("GetCWMgrPort"); } -QDErr NewGWorld(GWorldPtr * p0, INTEGER p1, const Rect * p2, CTabHandle p3, GDHandle p4, GWorldFlags p5) -{ - (void) p0; - (void) p1; - (void) p2; - (void) p3; - (void) p4; - (void) p5; - cquickdraw_unimplemented("NewGWorld"); - static QDErr none; - return none; -} - -Boolean LockPixels(PixMapHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("LockPixels"); - static Boolean none; - return none; -} - -void UnlockPixels(PixMapHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("UnlockPixels"); -} - -GWorldFlags UpdateGWorld(GWorldPtr * p0, INTEGER p1, const Rect * p2, CTabHandle p3, GDHandle p4, GWorldFlags p5) -{ - (void) p0; - (void) p1; - (void) p2; - (void) p3; - (void) p4; - (void) p5; - cquickdraw_unimplemented("UpdateGWorld"); - static GWorldFlags none; - return none; -} - -void DisposeGWorld(GWorldPtr p0) -{ - (void) p0; - cquickdraw_unimplemented("DisposeGWorld"); -} - -void GetGWorld(CGrafPtr * p0, GDHandle * p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("GetGWorld"); -} - -void SetGWorld(CGrafPtr p0, GDHandle p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("SetGWorld"); -} - -void AllowPurgePixels(PixMapHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("AllowPurgePixels"); -} - -void NoPurgePixels(PixMapHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("NoPurgePixels"); -} - -GWorldFlags GetPixelsState(PixMapHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("GetPixelsState"); - static GWorldFlags none; - return none; -} - -void SetPixelsState(PixMapHandle p0, GWorldFlags p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("SetPixelsState"); -} - -Ptr GetPixBaseAddr(PixMapHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("GetPixBaseAddr"); - static Ptr none; - return none; -} - QDErr NewScreenBuffer(const Rect * p0, Boolean p1, GDHandle * p2, PixMapHandle * p3) { (void) p0; @@ -1217,30 +1047,6 @@ void DisposeScreenBuffer(PixMapHandle p0) cquickdraw_unimplemented("DisposeScreenBuffer"); } -GDHandle GetGWorldDevice(GWorldPtr p0) -{ - (void) p0; - cquickdraw_unimplemented("GetGWorldDevice"); - static GDHandle none; - return none; -} - -Boolean PixMap32Bit(PixMapHandle p0) -{ - (void) p0; - cquickdraw_unimplemented("PixMap32Bit"); - static Boolean none; - return none; -} - -PixMapHandle GetGWorldPixMap(GWorldPtr p0) -{ - (void) p0; - cquickdraw_unimplemented("GetGWorldPixMap"); - static PixMapHandle none; - return none; -} - QDErr NewTempScreenBuffer(const Rect * p0, Boolean p1, GDHandle * p2, PixMapHandle * p3) { (void) p0; @@ -1276,21 +1082,6 @@ void CTabChanged(CTabHandle p0) cquickdraw_unimplemented("CTabChanged"); } -Boolean QDDone(GrafPtr p0) -{ - (void) p0; - cquickdraw_unimplemented("QDDone"); - static Boolean none; - return none; -} - -LONGINT OffscreenVersion(void) -{ - cquickdraw_unimplemented("OffscreenVersion"); - static LONGINT none; - return none; -} - LONGINT Entry2Index(INTEGER p0) { (void) p0; diff --git a/Sources/Graphite2D/ColorTable.swift b/Sources/Graphite2D/ColorTable.swift index 7bb2fdb..2cc2210 100644 --- a/Sources/Graphite2D/ColorTable.swift +++ b/Sources/Graphite2D/ColorTable.swift @@ -50,6 +50,17 @@ public struct RGBColor: Hashable, Sendable { public var green8: UInt8 { UInt8(green >> 8) } public var blue8: UInt8 { UInt8(blue >> 8) } + /// Each component's top four bits, repeated. + var coarse: RGBColor { + RGBColor(red: (red >> 12) * 0x1111, green: (green >> 12) * 0x1111, blue: (blue >> 12) * 0x1111) + } + + /// The gray of the same luminance, by the weights 0.30, 0.59 and 0.11. + var gray: RGBColor { + let level = UInt16((Int(red) * 30 + Int(green) * 59 + Int(blue) * 11) / 100) + return RGBColor(red: level, green: level, blue: level) + } + /// How far apart two colors are, for finding the nearest in a table. func distance(to other: RGBColor) -> Int { let r = Int(red >> 8) - Int(other.red >> 8) @@ -89,6 +100,11 @@ public struct ColorTable: Hashable, Sendable { /// are reserved. public func index(of color: RGBColor, excluding reserved: Set) -> Int { if let exact = colors.indices.first(where: { colors[$0] == color && !reserved.contains($0) }) { return exact } + // As an inverse table of 4 bits a component finds it, but in a table of + // grays alone by luminance: Mac OS 9.2.2 gave green the lighter gray of + // the 2 bit table. + let grays = colors.allSatisfy { $0.red == $0.green && $0.green == $0.blue } + let color = grays ? color.gray : color.coarse var best = 0 var bestDistance = Int.max for (index, candidate) in colors.enumerated() where !reserved.contains(index) { @@ -107,7 +123,7 @@ public struct ColorTable: Hashable, Sendable { switch depth { case 1: return ColorTable(seed: 1, colors: [.white, .black]) case 2: return ColorTable(seed: 2, colors: [ - .white, RGBColor(red: 0xAAAA, green: 0xAAAA, blue: 0xAAAA), RGBColor(red: 0x5555, green: 0x5555, blue: 0x5555), .black, + .white, RGBColor(red: 0xACAC, green: 0xACAC, blue: 0xACAC), RGBColor(red: 0x5555, green: 0x5555, blue: 0x5555), .black, ]) case 4: return standard4 case 8: return standard8 @@ -115,13 +131,14 @@ public struct ColorTable: Hashable, Sendable { } } - /// The 16 colors of the system's 4-bit table. + /// The 16 colors of the system's 4-bit table, as Mac OS 9.2.2 gave them + /// (ReferenceTests/Retro68/GWorld-macos-9.2.2-ppc.txt). private static let standard4 = ColorTable(seed: 4, colors: [ - (0xFF, 0xFF, 0xFF), (0xFC, 0xF3, 0x05), (0xFF, 0x64, 0x02), (0xDD, 0x08, 0x06), - (0xF2, 0x08, 0x84), (0x46, 0x00, 0xA5), (0x00, 0x00, 0xD4), (0x02, 0xAB, 0xEA), - (0x1F, 0xB7, 0x14), (0x00, 0x64, 0x11), (0x56, 0x2C, 0x05), (0x90, 0x71, 0x3A), - (0xC0, 0xC0, 0xC0), (0x80, 0x80, 0x80), (0x40, 0x40, 0x40), (0x00, 0x00, 0x00), - ].map { RGBColor(red8: $0.0, green8: $0.1, blue8: $0.2) }) + (0xFFFF, 0xFFFF, 0xFFFF), (0xFC00, 0xF37D, 0x052F), (0xFFFF, 0x648A, 0x028C), (0xDD6B, 0x08C2, 0x06A2), + (0xF2D7, 0x0856, 0x84EC), (0x46E3, 0x0000, 0xA53E), (0x0000, 0x0000, 0xD400), (0x0241, 0xAB54, 0xEAFF), + (0x1F21, 0xB793, 0x1431), (0x0000, 0x64AF, 0x11B0), (0x5600, 0x2C9D, 0x0524), (0x90D7, 0x7160, 0x3A34), + (0xC000, 0xC000, 0xC000), (0x8000, 0x8000, 0x8000), (0x4000, 0x4000, 0x4000), (0x0000, 0x0000, 0x0000), + ].map { RGBColor(red: $0.0, green: $0.1, blue: $0.2) }) /// The 256 colors of the system's 8-bit table: a 6×6×6 cube from white /// down without its black, then ramps of ten reds, greens, blues and diff --git a/Sources/Graphite2D/CopyBits.swift b/Sources/Graphite2D/CopyBits.swift index e6e4cb4..a2bf343 100644 --- a/Sources/Graphite2D/CopyBits.swift +++ b/Sources/Graphite2D/CopyBits.swift @@ -27,6 +27,7 @@ public extension Port { if let mask { area = area.intersection(mask) } guard !area.isEmpty, !sourceRect.isEmpty, !destinationRect.isEmpty else { return } let isBitmap = source.format.depth == 1 + let ink = inkColor // A copy within the same memory reads it as it was. let source = source.baseAddress == pixels.baseAddress ? source.duplicate() : source // The same pixels, unscaled, in the same format and table: copied as @@ -49,9 +50,9 @@ public extension Port { if isBitmap { // The arithmetic modes see the colorized pixel; the // boolean ones only whether it is set. - let drawn = mode.booleanOperation != nil || value != 0 ? foreColor : backColor + let drawn = mode.booleanOperation != nil || value != 0 ? ink : backColor color = mode.combine(isForeground: value != 0, drawn: drawn, existing: existing, - fore: foreColor, back: backColor, opColor: opColor) + fore: ink, back: backColor, opColor: opColor) } else { color = combine(source.color(for: value), existing: existing, mode: mode) } diff --git a/Sources/Graphite2D/Drawing.swift b/Sources/Graphite2D/Drawing.swift index 6af0d14..8bb7a6f 100644 --- a/Sources/Graphite2D/Drawing.swift +++ b/Sources/Graphite2D/Drawing.swift @@ -32,10 +32,21 @@ public extension Port { draw(clipped: region.intersection(drawableRegion), pattern: pattern, mode: mode) } + /// The foreground color as indexed pixels show it: if it and a different + /// background color have the same pixel value, the inverse of that value, + /// as Color QuickDraw kept them apart. + internal var inkColor: RGBColor { + guard let pixels, pixels.format.isIndexed, foreColor != backColor else { return foreColor } + let back = pixels.value(for: backColor) + guard pixels.value(for: foreColor) == back else { return foreColor } + return pixels.color(for: ~back & (1 << UInt32(pixels.format.depth) - 1)) + } + /// Draws a region already clipped to what may be drawn, which must lie /// within the pixels. internal func draw(clipped area: Region, pattern: Pattern, mode: TransferMode) { guard let pixels, !area.isEmpty else { return } + let foreColor = inkColor // The fast case: one color copied over what is there. if mode.base == .patCopy, pattern.isSolid { let value = pixels.value(for: pattern.rows[0] == 0 ? backColor : foreColor) diff --git a/Sources/Graphite2D/GraphicsDevice.swift b/Sources/Graphite2D/GraphicsDevice.swift index 7288774..0ce7615 100644 --- a/Sources/Graphite2D/GraphicsDevice.swift +++ b/Sources/Graphite2D/GraphicsDevice.swift @@ -78,9 +78,9 @@ public struct GraphicsWorldFlags: OptionSet, Hashable, Sendable { /// pixelsLocked. public static var pixelsLocked: GraphicsWorldFlags { GraphicsWorldFlags(rawValue: 1 << 7) } /// clipPix: UpdateGWorld keeps the pixels, clipped to the new bounds. - public static var clipPixels: GraphicsWorldFlags { GraphicsWorldFlags(rawValue: 1 << 0) } + public static var clipPixels: GraphicsWorldFlags { GraphicsWorldFlags(rawValue: 1 << 28) } /// stretchPix: UpdateGWorld scales the pixels to the new bounds. - public static var stretchPixels: GraphicsWorldFlags { GraphicsWorldFlags(rawValue: 1 << 1) } + public static var stretchPixels: GraphicsWorldFlags { GraphicsWorldFlags(rawValue: 1 << 29) } } public final class GraphicsWorld { diff --git a/Sources/Graphite2D/PixelBuffer.swift b/Sources/Graphite2D/PixelBuffer.swift index 1949e55..3bf52be 100644 --- a/Sources/Graphite2D/PixelBuffer.swift +++ b/Sources/Graphite2D/PixelBuffer.swift @@ -78,6 +78,10 @@ public final class PixelBuffer { /// because a program keeps them for its own use (ReserveEntry). public var reservedEntries: Set = [] + /// The high byte a color is stored with in 32-bit pixels: opaque, for a + /// host that composites them; Mac OS left it 0. + public var alphaByte: UInt8 = 0xFF + /// Whether the memory is the buffer's to free. private let ownsMemory: Bool @@ -164,12 +168,12 @@ public final class PixelBuffer { // MARK: Colors /// A color as this buffer stores it: the nearest index in its table, or - /// its bits; 32-bit pixels opaque. + /// its bits; 32-bit pixels with `alphaByte`. public func value(for color: RGBColor) -> UInt32 { switch format { case .indexed: UInt32(colorTable.index(of: color, excluding: reservedEntries)) case .rgb555: UInt32(color.red >> 11) << 10 | UInt32(color.green >> 11) << 5 | UInt32(color.blue >> 11) - case .argb32: 0xFF00_0000 | UInt32(color.red8) << 16 | UInt32(color.green8) << 8 | UInt32(color.blue8) + case .argb32: UInt32(alphaByte) << 24 | UInt32(color.red8) << 16 | UInt32(color.green8) << 8 | UInt32(color.blue8) } } diff --git a/Sources/GraphiteQuickDrawABI/BitmapAPI.swift b/Sources/GraphiteQuickDrawABI/BitmapAPI.swift index 2db5315..c3ed6ed 100644 --- a/Sources/GraphiteQuickDrawABI/BitmapAPI.swift +++ b/Sources/GraphiteQuickDrawABI/BitmapAPI.swift @@ -30,7 +30,13 @@ public func BitMapToRegion(_ p0: RgnHandle?, _ p1: UnsafePointer?) { + guard let p0 else { return fail() } + setColor(Graphite2D.RGBColor(p0.pointee), foreground: true) +} + +@c @implementation +public func RGBBackColor(_ p0: UnsafeMutablePointer?) { + guard let p0 else { return fail() } + setColor(Graphite2D.RGBColor(p0.pointee), foreground: false) +} + +@c @implementation +public func GetForeColor(_ p0: UnsafeMutablePointer?) { + guard let p0, let port = currentPort() else { return fail() } + p0.pointee = port.isColor ? port.color.pointee.rgbFgColor + : (Graphite2D.RGBColor(classic: port.fgColor) ?? .black).c +} + +@c @implementation +public func GetBackColor(_ p0: UnsafeMutablePointer?) { + guard let p0, let port = currentPort() else { return fail() } + p0.pointee = port.isColor ? port.color.pointee.rgbBkColor + : (Graphite2D.RGBColor(classic: port.bkColor) ?? .white).c +} + +/// The color of the pixel right of and below a point, in local coordinates. +@c @implementation +public func GetCPixel(_ h: Int16, _ v: Int16, _ colorp: UnsafeMutablePointer?) { + guard let colorp, let port = currentPort() else { return fail() } + guard let color = enginePort(port).pixels?.color(at: GraphiteCore.Point(v: v, h: h)) else { return fail() } + colorp.pointee = color.c +} + +/// Paints one pixel in a color, as clipped as any drawing; the port's own +/// colors and pen stay. +@c @implementation +public func SetCPixel(_ h: Int16, _ v: Int16, _ colorp: UnsafeMutablePointer?) { + guard let colorp else { return fail() } + let color = Graphite2D.RGBColor(colorp.pointee) + let rect = GraphiteCore.Rect(top: v, left: h, bottom: v &+ 1, right: h &+ 1) + drawing { + $0.foreColor = color + $0.perform(.fill(.black), rect: rect) + } +} diff --git a/Sources/GraphiteQuickDrawABI/CopyBitsAPI.swift b/Sources/GraphiteQuickDrawABI/CopyBitsAPI.swift index 6d3be3a..1e4a0f5 100644 --- a/Sources/GraphiteQuickDrawABI/CopyBitsAPI.swift +++ b/Sources/GraphiteQuickDrawABI/CopyBitsAPI.swift @@ -1,4 +1,4 @@ -// CopyBits and CopyMask between black and white bitmaps. A destination with +// CopyBits and CopyMask between bitmaps and pixel maps. A destination with // the current port's pixels is drawn into as the port: within its clip and // visible regions, and not with its pen hidden. Another bitmap is limited // only by its bounds and the mask region. Either takes the port's colors. @@ -9,16 +9,16 @@ import Graphite2D /// The port to copy into `bits` through; nil, with QDError set, if there is /// none to be had. -private func destination(_ bits: CBitMap) -> Graphite2D.Port? { +private func destination(_ bits: UnsafePointer) -> Graphite2D.Port? { guard let record = currentPort() else { return nil } - let current = enginePort(record.pointee) - if bits.baseAddr == record.pointee.portBits.baseAddr { - return current.isPenVisible ? current : nil - } + let current = enginePort(record) guard let buffer = pixels(of: bits) else { fail() return nil } + if buffer.baseAddress == current.pixels?.baseAddress { + return current.isPenVisible ? current : nil + } let port = Graphite2D.Port(pixels: buffer) port.foreColor = current.foreColor port.backColor = current.backColor @@ -30,13 +30,13 @@ public func CopyBits(_ src_bitmap: UnsafeMutablePointer?, _ d _ src_rect: UnsafePointer?, _ dst_rect: UnsafePointer?, _ mode: Int16, _ mask: RgnHandle?) { guard let src_bitmap, let dst_bitmap, let src_rect, let dst_rect, - let source = pixels(of: src_bitmap.pointee) else { return fail() } + let source = pixels(of: src_bitmap) else { return fail() } var region: Graphite2D.Region? if let mask { guard let loaded = RegionStorage.load(mask) else { return fail() } region = loaded } - guard let port = destination(dst_bitmap.pointee) else { return } + guard let port = destination(dst_bitmap) else { return } port.copyBits(from: source, sourceRect: GraphiteCore.Rect(src_rect.pointee), destinationRect: GraphiteCore.Rect(dst_rect.pointee), mode: TransferMode(rawValue: mode), mask: region) } @@ -46,8 +46,8 @@ public func CopyMask(_ srcbp: UnsafeMutablePointer?, _ mskbp: _ dstbp: UnsafeMutablePointer?, _ srcrp: UnsafePointer?, _ mskrp: UnsafePointer?, _ dstrp: UnsafePointer?) { guard let srcbp, let mskbp, let dstbp, let srcrp, let mskrp, let dstrp, - let source = pixels(of: srcbp.pointee), let mask = pixels(of: mskbp.pointee) else { return fail() } - guard let port = destination(dstbp.pointee) else { return } + let source = pixels(of: srcbp), let mask = pixels(of: mskbp) else { return fail() } + guard let port = destination(dstbp) else { return } port.copyMask(from: source, mask: mask, sourceRect: GraphiteCore.Rect(srcrp.pointee), maskRect: GraphiteCore.Rect(mskrp.pointee), destinationRect: GraphiteCore.Rect(dstrp.pointee)) } diff --git a/Sources/GraphiteQuickDrawABI/GWorldAPI.swift b/Sources/GraphiteQuickDrawABI/GWorldAPI.swift new file mode 100644 index 0000000..9c68785 --- /dev/null +++ b/Sources/GraphiteQuickDrawABI/GWorldAPI.swift @@ -0,0 +1,247 @@ +// Graphics worlds and devices. The flags, errors and rules are those Mac OS +// 9.2.2 showed (ReferenceTests/Retro68/GWorld-macos-9.2.2-ppc.txt). A world's +// pixels never move or purge, and UpdateGWorld keeps the world and its pixel +// map where they are. + +import CQuickDraw +import GraphiteCore +import Graphite2D + +private enum Flag { + static let pixPurge: UInt32 = 1 << 0 + static let noNewDevice: UInt32 = 1 << 1 + static let pixelsPurgeable: UInt32 = 1 << 6 + static let pixelsLocked: UInt32 = 1 << 7 + static let mapPix: UInt32 = 1 << 16 + static let newDepth: UInt32 = 1 << 17 + static let alignPix: UInt32 = 1 << 18 + static let newRowBytes: UInt32 = 1 << 19 + static let reallocPix: UInt32 = 1 << 20 + static let clipPix: UInt32 = 1 << 28 + static let stretchPix: UInt32 = 1 << 29 +} + +/// cDepthErr. +private let depthError: Int16 = -157 + +/// The depth NewGWorld makes for the one asked: 0 and, as Mac OS 9.2.2 did, +/// 24 for the device's; or the error. +private func resolve(_ depth: Int16, device: GDHandle?) -> (depth: Int, error: Int16) { + switch depth { + case 0, 24: return (Devices.depth(of: device ?? Devices.main), 0) + case 1, 2, 4, 8, 16, 32: return (Int(depth), 0) + default: return (0, depth < 0 ? QuickDrawError.parameter : depthError) + } +} + +private func result(_ code: Int16) -> Int16 { + cquickdraw_set_error(code) + return code +} + +@c @implementation +public func NewGWorld(_ p0: UnsafeMutablePointer?, _ p1: Int16, _ p2: UnsafePointer?, + _ p3: CTabHandle?, _ p4: GDHandle?, _ p5: Int32) -> Int16 { + guard let p0, let p2 else { return result(QuickDrawError.parameter) } + let flags = UInt32(truncatingIfNeeded: p5) + p0.pointee = nil + let device = flags & Flag.noNewDevice != 0 ? p4 : nil + if flags & Flag.noNewDevice != 0, p4 == nil { return result(QuickDrawError.parameter) } + let (depth, error) = resolve(p1, device: p4) + if error != 0 { return result(error) } + if GraphiteCore.Rect(p2.pointee).isEmpty { return result(QuickDrawError.parameter) } + guard let storage = MapStorage(bounds: p2.pointee, depth: depth, colorTable: p3) else { + return result(QuickDrawError.parameter) + } + let world = World(storage: storage, device: device) + if flags & Flag.pixPurge != 0 { world.state = Flag.pixelsPurgeable } + Worlds.add(world) + p0.pointee = world.port + return result(QuickDrawError.none) +} + +@c @implementation +public func DisposeGWorld(_ p0: GWorldPtr?) { + guard let p0, let world = Worlds.world(UnsafeMutableRawPointer(p0)) else { return fail() } + let port = GrafPtr(OpaquePointer(p0)) + // With its world gone there is no current port, and drawing does nothing + // until one is set. + if CurrentPort.port == port { CurrentPort.port = nil } + if Devices.current == world.device { Devices.current = Devices.main } + Recordings.forget(port) + Worlds.remove(world) + world.free() + succeed() +} + +/// Remakes a world's pixels for other bounds or another depth or color table. +/// The result says what changed, or is the error. With neither clipPix nor +/// stretchPix the Macintosh left new pixels as it found the memory; here they +/// keep what they had, as clipPix does. +@c @implementation +public func UpdateGWorld(_ p0: UnsafeMutablePointer?, _ p1: Int16, _ p2: UnsafePointer?, + _ p3: CTabHandle?, _ p4: GDHandle?, _ p5: Int32) -> Int32 { + func failed(_ code: Int16) -> Int32 { + cquickdraw_set_error(code) + return Int32(code) + } + guard let p0, let port = p0.pointee, let world = Worlds.world(UnsafeMutableRawPointer(port)), let p2 else { + return failed(QuickDrawError.parameter) + } + let (depth, error) = resolve(p1, device: p4) + if error != 0 { return failed(error) } + let new = GraphiteCore.Rect(p2.pointee) + if new.isEmpty { return failed(QuickDrawError.parameter) } + + let record = world.storage.map.pointee!.pointee + let old = GraphiteCore.Rect(record.bounds) + let flags = UInt32(truncatingIfNeeded: p5) + let sized = new.width != old.width || new.height != old.height + let deepened = depth != Int(record.pixelSize) + var changes: UInt32 = 0 + if deepened { changes |= Flag.mapPix | Flag.newDepth } + if p3 != nil { changes |= Flag.mapPix } + if new.left != old.left || new.top != old.top { changes |= Flag.alignPix } + if sized { changes |= flags & Flag.clipPix != 0 ? Flag.clipPix : Flag.stretchPix } + + let ref = PortRef(GrafPtr(OpaquePointer(port))) + if sized || deepened || p3 != nil { + guard let storage = MapStorage(bounds: p2.pointee, depth: depth, colorTable: p3), + let source = pixels(ofMap: world.storage.map.pointee!), let destination = pixels(ofMap: storage.map.pointee!) else { + return failed(QuickDrawError.parameter) + } + changes |= Flag.reallocPix + if storage.map.pointee!.pointee.rowBytes != record.rowBytes { changes |= Flag.newRowBytes } + // Top left to top left, whatever the coordinates, unless stretched. + let into = Graphite2D.Port(pixels: destination) + let from = flags & Flag.stretchPix != 0 ? old + : GraphiteCore.Rect(top: old.top, left: old.left, + bottom: old.top &+ Int16(truncatingIfNeeded: new.height), + right: old.left &+ Int16(truncatingIfNeeded: new.width)) + into.copyBits(from: source, sourceRect: from, destinationRect: new) + world.replace(storage) + } else { + ref.bounds = p2.pointee + world.deviceChanged() + } + ref.portRect = p2.pointee + if let visible = ref.visRgn { RegionStorage.store(Graphite2D.Region(rect: new), in: visible) } + cquickdraw_set_error(QuickDrawError.none) + return Int32(bitPattern: changes) +} + +@c @implementation +public func GetGWorld(_ p0: UnsafeMutablePointer?, _ p1: UnsafeMutablePointer?) { + p0?.pointee = CurrentPort.port.map { UnsafeMutableRawPointer($0).assumingMemoryBound(to: CQuickDraw.CGrafPort.self) } + p1?.pointee = Devices.current + succeed() +} + +/// A world brings its own device; for another port it is the one given, or +/// the main one. +@c @implementation +public func SetGWorld(_ p0: CGrafPtr?, _ p1: GDHandle?) { + guard let p0 else { return fail() } + CurrentPort.port = GrafPtr(OpaquePointer(p0)) + Devices.current = Worlds.world(UnsafeMutableRawPointer(p0))?.device ?? p1 ?? Devices.main + succeed() +} + +@c @implementation +public func GetGWorldDevice(_ p0: GWorldPtr?) -> GDHandle? { + guard let p0, let world = Worlds.world(UnsafeMutableRawPointer(p0)) else { + fail() + return nil + } + succeed() + return world.device +} + +@c @implementation +public func GetGWorldPixMap(_ p0: GWorldPtr?) -> PixMapHandle? { + guard let p0 else { + fail() + return nil + } + succeed() + return p0.pointee.portPixMap +} + +@c @implementation +public func GetMainDevice() -> GDHandle? { Devices.main } + +@c @implementation +public func GetDeviceList() -> GDHandle? { Devices.main } + +/// There is one screen. +@c @implementation +public func GetNextDevice(_ p0: GDHandle?) -> GDHandle? { nil } + +@c @implementation +public func GetGDevice() -> GDHandle? { Devices.current } + +@c @implementation +public func SetGDevice(_ p0: GDHandle?) { + guard let p0 else { return fail() } + Devices.current = p0 + succeed() +} + +// MARK: Pixels + +private func change(_ map: PixMapHandle?, _ transform: (inout UInt32) -> Void) { + guard let map, let world = Worlds.world(map: map) else { return fail() } + transform(&world.state) + succeed() +} + +/// Pixels here are always where they are, and never purged. +@c @implementation +public func LockPixels(_ p0: PixMapHandle?) -> QDBoolean { + change(p0) { $0 |= Flag.pixelsLocked } + return boolean(p0 != nil) +} + +@c @implementation +public func UnlockPixels(_ p0: PixMapHandle?) { + change(p0) { $0 &= ~Flag.pixelsLocked } +} + +@c @implementation +public func GetPixBaseAddr(_ p0: PixMapHandle?) -> UnsafeMutablePointer? { + p0?.pointee?.pointee.baseAddr +} + +@c @implementation +public func GetPixelsState(_ p0: PixMapHandle?) -> Int32 { + guard let p0, let world = Worlds.world(map: p0) else { + fail() + return 0 + } + succeed() + return Int32(world.state) +} + +@c @implementation +public func SetPixelsState(_ p0: PixMapHandle?, _ p1: Int32) { + change(p0) { $0 = UInt32(truncatingIfNeeded: p1) & (Flag.pixelsLocked | Flag.pixelsPurgeable) } +} + +@c @implementation +public func AllowPurgePixels(_ p0: PixMapHandle?) { + change(p0) { $0 |= Flag.pixelsPurgeable } +} + +@c @implementation +public func NoPurgePixels(_ p0: PixMapHandle?) { + change(p0) { $0 &= ~Flag.pixelsPurgeable } +} + +@c @implementation +public func PixMap32Bit(_ p0: PixMapHandle?) -> QDBoolean { boolean(false) } + +@c @implementation +public func QDDone(_ p0: GrafPtr?) -> QDBoolean { boolean(true) } + +@c @implementation +public func OffscreenVersion() -> Int32 { 336 } diff --git a/Sources/GraphiteQuickDrawABI/GWorldStorage.swift b/Sources/GraphiteQuickDrawABI/GWorldStorage.swift new file mode 100644 index 0000000..caee8c1 --- /dev/null +++ b/Sources/GraphiteQuickDrawABI/GWorldStorage.swift @@ -0,0 +1,332 @@ +// A graphics world and the screen's device as a C program sees them: a color +// port, its pixel map and color table, and a device, all the library's own. +// The program reads their fields; the handles are not Memory Manager handles, +// and the pixels never move or purge. What they hold is what Mac OS 9.2.2 gave +// (ReferenceTests/Retro68/GWorld-macos-9.2.2-ppc.txt). + +import CQuickDraw +import GraphiteCore +import Graphite2D + +typealias CPixMap = CQuickDraw.PixMap +typealias CColorTable = CQuickDraw.ColorTable +typealias CColorSpec = CQuickDraw.ColorSpec +typealias CRGBColor = CQuickDraw.RGBColor +typealias CGDevice = CQuickDraw.GDevice +typealias CPixPat = CQuickDraw.PixPat + +extension Graphite2D.RGBColor { + init(_ color: CRGBColor) { self.init(red: color.red, green: color.green, blue: color.blue) } + var c: CRGBColor { CRGBColor(red: red, green: green, blue: blue) } +} + +/// A handle: a master pointer and what it points to. +private func newHandle(_ value: T) -> UnsafeMutablePointer?> { + let block = UnsafeMutablePointer.allocate(capacity: 1) + block.initialize(to: value) + let handle = UnsafeMutablePointer?>.allocate(capacity: 1) + handle.initialize(to: block) + return handle +} + +private func dispose(_ handle: UnsafeMutablePointer?>) { + if let block = handle.pointee { UnsafeMutableRawPointer(block).deallocate() } + handle.deallocate() +} + +// MARK: Color tables + +/// Where a ColorTable's entries start: ctSeed, ctFlags and ctSize come first. +private let entriesOffset = 8 + +private func entries(_ table: UnsafeMutablePointer) -> UnsafeMutablePointer { + (UnsafeMutableRawPointer(table) + entriesOffset).assumingMemoryBound(to: CColorSpec.self) +} + +/// A table of the colors, with the flags and seed given. +private func newTable(_ colors: [Graphite2D.RGBColor], flags: UInt16, seed: Int32) -> CTabHandle { + let size = entriesOffset + max(1, colors.count) * MemoryLayout.stride + let memory = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: 8) + memory.initializeMemory(as: UInt8.self, repeating: 0, count: size) + let table = memory.assumingMemoryBound(to: CColorTable.self) + table.pointee.ctSeed = .init(truncatingIfNeeded: seed) + table.pointee.ctFlags = flags + table.pointee.ctSize = Int16(max(0, colors.count - 1)) + for (index, color) in colors.enumerated() { entries(table)[index] = CColorSpec(value: 0, rgb: color.c) } + let handle = CTabHandle.allocate(capacity: 1) + handle.initialize(to: table) + return handle +} + +/// The colors of a table, by pixel value. +private func colors(of handle: CTabHandle?) -> [Graphite2D.RGBColor]? { + guard let table = handle?.pointee else { return nil } + let count = Int(table.pointee.ctSize) + 1 + guard count > 0, count <= 256 else { return nil } + // A device table's entries are in order, whatever their values say. + let isDevice = table.pointee.ctFlags & 0x8000 != 0 + var colors = [Graphite2D.RGBColor](repeating: .black, count: count) + for index in 0 ..< count { + let entry = entries(table)[index] + let slot = isDevice ? index : Int(entry.value) + if slot >= 0, slot < count { colors[slot] = Graphite2D.RGBColor(entry.rgb) } + } + return colors +} + +// MARK: Pixel maps + +/// The pixels a pixel map describes; nil if it has none. +func pixels(ofMap map: UnsafePointer) -> PixelBuffer? { + let bounds = GraphiteCore.Rect(map.pointee.bounds) + let rowBytes = Int(UInt16(bitPattern: map.pointee.rowBytes) & 0x3FFF) + let depth = Int(map.pointee.pixelSize) + guard let base = map.pointee.baseAddr, let format = PixelFormat(depth: depth), !bounds.isEmpty, + rowBytes >= (bounds.width * depth + 7) / 8 else { return nil } + let table = format.isIndexed ? colors(of: map.pointee.pmTable).map { Graphite2D.ColorTable(colors: $0) } : nil + let buffer = PixelBuffer(wrapping: UnsafeMutableRawPointer(base), rowBytes: rowBytes, bounds: bounds, + format: format, colorTable: table) + buffer.alphaByte = 0 + return buffer +} + +/// The bytes a row of a world's pixels takes: its pixels in longs, one long +/// more, in sixteens. +private func rowBytes(width: Int, depth: Int) -> Int { + ((width * depth + 31) / 32 * 4 + 4 + 15) / 16 * 16 +} + +/// A pixel map and the memory and table that go with it. +struct MapStorage { + + let map: PixMapHandle + let memory: UnsafeMutableRawPointer + let table: CTabHandle + + /// White indexed pixels or black direct ones, in a map as NewGWorld + /// makes it. + init?(bounds: CRect, depth: Int, colorTable: CTabHandle?) { + let rect = GraphiteCore.Rect(bounds) + let row = rowBytes(width: rect.width, depth: depth) + // rowBytes shares its word with the flags. + guard row < 0x4000 else { return nil } + let size = row * rect.height + memory = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: 16) + memory.initializeMemory(as: UInt8.self, repeating: 0, count: size) + if depth <= 8 { + let standard = Graphite2D.ColorTable.standard(depth: depth)?.colors ?? [] + let theirs = colors(of: colorTable) + table = newTable(theirs ?? standard, flags: 0x8000, seed: theirs == nil ? Int32(depth) : 1024) + } else { + table = newTable([.black], flags: 0, seed: 0) + table.pointee!.pointee.ctSize = 0 + } + var record = CPixMap() + record.baseAddr = memory.assumingMemoryBound(to: CChar.self) + record.rowBytes = Int16(bitPattern: UInt16(row) | 0x8000) + record.bounds = bounds + record.pmVersion = 2 + record.hRes = 72 << 16 + record.vRes = 72 << 16 + record.pixelType = depth <= 8 ? 0 : 16 + record.pixelSize = Int16(depth) + record.cmpCount = depth <= 8 ? 1 : 3 + record.cmpSize = Int16(depth <= 8 ? depth : depth == 16 ? 5 : 8) + record.pmTable = table + map = newHandle(record) + } + + func free() { + dispose(map) + dispose(table) + memory.deallocate() + } +} + +// MARK: Devices + +private func newDevice(map: PixMapHandle, type: Int16, flags: UInt16, rect: CRect, mode: Int32) -> GDHandle { + var record = CGDevice() + record.gdType = type + record.gdFlags = Int16(bitPattern: flags) + record.gdPMap = map + record.gdRect = rect + record.gdMode = .init(truncatingIfNeeded: mode) + return newHandle(record) +} + +/// A world's device: its pixel map is another, over the same pixels. +private func newDevice(for storage: MapStorage) -> GDHandle { + let record = storage.map.pointee!.pointee + return newDevice(map: newHandle(record), type: record.pixelSize <= 8 ? 0 : 2, + flags: record.pixelSize == 1 ? 0x4000 : 0x4001, rect: record.bounds, mode: -1) +} + +private func disposeDevice(_ handle: GDHandle) { + if let map = handle.pointee?.pointee.gdPMap { dispose(map) } + dispose(handle) +} + +enum Devices { + + /// The screen's: 1 bit, over the memory qd.screenBits points at. + nonisolated(unsafe) static let main: GDHandle = { + var record = CPixMap() + record.baseAddr = Screen.pixels + record.rowBytes = Int16(bitPattern: UInt16(Screen.rowBytes) | 0x8000) + record.bounds = Screen.bounds + record.pmVersion = 2 + record.hRes = 72 << 16 + record.vRes = 72 << 16 + record.pixelSize = 1 + record.cmpCount = 1 + record.cmpSize = 1 + record.pmTable = newTable([.white, .black], flags: 0x8000, seed: 1) + // screenDevice, mainScreen and screenActive. + return newDevice(map: newHandle(record), type: 0, flags: 0xA800, rect: Screen.bounds, mode: 128) + }() + + nonisolated(unsafe) private static var chosen: GDHandle? + + /// SetGDevice's and SetGWorld's. + static var current: GDHandle { + get { chosen ?? main } + set { chosen = newValue } + } + + static func depth(of device: GDHandle) -> Int { + Int(device.pointee?.pointee.gdPMap?.pointee?.pointee.pixelSize ?? 1) + } +} + +// MARK: Worlds + +final class World { + + let port: CGrafPtr + var storage: MapStorage + /// The device a program gave with noNewDevice; otherwise the world's own, + /// which is disposed with it. + var device: GDHandle + let ownsDevice: Bool + /// pixelsLocked and pixelsPurgeable. + var state: UInt32 = 0 + private let patterns: [PixPatHandle] + private let variables: UnsafeMutableRawPointer + + /// A color port as NewGWorld leaves it. + init(storage: MapStorage, device: GDHandle?) { + self.storage = storage + ownsDevice = device == nil + self.device = device ?? newDevice(for: storage) + + func pattern(_ value: Graphite2D.Pattern) -> PixPatHandle { + var record = CPixPat() + record.pat1Data = value.c + return newHandle(record) + } + patterns = [pattern(.white), pattern(.black), pattern(.black)] + variables = UnsafeMutableRawPointer.allocate(byteCount: 32, alignment: 8) + variables.initializeMemory(as: UInt8.self, repeating: 0, count: 32) + let master = UnsafeMutablePointer?>.allocate(capacity: 1) + master.initialize(to: variables.assumingMemoryBound(to: CChar.self)) + + let bounds = storage.map.pointee!.pointee.bounds + let visible = RegionStorage.make(), clip = RegionStorage.make() + RegionStorage.store(Graphite2D.Region(rect: GraphiteCore.Rect(bounds)), in: visible) + RegionStorage.store(Graphite2D.Region(rect: GraphiteCore.Rect(top: -32767, left: -32767, bottom: 32767, right: 32767)), + in: clip) + var record = CQuickDraw.CGrafPort() + record.portPixMap = storage.map + record.portVersion = Int16(bitPattern: 0xC001) + record.grafVars = master + record.pnLocHFrac = Int16(bitPattern: 0x8000) + record.portRect = bounds + record.visRgn = visible + record.clipRgn = clip + record.bkPixPat = patterns[0] + record.pnPixPat = patterns[1] + record.fillPixPat = patterns[2] + record.rgbFgColor = Graphite2D.RGBColor.black.c + record.rgbBkColor = Graphite2D.RGBColor.white.c + record.pnSize = CPoint(v: 1, h: 1) + record.pnMode = TransferMode.patCopy.rawValue + record.txMode = TransferMode.srcOr.rawValue + port = CGrafPtr.allocate(capacity: 1) + port.initialize(to: record) + colorsChanged() + } + + /// fgColor and bkColor are the pixel values of the port's colors. + func colorsChanged() { + guard let buffer = pixels(ofMap: storage.map.pointee!) else { return } + port.pointee.fgColor = .init(truncatingIfNeeded: buffer.value(for: Graphite2D.RGBColor(port.pointee.rgbFgColor))) + port.pointee.bkColor = .init(truncatingIfNeeded: buffer.value(for: Graphite2D.RGBColor(port.pointee.rgbBkColor))) + } + + /// Gives the world other pixels, and its own device with them. + func replace(_ new: MapStorage) { + let old = storage + // The pixel map's handle stays the one the program has. + let record = new.map.pointee!.pointee + old.map.pointee!.pointee = record + dispose(new.map) + storage = MapStorage(map: old.map, memory: new.memory, table: new.table) + old.memory.deallocate() + dispose(old.table) + deviceChanged() + colorsChanged() + } + + /// Keeps the world's own device with its pixel map. + func deviceChanged() { + guard ownsDevice, let device = device.pointee, let map = device.pointee.gdPMap else { return } + let record = storage.map.pointee!.pointee + map.pointee!.pointee = record + device.pointee.gdRect = record.bounds + device.pointee.gdType = record.pixelSize <= 8 ? 0 : 2 + device.pointee.gdFlags = record.pixelSize == 1 ? 0x4000 : 0x4001 + } + + func free() { + if let visible = port.pointee.visRgn { RegionStorage.dispose(visible) } + if let clip = port.pointee.clipRgn { RegionStorage.dispose(clip) } + if let master = port.pointee.grafVars { master.deallocate() } + variables.deallocate() + for pattern in patterns { dispose(pattern) } + if ownsDevice { disposeDevice(device) } + storage.free() + port.deallocate() + } +} + +extension MapStorage { + init(map: PixMapHandle, memory: UnsafeMutableRawPointer, table: CTabHandle) { + self.map = map + self.memory = memory + self.table = table + } +} + +enum Worlds { + + nonisolated(unsafe) private static var all: [UnsafeMutableRawPointer: World] = [:] + + static func add(_ world: World) { + all[UnsafeMutableRawPointer(world.port)] = world + } + + /// The world a port is; nil for a port that is none. + static func world(_ port: UnsafeMutableRawPointer) -> World? { + all.isEmpty ? nil : all[port] + } + + /// The world a pixel map's handle belongs to. + static func world(map: PixMapHandle) -> World? { + all.values.first { $0.storage.map == map } + } + + static func remove(_ world: World) { + all[UnsafeMutableRawPointer(world.port)] = nil + } +} diff --git a/Sources/GraphiteQuickDrawABI/PenAPI.swift b/Sources/GraphiteQuickDrawABI/PenAPI.swift index 9800116..b34be8b 100644 --- a/Sources/GraphiteQuickDrawABI/PenAPI.swift +++ b/Sources/GraphiteQuickDrawABI/PenAPI.swift @@ -5,9 +5,9 @@ import CQuickDraw import GraphiteCore import Graphite2D -private func port(_ change: (inout CGrafPortRecord) -> Void) { +private func port(_ change: (PortRef) -> Void) { guard let record = currentPort() else { return } - change(&record.pointee) + change(record) } @c @implementation @@ -78,12 +78,6 @@ public func Line(_ dh: Int16, _ dv: Int16) { drawing { $0.line(dh: dh, dv: dv) } } -@c @implementation -public func ForeColor(_ c: Int32) { port { $0.fgColor = c } } - -@c @implementation -public func BackColor(_ c: Int32) { port { $0.bkColor = c } } - @c @implementation public func BackPat(_ pp: UnsafePointer?) { guard let pp else { return fail() } diff --git a/Sources/GraphiteQuickDrawABI/PolygonsAPI.swift b/Sources/GraphiteQuickDrawABI/PolygonsAPI.swift index 780708d..38b4343 100644 --- a/Sources/GraphiteQuickDrawABI/PolygonsAPI.swift +++ b/Sources/GraphiteQuickDrawABI/PolygonsAPI.swift @@ -26,16 +26,16 @@ public func OpenPoly() -> PolyHandle? { guard let record = currentPort() else { return nil } let handle = PolygonStorage.make() drawing { $0.beginPolygon() } - var recordings = Recordings[record] + var recordings = Recordings[record.pointer] recordings.polygon?.handle = handle - Recordings[record] = recordings + Recordings[record.pointer] = recordings return handle } @c @implementation public func ClosePoly() { guard let record = currentPort() else { return } - let handle = Recordings[record].polygon?.handle + let handle = Recordings[record.pointer].polygon?.handle var polygon: Graphite2D.Polygon? drawing { polygon = $0.endPolygon() } if let handle, let polygon { PolygonStorage.store(polygon, in: handle) } diff --git a/Sources/GraphiteQuickDrawABI/PortRef.swift b/Sources/GraphiteQuickDrawABI/PortRef.swift new file mode 100644 index 0000000..0cd1d03 --- /dev/null +++ b/Sources/GraphiteQuickDrawABI/PortRef.swift @@ -0,0 +1,143 @@ +// A port a program names by a GrafPtr: a GrafPort of its own, or the color +// port of a graphics world. Their records differ, so what a call reads or +// writes of a port goes through here. + +import CQuickDraw + +typealias CHandle = UnsafeMutablePointer?> + +struct PortRef { + + let pointer: GrafPtr + + init(_ pointer: GrafPtr) { self.pointer = pointer } + + /// The graphics world the port is, if it is one. Only NewGWorld makes + /// color ports here, so nothing else is taken for one. + var world: World? { Worlds.world(UnsafeMutableRawPointer(pointer)) } + + var isColor: Bool { world != nil } + + var color: CGrafPtr { UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: CQuickDraw.CGrafPort.self) } + + private func field(_ mono: WritableKeyPath, _ colored: WritableKeyPath) -> T { + isColor ? color.pointee[keyPath: colored] : pointer.pointee[keyPath: mono] + } + + private func set(_ mono: WritableKeyPath, _ colored: WritableKeyPath, _ value: T) { + if isColor { color.pointee[keyPath: colored] = value } else { pointer.pointee[keyPath: mono] = value } + } + + /// The bounds of the port's pixels: its bitmap's or its pixel map's. + var bounds: CRect { + get { isColor ? color.pointee.portPixMap.pointee!.pointee.bounds : pointer.pointee.portBits.bounds } + nonmutating set { + if isColor { color.pointee.portPixMap.pointee!.pointee.bounds = newValue } else { pointer.pointee.portBits.bounds = newValue } + } + } + + var portRect: CRect { + get { field(\.portRect, \.portRect) } + nonmutating set { set(\.portRect, \.portRect, newValue) } + } + + var visRgn: RgnHandle? { + get { field(\.visRgn, \.visRgn) } + nonmutating set { set(\.visRgn, \.visRgn, newValue) } + } + + var clipRgn: RgnHandle? { + get { field(\.clipRgn, \.clipRgn) } + nonmutating set { set(\.clipRgn, \.clipRgn, newValue) } + } + + // A color port keeps each of its patterns in a PixPat. + + var bkPat: CPattern { + get { isColor ? color.pointee.bkPixPat.pointee!.pointee.pat1Data : pointer.pointee.bkPat } + nonmutating set { + if isColor { color.pointee.bkPixPat.pointee!.pointee.pat1Data = newValue } else { pointer.pointee.bkPat = newValue } + } + } + + var pnPat: CPattern { + get { isColor ? color.pointee.pnPixPat.pointee!.pointee.pat1Data : pointer.pointee.pnPat } + nonmutating set { + if isColor { color.pointee.pnPixPat.pointee!.pointee.pat1Data = newValue } else { pointer.pointee.pnPat = newValue } + } + } + + var fillPat: CPattern { + get { isColor ? color.pointee.fillPixPat.pointee!.pointee.pat1Data : pointer.pointee.fillPat } + nonmutating set { + if isColor { color.pointee.fillPixPat.pointee!.pointee.pat1Data = newValue } else { pointer.pointee.fillPat = newValue } + } + } + + var pnLoc: CPoint { + get { field(\.pnLoc, \.pnLoc) } + nonmutating set { set(\.pnLoc, \.pnLoc, newValue) } + } + + var pnSize: CPoint { + get { field(\.pnSize, \.pnSize) } + nonmutating set { set(\.pnSize, \.pnSize, newValue) } + } + + var pnMode: Int16 { + get { field(\.pnMode, \.pnMode) } + nonmutating set { set(\.pnMode, \.pnMode, newValue) } + } + + var pnVis: Int16 { + get { field(\.pnVis, \.pnVis) } + nonmutating set { set(\.pnVis, \.pnVis, newValue) } + } + + var txFont: Int16 { + get { field(\.txFont, \.txFont) } + nonmutating set { set(\.txFont, \.txFont, newValue) } + } + + var txFace: UInt8 { + get { field(\.txFace, \.txFace) } + nonmutating set { set(\.txFace, \.txFace, newValue) } + } + + var txMode: Int16 { + get { field(\.txMode, \.txMode) } + nonmutating set { set(\.txMode, \.txMode, newValue) } + } + + var txSize: Int16 { + get { field(\.txSize, \.txSize) } + nonmutating set { set(\.txSize, \.txSize, newValue) } + } + + var spExtra: Int32 { + get { field(\.spExtra, \.spExtra) } + nonmutating set { set(\.spExtra, \.spExtra, newValue) } + } + + /// In a GrafPort, one of the old color constants; in a color port, the + /// pixel value of its color. + var fgColor: Int32 { + get { Int32(truncatingIfNeeded: field(\.fgColor, \.fgColor)) } + nonmutating set { set(\.fgColor, \.fgColor, .init(truncatingIfNeeded: newValue)) } + } + + var bkColor: Int32 { + get { Int32(truncatingIfNeeded: field(\.bkColor, \.bkColor)) } + nonmutating set { set(\.bkColor, \.bkColor, .init(truncatingIfNeeded: newValue)) } + } + + var rgnSave: CHandle? { + get { field(\.rgnSave, \.rgnSave) } + nonmutating set { set(\.rgnSave, \.rgnSave, newValue) } + } + + var polySave: CHandle? { + get { field(\.polySave, \.polySave) } + nonmutating set { set(\.polySave, \.polySave, newValue) } + } +} diff --git a/Sources/GraphiteQuickDrawABI/PortStorage.swift b/Sources/GraphiteQuickDrawABI/PortStorage.swift index 66d9ef7..c997a24 100644 --- a/Sources/GraphiteQuickDrawABI/PortStorage.swift +++ b/Sources/GraphiteQuickDrawABI/PortStorage.swift @@ -1,6 +1,7 @@ // A GrafPort belongs to the program: it declares the record, reads its fields // and writes them. So the record is the port, and a call makes a Graphite2D -// Port from it, over the same pixels, for as long as the call takes. +// Port from it, over the same pixels, for as long as the call takes. A +// graphics world's color port is read the same way, through PortRef. // // QuickDraw's globals are the program's too; InitGraf says where. The one // thing here is the screen: there is no display, so it is 640 by 480 pixels of @@ -46,13 +47,13 @@ enum CurrentPort { } /// The current port, or nil with QDError set. -func currentPort() -> GrafPtr? { +func currentPort() -> PortRef? { guard let port = CurrentPort.port else { fail() return nil } succeed() - return port + return PortRef(port) } extension Graphite2D.Pattern { @@ -73,10 +74,10 @@ private func region(of handle: RgnHandle?) -> Graphite2D.Region { handle.flatMap(RegionStorage.load) ?? Graphite2D.Region() } -/// The record's bitmap as pixels; nil if it has no memory, covers nothing, has +/// A GrafPort's bitmap as pixels; nil if it has no memory, covers nothing, has /// rows too short for its bounds, or has a rowBytes of 0x4000 or more, which -/// is a color port's pixel map or not a bitmap. -func pixels(of bits: CBitMap) -> PixelBuffer? { +/// is not a bitmap. +private func pixels(ofBitMap bits: CBitMap) -> PixelBuffer? { let bounds = GraphiteCore.Rect(bits.bounds) let rowBytes = Int(bits.rowBytes) guard let base = bits.baseAddr, !bounds.isEmpty, rowBytes >= (bounds.width + 7) / 8, rowBytes < 0x4000 else { @@ -85,16 +86,39 @@ func pixels(of bits: CBitMap) -> PixelBuffer? { return PixelBuffer(wrapping: UnsafeMutableRawPointer(base), rowBytes: rowBytes, bounds: bounds, format: .indexed(depth: 1)) } +/// The pixels a BitMap a program passes stands for: its own, a PixMap's, or, +/// for a color port's portBits, the world's. +func pixels(of bits: UnsafePointer) -> PixelBuffer? { + let rowBytes = UInt16(bitPattern: bits.pointee.rowBytes) + if rowBytes & 0xC000 == 0xC000 { + // portVersion lies where rowBytes does, and portPixMap where baseAddr does. + guard let base = bits.pointee.baseAddr else { return nil } + let map = PixMapHandle(OpaquePointer(base)) + guard Worlds.world(map: map) != nil else { return nil } + return pixels(ofMap: map.pointee!) + } + if rowBytes & 0x8000 != 0 { + return pixels(ofMap: UnsafeRawPointer(bits).assumingMemoryBound(to: CPixMap.self)) + } + return pixels(ofBitMap: bits.pointee) +} + +/// The pixels a port draws in. +func pixels(of port: PortRef) -> PixelBuffer? { + if port.isColor { return pixels(ofMap: port.color.pointee.portPixMap.pointee!) } + return pixels(ofBitMap: port.pointer.pointee.portBits) +} + /// A Graphite2D port with what the record says. It draws into the record's /// pixels. -func enginePort(_ record: CGrafPortRecord) -> Graphite2D.Port { +func enginePort(_ record: PortRef) -> Graphite2D.Port { let port: Graphite2D.Port - if let pixels = pixels(of: record.portBits) { + if let pixels = pixels(of: record) { port = Graphite2D.Port(pixels: pixels) port.portRect = GraphiteCore.Rect(record.portRect) } else { port = Graphite2D.Port(portRect: GraphiteCore.Rect(record.portRect)) - port.pixelBounds = GraphiteCore.Rect(record.portBits.bounds) + port.pixelBounds = GraphiteCore.Rect(record.bounds) } port.visibleRegion = region(of: record.visRgn) port.clipRegion = region(of: record.clipRgn) @@ -105,10 +129,15 @@ func enginePort(_ record: CGrafPortRecord) -> Graphite2D.Port { visibility: record.pnVis) port.text = Graphite2D.TextSettings(font: record.txFont, face: record.txFace, size: record.txSize, mode: TransferMode(rawValue: record.txMode), spaceExtra: Fixed(rawValue: record.spExtra)) - // In a black and white port every color but white is black, as Mac OS - // 9.2.2 copied them. - port.foreColor = Int32(record.fgColor) == 30 ? .white : .black - port.backColor = Int32(record.bkColor) == 30 ? .white : .black + if record.isColor { + port.foreColor = Graphite2D.RGBColor(record.color.pointee.rgbFgColor) + port.backColor = Graphite2D.RGBColor(record.color.pointee.rgbBkColor) + } else { + // In a black and white port every color but white is black, as Mac OS + // 9.2.2 copied them. + port.foreColor = record.fgColor == 30 ? .white : .black + port.backColor = record.bkColor == 30 ? .white : .black + } return port } @@ -116,28 +145,28 @@ func enginePort(_ record: CGrafPortRecord) -> Graphite2D.Port { /// location and visibility are all that is left in the record. func drawing(_ draw: (Graphite2D.Port) -> Void) { guard let record = currentPort() else { return } - let port = enginePort(record.pointee) - var recordings = Recordings[record] + let port = enginePort(record) + var recordings = Recordings[record.pointer] port.regionRecording = recordings.region port.openPolygon = recordings.polygon?.points draw(port) - record.pointee.pnLoc = port.pen.location.c - record.pointee.pnVis = port.pen.visibility + record.pnLoc = port.pen.location.c + record.pnVis = port.pen.visibility recordings.region = port.regionRecording recordings.polygon = port.openPolygon.map { ($0, recordings.polygon?.handle) } - Recordings[record] = recordings + Recordings[record.pointer] = recordings } /// Changes the current port's rectangles and visible region as the engine /// does for SetOrigin, PortSize and MovePortTo. func changingGeometry(_ change: (Graphite2D.Port) -> Void) { guard let record = currentPort() else { return } - let port = enginePort(record.pointee) + let port = enginePort(record) let visible = port.visibleRegion change(port) - record.pointee.portBits.bounds = port.pixelBounds.c - record.pointee.portRect = port.portRect.c - if port.visibleRegion != visible, let handle = record.pointee.visRgn { + record.bounds = port.pixelBounds.c + record.portRect = port.portRect.c + if port.visibleRegion != visible, let handle = record.visRgn { RegionStorage.store(port.visibleRegion, in: handle) } } diff --git a/Sources/GraphiteQuickDrawABI/PortsAPI.swift b/Sources/GraphiteQuickDrawABI/PortsAPI.swift index a4adbe0..39393d9 100644 --- a/Sources/GraphiteQuickDrawABI/PortsAPI.swift +++ b/Sources/GraphiteQuickDrawABI/PortsAPI.swift @@ -36,7 +36,7 @@ public func InitGraf(_ gp: UnsafeMutablePointer?) { /// it has none. @c @implementation public func InitPort(_ p: GrafPtr?) { - guard let p else { return fail() } + guard let p, !PortRef(p).isColor else { return fail() } CurrentPort.port = p let visible = p.pointee.visRgn ?? RegionStorage.make() let clip = p.pointee.clipRgn ?? RegionStorage.make() @@ -64,7 +64,7 @@ public func InitPort(_ p: GrafPtr?) { /// InitPort on a record that holds nothing yet. @c @implementation public func OpenPort(_ p: GrafPtr?) { - guard let p else { return fail() } + guard let p, !PortRef(p).isColor else { return fail() } p.pointee.visRgn = nil p.pointee.clipRgn = nil InitPort(p) @@ -73,7 +73,7 @@ public func OpenPort(_ p: GrafPtr?) { /// Frees the port's regions. A current port stays current, and draws nothing. @c @implementation public func ClosePort(_ p: GrafPtr?) { - guard let p else { return fail() } + guard let p, !PortRef(p).isColor else { return fail() } Recordings.forget(p) p.pointee.rgnSave = nil p.pointee.polySave = nil @@ -100,8 +100,9 @@ public func GetPort(_ pp: UnsafeMutablePointer?) { @c @implementation public func SetPortBits(_ bm: UnsafeMutablePointer?) { - guard let bm, let port = currentPort() else { return fail() } - port.pointee.portBits = bm.pointee + // A color port's pixels are a pixel map's, which SetPortPix would change. + guard let bm, let port = currentPort(), !port.isColor else { return fail() } + port.pointer.pointee.portBits = bm.pointee } @c @implementation @@ -122,31 +123,31 @@ public func SetOrigin(_ h: Int16, _ v: Int16) { @c @implementation public func SetClip(_ r: RgnHandle?) { guard let port = currentPort() else { return } - CopyRgn(r, port.pointee.clipRgn) + CopyRgn(r, port.clipRgn) } @c @implementation public func GetClip(_ r: RgnHandle?) { guard let port = currentPort() else { return } - CopyRgn(port.pointee.clipRgn, r) + CopyRgn(port.clipRgn, r) } @c @implementation public func ClipRect(_ r: UnsafeMutablePointer?) { guard let port = currentPort() else { return } - RectRgn(port.pointee.clipRgn, r) + RectRgn(port.clipRgn, r) } @c @implementation public func LocalToGlobal(_ pt: UnsafeMutablePointer?) { guard let pt, let port = currentPort() else { return fail() } - pt.pointee = (GraphiteCore.Point(pt.pointee) - GraphiteCore.Rect(port.pointee.portBits.bounds).topLeft).c + pt.pointee = (GraphiteCore.Point(pt.pointee) - GraphiteCore.Rect(port.bounds).topLeft).c } @c @implementation public func GlobalToLocal(_ pt: UnsafeMutablePointer?) { guard let pt, let port = currentPort() else { return fail() } - pt.pointee = (GraphiteCore.Point(pt.pointee) + GraphiteCore.Rect(port.pointee.portBits.bounds).topLeft).c + pt.pointee = (GraphiteCore.Point(pt.pointee) + GraphiteCore.Rect(port.bounds).topLeft).c } /// Whether the pixel right of and below a point, in local coordinates, is @@ -154,6 +155,9 @@ public func GlobalToLocal(_ pt: UnsafeMutablePointer?) { @c @implementation public func GetPixel(_ h: Int16, _ v: Int16) -> QDBoolean { guard let port = currentPort() else { return boolean(false) } - let pixels = enginePort(port.pointee).pixels - return boolean(pixels?.value(at: GraphiteCore.Point(v: v, h: h)) == 1) + guard let pixels = enginePort(port).pixels, let value = pixels.value(at: GraphiteCore.Point(v: v, h: h)) else { + return boolean(false) + } + // Indexed pixels but 0 count, and direct ones that are black. + return boolean(pixels.format.isIndexed ? value != 0 : value & 0x00FF_FFFF == 0) } diff --git a/Sources/GraphiteQuickDrawABI/RecordingStorage.swift b/Sources/GraphiteQuickDrawABI/RecordingStorage.swift index 8ae2573..089a60e 100644 --- a/Sources/GraphiteQuickDrawABI/RecordingStorage.swift +++ b/Sources/GraphiteQuickDrawABI/RecordingStorage.swift @@ -31,8 +31,8 @@ struct Recordings { set { let key = UnsafeMutableRawPointer(port) open[key] = newValue.region == nil && newValue.polygon == nil ? nil : newValue - port.pointee.rgnSave = newValue.region == nil ? nil : mark - port.pointee.polySave = newValue.polygon == nil ? nil : mark + PortRef(port).rgnSave = newValue.region == nil ? nil : mark + PortRef(port).polySave = newValue.polygon == nil ? nil : mark } } diff --git a/Sources/GraphiteQuickDrawABI/ShapesAPI.swift b/Sources/GraphiteQuickDrawABI/ShapesAPI.swift index 22bcc6d..37ad6e9 100644 --- a/Sources/GraphiteQuickDrawABI/ShapesAPI.swift +++ b/Sources/GraphiteQuickDrawABI/ShapesAPI.swift @@ -23,7 +23,7 @@ func filling(_ pattern: UnsafePointer?) -> ShapeOperation? { fail() return nil } - port.pointee.fillPat = pattern.pointee + port.fillPat = pattern.pointee return .fill(Graphite2D.Pattern(pattern.pointee)) } diff --git a/Sources/GraphiteQuickDrawABI/TextAPI.swift b/Sources/GraphiteQuickDrawABI/TextAPI.swift index 6a486b2..13aeee2 100644 --- a/Sources/GraphiteQuickDrawABI/TextAPI.swift +++ b/Sources/GraphiteQuickDrawABI/TextAPI.swift @@ -35,7 +35,7 @@ private func draw(_ characters: String.UnicodeScalarView) { private func width(_ characters: String.UnicodeScalarView) -> Int16 { guard let record = currentPort() else { return 0 } - return Int16(clamping: enginePort(record.pointee).textWidth(characters, fonts: Fonts.registry)) + return Int16(clamping: enginePort(record).textWidth(characters, fonts: Fonts.registry)) } private func character(_ code: Int16) -> String.UnicodeScalarView { @@ -85,7 +85,7 @@ public func TextWidth(_ tb: UnsafeMutablePointer?, _ fb: Int16, _ bc: Int @c @implementation public func MeasureText(_ n: Int16, _ text: UnsafeMutablePointer?, _ chars: UnsafeMutablePointer?) { guard let text, let chars, let record = currentPort() else { return fail() } - let positions = enginePort(record.pointee).measureText(characters(text, 0, n), fonts: Fonts.registry) + let positions = enginePort(record).measureText(characters(text, 0, n), fonts: Fonts.registry) for (index, position) in positions.enumerated() { UnsafeMutableRawPointer(chars).storeBytes(of: Int16(clamping: position), toByteOffset: index * 2, as: Int16.self) } @@ -94,7 +94,7 @@ public func MeasureText(_ n: Int16, _ text: UnsafeMutablePointer?, _ char @c @implementation public func GetFontInfo(_ ip: UnsafeMutablePointer?) { guard let ip, let record = currentPort() else { return fail() } - let metrics = enginePort(record.pointee).fontInfo(fonts: Fonts.registry) + let metrics = enginePort(record).fontInfo(fonts: Fonts.registry) ip.pointee = CQuickDraw.FontInfo(ascent: metrics.ascent, descent: metrics.descent, widMax: metrics.widMax, leading: metrics.leading) } diff --git a/Tests/GraphiteQuickDrawABITests/GWorldCaptureTests.swift b/Tests/GraphiteQuickDrawABITests/GWorldCaptureTests.swift new file mode 100644 index 0000000..71d430c --- /dev/null +++ b/Tests/GraphiteQuickDrawABITests/GWorldCaptureTests.swift @@ -0,0 +1,539 @@ +import Testing +import Foundation +import CQuickDraw +import GraphiteCore +@testable import GraphiteQuickDrawABI + +private typealias CRect = CQuickDraw.Rect +private typealias CColor = CQuickDraw.RGBColor +private typealias CPixMapRecord = CQuickDraw.PixMap + +private let retro68 = URL(fileURLWithPath: #filePath).deletingLastPathComponent() + .deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("ReferenceTests/Retro68") + +private func rect(_ top: Int16, _ left: Int16, _ bottom: Int16, _ right: Int16) -> CRect { + CRect(top: top, left: left, bottom: bottom, right: right) +} + +private func rgb(_ red: UInt16, _ green: UInt16, _ blue: UInt16) -> CColor { + CColor(red: red, green: green, blue: blue) +} + +private func hex(_ value: some BinaryInteger, _ digits: Int) -> String { + let text = String(value, radix: 16, uppercase: true) + return String(repeating: "0", count: max(0, digits - text.count)) + text +} + +private func described(_ name: String, _ r: CRect) -> String { " \(name) (\(r.top),\(r.left),\(r.bottom),\(r.right))" } + +private func described(_ name: String, _ c: CColor) -> String { " \(name) \(hex(c.red, 4)),\(hex(c.green, 4)),\(hex(c.blue, 4))" } + +private func described(_ name: String, region: RgnHandle?) -> String { + guard let region = region?.pointee else { return " \(name) nil" } + return " \(name) size \(region.pointee.rgnSize)" + described("box", region.pointee.rgnBBox) +} + +private func described(_ name: String, pattern: PixPatHandle?) -> String { + guard let pattern = pattern?.pointee else { return " \(name) nil" } + let data = withUnsafeBytes(of: pattern.pointee.pat1Data.pat) { $0.map { hex($0, 2) }.joined() } + return " \(name) type \(pattern.pointee.patType) data \(data)" +} + +private func isSet(_ pointer: (some Any)?) -> String { pointer == nil ? "nil" : "set" } + +private let stripeColors: [CColor] = [ + rgb(0xFFFF, 0xFFFF, 0xFFFF), rgb(0, 0, 0), rgb(0xFFFF, 0, 0), rgb(0, 0xFFFF, 0), + rgb(0, 0, 0xFFFF), rgb(0xFFFF, 0xFFFF, 0), rgb(0x9999, 0x9999, 0x9999), rgb(0x6666, 0x6666, 0x6666), +] + +private let depths: [Int16] = [1, 2, 4, 8, 16, 32] + +/// GWorldCapture.c's calls, made through the C API, and the lines the probe +/// wrote. +private final class Probe { + let globals = UnsafeMutablePointer.allocate(capacity: 1) + let port = UnsafeMutablePointer.allocate(capacity: 1) + let bits = UnsafeMutablePointer.allocate(capacity: 4 * 16) + let screen: GDHandle + var lines: [String] = [] + + init() { + globals.initialize(to: QDGlobals()) + port.initialize(to: GrafPort()) + bits.initialize(repeating: 0, count: 4 * 16) + InitGraf(UnsafeMutableRawPointer(globals) + .advanced(by: MemoryLayout.offset(of: \.thePort)!).assumingMemoryBound(to: GrafPtr?.self)) + OpenPort(port) + port.pointee.portBits.baseAddr = UnsafeMutableRawPointer(bits).assumingMemoryBound(to: CChar.self) + port.pointee.portBits.rowBytes = 4 + port.pointee.portBits.bounds = rect(0, 0, 16, 32) + port.pointee.portRect = port.pointee.portBits.bounds + var bounds = port.pointee.portRect + RectRgn(port.pointee.visRgn, &bounds) + screen = GetMainDevice()! + SetGDevice(screen) + } + + deinit { + SetGDevice(screen) + ClosePort(port) + CurrentPort.global = nil + CurrentPort.port = nil + port.deallocate() + bits.deallocate() + globals.deallocate() + } + + var mine: CGrafPtr { UnsafeMutableRawPointer(port).assumingMemoryBound(to: CQuickDraw.CGrafPort.self) } + var thePort: GrafPtr? { globals.pointee.thePort } + + func line(_ label: String, _ rest: String) { lines.append("\(label) ->\(rest)") } + + func newWorld(_ depth: Int16, _ bounds: CRect, _ flags: Int32 = 0) -> (Int16, GWorldPtr?) { + var world: GWorldPtr? + var bounds = bounds + let result = NewGWorld(&world, depth, &bounds, nil, nil, flags) + return (result, world) + } + + func world(_ depth: Int16, _ bounds: CRect) -> GWorldPtr { newWorld(depth, bounds).1! } + + func where_(_ label: String, _ world: GWorldPtr?) { + var current: CGrafPtr? + var device: GDHandle? + GetGWorld(¤t, &device) + let port = current == mine ? "mine" : world != nil && current == world ? "world" : "other" + let which = device == screen ? "main" : world != nil && device == GetGWorldDevice(world) ? "world" : "other" + let same = thePort.map { UnsafeMutableRawPointer($0) } == current.map { UnsafeMutableRawPointer($0) } + line(label, " port \(port) device \(which) thePort \(same ? "same" : "differs")") + } + + func map(_ world: GWorldPtr) -> UnsafeMutablePointer { GetGWorldPixMap(world)!.pointee! } + + func dumpPixMap(_ label: String, _ world: GWorldPtr) { + let m = map(world).pointee + line(label, " rowBytes 0x\(hex(UInt16(bitPattern: m.rowBytes), 4))" + described("bounds", m.bounds) + + " version \(m.pmVersion) packType \(m.packType) packSize \(m.packSize) hRes \(m.hRes) vRes \(m.vRes)" + + " pixelType \(m.pixelType) pixelSize \(m.pixelSize) cmpCount \(m.cmpCount) cmpSize \(m.cmpSize)" + + " planeBytes \(m.planeBytes) reserved \(m.pmReserved) table \(isSet(m.pmTable))") + guard let table = m.pmTable?.pointee else { return } + let count = Int(table.pointee.ctSize) + 1 + let seed = Int(table.pointee.ctSeed) + var text = " flags 0x\(hex(table.pointee.ctFlags, 4)) size \(table.pointee.ctSize) seed " + + (seed == Int(m.pixelSize) ? "depth" : seed < 1024 ? "small" : "large") + let entries = (UnsafeMutableRawPointer(table) + 8).assumingMemoryBound(to: CQuickDraw.ColorSpec.self) + for i in 0 ..< min(count, 256) where !(count > 16 && i > 3 && i < count - 2) { + text += " [\(i)] \(entries[i].value)" + described("", entries[i].rgb) + } + line("\(label), color table", text) + } + + func dumpPort(_ label: String, _ world: GWorldPtr) { + let p = world.pointee + line("\(label), port", " device \(p.device) portVersion 0x\(hex(UInt16(bitPattern: p.portVersion), 4))" + + " grafVars \(isSet(p.grafVars)) chExtra \(p.chExtra) pnLocHFrac \(UInt16(bitPattern: p.pnLocHFrac))" + + described("portRect", p.portRect) + described("visRgn", region: p.visRgn) + described("clipRgn", region: p.clipRgn) + + described("bkPixPat", pattern: p.bkPixPat) + described("pnPixPat", pattern: p.pnPixPat) + + described("fillPixPat", pattern: p.fillPixPat) + described("rgbFg", p.rgbFgColor) + described("rgbBk", p.rgbBkColor)) + var rest = " pnLoc (\(p.pnLoc.v),\(p.pnLoc.h)) pnSize (\(p.pnSize.v),\(p.pnSize.h)) pnMode \(p.pnMode) pnVis \(p.pnVis)" + rest += " txFont \(p.txFont) txFace \(p.txFace) txMode \(p.txMode) txSize \(p.txSize) spExtra \(p.spExtra)" + rest += " fgColor \(p.fgColor) bkColor \(p.bkColor) colrBit \(p.colrBit) patStretch \(p.patStretch)" + rest += " picSave \(isSet(p.picSave)) rgnSave \(isSet(p.rgnSave)) polySave \(isSet(p.polySave))" + rest += " grafProcs \(isSet(p.grafProcs))" + line("\(label), pen and text", rest) + } + + func dumpDevice(_ label: String, _ world: GWorldPtr) { + let handle = GetGWorldDevice(world) + let own = GetGWorldPixMap(world)! + if handle == screen { return line("\(label), device", " main") } + guard let d = handle?.pointee?.pointee else { return line("\(label), device", " nil") } + var rest = " refNum \(d.gdRefNum) id \(d.gdID) type \(d.gdType) resPref \(d.gdResPref)" + rest += " flags 0x\(hex(UInt16(bitPattern: d.gdFlags), 4)) refCon \(d.gdRefCon) mode \(d.gdMode) next \(isSet(d.gdNextGD))" + rest += described("rect", d.gdRect) + " gdPMap \(d.gdPMap == own ? "the world's" : "another")" + if let m = d.gdPMap?.pointee?.pointee { + rest += " depth \(m.pixelSize) rowBytes 0x\(hex(UInt16(bitPattern: m.rowBytes), 4))" + rest += " base \(m.baseAddr == own.pointee!.pointee.baseAddr ? "same" : "differs")" + rest += " table \(m.pmTable == own.pointee!.pointee.pmTable ? "same" : "differs")" + } + line("\(label), device", rest) + } + + func locking(_ label: String, _ world: GWorldPtr) { + let handle = GetGWorldPixMap(world)! + var rest = " pixmap \(handle == world.pointee.portPixMap ? "the port's" : "another")" + rest += " state 0x\(hex(GetPixelsState(handle), 1).lowercased())" + let before = GetPixBaseAddr(handle) + rest += " baseAddr \(handle.pointee!.pointee.baseAddr == before ? "pixels" : "not pixels")" + let locked = LockPixels(handle) + rest += " lock \(locked == boolean(false) ? 0 : 1) state 0x\(hex(GetPixelsState(handle), 1).lowercased()) error \(QDError())" + rest += " baseAddr \(handle.pointee!.pointee.baseAddr == GetPixBaseAddr(handle) ? "pixels" : "not pixels")" + rest += " moved \(GetPixBaseAddr(handle) != before ? 1 : 0)" + rest += " aligned4 \(UInt(bitPattern: GetPixBaseAddr(handle)) & 3 == 0 ? 1 : 0)" + UnlockPixels(handle) + rest += " unlock state 0x\(hex(GetPixelsState(handle), 1).lowercased())" + AllowPurgePixels(handle) + rest += " allow 0x\(hex(GetPixelsState(handle), 1).lowercased())" + NoPurgePixels(handle) + rest += " disallow 0x\(hex(GetPixelsState(handle), 1).lowercased())" + line("\(label), pixels", rest) + } + + func creation() { + for depth in depths { + let (result, made) = newWorld(depth, rect(0, 0, 16, 35)) + line("depth \(depth), NewGWorld", " \(result) error \(QDError()) world \(isSet(made))") + guard let world = made else { continue } + where_("depth \(depth), after NewGWorld", world) + dumpPort("depth \(depth)", world) + dumpPixMap("depth \(depth)", world) + dumpDevice("depth \(depth)", world) + locking("depth \(depth)", world) + DisposeGWorld(world) + } + } + + func widths() { + for depth in depths { + var rest = "" + for width: Int16 in [1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 100, 127, 128, 129, 640] { + let (result, made) = newWorld(depth, rect(0, 0, 2, width)) + guard result == 0, let world = made else { rest += " \(width):error"; continue } + rest += " \(width):\(map(world).pointee.rowBytes & 0x3FFF)" + DisposeGWorld(world) + } + line("rowBytes at depth \(depth)", rest) + } + } + + func attempt(_ label: String, _ depth: Int16, _ bounds: CRect, _ flags: Int32 = 0, detail: Bool = false) { + let (result, made) = newWorld(depth, bounds, flags) + var rest = " \(result) error \(QDError()) world \(isSet(made))" + if let world = made { + let m = map(world).pointee + let screens = m.pixelSize == screen.pointee!.pointee.gdPMap!.pointee!.pointee.pixelSize + rest += " depth \(screens ? "screen's" : "own") \(depth != 0 ? m.pixelSize : 0)" + rest += " rowBytes 0x\(hex(UInt16(bitPattern: m.rowBytes), 4))" + described("bounds", m.bounds) + rest += " device \(GetGWorldDevice(world) == screen ? "main" : "own")" + rest += " state 0x\(hex(GetPixelsState(GetGWorldPixMap(world)), 1).lowercased())" + } + line("NewGWorld \(label)", rest) + if let world = made { + if detail { + dumpPort(label, world) + dumpDevice(label, world) + } + DisposeGWorld(world) + } + } + + func flagsAndErrors() { + let small = rect(0, 0, 4, 8) + attempt("away from the origin", 8, rect(10, 20, 26, 53), detail: true) + attempt("at negative coordinates", 8, rect(-5, -7, 3, 1), detail: true) + attempt("depth 0", 0, small) + attempt("pixPurge", 8, small, 1) + attempt("noNewDevice", 8, small, 2, detail: true) + attempt("useTempMem", 8, small, 4) + attempt("keepLocal", 8, small, 8) + attempt("depth 3", 3, small) + attempt("depth 24", 24, small) + attempt("depth -1", -1, small) + attempt("empty bounds", 8, rect(0, 0, 0, 0)) + attempt("no width", 8, rect(0, 4, 4, 4)) + attempt("inverted bounds", 8, rect(4, 8, 0, 0)) + } + + func switching() { + let bounds = rect(0, 0, 4, 8) + SetGWorld(mine, screen) + where_("at first", nil) + let a = world(8, bounds), b = world(32, bounds) + SetGWorld(a, nil); where_("SetGWorld of world, nil", a) + SetGWorld(mine, screen); where_("SetGWorld of mine, main", a) + SetPort(GrafPtr(OpaquePointer(a))); where_("SetPort of world", a) + SetPort(port); where_("SetPort of mine", a) + SetGWorld(a, screen); where_("SetGWorld of world, main", a) + SetGWorld(mine, GetGWorldDevice(a)); where_("SetGWorld of mine, world's device", a) + SetGWorld(mine, nil); where_("SetGWorld of mine, nil", a) + SetGWorld(a, nil) + SetGWorld(b, GetGWorldDevice(b)); where_("SetGWorld of other, world's device (world here is other)", b) + SetGWorld(mine, screen) + DisposeGWorld(b) + DisposeGWorld(a) + } + + func dumpPixels(_ label: String, _ world: GWorldPtr) { + let handle = GetGWorldPixMap(world)! + let base = UnsafeRawPointer(GetPixBaseAddr(handle)!).assumingMemoryBound(to: UInt8.self) + let m = handle.pointee!.pointee + let row = Int(m.rowBytes & 0x3FFF) + let length = (Int(m.bounds.right - m.bounds.left) * Int(m.pixelSize) + 7) / 8 + var rest = "" + for y in 0 ..< Int(m.bounds.bottom - m.bounds.top) { + rest += " " + (0 ..< length).map { hex(base[y * row + $0], 2) }.joined() + } + line(label, rest) + } + + func colors(_ label: String) { + let p = UnsafeMutableRawPointer(thePort!).assumingMemoryBound(to: CQuickDraw.CGrafPort.self).pointee + var fore = CColor(), back = CColor() + GetForeColor(&fore) + GetBackColor(&back) + line(label, " fgColor \(p.fgColor) bkColor \(p.bkColor) error \(QDError())" + described("rgbFg", p.rgbFgColor) + + described("rgbBk", p.rgbBkColor) + described("GetForeColor", fore) + described("GetBackColor", back)) + } + + func pixel(_ label: String, _ h: Int16, _ v: Int16) { + var color = CColor() + GetCPixel(h, v, &color) + line(label, " GetPixel \(GetPixel(h, v) == boolean(false) ? 0 : 1)" + described("GetCPixel", color)) + } + + func drawing() { + for depth in depths { + var all = rect(0, 0, 4, 8), r = CRect() + var color = CColor() + let world = world(depth, all) + SetGWorld(world, nil) + _ = LockPixels(GetGWorldPixMap(world)) + func show(_ what: String) { dumpPixels("draw \(depth), \(what)", world) } + EraseRect(&all); show("erased") + r = rect(1, 1, 3, 3); PaintRect(&r); show("painted black") + r = rect(2, 2, 4, 6); InvertRect(&r); show("inverted") + ForeColor(205) + colors("draw \(depth), ForeColor red") + r = rect(0, 0, 2, 4); PaintRect(&r); show("painted red") + color = rgb(0x8000, 0x4000, 0xC000); RGBForeColor(&color) + colors("draw \(depth), RGBForeColor") + r = rect(0, 4, 2, 8); PaintRect(&r); show("painted RGB") + pixel("draw \(depth), pixel painted RGB", 5, 0) + color = rgb(0x2000, 0xC000, 0x6000); RGBBackColor(&color) + colors("draw \(depth), RGBBackColor") + EraseRect(&all); show("erased with RGB") + FillRect(&all, &globals.pointee.gray); show("filled gray") + r = rect(0, 0, 2, 8); PenMode(10); PaintRect(&r); show("pen xor") + PenNormal() + r = rect(2, 0, 4, 8); InvertRect(&r); show("inverted colors") + ForeColor(33); BackColor(30) + colors("draw \(depth), black and white again") + MoveTo(0, 0); LineTo(7, 3); show("line") + color = rgb(0x1234, 0x5678, 0x9ABC); SetCPixel(1, 3, &color); show("SetCPixel") + pixel("draw \(depth), pixel set", 1, 3) + SetGWorld(mine, screen) + DisposeGWorld(world) + } + } + + /// Eight columns of colors in a world, which is left locked. + func striped(_ depth: Int16) -> GWorldPtr { + let world = world(depth, rect(0, 0, 4, 8)) + SetGWorld(world, nil) + _ = LockPixels(GetGWorldPixMap(world)) + for (i, stripe) in stripeColors.enumerated() { + var color = stripe + RGBForeColor(&color) + var r = rect(0, Int16(i), 4, Int16(i + 1)) + PaintRect(&r) + } + ForeColor(33) + SetGWorld(mine, screen) + return world + } + + func dumpBits(_ label: String) { + line(label, (0 ..< 4).map { " " + hex(bits[$0 * 4], 2) }.joined()) + } + + private func bitMap(of world: GWorldPtr) -> UnsafeMutablePointer { + UnsafeMutableRawPointer(map(world)).assumingMemoryBound(to: BitMap.self) + } + + func copying() { + var all = rect(0, 0, 4, 8) + var source: [UInt8] = [0xF0, 0, 0xCC, 0, 0xAA, 0, 0x00, 0] + source.withUnsafeMutableBytes { bytes in + var small = BitMap(baseAddr: bytes.baseAddress!.assumingMemoryBound(to: CChar.self), rowBytes: 2, + bounds: rect(0, 0, 4, 16)) + let portBits = UnsafeMutableRawPointer(port).advanced(by: MemoryLayout.offset(of: \.portBits)!) + .assumingMemoryBound(to: BitMap.self) + for from in depths { + let source = striped(from) + dumpPixels("stripes at \(from)", source) + bits.update(repeating: 0, count: 4 * 16) + SetPort(port) + CopyBits(bitMap(of: source), portBits, &all, &all, 0, nil) + dumpBits("CopyBits \(from) to a BitMap") + bits.update(repeating: 0, count: 4 * 16) + let theirs = UnsafeMutableRawPointer(source).advanced(by: MemoryLayout.offset(of: \.portBits)!) + .assumingMemoryBound(to: BitMap.self) + CopyBits(theirs, portBits, &all, &all, 0, nil) + dumpBits("CopyBits \(from) to a BitMap by portBits") + for to in depths { + let into = world(to, all) + SetGWorld(into, nil) + _ = LockPixels(GetGWorldPixMap(into)) + EraseRect(&all) + CopyBits(bitMap(of: source), bitMap(of: into), &all, &all, 0, nil) + dumpPixels("CopyBits \(from) to \(to)", into) + SetGWorld(mine, screen) + DisposeGWorld(into) + } + DisposeGWorld(source) + } + for to in depths { + let into = world(to, all) + SetGWorld(into, nil) + _ = LockPixels(GetGWorldPixMap(into)) + EraseRect(&all) + CopyBits(&small, bitMap(of: into), &all, &all, 0, nil) + dumpPixels("CopyBits BitMap to \(to)", into) + var color = rgb(0xFFFF, 0, 0) + RGBForeColor(&color) + color = rgb(0, 0, 0xFFFF) + RGBBackColor(&color) + CopyBits(&small, bitMap(of: into), &all, &all, 0, nil) + dumpPixels("CopyBits BitMap to \(to), red on blue", into) + SetGWorld(mine, screen) + DisposeGWorld(into) + } + } + } + + func updated(_ label: String, _ world: inout GWorldPtr?, _ depth: Int16, _ bounds: CRect, _ flags: Int32) { + let before = world + let handle = GetGWorldPixMap(world) + let base = GetPixBaseAddr(handle) + var bounds = bounds + let result = UpdateGWorld(&world, depth, &bounds, nil, nil, flags) + var rest = " 0x\(hex(UInt32(bitPattern: result), 1)) error \(QDError())" + if let now = world { + let map = GetGWorldPixMap(now)! + let m = map.pointee!.pointee + rest += " world \(now == before ? "same" : "new") pixmap \(map == handle ? "same" : "new")" + rest += " base \(GetPixBaseAddr(map) == base ? "same" : "new") depth \(m.pixelSize)" + rest += " rowBytes 0x\(hex(UInt16(bitPattern: m.rowBytes), 4)) state 0x\(hex(GetPixelsState(map), 1).lowercased())" + rest += described("bounds", m.bounds) + described("portRect", now.pointee.portRect) + rest += described("visRgn", region: now.pointee.visRgn) + described("clipRgn", region: now.pointee.clipRgn) + } + line("UpdateGWorld \(label)", rest) + if let now = world { + _ = LockPixels(GetGWorldPixMap(now)) + dumpPixels("UpdateGWorld \(label), pixels", now) + UnlockPixels(GetGWorldPixMap(now)) + } + } + + func updating() { + let ways: [(String, Int32)] = [("no flags", 0), ("clipPix", 1 << 28), ("stretchPix", 1 << 29)] + for (name, flags) in ways { + var world: GWorldPtr? = striped(8) + UnlockPixels(GetGWorldPixMap(world)) + updated("with the same, \(name)", &world, 8, rect(0, 0, 4, 8), flags) + updated("offset, \(name)", &world, 8, rect(1, 2, 5, 10), flags) + updated("larger, \(name)", &world, 8, rect(0, 0, 6, 12), flags) + updated("smaller, \(name)", &world, 8, rect(0, 0, 3, 5), flags) + updated("deeper, \(name)", &world, 32, rect(0, 0, 3, 5), flags) + updated("to depth 3, \(name)", &world, 3, rect(0, 0, 3, 5), flags) + DisposeGWorld(world) + } + var world: GWorldPtr? = striped(8) + SetGWorld(world, nil) + updated("of the current world", &world, 16, rect(0, 0, 6, 12), 0) + where_("after UpdateGWorld of the current world", world) + SetGWorld(mine, screen) + DisposeGWorld(world) + } + + func last() { + let world = world(8, rect(0, 0, 4, 8)) + line("OffscreenVersion", " \(OffscreenVersion())") + line("QDDone", " \(QDDone(GrafPtr(OpaquePointer(world))) == boolean(false) ? 0 : 1) \(QDDone(nil) == boolean(false) ? 0 : 1)") + line("PixMap32Bit", " \(PixMap32Bit(GetGWorldPixMap(world)) == boolean(false) ? 0 : 1)") + SetGWorld(world, nil) + DisposeGWorld(world) + where_("after DisposeGWorld of the current world", nil) + } + + func run() { + let m = screen.pointee!.pointee + line("main device", " depth \(m.gdPMap!.pointee!.pointee.pixelSize) type \(m.gdType) flags 0x\(hex(UInt16(bitPattern: m.gdFlags), 4))") + creation() + widths() + flagsAndErrors() + switching() + drawing() + copying() + updating() + last() + line("done", "") + } +} + +extension QuickDrawTests { + + /// Graphics worlds, compared with what Mac OS 9.2.2 did + /// (ReferenceTests/Retro68/GWorldCapture.c). + @Suite struct GWorldCaptureTests { + + /// What differs by design is taken out of both: the pixel map's reserved + /// long, which is whatever the Macintosh had there; its baseAddr before + /// LockPixels, which there is a handle; and UpdateGWorld's new world and + /// pixel map, where here both stay. With clipPix Mac OS also left the + /// clipRgn's box garbled and whatever was in memory outside the old + /// pixels. + private static func comparable(_ line: String) -> String { + var line = line + func replace(_ pattern: String, _ with: String) { + line = line.replacingOccurrences(of: pattern, with: with, options: .regularExpression) + } + replace("reserved [0-9]+", "reserved") + replace("baseAddr (not )?pixels lock", "baseAddr lock") + replace("world (new|same) pixmap (new|same) base (new|same)", "world") + guard line.hasPrefix("UpdateGWorld"), line.contains("clipPix") else { return line } + replace("clipRgn size 10 box \\([-0-9,]+\\)", "clipRgn") + if line.hasPrefix("UpdateGWorld larger, clipPix, pixels -> ") { + let rows = line.split(separator: " ").suffix(6).prefix(4) + line = "UpdateGWorld larger, clipPix, pixels -> " + rows.map { $0.prefix(16) }.joined(separator: " ") + } + return line + } + + private static let capture: [String: String] = { + let data = try! Data(contentsOf: retro68.appendingPathComponent("GWorld-macos-9.2.2-ppc.txt")) + var lines: [String: String] = [:] + for line in String(decoding: data, as: UTF8.self).split(separator: "\r").map(String.init) { + if let arrow = line.range(of: " ->") { lines[String(line[.. = [ + "main device", "NewGWorld depth 0", "NewGWorld depth 24", + "UpdateGWorld larger, no flags, pixels", "UpdateGWorld smaller, no flags, pixels", + "UpdateGWorld deeper, no flags, pixels", "UpdateGWorld to depth 3, no flags, pixels", + "UpdateGWorld of the current world, pixels", + "UpdateGWorld smaller, no flags", "UpdateGWorld smaller, clipPix", "UpdateGWorld smaller, stretchPix", + "UpdateGWorld larger, stretchPix, pixels", "UpdateGWorld smaller, stretchPix, pixels", + "UpdateGWorld deeper, stretchPix, pixels", "UpdateGWorld to depth 3, stretchPix, pixels", + ] + + @Test func doesWhatMacOSDid() { + let probe = Probe() + probe.run() + #expect(probe.lines.count == Self.capture.count) + for line in probe.lines.map(Self.comparable) { + let label = String(line[..")!.lowerBound]) + #expect((Self.capture[label] == line) != Self.knownDifferences.contains(label), + "DIFF \(label)\n ours \(line)\n mac \(Self.capture[label] ?? "?")") + } + } + } +} diff --git a/Tools/quickdraw-abi/README.md b/Tools/quickdraw-abi/README.md index e14fa84..25cb222 100644 --- a/Tools/quickdraw-abi/README.md +++ b/Tools/quickdraw-abi/README.md @@ -53,12 +53,22 @@ headers change only through `generate-headers.py `. arcs, regions and polygons; - `OpenRgn` and `CloseRgn`, `OpenPoly`, `ClosePoly`, `KillPoly` and `OffsetPoly`, and `MapPt`, `ScalePt`, `MapRect`, `MapRgn` and `MapPoly`; -- `CopyBits` and `CopyMask` between black and white bitmaps. +- `CopyBits` and `CopyMask`, between bitmaps and pixel maps of any depth; +- graphics worlds: `NewGWorld`, `UpdateGWorld`, `DisposeGWorld`, `GetGWorld`, + `SetGWorld`, `GetGWorldDevice`, `GetGWorldPixMap`, `LockPixels`, + `UnlockPixels`, `GetPixBaseAddr`, `GetPixelsState`, `SetPixelsState`, + `AllowPurgePixels`, `NoPurgePixels`, `PixMap32Bit`, `QDDone` and + `OffscreenVersion`; +- `GetMainDevice`, `GetDeviceList`, `GetNextDevice`, `GetGDevice` and + `SetGDevice`; +- `RGBForeColor`, `RGBBackColor`, `GetForeColor`, `GetBackColor`, `GetCPixel` + and `SetCPixel`. Every other function in `symbols.txt` is exported as a stub that sets `unimpErr` (-4), which `QDError` returns, and gives zero; set -`QUICKDRAW_TRACE_UNIMPLEMENTED` to have each one name itself. Color ports, -GWorlds, `CopyDeepMask` and pictures are stubs. +`QUICKDRAW_TRACE_UNIMPLEMENTED` to have each one name itself. Color ports of +the program's own (`OpenCPort`), pixel patterns, palettes, `CopyDeepMask` and +pictures are stubs. The behaviour is Graphite's engine's. The port, pen and text-setting calls replay what Mac OS 9.2.2 left in a GrafPort @@ -72,7 +82,8 @@ rectangle differs. So are `CopyBits` and `CopyMask` (`ReferenceTests/Retro68/CopyBitsCapture.c`); only a source rectangle outside its bitmap differs, which Graphite doesn't read. Text is compared in a bitmap font of the probe's own (`ReferenceTests/Retro68/TextCapture.c`); a few pixels -differ in bold condensed text and at 9 points. +differ in bold condensed text and at 9 points. Graphics worlds are compared +too (`ReferenceTests/Retro68/GWorldCapture.c`); what differs is listed below. ## Text and fonts @@ -87,7 +98,23 @@ yet. There is no display. `qd.screenBits`, and so a new port, is 640 by 480 pixels of memory, 1 bit deep. A program that wants its pixels reads them there, or points -the port at its own bitmap with `SetPortBits`. +the port at its own bitmap with `SetPortBits`. Its device is the only one. + +## Graphics worlds + +A world is a color port with pixels of 1, 2, 4, 8, 16 or 32 bits, big-endian on +every host. It differs from a Macintosh's in that: + +- depth 0 means the screen's, which is 1 bit here; +- its pixels never move and are never purged: baseAddr is always a pointer, not + a handle, and `LockPixels` only sets the state; +- `UpdateGWorld` keeps the world and its pixel map where they are. Without + `clipPix` or `stretchPix` it keeps the pixels there were, and `stretchPix` + does not mix colors as a Macintosh did; +- disposing of the world that is the current port leaves no current port, so + drawing does nothing until one is set. + +`CharExtra` still does nothing, in a color port too. ## Structures and ownership @@ -114,9 +141,16 @@ The program reads QuickDraw's structures, so their layout is part of the API. to NULL, so drawing in a closed port that is still current does nothing. - `InitGraf` takes the address of a `QDGlobals`' `thePort` and fills in the fields before it. -- A line, shape or `GetPixel` needs `portBits.rowBytes` long enough for the - bounds; a port whose `rowBytes` has its high bits set (a color port) draws - nothing. - -`CGrafPort`, `PixMap` and the other structures a program reaches into come with -the calls that use them. +- A line, shape or `GetPixel` needs `rowBytes` long enough for the bounds. + +- A world's port, pixel map, color table and device are the library's, and + `DisposeGWorld` frees them (not a device the program gave with + `noNewDevice`). Their handles are not Memory Manager handles. The program + reads them; change a world's colors and bounds with calls. +- The only color ports are the ones `NewGWorld` makes. `SetPort` takes one, + cast to `GrafPtr`, and `CopyBits` takes `&((GrafPtr)world)->portBits`. +- On 64-bit hosts `CGrafPort`'s fields are in another order than a Macintosh's, + so that `portVersion` and `portPixMap` stay where `portBits` tells a color + port from a `GrafPort`. `portRect`, `visRgn` and `clipRgn` are at the same + offsets in both records; `pnLoc`, `txFont` and the later fields are not, so + read those through the right type. diff --git a/Tools/quickdraw-abi/conformance/drawing.c b/Tools/quickdraw-abi/conformance/drawing.c index ea0fbde..a7a93fb 100644 --- a/Tools/quickdraw-abi/conformance/drawing.c +++ b/Tools/quickdraw-abi/conformance/drawing.c @@ -220,6 +220,43 @@ int main(void) CHECK(QDError() == noErr); } + { + GWorldPtr world = NULL; + CGrafPtr old; + GDHandle device; + PixMapHandle map; + RGBColor red = { 0xFFFF, 0, 0 }, color; + Rect all; + + SetRect(&all, 0, 0, 32, 16); + CHECK(NewGWorld(&world, 32, &all, NULL, NULL, 0) == noErr && world != NULL); + map = GetGWorldPixMap(world); + CHECK(map == world->portPixMap && (**map).pixelSize == 32); + CHECK(((GrafPtr)world)->portRect.right == 32 && ((GrafPtr)world)->visRgn == world->visRgn); + GetGWorld(&old, &device); + CHECK(old == (CGrafPtr)&port && device == GetMainDevice()); + SetGWorld(world, NULL); + CHECK(GetGDevice() == GetGWorldDevice(world) && (**GetGDevice()).gdRect.bottom == 16); + CHECK(LockPixels(map) && GetPixBaseAddr(map) == (**map).baseAddr); + EraseRect(&all); + RGBForeColor(&red); + PaintRect(&all); + GetCPixel(3, 3, &color); + CHECK(color.red == 0xFFFF && color.green == 0 && color.blue == 0); + CHECK(world->fgColor == 0xFF0000); + ForeColor(blackColor); + SetGWorld(old, device); + clear(); + CopyBits(&((GrafPtr)world)->portBits, &port.portBits, &all, &all, srcCopy, NULL); + CHECK(black() == 32 * 16); + UnlockPixels(map); + SetRect(&all, 0, 0, 64, 16); + UpdateGWorld(&world, 8, &all, NULL, NULL, clipPix); + CHECK(QDError() == noErr && world->portRect.right == 64 && (**world->portPixMap).pixelSize == 8); + DisposeGWorld(world); + CHECK(QDError() == noErr); + } + ClosePort(&port); clear(); PaintRect(&box); diff --git a/Tools/quickdraw-abi/generate-headers.py b/Tools/quickdraw-abi/generate-headers.py index 5fa4869..eeab03f 100755 --- a/Tools/quickdraw-abi/generate-headers.py +++ b/Tools/quickdraw-abi/generate-headers.py @@ -78,6 +78,20 @@ def new(self, name: str) -> bool: self.seen.add(name) return True + def define(self, name: str, members: list[dict]) -> str: + """A struct's definition.""" + def body(list): + return f"struct {name} {{\n" + "".join(f" {declare(m['name'], m['type'])};\n" for m in list) + "};" + if name != "CGrafPort": + return body(members) + # With 8 byte pointers, Mac OS's order would put portRect, visRgn and clipRgn + # elsewhere than a GrafPort has them, and programs reach a color port through + # a GrafPtr. This order keeps those three where they are. + order = ["device", "portPixMap", "portVersion", "chExtra", "pnLocHFrac", "grafVars"] + named = {m["name"]: m for m in members} + wide = [named[n] for n in order] + [m for m in members if m["name"] not in order] + return f"#if __SIZEOF_POINTER__ == 8\n{body(wide)}\n#else\n{body(members)}\n#endif" + def add(self, entry: dict) -> None: if "enum" in entry: enum = entry["enum"] @@ -111,10 +125,10 @@ def add(self, entry: dict) -> None: if self.new(name): forward = f"typedef struct {name} {name};" self.forward.append(unless_apple(forward) if name in APPLE else forward) - if struct.get("members"): - members = "".join(f" {declare(m['name'], m['type'])};\n" for m in struct["members"]) - text = f"struct {name} {{\n{members}}};" - self.body.append(unless_apple(text) if name in APPLE else text) + # A struct may be named on its own before its members are given. + if struct.get("members") and name not in SKIPPED and self.new("members " + name): + text = self.define(name, struct["members"]) + self.body.append(unless_apple(text) if name in APPLE else text) elif "funptr" in entry: funptr = entry["funptr"] if self.new(funptr["name"]): diff --git a/Tools/quickdraw-abi/implemented.txt b/Tools/quickdraw-abi/implemented.txt index ea9921a..fdc5d7f 100644 --- a/Tools/quickdraw-abi/implemented.txt +++ b/Tools/quickdraw-abi/implemented.txt @@ -120,3 +120,31 @@ TextWidth MeasureText GetFontInfo CharExtra +NewGWorld +DisposeGWorld +UpdateGWorld +GetGWorld +SetGWorld +GetGWorldDevice +GetGWorldPixMap +LockPixels +UnlockPixels +GetPixBaseAddr +GetPixelsState +SetPixelsState +AllowPurgePixels +NoPurgePixels +PixMap32Bit +QDDone +OffscreenVersion +GetMainDevice +GetDeviceList +GetNextDevice +GetGDevice +SetGDevice +RGBForeColor +RGBBackColor +GetForeColor +GetBackColor +GetCPixel +SetCPixel