1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//!# Test example
//!
//! ```
//! use pdf_min::{Writer, html, writer::Fetcher, image::{ImageSpec,Image}};
//! struct MyFetcher;
//! impl Fetcher for MyFetcher {
//! fn image(&mut self, w: &mut Writer, _name: &str) -> Image {
//! let mut data = Vec::new();
//! for i in 0..3 * 16 * 16 {
//! data.push( i as u8 ); // Red
//! data.push( (i + 85 ) as u8 ); // Green
//! data.push( (i + 170 ) as u8 ); // Blue
//! }
//! let ims = ImageSpec{ data: &data, width:16, height:16,
//! bits_per_component:8, color_space: b"/DeviceRGB", other: b"" };
//! Image::new( &ims, &mut w.b )
//! }
//! }
//! let mut w = Writer::default();
//! w.b.nocomp = true;
//! w.font_size = 20;
//! w.fetcher = Some(Box::new(MyFetcher));
//!
//! // Draw text with image
//! html( &mut w, b"<p><b>Bold Text Before Image</b> <img width=32 src=myimg> Text after image" );
//! let bytes = w.finish();
//!
//! use std::fs::File;
//! use std::io::prelude::*;
//!
//! let mut file = File::create("image_test.pdf").unwrap();
//! file.write_all(bytes).unwrap();
//! ```
//!
//!# JPG Test example
//! ```
//! // Setup the PDF Writer
//! let mut doc = pdf_min::Writer::default();
//! doc.b.nocomp = true;
//!
//! // Read jpg from file
//! let file_bytes = std::fs::read("one.jpg").unwrap();
//!
//! // Use jpeg_decoder::Decoder to get jpg info ( color space, bits_per_component, width, height ).
//! let mut decoder = jpeg_decoder::Decoder::new(std::io::Cursor::new(&file_bytes));
//! decoder.read_info().unwrap();
//! let info = decoder.info().unwrap();
//!
//! use jpeg_decoder::{PixelFormat};
//!
//! let color_space: &[u8] = match info.pixel_format {
//! PixelFormat::RGB24 => b"/DeviceRGB",
//! PixelFormat::CMYK32 => b"/DeviceCMYK",
//! PixelFormat::L8 | PixelFormat::L16 => b"/DeviceGray",
//! };
//!
//! let bits_per_component = match info.pixel_format {
//! PixelFormat::L16 => 16,
//! _ => 8
//! };
//!
//! // Make the ImageSpec.
//! use pdf_min::{Px, image::{ImageSpec, Image}};
//! let ims = ImageSpec {
//! data: &file_bytes,
//! width: info.width as Px,
//! height: info.height as Px,
//! color_space,
//! bits_per_component,
//! other: b"/Filter/DCT",
//! };
//!
//! // Make the PDF Image from the ImageSpec.
//! let im = Image::new(&ims, &mut doc.b);
//!
//! // Draw the image on the current page.
//! im.draw(&mut doc.p, 20.0, 40.0, 0.20);
//!
//! // Save the pdf as a file.
//! let bytes = doc.finish();
//! let mut file = std::fs::File::create("jpg_image_test.pdf").unwrap();
//! use std::io::Write;
//! file.write_all(bytes).unwrap();
//! ```
use crateBasicPdfWriter;
use cratePage;
use crate::*;
use write_bytes as wb;
/// PDF image specification - byte data and attributes that describe how image is encoded.
/// PDF image - obj id, width and height