Skip to content

Commit

Permalink
yay, it's working!
Browse files Browse the repository at this point in the history
  • Loading branch information
InventivetalentDev committed Aug 30, 2024
1 parent d9fe058 commit 9f4ce3c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
94 changes: 47 additions & 47 deletions __test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ test('sum from native', (t) => {

test('encode empty classic', t => {
let buffer = fs.readFileSync('images/a67c89456ea2fcd58cdf557c01357632d4f0d28c51dd0d8f79a4c739352f71a7.png');
console.info('buffer.length',buffer.length);
console.info('buffer.byteLength', buffer.byteLength)
console.info('buffer',buffer);
buffer = Buffer.from(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength))
console.info('buffer.length',buffer.length);
console.info('buffer.byteLength', buffer.byteLength)
console.info('buffer',buffer);


//let arr = new Uint8Array(buffer);
let encoded = encodeImage(buffer, true);
Expand All @@ -34,42 +26,50 @@ test('encode empty classic', t => {
console.log(encoded.hex)
})

// test('encode random 1 classic', t => {
// let buffer = fs.readFileSync('images/6a12c0010c6e708422f5ba5121ba4a8adc9c7374c2b96fd0754c84f25e181598.png');
// buffer = Buffer.from(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength))
// console.log(buffer.length);
// console.log(buffer.byteLength);
// //let arr = new Uint8Array(buffer);
// let encoded = encodeImage(buffer, true);
//
// let hash = Buffer.from(encoded.hash).toString('hex');
// console.log(hash);
//
// let mchash = Buffer.from(encoded.minecraftHash).toString('hex');
// console.log(mchash);
//
// t.is(mchash,'6a12c0010c6e708422f5ba5121ba4a8adc9c7374c2b96fd0754c84f25e181598')
//
// console.log(encoded.hex)
// })
//
// test('encode random 1 slim', t => {
// let buffer = fs.readFileSync('images/6a12c0010c6e708422f5ba5121ba4a8adc9c7374c2b96fd0754c84f25e181598.png');
// buffer = Buffer.from(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength))
// console.log(buffer.length);
// console.log(buffer.byteLength);
// //let arr = new Uint8Array(buffer);
// let encoded = encodeImage(buffer, false);
//
// console.log(Buffer.from('6a12c0010c6e708422f5ba5121ba4a8adc9c7374c2b96fd0754c84f25e181598', 'hex'))
// console.log(Buffer.from(encoded.minecraftHash))
// console.log(encoded.minecraftHash)
//
// let hash = encoded.hash.toString('hex');
// console.log(hash);
//
// let mchash = encoded.minecraftHash.toString('hex');
// console.log(mchash);
//
// console.log(encoded.hex)
// })
test('encode random 1 classic', t => {
let buffer = fs.readFileSync('images/6a12c0010c6e708422f5ba5121ba4a8adc9c7374c2b96fd0754c84f25e181598.png');
buffer = Buffer.from(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength))
//let arr = new Uint8Array(buffer);
let encoded = encodeImage(buffer, true);

let hash = Buffer.from(encoded.hash).toString('hex');
console.log(hash);

let mchash = Buffer.from(encoded.minecraftHash).toString('hex');
console.log(mchash);

t.is(mchash,'6a12c0010c6e708422f5ba5121ba4a8adc9c7374c2b96fd0754c84f25e181598')

console.log(encoded.hex)
})


test('encode random 2 classic', t => {
let buffer = fs.readFileSync('images/randomImage1.png');
let encoded = encodeImage(buffer, true);

let hash = encoded.hash.toString('hex');
console.log(hash);

let mchash = encoded.minecraftHash.toString('hex');
console.log(mchash);

t.is(mchash, '512e1f13b41b6214c7e2597c3a69a06f56564eed5e5d42b3c8f519c079c47806');

console.log(encoded.hex)
})

test('encode random 3 classic', t => {
let buffer = fs.readFileSync('images/randomImage2.png');
let encoded = encodeImage(buffer);

let hash = encoded.hash.toString('hex');
console.log(hash);

let mchash = encoded.minecraftHash.toString('hex');
console.log(mchash);

t.is(mchash, '57a6d6ddebafe045e9a0501c7c489f6de47e670fc4ef1b0c1b2c2a9a2c8c0cbc');

console.log(encoded.hex)
})
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* auto-generated by NAPI-RS */

export declare function sum(a: number, b: number): number
export declare function encodeImage(buffer: Uint8Array, isClassic: boolean): ImageWithHashes
export declare function encodeImage(buffer: Uint8Array): ImageWithHashes
export declare class ImageWithHashes {
png: Buffer
minecraftHash: Buffer
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub struct ImageWithHashes {
}

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

fn copy_slice(dst: &mut [u8], src: &[u8]) -> usize {
Expand All @@ -49,7 +49,7 @@ fn copy_slice(dst: &mut [u8], src: &[u8]) -> usize {

// based on https://github.com/GeyserMC/global_api/blob/dev/1.0.2/native/skins/src/skin_convert/skin_codec.rs#L100
//#[napi]
pub fn encode_custom_image(buffer: &[u8], is_classic: bool, width: usize, height: usize) -> ImageWithHashes {
pub fn encode_custom_image(buffer: &[u8], width: usize, height: usize) -> ImageWithHashes {
println!("Buffer length: {}", buffer.len());

let mut decoder = lodepng::Decoder::new();
Expand All @@ -61,8 +61,9 @@ pub fn encode_custom_image(buffer: &[u8], is_classic: bool, width: usize, height

let mut raw_data = vec![0; SKIN_DATA_LENGTH];
println!("Raw length: {}", raw_data.len());
copy_slice(&mut raw_data, &decoded_data);
clear_unused_pixels(&mut raw_data, is_classic);
//copy_slice(&mut raw_data, &decoded_data);
decoded_data.clone_into(&mut raw_data);
//clear_unused_pixels(&mut raw_data, is_classic);



Expand Down

0 comments on commit 9f4ce3c

Please sign in to comment.