mirror of https://github.com/opencv/opencv.git
parent
35a869f6f0
commit
d28af08ef3
58 changed files with 15828 additions and 5624 deletions
@ -0,0 +1,346 @@ |
||||
CHANGE LOG for Independent JPEG Group's JPEG software |
||||
|
||||
|
||||
Version 8d 15-Jan-2012 |
||||
----------------------- |
||||
|
||||
Add cjpeg -rgb option to create RGB JPEG files. |
||||
Using this switch suppresses the conversion from RGB |
||||
colorspace input to the default YCbCr JPEG colorspace. |
||||
This feature allows true lossless JPEG coding of RGB color images. |
||||
The recommended command for this purpose is currently |
||||
cjpeg -rgb -block 1 -arithmetic. |
||||
SmartScale capable decoder (introduced with IJG JPEG 8) required. |
||||
Thank to Michael Koch for the initial suggestion. |
||||
|
||||
Add option to disable the region adjustment in the transupp crop code. |
||||
Thank to Jeffrey Friedl for the suggestion. |
||||
|
||||
Thank to Richard Jones and Edd Dawson for various minor corrections. |
||||
|
||||
Thank to Akim Demaille for configure.ac cleanup. |
||||
|
||||
|
||||
Version 8c 16-Jan-2011 |
||||
----------------------- |
||||
|
||||
Add option to compression library and cjpeg (-block N) to use |
||||
different DCT block size. |
||||
All N from 1 to 16 are possible. Default is 8 (baseline format). |
||||
Larger values produce higher compression, |
||||
smaller values produce higher quality. |
||||
SmartScale capable decoder (introduced with IJG JPEG 8) required. |
||||
|
||||
|
||||
Version 8b 16-May-2010 |
||||
----------------------- |
||||
|
||||
Repair problem in new memory source manager with corrupt JPEG data. |
||||
Thank to Ted Campbell and Samuel Chun for the report. |
||||
|
||||
Repair problem in Makefile.am test target. |
||||
Thank to anonymous user for the report. |
||||
|
||||
Support MinGW installation with automatic configure. |
||||
Thank to Volker Grabsch for the suggestion. |
||||
|
||||
|
||||
Version 8a 28-Feb-2010 |
||||
----------------------- |
||||
|
||||
Writing tables-only datastreams via jpeg_write_tables works again. |
||||
|
||||
Support 32-bit BMPs (RGB image with Alpha channel) for read in cjpeg. |
||||
Thank to Brett Blackham for the suggestion. |
||||
|
||||
Improve accuracy in floating point IDCT calculation. |
||||
Thank to Robert Hooke for the hint. |
||||
|
||||
|
||||
Version 8 10-Jan-2010 |
||||
---------------------- |
||||
|
||||
jpegtran now supports the same -scale option as djpeg for "lossless" resize. |
||||
An implementation of the JPEG SmartScale extension is required for this |
||||
feature. A (draft) specification of the JPEG SmartScale extension is |
||||
available as a contributed document at ITU and ISO. Revision 2 or later |
||||
of the document is required (latest document version is Revision 3). |
||||
The SmartScale extension will enable more features beside lossless resize |
||||
in future implementations, as described in the document (new compression |
||||
options). |
||||
|
||||
Add sanity check in BMP reader module to avoid cjpeg crash for empty input |
||||
image (thank to Isaev Ildar of ISP RAS, Moscow, RU for reporting this error). |
||||
|
||||
Add data source and destination managers for read from and write to |
||||
memory buffers. New API functions jpeg_mem_src and jpeg_mem_dest. |
||||
Thank to Roberto Boni from Italy for the suggestion. |
||||
|
||||
|
||||
Version 7 27-Jun-2009 |
||||
---------------------- |
||||
|
||||
New scaled DCTs implemented. |
||||
djpeg now supports scalings N/8 with all N from 1 to 16. |
||||
cjpeg now supports scalings 8/N with all N from 1 to 16. |
||||
Scaled DCTs with size larger than 8 are now also used for resolving the |
||||
common 2x2 chroma subsampling case without additional spatial resampling. |
||||
Separate spatial resampling for those kind of files is now only necessary |
||||
for N>8 scaling cases. |
||||
Furthermore, separate scaled DCT functions are provided for direct resolving |
||||
of the common asymmetric subsampling cases (2x1 and 1x2) without additional |
||||
spatial resampling. |
||||
|
||||
cjpeg -quality option has been extended for support of separate quality |
||||
settings for luminance and chrominance (or in general, for every provided |
||||
quantization table slot). |
||||
New API function jpeg_default_qtables() and q_scale_factor array in library. |
||||
|
||||
Added -nosmooth option to cjpeg, complementary to djpeg. |
||||
New variable "do_fancy_downsampling" in library, complement to fancy |
||||
upsampling. Fancy upsampling now uses direct DCT scaling with sizes |
||||
larger than 8. The old method is not reversible and has been removed. |
||||
|
||||
Support arithmetic entropy encoding and decoding. |
||||
Added files jaricom.c, jcarith.c, jdarith.c. |
||||
|
||||
Straighten the file structure: |
||||
Removed files jidctred.c, jcphuff.c, jchuff.h, jdphuff.c, jdhuff.h. |
||||
|
||||
jpegtran has a new "lossless" cropping feature. |
||||
|
||||
Implement -perfect option in jpegtran, new API function |
||||
jtransform_perfect_transform() in transupp. (DP 204_perfect.dpatch) |
||||
|
||||
Better error messages for jpegtran fopen failure. |
||||
(DP 203_jpegtran_errmsg.dpatch) |
||||
|
||||
Fix byte order issue with 16bit PPM/PGM files in rdppm.c/wrppm.c: |
||||
according to Netpbm, the de facto standard implementation of the PNM formats, |
||||
the most significant byte is first. (DP 203_rdppm.dpatch) |
||||
|
||||
Add -raw option to rdjpgcom not to mangle the output. |
||||
(DP 205_rdjpgcom_raw.dpatch) |
||||
|
||||
Make rdjpgcom locale aware. (DP 201_rdjpgcom_locale.dpatch) |
||||
|
||||
Add extern "C" to jpeglib.h. |
||||
This avoids the need to put extern "C" { ... } around #include "jpeglib.h" |
||||
in your C++ application. Defining the symbol DONT_USE_EXTERN_C in the |
||||
configuration prevents this. (DP 202_jpeglib.h_c++.dpatch) |
||||
|
||||
|
||||
Version 6b 27-Mar-1998 |
||||
----------------------- |
||||
|
||||
jpegtran has new features for lossless image transformations (rotation |
||||
and flipping) as well as "lossless" reduction to grayscale. |
||||
|
||||
jpegtran now copies comments by default; it has a -copy switch to enable |
||||
copying all APPn blocks as well, or to suppress comments. (Formerly it |
||||
always suppressed comments and APPn blocks.) jpegtran now also preserves |
||||
JFIF version and resolution information. |
||||
|
||||
New decompressor library feature: COM and APPn markers found in the input |
||||
file can be saved in memory for later use by the application. (Before, |
||||
you had to code this up yourself with a custom marker processor.) |
||||
|
||||
There is an unused field "void * client_data" now in compress and decompress |
||||
parameter structs; this may be useful in some applications. |
||||
|
||||
JFIF version number information is now saved by the decoder and accepted by |
||||
the encoder. jpegtran uses this to copy the source file's version number, |
||||
to ensure "jpegtran -copy all" won't create bogus files that contain JFXX |
||||
extensions but claim to be version 1.01. Applications that generate their |
||||
own JFXX extension markers also (finally) have a supported way to cause the |
||||
encoder to emit JFIF version number 1.02. |
||||
|
||||
djpeg's trace mode reports JFIF 1.02 thumbnail images as such, rather |
||||
than as unknown APP0 markers. |
||||
|
||||
In -verbose mode, djpeg and rdjpgcom will try to print the contents of |
||||
APP12 markers as text. Some digital cameras store useful text information |
||||
in APP12 markers. |
||||
|
||||
Handling of truncated data streams is more robust: blocks beyond the one in |
||||
which the error occurs will be output as uniform gray, or left unchanged |
||||
if decoding a progressive JPEG. The appearance no longer depends on the |
||||
Huffman tables being used. |
||||
|
||||
Huffman tables are checked for validity much more carefully than before. |
||||
|
||||
To avoid the Unisys LZW patent, djpeg's GIF output capability has been |
||||
changed to produce "uncompressed GIFs", and cjpeg's GIF input capability |
||||
has been removed altogether. We're not happy about it either, but there |
||||
seems to be no good alternative. |
||||
|
||||
The configure script now supports building libjpeg as a shared library |
||||
on many flavors of Unix (all the ones that GNU libtool knows how to |
||||
build shared libraries for). Use "./configure --enable-shared" to |
||||
try this out. |
||||
|
||||
New jconfig file and makefiles for Microsoft Visual C++ and Developer Studio. |
||||
Also, a jconfig file and a build script for Metrowerks CodeWarrior |
||||
on Apple Macintosh. makefile.dj has been updated for DJGPP v2, and there |
||||
are miscellaneous other minor improvements in the makefiles. |
||||
|
||||
jmemmac.c now knows how to create temporary files following Mac System 7 |
||||
conventions. |
||||
|
||||
djpeg's -map switch is now able to read raw-format PPM files reliably. |
||||
|
||||
cjpeg -progressive -restart no longer generates any unnecessary DRI markers. |
||||
|
||||
Multiple calls to jpeg_simple_progression for a single JPEG object |
||||
no longer leak memory. |
||||
|
||||
|
||||
Version 6a 7-Feb-96 |
||||
-------------------- |
||||
|
||||
Library initialization sequence modified to detect version mismatches |
||||
and struct field packing mismatches between library and calling application. |
||||
This change requires applications to be recompiled, but does not require |
||||
any application source code change. |
||||
|
||||
All routine declarations changed to the style "GLOBAL(type) name ...", |
||||
that is, GLOBAL, LOCAL, METHODDEF, EXTERN are now macros taking the |
||||
routine's return type as an argument. This makes it possible to add |
||||
Microsoft-style linkage keywords to all the routines by changing just |
||||
these macros. Note that any application code that was using these macros |
||||
will have to be changed. |
||||
|
||||
DCT coefficient quantization tables are now stored in normal array order |
||||
rather than zigzag order. Application code that calls jpeg_add_quant_table, |
||||
or otherwise manipulates quantization tables directly, will need to be |
||||
changed. If you need to make such code work with either older or newer |
||||
versions of the library, a test like "#if JPEG_LIB_VERSION >= 61" is |
||||
recommended. |
||||
|
||||
djpeg's trace capability now dumps DQT tables in natural order, not zigzag |
||||
order. This allows the trace output to be made into a "-qtables" file |
||||
more easily. |
||||
|
||||
New system-dependent memory manager module for use on Apple Macintosh. |
||||
|
||||
Fix bug in cjpeg's -smooth option: last one or two scanlines would be |
||||
duplicates of the prior line unless the image height mod 16 was 1 or 2. |
||||
|
||||
Repair minor problems in VMS, BCC, MC6 makefiles. |
||||
|
||||
New configure script based on latest GNU Autoconf. |
||||
|
||||
Correct the list of include files needed by MetroWerks C for ccommand(). |
||||
|
||||
Numerous small documentation updates. |
||||
|
||||
|
||||
Version 6 2-Aug-95 |
||||
------------------- |
||||
|
||||
Progressive JPEG support: library can read and write full progressive JPEG |
||||
files. A "buffered image" mode supports incremental decoding for on-the-fly |
||||
display of progressive images. Simply recompiling an existing IJG-v5-based |
||||
decoder with v6 should allow it to read progressive files, though of course |
||||
without any special progressive display. |
||||
|
||||
New "jpegtran" application performs lossless transcoding between different |
||||
JPEG formats; primarily, it can be used to convert baseline to progressive |
||||
JPEG and vice versa. In support of jpegtran, the library now allows lossless |
||||
reading and writing of JPEG files as DCT coefficient arrays. This ability |
||||
may be of use in other applications. |
||||
|
||||
Notes for programmers: |
||||
* We changed jpeg_start_decompress() to be able to suspend; this makes all |
||||
decoding modes available to suspending-input applications. However, |
||||
existing applications that use suspending input will need to be changed |
||||
to check the return value from jpeg_start_decompress(). You don't need to |
||||
do anything if you don't use a suspending data source. |
||||
* We changed the interface to the virtual array routines: access_virt_array |
||||
routines now take a count of the number of rows to access this time. The |
||||
last parameter to request_virt_array routines is now interpreted as the |
||||
maximum number of rows that may be accessed at once, but not necessarily |
||||
the height of every access. |
||||
|
||||
|
||||
Version 5b 15-Mar-95 |
||||
--------------------- |
||||
|
||||
Correct bugs with grayscale images having v_samp_factor > 1. |
||||
|
||||
jpeg_write_raw_data() now supports output suspension. |
||||
|
||||
Correct bugs in "configure" script for case of compiling in |
||||
a directory other than the one containing the source files. |
||||
|
||||
Repair bug in jquant1.c: sometimes didn't use as many colors as it could. |
||||
|
||||
Borland C makefile and jconfig file work under either MS-DOS or OS/2. |
||||
|
||||
Miscellaneous improvements to documentation. |
||||
|
||||
|
||||
Version 5a 7-Dec-94 |
||||
-------------------- |
||||
|
||||
Changed color conversion roundoff behavior so that grayscale values are |
||||
represented exactly. (This causes test image files to change.) |
||||
|
||||
Make ordered dither use 16x16 instead of 4x4 pattern for a small quality |
||||
improvement. |
||||
|
||||
New configure script based on latest GNU Autoconf. |
||||
Fix configure script to handle CFLAGS correctly. |
||||
Rename *.auto files to *.cfg, so that configure script still works if |
||||
file names have been truncated for DOS. |
||||
|
||||
Fix bug in rdbmp.c: didn't allow for extra data between header and image. |
||||
|
||||
Modify rdppm.c/wrppm.c to handle 2-byte raw PPM/PGM formats for 12-bit data. |
||||
|
||||
Fix several bugs in rdrle.c. |
||||
|
||||
NEED_SHORT_EXTERNAL_NAMES option was broken. |
||||
|
||||
Revise jerror.h/jerror.c for more flexibility in message table. |
||||
|
||||
Repair oversight in jmemname.c NO_MKTEMP case: file could be there |
||||
but unreadable. |
||||
|
||||
|
||||
Version 5 24-Sep-94 |
||||
-------------------- |
||||
|
||||
Version 5 represents a nearly complete redesign and rewrite of the IJG |
||||
software. Major user-visible changes include: |
||||
* Automatic configuration simplifies installation for most Unix systems. |
||||
* A range of speed vs. image quality tradeoffs are supported. |
||||
This includes resizing of an image during decompression: scaling down |
||||
by a factor of 1/2, 1/4, or 1/8 is handled very efficiently. |
||||
* New programs rdjpgcom and wrjpgcom allow insertion and extraction |
||||
of text comments in a JPEG file. |
||||
|
||||
The application programmer's interface to the library has changed completely. |
||||
Notable improvements include: |
||||
* We have eliminated the use of callback routines for handling the |
||||
uncompressed image data. The application now sees the library as a |
||||
set of routines that it calls to read or write image data on a |
||||
scanline-by-scanline basis. |
||||
* The application image data is represented in a conventional interleaved- |
||||
pixel format, rather than as a separate array for each color channel. |
||||
This can save a copying step in many programs. |
||||
* The handling of compressed data has been cleaned up: the application can |
||||
supply routines to source or sink the compressed data. It is possible to |
||||
suspend processing on source/sink buffer overrun, although this is not |
||||
supported in all operating modes. |
||||
* All static state has been eliminated from the library, so that multiple |
||||
instances of compression or decompression can be active concurrently. |
||||
* JPEG abbreviated datastream formats are supported, ie, quantization and |
||||
Huffman tables can be stored separately from the image data. |
||||
* And not only that, but the documentation of the library has improved |
||||
considerably! |
||||
|
||||
|
||||
The last widely used release before the version 5 rewrite was version 4A of |
||||
18-Feb-93. Change logs before that point have been discarded, since they |
||||
are not of much interest after the rewrite. |
@ -0,0 +1,153 @@ |
||||
/*
|
||||
* jaricom.c |
||||
* |
||||
* Developed 1997-2011 by Guido Vollbeding. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains probability estimation tables for common use in |
||||
* arithmetic entropy encoding and decoding routines. |
||||
* |
||||
* This data represents Table D.3 in the JPEG spec (D.2 in the draft), |
||||
* ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81, and Table 24 |
||||
* in the JBIG spec, ISO/IEC IS 11544 and CCITT Recommendation ITU-T T.82. |
||||
*/ |
||||
|
||||
#define JPEG_INTERNALS |
||||
#include "jinclude.h" |
||||
#include "jpeglib.h" |
||||
|
||||
/* The following #define specifies the packing of the four components
|
||||
* into the compact INT32 representation. |
||||
* Note that this formula must match the actual arithmetic encoder |
||||
* and decoder implementation. The implementation has to be changed |
||||
* if this formula is changed. |
||||
* The current organization is leaned on Markus Kuhn's JBIG |
||||
* implementation (jbig_tab.c). |
||||
*/ |
||||
|
||||
#define V(i,a,b,c,d) (((INT32)a << 16) | ((INT32)c << 8) | ((INT32)d << 7) | b) |
||||
|
||||
const INT32 jpeg_aritab[113+1] = { |
||||
/*
|
||||
* Index, Qe_Value, Next_Index_LPS, Next_Index_MPS, Switch_MPS |
||||
*/ |
||||
V( 0, 0x5a1d, 1, 1, 1 ), |
||||
V( 1, 0x2586, 14, 2, 0 ), |
||||
V( 2, 0x1114, 16, 3, 0 ), |
||||
V( 3, 0x080b, 18, 4, 0 ), |
||||
V( 4, 0x03d8, 20, 5, 0 ), |
||||
V( 5, 0x01da, 23, 6, 0 ), |
||||
V( 6, 0x00e5, 25, 7, 0 ), |
||||
V( 7, 0x006f, 28, 8, 0 ), |
||||
V( 8, 0x0036, 30, 9, 0 ), |
||||
V( 9, 0x001a, 33, 10, 0 ), |
||||
V( 10, 0x000d, 35, 11, 0 ), |
||||
V( 11, 0x0006, 9, 12, 0 ), |
||||
V( 12, 0x0003, 10, 13, 0 ), |
||||
V( 13, 0x0001, 12, 13, 0 ), |
||||
V( 14, 0x5a7f, 15, 15, 1 ), |
||||
V( 15, 0x3f25, 36, 16, 0 ), |
||||
V( 16, 0x2cf2, 38, 17, 0 ), |
||||
V( 17, 0x207c, 39, 18, 0 ), |
||||
V( 18, 0x17b9, 40, 19, 0 ), |
||||
V( 19, 0x1182, 42, 20, 0 ), |
||||
V( 20, 0x0cef, 43, 21, 0 ), |
||||
V( 21, 0x09a1, 45, 22, 0 ), |
||||
V( 22, 0x072f, 46, 23, 0 ), |
||||
V( 23, 0x055c, 48, 24, 0 ), |
||||
V( 24, 0x0406, 49, 25, 0 ), |
||||
V( 25, 0x0303, 51, 26, 0 ), |
||||
V( 26, 0x0240, 52, 27, 0 ), |
||||
V( 27, 0x01b1, 54, 28, 0 ), |
||||
V( 28, 0x0144, 56, 29, 0 ), |
||||
V( 29, 0x00f5, 57, 30, 0 ), |
||||
V( 30, 0x00b7, 59, 31, 0 ), |
||||
V( 31, 0x008a, 60, 32, 0 ), |
||||
V( 32, 0x0068, 62, 33, 0 ), |
||||
V( 33, 0x004e, 63, 34, 0 ), |
||||
V( 34, 0x003b, 32, 35, 0 ), |
||||
V( 35, 0x002c, 33, 9, 0 ), |
||||
V( 36, 0x5ae1, 37, 37, 1 ), |
||||
V( 37, 0x484c, 64, 38, 0 ), |
||||
V( 38, 0x3a0d, 65, 39, 0 ), |
||||
V( 39, 0x2ef1, 67, 40, 0 ), |
||||
V( 40, 0x261f, 68, 41, 0 ), |
||||
V( 41, 0x1f33, 69, 42, 0 ), |
||||
V( 42, 0x19a8, 70, 43, 0 ), |
||||
V( 43, 0x1518, 72, 44, 0 ), |
||||
V( 44, 0x1177, 73, 45, 0 ), |
||||
V( 45, 0x0e74, 74, 46, 0 ), |
||||
V( 46, 0x0bfb, 75, 47, 0 ), |
||||
V( 47, 0x09f8, 77, 48, 0 ), |
||||
V( 48, 0x0861, 78, 49, 0 ), |
||||
V( 49, 0x0706, 79, 50, 0 ), |
||||
V( 50, 0x05cd, 48, 51, 0 ), |
||||
V( 51, 0x04de, 50, 52, 0 ), |
||||
V( 52, 0x040f, 50, 53, 0 ), |
||||
V( 53, 0x0363, 51, 54, 0 ), |
||||
V( 54, 0x02d4, 52, 55, 0 ), |
||||
V( 55, 0x025c, 53, 56, 0 ), |
||||
V( 56, 0x01f8, 54, 57, 0 ), |
||||
V( 57, 0x01a4, 55, 58, 0 ), |
||||
V( 58, 0x0160, 56, 59, 0 ), |
||||
V( 59, 0x0125, 57, 60, 0 ), |
||||
V( 60, 0x00f6, 58, 61, 0 ), |
||||
V( 61, 0x00cb, 59, 62, 0 ), |
||||
V( 62, 0x00ab, 61, 63, 0 ), |
||||
V( 63, 0x008f, 61, 32, 0 ), |
||||
V( 64, 0x5b12, 65, 65, 1 ), |
||||
V( 65, 0x4d04, 80, 66, 0 ), |
||||
V( 66, 0x412c, 81, 67, 0 ), |
||||
V( 67, 0x37d8, 82, 68, 0 ), |
||||
V( 68, 0x2fe8, 83, 69, 0 ), |
||||
V( 69, 0x293c, 84, 70, 0 ), |
||||
V( 70, 0x2379, 86, 71, 0 ), |
||||
V( 71, 0x1edf, 87, 72, 0 ), |
||||
V( 72, 0x1aa9, 87, 73, 0 ), |
||||
V( 73, 0x174e, 72, 74, 0 ), |
||||
V( 74, 0x1424, 72, 75, 0 ), |
||||
V( 75, 0x119c, 74, 76, 0 ), |
||||
V( 76, 0x0f6b, 74, 77, 0 ), |
||||
V( 77, 0x0d51, 75, 78, 0 ), |
||||
V( 78, 0x0bb6, 77, 79, 0 ), |
||||
V( 79, 0x0a40, 77, 48, 0 ), |
||||
V( 80, 0x5832, 80, 81, 1 ), |
||||
V( 81, 0x4d1c, 88, 82, 0 ), |
||||
V( 82, 0x438e, 89, 83, 0 ), |
||||
V( 83, 0x3bdd, 90, 84, 0 ), |
||||
V( 84, 0x34ee, 91, 85, 0 ), |
||||
V( 85, 0x2eae, 92, 86, 0 ), |
||||
V( 86, 0x299a, 93, 87, 0 ), |
||||
V( 87, 0x2516, 86, 71, 0 ), |
||||
V( 88, 0x5570, 88, 89, 1 ), |
||||
V( 89, 0x4ca9, 95, 90, 0 ), |
||||
V( 90, 0x44d9, 96, 91, 0 ), |
||||
V( 91, 0x3e22, 97, 92, 0 ), |
||||
V( 92, 0x3824, 99, 93, 0 ), |
||||
V( 93, 0x32b4, 99, 94, 0 ), |
||||
V( 94, 0x2e17, 93, 86, 0 ), |
||||
V( 95, 0x56a8, 95, 96, 1 ), |
||||
V( 96, 0x4f46, 101, 97, 0 ), |
||||
V( 97, 0x47e5, 102, 98, 0 ), |
||||
V( 98, 0x41cf, 103, 99, 0 ), |
||||
V( 99, 0x3c3d, 104, 100, 0 ), |
||||
V( 100, 0x375e, 99, 93, 0 ), |
||||
V( 101, 0x5231, 105, 102, 0 ), |
||||
V( 102, 0x4c0f, 106, 103, 0 ), |
||||
V( 103, 0x4639, 107, 104, 0 ), |
||||
V( 104, 0x415e, 103, 99, 0 ), |
||||
V( 105, 0x5627, 105, 106, 1 ), |
||||
V( 106, 0x50e7, 108, 107, 0 ), |
||||
V( 107, 0x4b85, 109, 103, 0 ), |
||||
V( 108, 0x5597, 110, 109, 0 ), |
||||
V( 109, 0x504f, 111, 107, 0 ), |
||||
V( 110, 0x5a10, 110, 111, 1 ), |
||||
V( 111, 0x5522, 112, 109, 0 ), |
||||
V( 112, 0x59eb, 112, 111, 1 ), |
||||
/*
|
||||
* This last entry is used for fixed probability estimate of 0.5 |
||||
* as suggested in Section 10.3 Table 5 of ITU-T Rec. T.851. |
||||
*/ |
||||
V( 113, 0x5a1d, 113, 113, 0 ) |
||||
}; |
@ -0,0 +1,937 @@ |
||||
/*
|
||||
* jcarith.c |
||||
* |
||||
* Developed 1997-2011 by Guido Vollbeding. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains portable arithmetic entropy encoding routines for JPEG |
||||
* (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). |
||||
* |
||||
* Both sequential and progressive modes are supported in this single module. |
||||
* |
||||
* Suspension is not currently supported in this module. |
||||
*/ |
||||
|
||||
#define JPEG_INTERNALS |
||||
#include "jinclude.h" |
||||
#include "jpeglib.h" |
||||
|
||||
|
||||
/* Expanded entropy encoder object for arithmetic encoding. */ |
||||
|
||||
typedef struct { |
||||
struct jpeg_entropy_encoder pub; /* public fields */ |
||||
|
||||
INT32 c; /* C register, base of coding interval, layout as in sec. D.1.3 */ |
||||
INT32 a; /* A register, normalized size of coding interval */ |
||||
INT32 sc; /* counter for stacked 0xFF values which might overflow */ |
||||
INT32 zc; /* counter for pending 0x00 output values which might *
|
||||
* be discarded at the end ("Pacman" termination) */ |
||||
int ct; /* bit shift counter, determines when next byte will be written */ |
||||
int buffer; /* buffer for most recent output byte != 0xFF */ |
||||
|
||||
int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
||||
int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ |
||||
|
||||
unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
||||
int next_restart_num; /* next restart number to write (0-7) */ |
||||
|
||||
/* Pointers to statistics areas (these workspaces have image lifespan) */ |
||||
unsigned char * dc_stats[NUM_ARITH_TBLS]; |
||||
unsigned char * ac_stats[NUM_ARITH_TBLS]; |
||||
|
||||
/* Statistics bin for coding with fixed probability 0.5 */ |
||||
unsigned char fixed_bin[4]; |
||||
} arith_entropy_encoder; |
||||
|
||||
typedef arith_entropy_encoder * arith_entropy_ptr; |
||||
|
||||
/* The following two definitions specify the allocation chunk size
|
||||
* for the statistics area. |
||||
* According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least |
||||
* 49 statistics bins for DC, and 245 statistics bins for AC coding. |
||||
* |
||||
* We use a compact representation with 1 byte per statistics bin, |
||||
* thus the numbers directly represent byte sizes. |
||||
* This 1 byte per statistics bin contains the meaning of the MPS |
||||
* (more probable symbol) in the highest bit (mask 0x80), and the |
||||
* index into the probability estimation state machine table |
||||
* in the lower bits (mask 0x7F). |
||||
*/ |
||||
|
||||
#define DC_STAT_BINS 64 |
||||
#define AC_STAT_BINS 256 |
||||
|
||||
/* NOTE: Uncomment the following #define if you want to use the
|
||||
* given formula for calculating the AC conditioning parameter Kx |
||||
* for spectral selection progressive coding in section G.1.3.2 |
||||
* of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4). |
||||
* Although the spec and P&M authors claim that this "has proven |
||||
* to give good results for 8 bit precision samples", I'm not |
||||
* convinced yet that this is really beneficial. |
||||
* Early tests gave only very marginal compression enhancements |
||||
* (a few - around 5 or so - bytes even for very large files), |
||||
* which would turn out rather negative if we'd suppress the |
||||
* DAC (Define Arithmetic Conditioning) marker segments for |
||||
* the default parameters in the future. |
||||
* Note that currently the marker writing module emits 12-byte |
||||
* DAC segments for a full-component scan in a color image. |
||||
* This is not worth worrying about IMHO. However, since the |
||||
* spec defines the default values to be used if the tables |
||||
* are omitted (unlike Huffman tables, which are required |
||||
* anyway), one might optimize this behaviour in the future, |
||||
* and then it would be disadvantageous to use custom tables if |
||||
* they don't provide sufficient gain to exceed the DAC size. |
||||
* |
||||
* On the other hand, I'd consider it as a reasonable result |
||||
* that the conditioning has no significant influence on the |
||||
* compression performance. This means that the basic |
||||
* statistical model is already rather stable. |
||||
* |
||||
* Thus, at the moment, we use the default conditioning values |
||||
* anyway, and do not use the custom formula. |
||||
* |
||||
#define CALCULATE_SPECTRAL_CONDITIONING |
||||
*/ |
||||
|
||||
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
|
||||
* We assume that int right shift is unsigned if INT32 right shift is, |
||||
* which should be safe. |
||||
*/ |
||||
|
||||
#ifdef RIGHT_SHIFT_IS_UNSIGNED |
||||
#define ISHIFT_TEMPS int ishift_temp; |
||||
#define IRIGHT_SHIFT(x,shft) \ |
||||
((ishift_temp = (x)) < 0 ? \
|
||||
(ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
|
||||
(ishift_temp >> (shft))) |
||||
#else |
||||
#define ISHIFT_TEMPS |
||||
#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) |
||||
#endif |
||||
|
||||
|
||||
LOCAL(void) |
||||
emit_byte (int val, j_compress_ptr cinfo) |
||||
/* Write next output byte; we do not support suspension in this module. */ |
||||
{ |
||||
struct jpeg_destination_mgr * dest = cinfo->dest; |
||||
|
||||
*dest->next_output_byte++ = (JOCTET) val; |
||||
if (--dest->free_in_buffer == 0) |
||||
if (! (*dest->empty_output_buffer) (cinfo)) |
||||
ERREXIT(cinfo, JERR_CANT_SUSPEND); |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Finish up at the end of an arithmetic-compressed scan. |
||||
*/ |
||||
|
||||
METHODDEF(void) |
||||
finish_pass (j_compress_ptr cinfo) |
||||
{ |
||||
arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; |
||||
INT32 temp; |
||||
|
||||
/* Section D.1.8: Termination of encoding */ |
||||
|
||||
/* Find the e->c in the coding interval with the largest
|
||||
* number of trailing zero bits */ |
||||
if ((temp = (e->a - 1 + e->c) & 0xFFFF0000L) < e->c) |
||||
e->c = temp + 0x8000L; |
||||
else |
||||
e->c = temp; |
||||
/* Send remaining bytes to output */ |
||||
e->c <<= e->ct; |
||||
if (e->c & 0xF8000000L) { |
||||
/* One final overflow has to be handled */ |
||||
if (e->buffer >= 0) { |
||||
if (e->zc) |
||||
do emit_byte(0x00, cinfo); |
||||
while (--e->zc); |
||||
emit_byte(e->buffer + 1, cinfo); |
||||
if (e->buffer + 1 == 0xFF) |
||||
emit_byte(0x00, cinfo); |
||||
} |
||||
e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ |
||||
e->sc = 0; |
||||
} else { |
||||
if (e->buffer == 0) |
||||
++e->zc; |
||||
else if (e->buffer >= 0) { |
||||
if (e->zc) |
||||
do emit_byte(0x00, cinfo); |
||||
while (--e->zc); |
||||
emit_byte(e->buffer, cinfo); |
||||
} |
||||
if (e->sc) { |
||||
if (e->zc) |
||||
do emit_byte(0x00, cinfo); |
||||
while (--e->zc); |
||||
do { |
||||
emit_byte(0xFF, cinfo); |
||||
emit_byte(0x00, cinfo); |
||||
} while (--e->sc); |
||||
} |
||||
} |
||||
/* Output final bytes only if they are not 0x00 */ |
||||
if (e->c & 0x7FFF800L) { |
||||
if (e->zc) /* output final pending zero bytes */ |
||||
do emit_byte(0x00, cinfo); |
||||
while (--e->zc); |
||||
emit_byte((e->c >> 19) & 0xFF, cinfo); |
||||
if (((e->c >> 19) & 0xFF) == 0xFF) |
||||
emit_byte(0x00, cinfo); |
||||
if (e->c & 0x7F800L) { |
||||
emit_byte((e->c >> 11) & 0xFF, cinfo); |
||||
if (((e->c >> 11) & 0xFF) == 0xFF) |
||||
emit_byte(0x00, cinfo); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* The core arithmetic encoding routine (common in JPEG and JBIG). |
||||
* This needs to go as fast as possible. |
||||
* Machine-dependent optimization facilities |
||||
* are not utilized in this portable implementation. |
||||
* However, this code should be fairly efficient and |
||||
* may be a good base for further optimizations anyway. |
||||
* |
||||
* Parameter 'val' to be encoded may be 0 or 1 (binary decision). |
||||
* |
||||
* Note: I've added full "Pacman" termination support to the |
||||
* byte output routines, which is equivalent to the optional |
||||
* Discard_final_zeros procedure (Figure D.15) in the spec. |
||||
* Thus, we always produce the shortest possible output |
||||
* stream compliant to the spec (no trailing zero bytes, |
||||
* except for FF stuffing). |
||||
* |
||||
* I've also introduced a new scheme for accessing |
||||
* the probability estimation state machine table, |
||||
* derived from Markus Kuhn's JBIG implementation. |
||||
*/ |
||||
|
||||
LOCAL(void) |
||||
arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
|
||||
{ |
||||
register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; |
||||
register unsigned char nl, nm; |
||||
register INT32 qe, temp; |
||||
register int sv; |
||||
|
||||
/* Fetch values from our compact representation of Table D.3(D.2):
|
||||
* Qe values and probability estimation state machine |
||||
*/ |
||||
sv = *st; |
||||
qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ |
||||
nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ |
||||
nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ |
||||
|
||||
/* Encode & estimation procedures per sections D.1.4 & D.1.5 */ |
||||
e->a -= qe; |
||||
if (val != (sv >> 7)) { |
||||
/* Encode the less probable symbol */ |
||||
if (e->a >= qe) { |
||||
/* If the interval size (qe) for the less probable symbol (LPS)
|
||||
* is larger than the interval size for the MPS, then exchange |
||||
* the two symbols for coding efficiency, otherwise code the LPS |
||||
* as usual: */ |
||||
e->c += e->a; |
||||
e->a = qe; |
||||
} |
||||
*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ |
||||
} else { |
||||
/* Encode the more probable symbol */ |
||||
if (e->a >= 0x8000L) |
||||
return; /* A >= 0x8000 -> ready, no renormalization required */ |
||||
if (e->a < qe) { |
||||
/* If the interval size (qe) for the less probable symbol (LPS)
|
||||
* is larger than the interval size for the MPS, then exchange |
||||
* the two symbols for coding efficiency: */ |
||||
e->c += e->a; |
||||
e->a = qe; |
||||
} |
||||
*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ |
||||
} |
||||
|
||||
/* Renormalization & data output per section D.1.6 */ |
||||
do { |
||||
e->a <<= 1; |
||||
e->c <<= 1; |
||||
if (--e->ct == 0) { |
||||
/* Another byte is ready for output */ |
||||
temp = e->c >> 19; |
||||
if (temp > 0xFF) { |
||||
/* Handle overflow over all stacked 0xFF bytes */ |
||||
if (e->buffer >= 0) { |
||||
if (e->zc) |
||||
do emit_byte(0x00, cinfo); |
||||
while (--e->zc); |
||||
emit_byte(e->buffer + 1, cinfo); |
||||
if (e->buffer + 1 == 0xFF) |
||||
emit_byte(0x00, cinfo); |
||||
} |
||||
e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ |
||||
e->sc = 0; |
||||
/* Note: The 3 spacer bits in the C register guarantee
|
||||
* that the new buffer byte can't be 0xFF here |
||||
* (see page 160 in the P&M JPEG book). */ |
||||
e->buffer = temp & 0xFF; /* new output byte, might overflow later */ |
||||
} else if (temp == 0xFF) { |
||||
++e->sc; /* stack 0xFF byte (which might overflow later) */ |
||||
} else { |
||||
/* Output all stacked 0xFF bytes, they will not overflow any more */ |
||||
if (e->buffer == 0) |
||||
++e->zc; |
||||
else if (e->buffer >= 0) { |
||||
if (e->zc) |
||||
do emit_byte(0x00, cinfo); |
||||
while (--e->zc); |
||||
emit_byte(e->buffer, cinfo); |
||||
} |
||||
if (e->sc) { |
||||
if (e->zc) |
||||
do emit_byte(0x00, cinfo); |
||||
while (--e->zc); |
||||
do { |
||||
emit_byte(0xFF, cinfo); |
||||
emit_byte(0x00, cinfo); |
||||
} while (--e->sc); |
||||
} |
||||
e->buffer = temp & 0xFF; /* new output byte (can still overflow) */ |
||||
} |
||||
e->c &= 0x7FFFFL; |
||||
e->ct += 8; |
||||
} |
||||
} while (e->a < 0x8000L); |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Emit a restart marker & resynchronize predictions. |
||||
*/ |
||||
|
||||
LOCAL(void) |
||||
emit_restart (j_compress_ptr cinfo, int restart_num) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
int ci; |
||||
jpeg_component_info * compptr; |
||||
|
||||
finish_pass(cinfo); |
||||
|
||||
emit_byte(0xFF, cinfo); |
||||
emit_byte(JPEG_RST0 + restart_num, cinfo); |
||||
|
||||
/* Re-initialize statistics areas */ |
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
/* DC needs no table for refinement scan */ |
||||
if (cinfo->Ss == 0 && cinfo->Ah == 0) { |
||||
MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); |
||||
/* Reset DC predictions to 0 */ |
||||
entropy->last_dc_val[ci] = 0; |
||||
entropy->dc_context[ci] = 0; |
||||
} |
||||
/* AC needs no table when not present */ |
||||
if (cinfo->Se) { |
||||
MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); |
||||
} |
||||
} |
||||
|
||||
/* Reset arithmetic encoding variables */ |
||||
entropy->c = 0; |
||||
entropy->a = 0x10000L; |
||||
entropy->sc = 0; |
||||
entropy->zc = 0; |
||||
entropy->ct = 11; |
||||
entropy->buffer = -1; /* empty */ |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU encoding for DC initial scan (either spectral selection, |
||||
* or first pass of successive approximation). |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
JBLOCKROW block; |
||||
unsigned char *st; |
||||
int blkn, ci, tbl; |
||||
int v, v2, m; |
||||
ISHIFT_TEMPS |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
emit_restart(cinfo, entropy->next_restart_num); |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
/* Encode the MCU data blocks */ |
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
block = MCU_data[blkn]; |
||||
ci = cinfo->MCU_membership[blkn]; |
||||
tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; |
||||
|
||||
/* Compute the DC value after the required point transform by Al.
|
||||
* This is simply an arithmetic right shift. |
||||
*/ |
||||
m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al); |
||||
|
||||
/* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ |
||||
|
||||
/* Table F.4: Point to statistics bin S0 for DC coefficient coding */ |
||||
st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; |
||||
|
||||
/* Figure F.4: Encode_DC_DIFF */ |
||||
if ((v = m - entropy->last_dc_val[ci]) == 0) { |
||||
arith_encode(cinfo, st, 0); |
||||
entropy->dc_context[ci] = 0; /* zero diff category */ |
||||
} else { |
||||
entropy->last_dc_val[ci] = m; |
||||
arith_encode(cinfo, st, 1); |
||||
/* Figure F.6: Encoding nonzero value v */ |
||||
/* Figure F.7: Encoding the sign of v */ |
||||
if (v > 0) { |
||||
arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */ |
||||
st += 2; /* Table F.4: SP = S0 + 2 */ |
||||
entropy->dc_context[ci] = 4; /* small positive diff category */ |
||||
} else { |
||||
v = -v; |
||||
arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */ |
||||
st += 3; /* Table F.4: SN = S0 + 3 */ |
||||
entropy->dc_context[ci] = 8; /* small negative diff category */ |
||||
} |
||||
/* Figure F.8: Encoding the magnitude category of v */ |
||||
m = 0; |
||||
if (v -= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m = 1; |
||||
v2 = v; |
||||
st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ |
||||
while (v2 >>= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m <<= 1; |
||||
st += 1; |
||||
} |
||||
} |
||||
arith_encode(cinfo, st, 0); |
||||
/* Section F.1.4.4.1.2: Establish dc_context conditioning category */ |
||||
if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) |
||||
entropy->dc_context[ci] = 0; /* zero diff category */ |
||||
else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) |
||||
entropy->dc_context[ci] += 8; /* large diff category */ |
||||
/* Figure F.9: Encoding the magnitude bit pattern of v */ |
||||
st += 14; |
||||
while (m >>= 1) |
||||
arith_encode(cinfo, st, (m & v) ? 1 : 0); |
||||
} |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU encoding for AC initial scan (either spectral selection, |
||||
* or first pass of successive approximation). |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
JBLOCKROW block; |
||||
unsigned char *st; |
||||
int tbl, k, ke; |
||||
int v, v2, m; |
||||
const int * natural_order; |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
emit_restart(cinfo, entropy->next_restart_num); |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
natural_order = cinfo->natural_order; |
||||
|
||||
/* Encode the MCU data block */ |
||||
block = MCU_data[0]; |
||||
tbl = cinfo->cur_comp_info[0]->ac_tbl_no; |
||||
|
||||
/* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ |
||||
|
||||
/* Establish EOB (end-of-block) index */ |
||||
for (ke = cinfo->Se; ke > 0; ke--) |
||||
/* We must apply the point transform by Al. For AC coefficients this
|
||||
* is an integer division with rounding towards 0. To do this portably |
||||
* in C, we shift after obtaining the absolute value. |
||||
*/ |
||||
if ((v = (*block)[natural_order[ke]]) >= 0) { |
||||
if (v >>= cinfo->Al) break; |
||||
} else { |
||||
v = -v; |
||||
if (v >>= cinfo->Al) break; |
||||
} |
||||
|
||||
/* Figure F.5: Encode_AC_Coefficients */ |
||||
for (k = cinfo->Ss; k <= ke; k++) { |
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1); |
||||
arith_encode(cinfo, st, 0); /* EOB decision */ |
||||
for (;;) { |
||||
if ((v = (*block)[natural_order[k]]) >= 0) { |
||||
if (v >>= cinfo->Al) { |
||||
arith_encode(cinfo, st + 1, 1); |
||||
arith_encode(cinfo, entropy->fixed_bin, 0); |
||||
break; |
||||
} |
||||
} else { |
||||
v = -v; |
||||
if (v >>= cinfo->Al) { |
||||
arith_encode(cinfo, st + 1, 1); |
||||
arith_encode(cinfo, entropy->fixed_bin, 1); |
||||
break; |
||||
} |
||||
} |
||||
arith_encode(cinfo, st + 1, 0); st += 3; k++; |
||||
} |
||||
st += 2; |
||||
/* Figure F.8: Encoding the magnitude category of v */ |
||||
m = 0; |
||||
if (v -= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m = 1; |
||||
v2 = v; |
||||
if (v2 >>= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m <<= 1; |
||||
st = entropy->ac_stats[tbl] + |
||||
(k <= cinfo->arith_ac_K[tbl] ? 189 : 217); |
||||
while (v2 >>= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m <<= 1; |
||||
st += 1; |
||||
} |
||||
} |
||||
} |
||||
arith_encode(cinfo, st, 0); |
||||
/* Figure F.9: Encoding the magnitude bit pattern of v */ |
||||
st += 14; |
||||
while (m >>= 1) |
||||
arith_encode(cinfo, st, (m & v) ? 1 : 0); |
||||
} |
||||
/* Encode EOB decision only if k <= cinfo->Se */ |
||||
if (k <= cinfo->Se) { |
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1); |
||||
arith_encode(cinfo, st, 1); |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU encoding for DC successive approximation refinement scan. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
unsigned char *st; |
||||
int Al, blkn; |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
emit_restart(cinfo, entropy->next_restart_num); |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
st = entropy->fixed_bin; /* use fixed probability estimation */ |
||||
Al = cinfo->Al; |
||||
|
||||
/* Encode the MCU data blocks */ |
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
/* We simply emit the Al'th bit of the DC coefficient value. */ |
||||
arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1); |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU encoding for AC successive approximation refinement scan. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
JBLOCKROW block; |
||||
unsigned char *st; |
||||
int tbl, k, ke, kex; |
||||
int v; |
||||
const int * natural_order; |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
emit_restart(cinfo, entropy->next_restart_num); |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
natural_order = cinfo->natural_order; |
||||
|
||||
/* Encode the MCU data block */ |
||||
block = MCU_data[0]; |
||||
tbl = cinfo->cur_comp_info[0]->ac_tbl_no; |
||||
|
||||
/* Section G.1.3.3: Encoding of AC coefficients */ |
||||
|
||||
/* Establish EOB (end-of-block) index */ |
||||
for (ke = cinfo->Se; ke > 0; ke--) |
||||
/* We must apply the point transform by Al. For AC coefficients this
|
||||
* is an integer division with rounding towards 0. To do this portably |
||||
* in C, we shift after obtaining the absolute value. |
||||
*/ |
||||
if ((v = (*block)[natural_order[ke]]) >= 0) { |
||||
if (v >>= cinfo->Al) break; |
||||
} else { |
||||
v = -v; |
||||
if (v >>= cinfo->Al) break; |
||||
} |
||||
|
||||
/* Establish EOBx (previous stage end-of-block) index */ |
||||
for (kex = ke; kex > 0; kex--) |
||||
if ((v = (*block)[natural_order[kex]]) >= 0) { |
||||
if (v >>= cinfo->Ah) break; |
||||
} else { |
||||
v = -v; |
||||
if (v >>= cinfo->Ah) break; |
||||
} |
||||
|
||||
/* Figure G.10: Encode_AC_Coefficients_SA */ |
||||
for (k = cinfo->Ss; k <= ke; k++) { |
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1); |
||||
if (k > kex) |
||||
arith_encode(cinfo, st, 0); /* EOB decision */ |
||||
for (;;) { |
||||
if ((v = (*block)[natural_order[k]]) >= 0) { |
||||
if (v >>= cinfo->Al) { |
||||
if (v >> 1) /* previously nonzero coef */ |
||||
arith_encode(cinfo, st + 2, (v & 1)); |
||||
else { /* newly nonzero coef */ |
||||
arith_encode(cinfo, st + 1, 1); |
||||
arith_encode(cinfo, entropy->fixed_bin, 0); |
||||
} |
||||
break; |
||||
} |
||||
} else { |
||||
v = -v; |
||||
if (v >>= cinfo->Al) { |
||||
if (v >> 1) /* previously nonzero coef */ |
||||
arith_encode(cinfo, st + 2, (v & 1)); |
||||
else { /* newly nonzero coef */ |
||||
arith_encode(cinfo, st + 1, 1); |
||||
arith_encode(cinfo, entropy->fixed_bin, 1); |
||||
} |
||||
break; |
||||
} |
||||
} |
||||
arith_encode(cinfo, st + 1, 0); st += 3; k++; |
||||
} |
||||
} |
||||
/* Encode EOB decision only if k <= cinfo->Se */ |
||||
if (k <= cinfo->Se) { |
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1); |
||||
arith_encode(cinfo, st, 1); |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Encode and output one MCU's worth of arithmetic-compressed coefficients. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
jpeg_component_info * compptr; |
||||
JBLOCKROW block; |
||||
unsigned char *st; |
||||
int blkn, ci, tbl, k, ke; |
||||
int v, v2, m; |
||||
const int * natural_order; |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
emit_restart(cinfo, entropy->next_restart_num); |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
natural_order = cinfo->natural_order; |
||||
|
||||
/* Encode the MCU data blocks */ |
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
block = MCU_data[blkn]; |
||||
ci = cinfo->MCU_membership[blkn]; |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
|
||||
/* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ |
||||
|
||||
tbl = compptr->dc_tbl_no; |
||||
|
||||
/* Table F.4: Point to statistics bin S0 for DC coefficient coding */ |
||||
st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; |
||||
|
||||
/* Figure F.4: Encode_DC_DIFF */ |
||||
if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) { |
||||
arith_encode(cinfo, st, 0); |
||||
entropy->dc_context[ci] = 0; /* zero diff category */ |
||||
} else { |
||||
entropy->last_dc_val[ci] = (*block)[0]; |
||||
arith_encode(cinfo, st, 1); |
||||
/* Figure F.6: Encoding nonzero value v */ |
||||
/* Figure F.7: Encoding the sign of v */ |
||||
if (v > 0) { |
||||
arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */ |
||||
st += 2; /* Table F.4: SP = S0 + 2 */ |
||||
entropy->dc_context[ci] = 4; /* small positive diff category */ |
||||
} else { |
||||
v = -v; |
||||
arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */ |
||||
st += 3; /* Table F.4: SN = S0 + 3 */ |
||||
entropy->dc_context[ci] = 8; /* small negative diff category */ |
||||
} |
||||
/* Figure F.8: Encoding the magnitude category of v */ |
||||
m = 0; |
||||
if (v -= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m = 1; |
||||
v2 = v; |
||||
st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ |
||||
while (v2 >>= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m <<= 1; |
||||
st += 1; |
||||
} |
||||
} |
||||
arith_encode(cinfo, st, 0); |
||||
/* Section F.1.4.4.1.2: Establish dc_context conditioning category */ |
||||
if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) |
||||
entropy->dc_context[ci] = 0; /* zero diff category */ |
||||
else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) |
||||
entropy->dc_context[ci] += 8; /* large diff category */ |
||||
/* Figure F.9: Encoding the magnitude bit pattern of v */ |
||||
st += 14; |
||||
while (m >>= 1) |
||||
arith_encode(cinfo, st, (m & v) ? 1 : 0); |
||||
} |
||||
|
||||
/* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ |
||||
|
||||
if ((ke = cinfo->lim_Se) == 0) continue; |
||||
tbl = compptr->ac_tbl_no; |
||||
|
||||
/* Establish EOB (end-of-block) index */ |
||||
do { |
||||
if ((*block)[natural_order[ke]]) break; |
||||
} while (--ke); |
||||
|
||||
/* Figure F.5: Encode_AC_Coefficients */ |
||||
for (k = 0; k < ke;) { |
||||
st = entropy->ac_stats[tbl] + 3 * k; |
||||
arith_encode(cinfo, st, 0); /* EOB decision */ |
||||
while ((v = (*block)[natural_order[++k]]) == 0) { |
||||
arith_encode(cinfo, st + 1, 0); |
||||
st += 3; |
||||
} |
||||
arith_encode(cinfo, st + 1, 1); |
||||
/* Figure F.6: Encoding nonzero value v */ |
||||
/* Figure F.7: Encoding the sign of v */ |
||||
if (v > 0) { |
||||
arith_encode(cinfo, entropy->fixed_bin, 0); |
||||
} else { |
||||
v = -v; |
||||
arith_encode(cinfo, entropy->fixed_bin, 1); |
||||
} |
||||
st += 2; |
||||
/* Figure F.8: Encoding the magnitude category of v */ |
||||
m = 0; |
||||
if (v -= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m = 1; |
||||
v2 = v; |
||||
if (v2 >>= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m <<= 1; |
||||
st = entropy->ac_stats[tbl] + |
||||
(k <= cinfo->arith_ac_K[tbl] ? 189 : 217); |
||||
while (v2 >>= 1) { |
||||
arith_encode(cinfo, st, 1); |
||||
m <<= 1; |
||||
st += 1; |
||||
} |
||||
} |
||||
} |
||||
arith_encode(cinfo, st, 0); |
||||
/* Figure F.9: Encoding the magnitude bit pattern of v */ |
||||
st += 14; |
||||
while (m >>= 1) |
||||
arith_encode(cinfo, st, (m & v) ? 1 : 0); |
||||
} |
||||
/* Encode EOB decision only if k < cinfo->lim_Se */ |
||||
if (k < cinfo->lim_Se) { |
||||
st = entropy->ac_stats[tbl] + 3 * k; |
||||
arith_encode(cinfo, st, 1); |
||||
} |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Initialize for an arithmetic-compressed scan. |
||||
*/ |
||||
|
||||
METHODDEF(void) |
||||
start_pass (j_compress_ptr cinfo, boolean gather_statistics) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
int ci, tbl; |
||||
jpeg_component_info * compptr; |
||||
|
||||
if (gather_statistics) |
||||
/* Make sure to avoid that in the master control logic!
|
||||
* We are fully adaptive here and need no extra |
||||
* statistics gathering pass! |
||||
*/ |
||||
ERREXIT(cinfo, JERR_NOT_COMPILED); |
||||
|
||||
/* We assume jcmaster.c already validated the progressive scan parameters. */ |
||||
|
||||
/* Select execution routines */ |
||||
if (cinfo->progressive_mode) { |
||||
if (cinfo->Ah == 0) { |
||||
if (cinfo->Ss == 0) |
||||
entropy->pub.encode_mcu = encode_mcu_DC_first; |
||||
else |
||||
entropy->pub.encode_mcu = encode_mcu_AC_first; |
||||
} else { |
||||
if (cinfo->Ss == 0) |
||||
entropy->pub.encode_mcu = encode_mcu_DC_refine; |
||||
else |
||||
entropy->pub.encode_mcu = encode_mcu_AC_refine; |
||||
} |
||||
} else |
||||
entropy->pub.encode_mcu = encode_mcu; |
||||
|
||||
/* Allocate & initialize requested statistics areas */ |
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
/* DC needs no table for refinement scan */ |
||||
if (cinfo->Ss == 0 && cinfo->Ah == 0) { |
||||
tbl = compptr->dc_tbl_no; |
||||
if (tbl < 0 || tbl >= NUM_ARITH_TBLS) |
||||
ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); |
||||
if (entropy->dc_stats[tbl] == NULL) |
||||
entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) |
||||
((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); |
||||
MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); |
||||
/* Initialize DC predictions to 0 */ |
||||
entropy->last_dc_val[ci] = 0; |
||||
entropy->dc_context[ci] = 0; |
||||
} |
||||
/* AC needs no table when not present */ |
||||
if (cinfo->Se) { |
||||
tbl = compptr->ac_tbl_no; |
||||
if (tbl < 0 || tbl >= NUM_ARITH_TBLS) |
||||
ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); |
||||
if (entropy->ac_stats[tbl] == NULL) |
||||
entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) |
||||
((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); |
||||
MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); |
||||
#ifdef CALCULATE_SPECTRAL_CONDITIONING |
||||
if (cinfo->progressive_mode) |
||||
/* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */ |
||||
cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4); |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
/* Initialize arithmetic encoding variables */ |
||||
entropy->c = 0; |
||||
entropy->a = 0x10000L; |
||||
entropy->sc = 0; |
||||
entropy->zc = 0; |
||||
entropy->ct = 11; |
||||
entropy->buffer = -1; /* empty */ |
||||
|
||||
/* Initialize restart stuff */ |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num = 0; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Module initialization routine for arithmetic entropy encoding. |
||||
*/ |
||||
|
||||
GLOBAL(void) |
||||
jinit_arith_encoder (j_compress_ptr cinfo) |
||||
{ |
||||
arith_entropy_ptr entropy; |
||||
int i; |
||||
|
||||
entropy = (arith_entropy_ptr) |
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
||||
SIZEOF(arith_entropy_encoder)); |
||||
cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; |
||||
entropy->pub.start_pass = start_pass; |
||||
entropy->pub.finish_pass = finish_pass; |
||||
|
||||
/* Mark tables unallocated */ |
||||
for (i = 0; i < NUM_ARITH_TBLS; i++) { |
||||
entropy->dc_stats[i] = NULL; |
||||
entropy->ac_stats[i] = NULL; |
||||
} |
||||
|
||||
/* Initialize index for fixed probability estimation */ |
||||
entropy->fixed_bin[0] = 113; |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,47 +0,0 @@ |
||||
/*
|
||||
* jchuff.h |
||||
* |
||||
* Copyright (C) 1991-1997, Thomas G. Lane. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains declarations for Huffman entropy encoding routines |
||||
* that are shared between the sequential encoder (jchuff.c) and the |
||||
* progressive encoder (jcphuff.c). No other modules need to see these. |
||||
*/ |
||||
|
||||
/* The legal range of a DCT coefficient is
|
||||
* -1024 .. +1023 for 8-bit data; |
||||
* -16384 .. +16383 for 12-bit data. |
||||
* Hence the magnitude should always fit in 10 or 14 bits respectively. |
||||
*/ |
||||
|
||||
#if BITS_IN_JSAMPLE == 8 |
||||
#define MAX_COEF_BITS 10 |
||||
#else |
||||
#define MAX_COEF_BITS 14 |
||||
#endif |
||||
|
||||
/* Derived data constructed for each Huffman table */ |
||||
|
||||
typedef struct { |
||||
unsigned int ehufco[256]; /* code for each symbol */ |
||||
char ehufsi[256]; /* length of code for each symbol */ |
||||
/* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */ |
||||
} c_derived_tbl; |
||||
|
||||
/* Short forms of external names for systems with brain-damaged linkers. */ |
||||
|
||||
#ifdef NEED_SHORT_EXTERNAL_NAMES |
||||
#define jpeg_make_c_derived_tbl jMkCDerived |
||||
#define jpeg_gen_optimal_table jGenOptTbl |
||||
#endif /* NEED_SHORT_EXTERNAL_NAMES */ |
||||
|
||||
/* Expand a Huffman table definition into the derived format */ |
||||
EXTERN(void) jpeg_make_c_derived_tbl |
||||
JPP((j_compress_ptr cinfo, boolean isDC, int tblno, |
||||
c_derived_tbl ** pdtbl)); |
||||
|
||||
/* Generate an optimal table definition given the specified counts */ |
||||
EXTERN(void) jpeg_gen_optimal_table |
||||
JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])); |
@ -1,833 +0,0 @@ |
||||
/*
|
||||
* jcphuff.c |
||||
* |
||||
* Copyright (C) 1995-1997, Thomas G. Lane. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains Huffman entropy encoding routines for progressive JPEG. |
||||
* |
||||
* We do not support output suspension in this module, since the library |
||||
* currently does not allow multiple-scan files to be written with output |
||||
* suspension. |
||||
*/ |
||||
|
||||
#define JPEG_INTERNALS |
||||
#include "jinclude.h" |
||||
#include "jpeglib.h" |
||||
#include "jchuff.h" /* Declarations shared with jchuff.c */ |
||||
|
||||
#ifdef C_PROGRESSIVE_SUPPORTED |
||||
|
||||
/* Expanded entropy encoder object for progressive Huffman encoding. */ |
||||
|
||||
typedef struct { |
||||
struct jpeg_entropy_encoder pub; /* public fields */ |
||||
|
||||
/* Mode flag: TRUE for optimization, FALSE for actual data output */ |
||||
boolean gather_statistics; |
||||
|
||||
/* Bit-level coding status.
|
||||
* next_output_byte/free_in_buffer are local copies of cinfo->dest fields. |
||||
*/ |
||||
JOCTET * next_output_byte; /* => next byte to write in buffer */ |
||||
size_t free_in_buffer; /* # of byte spaces remaining in buffer */ |
||||
INT32 put_buffer; /* current bit-accumulation buffer */ |
||||
int put_bits; /* # of bits now in it */ |
||||
j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */ |
||||
|
||||
/* Coding status for DC components */ |
||||
int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
||||
|
||||
/* Coding status for AC components */ |
||||
int ac_tbl_no; /* the table number of the single component */ |
||||
unsigned int EOBRUN; /* run length of EOBs */ |
||||
unsigned int BE; /* # of buffered correction bits before MCU */ |
||||
char * bit_buffer; /* buffer for correction bits (1 per char) */ |
||||
/* packing correction bits tightly would save some space but cost time... */ |
||||
|
||||
unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
||||
int next_restart_num; /* next restart number to write (0-7) */ |
||||
|
||||
/* Pointers to derived tables (these workspaces have image lifespan).
|
||||
* Since any one scan codes only DC or only AC, we only need one set |
||||
* of tables, not one for DC and one for AC. |
||||
*/ |
||||
c_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; |
||||
|
||||
/* Statistics tables for optimization; again, one set is enough */ |
||||
long * count_ptrs[NUM_HUFF_TBLS]; |
||||
} phuff_entropy_encoder; |
||||
|
||||
typedef phuff_entropy_encoder * phuff_entropy_ptr; |
||||
|
||||
/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
|
||||
* buffer can hold. Larger sizes may slightly improve compression, but |
||||
* 1000 is already well into the realm of overkill. |
||||
* The minimum safe size is 64 bits. |
||||
*/ |
||||
|
||||
#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */ |
||||
|
||||
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
|
||||
* We assume that int right shift is unsigned if INT32 right shift is, |
||||
* which should be safe. |
||||
*/ |
||||
|
||||
#ifdef RIGHT_SHIFT_IS_UNSIGNED |
||||
#define ISHIFT_TEMPS int ishift_temp; |
||||
#define IRIGHT_SHIFT(x,shft) \ |
||||
((ishift_temp = (x)) < 0 ? \
|
||||
(ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
|
||||
(ishift_temp >> (shft))) |
||||
#else |
||||
#define ISHIFT_TEMPS |
||||
#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) |
||||
#endif |
||||
|
||||
/* Forward declarations */ |
||||
METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo, |
||||
JBLOCKROW *MCU_data)); |
||||
METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo, |
||||
JBLOCKROW *MCU_data)); |
||||
METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo, |
||||
JBLOCKROW *MCU_data)); |
||||
METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo, |
||||
JBLOCKROW *MCU_data)); |
||||
METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo)); |
||||
METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo)); |
||||
|
||||
|
||||
/*
|
||||
* Initialize for a Huffman-compressed scan using progressive JPEG. |
||||
*/ |
||||
|
||||
METHODDEF(void) |
||||
start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) |
||||
{
|
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
boolean is_DC_band; |
||||
int ci, tbl; |
||||
jpeg_component_info * compptr; |
||||
|
||||
entropy->cinfo = cinfo; |
||||
entropy->gather_statistics = gather_statistics; |
||||
|
||||
is_DC_band = (cinfo->Ss == 0); |
||||
|
||||
/* We assume jcmaster.c already validated the scan parameters. */ |
||||
|
||||
/* Select execution routines */ |
||||
if (cinfo->Ah == 0) { |
||||
if (is_DC_band) |
||||
entropy->pub.encode_mcu = encode_mcu_DC_first; |
||||
else |
||||
entropy->pub.encode_mcu = encode_mcu_AC_first; |
||||
} else { |
||||
if (is_DC_band) |
||||
entropy->pub.encode_mcu = encode_mcu_DC_refine; |
||||
else { |
||||
entropy->pub.encode_mcu = encode_mcu_AC_refine; |
||||
/* AC refinement needs a correction bit buffer */ |
||||
if (entropy->bit_buffer == NULL) |
||||
entropy->bit_buffer = (char *) |
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
||||
MAX_CORR_BITS * SIZEOF(char)); |
||||
} |
||||
} |
||||
if (gather_statistics) |
||||
entropy->pub.finish_pass = finish_pass_gather_phuff; |
||||
else |
||||
entropy->pub.finish_pass = finish_pass_phuff; |
||||
|
||||
/* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
|
||||
* for AC coefficients. |
||||
*/ |
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
/* Initialize DC predictions to 0 */ |
||||
entropy->last_dc_val[ci] = 0; |
||||
/* Get table index */ |
||||
if (is_DC_band) { |
||||
if (cinfo->Ah != 0) /* DC refinement needs no table */ |
||||
continue; |
||||
tbl = compptr->dc_tbl_no; |
||||
} else { |
||||
entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; |
||||
} |
||||
if (gather_statistics) { |
||||
/* Check for invalid table index */ |
||||
/* (make_c_derived_tbl does this in the other path) */ |
||||
if (tbl < 0 || tbl >= NUM_HUFF_TBLS) |
||||
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); |
||||
/* Allocate and zero the statistics tables */ |
||||
/* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ |
||||
if (entropy->count_ptrs[tbl] == NULL) |
||||
entropy->count_ptrs[tbl] = (long *) |
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
||||
257 * SIZEOF(long)); |
||||
MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long)); |
||||
} else { |
||||
/* Compute derived values for Huffman table */ |
||||
/* We may do this more than once for a table, but it's not expensive */ |
||||
jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, |
||||
& entropy->derived_tbls[tbl]); |
||||
} |
||||
} |
||||
|
||||
/* Initialize AC stuff */ |
||||
entropy->EOBRUN = 0; |
||||
entropy->BE = 0; |
||||
|
||||
/* Initialize bit buffer to empty */ |
||||
entropy->put_buffer = 0; |
||||
entropy->put_bits = 0; |
||||
|
||||
/* Initialize restart stuff */ |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num = 0; |
||||
} |
||||
|
||||
|
||||
/* Outputting bytes to the file.
|
||||
* NB: these must be called only when actually outputting, |
||||
* that is, entropy->gather_statistics == FALSE. |
||||
*/ |
||||
|
||||
/* Emit a byte */ |
||||
#define emit_byte(entropy,val) \ |
||||
{ *(entropy)->next_output_byte++ = (JOCTET) (val); \
|
||||
if (--(entropy)->free_in_buffer == 0) \
|
||||
dump_buffer(entropy); } |
||||
|
||||
|
||||
LOCAL(void) |
||||
dump_buffer (phuff_entropy_ptr entropy) |
||||
/* Empty the output buffer; we do not support suspension in this module. */ |
||||
{ |
||||
struct jpeg_destination_mgr * dest = entropy->cinfo->dest; |
||||
|
||||
if (! (*dest->empty_output_buffer) (entropy->cinfo)) |
||||
ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); |
||||
/* After a successful buffer dump, must reset buffer pointers */ |
||||
entropy->next_output_byte = dest->next_output_byte; |
||||
entropy->free_in_buffer = dest->free_in_buffer; |
||||
} |
||||
|
||||
|
||||
/* Outputting bits to the file */ |
||||
|
||||
/* Only the right 24 bits of put_buffer are used; the valid bits are
|
||||
* left-justified in this part. At most 16 bits can be passed to emit_bits |
||||
* in one call, and we never retain more than 7 bits in put_buffer |
||||
* between calls, so 24 bits are sufficient. |
||||
*/ |
||||
|
||||
INLINE |
||||
LOCAL(void) |
||||
emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) |
||||
/* Emit some bits, unless we are in gather mode */ |
||||
{ |
||||
/* This routine is heavily used, so it's worth coding tightly. */ |
||||
register INT32 put_buffer = (INT32) code; |
||||
register int put_bits = entropy->put_bits; |
||||
|
||||
/* if size is 0, caller used an invalid Huffman table entry */ |
||||
if (size == 0) |
||||
ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); |
||||
|
||||
if (entropy->gather_statistics) |
||||
return; /* do nothing if we're only getting stats */ |
||||
|
||||
put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */ |
||||
|
||||
put_bits += size; /* new number of bits in buffer */ |
||||
|
||||
put_buffer <<= 24 - put_bits; /* align incoming bits */ |
||||
|
||||
put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */ |
||||
|
||||
while (put_bits >= 8) { |
||||
int c = (int) ((put_buffer >> 16) & 0xFF); |
||||
|
||||
emit_byte(entropy, c); |
||||
if (c == 0xFF) { /* need to stuff a zero byte? */ |
||||
emit_byte(entropy, 0); |
||||
} |
||||
put_buffer <<= 8; |
||||
put_bits -= 8; |
||||
} |
||||
|
||||
entropy->put_buffer = put_buffer; /* update variables */ |
||||
entropy->put_bits = put_bits; |
||||
} |
||||
|
||||
|
||||
LOCAL(void) |
||||
flush_bits (phuff_entropy_ptr entropy) |
||||
{ |
||||
emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */ |
||||
entropy->put_buffer = 0; /* and reset bit-buffer to empty */ |
||||
entropy->put_bits = 0; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Emit (or just count) a Huffman symbol. |
||||
*/ |
||||
|
||||
INLINE |
||||
LOCAL(void) |
||||
emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) |
||||
{ |
||||
if (entropy->gather_statistics) |
||||
entropy->count_ptrs[tbl_no][symbol]++; |
||||
else { |
||||
c_derived_tbl * tbl = entropy->derived_tbls[tbl_no]; |
||||
emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Emit bits from a correction bit buffer. |
||||
*/ |
||||
|
||||
LOCAL(void) |
||||
emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart, |
||||
unsigned int nbits) |
||||
{ |
||||
if (entropy->gather_statistics) |
||||
return; /* no real work */ |
||||
|
||||
while (nbits > 0) { |
||||
emit_bits(entropy, (unsigned int) (*bufstart), 1); |
||||
bufstart++; |
||||
nbits--; |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Emit any pending EOBRUN symbol. |
||||
*/ |
||||
|
||||
LOCAL(void) |
||||
emit_eobrun (phuff_entropy_ptr entropy) |
||||
{ |
||||
register int temp, nbits; |
||||
|
||||
if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */ |
||||
temp = entropy->EOBRUN; |
||||
nbits = 0; |
||||
while ((temp >>= 1)) |
||||
nbits++; |
||||
/* safety check: shouldn't happen given limited correction-bit buffer */ |
||||
if (nbits > 14) |
||||
ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); |
||||
|
||||
emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); |
||||
if (nbits) |
||||
emit_bits(entropy, entropy->EOBRUN, nbits); |
||||
|
||||
entropy->EOBRUN = 0; |
||||
|
||||
/* Emit any buffered correction bits */ |
||||
emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); |
||||
entropy->BE = 0; |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Emit a restart marker & resynchronize predictions. |
||||
*/ |
||||
|
||||
LOCAL(void) |
||||
emit_restart (phuff_entropy_ptr entropy, int restart_num) |
||||
{ |
||||
int ci; |
||||
|
||||
emit_eobrun(entropy); |
||||
|
||||
if (! entropy->gather_statistics) { |
||||
flush_bits(entropy); |
||||
emit_byte(entropy, 0xFF); |
||||
emit_byte(entropy, JPEG_RST0 + restart_num); |
||||
} |
||||
|
||||
if (entropy->cinfo->Ss == 0) { |
||||
/* Re-initialize DC predictions to 0 */ |
||||
for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) |
||||
entropy->last_dc_val[ci] = 0; |
||||
} else { |
||||
/* Re-initialize all AC-related fields to 0 */ |
||||
entropy->EOBRUN = 0; |
||||
entropy->BE = 0; |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU encoding for DC initial scan (either spectral selection, |
||||
* or first pass of successive approximation). |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
register int temp, temp2; |
||||
register int nbits; |
||||
int blkn, ci; |
||||
int Al = cinfo->Al; |
||||
JBLOCKROW block; |
||||
jpeg_component_info * compptr; |
||||
ISHIFT_TEMPS |
||||
|
||||
entropy->next_output_byte = cinfo->dest->next_output_byte; |
||||
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) |
||||
if (entropy->restarts_to_go == 0) |
||||
emit_restart(entropy, entropy->next_restart_num); |
||||
|
||||
/* Encode the MCU data blocks */ |
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
block = MCU_data[blkn]; |
||||
ci = cinfo->MCU_membership[blkn]; |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
|
||||
/* Compute the DC value after the required point transform by Al.
|
||||
* This is simply an arithmetic right shift. |
||||
*/ |
||||
temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); |
||||
|
||||
/* DC differences are figured on the point-transformed values. */ |
||||
temp = temp2 - entropy->last_dc_val[ci]; |
||||
entropy->last_dc_val[ci] = temp2; |
||||
|
||||
/* Encode the DC coefficient difference per section G.1.2.1 */ |
||||
temp2 = temp; |
||||
if (temp < 0) { |
||||
temp = -temp; /* temp is abs value of input */ |
||||
/* For a negative input, want temp2 = bitwise complement of abs(input) */ |
||||
/* This code assumes we are on a two's complement machine */ |
||||
temp2--; |
||||
} |
||||
|
||||
/* Find the number of bits needed for the magnitude of the coefficient */ |
||||
nbits = 0; |
||||
while (temp) { |
||||
nbits++; |
||||
temp >>= 1; |
||||
} |
||||
/* Check for out-of-range coefficient values.
|
||||
* Since we're encoding a difference, the range limit is twice as much. |
||||
*/ |
||||
if (nbits > MAX_COEF_BITS+1) |
||||
ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
||||
|
||||
/* Count/emit the Huffman-coded symbol for the number of bits */ |
||||
emit_symbol(entropy, compptr->dc_tbl_no, nbits); |
||||
|
||||
/* Emit that number of bits of the value, if positive, */ |
||||
/* or the complement of its magnitude, if negative. */ |
||||
if (nbits) /* emit_bits rejects calls with size 0 */ |
||||
emit_bits(entropy, (unsigned int) temp2, nbits); |
||||
} |
||||
|
||||
cinfo->dest->next_output_byte = entropy->next_output_byte; |
||||
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
||||
|
||||
/* Update restart-interval state too */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU encoding for AC initial scan (either spectral selection, |
||||
* or first pass of successive approximation). |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
register int temp, temp2; |
||||
register int nbits; |
||||
register int r, k; |
||||
int Se = cinfo->Se; |
||||
int Al = cinfo->Al; |
||||
JBLOCKROW block; |
||||
|
||||
entropy->next_output_byte = cinfo->dest->next_output_byte; |
||||
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) |
||||
if (entropy->restarts_to_go == 0) |
||||
emit_restart(entropy, entropy->next_restart_num); |
||||
|
||||
/* Encode the MCU data block */ |
||||
block = MCU_data[0]; |
||||
|
||||
/* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ |
||||
|
||||
r = 0; /* r = run length of zeros */ |
||||
|
||||
for (k = cinfo->Ss; k <= Se; k++) { |
||||
if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { |
||||
r++; |
||||
continue; |
||||
} |
||||
/* We must apply the point transform by Al. For AC coefficients this
|
||||
* is an integer division with rounding towards 0. To do this portably |
||||
* in C, we shift after obtaining the absolute value; so the code is |
||||
* interwoven with finding the abs value (temp) and output bits (temp2). |
||||
*/ |
||||
if (temp < 0) { |
||||
temp = -temp; /* temp is abs value of input */ |
||||
temp >>= Al; /* apply the point transform */ |
||||
/* For a negative coef, want temp2 = bitwise complement of abs(coef) */ |
||||
temp2 = ~temp; |
||||
} else { |
||||
temp >>= Al; /* apply the point transform */ |
||||
temp2 = temp; |
||||
} |
||||
/* Watch out for case that nonzero coef is zero after point transform */ |
||||
if (temp == 0) { |
||||
r++; |
||||
continue; |
||||
} |
||||
|
||||
/* Emit any pending EOBRUN */ |
||||
if (entropy->EOBRUN > 0) |
||||
emit_eobrun(entropy); |
||||
/* if run length > 15, must emit special run-length-16 codes (0xF0) */ |
||||
while (r > 15) { |
||||
emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); |
||||
r -= 16; |
||||
} |
||||
|
||||
/* Find the number of bits needed for the magnitude of the coefficient */ |
||||
nbits = 1; /* there must be at least one 1 bit */ |
||||
while ((temp >>= 1)) |
||||
nbits++; |
||||
/* Check for out-of-range coefficient values */ |
||||
if (nbits > MAX_COEF_BITS) |
||||
ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
||||
|
||||
/* Count/emit Huffman symbol for run length / number of bits */ |
||||
emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); |
||||
|
||||
/* Emit that number of bits of the value, if positive, */ |
||||
/* or the complement of its magnitude, if negative. */ |
||||
emit_bits(entropy, (unsigned int) temp2, nbits); |
||||
|
||||
r = 0; /* reset zero run length */ |
||||
} |
||||
|
||||
if (r > 0) { /* If there are trailing zeroes, */ |
||||
entropy->EOBRUN++; /* count an EOB */ |
||||
if (entropy->EOBRUN == 0x7FFF) |
||||
emit_eobrun(entropy); /* force it out to avoid overflow */ |
||||
} |
||||
|
||||
cinfo->dest->next_output_byte = entropy->next_output_byte; |
||||
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
||||
|
||||
/* Update restart-interval state too */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU encoding for DC successive approximation refinement scan. |
||||
* Note: we assume such scans can be multi-component, although the spec |
||||
* is not very clear on the point. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
register int temp; |
||||
int blkn; |
||||
int Al = cinfo->Al; |
||||
JBLOCKROW block; |
||||
|
||||
entropy->next_output_byte = cinfo->dest->next_output_byte; |
||||
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) |
||||
if (entropy->restarts_to_go == 0) |
||||
emit_restart(entropy, entropy->next_restart_num); |
||||
|
||||
/* Encode the MCU data blocks */ |
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
block = MCU_data[blkn]; |
||||
|
||||
/* We simply emit the Al'th bit of the DC coefficient value. */ |
||||
temp = (*block)[0]; |
||||
emit_bits(entropy, (unsigned int) (temp >> Al), 1); |
||||
} |
||||
|
||||
cinfo->dest->next_output_byte = entropy->next_output_byte; |
||||
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
||||
|
||||
/* Update restart-interval state too */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU encoding for AC successive approximation refinement scan. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
register int temp; |
||||
register int r, k; |
||||
int EOB; |
||||
char *BR_buffer; |
||||
unsigned int BR; |
||||
int Se = cinfo->Se; |
||||
int Al = cinfo->Al; |
||||
JBLOCKROW block; |
||||
int absvalues[DCTSIZE2]; |
||||
|
||||
entropy->next_output_byte = cinfo->dest->next_output_byte; |
||||
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
||||
|
||||
/* Emit restart marker if needed */ |
||||
if (cinfo->restart_interval) |
||||
if (entropy->restarts_to_go == 0) |
||||
emit_restart(entropy, entropy->next_restart_num); |
||||
|
||||
/* Encode the MCU data block */ |
||||
block = MCU_data[0]; |
||||
|
||||
/* It is convenient to make a pre-pass to determine the transformed
|
||||
* coefficients' absolute values and the EOB position. |
||||
*/ |
||||
EOB = 0; |
||||
for (k = cinfo->Ss; k <= Se; k++) { |
||||
temp = (*block)[jpeg_natural_order[k]]; |
||||
/* We must apply the point transform by Al. For AC coefficients this
|
||||
* is an integer division with rounding towards 0. To do this portably |
||||
* in C, we shift after obtaining the absolute value. |
||||
*/ |
||||
if (temp < 0) |
||||
temp = -temp; /* temp is abs value of input */ |
||||
temp >>= Al; /* apply the point transform */ |
||||
absvalues[k] = temp; /* save abs value for main pass */ |
||||
if (temp == 1) |
||||
EOB = k; /* EOB = index of last newly-nonzero coef */ |
||||
} |
||||
|
||||
/* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ |
||||
|
||||
r = 0; /* r = run length of zeros */ |
||||
BR = 0; /* BR = count of buffered bits added now */ |
||||
BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ |
||||
|
||||
for (k = cinfo->Ss; k <= Se; k++) { |
||||
if ((temp = absvalues[k]) == 0) { |
||||
r++; |
||||
continue; |
||||
} |
||||
|
||||
/* Emit any required ZRLs, but not if they can be folded into EOB */ |
||||
while (r > 15 && k <= EOB) { |
||||
/* emit any pending EOBRUN and the BE correction bits */ |
||||
emit_eobrun(entropy); |
||||
/* Emit ZRL */ |
||||
emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); |
||||
r -= 16; |
||||
/* Emit buffered correction bits that must be associated with ZRL */ |
||||
emit_buffered_bits(entropy, BR_buffer, BR); |
||||
BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ |
||||
BR = 0; |
||||
} |
||||
|
||||
/* If the coef was previously nonzero, it only needs a correction bit.
|
||||
* NOTE: a straight translation of the spec's figure G.7 would suggest |
||||
* that we also need to test r > 15. But if r > 15, we can only get here |
||||
* if k > EOB, which implies that this coefficient is not 1. |
||||
*/ |
||||
if (temp > 1) { |
||||
/* The correction bit is the next bit of the absolute value. */ |
||||
BR_buffer[BR++] = (char) (temp & 1); |
||||
continue; |
||||
} |
||||
|
||||
/* Emit any pending EOBRUN and the BE correction bits */ |
||||
emit_eobrun(entropy); |
||||
|
||||
/* Count/emit Huffman symbol for run length / number of bits */ |
||||
emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); |
||||
|
||||
/* Emit output bit for newly-nonzero coef */ |
||||
temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; |
||||
emit_bits(entropy, (unsigned int) temp, 1); |
||||
|
||||
/* Emit buffered correction bits that must be associated with this code */ |
||||
emit_buffered_bits(entropy, BR_buffer, BR); |
||||
BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ |
||||
BR = 0; |
||||
r = 0; /* reset zero run length */ |
||||
} |
||||
|
||||
if (r > 0 || BR > 0) { /* If there are trailing zeroes, */ |
||||
entropy->EOBRUN++; /* count an EOB */ |
||||
entropy->BE += BR; /* concat my correction bits to older ones */ |
||||
/* We force out the EOB if we risk either:
|
||||
* 1. overflow of the EOB counter; |
||||
* 2. overflow of the correction bit buffer during the next MCU. |
||||
*/ |
||||
if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) |
||||
emit_eobrun(entropy); |
||||
} |
||||
|
||||
cinfo->dest->next_output_byte = entropy->next_output_byte; |
||||
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
||||
|
||||
/* Update restart-interval state too */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) { |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
entropy->next_restart_num++; |
||||
entropy->next_restart_num &= 7; |
||||
} |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Finish up at the end of a Huffman-compressed progressive scan. |
||||
*/ |
||||
|
||||
METHODDEF(void) |
||||
finish_pass_phuff (j_compress_ptr cinfo) |
||||
{
|
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
|
||||
entropy->next_output_byte = cinfo->dest->next_output_byte; |
||||
entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
||||
|
||||
/* Flush out any buffered data */ |
||||
emit_eobrun(entropy); |
||||
flush_bits(entropy); |
||||
|
||||
cinfo->dest->next_output_byte = entropy->next_output_byte; |
||||
cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Finish up a statistics-gathering pass and create the new Huffman tables. |
||||
*/ |
||||
|
||||
METHODDEF(void) |
||||
finish_pass_gather_phuff (j_compress_ptr cinfo) |
||||
{ |
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
boolean is_DC_band; |
||||
int ci, tbl; |
||||
jpeg_component_info * compptr; |
||||
JHUFF_TBL **htblptr; |
||||
boolean did[NUM_HUFF_TBLS]; |
||||
|
||||
/* Flush out buffered data (all we care about is counting the EOB symbol) */ |
||||
emit_eobrun(entropy); |
||||
|
||||
is_DC_band = (cinfo->Ss == 0); |
||||
|
||||
/* It's important not to apply jpeg_gen_optimal_table more than once
|
||||
* per table, because it clobbers the input frequency counts! |
||||
*/ |
||||
MEMZERO(did, SIZEOF(did)); |
||||
|
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
if (is_DC_band) { |
||||
if (cinfo->Ah != 0) /* DC refinement needs no table */ |
||||
continue; |
||||
tbl = compptr->dc_tbl_no; |
||||
} else { |
||||
tbl = compptr->ac_tbl_no; |
||||
} |
||||
if (! did[tbl]) { |
||||
if (is_DC_band) |
||||
htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; |
||||
else |
||||
htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; |
||||
if (*htblptr == NULL) |
||||
*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); |
||||
jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]); |
||||
did[tbl] = TRUE; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Module initialization routine for progressive Huffman entropy encoding. |
||||
*/ |
||||
|
||||
GLOBAL(void) |
||||
jinit_phuff_encoder (j_compress_ptr cinfo) |
||||
{ |
||||
phuff_entropy_ptr entropy; |
||||
int i; |
||||
|
||||
entropy = (phuff_entropy_ptr) |
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
||||
SIZEOF(phuff_entropy_encoder)); |
||||
cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; |
||||
entropy->pub.start_pass = start_pass_phuff; |
||||
|
||||
/* Mark tables unallocated */ |
||||
for (i = 0; i < NUM_HUFF_TBLS; i++) { |
||||
entropy->derived_tbls[i] = NULL; |
||||
entropy->count_ptrs[i] = NULL; |
||||
} |
||||
entropy->bit_buffer = NULL; /* needed only in AC refinement scan */ |
||||
} |
||||
|
||||
#endif /* C_PROGRESSIVE_SUPPORTED */ |
@ -0,0 +1,776 @@ |
||||
/*
|
||||
* jdarith.c |
||||
* |
||||
* Developed 1997-2011 by Guido Vollbeding. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains portable arithmetic entropy decoding routines for JPEG |
||||
* (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). |
||||
* |
||||
* Both sequential and progressive modes are supported in this single module. |
||||
* |
||||
* Suspension is not currently supported in this module. |
||||
*/ |
||||
|
||||
#define JPEG_INTERNALS |
||||
#include "jinclude.h" |
||||
#include "jpeglib.h" |
||||
|
||||
|
||||
/* Expanded entropy decoder object for arithmetic decoding. */ |
||||
|
||||
typedef struct { |
||||
struct jpeg_entropy_decoder pub; /* public fields */ |
||||
|
||||
INT32 c; /* C register, base of coding interval + input bit buffer */ |
||||
INT32 a; /* A register, normalized size of coding interval */ |
||||
int ct; /* bit shift counter, # of bits left in bit buffer part of C */ |
||||
/* init: ct = -16 */ |
||||
/* run: ct = 0..7 */ |
||||
/* error: ct = -1 */ |
||||
int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
||||
int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ |
||||
|
||||
unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
||||
|
||||
/* Pointers to statistics areas (these workspaces have image lifespan) */ |
||||
unsigned char * dc_stats[NUM_ARITH_TBLS]; |
||||
unsigned char * ac_stats[NUM_ARITH_TBLS]; |
||||
|
||||
/* Statistics bin for coding with fixed probability 0.5 */ |
||||
unsigned char fixed_bin[4]; |
||||
} arith_entropy_decoder; |
||||
|
||||
typedef arith_entropy_decoder * arith_entropy_ptr; |
||||
|
||||
/* The following two definitions specify the allocation chunk size
|
||||
* for the statistics area. |
||||
* According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least |
||||
* 49 statistics bins for DC, and 245 statistics bins for AC coding. |
||||
* |
||||
* We use a compact representation with 1 byte per statistics bin, |
||||
* thus the numbers directly represent byte sizes. |
||||
* This 1 byte per statistics bin contains the meaning of the MPS |
||||
* (more probable symbol) in the highest bit (mask 0x80), and the |
||||
* index into the probability estimation state machine table |
||||
* in the lower bits (mask 0x7F). |
||||
*/ |
||||
|
||||
#define DC_STAT_BINS 64 |
||||
#define AC_STAT_BINS 256 |
||||
|
||||
|
||||
LOCAL(int) |
||||
get_byte (j_decompress_ptr cinfo) |
||||
/* Read next input byte; we do not support suspension in this module. */ |
||||
{ |
||||
struct jpeg_source_mgr * src = cinfo->src; |
||||
|
||||
if (src->bytes_in_buffer == 0) |
||||
if (! (*src->fill_input_buffer) (cinfo)) |
||||
ERREXIT(cinfo, JERR_CANT_SUSPEND); |
||||
src->bytes_in_buffer--; |
||||
return GETJOCTET(*src->next_input_byte++); |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* The core arithmetic decoding routine (common in JPEG and JBIG). |
||||
* This needs to go as fast as possible. |
||||
* Machine-dependent optimization facilities |
||||
* are not utilized in this portable implementation. |
||||
* However, this code should be fairly efficient and |
||||
* may be a good base for further optimizations anyway. |
||||
* |
||||
* Return value is 0 or 1 (binary decision). |
||||
* |
||||
* Note: I've changed the handling of the code base & bit |
||||
* buffer register C compared to other implementations |
||||
* based on the standards layout & procedures. |
||||
* While it also contains both the actual base of the |
||||
* coding interval (16 bits) and the next-bits buffer, |
||||
* the cut-point between these two parts is floating |
||||
* (instead of fixed) with the bit shift counter CT. |
||||
* Thus, we also need only one (variable instead of |
||||
* fixed size) shift for the LPS/MPS decision, and |
||||
* we can get away with any renormalization update |
||||
* of C (except for new data insertion, of course). |
||||
* |
||||
* I've also introduced a new scheme for accessing |
||||
* the probability estimation state machine table, |
||||
* derived from Markus Kuhn's JBIG implementation. |
||||
*/ |
||||
|
||||
LOCAL(int) |
||||
arith_decode (j_decompress_ptr cinfo, unsigned char *st) |
||||
{ |
||||
register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; |
||||
register unsigned char nl, nm; |
||||
register INT32 qe, temp; |
||||
register int sv, data; |
||||
|
||||
/* Renormalization & data input per section D.2.6 */ |
||||
while (e->a < 0x8000L) { |
||||
if (--e->ct < 0) { |
||||
/* Need to fetch next data byte */ |
||||
if (cinfo->unread_marker) |
||||
data = 0; /* stuff zero data */ |
||||
else { |
||||
data = get_byte(cinfo); /* read next input byte */ |
||||
if (data == 0xFF) { /* zero stuff or marker code */ |
||||
do data = get_byte(cinfo); |
||||
while (data == 0xFF); /* swallow extra 0xFF bytes */ |
||||
if (data == 0) |
||||
data = 0xFF; /* discard stuffed zero byte */ |
||||
else { |
||||
/* Note: Different from the Huffman decoder, hitting
|
||||
* a marker while processing the compressed data |
||||
* segment is legal in arithmetic coding. |
||||
* The convention is to supply zero data |
||||
* then until decoding is complete. |
||||
*/ |
||||
cinfo->unread_marker = data; |
||||
data = 0; |
||||
} |
||||
} |
||||
} |
||||
e->c = (e->c << 8) | data; /* insert data into C register */ |
||||
if ((e->ct += 8) < 0) /* update bit shift counter */ |
||||
/* Need more initial bytes */ |
||||
if (++e->ct == 0) |
||||
/* Got 2 initial bytes -> re-init A and exit loop */ |
||||
e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ |
||||
} |
||||
e->a <<= 1; |
||||
} |
||||
|
||||
/* Fetch values from our compact representation of Table D.3(D.2):
|
||||
* Qe values and probability estimation state machine |
||||
*/ |
||||
sv = *st; |
||||
qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ |
||||
nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ |
||||
nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ |
||||
|
||||
/* Decode & estimation procedures per sections D.2.4 & D.2.5 */ |
||||
temp = e->a - qe; |
||||
e->a = temp; |
||||
temp <<= e->ct; |
||||
if (e->c >= temp) { |
||||
e->c -= temp; |
||||
/* Conditional LPS (less probable symbol) exchange */ |
||||
if (e->a < qe) { |
||||
e->a = qe; |
||||
*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ |
||||
} else { |
||||
e->a = qe; |
||||
*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ |
||||
sv ^= 0x80; /* Exchange LPS/MPS */ |
||||
} |
||||
} else if (e->a < 0x8000L) { |
||||
/* Conditional MPS (more probable symbol) exchange */ |
||||
if (e->a < qe) { |
||||
*st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ |
||||
sv ^= 0x80; /* Exchange LPS/MPS */ |
||||
} else { |
||||
*st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ |
||||
} |
||||
} |
||||
|
||||
return sv >> 7; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Check for a restart marker & resynchronize decoder. |
||||
*/ |
||||
|
||||
LOCAL(void) |
||||
process_restart (j_decompress_ptr cinfo) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
int ci; |
||||
jpeg_component_info * compptr; |
||||
|
||||
/* Advance past the RSTn marker */ |
||||
if (! (*cinfo->marker->read_restart_marker) (cinfo)) |
||||
ERREXIT(cinfo, JERR_CANT_SUSPEND); |
||||
|
||||
/* Re-initialize statistics areas */ |
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { |
||||
MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); |
||||
/* Reset DC predictions to 0 */ |
||||
entropy->last_dc_val[ci] = 0; |
||||
entropy->dc_context[ci] = 0; |
||||
} |
||||
if ((! cinfo->progressive_mode && cinfo->lim_Se) || |
||||
(cinfo->progressive_mode && cinfo->Ss)) { |
||||
MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); |
||||
} |
||||
} |
||||
|
||||
/* Reset arithmetic decoding variables */ |
||||
entropy->c = 0; |
||||
entropy->a = 0; |
||||
entropy->ct = -16; /* force reading 2 initial bytes to fill C */ |
||||
|
||||
/* Reset restart counter */ |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Arithmetic MCU decoding. |
||||
* Each of these routines decodes and returns one MCU's worth of |
||||
* arithmetic-compressed coefficients. |
||||
* The coefficients are reordered from zigzag order into natural array order, |
||||
* but are not dequantized. |
||||
* |
||||
* The i'th block of the MCU is stored into the block pointed to by |
||||
* MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. |
||||
*/ |
||||
|
||||
/*
|
||||
* MCU decoding for DC initial scan (either spectral selection, |
||||
* or first pass of successive approximation). |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
JBLOCKROW block; |
||||
unsigned char *st; |
||||
int blkn, ci, tbl, sign; |
||||
int v, m; |
||||
|
||||
/* Process restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
process_restart(cinfo); |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
||||
|
||||
/* Outer loop handles each block in the MCU */ |
||||
|
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
block = MCU_data[blkn]; |
||||
ci = cinfo->MCU_membership[blkn]; |
||||
tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; |
||||
|
||||
/* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ |
||||
|
||||
/* Table F.4: Point to statistics bin S0 for DC coefficient coding */ |
||||
st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; |
||||
|
||||
/* Figure F.19: Decode_DC_DIFF */ |
||||
if (arith_decode(cinfo, st) == 0) |
||||
entropy->dc_context[ci] = 0; |
||||
else { |
||||
/* Figure F.21: Decoding nonzero value v */ |
||||
/* Figure F.22: Decoding the sign of v */ |
||||
sign = arith_decode(cinfo, st + 1); |
||||
st += 2; st += sign; |
||||
/* Figure F.23: Decoding the magnitude category of v */ |
||||
if ((m = arith_decode(cinfo, st)) != 0) { |
||||
st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ |
||||
while (arith_decode(cinfo, st)) { |
||||
if ((m <<= 1) == 0x8000) { |
||||
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
||||
entropy->ct = -1; /* magnitude overflow */ |
||||
return TRUE; |
||||
} |
||||
st += 1; |
||||
} |
||||
} |
||||
/* Section F.1.4.4.1.2: Establish dc_context conditioning category */ |
||||
if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) |
||||
entropy->dc_context[ci] = 0; /* zero diff category */ |
||||
else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) |
||||
entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ |
||||
else |
||||
entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ |
||||
v = m; |
||||
/* Figure F.24: Decoding the magnitude bit pattern of v */ |
||||
st += 14; |
||||
while (m >>= 1) |
||||
if (arith_decode(cinfo, st)) v |= m; |
||||
v += 1; if (sign) v = -v; |
||||
entropy->last_dc_val[ci] += v; |
||||
} |
||||
|
||||
/* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ |
||||
(*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU decoding for AC initial scan (either spectral selection, |
||||
* or first pass of successive approximation). |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
JBLOCKROW block; |
||||
unsigned char *st; |
||||
int tbl, sign, k; |
||||
int v, m; |
||||
const int * natural_order; |
||||
|
||||
/* Process restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
process_restart(cinfo); |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
||||
|
||||
natural_order = cinfo->natural_order; |
||||
|
||||
/* There is always only one block per MCU */ |
||||
block = MCU_data[0]; |
||||
tbl = cinfo->cur_comp_info[0]->ac_tbl_no; |
||||
|
||||
/* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ |
||||
|
||||
/* Figure F.20: Decode_AC_coefficients */ |
||||
for (k = cinfo->Ss; k <= cinfo->Se; k++) { |
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1); |
||||
if (arith_decode(cinfo, st)) break; /* EOB flag */ |
||||
while (arith_decode(cinfo, st + 1) == 0) { |
||||
st += 3; k++; |
||||
if (k > cinfo->Se) { |
||||
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
||||
entropy->ct = -1; /* spectral overflow */ |
||||
return TRUE; |
||||
} |
||||
} |
||||
/* Figure F.21: Decoding nonzero value v */ |
||||
/* Figure F.22: Decoding the sign of v */ |
||||
sign = arith_decode(cinfo, entropy->fixed_bin); |
||||
st += 2; |
||||
/* Figure F.23: Decoding the magnitude category of v */ |
||||
if ((m = arith_decode(cinfo, st)) != 0) { |
||||
if (arith_decode(cinfo, st)) { |
||||
m <<= 1; |
||||
st = entropy->ac_stats[tbl] + |
||||
(k <= cinfo->arith_ac_K[tbl] ? 189 : 217); |
||||
while (arith_decode(cinfo, st)) { |
||||
if ((m <<= 1) == 0x8000) { |
||||
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
||||
entropy->ct = -1; /* magnitude overflow */ |
||||
return TRUE; |
||||
} |
||||
st += 1; |
||||
} |
||||
} |
||||
} |
||||
v = m; |
||||
/* Figure F.24: Decoding the magnitude bit pattern of v */ |
||||
st += 14; |
||||
while (m >>= 1) |
||||
if (arith_decode(cinfo, st)) v |= m; |
||||
v += 1; if (sign) v = -v; |
||||
/* Scale and output coefficient in natural (dezigzagged) order */ |
||||
(*block)[natural_order[k]] = (JCOEF) (v << cinfo->Al); |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU decoding for DC successive approximation refinement scan. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
unsigned char *st; |
||||
int p1, blkn; |
||||
|
||||
/* Process restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
process_restart(cinfo); |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
st = entropy->fixed_bin; /* use fixed probability estimation */ |
||||
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
||||
|
||||
/* Outer loop handles each block in the MCU */ |
||||
|
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
/* Encoded data is simply the next bit of the two's-complement DC value */ |
||||
if (arith_decode(cinfo, st)) |
||||
MCU_data[blkn][0][0] |= p1; |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU decoding for AC successive approximation refinement scan. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
JBLOCKROW block; |
||||
JCOEFPTR thiscoef; |
||||
unsigned char *st; |
||||
int tbl, k, kex; |
||||
int p1, m1; |
||||
const int * natural_order; |
||||
|
||||
/* Process restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
process_restart(cinfo); |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
||||
|
||||
natural_order = cinfo->natural_order; |
||||
|
||||
/* There is always only one block per MCU */ |
||||
block = MCU_data[0]; |
||||
tbl = cinfo->cur_comp_info[0]->ac_tbl_no; |
||||
|
||||
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
||||
m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ |
||||
|
||||
/* Establish EOBx (previous stage end-of-block) index */ |
||||
for (kex = cinfo->Se; kex > 0; kex--) |
||||
if ((*block)[natural_order[kex]]) break; |
||||
|
||||
for (k = cinfo->Ss; k <= cinfo->Se; k++) { |
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1); |
||||
if (k > kex) |
||||
if (arith_decode(cinfo, st)) break; /* EOB flag */ |
||||
for (;;) { |
||||
thiscoef = *block + natural_order[k]; |
||||
if (*thiscoef) { /* previously nonzero coef */ |
||||
if (arith_decode(cinfo, st + 2)) { |
||||
if (*thiscoef < 0) |
||||
*thiscoef += m1; |
||||
else |
||||
*thiscoef += p1; |
||||
} |
||||
break; |
||||
} |
||||
if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ |
||||
if (arith_decode(cinfo, entropy->fixed_bin)) |
||||
*thiscoef = m1; |
||||
else |
||||
*thiscoef = p1; |
||||
break; |
||||
} |
||||
st += 3; k++; |
||||
if (k > cinfo->Se) { |
||||
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
||||
entropy->ct = -1; /* spectral overflow */ |
||||
return TRUE; |
||||
} |
||||
} |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Decode one MCU's worth of arithmetic-compressed coefficients. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
jpeg_component_info * compptr; |
||||
JBLOCKROW block; |
||||
unsigned char *st; |
||||
int blkn, ci, tbl, sign, k; |
||||
int v, m; |
||||
const int * natural_order; |
||||
|
||||
/* Process restart marker if needed */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
process_restart(cinfo); |
||||
entropy->restarts_to_go--; |
||||
} |
||||
|
||||
if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
||||
|
||||
natural_order = cinfo->natural_order; |
||||
|
||||
/* Outer loop handles each block in the MCU */ |
||||
|
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
block = MCU_data[blkn]; |
||||
ci = cinfo->MCU_membership[blkn]; |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
|
||||
/* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ |
||||
|
||||
tbl = compptr->dc_tbl_no; |
||||
|
||||
/* Table F.4: Point to statistics bin S0 for DC coefficient coding */ |
||||
st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; |
||||
|
||||
/* Figure F.19: Decode_DC_DIFF */ |
||||
if (arith_decode(cinfo, st) == 0) |
||||
entropy->dc_context[ci] = 0; |
||||
else { |
||||
/* Figure F.21: Decoding nonzero value v */ |
||||
/* Figure F.22: Decoding the sign of v */ |
||||
sign = arith_decode(cinfo, st + 1); |
||||
st += 2; st += sign; |
||||
/* Figure F.23: Decoding the magnitude category of v */ |
||||
if ((m = arith_decode(cinfo, st)) != 0) { |
||||
st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ |
||||
while (arith_decode(cinfo, st)) { |
||||
if ((m <<= 1) == 0x8000) { |
||||
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
||||
entropy->ct = -1; /* magnitude overflow */ |
||||
return TRUE; |
||||
} |
||||
st += 1; |
||||
} |
||||
} |
||||
/* Section F.1.4.4.1.2: Establish dc_context conditioning category */ |
||||
if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) |
||||
entropy->dc_context[ci] = 0; /* zero diff category */ |
||||
else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) |
||||
entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ |
||||
else |
||||
entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ |
||||
v = m; |
||||
/* Figure F.24: Decoding the magnitude bit pattern of v */ |
||||
st += 14; |
||||
while (m >>= 1) |
||||
if (arith_decode(cinfo, st)) v |= m; |
||||
v += 1; if (sign) v = -v; |
||||
entropy->last_dc_val[ci] += v; |
||||
} |
||||
|
||||
(*block)[0] = (JCOEF) entropy->last_dc_val[ci]; |
||||
|
||||
/* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ |
||||
|
||||
if (cinfo->lim_Se == 0) continue; |
||||
tbl = compptr->ac_tbl_no; |
||||
k = 0; |
||||
|
||||
/* Figure F.20: Decode_AC_coefficients */ |
||||
do { |
||||
st = entropy->ac_stats[tbl] + 3 * k; |
||||
if (arith_decode(cinfo, st)) break; /* EOB flag */ |
||||
for (;;) { |
||||
k++; |
||||
if (arith_decode(cinfo, st + 1)) break; |
||||
st += 3; |
||||
if (k >= cinfo->lim_Se) { |
||||
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
||||
entropy->ct = -1; /* spectral overflow */ |
||||
return TRUE; |
||||
} |
||||
} |
||||
/* Figure F.21: Decoding nonzero value v */ |
||||
/* Figure F.22: Decoding the sign of v */ |
||||
sign = arith_decode(cinfo, entropy->fixed_bin); |
||||
st += 2; |
||||
/* Figure F.23: Decoding the magnitude category of v */ |
||||
if ((m = arith_decode(cinfo, st)) != 0) { |
||||
if (arith_decode(cinfo, st)) { |
||||
m <<= 1; |
||||
st = entropy->ac_stats[tbl] + |
||||
(k <= cinfo->arith_ac_K[tbl] ? 189 : 217); |
||||
while (arith_decode(cinfo, st)) { |
||||
if ((m <<= 1) == 0x8000) { |
||||
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
||||
entropy->ct = -1; /* magnitude overflow */ |
||||
return TRUE; |
||||
} |
||||
st += 1; |
||||
} |
||||
} |
||||
} |
||||
v = m; |
||||
/* Figure F.24: Decoding the magnitude bit pattern of v */ |
||||
st += 14; |
||||
while (m >>= 1) |
||||
if (arith_decode(cinfo, st)) v |= m; |
||||
v += 1; if (sign) v = -v; |
||||
(*block)[natural_order[k]] = (JCOEF) v; |
||||
} while (k < cinfo->lim_Se); |
||||
} |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Initialize for an arithmetic-compressed scan. |
||||
*/ |
||||
|
||||
METHODDEF(void) |
||||
start_pass (j_decompress_ptr cinfo) |
||||
{ |
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; |
||||
int ci, tbl; |
||||
jpeg_component_info * compptr; |
||||
|
||||
if (cinfo->progressive_mode) { |
||||
/* Validate progressive scan parameters */ |
||||
if (cinfo->Ss == 0) { |
||||
if (cinfo->Se != 0) |
||||
goto bad; |
||||
} else { |
||||
/* need not check Ss/Se < 0 since they came from unsigned bytes */ |
||||
if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se) |
||||
goto bad; |
||||
/* AC scans may have only one component */ |
||||
if (cinfo->comps_in_scan != 1) |
||||
goto bad; |
||||
} |
||||
if (cinfo->Ah != 0) { |
||||
/* Successive approximation refinement scan: must have Al = Ah-1. */ |
||||
if (cinfo->Ah-1 != cinfo->Al) |
||||
goto bad; |
||||
} |
||||
if (cinfo->Al > 13) { /* need not check for < 0 */ |
||||
bad: |
||||
ERREXIT4(cinfo, JERR_BAD_PROGRESSION, |
||||
cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); |
||||
} |
||||
/* Update progression status, and verify that scan order is legal.
|
||||
* Note that inter-scan inconsistencies are treated as warnings |
||||
* not fatal errors ... not clear if this is right way to behave. |
||||
*/ |
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; |
||||
int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; |
||||
if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ |
||||
WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); |
||||
for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { |
||||
int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; |
||||
if (cinfo->Ah != expected) |
||||
WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); |
||||
coef_bit_ptr[coefi] = cinfo->Al; |
||||
} |
||||
} |
||||
/* Select MCU decoding routine */ |
||||
if (cinfo->Ah == 0) { |
||||
if (cinfo->Ss == 0) |
||||
entropy->pub.decode_mcu = decode_mcu_DC_first; |
||||
else |
||||
entropy->pub.decode_mcu = decode_mcu_AC_first; |
||||
} else { |
||||
if (cinfo->Ss == 0) |
||||
entropy->pub.decode_mcu = decode_mcu_DC_refine; |
||||
else |
||||
entropy->pub.decode_mcu = decode_mcu_AC_refine; |
||||
} |
||||
} else { |
||||
/* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
|
||||
* This ought to be an error condition, but we make it a warning. |
||||
*/ |
||||
if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || |
||||
(cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se)) |
||||
WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); |
||||
/* Select MCU decoding routine */ |
||||
entropy->pub.decode_mcu = decode_mcu; |
||||
} |
||||
|
||||
/* Allocate & initialize requested statistics areas */ |
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { |
||||
tbl = compptr->dc_tbl_no; |
||||
if (tbl < 0 || tbl >= NUM_ARITH_TBLS) |
||||
ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); |
||||
if (entropy->dc_stats[tbl] == NULL) |
||||
entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) |
||||
((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); |
||||
MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); |
||||
/* Initialize DC predictions to 0 */ |
||||
entropy->last_dc_val[ci] = 0; |
||||
entropy->dc_context[ci] = 0; |
||||
} |
||||
if ((! cinfo->progressive_mode && cinfo->lim_Se) || |
||||
(cinfo->progressive_mode && cinfo->Ss)) { |
||||
tbl = compptr->ac_tbl_no; |
||||
if (tbl < 0 || tbl >= NUM_ARITH_TBLS) |
||||
ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); |
||||
if (entropy->ac_stats[tbl] == NULL) |
||||
entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) |
||||
((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); |
||||
MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); |
||||
} |
||||
} |
||||
|
||||
/* Initialize arithmetic decoding variables */ |
||||
entropy->c = 0; |
||||
entropy->a = 0; |
||||
entropy->ct = -16; /* force reading 2 initial bytes to fill C */ |
||||
|
||||
/* Initialize restart counter */ |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Module initialization routine for arithmetic entropy decoding. |
||||
*/ |
||||
|
||||
GLOBAL(void) |
||||
jinit_arith_decoder (j_decompress_ptr cinfo) |
||||
{ |
||||
arith_entropy_ptr entropy; |
||||
int i; |
||||
|
||||
entropy = (arith_entropy_ptr) |
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
||||
SIZEOF(arith_entropy_decoder)); |
||||
cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; |
||||
entropy->pub.start_pass = start_pass; |
||||
|
||||
/* Mark tables unallocated */ |
||||
for (i = 0; i < NUM_ARITH_TBLS; i++) { |
||||
entropy->dc_stats[i] = NULL; |
||||
entropy->ac_stats[i] = NULL; |
||||
} |
||||
|
||||
/* Initialize index for fixed probability estimation */ |
||||
entropy->fixed_bin[0] = 113; |
||||
|
||||
if (cinfo->progressive_mode) { |
||||
/* Create progression status table */ |
||||
int *coef_bit_ptr, ci; |
||||
cinfo->coef_bits = (int (*)[DCTSIZE2]) |
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
||||
cinfo->num_components*DCTSIZE2*SIZEOF(int)); |
||||
coef_bit_ptr = & cinfo->coef_bits[0][0]; |
||||
for (ci = 0; ci < cinfo->num_components; ci++)
|
||||
for (i = 0; i < DCTSIZE2; i++) |
||||
*coef_bit_ptr++ = -1; |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,201 +0,0 @@ |
||||
/*
|
||||
* jdhuff.h |
||||
* |
||||
* Copyright (C) 1991-1997, Thomas G. Lane. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains declarations for Huffman entropy decoding routines |
||||
* that are shared between the sequential decoder (jdhuff.c) and the |
||||
* progressive decoder (jdphuff.c). No other modules need to see these. |
||||
*/ |
||||
|
||||
/* Short forms of external names for systems with brain-damaged linkers. */ |
||||
|
||||
#ifdef NEED_SHORT_EXTERNAL_NAMES |
||||
#define jpeg_make_d_derived_tbl jMkDDerived |
||||
#define jpeg_fill_bit_buffer jFilBitBuf |
||||
#define jpeg_huff_decode jHufDecode |
||||
#endif /* NEED_SHORT_EXTERNAL_NAMES */ |
||||
|
||||
|
||||
/* Derived data constructed for each Huffman table */ |
||||
|
||||
#define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */ |
||||
|
||||
typedef struct { |
||||
/* Basic tables: (element [0] of each array is unused) */ |
||||
INT32 maxcode[18]; /* largest code of length k (-1 if none) */ |
||||
/* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */ |
||||
INT32 valoffset[17]; /* huffval[] offset for codes of length k */ |
||||
/* valoffset[k] = huffval[] index of 1st symbol of code length k, less
|
||||
* the smallest code of length k; so given a code of length k, the |
||||
* corresponding symbol is huffval[code + valoffset[k]] |
||||
*/ |
||||
|
||||
/* Link to public Huffman table (needed only in jpeg_huff_decode) */ |
||||
JHUFF_TBL *pub; |
||||
|
||||
/* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
|
||||
* the input data stream. If the next Huffman code is no more |
||||
* than HUFF_LOOKAHEAD bits long, we can obtain its length and |
||||
* the corresponding symbol directly from these tables. |
||||
*/ |
||||
int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */ |
||||
UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */ |
||||
} d_derived_tbl; |
||||
|
||||
/* Expand a Huffman table definition into the derived format */ |
||||
EXTERN(void) jpeg_make_d_derived_tbl |
||||
JPP((j_decompress_ptr cinfo, boolean isDC, int tblno, |
||||
d_derived_tbl ** pdtbl)); |
||||
|
||||
|
||||
/*
|
||||
* Fetching the next N bits from the input stream is a time-critical operation |
||||
* for the Huffman decoders. We implement it with a combination of inline |
||||
* macros and out-of-line subroutines. Note that N (the number of bits |
||||
* demanded at one time) never exceeds 15 for JPEG use. |
||||
* |
||||
* We read source bytes into get_buffer and dole out bits as needed. |
||||
* If get_buffer already contains enough bits, they are fetched in-line |
||||
* by the macros CHECK_BIT_BUFFER and GET_BITS. When there aren't enough |
||||
* bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer |
||||
* as full as possible (not just to the number of bits needed; this |
||||
* prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer). |
||||
* Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension. |
||||
* On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains |
||||
* at least the requested number of bits --- dummy zeroes are inserted if |
||||
* necessary. |
||||
*/ |
||||
|
||||
typedef INT32 bit_buf_type; /* type of bit-extraction buffer */ |
||||
#define BIT_BUF_SIZE 32 /* size of buffer in bits */ |
||||
|
||||
/* If long is > 32 bits on your machine, and shifting/masking longs is
|
||||
* reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE |
||||
* appropriately should be a win. Unfortunately we can't define the size |
||||
* with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8) |
||||
* because not all machines measure sizeof in 8-bit bytes. |
||||
*/ |
||||
|
||||
typedef struct { /* Bitreading state saved across MCUs */ |
||||
bit_buf_type get_buffer; /* current bit-extraction buffer */ |
||||
int bits_left; /* # of unused bits in it */ |
||||
} bitread_perm_state; |
||||
|
||||
typedef struct { /* Bitreading working state within an MCU */ |
||||
/* Current data source location */ |
||||
/* We need a copy, rather than munging the original, in case of suspension */ |
||||
const JOCTET * next_input_byte; /* => next byte to read from source */ |
||||
size_t bytes_in_buffer; /* # of bytes remaining in source buffer */ |
||||
/* Bit input buffer --- note these values are kept in register variables,
|
||||
* not in this struct, inside the inner loops. |
||||
*/ |
||||
bit_buf_type get_buffer; /* current bit-extraction buffer */ |
||||
int bits_left; /* # of unused bits in it */ |
||||
/* Pointer needed by jpeg_fill_bit_buffer. */ |
||||
j_decompress_ptr cinfo; /* back link to decompress master record */ |
||||
} bitread_working_state; |
||||
|
||||
/* Macros to declare and load/save bitread local variables. */ |
||||
#define BITREAD_STATE_VARS \ |
||||
register bit_buf_type get_buffer; \
|
||||
register int bits_left; \
|
||||
bitread_working_state br_state |
||||
|
||||
#define BITREAD_LOAD_STATE(cinfop,permstate) \ |
||||
br_state.cinfo = cinfop; \
|
||||
br_state.next_input_byte = cinfop->src->next_input_byte; \
|
||||
br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
|
||||
get_buffer = permstate.get_buffer; \
|
||||
bits_left = permstate.bits_left; |
||||
|
||||
#define BITREAD_SAVE_STATE(cinfop,permstate) \ |
||||
cinfop->src->next_input_byte = br_state.next_input_byte; \
|
||||
cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
|
||||
permstate.get_buffer = get_buffer; \
|
||||
permstate.bits_left = bits_left |
||||
|
||||
/*
|
||||
* These macros provide the in-line portion of bit fetching. |
||||
* Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer |
||||
* before using GET_BITS, PEEK_BITS, or DROP_BITS. |
||||
* The variables get_buffer and bits_left are assumed to be locals, |
||||
* but the state struct might not be (jpeg_huff_decode needs this). |
||||
* CHECK_BIT_BUFFER(state,n,action); |
||||
* Ensure there are N bits in get_buffer; if suspend, take action. |
||||
* val = GET_BITS(n); |
||||
* Fetch next N bits. |
||||
* val = PEEK_BITS(n); |
||||
* Fetch next N bits without removing them from the buffer. |
||||
* DROP_BITS(n); |
||||
* Discard next N bits. |
||||
* The value N should be a simple variable, not an expression, because it |
||||
* is evaluated multiple times. |
||||
*/ |
||||
|
||||
#define CHECK_BIT_BUFFER(state,nbits,action) \ |
||||
{ if (bits_left < (nbits)) { \
|
||||
if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \
|
||||
{ action; } \
|
||||
get_buffer = (state).get_buffer; bits_left = (state).bits_left; } } |
||||
|
||||
#define GET_BITS(nbits) \ |
||||
(((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1)) |
||||
|
||||
#define PEEK_BITS(nbits) \ |
||||
(((int) (get_buffer >> (bits_left - (nbits)))) & ((1<<(nbits))-1)) |
||||
|
||||
#define DROP_BITS(nbits) \ |
||||
(bits_left -= (nbits)) |
||||
|
||||
/* Load up the bit buffer to a depth of at least nbits */ |
||||
EXTERN(boolean) jpeg_fill_bit_buffer |
||||
JPP((bitread_working_state * state, register bit_buf_type get_buffer, |
||||
register int bits_left, int nbits)); |
||||
|
||||
|
||||
/*
|
||||
* Code for extracting next Huffman-coded symbol from input bit stream. |
||||
* Again, this is time-critical and we make the main paths be macros. |
||||
* |
||||
* We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits |
||||
* without looping. Usually, more than 95% of the Huffman codes will be 8 |
||||
* or fewer bits long. The few overlength codes are handled with a loop, |
||||
* which need not be inline code. |
||||
* |
||||
* Notes about the HUFF_DECODE macro: |
||||
* 1. Near the end of the data segment, we may fail to get enough bits |
||||
* for a lookahead. In that case, we do it the hard way. |
||||
* 2. If the lookahead table contains no entry, the next code must be |
||||
* more than HUFF_LOOKAHEAD bits long. |
||||
* 3. jpeg_huff_decode returns -1 if forced to suspend. |
||||
*/ |
||||
|
||||
#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \ |
||||
{ register int nb, look; \
|
||||
if (bits_left < HUFF_LOOKAHEAD) { \
|
||||
if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
|
||||
get_buffer = state.get_buffer; bits_left = state.bits_left; \
|
||||
if (bits_left < HUFF_LOOKAHEAD) { \
|
||||
nb = 1; goto slowlabel; \
|
||||
} \
|
||||
} \
|
||||
look = PEEK_BITS(HUFF_LOOKAHEAD); \
|
||||
if ((nb = htbl->look_nbits[look]) != 0) { \
|
||||
DROP_BITS(nb); \
|
||||
result = htbl->look_sym[look]; \
|
||||
} else { \
|
||||
nb = HUFF_LOOKAHEAD+1; \
|
||||
slowlabel: \
|
||||
if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
|
||||
{ failaction; } \
|
||||
get_buffer = state.get_buffer; bits_left = state.bits_left; \
|
||||
} \
|
||||
} |
||||
|
||||
/* Out-of-line case for Huffman code fetching */ |
||||
EXTERN(int) jpeg_huff_decode |
||||
JPP((bitread_working_state * state, register bit_buf_type get_buffer, |
||||
register int bits_left, d_derived_tbl * htbl, int min_bits)); |
@ -1,668 +0,0 @@ |
||||
/*
|
||||
* jdphuff.c |
||||
* |
||||
* Copyright (C) 1995-1997, Thomas G. Lane. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains Huffman entropy decoding routines for progressive JPEG. |
||||
* |
||||
* Much of the complexity here has to do with supporting input suspension. |
||||
* If the data source module demands suspension, we want to be able to back |
||||
* up to the start of the current MCU. To do this, we copy state variables |
||||
* into local working storage, and update them back to the permanent |
||||
* storage only upon successful completion of an MCU. |
||||
*/ |
||||
|
||||
#define JPEG_INTERNALS |
||||
#include "jinclude.h" |
||||
#include "jpeglib.h" |
||||
#include "jdhuff.h" /* Declarations shared with jdhuff.c */ |
||||
|
||||
|
||||
#ifdef D_PROGRESSIVE_SUPPORTED |
||||
|
||||
/*
|
||||
* Expanded entropy decoder object for progressive Huffman decoding. |
||||
* |
||||
* The savable_state subrecord contains fields that change within an MCU, |
||||
* but must not be updated permanently until we complete the MCU. |
||||
*/ |
||||
|
||||
typedef struct { |
||||
unsigned int EOBRUN; /* remaining EOBs in EOBRUN */ |
||||
int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
||||
} savable_state; |
||||
|
||||
/* This macro is to work around compilers with missing or broken
|
||||
* structure assignment. You'll need to fix this code if you have |
||||
* such a compiler and you change MAX_COMPS_IN_SCAN. |
||||
*/ |
||||
|
||||
#ifndef NO_STRUCT_ASSIGN |
||||
#define ASSIGN_STATE(dest,src) ((dest) = (src)) |
||||
#else |
||||
#if MAX_COMPS_IN_SCAN == 4 |
||||
#define ASSIGN_STATE(dest,src) \ |
||||
((dest).EOBRUN = (src).EOBRUN, \
|
||||
(dest).last_dc_val[0] = (src).last_dc_val[0], \
|
||||
(dest).last_dc_val[1] = (src).last_dc_val[1], \
|
||||
(dest).last_dc_val[2] = (src).last_dc_val[2], \
|
||||
(dest).last_dc_val[3] = (src).last_dc_val[3]) |
||||
#endif |
||||
#endif |
||||
|
||||
|
||||
typedef struct { |
||||
struct jpeg_entropy_decoder pub; /* public fields */ |
||||
|
||||
/* These fields are loaded into local variables at start of each MCU.
|
||||
* In case of suspension, we exit WITHOUT updating them. |
||||
*/ |
||||
bitread_perm_state bitstate; /* Bit buffer at start of MCU */ |
||||
savable_state saved; /* Other state at start of MCU */ |
||||
|
||||
/* These fields are NOT loaded into local working state. */ |
||||
unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
||||
|
||||
/* Pointers to derived tables (these workspaces have image lifespan) */ |
||||
d_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; |
||||
|
||||
d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */ |
||||
} phuff_entropy_decoder; |
||||
|
||||
typedef phuff_entropy_decoder * phuff_entropy_ptr; |
||||
|
||||
/* Forward declarations */ |
||||
METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo, |
||||
JBLOCKROW *MCU_data)); |
||||
METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo, |
||||
JBLOCKROW *MCU_data)); |
||||
METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo, |
||||
JBLOCKROW *MCU_data)); |
||||
METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo, |
||||
JBLOCKROW *MCU_data)); |
||||
|
||||
|
||||
/*
|
||||
* Initialize for a Huffman-compressed scan. |
||||
*/ |
||||
|
||||
METHODDEF(void) |
||||
start_pass_phuff_decoder (j_decompress_ptr cinfo) |
||||
{ |
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
boolean is_DC_band, bad; |
||||
int ci, coefi, tbl; |
||||
int *coef_bit_ptr; |
||||
jpeg_component_info * compptr; |
||||
|
||||
is_DC_band = (cinfo->Ss == 0); |
||||
|
||||
/* Validate scan parameters */ |
||||
bad = FALSE; |
||||
if (is_DC_band) { |
||||
if (cinfo->Se != 0) |
||||
bad = TRUE; |
||||
} else { |
||||
/* need not check Ss/Se < 0 since they came from unsigned bytes */ |
||||
if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) |
||||
bad = TRUE; |
||||
/* AC scans may have only one component */ |
||||
if (cinfo->comps_in_scan != 1) |
||||
bad = TRUE; |
||||
} |
||||
if (cinfo->Ah != 0) { |
||||
/* Successive approximation refinement scan: must have Al = Ah-1. */ |
||||
if (cinfo->Al != cinfo->Ah-1) |
||||
bad = TRUE; |
||||
} |
||||
if (cinfo->Al > 13) /* need not check for < 0 */ |
||||
bad = TRUE; |
||||
/* Arguably the maximum Al value should be less than 13 for 8-bit precision,
|
||||
* but the spec doesn't say so, and we try to be liberal about what we |
||||
* accept. Note: large Al values could result in out-of-range DC |
||||
* coefficients during early scans, leading to bizarre displays due to |
||||
* overflows in the IDCT math. But we won't crash. |
||||
*/ |
||||
if (bad) |
||||
ERREXIT4(cinfo, JERR_BAD_PROGRESSION, |
||||
cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); |
||||
/* Update progression status, and verify that scan order is legal.
|
||||
* Note that inter-scan inconsistencies are treated as warnings |
||||
* not fatal errors ... not clear if this is right way to behave. |
||||
*/ |
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
int cindex = cinfo->cur_comp_info[ci]->component_index; |
||||
coef_bit_ptr = & cinfo->coef_bits[cindex][0]; |
||||
if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ |
||||
WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); |
||||
for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { |
||||
int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; |
||||
if (cinfo->Ah != expected) |
||||
WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); |
||||
coef_bit_ptr[coefi] = cinfo->Al; |
||||
} |
||||
} |
||||
|
||||
/* Select MCU decoding routine */ |
||||
if (cinfo->Ah == 0) { |
||||
if (is_DC_band) |
||||
entropy->pub.decode_mcu = decode_mcu_DC_first; |
||||
else |
||||
entropy->pub.decode_mcu = decode_mcu_AC_first; |
||||
} else { |
||||
if (is_DC_band) |
||||
entropy->pub.decode_mcu = decode_mcu_DC_refine; |
||||
else |
||||
entropy->pub.decode_mcu = decode_mcu_AC_refine; |
||||
} |
||||
|
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
/* Make sure requested tables are present, and compute derived tables.
|
||||
* We may build same derived table more than once, but it's not expensive. |
||||
*/ |
||||
if (is_DC_band) { |
||||
if (cinfo->Ah == 0) { /* DC refinement needs no table */ |
||||
tbl = compptr->dc_tbl_no; |
||||
jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, |
||||
& entropy->derived_tbls[tbl]); |
||||
} |
||||
} else { |
||||
tbl = compptr->ac_tbl_no; |
||||
jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, |
||||
& entropy->derived_tbls[tbl]); |
||||
/* remember the single active table */ |
||||
entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; |
||||
} |
||||
/* Initialize DC predictions to 0 */ |
||||
entropy->saved.last_dc_val[ci] = 0; |
||||
} |
||||
|
||||
/* Initialize bitread state variables */ |
||||
entropy->bitstate.bits_left = 0; |
||||
entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ |
||||
entropy->pub.insufficient_data = FALSE; |
||||
|
||||
/* Initialize private state variables */ |
||||
entropy->saved.EOBRUN = 0; |
||||
|
||||
/* Initialize restart counter */ |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Figure F.12: extend sign bit. |
||||
* On some machines, a shift and add will be faster than a table lookup. |
||||
*/ |
||||
|
||||
#ifdef AVOID_TABLES |
||||
|
||||
#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x)) |
||||
|
||||
#else |
||||
|
||||
#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) |
||||
|
||||
static const int extend_test[16] = /* entry n is 2**(n-1) */ |
||||
{ 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, |
||||
0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; |
||||
|
||||
static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ |
||||
{ 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, |
||||
((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, |
||||
((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, |
||||
((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; |
||||
|
||||
#endif /* AVOID_TABLES */ |
||||
|
||||
|
||||
/*
|
||||
* Check for a restart marker & resynchronize decoder. |
||||
* Returns FALSE if must suspend. |
||||
*/ |
||||
|
||||
LOCAL(boolean) |
||||
process_restart (j_decompress_ptr cinfo) |
||||
{ |
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
int ci; |
||||
|
||||
/* Throw away any unused bits remaining in bit buffer; */ |
||||
/* include any full bytes in next_marker's count of discarded bytes */ |
||||
cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; |
||||
entropy->bitstate.bits_left = 0; |
||||
|
||||
/* Advance past the RSTn marker */ |
||||
if (! (*cinfo->marker->read_restart_marker) (cinfo)) |
||||
return FALSE; |
||||
|
||||
/* Re-initialize DC predictions to 0 */ |
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) |
||||
entropy->saved.last_dc_val[ci] = 0; |
||||
/* Re-init EOB run count, too */ |
||||
entropy->saved.EOBRUN = 0; |
||||
|
||||
/* Reset restart counter */ |
||||
entropy->restarts_to_go = cinfo->restart_interval; |
||||
|
||||
/* Reset out-of-data flag, unless read_restart_marker left us smack up
|
||||
* against a marker. In that case we will end up treating the next data |
||||
* segment as empty, and we can avoid producing bogus output pixels by |
||||
* leaving the flag set. |
||||
*/ |
||||
if (cinfo->unread_marker == 0) |
||||
entropy->pub.insufficient_data = FALSE; |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Huffman MCU decoding. |
||||
* Each of these routines decodes and returns one MCU's worth of |
||||
* Huffman-compressed coefficients.
|
||||
* The coefficients are reordered from zigzag order into natural array order, |
||||
* but are not dequantized. |
||||
* |
||||
* The i'th block of the MCU is stored into the block pointed to by |
||||
* MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. |
||||
* |
||||
* We return FALSE if data source requested suspension. In that case no |
||||
* changes have been made to permanent state. (Exception: some output |
||||
* coefficients may already have been assigned. This is harmless for |
||||
* spectral selection, since we'll just re-assign them on the next call. |
||||
* Successive approximation AC refinement has to be more careful, however.) |
||||
*/ |
||||
|
||||
/*
|
||||
* MCU decoding for DC initial scan (either spectral selection, |
||||
* or first pass of successive approximation). |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{
|
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
int Al = cinfo->Al; |
||||
register int s, r; |
||||
int blkn, ci; |
||||
JBLOCKROW block; |
||||
BITREAD_STATE_VARS; |
||||
savable_state state; |
||||
d_derived_tbl * tbl; |
||||
jpeg_component_info * compptr; |
||||
|
||||
/* Process restart marker if needed; may have to suspend */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
if (! process_restart(cinfo)) |
||||
return FALSE; |
||||
} |
||||
|
||||
/* If we've run out of data, just leave the MCU set to zeroes.
|
||||
* This way, we return uniform gray for the remainder of the segment. |
||||
*/ |
||||
if (! entropy->pub.insufficient_data) { |
||||
|
||||
/* Load up working state */ |
||||
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); |
||||
ASSIGN_STATE(state, entropy->saved); |
||||
|
||||
/* Outer loop handles each block in the MCU */ |
||||
|
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
block = MCU_data[blkn]; |
||||
ci = cinfo->MCU_membership[blkn]; |
||||
compptr = cinfo->cur_comp_info[ci]; |
||||
tbl = entropy->derived_tbls[compptr->dc_tbl_no]; |
||||
|
||||
/* Decode a single block's worth of coefficients */ |
||||
|
||||
/* Section F.2.2.1: decode the DC coefficient difference */ |
||||
HUFF_DECODE(s, br_state, tbl, return FALSE, label1); |
||||
if (s) { |
||||
CHECK_BIT_BUFFER(br_state, s, return FALSE); |
||||
r = GET_BITS(s); |
||||
s = HUFF_EXTEND(r, s); |
||||
} |
||||
|
||||
/* Convert DC difference to actual value, update last_dc_val */ |
||||
s += state.last_dc_val[ci]; |
||||
state.last_dc_val[ci] = s; |
||||
/* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ |
||||
(*block)[0] = (JCOEF) (s << Al); |
||||
} |
||||
|
||||
/* Completed MCU, so update state */ |
||||
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); |
||||
ASSIGN_STATE(entropy->saved, state); |
||||
} |
||||
|
||||
/* Account for restart interval (no-op if not using restarts) */ |
||||
entropy->restarts_to_go--; |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU decoding for AC initial scan (either spectral selection, |
||||
* or first pass of successive approximation). |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{
|
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
int Se = cinfo->Se; |
||||
int Al = cinfo->Al; |
||||
register int s, k, r; |
||||
unsigned int EOBRUN; |
||||
JBLOCKROW block; |
||||
BITREAD_STATE_VARS; |
||||
d_derived_tbl * tbl; |
||||
|
||||
/* Process restart marker if needed; may have to suspend */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
if (! process_restart(cinfo)) |
||||
return FALSE; |
||||
} |
||||
|
||||
/* If we've run out of data, just leave the MCU set to zeroes.
|
||||
* This way, we return uniform gray for the remainder of the segment. |
||||
*/ |
||||
if (! entropy->pub.insufficient_data) { |
||||
|
||||
/* Load up working state.
|
||||
* We can avoid loading/saving bitread state if in an EOB run. |
||||
*/ |
||||
EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ |
||||
|
||||
/* There is always only one block per MCU */ |
||||
|
||||
if (EOBRUN > 0) /* if it's a band of zeroes... */ |
||||
EOBRUN--; /* ...process it now (we do nothing) */ |
||||
else { |
||||
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); |
||||
block = MCU_data[0]; |
||||
tbl = entropy->ac_derived_tbl; |
||||
|
||||
for (k = cinfo->Ss; k <= Se; k++) { |
||||
HUFF_DECODE(s, br_state, tbl, return FALSE, label2); |
||||
r = s >> 4; |
||||
s &= 15; |
||||
if (s) { |
||||
k += r; |
||||
CHECK_BIT_BUFFER(br_state, s, return FALSE); |
||||
r = GET_BITS(s); |
||||
s = HUFF_EXTEND(r, s); |
||||
/* Scale and output coefficient in natural (dezigzagged) order */ |
||||
(*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al); |
||||
} else { |
||||
if (r == 15) { /* ZRL */ |
||||
k += 15; /* skip 15 zeroes in band */ |
||||
} else { /* EOBr, run length is 2^r + appended bits */ |
||||
EOBRUN = 1 << r; |
||||
if (r) { /* EOBr, r > 0 */ |
||||
CHECK_BIT_BUFFER(br_state, r, return FALSE); |
||||
r = GET_BITS(r); |
||||
EOBRUN += r; |
||||
} |
||||
EOBRUN--; /* this band is processed at this moment */ |
||||
break; /* force end-of-band */ |
||||
} |
||||
} |
||||
} |
||||
|
||||
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); |
||||
} |
||||
|
||||
/* Completed MCU, so update state */ |
||||
entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ |
||||
} |
||||
|
||||
/* Account for restart interval (no-op if not using restarts) */ |
||||
entropy->restarts_to_go--; |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU decoding for DC successive approximation refinement scan. |
||||
* Note: we assume such scans can be multi-component, although the spec |
||||
* is not very clear on the point. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{
|
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
||||
int blkn; |
||||
JBLOCKROW block; |
||||
BITREAD_STATE_VARS; |
||||
|
||||
/* Process restart marker if needed; may have to suspend */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
if (! process_restart(cinfo)) |
||||
return FALSE; |
||||
} |
||||
|
||||
/* Not worth the cycles to check insufficient_data here,
|
||||
* since we will not change the data anyway if we read zeroes. |
||||
*/ |
||||
|
||||
/* Load up working state */ |
||||
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); |
||||
|
||||
/* Outer loop handles each block in the MCU */ |
||||
|
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
||||
block = MCU_data[blkn]; |
||||
|
||||
/* Encoded data is simply the next bit of the two's-complement DC value */ |
||||
CHECK_BIT_BUFFER(br_state, 1, return FALSE); |
||||
if (GET_BITS(1)) |
||||
(*block)[0] |= p1; |
||||
/* Note: since we use |=, repeating the assignment later is safe */ |
||||
} |
||||
|
||||
/* Completed MCU, so update state */ |
||||
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); |
||||
|
||||
/* Account for restart interval (no-op if not using restarts) */ |
||||
entropy->restarts_to_go--; |
||||
|
||||
return TRUE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* MCU decoding for AC successive approximation refinement scan. |
||||
*/ |
||||
|
||||
METHODDEF(boolean) |
||||
decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
||||
{
|
||||
phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; |
||||
int Se = cinfo->Se; |
||||
int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
||||
int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ |
||||
register int s, k, r; |
||||
unsigned int EOBRUN; |
||||
JBLOCKROW block; |
||||
JCOEFPTR thiscoef; |
||||
BITREAD_STATE_VARS; |
||||
d_derived_tbl * tbl; |
||||
int num_newnz; |
||||
int newnz_pos[DCTSIZE2]; |
||||
|
||||
/* Process restart marker if needed; may have to suspend */ |
||||
if (cinfo->restart_interval) { |
||||
if (entropy->restarts_to_go == 0) |
||||
if (! process_restart(cinfo)) |
||||
return FALSE; |
||||
} |
||||
|
||||
/* If we've run out of data, don't modify the MCU.
|
||||
*/ |
||||
if (! entropy->pub.insufficient_data) { |
||||
|
||||
/* Load up working state */ |
||||
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); |
||||
EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ |
||||
|
||||
/* There is always only one block per MCU */ |
||||
block = MCU_data[0]; |
||||
tbl = entropy->ac_derived_tbl; |
||||
|
||||
/* If we are forced to suspend, we must undo the assignments to any newly
|
||||
* nonzero coefficients in the block, because otherwise we'd get confused |
||||
* next time about which coefficients were already nonzero. |
||||
* But we need not undo addition of bits to already-nonzero coefficients; |
||||
* instead, we can test the current bit to see if we already did it. |
||||
*/ |
||||
num_newnz = 0; |
||||
|
||||
/* initialize coefficient loop counter to start of band */ |
||||
k = cinfo->Ss; |
||||
|
||||
if (EOBRUN == 0) { |
||||
for (; k <= Se; k++) { |
||||
HUFF_DECODE(s, br_state, tbl, goto undoit, label3); |
||||
r = s >> 4; |
||||
s &= 15; |
||||
if (s) { |
||||
if (s != 1) /* size of new coef should always be 1 */ |
||||
WARNMS(cinfo, JWRN_HUFF_BAD_CODE); |
||||
CHECK_BIT_BUFFER(br_state, 1, goto undoit); |
||||
if (GET_BITS(1)) |
||||
s = p1; /* newly nonzero coef is positive */ |
||||
else |
||||
s = m1; /* newly nonzero coef is negative */ |
||||
} else { |
||||
if (r != 15) { |
||||
EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */ |
||||
if (r) { |
||||
CHECK_BIT_BUFFER(br_state, r, goto undoit); |
||||
r = GET_BITS(r); |
||||
EOBRUN += r; |
||||
} |
||||
break; /* rest of block is handled by EOB logic */ |
||||
} |
||||
/* note s = 0 for processing ZRL */ |
||||
} |
||||
/* Advance over already-nonzero coefs and r still-zero coefs,
|
||||
* appending correction bits to the nonzeroes. A correction bit is 1 |
||||
* if the absolute value of the coefficient must be increased. |
||||
*/ |
||||
do { |
||||
thiscoef = *block + jpeg_natural_order[k]; |
||||
if (*thiscoef != 0) { |
||||
CHECK_BIT_BUFFER(br_state, 1, goto undoit); |
||||
if (GET_BITS(1)) { |
||||
if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ |
||||
if (*thiscoef >= 0) |
||||
*thiscoef += p1; |
||||
else |
||||
*thiscoef += m1; |
||||
} |
||||
} |
||||
} else { |
||||
if (--r < 0) |
||||
break; /* reached target zero coefficient */ |
||||
} |
||||
k++; |
||||
} while (k <= Se); |
||||
if (s) { |
||||
int pos = jpeg_natural_order[k]; |
||||
/* Output newly nonzero coefficient */ |
||||
(*block)[pos] = (JCOEF) s; |
||||
/* Remember its position in case we have to suspend */ |
||||
newnz_pos[num_newnz++] = pos; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (EOBRUN > 0) { |
||||
/* Scan any remaining coefficient positions after the end-of-band
|
||||
* (the last newly nonzero coefficient, if any). Append a correction |
||||
* bit to each already-nonzero coefficient. A correction bit is 1 |
||||
* if the absolute value of the coefficient must be increased. |
||||
*/ |
||||
for (; k <= Se; k++) { |
||||
thiscoef = *block + jpeg_natural_order[k]; |
||||
if (*thiscoef != 0) { |
||||
CHECK_BIT_BUFFER(br_state, 1, goto undoit); |
||||
if (GET_BITS(1)) { |
||||
if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ |
||||
if (*thiscoef >= 0) |
||||
*thiscoef += p1; |
||||
else |
||||
*thiscoef += m1; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
/* Count one block completed in EOB run */ |
||||
EOBRUN--; |
||||
} |
||||
|
||||
/* Completed MCU, so update state */ |
||||
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); |
||||
entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ |
||||
} |
||||
|
||||
/* Account for restart interval (no-op if not using restarts) */ |
||||
entropy->restarts_to_go--; |
||||
|
||||
return TRUE; |
||||
|
||||
undoit: |
||||
/* Re-zero any output coefficients that we made newly nonzero */ |
||||
while (num_newnz > 0) |
||||
(*block)[newnz_pos[--num_newnz]] = 0; |
||||
|
||||
return FALSE; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Module initialization routine for progressive Huffman entropy decoding. |
||||
*/ |
||||
|
||||
GLOBAL(void) |
||||
jinit_phuff_decoder (j_decompress_ptr cinfo) |
||||
{ |
||||
phuff_entropy_ptr entropy; |
||||
int *coef_bit_ptr; |
||||
int ci, i; |
||||
|
||||
entropy = (phuff_entropy_ptr) |
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
||||
SIZEOF(phuff_entropy_decoder)); |
||||
cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; |
||||
entropy->pub.start_pass = start_pass_phuff_decoder; |
||||
|
||||
/* Mark derived tables unallocated */ |
||||
for (i = 0; i < NUM_HUFF_TBLS; i++) { |
||||
entropy->derived_tbls[i] = NULL; |
||||
} |
||||
|
||||
/* Create progression status table */ |
||||
cinfo->coef_bits = (int (*)[DCTSIZE2]) |
||||
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
||||
cinfo->num_components*DCTSIZE2*SIZEOF(int)); |
||||
coef_bit_ptr = & cinfo->coef_bits[0][0]; |
||||
for (ci = 0; ci < cinfo->num_components; ci++)
|
||||
for (i = 0; i < DCTSIZE2; i++) |
||||
*coef_bit_ptr++ = -1; |
||||
} |
||||
|
||||
#endif /* D_PROGRESSIVE_SUPPORTED */ |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,398 +0,0 @@ |
||||
/*
|
||||
* jidctred.c |
||||
* |
||||
* Copyright (C) 1994-1998, Thomas G. Lane. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains inverse-DCT routines that produce reduced-size output: |
||||
* either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block. |
||||
* |
||||
* The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M) |
||||
* algorithm used in jidctint.c. We simply replace each 8-to-8 1-D IDCT step |
||||
* with an 8-to-4 step that produces the four averages of two adjacent outputs |
||||
* (or an 8-to-2 step producing two averages of four outputs, for 2x2 output). |
||||
* These steps were derived by computing the corresponding values at the end |
||||
* of the normal LL&M code, then simplifying as much as possible. |
||||
* |
||||
* 1x1 is trivial: just take the DC coefficient divided by 8. |
||||
* |
||||
* See jidctint.c for additional comments. |
||||
*/ |
||||
|
||||
#define JPEG_INTERNALS |
||||
#include "jinclude.h" |
||||
#include "jpeglib.h" |
||||
#include "jdct.h" /* Private declarations for DCT subsystem */ |
||||
|
||||
#ifdef IDCT_SCALING_SUPPORTED |
||||
|
||||
|
||||
/*
|
||||
* This module is specialized to the case DCTSIZE = 8. |
||||
*/ |
||||
|
||||
#if DCTSIZE != 8 |
||||
Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ |
||||
#endif |
||||
|
||||
|
||||
/* Scaling is the same as in jidctint.c. */ |
||||
|
||||
#if BITS_IN_JSAMPLE == 8 |
||||
#define CONST_BITS 13 |
||||
#define PASS1_BITS 2 |
||||
#else |
||||
#define CONST_BITS 13 |
||||
#define PASS1_BITS 1 /* lose a little precision to avoid overflow */ |
||||
#endif |
||||
|
||||
/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
|
||||
* causing a lot of useless floating-point operations at run time. |
||||
* To get around this we use the following pre-calculated constants. |
||||
* If you change CONST_BITS you may want to add appropriate values. |
||||
* (With a reasonable C compiler, you can just rely on the FIX() macro...) |
||||
*/ |
||||
|
||||
#if CONST_BITS == 13 |
||||
#define FIX_0_211164243 ((INT32) 1730) /* FIX(0.211164243) */ |
||||
#define FIX_0_509795579 ((INT32) 4176) /* FIX(0.509795579) */ |
||||
#define FIX_0_601344887 ((INT32) 4926) /* FIX(0.601344887) */ |
||||
#define FIX_0_720959822 ((INT32) 5906) /* FIX(0.720959822) */ |
||||
#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */ |
||||
#define FIX_0_850430095 ((INT32) 6967) /* FIX(0.850430095) */ |
||||
#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */ |
||||
#define FIX_1_061594337 ((INT32) 8697) /* FIX(1.061594337) */ |
||||
#define FIX_1_272758580 ((INT32) 10426) /* FIX(1.272758580) */ |
||||
#define FIX_1_451774981 ((INT32) 11893) /* FIX(1.451774981) */ |
||||
#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */ |
||||
#define FIX_2_172734803 ((INT32) 17799) /* FIX(2.172734803) */ |
||||
#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */ |
||||
#define FIX_3_624509785 ((INT32) 29692) /* FIX(3.624509785) */ |
||||
#else |
||||
#define FIX_0_211164243 FIX(0.211164243) |
||||
#define FIX_0_509795579 FIX(0.509795579) |
||||
#define FIX_0_601344887 FIX(0.601344887) |
||||
#define FIX_0_720959822 FIX(0.720959822) |
||||
#define FIX_0_765366865 FIX(0.765366865) |
||||
#define FIX_0_850430095 FIX(0.850430095) |
||||
#define FIX_0_899976223 FIX(0.899976223) |
||||
#define FIX_1_061594337 FIX(1.061594337) |
||||
#define FIX_1_272758580 FIX(1.272758580) |
||||
#define FIX_1_451774981 FIX(1.451774981) |
||||
#define FIX_1_847759065 FIX(1.847759065) |
||||
#define FIX_2_172734803 FIX(2.172734803) |
||||
#define FIX_2_562915447 FIX(2.562915447) |
||||
#define FIX_3_624509785 FIX(3.624509785) |
||||
#endif |
||||
|
||||
|
||||
/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
|
||||
* For 8-bit samples with the recommended scaling, all the variable |
||||
* and constant values involved are no more than 16 bits wide, so a |
||||
* 16x16->32 bit multiply can be used instead of a full 32x32 multiply. |
||||
* For 12-bit samples, a full 32-bit multiplication will be needed. |
||||
*/ |
||||
|
||||
#if BITS_IN_JSAMPLE == 8 |
||||
#define MULTIPLY(var,const) MULTIPLY16C16(var,const) |
||||
#else |
||||
#define MULTIPLY(var,const) ((var) * (const)) |
||||
#endif |
||||
|
||||
|
||||
/* Dequantize a coefficient by multiplying it by the multiplier-table
|
||||
* entry; produce an int result. In this module, both inputs and result |
||||
* are 16 bits or less, so either int or short multiply will work. |
||||
*/ |
||||
|
||||
#define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval)) |
||||
|
||||
|
||||
/*
|
||||
* Perform dequantization and inverse DCT on one block of coefficients, |
||||
* producing a reduced-size 4x4 output block. |
||||
*/ |
||||
|
||||
GLOBAL(void) |
||||
jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
||||
JCOEFPTR coef_block, |
||||
JSAMPARRAY output_buf, JDIMENSION output_col) |
||||
{ |
||||
INT32 tmp0, tmp2, tmp10, tmp12; |
||||
INT32 z1, z2, z3, z4; |
||||
JCOEFPTR inptr; |
||||
ISLOW_MULT_TYPE * quantptr; |
||||
int * wsptr; |
||||
JSAMPROW outptr; |
||||
JSAMPLE *range_limit = IDCT_range_limit(cinfo); |
||||
int ctr; |
||||
int workspace[DCTSIZE*4]; /* buffers data between passes */ |
||||
SHIFT_TEMPS |
||||
|
||||
/* Pass 1: process columns from input, store into work array. */ |
||||
|
||||
inptr = coef_block; |
||||
quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; |
||||
wsptr = workspace; |
||||
for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) { |
||||
/* Don't bother to process column 4, because second pass won't use it */ |
||||
if (ctr == DCTSIZE-4) |
||||
continue; |
||||
if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && |
||||
inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 && |
||||
inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { |
||||
/* AC terms all zero; we need not examine term 4 for 4x4 output */ |
||||
int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; |
||||
|
||||
wsptr[DCTSIZE*0] = dcval; |
||||
wsptr[DCTSIZE*1] = dcval; |
||||
wsptr[DCTSIZE*2] = dcval; |
||||
wsptr[DCTSIZE*3] = dcval; |
||||
|
||||
continue; |
||||
} |
||||
|
||||
/* Even part */ |
||||
|
||||
tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); |
||||
tmp0 <<= (CONST_BITS+1); |
||||
|
||||
z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); |
||||
z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); |
||||
|
||||
tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865); |
||||
|
||||
tmp10 = tmp0 + tmp2; |
||||
tmp12 = tmp0 - tmp2; |
||||
|
||||
/* Odd part */ |
||||
|
||||
z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); |
||||
z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); |
||||
z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); |
||||
z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); |
||||
|
||||
tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */ |
||||
+ MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */ |
||||
+ MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */ |
||||
+ MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */ |
||||
|
||||
tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */ |
||||
+ MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */ |
||||
+ MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */ |
||||
+ MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */ |
||||
|
||||
/* Final output stage */ |
||||
|
||||
wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1); |
||||
wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1); |
||||
wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1); |
||||
wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1); |
||||
} |
||||
|
||||
/* Pass 2: process 4 rows from work array, store into output array. */ |
||||
|
||||
wsptr = workspace; |
||||
for (ctr = 0; ctr < 4; ctr++) { |
||||
outptr = output_buf[ctr] + output_col; |
||||
/* It's not clear whether a zero row test is worthwhile here ... */ |
||||
|
||||
#ifndef NO_ZERO_ROW_TEST |
||||
if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && |
||||
wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { |
||||
/* AC terms all zero */ |
||||
JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3) |
||||
& RANGE_MASK]; |
||||
|
||||
outptr[0] = dcval; |
||||
outptr[1] = dcval; |
||||
outptr[2] = dcval; |
||||
outptr[3] = dcval; |
||||
|
||||
wsptr += DCTSIZE; /* advance pointer to next row */ |
||||
continue; |
||||
} |
||||
#endif |
||||
|
||||
/* Even part */ |
||||
|
||||
tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1); |
||||
|
||||
tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065) |
||||
+ MULTIPLY((INT32) wsptr[6], - FIX_0_765366865); |
||||
|
||||
tmp10 = tmp0 + tmp2; |
||||
tmp12 = tmp0 - tmp2; |
||||
|
||||
/* Odd part */ |
||||
|
||||
z1 = (INT32) wsptr[7]; |
||||
z2 = (INT32) wsptr[5]; |
||||
z3 = (INT32) wsptr[3]; |
||||
z4 = (INT32) wsptr[1]; |
||||
|
||||
tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */ |
||||
+ MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */ |
||||
+ MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */ |
||||
+ MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */ |
||||
|
||||
tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */ |
||||
+ MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */ |
||||
+ MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */ |
||||
+ MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */ |
||||
|
||||
/* Final output stage */ |
||||
|
||||
outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2, |
||||
CONST_BITS+PASS1_BITS+3+1) |
||||
& RANGE_MASK]; |
||||
outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2, |
||||
CONST_BITS+PASS1_BITS+3+1) |
||||
& RANGE_MASK]; |
||||
outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0, |
||||
CONST_BITS+PASS1_BITS+3+1) |
||||
& RANGE_MASK]; |
||||
outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0, |
||||
CONST_BITS+PASS1_BITS+3+1) |
||||
& RANGE_MASK]; |
||||
|
||||
wsptr += DCTSIZE; /* advance pointer to next row */ |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Perform dequantization and inverse DCT on one block of coefficients, |
||||
* producing a reduced-size 2x2 output block. |
||||
*/ |
||||
|
||||
GLOBAL(void) |
||||
jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
||||
JCOEFPTR coef_block, |
||||
JSAMPARRAY output_buf, JDIMENSION output_col) |
||||
{ |
||||
INT32 tmp0, tmp10, z1; |
||||
JCOEFPTR inptr; |
||||
ISLOW_MULT_TYPE * quantptr; |
||||
int * wsptr; |
||||
JSAMPROW outptr; |
||||
JSAMPLE *range_limit = IDCT_range_limit(cinfo); |
||||
int ctr; |
||||
int workspace[DCTSIZE*2]; /* buffers data between passes */ |
||||
SHIFT_TEMPS |
||||
|
||||
/* Pass 1: process columns from input, store into work array. */ |
||||
|
||||
inptr = coef_block; |
||||
quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; |
||||
wsptr = workspace; |
||||
for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) { |
||||
/* Don't bother to process columns 2,4,6 */ |
||||
if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6) |
||||
continue; |
||||
if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 && |
||||
inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) { |
||||
/* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */ |
||||
int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; |
||||
|
||||
wsptr[DCTSIZE*0] = dcval; |
||||
wsptr[DCTSIZE*1] = dcval; |
||||
|
||||
continue; |
||||
} |
||||
|
||||
/* Even part */ |
||||
|
||||
z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); |
||||
tmp10 = z1 << (CONST_BITS+2); |
||||
|
||||
/* Odd part */ |
||||
|
||||
z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); |
||||
tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */ |
||||
z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); |
||||
tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */ |
||||
z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); |
||||
tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */ |
||||
z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); |
||||
tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */ |
||||
|
||||
/* Final output stage */ |
||||
|
||||
wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2); |
||||
wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2); |
||||
} |
||||
|
||||
/* Pass 2: process 2 rows from work array, store into output array. */ |
||||
|
||||
wsptr = workspace; |
||||
for (ctr = 0; ctr < 2; ctr++) { |
||||
outptr = output_buf[ctr] + output_col; |
||||
/* It's not clear whether a zero row test is worthwhile here ... */ |
||||
|
||||
#ifndef NO_ZERO_ROW_TEST |
||||
if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) { |
||||
/* AC terms all zero */ |
||||
JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3) |
||||
& RANGE_MASK]; |
||||
|
||||
outptr[0] = dcval; |
||||
outptr[1] = dcval; |
||||
|
||||
wsptr += DCTSIZE; /* advance pointer to next row */ |
||||
continue; |
||||
} |
||||
#endif |
||||
|
||||
/* Even part */ |
||||
|
||||
tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2); |
||||
|
||||
/* Odd part */ |
||||
|
||||
tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */ |
||||
+ MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */ |
||||
+ MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */ |
||||
+ MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */ |
||||
|
||||
/* Final output stage */ |
||||
|
||||
outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0, |
||||
CONST_BITS+PASS1_BITS+3+2) |
||||
& RANGE_MASK]; |
||||
outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0, |
||||
CONST_BITS+PASS1_BITS+3+2) |
||||
& RANGE_MASK]; |
||||
|
||||
wsptr += DCTSIZE; /* advance pointer to next row */ |
||||
} |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Perform dequantization and inverse DCT on one block of coefficients, |
||||
* producing a reduced-size 1x1 output block. |
||||
*/ |
||||
|
||||
GLOBAL(void) |
||||
jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
||||
JCOEFPTR coef_block, |
||||
JSAMPARRAY output_buf, JDIMENSION output_col) |
||||
{ |
||||
int dcval; |
||||
ISLOW_MULT_TYPE * quantptr; |
||||
JSAMPLE *range_limit = IDCT_range_limit(cinfo); |
||||
SHIFT_TEMPS |
||||
|
||||
/* We hardly need an inverse DCT routine for this: just take the
|
||||
* average pixel value, which is one-eighth of the DC coefficient. |
||||
*/ |
||||
quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; |
||||
dcval = DEQUANTIZE(coef_block[0], quantptr[0]); |
||||
dcval = (int) DESCALE((INT32) dcval, 3); |
||||
|
||||
output_buf[0][output_col] = range_limit[dcval & RANGE_MASK]; |
||||
} |
||||
|
||||
#endif /* IDCT_SCALING_SUPPORTED */ |
@ -0,0 +1,109 @@ |
||||
/*
|
||||
* jmemnobs.c |
||||
* |
||||
* Copyright (C) 1992-1996, Thomas G. Lane. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file provides a really simple implementation of the system- |
||||
* dependent portion of the JPEG memory manager. This implementation |
||||
* assumes that no backing-store files are needed: all required space |
||||
* can be obtained from malloc(). |
||||
* This is very portable in the sense that it'll compile on almost anything, |
||||
* but you'd better have lots of main memory (or virtual memory) if you want |
||||
* to process big images. |
||||
* Note that the max_memory_to_use option is ignored by this implementation. |
||||
*/ |
||||
|
||||
#define JPEG_INTERNALS |
||||
#include "jinclude.h" |
||||
#include "jpeglib.h" |
||||
#include "jmemsys.h" /* import the system-dependent declarations */ |
||||
|
||||
#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
||||
extern void * malloc JPP((size_t size)); |
||||
extern void free JPP((void *ptr)); |
||||
#endif |
||||
|
||||
|
||||
/*
|
||||
* Memory allocation and freeing are controlled by the regular library |
||||
* routines malloc() and free(). |
||||
*/ |
||||
|
||||
GLOBAL(void *) |
||||
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject) |
||||
{ |
||||
return (void *) malloc(sizeofobject); |
||||
} |
||||
|
||||
GLOBAL(void) |
||||
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject) |
||||
{ |
||||
free(object); |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* "Large" objects are treated the same as "small" ones. |
||||
* NB: although we include FAR keywords in the routine declarations, |
||||
* this file won't actually work in 80x86 small/medium model; at least, |
||||
* you probably won't be able to process useful-size images in only 64KB. |
||||
*/ |
||||
|
||||
GLOBAL(void FAR *) |
||||
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject) |
||||
{ |
||||
return (void FAR *) malloc(sizeofobject); |
||||
} |
||||
|
||||
GLOBAL(void) |
||||
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject) |
||||
{ |
||||
free(object); |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* This routine computes the total memory space available for allocation. |
||||
* Here we always say, "we got all you want bud!" |
||||
*/ |
||||
|
||||
GLOBAL(long) |
||||
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, |
||||
long max_bytes_needed, long already_allocated) |
||||
{ |
||||
return max_bytes_needed; |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* Backing store (temporary file) management. |
||||
* Since jpeg_mem_available always promised the moon, |
||||
* this should never be called and we can just error out. |
||||
*/ |
||||
|
||||
GLOBAL(void) |
||||
jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info, |
||||
long total_bytes_needed) |
||||
{ |
||||
ERREXIT(cinfo, JERR_NO_BACKING_STORE); |
||||
} |
||||
|
||||
|
||||
/*
|
||||
* These routines take care of any system-dependent initialization and |
||||
* cleanup required. Here, there isn't any. |
||||
*/ |
||||
|
||||
GLOBAL(long) |
||||
jpeg_mem_init (j_common_ptr cinfo) |
||||
{ |
||||
return 0; /* just set max_memory_to_use to 0 */ |
||||
} |
||||
|
||||
GLOBAL(void) |
||||
jpeg_mem_term (j_common_ptr cinfo) |
||||
{ |
||||
/* no work */ |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,205 +0,0 @@ |
||||
/*
|
||||
* transupp.h |
||||
* |
||||
* Copyright (C) 1997-2001, Thomas G. Lane. |
||||
* This file is part of the Independent JPEG Group's software. |
||||
* For conditions of distribution and use, see the accompanying README file. |
||||
* |
||||
* This file contains declarations for image transformation routines and |
||||
* other utility code used by the jpegtran sample application. These are |
||||
* NOT part of the core JPEG library. But we keep these routines separate |
||||
* from jpegtran.c to ease the task of maintaining jpegtran-like programs |
||||
* that have other user interfaces. |
||||
* |
||||
* NOTE: all the routines declared here have very specific requirements |
||||
* about when they are to be executed during the reading and writing of the |
||||
* source and destination files. See the comments in transupp.c, or see |
||||
* jpegtran.c for an example of correct usage. |
||||
*/ |
||||
|
||||
/* If you happen not to want the image transform support, disable it here */ |
||||
#ifndef TRANSFORMS_SUPPORTED |
||||
#define TRANSFORMS_SUPPORTED 1 /* 0 disables transform code */ |
||||
#endif |
||||
|
||||
/*
|
||||
* Although rotating and flipping data expressed as DCT coefficients is not |
||||
* hard, there is an asymmetry in the JPEG format specification for images |
||||
* whose dimensions aren't multiples of the iMCU size. The right and bottom |
||||
* image edges are padded out to the next iMCU boundary with junk data; but |
||||
* no padding is possible at the top and left edges. If we were to flip |
||||
* the whole image including the pad data, then pad garbage would become |
||||
* visible at the top and/or left, and real pixels would disappear into the |
||||
* pad margins --- perhaps permanently, since encoders & decoders may not |
||||
* bother to preserve DCT blocks that appear to be completely outside the |
||||
* nominal image area. So, we have to exclude any partial iMCUs from the |
||||
* basic transformation. |
||||
* |
||||
* Transpose is the only transformation that can handle partial iMCUs at the |
||||
* right and bottom edges completely cleanly. flip_h can flip partial iMCUs |
||||
* at the bottom, but leaves any partial iMCUs at the right edge untouched. |
||||
* Similarly flip_v leaves any partial iMCUs at the bottom edge untouched. |
||||
* The other transforms are defined as combinations of these basic transforms |
||||
* and process edge blocks in a way that preserves the equivalence. |
||||
* |
||||
* The "trim" option causes untransformable partial iMCUs to be dropped; |
||||
* this is not strictly lossless, but it usually gives the best-looking |
||||
* result for odd-size images. Note that when this option is active, |
||||
* the expected mathematical equivalences between the transforms may not hold. |
||||
* (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim |
||||
* followed by -rot 180 -trim trims both edges.) |
||||
* |
||||
* We also offer a lossless-crop option, which discards data outside a given |
||||
* image region but losslessly preserves what is inside. Like the rotate and |
||||
* flip transforms, lossless crop is restricted by the JPEG format: the upper |
||||
* left corner of the selected region must fall on an iMCU boundary. If this |
||||
* does not hold for the given crop parameters, we silently move the upper left |
||||
* corner up and/or left to make it so, simultaneously increasing the region |
||||
* dimensions to keep the lower right crop corner unchanged. (Thus, the |
||||
* output image covers at least the requested region, but may cover more.) |
||||
* |
||||
* If both crop and a rotate/flip transform are requested, the crop is applied |
||||
* last --- that is, the crop region is specified in terms of the destination |
||||
* image. |
||||
* |
||||
* We also offer a "force to grayscale" option, which simply discards the |
||||
* chrominance channels of a YCbCr image. This is lossless in the sense that |
||||
* the luminance channel is preserved exactly. It's not the same kind of |
||||
* thing as the rotate/flip transformations, but it's convenient to handle it |
||||
* as part of this package, mainly because the transformation routines have to |
||||
* be aware of the option to know how many components to work on. |
||||
*/ |
||||
|
||||
|
||||
/* Short forms of external names for systems with brain-damaged linkers. */ |
||||
|
||||
#ifdef NEED_SHORT_EXTERNAL_NAMES |
||||
#define jtransform_parse_crop_spec jTrParCrop |
||||
#define jtransform_request_workspace jTrRequest |
||||
#define jtransform_adjust_parameters jTrAdjust |
||||
#define jtransform_execute_transform jTrExec |
||||
#define jtransform_perfect_transform jTrPerfect |
||||
#define jcopy_markers_setup jCMrkSetup |
||||
#define jcopy_markers_execute jCMrkExec |
||||
#endif /* NEED_SHORT_EXTERNAL_NAMES */ |
||||
|
||||
|
||||
/*
|
||||
* Codes for supported types of image transformations. |
||||
*/ |
||||
|
||||
typedef enum { |
||||
JXFORM_NONE, /* no transformation */ |
||||
JXFORM_FLIP_H, /* horizontal flip */ |
||||
JXFORM_FLIP_V, /* vertical flip */ |
||||
JXFORM_TRANSPOSE, /* transpose across UL-to-LR axis */ |
||||
JXFORM_TRANSVERSE, /* transpose across UR-to-LL axis */ |
||||
JXFORM_ROT_90, /* 90-degree clockwise rotation */ |
||||
JXFORM_ROT_180, /* 180-degree rotation */ |
||||
JXFORM_ROT_270 /* 270-degree clockwise (or 90 ccw) */ |
||||
} JXFORM_CODE; |
||||
|
||||
/*
|
||||
* Codes for crop parameters, which can individually be unspecified, |
||||
* positive, or negative. (Negative width or height makes no sense, though.) |
||||
*/ |
||||
|
||||
typedef enum { |
||||
JCROP_UNSET, |
||||
JCROP_POS, |
||||
JCROP_NEG |
||||
} JCROP_CODE; |
||||
|
||||
/*
|
||||
* Transform parameters struct. |
||||
* NB: application must not change any elements of this struct after |
||||
* calling jtransform_request_workspace. |
||||
*/ |
||||
|
||||
typedef struct { |
||||
/* Options: set by caller */ |
||||
JXFORM_CODE transform; /* image transform operator */ |
||||
boolean perfect; /* if TRUE, fail if partial MCUs are requested */ |
||||
boolean trim; /* if TRUE, trim partial MCUs as needed */ |
||||
boolean force_grayscale; /* if TRUE, convert color image to grayscale */ |
||||
boolean crop; /* if TRUE, crop source image */ |
||||
|
||||
/* Crop parameters: application need not set these unless crop is TRUE.
|
||||
* These can be filled in by jtransform_parse_crop_spec(). |
||||
*/ |
||||
JDIMENSION crop_width; /* Width of selected region */ |
||||
JCROP_CODE crop_width_set; |
||||
JDIMENSION crop_height; /* Height of selected region */ |
||||
JCROP_CODE crop_height_set; |
||||
JDIMENSION crop_xoffset; /* X offset of selected region */ |
||||
JCROP_CODE crop_xoffset_set; /* (negative measures from right edge) */ |
||||
JDIMENSION crop_yoffset; /* Y offset of selected region */ |
||||
JCROP_CODE crop_yoffset_set; /* (negative measures from bottom edge) */ |
||||
|
||||
/* Internal workspace: caller should not touch these */ |
||||
int num_components; /* # of components in workspace */ |
||||
jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */ |
||||
JDIMENSION output_width; /* cropped destination dimensions */ |
||||
JDIMENSION output_height; |
||||
JDIMENSION x_crop_offset; /* destination crop offsets measured in iMCUs */ |
||||
JDIMENSION y_crop_offset; |
||||
int max_h_samp_factor; /* destination iMCU size */ |
||||
int max_v_samp_factor; |
||||
} jpeg_transform_info; |
||||
|
||||
|
||||
#if TRANSFORMS_SUPPORTED |
||||
|
||||
/* Parse a crop specification (written in X11 geometry style) */ |
||||
EXTERN(boolean) jtransform_parse_crop_spec |
||||
JPP((jpeg_transform_info *info, const char *spec)); |
||||
/* Request any required workspace */ |
||||
EXTERN(void) jtransform_request_workspace |
||||
JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info)); |
||||
/* Adjust output image parameters */ |
||||
EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters |
||||
JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
||||
jvirt_barray_ptr *src_coef_arrays, |
||||
jpeg_transform_info *info)); |
||||
/* Execute the actual transformation, if any */ |
||||
EXTERN(void) jtransform_execute_transform |
||||
JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
||||
jvirt_barray_ptr *src_coef_arrays, |
||||
jpeg_transform_info *info)); |
||||
/* Determine whether lossless transformation is perfectly
|
||||
* possible for a specified image and transformation. |
||||
*/ |
||||
EXTERN(boolean) jtransform_perfect_transform |
||||
JPP((JDIMENSION image_width, JDIMENSION image_height, |
||||
int MCU_width, int MCU_height, |
||||
JXFORM_CODE transform)); |
||||
|
||||
/* jtransform_execute_transform used to be called
|
||||
* jtransform_execute_transformation, but some compilers complain about |
||||
* routine names that long. This macro is here to avoid breaking any |
||||
* old source code that uses the original name... |
||||
*/ |
||||
#define jtransform_execute_transformation jtransform_execute_transform |
||||
|
||||
#endif /* TRANSFORMS_SUPPORTED */ |
||||
|
||||
|
||||
/*
|
||||
* Support for copying optional markers from source to destination file. |
||||
*/ |
||||
|
||||
typedef enum { |
||||
JCOPYOPT_NONE, /* copy no optional markers */ |
||||
JCOPYOPT_COMMENTS, /* copy only comment (COM) markers */ |
||||
JCOPYOPT_ALL /* copy all optional markers */ |
||||
} JCOPY_OPTION; |
||||
|
||||
#define JCOPYOPT_DEFAULT JCOPYOPT_COMMENTS /* recommended default */ |
||||
|
||||
/* Setup decompression object to save desired markers in memory */ |
||||
EXTERN(void) jcopy_markers_setup |
||||
JPP((j_decompress_ptr srcinfo, JCOPY_OPTION option)); |
||||
/* Copy markers saved in the given source object to the destination object */ |
||||
EXTERN(void) jcopy_markers_execute |
||||
JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo, |
||||
JCOPY_OPTION option)); |
Loading…
Reference in new issue