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
//!  A module for interpolating pixels.

// from rust
use std::cmp;

// from external crate

// from local crate
use error::RasterResult;
use Image;
use Color;

/// An enum for the various modes that can be used for interpolation.
#[derive(Debug)]
pub enum InterpolationMode {
    Bilinear,
    Bicubic,
    Nearest,
}

/// Resample an image into a new size using a given interpolation method.
pub fn resample(
    src: &mut Image,
    w: i32,
    h: i32,
    interpolation: InterpolationMode,
) -> RasterResult<()> {
    match interpolation {
        InterpolationMode::Bilinear => bilinear(src, w, h),
        InterpolationMode::Bicubic => bilinear(src, w, h), // TODO: bicubic
        InterpolationMode::Nearest => nearest(src, w, h),
    }
}

/// Interpolate using nearest neighbor.
pub fn nearest(src: &mut Image, w: i32, h: i32) -> RasterResult<()> {
    let x_ratio: f64 = src.width as f64 / w as f64;
    let y_ratio: f64 = src.height as f64 / h as f64;

    let mut dest = Image::blank(w, h);
    for y in 0..h {
        for x in 0..w {
            let px: i32 = (x as f64 * x_ratio).floor() as i32;
            let py: i32 = (y as f64 * y_ratio).floor() as i32;
            let pixel = src.get_pixel(px, py)?;

            dest.set_pixel(x, y, &pixel)?;
        }
    }
    src.width = dest.width;
    src.height = dest.height;
    src.bytes = dest.bytes;

    Ok(())
}

/// Interpolate using linear function.
pub fn bilinear(src: &mut Image, w2: i32, h2: i32) -> RasterResult<()> {
    bilinear_width(src, w2).and_then(|_| bilinear_height(src, h2))
}

// Private functions

/// Interpolate the width using linear function.
fn bilinear_width(src: &mut Image, w2: i32) -> RasterResult<()> {
    let w1 = src.width;
    let h1 = src.height;

    let x_ratio: f64 = w1 as f64 / w2 as f64;

    let mut dest = Image::blank(w2, h1);

    let offset_x = (w2 / w1 / 2) as i32;

    let x_start = 0 - offset_x;
    let x_end = w2 - offset_x;

    for y in 0..h1 {
        for x in x_start..x_end {
            let src_x = {
                let src_x = x as f64 * x_ratio;
                if src_x < 0.0 {
                    0.0 // limit lower bound to 0
                } else {
                    src_x
                }
            };

            let src_x_int = (src_x).floor() as i32;

            let src_x_int2 = cmp::min(src_x_int + 1, w1 - 1); // limit range within $w1-1

            // limit range from 0 - 1
            let t_x = src_x - src_x_int as f64;

            let src_color1 = src.get_pixel(src_x_int, y)?;
            let src_color2 = src.get_pixel(src_x_int2, y)?;

            // red
            let red = _lerp(src_color1.r, src_color2.r, t_x);

            // green
            let green = _lerp(src_color1.g, src_color2.g, t_x);

            // blue
            let blue = _lerp(src_color1.b, src_color2.b, t_x);

            // alpha
            let alpha = _lerp(src_color1.a, src_color2.a, t_x);

            dest.set_pixel(x + offset_x, y, &Color::rgba(red, green, blue, alpha))?;
        }
    }
    src.width = dest.width;
    src.height = dest.height;
    src.bytes = dest.bytes;

    Ok(())
}

/// Interpolate the height using linear function.
fn bilinear_height(src: &mut Image, h2: i32) -> RasterResult<()> {
    let w1 = src.width;
    let h1 = src.height;

    let y_ratio: f64 = h1 as f64 / h2 as f64;

    let mut dest = Image::blank(w1, h2);

    let offset_y = (h2 / h1 / 2) as i32;

    let y_start = 0 - offset_y;
    let y_end = h2 - offset_y;

    for x in 0..w1 {
        for y in y_start..y_end {
            let src_y = {
                let src_y = y as f64 * y_ratio;
                if src_y < 0.0 {
                    0.0 // limit lower bound to 0
                } else {
                    src_y
                }
            };

            let src_y_int = (src_y).floor() as i32;

            let src_y_int2 = cmp::min(src_y_int + 1, h1 - 1); // limit range within $h1-1

            // limit range from 0 - 1
            let t_y = src_y - src_y_int as f64;

            let src_color1 = src.get_pixel(x, src_y_int)?;
            let src_color2 = src.get_pixel(x, src_y_int2)?;

            // red
            let red = _lerp(src_color1.r, src_color2.r, t_y);

            // green
            let green = _lerp(src_color1.g, src_color2.g, t_y);

            // blue
            let blue = _lerp(src_color1.b, src_color2.b, t_y);

            // alpha
            let alpha = _lerp(src_color1.a, src_color2.a, t_y);

            dest.set_pixel(x, y + offset_y, &Color::rgba(red, green, blue, alpha))?;
        }
    }
    src.width = dest.width;
    src.height = dest.height;
    src.bytes = dest.bytes;

    Ok(())
}

// Simple linear function
fn _lerp(a: u8, b: u8, t: f64) -> u8 {
    let a = a as f64;
    let b = b as f64;

    (a + (t * (b - a))) as u8
}

// Linear function using difference
fn _bilinear(a: u8, b: u8, c: u8, d: u8, x_diff: f64, y_diff: f64) -> u8 {
    // Y = A(1-w)(1-h) + B(w)(1-h) + C(h)(1-w) + Dwh
    (a as f64 * (1.0 - x_diff) * (1.0 - y_diff) + b as f64 * (x_diff) * (1.0 - y_diff)
        + c as f64 * (y_diff) * (1.0 - x_diff) + d as f64 * (x_diff * y_diff)) as u8
}