Skip to content

Commit

Permalink
that works
Browse files Browse the repository at this point in the history
  • Loading branch information
InventivetalentDev committed Aug 17, 2024
1 parent a068a3c commit ef35b0c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ version = "0.0.0"
crate-type = ["cdylib"]

[dependencies]
lodepng = "3.10.4"
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi-derive = "2.12.2"
sha2 = "0.10.8"

[build-dependencies]
napi-build = "2.0.1"
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
/* auto-generated by NAPI-RS */

export declare function sum(a: number, b: number): number
export declare function encodeImage(rawData: Uint8Array): ImageWithHashes
export declare class ImageWithHashes {
png: Uint8Array
minecraftHash: Uint8Array
hash: Uint8Array
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { sum } = nativeBinding
const { sum, ImageWithHashes, encodeImage } = nativeBinding

module.exports.sum = sum
module.exports.ImageWithHashes = ImageWithHashes
module.exports.encodeImage = encodeImage
53 changes: 53 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
#![deny(clippy::all)]

use lodepng::FilterStrategy;
use sha2::{Digest, Sha256};
use napi::{
bindgen_prelude::{Buffer, ClassInstance, ObjectFinalize, This, Uint8Array, Unknown},
Env, Property, Result,
};
use napi_derive::napi;

#[macro_use]
extern crate napi_derive;

pub const SKIN_WIDTH: u8 = 64;
pub const SKIN_HEIGHT: u8 = 64;
pub const SKIN_CHANNELS: u8 = 4;

#[napi]
pub fn sum(a: i32, b: i32) -> i32 {
a + b
}

#[napi]
pub struct ImageWithHashes {
pub png: Uint8Array,
pub minecraft_hash: Uint8Array,
pub hash: Uint8Array,
}

#[napi]
pub fn encode_image(raw_data: &[u8]) -> ImageWithHashes {
encode_custom_image(raw_data, SKIN_WIDTH, SKIN_HEIGHT)
}

//#[napi]
pub fn encode_custom_image(raw_data: &[u8], width: u8, height: u8) -> ImageWithHashes {
// encode images like Minecraft does
let mut encoder = lodepng::Encoder::new();
encoder.set_auto_convert(false);
encoder.info_png_mut().interlace_method = 0; // should be 0 but just to be sure

let mut encoder_settings = encoder.settings_mut();
encoder_settings.zlibsettings.set_level(4);
encoder_settings.filter_strategy = FilterStrategy::ZERO;

let png = encoder.encode(raw_data, width as usize, height as usize).unwrap();

let mut hasher = Sha256::new();

hasher.update(&png);
let minecraft_hash = hasher.finalize_reset();

// make our own hash
hasher.update(raw_data);
let hash = hasher.finalize();

ImageWithHashes {
png: Uint8Array::from(png.as_slice()),
minecraft_hash: Uint8Array::from(minecraft_hash.as_slice()),
hash: Uint8Array::from(hash.as_slice()),
}
}

0 comments on commit ef35b0c

Please sign in to comment.