Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use dicom-rs with wasm bindgen #39

Closed
vampolo opened this issue May 18, 2020 · 3 comments
Closed

use dicom-rs with wasm bindgen #39

vampolo opened this issue May 18, 2020 · 3 comments

Comments

@vampolo
Copy link

vampolo commented May 18, 2020

Hey,

I'm trying to use dicom-rs with wasm bindgen for a subassembly app. So far I have something like this function to load a dicom file using JS fetch

pub async fn load_url(url: String) -> Result<(), JsValue> {
    let mut opts = RequestInit::new();
    opts.method("GET");
    opts.mode(RequestMode::Cors);

    let request = Request::new_with_str_and_init(&url, &opts)?;

    let window = web_sys::window().unwrap();
    let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;

    // `resp_value` is a `Response` object.
    assert!(resp_value.is_instance_of::<Response>());
    let resp: Response = resp_value.dyn_into().unwrap();

    // Convert this other `Promise` into a rust `Future`.
    let array_buffer = JsFuture::from(resp.array_buffer()?).await?;

    let typebuf: js_sys::Uint8Array = js_sys::Uint8Array::new(&array_buffer);

    let mut slice = vec![0; typebuf.length() as usize];
    typebuf.copy_to(&mut slice[..]);

    let mut file = Cursor::new(slice);

    let obj = from_reader(&mut file).unwrap();

    // let patient_name = obj.element_by_name("PatientName").unwrap().to_str().unwrap();

    // console::log_1(&patient_name.to_string().into());

    Ok(())
}

Problem is the line let obj = from_reader(&mut file).unwrap() fails with :

image

I'm using the latest master. Not sure if the function I'm calling on the dicom-rs side is the right one. The import is the following: use dicom_object::from_reader;

@Enet4
Copy link
Owner

Enet4 commented May 18, 2020

Thank you for the example! from_reader does not expect a preamble, which is probably what made the operation fail. Can you try and skip the first 128 bytes before calling from_reader?

I agree that the ergonomics of this API can be improved here. At some point the options builder pattern might be more interesting here.

@vampolo
Copy link
Author

vampolo commented May 18, 2020

Thank you @Enet4

Yeah dropping the first 128 bytes did it

    let mut file = Cursor::new(&slice[128..]);

    let obj = from_reader(&mut file).unwrap();

    let patient_name = obj.element_by_name("PatientName").unwrap().to_str().unwrap();

    console::log_1(&patient_name.to_string().into());

@Enet4
Copy link
Owner

Enet4 commented May 24, 2020

Closing, as the main concern appears to have been resolved, and the current behavior of the API is by design. Improvements to the API can be suggested in #43.

@Enet4 Enet4 closed this as completed May 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants