iiif/api.rs
1//! The main Image API
2use crate::parameters::*;
3
4#[derive(Default, Debug, Clone, PartialEq)]
5pub struct Image {
6 pub host: String,
7 pub prefixes: Vec<String>,
8 pub identifier: String,
9 pub region: Region,
10 pub size: Size,
11 pub rotation: Rotation,
12 pub quality: Quality,
13 pub format: Format
14}
15
16impl Image {
17 /// Creates a new Image API with the host required or optionally the host
18 /// + prefixes. The host argument requires the full base url with the scheme
19 /// included.
20 ///
21 /// ```rust,ignore
22 /// use iiif::*;
23 ///
24 /// // Including prefixes upfront
25 /// let api1 = Image::new(https://ids.lib.harvard.edu/ids/iiif/);
26 ///
27 /// // Adding prefixes later
28 /// let mut api2 = Image::new("https://ids.lib.harvard.edu");
29 /// api2.prefixes(vec!["ids", "iiif"]);
30 /// ```
31 ///
32 pub fn new(host: &str) -> Image {
33 Image {
34 host: host.into(),
35 ..Default::default()
36 }
37 }
38
39 /// Sets the prefix/es, these can be optionally provided in the host field
40 /// when creating a new Image struct, if placed in there they can't be changed later.
41 pub fn prefixes(&mut self, prefixes: Vec<&str>) {
42 self.prefixes = prefixes.iter().map(|s| s.to_string()).collect();
43 }
44
45 /// Set the image identifier for the next request
46 pub fn identifier(&mut self, identifier: &str) {
47 self.identifier = identifier.into()
48 }
49
50 /// Sets the current image region to full, this is the default and unnecessary
51 /// for a newly created Image struct
52 pub fn full_region(&mut self) {
53 self.region = Region::Full;
54 }
55
56 /// Sets the region is defined as an area where the width and height are both equal to the length of the shorter dimension of the complete image. The region may be positioned anywhere in the longer dimension of the image content at the server’s discretion, and centered is often a reasonable default.
57 pub fn square_region(&mut self) {
58 self.region = Region::Square;
59 }
60
61 /// Sets the region of the full image to be returned is specified in terms of absolute pixel values. The value of x represents the number of pixels from the 0 position on the horizontal axis. The value of y represents the number of pixels from the 0 position on the vertical axis. Thus the x,y position 0,0 is the upper left-most pixel of the image. w represents the width of the region and h represents the height of the region in pixels.
62 pub fn absolute_region(&mut self, x: usize, y: usize, w: usize, h: usize) {
63 self.region = Region::Abs(Absolute{ x, y, w, h });
64 }
65
66 /// Sets region to be returned is specified as a sequence of percentages of the full image’s dimensions, as reported in the image information document. Thus, x represents the number of pixels from the 0 position on the horizontal axis, calculated as a percentage of the reported width. w represents the width of the region, also calculated as a percentage of the reported width. The same applies to y and h respectively
67 pub fn pct_region(&mut self, x: f32, y: f32, w: f32, h: f32) {
68 self.region = Region::Pct({Percentage{ x, y, w, h }})
69 }
70
71 /// Sets the image or region is returned at the maximum size available, as indicated by maxWidth, maxHeight, maxArea in the profile description.
72 pub fn max_size(&mut self) {
73 self.size = Size::Max;
74 }
75
76 /// Sets the image or region should be scaled so that its width is exactly equal to w, and the height will be a calculated value that maintains the aspect ratio of the extracted region.
77 ///
78 /// Do not use this method in conjunction with height, use `width_height(w, h)` instead.
79 pub fn width(&mut self, w: usize) {
80 self.size = Size::W(w);
81 }
82
83 /// Sets the image or region should be scaled so that its height is exactly equal to h, and the width will be a calculated value that maintains the aspect ratio of the extracted region.
84 pub fn height(&mut self, w: usize) {
85 self.size = Size::W(w);
86 }
87
88 /// Sets the width and height of the returned image is scaled to n% of the width and height of the extracted region. The aspect ratio of the returned image is the same as that of the extracted region.
89 pub fn pct_size(&mut self, n: u16) {
90 self.size = Size::Pct(n);
91 }
92
93 /// Sets the width and height of the returned image are exactly w and h. The aspect ratio of the returned image may be different than the extracted region, resulting in a distorted image.
94 pub fn width_height(&mut self, w: usize, h: usize) {
95 self.size = Size::WH(w,h);
96 }
97
98 /// Sets the image content is scaled for the best fit such that the resulting width and height are less than or equal to the requested width and height. The exact scaling may be determined by the service provider, based on characteristics including image quality and system performance. The dimensions of the returned image content are calculated to maintain the aspect ratio of the extracted region.
99 pub fn less_than_width_height(&mut self, w: usize, h: usize) {
100 self.size = Size::LtWH(w,h);
101 }
102
103 /// Sets the image to be mirrored
104 pub fn mirrored(&mut self) {
105 self.rotation = Rotation::Mirror(0.0);
106 }
107
108 /// Sets the image to be rotated 90 degrees
109 pub fn rotate_right(&mut self) {
110 self.rotation = Rotation::Normal(90.0);
111 }
112
113 /// Sets the image to be rotated 270 degrees
114 pub fn rotate_left(&mut self) {
115 self.rotation = Rotation::Normal(270.0);
116 }
117
118 /// Sets the quality to be full color
119 pub fn full_color(&mut self) {
120 self.quality = Quality::Color;
121 }
122
123 /// Sets the image quality to be gray
124 pub fn gray(&mut self) {
125 self.quality = Quality::Gray;
126 }
127
128 /// Sets the image quality to be bitonal
129 pub fn bitonal(&mut self) {
130 self.quality = Quality::Bitonal;
131 }
132
133 /// Sets the image format to be jpg
134 pub fn jpg(&mut self) {
135 self.format = Format::Jpg;
136 }
137
138 /// Sets the image format to be tif
139 pub fn tif(&mut self) {
140 self.format = Format::Tif;
141 }
142
143 /// Sets the image format to be png
144 pub fn png(&mut self) {
145 self.format = Format::Png;
146 }
147
148 /// Sets the image format to be gif
149 pub fn gif(&mut self) {
150 self.format = Format::Gif;
151 }
152
153 /// Sets the image format to be jp2
154 pub fn jp2(&mut self) {
155 self.format = Format::Jp2;
156 }
157
158 /// Sets the image format to be pdf
159 pub fn pdf(&mut self) {
160 self.format = Format::Pdf;
161 }
162
163 /// Sets the image format to be webp
164 pub fn webp(&mut self) {
165 self.format = Format::Webp;
166 }
167}