-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.ts
More file actions
193 lines (172 loc) · 5.88 KB
/
Copy pathscreen.ts
File metadata and controls
193 lines (172 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type {Vm} from './testvm.ts'
import { coloursFromAttr, type RGB } from '@zx/sys'
import * as PImage from 'pureimage'
import { createWriteStream, createReadStream, existsSync } from 'node:fs'
import * as fs from 'node:fs'
import * as path from 'node:path'
import { expect } from "jsr:@std/expect";
import type { byte } from '@zx/sys'
const rootExpectedFiles = "./"
const rootActualMismatchFiles = "./testout"
export function cls(vm: Vm) {
const core = vm.core
for(let p = 0x4000, n = 32 * 192; n > 0; p ++, n --) {
core.poke(p, 0)
}
clearAttrs(vm, 0b00111000) // Standard black ink on white paper
}
export function cls1(vm: Vm) {
const core = vm.core
for(let p = 0x4000, n = 32 * 192; n > 0; p ++, n --) {
core.poke(p, 255)
}
clearAttrs(vm, 0b00111000) // Standard black ink on white paper
}
export function clearAttrs(vm: Vm, attr: byte) {
const core = vm.core
for(let p = 0x5800, n = 32 * 24; n > 0; p ++, n --) {
core.poke(p, attr)
}
}
export function clsObscured(vm: Vm) {
const core = vm.core
for(let p = 0x4000, n = 32 * 192; n > 0; p ++, n --) {
core.poke(p, 0xAA)
}
for(let p = 0x5800, n = 32 * 24; n > 0; p ++, n --) {
core.poke(p, 0b00000000) // Black-on-black
}
}
export type Bitmap = PImage.Bitmap
export function getScreenMono(vm: Vm, xStart: number = 0, yStart: number = 0, w: number = 256, h: number = 192): Bitmap {
if ((xStart & 0b111) !== 0) throw "xStart coordinate must lie on byte boundary."
if ((w & 0b111) !== 0) throw "w size must be integer number of bytes."
const colStart = xStart >> 3
const colCount = w >> 3
const clip = PImage.make(w, h)
const context = clip.getContext("2d")
const pixels = context.getImageData(0, 0, w, h)
const pixelData = pixels.data
let pixelByte = 0
function writeMono(v: boolean) {
const vByte = v ? 0 : 255
pixelData[pixelByte++] = vByte // R
pixelData[pixelByte++] = vByte // G
pixelData[pixelByte++] = vByte // B
pixelData[pixelByte++] = 255 // A
}
for (let y = yStart, hRemaining = h; hRemaining > 0; y ++, hRemaining --) {
const pStart = lineAddressFromY(y) + colStart
for (let p = pStart, bRemaining = colCount; bRemaining > 0; p ++, bRemaining --) {
const b = vm.core.peek(p)
for (let mask = 0x80; mask != 0; mask >>= 1) {
writeMono((b & mask) !== 0)
}
}
}
context.putImageData(pixels, 0, 0)
return clip
}
export function getScreenColour(vm: Vm, xStart: number = 0, yStart: number = 0, w: number = 256, h: number = 192): Bitmap {
if ((xStart & 0b111) !== 0) throw "xStart coordinate must lie on byte boundary."
if ((w & 0b111) !== 0) throw "w size must be integer number of bytes."
const colStart = xStart >> 3
const colCount = w >> 3
const clip = PImage.make(w, h)
const context = clip.getContext("2d")
const pixels = context.getImageData(0, 0, w, h)
const pixelData = pixels.data
let pixelByte = 0
function writeAttr(c: RGB) {
pixelData[pixelByte++] = c[0] // R
pixelData[pixelByte++] = c[1] // G
pixelData[pixelByte++] = c[2] // B
pixelData[pixelByte++] = 255 // A
}
for (let y = yStart, hRemaining = h; hRemaining > 0; y ++, hRemaining --) {
const pStart = lineAddressFromY(y) + colStart
const aStart = attrLineAddressFromY(y) + colStart
for (let
p = pStart, pa = aStart, bRemaining = colCount;
bRemaining > 0;
p ++, pa++, bRemaining --) {
const b = vm.core.peek(p)
const inkAndPaper = coloursFromAttr(vm.core.peek(pa))
for (let mask = 0x80; mask != 0; mask >>= 1) {
const v = (b & mask) !== 0
writeAttr(v ? inkAndPaper[1] : inkAndPaper[0])
}
}
}
context.putImageData(pixels, 0, 0)
return clip
}
export async function assertBitmapImageMatches(subdir: string, expectedPngFilename: string, actualOutput: Bitmap): Promise<void> {
const expectFilePath = `${subdir}/${expectedPngFilename}-expected.png`
const expected = await readClip(`${rootExpectedFiles}/${expectFilePath}`)
try {
if (!expected) throw `Cannot find file: ${expectFilePath}`
assertSamePixels(expected, actualOutput)
} catch (problem) {
const actualFilePath = expectFilePath.replace('-expected.', '-actual.')
const actualFullPath = `${rootActualMismatchFiles}/${actualFilePath}`
await writeClip(actualOutput, actualFullPath)
throw problem
}
}
export async function writeClip(clipImage: Bitmap, filename: string) {
await fs.promises.mkdir(path.dirname(filename), { recursive: true })
const [output, closeEvent] = writeStream(filename)
try {
await PImage.encodePNGToStream(clipImage, output)
} finally {
await closeEvent
}
}
export async function readClip(filename: string): Promise<Bitmap|null> {
if (!existsSync(filename)) return Promise.resolve(null)
const [input, closeEvent] = readStream(filename)
const result = await PImage.decodePNGFromStream(input)
await closeEvent
return result
}
export function assertSamePixels(expected: Bitmap, actual: Bitmap) {
const w = expected.width
const h = expected.height
expect(actual.width).toBe(w)
expect(actual.height).toBe(h)
const expectedData = expected.getContext("2d").getImageData(0, 0, w, h).data
const actualData = actual.getContext("2d").getImageData(0, 0, w, h).data
for(let i = 0; i < expectedData.length; i++) {
if (actualData[i] != expectedData[i])
throw "Mismatch at byte "+i
}
}
function lineAddressFromY(y: number) {
return 0x4000 +
(((y >> 6) & 0b11) << 11) +
((y & 0b111) << 8) +
(((y >> 3) & 0b111) << 5)
}
function attrLineAddressFromY(y: number) {
const row = y >> 3
return 0x5800 + (row * 32)
}
function readStream(filename: string): [fs.ReadStream, Promise<void>] {
const stream = createReadStream(filename)
const closure = new Promise<void>((accept, reject) => {
stream
.on("close", accept)
.on("error", reject)
})
return [stream, closure]
}
function writeStream(filename: string): [fs.WriteStream, Promise<void>] {
const stream = createWriteStream(filename)
const closure = new Promise<void>((accept, reject) => {
stream
.on("close", accept)
.on("error", reject)
})
return [stream, closure]
}