-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloatencode_test.go
More file actions
275 lines (235 loc) · 6.79 KB
/
floatencode_test.go
File metadata and controls
275 lines (235 loc) · 6.79 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package jpeg2000
import (
"bytes"
"math"
"testing"
"github.com/mrjoshuak/go-jpeg2000/internal/codestream"
)
// TestEncodeFloatGrayscaleRoundtrip tests encoding and decoding a single-channel
// float32 image through the NLT pipeline, verifying bitwise equality.
func TestEncodeFloatGrayscaleRoundtrip(t *testing.T) {
width, height := 8, 8
values := []float32{
0, math.Float32frombits(0x80000000), // +0, -0
1.0, -1.0,
math.SmallestNonzeroFloat32,
math.MaxFloat32,
float32(math.Inf(1)),
float32(math.Inf(-1)),
}
// Fill an 8x8 image with test values, cycling through them
comp := make([]float32, width*height)
for i := range comp {
comp[i] = values[i%len(values)]
}
img := &FloatImage{
Width: width,
Height: height,
Components: [][]float32{comp},
BitDepth: 32,
Signed: true,
}
opts := DefaultOptions()
opts.Format = FormatJ2K
opts.NumResolutions = 2
var buf bytes.Buffer
if err := EncodeFloat(&buf, img, opts); err != nil {
t.Fatalf("EncodeFloat: %v", err)
}
decoded, err := DecodeFloatConfig(bytes.NewReader(buf.Bytes()), nil)
if err != nil {
t.Fatalf("DecodeFloat: %v", err)
}
if decoded.Width != width || decoded.Height != height {
t.Fatalf("dimensions mismatch: got %dx%d, want %dx%d",
decoded.Width, decoded.Height, width, height)
}
if len(decoded.Components) != 1 {
t.Fatalf("component count: got %d, want 1", len(decoded.Components))
}
for i := 0; i < width*height; i++ {
got := decoded.Components[0][i]
want := comp[i]
// NaN needs special comparison
if math.IsNaN(float64(want)) {
if !math.IsNaN(float64(got)) {
t.Errorf("pixel %d: got %v, want NaN", i, got)
}
continue
}
gotBits := math.Float32bits(got)
wantBits := math.Float32bits(want)
if gotBits != wantBits {
t.Errorf("pixel %d: got bits 0x%08X (%v), want bits 0x%08X (%v)",
i, gotBits, got, wantBits, want)
}
}
}
// TestEncodeFloatRGBRoundtrip tests encoding and decoding a 3-component
// float32 image, verifying the RCT + NLT interaction.
func TestEncodeFloatRGBRoundtrip(t *testing.T) {
width, height := 8, 8
numPixels := width * height
img := &FloatImage{
Width: width,
Height: height,
Components: make([][]float32, 3),
BitDepth: 32,
Signed: true,
}
for c := 0; c < 3; c++ {
img.Components[c] = make([]float32, numPixels)
for i := 0; i < numPixels; i++ {
// Each component gets different values to stress the RCT
img.Components[c][i] = float32(c+1) * float32(i) * 0.01
}
}
opts := DefaultOptions()
opts.Format = FormatJ2K
opts.NumResolutions = 2
var buf bytes.Buffer
if err := EncodeFloat(&buf, img, opts); err != nil {
t.Fatalf("EncodeFloat: %v", err)
}
decoded, err := DecodeFloatConfig(bytes.NewReader(buf.Bytes()), nil)
if err != nil {
t.Fatalf("DecodeFloat: %v", err)
}
if len(decoded.Components) != 3 {
t.Fatalf("component count: got %d, want 3", len(decoded.Components))
}
for c := 0; c < 3; c++ {
for i := 0; i < numPixels; i++ {
gotBits := math.Float32bits(decoded.Components[c][i])
wantBits := math.Float32bits(img.Components[c][i])
if gotBits != wantBits {
t.Errorf("comp %d pixel %d: got bits 0x%08X (%v), want 0x%08X (%v)",
c, i, gotBits, decoded.Components[c][i],
wantBits, img.Components[c][i])
}
}
}
}
// TestEncodeFloatNLTMarkerPresent verifies that the encoded codestream
// contains NLT markers.
func TestEncodeFloatNLTMarkerPresent(t *testing.T) {
width, height := 4, 4
img := &FloatImage{
Width: width,
Height: height,
Components: [][]float32{make([]float32, width*height)},
BitDepth: 32,
Signed: true,
}
for i := range img.Components[0] {
img.Components[0][i] = float32(i) * 0.5
}
opts := DefaultOptions()
opts.Format = FormatJ2K
opts.NumResolutions = 2
var buf bytes.Buffer
if err := EncodeFloat(&buf, img, opts); err != nil {
t.Fatalf("EncodeFloat: %v", err)
}
// Parse the codestream header and check for NLT markers
parser := codestream.NewParser(bytes.NewReader(buf.Bytes()))
header, err := parser.ReadHeader()
if err != nil {
t.Fatalf("ReadHeader: %v", err)
}
if len(header.NLTMarkers) == 0 {
t.Fatal("expected NLT markers in codestream, found none")
}
nlt := header.NLTMarkers[0]
if nlt.ComponentIndex != 0 {
t.Errorf("NLT component index: got %d, want 0", nlt.ComponentIndex)
}
if nlt.BitDepth != 0x9F {
t.Errorf("NLT bit depth: got 0x%02X, want 0x9F", nlt.BitDepth)
}
if nlt.TransformType != 3 {
t.Errorf("NLT transform type: got %d, want 3", nlt.TransformType)
}
}
// TestNLTType3SelfInverse verifies the NLT transform is its own inverse.
func TestNLTType3SelfInverse(t *testing.T) {
testValues := []float32{
0, 1.0, -1.0,
math.SmallestNonzeroFloat32,
-math.SmallestNonzeroFloat32,
math.MaxFloat32,
-math.MaxFloat32,
float32(math.Inf(1)),
float32(math.Inf(-1)),
3.14159,
-2.71828,
}
for _, v := range testValues {
original := int32(math.Float32bits(v))
data := []int32{original}
// Apply twice (self-inverse)
nltType3(data)
nltType3(data)
if data[0] != original {
t.Errorf("NLT not self-inverse for %v (0x%08X): got 0x%08X",
v, uint32(original), uint32(data[0]))
}
}
}
// TestEncodeFloatNaN tests that NaN values survive the roundtrip.
// NaN has many bit patterns; we test a canonical one.
func TestEncodeFloatNaN(t *testing.T) {
width, height := 4, 4
img := &FloatImage{
Width: width,
Height: height,
Components: [][]float32{make([]float32, width*height)},
BitDepth: 32,
Signed: true,
}
// Fill with NaN
nan := float32(math.NaN())
for i := range img.Components[0] {
img.Components[0][i] = nan
}
opts := DefaultOptions()
opts.Format = FormatJ2K
opts.NumResolutions = 2
var buf bytes.Buffer
if err := EncodeFloat(&buf, img, opts); err != nil {
t.Fatalf("EncodeFloat: %v", err)
}
decoded, err := DecodeFloatConfig(bytes.NewReader(buf.Bytes()), nil)
if err != nil {
t.Fatalf("DecodeFloat: %v", err)
}
for i := 0; i < width*height; i++ {
if !math.IsNaN(float64(decoded.Components[0][i])) {
t.Errorf("pixel %d: expected NaN, got %v", i, decoded.Components[0][i])
}
}
}
// TestEncodeIntegerRegression ensures existing integer encode paths still work
// after the float encoder changes.
func TestEncodeIntegerRegression(t *testing.T) {
// Create a simple 8-bit grayscale image
width, height := 16, 16
img := makeGrayImage(width, height)
opts := DefaultOptions()
opts.Format = FormatJ2K
opts.Lossless = true
opts.NumResolutions = 3
var buf bytes.Buffer
if err := Encode(&buf, img, opts); err != nil {
t.Fatalf("Encode (integer): %v", err)
}
decoded, err := Decode(bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatalf("Decode (integer): %v", err)
}
bounds := decoded.Bounds()
if bounds.Dx() != width || bounds.Dy() != height {
t.Fatalf("dimensions: got %dx%d, want %dx%d",
bounds.Dx(), bounds.Dy(), width, height)
}
}