| 1 |
package kitty |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"compress/zlib" |
| 6 |
"image" |
| 7 |
"image/color" |
| 8 |
"io" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
// taken from "image/png" package |
| 13 |
const pngHeader = "\x89PNG\r\n\x1a\n" |
| 14 |
|
| 15 |
// testImage creates a simple test image with a red and blue pattern |
| 16 |
func testImage() *image.RGBA { |
| 17 |
img := image.NewRGBA(image.Rect(0, 0, 2, 2)) |
| 18 |
img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255}) // Red |
| 19 |
img.Set(1, 0, color.RGBA{R: 0, G: 0, B: 255, A: 255}) // Blue |
| 20 |
img.Set(0, 1, color.RGBA{R: 0, G: 0, B: 255, A: 255}) // Blue |
| 21 |
img.Set(1, 1, color.RGBA{R: 255, G: 0, B: 0, A: 255}) // Red |
| 22 |
return img |
| 23 |
} |
| 24 |
|
| 25 |
func TestEncoder_Encode(t *testing.T) { |
| 26 |
tests := []struct { |
| 27 |
name string |
| 28 |
encoder Encoder |
| 29 |
img image.Image |
| 30 |
wantErr bool |
| 31 |
verify func([]byte) error |
| 32 |
}{ |
| 33 |
{ |
| 34 |
name: "nil image", |
| 35 |
encoder: Encoder{ |
| 36 |
Format: RGBA, |
| 37 |
}, |
| 38 |
img: nil, |
| 39 |
wantErr: false, |
| 40 |
verify: func(got []byte) error { |
| 41 |
if len(got) != 0 { |
| 42 |
t.Errorf("expected empty output for nil image, got %d bytes", len(got)) |
| 43 |
} |
| 44 |
return nil |
| 45 |
}, |
| 46 |
}, |
| 47 |
{ |
| 48 |
name: "RGBA format", |
| 49 |
encoder: Encoder{ |
| 50 |
Format: RGBA, |
| 51 |
}, |
| 52 |
img: testImage(), |
| 53 |
wantErr: false, |
| 54 |
verify: func(got []byte) error { |
| 55 |
expected := []byte{ |
| 56 |
255, 0, 0, 255, // Red pixel |
| 57 |
0, 0, 255, 255, // Blue pixel |
| 58 |
0, 0, 255, 255, // Blue pixel |
| 59 |
255, 0, 0, 255, // Red pixel |
| 60 |
} |
| 61 |
if !bytes.Equal(got, expected) { |
| 62 |
t.Errorf("unexpected RGBA output\ngot: %v\nwant: %v", got, expected) |
| 63 |
} |
| 64 |
return nil |
| 65 |
}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "RGB format", |
| 69 |
encoder: Encoder{ |
| 70 |
Format: RGB, |
| 71 |
}, |
| 72 |
img: testImage(), |
| 73 |
wantErr: false, |
| 74 |
verify: func(got []byte) error { |
| 75 |
expected := []byte{ |
| 76 |
255, 0, 0, // Red pixel |
| 77 |
0, 0, 255, // Blue pixel |
| 78 |
0, 0, 255, // Blue pixel |
| 79 |
255, 0, 0, // Red pixel |
| 80 |
} |
| 81 |
if !bytes.Equal(got, expected) { |
| 82 |
t.Errorf("unexpected RGB output\ngot: %v\nwant: %v", got, expected) |
| 83 |
} |
| 84 |
return nil |
| 85 |
}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "PNG format", |
| 89 |
encoder: Encoder{ |
| 90 |
Format: PNG, |
| 91 |
}, |
| 92 |
img: testImage(), |
| 93 |
wantErr: false, |
| 94 |
verify: func(got []byte) error { |
| 95 |
// Verify PNG header |
| 96 |
// if len(got) < 8 || !bytes.Equal(got[:8], []byte{137, 80, 78, 71, 13, 10, 26, 10}) { |
| 97 |
if len(got) < 8 || !bytes.Equal(got[:8], []byte(pngHeader)) { |
| 98 |
t.Error("invalid PNG header") |
| 99 |
} |
| 100 |
return nil |
| 101 |
}, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "invalid format", |
| 105 |
encoder: Encoder{ |
| 106 |
Format: 999, // Invalid format |
| 107 |
}, |
| 108 |
img: testImage(), |
| 109 |
wantErr: true, |
| 110 |
verify: nil, |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "RGBA with compression", |
| 114 |
encoder: Encoder{ |
| 115 |
Format: RGBA, |
| 116 |
Compress: true, |
| 117 |
}, |
| 118 |
img: testImage(), |
| 119 |
wantErr: false, |
| 120 |
verify: func(got []byte) error { |
| 121 |
// Decompress the data |
| 122 |
r, err := zlib.NewReader(bytes.NewReader(got)) |
| 123 |
if err != nil { |
| 124 |
return err //nolint:wrapcheck |
| 125 |
} |
| 126 |
defer r.Close() |
| 127 |
|
| 128 |
decompressed, err := io.ReadAll(r) |
| 129 |
if err != nil { |
| 130 |
return err //nolint:wrapcheck |
| 131 |
} |
| 132 |
|
| 133 |
expected := []byte{ |
| 134 |
255, 0, 0, 255, // Red pixel |
| 135 |
0, 0, 255, 255, // Blue pixel |
| 136 |
0, 0, 255, 255, // Blue pixel |
| 137 |
255, 0, 0, 255, // Red pixel |
| 138 |
} |
| 139 |
if !bytes.Equal(decompressed, expected) { |
| 140 |
t.Errorf("unexpected decompressed output\ngot: %v\nwant: %v", decompressed, expected) |
| 141 |
} |
| 142 |
return nil |
| 143 |
}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "zero format defaults to RGBA", |
| 147 |
encoder: Encoder{ |
| 148 |
Format: 0, |
| 149 |
}, |
| 150 |
img: testImage(), |
| 151 |
wantErr: false, |
| 152 |
verify: func(got []byte) error { |
| 153 |
expected := []byte{ |
| 154 |
255, 0, 0, 255, // Red pixel |
| 155 |
0, 0, 255, 255, // Blue pixel |
| 156 |
0, 0, 255, 255, // Blue pixel |
| 157 |
255, 0, 0, 255, // Red pixel |
| 158 |
} |
| 159 |
if !bytes.Equal(got, expected) { |
| 160 |
t.Errorf("unexpected RGBA output\ngot: %v\nwant: %v", got, expected) |
| 161 |
} |
| 162 |
return nil |
| 163 |
}, |
| 164 |
}, |
| 165 |
} |
| 166 |
|
| 167 |
for _, tt := range tests { |
| 168 |
t.Run(tt.name, func(t *testing.T) { |
| 169 |
var buf bytes.Buffer |
| 170 |
err := tt.encoder.Encode(&buf, tt.img) |
| 171 |
|
| 172 |
if (err != nil) != tt.wantErr { |
| 173 |
t.Errorf("Encode() error = %v, wantErr %v", err, tt.wantErr) |
| 174 |
return |
| 175 |
} |
| 176 |
|
| 177 |
if !tt.wantErr && tt.verify != nil { |
| 178 |
if err := tt.verify(buf.Bytes()); err != nil { |
| 179 |
t.Errorf("verification failed: %v", err) |
| 180 |
} |
| 181 |
} |
| 182 |
}) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func TestEncoder_EncodeWithDifferentImageTypes(t *testing.T) { |
| 187 |
// Create different image types for testing |
| 188 |
rgba := image.NewRGBA(image.Rect(0, 0, 1, 1)) |
| 189 |
rgba.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255}) |
| 190 |
|
| 191 |
gray := image.NewGray(image.Rect(0, 0, 1, 1)) |
| 192 |
gray.Set(0, 0, color.Gray{Y: 128}) |
| 193 |
|
| 194 |
tests := []struct { |
| 195 |
name string |
| 196 |
img image.Image |
| 197 |
format int |
| 198 |
wantLen int |
| 199 |
}{ |
| 200 |
{ |
| 201 |
name: "RGBA image to RGBA format", |
| 202 |
img: rgba, |
| 203 |
format: RGBA, |
| 204 |
wantLen: 4, // 4 bytes per pixel |
| 205 |
}, |
| 206 |
{ |
| 207 |
name: "Gray image to RGBA format", |
| 208 |
img: gray, |
| 209 |
format: RGBA, |
| 210 |
wantLen: 4, // 4 bytes per pixel |
| 211 |
}, |
| 212 |
{ |
| 213 |
name: "RGBA image to RGB format", |
| 214 |
img: rgba, |
| 215 |
format: RGB, |
| 216 |
wantLen: 3, // 3 bytes per pixel |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "Gray image to RGB format", |
| 220 |
img: gray, |
| 221 |
format: RGB, |
| 222 |
wantLen: 3, // 3 bytes per pixel |
| 223 |
}, |
| 224 |
} |
| 225 |
|
| 226 |
for _, tt := range tests { |
| 227 |
t.Run(tt.name, func(t *testing.T) { |
| 228 |
var buf bytes.Buffer |
| 229 |
enc := Encoder{Format: tt.format} |
| 230 |
|
| 231 |
err := enc.Encode(&buf, tt.img) |
| 232 |
if err != nil { |
| 233 |
t.Errorf("Encode() error = %v", err) |
| 234 |
return |
| 235 |
} |
| 236 |
|
| 237 |
if got := buf.Len(); got != tt.wantLen { |
| 238 |
t.Errorf("Encode() output length = %v, want %v", got, tt.wantLen) |
| 239 |
} |
| 240 |
}) |
| 241 |
} |
| 242 |
} |