Skip to content

A fast, helpful, and open-source document parser

Merge pull request #283 from jmho-govsignals/perf/collapse-bgra-rgb-ocr-render

Logancbf8779597b7
diff --cc crates/pdfium/src/bitmap.rs
index 0218c4e,6614fdc..2d4833f
--- a/crates/pdfium/src/bitmap.rs
+++ b/crates/pdfium/src/bitmap.rs
@@@ -115,9 -92,31 +115,31 @@@ impl<'lib> Bitmap<'lib>
rgba
}
+
+ /// Convert the BGRA buffer to tightly-packed RGB in a new Vec, dropping the
+ /// alpha channel (pages render onto opaque white, so alpha is constant 255).
+ pub fn to_rgb(&self) -> Vec<u8> {
+ let width = self.width() as usize;
+ let height = self.height() as usize;
+ let stride = self.stride() as usize;
+ let src = self.buffer();
+ let mut rgb = Vec::with_capacity(width * height * 3);
+
+ for y in 0..height {
+ let row = &src[y * stride..y * stride + width * 4];
+ for pixel in row.chunks_exact(4) {
+ // BGRA -> RGB (drop A)
+ rgb.push(pixel[2]); // R
+ rgb.push(pixel[1]); // G
+ rgb.push(pixel[0]); // B
+ }
+ }
+
+ rgb
+ }
}
-impl Drop for Bitmap {
+impl Drop for Bitmap<'_> {
fn drop(&mut self) {
unsafe { ffi!(FPDFBitmap_Destroy(self.handle)) };
}