@@ -107,6 +107,66 @@ func TestImageRGB444BE(t *testing.T) {
107107 }
108108}
109109
110+ func TestImageGrayscale2bit (t * testing.T ) {
111+ image := pixel .NewImage [pixel.Grayscale2bit ](128 , 64 )
112+
113+ if width , height := image .Size (); width != 128 || height != 64 {
114+ t .Errorf ("image.Size(): expected 128, 64 but got %d, %d" , width , height )
115+ }
116+
117+ // Define test colors representing 4 Grayscale levels.
118+ testColors := []color.RGBA {
119+ {R : 0x00 , G : 0x00 , B : 0x00 , A : 0xff }, // black
120+ {R : 0x55 , G : 0x55 , B : 0x55 , A : 0xff }, // dark gray
121+ {R : 0xaa , G : 0xaa , B : 0xaa , A : 0xff }, // light gray
122+ {R : 0xff , G : 0xff , B : 0xff , A : 0xff }, // white
123+ }
124+
125+ // Single pixel roundtrip test at a fixed coordinate.
126+ for _ , c := range testColors {
127+ encoded := pixel .NewColor [pixel.Grayscale2bit ](c .R , c .G , c .B )
128+ image .Set (5 , 3 , encoded )
129+ actual := image .Get (5 , 3 ).RGBA ()
130+ if actual != c {
131+ t .Errorf ("failed to roundtrip color: expected %v but got %v" , c , actual )
132+ }
133+ }
134+
135+ // Multi-coordinate test across the image.
136+ for x := 0 ; x < 8 ; x ++ {
137+ for y , c := range testColors {
138+ encoded := pixel .NewColor [pixel.Grayscale2bit ](c .R , c .G , c .B )
139+ image .Set (x , y , encoded )
140+ actual := image .Get (x , y ).RGBA ()
141+ if actual != c {
142+ t .Errorf ("Set/Get mismatch at (%d,%d): expected %v but got %v" , x , y , c , actual )
143+ }
144+ }
145+ }
146+ }
147+
148+ func TestNewGrayscale2bitMapping (t * testing.T ) {
149+ testCases := []struct {
150+ input color.RGBA
151+ expect pixel.Grayscale2bit
152+ }{
153+ {color.RGBA {R : 0x00 , G : 0x00 , B : 0x00 }, 0 }, // 0
154+ {color.RGBA {R : 0x3F , G : 0x3F , B : 0x3F }, 0 }, // 63
155+ {color.RGBA {R : 0x40 , G : 0x40 , B : 0x40 }, 1 }, // 64
156+ {color.RGBA {R : 0x7F , G : 0x7F , B : 0x7F }, 1 }, // 127
157+ {color.RGBA {R : 0x80 , G : 0x80 , B : 0x80 }, 2 }, // 128
158+ {color.RGBA {R : 0xBF , G : 0xBF , B : 0xBF }, 2 }, // 191
159+ {color.RGBA {R : 0xC0 , G : 0xC0 , B : 0xC0 }, 3 }, // 192
160+ {color.RGBA {R : 0xFF , G : 0xFF , B : 0xFF }, 3 }, // 255
161+ }
162+ for _ , tc := range testCases {
163+ actual := pixel .NewColor [pixel.Grayscale2bit ](tc .input .R , tc .input .G , tc .input .B )
164+ if actual != tc .expect {
165+ t .Errorf ("NewGrayscale2bit(%#v) = %d, want %d" , tc .input , actual , tc .expect )
166+ }
167+ }
168+ }
169+
110170func TestImageMonochrome (t * testing.T ) {
111171 image := pixel .NewImage [pixel.Monochrome ](128 , 64 )
112172 if width , height := image .Size (); width != 128 || height != 64 {
@@ -236,6 +296,9 @@ func TestImageNoise(t *testing.T) {
236296 t .Run ("RGB444BE" , func (t * testing.T ) {
237297 testImageNoiseN [pixel.RGB444BE ](t )
238298 })
299+ t .Run ("Grayscale2bit" , func (t * testing.T ) {
300+ testImageNoiseN [pixel.Grayscale2bit ](t )
301+ })
239302 t .Run ("Monochrome" , func (t * testing.T ) {
240303 testImageNoiseN [pixel.Monochrome ](t )
241304 })
0 commit comments