• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H03-May-2022-7543

benches/H03-May-2022-12397

docs/H03-May-2022-5543

src/H03-May-2022-19,90314,638

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

.gitattributesH A D09-Nov-201740 32

.gitignoreH A D03-Oct-2019178 2322

.travis.ymlH A D03-Oct-20191.4 KiB6362

CHANGES.mdH A D03-Oct-20196.4 KiB162135

Cargo.lockH A D01-Jan-197025.6 KiB587520

Cargo.tomlH A D01-Jan-19702.2 KiB8873

Cargo.toml.orig-cargoH A D03-Oct-20191.6 KiB8068

LICENSEH A D09-Nov-20171.1 KiB2117

README.mdH A D26-Sep-20198.6 KiB256190

release.shH A D12-Apr-2019799 2516

README.md

1# Image [![crates.io](https://img.shields.io/crates/v/image.svg)](https://crates.io/crates/image) [![Build Status](https://travis-ci.org/image-rs/image.svg?branch=master)](https://travis-ci.org/image-rs/image) [![Gitter](https://badges.gitter.im/image-rs/image.svg)](https://gitter.im/image-rs/image?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
2
3Maintainers: @HeroicKatora, @fintelia
4
5[How to contribute](https://github.com/image-rs/organization/blob/master/CONTRIBUTING.md)
6
7## An Image Processing Library
8
9This crate provides basic imaging processing functions and methods for converting to and from image formats.
10
11All image processing functions provided operate on types that implement the ```GenericImage``` trait and return an ```ImageBuffer```.
12
13## 1. Documentation
14
15https://docs.rs/image
16
17## 2. Supported Image Formats
18```image``` provides implementations of common image format encoders and decoders.
19
20### 2.1 Supported Image Formats
21| Format | Decoding | Encoding |
22| ------ | -------- | -------- |
23| PNG    | All supported color types | Same as decoding|
24| JPEG   | Baseline and progressive | Baseline JPEG |
25| GIF    | Yes | Yes |
26| BMP    | Yes | RGB(8), RGBA(8), Gray(8), GrayA(8) |
27| ICO    | Yes | Yes |
28| TIFF   | Baseline(no fax support) + LZW + PackBits | RGB(8), RGBA(8), Gray(8) |
29| WebP   | Lossy(Luma channel only) | No |
30| PNM    | PBM, PGM, PPM, standard PAM | Yes |
31
32### 2.2 The ```ImageDecoder``` Trait
33All image format decoders implement the ```ImageDecoder``` trait which provides the following methods:
34+ **dimensions**: Return a tuple containing the width and height of the image
35+ **colortype**: Return the color type of the image.
36+ **row_len**: Returns the length in bytes of one decoded row of the image
37+ **read_scanline**: Read one row from the image into buf Returns the row index
38+ **read_image**: Decode the entire image and return it as a Vector
39+ **load_rect**: Decode a specific region of the image
40
41## 3 Pixels
42```image``` provides the following pixel types:
43+ **Rgb**: RGB pixel
44+ **Rgba**: RGBA pixel
45+ **Luma**: Grayscale pixel
46+ **LumaA**: Grayscale with alpha
47
48All pixels are parameterised by their component type.
49
50## 4 Images
51### 4.1 The ```GenericImage``` Trait
52A trait that provides functions for manipulating images, parameterised over the image's pixel type.
53
54```rust
55# use image::{Pixel, Pixels};
56pub trait GenericImage {
57    /// The pixel type.
58    type Pixel: Pixel;
59
60    /// The width and height of this image.
61    fn dimensions(&self) -> (u32, u32);
62
63    /// The bounding rectangle of this image.
64    fn bounds(&self) -> (u32, u32, u32, u32);
65
66    /// Return the pixel located at (x, y)
67    fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel;
68
69    /// Put a pixel at location (x, y)
70    fn put_pixel(&mut self, x: u32, y: u32, pixel: Self::Pixel);
71
72    /// Return an Iterator over the pixels of this image.
73    /// The iterator yields the coordinates of each pixel
74    /// along with their value
75    fn pixels(&self) -> Pixels<Self>;
76}
77```
78
79### 4.2 Representation of Images
80```image``` provides two main ways of representing image data:
81
82#### 4.2.1 ```ImageBuffer```
83An image parameterised by its Pixel types, represented by a width and height and a vector of pixels. It provides direct access to its pixels and implements the ```GenericImage``` trait.
84
85```rust
86extern crate image;
87
88use image::{GenericImage, GenericImageView, ImageBuffer, RgbImage};
89
90// Construct a new RGB ImageBuffer with the specified width and height.
91let img: RgbImage = ImageBuffer::new(512, 512);
92
93// Construct a new by repeated calls to the supplied closure.
94let mut img = ImageBuffer::from_fn(512, 512, |x, y| {
95    if x % 2 == 0 {
96        image::Luma([0u8])
97    } else {
98        image::Luma([255u8])
99    }
100});
101
102// Obtain the image's width and height.
103let (width, height) = img.dimensions();
104
105// Access the pixel at coordinate (100, 100).
106let pixel = img[(100, 100)];
107
108// Or use the ```get_pixel``` method from the ```GenericImage``` trait.
109let pixel = *img.get_pixel(100, 100);
110
111// Put a pixel at coordinate (100, 100).
112img.put_pixel(100, 100, pixel);
113
114// Iterate over all pixels in the image.
115for pixel in img.pixels() {
116    // Do something with pixel.
117}
118```
119
120#### 4.2.2 ```DynamicImage```
121A ```DynamicImage``` is an enumeration over all supported ```ImageBuffer<P>``` types.
122Its exact image type is determined at runtime. It is the type returned when opening an image.
123For convenience ```DynamicImage```'s reimplement all image processing functions.
124
125```DynamicImage``` implement the ```GenericImage``` trait for RGBA pixels.
126
127#### 4.2.3 ```SubImage```
128A view into another image, delimited by the coordinates of a rectangle.
129This is used to perform image processing functions on a subregion of an image.
130
131```rust
132extern crate image;
133
134use image::{GenericImageView, ImageBuffer, RgbImage, imageops};
135
136let mut img: RgbImage = ImageBuffer::new(512, 512);
137let subimg = imageops::crop(&mut img, 0, 0, 100, 100);
138
139assert!(subimg.dimensions() == (100, 100));
140```
141
142## 5 Image Processing Functions
143These are the functions defined in the ```imageops``` module. All functions operate on types that implement the ```GenericImage``` trait.
144
145+ **blur**: Performs a Gaussian blur on the supplied image.
146+ **brighten**: Brighten the supplied image
147+ **huerotate**: Hue rotate the supplied image by degrees
148+ **contrast**: Adjust the contrast of the supplied image
149+ **crop**: Return a mutable view into an image
150+ **filter3x3**: Perform a 3x3 box filter on the supplied image.
151+ **flip_horizontal**: Flip an image horizontally
152+ **flip_vertical**: Flip an image vertically
153+ **grayscale**: Convert the supplied image to grayscale
154+ **invert**: Invert each pixel within the supplied image This function operates in place.
155+ **resize**: Resize the supplied image to the specified dimensions
156+ **rotate180**: Rotate an image 180 degrees clockwise.
157+ **rotate270**: Rotate an image 270 degrees clockwise.
158+ **rotate90**: Rotate an image 90 degrees clockwise.
159+ **unsharpen**: Performs an unsharpen mask on the supplied image
160
161## 6 Examples
162### 6.1 Opening And Saving Images
163```image``` provides the ```open``` function for opening images from a path.
164
165The image format is determined from the path's file extension.
166
167```rust,no_run
168extern crate image;
169
170use image::GenericImageView;
171
172fn main() {
173    // Use the open function to load an image from a Path.
174    // ```open``` returns a `DynamicImage` on success.
175    let img = image::open("tests/images/jpg/progressive/cat.jpg").unwrap();
176
177    // The dimensions method returns the images width and height.
178    println!("dimensions {:?}", img.dimensions());
179
180    // The color method returns the image's `ColorType`.
181    println!("{:?}", img.color());
182
183    // Write the contents of this image to the Writer in PNG format.
184    img.save("test.png").unwrap();
185}
186```
187
188### 6.2 Generating Fractals
189```rust,no_run
190//! An example of generating julia fractals.
191extern crate image;
192extern crate num_complex;
193
194fn main() {
195    let imgx = 800;
196    let imgy = 800;
197
198    let scalex = 3.0 / imgx as f32;
199    let scaley = 3.0 / imgy as f32;
200
201    // Create a new ImgBuf with width: imgx and height: imgy
202    let mut imgbuf = image::ImageBuffer::new(imgx, imgy);
203
204    // Iterate over the coordinates and pixels of the image
205    for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
206        let r = (0.3 * x as f32) as u8;
207        let b = (0.3 * y as f32) as u8;
208        *pixel = image::Rgb([r, 0, b]);
209    }
210
211    // A redundant loop to demonstrate reading image data
212    for x in 0..imgx {
213        for y in 0..imgy {
214            let cx = y as f32 * scalex - 1.5;
215            let cy = x as f32 * scaley - 1.5;
216
217            let c = num_complex::Complex::new(-0.4, 0.6);
218            let mut z = num_complex::Complex::new(cx, cy);
219
220            let mut i = 0;
221            while i < 255 && z.norm() <= 2.0 {
222                z = z * z + c;
223                i += 1;
224            }
225
226            let pixel = imgbuf.get_pixel_mut(x, y);
227            let image::Rgb(data) = *pixel;
228            *pixel = image::Rgb([data[0], i as u8, data[2]]);
229        }
230    }
231
232    // Save the image as “fractal.png”, the format is deduced from the path
233    imgbuf.save("fractal.png").unwrap();
234}
235```
236
237Example output:
238
239<img src="examples/fractal.png" alt="A Julia Fractal, c: -0.4 + 0.6i" width="500" />
240
241### 6.3 Writing raw buffers
242If the high level interface is not needed because the image was obtained by other means, `image` provides the function `save_buffer` to save a buffer to a file.
243
244```rust,no_run
245extern crate image;
246
247fn main() {
248
249    let buffer: &[u8] = unimplemented!(); // Generate the image data
250
251    // Save the buffer as "image.png"
252    image::save_buffer("image.png", buffer, 800, 600, image::RGB(8)).unwrap()
253}
254
255```
256