Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / main / jdscale.c
blob85c1eef8ac8dbb41fc3dc39fdac9beacebda5004
1 /*
2 $Id$
3 */
5 /*
6 * jdscale.c
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
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jlossls.h" /* Private declarations for lossless codec */
23 #ifdef D_LOSSLESS_SUPPORTED
26 * Private scaler object for lossless decoding.
29 typedef struct {
30 int scale_factor;
31 } scaler;
33 typedef scaler * scaler_ptr;
37 * Scalers for packing sample differences into JSAMPLEs.
40 METHODDEF(void)
41 simple_upscale(j_decompress_ptr cinfo,
42 JDIFFROW diff_buf, JSAMPROW output_buf,
43 JDIMENSION width)
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;
48 int xindex;
50 for (xindex = 0; xindex < width; xindex++)
51 output_buf[xindex] = (JSAMPLE) (diff_buf[xindex] << scale_factor);
54 METHODDEF(void)
55 simple_downscale(j_decompress_ptr cinfo,
56 JDIFFROW diff_buf, JSAMPROW output_buf,
57 JDIMENSION width)
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;
62 int xindex;
64 for (xindex = 0; xindex < width; xindex++)
65 output_buf[xindex] = (JSAMPLE) RIGHT_SHIFT(diff_buf[xindex], scale_factor);
68 METHODDEF(void)
69 noscale(j_decompress_ptr cinfo,
70 JDIFFROW diff_buf, JSAMPROW output_buf,
71 JDIMENSION width)
73 int xindex;
75 for (xindex = 0; xindex < width; xindex++)
76 output_buf[xindex] = (JSAMPLE) diff_buf[xindex];
80 METHODDEF(void)
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;
85 int downscale;
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;
103 else
104 losslsd->scaler_scale = noscale;
108 JGLOBAL(void)
109 jinit_d_scaler (j_decompress_ptr cinfo)
111 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
112 scaler_ptr scaler;
114 scaler = (scaler_ptr)
115 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
116 SIZEOF(scaler));
117 losslsd->scaler_private = (void *) scaler;
118 losslsd->scaler_start_pass = scaler_start_pass;
121 #endif /* D_LOSSLESS_SUPPORTED */