📄 region_decompressor.cpp
字号:
/******************************************************************************/// File: region_decompressor.cpp [scope = APPS/SHOW]// Version: Kakadu, V2.2// Author: David Taubman// Last Revised: 20 June, 2001/******************************************************************************/// Copyright 2001, David Taubman, The University of New South Wales (UNSW)// The copyright owner is Unisearch Ltd, Australia (commercial arm of UNSW)// Neither this copyright statement, nor the licensing details below// may be removed from this file or dissociated from its contents./******************************************************************************/// Licensee: Book Owner// License number: 99999// The Licensee has been granted a NON-COMMERCIAL license to the contents of// this source file, said Licensee being the owner of a copy of the book,// "JPEG2000: Image Compression Fundamentals, Standards and Practice," by// Taubman and Marcellin (Kluwer Academic Publishers, 2001). A brief summary// of the license appears below. This summary is not to be relied upon in// preference to the full text of the license agreement, which was accepted// upon breaking the seal of the compact disc accompanying the above-mentioned// book.// 1. The Licensee has the right to Non-Commercial Use of the Kakadu software,// Version 2.2, including distribution of one or more Applications built// using the software, provided such distribution is not for financial// return.// 2. The Licensee has the right to personal use of the Kakadu software,// Version 2.2.// 3. The Licensee has the right to distribute Reusable Code (including// source code and dynamically or statically linked libraries) to a Third// Party, provided the Third Party possesses a license to use the Kakadu// software, Version 2.2, and provided such distribution is not for// financial return./*******************************************************************************Description: Implements the incremental, region-based decompression services of the"kd_region_decompressor" object. These services should prove usefulto many interactive applications which require JPEG2000 rendering capabilities.*******************************************************************************/#include <assert.h>#include "kdu_utils.h"#include "kdu_compressed.h"#include "kdu_sample_processing.h"#include "region_decompressor.h"/* ========================================================================== *//* Internal Functions *//* ========================================================================== *//******************************************************************************//* STATIC reset_line *//******************************************************************************/void reset_line_buf(kdu_line_buf &buf){ int num_samples = buf.get_width(); if (buf.get_buf32() != NULL) { assert(buf.is_absolute()); kdu_sample32 *sp = buf.get_buf32(); while (num_samples--) (sp++)->ival = 0; } else { kdu_sample16 *sp = buf.get_buf16(); while (num_samples--) (sp++)->ival = 0; }}/*****************************************************************************//* STATIC convert_samples_to_palette_indices *//*****************************************************************************/static void convert_samples_to_palette_indices(kdu_line_buf &line, int bit_depth, bool is_signed, int palette_bits){ int i=line.get_width(); if (line.get_buf32() != NULL) { assert(line.is_absolute()); kdu_sample32 *sp = line.get_buf32(); kdu_int32 offset = (is_signed)?0:((1<<bit_depth)>>1); kdu_int32 mask = ((kdu_int32)(-1))<<palette_bits; kdu_int32 val; for (; i > 0; i--, sp++) { val = sp->ival + offset; if (val & mask) val = (val<0)?0:(~mask); sp->ival = val; } } else if (line.is_absolute()) { kdu_sample16 *sp = line.get_buf16(); kdu_int16 offset = (kdu_int16)((is_signed)?0:((1<<bit_depth)>>1)); kdu_int16 mask = ((kdu_int16)(-1))<<palette_bits; kdu_int16 val; for (; i > 0; i--, sp++) { val = sp->ival + offset; if (val & mask) val = (val<0)?0:(~mask); sp->ival = val; } } else { kdu_sample16 *sp = line.get_buf16(); kdu_int16 offset = (kdu_int16)((is_signed)?0:((1<<KDU_FIX_POINT)>>1)); int downshift = KDU_FIX_POINT-palette_bits; assert(downshift > 0); offset += (kdu_int16)((1<<downshift)>>1); kdu_int32 mask = ((kdu_int16)(-1))<<palette_bits; kdu_int16 val; for (; i > 0; i--, sp++) { val = (sp->ival + offset) >> downshift; if (val & mask) val = (val<0)?0:(~mask); sp->ival = val; } }}/******************************************************************************//* STATIC interpolate_and_map *//******************************************************************************/static void interpolate_and_map(kdu_line_buf &src, int interp_first, int interp_factor, kdu_sample16 *lut, kdu_line_buf &dst, int skip_cols, int num_cols) /* This function maps the elements of the `src' buffer through the supplied lookup table (the `src' samples are indices to the `lut' array) to produce 16-bit fixed point output samples, stored in the `dst' buffer. At the same time, the function interpolates the sources samples, using a simple zero-order hold (nearest neighbour) policy. The interpolation factor is given by `interp_factor'. In this way, each input sample nominally produces `interp_factor' output samples. The first input sample produces only `interp_first' output samples. This allows for the fact that the interpolation might start from any point. It can happen that `interp_first' is larger than `interp_facto', in which case some extrapolation is involved at the left boundary. Similarly, some extrapolation may be required at the right boundary if there are insufficient source samples. If there are no source samples at all, they are extrapolated as 0, although this should not happen in a well constructed code-stream (it may happen if a tiled image is cropped down -- another reason to avoid tiling). Notionally, the line is interpolated as described above and then the first `skip_cols' samples are discarded before writing the result into the `dst' buffer. The number of output samples to be written to `dst' is given by `num_cols'. */{ kdu_sample16 val, *dp = dst.get_buf16(); assert((dp != NULL) && !dst.is_absolute()); int in_cols = src.get_width(); if (in_cols == 0) { // No data available. Synthesize 0's. for (; num_cols > 0; num_cols--) *(dp++) = lut[0]; return; } // Determine the source offset and the number of outputs from the first input interp_first -= skip_cols; int src_offset = 0; for (; interp_first <= 0; interp_first += interp_factor) { src_offset++; in_cols--; } if (in_cols <= 0) { src_offset += (in_cols-1); in_cols = 1; } if (interp_first > num_cols) interp_first = num_cols; num_cols -= interp_first; // Number of samples left to be interpolated // Determine the number of input samples which expand fully to exactly // `interp_factor' output samples. int fast_count = num_cols / interp_factor; if (fast_count > (in_cols-2)) fast_count = in_cols-2; // Convert `num_cols' to mean the number of remaining output samples which // should all be set equal to the last input sample. if (fast_count > 0) num_cols -= fast_count * interp_factor; // Now for the processing int k; if (src.get_buf32() != NULL) { // Indices are 32-bit integers kdu_sample32 *sp = src.get_buf32() + src_offset; for (val=lut[(sp++)->ival]; interp_first > 0; interp_first--) *(dp++) = val; if (interp_factor == 1) for (; fast_count > 0; fast_count--) *(dp++) = val = lut[(sp++)->ival]; else for (; fast_count > 0; fast_count--) for (val=lut[(sp++)->ival], k=interp_factor; k > 0; k--) *(dp++) = val; if (fast_count == 0) val = lut[sp->ival]; // Otherwise, we had only one input sample. for (; num_cols > 0; num_cols--) *(dp++) = val; } else { // Indices are 16-bit integers kdu_sample16 *sp = src.get_buf16() + src_offset; for (val=lut[(sp++)->ival]; interp_first > 0; interp_first--) *(dp++) = val; if (interp_factor == 1) for (; fast_count > 0; fast_count--) *(dp++) = val = lut[(sp++)->ival]; else for (; fast_count > 0; fast_count--) for (val=lut[(sp++)->ival], k=interp_factor; k > 0; k--) *(dp++) = val; if (fast_count == 0) val = lut[sp->ival]; // Otherwise, we had only one input sample. for (; num_cols > 0; num_cols--) *(dp++) = val; }}/******************************************************************************//* STATIC interpolate_and_convert *//******************************************************************************/static void interpolate_and_convert(kdu_line_buf &src, int interp_first, int interp_factor, int bit_depth, kdu_line_buf &dst, int skip_cols, int num_cols) /* Same as `interpolate_and_map' except that there is no lookup table and the source values may need to be converted to the 16-bit fixed point output data type. */{ kdu_sample16 *dp = dst.get_buf16(); assert((dp != NULL) && !dst.is_absolute()); int in_cols = src.get_width(); if (in_cols == 0) { // No data available. Synthesize 0's. for (; num_cols > 0; num_cols--) (dp++)->ival = 0; return; } // Determine the source offset and the number of outputs from the first input interp_first -= skip_cols; int src_offset = 0; for (; interp_first <= 0; interp_first += interp_factor) { src_offset++; in_cols--; } if (in_cols <= 0) { src_offset += (in_cols-1); in_cols = 1; } if (interp_first > num_cols) interp_first = num_cols; num_cols -= interp_first; // Number of samples left to be interpolated // Determine the number of input samples which expand fully to exactly // `interp_factor' output samples. int fast_count = num_cols / interp_factor; if (fast_count > (in_cols-2)) fast_count = in_cols-2; // Convert `num_cols' to mean the number of remaining output samples which // should all be set equal to the last input sample. if (fast_count > 0) num_cols -= fast_count * interp_factor; // Now for the processing int k; if (!src.is_absolute()) { // Source already contains 16-bit fixed point numbers kdu_sample16 val, *sp = src.get_buf16(); assert(sp != NULL); sp += src_offset; for (val=*(sp++); interp_first > 0; interp_first--) *(dp++) = val;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -