galactixx/imagehash
Imagehash is a Zig package for generating robust image fingerprints using four popular algorithms: average (ahash), difference (dhash), perceptual (ph...
stb_image
and resizing via stb_image_resize
.toJSON
, fromJSON
).zig fetch
You can use the builtβin Zig fetcher to download and pin a tarball:
bash
zig fetch --save=imagehash \
https://github.com/galactixx/imagehash/archive/v0.1.0.tar.gz
This adds anThen in your build.zig:imagehash
entry under.dependencies
in yourbuild.zig.zon
.
zig
const pkg = b.dependency("imagehash", .{});
const ih = pkg.module("imagehash");
lib.addPackagePath("imagehash", ih.src_path);
2. Manual
bash
git clone https://github.com/galactixx/imagehash.git
In your build.zig
:
zig
lib.addPackagePath("imagehash", "../imagehash/src");
Both approaches let you const ih = @import("imagehash");
in your Zig code. Pick whichever workflow suits you.
π Usage
```zig
const std = @import("std");
const imagehash = @import("imagehash");
pub fn main() !void {
const file = "testdata/checkerboard.png";
// Compute all four hashes
const ahash = try imagehash.averageHash(file);
const dhash = try imagehash.differenceHash(file);
const phash = try imagehash.perceptualHash(file);
const whash = try imagehash.waveletHash(file);
// Print hex digests
var buf: [16]u8 = undefined;
std.debug.print("ahash: {s}\n", .{ahash.hexDigest(buf[0..])});
std.debug.print("phash: {s}\n", .{phash.hexDigest(buf[0..])});
// Compare two hashes
const dist = ahash.distance(dhash);
std.debug.print("Hamming distance: {}\n", .{dist});
}
```
π API
pub fn averageHash(filename: []const u8) Error!ImageHash
Computes an 8Γ8 average-based hash (ahash
).
pub fn differenceHash(filename: []const u8) Error!ImageHash
Computes a 9Γ8 horizontal difference-based hash (dhash
).
s
pub fn perceptualHash(filename: []const u8) Error!ImageHash
Computes a 32Γ32 perceptual (DCT-based) hash and reduces to an 8Γ8 block (phash
).
pub fn waveletHash(filename: []const u8) Error!ImageHash
Computes a 64Γ64 wavelet-based hash with 4-level decomposition (whash
).
pub fn (self: ImageHash) distance(other: ImageHash) u64
Returns the Hamming distance between two hashes.
pub fn (self: ImageHash) toJSON(alloc: *std.mem.Allocator) ![]u8
Serialize an ImageHash
to a JSON byte slice.
pub fn fromJSON(json: []const u8, alloc: std.mem.Allocator) ParseError!ImageHash
Parse an ImageHash
from JSON.
π€ License
This project is licensed under the MIT License. See the LICENSE file for details.
π Contact
Have questions or need help? Open an issue on the GitHub repository.