1use crate::BasicPdfWriter;
2use crate::page::Page;
3use format_bytes::write_bytes as wb;
4
5pub struct Image<'a> {
7 pub obj: usize,
9 pub data: &'a [u8],
11 pub width: usize,
13 pub height: usize,
15 pub bits_per_component: u8,
17 pub color_space: &'a [u8],
19 pub other: &'a [u8],
21}
22
23impl<'a> Image<'a>
24{
25 pub fn init(&mut self, w: &mut BasicPdfWriter) {
27 self.obj = w.begin();
28 let _ = wb!(
29 &mut w.b,
30 b"<</Type/XObject/Subtype/Image/Width {}/Height {}/ColorSpace{}/BitsPerComponent {}/Length {}{}>>stream\n",
31 self.width, self.height, self.color_space, self.bits_per_component, self.data.len(), self.other
32 );
33 w.b.extend_from_slice(&self.data);
34 w.b.extend_from_slice(b"\nendstream");
35 w.end();
36 }
37
38 pub fn draw(&self, page: &mut Page, x: f32, y: f32, scale:f32)
40 {
41 let obj = self.obj;
42 let w = (self.width as f32) * scale;
43 let h = (self.height as f32) * scale;
44 page.xobjs.insert(obj);
45 let _ = wb!(&mut page.os, b"\nq {} 0 0 {} {} {} cm /X{} Do Q", w,h,x,y,obj);
46 }
47}