@amritk/nish-aarch64-linux 0.45.1 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -143,21 +143,30 @@ const PIXELS_ANSI16: string =
143
143
  const pixelsHex = (text: string, at: i32, count: i32): i32 => {
144
144
  let v: i32 = 0
145
145
  const length: i32 = toI32(text.length)
146
- for (let i: i32 = at; i < at + count && i >= 0 && i < length; i++) {
146
+ const end: i64 = toI64(at) + toI64(count)
147
+ for (let i: i32 = at; toI64(i) < end && i >= 0 && i < length; i++) {
147
148
  const c: i32 = toI32(text.charCodeAt(i))
148
149
  v = (v << 4) | (c >= 97 ? c - 87 : c - 48)
149
150
  }
150
151
  return v
151
152
  }
152
153
 
153
- /** Panic, as `who`, unless `width × height` is a size an array of pixels can have. */
154
- const pixelsCheckSize = (who: string, width: i32, height: i32): void => {
154
+ /** `width × height`, after panicking, as `who`, unless it is a size an array of pixels can have. */
155
+ const pixelsArea = (who: string, width: i32, height: i32): i32 => {
155
156
  if (width < 0 || height < 0) {
156
157
  panic(`${who}: a negative size`)
157
158
  }
158
- if (width > 0 && height > ((2147483647 / width) | 0)) {
159
+ const area: i64 = toI64(width) * toI64(height)
160
+ if (area > toI64(2147483647)) {
159
161
  panic(`${who}: more than 2^31 - 1 pixels`)
160
162
  }
163
+ return toI32(area)
164
+ }
165
+
166
+ /** `y * width + x` as an index below `length`, or -1 when it is not one. */
167
+ const pixelsIndex = (x: i32, y: i32, width: i32, length: i32): i32 => {
168
+ const wide: i64 = toI64(y) * toI64(width) + toI64(x)
169
+ return wide < toI64(0) || wide >= toI64(length) ? -1 : toI32(wide)
161
170
  }
162
171
 
163
172
  /** A `width × height` grid of `0xRRGGBB` pixels, row by row, all black when made. */
@@ -176,10 +185,10 @@ export class Framebuffer {
176
185
  * @param height Pixels down.
177
186
  */
178
187
  constructor(width: i32, height: i32) {
179
- pixelsCheckSize("Framebuffer", width, height)
188
+ const area: i32 = pixelsArea("Framebuffer", width, height)
180
189
  this.width = width
181
190
  this.height = height
182
- this.pixels = new Array<i32>(width * height)
191
+ this.pixels = new Array<i32>(area)
183
192
  this.pixels.fill(0)
184
193
  }
185
194
 
@@ -190,17 +199,19 @@ export class Framebuffer {
190
199
  * @param height The new pixels down; negative panics, as in the constructor.
191
200
  */
192
201
  resize(width: i32, height: i32): void {
193
- pixelsCheckSize("Framebuffer.resize", width, height)
202
+ const area: i32 = pixelsArea("Framebuffer.resize", width, height)
194
203
  if (width !== this.width || height !== this.height) {
195
204
  this.width = width
196
205
  this.height = height
197
- this.pixels = new Array<i32>(width * height)
206
+ this.pixels = new Array<i32>(area)
198
207
  }
199
208
  this.pixels.fill(0)
200
209
  }
201
210
 
202
211
  /**
203
212
  * Set the pixel at `x`, `y` to `colour`'s low 24 bits; a pixel off the framebuffer is not drawn.
213
+ * Panics when `pixels` does not hold `width × height`, as after setting those public fields
214
+ * out of step.
204
215
  *
205
216
  * @param x The pixel column, from 0 at the left.
206
217
  * @param y The pixel row, from 0 at the top.
@@ -210,11 +221,20 @@ export class Framebuffer {
210
221
  if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
211
222
  return
212
223
  }
213
- this.pixels[y * this.width + x] = colour & 0xffffff
224
+ const pixels: i32[] = this.pixels
225
+ const at: i32 = pixelsIndex(x, y, this.width, toI32(pixels.length))
226
+ // In range while `pixels` holds `width * height`. The fields are public,
227
+ // and one set out of step with the others is a broken framebuffer, which
228
+ // panics as indexing past `pixels` did, rather than drop the write.
229
+ if (at < 0 || at >= toI32(pixels.length)) {
230
+ panic("Framebuffer.set: pixels does not hold width × height")
231
+ }
232
+ pixels[at] = colour & 0xffffff
214
233
  }
215
234
 
216
235
  /**
217
- * The colour of the pixel at `x`, `y`, or black off the framebuffer.
236
+ * The colour of the pixel at `x`, `y`, or black off the framebuffer. Panics when `pixels` does
237
+ * not hold `width × height`, as after setting those public fields out of step.
218
238
  *
219
239
  * @param x The pixel column, from 0 at the left.
220
240
  * @param y The pixel row, from 0 at the top.
@@ -224,7 +244,15 @@ export class Framebuffer {
224
244
  if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
225
245
  return 0
226
246
  }
227
- return this.pixels[y * this.width + x]
247
+ const pixels: i32[] = this.pixels
248
+ const at: i32 = pixelsIndex(x, y, this.width, toI32(pixels.length))
249
+ // In range while `pixels` holds `width * height`. The fields are public,
250
+ // and one set out of step with the others is a broken framebuffer, which
251
+ // panics as indexing past `pixels` did, rather than read as black.
252
+ if (at < 0 || at >= toI32(pixels.length)) {
253
+ panic("Framebuffer.get: pixels does not hold width × height")
254
+ }
255
+ return pixels[at]
228
256
  }
229
257
 
230
258
  /**
@@ -261,10 +289,23 @@ export const scaleFramebuffer = (src: Framebuffer, divisor: i32, dst: Framebuffe
261
289
  const w: i32 = pixelsCeilDiv(src.width, divisor)
262
290
  const h: i32 = pixelsCeilDiv(src.height, divisor)
263
291
  dst.resize(w, h)
264
- for (let y: i32 = 0; y < h; y++) {
265
- const from: i32 = y * divisor * src.width
266
- for (let x: i32 = 0; x < w; x++) {
267
- dst.pixels[y * w + x] = src.pixels[from + x * divisor]
292
+ const from: i32[] = src.pixels
293
+ const to: i32[] = dst.pixels
294
+ const srcW: i32 = src.width
295
+ // `x * divisor` and `y * divisor` stay below the source's size, as `w` and
296
+ // `h` are its size over `divisor` rounded up; the loops say so for the
297
+ // prover. Each index is then in its array while each framebuffer's `pixels`
298
+ // holds its `width * height`; one whose public fields a caller set out of
299
+ // step panics, as indexing past `pixels` did, rather than skip the pixel.
300
+ for (let y: i32 = 0; y < h && toI64(y) * toI64(divisor) < toI64(src.height); y++) {
301
+ const sy: i32 = toI32(toI64(y) * toI64(divisor))
302
+ for (let x: i32 = 0; x < w && toI64(x) * toI64(divisor) < toI64(srcW); x++) {
303
+ const s: i32 = pixelsIndex(toI32(toI64(x) * toI64(divisor)), sy, srcW, toI32(from.length))
304
+ const d: i32 = pixelsIndex(x, y, w, toI32(to.length))
305
+ if (s < 0 || s >= toI32(from.length) || d < 0 || d >= toI32(to.length)) {
306
+ panic("scaleFramebuffer: pixels does not hold width × height")
307
+ }
308
+ to[d] = from[s]
268
309
  }
269
310
  }
270
311
  }
@@ -314,7 +355,14 @@ export class PixelRect {
314
355
  }
315
356
 
316
357
  /** `a / b` rounded up, for `a >= 0` and `b > 0`. */
317
- const pixelsCeilDiv = (a: i32, b: i32): i32 => (a === 0 ? 0 : (((a - 1) / b) | 0) + 1)
358
+ const pixelsCeilDiv = (a: i32, b: i32): i32 => {
359
+ if (a <= 0 || b <= 0) {
360
+ return 0
361
+ }
362
+ // `q` is at most `a - 1`, so `q + 1` cannot overflow; the test says so.
363
+ const q: i32 = ((a - 1) / b) | 0
364
+ return q < a ? q + 1 : a
365
+ }
318
366
 
319
367
  /**
320
368
  * The rect a `fbW × fbH` framebuffer is drawn in on a `cols × rows` area of
@@ -376,8 +424,8 @@ export const fitFramebuffer = (
376
424
  const usedCols: i32 = pixelsCeilDiv(w, pw)
377
425
  const usedRows: i32 = pixelsCeilDiv(h, ph)
378
426
  const scale: f64 = toF64(w) / toF64(fbW)
379
- const col: i32 = (cols - usedCols) >> 1
380
- const row: i32 = (rows - usedRows) >> 1
427
+ const col: i32 = toI32(Math.floor((toF64(cols) - toF64(usedCols)) / 2.0))
428
+ const row: i32 = toI32(Math.floor((toF64(rows) - toF64(usedRows)) / 2.0))
381
429
  if (backend === GRAPHICS_ASCII) {
382
430
  return new PixelRect(col, row, usedCols, usedRows, usedCols, usedRows, scale)
383
431
  }
@@ -470,13 +518,16 @@ export class PixelBytes {
470
518
  * @param n How many bytes the caller is about to `put`.
471
519
  */
472
520
  reserve(n: i32): void {
473
- const need: i32 = this.length + n
474
- if (need <= toI32(this.bytes.length)) {
521
+ // Summed and doubled in f64, which cannot overflow.
522
+ const need: f64 = toF64(this.length) + toF64(n)
523
+ if (need <= toF64(this.bytes.length)) {
475
524
  return
476
525
  }
477
- // Doubled in f64, which cannot overflow, and capped at the longest array.
526
+ if (need > 2147483647.0) {
527
+ panic("PixelBytes.reserve: more than 2^31 - 1 bytes")
528
+ }
478
529
  let size: f64 = toF64(this.bytes.length)
479
- while (size < toF64(need)) {
530
+ while (size < need) {
480
531
  size = size * 2.0
481
532
  }
482
533
  const grown: u8[] = new Array<u8>(toI32(Math.min(size, 2147483647.0)))
@@ -492,8 +543,13 @@ export class PixelBytes {
492
543
  */
493
544
  put(b: i32): void {
494
545
  this.reserve(PIXELS_ONE)
495
- this.bytes[this.length] = toU8(b)
496
- this.length = this.length + 1
546
+ const bytes: u8[] = this.bytes
547
+ const at: i32 = this.length
548
+ // Always true after `reserve`; it proves the store and the count in range.
549
+ if (at >= 0 && at < toI32(bytes.length)) {
550
+ bytes[at] = toU8(b)
551
+ this.length = at + 1
552
+ }
497
553
  }
498
554
 
499
555
  /**
@@ -504,10 +560,14 @@ export class PixelBytes {
504
560
  putText(s: string): void {
505
561
  const n: i32 = toI32(s.length)
506
562
  this.reserve(n)
507
- for (let i: i32 = 0; i < n; i++) {
508
- this.bytes[this.length + i] = toU8(s.charCodeAt(i))
563
+ const bytes: u8[] = this.bytes
564
+ let at: i32 = this.length
565
+ // `at` stays in range after `reserve`; bounding the loop by it proves so.
566
+ for (let i: i32 = 0; i < n && at >= 0 && at < toI32(bytes.length); i++) {
567
+ bytes[at] = toU8(s.charCodeAt(i))
568
+ at = at + 1
509
569
  }
510
- this.length = this.length + n
570
+ this.length = at
511
571
  }
512
572
 
513
573
  /**
@@ -546,8 +606,7 @@ const pixelsSampleMap = (n: i32, source: i32): i32[] => {
546
606
  /** `pixelsSampleMap` written into the first `n` entries of `map`, which is at least `n` long. */
547
607
  const pixelsSampleMapInto = (map: i32[], n: i32, source: i32): void => {
548
608
  for (let i: i32 = 0; i < n && i < toI32(map.length); i++) {
549
- const s: i32 = toI32(Math.floor(((toF64(i) + 0.5) * toF64(source)) / toF64(n)))
550
- map[i] = s >= source ? source - 1 : s
609
+ map[i] = toI32(Math.min(Math.floor(((toF64(i) + 0.5) * toF64(source)) / toF64(n)), toF64(source) - 1.0))
551
610
  }
552
611
  }
553
612
 
@@ -567,13 +626,22 @@ const pixelsAtLeast = (a: i32[], n: i32): i32[] => {
567
626
  return grown
568
627
  }
569
628
 
629
+ /** Append `v + 1` in decimal: the 1-based number escapes give the 0-based `v`. */
630
+ const pixelsPutOneBased = (out: PixelBytes, v: i32): void => {
631
+ if (v < 2147483647) {
632
+ out.putDecimal(v + 1)
633
+ } else {
634
+ out.putText("2147483648")
635
+ }
636
+ }
637
+
570
638
  /** Append `CSI row ; col H`, the cursor to the 0-based cell (`col`, `row`), as `moveCursor` writes it. */
571
639
  const pixelsMoveTo = (out: PixelBytes, col: i32, row: i32): void => {
572
640
  out.put(PIXELS_ESC)
573
641
  out.put(PIXELS_BRACKET)
574
- out.putDecimal(row + 1)
642
+ pixelsPutOneBased(out, row)
575
643
  out.put(PIXELS_SEMICOLON)
576
- out.putDecimal(col + 1)
644
+ pixelsPutOneBased(out, col)
577
645
  out.put(PIXELS_CUP)
578
646
  }
579
647
 
@@ -615,7 +683,8 @@ const pixelsStringEnd = (out: PixelBytes): void => {
615
683
  * is done with it.
616
684
  *
617
685
  * `imageId` is 1 to 2^31 - 1, since 0 is the protocol's "no id"; anything
618
- * else panics. An empty framebuffer or rect writes nothing.
686
+ * else panics. So does a framebuffer whose `pixels` does not hold
687
+ * `width × height`. An empty framebuffer or rect writes nothing.
619
688
  *
620
689
  * @param fb The frame's pixels, at its own size.
621
690
  * @param rect Where to draw it, from `fitFramebuffer` with `GRAPHICS_KITTY`.
@@ -624,28 +693,35 @@ const pixelsStringEnd = (out: PixelBytes): void => {
624
693
  */
625
694
  export const kittyFrame = (fb: Framebuffer, rect: PixelRect, imageId: i32, out: PixelBytes): void => {
626
695
  pixelsCheckImageId("kittyFrame", imageId)
627
- const count: i32 = fb.width * fb.height
628
- if (count === 0 || rect.cols <= 0 || rect.rows <= 0) {
696
+ const pixels: i32[] = fb.pixels
697
+ const count: i32 = toI32(pixels.length)
698
+ // The header gives `width` and `height`; pixels of another count are a
699
+ // framebuffer whose public fields a caller set out of step, not an image.
700
+ if (toI64(count) !== toI64(fb.width) * toI64(fb.height)) {
701
+ panic("kittyFrame: pixels does not hold width × height")
702
+ }
703
+ const raw: u8[] = out.kittyRaw
704
+ // A `kittyRaw` cut below one pixel's 3 bytes holds none: nothing is drawn,
705
+ // and the cursor is not moved for it.
706
+ if (count === 0 || rect.cols <= 0 || rect.rows <= 0 || toI32(raw.length) < 3) {
629
707
  return
630
708
  }
631
709
  pixelsMoveTo(out, rect.col, rect.row)
632
- const raw: u8[] = out.kittyRaw
633
- const total: i32 = count * 3
634
- let sent: i32 = 0
635
710
  let pixel: i32 = 0
636
- while (sent < total) {
637
- // The next chunk's pixels as RGB bytes, 1024 pixels to a full chunk.
638
- const n: i32 = total - sent < PIXELS_KITTY_RAW ? total - sent : PIXELS_KITTY_RAW
639
- // `k + 2 < raw.length` always holds below `n`; it proves the stores in range.
640
- for (let k: i32 = 0; k < n && k + 2 < toI32(raw.length); k = k + 3) {
641
- const c: i32 = fb.pixels[pixel]
642
- raw[k] = toU8((c >> 16) & 255)
643
- raw[k + 1] = toU8((c >> 8) & 255)
644
- raw[k + 2] = toU8(c & 255)
711
+ while (pixel < count) {
712
+ const first: boolean = pixel === 0
713
+ // The next chunk's pixels as RGB bytes, `raw` (1024 pixels) to a full chunk.
714
+ let n: i32 = 0
715
+ while (n < toI32(raw.length) - 2 && pixel >= 0 && pixel < toI32(pixels.length)) {
716
+ const c: i32 = pixels[pixel]
717
+ raw[n] = toU8((c >> 16) & 255)
718
+ raw[n + 1] = toU8((c >> 8) & 255)
719
+ raw[n + 2] = toU8(c & 255)
720
+ n = n + 3
645
721
  pixel = pixel + 1
646
722
  }
647
723
  pixelsKittyOpen(out)
648
- if (sent === 0) {
724
+ if (first) {
649
725
  out.putText("a=T,f=24,t=d,i=")
650
726
  out.putDecimal(imageId)
651
727
  out.putText(",p=1,s=")
@@ -659,8 +735,7 @@ export const kittyFrame = (fb: Framebuffer, rect: PixelRect, imageId: i32, out:
659
735
  out.putText(",C=1,")
660
736
  }
661
737
  out.putText("q=2,")
662
- sent = sent + n
663
- out.putText(sent < total ? "m=1;" : "m=0;")
738
+ out.putText(pixel < count ? "m=1;" : "m=0;")
664
739
  out.reserve(PIXELS_KITTY_CHUNK)
665
740
  out.length = base64EncodeInto(raw, 0, n, out.bytes, out.length)
666
741
  pixelsStringEnd(out)
@@ -689,16 +764,22 @@ export const kittyDelete = (imageId: i32): string => {
689
764
  return `\x1b_Ga=d,d=I,i=${imageId},q=2\x1b\\`
690
765
  }
691
766
 
692
- /** The Sixel register of the palette colour with red level `r` (0–5), green `g` (0–6) and blue `b` (0–5). */
693
- const pixelsSixelRegister = (r: i32, g: i32, b: i32): i32 => r * 42 + g * 6 + b
767
+ /**
768
+ * The Sixel register of the palette colour with red level `r` (0–5), green `g`
769
+ * (0–6) and blue `b` (0–5); -1, never met, for a level outside those, which
770
+ * `sixelFrame` skips.
771
+ */
772
+ const pixelsSixelRegister = (r: i32, g: i32, b: i32): i32 =>
773
+ r >= 0 && r < 6 && g >= 0 && g < 7 && b >= 0 && b < 6 ? r * 42 + g * 6 + b : -1
694
774
 
695
775
  /**
696
776
  * The level of one 0–255 component among `levels`, nearest, after `bias` (a
697
777
  * dither offset, 0 without dithering) is added and the sum clamped to 0–255.
698
778
  */
699
779
  const pixelsLevel = (v: i32, bias: i32, levels: i32): i32 => {
700
- const x: i32 = Math.min(Math.max(v + bias, 0), 255)
701
- return ((x * (levels - 1) * 2 + 255) / 510) | 0
780
+ // In f64, which cannot overflow; every value is a small whole number, so it is exact.
781
+ const x: f64 = Math.min(Math.max(toF64(v) + toF64(bias), 0.0), 255.0)
782
+ return toI32(Math.floor((x * (toF64(levels) - 1.0) * 2.0 + 255.0) / 510.0))
702
783
  }
703
784
 
704
785
  /**
@@ -706,10 +787,19 @@ const pixelsLevel = (v: i32, bias: i32, levels: i32): i32 => {
706
787
  * threshold `t` (0–15): spread evenly over one quantisation step, centred on
707
788
  * zero, so a flat colour between two levels becomes a fixed pattern of both.
708
789
  */
709
- const pixelsDitherBias = (t: i32, levels: i32): i32 => (((t * 2 - 15) * 255) / ((levels - 1) * 32)) | 0
790
+ const pixelsDitherBias = (t: i32, levels: i32): i32 => {
791
+ // In f64, which cannot overflow or divide by zero, rounded toward zero.
792
+ const q: f64 = ((toF64(t) * 2.0 - 15.0) * 255.0) / ((toF64(levels) - 1.0) * 32.0)
793
+ return toI32(q < 0.0 ? Math.ceil(q) : Math.floor(q))
794
+ }
710
795
 
711
796
  /** Append the sixel character for the six-pixel column `bits`, `count` times, run-length encoded. */
712
797
  const pixelsSixelRun = (out: PixelBytes, bits: i32, count: i32): void => {
798
+ // `bits` is six bits, 0 to 63; the test, never true for a run that is
799
+ // written, proves the character in range.
800
+ if (count <= 0 || bits < 0 || bits > 63) {
801
+ return
802
+ }
713
803
  // `!n` followed by the character repeats it n times (DECGRI); three or fewer
714
804
  // are as short written out.
715
805
  if (count > 3) {
@@ -759,13 +849,13 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
759
849
  if (w <= 0 || h <= 0 || fb.width <= 0 || fb.height <= 0) {
760
850
  return
761
851
  }
762
- pixelsCheckSize("sixelFrame", w, h)
763
- pixelsCheckSize("sixelFrame", w, SIXEL_REGISTERS)
852
+ const area: i32 = pixelsArea("sixelFrame", w, h)
853
+ const bitsArea: i32 = pixelsArea("sixelFrame", w, SIXEL_REGISTERS)
764
854
  // The working arrays live on `out`, grown once to the largest frame.
765
855
  out.sampleXs = pixelsAtLeast(out.sampleXs, w)
766
856
  out.sampleYs = pixelsAtLeast(out.sampleYs, h)
767
- out.sixelReg = pixelsAtLeast(out.sixelReg, w * h)
768
- out.sixelBits = pixelsAtLeast(out.sixelBits, SIXEL_REGISTERS * w)
857
+ out.sixelReg = pixelsAtLeast(out.sixelReg, area)
858
+ out.sixelBits = pixelsAtLeast(out.sixelBits, bitsArea)
769
859
  const xs: i32[] = out.sampleXs
770
860
  const ys: i32[] = out.sampleYs
771
861
  pixelsSampleMapInto(xs, w, fb.width)
@@ -777,9 +867,10 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
777
867
  // `ys` is at least `h` long and `xs` `w`: bounding the loops by their lengths
778
868
  // too is what proves the reads in range.
779
869
  for (let y: i32 = 0; y < h && y < toI32(ys.length); y++) {
780
- const from: i32 = ys[y] * fb.width
870
+ const sy: i32 = ys[y]
781
871
  for (let x: i32 = 0; x < w && x < toI32(xs.length); x++) {
782
- const c: i32 = fb.pixels[from + xs[x]]
872
+ // The sample maps hold pixels of `fb`, so this is never `get`'s black.
873
+ const c: i32 = fb.get(xs[x], sy)
783
874
  let br: i32 = 0
784
875
  let bg: i32 = 0
785
876
  let bb: i32 = 0
@@ -794,8 +885,14 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
794
885
  pixelsLevel((c >> 8) & 255, bg, 7),
795
886
  pixelsLevel(c & 255, bb, 6)
796
887
  )
797
- reg[y * w + x] = n
798
- // Always true (a register is 0 to 251); it proves the store in range.
888
+ // Never taken: `reg` holds at least `w * h`, from `pixelsAtLeast`, which
889
+ // the prover cannot see past.
890
+ const at: i32 = pixelsIndex(x, y, w, toI32(reg.length))
891
+ if (at < 0 || at >= toI32(reg.length)) {
892
+ panic("sixelFrame: the register array is shorter than the frame")
893
+ }
894
+ reg[at] = n
895
+ // Always true (a register is 0 to 251, or -1); it proves the store in range.
799
896
  if (n >= 0 && n < toI32(used.length)) {
800
897
  used[n] = 1
801
898
  }
@@ -807,7 +904,7 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
807
904
  out.putDecimal(w)
808
905
  out.put(PIXELS_SEMICOLON)
809
906
  out.putDecimal(h)
810
- for (let n: i32 = 0; n < toI32(used.length); n++) {
907
+ for (let n: i32 = 0; n < toI32(used.length) && n < SIXEL_REGISTERS; n++) {
811
908
  if (used[n] !== 0) {
812
909
  const r: i32 = (n / 42) | 0
813
910
  const g: i32 = ((n / 6) | 0) % 7
@@ -815,11 +912,11 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
815
912
  out.put(SIXEL_COLOUR)
816
913
  out.putDecimal(n)
817
914
  out.putText(";2;")
818
- out.putDecimal(((r * 100) / 5) | 0)
915
+ out.putDecimal(toI32(Math.floor((toF64(r) * 100.0) / 5.0)))
819
916
  out.put(PIXELS_SEMICOLON)
820
- out.putDecimal(((g * 100) / 6) | 0)
917
+ out.putDecimal(toI32(Math.floor((toF64(g) * 100.0) / 6.0)))
821
918
  out.put(PIXELS_SEMICOLON)
822
- out.putDecimal(((b * 100) / 5) | 0)
919
+ out.putDecimal(toI32(Math.floor((toF64(b) * 100.0) / 5.0)))
823
920
  }
824
921
  }
825
922
  // Per band: the first and last column each register appears in, so a colour
@@ -830,19 +927,35 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
830
927
  const first: i32[] = out.sixelFirst
831
928
  const last: i32[] = out.sixelLast
832
929
  const bits: i32[] = out.sixelBits
833
- for (let top: i32 = 0; top < h; top = top + 6) {
834
- const bottom: i32 = top + 6 < h ? top + 6 : h
930
+ for (let top: i32 = 0; top < h; ) {
931
+ // `top` below the largest i32 less 6 proves `top + 6`.
932
+ const bottom: i32 = top < 2147483641 && top + 6 < h ? top + 6 : h
835
933
  first.fill(-1)
934
+ let nextBit: i32 = PIXELS_ONE
836
935
  for (let y: i32 = top; y < bottom; y++) {
837
- const bit: i32 = PIXELS_ONE << (y - top)
936
+ const bit: i32 = nextBit
937
+ nextBit = nextBit << 1
838
938
  for (let x: i32 = 0; x < w; x++) {
839
- const n: i32 = reg[y * w + x]
840
- // Never taken (a register is 0 to 251); it proves `first[n]` and
841
- // `last[n]` in range.
842
- if (n < 0 || n >= toI32(first.length) || n >= toI32(last.length)) {
939
+ const at: i32 = pixelsIndex(x, y, w, toI32(reg.length))
940
+ // Never taken, as where `reg` was written.
941
+ if (at < 0 || at >= toI32(reg.length)) {
942
+ panic("sixelFrame: the register array is shorter than the frame")
943
+ }
944
+ const n: i32 = reg[at]
945
+ // `bits` holds a row of `w` for each register.
946
+ const slot: i32 = pixelsIndex(x, n, w, toI32(bits.length))
947
+ // Never taken (a register is 0 to 251, and `reg` and `bits` are
948
+ // large enough); it proves the reads and stores below in range.
949
+ if (
950
+ n < 0 ||
951
+ n >= toI32(first.length) ||
952
+ n >= toI32(last.length) ||
953
+ slot < 0 ||
954
+ slot >= toI32(bits.length)
955
+ ) {
843
956
  continue
844
957
  }
845
- bits[n * w + x] = bits[n * w + x] | bit
958
+ bits[slot] = bits[slot] | bit
846
959
  if (first[n] < 0) {
847
960
  first[n] = x
848
961
  last[n] = x
@@ -872,24 +985,33 @@ export const sixelFrame = (fb: Framebuffer, rect: PixelRect, dither: boolean, ou
872
985
  out.putDecimal(n)
873
986
  // Blank columns before the colour's first one, then its runs to its last.
874
987
  pixelsSixelRun(out, 0, start)
988
+ // The run is `runBits` from column `runStart` up to `next`.
875
989
  let runBits: i32 = -1
876
- let runCount: i32 = 0
877
- const row: i32 = n * w
878
- for (let x: i32 = start; x <= end; x++) {
879
- const sixel: i32 = bits[row + x]
880
- bits[row + x] = 0
881
- if (sixel === runBits) {
882
- runCount = runCount + 1
883
- } else {
884
- if (runCount > 0) {
885
- pixelsSixelRun(out, runBits, runCount)
990
+ let runStart: i32 = start
991
+ let next: i32 = start
992
+ for (let x: i32 = start; x <= end && x < w; x++) {
993
+ next = x + 1
994
+ const slot: i32 = pixelsIndex(x, n, w, toI32(bits.length))
995
+ // Never taken: `bits` holds a row of `w` for each register.
996
+ if (slot < 0 || slot >= toI32(bits.length)) {
997
+ continue
998
+ }
999
+ const sixel: i32 = bits[slot]
1000
+ bits[slot] = 0
1001
+ if (sixel !== runBits) {
1002
+ // `runStart` is never negative; the test says so for the prover.
1003
+ if (runStart >= 0 && x > runStart) {
1004
+ pixelsSixelRun(out, runBits, x - runStart)
886
1005
  }
887
1006
  runBits = sixel
888
- runCount = 1
1007
+ runStart = x
889
1008
  }
890
1009
  }
891
- pixelsSixelRun(out, runBits, runCount)
1010
+ if (runStart >= 0 && next > runStart) {
1011
+ pixelsSixelRun(out, runBits, toI32(toI64(next) - toI64(runStart)))
1012
+ }
892
1013
  }
1014
+ top = bottom
893
1015
  }
894
1016
  pixelsStringEnd(out)
895
1017
  }
@@ -912,29 +1034,61 @@ export const drawHalfBlocks = (fb: Framebuffer, canvas: Canvas, rect: PixelRect)
912
1034
  }
913
1035
  const xs: i32[] = pixelsSampleMap(rect.width, fb.width)
914
1036
  const ys: i32[] = pixelsSampleMap(rect.height, fb.height)
1037
+ // Read once: the calls below drop what the prover knows of fields.
915
1038
  const left: i32 = rect.col
916
- const top: i32 = rect.row * 2
917
- for (let y: i32 = 0; y < rect.rows * 2; y++) {
918
- const py: i32 = top + y
919
- if (py < 0 || py >= canvas.height) {
920
- continue
921
- }
922
- // `xs` is `rect.width` long and `ys` `rect.height`; their lengths bound the
923
- // reads so the prover sees them in range.
924
- for (let x: i32 = 0; x < toI32(xs.length); x++) {
925
- const px: i32 = left + x
926
- if (px < 0 || px >= canvas.width) {
1039
+ const cw: i32 = canvas.width
1040
+ const ch: i32 = canvas.height
1041
+ const cpixels: i32[] = canvas.pixels
1042
+ // The rect's canvas rows run from `top` to `bottom`, two to a cell row; f64
1043
+ // holds them exactly for any rect. Walk only the part on the canvas.
1044
+ const top: f64 = toF64(rect.row) * 2.0
1045
+ const bottom: f64 = top + toF64(rect.rows) * 2.0
1046
+ let pyStart: i32 = 0
1047
+ if (top >= toF64(ch)) {
1048
+ pyStart = ch
1049
+ } else if (top > 0.0) {
1050
+ pyStart = toI32(top)
1051
+ }
1052
+ const pxStart: i32 = left > 0 ? left : 0
1053
+ for (let py: i32 = pyStart; py < ch && toF64(py) < bottom; py++) {
1054
+ // `py` is at least `top` and below `ch`, so `yRow` is a row of the rect;
1055
+ // it can pass 2^31 - 1 when the rect starts far above the canvas, so it is
1056
+ // checked against `ys` before it becomes an index.
1057
+ const yRow: f64 = toF64(py) - top
1058
+ const y: i32 = yRow < toF64(ys.length) ? toI32(yRow) : -1
1059
+ for (let px: i32 = pxStart; px < cw; px++) {
1060
+ const x: i64 = toI64(px) - toI64(left)
1061
+ // `xs` is `rect.width` long and `ys` `rect.height`: past them, the
1062
+ // rect ends.
1063
+ if (x >= toI64(xs.length)) {
1064
+ break
1065
+ }
1066
+ const at: i32 = pixelsIndex(px, py, cw, toI32(cpixels.length))
1067
+ // In range while the canvas's `pixels` holds `width * height`. Its fields
1068
+ // are public, and one set out of step with the others is a broken
1069
+ // canvas, which panics as indexing past `pixels` did, rather than skip
1070
+ // the pixel.
1071
+ if (at < 0 || at >= toI32(cpixels.length)) {
1072
+ panic("drawHalfBlocks: the canvas's pixels does not hold width × height")
1073
+ }
1074
+ // Never taken: `px` starts at `left` or past it, and `x` is below
1075
+ // `xs.length` from the test above; it proves the read in range.
1076
+ const sx: i32 = x < toI64(0) ? -1 : toI32(x)
1077
+ if (sx < 0 || sx >= toI32(xs.length)) {
927
1078
  continue
928
1079
  }
929
- canvas.pixels[py * canvas.width + px] =
930
- y < toI32(ys.length) ? fb.pixels[ys[y] * fb.width + xs[x]] : COLOR_DEFAULT
1080
+ cpixels[at] = y >= 0 && y < toI32(ys.length) ? fb.get(xs[sx], ys[y]) : COLOR_DEFAULT
931
1081
  }
932
1082
  }
933
- for (let row: i32 = rect.row; row < rect.row + rect.rows; row++) {
934
- for (let col: i32 = left; col < left + rect.width; col++) {
935
- if (row >= 0 && col >= 0 && row * 2 < canvas.height && col < canvas.width) {
936
- canvas.paintCell(col, row)
937
- }
1083
+ // The cell rows `rect.row` up to `rect.row + rect.rows`, those on the canvas.
1084
+ const rowEnd: i64 = toI64(rect.row) + toI64(rect.rows)
1085
+ const colEnd: i64 = toI64(left) + toI64(rect.width)
1086
+ for (let row: i32 = rect.row > 0 ? rect.row : 0; row < ch && toI64(row) < rowEnd; row++) {
1087
+ if (toI64(row) * toI64(2) >= toI64(ch)) {
1088
+ break
1089
+ }
1090
+ for (let col: i32 = pxStart; col < cw && toI64(col) < colEnd; col++) {
1091
+ canvas.paintCell(col, row)
938
1092
  }
939
1093
  }
940
1094
  }
@@ -945,13 +1099,15 @@ const pixelsNearestAnsi16 = (colour: i32): i32 => {
945
1099
  const g: i32 = (colour >> 8) & 255
946
1100
  const b: i32 = colour & 255
947
1101
  let best: i32 = 0
948
- let bestDistance: i32 = 2147483647
1102
+ let bestDistance: f64 = 2147483647.0
949
1103
  for (let i: i32 = 0; i < 16; i++) {
950
1104
  const c: i32 = pixelsHex(PIXELS_ANSI16, i * 6, 6)
951
1105
  const dr: i32 = r - ((c >> 16) & 255)
952
1106
  const dg: i32 = g - ((c >> 8) & 255)
953
1107
  const db: i32 = b - (c & 255)
954
- const d: i32 = dr * dr + dg * dg + db * db
1108
+ // Each difference is within 255 of zero; f64 holds the sum exactly, where
1109
+ // the prover cannot bound it in i32.
1110
+ const d: f64 = toF64(dr) * toF64(dr) + toF64(dg) * toF64(dg) + toF64(db) * toF64(db)
955
1111
  if (d < bestDistance) {
956
1112
  best = c
957
1113
  bestDistance = d
@@ -983,17 +1139,36 @@ export const drawAscii = (fb: Framebuffer, screen: Screen, rect: PixelRect, colo
983
1139
  }
984
1140
  const xs: i32[] = pixelsSampleMap(rect.width, fb.width)
985
1141
  const ys: i32[] = pixelsSampleMap(rect.height, fb.height)
986
- const ramp: i32 = toI32(PIXELS_RAMP.length)
1142
+ // A local the prover can bound reads by.
1143
+ const rampText: string = PIXELS_RAMP
1144
+ const ramp: i32 = toI32(rampText.length)
987
1145
  // `ys` is `rect.height` long and `xs` `rect.width`; bounding the loops by
988
1146
  // their lengths proves the reads in range.
989
1147
  for (let y: i32 = 0; y < toI32(ys.length); y++) {
990
- const from: i32 = ys[y] * fb.width
1148
+ const sy: i32 = ys[y]
1149
+ // Past the largest i32 there is no screen row; `screen.set` drops rows
1150
+ // past its own height.
1151
+ const row: i64 = toI64(rect.row) + toI64(y)
1152
+ if (row > toI64(2147483647)) {
1153
+ break
1154
+ }
991
1155
  for (let x: i32 = 0; x < toI32(xs.length); x++) {
992
- const c: i32 = fb.pixels[from + xs[x]]
993
- const luma: i32 = ((((c >> 16) & 255) * 299 + ((c >> 8) & 255) * 587 + (c & 255) * 114) / 1000) | 0
994
- const glyph: i32 = toI32(PIXELS_RAMP.charCodeAt(((luma * ramp) / 256) | 0))
1156
+ const col: i64 = toI64(rect.col) + toI64(x)
1157
+ if (col > toI64(2147483647)) {
1158
+ break
1159
+ }
1160
+ const c: i32 = fb.get(xs[x], sy)
1161
+ const luma: f64 = Math.floor(
1162
+ (toF64((c >> 16) & 255) * 299.0 + toF64((c >> 8) & 255) * 587.0 + toF64(c & 255) * 114.0) / 1000.0
1163
+ )
1164
+ const k: i32 = toI32(Math.floor((luma * toF64(ramp)) / 256.0))
1165
+ // Never taken: `luma` is below 256, so `k` is below `ramp`.
1166
+ if (k < 0 || k >= ramp) {
1167
+ continue
1168
+ }
1169
+ const glyph: i32 = toI32(rampText.charCodeAt(k))
995
1170
  const fg: i32 = colors === 16 ? pixelsNearestAnsi16(c) : c
996
- screen.set(rect.col + x, rect.row + y, glyph, fg, COLOR_DEFAULT, ATTR_NONE)
1171
+ screen.set(toI32(col), toI32(row), glyph, fg, COLOR_DEFAULT, ATTR_NONE)
997
1172
  }
998
1173
  }
999
1174
  }