4 * Copyright (C) 1998, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains sample scaling for lossless JPEG. This is a
9 * combination of upscaling the undifferenced sample by 2^Pt and downscaling
10 * the sample to fit into JSAMPLE.
13 #define JPEG_INTERNALS
16 #include "jlossls.h" /* Private declarations for lossless codec */
19 #ifdef D_LOSSLESS_SUPPORTED
22 * Private scaler object for lossless decoding.
29 typedef scaler * scaler_ptr;
33 * Scalers for packing sample differences into JSAMPLEs.
37 simple_upscale(j_decompress_ptr cinfo,
38 JDIFFROW diff_buf, JSAMPROW output_buf,
41 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
42 scaler_ptr scaler = (scaler_ptr) losslsd->scaler_private;
43 int scale_factor = scaler->scale_factor;
46 for (xindex = 0; xindex < width; xindex++)
47 output_buf[xindex] = (JSAMPLE) (diff_buf[xindex] << scale_factor);
51 simple_downscale(j_decompress_ptr cinfo,
52 JDIFFROW diff_buf, JSAMPROW output_buf,
55 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
56 scaler_ptr scaler = (scaler_ptr) losslsd->scaler_private;
57 int scale_factor = scaler->scale_factor;
61 for (xindex = 0; xindex < width; xindex++)
62 output_buf[xindex] = (JSAMPLE) RIGHT_SHIFT(diff_buf[xindex], scale_factor);
66 noscale(j_decompress_ptr cinfo,
67 JDIFFROW diff_buf, JSAMPROW output_buf,
73 for (xindex = 0; xindex < width; xindex++)
74 output_buf[xindex] = (JSAMPLE) diff_buf[xindex];
79 scaler_start_pass (j_decompress_ptr cinfo)
81 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
82 scaler_ptr scaler = (scaler_ptr) losslsd->scaler_private;
86 * Downscale by the difference in the input vs. output precision. If the
87 * output precision >= input precision, then do not downscale.
89 downscale = BITS_IN_JSAMPLE < cinfo->data_precision ?
90 cinfo->data_precision - BITS_IN_JSAMPLE : 0;
92 scaler->scale_factor = cinfo->Al - downscale;
94 /* Set scaler functions based on scale_factor (positive = left shift) */
95 if (scaler->scale_factor > 0)
96 losslsd->scaler_scale = simple_upscale;
97 else if (scaler->scale_factor < 0) {
98 scaler->scale_factor = -scaler->scale_factor;
99 losslsd->scaler_scale = simple_downscale;
102 losslsd->scaler_scale = noscale;
107 jinit_d_scaler (j_decompress_ptr cinfo)
109 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
112 scaler = (scaler_ptr)
113 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
115 losslsd->scaler_private = (void *) scaler;
116 losslsd->scaler_start_pass = scaler_start_pass;
119 #endif /* D_LOSSLESS_SUPPORTED */