Coverage Report

Created: 2026-12-01 08:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pic-scale-safe-0.1.12/src/trc_handler.rs
Line
Count
Source
1
/*
2
 * Copyright (c) Radzivon Bartoshyk, 10/2024. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without modification,
5
 * are permitted provided that the following conditions are met:
6
 *
7
 * 1.  Redistributions of source code must retain the above copyright notice, this
8
 * list of conditions and the following disclaimer.
9
 *
10
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
11
 * this list of conditions and the following disclaimer in the documentation
12
 * and/or other materials provided with the distribution.
13
 *
14
 * 3.  Neither the name of the copyright holder nor the names of its
15
 * contributors may be used to endorse or promote products derived from
16
 * this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
use crate::TransferFunction;
30
#[cfg(feature = "rayon")]
31
use rayon::iter::ParallelIterator;
32
#[cfg(feature = "rayon")]
33
use rayon::prelude::ParallelSliceMut;
34
35
/// Converts 8-bit image to linear
36
///
37
/// On `CHANNELS` == 2 or `CHANNELS` == 4 alpha will be considered as last item
38
///
39
/// # Arguments
40
///
41
/// * `in_place`: Where convert to
42
/// * `trc` - Transfer function, see [TransferFunction] for more info
43
///
44
0
pub fn image_to_linear<const CHANNELS: usize>(in_place: &mut [u8], trc: TransferFunction) {
45
0
    assert!(CHANNELS != 0 && CHANNELS <= 4, "Channels must be 1..=4");
46
0
    let mut lut_table = [0u8; 256];
47
0
    for (i, item) in lut_table.iter_mut().enumerate() {
48
0
        *item = (trc.linearize(i as f32 * (1. / 255.0)) * 255.).min(255.) as u8;
49
0
    }
50
    let iter;
51
    #[cfg(feature = "rayon")]
52
    {
53
        iter = in_place.par_chunks_exact_mut(CHANNELS);
54
    }
55
    #[cfg(not(feature = "rayon"))]
56
0
    {
57
0
        iter = in_place.chunks_exact_mut(CHANNELS);
58
0
    }
59
0
    iter.for_each(|dst| {
60
0
        if CHANNELS == 1 || CHANNELS == 2 {
61
0
            dst[0] = lut_table[dst[0] as usize];
62
0
        } else if CHANNELS == 3 || CHANNELS == 4 {
63
0
            dst[0] = lut_table[dst[0] as usize];
64
0
            dst[1] = lut_table[dst[1] as usize];
65
0
            dst[2] = lut_table[dst[2] as usize];
66
0
        }
67
0
    });
68
0
}
69
70
/// Converts 8-bit linear image to gamma
71
///
72
/// On `CHANNELS` == 2 or `CHANNELS` == 4 alpha will be considered as last item
73
///
74
/// # Arguments
75
///
76
/// * `in_place`: Where convert to
77
/// * `trc` - Transfer function, see [TransferFunction] for more info
78
///
79
0
pub fn linear_to_gamma_image<const CHANNELS: usize>(in_place: &mut [u8], trc: TransferFunction) {
80
0
    assert!(CHANNELS != 0 && CHANNELS <= 4, "Channels must be 1..=4");
81
0
    let mut lut_table = [0u8; 256];
82
0
    for (i, item) in lut_table.iter_mut().enumerate() {
83
0
        *item = (trc.gamma(i as f32 * (1. / 255.0)) * 255.).min(255.) as u8;
84
0
    }
85
    let iter;
86
    #[cfg(feature = "rayon")]
87
    {
88
        iter = in_place.par_chunks_exact_mut(CHANNELS);
89
    }
90
    #[cfg(not(feature = "rayon"))]
91
0
    {
92
0
        iter = in_place.chunks_exact_mut(CHANNELS);
93
0
    }
94
0
    iter.for_each(|dst| {
95
0
        if CHANNELS == 1 || CHANNELS == 2 {
96
0
            dst[0] = lut_table[dst[0] as usize];
97
0
        } else if CHANNELS == 3 || CHANNELS == 4 {
98
0
            dst[0] = lut_table[dst[0] as usize];
99
0
            dst[1] = lut_table[dst[1] as usize];
100
0
            dst[2] = lut_table[dst[2] as usize];
101
0
        }
102
0
    });
103
0
}
104
105
/// Converts 8-16-bit image to linear
106
///
107
/// On `CHANNELS` == 2 or `CHANNELS` == 4 alpha will be considered as last item
108
///
109
/// # Arguments
110
///
111
/// * `in_place`: Where convert to
112
/// * `trc` - Transfer function, see [TransferFunction] for more info
113
///
114
0
pub fn image16_to_linear16<const CHANNELS: usize>(
115
0
    in_place: &mut [u16],
116
0
    bit_depth: u32,
117
0
    trc: TransferFunction,
118
0
) {
119
0
    assert!(CHANNELS != 0 && CHANNELS <= 4, "Channels must be 1..=4");
120
0
    assert!((1..=16).contains(&bit_depth), "Bit depth must be 1..=16");
121
0
    let max_colors = (1 << bit_depth) - 1;
122
0
    let mut lut_table = vec![0u16; max_colors + 1];
123
0
    for (i, item) in lut_table.iter_mut().enumerate() {
124
0
        *item = (trc.linearize(i as f32 * (1. / max_colors as f32)) * max_colors as f32)
125
0
            .min(max_colors as f32) as u16;
126
0
    }
127
    let iter;
128
    #[cfg(feature = "rayon")]
129
    {
130
        iter = in_place.par_chunks_exact_mut(CHANNELS);
131
    }
132
    #[cfg(not(feature = "rayon"))]
133
0
    {
134
0
        iter = in_place.chunks_exact_mut(CHANNELS);
135
0
    }
136
0
    iter.for_each(|dst| {
137
0
        if CHANNELS == 1 || CHANNELS == 2 {
138
0
            dst[0] = lut_table[dst[0] as usize];
139
0
        } else if CHANNELS == 3 || CHANNELS == 4 {
140
0
            dst[0] = lut_table[dst[0] as usize];
141
0
            dst[1] = lut_table[dst[1] as usize];
142
0
            dst[2] = lut_table[dst[2] as usize];
143
0
        }
144
0
    });
145
0
}
146
147
/// Converts 8-16-bit linear image to gamma
148
///
149
/// On `CHANNELS` == 2 or `CHANNELS` == 4 alpha will be considered as last item
150
///
151
/// # Arguments
152
///
153
/// * `in_place`: Where convert to
154
/// * `trc` - Transfer function, see [TransferFunction] for more info
155
///
156
0
pub fn linear16_to_gamma_image16<const CHANNELS: usize>(
157
0
    in_place: &mut [u16],
158
0
    bit_depth: u32,
159
0
    trc: TransferFunction,
160
0
) {
161
0
    assert!(CHANNELS != 0 && CHANNELS <= 4, "Channels must be 1..=4");
162
0
    assert!((1..=16).contains(&bit_depth), "Bit depth must be 1..=16");
163
0
    let max_colors = (1 << bit_depth) - 1;
164
0
    let mut lut_table = vec![0u16; max_colors + 1];
165
0
    for (i, item) in lut_table.iter_mut().enumerate() {
166
0
        *item = (trc.gamma(i as f32 * (1. / max_colors as f32)) * max_colors as f32)
167
0
            .min(max_colors as f32) as u16;
168
0
    }
169
    let iter;
170
    #[cfg(feature = "rayon")]
171
    {
172
        iter = in_place.par_chunks_exact_mut(CHANNELS);
173
    }
174
    #[cfg(not(feature = "rayon"))]
175
0
    {
176
0
        iter = in_place.chunks_exact_mut(CHANNELS);
177
0
    }
178
0
    iter.for_each(|dst| {
179
0
        if CHANNELS == 1 || CHANNELS == 2 {
180
0
            dst[0] = lut_table[dst[0] as usize];
181
0
        } else if CHANNELS == 3 || CHANNELS == 4 {
182
0
            dst[0] = lut_table[dst[0] as usize];
183
0
            dst[1] = lut_table[dst[1] as usize];
184
0
            dst[2] = lut_table[dst[2] as usize];
185
0
        }
186
0
    });
187
0
}
188
189
/// Converts `f32` image to linear
190
///
191
/// On `CHANNELS` == 2 or `CHANNELS` == 4 alpha will be considered as last item
192
///
193
/// # Arguments
194
///
195
/// * `in_place`: Where convert to
196
/// * `trc` - Transfer function, see [TransferFunction] for more info
197
///
198
0
pub fn image_f32_to_linear_f32<const CHANNELS: usize>(in_place: &mut [f32], trc: TransferFunction) {
199
0
    assert!(CHANNELS != 0 && CHANNELS <= 4, "Channels must be 1..=4");
200
    let iter;
201
    #[cfg(feature = "rayon")]
202
    {
203
        iter = in_place.par_chunks_exact_mut(CHANNELS);
204
    }
205
    #[cfg(not(feature = "rayon"))]
206
0
    {
207
0
        iter = in_place.chunks_exact_mut(CHANNELS);
208
0
    }
209
0
    iter.for_each(|dst| {
210
0
        if CHANNELS == 1 || CHANNELS == 2 {
211
0
            dst[0] = trc.linearize(dst[0]);
212
0
        } else if CHANNELS == 3 || CHANNELS == 4 {
213
0
            dst[0] = trc.linearize(dst[0]);
214
0
            dst[1] = trc.linearize(dst[1]);
215
0
            dst[2] = trc.linearize(dst[2]);
216
0
        }
217
0
    });
218
0
}
219
220
/// Converts `f32` linear image to gamma
221
///
222
/// On `CHANNELS` == 2 or `CHANNELS` == 4 alpha will be considered as last item
223
///
224
/// # Arguments
225
///
226
/// * `in_place`: Where convert to
227
/// * `trc` - Transfer function, see [TransferFunction] for more info
228
///
229
0
pub fn linear_f32_to_gamma_image_f32<const CHANNELS: usize>(
230
0
    in_place: &mut [f32],
231
0
    trc: TransferFunction,
232
0
) {
233
0
    assert!(CHANNELS != 0 && CHANNELS <= 4, "Channels must be 1..=4");
234
    let iter;
235
    #[cfg(feature = "rayon")]
236
    {
237
        iter = in_place.par_chunks_exact_mut(CHANNELS);
238
    }
239
    #[cfg(not(feature = "rayon"))]
240
0
    {
241
0
        iter = in_place.chunks_exact_mut(CHANNELS);
242
0
    }
243
0
    iter.for_each(|dst| {
244
0
        if CHANNELS == 1 || CHANNELS == 2 {
245
0
            dst[0] = trc.gamma(dst[0]);
246
0
        } else if CHANNELS == 3 || CHANNELS == 4 {
247
0
            dst[0] = trc.gamma(dst[0]);
248
0
            dst[1] = trc.gamma(dst[1]);
249
0
            dst[2] = trc.gamma(dst[2]);
250
0
        }
251
0
    });
252
0
}