Skip to content

Commit

Permalink
Add initial version of JpegStreamReader
Browse files Browse the repository at this point in the history
  • Loading branch information
vbaderks committed Sep 9, 2023
1 parent 127f209 commit 371b90e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ use std::io::{Read, self};

#[warn(unused_variables)]

#[derive(Debug)]
pub enum DecodingError {
/// An error in IO of the underlying reader.
IoError(io::Error),

UnknownError
}


#[derive(Debug)]
Expand Down
13 changes: 13 additions & 0 deletions src/decoding_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Team CharLS.
// SPDX-License-Identifier: BSD-3-Clause

use std::io;

#[derive(Debug)]
pub enum DecodingError {
/// An error in IO of the underlying reader.
IoError(io::Error),

StartOfImageMarkerNotFound,
UnknownError
}
48 changes: 48 additions & 0 deletions src/jpeg_marker_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Team CharLS.
// SPDX-License-Identifier: BSD-3-Clause


#[derive(Debug, Eq, PartialEq)]
pub enum JpegMarkerCode {
StartOfImage = 0xD8, // SOI: Marks the start of an image.
EndOfImage = 0xD9, // EOI: Marks the end of an image.
StartOfScan = 0xDA, // SOS: Marks the start of scan.
DefineRestartInterval = 0xDD, // DRI: Defines the restart interval used in succeeding scans.

// The following markers are defined in ISO/IEC 10918-1 | ITU T.81.
StartOfFrameBaselineJpeg = 0xC0, // SOF_0: Marks the start of a baseline jpeg encoded frame.
StartOfFrameExtendedSequential = 0xC1, // SOF_1: Marks the start of a extended sequential Huffman encoded frame.
StartOfFrameProgressive = 0xC2, // SOF_2: Marks the start of a progressive Huffman encoded frame.
StartOfFrameLossless = 0xC3, // SOF_3: Marks the start of a lossless Huffman encoded frame.
StartOfFrameDifferentialSequential =
0xC5, // SOF_5: Marks the start of a differential sequential Huffman encoded frame.
StartOfFrameDifferentialProgressive =
0xC6, // SOF_6: Marks the start of a differential progressive Huffman encoded frame.
StartOfFrameDifferentialLossless = 0xC7, // SOF_7: Marks the start of a differential lossless Huffman encoded frame.
StartOfFrameExtendedArithmetic = 0xC9, // SOF_9: Marks the start of a extended sequential arithmetic encoded frame.
StartOfFrameProgressiveArithmetic = 0xCA, // SOF_10: Marks the start of a progressive arithmetic encoded frame.
StartOfFrameLosslessArithmetic = 0xCB, // SOF_11: Marks the start of a lossless arithmetic encoded frame.

// The following markers are defined in ISO/IEC 14495-1 | ITU T.87.
StartOfFrameJpegls = 0xF7, // SOF_55: Marks the start of a JPEG-LS encoded frame.
JpeglsPresetParameters = 0xF8, // LSE: Marks the start of a JPEG-LS preset parameters segment.
StartOfFrameJpeglsExtended = 0xF9, // SOF_57: Marks the start of a JPEG-LS extended (ISO/IEC 14495-2) encoded frame.

ApplicationData0 = 0xE0, // APP0: Application data 0: used for JFIF header.
ApplicationData1 = 0xE1, // APP1: Application data 1: used for EXIF or XMP header.
ApplicationData2 = 0xE2, // APP2: Application data 2: used for ICC profile.
ApplicationData3 = 0xE3, // APP3: Application data 3: used for meta info
ApplicationData4 = 0xE4, // APP4: Application data 4.
ApplicationData5 = 0xE5, // APP5: Application data 5.
ApplicationData6 = 0xE6, // APP6: Application data 6.
ApplicationData7 = 0xE7, // APP7: Application data 7: used for HP color-space info.
ApplicationData8 = 0xE8, // APP8: Application data 8: used for HP color-transformation info or SPIFF header.
ApplicationData9 = 0xE9, // APP9: Application data 9.
ApplicationData10 = 0xEA, // APP10: Application data 10.
ApplicationData11 = 0xEB, // APP11: Application data 11.
ApplicationData12 = 0xEC, // APP12: Application data 12: used for Picture info.
ApplicationData13 = 0xED, // APP13: Application data 13: used by PhotoShop IRB
ApplicationData14 = 0xEE, // APP14: Application data 14: used by Adobe
ApplicationData15 = 0xEF, // APP15: Application data 15.
Comment = 0xFE // COM: Comment block.
}
77 changes: 77 additions & 0 deletions src/jpeg_stream_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Team CharLS.
// SPDX-License-Identifier: BSD-3-Clause

//mod jpeg_marker_code;

use std::io::{Read, self};

use crate::jpeg_marker_code::JpegMarkerCode;
use crate::decoding_error::DecodingError;

#[derive(Clone, Debug)]
pub struct FrameInfo {
width: u32,
height: u32,
bits_per_sample: u8,
component_count: u8
}


#[derive(Debug, Eq, PartialEq)]
enum ReaderState
{
BeforeStartOfImage,
HeaderSection,
SpiffHeaderSection,
ImageSection,
FrameSection,
ScanSection,
BitStreamSection,
AfterEndOfImage
}


#[derive(Debug)]
pub struct JpegStreamReader<R: Read> {
reader: R,
frame_info: FrameInfo,
state: ReaderState
}


impl<R: Read> JpegStreamReader<R> {
pub fn new(mut r: R) -> JpegStreamReader<R> {
let width = 0;
let height = 0;
let bits_per_sample = 0;
let component_count = 0;

JpegStreamReader {
reader: r,
frame_info: FrameInfo {
width,
height,
bits_per_sample,
component_count
},
state: ReaderState::BeforeStartOfImage
}
}

pub fn read_next_marker_code(&mut self) -> JpegMarkerCode {
JpegMarkerCode::StartOfImage
}

pub fn read_header(&mut self) -> Result<(), DecodingError> {
if self.state == ReaderState::BeforeStartOfImage {
if self.read_next_marker_code() != JpegMarkerCode::StartOfImage {
return Err(DecodingError::StartOfImageMarkerNotFound);
}

self.state = ReaderState::HeaderSection;
}

Ok(())
}
}

3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
// SPDX-License-Identifier: BSD-3-Clause

mod decoder;
mod jpeg_stream_reader;
mod jpeg_marker_code;
mod decoding_error;

0 comments on commit 371b90e

Please sign in to comment.