@@ -4,17 +4,58 @@ use crate::marker::{Marker, SOFType};
44use crate :: quantization:: QuantizationTable ;
55use crate :: EncodingError ;
66
7- /// Density settings
8- #[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
9- pub enum Density {
10- /// No pixel density is set, which means "1 pixel per pixel"
11- None ,
7+ /// Represents the pixel density of an image
8+ ///
9+ /// For example, a 300 DPI image is represented by:
10+ ///
11+ /// ```rust
12+ /// # use jpeg_encoder::{PixelDensity, PixelDensityUnit};
13+ /// let hdpi = PixelDensity::dpi(300);
14+ /// assert_eq!(hdpi, PixelDensity {density: (300,300), unit: PixelDensityUnit::Inches})
15+ /// ```
16+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
17+ pub struct PixelDensity {
18+ /// A couple of values for (Xdensity, Ydensity)
19+ pub density : ( u16 , u16 ) ,
20+ /// The unit in which the density is measured
21+ pub unit : PixelDensityUnit ,
22+ }
23+
24+ impl PixelDensity {
25+ /// Creates the most common pixel density type:
26+ /// the horizontal and the vertical density are equal,
27+ /// and measured in pixels per inch.
28+ #[ must_use]
29+ pub fn dpi ( density : u16 ) -> Self {
30+ PixelDensity {
31+ density : ( density, density) ,
32+ unit : PixelDensityUnit :: Inches ,
33+ }
34+ }
35+ }
36+
37+ impl Default for PixelDensity {
38+ /// Returns a pixel density with a pixel aspect ratio of 1
39+ fn default ( ) -> Self {
40+ PixelDensity {
41+ density : ( 1 , 1 ) ,
42+ unit : PixelDensityUnit :: PixelAspectRatio ,
43+ }
44+ }
45+ }
46+
47+ /// Represents a unit in which the density of an image is measured
48+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
49+ pub enum PixelDensityUnit {
50+ /// Represents the absence of a unit, the values indicate only a
51+ /// [pixel aspect ratio](https://en.wikipedia.org/wiki/Pixel_aspect_ratio)
52+ PixelAspectRatio ,
1253
13- /// Horizontal and vertical dots per inch (dpi )
14- Inch { x : u16 , y : u16 } ,
54+ /// Pixels per inch (2.54 cm )
55+ Inches ,
1556
16- /// Horizontal and vertical dots per centimeters
17- Centimeter { x : u16 , y : u16 } ,
57+ /// Pixels per centimeter
58+ Centimeters ,
1859}
1960
2061/// Zig-zag sequence of quantized DCT coefficients
@@ -172,30 +213,27 @@ impl<W: JfifWrite> JfifWriter<W> {
172213 Ok ( ( ) )
173214 }
174215
175- pub fn write_header ( & mut self , density : & Density ) -> Result < ( ) , EncodingError > {
216+ pub fn write_header ( & mut self , density : & PixelDensity ) -> Result < ( ) , EncodingError > {
176217 self . write_marker ( Marker :: APP ( 0 ) ) ?;
177218 self . write_u16 ( 16 ) ?;
178219
179220 self . write ( b"JFIF\0 " ) ?;
180221 self . write ( & [ 0x01 , 0x02 ] ) ?;
181222
182- match * density {
183- Density :: None => {
223+ match density. unit {
224+ PixelDensityUnit :: PixelAspectRatio => {
184225 self . write_u8 ( 0x00 ) ?;
185- self . write_u16 ( 1 ) ?;
186- self . write_u16 ( 1 ) ?;
187226 }
188- Density :: Inch { x , y } => {
227+ PixelDensityUnit :: Inches => {
189228 self . write_u8 ( 0x01 ) ?;
190- self . write_u16 ( x) ?;
191- self . write_u16 ( y) ?;
192229 }
193- Density :: Centimeter { x , y } => {
230+ PixelDensityUnit :: Centimeters => {
194231 self . write_u8 ( 0x02 ) ?;
195- self . write_u16 ( x) ?;
196- self . write_u16 ( y) ?;
197232 }
198233 }
234+ let ( x, y) = density. density ;
235+ self . write_u16 ( x) ?;
236+ self . write_u16 ( y) ?;
199237
200238 self . write ( & [ 0x00 , 0x00 ] )
201239 }
0 commit comments