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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use super::*;
use context::Context;
use std::os::raw::c_void;
use std::marker::PhantomData;
pub struct Transform<InputPixelFormat, OutputPixelFormat, Context = GlobalContext, Flags = AllowCache> {
pub(crate) handle: ffi::HTRANSFORM,
_from: PhantomData<InputPixelFormat>,
_to: PhantomData<OutputPixelFormat>,
_context_ref: PhantomData<Context>,
_flags_ref: PhantomData<Flags>,
}
unsafe impl<'a, F, T, C: Send, Z> Send for Transform<F, T, C, Z> {}
unsafe impl<'a, F, T, C: Send> Sync for Transform<F, T, C, DisallowCache> {}
impl<InputPixelFormat: Copy + Clone, OutputPixelFormat: Copy + Clone> Transform<InputPixelFormat, OutputPixelFormat, GlobalContext, AllowCache> {
pub fn new(input: &Profile,
in_format: PixelFormat,
output: &Profile,
out_format: PixelFormat,
intent: Intent) -> LCMSResult<Self> {
Self::new_flags(input, in_format, output, out_format, intent, Flags::default())
}
pub fn new_flags<Fl: CacheFlag>(input: &Profile,
in_format: PixelFormat,
output: &Profile,
out_format: PixelFormat,
intent: Intent,
flags: Flags<Fl>)
-> LCMSResult<Self> {
Self::new_flags_context(GlobalContext::new(), input, in_format, output, out_format, intent, flags.allow_cache())
}
pub fn new_proofing(input: &Profile, in_format: PixelFormat,
output: &Profile, out_format: PixelFormat,
proofing: &Profile, intent: Intent, proofng_intent: Intent,
flags: Flags)
-> LCMSResult<Self> {
Self::new_proofing_context(GlobalContext::new(), input, in_format, output, out_format, proofing, intent, proofng_intent, flags)
}
pub fn new_multiprofile(profiles: &[&Profile], in_format: PixelFormat, out_format: PixelFormat, intent: Intent, flags: Flags) -> LCMSResult<Self> {
Self::new_multiprofile_context(GlobalContext::new(), profiles, in_format, out_format, intent, flags)
}
}
impl<PixelFormat: Copy + Clone, Ctx: Context, C> Transform<PixelFormat, PixelFormat, Ctx, C> {
pub fn transform_in_place(&self, srcdst: &mut [PixelFormat]) {
let size = srcdst.len();
assert!(size < std::u32::MAX as usize);
unsafe {
ffi::cmsDoTransform(self.handle,
srcdst.as_ptr() as *const c_void,
srcdst.as_ptr() as *mut c_void,
size as u32);
}
}
}
impl<InputPixelFormat: Copy + Clone, OutputPixelFormat: Copy + Clone, Ctx: Context> Transform<InputPixelFormat, OutputPixelFormat, Ctx, AllowCache> {
pub fn new_context(context: Ctx, input: &Profile<Ctx>, in_format: PixelFormat,
output: &Profile<Ctx>, out_format: PixelFormat, intent: Intent) -> LCMSResult<Self> {
Self::new_flags_context(context, input, in_format, output, out_format, intent, Flags::default())
}
}
impl<InputPixelFormat: Copy + Clone, OutputPixelFormat: Copy + Clone, Ctx: Context, Fl: CacheFlag> Transform<InputPixelFormat, OutputPixelFormat, Ctx, Fl> {
fn new_handle(handle: ffi::HTRANSFORM, in_format: PixelFormat, out_format: PixelFormat) -> LCMSResult<Self> {
if handle.is_null() {
Err(Error::ObjectCreationError)
} else {
Ok(Transform {
handle: handle,
_from: Self::check_format::<InputPixelFormat>(in_format, true),
_to: Self::check_format::<OutputPixelFormat>(out_format, false),
_context_ref: PhantomData,
_flags_ref: PhantomData,
})
}
}
fn check_format<Z>(format: PixelFormat, input: bool) -> PhantomData<Z> {
assert!(!format.planar(), "Planar not supported");
assert_eq!(format.bytes_per_pixel(),
std::mem::size_of::<Z>(),
"PixelFormat {:?} has {} bytes per pixel, but the {} format has {}",
format,
format.bytes_per_pixel(),
if input {"input"} else {"output"},
std::mem::size_of::<Z>());
PhantomData
}
pub fn transform_pixels(&self, src: &[InputPixelFormat], dst: &mut [OutputPixelFormat]) {
let size = src.len();
assert_eq!(size, dst.len());
assert!(size < std::u32::MAX as usize);
unsafe {
ffi::cmsDoTransform(self.handle,
src.as_ptr() as *const c_void,
dst.as_ptr() as *mut c_void,
size as u32);
}
}
pub fn input_format(&self) -> PixelFormat {
unsafe { ffi::cmsGetTransformInputFormat(self.handle) as PixelFormat }
}
pub fn output_format(&self) -> PixelFormat {
unsafe { ffi::cmsGetTransformOutputFormat(self.handle) as PixelFormat }
}
pub fn new_flags_context(context: Ctx, input: &Profile<Ctx>, in_format: PixelFormat,
output: &Profile<Ctx>, out_format: PixelFormat,
intent: Intent, flags: Flags<Fl>)
-> LCMSResult<Self> {
Self::new_handle(unsafe {
ffi::cmsCreateTransformTHR(context.as_ptr(),
input.handle, in_format,
output.handle, out_format,
intent, flags.bits())
},
in_format, out_format)
}
pub fn new_proofing_context(context: Ctx, input: &Profile<Ctx>, in_format: PixelFormat,
output: &Profile<Ctx>, out_format: PixelFormat,
proofing: &Profile<Ctx>, intent: Intent, proofng_intent: Intent,
flags: Flags<Fl>)
-> LCMSResult<Self> {
Self::new_handle(unsafe {
ffi::cmsCreateProofingTransformTHR(context.as_ptr(), input.handle, in_format,
output.handle, out_format,
proofing.handle, intent, proofng_intent, flags.bits())
},
in_format, out_format)
}
fn new_multiprofile_context(context: Ctx, profiles: &[&Profile],
in_format: PixelFormat, out_format: PixelFormat, intent: Intent, flags: Flags<Fl>) -> LCMSResult<Self> {
let mut handles: Vec<_> = profiles.iter().map(|p| p.handle).collect();
unsafe {
Self::new_handle(
ffi::cmsCreateMultiprofileTransformTHR(context.as_ptr(), handles.as_mut_ptr(), handles.len() as u32, in_format, out_format, intent, flags.bits()),
in_format,
out_format,
)
}
}
}
impl<InputPixelFormat: Copy + Clone, OutputPixelFormat: Copy + Clone, C> Transform<InputPixelFormat, OutputPixelFormat, GlobalContext, C> {
pub fn global_adaptation_state() -> f64 {
unsafe { ffi::cmsSetAdaptationState(-1.) }
}
#[deprecated(note = "Use `ThreadContext::set_adaptation_state()`")]
pub fn set_global_adaptation_state(value: f64) {
unsafe {
ffi::cmsSetAdaptationState(value);
}
}
#[deprecated(note = "Use `ThreadContext::set_alarm_codes()`")]
pub fn set_global_alarm_codes(codes: [u16; ffi::MAXCHANNELS]) {
unsafe { ffi::cmsSetAlarmCodes(codes.as_ptr()) }
}
pub fn global_alarm_codes() -> [u16; ffi::MAXCHANNELS] {
let mut tmp = [0u16; ffi::MAXCHANNELS];
unsafe {
ffi::cmsGetAlarmCodes(tmp.as_mut_ptr());
}
tmp
}
}
impl<F, T, C, L> Drop for Transform<F, T, C, L> {
fn drop(&mut self) {
unsafe {
ffi::cmsDeleteTransform(self.handle);
}
}
}