Skip to content

Instantly share code, notes, and snippets.

@Pruxis
Created August 5, 2025 19:18
Show Gist options
  • Select an option

  • Save Pruxis/c76b2f4f5f4b2b113c413e1647360d4f to your computer and use it in GitHub Desktop.

Select an option

Save Pruxis/c76b2f4f5f4b2b113c413e1647360d4f to your computer and use it in GitHub Desktop.
#![feature(unsized_locals)]
use memmap::MmapOptions;
use neon::prelude::*;
use std::cmp;
use std::f32;
use std::fs::File;
fn parse_image(mut cx: FunctionContext) -> JsResult<JsNull> {
let file_path_arg: Handle<JsString> = cx.argument::<JsString>(0).unwrap();
let file_path = file_path_arg.value(&mut cx);
let file = File::open(file_path).unwrap();
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
let dimension = 2048 * 2048;
let redBand = 32;
let greenBand = 16;
let blueBand = 0;
let (_, red, __) =
unsafe { mmap[dimension * redBand * 4..dimension * (redBand + 1) * 4].align_to::<f32>() };
let (_, green, __) = unsafe {
mmap[dimension * greenBand * 4..dimension * (greenBand + 1) * 4].align_to::<f32>()
};
let (_, blue, __) =
unsafe { mmap[dimension * blueBand * 4..dimension * (blueBand + 1) * 4].align_to::<f32>() };
let mut red_result: Vec<u8> = Vec::with_capacity(dimension * 3);
let mut count: usize = 0;
let maxCount = dimension;
loop {
if count == maxCount {
break;
}
let r = (&red[count] * 255.0) as u8;
let g = (&green[count] * 255.0) as u8;
let b = (&blue[count] * 255.0) as u8;
red_result.push(r.to_owned());
red_result.push(g.to_owned());
red_result.push(b.to_owned());
count += 1;
}
image::save_buffer_with_format(
"output.jpeg",
&red_result,
2048,
2048,
image::ColorType::Rgb8,
image::ImageFormat::Jpeg,
)
.unwrap();
Ok(cx.null())
}
#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("parseImage", parse_image)?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment