|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* jcdctmgr.c
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This file was part of the Independent JPEG Group's software:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 1994-1996, Thomas G. Lane.
|
|
shun-iwasawa |
82a8f5 |
* libjpeg-turbo Modifications:
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 1999-2006, MIYASAKA Masaru.
|
|
shun-iwasawa |
82a8f5 |
* Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB</ossman@cendio.se>
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 2011, 2014-2015, D. R. Commander.
|
|
shun-iwasawa |
82a8f5 |
* For conditions of distribution and use, see the accompanying README.ijg
|
|
shun-iwasawa |
82a8f5 |
* file.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This file contains the forward-DCT management logic.
|
|
shun-iwasawa |
82a8f5 |
* This code selects a particular DCT implementation to be used,
|
|
shun-iwasawa |
82a8f5 |
* and it performs related housekeeping chores including coefficient
|
|
shun-iwasawa |
82a8f5 |
* quantization.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define JPEG_INTERNALS
|
|
shun-iwasawa |
82a8f5 |
#include "jinclude.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jpeglib.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jdct.h" /* Private declarations for DCT subsystem */
|
|
shun-iwasawa |
82a8f5 |
#include "jsimddct.h"
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Private subobject for this module */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef void (*forward_DCT_method_ptr) (DCTELEM *data);
|
|
shun-iwasawa |
82a8f5 |
typedef void (*float_DCT_method_ptr) (FAST_FLOAT *data);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef void (*convsamp_method_ptr) (JSAMPARRAY sample_data,
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION start_col,
|
|
shun-iwasawa |
82a8f5 |
DCTELEM *workspace);
|
|
shun-iwasawa |
82a8f5 |
typedef void (*float_convsamp_method_ptr) (JSAMPARRAY sample_data,
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION start_col,
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *workspace);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef void (*quantize_method_ptr) (JCOEFPTR coef_block, DCTELEM *divisors,
|
|
shun-iwasawa |
82a8f5 |
DCTELEM *workspace);
|
|
shun-iwasawa |
82a8f5 |
typedef void (*float_quantize_method_ptr) (JCOEFPTR coef_block,
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *divisors,
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *workspace);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void) quantize(JCOEFPTR, DCTELEM *, DCTELEM *);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef struct {
|
|
shun-iwasawa |
82a8f5 |
struct jpeg_forward_dct pub; /* public fields */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Pointer to the DCT routine actually in use */
|
|
shun-iwasawa |
82a8f5 |
forward_DCT_method_ptr dct;
|
|
shun-iwasawa |
82a8f5 |
convsamp_method_ptr convsamp;
|
|
shun-iwasawa |
82a8f5 |
quantize_method_ptr quantize;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* The actual post-DCT divisors --- not identical to the quant table
|
|
shun-iwasawa |
82a8f5 |
* entries, because of scaling (especially for an unnormalized DCT).
|
|
shun-iwasawa |
82a8f5 |
* Each table is given in normal array order.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
DCTELEM *divisors[NUM_QUANT_TBLS];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* work area for FDCT subroutine */
|
|
shun-iwasawa |
82a8f5 |
DCTELEM *workspace;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_FLOAT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
/* Same as above for the floating-point case. */
|
|
shun-iwasawa |
82a8f5 |
float_DCT_method_ptr float_dct;
|
|
shun-iwasawa |
82a8f5 |
float_convsamp_method_ptr float_convsamp;
|
|
shun-iwasawa |
82a8f5 |
float_quantize_method_ptr float_quantize;
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *float_divisors[NUM_QUANT_TBLS];
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *float_workspace;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
} my_fdct_controller;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
typedef my_fdct_controller *my_fdct_ptr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#if BITS_IN_JSAMPLE == 8
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Find the highest bit in an integer through binary search.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(int)
|
|
shun-iwasawa |
82a8f5 |
flss(UINT16 val)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
int bit;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
bit = 16;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (!val)
|
|
shun-iwasawa |
82a8f5 |
return 0;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (!(val & 0xff00)) {
|
|
shun-iwasawa |
82a8f5 |
bit -= 8;
|
|
shun-iwasawa |
82a8f5 |
val <<= 8;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
if (!(val & 0xf000)) {
|
|
shun-iwasawa |
82a8f5 |
bit -= 4;
|
|
shun-iwasawa |
82a8f5 |
val <<= 4;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
if (!(val & 0xc000)) {
|
|
shun-iwasawa |
82a8f5 |
bit -= 2;
|
|
shun-iwasawa |
82a8f5 |
val <<= 2;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
if (!(val & 0x8000)) {
|
|
shun-iwasawa |
82a8f5 |
bit -= 1;
|
|
shun-iwasawa |
82a8f5 |
val <<= 1;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
return bit;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Compute values to do a division using reciprocal.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This implementation is based on an algorithm described in
|
|
shun-iwasawa |
82a8f5 |
* "How to optimize for the Pentium family of microprocessors"
|
|
shun-iwasawa |
82a8f5 |
* (http://www.agner.org/assem/).
|
|
shun-iwasawa |
82a8f5 |
* More information about the basic algorithm can be found in
|
|
shun-iwasawa |
82a8f5 |
* the paper "Integer Division Using Reciprocals" by Robert Alverson.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The basic idea is to replace x/d by x * d^-1. In order to store
|
|
shun-iwasawa |
82a8f5 |
* d^-1 with enough precision we shift it left a few places. It turns
|
|
shun-iwasawa |
82a8f5 |
* out that this algoright gives just enough precision, and also fits
|
|
shun-iwasawa |
82a8f5 |
* into DCTELEM:
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* b = (the number of significant bits in divisor) - 1
|
|
shun-iwasawa |
82a8f5 |
* r = (word size) + b
|
|
shun-iwasawa |
82a8f5 |
* f = 2^r / divisor
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* f will not be an integer for most cases, so we need to compensate
|
|
shun-iwasawa |
82a8f5 |
* for the rounding error introduced:
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* no fractional part:
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* result = input >> r
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* fractional part of f < 0.5:
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* round f down to nearest integer
|
|
shun-iwasawa |
82a8f5 |
* result = ((input + 1) * f) >> r
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* fractional part of f > 0.5:
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* round f up to nearest integer
|
|
shun-iwasawa |
82a8f5 |
* result = (input * f) >> r
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This is the original algorithm that gives truncated results. But we
|
|
shun-iwasawa |
82a8f5 |
* want properly rounded results, so we replace "input" with
|
|
shun-iwasawa |
82a8f5 |
* "input + divisor/2".
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* In order to allow SIMD implementations we also tweak the values to
|
|
shun-iwasawa |
82a8f5 |
* allow the same calculation to be made at all times:
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* dctbl[0] = f rounded to nearest integer
|
|
shun-iwasawa |
82a8f5 |
* dctbl[1] = divisor / 2 (+ 1 if fractional part of f < 0.5)
|
|
shun-iwasawa |
82a8f5 |
* dctbl[2] = 1 << ((word size) * 2 - r)
|
|
shun-iwasawa |
82a8f5 |
* dctbl[3] = r - (word size)
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* dctbl[2] is for stupid instruction sets where the shift operation
|
|
shun-iwasawa |
82a8f5 |
* isn't member wise (e.g. MMX).
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The reason dctbl[2] and dctbl[3] reduce the shift with (word size)
|
|
shun-iwasawa |
82a8f5 |
* is that most SIMD implementations have a "multiply and store top
|
|
shun-iwasawa |
82a8f5 |
* half" operation.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Lastly, we store each of the values in their own table instead
|
|
shun-iwasawa |
82a8f5 |
* of in a consecutive manner, yet again in order to allow SIMD
|
|
shun-iwasawa |
82a8f5 |
* routines.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
LOCAL(int)
|
|
shun-iwasawa |
82a8f5 |
compute_reciprocal(UINT16 divisor, DCTELEM *dtbl)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
UDCTELEM2 fq, fr;
|
|
shun-iwasawa |
82a8f5 |
UDCTELEM c;
|
|
shun-iwasawa |
82a8f5 |
int b, r;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (divisor == 1) {
|
|
shun-iwasawa |
82a8f5 |
/* divisor == 1 means unquantized, so these reciprocal/correction/shift
|
|
shun-iwasawa |
82a8f5 |
* values will cause the C quantization algorithm to act like the
|
|
shun-iwasawa |
82a8f5 |
* identity function. Since only the C quantization algorithm is used in
|
|
shun-iwasawa |
82a8f5 |
* these cases, the scale value is irrelevant.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 0] = (DCTELEM)1; /* reciprocal */
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 1] = (DCTELEM)0; /* correction */
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 2] = (DCTELEM)1; /* scale */
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 3] = -(DCTELEM)(sizeof(DCTELEM) * 8); /* shift */
|
|
shun-iwasawa |
82a8f5 |
return 0;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
b = flss(divisor) - 1;
|
|
shun-iwasawa |
82a8f5 |
r = sizeof(DCTELEM) * 8 + b;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
fq = ((UDCTELEM2)1 << r) / divisor;
|
|
shun-iwasawa |
82a8f5 |
fr = ((UDCTELEM2)1 << r) % divisor;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
c = divisor / 2; /* for rounding */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (fr == 0) { /* divisor is power of two */
|
|
shun-iwasawa |
82a8f5 |
/* fq will be one bit too large to fit in DCTELEM, so adjust */
|
|
shun-iwasawa |
82a8f5 |
fq >>= 1;
|
|
shun-iwasawa |
82a8f5 |
r--;
|
|
shun-iwasawa |
82a8f5 |
} else if (fr <= (divisor / 2U)) { /* fractional part is < 0.5 */
|
|
shun-iwasawa |
82a8f5 |
c++;
|
|
shun-iwasawa |
82a8f5 |
} else { /* fractional part is > 0.5 */
|
|
shun-iwasawa |
82a8f5 |
fq++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 0] = (DCTELEM)fq; /* reciprocal */
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 1] = (DCTELEM)c; /* correction + roundfactor */
|
|
shun-iwasawa |
82a8f5 |
#ifdef WITH_SIMD
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 2] = (DCTELEM)(1 << (sizeof(DCTELEM) * 8 * 2 - r)); /* scale */
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 2] = 1;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
dtbl[DCTSIZE2 * 3] = (DCTELEM)r - sizeof(DCTELEM) * 8; /* shift */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (r <= 16) return 0;
|
|
shun-iwasawa |
82a8f5 |
else return 1;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Initialize for a processing pass.
|
|
shun-iwasawa |
82a8f5 |
* Verify that all referenced Q-tables are present, and set up
|
|
shun-iwasawa |
82a8f5 |
* the divisor table for each one.
|
|
shun-iwasawa |
82a8f5 |
* In the current implementation, DCT of all components is done during
|
|
shun-iwasawa |
82a8f5 |
* the first pass, even if only some components will be output in the
|
|
shun-iwasawa |
82a8f5 |
* first scan. Hence all components should be examined here.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
start_pass_fdctmgr(j_compress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
|
|
shun-iwasawa |
82a8f5 |
int ci, qtblno, i;
|
|
shun-iwasawa |
82a8f5 |
jpeg_component_info *compptr;
|
|
shun-iwasawa |
82a8f5 |
JQUANT_TBL *qtbl;
|
|
shun-iwasawa |
82a8f5 |
DCTELEM *dtbl;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
|
shun-iwasawa |
82a8f5 |
ci++, compptr++) {
|
|
shun-iwasawa |
82a8f5 |
qtblno = compptr->quant_tbl_no;
|
|
shun-iwasawa |
82a8f5 |
/* Make sure specified quantization table is present */
|
|
shun-iwasawa |
82a8f5 |
if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
|
|
shun-iwasawa |
82a8f5 |
cinfo->quant_tbl_ptrs[qtblno] == NULL)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
|
|
shun-iwasawa |
82a8f5 |
qtbl = cinfo->quant_tbl_ptrs[qtblno];
|
|
shun-iwasawa |
82a8f5 |
/* Compute divisors for this quant table */
|
|
shun-iwasawa |
82a8f5 |
/* We may do this more than once for same table, but it's not a big deal */
|
|
shun-iwasawa |
82a8f5 |
switch (cinfo->dct_method) {
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_ISLOW_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_ISLOW:
|
|
shun-iwasawa |
82a8f5 |
/* For LL&M IDCT method, divisors are equal to raw quantization
|
|
shun-iwasawa |
82a8f5 |
* coefficients multiplied by 8 (to counteract scaling).
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
if (fdct->divisors[qtblno] == NULL) {
|
|
shun-iwasawa |
82a8f5 |
fdct->divisors[qtblno] = (DCTELEM *)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
(DCTSIZE2 * 4) * sizeof(DCTELEM));
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
dtbl = fdct->divisors[qtblno];
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < DCTSIZE2; i++) {
|
|
shun-iwasawa |
82a8f5 |
#if BITS_IN_JSAMPLE == 8
|
|
shun-iwasawa |
82a8f5 |
if (!compute_reciprocal(qtbl->quantval[i] << 3, &dtbl[i]) &&
|
|
shun-iwasawa |
82a8f5 |
fdct->quantize == jsimd_quantize)
|
|
shun-iwasawa |
82a8f5 |
fdct->quantize = quantize;
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
dtbl[i] = ((DCTELEM)qtbl->quantval[i]) << 3;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_IFAST_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_IFAST:
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
/* For AA&N IDCT method, divisors are equal to quantization
|
|
shun-iwasawa |
82a8f5 |
* coefficients scaled by scalefactor[row]*scalefactor[col], where
|
|
shun-iwasawa |
82a8f5 |
* scalefactor[0] = 1
|
|
shun-iwasawa |
82a8f5 |
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
|
|
shun-iwasawa |
82a8f5 |
* We apply a further scale factor of 8.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
#define CONST_BITS 14
|
|
shun-iwasawa |
82a8f5 |
static const INT16 aanscales[DCTSIZE2] = {
|
|
shun-iwasawa |
82a8f5 |
/* precomputed values scaled up by 14 bits */
|
|
shun-iwasawa |
82a8f5 |
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
|
|
shun-iwasawa |
82a8f5 |
22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
|
|
shun-iwasawa |
82a8f5 |
21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
|
|
shun-iwasawa |
82a8f5 |
19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
|
|
shun-iwasawa |
82a8f5 |
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
|
|
shun-iwasawa |
82a8f5 |
12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
|
|
shun-iwasawa |
82a8f5 |
8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
|
|
shun-iwasawa |
82a8f5 |
4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
|
|
shun-iwasawa |
82a8f5 |
};
|
|
shun-iwasawa |
82a8f5 |
SHIFT_TEMPS
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (fdct->divisors[qtblno] == NULL) {
|
|
shun-iwasawa |
82a8f5 |
fdct->divisors[qtblno] = (DCTELEM *)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
(DCTSIZE2 * 4) * sizeof(DCTELEM));
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
dtbl = fdct->divisors[qtblno];
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < DCTSIZE2; i++) {
|
|
shun-iwasawa |
82a8f5 |
#if BITS_IN_JSAMPLE == 8
|
|
shun-iwasawa |
82a8f5 |
if (!compute_reciprocal(
|
|
shun-iwasawa |
82a8f5 |
DESCALE(MULTIPLY16V16((JLONG)qtbl->quantval[i],
|
|
shun-iwasawa |
82a8f5 |
(JLONG)aanscales[i]),
|
|
shun-iwasawa |
82a8f5 |
CONST_BITS - 3), &dtbl[i]) &&
|
|
shun-iwasawa |
82a8f5 |
fdct->quantize == jsimd_quantize)
|
|
shun-iwasawa |
82a8f5 |
fdct->quantize = quantize;
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
dtbl[i] = (DCTELEM)
|
|
shun-iwasawa |
82a8f5 |
DESCALE(MULTIPLY16V16((JLONG)qtbl->quantval[i],
|
|
shun-iwasawa |
82a8f5 |
(JLONG)aanscales[i]),
|
|
shun-iwasawa |
82a8f5 |
CONST_BITS - 3);
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_FLOAT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_FLOAT:
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
/* For float AA&N IDCT method, divisors are equal to quantization
|
|
shun-iwasawa |
82a8f5 |
* coefficients scaled by scalefactor[row]*scalefactor[col], where
|
|
shun-iwasawa |
82a8f5 |
* scalefactor[0] = 1
|
|
shun-iwasawa |
82a8f5 |
* scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
|
|
shun-iwasawa |
82a8f5 |
* We apply a further scale factor of 8.
|
|
shun-iwasawa |
82a8f5 |
* What's actually stored is 1/divisor so that the inner loop can
|
|
shun-iwasawa |
82a8f5 |
* use a multiplication rather than a division.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *fdtbl;
|
|
shun-iwasawa |
82a8f5 |
int row, col;
|
|
shun-iwasawa |
82a8f5 |
static const double aanscalefactor[DCTSIZE] = {
|
|
shun-iwasawa |
82a8f5 |
1.0, 1.387039845, 1.306562965, 1.175875602,
|
|
shun-iwasawa |
82a8f5 |
1.0, 0.785694958, 0.541196100, 0.275899379
|
|
shun-iwasawa |
82a8f5 |
};
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (fdct->float_divisors[qtblno] == NULL) {
|
|
shun-iwasawa |
82a8f5 |
fdct->float_divisors[qtblno] = (FAST_FLOAT *)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
DCTSIZE2 * sizeof(FAST_FLOAT));
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
fdtbl = fdct->float_divisors[qtblno];
|
|
shun-iwasawa |
82a8f5 |
i = 0;
|
|
shun-iwasawa |
82a8f5 |
for (row = 0; row < DCTSIZE; row++) {
|
|
shun-iwasawa |
82a8f5 |
for (col = 0; col < DCTSIZE; col++) {
|
|
shun-iwasawa |
82a8f5 |
fdtbl[i] = (FAST_FLOAT)
|
|
shun-iwasawa |
82a8f5 |
(1.0 / (((double)qtbl->quantval[i] *
|
|
shun-iwasawa |
82a8f5 |
aanscalefactor[row] * aanscalefactor[col] * 8.0)));
|
|
shun-iwasawa |
82a8f5 |
i++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
default:
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Load data into workspace, applying unsigned->signed conversion.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
convsamp(JSAMPARRAY sample_data, JDIMENSION start_col, DCTELEM *workspace)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
register DCTELEM *workspaceptr;
|
|
shun-iwasawa |
82a8f5 |
register JSAMPROW elemptr;
|
|
shun-iwasawa |
82a8f5 |
register int elemr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
workspaceptr = workspace;
|
|
shun-iwasawa |
82a8f5 |
for (elemr = 0; elemr < DCTSIZE; elemr++) {
|
|
shun-iwasawa |
82a8f5 |
elemptr = sample_data[elemr] + start_col;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#if DCTSIZE == 8 /* unroll the inner loop */
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
register int elemc;
|
|
shun-iwasawa |
82a8f5 |
for (elemc = DCTSIZE; elemc > 0; elemc--)
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Quantize/descale the coefficients, and store into coef_blocks[].
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
quantize(JCOEFPTR coef_block, DCTELEM *divisors, DCTELEM *workspace)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
int i;
|
|
shun-iwasawa |
82a8f5 |
DCTELEM temp;
|
|
shun-iwasawa |
82a8f5 |
JCOEFPTR output_ptr = coef_block;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#if BITS_IN_JSAMPLE == 8
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
UDCTELEM recip, corr;
|
|
shun-iwasawa |
82a8f5 |
int shift;
|
|
shun-iwasawa |
82a8f5 |
UDCTELEM2 product;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < DCTSIZE2; i++) {
|
|
shun-iwasawa |
82a8f5 |
temp = workspace[i];
|
|
shun-iwasawa |
82a8f5 |
recip = divisors[i + DCTSIZE2 * 0];
|
|
shun-iwasawa |
82a8f5 |
corr = divisors[i + DCTSIZE2 * 1];
|
|
shun-iwasawa |
82a8f5 |
shift = divisors[i + DCTSIZE2 * 3];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (temp < 0) {
|
|
shun-iwasawa |
82a8f5 |
temp = -temp;
|
|
shun-iwasawa |
82a8f5 |
product = (UDCTELEM2)(temp + corr) * recip;
|
|
shun-iwasawa |
82a8f5 |
product >>= shift + sizeof(DCTELEM) * 8;
|
|
shun-iwasawa |
82a8f5 |
temp = (DCTELEM)product;
|
|
shun-iwasawa |
82a8f5 |
temp = -temp;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
product = (UDCTELEM2)(temp + corr) * recip;
|
|
shun-iwasawa |
82a8f5 |
product >>= shift + sizeof(DCTELEM) * 8;
|
|
shun-iwasawa |
82a8f5 |
temp = (DCTELEM)product;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
output_ptr[i] = (JCOEF)temp;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
register DCTELEM qval;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < DCTSIZE2; i++) {
|
|
shun-iwasawa |
82a8f5 |
qval = divisors[i];
|
|
shun-iwasawa |
82a8f5 |
temp = workspace[i];
|
|
shun-iwasawa |
82a8f5 |
/* Divide the coefficient value by qval, ensuring proper rounding.
|
|
shun-iwasawa |
82a8f5 |
* Since C does not specify the direction of rounding for negative
|
|
shun-iwasawa |
82a8f5 |
* quotients, we have to force the dividend positive for portability.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* In most files, at least half of the output values will be zero
|
|
shun-iwasawa |
82a8f5 |
* (at default quantization settings, more like three-quarters...)
|
|
shun-iwasawa |
82a8f5 |
* so we should ensure that this case is fast. On many machines,
|
|
shun-iwasawa |
82a8f5 |
* a comparison is enough cheaper than a divide to make a special test
|
|
shun-iwasawa |
82a8f5 |
* a win. Since both inputs will be nonnegative, we need only test
|
|
shun-iwasawa |
82a8f5 |
* for a < b to discover whether a/b is 0.
|
|
shun-iwasawa |
82a8f5 |
* If your machine's division is fast enough, define FAST_DIVIDE.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
#ifdef FAST_DIVIDE
|
|
shun-iwasawa |
82a8f5 |
#define DIVIDE_BY(a, b) a /= b
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
#define DIVIDE_BY(a, b) if (a >= b) a /= b; else a = 0
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
if (temp < 0) {
|
|
shun-iwasawa |
82a8f5 |
temp = -temp;
|
|
shun-iwasawa |
82a8f5 |
temp += qval >> 1; /* for rounding */
|
|
shun-iwasawa |
82a8f5 |
DIVIDE_BY(temp, qval);
|
|
shun-iwasawa |
82a8f5 |
temp = -temp;
|
|
shun-iwasawa |
82a8f5 |
} else {
|
|
shun-iwasawa |
82a8f5 |
temp += qval >> 1; /* for rounding */
|
|
shun-iwasawa |
82a8f5 |
DIVIDE_BY(temp, qval);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
output_ptr[i] = (JCOEF)temp;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Perform forward DCT on one or more blocks of a component.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* The input samples are taken from the sample_data[] array starting at
|
|
shun-iwasawa |
82a8f5 |
* position start_row/start_col, and moving to the right for any additional
|
|
shun-iwasawa |
82a8f5 |
* blocks. The quantized coefficients are returned in coef_blocks[].
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
forward_DCT(j_compress_ptr cinfo, jpeg_component_info *compptr,
|
|
shun-iwasawa |
82a8f5 |
JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION start_row, JDIMENSION start_col, JDIMENSION num_blocks)
|
|
shun-iwasawa |
82a8f5 |
/* This version is used for integer DCT implementations. */
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
/* This routine is heavily used, so it's worth coding it tightly. */
|
|
shun-iwasawa |
82a8f5 |
my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
|
|
shun-iwasawa |
82a8f5 |
DCTELEM *divisors = fdct->divisors[compptr->quant_tbl_no];
|
|
shun-iwasawa |
82a8f5 |
DCTELEM *workspace;
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION bi;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Make sure the compiler doesn't look up these every pass */
|
|
shun-iwasawa |
82a8f5 |
forward_DCT_method_ptr do_dct = fdct->dct;
|
|
shun-iwasawa |
82a8f5 |
convsamp_method_ptr do_convsamp = fdct->convsamp;
|
|
shun-iwasawa |
82a8f5 |
quantize_method_ptr do_quantize = fdct->quantize;
|
|
shun-iwasawa |
82a8f5 |
workspace = fdct->workspace;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
sample_data += start_row; /* fold in the vertical offset once */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
|
|
shun-iwasawa |
82a8f5 |
/* Load data into workspace, applying unsigned->signed conversion */
|
|
shun-iwasawa |
82a8f5 |
(*do_convsamp) (sample_data, start_col, workspace);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Perform the DCT */
|
|
shun-iwasawa |
82a8f5 |
(*do_dct) (workspace);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Quantize/descale the coefficients, and store into coef_blocks[] */
|
|
shun-iwasawa |
82a8f5 |
(*do_quantize) (coef_blocks[bi], divisors, workspace);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_FLOAT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
convsamp_float(JSAMPARRAY sample_data, JDIMENSION start_col,
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *workspace)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
register FAST_FLOAT *workspaceptr;
|
|
shun-iwasawa |
82a8f5 |
register JSAMPROW elemptr;
|
|
shun-iwasawa |
82a8f5 |
register int elemr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
workspaceptr = workspace;
|
|
shun-iwasawa |
82a8f5 |
for (elemr = 0; elemr < DCTSIZE; elemr++) {
|
|
shun-iwasawa |
82a8f5 |
elemptr = sample_data[elemr] + start_col;
|
|
shun-iwasawa |
82a8f5 |
#if DCTSIZE == 8 /* unroll the inner loop */
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
#else
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
register int elemc;
|
|
shun-iwasawa |
82a8f5 |
for (elemc = DCTSIZE; elemc > 0; elemc--)
|
|
shun-iwasawa |
82a8f5 |
*workspaceptr++ = (FAST_FLOAT)
|
|
shun-iwasawa |
82a8f5 |
(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
quantize_float(JCOEFPTR coef_block, FAST_FLOAT *divisors,
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *workspace)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
register FAST_FLOAT temp;
|
|
shun-iwasawa |
82a8f5 |
register int i;
|
|
shun-iwasawa |
82a8f5 |
register JCOEFPTR output_ptr = coef_block;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < DCTSIZE2; i++) {
|
|
shun-iwasawa |
82a8f5 |
/* Apply the quantization and scaling factor */
|
|
shun-iwasawa |
82a8f5 |
temp = workspace[i] * divisors[i];
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Round to nearest integer.
|
|
shun-iwasawa |
82a8f5 |
* Since C does not specify the direction of rounding for negative
|
|
shun-iwasawa |
82a8f5 |
* quotients, we have to force the dividend positive for portability.
|
|
shun-iwasawa |
82a8f5 |
* The maximum coefficient size is +-16K (for 12-bit data), so this
|
|
shun-iwasawa |
82a8f5 |
* code should work for either 16-bit or 32-bit ints.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
output_ptr[i] = (JCOEF)((int)(temp + (FAST_FLOAT)16384.5) - 16384);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
METHODDEF(void)
|
|
shun-iwasawa |
82a8f5 |
forward_DCT_float(j_compress_ptr cinfo, jpeg_component_info *compptr,
|
|
shun-iwasawa |
82a8f5 |
JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION start_row, JDIMENSION start_col,
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION num_blocks)
|
|
shun-iwasawa |
82a8f5 |
/* This version is used for floating-point DCT implementations. */
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
/* This routine is heavily used, so it's worth coding it tightly. */
|
|
shun-iwasawa |
82a8f5 |
my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *divisors = fdct->float_divisors[compptr->quant_tbl_no];
|
|
shun-iwasawa |
82a8f5 |
FAST_FLOAT *workspace;
|
|
shun-iwasawa |
82a8f5 |
JDIMENSION bi;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Make sure the compiler doesn't look up these every pass */
|
|
shun-iwasawa |
82a8f5 |
float_DCT_method_ptr do_dct = fdct->float_dct;
|
|
shun-iwasawa |
82a8f5 |
float_convsamp_method_ptr do_convsamp = fdct->float_convsamp;
|
|
shun-iwasawa |
82a8f5 |
float_quantize_method_ptr do_quantize = fdct->float_quantize;
|
|
shun-iwasawa |
82a8f5 |
workspace = fdct->float_workspace;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
sample_data += start_row; /* fold in the vertical offset once */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
|
|
shun-iwasawa |
82a8f5 |
/* Load data into workspace, applying unsigned->signed conversion */
|
|
shun-iwasawa |
82a8f5 |
(*do_convsamp) (sample_data, start_col, workspace);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Perform the DCT */
|
|
shun-iwasawa |
82a8f5 |
(*do_dct) (workspace);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Quantize/descale the coefficients, and store into coef_blocks[] */
|
|
shun-iwasawa |
82a8f5 |
(*do_quantize) (coef_blocks[bi], divisors, workspace);
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#endif /* DCT_FLOAT_SUPPORTED */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Initialize FDCT manager.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jinit_forward_dct(j_compress_ptr cinfo)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
my_fdct_ptr fdct;
|
|
shun-iwasawa |
82a8f5 |
int i;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
fdct = (my_fdct_ptr)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
sizeof(my_fdct_controller));
|
|
shun-iwasawa |
82a8f5 |
cinfo->fdct = (struct jpeg_forward_dct *)fdct;
|
|
shun-iwasawa |
82a8f5 |
fdct->pub.start_pass = start_pass_fdctmgr;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* First determine the DCT... */
|
|
shun-iwasawa |
82a8f5 |
switch (cinfo->dct_method) {
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_ISLOW_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_ISLOW:
|
|
shun-iwasawa |
82a8f5 |
fdct->pub.forward_DCT = forward_DCT;
|
|
shun-iwasawa |
82a8f5 |
if (jsimd_can_fdct_islow())
|
|
shun-iwasawa |
82a8f5 |
fdct->dct = jsimd_fdct_islow;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
fdct->dct = jpeg_fdct_islow;
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_IFAST_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_IFAST:
|
|
shun-iwasawa |
82a8f5 |
fdct->pub.forward_DCT = forward_DCT;
|
|
shun-iwasawa |
82a8f5 |
if (jsimd_can_fdct_ifast())
|
|
shun-iwasawa |
82a8f5 |
fdct->dct = jsimd_fdct_ifast;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
fdct->dct = jpeg_fdct_ifast;
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_FLOAT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_FLOAT:
|
|
shun-iwasawa |
82a8f5 |
fdct->pub.forward_DCT = forward_DCT_float;
|
|
shun-iwasawa |
82a8f5 |
if (jsimd_can_fdct_float())
|
|
shun-iwasawa |
82a8f5 |
fdct->float_dct = jsimd_fdct_float;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
fdct->float_dct = jpeg_fdct_float;
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
default:
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* ...then the supporting stages. */
|
|
shun-iwasawa |
82a8f5 |
switch (cinfo->dct_method) {
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_ISLOW_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_ISLOW:
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_IFAST_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_IFAST:
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#if defined(DCT_ISLOW_SUPPORTED) || defined(DCT_IFAST_SUPPORTED)
|
|
shun-iwasawa |
82a8f5 |
if (jsimd_can_convsamp())
|
|
shun-iwasawa |
82a8f5 |
fdct->convsamp = jsimd_convsamp;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
fdct->convsamp = convsamp;
|
|
shun-iwasawa |
82a8f5 |
if (jsimd_can_quantize())
|
|
shun-iwasawa |
82a8f5 |
fdct->quantize = jsimd_quantize;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
fdct->quantize = quantize;
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_FLOAT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
case JDCT_FLOAT:
|
|
shun-iwasawa |
82a8f5 |
if (jsimd_can_convsamp_float())
|
|
shun-iwasawa |
82a8f5 |
fdct->float_convsamp = jsimd_convsamp_float;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
fdct->float_convsamp = convsamp_float;
|
|
shun-iwasawa |
82a8f5 |
if (jsimd_can_quantize_float())
|
|
shun-iwasawa |
82a8f5 |
fdct->float_quantize = jsimd_quantize_float;
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
fdct->float_quantize = quantize_float;
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
default:
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
shun-iwasawa |
82a8f5 |
break;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Allocate workspace memory */
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_FLOAT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->dct_method == JDCT_FLOAT)
|
|
shun-iwasawa |
82a8f5 |
fdct->float_workspace = (FAST_FLOAT *)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
sizeof(FAST_FLOAT) * DCTSIZE2);
|
|
shun-iwasawa |
82a8f5 |
else
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
fdct->workspace = (DCTELEM *)
|
|
shun-iwasawa |
82a8f5 |
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
|
|
shun-iwasawa |
82a8f5 |
sizeof(DCTELEM) * DCTSIZE2);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Mark divisor tables unallocated */
|
|
shun-iwasawa |
82a8f5 |
for (i = 0; i < NUM_QUANT_TBLS; i++) {
|
|
shun-iwasawa |
82a8f5 |
fdct->divisors[i] = NULL;
|
|
shun-iwasawa |
82a8f5 |
#ifdef DCT_FLOAT_SUPPORTED
|
|
shun-iwasawa |
82a8f5 |
fdct->float_divisors[i] = NULL;
|
|
shun-iwasawa |
82a8f5 |
#endif
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|