image/codecs/jpeg/
decoder.rs

1use std::io::{self, Cursor, Read};
2use std::marker::PhantomData;
3use std::mem;
4
5use crate::color::ColorType;
6use crate::error::{
7    DecodingError, ImageError, ImageResult, UnsupportedError, UnsupportedErrorKind,
8};
9use crate::image::{ImageDecoder, ImageFormat};
10
11/// JPEG decoder
12pub struct JpegDecoder<R> {
13    decoder: jpeg::Decoder<R>,
14    metadata: jpeg::ImageInfo,
15}
16
17impl<R: Read> JpegDecoder<R> {
18    /// Create a new decoder that decodes from the stream ```r```
19    pub fn new(r: R) -> ImageResult<JpegDecoder<R>> {
20        let mut decoder = jpeg::Decoder::new(r);
21
22        decoder.read_info().map_err(ImageError::from_jpeg)?;
23        let mut metadata = decoder.info().ok_or_else(|| {
24            ImageError::Decoding(DecodingError::from_format_hint(ImageFormat::Jpeg.into()))
25        })?;
26
27        // We convert CMYK data to RGB before returning it to the user.
28        if metadata.pixel_format == jpeg::PixelFormat::CMYK32 {
29            metadata.pixel_format = jpeg::PixelFormat::RGB24;
30        }
31
32        Ok(JpegDecoder { decoder, metadata })
33    }
34
35    /// Configure the decoder to scale the image during decoding.
36    ///
37    /// This efficiently scales the image by the smallest supported
38    /// scale factor that produces an image larger than or equal to
39    /// the requested size in at least one axis. The currently
40    /// implemented scale factors are 1/8, 1/4, 1/2 and 1.
41    ///
42    /// To generate a thumbnail of an exact size, pass the desired
43    /// size and then scale to the final size using a traditional
44    /// resampling algorithm.
45    ///
46    /// The size of the image to be loaded, with the scale factor
47    /// applied, is returned.
48    pub fn scale(
49        &mut self,
50        requested_width: u16,
51        requested_height: u16,
52    ) -> ImageResult<(u16, u16)> {
53        let result = self
54            .decoder
55            .scale(requested_width, requested_height)
56            .map_err(ImageError::from_jpeg)?;
57
58        self.metadata.width = result.0;
59        self.metadata.height = result.1;
60
61        Ok(result)
62    }
63}
64
65/// Wrapper struct around a `Cursor<Vec<u8>>`
66pub struct JpegReader<R>(Cursor<Vec<u8>>, PhantomData<R>);
67impl<R> Read for JpegReader<R> {
68    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
69        self.0.read(buf)
70    }
71    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
72        if self.0.position() == 0 && buf.is_empty() {
73            mem::swap(buf, self.0.get_mut());
74            Ok(buf.len())
75        } else {
76            self.0.read_to_end(buf)
77        }
78    }
79}
80
81impl<'a, R: 'a + Read> ImageDecoder<'a> for JpegDecoder<R> {
82    type Reader = JpegReader<R>;
83
84    fn dimensions(&self) -> (u32, u32) {
85        (
86            u32::from(self.metadata.width),
87            u32::from(self.metadata.height),
88        )
89    }
90
91    fn color_type(&self) -> ColorType {
92        ColorType::from_jpeg(self.metadata.pixel_format)
93    }
94
95    fn icc_profile(&mut self) -> Option<Vec<u8>> {
96        self.decoder.icc_profile()
97    }
98
99    fn into_reader(mut self) -> ImageResult<Self::Reader> {
100        let mut data = self.decoder.decode().map_err(ImageError::from_jpeg)?;
101        data = match self.decoder.info().unwrap().pixel_format {
102            jpeg::PixelFormat::CMYK32 => cmyk_to_rgb(&data),
103            _ => data,
104        };
105
106        Ok(JpegReader(Cursor::new(data), PhantomData))
107    }
108
109    fn read_image(mut self, buf: &mut [u8]) -> ImageResult<()> {
110        assert_eq!(u64::try_from(buf.len()), Ok(self.total_bytes()));
111
112        let mut data = self.decoder.decode().map_err(ImageError::from_jpeg)?;
113        data = match self.decoder.info().unwrap().pixel_format {
114            jpeg::PixelFormat::CMYK32 => cmyk_to_rgb(&data),
115            _ => data,
116        };
117
118        buf.copy_from_slice(&data);
119        Ok(())
120    }
121}
122
123fn cmyk_to_rgb(input: &[u8]) -> Vec<u8> {
124    let count = input.len() / 4;
125    let mut output = vec![0; 3 * count];
126
127    let in_pixels = input[..4 * count].chunks_exact(4);
128    let out_pixels = output[..3 * count].chunks_exact_mut(3);
129
130    for (pixel, outp) in in_pixels.zip(out_pixels) {
131        let c = 255 - u16::from(pixel[0]);
132        let m = 255 - u16::from(pixel[1]);
133        let y = 255 - u16::from(pixel[2]);
134        let k = 255 - u16::from(pixel[3]);
135        // CMY -> RGB
136        let r = (k * c) / 255;
137        let g = (k * m) / 255;
138        let b = (k * y) / 255;
139
140        outp[0] = r as u8;
141        outp[1] = g as u8;
142        outp[2] = b as u8;
143    }
144
145    output
146}
147
148impl ColorType {
149    fn from_jpeg(pixel_format: jpeg::PixelFormat) -> ColorType {
150        use jpeg::PixelFormat::*;
151        match pixel_format {
152            L8 => ColorType::L8,
153            L16 => ColorType::L16,
154            RGB24 => ColorType::Rgb8,
155            CMYK32 => panic!(),
156        }
157    }
158}
159
160impl ImageError {
161    fn from_jpeg(err: jpeg::Error) -> ImageError {
162        use jpeg::Error::*;
163        match err {
164            err @ Format(_) => {
165                ImageError::Decoding(DecodingError::new(ImageFormat::Jpeg.into(), err))
166            }
167            Unsupported(desc) => ImageError::Unsupported(UnsupportedError::from_format_and_kind(
168                ImageFormat::Jpeg.into(),
169                UnsupportedErrorKind::GenericFeature(format!("{:?}", desc)),
170            )),
171            Io(err) => ImageError::IoError(err),
172            Internal(err) => {
173                ImageError::Decoding(DecodingError::new(ImageFormat::Jpeg.into(), err))
174            }
175        }
176    }
177}
178
179#[cfg(test)]
180mod tests {
181    #[cfg(feature = "benchmarks")]
182    extern crate test;
183
184    use super::cmyk_to_rgb;
185    #[cfg(feature = "benchmarks")]
186    use test::Bencher;
187
188    #[cfg(feature = "benchmarks")]
189    const W: usize = 256;
190    #[cfg(feature = "benchmarks")]
191    const H: usize = 256;
192
193    #[test]
194    fn cmyk_to_rgb_correct() {
195        for c in 0..=255 {
196            for k in 0..=255 {
197                // Based on R = 255 * (1-C/255) * (1-K/255)
198                let r = (255.0 - f32::from(c)) * (255.0 - f32::from(k)) / 255.0;
199                let r_u8 = r as u8;
200                let convert_r = cmyk_to_rgb(&[c, 0, 0, k])[0];
201                let convert_g = cmyk_to_rgb(&[0, c, 0, k])[1];
202                let convert_b = cmyk_to_rgb(&[0, 0, c, k])[2];
203
204                assert_eq!(
205                    convert_r, r_u8,
206                    "c = {}, k = {}, cymk_to_rgb[0] = {}, should be {}",
207                    c, k, convert_r, r_u8
208                );
209                assert_eq!(
210                    convert_g, r_u8,
211                    "m = {}, k = {}, cymk_to_rgb[1] = {}, should be {}",
212                    c, k, convert_g, r_u8
213                );
214                assert_eq!(
215                    convert_b, r_u8,
216                    "y = {}, k = {}, cymk_to_rgb[2] = {}, should be {}",
217                    c, k, convert_b, r_u8
218                );
219            }
220        }
221    }
222
223    fn single_pix_correct(cmyk_pix: [u8; 4], rgb_pix_true: [u8; 3]) {
224        let rgb_pix = cmyk_to_rgb(&cmyk_pix);
225        assert_eq!(
226            rgb_pix_true[0], rgb_pix[0],
227            "With CMYK {:?} expected {:?}, got {:?}",
228            cmyk_pix, rgb_pix_true, rgb_pix
229        );
230        assert_eq!(
231            rgb_pix_true[1], rgb_pix[1],
232            "With CMYK {:?} expected {:?}, got {:?}",
233            cmyk_pix, rgb_pix_true, rgb_pix
234        );
235        assert_eq!(
236            rgb_pix_true[2], rgb_pix[2],
237            "With CMYK {:?} expected {:?}, got {:?}",
238            cmyk_pix, rgb_pix_true, rgb_pix
239        );
240    }
241
242    #[test]
243    fn test_assorted_colors() {
244        let cmyk_pixels = vec![
245            [0, 51, 102, 65],
246            [153, 204, 0, 65],
247            [0, 0, 0, 67],
248            [0, 85, 170, 69],
249            [0, 0, 0, 71],
250            [0, 0, 0, 73],
251            [0, 17, 34, 75],
252            [51, 68, 85, 75],
253            [102, 119, 136, 75],
254            [153, 170, 187, 75],
255            [204, 221, 238, 75],
256            [0, 0, 0, 77],
257            [0, 0, 0, 79],
258            [0, 85, 170, 81],
259            [0, 0, 0, 83],
260            [0, 3, 6, 85],
261            [9, 12, 15, 85],
262            [18, 21, 24, 85],
263            [27, 30, 33, 85],
264            [36, 39, 42, 85],
265            [45, 48, 51, 85],
266            [54, 57, 60, 85],
267            [63, 66, 69, 85],
268            [72, 75, 78, 85],
269            [81, 84, 87, 85],
270            [90, 93, 96, 85],
271            [99, 102, 105, 85],
272            [108, 111, 114, 85],
273            [117, 120, 123, 85],
274            [126, 129, 132, 85],
275            [135, 138, 141, 85],
276            [144, 147, 150, 85],
277            [153, 156, 159, 85],
278            [162, 165, 168, 85],
279            [171, 174, 177, 85],
280            [180, 183, 186, 85],
281            [189, 192, 195, 85],
282            [198, 201, 204, 85],
283            [207, 210, 213, 85],
284            [216, 219, 222, 85],
285            [225, 228, 231, 85],
286            [234, 237, 240, 85],
287            [243, 246, 249, 85],
288            [252, 0, 0, 85],
289            [0, 85, 170, 87],
290            [0, 0, 0, 89],
291            [0, 0, 0, 91],
292            [0, 85, 170, 93],
293            [0, 51, 102, 95],
294            [153, 204, 0, 95],
295            [0, 0, 0, 97],
296            [0, 85, 170, 99],
297            [0, 0, 0, 101],
298            [0, 0, 0, 103],
299            [0, 17, 34, 105],
300            [51, 68, 85, 105],
301            [102, 119, 136, 105],
302            [153, 170, 187, 105],
303            [204, 221, 238, 105],
304            [0, 0, 0, 107],
305            [0, 0, 0, 109],
306            [0, 85, 170, 111],
307            [0, 0, 0, 113],
308            [0, 51, 102, 115],
309            [153, 204, 0, 115],
310            [0, 85, 170, 117],
311            [0, 15, 30, 119],
312            [45, 60, 75, 119],
313            [90, 105, 120, 119],
314            [135, 150, 165, 119],
315            [180, 195, 210, 119],
316            [225, 240, 0, 119],
317            [0, 0, 0, 121],
318            [0, 85, 170, 123],
319            [0, 51, 102, 125],
320            [153, 204, 0, 125],
321            [0, 0, 0, 127],
322            [0, 0, 0, 128],
323            [0, 85, 170, 129],
324            [0, 51, 102, 130],
325            [153, 204, 0, 130],
326            [0, 0, 0, 131],
327            [0, 85, 170, 132],
328            [0, 0, 0, 133],
329            [0, 0, 0, 134],
330            [0, 17, 34, 135],
331            [51, 68, 85, 135],
332            [102, 119, 136, 135],
333            [153, 170, 187, 135],
334            [204, 221, 238, 135],
335            [0, 15, 30, 136],
336            [45, 60, 75, 136],
337            [90, 105, 120, 136],
338            [135, 150, 165, 136],
339            [180, 195, 210, 136],
340            [225, 240, 0, 136],
341            [0, 0, 0, 137],
342            [0, 85, 170, 138],
343            [0, 0, 0, 139],
344            [0, 51, 102, 140],
345            [153, 204, 0, 140],
346            [0, 85, 170, 141],
347            [0, 0, 0, 142],
348            [0, 0, 0, 143],
349            [0, 85, 170, 144],
350            [0, 51, 102, 145],
351            [153, 204, 0, 145],
352            [0, 0, 0, 146],
353            [0, 85, 170, 147],
354            [0, 0, 0, 148],
355            [0, 0, 0, 149],
356            [0, 17, 34, 150],
357            [51, 68, 85, 150],
358            [102, 119, 136, 150],
359            [153, 170, 187, 150],
360            [204, 221, 238, 150],
361            [0, 0, 0, 151],
362            [0, 0, 0, 152],
363            [0, 5, 10, 153],
364            [15, 20, 25, 153],
365            [30, 35, 40, 153],
366            [45, 50, 55, 153],
367            [60, 65, 70, 153],
368            [75, 80, 85, 153],
369            [90, 95, 100, 153],
370            [105, 110, 115, 153],
371            [120, 125, 130, 153],
372            [135, 140, 145, 153],
373            [150, 155, 160, 153],
374            [165, 170, 175, 153],
375            [180, 185, 190, 153],
376            [195, 200, 205, 153],
377            [210, 215, 220, 153],
378            [225, 230, 235, 153],
379            [240, 245, 250, 153],
380            [0, 0, 0, 154],
381            [0, 51, 102, 155],
382            [153, 204, 0, 155],
383            [0, 85, 170, 156],
384            [0, 0, 0, 157],
385            [0, 0, 0, 158],
386            [0, 85, 170, 159],
387            [0, 51, 102, 160],
388            [153, 204, 0, 160],
389            [0, 0, 0, 161],
390            [0, 85, 170, 162],
391            [0, 0, 0, 163],
392            [0, 0, 0, 164],
393            [0, 17, 34, 165],
394            [51, 68, 85, 165],
395            [102, 119, 136, 165],
396            [153, 170, 187, 165],
397            [204, 221, 238, 165],
398            [0, 0, 0, 166],
399            [0, 0, 0, 167],
400            [0, 85, 170, 168],
401            [0, 0, 0, 169],
402            [0, 3, 6, 170],
403            [9, 12, 15, 170],
404            [18, 21, 24, 170],
405            [27, 30, 33, 170],
406            [36, 39, 42, 170],
407            [45, 48, 51, 170],
408            [54, 57, 60, 170],
409            [63, 66, 69, 170],
410            [72, 75, 78, 170],
411            [81, 84, 87, 170],
412            [90, 93, 96, 170],
413            [99, 102, 105, 170],
414            [108, 111, 114, 170],
415            [117, 120, 123, 170],
416            [126, 129, 132, 170],
417            [135, 138, 141, 170],
418            [144, 147, 150, 170],
419            [153, 156, 159, 170],
420            [162, 165, 168, 170],
421            [171, 174, 177, 170],
422            [180, 183, 186, 170],
423            [189, 192, 195, 170],
424            [198, 201, 204, 170],
425            [207, 210, 213, 170],
426            [216, 219, 222, 170],
427            [225, 228, 231, 170],
428            [234, 237, 240, 170],
429            [243, 246, 249, 170],
430            [252, 0, 0, 170],
431            [0, 85, 170, 171],
432            [0, 0, 0, 172],
433            [0, 0, 0, 173],
434            [0, 85, 170, 174],
435            [0, 51, 102, 175],
436            [153, 204, 0, 175],
437            [0, 0, 0, 176],
438            [0, 85, 170, 177],
439            [0, 0, 0, 178],
440            [0, 0, 0, 179],
441            [0, 17, 34, 180],
442            [51, 68, 85, 180],
443            [102, 119, 136, 180],
444            [153, 170, 187, 180],
445            [204, 221, 238, 180],
446            [0, 0, 0, 181],
447            [0, 0, 0, 182],
448            [0, 85, 170, 183],
449            [0, 0, 0, 184],
450            [0, 51, 102, 185],
451            [153, 204, 0, 185],
452            [0, 85, 170, 186],
453            [0, 15, 30, 187],
454            [45, 60, 75, 187],
455            [90, 105, 120, 187],
456            [135, 150, 165, 187],
457            [180, 195, 210, 187],
458            [225, 240, 0, 187],
459            [0, 0, 0, 188],
460            [0, 85, 170, 189],
461            [0, 51, 102, 190],
462            [153, 204, 0, 190],
463            [0, 0, 0, 191],
464            [0, 85, 170, 192],
465            [0, 0, 0, 193],
466            [0, 0, 0, 194],
467            [0, 17, 34, 195],
468            [51, 68, 85, 195],
469            [102, 119, 136, 195],
470            [153, 170, 187, 195],
471            [204, 221, 238, 195],
472            [0, 0, 0, 196],
473            [0, 0, 0, 197],
474            [0, 85, 170, 198],
475            [0, 0, 0, 199],
476            [0, 51, 102, 200],
477            [153, 204, 0, 200],
478            [0, 85, 170, 201],
479            [0, 0, 0, 202],
480            [0, 0, 0, 203],
481            [0, 5, 10, 204],
482            [15, 20, 25, 204],
483            [30, 35, 40, 204],
484            [45, 50, 55, 204],
485            [60, 65, 70, 204],
486            [75, 80, 85, 204],
487            [90, 95, 100, 204],
488            [105, 110, 115, 204],
489            [120, 125, 130, 204],
490            [135, 140, 145, 204],
491            [150, 155, 160, 204],
492            [165, 170, 175, 204],
493            [180, 185, 190, 204],
494            [195, 200, 205, 204],
495            [210, 215, 220, 204],
496            [225, 230, 235, 204],
497            [240, 245, 250, 204],
498            [0, 51, 102, 205],
499            [153, 204, 0, 205],
500            [0, 0, 0, 206],
501            [0, 85, 170, 207],
502            [0, 0, 0, 208],
503            [0, 0, 0, 209],
504            [0, 17, 34, 210],
505            [51, 68, 85, 210],
506            [102, 119, 136, 210],
507            [153, 170, 187, 210],
508            [204, 221, 238, 210],
509            [0, 0, 0, 211],
510            [0, 0, 0, 212],
511            [0, 85, 170, 213],
512            [0, 0, 0, 214],
513            [0, 51, 102, 215],
514            [153, 204, 0, 215],
515            [0, 85, 170, 216],
516            [0, 0, 0, 217],
517            [0, 0, 0, 218],
518            [0, 85, 170, 219],
519            [0, 51, 102, 220],
520            [153, 204, 0, 220],
521            [0, 15, 30, 221],
522            [45, 60, 75, 221],
523            [90, 105, 120, 221],
524            [135, 150, 165, 221],
525            [180, 195, 210, 221],
526            [225, 240, 0, 221],
527            [0, 85, 170, 222],
528            [0, 0, 0, 223],
529            [0, 0, 0, 224],
530            [0, 17, 34, 225],
531            [51, 68, 85, 225],
532            [102, 119, 136, 225],
533            [153, 170, 187, 225],
534            [204, 221, 238, 225],
535            [0, 0, 0, 226],
536            [0, 0, 0, 227],
537            [0, 85, 170, 228],
538            [0, 0, 0, 229],
539            [0, 51, 102, 230],
540            [153, 204, 0, 230],
541            [0, 85, 170, 231],
542            [0, 0, 0, 232],
543            [0, 0, 0, 233],
544            [0, 85, 170, 234],
545            [0, 51, 102, 235],
546            [153, 204, 0, 235],
547            [0, 0, 0, 236],
548            [0, 85, 170, 237],
549            [0, 15, 30, 238],
550            [45, 60, 75, 238],
551            [90, 105, 120, 238],
552            [135, 150, 165, 238],
553            [180, 195, 210, 238],
554            [225, 240, 0, 238],
555            [0, 0, 0, 239],
556            [0, 17, 34, 240],
557            [51, 68, 85, 240],
558            [102, 119, 136, 240],
559            [153, 170, 187, 240],
560            [204, 221, 238, 240],
561            [0, 0, 0, 241],
562            [0, 0, 0, 242],
563            [0, 85, 170, 243],
564            [0, 0, 0, 244],
565            [0, 51, 102, 245],
566            [153, 204, 0, 245],
567            [0, 85, 170, 246],
568            [0, 0, 0, 247],
569            [0, 0, 0, 248],
570            [0, 85, 170, 249],
571            [0, 51, 102, 250],
572            [153, 204, 0, 250],
573            [0, 0, 0, 251],
574            [0, 85, 170, 252],
575            [0, 0, 0, 253],
576            [0, 0, 0, 254],
577            [5, 15, 25, 102],
578            [35, 40, 45, 102],
579            [50, 55, 60, 102],
580            [65, 70, 75, 102],
581            [80, 85, 90, 102],
582            [95, 100, 105, 102],
583            [110, 115, 120, 102],
584            [125, 130, 135, 102],
585            [140, 145, 150, 102],
586            [155, 160, 165, 102],
587            [170, 175, 180, 102],
588            [185, 190, 195, 102],
589            [200, 205, 210, 102],
590            [215, 220, 225, 102],
591            [230, 235, 240, 102],
592            [245, 250, 0, 102],
593            [15, 45, 60, 68],
594            [75, 90, 105, 68],
595            [120, 135, 150, 68],
596            [165, 180, 195, 68],
597            [210, 225, 240, 68],
598            [17, 34, 51, 45],
599            [68, 85, 102, 45],
600            [119, 136, 153, 45],
601            [170, 187, 204, 45],
602            [221, 238, 0, 45],
603            [17, 51, 68, 60],
604            [85, 102, 119, 60],
605            [136, 153, 170, 60],
606            [187, 204, 221, 60],
607            [238, 0, 0, 60],
608            [17, 34, 51, 90],
609            [68, 85, 102, 90],
610            [119, 136, 153, 90],
611            [170, 187, 204, 90],
612            [221, 238, 0, 90],
613            [17, 34, 51, 120],
614            [68, 85, 102, 120],
615            [119, 136, 153, 120],
616            [170, 187, 204, 120],
617            [221, 238, 0, 120],
618            [20, 25, 30, 51],
619            [35, 40, 45, 51],
620            [50, 55, 60, 51],
621            [65, 70, 75, 51],
622            [80, 85, 90, 51],
623            [95, 100, 105, 51],
624            [110, 115, 120, 51],
625            [125, 130, 135, 51],
626            [140, 145, 150, 51],
627            [155, 160, 165, 51],
628            [170, 175, 180, 51],
629            [185, 190, 195, 51],
630            [200, 205, 210, 51],
631            [215, 220, 225, 51],
632            [230, 235, 240, 51],
633            [245, 250, 0, 51],
634            [45, 60, 75, 17],
635            [90, 105, 120, 17],
636            [135, 150, 165, 17],
637            [180, 195, 210, 17],
638            [225, 240, 0, 17],
639            [45, 75, 90, 34],
640            [105, 120, 135, 34],
641            [150, 165, 180, 34],
642            [195, 210, 225, 34],
643            [240, 0, 0, 34],
644            [51, 153, 204, 20],
645            [51, 102, 153, 25],
646            [204, 0, 0, 25],
647            [51, 85, 119, 30],
648            [136, 153, 170, 30],
649            [187, 204, 221, 30],
650            [238, 0, 0, 30],
651            [51, 102, 153, 35],
652            [204, 0, 0, 35],
653            [51, 102, 153, 40],
654            [204, 0, 0, 40],
655            [51, 102, 153, 50],
656            [204, 0, 0, 50],
657            [51, 102, 153, 55],
658            [204, 0, 0, 55],
659            [51, 102, 153, 70],
660            [204, 0, 0, 70],
661            [51, 102, 153, 80],
662            [204, 0, 0, 80],
663            [51, 102, 153, 100],
664            [204, 0, 0, 100],
665            [51, 102, 153, 110],
666            [204, 0, 0, 110],
667            [65, 67, 69, 0],
668            [71, 73, 75, 0],
669            [77, 79, 81, 0],
670            [83, 85, 87, 0],
671            [89, 91, 93, 0],
672            [95, 97, 99, 0],
673            [101, 103, 105, 0],
674            [107, 109, 111, 0],
675            [113, 115, 117, 0],
676            [119, 121, 123, 0],
677            [125, 127, 128, 0],
678            [129, 130, 131, 0],
679            [132, 133, 134, 0],
680            [135, 136, 137, 0],
681            [138, 139, 140, 0],
682            [141, 142, 143, 0],
683            [144, 145, 146, 0],
684            [147, 148, 149, 0],
685            [150, 151, 152, 0],
686            [153, 154, 155, 0],
687            [156, 157, 158, 0],
688            [159, 160, 161, 0],
689            [162, 163, 164, 0],
690            [165, 166, 167, 0],
691            [168, 169, 170, 0],
692            [171, 172, 173, 0],
693            [174, 175, 176, 0],
694            [177, 178, 179, 0],
695            [180, 181, 182, 0],
696            [183, 184, 185, 0],
697            [186, 187, 188, 0],
698            [189, 190, 191, 0],
699            [192, 193, 194, 0],
700            [195, 196, 197, 0],
701            [198, 199, 200, 0],
702            [201, 202, 203, 0],
703            [204, 205, 206, 0],
704            [207, 208, 209, 0],
705            [210, 211, 212, 0],
706            [213, 214, 215, 0],
707            [216, 217, 218, 0],
708            [219, 220, 221, 0],
709            [222, 223, 224, 0],
710            [225, 226, 227, 0],
711            [228, 229, 230, 0],
712            [231, 232, 233, 0],
713            [234, 235, 236, 0],
714            [237, 238, 239, 0],
715            [240, 241, 242, 0],
716            [243, 244, 245, 0],
717            [246, 247, 248, 0],
718            [249, 250, 251, 0],
719            [252, 253, 254, 0],
720            [68, 85, 102, 15],
721            [119, 136, 153, 15],
722            [170, 187, 204, 15],
723            [221, 238, 0, 15],
724            [85, 170, 0, 3],
725            [85, 170, 0, 6],
726            [85, 170, 0, 9],
727            [85, 170, 0, 12],
728            [85, 170, 0, 18],
729            [85, 170, 0, 21],
730            [85, 170, 0, 24],
731            [85, 170, 0, 27],
732            [85, 170, 0, 33],
733            [85, 170, 0, 36],
734            [85, 170, 0, 39],
735            [85, 170, 0, 42],
736            [85, 170, 0, 48],
737            [85, 170, 0, 54],
738            [85, 170, 0, 57],
739            [85, 170, 0, 63],
740            [85, 170, 0, 66],
741            [85, 170, 0, 72],
742            [85, 170, 0, 78],
743            [85, 170, 0, 84],
744            [85, 170, 0, 96],
745            [85, 170, 0, 108],
746            [85, 170, 0, 114],
747            [85, 170, 0, 126],
748            [102, 153, 204, 5],
749            [153, 204, 0, 10],
750        ];
751        let rgb_pixels = vec![
752            [190, 152, 114],
753            [76, 38, 190],
754            [188, 188, 188],
755            [186, 124, 62],
756            [184, 184, 184],
757            [182, 182, 182],
758            [180, 168, 156],
759            [144, 132, 120],
760            [108, 96, 84],
761            [72, 60, 48],
762            [36, 24, 12],
763            [178, 178, 178],
764            [176, 176, 176],
765            [174, 116, 58],
766            [172, 172, 172],
767            [170, 168, 166],
768            [164, 162, 160],
769            [158, 156, 154],
770            [152, 150, 148],
771            [146, 144, 142],
772            [140, 138, 136],
773            [134, 132, 130],
774            [128, 126, 124],
775            [122, 120, 118],
776            [116, 114, 112],
777            [110, 108, 106],
778            [104, 102, 100],
779            [98, 96, 94],
780            [92, 90, 88],
781            [86, 84, 82],
782            [80, 78, 76],
783            [74, 72, 70],
784            [68, 66, 64],
785            [62, 60, 58],
786            [56, 54, 52],
787            [50, 48, 46],
788            [44, 42, 40],
789            [38, 36, 34],
790            [32, 30, 28],
791            [26, 24, 22],
792            [20, 18, 16],
793            [14, 12, 10],
794            [8, 6, 4],
795            [2, 170, 170],
796            [168, 112, 56],
797            [166, 166, 166],
798            [164, 164, 164],
799            [162, 108, 54],
800            [160, 128, 96],
801            [64, 32, 160],
802            [158, 158, 158],
803            [156, 104, 52],
804            [154, 154, 154],
805            [152, 152, 152],
806            [150, 140, 130],
807            [120, 110, 100],
808            [90, 80, 70],
809            [60, 50, 40],
810            [30, 20, 10],
811            [148, 148, 148],
812            [146, 146, 146],
813            [144, 96, 48],
814            [142, 142, 142],
815            [140, 112, 84],
816            [56, 28, 140],
817            [138, 92, 46],
818            [136, 128, 120],
819            [112, 104, 96],
820            [88, 80, 72],
821            [64, 56, 48],
822            [40, 32, 24],
823            [16, 8, 136],
824            [134, 134, 134],
825            [132, 88, 44],
826            [130, 104, 78],
827            [52, 26, 130],
828            [128, 128, 128],
829            [127, 127, 127],
830            [126, 84, 42],
831            [125, 100, 75],
832            [50, 25, 125],
833            [124, 124, 124],
834            [123, 82, 41],
835            [122, 122, 122],
836            [121, 121, 121],
837            [120, 112, 104],
838            [96, 88, 80],
839            [72, 64, 56],
840            [48, 40, 32],
841            [24, 16, 8],
842            [119, 112, 105],
843            [98, 91, 84],
844            [77, 70, 63],
845            [56, 49, 42],
846            [35, 28, 21],
847            [14, 7, 119],
848            [118, 118, 118],
849            [117, 78, 39],
850            [116, 116, 116],
851            [115, 92, 69],
852            [46, 23, 115],
853            [114, 76, 38],
854            [113, 113, 113],
855            [112, 112, 112],
856            [111, 74, 37],
857            [110, 88, 66],
858            [44, 22, 110],
859            [109, 109, 109],
860            [108, 72, 36],
861            [107, 107, 107],
862            [106, 106, 106],
863            [105, 98, 91],
864            [84, 77, 70],
865            [63, 56, 49],
866            [42, 35, 28],
867            [21, 14, 7],
868            [104, 104, 104],
869            [103, 103, 103],
870            [102, 100, 98],
871            [96, 94, 92],
872            [90, 88, 86],
873            [84, 82, 80],
874            [78, 76, 74],
875            [72, 70, 68],
876            [66, 64, 62],
877            [60, 58, 56],
878            [54, 52, 50],
879            [48, 46, 44],
880            [42, 40, 38],
881            [36, 34, 32],
882            [30, 28, 26],
883            [24, 22, 20],
884            [18, 16, 14],
885            [12, 10, 8],
886            [6, 4, 2],
887            [101, 101, 101],
888            [100, 80, 60],
889            [40, 20, 100],
890            [99, 66, 33],
891            [98, 98, 98],
892            [97, 97, 97],
893            [96, 64, 32],
894            [95, 76, 57],
895            [38, 19, 95],
896            [94, 94, 94],
897            [93, 62, 31],
898            [92, 92, 92],
899            [91, 91, 91],
900            [90, 84, 78],
901            [72, 66, 60],
902            [54, 48, 42],
903            [36, 30, 24],
904            [18, 12, 6],
905            [89, 89, 89],
906            [88, 88, 88],
907            [87, 58, 29],
908            [86, 86, 86],
909            [85, 84, 83],
910            [82, 81, 80],
911            [79, 78, 77],
912            [76, 75, 74],
913            [73, 72, 71],
914            [70, 69, 68],
915            [67, 66, 65],
916            [64, 63, 62],
917            [61, 60, 59],
918            [58, 57, 56],
919            [55, 54, 53],
920            [52, 51, 50],
921            [49, 48, 47],
922            [46, 45, 44],
923            [43, 42, 41],
924            [40, 39, 38],
925            [37, 36, 35],
926            [34, 33, 32],
927            [31, 30, 29],
928            [28, 27, 26],
929            [25, 24, 23],
930            [22, 21, 20],
931            [19, 18, 17],
932            [16, 15, 14],
933            [13, 12, 11],
934            [10, 9, 8],
935            [7, 6, 5],
936            [4, 3, 2],
937            [1, 85, 85],
938            [84, 56, 28],
939            [83, 83, 83],
940            [82, 82, 82],
941            [81, 54, 27],
942            [80, 64, 48],
943            [32, 16, 80],
944            [79, 79, 79],
945            [78, 52, 26],
946            [77, 77, 77],
947            [76, 76, 76],
948            [75, 70, 65],
949            [60, 55, 50],
950            [45, 40, 35],
951            [30, 25, 20],
952            [15, 10, 5],
953            [74, 74, 74],
954            [73, 73, 73],
955            [72, 48, 24],
956            [71, 71, 71],
957            [70, 56, 42],
958            [28, 14, 70],
959            [69, 46, 23],
960            [68, 64, 60],
961            [56, 52, 48],
962            [44, 40, 36],
963            [32, 28, 24],
964            [20, 16, 12],
965            [8, 4, 68],
966            [67, 67, 67],
967            [66, 44, 22],
968            [65, 52, 39],
969            [26, 13, 65],
970            [64, 64, 64],
971            [63, 42, 21],
972            [62, 62, 62],
973            [61, 61, 61],
974            [60, 56, 52],
975            [48, 44, 40],
976            [36, 32, 28],
977            [24, 20, 16],
978            [12, 8, 4],
979            [59, 59, 59],
980            [58, 58, 58],
981            [57, 38, 19],
982            [56, 56, 56],
983            [55, 44, 33],
984            [22, 11, 55],
985            [54, 36, 18],
986            [53, 53, 53],
987            [52, 52, 52],
988            [51, 50, 49],
989            [48, 47, 46],
990            [45, 44, 43],
991            [42, 41, 40],
992            [39, 38, 37],
993            [36, 35, 34],
994            [33, 32, 31],
995            [30, 29, 28],
996            [27, 26, 25],
997            [24, 23, 22],
998            [21, 20, 19],
999            [18, 17, 16],
1000            [15, 14, 13],
1001            [12, 11, 10],
1002            [9, 8, 7],
1003            [6, 5, 4],
1004            [3, 2, 1],
1005            [50, 40, 30],
1006            [20, 10, 50],
1007            [49, 49, 49],
1008            [48, 32, 16],
1009            [47, 47, 47],
1010            [46, 46, 46],
1011            [45, 42, 39],
1012            [36, 33, 30],
1013            [27, 24, 21],
1014            [18, 15, 12],
1015            [9, 6, 3],
1016            [44, 44, 44],
1017            [43, 43, 43],
1018            [42, 28, 14],
1019            [41, 41, 41],
1020            [40, 32, 24],
1021            [16, 8, 40],
1022            [39, 26, 13],
1023            [38, 38, 38],
1024            [37, 37, 37],
1025            [36, 24, 12],
1026            [35, 28, 21],
1027            [14, 7, 35],
1028            [34, 32, 30],
1029            [28, 26, 24],
1030            [22, 20, 18],
1031            [16, 14, 12],
1032            [10, 8, 6],
1033            [4, 2, 34],
1034            [33, 22, 11],
1035            [32, 32, 32],
1036            [31, 31, 31],
1037            [30, 28, 26],
1038            [24, 22, 20],
1039            [18, 16, 14],
1040            [12, 10, 8],
1041            [6, 4, 2],
1042            [29, 29, 29],
1043            [28, 28, 28],
1044            [27, 18, 9],
1045            [26, 26, 26],
1046            [25, 20, 15],
1047            [10, 5, 25],
1048            [24, 16, 8],
1049            [23, 23, 23],
1050            [22, 22, 22],
1051            [21, 14, 7],
1052            [20, 16, 12],
1053            [8, 4, 20],
1054            [19, 19, 19],
1055            [18, 12, 6],
1056            [17, 16, 15],
1057            [14, 13, 12],
1058            [11, 10, 9],
1059            [8, 7, 6],
1060            [5, 4, 3],
1061            [2, 1, 17],
1062            [16, 16, 16],
1063            [15, 14, 13],
1064            [12, 11, 10],
1065            [9, 8, 7],
1066            [6, 5, 4],
1067            [3, 2, 1],
1068            [14, 14, 14],
1069            [13, 13, 13],
1070            [12, 8, 4],
1071            [11, 11, 11],
1072            [10, 8, 6],
1073            [4, 2, 10],
1074            [9, 6, 3],
1075            [8, 8, 8],
1076            [7, 7, 7],
1077            [6, 4, 2],
1078            [5, 4, 3],
1079            [2, 1, 5],
1080            [4, 4, 4],
1081            [3, 2, 1],
1082            [2, 2, 2],
1083            [1, 1, 1],
1084            [150, 144, 138],
1085            [132, 129, 126],
1086            [123, 120, 117],
1087            [114, 111, 108],
1088            [105, 102, 99],
1089            [96, 93, 90],
1090            [87, 84, 81],
1091            [78, 75, 72],
1092            [69, 66, 63],
1093            [60, 57, 54],
1094            [51, 48, 45],
1095            [42, 39, 36],
1096            [33, 30, 27],
1097            [24, 21, 18],
1098            [15, 12, 9],
1099            [6, 3, 153],
1100            [176, 154, 143],
1101            [132, 121, 110],
1102            [99, 88, 77],
1103            [66, 55, 44],
1104            [33, 22, 11],
1105            [196, 182, 168],
1106            [154, 140, 126],
1107            [112, 98, 84],
1108            [70, 56, 42],
1109            [28, 14, 210],
1110            [182, 156, 143],
1111            [130, 117, 104],
1112            [91, 78, 65],
1113            [52, 39, 26],
1114            [13, 195, 195],
1115            [154, 143, 132],
1116            [121, 110, 99],
1117            [88, 77, 66],
1118            [55, 44, 33],
1119            [22, 11, 165],
1120            [126, 117, 108],
1121            [99, 90, 81],
1122            [72, 63, 54],
1123            [45, 36, 27],
1124            [18, 9, 135],
1125            [188, 184, 180],
1126            [176, 172, 168],
1127            [164, 160, 156],
1128            [152, 148, 144],
1129            [140, 136, 132],
1130            [128, 124, 120],
1131            [116, 112, 108],
1132            [104, 100, 96],
1133            [92, 88, 84],
1134            [80, 76, 72],
1135            [68, 64, 60],
1136            [56, 52, 48],
1137            [44, 40, 36],
1138            [32, 28, 24],
1139            [20, 16, 12],
1140            [8, 4, 204],
1141            [196, 182, 168],
1142            [154, 140, 126],
1143            [112, 98, 84],
1144            [70, 56, 42],
1145            [28, 14, 238],
1146            [182, 156, 143],
1147            [130, 117, 104],
1148            [91, 78, 65],
1149            [52, 39, 26],
1150            [13, 221, 221],
1151            [188, 94, 47],
1152            [184, 138, 92],
1153            [46, 230, 230],
1154            [180, 150, 120],
1155            [105, 90, 75],
1156            [60, 45, 30],
1157            [15, 225, 225],
1158            [176, 132, 88],
1159            [44, 220, 220],
1160            [172, 129, 86],
1161            [43, 215, 215],
1162            [164, 123, 82],
1163            [41, 205, 205],
1164            [160, 120, 80],
1165            [40, 200, 200],
1166            [148, 111, 74],
1167            [37, 185, 185],
1168            [140, 105, 70],
1169            [35, 175, 175],
1170            [124, 93, 62],
1171            [31, 155, 155],
1172            [116, 87, 58],
1173            [29, 145, 145],
1174            [190, 188, 186],
1175            [184, 182, 180],
1176            [178, 176, 174],
1177            [172, 170, 168],
1178            [166, 164, 162],
1179            [160, 158, 156],
1180            [154, 152, 150],
1181            [148, 146, 144],
1182            [142, 140, 138],
1183            [136, 134, 132],
1184            [130, 128, 127],
1185            [126, 125, 124],
1186            [123, 122, 121],
1187            [120, 119, 118],
1188            [117, 116, 115],
1189            [114, 113, 112],
1190            [111, 110, 109],
1191            [108, 107, 106],
1192            [105, 104, 103],
1193            [102, 101, 100],
1194            [99, 98, 97],
1195            [96, 95, 94],
1196            [93, 92, 91],
1197            [90, 89, 88],
1198            [87, 86, 85],
1199            [84, 83, 82],
1200            [81, 80, 79],
1201            [78, 77, 76],
1202            [75, 74, 73],
1203            [72, 71, 70],
1204            [69, 68, 67],
1205            [66, 65, 64],
1206            [63, 62, 61],
1207            [60, 59, 58],
1208            [57, 56, 55],
1209            [54, 53, 52],
1210            [51, 50, 49],
1211            [48, 47, 46],
1212            [45, 44, 43],
1213            [42, 41, 40],
1214            [39, 38, 37],
1215            [36, 35, 34],
1216            [33, 32, 31],
1217            [30, 29, 28],
1218            [27, 26, 25],
1219            [24, 23, 22],
1220            [21, 20, 19],
1221            [18, 17, 16],
1222            [15, 14, 13],
1223            [12, 11, 10],
1224            [9, 8, 7],
1225            [6, 5, 4],
1226            [3, 2, 1],
1227            [176, 160, 144],
1228            [128, 112, 96],
1229            [80, 64, 48],
1230            [32, 16, 240],
1231            [168, 84, 252],
1232            [166, 83, 249],
1233            [164, 82, 246],
1234            [162, 81, 243],
1235            [158, 79, 237],
1236            [156, 78, 234],
1237            [154, 77, 231],
1238            [152, 76, 228],
1239            [148, 74, 222],
1240            [146, 73, 219],
1241            [144, 72, 216],
1242            [142, 71, 213],
1243            [138, 69, 207],
1244            [134, 67, 201],
1245            [132, 66, 198],
1246            [128, 64, 192],
1247            [126, 63, 189],
1248            [122, 61, 183],
1249            [118, 59, 177],
1250            [114, 57, 171],
1251            [106, 53, 159],
1252            [98, 49, 147],
1253            [94, 47, 141],
1254            [86, 43, 129],
1255            [150, 100, 50],
1256            [98, 49, 245],
1257        ];
1258        for (&cmyk_pixel, rgb_pixel) in cmyk_pixels.iter().zip(rgb_pixels) {
1259            single_pix_correct(cmyk_pixel, rgb_pixel);
1260        }
1261    }
1262
1263    #[cfg(feature = "benchmarks")]
1264    #[bench]
1265    fn bench_cmyk_to_rgb(b: &mut Bencher) {
1266        let mut v = Vec::with_capacity(W * H * 4);
1267        for c in 0..=255 {
1268            for k in 0..=255 {
1269                v.push(c as u8);
1270                v.push(0);
1271                v.push(0);
1272                v.push(k as u8);
1273            }
1274        }
1275
1276        b.iter(|| {
1277            cmyk_to_rgb(&v);
1278        });
1279    }
1280
1281    #[cfg(feature = "benchmarks")]
1282    #[bench]
1283    fn bench_cmyk_to_rgb_single(b: &mut Bencher) {
1284        b.iter(|| {
1285            cmyk_to_rgb(&[128, 128, 128, 128]);
1286        });
1287    }
1288}