Debian Codegraph @

.cargo/vendor/zune-jpeg/src/worker.rs

raw
1 /*
2 * Copyright (c) 2023.
3 *
4 * This software is free software;
5 *
6 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
7 */
8
9 use alloc::format;
10 use core::convert::TryInto;
11
12 use zune_core::colorspace::ColorSpace;
13
14 use crate::color_convert::ycbcr_to_grayscale;
15 use crate::components::{Components, SampleRatios};
16 use crate::decoder::{ColorConvert16Ptr, MAX_COMPONENTS};
17 use crate::errors::DecodeErrors;
18
19 /// fast 0..255 * 0..255 => 0..255 rounded multiplication
20 ///
21 /// Borrowed from stb
22 #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
23 #[inline]
24 fn blinn_8x8(in_val: u8, y: u8) -> u8 {
25 let t = i32::from(in_val) * i32::from(y) + 128;
26 return ((t + (t >> 8)) >> 8) as u8;
27 }
28
29 #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
30 pub(crate) fn color_convert(
31 unprocessed: &[&[i16]; MAX_COMPONENTS], color_convert_16: ColorConvert16Ptr,
32 input_colorspace: ColorSpace, output_colorspace: ColorSpace, output: &mut [u8], width: usize,
33 padded_width: usize
34 ) -> Result<(), DecodeErrors> {
35 if input_colorspace.num_components() == 3 && input_colorspace == output_colorspace {
36 // sort things like RGB to RGB conversion
37 copy_removing_padding(unprocessed, width, padded_width, output);
38 return Ok(());
39 }
40 if input_colorspace.num_components() == 4 && input_colorspace == output_colorspace {
41 copy_removing_padding_4x(unprocessed, width, padded_width, output);
42 return Ok(());
43 }
44 // color convert
45 match (input_colorspace, output_colorspace) {
46 (ColorSpace::YCbCr | ColorSpace::Luma, ColorSpace::Luma) => {
47 ycbcr_to_grayscale(unprocessed[0], width, padded_width, output);
48 }
49 (
50 ColorSpace::YCbCr,
51 ColorSpace::RGB | ColorSpace::RGBA | ColorSpace::BGR | ColorSpace::BGRA
52 ) => {
53 color_convert_ycbcr(
54 unprocessed,
55 width,
56 padded_width,
57 output_colorspace,
58 color_convert_16,
59 output
60 );
61 }
62 (ColorSpace::YCCK, ColorSpace::RGB) => {
63 color_convert_ycck_to_rgb::<3>(
64 unprocessed,
65 width,
66 padded_width,
67 output_colorspace,
68 color_convert_16,
69 output
70 );
71 }
72
73 (ColorSpace::YCCK, ColorSpace::RGBA) => {
74 color_convert_ycck_to_rgb::<4>(
75 unprocessed,
76 width,
77 padded_width,
78 output_colorspace,
79 color_convert_16,
80 output
81 );
82 }
83 (ColorSpace::CMYK, ColorSpace::RGB) => {
84 color_convert_cymk_to_rgb::<3>(unprocessed, width, padded_width, output);
85 }
86 (ColorSpace::CMYK, ColorSpace::RGBA) => {
87 color_convert_cymk_to_rgb::<4>(unprocessed, width, padded_width, output);
88 }
89 (ColorSpace::MultiBand(n), _) => {
90 if n.get() != 2 {
91 return Err(DecodeErrors::Format(format!(
92 "Unknown multiband sample ({n}), please share sample"
93 )));
94 }
95 copy_removing_padding_generic(
96 unprocessed,
97 width,
98 padded_width,
99 output,
100 n.get() as usize
101 );
102 }
103 (ColorSpace::Luma, ColorSpace::RGB) => {
104 // duplicate the luma channel three times to form RGB
105 // Note, this may assume the direct conversion
106 // from luma to RGB is by duplicating
107 //
108 // There may be a bit more complex ways
109 // of doing it but won't get onto it
110 convert_luma_to_rgb(unprocessed, width, padded_width, output)
111 }
112 (ColorSpace::Luma, ColorSpace::RGBA) => {
113 // duplicate the luma channel three times to form RGB
114 // add 255 as alpha
115 // Note, this may assume the direct conversion
116 // from luma to RGB is by duplicating
117 //
118 // There may be a bit more complex ways
119 // of doing it but won't get onto it
120 convert_luma_to_rgba(unprocessed, width, padded_width, output)
121 }
122
123 // For the other components we do nothing(currently)
124 _ => {
125 let msg = format!(
126 "Unimplemented colorspace mapping from {input_colorspace:?} to {output_colorspace:?}");
127
128 return Err(DecodeErrors::Format(msg));
129 }
130 }
131 Ok(())
132 }
133
134 fn convert_luma_to_rgb(
135 mcu_block: &[&[i16]; MAX_COMPONENTS], width: usize, padded_width: usize, output: &mut [u8]
136 ) {
137 for (pix_w, y_w) in output
138 .chunks_exact_mut(width * 3)
139 .zip(mcu_block[0].chunks_exact(padded_width))
140 {
141 for (pix, c) in pix_w.chunks_exact_mut(3).zip(y_w) {
142 pix[0] = *c as u8;
143 pix[1] = *c as u8;
144 pix[2] = *c as u8;
145 }
146 }
147 }
148 fn convert_luma_to_rgba(
149 mcu_block: &[&[i16]; MAX_COMPONENTS], width: usize, padded_width: usize, output: &mut [u8]
150 ) {
151 for (pix_w, y_w) in output
152 .chunks_exact_mut(width * 4)
153 .zip(mcu_block[0].chunks_exact(padded_width))
154 {
155 for (pix, c) in pix_w.chunks_exact_mut(4).zip(y_w) {
156 pix[0] = *c as u8;
157 pix[1] = *c as u8;
158 pix[2] = *c as u8;
159 pix[3] = 255;
160 }
161 }
162 }
163 /// Copy a block to output removing padding bytes from input
164 /// if necessary
165 #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
166 fn copy_removing_padding(
167 mcu_block: &[&[i16]; MAX_COMPONENTS], width: usize, padded_width: usize, output: &mut [u8]
168 ) {
169 for (((pix_w, c_w), m_w), y_w) in output
170 .chunks_exact_mut(width * 3)
171 .zip(mcu_block[0].chunks_exact(padded_width))
172 .zip(mcu_block[1].chunks_exact(padded_width))
173 .zip(mcu_block[2].chunks_exact(padded_width))
174 {
175 for (((pix, c), y), m) in pix_w.chunks_exact_mut(3).zip(c_w).zip(m_w).zip(y_w) {
176 pix[0] = *c as u8;
177 pix[1] = *y as u8;
178 pix[2] = *m as u8;
179 }
180 }
181 }
182 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
183 fn copy_removing_padding_4x(
184 mcu_block: &[&[i16]; MAX_COMPONENTS], width: usize, padded_width: usize, output: &mut [u8]
185 ) {
186 for ((((pix_w, c_w), m_w), y_w), k_w) in output
187 .chunks_exact_mut(width * 4)
188 .zip(mcu_block[0].chunks_exact(padded_width))
189 .zip(mcu_block[1].chunks_exact(padded_width))
190 .zip(mcu_block[2].chunks_exact(padded_width))
191 .zip(mcu_block[3].chunks_exact(padded_width))
192 {
193 for ((((pix, c), y), m), k) in pix_w
194 .chunks_exact_mut(4)
195 .zip(c_w)
196 .zip(m_w)
197 .zip(y_w)
198 .zip(k_w)
199 {
200 pix[0] = *c as u8;
201 pix[1] = *y as u8;
202 pix[2] = *m as u8;
203 pix[3] = *k as u8;
204 }
205 }
206 }
207 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
208 fn copy_removing_padding_generic(
209 mcu_block: &[&[i16]; MAX_COMPONENTS], width: usize, padded_width: usize, output: &mut [u8],
210 channels: usize
211 ) {
212 match channels {
213 // just do 2 for now
214 2 => {
215 for ((pix_w, y_w), k_w) in output
216 .chunks_exact_mut(width * channels)
217 .zip(mcu_block[0].chunks_exact(padded_width))
218 .zip(mcu_block[1].chunks_exact(padded_width))
219 {
220 for ((pix, c), k) in pix_w.chunks_exact_mut(2).zip(y_w).zip(k_w) {
221 pix[0] = *c as u8;
222 pix[1] = *k as u8;
223 }
224 }
225 }
226 _ => unreachable!()
227 }
228 }
229 /// Convert YCCK image to rgb
230 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
231 fn color_convert_ycck_to_rgb<const NUM_COMPONENTS: usize>(
232 mcu_block: &[&[i16]; MAX_COMPONENTS], width: usize, padded_width: usize,
233 output_colorspace: ColorSpace, color_convert_16: ColorConvert16Ptr, output: &mut [u8]
234 ) {
235 color_convert_ycbcr(
236 mcu_block,
237 width,
238 padded_width,
239 output_colorspace,
240 color_convert_16,
241 output
242 );
243 for (pix_w, m_w) in output
244 .chunks_exact_mut(width * 3)
245 .zip(mcu_block[3].chunks_exact(padded_width))
246 {
247 for (pix, m) in pix_w.chunks_exact_mut(NUM_COMPONENTS).zip(m_w) {
248 let m = (*m) as u8;
249 pix[0] = blinn_8x8(255 - pix[0], m);
250 pix[1] = blinn_8x8(255 - pix[1], m);
251 pix[2] = blinn_8x8(255 - pix[2], m);
252 }
253 }
254 }
255
256 #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
257 fn color_convert_cymk_to_rgb<const NUM_COMPONENTS: usize>(
258 mcu_block: &[&[i16]; MAX_COMPONENTS], width: usize, padded_width: usize, output: &mut [u8]
259 ) {
260 for ((((pix_w, c_w), m_w), y_w), k_w) in output
261 .chunks_exact_mut(width * NUM_COMPONENTS)
262 .zip(mcu_block[0].chunks_exact(padded_width))
263 .zip(mcu_block[1].chunks_exact(padded_width))
264 .zip(mcu_block[2].chunks_exact(padded_width))
265 .zip(mcu_block[3].chunks_exact(padded_width))
266 {
267 for ((((pix, c), m), y), k) in pix_w
268 .chunks_exact_mut(3)
269 .zip(c_w)
270 .zip(m_w)
271 .zip(y_w)
272 .zip(k_w)
273 {
274 let c = *c as u8;
275 let m = *m as u8;
276 let y = *y as u8;
277 let k = *k as u8;
278
279 pix[0] = blinn_8x8(c, k);
280 pix[1] = blinn_8x8(m, k);
281 pix[2] = blinn_8x8(y, k);
282 }
283 }
284 }
285
286 /// Do color-conversion for interleaved MCU
287 #[allow(
288 clippy::similar_names,
289 clippy::too_many_arguments,
290 clippy::needless_pass_by_value,
291 clippy::unwrap_used
292 )]
293 fn color_convert_ycbcr(
294 mcu_block: &[&[i16]; MAX_COMPONENTS], width: usize, padded_width: usize,
295 output_colorspace: ColorSpace, color_convert_16: ColorConvert16Ptr, output: &mut [u8]
296 ) {
297 let num_components = output_colorspace.num_components();
298
299 let stride = width * num_components;
300 // Allocate temporary buffer for small widths less than 16.
301 let mut temp = [0; 64];
302 // We need to chunk per width to ensure we can discard extra values at the end of the width.
303 // Since the encoder may pad bits to ensure the width is a multiple of 8.
304 for (((y_width, cb_width), cr_width), out) in mcu_block[0]
305 .chunks_exact(padded_width)
306 .zip(mcu_block[1].chunks_exact(padded_width))
307 .zip(mcu_block[2].chunks_exact(padded_width))
308 .zip(output.chunks_exact_mut(stride))
309 {
310 if width < 16 {
311 // allocate temporary buffers for the values received from idct
312 let mut y_out = [0; 16];
313 let mut cb_out = [0; 16];
314 let mut cr_out = [0; 16];
315 // copy those small widths to that buffer
316 y_out[0..y_width.len()].copy_from_slice(y_width);
317 cb_out[0..cb_width.len()].copy_from_slice(cb_width);
318 cr_out[0..cr_width.len()].copy_from_slice(cr_width);
319 // we handle widths less than 16 a bit differently, allocating a temporary
320 // buffer and writing to that and then flushing to the out buffer
321 // because of the optimizations applied below,
322 (color_convert_16)(&y_out, &cb_out, &cr_out, &mut temp, &mut 0);
323 // copy to stride
324 out[0..width * num_components].copy_from_slice(&temp[0..width * num_components]);
325 // next
326 continue;
327 }
328
329 // Chunk in outputs of 16 to pass to color_convert as an array of 16 i16's.
330 for (((y, cb), cr), out_c) in y_width
331 .chunks_exact(16)
332 .zip(cb_width.chunks_exact(16))
333 .zip(cr_width.chunks_exact(16))
334 .zip(out.chunks_exact_mut(16 * num_components))
335 {
336 (color_convert_16)(
337 y.try_into().unwrap(),
338 cb.try_into().unwrap(),
339 cr.try_into().unwrap(),
340 out_c,
341 &mut 0
342 );
343 }
344 //we have more pixels in the end that can't be handled by the main loop.
345 //move pointer back a little bit to get last 16 bytes,
346 //color convert, and overwrite
347 //This means some values will be color converted twice.
348 for ((y, cb), cr) in y_width[width - 16..]
349 .chunks_exact(16)
350 .zip(cb_width[width - 16..].chunks_exact(16))
351 .zip(cr_width[width - 16..].chunks_exact(16))
352 .take(1)
353 {
354 (color_convert_16)(
355 y.try_into().unwrap(),
356 cb.try_into().unwrap(),
357 cr.try_into().unwrap(),
358 &mut temp,
359 &mut 0
360 );
361 }
362
363 let rem = out[(width - 16) * num_components..]
364 .chunks_exact_mut(16 * num_components)
365 .next()
366 .unwrap();
367
368 rem.copy_from_slice(&temp[0..rem.len()]);
369 }
370 }
371 pub(crate) fn upsample(
372 component: &mut Components, mcu_height: usize, i: usize, upsampler_scratch_space: &mut [i16],
373 has_vertical_sample: bool
374 ) -> Result<(), DecodeErrors> {
375 match component.sample_ratio {
376 SampleRatios::V | SampleRatios::HV => {
377 /*
378 When upsampling vertically sampled images, we have a certain problem
379 which is that we do not have all MCU's decoded, this usually sucks at boundaries
380 e.g we can't upsample the last mcu row, since the row_down currently doesn't exist
381
382 To solve this we need to do two things
383
384 1. Carry over coefficients when we lack enough data to upsample
385 2. Upsample when we have enough data
386
387 To achieve (1), we store a previous row, and the current row in components themselves
388 which will later be used to make (2)
389
390 To achieve (2), we take the stored previous row(second last MCU row),
391 current row(last mcu row) and row down(first row of newly decoded MCU)
392
393 and upsample that and store it in first_row_upsample_dest, this contains
394 up-sampled coefficients for the last for the previous decoded mcu row.
395
396 The caller is then expected to process first_row_upsample_dest before processing data
397 in component.upsample_dest which stores the up-sampled components excluding the last row
398 */
399
400 let mut dest_start = 0;
401 let stride_bytes_written = component.width_stride * component.sample_ratio.sample();
402
403 if i > 0 {
404 // Handle the last MCU of the previous row
405 // This wasn't up-sampled as we didn't have the row_down
406 // so we do it now
407
408 let stride = component.width_stride;
409
410 let dest = &mut component.first_row_upsample_dest[0..stride_bytes_written];
411
412 // get current row
413 let row = &component.row[..];
414 let row_up = &component.row_up[..];
415 let row_down = &component.raw_coeff[0..stride];
416 (component.up_sampler)(row, row_up, row_down, upsampler_scratch_space, dest);
417 }
418
419 // we have the Y component width stride.
420 // this may be higher than the actual width,(2x because vertical sampling)
421 //
422 // This will not upsample the last row
423
424 // if false, do not upsample.
425 // set to false on the last row of an mcu
426 let mut upsample = true;
427
428 let stride = component.width_stride * component.vertical_sample;
429 let stop_offset = component.raw_coeff.len() / component.width_stride;
430
431 if component.raw_coeff.len() != stop_offset * stride {
432 // slice would panic below
433 return Err(DecodeErrors::FormatStatic(
434 "Invalid component dimensions, would panic"
435 ));
436 }
437 for (pos, curr_row) in component
438 .raw_coeff
439 .chunks_exact(component.width_stride)
440 .enumerate()
441 {
442 let mut dest: &mut [i16] = &mut [];
443 let mut row_up: &[i16] = &[];
444 // row below current sample
445 let mut row_down: &[i16] = &[];
446
447 // Order of ifs matters
448
449 if i == 0 && pos == 0 {
450 // first IMAGE row, row_up is the same as current row
451 // row_down is the row below.
452 row_up = &component.raw_coeff[pos * stride..(pos + 1) * stride];
453 row_down = &component.raw_coeff[(pos + 1) * stride..(pos + 2) * stride];
454 } else if i > 0 && pos == 0 {
455 // first row of a new mcu, previous row was copied so use that
456 row_up = &component.row[..];
457 row_down = &component.raw_coeff[(pos + 1) * stride..(pos + 2) * stride];
458 } else if i == mcu_height.saturating_sub(1) && pos == stop_offset - 1 {
459 // last IMAGE row, adjust pointer to use previous row and current row
460 row_up = &component.raw_coeff[(pos - 1) * stride..pos * stride];
461 row_down = &component.raw_coeff[pos * stride..(pos + 1) * stride];
462 } else if pos > 0 && pos < stop_offset - 1 {
463 // other rows, get row up and row down relative to our current row
464 // ignore last row of each mcu
465 row_up = &component.raw_coeff[(pos - 1) * stride..pos * stride];
466 row_down = &component.raw_coeff[(pos + 1) * stride..(pos + 2) * stride];
467 } else if pos == stop_offset - 1 {
468 // last MCU in a row
469 //
470 // we need a row at the next MCU but we haven't decoded that MCU yet
471 // so we should save this and when we have the next MCU,
472 // do the upsampling
473
474 // store the current row and previous row in a buffer
475 let prev_row = &component.raw_coeff[(pos - 1) * stride..pos * stride];
476
477 component.row_up.copy_from_slice(prev_row);
478 component.row.copy_from_slice(curr_row);
479 upsample = false;
480 } else {
481 unreachable!("Uh oh!");
482 }
483 if upsample {
484 dest =
485 &mut component.upsample_dest[dest_start..dest_start + stride_bytes_written];
486 dest_start += stride_bytes_written;
487 }
488
489 if upsample {
490 // upsample
491 (component.up_sampler)(
492 curr_row,
493 row_up,
494 row_down,
495 upsampler_scratch_space,
496 dest
497 );
498 }
499 }
500 }
501 SampleRatios::H => {
502 //assert_eq!(component.raw_coeff.len() * 2, component.upsample_dest.len());
503 // Before it was an assert, but numerous and numerous and numerous
504 // bug fixes and ad hoc solutions later, I have now just decided to keep it as a resize
505 component
506 .upsample_dest
507 .resize(component.raw_coeff.len() * 2, 0);
508
509 let raw_coeff = &component.raw_coeff;
510 let dest_coeff = &mut component.upsample_dest;
511
512 if has_vertical_sample {
513 /*
514 There have been images that have the following configurations.
515
516 Component ID:Y HS:2 VS:2 QT:0
517 Component ID:Cb HS:1 VS:1 QT:1
518 Component ID:Cr HS:1 VS:2 QT:1
519
520 This brings out a nasty case of misaligned sampling factors. Cr will need to save a row because
521 of the way we process boundaries but Cb won't since Cr is horizontally sampled while Cb is
522 HV sampled with respect to the image sampling factors.
523
524 So during decoding of one MCU, we could only do 7 and not 8 rows, but the SampleRatio::H never had to
525 save a single line, since it doesn't suffer from boundary issues.
526
527 Now this takes care of that, saving the last MCU row in case it will be needed.
528 We save the previous row before up-sampling this row because the boundary issue is in
529 the last MCU row of the previous MCU.
530
531 PS(cae): I can't add the image to the repo as it is nsfw, but can send if required
532 */
533 let length = component.first_row_upsample_dest.len();
534 component
535 .first_row_upsample_dest
536 .copy_from_slice(&dest_coeff.rchunks_exact(length).next().unwrap());
537 }
538 // up-sample each row
539 for (single_row, output_stride) in raw_coeff
540 .chunks_exact(component.width_stride)
541 .zip(dest_coeff.chunks_exact_mut(component.width_stride * 2))
542 {
543 // upsample using the fn pointer, should only be H, so no need for
544 // row up and row down
545 (component.up_sampler)(single_row, &[], &[], &mut [], output_stride);
546 }
547 }
548 SampleRatios::Generic(h, v) => {
549 let raw_coeff = &component.raw_coeff;
550 let dest_coeff = &mut component.upsample_dest;
551
552 //let size = component.width_stride.div_ceil(v);
553
554 // for (single_row, output_stride) in raw_coeff
555 // .chunks_exact(size)
556 // .zip(dest_coeff.chunks_exact_mut(component.width_stride * h))
557 // {
558 // (component.up_sampler)(single_row, &[], &[], &mut [], output_stride);
559 //
560 // }
561 for (single_row, output_stride) in raw_coeff
562 .chunks_exact(component.width_stride)
563 .zip(dest_coeff.chunks_exact_mut(component.width_stride * h * v))
564 {
565 for row in output_stride.chunks_exact_mut(component.width_stride * h) {
566 (component.up_sampler)(single_row, &[], &[], &mut [], row);
567 }
568 }
569 }
570 SampleRatios::None => {}
571 };
572 Ok(())
573 }