Initial commit

This commit is contained in:
svartalf
2016-07-18 13:20:57 +03:00
commit 3b0676281b
17 changed files with 425 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
target
Cargo.lock
+8
View File
@@ -0,0 +1,8 @@
language: rust
rust:
- stable
- beta
- nightly
matrix:
allow_failures:
- rust: nightly
+12
View File
@@ -0,0 +1,12 @@
[package]
name = "imghdr"
version = "0.1.2"
authors = ["svartalf <self@svartalf.info>"]
description = "Library that determines the type of image contained in a file or byte stream."
documentation = "https://svartalf.github.io/rust-imghdr/imghdr/index.html"
keywords = ["image", "gif", "jpeg", "png", "webp"]
repository = "https://github.com/svartalf/rust-imghdr"
license = "MIT"
[dependencies]
clippy = { version = "0.0.79", optional = true }
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 svartalf
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+43
View File
@@ -0,0 +1,43 @@
# imghdr
Library that determines the type of image contained in a file or byte stream, basically clone of the Python [imghdr](docs.python.org/library/imghdr.html) module.
[![Build Status](https://travis-ci.org/svartalf/rust-imghdr.svg?branch=master)](https://travis-ci.org/svartalf/rust-imghdr)
![License](http://img.shields.io/:license-mit-blue.svg)
[![Crates.io](https://img.shields.io/badge/crates.io-imghdr-green.svg)](https://crates.io/crates/imghdr)
Documentation is [here](https://svartalf.github.io/rust-imghdr/imghdr/index.html).
## Examples
Check a file directly:
```rust
extern crate imghdr;
fn main() {
match imghdr::open("/path/to/image.png") {
Ok(imghdr::Type::Png) => println!("Yep, it is a PNG"),
_ => println!("Nope, it is definitely not a PNG"),
}
}
```
Or check a byte stream:
```rust
extern crate imghdr;
fn main() {
let mut file = File::open("/path/to/image.png").unwrap();
let mut content: Vec<u8> = vec![];
file.read_to_end(&mut content).unwrap();
match imghdr::what(content.as_slice()) {
Some(imghdr::Type::Jpg) => println!("And this is a Jpeg"),
_ => println!("Can a Png, Bmp or other file format"),
}
}
```
+10
View File
@@ -0,0 +1,10 @@
use std::env;
extern crate imghdr;
fn main() {
for path in env::args().skip(1) {
println!("{:?}", imghdr::open(&path));
}
}
+112
View File
@@ -0,0 +1,112 @@
use std::fs::File;
use std::io::{Read, Error, ErrorKind};
use std::path::Path;
/// Recognized image types
#[derive(Debug, PartialEq)]
pub enum Type {
/// SGI ImgLib files
Rgb,
/// Gif 87a and 89a Files
Gif,
/// Portable Bitmap files
Pbm,
/// Portable Graymap files
Pgm,
/// Portable Pixmap files
Ppm,
/// TIFF files
Tiff,
/// Sun Raster files
Rast,
/// X Bitmap files
Xbm,
/// JPEG data in JFIF or Exif formats
Jpeg,
/// BMP files
Bmp,
/// Portable Network Graphics
Png,
/// WebP files
Webp,
/// OpenEXR files
Exr,
}
// Magic numbers
const PNG: &'static [u8] = b"\x89PNG\r\n\x1a\n";
const JFIF: &'static [u8] = b"JFIF";
const EXIF: &'static [u8] = b"Exif";
const GIF87A: &'static [u8] = b"GIF87a";
const GIF89A: &'static [u8] = b"GIF89a";
const TIFF_MM: &'static [u8] = b"MM"; // Motorola byte order
const TIFF_II: &'static [u8] = b"II"; // Intel byte order
const RGB: &'static [u8] = b"\x01\xda";
const RAST: &'static [u8] = b"\x59\xA6\x6A\x95";
const XBM: &'static [u8] = b"#define ";
const RIFF: &'static [u8] = b"RIFF";
const WEBP: &'static [u8] = b"WEBP";
const BMP: &'static [u8] = b"BM";
fn guess(ref bytes: [u8; 32]) -> Option<Type> {
match () {
_ if &bytes[0..8] == PNG => Some(Type::Png),
_ if (&bytes[6..10] == JFIF) | (&bytes[6..10] == EXIF) => Some(Type::Jpeg),
_ if (&bytes[..6] == GIF87A) | (&bytes[..6] == GIF89A) => Some(Type::Gif),
_ if (&bytes[..2] == TIFF_MM) | (&bytes[..2] == TIFF_II) => Some(Type::Tiff),
_ if &bytes[0..2] == RGB => Some(Type::Rgb),
_ if &bytes[0..4] == RAST => Some(Type::Rast),
_ if &bytes[0..8] == XBM => Some(Type::Xbm),
_ if (&bytes[0..4] == RIFF) & (&bytes[8..12] == WEBP) => Some(Type::Webp),
_ if &bytes[0..2] == BMP => Some(Type::Bmp),
_ => None
}
}
/// Tests the image data contained in the `f` bytes stream.
///
/// # Examples
///
/// ```rust,ignore
/// use std::fs::File;
/// use std::io::Read;
///
/// let mut file = File::open("/path/to/image.png").unwrap();
/// let mut content: Vec<u8> = vec![];
/// file.read_to_end(&mut content).unwrap();
/// println!("{:?}", imghdr::what(content.as_slice()));
/// ```
pub fn what<T: Read>(mut f: T) -> Option<Type> {
let mut buffer = [0; 32];
f.read(&mut buffer).unwrap();
guess(buffer)
}
/// Open file and test if it an image.
///
/// # Errors
///
/// This function will return an `std::io::Error` if file is inaccessible or can't be read.
///
/// Also it will return an `std::io::Error` with a `std::io::ErrorKind::InvalidData` kind
/// if file is not an image.
///
/// # Examples
///
/// ```rust,ignore
/// imghdr.open("/path/to/image.jpg").unwrap();
/// ```
pub fn open<T: AsRef<Path>>(path: T) -> Result<Type, Error> {
let mut file = try!(File::open(path));
let mut buffer = [0; 32];
try!(file.read_exact(&mut buffer));
match guess(buffer) {
Some(image) => Ok(image),
None => Err(Error::new(ErrorKind::InvalidData, "Unknown file format"))
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 562 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1011 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 B

File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 292 B

+91
View File
@@ -0,0 +1,91 @@
#define im_width 100
#define im_height 100
static char im_bits[] = {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,
0xff,0xff,0xff,0x3f,0xfe,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,
0xff,0x7f,0xfe,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0x51,
0x08,0xfe,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0x43,0x12,0xfc,
0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0x67,0x52,0xfd,0xff,0xff,
0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0x43,0x52,0xfd,0xff,0xff,0xff,0xff,
0x0f,0xff,0xff,0xff,0xff,0xff,0x09,0x58,0xfd,0xff,0xff,0xff,0xff,0x0f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0f
};
+42
View File
@@ -0,0 +1,42 @@
To sing a song that old was sung,
From ashes ancient Gower is come;
Assuming man's infirmities,
To glad your ear, and please your eyes.
It hath been sung at festivals,
On ember-eves and holy-ales;
And lords and ladies in their lives
Have read it for restoratives:
The purchase is to make men glorious;
Et bonum quo antiquius, eo melius.
If you, born in these latter times,
When wit's more ripe, accept my rhymes.
And that to hear an old man sing
May to your wishes pleasure bring
I life would wish, and that I might
Waste it for you, like taper-light.
This Antioch, then, Antiochus the Great
Built up, this city, for his chiefest seat:
The fairest in all Syria,
I tell you what mine authors say:
This king unto him took a fere,
Who died and left a female heir,
So buxom, blithe, and full of face,
As heaven had lent her all his grace;
With whom the father liking took,
And her to incest did provoke:
Bad child; worse father! to entice his own
To evil should be done by none:
But custom what they did begin
Was with long use account no sin.
The beauty of this sinful dame
Made many princes thither frame,
To seek her as a bed-fellow,
In marriage-pleasures play-fellow:
Which to prevent he made a law,
To keep her still, and men in awe,
That whoso ask'd her for his wife,
His riddle told not, lost his life:
So for her many a wight did die,
As yon grim looks do testify.
What now ensues, to the judgment of your eye
I give, my cause who best can justify.
+80
View File
@@ -0,0 +1,80 @@
#[cfg(test)]
use std::fs::File;
use std::io::Read;
extern crate imghdr;
macro_rules! assert_result {
($format:expr, $path:expr) => (
{
let result = imghdr::open($path);
match $format {
Some(type_) => assert_eq!(type_, result.unwrap()),
None => assert!(result.is_err()),
}
}
{
let file = File::open($path);
if file.is_err() & $format.is_none() {
return;
} else {
let mut content: Vec<u8> = vec![];
file.unwrap().read_to_end(&mut content).unwrap();
let result = imghdr::what(content.as_slice());
match $format {
Some(type_) => assert_eq!(type_, result.unwrap()),
None => assert!(result.is_none()),
}
}
/*
*/
}
)
}
#[test]
fn test_png() {
assert_result!(Some(imghdr::Type::Png), "./tests/images/example.png");
}
#[test]
fn test_jpeg() {
assert_result!(Some(imghdr::Type::Jpeg), "./tests/images/example.jpeg");
}
#[test]
fn test_gif() {
assert_result!(Some(imghdr::Type::Gif), "./tests/images/example.gif");
}
#[test]
fn test_webp() {
assert_result!(Some(imghdr::Type::Webp), "./tests/images/example.webp");
}
#[test]
fn test_bmp() {
assert_result!(Some(imghdr::Type::Bmp), "./tests/images/example.bmp");
}
#[test]
fn test_tiff() {
assert_result!(Some(imghdr::Type::Tiff), "./tests/images/example.tiff");
}
#[test]
fn test_xbm() {
assert_result!(Some(imghdr::Type::Xbm), "./tests/images/example.xbm");
}
#[test]
fn test_not_a_image() {
assert_result!(None::<imghdr::Type>, "./tests/images/not-a-image.txt");
}
#[test]
fn test_not_a_file() {
assert_result!(None::<imghdr::Type>, "./tests/images/not-existing-file.foo");
}