-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrayscale_test.go
More file actions
106 lines (95 loc) · 2.85 KB
/
grayscale_test.go
File metadata and controls
106 lines (95 loc) · 2.85 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
package xtx
import (
"image"
"testing"
)
func TestGrayscale_SetGet(t *testing.T) {
img := NewGrayscale(image.Rect(0, 0, 4, 4))
// All pixels default to 0 (white).
for y := 0; y < 4; y++ {
for x := 0; x < 4; x++ {
if got := img.Gray4At(x, y); got.V != 0 {
t.Errorf("default pixel (%d,%d) = %d, want 0", x, y, got.V)
}
}
}
// Set each pixel to a different level.
levels := []uint8{0, 1, 2, 3}
for y := 0; y < 4; y++ {
for x := 0; x < 4; x++ {
img.SetGray4(x, y, Gray4{V: levels[(x+y*4)%4]})
}
}
for y := 0; y < 4; y++ {
for x := 0; x < 4; x++ {
want := levels[(x+y*4)%4]
got := img.Gray4At(x, y)
if got.V != want {
t.Errorf("pixel (%d,%d) = %d, want %d", x, y, got.V, want)
}
}
}
}
func TestGrayscale_AllLevels(t *testing.T) {
// Verify all 4 levels survive set→get round-trip at every position in a byte.
img := NewGrayscale(image.Rect(0, 0, 1, 16))
for y := 0; y < 16; y++ {
level := uint8(y % 4)
img.SetGray4(0, y, Gray4{V: level})
}
for y := 0; y < 16; y++ {
want := uint8(y % 4)
got := img.Gray4At(0, y)
if got.V != want {
t.Errorf("pixel (0,%d) = %d, want %d", y, got.V, want)
}
}
}
func TestGrayscale_DataSize(t *testing.T) {
tests := []struct {
w, h int
want uint32
}{
{480, 800, 96000},
{1, 8, 2}, // 1 column, 1 byte per plane, 2 planes
{1, 1, 2}, // 1 column, 1 byte per plane (rounded up), 2 planes
{8, 8, 16}, // 8 columns × 1 byte each × 2 planes
}
for _, tt := range tests {
img := NewGrayscale(image.Rect(0, 0, tt.w, tt.h))
if got := img.DataSize(); got != tt.want {
t.Errorf("DataSize(%dx%d) = %d, want %d", tt.w, tt.h, got, tt.want)
}
}
}
func TestGrayscale_VerticalScan(t *testing.T) {
// Verify the vertical scan order: columns right-to-left, 8 pixels per byte.
// Create a 2×8 image: column 0 (right in scan) = all white (0),
// column 1 (left in scan, first in buffer) = all black (3).
img := NewGrayscale(image.Rect(0, 0, 2, 8))
// Set right column (x=1) to black.
for y := 0; y < 8; y++ {
img.SetGray4(1, y, Gray4Black) // bit1=1, bit2=1
}
// Left column (x=0) stays white (0).
// In vertical scan order, column index 0 in buffer = rightmost pixel column (x=1).
// colBytes = 1, so plane1 byte 0 = column x=1 bit1 values.
// Black = level 3: bit1=1, bit2=1 → plane1 byte 0 should be 0xFF.
if img.Pix[0] != 0xFF {
t.Errorf("plane1 col x=1: got 0x%02x, want 0xFF", img.Pix[0])
}
// Plane2 byte 0 should also be 0xFF.
if img.Pix[img.PlaneSize] != 0xFF {
t.Errorf("plane2 col x=1: got 0x%02x, want 0xFF", img.Pix[img.PlaneSize])
}
// Column x=0 (scan index 1) should be all zeros.
if img.Pix[1] != 0x00 {
t.Errorf("plane1 col x=0: got 0x%02x, want 0x00", img.Pix[1])
}
}
func TestGrayscale_ColorModel(t *testing.T) {
img := NewGrayscale(image.Rect(0, 0, 8, 8))
if img.ColorModel() != Gray4Model {
t.Error("ColorModel should be Gray4Model")
}
}