Skip to content

Commit

Permalink
Use tiff tag types from tiff crate (#11)
Browse files Browse the repository at this point in the history
* Add tiff

TIFF decoding and encoding library in pure Rust! Repo at https://github.com/image-rs/image-tiff

* Rename src/tiff.rs to src/geotiff.rs

Reduce confusion when using the `tiff` crate later.

* Use tiff tag type enums from upstream tiff crate

Remove the TagType enum in src/lowlevel.rs, use tiff:tags:Type instead (https://docs.rs/tiff/0.9.1/tiff/tags/enum.Type.html). Added support for IFD, LONG8, SLONG8 and IFD8 types.

* Fix clippy lint error on match_ref_pats

Remove the & from the match statements. Xref https://rust-lang.github.io/rust-clippy/master/index.html#/match_ref_pats

* Add note to main README.md on refactoring process to rely on tiff crate

Also bumped up version in badge from v0.0.1 to v0.0.2 to match that in crates.io.
  • Loading branch information
weiji14 authored May 27, 2024
1 parent 56f30de commit f94ea53
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 95 deletions.
126 changes: 95 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ repository = "https://github.com/georust/geotiff"
byteorder = "*"
enum_primitive = "*"
num = "*"
tiff = "0.9"
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# A TIFF Library for Rust

![Version](https://img.shields.io/badge/version-v0.0.1-red.svg)
![Version](https://img.shields.io/badge/version-v0.0.2-red.svg)

> [!IMPORTANT]
> This crate is currently undergoing a significant refactoring process to be built on
> top of the [`tiff`](https://crates.io/crates/tiff) crate, so expect breaking changes
> as we work towards a v0.1.0 release sometime in 2024 (contributions are welcome!). See
> the thread at https://github.com/georust/geotiff/issues/7 for more details.
## Motivation (pre-2020)

I needed this library to import elevation models for a routing library. As elevation models usually come in GeoTIFF format, but no such library was available for Rust, I created this library, taking other libraries as inspiration:

Expand Down Expand Up @@ -46,4 +54,3 @@ Several documents describe the structure of a (Geo)TIFF:
* The official TIFF specification: http://download.osgeo.org/geotiff/spec/tiff6.pdf.
* The official GeoTIFF specitication: http://download.osgeo.org/geotiff/spec/geotiff.rtf
* The article "GeoTIFF – A standard image file format for GIS applications" by Mahammad and Ramakrishnan: https://www.geospatialworld.net/article/geotiff-a-standard-image-file-format-for-gis-applications/

14 changes: 5 additions & 9 deletions src/tiff.rs → src/geotiff.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashSet;

use enum_primitive::FromPrimitive;
use tiff::tags::Type;

use crate::lowlevel::*;

Expand Down Expand Up @@ -35,11 +36,11 @@ pub struct IFD {
/// tag values.
#[derive(Debug)]
pub struct IFDEntry {
pub tag: TIFFTag,
pub tpe: TagType,
pub count: LONG,
pub tag: TIFFTag,
pub tpe: Type,
pub count: LONG,
pub value_offset: LONG,
pub value: Vec<TagValue>,
pub value: Vec<TagValue>,
}

/// Implementations for the IFD struct.
Expand All @@ -62,11 +63,6 @@ pub fn decode_tag(value: u16) -> Option<TIFFTag> {
TIFFTag::from_u16(value)
}

/// Decodes an u16 value into a TagType.
pub fn decode_tag_type(tpe: u16) -> Option<TagType> {
TagType::from_u16(tpe)
}

/// Validation functions to make sure all the required tags are existing for a certain GeoTiff
/// image type (e.g., grayscale or RGB image).
pub fn validate_required_tags_for(typ: &ImageType) -> Option<HashSet<TIFFTag>> {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ extern crate num;
use std::fmt;
use std::io::Result;

pub mod geotiff;
mod lowlevel;
mod reader;
pub mod tiff;

pub use geotiff::TIFF;
use reader::*;
pub use tiff::TIFF;

/// The GeoTIFF library reads `.tiff` files.
///
Expand Down
52 changes: 19 additions & 33 deletions src/lowlevel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(dead_code)]
use tiff::tags::Type;

// Base types of the TIFF format.
pub type BYTE = u8;
Expand All @@ -23,40 +23,26 @@ enum_from_primitive! {
}
}

enum_from_primitive! {
#[repr(u16)]
#[derive(Debug,PartialEq)]
pub enum TagType {
ByteTag = 1,
ASCIITag = 2,
ShortTag = 3,
LongTag = 4,
RationalTag = 5,
SignedByteTag = 6,
UndefinedTag = 7,
SignedShortTag = 8,
SignedLongTag = 9,
SignedRationalTag = 10,
FloatTag = 11,
DoubleTag = 12,
}
}

/// Helper function that returns the size of a certain tag.
pub fn tag_size(t: &TagType) -> u32 {
pub fn tag_size(t: &Type) -> u32 {
match *t {
TagType::ByteTag => 1,
TagType::ASCIITag => 1,
TagType::ShortTag => 2,
TagType::LongTag => 4,
TagType::RationalTag => 8,
TagType::SignedByteTag => 1,
TagType::UndefinedTag => 1,
TagType::SignedShortTag => 2,
TagType::SignedLongTag => 2,
TagType::SignedRationalTag => 8,
TagType::FloatTag => 4,
TagType::DoubleTag => 8,
Type::BYTE => 1,
Type::ASCII => 1,
Type::SHORT => 2,
Type::LONG => 4,
Type::RATIONAL => 8,
Type::SBYTE => 1,
Type::UNDEFINED => 1,
Type::SSHORT => 2,
Type::SLONG => 2,
Type::SRATIONAL => 8,
Type::FLOAT => 4,
Type::DOUBLE => 8,
Type::IFD => 4,
Type::LONG8 => 8,
Type::SLONG8 => 8,
Type::IFD8 => 8,
_ => unimplemented!(),
}
}

Expand Down
Loading

0 comments on commit f94ea53

Please sign in to comment.