pub const fn error_code_description(code: i32) -> &'static strExpand description
Returns a human-readable description of a PhotoDNA error code.
This is a compile-time lookup that doesn’t require a library instance.
Examples found in repository?
examples/hash.rs (line 81)
12fn main() {
13 println!("PhotoDNA Hash Computation Example");
14 println!("==================================");
15 println!();
16
17 #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
18 {
19 // Load the library
20 let lib = match EdgeHashGenerator::new(None, 4) {
21 Ok(lib) => {
22 println!(
23 "✓ Library loaded: version {}",
24 lib.library_version_text().unwrap_or("unknown")
25 );
26 lib
27 }
28 Err(e) => {
29 eprintln!("✗ Failed to load library: {}", e);
30 std::process::exit(1);
31 }
32 };
33
34 // Create a simple test image (gradient pattern)
35 // Images must be at least 50x50 pixels
36 let width = 100;
37 let height = 100;
38 let bytes_per_pixel = 3; // RGB format
39 let stride = width * bytes_per_pixel;
40
41 // Generate a simple gradient image
42 let mut image_data = vec![0u8; (height * stride) as usize];
43 for y in 0..height {
44 for x in 0..width {
45 let offset = ((y * stride) + (x * bytes_per_pixel)) as usize;
46 // Create a diagonal gradient pattern
47 let r = ((x * 255) / width) as u8;
48 let g = ((y * 255) / height) as u8;
49 let b = (((x + y) * 255) / (width + height)) as u8;
50 image_data[offset] = r;
51 image_data[offset + 1] = g;
52 image_data[offset + 2] = b;
53 }
54 }
55
56 println!(
57 "Created test image: {}x{} RGB ({} bytes)",
58 width,
59 height,
60 image_data.len()
61 );
62 println!();
63
64 // Compute the hash
65 let mut hash = [0u8; PHOTODNA_HASH_SIZE_MAX];
66
67 let result = unsafe {
68 lib.photo_dna_edge_hash(
69 image_data.as_ptr(),
70 hash.as_mut_ptr(),
71 width,
72 height,
73 stride,
74 PhotoDna_Default | PhotoDna_Rgb,
75 )
76 };
77
78 if result < 0 {
79 eprintln!(
80 "✗ Hash computation failed: {} (code {})",
81 error_code_description(result),
82 result
83 );
84 if let Some(lib_msg) = lib.get_error_string(result) {
85 eprintln!(" Library message: {}", lib_msg);
86 }
87 std::process::exit(1);
88 }
89
90 println!("✓ Hash computed successfully!");
91 println!();
92
93 // Display first 32 bytes of hash in hex
94 let hash_preview: String = hash[..32]
95 .iter()
96 .map(|b| format!("{:02x}", b))
97 .collect::<Vec<_>>()
98 .join(" ");
99
100 println!("Hash (first 32 bytes): {}", hash_preview);
101 println!("Hash size: {} bytes", PHOTODNA_HASH_SIZE_EDGE_V2);
102
103 // Also demonstrate border detection
104 println!();
105 println!("Testing border detection...");
106
107 let mut hash_results = [HashResult::default(); 2];
108
109 let border_result = unsafe {
110 lib.photo_dna_edge_hash_border(
111 image_data.as_ptr(),
112 hash_results.as_mut_ptr(),
113 2,
114 width,
115 height,
116 stride,
117 PhotoDna_Default | PhotoDna_Rgb,
118 )
119 };
120
121 if border_result < 0 {
122 println!(
123 "Border detection: {} (code {})",
124 error_code_description(border_result),
125 border_result
126 );
127 } else {
128 println!("Border detection returned {} hash(es)", border_result);
129 for i in 0..border_result as usize {
130 let result = hash_results[i].result;
131 let x = hash_results[i].header_dimensions_image_x;
132 let y = hash_results[i].header_dimensions_image_y;
133 let w = hash_results[i].header_dimensions_image_w;
134 let h = hash_results[i].header_dimensions_image_h;
135 println!(
136 " Hash {}: result={}, region=({},{},{},{})",
137 i, result, x, y, w, h
138 );
139 }
140 }
141 }
142
143 #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
144 {
145 println!("Native library not available on this platform.");
146 println!("Use the 'wasm' feature with a WASM runtime for BSD platforms.");
147 }
148}