Skip to content

Commit

Permalink
Assemble all the test outputs in one big png.
Browse files Browse the repository at this point in the history
Uses pngdiff from toit-png-tools as a submodule
in order to make a diff when test results diff
from gold.  This lets us use highly compressed
pngs in the gold directory.
  • Loading branch information
Erik Corry committed Dec 4, 2023
1 parent dddb92e commit ab9c004
Show file tree
Hide file tree
Showing 65 changed files with 117 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tests/toit-png-tools"]
path = tests/toit-png-tools
url = https://github.com/toitware/toit-png-tools.git
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build/CMakeCache.txt:

install-pkgs: rebuild-cmake
(cd build && ninja install-pkgs)
(cd tests/toit-png-tools && $(MAKE) install-pkgs)

test: install-pkgs rebuild-cmake
(cd build && ninja check)
Expand Down
2 changes: 2 additions & 0 deletions tests/4_gray_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ main args:

middle_line.text = "the DISPLAY"
display.draw

driver.write_png
2 changes: 2 additions & 0 deletions tests/bw_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ main args:

middle_line.text = "the DISPLAY"
display.draw

driver.write_png
2 changes: 2 additions & 0 deletions tests/drop_shadow_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ main args:
text.move_to 15 37

display.draw

driver.write_png
Binary file removed tests/gold/4_gray_visualized.toit-000000.png
Binary file not shown.
Binary file removed tests/gold/4_gray_visualized.toit-000001.png
Binary file not shown.
Binary file removed tests/gold/4_gray_visualized.toit-000002.png
Binary file not shown.
Binary file removed tests/gold/4_gray_visualized.toit-000003.png
Binary file not shown.
Binary file removed tests/gold/4_gray_visualized.toit-000004.png
Binary file not shown.
Binary file removed tests/gold/4_gray_visualized.toit-000005.png
Binary file not shown.
Binary file removed tests/gold/4_gray_visualized.toit-000006.png
Binary file not shown.
Binary file removed tests/gold/4_gray_visualized.toit-000007.png
Binary file not shown.
Binary file added tests/gold/4_gray_visualized.toit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed tests/gold/bw_visualized.toit-000000.png
Binary file not shown.
Binary file removed tests/gold/bw_visualized.toit-000001.png
Binary file not shown.
Binary file removed tests/gold/bw_visualized.toit-000002.png
Binary file not shown.
Binary file removed tests/gold/bw_visualized.toit-000003.png
Binary file not shown.
Binary file removed tests/gold/bw_visualized.toit-000004.png
Binary file not shown.
Binary file removed tests/gold/bw_visualized.toit-000005.png
Binary file not shown.
Binary file removed tests/gold/bw_visualized.toit-000006.png
Binary file not shown.
Binary file removed tests/gold/bw_visualized.toit-000007.png
Binary file not shown.
Binary file added tests/gold/bw_visualized.toit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed tests/gold/drop_shadow_visualized.toit-000000.png
Binary file not shown.
Binary file removed tests/gold/drop_shadow_visualized.toit-000001.png
Binary file not shown.
Binary file removed tests/gold/drop_shadow_visualized.toit-000002.png
Binary file not shown.
Binary file removed tests/gold/drop_shadow_visualized.toit-000003.png
Binary file not shown.
Binary file removed tests/gold/drop_shadow_visualized.toit-000004.png
Binary file not shown.
Binary file removed tests/gold/drop_shadow_visualized.toit-000005.png
Binary file not shown.
Binary file added tests/gold/drop_shadow_visualized.toit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed tests/gold/gray_scale_visualized.toit-000000.png
Diff not rendered.
Binary file removed tests/gold/gray_scale_visualized.toit-000001.png
Diff not rendered.
Binary file removed tests/gold/gray_scale_visualized.toit-000002.png
Diff not rendered.
Binary file removed tests/gold/gray_scale_visualized.toit-000003.png
Diff not rendered.
Binary file removed tests/gold/gray_scale_visualized.toit-000004.png
Diff not rendered.
Binary file removed tests/gold/gray_scale_visualized.toit-000005.png
Diff not rendered.
Binary file removed tests/gold/gray_scale_visualized.toit-000006.png
Diff not rendered.
Binary file removed tests/gold/gray_scale_visualized.toit-000007.png
Diff not rendered.
Binary file added tests/gold/gray_scale_visualized.toit.png
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Binary file removed tests/gold/true_color_visualized.toit-000000.png
Diff not rendered.
Binary file removed tests/gold/true_color_visualized.toit-000001.png
Diff not rendered.
Binary file removed tests/gold/true_color_visualized.toit-000002.png
Diff not rendered.
Binary file removed tests/gold/true_color_visualized.toit-000003.png
Diff not rendered.
Binary file removed tests/gold/true_color_visualized.toit-000004.png
Diff not rendered.
Binary file removed tests/gold/true_color_visualized.toit-000005.png
Diff not rendered.
Binary file removed tests/gold/true_color_visualized.toit-000006.png
Diff not rendered.
Binary file removed tests/gold/true_color_visualized.toit-000007.png
Diff not rendered.
Binary file added tests/gold/true_color_visualized.toit.png
2 changes: 2 additions & 0 deletions tests/gray_scale_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ main args:

middle_line.text = "the DISPLAY"
display.draw

driver.write_png
3 changes: 3 additions & 0 deletions tests/package.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sdk: ^2.0.0-alpha.121
prefixes:
host: pkg-host
pixel_display: ..
png-tools: toit-png-tools
packages:
..:
path: ..
Expand All @@ -10,3 +11,5 @@ packages:
name: host
version: 1.10.0
hash: e1c11a0e0bf65fe810520e8d893e554a8fe33b78
toit-png-tools:
path: toit-png-tools
2 changes: 2 additions & 0 deletions tests/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ dependencies:
version: ^1.10.0
pixel_display:
path: ..
png-tools:
path: toit-png-tools/
160 changes: 94 additions & 66 deletions tests/png-visualizer.toit
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,57 @@ import crypto.crc show *
import host.file
import monitor show Latch
import pixel_display show *
import png_tools.png_writer
import png_tools.png_reader show *
import zlib

class TwoColorPngVisualizer extends PngVisualizingDriver_:
flags ::= FLAG_2_COLOR | FLAG_PARTIAL_UPDATES
constructor width height basename --outline/int?=null: super width height basename --outline=outline
width_to_byte_width w: return (round_up w 8) >> 3
x_rounding := 8 // 8 pixels per byte in PNG.
y_rounding := 8 // 8 pixels per byte in canvas.
class ThreeColorPngVisualizer extends PngVisualizingDriver_:
flags ::= FLAG_3_COLOR | FLAG_PARTIAL_UPDATES
constructor width height basename --outline/int?=null: super width height basename --outline=outline
width_to_byte_width w: return (round_up w 4) >> 2
x_rounding := 4 // 4 pixels per byte.
y_rounding := 8 // 8 pixels per byte in canvas.
class FourGrayPngVisualizer extends PngVisualizingDriver_:
flags ::= FLAG_4_COLOR | FLAG_PARTIAL_UPDATES
constructor width height basename --outline/int?=null: super width height basename --outline=outline
width_to_byte_width w: return (round_up w 4) >> 2
x_rounding := 4 // 4 pixels per byte.
y_rounding := 8 // 8 pixels per byte in canvas.
class TrueColorPngVisualizer extends PngVisualizingDriver_:
flags ::= FLAG_TRUE_COLOR | FLAG_PARTIAL_UPDATES
constructor width height basename --outline/int?=null: super width height basename --outline=outline
width_to_byte_width w: return w * 3
x_rounding := 1
y_rounding := 1

class GrayScalePngVisualizer extends PngVisualizingDriver_:
flags ::= FLAG_GRAY_SCALE | FLAG_PARTIAL_UPDATES
constructor width height basename --outline/int?=null: super width height basename --outline=outline
width_to_byte_width w: return w
x_rounding := 1
y_rounding := 1

class SeveralColorPngVisualizer extends PngVisualizingDriver_:
flags ::= FLAG_SEVERAL_COLOR | FLAG_PARTIAL_UPDATES
constructor width height basename --outline/int?=null: super width height basename --outline=outline
width_to_byte_width w: return w
x_rounding := 1
y_rounding := 1

abstract class PngVisualizingDriver_ extends AbstractDriver:
width /int ::= ?
height /int ::= ?
width_ /int ::= ? // Rounded up to a multiple of 8.
height_ /int ::= ? // Rounded up to a multiple of 8.
width_ /int := 0 // Rounded up to a multiple of 8.
height_ /int := 0 // Rounded up to a multiple of 8.
outline_buffer_ /ByteArray? := null
buffer_ /ByteArray := #[]
temp_buffer_/ByteArray := #[]
Expand All @@ -58,14 +72,17 @@ abstract class PngVisualizingDriver_ extends AbstractDriver:
static INVERT_ := ByteArray 0x100: 0xff - it

constructor .width .height basename/string --.outline/int?=null:
width_ = round_up width 8
height_ = round_up height 8
png_basename_ = basename
///
width_ = round_up width x_rounding
height_ = round_up height y_rounding
buffer_ = ByteArray
(width_to_byte_width width_) * height_
if outline:
outline_buffer_ = buffer_.copy

snapshots_ := []

draw_true_color left/int top/int right/int bottom/int red/ByteArray green/ByteArray blue/ByteArray -> none:
patch_width := right - left
top_left := min buffer_.size (3 * (left + width_ * top))
Expand Down Expand Up @@ -95,7 +112,7 @@ abstract class PngVisualizingDriver_ extends AbstractDriver:
top_left := min buffer_.size (left + width_ * top)
if outline:
pixels2 := pixels.copy
draw_byte_outline_ outline pixels2 patch_width
draw_byte_outline_ outline pixels2 patch_width --dotted

blit pixels2 outline_buffer_[top_left..] patch_width --destination_line_stride=width_

Expand Down Expand Up @@ -180,14 +197,14 @@ abstract class PngVisualizingDriver_ extends AbstractDriver:
pixels[y] |= 0b01010101
pixels[y + patch_width - 1] |= 0b01010101

draw_byte_outline_ outline/int pixels/ByteArray patch_width/int -> none:
draw_byte_outline_ outline/int pixels/ByteArray patch_width/int --dotted=false -> none:
bottom_left := pixels.size - patch_width
// Dotted line along top and bottom.
for x := 0; x < patch_width; x += 2:
for x := 0; x < patch_width; x += dotted ? 2 : 1:
pixels[x] = outline
pixels[bottom_left + x] = outline
// Dotted line along left and right.
for y := 0; y < pixels.size; y += patch_width * 2:
for y := 0; y < pixels.size; y += patch_width * (dotted ? 2 : 1):
pixels[y] = outline
pixels[y + patch_width - 1] = outline

Expand All @@ -203,7 +220,7 @@ abstract class PngVisualizingDriver_ extends AbstractDriver:

write_png_two_bit buffer/ByteArray left/int top/int right/int bottom/int plane_0/ByteArray plane_1/ByteArray -> none:
patch_width := right - left
assert: patch_width == (round_up patch_width 8)
assert: patch_width == (round_up patch_width 4)
patch_height := bottom - top
assert: patch_height == (round_up patch_height 8)

Expand All @@ -229,7 +246,6 @@ abstract class PngVisualizingDriver_ extends AbstractDriver:
buffer[out_index + (width_to_byte_width x)] = out
row += patch_width

png_counter := 0
png_basename_/string

static HEADER ::= #[0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n']
Expand All @@ -255,62 +271,92 @@ abstract class PngVisualizingDriver_ extends AbstractDriver:

commit left/int top/int right/int bottom/int -> none:
if outline:
write_png outline_buffer_
write_snapshot outline_buffer_
outline_buffer_.replace 0 buffer_
write_png buffer_
write_snapshot buffer_

write_snapshot buffer/ByteArray -> none:
snapshots_.add buffer.copy

write_png buffer/ByteArray -> none:
write_png -> none:
true_color := flags & FLAG_TRUE_COLOR != 0
gray := flags & FLAG_4_COLOR != 0
three_color := flags & FLAG_3_COLOR != 0
gray_scale := flags & FLAG_GRAY_SCALE != 0
several_color := flags & FLAG_SEVERAL_COLOR != 0

writeable := file.Stream.for_write "$png_basename_-$(%06d png_counter++).png"
writeable := file.Stream.for_write "$(png_basename_).png"

frames_across := 2
frames_down := snapshots_.size / frames_across

padding := 32

mega_width := frames_across * width_ + (frames_across + 1) * padding
mega_height := frames_down * height_ + (frames_down + 1) * padding

write_ writeable HEADER
mega_buffer := ByteArray
(width_to_byte_width mega_width) * mega_height

bits_per_pixel := ?
bit_depth := ?
color_type := ?
if true_color:
bits_per_pixel = 8
color_type = 2 // True color.
bit_depth = 8
color_type = COLOR-TYPE-TRUECOLOR
else if gray_scale:
bits_per_pixel = 8
color_type = 0 // Gray scale.
bit_depth = 8
color_type = COLOR-TYPE-GRAYSCALE
else if three_color:
bits_per_pixel = 2
color_type = 3 // Palette.
bit_depth = 2
color_type = COLOR-TYPE-INDEXED
else if several_color:
bits_per_pixel = 8
color_type = 3 // Palette.
bit_depth = 8
color_type = COLOR-TYPE-INDEXED
else if gray:
bits_per_pixel = 2
color_type = 0 // Grayscale.
bit_depth = 2
color_type = COLOR-TYPE-GRAYSCALE
else:
bits_per_pixel = 1
color_type = 0 // Grayscale
ihdr := #[
0, 0, 0, 0, // Width.
0, 0, 0, 0, // Height.
bits_per_pixel,
color_type,
0, 0, 0,
]
BIG_ENDIAN.put_uint32 ihdr 0 width
BIG_ENDIAN.put_uint32 ihdr 4 height
write_chunk writeable "IHDR" ihdr
bit_depth = 1
color_type = COLOR-TYPE-GRAYSCALE

for y := 0; y < frames_down; y++:
for x := 0; x < frames_across; x++:
snapshot_index := y * frames_across + x
if snapshot_index >= snapshots_.size: continue
snapshot/ByteArray := snapshots_[snapshot_index]
bits_per_line_raw := bit_depth * width_
bits_per_line_rounded := round_up bits_per_line_raw 8
if bits_per_line_rounded != bits_per_line_raw:
// Zap the bits at the end of each line.
for y2 := 0; y2 < height_; y2++:
// Index of last byte of the line.
index := (y2 + 1) * (width_to_byte_width width_) - 1
// Mask out the bits that are beyond the last real pixel.
snapshot[index] &= 0xff << (bits_per_line_rounded - bits_per_line_raw)
pixel_index := (y * (height_ + padding) + padding) * (width_to_byte_width mega_width) + (width_to_byte_width (x * (width_ + padding) + padding))
blit
snapshot // Source.
mega_buffer[pixel_index..] // Destination.
width_to_byte_width width_ // Bytes per line
--destination_line_stride=(width_to_byte_width mega_width)
--source_line_stride=(width_to_byte_width width_)

png_writer := png_writer.PngWriter
writeable
mega_width
mega_height
--bit_depth=bit_depth
--color_type=color_type

if three_color:
write_chunk writeable "PLTE" #[ // Palette.
png_writer.write_chunk "PLTE" #[ // Palette.
0xff, 0xff, 0xff, // 0 is white.
0, 0, 0, // 1 is black.
0xff, 0, 0, // 2 is red.
]
else if several_color:
// Use color palette of 7-color epaper display.
write_chunk writeable "PLTE" #[ // Palette.
png_writer.write_chunk "PLTE" #[ // Palette.
0xff, 0xff, 0xff, // 0 is white.
0, 0, 0, // 1 is black.
0xff, 0, 0, // 2 is red.
Expand All @@ -324,38 +370,20 @@ abstract class PngVisualizingDriver_ extends AbstractDriver:
0xc0, 0xc0, 0xc0, // 9 is light gray.
]

compressor := zlib.RunLengthZlibEncoder
done := Latch
compressed := Buffer

task::
while data := compressor.reader.read:
compressed.write data
done.set null

zero_byte := #[0]
line_size := width_to_byte_width width
line_step := width_to_byte_width width_
height.repeat: | y |
compressor.write zero_byte // Adaptive scheme.
line_size := width_to_byte_width mega_width
line_step := width_to_byte_width mega_width
mega_height.repeat: | y |
png_writer.write_uncompressed zero_byte // Adaptive scheme.
index := y * line_step
line := buffer[index..index + line_size]
line := mega_buffer[index..index + line_size]
if gray:
line = ByteArray line.size: line[it] ^ 0xff
else if several_color:
line = ByteArray line.size: min SEVERAL_MAX_COLOR_ line[it]
compressor.write line

compressor.close

// Wait for the reader task to finish.
done.get

if compressed.size != 0:
write_chunk writeable "IDAT" compressed.bytes // Compressed pixel data.
write_chunk writeable "IEND" #[] // End chunk.
png_writer.write_uncompressed line

png_writer.close
writeable.close

SEVERAL_WHITE ::= 0
Expand Down
1 change: 1 addition & 0 deletions tests/toit-png-tools
Submodule toit-png-tools added at ee472a
2 changes: 2 additions & 0 deletions tests/true_color_portrait_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ main args:

middle_line.text = "the DisplaY"
display.draw

driver.write_png
2 changes: 2 additions & 0 deletions tests/true_color_visualized.toit
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ main args:

middle_line.text = "the DISPLAY"
display.draw

driver.write_png
14 changes: 1 addition & 13 deletions tests/visualized.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,4 @@ mkdir -p tests/out

$TOIT_EXE -Xenable_asserts tests/$TOIT_PROGRAM tests/out/$TOIT_PROGRAM

for outfilename in tests/out/$TOIT_PROGRAM-*.png
do
echo $outfilename
goldfilename=${outfilename/out/gold}
cmp $goldfilename $outfilename
done

for goldfilename in tests/gold/$TOIT_PROGRAM-*.png
do
outfilename=${goldfilename/gold/out}
cmp $goldfilename $outfilename
done

$TOIT_EXE tests/toit-png-tools/bin/pngdiff.toit -o tests/out/diff-$TOIT_PROGRAM.png -t tests/out/$TOIT_PROGRAM.png tests/gold/$TOIT_PROGRAM.png

0 comments on commit ab9c004

Please sign in to comment.