diff --git a/ReferenceTests/Retro68/CMakeLists.txt b/ReferenceTests/Retro68/CMakeLists.txt index f0a4966..630c9c7 100644 --- a/ReferenceTests/Retro68/CMakeLists.txt +++ b/ReferenceTests/Retro68/CMakeLists.txt @@ -24,3 +24,4 @@ add_application(PolygonCapture PolygonCapture.c) add_application(CopyBitsCapture CopyBitsCapture.c) add_application(TextCapture TextCapture.c TextCapture.r) add_application(GWorldCapture GWorldCapture.c) +add_application(UtilitiesCapture UtilitiesCapture.c) diff --git a/ReferenceTests/Retro68/README.md b/ReferenceTests/Retro68/README.md index 70fce6d..79101b7 100644 --- a/ReferenceTests/Retro68/README.md +++ b/ReferenceTests/Retro68/README.md @@ -741,3 +741,31 @@ 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. +## Utilities + +`UtilitiesCapture.c` records `Random` from a range of seeds, `StuffHex`, +`PtToAngle` over rectangles of several shapes, `SlopeFromAngle` and where +`AngleFromSlope` changes its answer, and the Color Picker's conversions. It ran +from the Finder on Mac OS 9.2.2 in UTM. It writes `Utilities.txt`, kept here as +`Utilities-macos-9.2.2-ppc.txt` (CR line endings). + +- `Random` stepped `randSeed`, taken as unsigned, by 16807 modulo 2^31 - 1, and + gave 0 for a low word of `0x8000`. +- `StuffHex` left out a last character without a pair and refused nothing: a + character up to `'9'`, or from `0x80`, gave its low four bits, any other those + of its code plus 9. +- `AngleFromSlope` stayed with a degree until the slope was 501 past + `SlopeFromAngle`'s, which from 73 degrees on was not quite the tangent in + fixed point. `PtToAngle`'s answers all follow from that. +- `PtToAngle`'s center was the rectangle's sides added and shifted down. A + point straight above or below it gave 0 or 180; then a rectangle without + width gave 90 or 270, and one without height 180 or 0. +- `Fix2SmallFract` gave `0xFFFF` for 1 and above, and a negative number's low + word. +- `RGB2HSL` and `RGB2HSV` rounded saturation to the nearest and hue down; + converting back rounded each product to the nearest, so colors did not always + survive the round trip. + +`UtilitiesCaptureTests` makes the probe's calls through libQuickDraw and +compares every line but the slope calls', which are not QuickDraw's. All match. + diff --git a/ReferenceTests/Retro68/Utilities-macos-9.2.2-ppc.txt b/ReferenceTests/Retro68/Utilities-macos-9.2.2-ppc.txt new file mode 100644 index 0000000..25bcda9 --- /dev/null +++ b/ReferenceTests/Retro68/Utilities-macos-9.2.2-ppc.txt @@ -0,0 +1 @@ +randSeed after InitGraf -> 1 Random from 00000001 -> 16807:000041A7 15089:10D63AF1 -21287:60B7ACD9 3114:3AB50C2A -18558:4431B782 -9528:1C06DAC8 -28968:06058ED8 2558:56E509FE Random from 00000000 -> 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 Random from FFFFFFFF -> 16807:000041A7 15089:10D63AF1 -21287:60B7ACD9 3114:3AB50C2A -18558:4431B782 -9528:1C06DAC8 -28968:06058ED8 2558:56E509FE Random from 00000002 -> -31922:0000834E 30178:21AC75E2 22963:416F59B3 6228:756A1854 28421:08636F05 -19056:380DB590 7600:0C0B1DB0 5117:2DCA13FD Random from 00003039 -> -4561:0C5DEE2F 22016:6AC05600 20672:794E50C0 -21640:0497AB78 21923:017A55A3 16663:068C4117 20572:6205505C -1462:4ADEFA4A Random from 7FFFFFFE -> -16808:7FFFBE58 -15090:6F29C50E 21286:1F485326 -3115:454AF3D5 18557:3BCE487D 9527:63F92537 28967:79FA7127 -2559:291AF601 Random from 7FFFFFFF -> 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 Random from 80000000 -> 16807:000041A7 15089:10D63AF1 -21287:60B7ACD9 3114:3AB50C2A -18558:4431B782 -9528:1C06DAC8 -28968:06058ED8 2558:56E509FE Random from 80000001 -> -31922:0000834E 30178:21AC75E2 22963:416F59B3 6228:756A1854 28421:08636F05 -19056:380DB590 7600:0C0B1DB0 5117:2DCA13FD Random from FFFFFFFE -> 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 0:00000000 Random from 392453E7 -> 0:00008000 0:20D38000 -28458:1D7890D6 -8009:566CE0B7 15029:06153AB5 17457:5BC14431 7174:6D641C06 1541:476C0605 Random from 2B6CFBB6 -> 0:00018000 0:627A8000 -19838:5869B282 -24025:0346A227 -20449:123FB01F -13163:1343CC95 21524:482C5414 4624:56441210 Random from 653CE19E -> 32767:00007FFF 15961:20D33E59 21989:0CA255E5 13277:75B533DD 11914:4B602E8A -29521:178F8CAF 16702:515D413E 30509:4166772D Random from 1E613586 -> -1:0000FFFF -16807:41A6BE59 -6469:2A1AE6BB 5269:4C221495 26943:5175693F -12064:7350D0E0 23877:3EC15D45 32051:08D27D33 StuffHex digits -> 01 23 45 67 89 AB CD EF A5 A5 A5 A5 StuffHex lowercase -> AB CD EF AB CD EF A5 A5 A5 A5 A5 A5 StuffHex odd length -> FF A5 A5 A5 A5 A5 A5 A5 A5 A5 A5 A5 StuffHex one character -> A5 A5 A5 A5 A5 A5 A5 A5 A5 A5 A5 A5 StuffHex empty -> A5 A5 A5 A5 A5 A5 A5 A5 A5 A5 A5 A5 StuffHex beyond F -> 01 23 45 67 89 AB CD EF A5 A5 A5 A5 StuffHex lowercase beyond f -> 01 23 45 67 89 AB CD EF A5 A5 A5 A5 StuffHex punctuation -> 01 34 56 89 AB CD EF A5 A5 A5 A5 A5 StuffHex more punctuation -> 45 67 89 46 78 94 56 A5 A5 A5 A5 A5 StuffHex high characters -> 01 F0 00 EF A5 A5 A5 A5 A5 A5 A5 A5 PtToAngle square (0,0,100,100) step 10 row -6 -> 315 320 326 333 341 350 0 10 19 27 34 40 45 PtToAngle square (0,0,100,100) step 10 row -5 -> 309 315 321 329 338 349 0 11 22 31 39 45 51 PtToAngle square (0,0,100,100) step 10 row -4 -> 303 308 315 323 333 346 0 14 27 37 45 52 57 PtToAngle square (0,0,100,100) step 10 row -3 -> 296 301 307 315 326 341 0 19 34 45 53 59 64 PtToAngle square (0,0,100,100) step 10 row -2 -> 288 291 296 303 315 333 0 27 45 57 64 69 72 PtToAngle square (0,0,100,100) step 10 row -1 -> 279 281 284 288 296 315 0 45 64 72 76 79 81 PtToAngle square (0,0,100,100) step 10 row 0 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle square (0,0,100,100) step 10 row 1 -> 261 259 256 252 244 225 180 135 116 108 104 101 99 PtToAngle square (0,0,100,100) step 10 row 2 -> 252 249 244 237 225 207 180 153 135 123 116 111 108 PtToAngle square (0,0,100,100) step 10 row 3 -> 244 239 233 225 214 199 180 161 146 135 127 121 116 PtToAngle square (0,0,100,100) step 10 row 4 -> 237 232 225 217 207 194 180 166 153 143 135 128 123 PtToAngle square (0,0,100,100) step 10 row 5 -> 231 225 219 211 202 191 180 169 158 149 141 135 129 PtToAngle square (0,0,100,100) step 10 row 6 -> 225 220 214 207 199 190 180 170 161 153 146 140 135 PtToAngle wide (0,0,100,200) step 10 row -6 -> 333 337 341 346 350 355 0 5 10 14 19 23 27 PtToAngle wide (0,0,100,200) step 10 row -5 -> 329 333 338 343 349 354 0 6 11 17 22 27 31 PtToAngle wide (0,0,100,200) step 10 row -4 -> 323 328 333 339 346 353 0 7 14 21 27 32 37 PtToAngle wide (0,0,100,200) step 10 row -3 -> 315 320 326 333 341 350 0 10 19 27 34 40 45 PtToAngle wide (0,0,100,200) step 10 row -2 -> 303 308 315 323 333 346 0 14 27 37 45 52 57 PtToAngle wide (0,0,100,200) step 10 row -1 -> 288 291 296 303 315 333 0 27 45 57 64 69 72 PtToAngle wide (0,0,100,200) step 10 row 0 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle wide (0,0,100,200) step 10 row 1 -> 252 249 244 237 225 207 180 153 135 123 116 111 108 PtToAngle wide (0,0,100,200) step 10 row 2 -> 237 232 225 217 207 194 180 166 153 143 135 128 123 PtToAngle wide (0,0,100,200) step 10 row 3 -> 225 220 214 207 199 190 180 170 161 153 146 140 135 PtToAngle wide (0,0,100,200) step 10 row 4 -> 217 212 207 201 194 187 180 173 166 159 153 148 143 PtToAngle wide (0,0,100,200) step 10 row 5 -> 211 207 202 197 191 186 180 174 169 163 158 153 149 PtToAngle wide (0,0,100,200) step 10 row 6 -> 207 203 199 194 190 185 180 175 170 166 161 157 153 PtToAngle tall (0,0,200,100) step 10 row -6 -> 296 301 307 315 326 341 0 19 34 45 53 59 64 PtToAngle tall (0,0,200,100) step 10 row -5 -> 292 296 302 309 321 338 0 22 39 51 58 64 68 PtToAngle tall (0,0,200,100) step 10 row -4 -> 288 291 296 303 315 333 0 27 45 57 64 69 72 PtToAngle tall (0,0,200,100) step 10 row -3 -> 284 286 290 296 307 326 0 34 53 64 70 74 76 PtToAngle tall (0,0,200,100) step 10 row -2 -> 279 281 284 288 296 315 0 45 64 72 76 79 81 PtToAngle tall (0,0,200,100) step 10 row -1 -> 274 275 277 279 284 296 0 64 76 81 83 85 86 PtToAngle tall (0,0,200,100) step 10 row 0 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle tall (0,0,200,100) step 10 row 1 -> 266 265 263 261 256 244 180 116 104 99 97 95 94 PtToAngle tall (0,0,200,100) step 10 row 2 -> 261 259 256 252 244 225 180 135 116 108 104 101 99 PtToAngle tall (0,0,200,100) step 10 row 3 -> 256 254 250 244 233 214 180 146 127 116 110 106 104 PtToAngle tall (0,0,200,100) step 10 row 4 -> 252 249 244 237 225 207 180 153 135 123 116 111 108 PtToAngle tall (0,0,200,100) step 10 row 5 -> 248 244 238 231 219 202 180 158 141 129 122 116 112 PtToAngle tall (0,0,200,100) step 10 row 6 -> 244 239 233 225 214 199 180 161 146 135 127 121 116 PtToAngle odd (0,0,7,13) step 1 row -6 -> 332 336 340 345 350 355 0 5 10 15 20 24 28 PtToAngle odd (0,0,7,13) step 1 row -5 -> 327 332 337 342 348 354 0 6 12 18 23 28 33 PtToAngle odd (0,0,7,13) step 1 row -4 -> 321 326 332 338 345 352 0 8 15 22 28 34 39 PtToAngle odd (0,0,7,13) step 1 row -3 -> 313 318 324 332 340 350 0 10 20 28 36 42 47 PtToAngle odd (0,0,7,13) step 1 row -2 -> 301 306 313 321 332 345 0 15 28 39 47 54 59 PtToAngle odd (0,0,7,13) step 1 row -1 -> 287 290 294 301 313 332 0 28 47 59 66 70 73 PtToAngle odd (0,0,7,13) step 1 row 0 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle odd (0,0,7,13) step 1 row 1 -> 253 250 246 239 227 208 180 152 133 121 114 110 107 PtToAngle odd (0,0,7,13) step 1 row 2 -> 239 234 227 219 208 195 180 165 152 141 133 126 121 PtToAngle odd (0,0,7,13) step 1 row 3 -> 227 222 216 208 200 190 180 170 160 152 144 138 133 PtToAngle odd (0,0,7,13) step 1 row 4 -> 219 214 208 202 195 188 180 172 165 158 152 146 141 PtToAngle odd (0,0,7,13) step 1 row 5 -> 213 208 203 198 192 186 180 174 168 162 157 152 147 PtToAngle odd (0,0,7,13) step 1 row 6 -> 208 204 200 195 190 185 180 175 170 165 160 156 152 PtToAngle away (-30,40,11,63) step 3 row -6 -> 298 302 308 316 328 342 0 18 32 44 52 58 62 PtToAngle away (-30,40,11,63) step 3 row -5 -> 293 297 303 311 322 339 0 21 38 49 57 63 67 PtToAngle away (-30,40,11,63) step 3 row -4 -> 288 292 297 304 316 334 0 26 44 56 63 68 72 PtToAngle away (-30,40,11,63) step 3 row -3 -> 284 286 290 296 306 326 0 34 54 64 70 74 76 PtToAngle away (-30,40,11,63) step 3 row -2 -> 278 280 283 287 295 313 0 47 65 73 77 80 82 PtToAngle away (-30,40,11,63) step 3 row -1 -> 273 274 275 277 280 290 0 70 80 83 85 86 87 PtToAngle away (-30,40,11,63) step 3 row 0 -> 269 268 268 267 265 260 180 100 95 93 92 92 91 PtToAngle away (-30,40,11,63) step 3 row 1 -> 263 262 260 256 250 234 180 126 110 104 100 98 97 PtToAngle away (-30,40,11,63) step 3 row 2 -> 258 256 252 247 237 218 180 142 123 113 108 104 102 PtToAngle away (-30,40,11,63) step 3 row 3 -> 253 250 245 238 227 208 180 152 133 122 115 110 107 PtToAngle away (-30,40,11,63) step 3 row 4 -> 248 244 239 231 220 202 180 158 140 129 121 116 112 PtToAngle away (-30,40,11,63) step 3 row 5 -> 244 239 234 225 214 199 180 161 146 135 126 121 116 PtToAngle away (-30,40,11,63) step 3 row 6 -> 240 235 229 220 210 196 180 164 150 140 131 125 120 PtToAngle empty (10,10,10,10) step 5 row -6 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row -5 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row -4 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row -3 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row -2 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row -1 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row 0 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row 1 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row 2 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row 3 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row 4 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row 5 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle empty (10,10,10,10) step 5 row 6 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row -6 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row -5 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row -4 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row -3 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row -2 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row -1 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row 0 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row 1 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row 2 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row 3 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row 4 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row 5 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle no width (0,10,40,10) step 5 row 6 -> 270 270 270 270 270 270 180 90 90 90 90 90 90 PtToAngle no height (10,0,10,40) step 5 row -6 -> 0 0 0 0 0 0 0 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row -5 -> 0 0 0 0 0 0 0 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row -4 -> 0 0 0 0 0 0 0 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row -3 -> 0 0 0 0 0 0 0 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row -2 -> 0 0 0 0 0 0 0 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row -1 -> 0 0 0 0 0 0 0 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row 0 -> 0 0 0 0 0 0 0 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row 1 -> 0 0 0 0 0 0 180 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row 2 -> 0 0 0 0 0 0 180 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row 3 -> 0 0 0 0 0 0 180 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row 4 -> 0 0 0 0 0 0 180 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row 5 -> 0 0 0 0 0 0 180 180 180 180 180 180 180 PtToAngle no height (10,0,10,40) step 5 row 6 -> 0 0 0 0 0 0 180 180 180 180 180 180 180 PtToAngle inverted (100,200,0,0) step 10 row -6 -> 333 337 341 346 350 355 0 5 10 14 19 23 27 PtToAngle inverted (100,200,0,0) step 10 row -5 -> 329 333 338 343 349 354 0 6 11 17 22 27 31 PtToAngle inverted (100,200,0,0) step 10 row -4 -> 323 328 333 339 346 353 0 7 14 21 27 32 37 PtToAngle inverted (100,200,0,0) step 10 row -3 -> 315 320 326 333 341 350 0 10 19 27 34 40 45 PtToAngle inverted (100,200,0,0) step 10 row -2 -> 303 308 315 323 333 346 0 14 27 37 45 52 57 PtToAngle inverted (100,200,0,0) step 10 row -1 -> 288 291 296 303 315 333 0 27 45 57 64 69 72 PtToAngle inverted (100,200,0,0) step 10 row 0 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle inverted (100,200,0,0) step 10 row 1 -> 252 249 244 237 225 207 180 153 135 123 116 111 108 PtToAngle inverted (100,200,0,0) step 10 row 2 -> 237 232 225 217 207 194 180 166 153 143 135 128 123 PtToAngle inverted (100,200,0,0) step 10 row 3 -> 225 220 214 207 199 190 180 170 161 153 146 140 135 PtToAngle inverted (100,200,0,0) step 10 row 4 -> 217 212 207 201 194 187 180 173 166 159 153 148 143 PtToAngle inverted (100,200,0,0) step 10 row 5 -> 211 207 202 197 191 186 180 174 169 163 158 153 149 PtToAngle inverted (100,200,0,0) step 10 row 6 -> 207 203 199 194 190 185 180 175 170 166 161 157 153 PtToAngle one pixel (0,0,1,1) step 1 row -6 -> 315 320 326 333 341 350 0 10 19 27 34 40 45 PtToAngle one pixel (0,0,1,1) step 1 row -5 -> 309 315 321 329 338 349 0 11 22 31 39 45 51 PtToAngle one pixel (0,0,1,1) step 1 row -4 -> 303 308 315 323 333 346 0 14 27 37 45 52 57 PtToAngle one pixel (0,0,1,1) step 1 row -3 -> 296 301 307 315 326 341 0 19 34 45 53 59 64 PtToAngle one pixel (0,0,1,1) step 1 row -2 -> 288 291 296 303 315 333 0 27 45 57 64 69 72 PtToAngle one pixel (0,0,1,1) step 1 row -1 -> 279 281 284 288 296 315 0 45 64 72 76 79 81 PtToAngle one pixel (0,0,1,1) step 1 row 0 -> 270 270 270 270 270 270 0 90 90 90 90 90 90 PtToAngle one pixel (0,0,1,1) step 1 row 1 -> 261 259 256 252 244 225 180 135 116 108 104 101 99 PtToAngle one pixel (0,0,1,1) step 1 row 2 -> 252 249 244 237 225 207 180 153 135 123 116 111 108 PtToAngle one pixel (0,0,1,1) step 1 row 3 -> 244 239 233 225 214 199 180 161 146 135 127 121 116 PtToAngle one pixel (0,0,1,1) step 1 row 4 -> 237 232 225 217 207 194 180 166 153 143 135 128 123 PtToAngle one pixel (0,0,1,1) step 1 row 5 -> 231 225 219 211 202 191 180 169 158 149 141 135 129 PtToAngle one pixel (0,0,1,1) step 1 row 6 -> 225 220 214 207 199 190 180 170 161 153 146 140 135 PtToAngle sweep square (0,0,100,100) -> 296 297 297 297 298 298 298 299 299 300 300 301 301 301 302 302 303 304 304 305 305 306 306 307 308 308 309 310 310 311 312 313 314 314 315 316 317 318 319 320 321 322 323 324 326 327 328 329 331 332 333 335 336 338 339 341 342 344 345 347 349 350 352 354 355 357 359 1 2 4 6 7 9 11 12 14 16 17 19 20 22 23 25 26 28 29 30 32 33 34 35 36 37 39 40 41 42 43 43 44 45 46 47 48 49 49 50 51 51 52 53 53 54 55 55 56 56 57 57 58 58 59 59 60 60 61 61 61 62 62 63 63 63 64 PtToAngle sweep wide (0,0,100,300) -> 326 327 327 327 328 328 329 329 329 330 330 331 331 332 332 333 333 333 334 334 335 335 336 336 337 337 338 338 339 339 340 340 341 341 342 342 343 343 344 344 345 346 346 347 347 348 348 349 349 350 350 351 352 352 353 353 354 354 355 356 356 357 357 358 358 359 0 0 1 1 2 3 3 4 4 5 5 6 7 7 8 8 9 9 10 10 11 12 12 13 13 14 14 15 15 16 16 17 17 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 25 26 26 27 27 28 28 29 29 30 30 30 31 31 32 32 33 33 33 34 PtToAngle sweep tall (0,0,300,100) -> 279 279 279 279 280 280 280 280 280 280 281 281 281 281 281 282 282 282 282 283 283 283 283 284 284 284 285 285 286 286 286 287 287 288 288 289 289 290 291 291 292 293 294 295 296 297 298 299 300 302 303 305 307 309 311 313 316 319 322 325 329 333 337 342 346 351 357 2 7 12 17 21 26 30 34 37 40 43 46 48 51 53 54 56 58 59 61 62 63 64 65 66 67 68 68 69 70 70 71 71 72 73 73 73 74 74 75 75 75 76 76 76 77 77 77 78 78 78 78 78 79 79 79 79 80 80 80 80 80 80 81 81 81 81 PtToAngle sweep odd (0,0,41,23) -> 285 285 286 286 286 286 287 287 287 288 288 288 288 289 289 289 290 290 291 291 291 292 292 293 293 294 294 295 295 296 297 297 298 299 299 300 301 302 303 304 305 306 307 308 309 310 312 313 315 316 318 320 322 324 326 328 330 333 335 338 340 343 346 349 352 355 358 1 4 7 10 13 16 19 22 24 27 29 31 34 36 38 40 41 43 45 46 48 49 50 52 53 54 55 56 57 58 59 60 60 61 62 63 63 64 64 65 66 66 67 67 68 68 68 69 69 70 70 70 71 71 71 72 72 72 73 73 73 74 74 74 74 74 75 PtToAngle sweep huge (-16000,-16000,16000,16000) -> 296 297 297 297 298 298 298 299 299 300 300 301 301 301 302 302 303 304 304 305 305 306 306 307 308 308 309 310 310 311 312 313 314 314 315 316 317 318 319 320 321 322 323 324 326 327 328 329 331 332 333 335 336 338 339 341 342 344 345 347 349 350 352 354 355 357 359 1 2 4 6 7 9 11 12 14 16 17 19 20 22 23 25 26 28 29 30 32 33 34 35 36 37 39 40 41 42 43 43 44 45 46 47 48 49 49 50 51 51 52 53 53 54 55 55 56 56 57 57 58 58 59 59 60 60 61 61 61 62 62 63 63 63 64 SlopeFromAngle -> 00000000 FFFFFB88 FFFFF70F FFFFF295 FFFFEE19 FFFFE99A FFFFE518 FFFFE091 FFFFDC06 FFFFD774 FFFFD2DC FFFFCE3D FFFFC996 FFFFC4E6 FFFFC02C FFFFBB68 FFFFB698 FFFFB1BC FFFFACD2 FFFFA7DA FFFFA2D3 FFFF9DBB FFFF9892 FFFF9356 FFFF8E05 FFFF88A0 FFFF8324 FFFF7D90 FFFF77E2 FFFF7219 FFFF6C33 FFFF662E FFFF6009 FFFF59C0 FFFF5353 FFFF4CBF FFFF4601 FFFF3F17 FFFF37FE FFFF30B2 FFFF2931 FFFF2176 FFFF197F FFFF1147 FFFF08C9 FFFF0000 FFFEF6E7 FFFEED79 FFFEE3AF FFFED981 FFFECEE9 FFFEC3DE FFFEB856 FFFEAC47 FFFE9FA5 FFFE9265 FFFE8477 FFFE75CB FFFE6651 FFFE55F2 FFFE4498 FFFE322A FFFE1E89 FFFE0992 FFFDF31F FFFDDB02 FFFDC104 FFFDA4E7 FFFD8661 FFFD6519 FFFD40A5 FFFD1886 FFFCEC1D FFFCBAAA FFFC8339 FFFC4498 FFFBFD3E FFFBAB25 FFFB4B9E FFFADAFF FFFA5427 FFF9AFAF FFF8E278 FFF7DB0D FFF67C53 FFF491E9 FFF1B30B FFECEB43 FFE35D29 FFC6B5D0 7FFFFFFF 00394A30 001CA2D7 001314BD 000E4CF5 000B6E17 000983AD 000824F3 00071D88 00065051 0005ABD9 00052501 0004B462 000454DB 000402C2 0003BB68 00037CC7 00034556 000313E3 0002E77A 0002BF5B 00029AE7 0002799F 00025B19 00023EFC 000224FE 00020CE1 0001F66E 0001E177 0001CDD6 0001BB68 0001AA0E 000199AF 00018A35 00017B89 00016D9B 0001605B 000153B9 000147AA 00013C22 00013117 0001267F 00011C51 00011287 00010919 00010000 0000F737 0000EEB9 0000E681 0000DE8A 0000D6CF 0000CF4E 0000C802 0000C0E9 0000B9FF 0000B341 0000ACAD 0000A640 00009FF7 000099D2 000093CD 00008DE7 0000881E 00008270 00007CDC 00007760 000071FB 00006CAA 0000676E 00006245 00005D2D 00005826 0000532E 00004E44 00004968 00004498 00003FD4 00003B1A 0000366A 000031C3 00002D24 0000288C 000023FA 00001F6F 00001AE8 00001666 000011E7 00000D6B 000008F1 00000478 00000000 AngleFromSlope of zero -> 180 AngleFromSlope positive -> 00000001:180 000001F5:179 0000066D:178 00000AE6:177 00000F60:176 000013DC:175 0000185B:174 00001CDD:173 00002164:172 000025EF:171 00002A81:170 00002F19:169 000033B8:168 0000385F:167 00003D0F:166 000041C9:165 0000468D:164 00004B5D:163 00005039:162 00005523:161 00005A1B:160 00005F22:159 0000643A:158 00006963:157 00006E9F:156 000073F0:155 00007955:154 00007ED1:153 00008465:152 00008A13:151 00008FDC:150 000095C2:149 00009BC7:148 0000A1EC:147 0000A835:146 0000AEA2:145 0000B536:144 0000BBF4:143 0000C2DE:142 0000C9F7:141 0000D143:140 0000D8C4:139 0000E07F:138 0000E876:137 0000F0AE:136 0000F92C:135 000101F5:134 00010B0E:133 0001147C:132 00011E46:131 00012874:130 0001330C:129 00013E17:128 0001499F:127 000155AE:126 00016250:125 00016F90:124 00017D7E:123 00018C2A:122 00019BA4:121 0001AC03:120 0001BD5D:119 0001CFCB:118 0001E36C:117 0001F863:116 00020ED6:115 000226F3:114 000240F1:113 00025D0E:112 00027B94:111 00029CDC:110 0002C150:109 0002E96F:108 000315D8:107 0003474B:106 00037EBC:105 0003BD5D:104 000404B7:103 000456D0:102 0004B657:101 000526F6:100 0005ADCE:99 00065246:98 00071F7D:97 000826E8:96 000985A2:95 000B700C:94 000E4EEA:93 001316B2:92 001CA4CC:91 00394C25:90 AngleFromSlope negative -> 00000001:0 000001F5:1 0000066D:2 00000AE6:3 00000F60:4 000013DC:5 0000185B:6 00001CDD:7 00002164:8 000025EF:9 00002A81:10 00002F19:11 000033B8:12 0000385F:13 00003D0F:14 000041C9:15 0000468D:16 00004B5D:17 00005039:18 00005523:19 00005A1B:20 00005F22:21 0000643A:22 00006963:23 00006E9F:24 000073F0:25 00007955:26 00007ED1:27 00008465:28 00008A13:29 00008FDC:30 000095C2:31 00009BC7:32 0000A1EC:33 0000A835:34 0000AEA2:35 0000B536:36 0000BBF4:37 0000C2DE:38 0000C9F7:39 0000D143:40 0000D8C4:41 0000E07F:42 0000E876:43 0000F0AE:44 0000F92C:45 000101F5:46 00010B0E:47 0001147C:48 00011E46:49 00012874:50 0001330C:51 00013E17:52 0001499F:53 000155AE:54 00016250:55 00016F90:56 00017D7E:57 00018C2A:58 00019BA4:59 0001AC03:60 0001BD5D:61 0001CFCB:62 0001E36C:63 0001F863:64 00020ED6:65 000226F3:66 000240F1:67 00025D0E:68 00027B94:69 00029CDC:70 0002C150:71 0002E96F:72 000315D8:73 0003474B:74 00037EBC:75 0003BD5D:76 000404B7:77 000456D0:78 0004B657:79 000526F6:80 0005ADCE:81 00065246:82 00071F7D:83 000826E8:84 000985A2:85 000B700C:86 000E4EEA:87 001316B2:88 001CA4CC:89 00394C25:90 Fix2SmallFract -> 00000000:0000 00000001:0001 00007FFF:7FFF 00008000:8000 0000FFFF:FFFF 00010000:FFFF 00010001:FFFF 00018000:FFFF 7FFFFFFF:FFFF FFFFFFFF:FFFF FFFF8000:8000 FFFF0000:0000 80000000:0000 SmallFract2Fix -> 0000:00000000 0001:00000001 7FFF:00007FFF 8000:00008000 FFFE:0000FFFE FFFF:0000FFFF RGB2CMY 0000,0000,0000 -> FFFF,FFFF,FFFF back 0000,0000,0000 CMY2RGB 0000,0000,0000 -> FFFF,FFFF,FFFF RGB2CMY FFFF,FFFF,FFFF -> 0000,0000,0000 back FFFF,FFFF,FFFF CMY2RGB FFFF,FFFF,FFFF -> 0000,0000,0000 RGB2CMY 8000,8000,8000 -> 7FFF,7FFF,7FFF back 8000,8000,8000 CMY2RGB 8000,8000,8000 -> 7FFF,7FFF,7FFF RGB2CMY FFFF,0000,0000 -> 0000,FFFF,FFFF back FFFF,0000,0000 CMY2RGB FFFF,0000,0000 -> 0000,FFFF,FFFF RGB2CMY 0000,FFFF,0000 -> FFFF,0000,FFFF back 0000,FFFF,0000 CMY2RGB 0000,FFFF,0000 -> FFFF,0000,FFFF RGB2CMY 0000,0000,FFFF -> FFFF,FFFF,0000 back 0000,0000,FFFF CMY2RGB 0000,0000,FFFF -> FFFF,FFFF,0000 RGB2CMY FFFF,FFFF,0000 -> 0000,0000,FFFF back FFFF,FFFF,0000 CMY2RGB FFFF,FFFF,0000 -> 0000,0000,FFFF RGB2CMY 0000,FFFF,FFFF -> FFFF,0000,0000 back 0000,FFFF,FFFF CMY2RGB 0000,FFFF,FFFF -> FFFF,0000,0000 RGB2CMY FFFF,0000,FFFF -> 0000,FFFF,0000 back FFFF,0000,FFFF CMY2RGB FFFF,0000,FFFF -> 0000,FFFF,0000 RGB2CMY 8000,4000,C000 -> 7FFF,BFFF,3FFF back 8000,4000,C000 CMY2RGB 8000,4000,C000 -> 7FFF,BFFF,3FFF RGB2CMY 1234,5678,9ABC -> EDCB,A987,6543 back 1234,5678,9ABC CMY2RGB 1234,5678,9ABC -> EDCB,A987,6543 RGB2CMY FEDC,BA98,7654 -> 0123,4567,89AB back FEDC,BA98,7654 CMY2RGB FEDC,BA98,7654 -> 0123,4567,89AB RGB2CMY 0001,0002,0003 -> FFFE,FFFD,FFFC back 0001,0002,0003 CMY2RGB 0001,0002,0003 -> FFFE,FFFD,FFFC RGB2CMY FFFE,FFFF,FFFD -> 0001,0000,0002 back FFFE,FFFF,FFFD CMY2RGB FFFE,FFFF,FFFD -> 0001,0000,0002 RGB2CMY 2000,C000,6000 -> DFFF,3FFF,9FFF back 2000,C000,6000 CMY2RGB 2000,C000,6000 -> DFFF,3FFF,9FFF RGB2CMY C000,2000,2001 -> 3FFF,DFFF,DFFE back C000,2000,2001 CMY2RGB C000,2000,2001 -> 3FFF,DFFF,DFFE RGB2CMY 00FF,FF00,0F0F -> FF00,00FF,F0F0 back 00FF,FF00,0F0F CMY2RGB 00FF,FF00,0F0F -> FF00,00FF,F0F0 RGB2CMY 5555,AAAA,AAAA -> AAAA,5555,5555 back 5555,AAAA,AAAA CMY2RGB 5555,AAAA,AAAA -> AAAA,5555,5555 RGB2CMY 7FFF,8000,8001 -> 8000,7FFF,7FFE back 7FFF,8000,8001 CMY2RGB 7FFF,8000,8001 -> 8000,7FFF,7FFE RGB2CMY 0000,0001,0000 -> FFFF,FFFE,FFFF back 0000,0001,0000 CMY2RGB 0000,0001,0000 -> FFFF,FFFE,FFFF RGB2HSL 0000,0000,0000 -> 0000,0000,0000 back 0000,0000,0000 HSL2RGB 0000,0000,0000 -> 0000,0000,0000 RGB2HSL FFFF,FFFF,FFFF -> 0000,0000,FFFF back FFFF,FFFF,FFFF HSL2RGB FFFF,FFFF,FFFF -> FFFF,FFFE,FFFE RGB2HSL 8000,8000,8000 -> 0000,0000,8000 back 8000,8000,8000 HSL2RGB 8000,8000,8000 -> 4000,C000,C000 RGB2HSL FFFF,0000,0000 -> 0000,FFFF,7FFF back FFFE,0000,0000 HSL2RGB FFFF,0000,0000 -> 0000,0000,0000 RGB2HSL 0000,FFFF,0000 -> 5555,FFFF,7FFF back 0004,FFFE,0000 HSL2RGB 0000,FFFF,0000 -> 0000,0000,0000 RGB2HSL 0000,0000,FFFF -> AAAA,FFFF,7FFF back 0000,0004,FFFE HSL2RGB 0000,0000,FFFF -> FFFF,FFFF,FFFF RGB2HSL FFFF,FFFF,0000 -> 2AAA,FFFF,7FFF back FFFE,FFFA,0000 HSL2RGB FFFF,FFFF,0000 -> 0000,0000,0000 RGB2HSL 0000,FFFF,FFFF -> 8000,FFFF,7FFF back 0000,FFFE,FFFE HSL2RGB 0000,FFFF,FFFF -> FFFF,FFFE,FFFE RGB2HSL FFFF,0000,FFFF -> D556,FFFF,7FFF back FFFE,0000,FFF8 HSL2RGB FFFF,0000,FFFF -> FFFF,FFFF,FFFF RGB2HSL 8000,4000,C000 -> C000,8000,8000 back 7FFF,4000,C000 HSL2RGB 8000,4000,C000 -> B000,D000,D000 RGB2HSL 1234,5678,9ABC -> 9555,CA1C,5678 back 1234,5679,9ABC HSL2RGB 1234,5678,9ABC -> BCF0,95B7,7888 RGB2HSL FEDC,BA98,7654 -> 1555,FBCB,BA98 back FEDC,BA97,7654 HSL2RGB FEDC,BA98,7654 -> CC93,2015,24B0 RGB2HSL 0001,0002,0003 -> 9555,8000,0002 back 0001,0002,0003 HSL2RGB 0001,0002,0003 -> 0003,0003,0003 RGB2HSL FFFE,FFFF,FFFD -> 4000,8000,FFFE back FFFE,FFFF,FFFD HSL2RGB FFFE,FFFF,FFFD -> FFFF,FFFA,FFFA RGB2HSL 2000,C000,6000 -> 6666,B6DB,7000 back 2000,C000,6000 HSL2RGB 2000,C000,6000 -> A800,8400,1800 RGB2HSL C000,2000,2001 -> 0000,B6DB,7000 back C000,2000,2000 HSL2RGB C000,2000,2001 -> 2001,1C01,2401 RGB2HSL 00FF,FF00,0F0F -> 57B2,FE02,7FFF back 00FF,FEFF,0F11 HSL2RGB 00FF,FF00,0F0F -> 1E0F,00C2,000F RGB2HSL 5555,AAAA,AAAA -> 8000,5555,7FFF back 5555,AAA9,AAA9 HSL2RGB 5555,AAAA,AAAA -> 71C8,E38E,71C6 RGB2HSL 7FFF,8000,8001 -> 9555,0002,8000 back 7FFF,8000,8001 HSL2RGB 7FFF,8000,8001 -> 4002,C000,BFFE RGB2HSL 0000,0001,0000 -> 5555,FFFF,0000 back 0000,0000,0000 HSL2RGB 0000,0001,0000 -> 0000,0000,0000 RGB2HSV 0000,0000,0000 -> 0000,0000,0000 back 0000,0000,0000 HSV2RGB 0000,0000,0000 -> 0000,0000,0000 RGB2HSV FFFF,FFFF,FFFF -> 0000,0000,FFFF back FFFF,FFFF,FFFF HSV2RGB FFFF,FFFF,FFFF -> FFFF,0001,0007 RGB2HSV 8000,8000,8000 -> 0000,0000,8000 back 8000,8000,8000 HSV2RGB 8000,8000,8000 -> 4000,8000,8000 RGB2HSV FFFF,0000,0000 -> 0000,FFFF,FFFF back FFFF,0001,0001 HSV2RGB FFFF,0000,0000 -> 0000,0000,0000 RGB2HSV 0000,FFFF,0000 -> 5555,FFFF,FFFF back 0003,FFFF,0001 HSV2RGB 0000,FFFF,0000 -> 0000,0000,0000 RGB2HSV 0000,0000,FFFF -> AAAA,FFFF,FFFF back 0001,0005,FFFF HSV2RGB 0000,0000,FFFF -> FFFF,FFFF,FFFF RGB2HSV FFFF,FFFF,0000 -> 2AAA,FFFF,FFFF back FFFF,FFFB,0001 HSV2RGB FFFF,FFFF,0000 -> 0000,0000,0000 RGB2HSV 0000,FFFF,FFFF -> 8000,FFFF,FFFF back 0001,FFFF,FFFF HSV2RGB 0000,FFFF,FFFF -> FFFF,0001,0001 RGB2HSV FFFF,0000,FFFF -> D556,FFFF,FFFF back FFFF,0001,FFFB HSV2RGB FFFF,0000,FFFF -> FFFF,FFFF,FFFF RGB2HSV 8000,4000,C000 -> C000,AAAB,C000 back 8000,4000,C000 HSV2RGB 8000,4000,C000 -> 9000,C000,C000 RGB2HSV 1234,5678,9ABC -> 9555,E1E2,9ABC back 1234,5679,9ABC HSV2RGB 1234,5678,9ABC -> 9ABC,7CC4,6678 RGB2HSV FEDC,BA98,7654 -> 1555,8924,FEDC back FEDC,BA97,7654 HSV2RGB FEDC,BA98,7654 -> 7654,2015,2263 RGB2HSV 0001,0002,0003 -> 9555,AAAB,0003 back 0001,0002,0003 HSV2RGB 0001,0002,0003 -> 0003,0003,0003 RGB2HSV FFFE,FFFF,FFFD -> 4000,0002,FFFF back FFFE,FFFF,FFFD HSV2RGB FFFE,FFFF,FFFD -> FFFD,0001,000D RGB2HSV 2000,C000,6000 -> 6666,D555,C000 back 2000,C000,5FFF HSV2RGB 2000,C000,6000 -> 6000,4E00,1800 RGB2HSV C000,2000,2001 -> 0000,D555,C000 back C000,2000,2000 HSV2RGB C000,2000,2001 -> 1E01,1C01,2001 RGB2HSV 00FF,FF00,0F0F -> 57B2,FF00,FF00 back 00FF,FF00,0F0F HSV2RGB 00FF,FF00,0F0F -> 0F0F,0069,000F RGB2HSV 5555,AAAA,AAAA -> 8000,8000,AAAA back 5555,AAAA,AAAA HSV2RGB 5555,AAAA,AAAA -> 38E5,AAAA,38E4 RGB2HSV 7FFF,8000,8001 -> 9555,0004,8001 back 7FFF,8000,8001 HSV2RGB 7FFF,8000,8001 -> 4000,8001,7FFF RGB2HSV 0000,0001,0000 -> 5555,FFFF,0001 back 0000,0001,0000 HSV2RGB 0000,0001,0000 -> 0000,0000,0000 done -> \ No newline at end of file diff --git a/ReferenceTests/Retro68/UtilitiesCapture.c b/ReferenceTests/Retro68/UtilitiesCapture.c new file mode 100644 index 0000000..6fb700c --- /dev/null +++ b/ReferenceTests/Retro68/UtilitiesCapture.c @@ -0,0 +1,294 @@ +/* + * Captures QuickDraw's small utilities: Random and randSeed, StuffHex, + * PtToAngle and the slope calls behind it, and the Color + * Picker's color conversions. It writes + * "Utilities.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 color conversions, which are the likeliest to be + * missing, come last. + */ +#include +#include +#include +#include +#include +#include +#include + +static short out; + +static char text[6000]; +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; +} + +/* Random */ + +static void sequence(long seed, int count) +{ + int i; + qd.randSeed = seed; + start("Random from %08lX", (unsigned long)seed); + for (i = 0; i < count; i++) { + short value = Random(); + add(" %d:%08lX", value, (unsigned long)qd.randSeed); + } + end(); +} + +static void randoms(void) +{ + static const long seeds[] = { + 1, 0, -1, 2, 12345, 0x7FFFFFFE, 0x7FFFFFFF, (long)0x80000000, (long)0x80000001, + (long)0xFFFFFFFE, 0x392453E7, 0x2B6CFBB6, 0x653CE19E, 0x1E613586 + }; + int i; + start("randSeed after InitGraf"); + add(" %ld", (long)qd.randSeed); + end(); + for (i = 0; i < sizeof seeds / sizeof seeds[0]; i++) + sequence(seeds[i], 8); +} + +/* StuffHex */ + +static void stuff(const char *label, const unsigned char *hex) +{ + unsigned char bytes[12]; + int i; + memset(bytes, 0xA5, sizeof bytes); + StuffHex(bytes, hex); + start("StuffHex %s", label); + for (i = 0; i < sizeof bytes; i++) + add(" %02X", bytes[i]); + end(); +} + +static void stuffs(void) +{ + stuff("digits", "\p0123456789ABCDEF"); + stuff("lowercase", "\pabcdefABCDEF"); + stuff("odd length", "\pFFF"); + stuff("one character", "\pF"); + stuff("empty", "\p"); + stuff("beyond F", "\pGHIJKLMNOPQRSTUV"); + stuff("lowercase beyond f", "\pghijklmnopqrstuv"); + stuff("punctuation", "\p !#$%&()*+,-./:"); + stuff("more punctuation", "\p;<=>?@[]^_`{|}~"); + stuff("high characters", "\p\x80\x81\x9F\xA0\xC0\xF0\xFE\xFF"); +} + +/* PtToAngle */ + +static void angles(const char *label, Rect r, short step) +{ + short midV = (r.top + r.bottom) / 2, midH = (r.left + r.right) / 2; + short dv, dh; + for (dv = -6; dv <= 6; dv++) { + start("PtToAngle %s (%d,%d,%d,%d) step %d row %d", label, r.top, r.left, r.bottom, r.right, step, dv); + for (dh = -6; dh <= 6; dh++) { + Point p; + short angle = -1; + p.v = midV + dv * step; + p.h = midH + dh * step; + PtToAngle(&r, p, &angle); + add(" %d", angle); + } + end(); + } +} + +/* Points around a quarter turn, finely enough to see the rounding. */ +static void sweep(const char *label, Rect r) +{ + short h; + start("PtToAngle sweep %s (%d,%d,%d,%d)", label, r.top, r.left, r.bottom, r.right); + for (h = -200; h <= 200; h += 3) { + Point p; + short angle = -1; + p.v = (r.top + r.bottom) / 2 - 100; + p.h = (r.left + r.right) / 2 + h; + PtToAngle(&r, p, &angle); + add(" %d", angle); + } + end(); +} + +static void ptAngles(void) +{ + angles("square", rect(0, 0, 100, 100), 10); + angles("wide", rect(0, 0, 100, 200), 10); + angles("tall", rect(0, 0, 200, 100), 10); + angles("odd", rect(0, 0, 7, 13), 1); + angles("away", rect(-30, 40, 11, 63), 3); + angles("empty", rect(10, 10, 10, 10), 5); + angles("no width", rect(0, 10, 40, 10), 5); + angles("no height", rect(10, 0, 10, 40), 5); + angles("inverted", rect(100, 200, 0, 0), 10); + angles("one pixel", rect(0, 0, 1, 1), 1); + sweep("square", rect(0, 0, 100, 100)); + sweep("wide", rect(0, 0, 100, 300)); + sweep("tall", rect(0, 0, 300, 100)); + sweep("odd", rect(0, 0, 41, 23)); + sweep("huge", rect(-16000, -16000, 16000, 16000)); +} + +/* The slopes PtToAngle's angles come from */ + +/* The least magnitude of slope, with the sign given, at which AngleFromSlope + * leaves the answer it gives for the smallest one for each further answer. */ +static void thresholds(const char *label, long sign) +{ + long low = 1; + short angle = AngleFromSlope(sign); + int count = 0; + start("AngleFromSlope %s", label); + add(" 00000001:%d", angle); + while (count < 100) { + long under = low, over = 0x7FFFFFFF; + if (AngleFromSlope(sign * over) == angle) break; + while (over - under > 1) { + long middle = under + (over - under) / 2; + if (AngleFromSlope(sign * middle) == angle) under = middle; else over = middle; + } + low = over; + angle = AngleFromSlope(sign * low); + add(" %08lX:%d", (unsigned long)low, angle); + count++; + } + end(); +} + +static void slopes(void) +{ + int angle; + start("SlopeFromAngle"); + for (angle = 0; angle <= 180; angle++) + add(" %08lX", (unsigned long)SlopeFromAngle(angle)); + end(); + start("AngleFromSlope of zero"); + add(" %d", AngleFromSlope(0)); + end(); + thresholds("positive", 1); + thresholds("negative", -1); +} + +/* The Color Picker's conversions */ + +static const unsigned short colors[][3] = { + {0, 0, 0}, {0xFFFF, 0xFFFF, 0xFFFF}, {0x8000, 0x8000, 0x8000}, + {0xFFFF, 0, 0}, {0, 0xFFFF, 0}, {0, 0, 0xFFFF}, + {0xFFFF, 0xFFFF, 0}, {0, 0xFFFF, 0xFFFF}, {0xFFFF, 0, 0xFFFF}, + {0x8000, 0x4000, 0xC000}, {0x1234, 0x5678, 0x9ABC}, {0xFEDC, 0xBA98, 0x7654}, + {1, 2, 3}, {0xFFFE, 0xFFFF, 0xFFFD}, {0x2000, 0xC000, 0x6000}, + {0xC000, 0x2000, 0x2001}, {0x00FF, 0xFF00, 0x0F0F}, {0x5555, 0xAAAA, 0xAAAA}, + {0x7FFF, 0x8000, 0x8001}, {0, 1, 0}, +}; + +static void fixes(void) +{ + static const long values[] = { + 0, 1, 0x7FFF, 0x8000, 0xFFFF, 0x10000, 0x10001, 0x18000, 0x7FFFFFFF, -1, -0x8000, -0x10000, + (long)0x80000000 + }; + static const unsigned short fracts[] = {0, 1, 0x7FFF, 0x8000, 0xFFFE, 0xFFFF}; + int i; + start("Fix2SmallFract"); + for (i = 0; i < sizeof values / sizeof values[0]; i++) + add(" %08lX:%04X", (unsigned long)values[i], (unsigned short)Fix2SmallFract(values[i])); + end(); + start("SmallFract2Fix"); + for (i = 0; i < sizeof fracts / sizeof fracts[0]; i++) + add(" %04X:%08lX", fracts[i], (unsigned long)SmallFract2Fix(fracts[i])); + end(); +} + +/* Each color to the other space and back, and its three numbers taken as a + * color of that space. */ +#define CONVERSION(Other, Space) \ + static void convert##Other(int i) \ + { \ + RGBColor from, back; \ + Space there; \ + const unsigned short *c = colors[i]; \ + unsigned short *n = (unsigned short *)&there; \ + from.red = c[0]; from.green = c[1]; from.blue = c[2]; \ + memset(&there, 0, sizeof there); \ + memset(&back, 0, sizeof back); \ + RGB2##Other(&from, &there); \ + Other##2RGB(&there, &back); \ + start("RGB2" #Other " %04X,%04X,%04X", c[0], c[1], c[2]); \ + add(" %04X,%04X,%04X back %04X,%04X,%04X", n[0], n[1], n[2], back.red, back.green, back.blue); \ + end(); \ + n[0] = c[0]; n[1] = c[1]; n[2] = c[2]; \ + memset(&back, 0, sizeof back); \ + Other##2RGB(&there, &back); \ + start(#Other "2RGB %04X,%04X,%04X", c[0], c[1], c[2]); \ + add(" %04X,%04X,%04X", back.red, back.green, back.blue); \ + end(); \ + } + +CONVERSION(CMY, CMYColor) +CONVERSION(HSL, HSLColor) +CONVERSION(HSV, HSVColor) + +static void conversions(void) +{ + int i, count = sizeof colors / sizeof colors[0]; + fixes(); + for (i = 0; i < count; i++) convertCMY(i); + for (i = 0; i < count; i++) convertHSL(i); + for (i = 0; i < count; i++) convertHSV(i); +} + +int main(void) +{ + static const unsigned char name[] = "\pUtilities.txt"; + + InitGraf(&qd.thePort); + InitFonts(); + if (Create(name, 0, 'Grph', 'TEXT') != noErr) return 1; + if (FSOpen(name, 0, &out) != noErr) return 2; + + randoms(); + stuffs(); + ptAngles(); + slopes(); + conversions(); + + start("done"); end(); + FSClose(out); + FlushVol(0, 0); + return 0; +} diff --git a/Sources/CQuickDraw/stubs.c b/Sources/CQuickDraw/stubs.c index 174b166..11bc1ab 100644 --- a/Sources/CQuickDraw/stubs.c +++ b/Sources/CQuickDraw/stubs.c @@ -88,20 +88,6 @@ void MakeRGBPat(PixPatHandle ph, RGBColor * colorp) cquickdraw_unimplemented("MakeRGBPat"); } -INTEGER Random(void) -{ - cquickdraw_unimplemented("Random"); - static INTEGER none; - return none; -} - -void StuffHex(Ptr p, ConstStringPtr s) -{ - (void) p; - (void) s; - cquickdraw_unimplemented("StuffHex"); -} - void DrawPicture(PicHandle pic, const Rect * destrp) { (void) pic; @@ -136,14 +122,6 @@ void KillPicture(PicHandle pic) cquickdraw_unimplemented("KillPicture"); } -void PtToAngle(const Rect * rp, Point p, INTEGER * angle) -{ - (void) rp; - (void) p; - (void) angle; - cquickdraw_unimplemented("PtToAngle"); -} - void SetStdProcs(QDProcs * procs) { (void) procs; @@ -293,30 +271,6 @@ void CopyDeepMask(BitMap * srcBits, BitMap * maskBits, BitMap * dstBits, const R cquickdraw_unimplemented("CopyDeepMask"); } -int16_t GetPortTextFont(CGrafPtr p) -{ - (void) p; - cquickdraw_unimplemented("GetPortTextFont"); - static int16_t none; - return none; -} - -int16_t GetPortTextSize(CGrafPtr p) -{ - (void) p; - cquickdraw_unimplemented("GetPortTextSize"); - static int16_t none; - return none; -} - -int16_t GetPortTextFace(CGrafPtr p) -{ - (void) p; - cquickdraw_unimplemented("GetPortTextFace"); - static int16_t none; - return none; -} - void QDFlushPortBuffer(CGrafPtr p, RgnHandle rgn) { (void) p; @@ -324,41 +278,6 @@ void QDFlushPortBuffer(CGrafPtr p, RgnHandle rgn) cquickdraw_unimplemented("QDFlushPortBuffer"); } -void GetPortBounds(CGrafPtr p, Rect * r) -{ - (void) p; - (void) r; - cquickdraw_unimplemented("GetPortBounds"); -} - -void GetPortVisibleRegion(CGrafPtr p, RgnHandle rgn) -{ - (void) p; - (void) rgn; - cquickdraw_unimplemented("GetPortVisibleRegion"); -} - -void GetRegionBounds(RgnHandle rgn, Rect * r) -{ - (void) rgn; - (void) r; - cquickdraw_unimplemented("GetRegionBounds"); -} - -BitMap * GetPortBitMapForCopyBits(CGrafPtr p) -{ - (void) p; - cquickdraw_unimplemented("GetPortBitMapForCopyBits"); - static BitMap * none; - return none; -} - -void GetQDGlobalsScreenBits(BitMap * bm) -{ - (void) bm; - cquickdraw_unimplemented("GetQDGlobalsScreenBits"); -} - void SetStdCProcs(CQDProcs * cProcs) { (void) cProcs; @@ -707,64 +626,6 @@ PixPatHandle GetPixPat(INTEGER p0) return none; } -void CMY2RGB(CMYColor * p0, RGBColor * p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("CMY2RGB"); -} - -void RGB2CMY(RGBColor * p0, CMYColor * p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("RGB2CMY"); -} - -void HSL2RGB(HSLColor * p0, RGBColor * p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("HSL2RGB"); -} - -void RGB2HSL(RGBColor * p0, HSLColor * p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("RGB2HSL"); -} - -void HSV2RGB(HSVColor * p0, RGBColor * p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("HSV2RGB"); -} - -void RGB2HSV(RGBColor * p0, HSVColor * p1) -{ - (void) p0; - (void) p1; - cquickdraw_unimplemented("RGB2HSV"); -} - -SmallFract Fix2SmallFract(Fixed p0) -{ - (void) p0; - cquickdraw_unimplemented("Fix2SmallFract"); - static SmallFract none; - return none; -} - -Fixed SmallFract2Fix(SmallFract p0) -{ - (void) p0; - cquickdraw_unimplemented("SmallFract2Fix"); - static Fixed none; - return none; -} - Boolean GetColor(Point p0, unsigned char * p1, RGBColor * p2, RGBColor * p3) { (void) p0; diff --git a/Sources/Graphite2D/ColorModels.swift b/Sources/Graphite2D/ColorModels.swift new file mode 100644 index 0000000..eb620bb --- /dev/null +++ b/Sources/Graphite2D/ColorModels.swift @@ -0,0 +1,120 @@ +// The Color Picker's other ways of naming a color, and its conversions to and +// from RGB. Each part is a SmallFract, a fraction of 0x10000 kept in 16 bits. +// The rounding is what Mac OS 9.2.2 gave +// (ReferenceTests/Retro68/Utilities-macos-9.2.2-ppc.txt). + +/// Cyan, magenta and yellow. +public struct CMYColor: Hashable, Sendable { + public var cyan: UInt16 + public var magenta: UInt16 + public var yellow: UInt16 + + public init(cyan: UInt16, magenta: UInt16, yellow: UInt16) { + (self.cyan, self.magenta, self.yellow) = (cyan, magenta, yellow) + } +} + +/// Hue, as a fraction of the circle from red, saturation and value. +public struct HSVColor: Hashable, Sendable { + public var hue: UInt16 + public var saturation: UInt16 + public var value: UInt16 + + public init(hue: UInt16, saturation: UInt16, value: UInt16) { + (self.hue, self.saturation, self.value) = (hue, saturation, value) + } +} + +/// Hue, as a fraction of the circle from red, saturation and lightness. +public struct HSLColor: Hashable, Sendable { + public var hue: UInt16 + public var saturation: UInt16 + public var lightness: UInt16 + + public init(hue: UInt16, saturation: UInt16, lightness: UInt16) { + (self.hue, self.saturation, self.lightness) = (hue, saturation, lightness) + } +} + +/// Fix2SmallFract: one and above is the largest fraction; a negative number +/// keeps its low 16 bits. +public func smallFract(fixed: Int32) -> UInt16 { + fixed >= 0x10000 ? .max : UInt16(truncatingIfNeeded: fixed) +} + +private let one = 0x10000 + +/// A product of fractions, to the nearest. +private func times(_ a: Int, _ b: Int) -> Int { (a * b + 0x8000) >> 16 } + +/// A quotient as a fraction, to the nearest. +private func over(_ a: Int, _ b: Int) -> Int { ((a << 16) + b / 2) / b } + +private func clamped(_ value: Int) -> UInt16 { UInt16(max(0, min(value, 0xFFFF))) } + +extension RGBColor { + + private var largest: Int { Int(max(red, green, blue)) } + private var smallest: Int { Int(min(red, green, blue)) } + + private var hue: UInt16 { + let delta = largest - smallest + if delta == 0 { return 0 } + let (sixth, difference) = largest == Int(red) ? (0, Int(green) - Int(blue)) + : largest == Int(green) ? (2, Int(blue) - Int(red)) : (4, Int(red) - Int(green)) + // Down to a fraction of a sixth, then toward zero to one of the circle. + let part = difference << 16 + let floored = part >= 0 ? part / delta : -((-part + delta - 1) / delta) + return UInt16(truncatingIfNeeded: (sixth * one + floored) / 6) + } + + public var cmy: CMYColor { CMYColor(cyan: ~red, magenta: ~green, yellow: ~blue) } + + public init(_ color: CMYColor) { + self.init(red: ~color.cyan, green: ~color.magenta, blue: ~color.yellow) + } + + public var hsv: HSVColor { + let saturation = largest == 0 ? 0 : over(largest - smallest, largest) + return HSVColor(hue: hue, saturation: clamped(saturation), value: UInt16(largest)) + } + + public init(_ color: HSVColor) { + let value = Int(color.value), turn = Int(color.hue) * 6, part = turn & 0xFFFF + let low = value - times(value, Int(color.saturation)) + let falling = value - times(value - low, part), rising = low + times(value - low, part) + let channels = switch turn >> 16 { + case 0: (value, rising, low) + case 1: (falling, value, low) + case 2: (low, value, rising) + case 3: (low, falling, value) + case 4: (rising, low, value) + default: (value, low, falling) + } + self.init(red: clamped(channels.0), green: clamped(channels.1), blue: clamped(channels.2)) + } + + public var hsl: HSLColor { + let sum = largest + smallest, delta = largest - smallest + let saturation = delta == 0 ? 0 : over(delta, sum <= 0xFFFF ? sum : 2 * one - sum) + return HSLColor(hue: hue, saturation: clamped(saturation), lightness: UInt16(sum / 2)) + } + + public init(_ color: HSLColor) { + let lightness = Int(color.lightness), saturation = Int(color.saturation) + let high = lightness < 0x8000 ? times(lightness, one + saturation) + : lightness + saturation - times(lightness, saturation) + let low = 2 * lightness - high + func channel(_ hue: Int) -> UInt16 { + let turn = (hue & 0xFFFF) * 6, part = turn & 0xFFFF + return switch turn >> 16 { + case 0: clamped(low + times(high - low, part)) + case 1, 2: clamped(high) + case 3: clamped(low + times(high - low, one - part)) + default: clamped(low) + } + } + let hue = Int(color.hue), third = 0x5555 + self.init(red: channel(hue + third), green: channel(hue), blue: channel(hue - third)) + } +} diff --git a/Sources/Graphite2D/PointAngle.swift b/Sources/Graphite2D/PointAngle.swift new file mode 100644 index 0000000..c050bcf --- /dev/null +++ b/Sources/Graphite2D/PointAngle.swift @@ -0,0 +1,53 @@ +// PtToAngle: the angle of a point seen from a rectangle's center, by way of +// the slopes QuickDraw keeps for whole degrees. The slopes and the rounding are +// what Mac OS 9.2.2 gave (ReferenceTests/Retro68/Utilities-macos-9.2.2-ppc.txt). + +import GraphiteCore + +/// SlopeFromAngle's magnitudes for 0 to 89 degrees, in 16.16 fixed point; the +/// larger angles are not quite the tangents. +private let slopes: [Int] = [ + 0, 1144, 2289, 3435, 4583, 5734, 6888, 8047, + 9210, 10380, 11556, 12739, 13930, 15130, 16340, 17560, + 18792, 20036, 21294, 22566, 23853, 25157, 26478, 27818, + 29179, 30560, 31964, 33392, 34846, 36327, 37837, 39378, + 40951, 42560, 44205, 45889, 47615, 49385, 51202, 53070, + 54991, 56970, 59009, 61113, 63287, 65536, 67865, 70279, + 72785, 75391, 78103, 80930, 83882, 86969, 90203, 93595, + 97161, 100917, 104879, 109070, 113512, 118230, 123255, 128622, + 134369, 140542, 147196, 154393, 162207, 170727, 180059, 190330, + 201699, 214358, 228551, 244584, 262850, 283867, 308322, 337153, + 371673, 413777, 466312, 533747, 623533, 749079, 937205, 1250493, + 1876695, 3754544, +] + +/// AngleFromSlope stays with a degree until the slope is this much past its. +private let margin = 501 + +extension Rect { + + /// PtToAngle: degrees clockwise from twelve o'clock, scaled so that 45 + /// degrees passes through the rectangle's corner. + public func angle(to point: Point) -> Int16 { + let dv = Int(point.v) - (Int(top) + Int(bottom)) >> 1 + let dh = Int(point.h) - (Int(left) + Int(right)) >> 1 + if dh == 0 { return dv > 0 ? 180 : 0 } + + let height = Int(bottom) - Int(top), width = Int(right) - Int(left) + // The angle as if the point were right of the center. + let half: Int + if width == 0 { + half = 90 + } else if height == 0 { + half = 180 + } else if dv == 0 { + half = 90 + } else { + let across = dh * height, down = dv * width + let slope = min((abs(across) << 16) / abs(down), Int(Int32.max)) + let degrees = slopes.firstIndex { slope < $0 + margin } ?? 90 + half = (across < 0) != (down < 0) ? degrees : 180 - degrees + } + return Int16(dh < 0 ? (half + 180) % 360 : half) + } +} diff --git a/Sources/GraphiteCore/StuffHex.swift b/Sources/GraphiteCore/StuffHex.swift new file mode 100644 index 0000000..fa960da --- /dev/null +++ b/Sources/GraphiteCore/StuffHex.swift @@ -0,0 +1,17 @@ +// StuffHex: bytes from a string of hexadecimal digits, as Mac OS 9.2.2 read +// them (ReferenceTests/Retro68/Utilities-macos-9.2.2-ppc.txt). + +/// The bytes of each pair of characters; a last character without a pair is +/// left out. Nothing is refused: a character up to "9", or from 0x80, gives its +/// low four bits, and any other gives those of its code plus 9. +public func stuffHex(_ characters: some Collection) -> [UInt8] { + func digit(_ character: UInt8) -> UInt8 { + (Int8(bitPattern: character) <= 0x39 ? character : character &+ 9) & 0x0F + } + var bytes: [UInt8] = [] + var iterator = characters.makeIterator() + while let high = iterator.next(), let low = iterator.next() { + bytes.append(digit(high) << 4 | digit(low)) + } + return bytes +} diff --git a/Sources/GraphiteQuickDrawABI/UtilitiesAPI.swift b/Sources/GraphiteQuickDrawABI/UtilitiesAPI.swift new file mode 100644 index 0000000..6cd3359 --- /dev/null +++ b/Sources/GraphiteQuickDrawABI/UtilitiesAPI.swift @@ -0,0 +1,166 @@ +// Random, StuffHex, PtToAngle, the color conversions and the accessors of a +// port, a region and QuickDraw's globals. + +import CQuickDraw +import GraphiteCore +import Graphite2D + +private enum Seed { + /// randSeed before InitGraf. + nonisolated(unsafe) static var unbound: UInt32 = 1 +} + +/// The program's globals, which lie around its thePort; nil before InitGraf. +private func globals() -> UnsafeMutablePointer? { + guard let thePort = CurrentPort.global, + let offset = MemoryLayout.offset(of: \.thePort) else { return nil } + return (UnsafeMutableRawPointer(thePort) - offset).assumingMemoryBound(to: CQuickDraw.QDGlobals.self) +} + +/// The next number from the program's randSeed, which it may set. +@c @implementation +public func Random() -> Int16 { + let globals = globals() + var random = QuickDrawRandom(seed: globals.map { UInt32(truncatingIfNeeded: $0.pointee.randSeed) } ?? Seed.unbound) + let value = random.next() + if let globals { globals.pointee.randSeed = .init(truncatingIfNeeded: random.seed) } else { Seed.unbound = random.seed } + succeed() + return value +} + +@c @implementation +public func StuffHex(_ p: UnsafeMutablePointer?, _ s: UnsafePointer?) { + guard let p, let s else { return fail() } + let bytes = stuffHex(UnsafeBufferPointer(start: s + 1, count: Int(s.pointee))) + for (index, byte) in bytes.enumerated() { p[index] = CChar(bitPattern: byte) } + succeed() +} + +@c @implementation +public func PtToAngle(_ rp: UnsafePointer?, _ p: CQuickDraw.Point, _ angle: UnsafeMutablePointer?) { + guard let rp, let angle else { return fail() } + angle.pointee = GraphiteCore.Rect(rp.pointee).angle(to: GraphiteCore.Point(p)) + succeed() +} + +// MARK: Color conversions + +@c @implementation +public func Fix2SmallFract(_ p0: Int32) -> UInt16 { + succeed() + return smallFract(fixed: .init(truncatingIfNeeded: p0)) +} + +@c @implementation +public func SmallFract2Fix(_ p0: UInt16) -> Int32 { + succeed() + return .init(p0) +} + +private func convert(_ from: UnsafeMutablePointer?, _ to: UnsafeMutablePointer?, + _ conversion: (From) -> To) { + guard let from, let to else { return fail() } + to.pointee = conversion(from.pointee) + succeed() +} + +@c @implementation +public func CMY2RGB(_ p0: UnsafeMutablePointer?, _ p1: UnsafeMutablePointer?) { + convert(p0, p1) { Graphite2D.RGBColor(Graphite2D.CMYColor(cyan: $0.cyan, magenta: $0.magenta, yellow: $0.yellow)).c } +} + +@c @implementation +public func RGB2CMY(_ p0: UnsafeMutablePointer?, _ p1: UnsafeMutablePointer?) { + convert(p0, p1) { + let color = Graphite2D.RGBColor($0).cmy + return CQuickDraw.CMYColor(cyan: color.cyan, magenta: color.magenta, yellow: color.yellow) + } +} + +@c @implementation +public func HSL2RGB(_ p0: UnsafeMutablePointer?, _ p1: UnsafeMutablePointer?) { + convert(p0, p1) { + Graphite2D.RGBColor(Graphite2D.HSLColor(hue: $0.hue, saturation: $0.saturation, lightness: $0.lightness)).c + } +} + +@c @implementation +public func RGB2HSL(_ p0: UnsafeMutablePointer?, _ p1: UnsafeMutablePointer?) { + convert(p0, p1) { + let color = Graphite2D.RGBColor($0).hsl + return CQuickDraw.HSLColor(hue: color.hue, saturation: color.saturation, lightness: color.lightness) + } +} + +@c @implementation +public func HSV2RGB(_ p0: UnsafeMutablePointer?, _ p1: UnsafeMutablePointer?) { + convert(p0, p1) { + Graphite2D.RGBColor(Graphite2D.HSVColor(hue: $0.hue, saturation: $0.saturation, value: $0.value)).c + } +} + +@c @implementation +public func RGB2HSV(_ p0: UnsafeMutablePointer?, _ p1: UnsafeMutablePointer?) { + convert(p0, p1) { + let color = Graphite2D.RGBColor($0).hsv + return CQuickDraw.HSVColor(hue: color.hue, saturation: color.saturation, value: color.value) + } +} + +// MARK: Accessors + +private func read(_ port: CGrafPtr?, _ value: (PortRef) -> T) -> T? { + guard let port else { + fail() + return nil + } + succeed() + return value(PortRef(GrafPtr(OpaquePointer(port)))) +} + +@c @implementation +public func GetPortBounds(_ p: CGrafPtr?, _ r: UnsafeMutablePointer?) { + guard let r, let bounds = read(p, { $0.portRect }) else { return fail() } + r.pointee = bounds +} + +@c @implementation +public func GetPortVisibleRegion(_ p: CGrafPtr?, _ rgn: RgnHandle?) { + guard let rgn, let handle = read(p, { $0.visRgn }), let visible = handle.flatMap(RegionStorage.load) else { + return fail() + } + RegionStorage.store(visible, in: rgn) +} + +@c @implementation +public func GetPortTextFont(_ p: CGrafPtr?) -> Int16 { read(p) { $0.txFont } ?? 0 } + +@c @implementation +public func GetPortTextSize(_ p: CGrafPtr?) -> Int16 { read(p) { $0.txSize } ?? 0 } + +@c @implementation +public func GetPortTextFace(_ p: CGrafPtr?) -> Int16 { read(p) { Int16($0.txFace) } ?? 0 } + +@c @implementation +public func GetPortBitMapForCopyBits(_ p: CGrafPtr?) -> UnsafeMutablePointer? { + guard let p, let offset = MemoryLayout.offset(of: \.portBits) else { + fail() + return nil + } + succeed() + return (UnsafeMutableRawPointer(p) + offset).assumingMemoryBound(to: CQuickDraw.BitMap.self) +} + +@c @implementation +public func GetRegionBounds(_ rgn: RgnHandle?, _ r: UnsafeMutablePointer?) { + guard let r, let region = rgn?.pointee else { return fail() } + r.pointee = region.pointee.rgnBBox + succeed() +} + +@c @implementation +public func GetQDGlobalsScreenBits(_ bm: UnsafeMutablePointer?) { + guard let bm else { return fail() } + bm.pointee = globals()?.pointee.screenBits ?? Screen.bitMap + succeed() +} diff --git a/Tests/GraphiteQuickDrawABITests/UtilitiesCaptureTests.swift b/Tests/GraphiteQuickDrawABITests/UtilitiesCaptureTests.swift new file mode 100644 index 0000000..d3fd23f --- /dev/null +++ b/Tests/GraphiteQuickDrawABITests/UtilitiesCaptureTests.swift @@ -0,0 +1,200 @@ +import Testing +import Foundation +import CQuickDraw +@testable import GraphiteQuickDrawABI + +private typealias CRect = CQuickDraw.Rect +private typealias CColor = CQuickDraw.RGBColor + +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 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 long(_ value: some BinaryInteger) -> String { hex(UInt32(truncatingIfNeeded: value), 8) } + +private let colors: [(UInt16, UInt16, UInt16)] = [ + (0, 0, 0), (0xFFFF, 0xFFFF, 0xFFFF), (0x8000, 0x8000, 0x8000), + (0xFFFF, 0, 0), (0, 0xFFFF, 0), (0, 0, 0xFFFF), + (0xFFFF, 0xFFFF, 0), (0, 0xFFFF, 0xFFFF), (0xFFFF, 0, 0xFFFF), + (0x8000, 0x4000, 0xC000), (0x1234, 0x5678, 0x9ABC), (0xFEDC, 0xBA98, 0x7654), + (1, 2, 3), (0xFFFE, 0xFFFF, 0xFFFD), (0x2000, 0xC000, 0x6000), + (0xC000, 0x2000, 0x2001), (0x00FF, 0xFF00, 0x0F0F), (0x5555, 0xAAAA, 0xAAAA), + (0x7FFF, 0x8000, 0x8001), (0, 1, 0), +] + +/// UtilitiesCapture.c's calls, made through the C API, and the lines the probe +/// wrote. The slope calls are left out: they are not QuickDraw's. +private final class Probe { + let globals = UnsafeMutablePointer.allocate(capacity: 1) + var lines: [String] = [] + + init() { + globals.initialize(to: QDGlobals()) + InitGraf(UnsafeMutableRawPointer(globals) + .advanced(by: MemoryLayout.offset(of: \.thePort)!).assumingMemoryBound(to: GrafPtr?.self)) + } + + deinit { + CurrentPort.global = nil + CurrentPort.port = nil + globals.deallocate() + } + + func line(_ label: String, _ rest: String) { lines.append("\(label) ->\(rest)") } + + func randoms() { + let seeds: [UInt32] = [ + 1, 0, 0xFFFF_FFFF, 2, 12345, 0x7FFF_FFFE, 0x7FFF_FFFF, 0x8000_0000, 0x8000_0001, + 0xFFFF_FFFE, 0x3924_53E7, 0x2B6C_FBB6, 0x653C_E19E, 0x1E61_3586, + ] + line("randSeed after InitGraf", " \(globals.pointee.randSeed)") + for seed in seeds { + globals.pointee.randSeed = .init(truncatingIfNeeded: seed) + var rest = "" + for _ in 0 ..< 8 { + let value = Random() + rest += " \(value):\(long(globals.pointee.randSeed))" + } + line("Random from \(long(seed))", rest) + } + } + + func stuff(_ label: String, _ characters: [UInt8]) { + var bytes = [UInt8](repeating: 0xA5, count: 12) + let string = [UInt8(characters.count)] + characters + bytes.withUnsafeMutableBytes { into in + StuffHex(into.baseAddress!.assumingMemoryBound(to: CChar.self), string) + } + line("StuffHex \(label)", bytes.map { " " + hex($0, 2) }.joined()) + } + + func stuffs() { + stuff("digits", Array("0123456789ABCDEF".utf8)) + stuff("lowercase", Array("abcdefABCDEF".utf8)) + stuff("odd length", Array("FFF".utf8)) + stuff("one character", Array("F".utf8)) + stuff("empty", []) + stuff("beyond F", Array("GHIJKLMNOPQRSTUV".utf8)) + stuff("lowercase beyond f", Array("ghijklmnopqrstuv".utf8)) + stuff("punctuation", Array(" !#$%&()*+,-./:".utf8)) + stuff("more punctuation", Array(";<=>?@[]^_`{|}~".utf8)) + stuff("high characters", [0x80, 0x81, 0x9F, 0xA0, 0xC0, 0xF0, 0xFE, 0xFF]) + } + + func angle(_ r: CRect, v: Int, h: Int) -> Int16 { + var r = r + var angle: Int16 = -1 + PtToAngle(&r, CQuickDraw.Point(v: Int16(v), h: Int16(h)), &angle) + return angle + } + + func angles(_ label: String, _ r: CRect, _ step: Int) { + // C's division, toward zero. + let midV = (Int(r.top) + Int(r.bottom)) / 2, midH = (Int(r.left) + Int(r.right)) / 2 + for dv in -6 ... 6 { + let rest = (-6 ... 6).map { " \(angle(r, v: midV + dv * step, h: midH + $0 * step))" }.joined() + line("PtToAngle \(label) (\(r.top),\(r.left),\(r.bottom),\(r.right)) step \(step) row \(dv)", rest) + } + } + + func sweep(_ label: String, _ r: CRect) { + let midV = (Int(r.top) + Int(r.bottom)) / 2, midH = (Int(r.left) + Int(r.right)) / 2 + let rest = stride(from: -200, through: 200, by: 3).map { " \(angle(r, v: midV - 100, h: midH + $0))" }.joined() + line("PtToAngle sweep \(label) (\(r.top),\(r.left),\(r.bottom),\(r.right))", rest) + } + + func ptAngles() { + angles("square", rect(0, 0, 100, 100), 10) + angles("wide", rect(0, 0, 100, 200), 10) + angles("tall", rect(0, 0, 200, 100), 10) + angles("odd", rect(0, 0, 7, 13), 1) + angles("away", rect(-30, 40, 11, 63), 3) + angles("empty", rect(10, 10, 10, 10), 5) + angles("no width", rect(0, 10, 40, 10), 5) + angles("no height", rect(10, 0, 10, 40), 5) + angles("inverted", rect(100, 200, 0, 0), 10) + angles("one pixel", rect(0, 0, 1, 1), 1) + sweep("square", rect(0, 0, 100, 100)) + sweep("wide", rect(0, 0, 100, 300)) + sweep("tall", rect(0, 0, 300, 100)) + sweep("odd", rect(0, 0, 41, 23)) + sweep("huge", rect(-16000, -16000, 16000, 16000)) + } + + func fixes() { + let values: [UInt32] = [ + 0, 1, 0x7FFF, 0x8000, 0xFFFF, 0x10000, 0x10001, 0x18000, 0x7FFF_FFFF, 0xFFFF_FFFF, 0xFFFF_8000, + 0xFFFF_0000, 0x8000_0000, + ] + line("Fix2SmallFract", values.map { + " \(long($0)):\(hex(Fix2SmallFract(.init(truncatingIfNeeded: $0)), 4))" + }.joined()) + let fracts: [UInt16] = [0, 1, 0x7FFF, 0x8000, 0xFFFE, 0xFFFF] + line("SmallFract2Fix", fracts.map { " \(hex($0, 4)):\(long(SmallFract2Fix($0)))" }.joined()) + } + + /// Each color to another space and back, and its three numbers taken as a + /// color of that space. The space's record is three SmallFracts. + func convert(_ name: String, _ blank: Space, + _ there: (UnsafeMutablePointer, UnsafeMutablePointer) -> Void, + _ back: (UnsafeMutablePointer, UnsafeMutablePointer) -> Void) { + func text(_ a: UInt16, _ b: UInt16, _ c: UInt16) -> String { "\(hex(a, 4)),\(hex(b, 4)),\(hex(c, 4))" } + for (a, b, c) in colors { + var from = CColor(red: a, green: b, blue: c), returned = CColor() + var space = blank + there(&from, &space) + let parts = withUnsafeBytes(of: space) { Array($0.bindMemory(to: UInt16.self)) } + back(&space, &returned) + line("RGB2\(name) \(text(a, b, c))", + " \(text(parts[0], parts[1], parts[2])) back \(text(returned.red, returned.green, returned.blue))") + withUnsafeMutableBytes(of: &space) { bytes in + let parts = bytes.bindMemory(to: UInt16.self) + (parts[0], parts[1], parts[2]) = (a, b, c) + } + back(&space, &returned) + line("\(name)2RGB \(text(a, b, c))", " \(text(returned.red, returned.green, returned.blue))") + } + } + + func run() { + randoms() + stuffs() + ptAngles() + fixes() + convert("CMY", CMYColor(), { RGB2CMY($0, $1) }, { CMY2RGB($0, $1) }) + convert("HSL", HSLColor(), { RGB2HSL($0, $1) }, { HSL2RGB($0, $1) }) + convert("HSV", HSVColor(), { RGB2HSV($0, $1) }, { HSV2RGB($0, $1) }) + line("done", "") + } +} + +extension QuickDrawTests { + + /// Random, StuffHex, PtToAngle and the color conversions, compared with + /// what Mac OS 9.2.2 did (ReferenceTests/Retro68/UtilitiesCapture.c). + @Suite struct UtilitiesCaptureTests { + + private static let capture: [String] = { + let data = try! Data(contentsOf: retro68.appendingPathComponent("Utilities-macos-9.2.2-ppc.txt")) + return String(decoding: data, as: UTF8.self).split(separator: "\r").map(String.init) + .filter { !$0.hasPrefix("SlopeFromAngle") && !$0.hasPrefix("AngleFromSlope") } + }() + + @Test func doesWhatMacOSDid() { + let probe = Probe() + probe.run() + #expect(probe.lines.count == Self.capture.count) + for (ours, mac) in zip(probe.lines, Self.capture) { + #expect(ours == mac) + } + } + } +} diff --git a/Tools/quickdraw-abi/README.md b/Tools/quickdraw-abi/README.md index 25cb222..11ffda3 100644 --- a/Tools/quickdraw-abi/README.md +++ b/Tools/quickdraw-abi/README.md @@ -62,7 +62,12 @@ headers change only through `generate-headers.py `. - `GetMainDevice`, `GetDeviceList`, `GetNextDevice`, `GetGDevice` and `SetGDevice`; - `RGBForeColor`, `RGBBackColor`, `GetForeColor`, `GetBackColor`, `GetCPixel` - and `SetCPixel`. + and `SetCPixel`; +- `Random`, `StuffHex`, `PtToAngle`, `Fix2SmallFract`, `SmallFract2Fix` and the + conversions between RGB and CMY, HSL and HSV; +- the accessors `GetPortBounds`, `GetPortVisibleRegion`, `GetPortTextFont`, + `GetPortTextSize`, `GetPortTextFace`, `GetPortBitMapForCopyBits`, + `GetRegionBounds` and `GetQDGlobalsScreenBits`. Every other function in `symbols.txt` is exported as a stub that sets `unimpErr` (-4), which `QDError` returns, and gives zero; set @@ -84,6 +89,9 @@ 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. Graphics worlds are compared too (`ReferenceTests/Retro68/GWorldCapture.c`); what differs is listed below. +`Random`, `StuffHex`, `PtToAngle` and the color conversions give what Mac OS +9.2.2 gave on every captured line +(`ReferenceTests/Retro68/UtilitiesCapture.c`); the accessors were not captured. ## Text and fonts diff --git a/Tools/quickdraw-abi/conformance/drawing.c b/Tools/quickdraw-abi/conformance/drawing.c index a7a93fb..ad4bd76 100644 --- a/Tools/quickdraw-abi/conformance/drawing.c +++ b/Tools/quickdraw-abi/conformance/drawing.c @@ -257,6 +257,32 @@ int main(void) CHECK(QDError() == noErr); } + { + unsigned char bytes[2] = { 0, 0 }; + Rect all, bounds; + BitMap screen; + RGBColor red = { 0xFFFF, 0, 0 }, back; + HSVColor hsv; + Point corner = { 0, 10 }; + short angle = 0; + + StuffHex((Ptr)bytes, (ConstStringPtr)"\4C0DE"); + CHECK(bytes[0] == 0xC0 && bytes[1] == 0xDE); + SetRect(&all, 0, 0, 10, 10); + PtToAngle(&all, corner, &angle); + CHECK(angle == 45); + qd.randSeed = 1; + CHECK(Random() == 16807 && qd.randSeed == 16807); + RGB2HSV(&red, &hsv); + HSV2RGB(&hsv, &back); + CHECK(hsv.hue == 0 && hsv.value == 0xFFFF && back.red == 0xFFFF && back.green == 1); + GetPortBounds((CGrafPtr)&port, &bounds); + CHECK(bounds.right == port.portRect.right && bounds.bottom == port.portRect.bottom); + CHECK(GetPortBitMapForCopyBits((CGrafPtr)&port) == &port.portBits); + GetQDGlobalsScreenBits(&screen); + CHECK(screen.baseAddr == qd.screenBits.baseAddr); + } + ClosePort(&port); clear(); PaintRect(&box); diff --git a/Tools/quickdraw-abi/implemented.txt b/Tools/quickdraw-abi/implemented.txt index fdc5d7f..ba56112 100644 --- a/Tools/quickdraw-abi/implemented.txt +++ b/Tools/quickdraw-abi/implemented.txt @@ -148,3 +148,24 @@ GetForeColor GetBackColor GetCPixel SetCPixel + +# Utilities and accessors +Random +StuffHex +PtToAngle +Fix2SmallFract +SmallFract2Fix +CMY2RGB +RGB2CMY +HSL2RGB +RGB2HSL +HSV2RGB +RGB2HSV +GetPortBounds +GetPortVisibleRegion +GetPortTextFont +GetPortTextSize +GetPortTextFace +GetPortBitMapForCopyBits +GetRegionBounds +GetQDGlobalsScreenBits