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

..03-May-2022-

.github/H03-May-2022-222189

benches/H03-May-2022-269225

docs/H03-May-2022-5543

src/H03-May-2022-27,17419,655

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

.cargo_vcs_info.jsonH A D01-Jan-197074 65

.gitattributesH A D01-Jan-197040 32

.gitignoreH A D01-Jan-1970178 2322

CHANGES.mdH A D01-Jan-197017.4 KiB394326

Cargo.lockH A D01-Jan-197036.1 KiB1,4691,311

Cargo.tomlH A D01-Jan-19702.8 KiB135109

Cargo.toml.orig-cargoH A D01-Jan-19702.4 KiB9079

Cargo.toml.public-private-dependenciesH A D01-Jan-19701.6 KiB6254

LICENSEH A D01-Jan-19701.1 KiB2117

README.mdH A D01-Jan-19709.4 KiB256189

clippy.tomlH A D01-Jan-197016 21

release.shH A D01-Jan-1970799 2516

README.md

1# Image
2[![crates.io](https://img.shields.io/crates/v/image.svg)](https://crates.io/crates/image)
3[![Documentation](https://docs.rs/image/badge.svg)](https://docs.rs/image)
4[![Build Status](https://travis-ci.org/image-rs/image.svg?branch=master)](https://travis-ci.org/image-rs/image)
5[![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)
6
7Maintainers: [@HeroicKatora](https://github.com/HeroicKatora), [@fintelia](https://github.com/fintelia)
8
9[How to contribute](https://github.com/image-rs/organization/blob/master/CONTRIBUTING.md)
10
11## An Image Processing Library
12
13This crate provides basic image processing functions and methods for converting to and from various image formats.
14
15All image processing functions provided operate on types that implement the `GenericImageView` and `GenericImage` traits and return an `ImageBuffer`.
16
17## Supported Image Formats
18
19`image` provides implementations of common image format encoders and decoders.
20
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| AVIF   | Only 8-bit | Lossy |
31| PNM    | PBM, PGM, PPM, standard PAM | Yes |
32| DDS    | DXT1, DXT3, DXT5 | No |
33| TGA    | Yes | RGB(8), RGBA(8), BGR(8), BGRA(8), Gray(8), GrayA(8) |
34| farbfeld | Yes | Yes |
35
36### The [`ImageDecoder`](https://docs.rs/image/*/image/trait.ImageDecoder.html) and [`ImageDecoderExt`](https://docs.rs/image/*/image/trait.ImageDecoderExt.html) Traits
37
38All image format decoders implement the `ImageDecoder` trait which provide
39basic methods for getting image metadata and decoding images. Some formats
40additionally provide `ImageDecoderExt` implementations which allow for
41decoding only part of an image at once.
42
43The most important methods for decoders are...
44+ **dimensions**: Return a tuple containing the width and height of the image.
45+ **color_type**: Return the color type of the image data produced by this decoder.
46+ **read_image**: Decode the entire image into a slice of bytes.
47
48## Pixels
49
50`image` provides the following pixel types:
51+ **Rgb**: RGB pixel
52+ **Rgba**: RGBA pixel
53+ **Luma**: Grayscale pixel
54+ **LumaA**: Grayscale with alpha
55
56All pixels are parameterised by their component type.
57
58## Images
59Individual pixels within images are indexed with (0,0) at the top left corner.
60### The [`GenericImageView`](https://docs.rs/image/*/image/trait.GenericImageView.html) and [`GenericImage`](https://docs.rs/image/*/image/trait.GenericImage.html) Traits
61
62Traits that provide methods for inspecting (`GenericImageView`) and manipulating (`GenericImage`) images, parameterised over the image's pixel type.
63
64Some of these methods for `GenericImageView` are...
65+ **dimensions**: Return a tuple containing the width and height of the image.
66+ **get_pixel**: Returns the pixel located at (x, y).
67+ **pixels**: Returns an Iterator over the pixels of this image.
68
69While some of the methods for `GenericImage` are...
70+ **put_pixel**: Put a pixel at location (x, y).
71+ **copy_from**: Copies all of the pixels from another image into this image.
72
73### Representation of Images
74`image` provides two main ways of representing image data:
75
76#### [`ImageBuffer`](https://docs.rs/image/*/image/struct.ImageBuffer.html)
77An 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 `GenericImageView` and `GenericImage` traits.
78
79```rust
80extern crate image;
81
82use image::{GenericImage, GenericImageView, ImageBuffer, RgbImage};
83
84// Construct a new RGB ImageBuffer with the specified width and height.
85let img: RgbImage = ImageBuffer::new(512, 512);
86
87// Construct a new by repeated calls to the supplied closure.
88let mut img = ImageBuffer::from_fn(512, 512, |x, y| {
89    if x % 2 == 0 {
90        image::Luma([0u8])
91    } else {
92        image::Luma([255u8])
93    }
94});
95
96// Obtain the image's width and height.
97let (width, height) = img.dimensions();
98
99// Access the pixel at coordinate (100, 100).
100let pixel = img[(100, 100)];
101
102// Or use the `get_pixel` method from the `GenericImage` trait.
103let pixel = *img.get_pixel(100, 100);
104
105// Put a pixel at coordinate (100, 100).
106img.put_pixel(100, 100, pixel);
107
108// Iterate over all pixels in the image.
109for pixel in img.pixels() {
110    // Do something with pixel.
111}
112```
113
114#### [`DynamicImage`](https://docs.rs/image/*/image/enum.DynamicImage.html)
115A `DynamicImage` is an enumeration over all supported `ImageBuffer<P>` types.
116Its exact image type is determined at runtime. It is the type returned when opening an image.
117For convenience `DynamicImage` reimplements all image processing functions.
118
119`DynamicImage` implement the `GenericImageView` and `GenericImage` traits for RGBA pixels.
120
121#### [`SubImage`](https://docs.rs/image/*/image/struct.SubImage.html)
122A view into another image, delimited by the coordinates of a rectangle.
123The coordinates given set the position of the top left corner of the rectangle.
124This is used to perform image processing functions on a subregion of an image.
125
126```rust
127extern crate image;
128
129use image::{GenericImageView, ImageBuffer, RgbImage, imageops};
130
131let mut img: RgbImage = ImageBuffer::new(512, 512);
132let subimg = imageops::crop(&mut img, 0, 0, 100, 100);
133
134assert!(subimg.dimensions() == (100, 100));
135```
136
137## Image Processing Functions
138These are the functions defined in the `imageops` module. All functions operate on types that implement the `GenericImage` trait.
139Note that some of the functions are very slow in debug mode. Make sure to use release mode if you experience any performance issues.
140
141+ **blur**: Performs a Gaussian blur on the supplied image.
142+ **brighten**: Brighten the supplied image.
143+ **huerotate**: Hue rotate the supplied image by degrees.
144+ **contrast**: Adjust the contrast of the supplied image.
145+ **crop**: Return a mutable view into an image.
146+ **filter3x3**: Perform a 3x3 box filter on the supplied image.
147+ **flip_horizontal**: Flip an image horizontally.
148+ **flip_vertical**: Flip an image vertically.
149+ **grayscale**: Convert the supplied image to grayscale.
150+ **invert**: Invert each pixel within the supplied image This function operates in place.
151+ **resize**: Resize the supplied image to the specified dimensions.
152+ **rotate180**: Rotate an image 180 degrees clockwise.
153+ **rotate270**: Rotate an image 270 degrees clockwise.
154+ **rotate90**: Rotate an image 90 degrees clockwise.
155+ **unsharpen**: Performs an unsharpen mask on the supplied image.
156
157For more options, see the [`imageproc`](https://crates.io/crates/imageproc) crate.
158
159## Examples
160### Opening and Saving Images
161
162`image` provides the `open` function for opening images from a path.  The image
163format is determined from the path's file extension. An `io` module provides a
164reader which offer some more control.
165
166```rust,no_run
167extern crate image;
168
169use image::GenericImageView;
170
171fn main() {
172    // Use the open function to load an image from a Path.
173    // `open` returns a `DynamicImage` on success.
174    let img = image::open("tests/images/jpg/progressive/cat.jpg").unwrap();
175
176    // The dimensions method returns the images width and height.
177    println!("dimensions {:?}", img.dimensions());
178
179    // The color method returns the image's `ColorType`.
180    println!("{:?}", img.color());
181
182    // Write the contents of this image to the Writer in PNG format.
183    img.save("test.png").unwrap();
184}
185```
186
187### Generating Fractals
188
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### 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::ColorType::Rgb8).unwrap()
253}
254
255```
256