MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / TestDCT2HashCalc

Method TestDCT2HashCalc

testutil/image_hash_test.go:21–79  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19}
20
21func (s *ImageHashTestSuite) TestDCT2HashCalc() {
22 testCases := []struct {
23 filename string
24 expectedHashLength int
25 }{
26 // PNG files
27 {"test-images/png/1-bpp.png", 64},
28 {"test-images/png/16-bpp-grayscale.png", 64},
29 {"test-images/png/16-bpp-linear.png", 192},
30 {"test-images/png/16-bpp.png", 192},
31 {"test-images/png/8-bpp-grayscale.png", 64},
32 {"test-images/png/8-bpp.png", 192},
33 {"test-images/png/png.png", 192},
34
35 // HEIF files
36 {"test-images/heif/8-bpp.heif", 192},
37 {"test-images/heif/heif.heif", 192},
38
39 // GIF files
40 {"test-images/gif/gif.gif", 192},
41
42 // TIFF files
43 {"test-images/tiff/16-bpp-grayscale.tiff", 64},
44 {"test-images/tiff/16-bpp.tiff", 192},
45 {"test-images/tiff/32-bpp-linear.tiff", 192},
46 {"test-images/tiff/4-bpp-grayscale.tiff", 64},
47 {"test-images/tiff/8-bpp-grayscale.tiff", 64},
48 {"test-images/tiff/8-bpp.tiff", 192},
49 {"test-images/tiff/tiff.tiff", 192},
50
51 // SVG files
52 {"test-images/svg/svg.svg", 192},
53 }
54
55 for _, tc := range testCases {
56 s.Run(tc.filename, func() {
57 // Calculate DCT hash
58 hash := s.calcHash(tc.filename, testutil.HashTypeDct)
59
60 // Check that dumped hash length matches expected length
61 // (multiply by 4 because DCT hash is stored as float32, which is 4 bytes)
62 s.Require().Equal(tc.expectedHashLength*4, hash.Len(), "unexpected hash length")
63
64 // Dump hash to buffer and load it back to check consistency
65 var buf bytes.Buffer
66 err := hash.Dump(&buf)
67 s.Require().NoError(err)
68
69 loadedHash, err := testutil.LoadImageHash(&buf)
70 s.Require().NoError(err)
71 s.Require().NotNil(loadedHash)
72
73 // Check that loaded hash matches original hash
74 distance, err := hash.Distance(loadedHash)
75 s.Require().NoError(err)
76 s.Require().InDelta(0, distance, 1e-10, "loaded hash does not match original hash")
77 })
78 }

Callers

nothing calls this directly

Calls 6

calcHashMethod · 0.95
LoadImageHashFunction · 0.92
RunMethod · 0.80
DistanceMethod · 0.80
LenMethod · 0.45
DumpMethod · 0.45

Tested by

no test coverage detected