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
/*
 * // Copyright (c) the Radzivon Bartoshyk. All rights reserved.
 * //
 * // Use of this source code is governed by a BSD-style
 * // license that can be found in the LICENSE file.
 */

use colorutils_rs::{
    linear_u8_to_rgb, linear_u8_to_rgba, rgb_to_linear_u8, rgba_to_linear_u8, TransferFunction,
};

use crate::scaler::Scaling;
use crate::{ImageSize, ImageStore, ResamplingFunction, Scaler, ThreadingPolicy};

#[derive(Debug, Copy, Clone)]
/// Linearize image into u8, scale and then convert it back. It's much faster than scale in f32, however involves some precision loss
pub struct LinearApproxScaler {
    pub(crate) scaler: Scaler,
    pub(crate) transfer_function: TransferFunction,
}

impl LinearApproxScaler {
    /// Creates new instance with sRGB transfer function
    pub fn new(filter: ResamplingFunction) -> Self {
        LinearApproxScaler {
            scaler: Scaler::new(filter),
            transfer_function: TransferFunction::Srgb,
        }
    }

    /// Creates new instance with provided transfer function
    pub fn new_with_transfer(
        filter: ResamplingFunction,
        transfer_function: TransferFunction,
    ) -> Self {
        LinearApproxScaler {
            scaler: Scaler::new(filter),
            transfer_function,
        }
    }
}

impl<'a> Scaling for LinearApproxScaler {
    fn set_threading_policy(&mut self, threading_policy: ThreadingPolicy) {
        self.scaler.threading_policy = threading_policy;
    }

    fn resize_rgb(&self, new_size: ImageSize, store: ImageStore<u8, 3>) -> ImageStore<u8, 3> {
        const CHANNELS: usize = 3;
        let mut linear_store = ImageStore::<u8, CHANNELS>::alloc(store.width, store.height);
        rgb_to_linear_u8(
            store.buffer.borrow(),
            store.width as u32 * CHANNELS as u32,
            linear_store.buffer.borrow_mut(),
            linear_store.width as u32 * CHANNELS as u32,
            linear_store.width as u32,
            linear_store.height as u32,
            self.transfer_function,
        );
        let new_store = self.scaler.resize_rgb(new_size, linear_store);
        let mut gamma_store = ImageStore::<u8, CHANNELS>::alloc(new_store.width, new_store.height);
        let src = new_store.buffer.borrow();
        let gamma_buffer = gamma_store.buffer.borrow_mut();
        linear_u8_to_rgb(
            src,
            new_store.width as u32 * CHANNELS as u32,
            gamma_buffer,
            gamma_store.width as u32 * CHANNELS as u32,
            gamma_store.width as u32,
            gamma_store.height as u32,
            self.transfer_function,
        );
        gamma_store
    }

    fn resize_rgb_f32(&self, new_size: ImageSize, store: ImageStore<f32, 3>) -> ImageStore<f32, 3> {
        self.scaler.resize_rgb_f32(new_size, store)
    }

    fn resize_rgba(
        &self,
        new_size: ImageSize,
        store: ImageStore<u8, 4>,
        is_alpha_premultiplied: bool,
    ) -> ImageStore<u8, 4> {
        const CHANNELS: usize = 4;
        let mut src_store = store;
        if is_alpha_premultiplied {
            let mut premultiplied_store =
                ImageStore::<u8, 4>::alloc(src_store.width, src_store.height);
            src_store.unpremultiply_alpha(&mut premultiplied_store);
            src_store = premultiplied_store;
        }
        let mut linear_store = ImageStore::<u8, CHANNELS>::alloc(src_store.width, src_store.height);
        rgba_to_linear_u8(
            src_store.buffer.borrow(),
            src_store.width as u32 * CHANNELS as u32,
            linear_store.buffer.borrow_mut(),
            linear_store.width as u32 * CHANNELS as u32,
            linear_store.width as u32,
            linear_store.height as u32,
            self.transfer_function,
        );
        let new_store = self.scaler.resize_rgba(new_size, linear_store, false);
        let mut gamma_store = ImageStore::<u8, CHANNELS>::alloc(new_store.width, new_store.height);
        let src = new_store.buffer.borrow();
        let gamma_buffer = gamma_store.buffer.borrow_mut();
        linear_u8_to_rgba(
            src,
            new_store.width as u32 * CHANNELS as u32,
            gamma_buffer,
            gamma_store.width as u32 * CHANNELS as u32,
            gamma_store.width as u32,
            gamma_store.height as u32,
            self.transfer_function,
        );
        if is_alpha_premultiplied {
            let mut premultiplied_store =
                ImageStore::<u8, 4>::alloc(gamma_store.width, gamma_store.height);
            gamma_store.premultiply_alpha(&mut premultiplied_store);
            return premultiplied_store;
        }
        gamma_store
    }

    fn resize_rgba_f32(
        &self,
        new_size: ImageSize,
        store: ImageStore<f32, 4>,
    ) -> ImageStore<f32, 4> {
        self.scaler.resize_rgba_f32(new_size, store)
    }
}