Libsdl2 saving bmp from Canvas<Window>

let pixel_format = canvas.default_pixel_format();
let mut pixels = canvas.read_pixels(canvas.viewport(), pixel_format).unwrap();
let (width, height) = canvas.output_size().unwrap();
let pitch = pixel_format.byte_size_of_pixels(width as usize);
let surf = sdl2::surface::Surface::from_data(pixels.as_mut_slice(), width, height, pitch as u32, pixel_format).unwrap();

Using the computed pitch results in a crash with Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Other values for pitch doesn't crash but produces an incorrect bmp.
From what I understand the pitch is 4*width.
Thanks for any help.