8 * Copyright (C) 1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains sample scaling for lossless JPEG. This is a
13 * combination of upscaling the undifferenced sample by 2^Pt and downscaling
14 * the sample to fit into JSAMPLE.
17 #define JPEG_INTERNALS
20 #include "jlossls.h" /* Private declarations for lossless codec */
23 #ifdef D_LOSSLESS_SUPPORTED
26 * Private scaler object for lossless decoding.
33 typedef scaler
* scaler_ptr
;
37 * Scalers for packing sample differences into JSAMPLEs.
41 simple_upscale(j_decompress_ptr cinfo
,
42 JDIFFROW diff_buf
, JSAMPROW output_buf
,
45 j_lossless_d_ptr losslsd
= (j_lossless_d_ptr
) cinfo
->codec
;
46 scaler_ptr scaler
= (scaler_ptr
) losslsd
->scaler_private
;
47 int scale_factor
= scaler
->scale_factor
;
50 for (xindex
= 0; xindex
< width
; xindex
++)
51 output_buf
[xindex
] = (JSAMPLE
) (diff_buf
[xindex
] << scale_factor
);
55 simple_downscale(j_decompress_ptr cinfo
,
56 JDIFFROW diff_buf
, JSAMPROW output_buf
,
59 j_lossless_d_ptr losslsd
= (j_lossless_d_ptr
) cinfo
->codec
;
60 scaler_ptr scaler
= (scaler_ptr
) losslsd
->scaler_private
;
61 int scale_factor
= scaler
->scale_factor
;
64 for (xindex
= 0; xindex
< width
; xindex
++)
65 output_buf
[xindex
] = (JSAMPLE
) RIGHT_SHIFT(diff_buf
[xindex
], scale_factor
);
69 noscale(j_decompress_ptr cinfo
,
70 JDIFFROW diff_buf
, JSAMPROW output_buf
,
75 for (xindex
= 0; xindex
< width
; xindex
++)
76 output_buf
[xindex
] = (JSAMPLE
) diff_buf
[xindex
];
81 scaler_start_pass (j_decompress_ptr cinfo
)
83 j_lossless_d_ptr losslsd
= (j_lossless_d_ptr
) cinfo
->codec
;
84 scaler_ptr scaler
= (scaler_ptr
) losslsd
->scaler_private
;
88 * Downscale by the difference in the input vs. output precision. If the
89 * output precision >= input precision, then do not downscale.
91 downscale
= BITS_IN_JSAMPLE
< cinfo
->data_precision
?
92 cinfo
->data_precision
- BITS_IN_JSAMPLE
: 0;
94 scaler
->scale_factor
= cinfo
->Al
- downscale
;
96 /* Set scaler functions based on scale_factor (positive = left shift) */
97 if (scaler
->scale_factor
> 0)
98 losslsd
->scaler_scale
= simple_upscale
;
99 else if (scaler
->scale_factor
< 0) {
100 scaler
->scale_factor
= -scaler
->scale_factor
;
101 losslsd
->scaler_scale
= simple_downscale
;
104 losslsd
->scaler_scale
= noscale
;
109 jinit_d_scaler (j_decompress_ptr cinfo
)
111 j_lossless_d_ptr losslsd
= (j_lossless_d_ptr
) cinfo
->codec
;
114 scaler
= (scaler_ptr
)
115 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
117 losslsd
->scaler_private
= (void *) scaler
;
118 losslsd
->scaler_start_pass
= scaler_start_pass
;
121 #endif /* D_LOSSLESS_SUPPORTED */