calcDct2Hash calculates the DCT hash for the given image data buffer
(buf []byte)
| 177 | |
| 178 | // calcDct2Hash calculates the DCT hash for the given image data buffer |
| 179 | func (h *ImageHash) calcDct2Hash(buf []byte) error { |
| 180 | bufPtr := unsafe.Pointer(unsafe.SliceData(buf)) |
| 181 | |
| 182 | var dctArray *C.float |
| 183 | var length C.size_t |
| 184 | |
| 185 | defer func() { |
| 186 | if dctArray != nil { |
| 187 | C.free(unsafe.Pointer(dctArray)) |
| 188 | } |
| 189 | }() |
| 190 | |
| 191 | //nolint:gocritic |
| 192 | result := C.vips_dct2_hash(bufPtr, C.size_t(len(buf)), &dctArray, &length) |
| 193 | |
| 194 | if result != 0 { |
| 195 | return fmt.Errorf("failed to calculate DCT hash: %s", vipsErrorMessage()) |
| 196 | } |
| 197 | |
| 198 | dctHash := make([]float32, int(length)) |
| 199 | |
| 200 | // Convert C array to Go slice safely |
| 201 | cSlice := unsafe.Slice(dctArray, int(length)) |
| 202 | for i, v := range cSlice { |
| 203 | dctHash[i] = float32(v) |
| 204 | } |
| 205 | |
| 206 | h.dctHash = dctHash |
| 207 | |
| 208 | return nil |
| 209 | } |
| 210 | |
| 211 | // dumpDctHash writes the DCT hash to a writer |
| 212 | func (h *ImageHash) dumpDctHash(w io.Writer) error { |
no test coverage detected