| 19 | } |
| 20 | |
| 21 | func (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 | } |