Skip to content

parse JFIF APP0 sections and expose Density with DensityUnits #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use byteorder::ReadBytesExt;
use error::{Error, Result, UnsupportedFeature};
use huffman::{fill_default_mjpeg_tables, HuffmanDecoder, HuffmanTable};
use marker::Marker;
use parser::{AdobeColorTransform, AppData, CodingProcess, Component, Dimensions, EntropyCoding, FrameInfo,
use parser::{AdobeColorTransform, AppData, CodingProcess, Component, Dimensions, JfifApp0, Density, EntropyCoding, FrameInfo,
parse_app, parse_com, parse_dht, parse_dqt, parse_dri, parse_sof, parse_sos, ScanInfo};
use upsampler::Upsampler;
use std::cmp;
Expand Down Expand Up @@ -45,6 +45,8 @@ pub struct ImageInfo {
pub height: u16,
/// The pixel format of the image.
pub pixel_format: PixelFormat,
/// The density of the image, if available.
pub density: Option<Density>,
}

/// JPEG decoder
Expand All @@ -58,7 +60,7 @@ pub struct Decoder<R> {

restart_interval: u16,
color_transform: Option<AdobeColorTransform>,
is_jfif: bool,
jfif_app0: Option<JfifApp0>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are color_transform and jfif_app0 mutually exclusive? If so, perhaps this could hold an AppData or Option instead?

is_mjpeg: bool,

// Used for progressive JPEGs.
Expand All @@ -78,7 +80,7 @@ impl<R: Read> Decoder<R> {
quantization_tables: [None, None, None, None],
restart_interval: 0,
color_transform: None,
is_jfif: false,
jfif_app0: None,
is_mjpeg: false,
coefficients: Vec::new(),
coefficients_finished: [0; MAX_COMPONENTS],
Expand All @@ -99,10 +101,13 @@ impl<R: Read> Decoder<R> {
_ => panic!(),
};

let density = self.jfif_app0.as_ref().map(|j| j.density.clone());

Some(ImageInfo {
width: frame.image_size.width,
height: frame.image_size.height,
pixel_format: pixel_format,
pixel_format,
density,
})
},
None => None,
Expand Down Expand Up @@ -274,7 +279,7 @@ impl<R: Read> Decoder<R> {
if let Some(data) = parse_app(&mut self.reader, marker)? {
match data {
AppData::Adobe(color_transform) => self.color_transform = Some(color_transform),
AppData::Jfif => {
AppData::Jfif(jfif) => {
// From the JFIF spec:
// "The APP0 marker is used to identify a JPEG FIF file.
// The JPEG FIF APP0 marker is mandatory right after the SOI marker."
Expand All @@ -286,7 +291,7 @@ impl<R: Read> Decoder<R> {
}
*/

self.is_jfif = true;
self.jfif_app0 = Some(jfif);
},
AppData::Avi1 => self.is_mjpeg = true,
}
Expand Down Expand Up @@ -329,7 +334,7 @@ impl<R: Read> Decoder<R> {
}

let frame = self.frame.as_ref().unwrap();
compute_image(&frame.components, &planes, frame.image_size, self.is_jfif, self.color_transform)
compute_image(&frame.components, &planes, frame.image_size, self.jfif_app0.is_some(), self.color_transform)
}

fn read_marker(&mut self) -> Result<Marker> {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern crate rayon;

pub use decoder::{Decoder, ImageInfo, PixelFormat};
pub use error::{Error, UnsupportedFeature};
pub use parser::{Density, DensityUnits};

mod decoder;
mod error;
Expand Down
82 changes: 80 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@ pub struct Dimensions {
pub height: u16,
}

/// The image density, in x and y directions with a common unit
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Density {
/// The pixel density, measured in `units`, in the x direction
pub x: u16,
/// The pixel density, measured in `units`, in the y direction
pub y: u16,
/// The unit of measurement that both `x` and `y` are specified in.
pub units: DensityUnits,
}

/// The different units that `x` and `y` pixel density can be measured in
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DensityUnits {
/// no units, `x` and `y` specify the pixel aspect ratio
PixelAspectRatio,
/// `x` and `y` are dots per inch
DotsPerInch,
/// `x` and `y` are dots per cm
DotsPerCm,
}

#[derive(Clone, Copy, Debug, PartialEq)]
struct Thumbnail {
pub width: u8,
pub height: u8,

// XXX: Thumbnail data is "(3n bytes) Packed (24-bit) RGB values for the thumbnail
// pixels, n = Xthumbnail * Ythumbnail".
// data: Vec<u8>
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct JfifApp0 {
// version: &[u8; 4],
pub density: Density,
thumbnail: Thumbnail,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum EntropyCoding {
Huffman,
Expand Down Expand Up @@ -65,7 +104,7 @@ pub struct Component {
#[derive(Debug)]
pub enum AppData {
Adobe(AdobeColorTransform),
Jfif,
Jfif(JfifApp0),
Avi1,
}

Expand Down Expand Up @@ -472,6 +511,43 @@ pub fn parse_com<R: Read>(reader: &mut R) -> Result<Vec<u8>> {
Ok(buffer)
}

// https://www.w3.org/Graphics/JPEG/jfif3.pdf
pub fn parse_jfif_app0<R: Read>(reader: &mut R, length: usize) -> Result<JfifApp0> {
// Total length of APP0 = 16 bytes + 3 * n, where n = Xthumbnail * Ythumbnail
// We already read the 2-byte length and 5-byte identifier, so at least 9 bytes remain.
// We are going to ignore the thumbnail for now.
if length < 9 {
return Err(Error::Format("JFIF APP0 with invalid length".to_owned()));
}

// version = 0x0102
skip_bytes(reader, 2)?;

let units = match reader.read_u8()? {
0 => DensityUnits::PixelAspectRatio,
1 => DensityUnits::DotsPerInch,
2 => DensityUnits::DotsPerCm,
_ => return Err(Error::Format("invalid density units in APP0".to_owned())),
};
let x_density = reader.read_u16::<BigEndian>()?;
let y_density = reader.read_u16::<BigEndian>()?;

let x_thumbnail = reader.read_u8()?;
let y_thumbnail = reader.read_u8()?;

Ok(JfifApp0 {
density: Density {
x: x_density,
y: y_density,
units,
},
thumbnail: Thumbnail {
width: x_thumbnail,
height: y_thumbnail,
}
})
}

// Section B.2.4.6
pub fn parse_app<R: Read>(reader: &mut R, marker: Marker) -> Result<Option<AppData>> {
let length = read_length(reader, marker)?;
Expand All @@ -487,7 +563,9 @@ pub fn parse_app<R: Read>(reader: &mut R, marker: Marker) -> Result<Option<AppDa

// http://www.w3.org/Graphics/JPEG/jfif3.pdf
if &buffer[0 .. 5] == &[b'J', b'F', b'I', b'F', b'\0'] {
result = Some(AppData::Jfif);
let jfif = parse_jfif_app0(reader, length - bytes_read)?;
bytes_read += 9;
result = Some(AppData::Jfif(jfif));
// https://sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#AVI1
} else if &buffer[0 .. 5] == &[b'A', b'V', b'I', b'1', b'\0'] {
result = Some(AppData::Avi1);
Expand Down