1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.

use buffer::Buffer;
use pixel;
use view;
use super::Scaler;

/// Trait for scalable types.
pub trait Scale<P, C>
	where P: pixel::Read<C> + pixel::Write<C>,
	      C: pixel::Channel,
{
	/// Resize to the given width and height.
	///
	/// # Example
	///
	/// ```
	/// use picto::read;
	/// use picto::color::Rgb;
	/// use picto::processing::prelude::*;
	///
	/// let image   = read::from_path::<Rgb, u8, _>("tests/boat.xyz").unwrap();
	/// let resized = image.resize::<scaler::Lanczos3>(100, 100);
	///
	/// assert_eq!(resized.width(), 100);
	/// assert_eq!(resized.height(), 100);
	/// ```
	fn resize<A>(self, width: u32, height: u32) -> Buffer<P, C, Vec<C>>
		where A: Scaler<P, C, P, C>;

	/// Scale by the given factor.
	///
	/// # Example
	///
	/// ```
	/// use picto::read;
	/// use picto::color::Rgb;
	/// use picto::processing::prelude::*;
	///
	/// let image   = read::from_path::<Rgb, u8, _>("tests/boat.xyz").unwrap();
	/// let resized = image.scale_by::<scaler::Lanczos3>(0.5);
	///
	/// assert_eq!(resized.width(), 160);
	/// assert_eq!(resized.height(), 120);
	/// ```
	fn scale_by<A>(self, factor: f32) -> Buffer<P, C, Vec<C>>
		where A: Scaler<P, C, P, C>;

	/// Scale to the given width and height, maintaining the aspect ratio.
	///
	/// # Example
	///
	/// ```
	/// use picto::read;
	/// use picto::color::Rgb;
	/// use picto::processing::prelude::*;
	///
	/// let image   = read::from_path::<Rgb, u8, _>("tests/boat.xyz").unwrap();
	/// let resized = image.scale_to::<scaler::Lanczos3>(160, 160);
	///
	/// assert_eq!(resized.width(), 160);
	/// assert_eq!(resized.height(), 120);
	/// ```
	fn scale_to<A>(self, width: u32, height: u32) -> Buffer<P, C, Vec<C>>
		where A: Scaler<P, C, P, C>;
}

impl<'i, P, C, I> Scale<P, C> for I
	where P: pixel::Read<C> + pixel::Write<C>,
	      C: pixel::Channel,
	      I: Into<view::Read<'i, P, C>>
{
	#[inline]
	fn resize<A>(self, width: u32, height: u32) -> Buffer<P, C, Vec<C>>
		where A: Scaler<P, C, P, C>,
	{
		resize::<A, _, P, C, P, C>(self, width, height)
	}

	#[inline]
	fn scale_by<A>(self, factor: f32) -> Buffer<P, C, Vec<C>>
		where A: Scaler<P, C, P, C>
	{
		by::<A, _, P, C, P, C>(self, factor)
	}

	#[inline]
	fn scale_to<A>(self, width: u32, height: u32) -> Buffer<P, C, Vec<C>>
		where A: Scaler<P, C, P, C>,
	{
		to::<A, _, P, C, P, C>(self, width, height)
	}
}

/// Resize to the given width and height.
#[inline]
pub fn resize<'i, A, I, PI, CI, PO, CO>(input: I, width: u32, height: u32) -> Buffer<PO, CO, Vec<CO>>
	where A:  Scaler<PI, CI, PO, CO>,
	      PO: From<PI>,
	      PO: pixel::Write<CO>,
	      CO: pixel::Channel,
	      PI: pixel::Read<CI>,
	      CI: pixel::Channel,
	      I:  Into<view::Read<'i, PI, CI>>
{
	let input = input.into();

	if input.width() == width && input.height() == height {
		return input.convert::<PO, CO>();
	}

	A::scale(&input.into(), width, height)
}

/// Scale by the given factor.
#[inline]
pub fn by<'i, A, I, PI, CI, PO, CO>(input: I, factor: f32) -> Buffer<PO, CO, Vec<CO>>
	where A:  Scaler<PI, CI, PO, CO>,
	      PO: From<PI>,
	      PO: pixel::Write<CO>,
	      CO: pixel::Channel,
	      PI: pixel::Read<CI>,
	      CI: pixel::Channel,
	      I:  Into<view::Read<'i, PI, CI>>
{
	let input  = input.into();
	let width  = input.width() as f32 * factor;
	let height = input.height() as f32 * factor;

	resize::<A, _, PI, CI, PO, CO>(input, width as u32, height as u32)
}

/// Scale to the given width and height, maintaining the aspect ratio.
#[inline]
pub fn to<'i, A, I, PI, CI, PO, CO>(input: I, width: u32, height: u32) -> Buffer<PO, CO, Vec<CO>>
	where A:  Scaler<PI, CI, PO, CO>,
	      PO: From<PI>,
	      PO: pixel::Write<CO>,
	      CO: pixel::Channel,
	      PI: pixel::Read<CI>,
	      CI: pixel::Channel,
	      I:  Into<view::Read<'i, PI, CI>>
{
	let input = input.into();
	let r_old = input.width() as f32 / input.height() as f32;
	let r_new = width as f32 / height as f32;

	let scale = if r_new > r_old {
		height as f32 / input.height() as f32
	}
	else {
		width as f32 / input.width() as f32
	};

	let width  = input.width() as f32 * scale;
	let height = input.height() as f32 * scale;

	resize::<A, _, PI, CI, PO, CO>(input, width as u32, height as u32)
}

#[cfg(test)]
mod test {
	use super::*;
	use processing::scaler::Nearest;
	use buffer;
	use color::Rgb;

	#[test]
	fn nearest() {
		let mut buffer = buffer::Rgb::new(2, 2);

		buffer.set(0, 0, &Rgb::new(1.0, 0.0, 0.0));
		buffer.set(1, 0, &Rgb::new(0.0, 1.0, 0.0));
		buffer.set(0, 1, &Rgb::new(0.0, 0.0, 1.0));
		buffer.set(1, 1, &Rgb::new(1.0, 0.0, 1.0));

		let result = buffer.resize::<Nearest>(4, 4);

		assert_eq!(Rgb::new(1.0, 0.0, 0.0), result.get(0, 0));
		assert_eq!(Rgb::new(1.0, 0.0, 0.0), result.get(1, 0));
		assert_eq!(Rgb::new(1.0, 0.0, 0.0), result.get(0, 1));
		assert_eq!(Rgb::new(1.0, 0.0, 0.0), result.get(1, 1));

		assert_eq!(Rgb::new(0.0, 1.0, 0.0), result.get(2, 0));
		assert_eq!(Rgb::new(0.0, 1.0, 0.0), result.get(3, 0));
		assert_eq!(Rgb::new(0.0, 1.0, 0.0), result.get(2, 1));
		assert_eq!(Rgb::new(0.0, 1.0, 0.0), result.get(3, 1));

		assert_eq!(Rgb::new(0.0, 0.0, 1.0), result.get(0, 2));
		assert_eq!(Rgb::new(0.0, 0.0, 1.0), result.get(1, 2));
		assert_eq!(Rgb::new(0.0, 0.0, 1.0), result.get(0, 3));
		assert_eq!(Rgb::new(0.0, 0.0, 1.0), result.get(1, 3));

		assert_eq!(Rgb::new(1.0, 0.0, 1.0), result.get(2, 2));
		assert_eq!(Rgb::new(1.0, 0.0, 1.0), result.get(3, 2));
		assert_eq!(Rgb::new(1.0, 0.0, 1.0), result.get(2, 3));
		assert_eq!(Rgb::new(1.0, 0.0, 1.0), result.get(3, 3));
	}
}