summaryrefslogtreecommitdiff
path: root/core/src/fxcodec
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/fxcodec')
-rw-r--r--core/src/fxcodec/codec/codec_int.h540
-rw-r--r--core/src/fxcodec/codec/fx_codec.cpp894
-rw-r--r--core/src/fxcodec/codec/fx_codec_fax.cpp1936
-rw-r--r--core/src/fxcodec/codec/fx_codec_flate.cpp1890
-rw-r--r--core/src/fxcodec/codec/fx_codec_icc.cpp2720
-rw-r--r--core/src/fxcodec/codec/fx_codec_jbig.cpp424
-rw-r--r--core/src/fxcodec/codec/fx_codec_jpeg.cpp1470
-rw-r--r--core/src/fxcodec/codec/fx_codec_jpx_opj.cpp1598
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithDecoder.h252
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp210
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h78
-rw-r--r--core/src/fxcodec/jbig2/JBig2_ArithQe.h128
-rw-r--r--core/src/fxcodec/jbig2/JBig2_BitStream.h632
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Context.cpp3624
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Context.h270
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Define.h68
-rw-r--r--core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp8580
-rw-r--r--core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h556
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp110
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h48
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp386
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable.h84
-rw-r--r--core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h502
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Image.cpp3238
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Image.h136
-rw-r--r--core/src/fxcodec/jbig2/JBig2_List.h134
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Module.h64
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Object.cpp144
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Object.h86
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Page.h38
-rw-r--r--core/src/fxcodec/jbig2/JBig2_PatternDict.cpp48
-rw-r--r--core/src/fxcodec/jbig2/JBig2_PatternDict.h44
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Segment.cpp106
-rw-r--r--core/src/fxcodec/jbig2/JBig2_Segment.h136
-rw-r--r--core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp68
-rw-r--r--core/src/fxcodec/jbig2/JBig2_SymbolDict.h52
-rw-r--r--core/src/fxcodec/lcms2/include/fx_lcms2.h20
-rw-r--r--core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h20
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c88
-rw-r--r--core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c1180
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmscam02.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmscgats.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmscnvrt.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmserr.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsgamma.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsgmt.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmshalf.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsintrp.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsio0.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsio1.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmslut.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsmd5.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsmtrx.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsnamed.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsopt.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmspack.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmspcs.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsplugin.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsps2.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmssamp.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmssm.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmstypes.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsvirt.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmswtpnt.c14
-rw-r--r--core/src/fxcodec/lcms2/src/fx_cmsxform.c14
-rw-r--r--core/src/fxcodec/libjpeg/cderror.h264
-rw-r--r--core/src/fxcodec/libjpeg/cdjpeg.h368
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c566
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c328
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c904
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jccolor.c924
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c780
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jchuff.c1830
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcinit.c150
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c592
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c1334
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c1186
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c218
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcparam.c1226
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c1672
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c714
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jcsample.c1044
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jctrans.c782
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c796
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c558
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c1478
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c798
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c544
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c1314
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdinput.c768
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c1030
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c2792
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c1120
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c812
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c1342
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c586
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdsample.c962
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c292
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jerror.c510
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c454
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c572
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c742
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jidctint.c784
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jidctred.c802
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c2246
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c252
-rw-r--r--core/src/fxcodec/libjpeg/fpdfapi_jutils.c364
-rw-r--r--core/src/fxcodec/libjpeg/jchuff.h94
-rw-r--r--core/src/fxcodec/libjpeg/jconfig.h90
-rw-r--r--core/src/fxcodec/libjpeg/jdct.h352
-rw-r--r--core/src/fxcodec/libjpeg/jdhuff.h402
-rw-r--r--core/src/fxcodec/libjpeg/jerror.h582
-rw-r--r--core/src/fxcodec/libjpeg/jinclude.h204
-rw-r--r--core/src/fxcodec/libjpeg/jmemsys.h400
-rw-r--r--core/src/fxcodec/libjpeg/jmorecfg.h752
-rw-r--r--core/src/fxcodec/libjpeg/jpegint.h784
-rw-r--r--core/src/fxcodec/libjpeg/jpeglib.h2330
-rw-r--r--core/src/fxcodec/libjpeg/jversion.h28
-rw-r--r--core/src/fxcodec/libjpeg/transupp.h270
119 files changed, 38020 insertions, 38020 deletions
diff --git a/core/src/fxcodec/codec/codec_int.h b/core/src/fxcodec/codec/codec_int.h
index 571af61470..638d96db85 100644
--- a/core/src/fxcodec/codec/codec_int.h
+++ b/core/src/fxcodec/codec/codec_int.h
@@ -1,270 +1,270 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include <limits.h>
-class CCodec_BasicModule : public ICodec_BasicModule
-{
-public:
- virtual FX_BOOL RunLengthEncode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf,
- FX_DWORD& dest_size);
- virtual FX_BOOL A85Encode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf,
- FX_DWORD& dest_size);
- virtual ICodec_ScanlineDecoder* CreateRunLengthDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int nComps, int bpc);
-};
-struct CCodec_ImageDataCache {
- int m_Width, m_Height;
- int m_nCachedLines;
- FX_BYTE m_Data;
-};
-class CCodec_ScanlineDecoder : public ICodec_ScanlineDecoder
-{
-public:
-
- CCodec_ScanlineDecoder();
-
- virtual ~CCodec_ScanlineDecoder();
-
- virtual FX_DWORD GetSrcOffset()
- {
- return -1;
- }
-
- virtual void DownScale(int dest_width, int dest_height);
-
- FX_LPBYTE GetScanline(int line);
-
- FX_BOOL SkipToScanline(int line, IFX_Pause* pPause);
-
- int GetWidth()
- {
- return m_OutputWidth;
- }
-
- int GetHeight()
- {
- return m_OutputHeight;
- }
-
- int CountComps()
- {
- return m_nComps;
- }
-
- int GetBPC()
- {
- return m_bpc;
- }
-
- FX_BOOL IsColorTransformed()
- {
- return m_bColorTransformed;
- }
-
- void ClearImageData()
- {
- if (m_pDataCache) {
- FX_Free(m_pDataCache);
- }
- m_pDataCache = NULL;
- }
-protected:
-
- int m_OrigWidth;
-
- int m_OrigHeight;
-
- int m_DownScale;
-
- int m_OutputWidth;
-
- int m_OutputHeight;
-
- int m_nComps;
-
- int m_bpc;
-
- int m_Pitch;
-
- FX_BOOL m_bColorTransformed;
-
- FX_LPBYTE ReadNextLine();
-
- virtual FX_BOOL v_Rewind() = 0;
-
- virtual FX_LPBYTE v_GetNextLine() = 0;
-
- virtual void v_DownScale(int dest_width, int dest_height) = 0;
-
- int m_NextLine;
-
- FX_LPBYTE m_pLastScanline;
-
- CCodec_ImageDataCache* m_pDataCache;
-};
-class CCodec_FaxModule : public ICodec_FaxModule
-{
-public:
- virtual ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows);
- FX_BOOL Encode(FX_LPCBYTE src_buf, int width, int height, int pitch, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
-};
-class CCodec_FlateModule : public ICodec_FlateModule
-{
-public:
- virtual ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, int Columns);
- virtual FX_DWORD FlateOrLZWDecode(FX_BOOL bLZW, const FX_BYTE* src_buf, FX_DWORD src_size, FX_BOOL bEarlyChange,
- int predictor, int Colors, int BitsPerComponent, int Columns,
- FX_DWORD estimated_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
- virtual FX_BOOL Encode(const FX_BYTE* src_buf, FX_DWORD src_size,
- int predictor, int Colors, int BitsPerComponent, int Columns,
- FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
- virtual FX_BOOL Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
-};
-class CCodec_JpegModule : public ICodec_JpegModule
-{
-public:
- CCodec_JpegModule() : m_pExtProvider(NULL) {}
- void SetPovider(IFX_JpegProvider* pJP)
- {
- m_pExtProvider = pJP;
- }
- ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size,
- int width, int height, int nComps, FX_BOOL ColorTransform);
- FX_BOOL LoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
- int& num_components, int& bits_per_components, FX_BOOL& color_transform,
- FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length);
- FX_BOOL Encode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length);
- virtual void* Start();
- virtual void Finish(void* pContext);
- virtual void Input(void* pContext, FX_LPCBYTE src_buf, FX_DWORD src_size);
- virtual int ReadHeader(void* pContext, int* width, int* height, int* nComps);
- virtual FX_BOOL StartScanline(void* pContext, int down_scale);
- virtual FX_BOOL ReadScanline(void* pContext, FX_LPBYTE dest_buf);
- virtual FX_DWORD GetAvailInput(void* pContext, FX_LPBYTE* avail_buf_ptr);
-protected:
- IFX_JpegProvider* m_pExtProvider;
-};
-class CCodec_IccModule : public ICodec_IccModule
-{
-public:
- virtual IccCS GetProfileCS(FX_LPCBYTE pProfileData, unsigned int dwProfileSize);
- virtual IccCS GetProfileCS(IFX_FileRead* pFile);
- virtual FX_LPVOID CreateTransform(ICodec_IccModule::IccParam* pInputParam,
- ICodec_IccModule::IccParam* pOutputParam,
- ICodec_IccModule::IccParam* pProofParam = NULL,
- FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL,
- FX_DWORD dwFlag = Icc_FLAGS_DEFAULT,
- FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC,
- FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING
- );
- virtual FX_LPVOID CreateTransform_sRGB(FX_LPCBYTE pProfileData, unsigned int dwProfileSize, int nComponents, int intent = 0,
- FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT);
- virtual FX_LPVOID CreateTransform_CMYK(FX_LPCBYTE pSrcProfileData, unsigned int dwSrcProfileSize, int nSrcComponents,
- FX_LPCBYTE pDstProfileData, unsigned int dwDstProfileSize, int intent = 0,
- FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT,
- FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT
- );
- virtual void DestroyTransform(FX_LPVOID pTransform);
- virtual void Translate(FX_LPVOID pTransform, FX_FLOAT* pSrcValues, FX_FLOAT* pDestValues);
- virtual void TranslateScanline(FX_LPVOID pTransform, FX_LPBYTE pDest, FX_LPCBYTE pSrc, int pixels);
- virtual ~CCodec_IccModule();
-protected:
- CFX_MapByteStringToPtr m_MapTranform;
- CFX_MapByteStringToPtr m_MapProfile;
- typedef enum {
- Icc_CLASS_INPUT = 0,
- Icc_CLASS_OUTPUT,
- Icc_CLASS_PROOF,
- Icc_CLASS_MAX
- } Icc_CLASS;
- FX_LPVOID CreateProfile(ICodec_IccModule::IccParam* pIccParam, Icc_CLASS ic, CFX_BinaryBuf* pTransformKey);
-};
-class CCodec_JpxModule : public ICodec_JpxModule
-{
-public:
- CCodec_JpxModule();
- void* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_BOOL useColorSpace = FALSE);
- void GetImageInfo(FX_LPVOID ctx, FX_DWORD& width, FX_DWORD& height,
- FX_DWORD& codestream_nComps, FX_DWORD& output_nComps);
- FX_BOOL Decode(void* ctx, FX_LPBYTE dest_data, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets);
- void DestroyDecoder(void* ctx);
-};
-#include "../jbig2/JBig2_Context.h"
-class CPDF_Jbig2Interface : public CFX_Object, public CJBig2_Module
-{
-public:
- virtual void *JBig2_Malloc(FX_DWORD dwSize)
- {
- return FX_Alloc(FX_BYTE, dwSize);
- }
- virtual void *JBig2_Malloc2(FX_DWORD num, FX_DWORD dwSize)
- {
- if (dwSize && num >= UINT_MAX / dwSize) {
- return NULL;
- }
- return FX_Alloc(FX_BYTE, num * dwSize);
- }
- virtual void *JBig2_Malloc3(FX_DWORD num, FX_DWORD dwSize, FX_DWORD dwSize2)
- {
- if (dwSize2 && dwSize >= UINT_MAX / dwSize2) {
- return NULL;
- }
- FX_DWORD size = dwSize2 * dwSize;
- if (size && num >= UINT_MAX / size) {
- return NULL;
- }
- return FX_Alloc(FX_BYTE, num * size);
- }
- virtual void *JBig2_Realloc(FX_LPVOID pMem, FX_DWORD dwSize)
- {
- return FX_Realloc(FX_BYTE, pMem, dwSize);
- }
- virtual void JBig2_Free(FX_LPVOID pMem)
- {
- FX_Free(pMem);
- }
-};
-class CCodec_Jbig2Context : public CFX_Object
-{
-public:
- CCodec_Jbig2Context();
- ~CCodec_Jbig2Context() {};
- IFX_FileRead* m_file_ptr;
- FX_DWORD m_width;
- FX_DWORD m_height;
- FX_LPBYTE m_src_buf;
- FX_DWORD m_src_size;
- FX_LPCBYTE m_global_data;
- FX_DWORD m_global_size;
- FX_LPBYTE m_dest_buf;
- FX_DWORD m_dest_pitch;
- FX_BOOL m_bFileReader;
- IFX_Pause* m_pPause;
- CJBig2_Context* m_pContext;
- CJBig2_Image* m_dest_image;
-};
-class CCodec_Jbig2Module : public ICodec_Jbig2Module
-{
-public:
- CCodec_Jbig2Module() {};
- ~CCodec_Jbig2Module();
- FX_BOOL Decode(FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size,
- FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch);
- FX_BOOL Decode(IFX_FileRead* file_ptr,
- FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf);
- void* CreateJbig2Context();
- FXCODEC_STATUS StartDecode(void* pJbig2Context, FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size,
- FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch, IFX_Pause* pPause);
-
- FXCODEC_STATUS StartDecode(void* pJbig2Context, IFX_FileRead* file_ptr,
- FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf, IFX_Pause* pPause);
- FXCODEC_STATUS ContinueDecode(void* pJbig2Context, IFX_Pause* pPause);
- void DestroyJbig2Context(void* pJbig2Context);
- CPDF_Jbig2Interface m_Module;
-private:
-};
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include <limits.h>
+class CCodec_BasicModule : public ICodec_BasicModule
+{
+public:
+ virtual FX_BOOL RunLengthEncode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf,
+ FX_DWORD& dest_size);
+ virtual FX_BOOL A85Encode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf,
+ FX_DWORD& dest_size);
+ virtual ICodec_ScanlineDecoder* CreateRunLengthDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int nComps, int bpc);
+};
+struct CCodec_ImageDataCache {
+ int m_Width, m_Height;
+ int m_nCachedLines;
+ FX_BYTE m_Data;
+};
+class CCodec_ScanlineDecoder : public ICodec_ScanlineDecoder
+{
+public:
+
+ CCodec_ScanlineDecoder();
+
+ virtual ~CCodec_ScanlineDecoder();
+
+ virtual FX_DWORD GetSrcOffset()
+ {
+ return -1;
+ }
+
+ virtual void DownScale(int dest_width, int dest_height);
+
+ FX_LPBYTE GetScanline(int line);
+
+ FX_BOOL SkipToScanline(int line, IFX_Pause* pPause);
+
+ int GetWidth()
+ {
+ return m_OutputWidth;
+ }
+
+ int GetHeight()
+ {
+ return m_OutputHeight;
+ }
+
+ int CountComps()
+ {
+ return m_nComps;
+ }
+
+ int GetBPC()
+ {
+ return m_bpc;
+ }
+
+ FX_BOOL IsColorTransformed()
+ {
+ return m_bColorTransformed;
+ }
+
+ void ClearImageData()
+ {
+ if (m_pDataCache) {
+ FX_Free(m_pDataCache);
+ }
+ m_pDataCache = NULL;
+ }
+protected:
+
+ int m_OrigWidth;
+
+ int m_OrigHeight;
+
+ int m_DownScale;
+
+ int m_OutputWidth;
+
+ int m_OutputHeight;
+
+ int m_nComps;
+
+ int m_bpc;
+
+ int m_Pitch;
+
+ FX_BOOL m_bColorTransformed;
+
+ FX_LPBYTE ReadNextLine();
+
+ virtual FX_BOOL v_Rewind() = 0;
+
+ virtual FX_LPBYTE v_GetNextLine() = 0;
+
+ virtual void v_DownScale(int dest_width, int dest_height) = 0;
+
+ int m_NextLine;
+
+ FX_LPBYTE m_pLastScanline;
+
+ CCodec_ImageDataCache* m_pDataCache;
+};
+class CCodec_FaxModule : public ICodec_FaxModule
+{
+public:
+ virtual ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows);
+ FX_BOOL Encode(FX_LPCBYTE src_buf, int width, int height, int pitch, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
+};
+class CCodec_FlateModule : public ICodec_FlateModule
+{
+public:
+ virtual ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, int Columns);
+ virtual FX_DWORD FlateOrLZWDecode(FX_BOOL bLZW, const FX_BYTE* src_buf, FX_DWORD src_size, FX_BOOL bEarlyChange,
+ int predictor, int Colors, int BitsPerComponent, int Columns,
+ FX_DWORD estimated_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
+ virtual FX_BOOL Encode(const FX_BYTE* src_buf, FX_DWORD src_size,
+ int predictor, int Colors, int BitsPerComponent, int Columns,
+ FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
+ virtual FX_BOOL Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
+};
+class CCodec_JpegModule : public ICodec_JpegModule
+{
+public:
+ CCodec_JpegModule() : m_pExtProvider(NULL) {}
+ void SetPovider(IFX_JpegProvider* pJP)
+ {
+ m_pExtProvider = pJP;
+ }
+ ICodec_ScanlineDecoder* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size,
+ int width, int height, int nComps, FX_BOOL ColorTransform);
+ FX_BOOL LoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
+ int& num_components, int& bits_per_components, FX_BOOL& color_transform,
+ FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length);
+ FX_BOOL Encode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length);
+ virtual void* Start();
+ virtual void Finish(void* pContext);
+ virtual void Input(void* pContext, FX_LPCBYTE src_buf, FX_DWORD src_size);
+ virtual int ReadHeader(void* pContext, int* width, int* height, int* nComps);
+ virtual FX_BOOL StartScanline(void* pContext, int down_scale);
+ virtual FX_BOOL ReadScanline(void* pContext, FX_LPBYTE dest_buf);
+ virtual FX_DWORD GetAvailInput(void* pContext, FX_LPBYTE* avail_buf_ptr);
+protected:
+ IFX_JpegProvider* m_pExtProvider;
+};
+class CCodec_IccModule : public ICodec_IccModule
+{
+public:
+ virtual IccCS GetProfileCS(FX_LPCBYTE pProfileData, unsigned int dwProfileSize);
+ virtual IccCS GetProfileCS(IFX_FileRead* pFile);
+ virtual FX_LPVOID CreateTransform(ICodec_IccModule::IccParam* pInputParam,
+ ICodec_IccModule::IccParam* pOutputParam,
+ ICodec_IccModule::IccParam* pProofParam = NULL,
+ FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL,
+ FX_DWORD dwFlag = Icc_FLAGS_DEFAULT,
+ FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC,
+ FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING
+ );
+ virtual FX_LPVOID CreateTransform_sRGB(FX_LPCBYTE pProfileData, unsigned int dwProfileSize, int nComponents, int intent = 0,
+ FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT);
+ virtual FX_LPVOID CreateTransform_CMYK(FX_LPCBYTE pSrcProfileData, unsigned int dwSrcProfileSize, int nSrcComponents,
+ FX_LPCBYTE pDstProfileData, unsigned int dwDstProfileSize, int intent = 0,
+ FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT,
+ FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT
+ );
+ virtual void DestroyTransform(FX_LPVOID pTransform);
+ virtual void Translate(FX_LPVOID pTransform, FX_FLOAT* pSrcValues, FX_FLOAT* pDestValues);
+ virtual void TranslateScanline(FX_LPVOID pTransform, FX_LPBYTE pDest, FX_LPCBYTE pSrc, int pixels);
+ virtual ~CCodec_IccModule();
+protected:
+ CFX_MapByteStringToPtr m_MapTranform;
+ CFX_MapByteStringToPtr m_MapProfile;
+ typedef enum {
+ Icc_CLASS_INPUT = 0,
+ Icc_CLASS_OUTPUT,
+ Icc_CLASS_PROOF,
+ Icc_CLASS_MAX
+ } Icc_CLASS;
+ FX_LPVOID CreateProfile(ICodec_IccModule::IccParam* pIccParam, Icc_CLASS ic, CFX_BinaryBuf* pTransformKey);
+};
+class CCodec_JpxModule : public ICodec_JpxModule
+{
+public:
+ CCodec_JpxModule();
+ void* CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_BOOL useColorSpace = FALSE);
+ void GetImageInfo(FX_LPVOID ctx, FX_DWORD& width, FX_DWORD& height,
+ FX_DWORD& codestream_nComps, FX_DWORD& output_nComps);
+ FX_BOOL Decode(void* ctx, FX_LPBYTE dest_data, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets);
+ void DestroyDecoder(void* ctx);
+};
+#include "../jbig2/JBig2_Context.h"
+class CPDF_Jbig2Interface : public CFX_Object, public CJBig2_Module
+{
+public:
+ virtual void *JBig2_Malloc(FX_DWORD dwSize)
+ {
+ return FX_Alloc(FX_BYTE, dwSize);
+ }
+ virtual void *JBig2_Malloc2(FX_DWORD num, FX_DWORD dwSize)
+ {
+ if (dwSize && num >= UINT_MAX / dwSize) {
+ return NULL;
+ }
+ return FX_Alloc(FX_BYTE, num * dwSize);
+ }
+ virtual void *JBig2_Malloc3(FX_DWORD num, FX_DWORD dwSize, FX_DWORD dwSize2)
+ {
+ if (dwSize2 && dwSize >= UINT_MAX / dwSize2) {
+ return NULL;
+ }
+ FX_DWORD size = dwSize2 * dwSize;
+ if (size && num >= UINT_MAX / size) {
+ return NULL;
+ }
+ return FX_Alloc(FX_BYTE, num * size);
+ }
+ virtual void *JBig2_Realloc(FX_LPVOID pMem, FX_DWORD dwSize)
+ {
+ return FX_Realloc(FX_BYTE, pMem, dwSize);
+ }
+ virtual void JBig2_Free(FX_LPVOID pMem)
+ {
+ FX_Free(pMem);
+ }
+};
+class CCodec_Jbig2Context : public CFX_Object
+{
+public:
+ CCodec_Jbig2Context();
+ ~CCodec_Jbig2Context() {};
+ IFX_FileRead* m_file_ptr;
+ FX_DWORD m_width;
+ FX_DWORD m_height;
+ FX_LPBYTE m_src_buf;
+ FX_DWORD m_src_size;
+ FX_LPCBYTE m_global_data;
+ FX_DWORD m_global_size;
+ FX_LPBYTE m_dest_buf;
+ FX_DWORD m_dest_pitch;
+ FX_BOOL m_bFileReader;
+ IFX_Pause* m_pPause;
+ CJBig2_Context* m_pContext;
+ CJBig2_Image* m_dest_image;
+};
+class CCodec_Jbig2Module : public ICodec_Jbig2Module
+{
+public:
+ CCodec_Jbig2Module() {};
+ ~CCodec_Jbig2Module();
+ FX_BOOL Decode(FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size,
+ FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch);
+ FX_BOOL Decode(IFX_FileRead* file_ptr,
+ FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf);
+ void* CreateJbig2Context();
+ FXCODEC_STATUS StartDecode(void* pJbig2Context, FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size,
+ FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch, IFX_Pause* pPause);
+
+ FXCODEC_STATUS StartDecode(void* pJbig2Context, IFX_FileRead* file_ptr,
+ FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf, IFX_Pause* pPause);
+ FXCODEC_STATUS ContinueDecode(void* pJbig2Context, IFX_Pause* pPause);
+ void DestroyJbig2Context(void* pJbig2Context);
+ CPDF_Jbig2Interface m_Module;
+private:
+};
diff --git a/core/src/fxcodec/codec/fx_codec.cpp b/core/src/fxcodec/codec/fx_codec.cpp
index 2f586ca94b..c6b3ccee43 100644
--- a/core/src/fxcodec/codec/fx_codec.cpp
+++ b/core/src/fxcodec/codec/fx_codec.cpp
@@ -1,447 +1,447 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../../include/fxcodec/fx_codec.h"
-#include "codec_int.h"
-CCodec_ModuleMgr::CCodec_ModuleMgr()
-{
- m_pBasicModule = FX_NEW CCodec_BasicModule;
- m_pFaxModule = FX_NEW CCodec_FaxModule;
- m_pJpegModule = FX_NEW CCodec_JpegModule;
- m_pJpxModule = FX_NEW CCodec_JpxModule;
- m_pJbig2Module = FX_NEW CCodec_Jbig2Module;
- m_pIccModule = FX_NEW CCodec_IccModule;
- m_pFlateModule = FX_NEW CCodec_FlateModule;
-}
-CCodec_ModuleMgr::~CCodec_ModuleMgr()
-{
- delete m_pBasicModule;
- delete m_pFaxModule;
- delete m_pJpegModule;
- delete m_pFlateModule;
- delete m_pJpxModule;
- delete m_pJbig2Module;
- delete m_pIccModule;
-}
-void CCodec_ModuleMgr::InitJbig2Decoder()
-{
-}
-void CCodec_ModuleMgr::InitJpxDecoder()
-{
-}
-void CCodec_ModuleMgr::InitIccDecoder()
-{
-}
-CCodec_ScanlineDecoder::CCodec_ScanlineDecoder()
-{
- m_NextLine = -1;
- m_pDataCache = NULL;
- m_pLastScanline = NULL;
-}
-CCodec_ScanlineDecoder::~CCodec_ScanlineDecoder()
-{
- if (m_pDataCache) {
- FX_Free(m_pDataCache);
- }
-}
-FX_LPBYTE CCodec_ScanlineDecoder::GetScanline(int line)
-{
- if (m_pDataCache && line < m_pDataCache->m_nCachedLines) {
- return &m_pDataCache->m_Data + line * m_Pitch;
- }
- if (m_NextLine == line + 1) {
- return m_pLastScanline;
- }
- if (m_NextLine < 0 || m_NextLine > line) {
- if (!v_Rewind()) {
- return NULL;
- }
- m_NextLine = 0;
- }
- while (m_NextLine < line) {
- ReadNextLine();
- m_NextLine ++;
- }
- m_pLastScanline = ReadNextLine();
- m_NextLine ++;
- return m_pLastScanline;
-}
-FX_BOOL CCodec_ScanlineDecoder::SkipToScanline(int line, IFX_Pause* pPause)
-{
- if (m_pDataCache && line < m_pDataCache->m_nCachedLines) {
- return FALSE;
- }
- if (m_NextLine == line || m_NextLine == line + 1) {
- return FALSE;
- }
- if (m_NextLine < 0 || m_NextLine > line) {
- v_Rewind();
- m_NextLine = 0;
- }
- m_pLastScanline = NULL;
- while (m_NextLine < line) {
- m_pLastScanline = ReadNextLine();
- m_NextLine ++;
- if (pPause && pPause->NeedToPauseNow()) {
- return TRUE;
- }
- }
- return FALSE;
-}
-FX_LPBYTE CCodec_ScanlineDecoder::ReadNextLine()
-{
- FX_LPBYTE pLine = v_GetNextLine();
- if (pLine == NULL) {
- return NULL;
- }
- if (m_pDataCache && m_NextLine == m_pDataCache->m_nCachedLines) {
- FXSYS_memcpy32(&m_pDataCache->m_Data + m_NextLine * m_Pitch, pLine, m_Pitch);
- m_pDataCache->m_nCachedLines ++;
- }
- return pLine;
-}
-void CCodec_ScanlineDecoder::DownScale(int dest_width, int dest_height)
-{
- if (dest_width < 0) {
- dest_width = -dest_width;
- }
- if (dest_height < 0) {
- dest_height = -dest_height;
- }
- v_DownScale(dest_width, dest_height);
- if (m_pDataCache) {
- if (m_pDataCache->m_Height == m_OutputHeight && m_pDataCache->m_Width == m_OutputWidth) {
- return;
- }
- FX_Free(m_pDataCache);
- m_pDataCache = NULL;
- }
- m_pDataCache = (CCodec_ImageDataCache*)FXMEM_DefaultAlloc(
- sizeof(CCodec_ImageDataCache) + m_Pitch * m_OutputHeight, FXMEM_NONLEAVE);
- if (m_pDataCache == NULL) {
- return;
- }
- m_pDataCache->m_Height = m_OutputHeight;
- m_pDataCache->m_Width = m_OutputWidth;
- m_pDataCache->m_nCachedLines = 0;
-}
-FX_BOOL CCodec_BasicModule::RunLengthEncode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf,
- FX_DWORD& dest_size)
-{
- return FALSE;
-}
-extern "C" double FXstrtod(const char* nptr, char** endptr)
-{
- double ret = 0.0;
- const char* ptr = nptr;
- const char* exp_ptr = NULL;
- int e_number = 0,
- e_signal = 0,
- e_point = 0,
- is_negative = 0;
- int exp_ret = 0, exp_sig = 1,
- fra_ret = 0, fra_count = 0, fra_base = 1;
- if(nptr == NULL) {
- return 0.0;
- }
- for (;; ptr++) {
- if(!e_number && !e_point && (*ptr == '\t' || *ptr == ' ')) {
- continue;
- }
- if(*ptr >= '0' && *ptr <= '9') {
- if(!e_number) {
- e_number = 1;
- }
- if(!e_point) {
- ret *= 10;
- ret += (*ptr - '0');
- } else {
- fra_count++;
- fra_ret *= 10;
- fra_ret += (*ptr - '0');
- }
- continue;
- }
- if(!e_point && *ptr == '.') {
- e_point = 1;
- continue;
- }
- if(!e_number && !e_point && !e_signal) {
- switch(*ptr) {
- case '-':
- is_negative = 1;
- case '+':
- e_signal = 1;
- continue;
- }
- }
- if(e_number && (*ptr == 'e' || *ptr == 'E')) {
-#define EXPONENT_DETECT(ptr) \
- for(;;ptr++){ \
- if(*ptr < '0' || *ptr > '9'){ \
- if(endptr) *endptr = (char*)ptr; \
- break; \
- }else{ \
- exp_ret *= 10; \
- exp_ret += (*ptr - '0'); \
- continue; \
- } \
- }
- exp_ptr = ptr++;
- if(*ptr == '+' || *ptr == '-') {
- exp_sig = (*ptr++ == '+') ? 1 : -1;
- if(*ptr < '0' || *ptr > '9') {
- if(endptr) {
- *endptr = (char*)exp_ptr;
- }
- break;
- }
- EXPONENT_DETECT(ptr);
- } else if(*ptr >= '0' && *ptr <= '9') {
- EXPONENT_DETECT(ptr);
- } else {
- if(endptr) {
- *endptr = (char*)exp_ptr;
- }
- break;
- }
-#undef EXPONENT_DETECT
- break;
- }
- if(ptr != nptr && !e_number) {
- if(endptr) {
- *endptr = (char*)nptr;
- }
- break;
- }
- if(endptr) {
- *endptr = (char*)ptr;
- }
- break;
- }
- while(fra_count--) {
- fra_base *= 10;
- }
- ret += (double)fra_ret / (double)fra_base;
- if(exp_sig == 1) {
- while(exp_ret--) {
- ret *= 10.0;
- }
- } else {
- while(exp_ret--) {
- ret /= 10.0;
- }
- }
- return is_negative ? -ret : ret;
-}
-FX_BOOL CCodec_BasicModule::A85Encode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf,
- FX_DWORD& dest_size)
-{
- return FALSE;
-}
-CCodec_ModuleMgr* CCodec_ModuleMgr::Create()
-{
- return FX_NEW CCodec_ModuleMgr;
-}
-void CCodec_ModuleMgr::Destroy()
-{
- delete this;
-}
-class CCodec_RLScanlineDecoder : public CCodec_ScanlineDecoder
-{
-public:
- CCodec_RLScanlineDecoder();
- virtual ~CCodec_RLScanlineDecoder();
- FX_BOOL Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps, int bpc);
- virtual void v_DownScale(int dest_width, int dest_height) {}
- virtual FX_BOOL v_Rewind();
- virtual FX_LPBYTE v_GetNextLine();
- virtual FX_DWORD GetSrcOffset()
- {
- return m_SrcOffset;
- }
-protected:
- FX_BOOL CheckDestSize();
- void GetNextOperator();
- void UpdateOperator(FX_BYTE used_bytes);
-
- FX_LPBYTE m_pScanline;
- FX_LPCBYTE m_pSrcBuf;
- FX_DWORD m_SrcSize;
- FX_DWORD m_dwLineBytes;
- FX_DWORD m_SrcOffset;
- FX_BOOL m_bEOD;
- FX_BYTE m_Operator;
-};
-CCodec_RLScanlineDecoder::CCodec_RLScanlineDecoder()
- : m_pScanline(NULL)
- , m_pSrcBuf(NULL)
- , m_SrcSize(0)
- , m_dwLineBytes(0)
- , m_SrcOffset(0)
- , m_bEOD(FALSE)
- , m_Operator(0)
-{
-}
-CCodec_RLScanlineDecoder::~CCodec_RLScanlineDecoder()
-{
- if (m_pScanline) {
- FX_Free(m_pScanline);
- }
-}
-FX_BOOL CCodec_RLScanlineDecoder::CheckDestSize()
-{
- FX_DWORD i = 0;
- FX_DWORD old_size = 0;
- FX_DWORD dest_size = 0;
- while (i < m_SrcSize) {
- if (m_pSrcBuf[i] < 128) {
- old_size = dest_size;
- dest_size += m_pSrcBuf[i] + 1;
- if (dest_size < old_size) {
- return FALSE;
- }
- i += m_pSrcBuf[i] + 2;
- } else if (m_pSrcBuf[i] > 128) {
- old_size = dest_size;
- dest_size += 257 - m_pSrcBuf[i];
- if (dest_size < old_size) {
- return FALSE;
- }
- i += 2;
- } else {
- break;
- }
- }
- if (((FX_DWORD)m_OrigWidth * m_nComps * m_bpc * m_OrigHeight + 7) / 8 > dest_size) {
- return FALSE;
- }
- return TRUE;
-}
-FX_BOOL CCodec_RLScanlineDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps, int bpc)
-{
- m_pSrcBuf = src_buf;
- m_SrcSize = src_size;
- m_OutputWidth = m_OrigWidth = width;
- m_OutputHeight = m_OrigHeight = height;
- m_nComps = nComps;
- m_bpc = bpc;
- m_bColorTransformed = FALSE;
- m_DownScale = 1;
- m_Pitch = (width * nComps * bpc + 31) / 32 * 4;
- m_dwLineBytes = (width * nComps * bpc + 7) / 8;
- m_pScanline = FX_Alloc(FX_BYTE, m_Pitch);
- if (m_pScanline == NULL) {
- return FALSE;
- }
- FXSYS_memset32(m_pScanline, 0, m_Pitch);
- return CheckDestSize();
-}
-FX_BOOL CCodec_RLScanlineDecoder::v_Rewind()
-{
- FXSYS_memset32(m_pScanline, 0, m_Pitch);
- m_SrcOffset = 0;
- m_bEOD = FALSE;
- m_Operator = 0;
- return TRUE;
-}
-FX_LPBYTE CCodec_RLScanlineDecoder::v_GetNextLine()
-{
- if (m_SrcOffset == 0) {
- GetNextOperator();
- } else {
- if (m_bEOD) {
- return NULL;
- }
- }
- FXSYS_memset32(m_pScanline, 0, m_Pitch);
- FX_DWORD col_pos = 0;
- FX_BOOL eol = FALSE;
- while (m_SrcOffset < m_SrcSize && !eol) {
- if (m_Operator < 128) {
- FX_DWORD copy_len = m_Operator + 1;
- if (col_pos + copy_len >= m_dwLineBytes) {
- copy_len = m_dwLineBytes - col_pos;
- eol = TRUE;
- }
- if (copy_len >= m_SrcSize - m_SrcOffset) {
- copy_len = m_SrcSize - m_SrcOffset;
- m_bEOD = TRUE;
- }
- FXSYS_memcpy32(m_pScanline + col_pos, m_pSrcBuf + m_SrcOffset, copy_len);
- col_pos += copy_len;
- UpdateOperator((FX_BYTE)copy_len);
- } else if (m_Operator > 128) {
- int fill = 0;
- if (m_SrcOffset - 1 < m_SrcSize - 1) {
- fill = m_pSrcBuf[m_SrcOffset];
- }
- FX_DWORD duplicate_len = 257 - m_Operator;
- if (col_pos + duplicate_len >= m_dwLineBytes) {
- duplicate_len = m_dwLineBytes - col_pos;
- eol = TRUE;
- }
- FXSYS_memset8(m_pScanline + col_pos, fill, duplicate_len);
- col_pos += duplicate_len;
- UpdateOperator((FX_BYTE)duplicate_len);
- } else {
- m_bEOD = TRUE;
- break;
- }
- }
- return m_pScanline;
-}
-void CCodec_RLScanlineDecoder::GetNextOperator()
-{
- if (m_SrcOffset >= m_SrcSize) {
- m_Operator = 128;
- return;
- }
- m_Operator = m_pSrcBuf[m_SrcOffset];
- m_SrcOffset ++;
-}
-void CCodec_RLScanlineDecoder::UpdateOperator(FX_BYTE used_bytes)
-{
- if (used_bytes == 0) {
- return;
- }
- if (m_Operator < 128) {
- FXSYS_assert((FX_DWORD)m_Operator + 1 >= used_bytes);
- if (used_bytes == m_Operator + 1) {
- m_SrcOffset += used_bytes;
- GetNextOperator();
- return;
- }
- m_Operator -= used_bytes;
- m_SrcOffset += used_bytes;
- if (m_SrcOffset >= m_SrcSize) {
- m_Operator = 128;
- }
- return;
- }
- FX_BYTE count = 257 - m_Operator;
- FXSYS_assert((FX_DWORD)count >= used_bytes);
- if (used_bytes == count) {
- m_SrcOffset ++;
- GetNextOperator();
- return;
- }
- count -= used_bytes;
- m_Operator = 257 - count;
-}
-ICodec_ScanlineDecoder* CCodec_BasicModule::CreateRunLengthDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int nComps, int bpc)
-{
- CCodec_RLScanlineDecoder* pRLScanlineDecoder = FX_NEW CCodec_RLScanlineDecoder;
- if (pRLScanlineDecoder == NULL) {
- return NULL;
- }
- if (!pRLScanlineDecoder->Create(src_buf, src_size, width, height, nComps, bpc)) {
- delete pRLScanlineDecoder;
- return NULL;
- }
- return pRLScanlineDecoder;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../../include/fxcodec/fx_codec.h"
+#include "codec_int.h"
+CCodec_ModuleMgr::CCodec_ModuleMgr()
+{
+ m_pBasicModule = FX_NEW CCodec_BasicModule;
+ m_pFaxModule = FX_NEW CCodec_FaxModule;
+ m_pJpegModule = FX_NEW CCodec_JpegModule;
+ m_pJpxModule = FX_NEW CCodec_JpxModule;
+ m_pJbig2Module = FX_NEW CCodec_Jbig2Module;
+ m_pIccModule = FX_NEW CCodec_IccModule;
+ m_pFlateModule = FX_NEW CCodec_FlateModule;
+}
+CCodec_ModuleMgr::~CCodec_ModuleMgr()
+{
+ delete m_pBasicModule;
+ delete m_pFaxModule;
+ delete m_pJpegModule;
+ delete m_pFlateModule;
+ delete m_pJpxModule;
+ delete m_pJbig2Module;
+ delete m_pIccModule;
+}
+void CCodec_ModuleMgr::InitJbig2Decoder()
+{
+}
+void CCodec_ModuleMgr::InitJpxDecoder()
+{
+}
+void CCodec_ModuleMgr::InitIccDecoder()
+{
+}
+CCodec_ScanlineDecoder::CCodec_ScanlineDecoder()
+{
+ m_NextLine = -1;
+ m_pDataCache = NULL;
+ m_pLastScanline = NULL;
+}
+CCodec_ScanlineDecoder::~CCodec_ScanlineDecoder()
+{
+ if (m_pDataCache) {
+ FX_Free(m_pDataCache);
+ }
+}
+FX_LPBYTE CCodec_ScanlineDecoder::GetScanline(int line)
+{
+ if (m_pDataCache && line < m_pDataCache->m_nCachedLines) {
+ return &m_pDataCache->m_Data + line * m_Pitch;
+ }
+ if (m_NextLine == line + 1) {
+ return m_pLastScanline;
+ }
+ if (m_NextLine < 0 || m_NextLine > line) {
+ if (!v_Rewind()) {
+ return NULL;
+ }
+ m_NextLine = 0;
+ }
+ while (m_NextLine < line) {
+ ReadNextLine();
+ m_NextLine ++;
+ }
+ m_pLastScanline = ReadNextLine();
+ m_NextLine ++;
+ return m_pLastScanline;
+}
+FX_BOOL CCodec_ScanlineDecoder::SkipToScanline(int line, IFX_Pause* pPause)
+{
+ if (m_pDataCache && line < m_pDataCache->m_nCachedLines) {
+ return FALSE;
+ }
+ if (m_NextLine == line || m_NextLine == line + 1) {
+ return FALSE;
+ }
+ if (m_NextLine < 0 || m_NextLine > line) {
+ v_Rewind();
+ m_NextLine = 0;
+ }
+ m_pLastScanline = NULL;
+ while (m_NextLine < line) {
+ m_pLastScanline = ReadNextLine();
+ m_NextLine ++;
+ if (pPause && pPause->NeedToPauseNow()) {
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+FX_LPBYTE CCodec_ScanlineDecoder::ReadNextLine()
+{
+ FX_LPBYTE pLine = v_GetNextLine();
+ if (pLine == NULL) {
+ return NULL;
+ }
+ if (m_pDataCache && m_NextLine == m_pDataCache->m_nCachedLines) {
+ FXSYS_memcpy32(&m_pDataCache->m_Data + m_NextLine * m_Pitch, pLine, m_Pitch);
+ m_pDataCache->m_nCachedLines ++;
+ }
+ return pLine;
+}
+void CCodec_ScanlineDecoder::DownScale(int dest_width, int dest_height)
+{
+ if (dest_width < 0) {
+ dest_width = -dest_width;
+ }
+ if (dest_height < 0) {
+ dest_height = -dest_height;
+ }
+ v_DownScale(dest_width, dest_height);
+ if (m_pDataCache) {
+ if (m_pDataCache->m_Height == m_OutputHeight && m_pDataCache->m_Width == m_OutputWidth) {
+ return;
+ }
+ FX_Free(m_pDataCache);
+ m_pDataCache = NULL;
+ }
+ m_pDataCache = (CCodec_ImageDataCache*)FXMEM_DefaultAlloc(
+ sizeof(CCodec_ImageDataCache) + m_Pitch * m_OutputHeight, FXMEM_NONLEAVE);
+ if (m_pDataCache == NULL) {
+ return;
+ }
+ m_pDataCache->m_Height = m_OutputHeight;
+ m_pDataCache->m_Width = m_OutputWidth;
+ m_pDataCache->m_nCachedLines = 0;
+}
+FX_BOOL CCodec_BasicModule::RunLengthEncode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf,
+ FX_DWORD& dest_size)
+{
+ return FALSE;
+}
+extern "C" double FXstrtod(const char* nptr, char** endptr)
+{
+ double ret = 0.0;
+ const char* ptr = nptr;
+ const char* exp_ptr = NULL;
+ int e_number = 0,
+ e_signal = 0,
+ e_point = 0,
+ is_negative = 0;
+ int exp_ret = 0, exp_sig = 1,
+ fra_ret = 0, fra_count = 0, fra_base = 1;
+ if(nptr == NULL) {
+ return 0.0;
+ }
+ for (;; ptr++) {
+ if(!e_number && !e_point && (*ptr == '\t' || *ptr == ' ')) {
+ continue;
+ }
+ if(*ptr >= '0' && *ptr <= '9') {
+ if(!e_number) {
+ e_number = 1;
+ }
+ if(!e_point) {
+ ret *= 10;
+ ret += (*ptr - '0');
+ } else {
+ fra_count++;
+ fra_ret *= 10;
+ fra_ret += (*ptr - '0');
+ }
+ continue;
+ }
+ if(!e_point && *ptr == '.') {
+ e_point = 1;
+ continue;
+ }
+ if(!e_number && !e_point && !e_signal) {
+ switch(*ptr) {
+ case '-':
+ is_negative = 1;
+ case '+':
+ e_signal = 1;
+ continue;
+ }
+ }
+ if(e_number && (*ptr == 'e' || *ptr == 'E')) {
+#define EXPONENT_DETECT(ptr) \
+ for(;;ptr++){ \
+ if(*ptr < '0' || *ptr > '9'){ \
+ if(endptr) *endptr = (char*)ptr; \
+ break; \
+ }else{ \
+ exp_ret *= 10; \
+ exp_ret += (*ptr - '0'); \
+ continue; \
+ } \
+ }
+ exp_ptr = ptr++;
+ if(*ptr == '+' || *ptr == '-') {
+ exp_sig = (*ptr++ == '+') ? 1 : -1;
+ if(*ptr < '0' || *ptr > '9') {
+ if(endptr) {
+ *endptr = (char*)exp_ptr;
+ }
+ break;
+ }
+ EXPONENT_DETECT(ptr);
+ } else if(*ptr >= '0' && *ptr <= '9') {
+ EXPONENT_DETECT(ptr);
+ } else {
+ if(endptr) {
+ *endptr = (char*)exp_ptr;
+ }
+ break;
+ }
+#undef EXPONENT_DETECT
+ break;
+ }
+ if(ptr != nptr && !e_number) {
+ if(endptr) {
+ *endptr = (char*)nptr;
+ }
+ break;
+ }
+ if(endptr) {
+ *endptr = (char*)ptr;
+ }
+ break;
+ }
+ while(fra_count--) {
+ fra_base *= 10;
+ }
+ ret += (double)fra_ret / (double)fra_base;
+ if(exp_sig == 1) {
+ while(exp_ret--) {
+ ret *= 10.0;
+ }
+ } else {
+ while(exp_ret--) {
+ ret /= 10.0;
+ }
+ }
+ return is_negative ? -ret : ret;
+}
+FX_BOOL CCodec_BasicModule::A85Encode(const FX_BYTE* src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf,
+ FX_DWORD& dest_size)
+{
+ return FALSE;
+}
+CCodec_ModuleMgr* CCodec_ModuleMgr::Create()
+{
+ return FX_NEW CCodec_ModuleMgr;
+}
+void CCodec_ModuleMgr::Destroy()
+{
+ delete this;
+}
+class CCodec_RLScanlineDecoder : public CCodec_ScanlineDecoder
+{
+public:
+ CCodec_RLScanlineDecoder();
+ virtual ~CCodec_RLScanlineDecoder();
+ FX_BOOL Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps, int bpc);
+ virtual void v_DownScale(int dest_width, int dest_height) {}
+ virtual FX_BOOL v_Rewind();
+ virtual FX_LPBYTE v_GetNextLine();
+ virtual FX_DWORD GetSrcOffset()
+ {
+ return m_SrcOffset;
+ }
+protected:
+ FX_BOOL CheckDestSize();
+ void GetNextOperator();
+ void UpdateOperator(FX_BYTE used_bytes);
+
+ FX_LPBYTE m_pScanline;
+ FX_LPCBYTE m_pSrcBuf;
+ FX_DWORD m_SrcSize;
+ FX_DWORD m_dwLineBytes;
+ FX_DWORD m_SrcOffset;
+ FX_BOOL m_bEOD;
+ FX_BYTE m_Operator;
+};
+CCodec_RLScanlineDecoder::CCodec_RLScanlineDecoder()
+ : m_pScanline(NULL)
+ , m_pSrcBuf(NULL)
+ , m_SrcSize(0)
+ , m_dwLineBytes(0)
+ , m_SrcOffset(0)
+ , m_bEOD(FALSE)
+ , m_Operator(0)
+{
+}
+CCodec_RLScanlineDecoder::~CCodec_RLScanlineDecoder()
+{
+ if (m_pScanline) {
+ FX_Free(m_pScanline);
+ }
+}
+FX_BOOL CCodec_RLScanlineDecoder::CheckDestSize()
+{
+ FX_DWORD i = 0;
+ FX_DWORD old_size = 0;
+ FX_DWORD dest_size = 0;
+ while (i < m_SrcSize) {
+ if (m_pSrcBuf[i] < 128) {
+ old_size = dest_size;
+ dest_size += m_pSrcBuf[i] + 1;
+ if (dest_size < old_size) {
+ return FALSE;
+ }
+ i += m_pSrcBuf[i] + 2;
+ } else if (m_pSrcBuf[i] > 128) {
+ old_size = dest_size;
+ dest_size += 257 - m_pSrcBuf[i];
+ if (dest_size < old_size) {
+ return FALSE;
+ }
+ i += 2;
+ } else {
+ break;
+ }
+ }
+ if (((FX_DWORD)m_OrigWidth * m_nComps * m_bpc * m_OrigHeight + 7) / 8 > dest_size) {
+ return FALSE;
+ }
+ return TRUE;
+}
+FX_BOOL CCodec_RLScanlineDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps, int bpc)
+{
+ m_pSrcBuf = src_buf;
+ m_SrcSize = src_size;
+ m_OutputWidth = m_OrigWidth = width;
+ m_OutputHeight = m_OrigHeight = height;
+ m_nComps = nComps;
+ m_bpc = bpc;
+ m_bColorTransformed = FALSE;
+ m_DownScale = 1;
+ m_Pitch = (width * nComps * bpc + 31) / 32 * 4;
+ m_dwLineBytes = (width * nComps * bpc + 7) / 8;
+ m_pScanline = FX_Alloc(FX_BYTE, m_Pitch);
+ if (m_pScanline == NULL) {
+ return FALSE;
+ }
+ FXSYS_memset32(m_pScanline, 0, m_Pitch);
+ return CheckDestSize();
+}
+FX_BOOL CCodec_RLScanlineDecoder::v_Rewind()
+{
+ FXSYS_memset32(m_pScanline, 0, m_Pitch);
+ m_SrcOffset = 0;
+ m_bEOD = FALSE;
+ m_Operator = 0;
+ return TRUE;
+}
+FX_LPBYTE CCodec_RLScanlineDecoder::v_GetNextLine()
+{
+ if (m_SrcOffset == 0) {
+ GetNextOperator();
+ } else {
+ if (m_bEOD) {
+ return NULL;
+ }
+ }
+ FXSYS_memset32(m_pScanline, 0, m_Pitch);
+ FX_DWORD col_pos = 0;
+ FX_BOOL eol = FALSE;
+ while (m_SrcOffset < m_SrcSize && !eol) {
+ if (m_Operator < 128) {
+ FX_DWORD copy_len = m_Operator + 1;
+ if (col_pos + copy_len >= m_dwLineBytes) {
+ copy_len = m_dwLineBytes - col_pos;
+ eol = TRUE;
+ }
+ if (copy_len >= m_SrcSize - m_SrcOffset) {
+ copy_len = m_SrcSize - m_SrcOffset;
+ m_bEOD = TRUE;
+ }
+ FXSYS_memcpy32(m_pScanline + col_pos, m_pSrcBuf + m_SrcOffset, copy_len);
+ col_pos += copy_len;
+ UpdateOperator((FX_BYTE)copy_len);
+ } else if (m_Operator > 128) {
+ int fill = 0;
+ if (m_SrcOffset - 1 < m_SrcSize - 1) {
+ fill = m_pSrcBuf[m_SrcOffset];
+ }
+ FX_DWORD duplicate_len = 257 - m_Operator;
+ if (col_pos + duplicate_len >= m_dwLineBytes) {
+ duplicate_len = m_dwLineBytes - col_pos;
+ eol = TRUE;
+ }
+ FXSYS_memset8(m_pScanline + col_pos, fill, duplicate_len);
+ col_pos += duplicate_len;
+ UpdateOperator((FX_BYTE)duplicate_len);
+ } else {
+ m_bEOD = TRUE;
+ break;
+ }
+ }
+ return m_pScanline;
+}
+void CCodec_RLScanlineDecoder::GetNextOperator()
+{
+ if (m_SrcOffset >= m_SrcSize) {
+ m_Operator = 128;
+ return;
+ }
+ m_Operator = m_pSrcBuf[m_SrcOffset];
+ m_SrcOffset ++;
+}
+void CCodec_RLScanlineDecoder::UpdateOperator(FX_BYTE used_bytes)
+{
+ if (used_bytes == 0) {
+ return;
+ }
+ if (m_Operator < 128) {
+ FXSYS_assert((FX_DWORD)m_Operator + 1 >= used_bytes);
+ if (used_bytes == m_Operator + 1) {
+ m_SrcOffset += used_bytes;
+ GetNextOperator();
+ return;
+ }
+ m_Operator -= used_bytes;
+ m_SrcOffset += used_bytes;
+ if (m_SrcOffset >= m_SrcSize) {
+ m_Operator = 128;
+ }
+ return;
+ }
+ FX_BYTE count = 257 - m_Operator;
+ FXSYS_assert((FX_DWORD)count >= used_bytes);
+ if (used_bytes == count) {
+ m_SrcOffset ++;
+ GetNextOperator();
+ return;
+ }
+ count -= used_bytes;
+ m_Operator = 257 - count;
+}
+ICodec_ScanlineDecoder* CCodec_BasicModule::CreateRunLengthDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int nComps, int bpc)
+{
+ CCodec_RLScanlineDecoder* pRLScanlineDecoder = FX_NEW CCodec_RLScanlineDecoder;
+ if (pRLScanlineDecoder == NULL) {
+ return NULL;
+ }
+ if (!pRLScanlineDecoder->Create(src_buf, src_size, width, height, nComps, bpc)) {
+ delete pRLScanlineDecoder;
+ return NULL;
+ }
+ return pRLScanlineDecoder;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_fax.cpp b/core/src/fxcodec/codec/fx_codec_fax.cpp
index 21c7deb076..979df38197 100644
--- a/core/src/fxcodec/codec/fx_codec_fax.cpp
+++ b/core/src/fxcodec/codec/fx_codec_fax.cpp
@@ -1,968 +1,968 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../../include/fxcodec/fx_codec.h"
-#include "codec_int.h"
-extern const FX_BYTE OneLeadPos[256];
-extern const FX_BYTE ZeroLeadPos[256];
-int _FindBit(const FX_BYTE* data_buf, int max_pos, int start_pos, int bit)
-{
- if (start_pos >= max_pos) {
- return max_pos;
- }
- FX_LPCBYTE leading_pos = bit ? OneLeadPos : ZeroLeadPos;
- if (start_pos % 8) {
- FX_BYTE data = data_buf[start_pos / 8];
- if (bit) {
- data &= 0xff >> (start_pos % 8);
- } else {
- data |= 0xff << (8 - start_pos % 8);
- }
- if (leading_pos[data] < 8) {
- return start_pos / 8 * 8 + leading_pos[data];
- }
- start_pos += 7;
- }
- FX_BYTE skip = bit ? 0x00 : 0xff;
- int byte_pos = start_pos / 8;
- int max_byte = (max_pos + 7) / 8;
- while (byte_pos < max_byte) {
- if (data_buf[byte_pos] != skip) {
- break;
- }
- byte_pos ++;
- }
- if (byte_pos == max_byte) {
- return max_pos;
- }
- int pos = leading_pos[data_buf[byte_pos]] + byte_pos * 8;
- if (pos > max_pos) {
- pos = max_pos;
- }
- return pos;
-}
-void _FaxG4FindB1B2(const FX_BYTE* ref_buf, int columns, int a0, FX_BOOL a0color, int& b1, int& b2)
-{
- if (a0color) {
- a0color = 1;
- }
- FX_BYTE first_bit = (a0 < 0) ? 1 : ((ref_buf[a0 / 8] & (1 << (7 - a0 % 8))) != 0);
- b1 = _FindBit(ref_buf, columns, a0 + 1, !first_bit);
- if (b1 >= columns) {
- b1 = b2 = columns;
- return;
- }
- if (first_bit == !a0color) {
- b1 = _FindBit(ref_buf, columns, b1 + 1, first_bit);
- first_bit = !first_bit;
- }
- if (b1 >= columns) {
- b1 = b2 = columns;
- return;
- }
- b2 = _FindBit(ref_buf, columns, b1 + 1, first_bit);
-}
-void _FaxFillBits(FX_LPBYTE dest_buf, int columns, int startpos, int endpos)
-{
- if (startpos < 0) {
- startpos = 0;
- }
- if (endpos < 0) {
- endpos = 0;
- }
- if (endpos >= columns) {
- endpos = columns;
- }
- if (startpos >= endpos) {
- return;
- }
- int first_byte = startpos / 8;
- int last_byte = (endpos - 1) / 8;
- if (first_byte == last_byte) {
- for (int i = startpos % 8; i <= (endpos - 1) % 8; i ++) {
- dest_buf[first_byte] -= 1 << (7 - i);
- }
- return;
- }
- int i;
- for (i = startpos % 8; i < 8; i ++) {
- dest_buf[first_byte] -= 1 << (7 - i);
- }
- for (i = 0; i <= (endpos - 1) % 8; i ++) {
- dest_buf[last_byte] -= 1 << (7 - i);
- }
- if (last_byte > first_byte + 1) {
- FXSYS_memset32(dest_buf + first_byte + 1, 0, last_byte - first_byte - 1);
- }
-}
-#define NEXTBIT src_buf[bitpos/8] & (1 << (7-bitpos%8)); bitpos ++;
-#define ADDBIT(code, bit) code = code << 1; if (bit) code ++;
-#define GETBIT(bitpos) src_buf[bitpos/8] & (1 << (7-bitpos%8))
-static const FX_BYTE FaxBlackRunIns[] = {
- 0,
- 2,
- 0x02, 3, 0,
- 0x03, 2, 0,
- 2,
- 0x02, 1, 0,
- 0x03, 4, 0,
- 2,
- 0x02, 6, 0,
- 0x03, 5, 0,
- 1,
- 0x03, 7, 0,
- 2,
- 0x04, 9, 0,
- 0x05, 8, 0,
- 3,
- 0x04, 10, 0,
- 0x05, 11, 0,
- 0x07, 12, 0,
- 2,
- 0x04, 13, 0,
- 0x07, 14, 0,
- 1,
- 0x18, 15, 0,
- 5,
- 0x08, 18, 0,
- 0x0f, 64, 0,
- 0x17, 16, 0,
- 0x18, 17, 0,
- 0x37, 0, 0,
- 10,
- 0x08, 0x00, 0x07,
- 0x0c, 0x40, 0x07,
- 0x0d, 0x80, 0x07,
- 0x17, 24, 0,
- 0x18, 25, 0,
- 0x28, 23, 0,
- 0x37, 22, 0,
- 0x67, 19, 0,
- 0x68, 20, 0,
- 0x6c, 21, 0,
- 54,
- 0x12, 1984 % 256, 1984 / 256,
- 0x13, 2048 % 256, 2048 / 256,
- 0x14, 2112 % 256, 2112 / 256,
- 0x15, 2176 % 256, 2176 / 256,
- 0x16, 2240 % 256, 2240 / 256,
- 0x17, 2304 % 256, 2304 / 256,
- 0x1c, 2368 % 256, 2368 / 256,
- 0x1d, 2432 % 256, 2432 / 256,
- 0x1e, 2496 % 256, 2496 / 256,
- 0x1f, 2560 % 256, 2560 / 256,
- 0x24, 52, 0,
- 0x27, 55, 0,
- 0x28, 56, 0,
- 0x2b, 59, 0,
- 0x2c, 60, 0,
- 0x33, 320 % 256, 320 / 256,
- 0x34, 384 % 256, 384 / 256,
- 0x35, 448 % 256, 448 / 256,
- 0x37, 53, 0,
- 0x38, 54, 0,
- 0x52, 50, 0,
- 0x53, 51, 0,
- 0x54, 44, 0,
- 0x55, 45, 0,
- 0x56, 46, 0,
- 0x57, 47, 0,
- 0x58, 57, 0,
- 0x59, 58, 0,
- 0x5a, 61, 0,
- 0x5b, 256 % 256, 256 / 256,
- 0x64, 48, 0,
- 0x65, 49, 0,
- 0x66, 62, 0,
- 0x67, 63, 0,
- 0x68, 30, 0,
- 0x69, 31, 0,
- 0x6a, 32, 0,
- 0x6b, 33, 0,
- 0x6c, 40, 0,
- 0x6d, 41, 0,
- 0xc8, 128, 0,
- 0xc9, 192, 0,
- 0xca, 26, 0,
- 0xcb, 27, 0,
- 0xcc, 28, 0,
- 0xcd, 29, 0,
- 0xd2, 34, 0,
- 0xd3, 35, 0,
- 0xd4, 36, 0,
- 0xd5, 37, 0,
- 0xd6, 38, 0,
- 0xd7, 39, 0,
- 0xda, 42, 0,
- 0xdb, 43, 0,
- 20,
- 0x4a, 640 % 256, 640 / 256,
- 0x4b, 704 % 256, 704 / 256,
- 0x4c, 768 % 256, 768 / 256,
- 0x4d, 832 % 256, 832 / 256,
- 0x52, 1280 % 256, 1280 / 256,
- 0x53, 1344 % 256, 1344 / 256,
- 0x54, 1408 % 256, 1408 / 256,
- 0x55, 1472 % 256, 1472 / 256,
- 0x5a, 1536 % 256, 1536 / 256,
- 0x5b, 1600 % 256, 1600 / 256,
- 0x64, 1664 % 256, 1664 / 256,
- 0x65, 1728 % 256, 1728 / 256,
- 0x6c, 512 % 256, 512 / 256,
- 0x6d, 576 % 256, 576 / 256,
- 0x72, 896 % 256, 896 / 256,
- 0x73, 960 % 256, 960 / 256,
- 0x74, 1024 % 256, 1024 / 256,
- 0x75, 1088 % 256, 1088 / 256,
- 0x76, 1152 % 256, 1152 / 256,
- 0x77, 1216 % 256, 1216 / 256,
- 0xff
-};
-static const FX_BYTE FaxWhiteRunIns[] = {
- 0,
- 0,
- 0,
- 6,
- 0x07, 2, 0,
- 0x08, 3, 0,
- 0x0B, 4, 0,
- 0x0C, 5, 0,
- 0x0E, 6, 0,
- 0x0F, 7, 0,
- 6,
- 0x07, 10, 0,
- 0x08, 11, 0,
- 0x12, 128, 0,
- 0x13, 8, 0,
- 0x14, 9, 0,
- 0x1b, 64, 0,
- 9,
- 0x03, 13, 0,
- 0x07, 1, 0,
- 0x08, 12, 0,
- 0x17, 192, 0,
- 0x18, 1664 % 256, 1664 / 256,
- 0x2a, 16, 0,
- 0x2B, 17, 0,
- 0x34, 14, 0,
- 0x35, 15, 0,
- 12,
- 0x03, 22, 0,
- 0x04, 23, 0,
- 0x08, 20, 0,
- 0x0c, 19, 0,
- 0x13, 26, 0,
- 0x17, 21, 0,
- 0x18, 28, 0,
- 0x24, 27, 0,
- 0x27, 18, 0,
- 0x28, 24, 0,
- 0x2B, 25, 0,
- 0x37, 256 % 256, 256 / 256,
- 42,
- 0x02, 29, 0,
- 0x03, 30, 0,
- 0x04, 45, 0,
- 0x05, 46, 0,
- 0x0a, 47, 0,
- 0x0b, 48, 0,
- 0x12, 33, 0,
- 0x13, 34, 0,
- 0x14, 35, 0,
- 0x15, 36, 0,
- 0x16, 37, 0,
- 0x17, 38, 0,
- 0x1a, 31, 0,
- 0x1b, 32, 0,
- 0x24, 53, 0,
- 0x25, 54, 0,
- 0x28, 39, 0,
- 0x29, 40, 0,
- 0x2a, 41, 0,
- 0x2b, 42, 0,
- 0x2c, 43, 0,
- 0x2d, 44, 0,
- 0x32, 61, 0,
- 0x33, 62, 0,
- 0x34, 63, 0,
- 0x35, 0, 0,
- 0x36, 320 % 256, 320 / 256,
- 0x37, 384 % 256, 384 / 256,
- 0x4a, 59, 0,
- 0x4b, 60, 0,
- 0x52, 49, 0,
- 0x53, 50, 0,
- 0x54, 51, 0,
- 0x55, 52, 0,
- 0x58, 55, 0,
- 0x59, 56, 0,
- 0x5a, 57, 0,
- 0x5b, 58, 0,
- 0x64, 448 % 256, 448 / 256,
- 0x65, 512 % 256, 512 / 256,
- 0x67, 640 % 256, 640 / 256,
- 0x68, 576 % 256, 576 / 256,
- 16,
- 0x98, 1472 % 256, 1472 / 256,
- 0x99, 1536 % 256, 1536 / 256,
- 0x9a, 1600 % 256, 1600 / 256,
- 0x9b, 1728 % 256, 1728 / 256,
- 0xcc, 704 % 256, 704 / 256,
- 0xcd, 768 % 256, 768 / 256,
- 0xd2, 832 % 256, 832 / 256,
- 0xd3, 896 % 256, 896 / 256,
- 0xd4, 960 % 256, 960 / 256,
- 0xd5, 1024 % 256, 1024 / 256,
- 0xd6, 1088 % 256, 1088 / 256,
- 0xd7, 1152 % 256, 1152 / 256,
- 0xd8, 1216 % 256, 1216 / 256,
- 0xd9, 1280 % 256, 1280 / 256,
- 0xda, 1344 % 256, 1344 / 256,
- 0xdb, 1408 % 256, 1408 / 256,
- 0,
- 3,
- 0x08, 1792 % 256, 1792 / 256,
- 0x0c, 1856 % 256, 1856 / 256,
- 0x0d, 1920 % 256, 1920 / 256,
- 10,
- 0x12, 1984 % 256, 1984 / 256,
- 0x13, 2048 % 256, 2048 / 256,
- 0x14, 2112 % 256, 2112 / 256,
- 0x15, 2176 % 256, 2176 / 256,
- 0x16, 2240 % 256, 2240 / 256,
- 0x17, 2304 % 256, 2304 / 256,
- 0x1c, 2368 % 256, 2368 / 256,
- 0x1d, 2432 % 256, 2432 / 256,
- 0x1e, 2496 % 256, 2496 / 256,
- 0x1f, 2560 % 256, 2560 / 256,
- 0xff,
-};
-int _FaxGetRun(FX_LPCBYTE ins_array, const FX_BYTE* src_buf, int& bitpos, int bitsize)
-{
- FX_DWORD code = 0;
- int ins_off = 0;
- while (1) {
- FX_BYTE ins = ins_array[ins_off++];
- if (ins == 0xff) {
- return -1;
- }
- if (bitpos >= bitsize) {
- return -1;
- }
- code <<= 1;
- if (src_buf[bitpos / 8] & (1 << (7 - bitpos % 8))) {
- code ++;
- }
- bitpos ++;
- int next_off = ins_off + ins * 3;
- for (; ins_off < next_off; ins_off += 3) {
- if (ins_array[ins_off] == code) {
- return ins_array[ins_off + 1] + ins_array[ins_off + 2] * 256;
- }
- }
- }
-}
-FX_BOOL _FaxG4GetRow(const FX_BYTE* src_buf, int bitsize, int& bitpos, FX_LPBYTE dest_buf, const FX_BYTE* ref_buf, int columns)
-{
- int a0 = -1, a0color = 1;
- while (1) {
- if (bitpos >= bitsize) {
- return FALSE;
- }
- int a1, a2, b1, b2;
- _FaxG4FindB1B2(ref_buf, columns, a0, a0color, b1, b2);
- FX_BOOL bit = NEXTBIT;
- int v_delta = 0;
- if (bit) {
- } else {
- if (bitpos >= bitsize) {
- return FALSE;
- }
- FX_BOOL bit1 = NEXTBIT;
- if (bitpos >= bitsize) {
- return FALSE;
- }
- FX_BOOL bit2 = NEXTBIT;
- if (bit1 && bit2) {
- v_delta = 1;
- } else if (bit1) {
- v_delta = -1;
- } else if (bit2) {
- int run_len1 = 0;
- while (1) {
- int run = _FaxGetRun(a0color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, bitpos, bitsize);
- run_len1 += run;
- if (run < 64) {
- break;
- }
- }
- if (a0 < 0) {
- run_len1 ++;
- }
- a1 = a0 + run_len1;
- if (!a0color) {
- _FaxFillBits(dest_buf, columns, a0, a1);
- }
- int run_len2 = 0;
- while (1) {
- int run = _FaxGetRun(a0color ? FaxBlackRunIns : FaxWhiteRunIns, src_buf, bitpos, bitsize);
- run_len2 += run;
- if (run < 64) {
- break;
- }
- }
- a2 = a1 + run_len2;
- if (a0color) {
- _FaxFillBits(dest_buf, columns, a1, a2);
- }
- a0 = a2;
- if (a0 < columns) {
- continue;
- }
- return TRUE;
- } else {
- if (bitpos >= bitsize) {
- return FALSE;
- }
- bit = NEXTBIT;
- if (bit) {
- if (!a0color) {
- _FaxFillBits(dest_buf, columns, a0, b2);
- }
- if (b2 >= columns) {
- return TRUE;
- }
- a0 = b2;
- continue;
- } else {
- if (bitpos >= bitsize) {
- return FALSE;
- }
- FX_BOOL bit1 = NEXTBIT;
- if (bitpos >= bitsize) {
- return FALSE;
- }
- FX_BOOL bit2 = NEXTBIT;
- if (bit1 && bit2) {
- v_delta = 2;
- } else if (bit1) {
- v_delta = -2;
- } else if (bit2) {
- if (bitpos >= bitsize) {
- return FALSE;
- }
- bit = NEXTBIT;
- if (bit) {
- v_delta = 3;
- } else {
- v_delta = -3;
- }
- } else {
- if (bitpos >= bitsize) {
- return FALSE;
- }
- bit = NEXTBIT;
- if (bit) {
- bitpos += 3;
- continue;
- } else {
- bitpos += 5;
- return TRUE;
- }
- }
- }
- }
- }
- a1 = b1 + v_delta;
- if (!a0color) {
- _FaxFillBits(dest_buf, columns, a0, a1);
- }
- if (a1 >= columns) {
- return TRUE;
- }
- a0 = a1;
- a0color = !a0color;
- }
-}
-FX_BOOL _FaxSkipEOL(const FX_BYTE* src_buf, int bitsize, int& bitpos)
-{
- int startbit = bitpos;
- while (bitpos < bitsize) {
- int bit = NEXTBIT;
- if (bit) {
- if (bitpos - startbit <= 11) {
- bitpos = startbit;
- }
- return TRUE;
- }
- }
- return FALSE;
-}
-FX_BOOL _FaxGet1DLine(const FX_BYTE* src_buf, int bitsize, int& bitpos, FX_LPBYTE dest_buf, int columns)
-{
- int color = TRUE;
- int startpos = 0;
- while (1) {
- if (bitpos >= bitsize) {
- return FALSE;
- }
- int run_len = 0;
- while (1) {
- int run = _FaxGetRun(color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, bitpos, bitsize);
- if (run < 0) {
- while (bitpos < bitsize) {
- int bit = NEXTBIT;
- if (bit) {
- return TRUE;
- }
- }
- return FALSE;
- }
- run_len += run;
- if (run < 64) {
- break;
- }
- }
- if (!color) {
- _FaxFillBits(dest_buf, columns, startpos, startpos + run_len);
- }
- startpos += run_len;
- if (startpos >= columns) {
- break;
- }
- color = !color;
- }
- return TRUE;
-}
-class CCodec_FaxDecoder : public CCodec_ScanlineDecoder
-{
-public:
- CCodec_FaxDecoder();
- virtual ~CCodec_FaxDecoder();
- FX_BOOL Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows);
- virtual void v_DownScale(int dest_width, int dest_height) {}
- virtual FX_BOOL v_Rewind();
- virtual FX_LPBYTE v_GetNextLine();
- virtual FX_DWORD GetSrcOffset();
- int m_Encoding, m_bEndOfLine, m_bByteAlign, m_bBlack;
- int bitpos;
- FX_LPCBYTE m_pSrcBuf;
- FX_DWORD m_SrcSize;
- FX_LPBYTE m_pScanlineBuf, m_pRefBuf;
-};
-CCodec_FaxDecoder::CCodec_FaxDecoder()
-{
- m_pScanlineBuf = NULL;
- m_pRefBuf = NULL;
-}
-CCodec_FaxDecoder::~CCodec_FaxDecoder()
-{
- if (m_pScanlineBuf) {
- FX_Free(m_pScanlineBuf);
- }
- if (m_pRefBuf) {
- FX_Free(m_pRefBuf);
- }
-}
-FX_BOOL CCodec_FaxDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows)
-{
- m_Encoding = K;
- m_bEndOfLine = EndOfLine;
- m_bByteAlign = EncodedByteAlign;
- m_bBlack = BlackIs1;
- m_OrigWidth = Columns;
- m_OrigHeight = Rows;
- if (m_OrigWidth == 0) {
- m_OrigWidth = width;
- }
- if (m_OrigHeight == 0) {
- m_OrigHeight = height;
- }
- m_Pitch = (m_OrigWidth + 31) / 32 * 4;
- m_OutputWidth = m_OrigWidth;
- m_OutputHeight = m_OrigHeight;
- m_pScanlineBuf = FX_Alloc(FX_BYTE, m_Pitch);
- if (m_pScanlineBuf == NULL) {
- return FALSE;
- }
- m_pRefBuf = FX_Alloc(FX_BYTE, m_Pitch);
- if (m_pRefBuf == NULL) {
- return FALSE;
- }
- m_pSrcBuf = src_buf;
- m_SrcSize = src_size;
- m_nComps = 1;
- m_bpc = 1;
- m_bColorTransformed = FALSE;
- return TRUE;
-}
-FX_BOOL CCodec_FaxDecoder::v_Rewind()
-{
- FXSYS_memset8(m_pRefBuf, 0xff, m_Pitch);
- bitpos = 0;
- return TRUE;
-}
-FX_LPBYTE CCodec_FaxDecoder::v_GetNextLine()
-{
- int bitsize = m_SrcSize * 8;
- _FaxSkipEOL(m_pSrcBuf, bitsize, bitpos);
- if (bitpos >= bitsize) {
- return NULL;
- }
- FXSYS_memset8(m_pScanlineBuf, 0xff, m_Pitch);
- if (m_Encoding < 0) {
- _FaxG4GetRow(m_pSrcBuf, bitsize, bitpos, m_pScanlineBuf, m_pRefBuf, m_OrigWidth);
- FXSYS_memcpy32(m_pRefBuf, m_pScanlineBuf, m_Pitch);
- } else if (m_Encoding == 0) {
- _FaxGet1DLine(m_pSrcBuf, bitsize, bitpos, m_pScanlineBuf, m_OrigWidth);
- } else {
- FX_BOOL bNext1D = m_pSrcBuf[bitpos / 8] & (1 << (7 - bitpos % 8));
- bitpos ++;
- if (bNext1D) {
- _FaxGet1DLine(m_pSrcBuf, bitsize, bitpos, m_pScanlineBuf, m_OrigWidth);
- } else {
- _FaxG4GetRow(m_pSrcBuf, bitsize, bitpos, m_pScanlineBuf, m_pRefBuf, m_OrigWidth);
- }
- FXSYS_memcpy32(m_pRefBuf, m_pScanlineBuf, m_Pitch);
- }
- if (m_bEndOfLine) {
- _FaxSkipEOL(m_pSrcBuf, bitsize, bitpos);
- }
- if (m_bByteAlign && bitpos < bitsize) {
- int bitpos0 = bitpos;
- int bitpos1 = (bitpos + 7) / 8 * 8;
- while (m_bByteAlign && bitpos0 < bitpos1) {
- int bit = m_pSrcBuf[bitpos0 / 8] & (1 << (7 - bitpos0 % 8));
- if (bit != 0) {
- m_bByteAlign = FALSE;
- } else {
- bitpos0 ++;
- }
- }
- if (m_bByteAlign) {
- bitpos = bitpos1;
- }
- }
- if (m_bBlack) {
- for (int i = 0; i < m_Pitch; i ++) {
- m_pScanlineBuf[i] = ~m_pScanlineBuf[i];
- }
- }
- return m_pScanlineBuf;
-}
-FX_DWORD CCodec_FaxDecoder::GetSrcOffset()
-{
- FX_DWORD ret = (bitpos + 7) / 8;
- if (ret > m_SrcSize) {
- ret = m_SrcSize;
- }
- return ret;
-}
-extern "C" {
- void _FaxG4Decode(void*, FX_LPCBYTE src_buf, FX_DWORD src_size, int* pbitpos, FX_LPBYTE dest_buf, int width, int height, int pitch)
- {
- if (pitch == 0) {
- pitch = (width + 7) / 8;
- }
- FX_LPBYTE ref_buf = FX_Alloc(FX_BYTE, pitch);
- if (ref_buf == NULL) {
- return;
- }
- FXSYS_memset8(ref_buf, 0xff, pitch);
- int bitpos = *pbitpos;
- for (int iRow = 0; iRow < height; iRow ++) {
- FX_LPBYTE line_buf = dest_buf + iRow * pitch;
- FXSYS_memset8(line_buf, 0xff, pitch);
- _FaxG4GetRow(src_buf, src_size << 3, bitpos, line_buf, ref_buf, width);
- FXSYS_memcpy32(ref_buf, line_buf, pitch);
- }
- FX_Free(ref_buf);
- *pbitpos = bitpos;
- }
-};
-static const FX_BYTE BlackRunTerminator[128] = {
- 0x37, 10, 0x02, 3, 0x03, 2, 0x02, 2, 0x03, 3, 0x03, 4, 0x02, 4, 0x03, 5,
- 0x05, 6, 0x04, 6, 0x04, 7, 0x05, 7, 0x07, 7, 0x04, 8, 0x07, 8, 0x18, 9,
- 0x17, 10, 0x18, 10, 0x08, 10, 0x67, 11, 0x68, 11, 0x6c, 11, 0x37, 11, 0x28, 11,
- 0x17, 11, 0x18, 11, 0xca, 12, 0xcb, 12, 0xcc, 12, 0xcd, 12, 0x68, 12, 0x69, 12,
- 0x6a, 12, 0x6b, 12, 0xd2, 12, 0xd3, 12, 0xd4, 12, 0xd5, 12, 0xd6, 12, 0xd7, 12,
- 0x6c, 12, 0x6d, 12, 0xda, 12, 0xdb, 12, 0x54, 12, 0x55, 12, 0x56, 12, 0x57, 12,
- 0x64, 12, 0x65, 12, 0x52, 12, 0x53, 12, 0x24, 12, 0x37, 12, 0x38, 12, 0x27, 12,
- 0x28, 12, 0x58, 12, 0x59, 12, 0x2b, 12, 0x2c, 12, 0x5a, 12, 0x66, 12, 0x67, 12,
-};
-static const FX_BYTE BlackRunMarkup[80] = {
- 0x0f, 10, 0xc8, 12, 0xc9, 12, 0x5b, 12, 0x33, 12, 0x34, 12, 0x35, 12, 0x6c, 13,
- 0x6d, 13, 0x4a, 13, 0x4b, 13, 0x4c, 13, 0x4d, 13, 0x72, 13, 0x73, 13, 0x74, 13,
- 0x75, 13, 0x76, 13, 0x77, 13, 0x52, 13, 0x53, 13, 0x54, 13, 0x55, 13, 0x5a, 13,
- 0x5b, 13, 0x64, 13, 0x65, 13, 0x08, 11, 0x0c, 11, 0x0d, 11, 0x12, 12, 0x13, 12,
- 0x14, 12, 0x15, 12, 0x16, 12, 0x17, 12, 0x1c, 12, 0x1d, 12, 0x1e, 12, 0x1f, 12,
-};
-static const FX_BYTE WhiteRunTerminator[128] = {
- 0x35, 8,
- 0x07, 6,
- 0x07, 4,
- 0x08, 4,
- 0x0B, 4,
- 0x0C, 4,
- 0x0E, 4,
- 0x0F, 4,
- 0x13, 5,
- 0x14, 5,
- 0x07, 5,
- 0x08, 5,
- 0x08, 6,
- 0x03, 6,
- 0x34, 6,
- 0x35, 6,
- 0x2a, 6,
- 0x2B, 6,
- 0x27, 7,
- 0x0c, 7,
- 0x08, 7,
- 0x17, 7,
- 0x03, 7,
- 0x04, 7,
- 0x28, 7,
- 0x2B, 7,
- 0x13, 7,
- 0x24, 7,
- 0x18, 7,
- 0x02, 8,
- 0x03, 8,
- 0x1a, 8,
- 0x1b, 8,
- 0x12, 8,
- 0x13, 8,
- 0x14, 8,
- 0x15, 8,
- 0x16, 8,
- 0x17, 8,
- 0x28, 8,
- 0x29, 8,
- 0x2a, 8,
- 0x2b, 8,
- 0x2c, 8,
- 0x2d, 8,
- 0x04, 8,
- 0x05, 8,
- 0x0a, 8,
- 0x0b, 8,
- 0x52, 8,
- 0x53, 8,
- 0x54, 8,
- 0x55, 8,
- 0x24, 8,
- 0x25, 8,
- 0x58, 8,
- 0x59, 8,
- 0x5a, 8,
- 0x5b, 8,
- 0x4a, 8,
- 0x4b, 8,
- 0x32, 8,
- 0x33, 8,
- 0x34, 8,
-};
-static const FX_BYTE WhiteRunMarkup[80] = {
- 0x1b, 5,
- 0x12, 5,
- 0x17, 6,
- 0x37, 7,
- 0x36, 8,
- 0x37, 8,
- 0x64, 8,
- 0x65, 8,
- 0x68, 8,
- 0x67, 8,
- 0xcc, 9,
- 0xcd, 9,
- 0xd2, 9,
- 0xd3, 9,
- 0xd4, 9,
- 0xd5, 9,
- 0xd6, 9,
- 0xd7, 9,
- 0xd8, 9,
- 0xd9, 9,
- 0xda, 9,
- 0xdb, 9,
- 0x98, 9,
- 0x99, 9,
- 0x9a, 9,
- 0x18, 6,
- 0x9b, 9,
- 0x08, 11,
- 0x0c, 11,
- 0x0d, 11,
- 0x12, 12,
- 0x13, 12,
- 0x14, 12,
- 0x15, 12,
- 0x16, 12,
- 0x17, 12,
- 0x1c, 12,
- 0x1d, 12,
- 0x1e, 12,
- 0x1f, 12,
-};
-static void _AddBitStream(FX_LPBYTE dest_buf, int& dest_bitpos, int data, int bitlen)
-{
- for (int i = bitlen - 1; i >= 0; i --) {
- if (data & (1 << i)) {
- dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
- }
- dest_bitpos ++;
- }
-}
-static void _FaxEncodeRun(FX_LPBYTE dest_buf, int& dest_bitpos, int run, FX_BOOL bWhite)
-{
- while (run >= 2560) {
- _AddBitStream(dest_buf, dest_bitpos, 0x1f, 12);
- run -= 2560;
- }
- if (run >= 64) {
- int markup = run - run % 64;
- FX_LPCBYTE p = bWhite ? WhiteRunMarkup : BlackRunMarkup;
- p += (markup / 64 - 1) * 2;
- _AddBitStream(dest_buf, dest_bitpos, *p, p[1]);
- }
- run %= 64;
- FX_LPCBYTE p = bWhite ? WhiteRunTerminator : BlackRunTerminator;
- p += run * 2;
- _AddBitStream(dest_buf, dest_bitpos, *p, p[1]);
-}
-static void _FaxEncode2DLine(FX_LPBYTE dest_buf, int& dest_bitpos, FX_LPCBYTE src_buf, FX_LPCBYTE ref_buf, int cols)
-{
- int a0 = -1, a0color = 1;
- while (1) {
- int a1 = _FindBit(src_buf, cols, a0 + 1, 1 - a0color);
- int b1, b2;
- _FaxG4FindB1B2(ref_buf, cols, a0, a0color, b1, b2);
- if (b2 < a1) {
- dest_bitpos += 3;
- dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
- dest_bitpos ++;
- a0 = b2;
- } else if (a1 - b1 <= 3 && b1 - a1 <= 3) {
- int delta = a1 - b1;
- switch (delta) {
- case 0:
- dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
- break;
- case 1:
- case 2:
- case 3:
- dest_bitpos += delta == 1 ? 1 : delta + 2;
- dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
- dest_bitpos ++;
- dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
- break;
- case -1:
- case -2:
- case -3:
- dest_bitpos += delta == -1 ? 1 : -delta + 2;
- dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
- dest_bitpos ++;
- break;
- }
- dest_bitpos ++;
- a0 = a1;
- a0color = 1 - a0color;
- } else {
- int a2 = _FindBit(src_buf, cols, a1 + 1, a0color);
- dest_bitpos ++;
- dest_bitpos ++;
- dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
- dest_bitpos ++;
- if (a0 < 0) {
- a0 = 0;
- }
- _FaxEncodeRun(dest_buf, dest_bitpos, a1 - a0, a0color);
- _FaxEncodeRun(dest_buf, dest_bitpos, a2 - a1, 1 - a0color);
- a0 = a2;
- }
- if (a0 >= cols) {
- return;
- }
- }
-}
-class CCodec_FaxEncoder : public CFX_Object
-{
-public:
- CCodec_FaxEncoder(FX_LPCBYTE src_buf, int width, int height, int pitch);
- ~CCodec_FaxEncoder();
- void Encode(FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
- void Encode2DLine(FX_LPCBYTE scan_line);
- CFX_BinaryBuf m_DestBuf;
- FX_LPBYTE m_pRefLine, m_pLineBuf;
- int m_Cols, m_Rows, m_Pitch;
- FX_LPCBYTE m_pSrcBuf;
-};
-CCodec_FaxEncoder::CCodec_FaxEncoder(FX_LPCBYTE src_buf, int width, int height, int pitch)
-{
- m_pSrcBuf = src_buf;
- m_Cols = width;
- m_Rows = height;
- m_Pitch = pitch;
- m_pRefLine = FX_Alloc(FX_BYTE, m_Pitch);
- if (m_pRefLine == NULL) {
- return;
- }
- FXSYS_memset8(m_pRefLine, 0xff, m_Pitch);
- m_pLineBuf = FX_Alloc(FX_BYTE, m_Pitch * 8);
- if (m_pLineBuf == NULL) {
- return;
- }
- m_DestBuf.EstimateSize(0, 10240);
-}
-CCodec_FaxEncoder::~CCodec_FaxEncoder()
-{
- if (m_pRefLine) {
- FX_Free(m_pRefLine);
- }
- if (m_pLineBuf) {
- FX_Free(m_pLineBuf);
- }
-}
-void CCodec_FaxEncoder::Encode(FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
-{
- int dest_bitpos = 0;
- FX_BYTE last_byte = 0;
- for (int i = 0; i < m_Rows; i ++) {
- FX_LPCBYTE scan_line = m_pSrcBuf + i * m_Pitch;
- FXSYS_memset32(m_pLineBuf, 0, m_Pitch * 8);
- m_pLineBuf[0] = last_byte;
- _FaxEncode2DLine(m_pLineBuf, dest_bitpos, scan_line, m_pRefLine, m_Cols);
- m_DestBuf.AppendBlock(m_pLineBuf, dest_bitpos / 8);
- last_byte = m_pLineBuf[dest_bitpos / 8];
- dest_bitpos %= 8;
- FXSYS_memcpy32(m_pRefLine, scan_line, m_Pitch);
- }
- if (dest_bitpos) {
- m_DestBuf.AppendByte(last_byte);
- }
- dest_buf = m_DestBuf.GetBuffer();
- dest_size = m_DestBuf.GetSize();
- m_DestBuf.DetachBuffer();
-}
-FX_BOOL CCodec_FaxModule::Encode(FX_LPCBYTE src_buf, int width, int height, int pitch, FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
-{
- CCodec_FaxEncoder encoder(src_buf, width, height, pitch);
- encoder.Encode(dest_buf, dest_size);
- return TRUE;
-}
-ICodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows)
-{
- CCodec_FaxDecoder* pDecoder = FX_NEW CCodec_FaxDecoder;
- if (pDecoder == NULL) {
- return NULL;
- }
- pDecoder->Create(src_buf, src_size, width, height, K, EndOfLine, EncodedByteAlign, BlackIs1, Columns, Rows);
- return pDecoder;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../../include/fxcodec/fx_codec.h"
+#include "codec_int.h"
+extern const FX_BYTE OneLeadPos[256];
+extern const FX_BYTE ZeroLeadPos[256];
+int _FindBit(const FX_BYTE* data_buf, int max_pos, int start_pos, int bit)
+{
+ if (start_pos >= max_pos) {
+ return max_pos;
+ }
+ FX_LPCBYTE leading_pos = bit ? OneLeadPos : ZeroLeadPos;
+ if (start_pos % 8) {
+ FX_BYTE data = data_buf[start_pos / 8];
+ if (bit) {
+ data &= 0xff >> (start_pos % 8);
+ } else {
+ data |= 0xff << (8 - start_pos % 8);
+ }
+ if (leading_pos[data] < 8) {
+ return start_pos / 8 * 8 + leading_pos[data];
+ }
+ start_pos += 7;
+ }
+ FX_BYTE skip = bit ? 0x00 : 0xff;
+ int byte_pos = start_pos / 8;
+ int max_byte = (max_pos + 7) / 8;
+ while (byte_pos < max_byte) {
+ if (data_buf[byte_pos] != skip) {
+ break;
+ }
+ byte_pos ++;
+ }
+ if (byte_pos == max_byte) {
+ return max_pos;
+ }
+ int pos = leading_pos[data_buf[byte_pos]] + byte_pos * 8;
+ if (pos > max_pos) {
+ pos = max_pos;
+ }
+ return pos;
+}
+void _FaxG4FindB1B2(const FX_BYTE* ref_buf, int columns, int a0, FX_BOOL a0color, int& b1, int& b2)
+{
+ if (a0color) {
+ a0color = 1;
+ }
+ FX_BYTE first_bit = (a0 < 0) ? 1 : ((ref_buf[a0 / 8] & (1 << (7 - a0 % 8))) != 0);
+ b1 = _FindBit(ref_buf, columns, a0 + 1, !first_bit);
+ if (b1 >= columns) {
+ b1 = b2 = columns;
+ return;
+ }
+ if (first_bit == !a0color) {
+ b1 = _FindBit(ref_buf, columns, b1 + 1, first_bit);
+ first_bit = !first_bit;
+ }
+ if (b1 >= columns) {
+ b1 = b2 = columns;
+ return;
+ }
+ b2 = _FindBit(ref_buf, columns, b1 + 1, first_bit);
+}
+void _FaxFillBits(FX_LPBYTE dest_buf, int columns, int startpos, int endpos)
+{
+ if (startpos < 0) {
+ startpos = 0;
+ }
+ if (endpos < 0) {
+ endpos = 0;
+ }
+ if (endpos >= columns) {
+ endpos = columns;
+ }
+ if (startpos >= endpos) {
+ return;
+ }
+ int first_byte = startpos / 8;
+ int last_byte = (endpos - 1) / 8;
+ if (first_byte == last_byte) {
+ for (int i = startpos % 8; i <= (endpos - 1) % 8; i ++) {
+ dest_buf[first_byte] -= 1 << (7 - i);
+ }
+ return;
+ }
+ int i;
+ for (i = startpos % 8; i < 8; i ++) {
+ dest_buf[first_byte] -= 1 << (7 - i);
+ }
+ for (i = 0; i <= (endpos - 1) % 8; i ++) {
+ dest_buf[last_byte] -= 1 << (7 - i);
+ }
+ if (last_byte > first_byte + 1) {
+ FXSYS_memset32(dest_buf + first_byte + 1, 0, last_byte - first_byte - 1);
+ }
+}
+#define NEXTBIT src_buf[bitpos/8] & (1 << (7-bitpos%8)); bitpos ++;
+#define ADDBIT(code, bit) code = code << 1; if (bit) code ++;
+#define GETBIT(bitpos) src_buf[bitpos/8] & (1 << (7-bitpos%8))
+static const FX_BYTE FaxBlackRunIns[] = {
+ 0,
+ 2,
+ 0x02, 3, 0,
+ 0x03, 2, 0,
+ 2,
+ 0x02, 1, 0,
+ 0x03, 4, 0,
+ 2,
+ 0x02, 6, 0,
+ 0x03, 5, 0,
+ 1,
+ 0x03, 7, 0,
+ 2,
+ 0x04, 9, 0,
+ 0x05, 8, 0,
+ 3,
+ 0x04, 10, 0,
+ 0x05, 11, 0,
+ 0x07, 12, 0,
+ 2,
+ 0x04, 13, 0,
+ 0x07, 14, 0,
+ 1,
+ 0x18, 15, 0,
+ 5,
+ 0x08, 18, 0,
+ 0x0f, 64, 0,
+ 0x17, 16, 0,
+ 0x18, 17, 0,
+ 0x37, 0, 0,
+ 10,
+ 0x08, 0x00, 0x07,
+ 0x0c, 0x40, 0x07,
+ 0x0d, 0x80, 0x07,
+ 0x17, 24, 0,
+ 0x18, 25, 0,
+ 0x28, 23, 0,
+ 0x37, 22, 0,
+ 0x67, 19, 0,
+ 0x68, 20, 0,
+ 0x6c, 21, 0,
+ 54,
+ 0x12, 1984 % 256, 1984 / 256,
+ 0x13, 2048 % 256, 2048 / 256,
+ 0x14, 2112 % 256, 2112 / 256,
+ 0x15, 2176 % 256, 2176 / 256,
+ 0x16, 2240 % 256, 2240 / 256,
+ 0x17, 2304 % 256, 2304 / 256,
+ 0x1c, 2368 % 256, 2368 / 256,
+ 0x1d, 2432 % 256, 2432 / 256,
+ 0x1e, 2496 % 256, 2496 / 256,
+ 0x1f, 2560 % 256, 2560 / 256,
+ 0x24, 52, 0,
+ 0x27, 55, 0,
+ 0x28, 56, 0,
+ 0x2b, 59, 0,
+ 0x2c, 60, 0,
+ 0x33, 320 % 256, 320 / 256,
+ 0x34, 384 % 256, 384 / 256,
+ 0x35, 448 % 256, 448 / 256,
+ 0x37, 53, 0,
+ 0x38, 54, 0,
+ 0x52, 50, 0,
+ 0x53, 51, 0,
+ 0x54, 44, 0,
+ 0x55, 45, 0,
+ 0x56, 46, 0,
+ 0x57, 47, 0,
+ 0x58, 57, 0,
+ 0x59, 58, 0,
+ 0x5a, 61, 0,
+ 0x5b, 256 % 256, 256 / 256,
+ 0x64, 48, 0,
+ 0x65, 49, 0,
+ 0x66, 62, 0,
+ 0x67, 63, 0,
+ 0x68, 30, 0,
+ 0x69, 31, 0,
+ 0x6a, 32, 0,
+ 0x6b, 33, 0,
+ 0x6c, 40, 0,
+ 0x6d, 41, 0,
+ 0xc8, 128, 0,
+ 0xc9, 192, 0,
+ 0xca, 26, 0,
+ 0xcb, 27, 0,
+ 0xcc, 28, 0,
+ 0xcd, 29, 0,
+ 0xd2, 34, 0,
+ 0xd3, 35, 0,
+ 0xd4, 36, 0,
+ 0xd5, 37, 0,
+ 0xd6, 38, 0,
+ 0xd7, 39, 0,
+ 0xda, 42, 0,
+ 0xdb, 43, 0,
+ 20,
+ 0x4a, 640 % 256, 640 / 256,
+ 0x4b, 704 % 256, 704 / 256,
+ 0x4c, 768 % 256, 768 / 256,
+ 0x4d, 832 % 256, 832 / 256,
+ 0x52, 1280 % 256, 1280 / 256,
+ 0x53, 1344 % 256, 1344 / 256,
+ 0x54, 1408 % 256, 1408 / 256,
+ 0x55, 1472 % 256, 1472 / 256,
+ 0x5a, 1536 % 256, 1536 / 256,
+ 0x5b, 1600 % 256, 1600 / 256,
+ 0x64, 1664 % 256, 1664 / 256,
+ 0x65, 1728 % 256, 1728 / 256,
+ 0x6c, 512 % 256, 512 / 256,
+ 0x6d, 576 % 256, 576 / 256,
+ 0x72, 896 % 256, 896 / 256,
+ 0x73, 960 % 256, 960 / 256,
+ 0x74, 1024 % 256, 1024 / 256,
+ 0x75, 1088 % 256, 1088 / 256,
+ 0x76, 1152 % 256, 1152 / 256,
+ 0x77, 1216 % 256, 1216 / 256,
+ 0xff
+};
+static const FX_BYTE FaxWhiteRunIns[] = {
+ 0,
+ 0,
+ 0,
+ 6,
+ 0x07, 2, 0,
+ 0x08, 3, 0,
+ 0x0B, 4, 0,
+ 0x0C, 5, 0,
+ 0x0E, 6, 0,
+ 0x0F, 7, 0,
+ 6,
+ 0x07, 10, 0,
+ 0x08, 11, 0,
+ 0x12, 128, 0,
+ 0x13, 8, 0,
+ 0x14, 9, 0,
+ 0x1b, 64, 0,
+ 9,
+ 0x03, 13, 0,
+ 0x07, 1, 0,
+ 0x08, 12, 0,
+ 0x17, 192, 0,
+ 0x18, 1664 % 256, 1664 / 256,
+ 0x2a, 16, 0,
+ 0x2B, 17, 0,
+ 0x34, 14, 0,
+ 0x35, 15, 0,
+ 12,
+ 0x03, 22, 0,
+ 0x04, 23, 0,
+ 0x08, 20, 0,
+ 0x0c, 19, 0,
+ 0x13, 26, 0,
+ 0x17, 21, 0,
+ 0x18, 28, 0,
+ 0x24, 27, 0,
+ 0x27, 18, 0,
+ 0x28, 24, 0,
+ 0x2B, 25, 0,
+ 0x37, 256 % 256, 256 / 256,
+ 42,
+ 0x02, 29, 0,
+ 0x03, 30, 0,
+ 0x04, 45, 0,
+ 0x05, 46, 0,
+ 0x0a, 47, 0,
+ 0x0b, 48, 0,
+ 0x12, 33, 0,
+ 0x13, 34, 0,
+ 0x14, 35, 0,
+ 0x15, 36, 0,
+ 0x16, 37, 0,
+ 0x17, 38, 0,
+ 0x1a, 31, 0,
+ 0x1b, 32, 0,
+ 0x24, 53, 0,
+ 0x25, 54, 0,
+ 0x28, 39, 0,
+ 0x29, 40, 0,
+ 0x2a, 41, 0,
+ 0x2b, 42, 0,
+ 0x2c, 43, 0,
+ 0x2d, 44, 0,
+ 0x32, 61, 0,
+ 0x33, 62, 0,
+ 0x34, 63, 0,
+ 0x35, 0, 0,
+ 0x36, 320 % 256, 320 / 256,
+ 0x37, 384 % 256, 384 / 256,
+ 0x4a, 59, 0,
+ 0x4b, 60, 0,
+ 0x52, 49, 0,
+ 0x53, 50, 0,
+ 0x54, 51, 0,
+ 0x55, 52, 0,
+ 0x58, 55, 0,
+ 0x59, 56, 0,
+ 0x5a, 57, 0,
+ 0x5b, 58, 0,
+ 0x64, 448 % 256, 448 / 256,
+ 0x65, 512 % 256, 512 / 256,
+ 0x67, 640 % 256, 640 / 256,
+ 0x68, 576 % 256, 576 / 256,
+ 16,
+ 0x98, 1472 % 256, 1472 / 256,
+ 0x99, 1536 % 256, 1536 / 256,
+ 0x9a, 1600 % 256, 1600 / 256,
+ 0x9b, 1728 % 256, 1728 / 256,
+ 0xcc, 704 % 256, 704 / 256,
+ 0xcd, 768 % 256, 768 / 256,
+ 0xd2, 832 % 256, 832 / 256,
+ 0xd3, 896 % 256, 896 / 256,
+ 0xd4, 960 % 256, 960 / 256,
+ 0xd5, 1024 % 256, 1024 / 256,
+ 0xd6, 1088 % 256, 1088 / 256,
+ 0xd7, 1152 % 256, 1152 / 256,
+ 0xd8, 1216 % 256, 1216 / 256,
+ 0xd9, 1280 % 256, 1280 / 256,
+ 0xda, 1344 % 256, 1344 / 256,
+ 0xdb, 1408 % 256, 1408 / 256,
+ 0,
+ 3,
+ 0x08, 1792 % 256, 1792 / 256,
+ 0x0c, 1856 % 256, 1856 / 256,
+ 0x0d, 1920 % 256, 1920 / 256,
+ 10,
+ 0x12, 1984 % 256, 1984 / 256,
+ 0x13, 2048 % 256, 2048 / 256,
+ 0x14, 2112 % 256, 2112 / 256,
+ 0x15, 2176 % 256, 2176 / 256,
+ 0x16, 2240 % 256, 2240 / 256,
+ 0x17, 2304 % 256, 2304 / 256,
+ 0x1c, 2368 % 256, 2368 / 256,
+ 0x1d, 2432 % 256, 2432 / 256,
+ 0x1e, 2496 % 256, 2496 / 256,
+ 0x1f, 2560 % 256, 2560 / 256,
+ 0xff,
+};
+int _FaxGetRun(FX_LPCBYTE ins_array, const FX_BYTE* src_buf, int& bitpos, int bitsize)
+{
+ FX_DWORD code = 0;
+ int ins_off = 0;
+ while (1) {
+ FX_BYTE ins = ins_array[ins_off++];
+ if (ins == 0xff) {
+ return -1;
+ }
+ if (bitpos >= bitsize) {
+ return -1;
+ }
+ code <<= 1;
+ if (src_buf[bitpos / 8] & (1 << (7 - bitpos % 8))) {
+ code ++;
+ }
+ bitpos ++;
+ int next_off = ins_off + ins * 3;
+ for (; ins_off < next_off; ins_off += 3) {
+ if (ins_array[ins_off] == code) {
+ return ins_array[ins_off + 1] + ins_array[ins_off + 2] * 256;
+ }
+ }
+ }
+}
+FX_BOOL _FaxG4GetRow(const FX_BYTE* src_buf, int bitsize, int& bitpos, FX_LPBYTE dest_buf, const FX_BYTE* ref_buf, int columns)
+{
+ int a0 = -1, a0color = 1;
+ while (1) {
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ int a1, a2, b1, b2;
+ _FaxG4FindB1B2(ref_buf, columns, a0, a0color, b1, b2);
+ FX_BOOL bit = NEXTBIT;
+ int v_delta = 0;
+ if (bit) {
+ } else {
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ FX_BOOL bit1 = NEXTBIT;
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ FX_BOOL bit2 = NEXTBIT;
+ if (bit1 && bit2) {
+ v_delta = 1;
+ } else if (bit1) {
+ v_delta = -1;
+ } else if (bit2) {
+ int run_len1 = 0;
+ while (1) {
+ int run = _FaxGetRun(a0color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, bitpos, bitsize);
+ run_len1 += run;
+ if (run < 64) {
+ break;
+ }
+ }
+ if (a0 < 0) {
+ run_len1 ++;
+ }
+ a1 = a0 + run_len1;
+ if (!a0color) {
+ _FaxFillBits(dest_buf, columns, a0, a1);
+ }
+ int run_len2 = 0;
+ while (1) {
+ int run = _FaxGetRun(a0color ? FaxBlackRunIns : FaxWhiteRunIns, src_buf, bitpos, bitsize);
+ run_len2 += run;
+ if (run < 64) {
+ break;
+ }
+ }
+ a2 = a1 + run_len2;
+ if (a0color) {
+ _FaxFillBits(dest_buf, columns, a1, a2);
+ }
+ a0 = a2;
+ if (a0 < columns) {
+ continue;
+ }
+ return TRUE;
+ } else {
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ bit = NEXTBIT;
+ if (bit) {
+ if (!a0color) {
+ _FaxFillBits(dest_buf, columns, a0, b2);
+ }
+ if (b2 >= columns) {
+ return TRUE;
+ }
+ a0 = b2;
+ continue;
+ } else {
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ FX_BOOL bit1 = NEXTBIT;
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ FX_BOOL bit2 = NEXTBIT;
+ if (bit1 && bit2) {
+ v_delta = 2;
+ } else if (bit1) {
+ v_delta = -2;
+ } else if (bit2) {
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ bit = NEXTBIT;
+ if (bit) {
+ v_delta = 3;
+ } else {
+ v_delta = -3;
+ }
+ } else {
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ bit = NEXTBIT;
+ if (bit) {
+ bitpos += 3;
+ continue;
+ } else {
+ bitpos += 5;
+ return TRUE;
+ }
+ }
+ }
+ }
+ }
+ a1 = b1 + v_delta;
+ if (!a0color) {
+ _FaxFillBits(dest_buf, columns, a0, a1);
+ }
+ if (a1 >= columns) {
+ return TRUE;
+ }
+ a0 = a1;
+ a0color = !a0color;
+ }
+}
+FX_BOOL _FaxSkipEOL(const FX_BYTE* src_buf, int bitsize, int& bitpos)
+{
+ int startbit = bitpos;
+ while (bitpos < bitsize) {
+ int bit = NEXTBIT;
+ if (bit) {
+ if (bitpos - startbit <= 11) {
+ bitpos = startbit;
+ }
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+FX_BOOL _FaxGet1DLine(const FX_BYTE* src_buf, int bitsize, int& bitpos, FX_LPBYTE dest_buf, int columns)
+{
+ int color = TRUE;
+ int startpos = 0;
+ while (1) {
+ if (bitpos >= bitsize) {
+ return FALSE;
+ }
+ int run_len = 0;
+ while (1) {
+ int run = _FaxGetRun(color ? FaxWhiteRunIns : FaxBlackRunIns, src_buf, bitpos, bitsize);
+ if (run < 0) {
+ while (bitpos < bitsize) {
+ int bit = NEXTBIT;
+ if (bit) {
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+ run_len += run;
+ if (run < 64) {
+ break;
+ }
+ }
+ if (!color) {
+ _FaxFillBits(dest_buf, columns, startpos, startpos + run_len);
+ }
+ startpos += run_len;
+ if (startpos >= columns) {
+ break;
+ }
+ color = !color;
+ }
+ return TRUE;
+}
+class CCodec_FaxDecoder : public CCodec_ScanlineDecoder
+{
+public:
+ CCodec_FaxDecoder();
+ virtual ~CCodec_FaxDecoder();
+ FX_BOOL Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows);
+ virtual void v_DownScale(int dest_width, int dest_height) {}
+ virtual FX_BOOL v_Rewind();
+ virtual FX_LPBYTE v_GetNextLine();
+ virtual FX_DWORD GetSrcOffset();
+ int m_Encoding, m_bEndOfLine, m_bByteAlign, m_bBlack;
+ int bitpos;
+ FX_LPCBYTE m_pSrcBuf;
+ FX_DWORD m_SrcSize;
+ FX_LPBYTE m_pScanlineBuf, m_pRefBuf;
+};
+CCodec_FaxDecoder::CCodec_FaxDecoder()
+{
+ m_pScanlineBuf = NULL;
+ m_pRefBuf = NULL;
+}
+CCodec_FaxDecoder::~CCodec_FaxDecoder()
+{
+ if (m_pScanlineBuf) {
+ FX_Free(m_pScanlineBuf);
+ }
+ if (m_pRefBuf) {
+ FX_Free(m_pRefBuf);
+ }
+}
+FX_BOOL CCodec_FaxDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows)
+{
+ m_Encoding = K;
+ m_bEndOfLine = EndOfLine;
+ m_bByteAlign = EncodedByteAlign;
+ m_bBlack = BlackIs1;
+ m_OrigWidth = Columns;
+ m_OrigHeight = Rows;
+ if (m_OrigWidth == 0) {
+ m_OrigWidth = width;
+ }
+ if (m_OrigHeight == 0) {
+ m_OrigHeight = height;
+ }
+ m_Pitch = (m_OrigWidth + 31) / 32 * 4;
+ m_OutputWidth = m_OrigWidth;
+ m_OutputHeight = m_OrigHeight;
+ m_pScanlineBuf = FX_Alloc(FX_BYTE, m_Pitch);
+ if (m_pScanlineBuf == NULL) {
+ return FALSE;
+ }
+ m_pRefBuf = FX_Alloc(FX_BYTE, m_Pitch);
+ if (m_pRefBuf == NULL) {
+ return FALSE;
+ }
+ m_pSrcBuf = src_buf;
+ m_SrcSize = src_size;
+ m_nComps = 1;
+ m_bpc = 1;
+ m_bColorTransformed = FALSE;
+ return TRUE;
+}
+FX_BOOL CCodec_FaxDecoder::v_Rewind()
+{
+ FXSYS_memset8(m_pRefBuf, 0xff, m_Pitch);
+ bitpos = 0;
+ return TRUE;
+}
+FX_LPBYTE CCodec_FaxDecoder::v_GetNextLine()
+{
+ int bitsize = m_SrcSize * 8;
+ _FaxSkipEOL(m_pSrcBuf, bitsize, bitpos);
+ if (bitpos >= bitsize) {
+ return NULL;
+ }
+ FXSYS_memset8(m_pScanlineBuf, 0xff, m_Pitch);
+ if (m_Encoding < 0) {
+ _FaxG4GetRow(m_pSrcBuf, bitsize, bitpos, m_pScanlineBuf, m_pRefBuf, m_OrigWidth);
+ FXSYS_memcpy32(m_pRefBuf, m_pScanlineBuf, m_Pitch);
+ } else if (m_Encoding == 0) {
+ _FaxGet1DLine(m_pSrcBuf, bitsize, bitpos, m_pScanlineBuf, m_OrigWidth);
+ } else {
+ FX_BOOL bNext1D = m_pSrcBuf[bitpos / 8] & (1 << (7 - bitpos % 8));
+ bitpos ++;
+ if (bNext1D) {
+ _FaxGet1DLine(m_pSrcBuf, bitsize, bitpos, m_pScanlineBuf, m_OrigWidth);
+ } else {
+ _FaxG4GetRow(m_pSrcBuf, bitsize, bitpos, m_pScanlineBuf, m_pRefBuf, m_OrigWidth);
+ }
+ FXSYS_memcpy32(m_pRefBuf, m_pScanlineBuf, m_Pitch);
+ }
+ if (m_bEndOfLine) {
+ _FaxSkipEOL(m_pSrcBuf, bitsize, bitpos);
+ }
+ if (m_bByteAlign && bitpos < bitsize) {
+ int bitpos0 = bitpos;
+ int bitpos1 = (bitpos + 7) / 8 * 8;
+ while (m_bByteAlign && bitpos0 < bitpos1) {
+ int bit = m_pSrcBuf[bitpos0 / 8] & (1 << (7 - bitpos0 % 8));
+ if (bit != 0) {
+ m_bByteAlign = FALSE;
+ } else {
+ bitpos0 ++;
+ }
+ }
+ if (m_bByteAlign) {
+ bitpos = bitpos1;
+ }
+ }
+ if (m_bBlack) {
+ for (int i = 0; i < m_Pitch; i ++) {
+ m_pScanlineBuf[i] = ~m_pScanlineBuf[i];
+ }
+ }
+ return m_pScanlineBuf;
+}
+FX_DWORD CCodec_FaxDecoder::GetSrcOffset()
+{
+ FX_DWORD ret = (bitpos + 7) / 8;
+ if (ret > m_SrcSize) {
+ ret = m_SrcSize;
+ }
+ return ret;
+}
+extern "C" {
+ void _FaxG4Decode(void*, FX_LPCBYTE src_buf, FX_DWORD src_size, int* pbitpos, FX_LPBYTE dest_buf, int width, int height, int pitch)
+ {
+ if (pitch == 0) {
+ pitch = (width + 7) / 8;
+ }
+ FX_LPBYTE ref_buf = FX_Alloc(FX_BYTE, pitch);
+ if (ref_buf == NULL) {
+ return;
+ }
+ FXSYS_memset8(ref_buf, 0xff, pitch);
+ int bitpos = *pbitpos;
+ for (int iRow = 0; iRow < height; iRow ++) {
+ FX_LPBYTE line_buf = dest_buf + iRow * pitch;
+ FXSYS_memset8(line_buf, 0xff, pitch);
+ _FaxG4GetRow(src_buf, src_size << 3, bitpos, line_buf, ref_buf, width);
+ FXSYS_memcpy32(ref_buf, line_buf, pitch);
+ }
+ FX_Free(ref_buf);
+ *pbitpos = bitpos;
+ }
+};
+static const FX_BYTE BlackRunTerminator[128] = {
+ 0x37, 10, 0x02, 3, 0x03, 2, 0x02, 2, 0x03, 3, 0x03, 4, 0x02, 4, 0x03, 5,
+ 0x05, 6, 0x04, 6, 0x04, 7, 0x05, 7, 0x07, 7, 0x04, 8, 0x07, 8, 0x18, 9,
+ 0x17, 10, 0x18, 10, 0x08, 10, 0x67, 11, 0x68, 11, 0x6c, 11, 0x37, 11, 0x28, 11,
+ 0x17, 11, 0x18, 11, 0xca, 12, 0xcb, 12, 0xcc, 12, 0xcd, 12, 0x68, 12, 0x69, 12,
+ 0x6a, 12, 0x6b, 12, 0xd2, 12, 0xd3, 12, 0xd4, 12, 0xd5, 12, 0xd6, 12, 0xd7, 12,
+ 0x6c, 12, 0x6d, 12, 0xda, 12, 0xdb, 12, 0x54, 12, 0x55, 12, 0x56, 12, 0x57, 12,
+ 0x64, 12, 0x65, 12, 0x52, 12, 0x53, 12, 0x24, 12, 0x37, 12, 0x38, 12, 0x27, 12,
+ 0x28, 12, 0x58, 12, 0x59, 12, 0x2b, 12, 0x2c, 12, 0x5a, 12, 0x66, 12, 0x67, 12,
+};
+static const FX_BYTE BlackRunMarkup[80] = {
+ 0x0f, 10, 0xc8, 12, 0xc9, 12, 0x5b, 12, 0x33, 12, 0x34, 12, 0x35, 12, 0x6c, 13,
+ 0x6d, 13, 0x4a, 13, 0x4b, 13, 0x4c, 13, 0x4d, 13, 0x72, 13, 0x73, 13, 0x74, 13,
+ 0x75, 13, 0x76, 13, 0x77, 13, 0x52, 13, 0x53, 13, 0x54, 13, 0x55, 13, 0x5a, 13,
+ 0x5b, 13, 0x64, 13, 0x65, 13, 0x08, 11, 0x0c, 11, 0x0d, 11, 0x12, 12, 0x13, 12,
+ 0x14, 12, 0x15, 12, 0x16, 12, 0x17, 12, 0x1c, 12, 0x1d, 12, 0x1e, 12, 0x1f, 12,
+};
+static const FX_BYTE WhiteRunTerminator[128] = {
+ 0x35, 8,
+ 0x07, 6,
+ 0x07, 4,
+ 0x08, 4,
+ 0x0B, 4,
+ 0x0C, 4,
+ 0x0E, 4,
+ 0x0F, 4,
+ 0x13, 5,
+ 0x14, 5,
+ 0x07, 5,
+ 0x08, 5,
+ 0x08, 6,
+ 0x03, 6,
+ 0x34, 6,
+ 0x35, 6,
+ 0x2a, 6,
+ 0x2B, 6,
+ 0x27, 7,
+ 0x0c, 7,
+ 0x08, 7,
+ 0x17, 7,
+ 0x03, 7,
+ 0x04, 7,
+ 0x28, 7,
+ 0x2B, 7,
+ 0x13, 7,
+ 0x24, 7,
+ 0x18, 7,
+ 0x02, 8,
+ 0x03, 8,
+ 0x1a, 8,
+ 0x1b, 8,
+ 0x12, 8,
+ 0x13, 8,
+ 0x14, 8,
+ 0x15, 8,
+ 0x16, 8,
+ 0x17, 8,
+ 0x28, 8,
+ 0x29, 8,
+ 0x2a, 8,
+ 0x2b, 8,
+ 0x2c, 8,
+ 0x2d, 8,
+ 0x04, 8,
+ 0x05, 8,
+ 0x0a, 8,
+ 0x0b, 8,
+ 0x52, 8,
+ 0x53, 8,
+ 0x54, 8,
+ 0x55, 8,
+ 0x24, 8,
+ 0x25, 8,
+ 0x58, 8,
+ 0x59, 8,
+ 0x5a, 8,
+ 0x5b, 8,
+ 0x4a, 8,
+ 0x4b, 8,
+ 0x32, 8,
+ 0x33, 8,
+ 0x34, 8,
+};
+static const FX_BYTE WhiteRunMarkup[80] = {
+ 0x1b, 5,
+ 0x12, 5,
+ 0x17, 6,
+ 0x37, 7,
+ 0x36, 8,
+ 0x37, 8,
+ 0x64, 8,
+ 0x65, 8,
+ 0x68, 8,
+ 0x67, 8,
+ 0xcc, 9,
+ 0xcd, 9,
+ 0xd2, 9,
+ 0xd3, 9,
+ 0xd4, 9,
+ 0xd5, 9,
+ 0xd6, 9,
+ 0xd7, 9,
+ 0xd8, 9,
+ 0xd9, 9,
+ 0xda, 9,
+ 0xdb, 9,
+ 0x98, 9,
+ 0x99, 9,
+ 0x9a, 9,
+ 0x18, 6,
+ 0x9b, 9,
+ 0x08, 11,
+ 0x0c, 11,
+ 0x0d, 11,
+ 0x12, 12,
+ 0x13, 12,
+ 0x14, 12,
+ 0x15, 12,
+ 0x16, 12,
+ 0x17, 12,
+ 0x1c, 12,
+ 0x1d, 12,
+ 0x1e, 12,
+ 0x1f, 12,
+};
+static void _AddBitStream(FX_LPBYTE dest_buf, int& dest_bitpos, int data, int bitlen)
+{
+ for (int i = bitlen - 1; i >= 0; i --) {
+ if (data & (1 << i)) {
+ dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
+ }
+ dest_bitpos ++;
+ }
+}
+static void _FaxEncodeRun(FX_LPBYTE dest_buf, int& dest_bitpos, int run, FX_BOOL bWhite)
+{
+ while (run >= 2560) {
+ _AddBitStream(dest_buf, dest_bitpos, 0x1f, 12);
+ run -= 2560;
+ }
+ if (run >= 64) {
+ int markup = run - run % 64;
+ FX_LPCBYTE p = bWhite ? WhiteRunMarkup : BlackRunMarkup;
+ p += (markup / 64 - 1) * 2;
+ _AddBitStream(dest_buf, dest_bitpos, *p, p[1]);
+ }
+ run %= 64;
+ FX_LPCBYTE p = bWhite ? WhiteRunTerminator : BlackRunTerminator;
+ p += run * 2;
+ _AddBitStream(dest_buf, dest_bitpos, *p, p[1]);
+}
+static void _FaxEncode2DLine(FX_LPBYTE dest_buf, int& dest_bitpos, FX_LPCBYTE src_buf, FX_LPCBYTE ref_buf, int cols)
+{
+ int a0 = -1, a0color = 1;
+ while (1) {
+ int a1 = _FindBit(src_buf, cols, a0 + 1, 1 - a0color);
+ int b1, b2;
+ _FaxG4FindB1B2(ref_buf, cols, a0, a0color, b1, b2);
+ if (b2 < a1) {
+ dest_bitpos += 3;
+ dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
+ dest_bitpos ++;
+ a0 = b2;
+ } else if (a1 - b1 <= 3 && b1 - a1 <= 3) {
+ int delta = a1 - b1;
+ switch (delta) {
+ case 0:
+ dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
+ break;
+ case 1:
+ case 2:
+ case 3:
+ dest_bitpos += delta == 1 ? 1 : delta + 2;
+ dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
+ dest_bitpos ++;
+ dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
+ break;
+ case -1:
+ case -2:
+ case -3:
+ dest_bitpos += delta == -1 ? 1 : -delta + 2;
+ dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
+ dest_bitpos ++;
+ break;
+ }
+ dest_bitpos ++;
+ a0 = a1;
+ a0color = 1 - a0color;
+ } else {
+ int a2 = _FindBit(src_buf, cols, a1 + 1, a0color);
+ dest_bitpos ++;
+ dest_bitpos ++;
+ dest_buf[dest_bitpos / 8] |= 1 << (7 - dest_bitpos % 8);
+ dest_bitpos ++;
+ if (a0 < 0) {
+ a0 = 0;
+ }
+ _FaxEncodeRun(dest_buf, dest_bitpos, a1 - a0, a0color);
+ _FaxEncodeRun(dest_buf, dest_bitpos, a2 - a1, 1 - a0color);
+ a0 = a2;
+ }
+ if (a0 >= cols) {
+ return;
+ }
+ }
+}
+class CCodec_FaxEncoder : public CFX_Object
+{
+public:
+ CCodec_FaxEncoder(FX_LPCBYTE src_buf, int width, int height, int pitch);
+ ~CCodec_FaxEncoder();
+ void Encode(FX_LPBYTE& dest_buf, FX_DWORD& dest_size);
+ void Encode2DLine(FX_LPCBYTE scan_line);
+ CFX_BinaryBuf m_DestBuf;
+ FX_LPBYTE m_pRefLine, m_pLineBuf;
+ int m_Cols, m_Rows, m_Pitch;
+ FX_LPCBYTE m_pSrcBuf;
+};
+CCodec_FaxEncoder::CCodec_FaxEncoder(FX_LPCBYTE src_buf, int width, int height, int pitch)
+{
+ m_pSrcBuf = src_buf;
+ m_Cols = width;
+ m_Rows = height;
+ m_Pitch = pitch;
+ m_pRefLine = FX_Alloc(FX_BYTE, m_Pitch);
+ if (m_pRefLine == NULL) {
+ return;
+ }
+ FXSYS_memset8(m_pRefLine, 0xff, m_Pitch);
+ m_pLineBuf = FX_Alloc(FX_BYTE, m_Pitch * 8);
+ if (m_pLineBuf == NULL) {
+ return;
+ }
+ m_DestBuf.EstimateSize(0, 10240);
+}
+CCodec_FaxEncoder::~CCodec_FaxEncoder()
+{
+ if (m_pRefLine) {
+ FX_Free(m_pRefLine);
+ }
+ if (m_pLineBuf) {
+ FX_Free(m_pLineBuf);
+ }
+}
+void CCodec_FaxEncoder::Encode(FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
+{
+ int dest_bitpos = 0;
+ FX_BYTE last_byte = 0;
+ for (int i = 0; i < m_Rows; i ++) {
+ FX_LPCBYTE scan_line = m_pSrcBuf + i * m_Pitch;
+ FXSYS_memset32(m_pLineBuf, 0, m_Pitch * 8);
+ m_pLineBuf[0] = last_byte;
+ _FaxEncode2DLine(m_pLineBuf, dest_bitpos, scan_line, m_pRefLine, m_Cols);
+ m_DestBuf.AppendBlock(m_pLineBuf, dest_bitpos / 8);
+ last_byte = m_pLineBuf[dest_bitpos / 8];
+ dest_bitpos %= 8;
+ FXSYS_memcpy32(m_pRefLine, scan_line, m_Pitch);
+ }
+ if (dest_bitpos) {
+ m_DestBuf.AppendByte(last_byte);
+ }
+ dest_buf = m_DestBuf.GetBuffer();
+ dest_size = m_DestBuf.GetSize();
+ m_DestBuf.DetachBuffer();
+}
+FX_BOOL CCodec_FaxModule::Encode(FX_LPCBYTE src_buf, int width, int height, int pitch, FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
+{
+ CCodec_FaxEncoder encoder(src_buf, width, height, pitch);
+ encoder.Encode(dest_buf, dest_size);
+ return TRUE;
+}
+ICodec_ScanlineDecoder* CCodec_FaxModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int K, FX_BOOL EndOfLine, FX_BOOL EncodedByteAlign, FX_BOOL BlackIs1, int Columns, int Rows)
+{
+ CCodec_FaxDecoder* pDecoder = FX_NEW CCodec_FaxDecoder;
+ if (pDecoder == NULL) {
+ return NULL;
+ }
+ pDecoder->Create(src_buf, src_size, width, height, K, EndOfLine, EncodedByteAlign, BlackIs1, Columns, Rows);
+ return pDecoder;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_flate.cpp b/core/src/fxcodec/codec/fx_codec_flate.cpp
index b6f94e021c..4724593a94 100644
--- a/core/src/fxcodec/codec/fx_codec_flate.cpp
+++ b/core/src/fxcodec/codec/fx_codec_flate.cpp
@@ -1,945 +1,945 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../fx_zlib.h"
-#include "../../../include/fxcodec/fx_codec.h"
-#include "codec_int.h"
-extern "C"
-{
- static void* my_alloc_func (void* opaque, unsigned int items, unsigned int size)
- {
- return FX_Alloc(FX_BYTE, items * size);
- }
- static void my_free_func (void* opaque, void* address)
- {
- FX_Free(address);
- }
- void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned int),
- void (*free_func)(void*, void*))
- {
- z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream));
- if (p == NULL) {
- return NULL;
- }
- FXSYS_memset32(p, 0, sizeof(z_stream));
- p->zalloc = alloc_func;
- p->zfree = free_func;
- inflateInit(p);
- return p;
- }
- void FPDFAPI_FlateInput(void* context, const unsigned char* src_buf, unsigned int src_size)
- {
- ((z_stream*)context)->next_in = (unsigned char*)src_buf;
- ((z_stream*)context)->avail_in = src_size;
- }
- int FPDFAPI_FlateGetTotalOut(void* context)
- {
- return ((z_stream*)context)->total_out;
- }
- int FPDFAPI_FlateOutput(void* context, unsigned char* dest_buf, unsigned int dest_size)
- {
- ((z_stream*)context)->next_out = dest_buf;
- ((z_stream*)context)->avail_out = dest_size;
- unsigned int pre_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context);
- int ret = inflate((z_stream*)context, Z_SYNC_FLUSH);
- unsigned int post_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context);
- unsigned int written = post_pos - pre_pos;
- if (written < dest_size) {
- FXSYS_memset8(dest_buf + written, '\0', dest_size - written);
- }
- return ret;
- }
- int FPDFAPI_FlateGetTotalIn(void* context)
- {
- return ((z_stream*)context)->total_in;
- }
- int FPDFAPI_FlateGetAvailOut(void* context)
- {
- return ((z_stream*)context)->avail_out;
- }
- int FPDFAPI_FlateGetAvailIn(void* context)
- {
- return ((z_stream*)context)->avail_in;
- }
- void FPDFAPI_FlateEnd(void* context)
- {
- inflateEnd((z_stream*)context);
- ((z_stream*)context)->zfree(0, context);
- }
- void FPDFAPI_FlateCompress(unsigned char* dest_buf, unsigned long* dest_size, const unsigned char* src_buf, unsigned long src_size)
- {
- compress(dest_buf, dest_size, src_buf, src_size);
- }
-}
-class CLZWDecoder : public CFX_Object
-{
-public:
- FX_BOOL Decode(FX_LPBYTE output, FX_DWORD& outlen, const FX_BYTE* input, FX_DWORD& size, FX_BOOL bEarlyChange);
-private:
- FX_DWORD m_InPos;
- FX_DWORD m_OutPos;
- FX_LPBYTE m_pOutput;
- const FX_BYTE* m_pInput;
- FX_BOOL m_Early;
- void AddCode(FX_DWORD prefix_code, FX_BYTE append_char);
- FX_DWORD m_CodeArray[5021];
- FX_DWORD m_nCodes;
- FX_BYTE m_DecodeStack[4000];
- FX_DWORD m_StackLen;
- void DecodeString(FX_DWORD code);
- int m_CodeLen;
-};
-void CLZWDecoder::AddCode(FX_DWORD prefix_code, FX_BYTE append_char)
-{
- if (m_nCodes + m_Early == 4094) {
- return;
- }
- m_CodeArray[m_nCodes ++] = (prefix_code << 16) | append_char;
- if (m_nCodes + m_Early == 512 - 258) {
- m_CodeLen = 10;
- } else if (m_nCodes + m_Early == 1024 - 258) {
- m_CodeLen = 11;
- } else if (m_nCodes + m_Early == 2048 - 258) {
- m_CodeLen = 12;
- }
-}
-void CLZWDecoder::DecodeString(FX_DWORD code)
-{
- while (1) {
- int index = code - 258;
- if (index < 0 || index >= (int)m_nCodes) {
- break;
- }
- FX_DWORD data = m_CodeArray[index];
- if (m_StackLen >= sizeof(m_DecodeStack)) {
- return;
- }
- m_DecodeStack[m_StackLen++] = (FX_BYTE)data;
- code = data >> 16;
- }
- if (m_StackLen >= sizeof(m_DecodeStack)) {
- return;
- }
- m_DecodeStack[m_StackLen++] = (FX_BYTE)code;
-}
-int CLZWDecoder::Decode(FX_LPBYTE dest_buf, FX_DWORD& dest_size, const FX_BYTE* src_buf, FX_DWORD& src_size, FX_BOOL bEarlyChange)
-{
- m_CodeLen = 9;
- m_InPos = 0;
- m_OutPos = 0;
- m_pInput = src_buf;
- m_pOutput = dest_buf;
- m_Early = bEarlyChange ? 1 : 0;
- m_nCodes = 0;
- FX_DWORD old_code = (FX_DWORD) - 1;
- FX_BYTE last_char;
- while (1) {
- if (m_InPos + m_CodeLen > src_size * 8) {
- break;
- }
- int byte_pos = m_InPos / 8;
- int bit_pos = m_InPos % 8, bit_left = m_CodeLen;
- FX_DWORD code = 0;
- if (bit_pos) {
- bit_left -= 8 - bit_pos;
- code = (m_pInput[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left;
- }
- if (bit_left < 8) {
- code |= m_pInput[byte_pos] >> (8 - bit_left);
- } else {
- bit_left -= 8;
- code |= m_pInput[byte_pos++] << bit_left;
- if (bit_left) {
- code |= m_pInput[byte_pos] >> (8 - bit_left);
- }
- }
- m_InPos += m_CodeLen;
- if (code < 256) {
- if (m_OutPos == dest_size) {
- return -5;
- }
- if (m_pOutput) {
- m_pOutput[m_OutPos] = (FX_BYTE)code;
- }
- m_OutPos ++;
- last_char = (FX_BYTE)code;
- if (old_code != (FX_DWORD) - 1) {
- AddCode(old_code, last_char);
- }
- old_code = code;
- } else if (code == 256) {
- m_CodeLen = 9;
- m_nCodes = 0;
- old_code = (FX_DWORD) - 1;
- } else if (code == 257) {
- break;
- } else {
- if (old_code == (FX_DWORD) - 1) {
- return 2;
- }
- m_StackLen = 0;
- if (code >= m_nCodes + 258) {
- if (m_StackLen < sizeof(m_DecodeStack)) {
- m_DecodeStack[m_StackLen++] = last_char;
- }
- DecodeString(old_code);
- } else {
- DecodeString(code);
- }
- if (m_OutPos + m_StackLen > dest_size) {
- return -5;
- }
- if (m_pOutput) {
- for (FX_DWORD i = 0; i < m_StackLen; i ++) {
- m_pOutput[m_OutPos + i] = m_DecodeStack[m_StackLen - i - 1];
- }
- }
- m_OutPos += m_StackLen;
- last_char = m_DecodeStack[m_StackLen - 1];
- if (old_code < 256) {
- AddCode(old_code, last_char);
- } else if (old_code - 258 >= m_nCodes) {
- dest_size = m_OutPos;
- src_size = (m_InPos + 7) / 8;
- return 0;
- } else {
- AddCode(old_code, last_char);
- }
- old_code = code;
- }
- }
- dest_size = m_OutPos;
- src_size = (m_InPos + 7) / 8;
- return 0;
-}
-static FX_BYTE PaethPredictor(int a, int b, int c)
-{
- int p = a + b - c;
- int pa = FXSYS_abs(p - a);
- int pb = FXSYS_abs(p - b);
- int pc = FXSYS_abs(p - c);
- if (pa <= pb && pa <= pc) {
- return (FX_BYTE)a;
- }
- if (pb <= pc) {
- return (FX_BYTE)b;
- }
- return (FX_BYTE)c;
-}
-static void PNG_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size, int predictor, int Colors, int BitsPerComponent, int Columns)
-{
- int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8;
- int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
- int row_count = (data_size + row_size - 1) / row_size;
- int last_row_size = data_size % row_size;
- FX_LPBYTE dest_buf = FX_Alloc( FX_BYTE, (row_size + 1) * row_count);
- if (dest_buf == NULL) {
- return;
- }
- int byte_cnt = 0;
- FX_LPBYTE pSrcData = data_buf;
- FX_LPBYTE pDestData = dest_buf;
- for (int row = 0; row < row_count; row++) {
- if (predictor == 10) {
- pDestData[0] = 0;
- int move_size = row_size;
- if (move_size * (row + 1) > (int)data_size) {
- move_size = data_size - (move_size * row);
- }
- FXSYS_memmove32(pDestData + 1, pSrcData, move_size);
- pDestData += (move_size + 1);
- pSrcData += move_size;
- byte_cnt += move_size;
- continue;
- }
- for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte++) {
- switch (predictor) {
- case 11: {
- pDestData[0] = 1;
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pSrcData[byte - BytesPerPixel];
- }
- pDestData[byte + 1] = pSrcData[byte] - left;
- }
- break;
- case 12: {
- pDestData[0] = 2;
- FX_BYTE up = 0;
- if (row) {
- up = pSrcData[byte - row_size];
- }
- pDestData[byte + 1] = pSrcData[byte] - up;
- }
- break;
- case 13: {
- pDestData[0] = 3;
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pSrcData[byte - BytesPerPixel];
- }
- FX_BYTE up = 0;
- if (row) {
- up = pSrcData[byte - row_size];
- }
- pDestData[byte + 1] = pSrcData[byte] - (left + up) / 2;
- }
- break;
- case 14: {
- pDestData[0] = 4;
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pSrcData[byte - BytesPerPixel];
- }
- FX_BYTE up = 0;
- if (row) {
- up = pSrcData[byte - row_size];
- }
- FX_BYTE upper_left = 0;
- if (byte >= BytesPerPixel && row) {
- upper_left = pSrcData[byte - row_size - BytesPerPixel];
- }
- pDestData[byte + 1] = pSrcData[byte] - PaethPredictor(left, up, upper_left);
- }
- break;
- default: {
- pDestData[byte + 1] = pSrcData[byte];
- }
- break;
- }
- byte_cnt++;
- }
- pDestData += (row_size + 1);
- pSrcData += row_size;
- }
- FX_Free(data_buf);
- data_buf = dest_buf;
- data_size = (row_size + 1) * row_count - (last_row_size > 0 ? (row_size - last_row_size) : 0);
-}
-static void PNG_PredictLine(FX_LPBYTE pDestData, FX_LPCBYTE pSrcData, FX_LPCBYTE pLastLine,
- int bpc, int nColors, int nPixels)
-{
- int row_size = (nPixels * bpc * nColors + 7) / 8;
- int BytesPerPixel = (bpc * nColors + 7) / 8;
- FX_BYTE tag = pSrcData[0];
- if (tag == 0) {
- FXSYS_memmove32(pDestData, pSrcData + 1, row_size);
- return;
- }
- for (int byte = 0; byte < row_size; byte ++) {
- FX_BYTE raw_byte = pSrcData[byte + 1];
- switch (tag) {
- case 1: {
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pDestData[byte - BytesPerPixel];
- }
- pDestData[byte] = raw_byte + left;
- break;
- }
- case 2: {
- FX_BYTE up = 0;
- if (pLastLine) {
- up = pLastLine[byte];
- }
- pDestData[byte] = raw_byte + up;
- break;
- }
- case 3: {
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pDestData[byte - BytesPerPixel];
- }
- FX_BYTE up = 0;
- if (pLastLine) {
- up = pLastLine[byte];
- }
- pDestData[byte] = raw_byte + (up + left) / 2;
- break;
- }
- case 4: {
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pDestData[byte - BytesPerPixel];
- }
- FX_BYTE up = 0;
- if (pLastLine) {
- up = pLastLine[byte];
- }
- FX_BYTE upper_left = 0;
- if (byte >= BytesPerPixel && pLastLine) {
- upper_left = pLastLine[byte - BytesPerPixel];
- }
- pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left);
- break;
- }
- default:
- pDestData[byte] = raw_byte;
- break;
- }
- }
-}
-static void PNG_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size,
- int Colors, int BitsPerComponent, int Columns)
-{
- int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8;
- int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
- int row_count = (data_size + row_size) / (row_size + 1);
- int last_row_size = data_size % (row_size + 1);
- FX_LPBYTE dest_buf = FX_Alloc( FX_BYTE, row_size * row_count);
- if (dest_buf == NULL) {
- return;
- }
- int byte_cnt = 0;
- FX_LPBYTE pSrcData = data_buf;
- FX_LPBYTE pDestData = dest_buf;
- for (int row = 0; row < row_count; row ++) {
- FX_BYTE tag = pSrcData[0];
- if (tag == 0) {
- int move_size = row_size;
- if ((row + 1) * (move_size + 1) > (int)data_size) {
- move_size = last_row_size - 1;
- }
- FXSYS_memmove32(pDestData, pSrcData + 1, move_size);
- pSrcData += move_size + 1;
- pDestData += move_size;
- byte_cnt += move_size + 1;
- continue;
- }
- for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte ++) {
- FX_BYTE raw_byte = pSrcData[byte + 1];
- switch (tag) {
- case 1: {
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pDestData[byte - BytesPerPixel];
- }
- pDestData[byte] = raw_byte + left;
- break;
- }
- case 2: {
- FX_BYTE up = 0;
- if (row) {
- up = pDestData[byte - row_size];
- }
- pDestData[byte] = raw_byte + up;
- break;
- }
- case 3: {
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pDestData[byte - BytesPerPixel];
- }
- FX_BYTE up = 0;
- if (row) {
- up = pDestData[byte - row_size];
- }
- pDestData[byte] = raw_byte + (up + left) / 2;
- break;
- }
- case 4: {
- FX_BYTE left = 0;
- if (byte >= BytesPerPixel) {
- left = pDestData[byte - BytesPerPixel];
- }
- FX_BYTE up = 0;
- if (row) {
- up = pDestData[byte - row_size];
- }
- FX_BYTE upper_left = 0;
- if (byte >= BytesPerPixel && row) {
- upper_left = pDestData[byte - row_size - BytesPerPixel];
- }
- pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left);
- break;
- }
- default:
- pDestData[byte] = raw_byte;
- break;
- }
- byte_cnt++;
- }
- pSrcData += row_size + 1;
- pDestData += row_size;
- byte_cnt++;
- }
- FX_Free(data_buf);
- data_buf = dest_buf;
- data_size = row_size * row_count - (last_row_size > 0 ? (row_size + 1 - last_row_size) : 0);
-}
-static void TIFF_PredictorEncodeLine(FX_LPBYTE dest_buf, int row_size, int BitsPerComponent, int Colors, int Columns)
-{
- int BytesPerPixel = BitsPerComponent * Colors / 8;
- if (BitsPerComponent < 8) {
- FX_BYTE mask = 0x01;
- if (BitsPerComponent == 2) {
- mask = 0x03;
- } else if (BitsPerComponent == 4) {
- mask = 0x0F;
- }
- int row_bits = Colors * BitsPerComponent * Columns;
- for (int i = row_bits - BitsPerComponent; i >= BitsPerComponent; i -= BitsPerComponent) {
- int col = i % 8;
- int index = i / 8;
- int col_pre = (col == 0) ? (8 - BitsPerComponent) : (col - BitsPerComponent);
- int index_pre = (col == 0) ? (index - 1) : index;
- FX_BYTE cur = (dest_buf[index] >> (8 - col - BitsPerComponent)) & mask;
- FX_BYTE left = (dest_buf[index_pre] >> (8 - col_pre - BitsPerComponent)) & mask;
- cur -= left;
- cur &= mask;
- cur <<= (8 - col - BitsPerComponent);
- dest_buf[index] &= ~(mask << ((8 - col - BitsPerComponent)));
- dest_buf[index] |= cur;
- }
- } else if (BitsPerComponent == 8) {
- for (int i = row_size - 1; i >= BytesPerPixel; i--) {
- dest_buf[i] -= dest_buf[i - BytesPerPixel];
- }
- } else {
- for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; i -= BytesPerPixel) {
- FX_WORD pixel = (dest_buf[i] << 8) | dest_buf[i + 1];
- pixel -= (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1];
- dest_buf[i] = pixel >> 8;
- dest_buf[i + 1] = (FX_BYTE)pixel;
- }
- }
-}
-static void TIFF_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size,
- int Colors, int BitsPerComponent, int Columns)
-{
- int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
- int row_count = (data_size + row_size - 1) / row_size;
- int last_row_size = data_size % row_size;
- for (int row = 0; row < row_count; row++) {
- FX_LPBYTE scan_line = data_buf + row * row_size;
- if ((row + 1) * row_size > (int)data_size) {
- row_size = last_row_size;
- }
- TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors, Columns);
- }
-}
-static void TIFF_PredictLine(FX_LPBYTE dest_buf, int row_size, int BitsPerComponent, int Colors, int Columns)
-{
- if (BitsPerComponent == 1) {
- int row_bits = BitsPerComponent * Colors * Columns;
- for(int i = 1; i < row_bits; i ++) {
- int col = i % 8;
- int index = i / 8;
- int index_pre = (col == 0) ? (index - 1) : index;
- int col_pre = (col == 0) ? 8 : col;
- if( ((dest_buf[index] >> (7 - col)) & 1) ^ ((dest_buf[index_pre] >> (8 - col_pre)) & 1) ) {
- dest_buf[index] |= 1 << (7 - col);
- } else {
- dest_buf[index] &= ~(1 << (7 - col));
- }
- }
- return;
- }
- int BytesPerPixel = BitsPerComponent * Colors / 8;
- if (BitsPerComponent == 16) {
- for (int i = BytesPerPixel; i < row_size; i += 2) {
- FX_WORD pixel = (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1];
- pixel += (dest_buf[i] << 8) | dest_buf[i + 1];
- dest_buf[i] = pixel >> 8;
- dest_buf[i + 1] = (FX_BYTE)pixel;
- }
- } else {
- for (int i = BytesPerPixel; i < row_size; i ++) {
- dest_buf[i] += dest_buf[i - BytesPerPixel];
- }
- }
-}
-static void TIFF_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size,
- int Colors, int BitsPerComponent, int Columns)
-{
- int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
- int row_count = (data_size + row_size - 1) / row_size;
- int last_row_size = data_size % row_size;
- for (int row = 0; row < row_count; row ++) {
- FX_LPBYTE scan_line = data_buf + row * row_size;
- if ((row + 1) * row_size > (int)data_size) {
- row_size = last_row_size;
- }
- TIFF_PredictLine(scan_line, row_size, BitsPerComponent, Colors, Columns);
- }
-}
-class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder
-{
-public:
- CCodec_FlateScanlineDecoder();
- ~CCodec_FlateScanlineDecoder();
- FX_BOOL Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps, int bpc,
- int predictor, int Colors, int BitsPerComponent, int Columns);
- virtual void Destroy()
- {
- delete this;
- }
- virtual void v_DownScale(int dest_width, int dest_height) {}
- virtual FX_BOOL v_Rewind();
- virtual FX_LPBYTE v_GetNextLine();
- virtual FX_DWORD GetSrcOffset();
- void* m_pFlate;
- FX_LPCBYTE m_SrcBuf;
- FX_DWORD m_SrcSize;
- FX_LPBYTE m_pScanline;
- FX_LPBYTE m_pLastLine;
- FX_LPBYTE m_pPredictBuffer;
- FX_LPBYTE m_pPredictRaw;
- int m_Predictor;
- int m_Colors, m_BitsPerComponent, m_Columns, m_PredictPitch, m_LeftOver;
-};
-CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder()
-{
- m_pFlate = NULL;
- m_pScanline = NULL;
- m_pLastLine = NULL;
- m_pPredictBuffer = NULL;
- m_pPredictRaw = NULL;
- m_LeftOver = 0;
-}
-CCodec_FlateScanlineDecoder::~CCodec_FlateScanlineDecoder()
-{
- if (m_pScanline) {
- FX_Free(m_pScanline);
- }
- if (m_pLastLine) {
- FX_Free(m_pLastLine);
- }
- if (m_pPredictBuffer) {
- FX_Free(m_pPredictBuffer);
- }
- if (m_pPredictRaw) {
- FX_Free(m_pPredictRaw);
- }
- if (m_pFlate) {
- FPDFAPI_FlateEnd(m_pFlate);
- }
-}
-FX_BOOL CCodec_FlateScanlineDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, int Columns)
-{
- m_SrcBuf = src_buf;
- m_SrcSize = src_size;
- m_OutputWidth = m_OrigWidth = width;
- m_OutputHeight = m_OrigHeight = height;
- m_nComps = nComps;
- m_bpc = bpc;
- m_bColorTransformed = FALSE;
- m_Pitch = (width * nComps * bpc + 7) / 8;
- m_pScanline = FX_Alloc(FX_BYTE, m_Pitch);
- if (m_pScanline == NULL) {
- return FALSE;
- }
- m_Predictor = 0;
- if (predictor) {
- if (predictor >= 10) {
- m_Predictor = 2;
- } else if (predictor == 2) {
- m_Predictor = 1;
- }
- if (m_Predictor) {
- if (BitsPerComponent * Colors * Columns == 0) {
- BitsPerComponent = m_bpc;
- Colors = m_nComps;
- Columns = m_OrigWidth;
- }
- m_Colors = Colors;
- m_BitsPerComponent = BitsPerComponent;
- m_Columns = Columns;
- m_PredictPitch = (m_BitsPerComponent * m_Colors * m_Columns + 7) / 8;
- m_pLastLine = FX_Alloc(FX_BYTE, m_PredictPitch);
- if (m_pLastLine == NULL) {
- return FALSE;
- }
- FXSYS_memset32(m_pLastLine, 0, m_PredictPitch);
- m_pPredictRaw = FX_Alloc(FX_BYTE, m_PredictPitch + 1);
- if (m_pPredictRaw == NULL) {
- return FALSE;
- }
- m_pPredictBuffer = FX_Alloc(FX_BYTE, m_PredictPitch);
- if (m_pPredictBuffer == NULL) {
- return FALSE;
- }
- }
- }
- return TRUE;
-}
-FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind()
-{
- if (m_pFlate) {
- FPDFAPI_FlateEnd(m_pFlate);
- }
- m_pFlate = FPDFAPI_FlateInit(my_alloc_func, my_free_func);
- if (m_pFlate == NULL) {
- return FALSE;
- }
- FPDFAPI_FlateInput(m_pFlate, m_SrcBuf, m_SrcSize);
- m_LeftOver = 0;
- return TRUE;
-}
-FX_LPBYTE CCodec_FlateScanlineDecoder::v_GetNextLine()
-{
- if (m_Predictor) {
- if (m_Pitch == m_PredictPitch) {
- if (m_Predictor == 2) {
- FPDFAPI_FlateOutput(m_pFlate, m_pPredictRaw, m_PredictPitch + 1);
- PNG_PredictLine(m_pScanline, m_pPredictRaw, m_pLastLine, m_BitsPerComponent, m_Colors, m_Columns);
- FXSYS_memcpy32(m_pLastLine, m_pScanline, m_PredictPitch);
- } else {
- FPDFAPI_FlateOutput(m_pFlate, m_pScanline, m_Pitch);
- TIFF_PredictLine(m_pScanline, m_PredictPitch, m_bpc, m_nComps, m_OutputWidth);
- }
- } else {
- int bytes_to_go = m_Pitch;
- int read_leftover = m_LeftOver > bytes_to_go ? bytes_to_go : m_LeftOver;
- if (read_leftover) {
- FXSYS_memcpy32(m_pScanline, m_pPredictBuffer + m_PredictPitch - m_LeftOver, read_leftover);
- m_LeftOver -= read_leftover;
- bytes_to_go -= read_leftover;
- }
- while (bytes_to_go) {
- if (m_Predictor == 2) {
- FPDFAPI_FlateOutput(m_pFlate, m_pPredictRaw, m_PredictPitch + 1);
- PNG_PredictLine(m_pPredictBuffer, m_pPredictRaw, m_pLastLine, m_BitsPerComponent, m_Colors, m_Columns);
- FXSYS_memcpy32(m_pLastLine, m_pPredictBuffer, m_PredictPitch);
- } else {
- FPDFAPI_FlateOutput(m_pFlate, m_pPredictBuffer, m_PredictPitch);
- TIFF_PredictLine(m_pPredictBuffer, m_PredictPitch, m_BitsPerComponent, m_Colors, m_Columns);
- }
- int read_bytes = m_PredictPitch > bytes_to_go ? bytes_to_go : m_PredictPitch;
- FXSYS_memcpy32(m_pScanline + m_Pitch - bytes_to_go, m_pPredictBuffer, read_bytes);
- m_LeftOver += m_PredictPitch - read_bytes;
- bytes_to_go -= read_bytes;
- }
- }
- } else {
- FPDFAPI_FlateOutput(m_pFlate, m_pScanline, m_Pitch);
- }
- return m_pScanline;
-}
-FX_DWORD CCodec_FlateScanlineDecoder::GetSrcOffset()
-{
- return FPDFAPI_FlateGetTotalIn(m_pFlate);
-}
-static void FlateUncompress(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_DWORD orig_size,
- FX_LPBYTE& dest_buf, FX_DWORD& dest_size, FX_DWORD& offset)
-{
- FX_DWORD guess_size = orig_size ? orig_size : src_size * 2;
- FX_DWORD alloc_step = orig_size ? 10240 : (src_size < 10240 ? 10240 : src_size);
- static const FX_DWORD kMaxInitialAllocSize = 10000000;
- if (guess_size > kMaxInitialAllocSize) {
- guess_size = kMaxInitialAllocSize;
- alloc_step = kMaxInitialAllocSize;
- }
- FX_LPBYTE guess_buf = FX_Alloc(FX_BYTE, guess_size + 1);
- if (!guess_buf) {
- dest_buf = NULL;
- dest_size = 0;
- return;
- }
- guess_buf[guess_size] = '\0';
- FX_BOOL useOldImpl = src_size < 10240;
- void* context = FPDFAPI_FlateInit(my_alloc_func, my_free_func);
- if (context == NULL) {
- dest_buf = NULL;
- dest_size = 0;
- return ;
- }
- FPDFAPI_FlateInput(context, src_buf, src_size);
- CFX_ArrayTemplate<FX_LPBYTE> result_tmp_bufs;
- FX_LPBYTE buf = guess_buf;
- FX_DWORD buf_size = guess_size;
- FX_DWORD last_buf_size = buf_size;
- while (1) {
- FX_INT32 ret = FPDFAPI_FlateOutput(context, buf, buf_size);
- FX_INT32 avail_buf_size = FPDFAPI_FlateGetAvailOut(context);
- if (!useOldImpl) {
- if (ret != Z_OK) {
- last_buf_size = buf_size - avail_buf_size;
- result_tmp_bufs.Add(buf);
- break;
- }
- if (avail_buf_size == 0) {
- result_tmp_bufs.Add(buf);
- buf = NULL;
- buf = FX_Alloc(FX_BYTE, buf_size + 1);
- if (!buf) {
- dest_buf = NULL;
- dest_size = 0;
- return;
- }
- buf[buf_size] = '\0';
- } else {
- last_buf_size = buf_size - avail_buf_size;
- result_tmp_bufs.Add(buf);
- buf = NULL;
- break;
- }
- } else {
- if (ret != Z_OK) {
- break;
- }
- if (avail_buf_size == 0) {
- FX_DWORD old_size = guess_size;
- guess_size += alloc_step;
- if (guess_size < old_size || guess_size + 1 < guess_size) {
- dest_buf = NULL;
- dest_size = 0;
- return;
- }
- guess_buf = FX_Realloc(FX_BYTE, guess_buf, guess_size + 1);
- if (!guess_buf) {
- dest_buf = NULL;
- dest_size = 0;
- return;
- }
- guess_buf[guess_size] = '\0';
- buf = guess_buf + old_size;
- buf_size = guess_size - old_size;
- } else {
- break;
- }
- }
- }
- dest_size = FPDFAPI_FlateGetTotalOut(context);
- offset = FPDFAPI_FlateGetTotalIn(context);
- if (!useOldImpl) {
- if (result_tmp_bufs.GetSize() == 1) {
- dest_buf = result_tmp_bufs[0];
- } else {
- FX_LPBYTE result_buf = FX_Alloc(FX_BYTE, dest_size);
- if (!result_buf) {
- dest_buf = NULL;
- dest_size = 0;
- return;
- }
- FX_DWORD result_pos = 0;
- for (FX_INT32 i = 0; i < result_tmp_bufs.GetSize(); i++) {
- FX_LPBYTE tmp_buf = result_tmp_bufs[i];
- FX_DWORD tmp_buf_size = buf_size;
- if (i == result_tmp_bufs.GetSize() - 1) {
- tmp_buf_size = last_buf_size;
- }
- FXSYS_memcpy32(result_buf + result_pos, tmp_buf, tmp_buf_size);
- result_pos += tmp_buf_size;
- FX_Free(tmp_buf);
- tmp_buf = NULL;
- result_tmp_bufs[i] = NULL;
- }
- dest_buf = result_buf;
- }
- } else {
- if (guess_size / 2 > dest_size) {
- guess_buf = FX_Realloc(FX_BYTE, guess_buf, dest_size + 1);
- if (!guess_buf) {
- dest_buf = NULL;
- dest_size = 0;
- return;
- }
- guess_size = dest_size;
- guess_buf[guess_size] = '\0';
- }
- dest_buf = guess_buf;
- }
- FPDFAPI_FlateEnd(context);
- context = NULL;
-}
-ICodec_ScanlineDecoder* CCodec_FlateModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, int Columns)
-{
- CCodec_FlateScanlineDecoder* pDecoder = FX_NEW CCodec_FlateScanlineDecoder;
- if (pDecoder == NULL) {
- return NULL;
- }
- pDecoder->Create(src_buf, src_size, width, height, nComps, bpc, predictor, Colors, BitsPerComponent, Columns);
- return pDecoder;
-}
-FX_DWORD CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, const FX_BYTE* src_buf, FX_DWORD src_size, FX_BOOL bEarlyChange,
- int predictor, int Colors, int BitsPerComponent, int Columns,
- FX_DWORD estimated_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
-{
- CLZWDecoder* pDecoder = NULL;
- dest_buf = NULL;
- FX_DWORD offset = 0;
- int predictor_type = 0;
- if (predictor) {
- if (predictor >= 10) {
- predictor_type = 2;
- } else if (predictor == 2) {
- predictor_type = 1;
- }
- }
- if (bLZW) {
- pDecoder = FX_NEW CLZWDecoder;
- if (pDecoder == NULL) {
- return -1;
- }
- dest_size = (FX_DWORD) - 1;
- offset = src_size;
- int err = pDecoder->Decode(NULL, dest_size, src_buf, offset, bEarlyChange);
- delete pDecoder;
- if (err || dest_size == 0 || dest_size + 1 < dest_size) {
- return (FX_DWORD) - 1;
- }
- pDecoder = FX_NEW CLZWDecoder;
- if (pDecoder == NULL) {
- return -1;
- }
- dest_buf = FX_Alloc( FX_BYTE, dest_size + 1);
- if (dest_buf == NULL) {
- return -1;
- }
- dest_buf[dest_size] = '\0';
- pDecoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange);
- delete pDecoder;
- } else {
- FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size, offset);
- }
- if (predictor_type == 0) {
- return offset;
- }
- if (predictor_type == 2) {
- PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns);
- } else if (predictor_type == 1) {
- TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns);
- }
- return offset;
-}
-FX_BOOL CCodec_FlateModule::Encode(const FX_BYTE* src_buf, FX_DWORD src_size,
- int predictor, int Colors, int BitsPerComponent, int Columns,
- FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
-{
- if (predictor != 2 && predictor < 10) {
- return Encode(src_buf, src_size, dest_buf, dest_size);
- }
- FX_BOOL ret = FALSE;
- FX_LPBYTE pSrcBuf = NULL;
- pSrcBuf = FX_Alloc(FX_BYTE, src_size);
- if (pSrcBuf == NULL) {
- return FALSE;
- }
- FXSYS_memcpy32(pSrcBuf, src_buf, src_size);
- if (predictor == 2) {
- TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, Columns);
- } else if (predictor >= 10) {
- PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, BitsPerComponent, Columns);
- }
- ret = Encode(pSrcBuf, src_size, dest_buf, dest_size);
- FX_Free(pSrcBuf);
- return ret;
-}
-FX_BOOL CCodec_FlateModule::Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
-{
- dest_size = src_size + src_size / 1000 + 12;
- dest_buf = FX_Alloc( FX_BYTE, dest_size);
- if (dest_buf == NULL) {
- return FALSE;
- }
- unsigned long temp_size = dest_size;
- FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size);
- dest_size = (FX_DWORD)temp_size;
- return TRUE;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../fx_zlib.h"
+#include "../../../include/fxcodec/fx_codec.h"
+#include "codec_int.h"
+extern "C"
+{
+ static void* my_alloc_func (void* opaque, unsigned int items, unsigned int size)
+ {
+ return FX_Alloc(FX_BYTE, items * size);
+ }
+ static void my_free_func (void* opaque, void* address)
+ {
+ FX_Free(address);
+ }
+ void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned int),
+ void (*free_func)(void*, void*))
+ {
+ z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream));
+ if (p == NULL) {
+ return NULL;
+ }
+ FXSYS_memset32(p, 0, sizeof(z_stream));
+ p->zalloc = alloc_func;
+ p->zfree = free_func;
+ inflateInit(p);
+ return p;
+ }
+ void FPDFAPI_FlateInput(void* context, const unsigned char* src_buf, unsigned int src_size)
+ {
+ ((z_stream*)context)->next_in = (unsigned char*)src_buf;
+ ((z_stream*)context)->avail_in = src_size;
+ }
+ int FPDFAPI_FlateGetTotalOut(void* context)
+ {
+ return ((z_stream*)context)->total_out;
+ }
+ int FPDFAPI_FlateOutput(void* context, unsigned char* dest_buf, unsigned int dest_size)
+ {
+ ((z_stream*)context)->next_out = dest_buf;
+ ((z_stream*)context)->avail_out = dest_size;
+ unsigned int pre_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context);
+ int ret = inflate((z_stream*)context, Z_SYNC_FLUSH);
+ unsigned int post_pos = (unsigned int)FPDFAPI_FlateGetTotalOut(context);
+ unsigned int written = post_pos - pre_pos;
+ if (written < dest_size) {
+ FXSYS_memset8(dest_buf + written, '\0', dest_size - written);
+ }
+ return ret;
+ }
+ int FPDFAPI_FlateGetTotalIn(void* context)
+ {
+ return ((z_stream*)context)->total_in;
+ }
+ int FPDFAPI_FlateGetAvailOut(void* context)
+ {
+ return ((z_stream*)context)->avail_out;
+ }
+ int FPDFAPI_FlateGetAvailIn(void* context)
+ {
+ return ((z_stream*)context)->avail_in;
+ }
+ void FPDFAPI_FlateEnd(void* context)
+ {
+ inflateEnd((z_stream*)context);
+ ((z_stream*)context)->zfree(0, context);
+ }
+ void FPDFAPI_FlateCompress(unsigned char* dest_buf, unsigned long* dest_size, const unsigned char* src_buf, unsigned long src_size)
+ {
+ compress(dest_buf, dest_size, src_buf, src_size);
+ }
+}
+class CLZWDecoder : public CFX_Object
+{
+public:
+ FX_BOOL Decode(FX_LPBYTE output, FX_DWORD& outlen, const FX_BYTE* input, FX_DWORD& size, FX_BOOL bEarlyChange);
+private:
+ FX_DWORD m_InPos;
+ FX_DWORD m_OutPos;
+ FX_LPBYTE m_pOutput;
+ const FX_BYTE* m_pInput;
+ FX_BOOL m_Early;
+ void AddCode(FX_DWORD prefix_code, FX_BYTE append_char);
+ FX_DWORD m_CodeArray[5021];
+ FX_DWORD m_nCodes;
+ FX_BYTE m_DecodeStack[4000];
+ FX_DWORD m_StackLen;
+ void DecodeString(FX_DWORD code);
+ int m_CodeLen;
+};
+void CLZWDecoder::AddCode(FX_DWORD prefix_code, FX_BYTE append_char)
+{
+ if (m_nCodes + m_Early == 4094) {
+ return;
+ }
+ m_CodeArray[m_nCodes ++] = (prefix_code << 16) | append_char;
+ if (m_nCodes + m_Early == 512 - 258) {
+ m_CodeLen = 10;
+ } else if (m_nCodes + m_Early == 1024 - 258) {
+ m_CodeLen = 11;
+ } else if (m_nCodes + m_Early == 2048 - 258) {
+ m_CodeLen = 12;
+ }
+}
+void CLZWDecoder::DecodeString(FX_DWORD code)
+{
+ while (1) {
+ int index = code - 258;
+ if (index < 0 || index >= (int)m_nCodes) {
+ break;
+ }
+ FX_DWORD data = m_CodeArray[index];
+ if (m_StackLen >= sizeof(m_DecodeStack)) {
+ return;
+ }
+ m_DecodeStack[m_StackLen++] = (FX_BYTE)data;
+ code = data >> 16;
+ }
+ if (m_StackLen >= sizeof(m_DecodeStack)) {
+ return;
+ }
+ m_DecodeStack[m_StackLen++] = (FX_BYTE)code;
+}
+int CLZWDecoder::Decode(FX_LPBYTE dest_buf, FX_DWORD& dest_size, const FX_BYTE* src_buf, FX_DWORD& src_size, FX_BOOL bEarlyChange)
+{
+ m_CodeLen = 9;
+ m_InPos = 0;
+ m_OutPos = 0;
+ m_pInput = src_buf;
+ m_pOutput = dest_buf;
+ m_Early = bEarlyChange ? 1 : 0;
+ m_nCodes = 0;
+ FX_DWORD old_code = (FX_DWORD) - 1;
+ FX_BYTE last_char;
+ while (1) {
+ if (m_InPos + m_CodeLen > src_size * 8) {
+ break;
+ }
+ int byte_pos = m_InPos / 8;
+ int bit_pos = m_InPos % 8, bit_left = m_CodeLen;
+ FX_DWORD code = 0;
+ if (bit_pos) {
+ bit_left -= 8 - bit_pos;
+ code = (m_pInput[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left;
+ }
+ if (bit_left < 8) {
+ code |= m_pInput[byte_pos] >> (8 - bit_left);
+ } else {
+ bit_left -= 8;
+ code |= m_pInput[byte_pos++] << bit_left;
+ if (bit_left) {
+ code |= m_pInput[byte_pos] >> (8 - bit_left);
+ }
+ }
+ m_InPos += m_CodeLen;
+ if (code < 256) {
+ if (m_OutPos == dest_size) {
+ return -5;
+ }
+ if (m_pOutput) {
+ m_pOutput[m_OutPos] = (FX_BYTE)code;
+ }
+ m_OutPos ++;
+ last_char = (FX_BYTE)code;
+ if (old_code != (FX_DWORD) - 1) {
+ AddCode(old_code, last_char);
+ }
+ old_code = code;
+ } else if (code == 256) {
+ m_CodeLen = 9;
+ m_nCodes = 0;
+ old_code = (FX_DWORD) - 1;
+ } else if (code == 257) {
+ break;
+ } else {
+ if (old_code == (FX_DWORD) - 1) {
+ return 2;
+ }
+ m_StackLen = 0;
+ if (code >= m_nCodes + 258) {
+ if (m_StackLen < sizeof(m_DecodeStack)) {
+ m_DecodeStack[m_StackLen++] = last_char;
+ }
+ DecodeString(old_code);
+ } else {
+ DecodeString(code);
+ }
+ if (m_OutPos + m_StackLen > dest_size) {
+ return -5;
+ }
+ if (m_pOutput) {
+ for (FX_DWORD i = 0; i < m_StackLen; i ++) {
+ m_pOutput[m_OutPos + i] = m_DecodeStack[m_StackLen - i - 1];
+ }
+ }
+ m_OutPos += m_StackLen;
+ last_char = m_DecodeStack[m_StackLen - 1];
+ if (old_code < 256) {
+ AddCode(old_code, last_char);
+ } else if (old_code - 258 >= m_nCodes) {
+ dest_size = m_OutPos;
+ src_size = (m_InPos + 7) / 8;
+ return 0;
+ } else {
+ AddCode(old_code, last_char);
+ }
+ old_code = code;
+ }
+ }
+ dest_size = m_OutPos;
+ src_size = (m_InPos + 7) / 8;
+ return 0;
+}
+static FX_BYTE PaethPredictor(int a, int b, int c)
+{
+ int p = a + b - c;
+ int pa = FXSYS_abs(p - a);
+ int pb = FXSYS_abs(p - b);
+ int pc = FXSYS_abs(p - c);
+ if (pa <= pb && pa <= pc) {
+ return (FX_BYTE)a;
+ }
+ if (pb <= pc) {
+ return (FX_BYTE)b;
+ }
+ return (FX_BYTE)c;
+}
+static void PNG_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size, int predictor, int Colors, int BitsPerComponent, int Columns)
+{
+ int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8;
+ int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
+ int row_count = (data_size + row_size - 1) / row_size;
+ int last_row_size = data_size % row_size;
+ FX_LPBYTE dest_buf = FX_Alloc( FX_BYTE, (row_size + 1) * row_count);
+ if (dest_buf == NULL) {
+ return;
+ }
+ int byte_cnt = 0;
+ FX_LPBYTE pSrcData = data_buf;
+ FX_LPBYTE pDestData = dest_buf;
+ for (int row = 0; row < row_count; row++) {
+ if (predictor == 10) {
+ pDestData[0] = 0;
+ int move_size = row_size;
+ if (move_size * (row + 1) > (int)data_size) {
+ move_size = data_size - (move_size * row);
+ }
+ FXSYS_memmove32(pDestData + 1, pSrcData, move_size);
+ pDestData += (move_size + 1);
+ pSrcData += move_size;
+ byte_cnt += move_size;
+ continue;
+ }
+ for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte++) {
+ switch (predictor) {
+ case 11: {
+ pDestData[0] = 1;
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pSrcData[byte - BytesPerPixel];
+ }
+ pDestData[byte + 1] = pSrcData[byte] - left;
+ }
+ break;
+ case 12: {
+ pDestData[0] = 2;
+ FX_BYTE up = 0;
+ if (row) {
+ up = pSrcData[byte - row_size];
+ }
+ pDestData[byte + 1] = pSrcData[byte] - up;
+ }
+ break;
+ case 13: {
+ pDestData[0] = 3;
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pSrcData[byte - BytesPerPixel];
+ }
+ FX_BYTE up = 0;
+ if (row) {
+ up = pSrcData[byte - row_size];
+ }
+ pDestData[byte + 1] = pSrcData[byte] - (left + up) / 2;
+ }
+ break;
+ case 14: {
+ pDestData[0] = 4;
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pSrcData[byte - BytesPerPixel];
+ }
+ FX_BYTE up = 0;
+ if (row) {
+ up = pSrcData[byte - row_size];
+ }
+ FX_BYTE upper_left = 0;
+ if (byte >= BytesPerPixel && row) {
+ upper_left = pSrcData[byte - row_size - BytesPerPixel];
+ }
+ pDestData[byte + 1] = pSrcData[byte] - PaethPredictor(left, up, upper_left);
+ }
+ break;
+ default: {
+ pDestData[byte + 1] = pSrcData[byte];
+ }
+ break;
+ }
+ byte_cnt++;
+ }
+ pDestData += (row_size + 1);
+ pSrcData += row_size;
+ }
+ FX_Free(data_buf);
+ data_buf = dest_buf;
+ data_size = (row_size + 1) * row_count - (last_row_size > 0 ? (row_size - last_row_size) : 0);
+}
+static void PNG_PredictLine(FX_LPBYTE pDestData, FX_LPCBYTE pSrcData, FX_LPCBYTE pLastLine,
+ int bpc, int nColors, int nPixels)
+{
+ int row_size = (nPixels * bpc * nColors + 7) / 8;
+ int BytesPerPixel = (bpc * nColors + 7) / 8;
+ FX_BYTE tag = pSrcData[0];
+ if (tag == 0) {
+ FXSYS_memmove32(pDestData, pSrcData + 1, row_size);
+ return;
+ }
+ for (int byte = 0; byte < row_size; byte ++) {
+ FX_BYTE raw_byte = pSrcData[byte + 1];
+ switch (tag) {
+ case 1: {
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pDestData[byte - BytesPerPixel];
+ }
+ pDestData[byte] = raw_byte + left;
+ break;
+ }
+ case 2: {
+ FX_BYTE up = 0;
+ if (pLastLine) {
+ up = pLastLine[byte];
+ }
+ pDestData[byte] = raw_byte + up;
+ break;
+ }
+ case 3: {
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pDestData[byte - BytesPerPixel];
+ }
+ FX_BYTE up = 0;
+ if (pLastLine) {
+ up = pLastLine[byte];
+ }
+ pDestData[byte] = raw_byte + (up + left) / 2;
+ break;
+ }
+ case 4: {
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pDestData[byte - BytesPerPixel];
+ }
+ FX_BYTE up = 0;
+ if (pLastLine) {
+ up = pLastLine[byte];
+ }
+ FX_BYTE upper_left = 0;
+ if (byte >= BytesPerPixel && pLastLine) {
+ upper_left = pLastLine[byte - BytesPerPixel];
+ }
+ pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left);
+ break;
+ }
+ default:
+ pDestData[byte] = raw_byte;
+ break;
+ }
+ }
+}
+static void PNG_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size,
+ int Colors, int BitsPerComponent, int Columns)
+{
+ int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8;
+ int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
+ int row_count = (data_size + row_size) / (row_size + 1);
+ int last_row_size = data_size % (row_size + 1);
+ FX_LPBYTE dest_buf = FX_Alloc( FX_BYTE, row_size * row_count);
+ if (dest_buf == NULL) {
+ return;
+ }
+ int byte_cnt = 0;
+ FX_LPBYTE pSrcData = data_buf;
+ FX_LPBYTE pDestData = dest_buf;
+ for (int row = 0; row < row_count; row ++) {
+ FX_BYTE tag = pSrcData[0];
+ if (tag == 0) {
+ int move_size = row_size;
+ if ((row + 1) * (move_size + 1) > (int)data_size) {
+ move_size = last_row_size - 1;
+ }
+ FXSYS_memmove32(pDestData, pSrcData + 1, move_size);
+ pSrcData += move_size + 1;
+ pDestData += move_size;
+ byte_cnt += move_size + 1;
+ continue;
+ }
+ for (int byte = 0; byte < row_size && byte_cnt < (int)data_size; byte ++) {
+ FX_BYTE raw_byte = pSrcData[byte + 1];
+ switch (tag) {
+ case 1: {
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pDestData[byte - BytesPerPixel];
+ }
+ pDestData[byte] = raw_byte + left;
+ break;
+ }
+ case 2: {
+ FX_BYTE up = 0;
+ if (row) {
+ up = pDestData[byte - row_size];
+ }
+ pDestData[byte] = raw_byte + up;
+ break;
+ }
+ case 3: {
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pDestData[byte - BytesPerPixel];
+ }
+ FX_BYTE up = 0;
+ if (row) {
+ up = pDestData[byte - row_size];
+ }
+ pDestData[byte] = raw_byte + (up + left) / 2;
+ break;
+ }
+ case 4: {
+ FX_BYTE left = 0;
+ if (byte >= BytesPerPixel) {
+ left = pDestData[byte - BytesPerPixel];
+ }
+ FX_BYTE up = 0;
+ if (row) {
+ up = pDestData[byte - row_size];
+ }
+ FX_BYTE upper_left = 0;
+ if (byte >= BytesPerPixel && row) {
+ upper_left = pDestData[byte - row_size - BytesPerPixel];
+ }
+ pDestData[byte] = raw_byte + PaethPredictor(left, up, upper_left);
+ break;
+ }
+ default:
+ pDestData[byte] = raw_byte;
+ break;
+ }
+ byte_cnt++;
+ }
+ pSrcData += row_size + 1;
+ pDestData += row_size;
+ byte_cnt++;
+ }
+ FX_Free(data_buf);
+ data_buf = dest_buf;
+ data_size = row_size * row_count - (last_row_size > 0 ? (row_size + 1 - last_row_size) : 0);
+}
+static void TIFF_PredictorEncodeLine(FX_LPBYTE dest_buf, int row_size, int BitsPerComponent, int Colors, int Columns)
+{
+ int BytesPerPixel = BitsPerComponent * Colors / 8;
+ if (BitsPerComponent < 8) {
+ FX_BYTE mask = 0x01;
+ if (BitsPerComponent == 2) {
+ mask = 0x03;
+ } else if (BitsPerComponent == 4) {
+ mask = 0x0F;
+ }
+ int row_bits = Colors * BitsPerComponent * Columns;
+ for (int i = row_bits - BitsPerComponent; i >= BitsPerComponent; i -= BitsPerComponent) {
+ int col = i % 8;
+ int index = i / 8;
+ int col_pre = (col == 0) ? (8 - BitsPerComponent) : (col - BitsPerComponent);
+ int index_pre = (col == 0) ? (index - 1) : index;
+ FX_BYTE cur = (dest_buf[index] >> (8 - col - BitsPerComponent)) & mask;
+ FX_BYTE left = (dest_buf[index_pre] >> (8 - col_pre - BitsPerComponent)) & mask;
+ cur -= left;
+ cur &= mask;
+ cur <<= (8 - col - BitsPerComponent);
+ dest_buf[index] &= ~(mask << ((8 - col - BitsPerComponent)));
+ dest_buf[index] |= cur;
+ }
+ } else if (BitsPerComponent == 8) {
+ for (int i = row_size - 1; i >= BytesPerPixel; i--) {
+ dest_buf[i] -= dest_buf[i - BytesPerPixel];
+ }
+ } else {
+ for (int i = row_size - BytesPerPixel; i >= BytesPerPixel; i -= BytesPerPixel) {
+ FX_WORD pixel = (dest_buf[i] << 8) | dest_buf[i + 1];
+ pixel -= (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1];
+ dest_buf[i] = pixel >> 8;
+ dest_buf[i + 1] = (FX_BYTE)pixel;
+ }
+ }
+}
+static void TIFF_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size,
+ int Colors, int BitsPerComponent, int Columns)
+{
+ int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
+ int row_count = (data_size + row_size - 1) / row_size;
+ int last_row_size = data_size % row_size;
+ for (int row = 0; row < row_count; row++) {
+ FX_LPBYTE scan_line = data_buf + row * row_size;
+ if ((row + 1) * row_size > (int)data_size) {
+ row_size = last_row_size;
+ }
+ TIFF_PredictorEncodeLine(scan_line, row_size, BitsPerComponent, Colors, Columns);
+ }
+}
+static void TIFF_PredictLine(FX_LPBYTE dest_buf, int row_size, int BitsPerComponent, int Colors, int Columns)
+{
+ if (BitsPerComponent == 1) {
+ int row_bits = BitsPerComponent * Colors * Columns;
+ for(int i = 1; i < row_bits; i ++) {
+ int col = i % 8;
+ int index = i / 8;
+ int index_pre = (col == 0) ? (index - 1) : index;
+ int col_pre = (col == 0) ? 8 : col;
+ if( ((dest_buf[index] >> (7 - col)) & 1) ^ ((dest_buf[index_pre] >> (8 - col_pre)) & 1) ) {
+ dest_buf[index] |= 1 << (7 - col);
+ } else {
+ dest_buf[index] &= ~(1 << (7 - col));
+ }
+ }
+ return;
+ }
+ int BytesPerPixel = BitsPerComponent * Colors / 8;
+ if (BitsPerComponent == 16) {
+ for (int i = BytesPerPixel; i < row_size; i += 2) {
+ FX_WORD pixel = (dest_buf[i - BytesPerPixel] << 8) | dest_buf[i - BytesPerPixel + 1];
+ pixel += (dest_buf[i] << 8) | dest_buf[i + 1];
+ dest_buf[i] = pixel >> 8;
+ dest_buf[i + 1] = (FX_BYTE)pixel;
+ }
+ } else {
+ for (int i = BytesPerPixel; i < row_size; i ++) {
+ dest_buf[i] += dest_buf[i - BytesPerPixel];
+ }
+ }
+}
+static void TIFF_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size,
+ int Colors, int BitsPerComponent, int Columns)
+{
+ int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
+ int row_count = (data_size + row_size - 1) / row_size;
+ int last_row_size = data_size % row_size;
+ for (int row = 0; row < row_count; row ++) {
+ FX_LPBYTE scan_line = data_buf + row * row_size;
+ if ((row + 1) * row_size > (int)data_size) {
+ row_size = last_row_size;
+ }
+ TIFF_PredictLine(scan_line, row_size, BitsPerComponent, Colors, Columns);
+ }
+}
+class CCodec_FlateScanlineDecoder : public CCodec_ScanlineDecoder
+{
+public:
+ CCodec_FlateScanlineDecoder();
+ ~CCodec_FlateScanlineDecoder();
+ FX_BOOL Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps, int bpc,
+ int predictor, int Colors, int BitsPerComponent, int Columns);
+ virtual void Destroy()
+ {
+ delete this;
+ }
+ virtual void v_DownScale(int dest_width, int dest_height) {}
+ virtual FX_BOOL v_Rewind();
+ virtual FX_LPBYTE v_GetNextLine();
+ virtual FX_DWORD GetSrcOffset();
+ void* m_pFlate;
+ FX_LPCBYTE m_SrcBuf;
+ FX_DWORD m_SrcSize;
+ FX_LPBYTE m_pScanline;
+ FX_LPBYTE m_pLastLine;
+ FX_LPBYTE m_pPredictBuffer;
+ FX_LPBYTE m_pPredictRaw;
+ int m_Predictor;
+ int m_Colors, m_BitsPerComponent, m_Columns, m_PredictPitch, m_LeftOver;
+};
+CCodec_FlateScanlineDecoder::CCodec_FlateScanlineDecoder()
+{
+ m_pFlate = NULL;
+ m_pScanline = NULL;
+ m_pLastLine = NULL;
+ m_pPredictBuffer = NULL;
+ m_pPredictRaw = NULL;
+ m_LeftOver = 0;
+}
+CCodec_FlateScanlineDecoder::~CCodec_FlateScanlineDecoder()
+{
+ if (m_pScanline) {
+ FX_Free(m_pScanline);
+ }
+ if (m_pLastLine) {
+ FX_Free(m_pLastLine);
+ }
+ if (m_pPredictBuffer) {
+ FX_Free(m_pPredictBuffer);
+ }
+ if (m_pPredictRaw) {
+ FX_Free(m_pPredictRaw);
+ }
+ if (m_pFlate) {
+ FPDFAPI_FlateEnd(m_pFlate);
+ }
+}
+FX_BOOL CCodec_FlateScanlineDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, int Columns)
+{
+ m_SrcBuf = src_buf;
+ m_SrcSize = src_size;
+ m_OutputWidth = m_OrigWidth = width;
+ m_OutputHeight = m_OrigHeight = height;
+ m_nComps = nComps;
+ m_bpc = bpc;
+ m_bColorTransformed = FALSE;
+ m_Pitch = (width * nComps * bpc + 7) / 8;
+ m_pScanline = FX_Alloc(FX_BYTE, m_Pitch);
+ if (m_pScanline == NULL) {
+ return FALSE;
+ }
+ m_Predictor = 0;
+ if (predictor) {
+ if (predictor >= 10) {
+ m_Predictor = 2;
+ } else if (predictor == 2) {
+ m_Predictor = 1;
+ }
+ if (m_Predictor) {
+ if (BitsPerComponent * Colors * Columns == 0) {
+ BitsPerComponent = m_bpc;
+ Colors = m_nComps;
+ Columns = m_OrigWidth;
+ }
+ m_Colors = Colors;
+ m_BitsPerComponent = BitsPerComponent;
+ m_Columns = Columns;
+ m_PredictPitch = (m_BitsPerComponent * m_Colors * m_Columns + 7) / 8;
+ m_pLastLine = FX_Alloc(FX_BYTE, m_PredictPitch);
+ if (m_pLastLine == NULL) {
+ return FALSE;
+ }
+ FXSYS_memset32(m_pLastLine, 0, m_PredictPitch);
+ m_pPredictRaw = FX_Alloc(FX_BYTE, m_PredictPitch + 1);
+ if (m_pPredictRaw == NULL) {
+ return FALSE;
+ }
+ m_pPredictBuffer = FX_Alloc(FX_BYTE, m_PredictPitch);
+ if (m_pPredictBuffer == NULL) {
+ return FALSE;
+ }
+ }
+ }
+ return TRUE;
+}
+FX_BOOL CCodec_FlateScanlineDecoder::v_Rewind()
+{
+ if (m_pFlate) {
+ FPDFAPI_FlateEnd(m_pFlate);
+ }
+ m_pFlate = FPDFAPI_FlateInit(my_alloc_func, my_free_func);
+ if (m_pFlate == NULL) {
+ return FALSE;
+ }
+ FPDFAPI_FlateInput(m_pFlate, m_SrcBuf, m_SrcSize);
+ m_LeftOver = 0;
+ return TRUE;
+}
+FX_LPBYTE CCodec_FlateScanlineDecoder::v_GetNextLine()
+{
+ if (m_Predictor) {
+ if (m_Pitch == m_PredictPitch) {
+ if (m_Predictor == 2) {
+ FPDFAPI_FlateOutput(m_pFlate, m_pPredictRaw, m_PredictPitch + 1);
+ PNG_PredictLine(m_pScanline, m_pPredictRaw, m_pLastLine, m_BitsPerComponent, m_Colors, m_Columns);
+ FXSYS_memcpy32(m_pLastLine, m_pScanline, m_PredictPitch);
+ } else {
+ FPDFAPI_FlateOutput(m_pFlate, m_pScanline, m_Pitch);
+ TIFF_PredictLine(m_pScanline, m_PredictPitch, m_bpc, m_nComps, m_OutputWidth);
+ }
+ } else {
+ int bytes_to_go = m_Pitch;
+ int read_leftover = m_LeftOver > bytes_to_go ? bytes_to_go : m_LeftOver;
+ if (read_leftover) {
+ FXSYS_memcpy32(m_pScanline, m_pPredictBuffer + m_PredictPitch - m_LeftOver, read_leftover);
+ m_LeftOver -= read_leftover;
+ bytes_to_go -= read_leftover;
+ }
+ while (bytes_to_go) {
+ if (m_Predictor == 2) {
+ FPDFAPI_FlateOutput(m_pFlate, m_pPredictRaw, m_PredictPitch + 1);
+ PNG_PredictLine(m_pPredictBuffer, m_pPredictRaw, m_pLastLine, m_BitsPerComponent, m_Colors, m_Columns);
+ FXSYS_memcpy32(m_pLastLine, m_pPredictBuffer, m_PredictPitch);
+ } else {
+ FPDFAPI_FlateOutput(m_pFlate, m_pPredictBuffer, m_PredictPitch);
+ TIFF_PredictLine(m_pPredictBuffer, m_PredictPitch, m_BitsPerComponent, m_Colors, m_Columns);
+ }
+ int read_bytes = m_PredictPitch > bytes_to_go ? bytes_to_go : m_PredictPitch;
+ FXSYS_memcpy32(m_pScanline + m_Pitch - bytes_to_go, m_pPredictBuffer, read_bytes);
+ m_LeftOver += m_PredictPitch - read_bytes;
+ bytes_to_go -= read_bytes;
+ }
+ }
+ } else {
+ FPDFAPI_FlateOutput(m_pFlate, m_pScanline, m_Pitch);
+ }
+ return m_pScanline;
+}
+FX_DWORD CCodec_FlateScanlineDecoder::GetSrcOffset()
+{
+ return FPDFAPI_FlateGetTotalIn(m_pFlate);
+}
+static void FlateUncompress(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_DWORD orig_size,
+ FX_LPBYTE& dest_buf, FX_DWORD& dest_size, FX_DWORD& offset)
+{
+ FX_DWORD guess_size = orig_size ? orig_size : src_size * 2;
+ FX_DWORD alloc_step = orig_size ? 10240 : (src_size < 10240 ? 10240 : src_size);
+ static const FX_DWORD kMaxInitialAllocSize = 10000000;
+ if (guess_size > kMaxInitialAllocSize) {
+ guess_size = kMaxInitialAllocSize;
+ alloc_step = kMaxInitialAllocSize;
+ }
+ FX_LPBYTE guess_buf = FX_Alloc(FX_BYTE, guess_size + 1);
+ if (!guess_buf) {
+ dest_buf = NULL;
+ dest_size = 0;
+ return;
+ }
+ guess_buf[guess_size] = '\0';
+ FX_BOOL useOldImpl = src_size < 10240;
+ void* context = FPDFAPI_FlateInit(my_alloc_func, my_free_func);
+ if (context == NULL) {
+ dest_buf = NULL;
+ dest_size = 0;
+ return ;
+ }
+ FPDFAPI_FlateInput(context, src_buf, src_size);
+ CFX_ArrayTemplate<FX_LPBYTE> result_tmp_bufs;
+ FX_LPBYTE buf = guess_buf;
+ FX_DWORD buf_size = guess_size;
+ FX_DWORD last_buf_size = buf_size;
+ while (1) {
+ FX_INT32 ret = FPDFAPI_FlateOutput(context, buf, buf_size);
+ FX_INT32 avail_buf_size = FPDFAPI_FlateGetAvailOut(context);
+ if (!useOldImpl) {
+ if (ret != Z_OK) {
+ last_buf_size = buf_size - avail_buf_size;
+ result_tmp_bufs.Add(buf);
+ break;
+ }
+ if (avail_buf_size == 0) {
+ result_tmp_bufs.Add(buf);
+ buf = NULL;
+ buf = FX_Alloc(FX_BYTE, buf_size + 1);
+ if (!buf) {
+ dest_buf = NULL;
+ dest_size = 0;
+ return;
+ }
+ buf[buf_size] = '\0';
+ } else {
+ last_buf_size = buf_size - avail_buf_size;
+ result_tmp_bufs.Add(buf);
+ buf = NULL;
+ break;
+ }
+ } else {
+ if (ret != Z_OK) {
+ break;
+ }
+ if (avail_buf_size == 0) {
+ FX_DWORD old_size = guess_size;
+ guess_size += alloc_step;
+ if (guess_size < old_size || guess_size + 1 < guess_size) {
+ dest_buf = NULL;
+ dest_size = 0;
+ return;
+ }
+ guess_buf = FX_Realloc(FX_BYTE, guess_buf, guess_size + 1);
+ if (!guess_buf) {
+ dest_buf = NULL;
+ dest_size = 0;
+ return;
+ }
+ guess_buf[guess_size] = '\0';
+ buf = guess_buf + old_size;
+ buf_size = guess_size - old_size;
+ } else {
+ break;
+ }
+ }
+ }
+ dest_size = FPDFAPI_FlateGetTotalOut(context);
+ offset = FPDFAPI_FlateGetTotalIn(context);
+ if (!useOldImpl) {
+ if (result_tmp_bufs.GetSize() == 1) {
+ dest_buf = result_tmp_bufs[0];
+ } else {
+ FX_LPBYTE result_buf = FX_Alloc(FX_BYTE, dest_size);
+ if (!result_buf) {
+ dest_buf = NULL;
+ dest_size = 0;
+ return;
+ }
+ FX_DWORD result_pos = 0;
+ for (FX_INT32 i = 0; i < result_tmp_bufs.GetSize(); i++) {
+ FX_LPBYTE tmp_buf = result_tmp_bufs[i];
+ FX_DWORD tmp_buf_size = buf_size;
+ if (i == result_tmp_bufs.GetSize() - 1) {
+ tmp_buf_size = last_buf_size;
+ }
+ FXSYS_memcpy32(result_buf + result_pos, tmp_buf, tmp_buf_size);
+ result_pos += tmp_buf_size;
+ FX_Free(tmp_buf);
+ tmp_buf = NULL;
+ result_tmp_bufs[i] = NULL;
+ }
+ dest_buf = result_buf;
+ }
+ } else {
+ if (guess_size / 2 > dest_size) {
+ guess_buf = FX_Realloc(FX_BYTE, guess_buf, dest_size + 1);
+ if (!guess_buf) {
+ dest_buf = NULL;
+ dest_size = 0;
+ return;
+ }
+ guess_size = dest_size;
+ guess_buf[guess_size] = '\0';
+ }
+ dest_buf = guess_buf;
+ }
+ FPDFAPI_FlateEnd(context);
+ context = NULL;
+}
+ICodec_ScanlineDecoder* CCodec_FlateModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int nComps, int bpc, int predictor, int Colors, int BitsPerComponent, int Columns)
+{
+ CCodec_FlateScanlineDecoder* pDecoder = FX_NEW CCodec_FlateScanlineDecoder;
+ if (pDecoder == NULL) {
+ return NULL;
+ }
+ pDecoder->Create(src_buf, src_size, width, height, nComps, bpc, predictor, Colors, BitsPerComponent, Columns);
+ return pDecoder;
+}
+FX_DWORD CCodec_FlateModule::FlateOrLZWDecode(FX_BOOL bLZW, const FX_BYTE* src_buf, FX_DWORD src_size, FX_BOOL bEarlyChange,
+ int predictor, int Colors, int BitsPerComponent, int Columns,
+ FX_DWORD estimated_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
+{
+ CLZWDecoder* pDecoder = NULL;
+ dest_buf = NULL;
+ FX_DWORD offset = 0;
+ int predictor_type = 0;
+ if (predictor) {
+ if (predictor >= 10) {
+ predictor_type = 2;
+ } else if (predictor == 2) {
+ predictor_type = 1;
+ }
+ }
+ if (bLZW) {
+ pDecoder = FX_NEW CLZWDecoder;
+ if (pDecoder == NULL) {
+ return -1;
+ }
+ dest_size = (FX_DWORD) - 1;
+ offset = src_size;
+ int err = pDecoder->Decode(NULL, dest_size, src_buf, offset, bEarlyChange);
+ delete pDecoder;
+ if (err || dest_size == 0 || dest_size + 1 < dest_size) {
+ return (FX_DWORD) - 1;
+ }
+ pDecoder = FX_NEW CLZWDecoder;
+ if (pDecoder == NULL) {
+ return -1;
+ }
+ dest_buf = FX_Alloc( FX_BYTE, dest_size + 1);
+ if (dest_buf == NULL) {
+ return -1;
+ }
+ dest_buf[dest_size] = '\0';
+ pDecoder->Decode(dest_buf, dest_size, src_buf, offset, bEarlyChange);
+ delete pDecoder;
+ } else {
+ FlateUncompress(src_buf, src_size, estimated_size, dest_buf, dest_size, offset);
+ }
+ if (predictor_type == 0) {
+ return offset;
+ }
+ if (predictor_type == 2) {
+ PNG_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns);
+ } else if (predictor_type == 1) {
+ TIFF_Predictor(dest_buf, dest_size, Colors, BitsPerComponent, Columns);
+ }
+ return offset;
+}
+FX_BOOL CCodec_FlateModule::Encode(const FX_BYTE* src_buf, FX_DWORD src_size,
+ int predictor, int Colors, int BitsPerComponent, int Columns,
+ FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
+{
+ if (predictor != 2 && predictor < 10) {
+ return Encode(src_buf, src_size, dest_buf, dest_size);
+ }
+ FX_BOOL ret = FALSE;
+ FX_LPBYTE pSrcBuf = NULL;
+ pSrcBuf = FX_Alloc(FX_BYTE, src_size);
+ if (pSrcBuf == NULL) {
+ return FALSE;
+ }
+ FXSYS_memcpy32(pSrcBuf, src_buf, src_size);
+ if (predictor == 2) {
+ TIFF_PredictorEncode(pSrcBuf, src_size, Colors, BitsPerComponent, Columns);
+ } else if (predictor >= 10) {
+ PNG_PredictorEncode(pSrcBuf, src_size, predictor, Colors, BitsPerComponent, Columns);
+ }
+ ret = Encode(pSrcBuf, src_size, dest_buf, dest_size);
+ FX_Free(pSrcBuf);
+ return ret;
+}
+FX_BOOL CCodec_FlateModule::Encode(FX_LPCBYTE src_buf, FX_DWORD src_size, FX_LPBYTE& dest_buf, FX_DWORD& dest_size)
+{
+ dest_size = src_size + src_size / 1000 + 12;
+ dest_buf = FX_Alloc( FX_BYTE, dest_size);
+ if (dest_buf == NULL) {
+ return FALSE;
+ }
+ unsigned long temp_size = dest_size;
+ FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size);
+ dest_size = (FX_DWORD)temp_size;
+ return TRUE;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_icc.cpp b/core/src/fxcodec/codec/fx_codec_icc.cpp
index 65617642fe..22659ba9ff 100644
--- a/core/src/fxcodec/codec/fx_codec_icc.cpp
+++ b/core/src/fxcodec/codec/fx_codec_icc.cpp
@@ -1,1360 +1,1360 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../../include/fxcodec/fx_codec.h"
-#include "codec_int.h"
-#include "../lcms2/include/fx_lcms2.h"
-FX_BOOL MD5ComputeID( FX_LPCVOID buf, FX_DWORD dwSize, FX_BYTE ID[16] )
-{
- return cmsMD5computeIDExt(buf, dwSize, ID);
-}
-struct CLcmsCmm : public CFX_Object {
- cmsHTRANSFORM m_hTransform;
- int m_nSrcComponents;
- int m_nDstComponents;
- FX_BOOL m_bLab;
-};
-extern "C" {
- int ourHandler(int ErrorCode, const char *ErrorText)
- {
- return TRUE;
- }
-};
-FX_BOOL CheckComponents(cmsColorSpaceSignature cs, int nComponents, FX_BOOL bDst)
-{
- if (nComponents <= 0 || nComponents > 15) {
- return FALSE;
- }
- switch(cs) {
- case cmsSigLabData:
- if (nComponents < 3) {
- return FALSE;
- }
- break;
- case cmsSigGrayData:
- if (bDst && nComponents != 1) {
- return FALSE;
- } else if (!bDst && nComponents > 2) {
- return FALSE;
- }
- break;
- case cmsSigRgbData:
- if (bDst && nComponents != 3) {
- return FALSE;
- }
- break;
- case cmsSigCmykData:
- if (bDst && nComponents != 4) {
- return FALSE;
- }
- break;
- default:
- if (nComponents != 3) {
- return FALSE;
- }
- break;
- }
- return TRUE;
-}
-void* IccLib_CreateTransform(const unsigned char* pSrcProfileData, unsigned int dwSrcProfileSize, int nSrcComponents,
- const unsigned char* pDstProfileData, unsigned int dwDstProfileSize, int nDstComponents,
- int intent, FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT, FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT)
-{
- cmsHPROFILE srcProfile = NULL;
- cmsHPROFILE dstProfile = NULL;
- cmsHTRANSFORM hTransform = NULL;
- CLcmsCmm* pCmm = NULL;
- srcProfile = cmsOpenProfileFromMem((void*)pSrcProfileData, dwSrcProfileSize);
- if (srcProfile == NULL) {
- return NULL;
- }
- if(pDstProfileData == NULL && dwDstProfileSize == 0 && nDstComponents == 3) {
- dstProfile = cmsCreate_sRGBProfile();
- } else {
- dstProfile = cmsOpenProfileFromMem((void*)pDstProfileData, dwDstProfileSize);
- }
- if (dstProfile == NULL) {
- cmsCloseProfile(srcProfile);
- return NULL;
- }
- int srcFormat;
- FX_BOOL bLab = FALSE;
- cmsColorSpaceSignature srcCS = cmsGetColorSpace(srcProfile);
- if (!CheckComponents(srcCS, nSrcComponents, FALSE)) {
- cmsCloseProfile(srcProfile);
- cmsCloseProfile(dstProfile);
- return NULL;
- }
- if (srcCS == cmsSigLabData) {
- srcFormat = COLORSPACE_SH(PT_Lab) | CHANNELS_SH(nSrcComponents) | BYTES_SH(0);
- bLab = TRUE;
- } else {
- srcFormat = COLORSPACE_SH(PT_ANY) | CHANNELS_SH(nSrcComponents) | BYTES_SH(1);
- if (srcCS == cmsSigRgbData && T_DOSWAP(dwSrcFormat)) {
- srcFormat |= DOSWAP_SH(1);
- }
- }
- cmsColorSpaceSignature dstCS = cmsGetColorSpace(dstProfile);
- if (!CheckComponents(dstCS, nDstComponents, TRUE)) {
- cmsCloseProfile(srcProfile);
- cmsCloseProfile(dstProfile);
- return NULL;
- }
- switch(dstCS) {
- case cmsSigGrayData:
- hTransform = cmsCreateTransform(srcProfile, srcFormat, dstProfile, TYPE_GRAY_8, intent, 0);
- break;
- case cmsSigRgbData:
- hTransform = cmsCreateTransform(srcProfile, srcFormat, dstProfile, TYPE_BGR_8, intent, 0);
- break;
- case cmsSigCmykData:
- hTransform = cmsCreateTransform(srcProfile, srcFormat, dstProfile,
- T_DOSWAP(dwDstFormat) ? TYPE_KYMC_8 : TYPE_CMYK_8,
- intent, 0);
- break;
- default:
- break;
- }
- if (hTransform == NULL) {
- cmsCloseProfile(srcProfile);
- cmsCloseProfile(dstProfile);
- return NULL;
- }
- pCmm = FX_NEW CLcmsCmm;
- if (pCmm == NULL) {
- return NULL;
- }
- pCmm->m_nSrcComponents = nSrcComponents;
- pCmm->m_nDstComponents = nDstComponents;
- pCmm->m_hTransform = hTransform;
- pCmm->m_bLab = bLab;
- cmsCloseProfile(srcProfile);
- cmsCloseProfile(dstProfile);
- return pCmm;
-}
-void* IccLib_CreateTransform_sRGB(const unsigned char* pProfileData, unsigned int dwProfileSize, int nComponents, int intent, FX_DWORD dwSrcFormat)
-{
- return IccLib_CreateTransform(pProfileData, dwProfileSize, nComponents, NULL, 0, 3, intent, dwSrcFormat);
-}
-void IccLib_DestroyTransform(void* pTransform)
-{
- if (pTransform == NULL) {
- return;
- }
- cmsDeleteTransform(((CLcmsCmm*)pTransform)->m_hTransform);
- delete (CLcmsCmm*)pTransform;
-}
-void IccLib_Translate(void* pTransform, FX_FLOAT* pSrcValues, FX_FLOAT* pDestValues)
-{
- if (pTransform == NULL) {
- return;
- }
- CLcmsCmm* p = (CLcmsCmm*)pTransform;
- FX_BYTE output[4];
- if (p->m_bLab) {
- CFX_FixedBufGrow<double, 16> inputs(p->m_nSrcComponents);
- double* input = inputs;
- for (int i = 0; i < p->m_nSrcComponents; i ++) {
- input[i] = pSrcValues[i];
- }
- cmsDoTransform(p->m_hTransform, input, output, 1);
- } else {
- CFX_FixedBufGrow<FX_BYTE, 16> inputs(p->m_nSrcComponents);
- FX_BYTE* input = inputs;
- for (int i = 0; i < p->m_nSrcComponents; i ++) {
- if (pSrcValues[i] > 1.0f) {
- input[i] = 255;
- } else if (pSrcValues[i] < 0) {
- input[i] = 0;
- } else {
- input[i] = (int)(pSrcValues[i] * 255.0f);
- }
- }
- cmsDoTransform(p->m_hTransform, input, output, 1);
- }
- switch(p->m_nDstComponents) {
- case 1:
- pDestValues[0] = output[0] / 255.0f;
- break;
- case 3:
- pDestValues[0] = output[2] / 255.0f;
- pDestValues[1] = output[1] / 255.0f;
- pDestValues[2] = output[0] / 255.0f;
- break;
- case 4:
- pDestValues[0] = output[0] / 255.0f;
- pDestValues[1] = output[1] / 255.0f;
- pDestValues[2] = output[2] / 255.0f;
- pDestValues[3] = output[3] / 255.0f;
- break;
- }
-}
-void IccLib_TranslateImage(void* pTransform, unsigned char* pDest, const unsigned char* pSrc, int pixels)
-{
- cmsDoTransform(((CLcmsCmm*)pTransform)->m_hTransform, (void*)pSrc, pDest, pixels);
-}
-FX_LPVOID CreateProfile_Gray(double gamma)
-{
- cmsCIExyY* D50 = (cmsCIExyY*)cmsD50_xyY();
- if (!cmsWhitePointFromTemp(D50, 6504)) {
- return NULL;
- }
- cmsToneCurve* curve = cmsBuildGamma(NULL, gamma);
- if (curve == NULL) {
- return NULL;
- }
- FX_LPVOID profile = cmsCreateGrayProfile(D50, curve);
- cmsFreeToneCurve(curve);
- return profile;
-}
-ICodec_IccModule::IccCS GetProfileCSFromHandle(FX_LPVOID pProfile)
-{
- if (pProfile == NULL) {
- return ICodec_IccModule::IccCS_Unknown;
- }
- switch (cmsGetColorSpace(pProfile)) {
- case cmsSigXYZData:
- return ICodec_IccModule::IccCS_XYZ;
- case cmsSigLabData:
- return ICodec_IccModule::IccCS_Lab;
- case cmsSigLuvData:
- return ICodec_IccModule::IccCS_Luv;
- case cmsSigYCbCrData:
- return ICodec_IccModule::IccCS_YCbCr;
- case cmsSigYxyData:
- return ICodec_IccModule::IccCS_Yxy;
- case cmsSigRgbData:
- return ICodec_IccModule::IccCS_Rgb;
- case cmsSigGrayData:
- return ICodec_IccModule::IccCS_Gray;
- case cmsSigHsvData:
- return ICodec_IccModule::IccCS_Hsv;
- case cmsSigHlsData:
- return ICodec_IccModule::IccCS_Hls;
- case cmsSigCmykData:
- return ICodec_IccModule::IccCS_Cmyk;
- case cmsSigCmyData:
- return ICodec_IccModule::IccCS_Cmy;
- default:
- return ICodec_IccModule::IccCS_Unknown;
- }
-}
-ICodec_IccModule::IccCS CCodec_IccModule::GetProfileCS(FX_LPCBYTE pProfileData, unsigned int dwProfileSize)
-{
- ICodec_IccModule::IccCS cs;
- cmsHPROFILE hProfile = cmsOpenProfileFromMem((void*)pProfileData, dwProfileSize);
- if (hProfile == NULL) {
- return IccCS_Unknown;
- }
- cs = GetProfileCSFromHandle(hProfile);
- if (hProfile) {
- cmsCloseProfile(hProfile);
- }
- return cs;
-}
-ICodec_IccModule::IccCS CCodec_IccModule::GetProfileCS(IFX_FileRead* pFile)
-{
- if (pFile == NULL) {
- return IccCS_Unknown;
- }
- ICodec_IccModule::IccCS cs;
- FX_DWORD dwSize = (FX_DWORD)pFile->GetSize();
- FX_LPBYTE pBuf = FX_Alloc(FX_BYTE, dwSize);
- if (pBuf == NULL) {
- return IccCS_Unknown;
- }
- pFile->ReadBlock(pBuf, 0, dwSize);
- cs = GetProfileCS(pBuf, dwSize);
- FX_Free(pBuf);
- return cs;
-}
-FX_DWORD TransferProfileType(FX_LPVOID pProfile, FX_DWORD dwFormat)
-{
- cmsColorSpaceSignature cs = cmsGetColorSpace(pProfile);
- switch (cs) {
- case cmsSigXYZData:
- return TYPE_XYZ_16;
- case cmsSigLabData:
- return TYPE_Lab_DBL;
- case cmsSigLuvData:
- return TYPE_YUV_8;
- case cmsSigYCbCrData:
- return TYPE_YCbCr_8;
- case cmsSigYxyData:
- return TYPE_Yxy_16;
- case cmsSigRgbData:
- return T_DOSWAP(dwFormat) ? TYPE_RGB_8 : TYPE_BGR_8;
- case cmsSigGrayData:
- return TYPE_GRAY_8;
- case cmsSigHsvData:
- return TYPE_HSV_8;
- case cmsSigHlsData:
- return TYPE_HLS_8;
- case cmsSigCmykData:
- return T_DOSWAP(dwFormat) ? TYPE_KYMC_8 : TYPE_CMYK_8;
- case cmsSigCmyData:
- return TYPE_CMY_8;
- case cmsSigMCH5Data:
- return T_DOSWAP(dwFormat) ? TYPE_KYMC5_8 : TYPE_CMYK5_8;
- case cmsSigMCH6Data:
- return TYPE_CMYK6_8;
- case cmsSigMCH7Data:
- return T_DOSWAP(dwFormat) ? TYPE_KYMC7_8 : TYPE_CMYK7_8;
- case cmsSigMCH8Data:
- return T_DOSWAP(dwFormat) ? TYPE_KYMC8_8 : TYPE_CMYK8_8;
- case cmsSigMCH9Data:
- return T_DOSWAP(dwFormat) ? TYPE_KYMC9_8 : TYPE_CMYK9_8;
- case cmsSigMCHAData:
- return T_DOSWAP(dwFormat) ? TYPE_KYMC10_8 : TYPE_CMYK10_8;
- case cmsSigMCHBData:
- return T_DOSWAP(dwFormat) ? TYPE_KYMC11_8 : TYPE_CMYK11_8;
- case cmsSigMCHCData:
- return T_DOSWAP(dwFormat) ? TYPE_KYMC12_8 : TYPE_CMYK12_8;
- default:
- return 0;
- }
-}
-class CFX_IccProfileCache : public CFX_Object
-{
-public:
- CFX_IccProfileCache();
- ~CFX_IccProfileCache();
- FX_LPVOID m_pProfile;
- FX_DWORD m_dwRate;
-protected:
- void Purge();
-};
-CFX_IccProfileCache::CFX_IccProfileCache()
-{
- m_pProfile = NULL;
- m_dwRate = 1;
-}
-CFX_IccProfileCache::~CFX_IccProfileCache()
-{
- if (m_pProfile) {
- cmsCloseProfile(m_pProfile);
- }
-}
-void CFX_IccProfileCache::Purge()
-{
-}
-class CFX_IccTransformCache : public CFX_Object
-{
-public:
- CFX_IccTransformCache(CLcmsCmm* pCmm = NULL);
- ~CFX_IccTransformCache();
- FX_LPVOID m_pIccTransform;
- FX_DWORD m_dwRate;
- CLcmsCmm* m_pCmm;
-protected:
- void Purge();
-};
-CFX_IccTransformCache::CFX_IccTransformCache(CLcmsCmm* pCmm)
-{
- m_pIccTransform = NULL;
- m_dwRate = 1;
- m_pCmm = pCmm;
-}
-CFX_IccTransformCache::~CFX_IccTransformCache()
-{
- if (m_pIccTransform) {
- cmsDeleteTransform(m_pIccTransform);
- }
- if (m_pCmm) {
- FX_Free(m_pCmm);
- }
-}
-void CFX_IccTransformCache::Purge()
-{
-}
-class CFX_ByteStringKey : public CFX_BinaryBuf
-{
-public:
- CFX_ByteStringKey() : CFX_BinaryBuf() {}
- CFX_ByteStringKey& operator << (FX_DWORD i);
-};
-CFX_ByteStringKey& CFX_ByteStringKey::operator << (FX_DWORD i)
-{
- AppendBlock(&i, sizeof(FX_DWORD));
- return *this;
-}
-FX_LPVOID CCodec_IccModule::CreateProfile(ICodec_IccModule::IccParam* pIccParam, Icc_CLASS ic, CFX_BinaryBuf* pTransformKey)
-{
- CFX_IccProfileCache* pCache = NULL;
- CFX_ByteStringKey key;
- CFX_ByteString text;
- key << pIccParam->ColorSpace << (pIccParam->dwProfileType | ic << 8);
- FX_BYTE ID[16];
- switch (pIccParam->dwProfileType) {
- case Icc_PARAMTYPE_NONE:
- return NULL;
- case Icc_PARAMTYPE_BUFFER:
- MD5ComputeID(pIccParam->pProfileData, pIccParam->dwProfileSize, ID);
- break;
- case Icc_PARAMTYPE_PARAM:
- FXSYS_memset32(ID, 0, 16);
- switch (pIccParam->ColorSpace) {
- case IccCS_Gray:
- text.Format("%lf", pIccParam->Gamma);
- break;
- default:
- ;
- }
- MD5ComputeID(text.GetBuffer(0), text.GetLength(), ID);
- break;
- default:
- ;
- }
- key.AppendBlock(ID, 16);
- CFX_ByteString ProfileKey(key.GetBuffer(), key.GetSize());
- ASSERT(pTransformKey);
- pTransformKey->AppendBlock(ProfileKey.GetBuffer(0), ProfileKey.GetLength());
- if (!m_MapProfile.Lookup(ProfileKey, (FX_LPVOID&)pCache)) {
- pCache = FX_NEW CFX_IccProfileCache;
- if (pCache == NULL) {
- return NULL;
- }
- switch (pIccParam->dwProfileType) {
- case Icc_PARAMTYPE_BUFFER:
- pCache->m_pProfile = cmsOpenProfileFromMem(pIccParam->pProfileData, pIccParam->dwProfileSize);
- break;
- case Icc_PARAMTYPE_PARAM:
- switch (pIccParam->ColorSpace) {
- case IccCS_Rgb:
- pCache->m_pProfile = cmsCreate_sRGBProfile();
- break;
- case IccCS_Gray:
- pCache->m_pProfile = CreateProfile_Gray(pIccParam->Gamma);
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- m_MapProfile.SetAt(ProfileKey, pCache);
- } else {
- pCache->m_dwRate++;
- }
- return pCache->m_pProfile;
-}
-FX_LPVOID CCodec_IccModule::CreateTransform(ICodec_IccModule::IccParam* pInputParam,
- ICodec_IccModule::IccParam* pOutputParam,
- ICodec_IccModule::IccParam* pProofParam,
- FX_DWORD dwIntent, FX_DWORD dwFlag, FX_DWORD dwPrfIntent, FX_DWORD dwPrfFlag)
-{
- CLcmsCmm* pCmm = NULL;
- ASSERT(pInputParam && pOutputParam);
- CFX_ByteStringKey key;
- FX_LPVOID pInputProfile = CreateProfile(pInputParam, Icc_CLASS_INPUT, &key);
- if (pInputProfile == NULL) {
- return NULL;
- }
- FX_LPVOID pOutputProfile = CreateProfile(pOutputParam, Icc_CLASS_OUTPUT, &key);
- if (pOutputProfile == NULL) {
- return NULL;
- }
- FX_DWORD dwInputProfileType = TransferProfileType(pInputProfile, pInputParam->dwFormat);
- FX_DWORD dwOutputProfileType = TransferProfileType(pOutputProfile, pOutputParam->dwFormat);
- if (dwInputProfileType == 0 || dwOutputProfileType == 0) {
- return NULL;
- }
- FX_LPVOID pProofProfile = NULL;
- if (pProofParam) {
- pProofProfile = CreateProfile(pProofParam, Icc_CLASS_PROOF, &key);
- }
- key << dwInputProfileType << dwOutputProfileType << dwIntent << dwFlag << (pProofProfile != NULL) << dwPrfIntent << dwPrfFlag;
- CFX_ByteStringC TransformKey(key.GetBuffer(), key.GetSize());
- CFX_IccTransformCache* pTransformCache;
- if (!m_MapTranform.Lookup(TransformKey, (FX_LPVOID&)pTransformCache)) {
- pCmm = FX_Alloc(CLcmsCmm, 1);
- if (pCmm == NULL) {
- return NULL;
- }
- pCmm->m_nSrcComponents = T_CHANNELS(dwInputProfileType);
- pCmm->m_nDstComponents = T_CHANNELS(dwOutputProfileType);
- pCmm->m_bLab = T_COLORSPACE(pInputParam->dwFormat) == PT_Lab;
- pTransformCache = FX_NEW CFX_IccTransformCache(pCmm);
- if (pTransformCache == NULL) {
- FX_Free(pCmm);
- return NULL;
- }
- if (pProofProfile) {
- pTransformCache->m_pIccTransform = cmsCreateProofingTransform(pInputProfile, dwInputProfileType, pOutputProfile, dwOutputProfileType,
- pProofProfile, dwIntent, dwPrfIntent, dwPrfFlag);
- } else {
- pTransformCache->m_pIccTransform = cmsCreateTransform(pInputProfile, dwInputProfileType, pOutputProfile, dwOutputProfileType,
- dwIntent, dwFlag);
- }
- pCmm->m_hTransform = pTransformCache->m_pIccTransform;
- m_MapTranform.SetAt(TransformKey, pTransformCache);
- } else {
- pTransformCache->m_dwRate++;
- }
- return pTransformCache->m_pCmm;
-}
-CCodec_IccModule::~CCodec_IccModule()
-{
- FX_POSITION pos = m_MapProfile.GetStartPosition();
- CFX_ByteString key;
- CFX_IccProfileCache* pProfileCache;
- while (pos) {
- m_MapProfile.GetNextAssoc(pos, key, (FX_LPVOID&)pProfileCache);
- if (pProfileCache) {
- delete pProfileCache;
- }
- }
- pos = m_MapTranform.GetStartPosition();
- CFX_IccTransformCache* pTransformCache;
- while (pos) {
- m_MapTranform.GetNextAssoc(pos, key, (FX_LPVOID&)pTransformCache);
- if (pTransformCache) {
- delete pTransformCache;
- }
- }
-}
-void* CCodec_IccModule::CreateTransform_sRGB(FX_LPCBYTE pProfileData, unsigned int dwProfileSize, int nComponents, int intent, FX_DWORD dwSrcFormat)
-{
- return IccLib_CreateTransform_sRGB(pProfileData, dwProfileSize, nComponents, intent, dwSrcFormat);
-}
-void* CCodec_IccModule::CreateTransform_CMYK(FX_LPCBYTE pSrcProfileData, unsigned int dwSrcProfileSize, int nSrcComponents,
- FX_LPCBYTE pDstProfileData, unsigned int dwDstProfileSize, int intent,
- FX_DWORD dwSrcFormat , FX_DWORD dwDstFormat)
-{
- return IccLib_CreateTransform(pSrcProfileData, dwSrcProfileSize, nSrcComponents,
- pDstProfileData, dwDstProfileSize, 4, intent, dwSrcFormat, dwDstFormat);
-}
-void CCodec_IccModule::DestroyTransform(void* pTransform)
-{
- IccLib_DestroyTransform(pTransform);
-}
-void CCodec_IccModule::Translate(void* pTransform, FX_FLOAT* pSrcValues, FX_FLOAT* pDestValues)
-{
- IccLib_Translate(pTransform, pSrcValues, pDestValues);
-}
-void CCodec_IccModule::TranslateScanline(void* pTransform, FX_LPBYTE pDest, FX_LPCBYTE pSrc, int pixels)
-{
- IccLib_TranslateImage(pTransform, pDest, pSrc, pixels);
-}
-const FX_BYTE g_CMYKSamples[81 * 81 * 3] = {
- 255, 255, 255, 225, 226, 228, 199, 200, 202, 173, 174, 178, 147, 149, 152, 123, 125, 128, 99, 99, 102, 69, 70, 71, 34, 30, 31,
- 255, 253, 229, 226, 224, 203, 200, 199, 182, 173, 173, 158, 149, 148, 135, 125, 124, 113, 99, 99, 90, 70, 69, 63, 33, 29, 24,
- 255, 251, 204, 228, 223, 182, 201, 198, 163, 174, 172, 142, 150, 147, 122, 125, 123, 101, 99, 98, 80, 70, 68, 54, 32, 28, 16,
- 255, 249, 179, 230, 222, 160, 203, 197, 144, 174, 170, 124, 150, 145, 105, 125, 122, 88, 99, 97, 69, 70, 68, 46, 31, 28, 6,
- 255, 247, 154, 229, 220, 138, 203, 195, 122, 176, 169, 107, 150, 145, 91, 125, 121, 74, 100, 96, 57, 70, 67, 35, 29, 26, 0,
- 255, 246, 128, 231, 217, 114, 205, 194, 101, 176, 167, 88, 150, 144, 75, 125, 120, 60, 100, 96, 44, 70, 66, 24, 28, 26, 0,
- 255, 244, 96, 231, 217, 87, 203, 192, 78, 175, 167, 66, 150, 143, 56, 125, 119, 43, 100, 95, 29, 69, 66, 7, 26, 26, 0,
- 255, 243, 51, 232, 215, 51, 204, 191, 43, 176, 165, 38, 150, 142, 28, 125, 118, 17, 99, 94, 0, 68, 65, 0, 24, 25, 0,
- 255, 241, 0, 231, 215, 0, 203, 190, 0, 176, 164, 0, 150, 141, 0, 126, 117, 0, 99, 93, 0, 68, 65, 0, 24, 25, 0,
- 252, 228, 238, 222, 201, 211, 197, 180, 190, 171, 156, 166, 147, 133, 143, 123, 111, 119, 99, 88, 94, 71, 61, 66, 34, 22, 26,
- 254, 226, 213, 224, 201, 191, 199, 179, 171, 172, 155, 148, 147, 133, 128, 123, 110, 106, 98, 87, 83, 70, 59, 57, 33, 21, 18,
- 254, 224, 191, 224, 199, 172, 200, 177, 153, 173, 154, 133, 147, 132, 115, 123, 109, 94, 98, 86, 74, 70, 59, 49, 32, 21, 9,
- 255, 222, 168, 227, 198, 150, 200, 175, 135, 173, 153, 118, 148, 130, 99, 123, 109, 82, 98, 86, 64, 69, 58, 40, 31, 19, 0,
- 255, 221, 145, 227, 196, 129, 201, 174, 115, 173, 151, 99, 148, 129, 85, 124, 108, 69, 98, 85, 52, 69, 58, 30, 30, 19, 0,
- 255, 219, 121, 227, 195, 109, 201, 174, 97, 174, 150, 83, 148, 129, 70, 124, 107, 55, 98, 84, 40, 69, 58, 19, 28, 18, 0,
- 255, 218, 92, 229, 194, 82, 202, 173, 75, 174, 150, 63, 149, 128, 51, 124, 106, 39, 98, 84, 24, 68, 57, 3, 26, 18, 0,
- 255, 217, 54, 228, 193, 52, 201, 172, 46, 174, 148, 36, 148, 127, 27, 123, 105, 14, 98, 83, 0, 68, 56, 0, 25, 18, 0,
- 255, 216, 0, 229, 192, 2, 202, 171, 4, 173, 148, 0, 148, 126, 0, 124, 105, 0, 98, 83, 0, 68, 56, 0, 24, 17, 0,
- 249, 204, 223, 219, 181, 199, 195, 160, 178, 170, 140, 156, 146, 119, 134, 123, 99, 112, 98, 77, 88, 70, 52, 61, 34, 11, 20,
- 250, 201, 200, 221, 180, 178, 197, 159, 161, 171, 139, 139, 147, 119, 120, 123, 98, 99, 98, 77, 78, 69, 51, 52, 34, 11, 10,
- 252, 201, 180, 223, 179, 162, 197, 159, 144, 170, 138, 125, 146, 117, 107, 122, 97, 89, 98, 76, 69, 69, 50, 44, 32, 11, 2,
- 252, 199, 158, 222, 177, 143, 199, 158, 127, 171, 137, 110, 147, 117, 93, 122, 96, 76, 97, 75, 58, 69, 50, 36, 32, 10, 0,
- 253, 198, 137, 223, 177, 123, 198, 156, 110, 171, 136, 95, 146, 116, 80, 122, 96, 65, 97, 75, 47, 69, 50, 25, 30, 10, 0,
- 254, 197, 115, 225, 175, 104, 198, 156, 92, 172, 135, 79, 147, 115, 66, 123, 95, 52, 98, 74, 37, 69, 49, 15, 29, 10, 0,
- 254, 196, 89, 224, 175, 80, 199, 154, 70, 172, 134, 59, 146, 114, 48, 122, 95, 36, 97, 74, 21, 68, 49, 0, 27, 9, 0,
- 255, 195, 57, 225, 173, 52, 198, 154, 44, 172, 133, 36, 147, 113, 26, 123, 94, 14, 98, 74, 0, 68, 49, 0, 26, 10, 0,
- 254, 194, 15, 225, 172, 12, 198, 153, 7, 172, 132, 3, 146, 113, 0, 123, 93, 0, 98, 73, 0, 68, 49, 0, 26, 9, 0,
- 246, 178, 209, 218, 159, 186, 194, 140, 166, 168, 122, 145, 144, 104, 125, 121, 85, 103, 97, 65, 81, 69, 41, 55, 34, 0, 12,
- 248, 176, 186, 219, 157, 166, 195, 139, 149, 168, 121, 130, 144, 103, 111, 121, 85, 91, 97, 65, 71, 69, 41, 46, 34, 0, 4,
- 249, 175, 168, 220, 156, 150, 196, 139, 135, 169, 121, 116, 144, 103, 100, 122, 84, 83, 98, 65, 63, 70, 41, 39, 33, 0, 0,
- 249, 175, 148, 220, 155, 133, 196, 138, 119, 169, 120, 103, 145, 101, 87, 121, 83, 71, 97, 65, 54, 69, 41, 31, 32, 0, 0,
- 249, 173, 128, 222, 154, 115, 195, 137, 102, 170, 119, 88, 145, 101, 74, 122, 83, 59, 97, 64, 43, 68, 40, 20, 30, 0, 0,
- 250, 172, 108, 221, 154, 98, 195, 136, 86, 170, 118, 73, 145, 100, 61, 122, 82, 48, 97, 63, 32, 69, 40, 11, 28, 0, 0,
- 250, 171, 85, 221, 153, 76, 196, 136, 67, 170, 117, 56, 145, 99, 44, 121, 82, 33, 97, 63, 17, 68, 40, 0, 28, 0, 0,
- 251, 171, 58, 222, 152, 50, 197, 135, 43, 169, 117, 34, 146, 99, 25, 121, 81, 10, 96, 63, 0, 68, 40, 0, 27, 0, 0,
- 250, 170, 26, 222, 151, 19, 196, 134, 13, 169, 116, 4, 145, 99, 0, 122, 81, 0, 97, 63, 0, 67, 40, 0, 26, 0, 0,
- 244, 153, 194, 215, 136, 173, 192, 121, 155, 167, 104, 135, 143, 89, 115, 121, 72, 96, 97, 54, 75, 70, 31, 49, 34, 0, 6,
- 245, 153, 173, 216, 136, 155, 192, 120, 138, 167, 104, 121, 144, 88, 103, 121, 71, 85, 97, 54, 66, 69, 31, 42, 34, 0, 0,
- 246, 152, 157, 217, 135, 140, 193, 120, 126, 167, 103, 109, 143, 88, 92, 121, 72, 76, 97, 54, 58, 69, 31, 35, 33, 0, 0,
- 245, 150, 139, 218, 134, 125, 193, 119, 111, 167, 103, 96, 144, 87, 80, 121, 71, 66, 96, 53, 49, 68, 31, 26, 32, 0, 0,
- 246, 151, 122, 218, 133, 108, 194, 118, 96, 168, 102, 81, 144, 86, 69, 120, 71, 55, 95, 53, 39, 68, 30, 17, 31, 0, 0,
- 248, 150, 103, 218, 133, 91, 193, 118, 81, 168, 102, 69, 143, 86, 56, 120, 70, 43, 96, 53, 28, 68, 31, 6, 29, 0, 0,
- 247, 149, 81, 218, 132, 72, 194, 117, 62, 168, 101, 52, 144, 86, 42, 121, 70, 29, 96, 52, 13, 68, 30, 0, 28, 0, 0,
- 247, 148, 55, 219, 131, 50, 194, 117, 43, 167, 101, 32, 144, 85, 22, 120, 69, 8, 96, 52, 0, 67, 30, 0, 27, 0, 0,
- 247, 147, 29, 218, 131, 24, 194, 116, 20, 168, 100, 11, 144, 85, 0, 120, 69, 0, 96, 52, 0, 67, 30, 0, 26, 0, 0,
- 242, 130, 179, 214, 114, 160, 190, 101, 143, 166, 87, 125, 143, 72, 107, 120, 58, 88, 96, 42, 68, 69, 17, 44, 35, 0, 0,
- 243, 129, 161, 215, 114, 143, 191, 101, 128, 166, 87, 113, 143, 73, 96, 120, 58, 79, 96, 41, 60, 69, 18, 37, 33, 0, 0,
- 243, 129, 146, 216, 114, 130, 192, 101, 117, 166, 87, 101, 143, 72, 86, 121, 58, 69, 96, 42, 52, 69, 18, 29, 31, 0, 0,
- 243, 128, 130, 216, 114, 115, 191, 101, 102, 165, 86, 88, 142, 72, 75, 120, 58, 60, 95, 42, 43, 68, 19, 21, 30, 0, 0,
- 244, 127, 112, 217, 113, 101, 192, 99, 89, 166, 85, 75, 142, 72, 63, 119, 57, 50, 96, 41, 35, 68, 19, 13, 30, 0, 0,
- 244, 127, 96, 216, 112, 86, 191, 99, 75, 166, 86, 64, 143, 72, 52, 120, 57, 40, 95, 41, 24, 67, 20, 1, 29, 0, 0,
- 245, 126, 77, 216, 113, 68, 191, 100, 59, 166, 85, 49, 142, 71, 38, 119, 57, 26, 95, 41, 10, 67, 20, 0, 28, 0, 0,
- 244, 126, 55, 216, 112, 48, 191, 99, 40, 166, 85, 31, 143, 71, 20, 119, 57, 6, 95, 42, 0, 67, 20, 0, 28, 0, 0,
- 245, 126, 33, 217, 112, 26, 192, 99, 22, 166, 84, 11, 142, 70, 0, 119, 57, 0, 95, 41, 0, 66, 20, 0, 27, 0, 0,
- 241, 102, 167, 213, 90, 149, 189, 79, 133, 165, 66, 115, 141, 54, 98, 119, 41, 81, 96, 25, 63, 69, 0, 38, 30, 0, 0,
- 241, 102, 149, 213, 90, 133, 189, 79, 119, 165, 66, 103, 142, 55, 88, 119, 41, 71, 96, 25, 53, 69, 0, 31, 28, 0, 0,
- 241, 102, 135, 214, 90, 121, 190, 79, 108, 165, 66, 92, 141, 55, 78, 119, 42, 63, 96, 26, 46, 69, 0, 24, 28, 0, 0,
- 241, 101, 120, 214, 90, 107, 189, 79, 95, 165, 67, 83, 141, 54, 68, 118, 41, 54, 95, 27, 39, 68, 0, 16, 27, 0, 0,
- 241, 102, 106, 213, 90, 93, 189, 78, 82, 164, 67, 70, 141, 55, 58, 118, 42, 45, 94, 27, 29, 67, 2, 6, 27, 0, 0,
- 242, 101, 90, 214, 89, 79, 190, 79, 69, 166, 67, 59, 141, 55, 47, 118, 41, 35, 95, 27, 19, 67, 3, 0, 26, 0, 0,
- 242, 102, 72, 213, 89, 63, 191, 79, 56, 164, 67, 45, 141, 55, 34, 118, 42, 22, 94, 28, 6, 67, 3, 0, 26, 0, 0,
- 242, 100, 51, 214, 89, 45, 190, 78, 38, 164, 67, 30, 141, 55, 18, 118, 42, 3, 95, 28, 0, 66, 4, 0, 26, 0, 0,
- 243, 100, 33, 214, 90, 27, 190, 78, 22, 165, 67, 13, 141, 55, 0, 118, 43, 0, 94, 29, 0, 66, 5, 0, 26, 0, 0,
- 237, 69, 153, 211, 58, 135, 187, 51, 121, 163, 41, 105, 141, 28, 90, 118, 15, 73, 96, 0, 56, 68, 0, 33, 25, 0, 0,
- 239, 67, 137, 212, 60, 123, 189, 50, 110, 163, 41, 94, 141, 29, 79, 118, 17, 65, 95, 0, 48, 69, 0, 26, 25, 0, 0,
- 240, 69, 124, 211, 60, 111, 188, 50, 98, 163, 42, 85, 141, 31, 72, 118, 18, 57, 94, 0, 41, 68, 0, 19, 25, 0, 0,
- 240, 70, 112, 212, 61, 99, 188, 52, 87, 163, 41, 74, 140, 31, 62, 118, 20, 48, 94, 2, 32, 68, 0, 11, 24, 0, 0,
- 239, 70, 98, 212, 62, 86, 188, 53, 77, 164, 42, 64, 140, 32, 52, 118, 20, 40, 94, 3, 24, 67, 0, 3, 23, 0, 0,
- 239, 71, 85, 212, 61, 74, 187, 53, 65, 163, 44, 54, 140, 34, 43, 118, 22, 30, 95, 3, 14, 67, 0, 0, 23, 0, 0,
- 239, 70, 67, 212, 62, 59, 188, 53, 51, 163, 45, 42, 141, 34, 31, 117, 22, 17, 94, 5, 2, 66, 0, 0, 23, 0, 0,
- 239, 71, 50, 213, 62, 43, 188, 54, 37, 164, 45, 28, 139, 34, 16, 117, 22, 2, 94, 7, 0, 65, 0, 0, 23, 0, 0,
- 240, 71, 34, 212, 63, 29, 189, 54, 24, 163, 46, 15, 139, 36, 2, 117, 25, 0, 94, 8, 0, 66, 0, 0, 23, 0, 0,
- 237, 0, 140, 209, 0, 124, 186, 0, 112, 162, 0, 97, 141, 0, 82, 118, 0, 67, 95, 0, 49, 68, 0, 27, 20, 0, 0,
- 237, 0, 126, 210, 0, 113, 187, 0, 99, 163, 0, 86, 139, 0, 72, 118, 0, 58, 95, 0, 42, 67, 0, 20, 20, 0, 0,
- 237, 1, 114, 209, 1, 102, 187, 0, 90, 163, 0, 78, 139, 0, 64, 118, 0, 50, 95, 0, 35, 67, 0, 13, 20, 0, 0,
- 236, 16, 102, 209, 7, 91, 186, 0, 80, 162, 0, 68, 139, 0, 56, 117, 0, 43, 94, 0, 27, 67, 0, 6, 20, 0, 0,
- 238, 15, 89, 209, 13, 79, 186, 6, 69, 162, 0, 58, 139, 0, 47, 117, 0, 34, 93, 0, 20, 66, 0, 2, 20, 0, 0,
- 237, 20, 78, 210, 12, 68, 187, 4, 59, 163, 0, 49, 139, 0, 38, 116, 0, 26, 94, 0, 11, 66, 0, 0, 20, 0, 0,
- 237, 25, 64, 210, 18, 56, 186, 11, 48, 162, 4, 39, 138, 0, 27, 117, 0, 14, 93, 0, 0, 66, 0, 0, 20, 0, 0,
- 238, 25, 48, 210, 22, 43, 186, 15, 35, 162, 8, 26, 140, 0, 14, 117, 0, 0, 93, 0, 0, 65, 0, 0, 20, 0, 0,
- 238, 28, 35, 210, 21, 30, 187, 15, 24, 162, 8, 16, 139, 1, 2, 117, 0, 0, 93, 0, 0, 65, 0, 0, 22, 0, 0,
- 219, 242, 252, 195, 214, 225, 172, 191, 201, 148, 165, 175, 127, 142, 150, 106, 119, 126, 84, 95, 101, 58, 66, 72, 24, 27, 32,
- 222, 239, 226, 196, 213, 202, 173, 189, 180, 150, 165, 158, 129, 141, 135, 107, 118, 113, 85, 94, 90, 58, 66, 63, 21, 26, 24,
- 223, 237, 203, 198, 211, 182, 175, 188, 163, 152, 164, 141, 129, 140, 121, 107, 117, 101, 85, 93, 80, 58, 64, 54, 21, 26, 18,
- 226, 236, 179, 201, 210, 160, 177, 187, 143, 153, 162, 125, 130, 139, 106, 108, 116, 89, 85, 92, 69, 58, 64, 45, 20, 25, 8,
- 227, 234, 153, 201, 208, 139, 178, 185, 124, 154, 161, 107, 131, 138, 91, 108, 115, 75, 85, 91, 58, 58, 63, 35, 17, 25, 0,
- 229, 233, 130, 203, 207, 116, 178, 184, 104, 154, 160, 90, 131, 137, 76, 109, 114, 62, 85, 90, 46, 58, 63, 25, 16, 24, 0,
- 230, 231, 100, 202, 205, 90, 179, 183, 80, 154, 159, 69, 131, 136, 57, 109, 113, 46, 86, 90, 32, 58, 63, 10, 14, 24, 0,
- 230, 230, 65, 204, 204, 58, 180, 182, 52, 155, 157, 44, 132, 135, 35, 110, 113, 24, 86, 89, 9, 57, 62, 0, 11, 24, 0,
- 232, 230, 19, 204, 204, 19, 180, 181, 17, 155, 157, 10, 131, 134, 2, 109, 112, 0, 85, 89, 0, 57, 62, 0, 10, 23, 0,
- 218, 216, 236, 194, 192, 211, 172, 171, 188, 149, 149, 164, 128, 127, 141, 106, 106, 119, 84, 84, 94, 59, 57, 66, 25, 18, 26,
- 221, 214, 211, 196, 191, 190, 174, 170, 170, 150, 148, 148, 128, 126, 127, 107, 105, 106, 85, 83, 84, 59, 56, 58, 23, 17, 18,
- 222, 213, 190, 197, 189, 170, 175, 169, 153, 151, 147, 133, 129, 126, 113, 108, 105, 94, 85, 82, 74, 59, 56, 49, 22, 17, 11,
- 224, 211, 168, 199, 188, 151, 175, 168, 135, 152, 146, 117, 129, 124, 99, 107, 103, 82, 84, 82, 64, 59, 55, 41, 21, 17, 1,
- 224, 210, 145, 199, 187, 130, 176, 166, 117, 152, 145, 101, 129, 123, 86, 107, 103, 70, 85, 81, 53, 58, 55, 31, 19, 17, 0,
- 227, 208, 123, 200, 186, 110, 177, 165, 98, 153, 143, 84, 130, 122, 70, 108, 102, 57, 85, 80, 41, 58, 54, 20, 18, 16, 0,
- 227, 208, 97, 202, 185, 86, 177, 164, 77, 153, 142, 65, 130, 122, 54, 108, 101, 42, 85, 80, 27, 58, 54, 7, 16, 16, 0,
- 228, 206, 66, 202, 184, 58, 178, 163, 50, 154, 141, 42, 131, 121, 33, 109, 101, 21, 86, 79, 5, 58, 54, 0, 13, 16, 0,
- 228, 206, 29, 202, 183, 25, 178, 163, 20, 154, 141, 15, 131, 121, 5, 108, 100, 0, 85, 79, 0, 58, 53, 0, 13, 16, 0,
- 217, 193, 221, 193, 172, 198, 172, 153, 178, 149, 133, 154, 128, 114, 132, 107, 94, 111, 85, 74, 89, 59, 49, 61, 25, 8, 22,
- 219, 191, 198, 195, 171, 178, 173, 153, 159, 149, 132, 139, 128, 113, 119, 107, 94, 100, 85, 73, 79, 59, 48, 52, 25, 7, 14,
- 221, 191, 180, 196, 170, 160, 174, 152, 144, 150, 132, 125, 129, 113, 107, 107, 93, 89, 85, 73, 69, 59, 48, 45, 23, 7, 4,
- 222, 189, 159, 197, 169, 142, 174, 151, 127, 151, 131, 110, 129, 112, 94, 108, 93, 78, 85, 72, 60, 58, 47, 37, 22, 7, 0,
- 223, 188, 138, 197, 168, 123, 175, 150, 109, 151, 130, 95, 130, 111, 81, 108, 92, 65, 85, 72, 49, 59, 47, 27, 21, 7, 0,
- 224, 187, 118, 198, 167, 105, 176, 149, 93, 152, 129, 79, 130, 110, 68, 108, 91, 54, 85, 71, 38, 59, 47, 17, 18, 7, 0,
- 224, 187, 93, 199, 166, 83, 176, 148, 73, 152, 128, 62, 129, 109, 51, 108, 90, 39, 85, 71, 25, 58, 46, 3, 16, 8, 0,
- 226, 186, 64, 200, 165, 57, 177, 147, 50, 153, 127, 40, 130, 108, 31, 108, 90, 19, 85, 70, 3, 58, 46, 0, 16, 8, 0,
- 227, 185, 35, 200, 165, 30, 176, 146, 25, 152, 127, 18, 130, 108, 7, 108, 89, 0, 85, 70, 0, 57, 46, 0, 14, 8, 0,
- 216, 169, 205, 192, 150, 184, 171, 134, 164, 149, 116, 144, 128, 99, 124, 107, 81, 103, 85, 63, 81, 60, 39, 55, 26, 0, 15,
- 217, 168, 186, 193, 150, 165, 172, 134, 149, 150, 116, 130, 128, 99, 111, 107, 81, 92, 85, 62, 72, 59, 39, 47, 25, 0, 6,
- 219, 168, 168, 194, 149, 150, 173, 133, 135, 150, 116, 117, 128, 98, 99, 107, 80, 82, 86, 62, 63, 59, 38, 39, 24, 0, 0,
- 219, 166, 148, 195, 149, 133, 173, 133, 119, 150, 115, 103, 128, 98, 88, 107, 80, 72, 85, 61, 54, 59, 38, 32, 23, 0, 0,
- 220, 166, 129, 196, 148, 116, 174, 132, 103, 151, 114, 89, 129, 97, 75, 107, 79, 60, 85, 61, 44, 59, 38, 22, 21, 0, 0,
- 222, 164, 110, 197, 147, 99, 175, 131, 87, 151, 113, 75, 129, 96, 63, 107, 79, 49, 85, 61, 33, 58, 38, 12, 19, 0, 0,
- 222, 164, 88, 197, 146, 79, 174, 130, 69, 151, 113, 58, 129, 95, 47, 107, 78, 35, 85, 60, 20, 58, 38, 0, 18, 0, 0,
- 223, 164, 63, 198, 145, 55, 175, 129, 48, 151, 112, 39, 129, 95, 29, 107, 78, 16, 85, 60, 1, 58, 38, 0, 17, 0, 0,
- 223, 163, 36, 198, 145, 32, 174, 129, 26, 151, 111, 17, 129, 95, 7, 107, 78, 0, 84, 60, 0, 57, 37, 0, 15, 0, 0,
- 215, 147, 192, 191, 130, 172, 170, 116, 153, 148, 100, 133, 127, 85, 115, 107, 69, 96, 85, 51, 75, 60, 28, 50, 25, 0, 8,
- 217, 146, 173, 192, 130, 154, 171, 115, 138, 149, 100, 121, 128, 84, 103, 107, 68, 85, 85, 51, 66, 60, 28, 42, 25, 0, 0,
- 217, 145, 157, 193, 129, 140, 173, 115, 125, 149, 100, 109, 128, 84, 92, 107, 68, 76, 85, 51, 58, 59, 28, 35, 23, 0, 0,
- 218, 145, 140, 193, 129, 125, 172, 114, 110, 149, 99, 96, 128, 83, 81, 107, 67, 65, 84, 51, 49, 59, 29, 27, 22, 0, 0,
- 219, 144, 121, 194, 128, 108, 172, 113, 96, 149, 98, 83, 128, 83, 69, 107, 68, 55, 85, 50, 40, 59, 28, 18, 20, 0, 0,
- 220, 143, 104, 195, 128, 93, 173, 114, 82, 150, 98, 69, 127, 82, 58, 107, 67, 45, 85, 50, 30, 59, 28, 7, 19, 0, 0,
- 220, 143, 84, 195, 127, 74, 173, 113, 65, 149, 97, 55, 128, 82, 44, 106, 67, 32, 84, 50, 16, 58, 28, 0, 18, 0, 0,
- 221, 142, 62, 196, 126, 53, 173, 112, 46, 150, 97, 37, 128, 82, 26, 107, 66, 14, 84, 50, 0, 58, 28, 0, 16, 0, 0,
- 222, 142, 38, 196, 126, 34, 174, 112, 27, 150, 96, 17, 128, 82, 6, 106, 66, 0, 84, 50, 0, 57, 29, 0, 16, 0, 0,
- 214, 123, 179, 191, 110, 159, 169, 98, 143, 147, 84, 124, 126, 70, 106, 107, 55, 88, 85, 39, 69, 60, 15, 45, 23, 0, 2,
- 216, 123, 161, 192, 110, 144, 170, 98, 129, 148, 84, 112, 127, 70, 95, 107, 55, 79, 85, 39, 61, 60, 15, 37, 20, 0, 0,
- 217, 122, 145, 192, 110, 130, 170, 97, 116, 149, 84, 101, 127, 70, 85, 106, 55, 70, 85, 39, 53, 59, 16, 30, 19, 0, 0,
- 217, 123, 131, 192, 109, 116, 171, 96, 103, 149, 83, 89, 127, 70, 75, 106, 55, 60, 85, 40, 45, 59, 16, 23, 17, 0, 0,
- 217, 122, 114, 193, 109, 101, 172, 96, 91, 149, 82, 77, 128, 69, 64, 106, 55, 50, 84, 39, 35, 59, 17, 14, 17, 0, 0,
- 218, 122, 98, 194, 108, 87, 171, 96, 77, 149, 82, 65, 127, 69, 52, 106, 55, 40, 84, 40, 25, 59, 18, 3, 15, 0, 0,
- 219, 122, 80, 193, 108, 70, 172, 95, 61, 149, 82, 51, 127, 69, 40, 106, 55, 28, 84, 39, 12, 58, 17, 0, 13, 0, 0,
- 219, 121, 59, 194, 108, 52, 172, 96, 44, 149, 82, 35, 127, 68, 24, 106, 55, 11, 84, 40, 0, 57, 18, 0, 13, 0, 0,
- 219, 121, 40, 193, 108, 33, 172, 95, 26, 149, 81, 19, 128, 68, 6, 106, 54, 0, 84, 39, 0, 57, 18, 0, 13, 0, 0,
- 213, 99, 165, 189, 87, 148, 169, 76, 132, 147, 64, 115, 126, 52, 98, 106, 39, 81, 85, 23, 63, 60, 0, 39, 16, 0, 0,
- 214, 98, 149, 191, 87, 133, 170, 76, 119, 148, 65, 103, 127, 53, 88, 106, 39, 72, 85, 24, 55, 60, 0, 32, 15, 0, 0,
- 215, 99, 136, 191, 87, 121, 170, 77, 108, 148, 65, 93, 126, 53, 79, 106, 40, 64, 85, 24, 47, 59, 0, 25, 14, 0, 0,
- 215, 99, 121, 192, 87, 108, 170, 77, 96, 148, 65, 82, 126, 53, 69, 106, 40, 55, 85, 25, 39, 59, 0, 18, 13, 0, 0,
- 216, 99, 106, 191, 87, 95, 170, 76, 83, 148, 65, 71, 126, 53, 58, 106, 41, 45, 85, 26, 30, 59, 0, 8, 11, 0, 0,
- 216, 98, 91, 192, 88, 82, 170, 77, 71, 148, 65, 60, 127, 53, 48, 105, 41, 36, 83, 26, 21, 58, 1, 2, 11, 0, 0,
- 217, 99, 75, 192, 87, 66, 170, 76, 57, 148, 65, 47, 126, 53, 36, 105, 41, 24, 83, 26, 8, 57, 2, 0, 9, 0, 0,
- 217, 98, 57, 192, 87, 49, 171, 77, 41, 147, 65, 32, 126, 53, 21, 105, 41, 8, 84, 27, 0, 57, 3, 0, 9, 0, 0,
- 217, 98, 40, 193, 87, 34, 171, 76, 27, 148, 65, 19, 126, 53, 6, 105, 41, 0, 83, 27, 0, 57, 4, 0, 9, 0, 0,
- 211, 67, 152, 189, 58, 136, 168, 50, 122, 147, 39, 105, 127, 28, 89, 106, 14, 74, 85, 0, 56, 59, 0, 33, 9, 0, 0,
- 213, 68, 138, 190, 59, 123, 169, 51, 109, 148, 40, 95, 126, 30, 80, 106, 16, 65, 85, 0, 48, 59, 0, 27, 9, 0, 0,
- 214, 69, 125, 190, 59, 111, 168, 51, 99, 148, 41, 86, 126, 31, 72, 106, 18, 58, 85, 0, 41, 59, 0, 20, 7, 0, 0,
- 215, 70, 112, 190, 61, 100, 169, 52, 88, 147, 42, 76, 126, 32, 63, 106, 19, 49, 84, 1, 34, 58, 0, 13, 7, 0, 0,
- 214, 70, 99, 190, 62, 88, 169, 53, 77, 147, 43, 65, 125, 32, 53, 106, 20, 40, 84, 3, 26, 58, 0, 4, 7, 0, 0,
- 214, 71, 86, 190, 61, 75, 169, 53, 65, 146, 43, 54, 126, 33, 44, 105, 21, 31, 83, 4, 17, 57, 0, 0, 7, 0, 0,
- 215, 71, 71, 191, 62, 62, 169, 53, 53, 147, 44, 44, 126, 34, 33, 105, 22, 20, 83, 5, 4, 57, 0, 0, 7, 0, 0,
- 215, 71, 54, 191, 62, 47, 169, 54, 39, 147, 44, 30, 126, 35, 20, 105, 23, 6, 83, 6, 0, 56, 0, 0, 5, 0, 0,
- 215, 71, 41, 191, 63, 34, 170, 54, 27, 147, 45, 17, 126, 35, 6, 105, 23, 0, 83, 8, 0, 56, 0, 0, 5, 0, 0,
- 210, 13, 140, 189, 1, 125, 167, 0, 110, 146, 0, 96, 126, 0, 81, 106, 0, 67, 85, 0, 51, 59, 0, 28, 4, 0, 0,
- 212, 18, 126, 190, 7, 113, 168, 0, 100, 146, 0, 86, 126, 0, 73, 106, 0, 59, 84, 0, 43, 59, 0, 22, 4, 0, 0,
- 212, 21, 115, 190, 13, 103, 168, 3, 91, 146, 0, 78, 125, 0, 65, 105, 0, 52, 84, 0, 36, 58, 0, 16, 4, 0, 0,
- 213, 24, 103, 189, 19, 91, 168, 9, 82, 146, 0, 69, 125, 0, 57, 105, 0, 44, 84, 0, 29, 58, 0, 7, 4, 0, 0,
- 213, 27, 92, 188, 21, 81, 168, 14, 71, 146, 1, 59, 125, 0, 48, 105, 0, 36, 84, 0, 21, 58, 0, 4, 4, 0, 0,
- 213, 30, 80, 189, 22, 69, 168, 17, 61, 146, 5, 50, 125, 0, 39, 104, 0, 27, 83, 0, 12, 57, 0, 0, 4, 0, 0,
- 214, 30, 67, 189, 25, 57, 168, 20, 50, 146, 9, 40, 125, 0, 29, 104, 0, 17, 83, 0, 2, 56, 0, 0, 4, 0, 0,
- 214, 32, 53, 189, 27, 44, 169, 20, 38, 146, 13, 28, 124, 2, 17, 104, 0, 4, 83, 0, 0, 56, 0, 0, 4, 0, 0,
- 214, 33, 41, 190, 27, 33, 168, 23, 27, 146, 13, 18, 125, 3, 5, 105, 0, 0, 83, 0, 0, 56, 0, 0, 4, 0, 0,
- 185, 229, 250, 164, 204, 223, 146, 182, 199, 127, 158, 174, 108, 136, 149, 89, 113, 125, 70, 90, 100, 46, 62, 71, 10, 25, 33,
- 189, 227, 225, 168, 202, 201, 148, 181, 179, 129, 157, 156, 109, 135, 134, 90, 113, 113, 70, 89, 90, 46, 62, 62, 8, 24, 25,
- 192, 226, 202, 170, 202, 182, 151, 179, 162, 130, 156, 141, 110, 133, 121, 91, 112, 101, 71, 89, 80, 46, 61, 54, 7, 24, 19,
- 194, 224, 179, 173, 200, 160, 153, 178, 144, 132, 155, 125, 112, 133, 107, 92, 111, 89, 71, 88, 69, 46, 61, 45, 6, 23, 10,
- 196, 223, 155, 174, 198, 139, 154, 176, 124, 132, 153, 107, 113, 131, 91, 92, 110, 75, 72, 87, 58, 47, 60, 37, 4, 23, 0,
- 198, 221, 131, 175, 197, 117, 155, 175, 105, 133, 152, 91, 113, 130, 76, 92, 109, 63, 72, 86, 47, 46, 60, 26, 3, 23, 0,
- 200, 220, 104, 176, 196, 94, 156, 175, 84, 134, 151, 72, 113, 129, 59, 93, 108, 47, 72, 85, 33, 46, 59, 13, 0, 23, 0,
- 201, 219, 73, 179, 195, 65, 157, 173, 57, 135, 150, 48, 114, 129, 39, 94, 108, 28, 72, 85, 15, 47, 59, 0, 0, 22, 0,
- 203, 219, 42, 178, 195, 37, 157, 173, 32, 135, 150, 26, 114, 128, 16, 94, 107, 6, 73, 85, 0, 46, 58, 0, 0, 22, 0,
- 186, 205, 233, 165, 183, 209, 148, 163, 187, 128, 142, 163, 109, 121, 140, 91, 101, 118, 71, 80, 94, 48, 54, 66, 12, 15, 27,
- 189, 204, 211, 169, 182, 189, 151, 163, 169, 131, 141, 147, 111, 121, 126, 92, 101, 105, 72, 79, 84, 48, 54, 58, 11, 15, 19,
- 192, 202, 190, 171, 181, 170, 152, 161, 152, 131, 141, 133, 112, 120, 113, 93, 100, 94, 72, 79, 74, 48, 53, 50, 10, 15, 11,
- 195, 201, 169, 172, 179, 151, 153, 160, 135, 132, 139, 117, 113, 119, 100, 93, 99, 82, 72, 78, 64, 48, 53, 41, 9, 14, 3,
- 195, 200, 146, 174, 179, 131, 154, 159, 117, 133, 138, 101, 113, 118, 86, 93, 98, 70, 73, 77, 53, 48, 52, 32, 8, 15, 0,
- 198, 199, 125, 175, 177, 111, 155, 158, 100, 133, 137, 85, 113, 117, 71, 93, 97, 57, 72, 77, 42, 47, 52, 22, 5, 14, 0,
- 199, 198, 101, 176, 177, 89, 155, 157, 79, 134, 136, 68, 113, 116, 56, 94, 97, 44, 73, 76, 30, 47, 52, 10, 2, 15, 0,
- 200, 197, 72, 178, 176, 63, 157, 156, 56, 135, 136, 46, 114, 116, 37, 94, 96, 26, 73, 76, 11, 47, 51, 0, 0, 14, 0,
- 201, 197, 45, 177, 175, 38, 156, 155, 31, 135, 135, 25, 114, 115, 17, 94, 96, 5, 73, 75, 0, 46, 51, 0, 0, 14, 0,
- 187, 183, 218, 167, 165, 197, 149, 147, 176, 129, 127, 153, 111, 109, 132, 92, 90, 111, 73, 70, 89, 49, 46, 62, 15, 4, 22,
- 190, 183, 197, 170, 164, 177, 151, 146, 159, 130, 127, 139, 112, 109, 119, 93, 90, 99, 72, 70, 78, 49, 45, 53, 14, 4, 15,
- 192, 182, 179, 171, 163, 161, 153, 145, 144, 132, 126, 125, 113, 108, 107, 93, 89, 88, 73, 70, 69, 49, 45, 45, 13, 5, 6,
- 195, 181, 159, 172, 162, 142, 152, 145, 127, 132, 125, 111, 113, 107, 94, 93, 88, 77, 73, 69, 59, 48, 45, 37, 11, 5, 0,
- 195, 180, 139, 173, 161, 124, 153, 143, 110, 133, 125, 96, 113, 106, 81, 94, 88, 66, 73, 68, 49, 49, 44, 28, 9, 6, 0,
- 196, 179, 118, 174, 160, 106, 154, 142, 94, 133, 124, 81, 113, 105, 68, 94, 87, 54, 73, 68, 39, 48, 44, 18, 5, 5, 0,
- 197, 178, 96, 176, 159, 86, 155, 141, 75, 134, 123, 64, 114, 105, 53, 94, 87, 40, 73, 68, 26, 48, 44, 5, 2, 6, 0,
- 199, 178, 70, 176, 158, 62, 156, 141, 54, 134, 122, 44, 114, 104, 35, 94, 86, 23, 73, 67, 8, 47, 44, 0, 2, 6, 0,
- 199, 177, 45, 178, 158, 40, 156, 140, 32, 135, 122, 26, 114, 104, 16, 94, 86, 4, 73, 67, 0, 47, 44, 0, 0, 7, 0,
- 188, 161, 204, 168, 144, 183, 149, 129, 164, 130, 112, 144, 112, 95, 123, 93, 78, 103, 74, 60, 81, 50, 36, 56, 16, 0, 16,
- 190, 160, 185, 170, 144, 165, 151, 128, 148, 132, 111, 130, 112, 95, 110, 93, 78, 92, 74, 59, 72, 50, 36, 48, 16, 0, 8,
- 192, 160, 167, 171, 143, 150, 153, 128, 134, 132, 111, 117, 112, 94, 100, 94, 77, 82, 74, 59, 63, 50, 36, 40, 14, 0, 0,
- 193, 159, 149, 172, 143, 134, 153, 127, 119, 133, 110, 103, 113, 94, 87, 93, 77, 72, 73, 59, 54, 50, 36, 32, 12, 0, 0,
- 195, 159, 131, 173, 142, 117, 153, 127, 104, 132, 110, 90, 113, 93, 76, 93, 76, 61, 74, 59, 45, 49, 36, 23, 9, 0, 0,
- 196, 158, 113, 174, 141, 101, 155, 126, 89, 133, 109, 76, 113, 93, 64, 94, 76, 51, 74, 58, 35, 49, 36, 14, 6, 0, 0,
- 197, 157, 92, 174, 141, 80, 154, 125, 71, 134, 108, 60, 114, 92, 50, 94, 75, 37, 73, 58, 22, 48, 36, 1, 5, 0, 0,
- 197, 157, 68, 175, 140, 59, 155, 124, 51, 134, 108, 41, 113, 91, 32, 94, 75, 21, 73, 57, 5, 48, 35, 0, 5, 0, 0,
- 198, 156, 46, 176, 140, 40, 155, 124, 32, 134, 107, 24, 114, 91, 14, 94, 75, 2, 73, 57, 0, 48, 36, 0, 3, 0, 0,
- 189, 140, 191, 168, 126, 172, 150, 112, 154, 131, 97, 134, 112, 82, 115, 94, 66, 96, 74, 49, 75, 51, 25, 50, 12, 0, 10,
- 191, 139, 173, 170, 125, 154, 152, 111, 138, 132, 96, 121, 113, 81, 103, 94, 66, 85, 74, 48, 66, 50, 26, 42, 12, 0, 1,
- 192, 139, 157, 171, 125, 140, 152, 111, 125, 132, 96, 109, 113, 81, 92, 94, 65, 76, 74, 48, 58, 50, 26, 35, 9, 0, 0,
- 193, 139, 140, 172, 124, 125, 153, 110, 112, 133, 95, 96, 113, 80, 82, 94, 65, 66, 74, 49, 50, 50, 26, 28, 7, 0, 0,
- 194, 138, 123, 172, 123, 109, 153, 110, 97, 133, 95, 84, 113, 80, 70, 94, 65, 56, 74, 48, 40, 50, 26, 20, 6, 0, 0,
- 194, 138, 105, 173, 123, 94, 153, 109, 83, 133, 94, 70, 112, 79, 59, 94, 64, 46, 74, 48, 31, 50, 26, 9, 4, 0, 0,
- 196, 138, 87, 174, 122, 77, 153, 109, 67, 133, 93, 56, 113, 79, 46, 94, 64, 34, 73, 48, 18, 49, 27, 0, 4, 0, 0,
- 196, 137, 65, 174, 122, 57, 154, 108, 49, 133, 93, 39, 113, 79, 29, 94, 64, 18, 74, 48, 3, 49, 27, 0, 2, 0, 0,
- 197, 137, 47, 175, 122, 40, 155, 108, 32, 133, 93, 23, 114, 79, 14, 94, 64, 1, 73, 48, 0, 48, 27, 0, 2, 0, 0,
- 189, 119, 177, 168, 106, 159, 150, 94, 142, 131, 81, 124, 113, 67, 107, 94, 53, 89, 74, 37, 69, 51, 11, 45, 6, 0, 3,
- 191, 119, 161, 170, 106, 144, 152, 94, 129, 132, 81, 112, 113, 67, 96, 94, 53, 79, 74, 37, 61, 51, 13, 38, 6, 0, 0,
- 192, 119, 146, 170, 106, 131, 152, 94, 117, 132, 80, 101, 112, 67, 85, 94, 53, 70, 74, 37, 53, 50, 14, 31, 4, 0, 0,
- 192, 119, 131, 171, 106, 117, 153, 94, 105, 132, 80, 89, 113, 67, 75, 94, 54, 61, 74, 38, 45, 51, 14, 23, 3, 0, 0,
- 193, 118, 114, 171, 106, 102, 153, 93, 90, 132, 80, 78, 113, 67, 65, 94, 53, 52, 74, 37, 36, 50, 15, 16, 1, 0, 0,
- 194, 118, 99, 172, 105, 89, 153, 93, 78, 132, 80, 66, 113, 67, 54, 94, 53, 42, 74, 38, 27, 50, 16, 5, 1, 0, 0,
- 194, 118, 82, 173, 105, 72, 153, 93, 63, 132, 79, 53, 113, 67, 42, 94, 53, 30, 74, 38, 15, 49, 16, 0, 0, 0, 0,
- 195, 117, 63, 173, 105, 55, 154, 93, 47, 133, 79, 37, 113, 66, 27, 94, 53, 15, 73, 38, 0, 48, 16, 0, 0, 0, 0,
- 195, 117, 46, 173, 104, 39, 154, 92, 32, 133, 79, 22, 113, 66, 13, 94, 53, 0, 73, 38, 0, 48, 17, 0, 0, 0, 0,
- 189, 96, 166, 168, 85, 147, 150, 74, 132, 131, 62, 115, 113, 51, 99, 94, 38, 82, 74, 21, 63, 51, 0, 40, 1, 0, 0,
- 190, 96, 150, 170, 85, 133, 152, 75, 119, 132, 63, 104, 113, 51, 88, 94, 38, 72, 75, 22, 55, 51, 0, 33, 1, 0, 0,
- 192, 96, 137, 170, 85, 121, 152, 74, 108, 132, 64, 94, 113, 52, 79, 94, 39, 64, 74, 23, 48, 50, 0, 26, 0, 0, 0,
- 192, 96, 122, 171, 86, 109, 152, 75, 96, 132, 63, 83, 113, 52, 69, 94, 39, 56, 74, 24, 41, 50, 0, 19, 0, 0, 0,
- 193, 96, 107, 171, 85, 96, 152, 75, 84, 132, 64, 72, 113, 52, 60, 94, 39, 47, 74, 24, 32, 50, 1, 10, 0, 0, 0,
- 193, 96, 93, 172, 85, 82, 152, 75, 72, 133, 63, 61, 113, 51, 49, 94, 39, 37, 73, 25, 23, 49, 2, 2, 0, 0, 0,
- 194, 96, 78, 172, 85, 68, 152, 75, 59, 132, 63, 49, 113, 52, 39, 94, 40, 26, 73, 25, 11, 48, 3, 0, 0, 0, 0,
- 194, 96, 60, 173, 85, 52, 153, 75, 44, 132, 64, 35, 112, 52, 25, 94, 40, 12, 73, 26, 0, 48, 4, 0, 0, 0, 0,
- 195, 96, 46, 173, 85, 38, 154, 74, 31, 133, 63, 22, 113, 52, 11, 93, 40, 0, 73, 26, 0, 47, 5, 0, 0, 0, 0,
- 188, 67, 153, 168, 58, 137, 151, 49, 122, 131, 39, 106, 113, 28, 90, 94, 13, 75, 75, 0, 57, 51, 0, 35, 0, 0, 0,
- 190, 68, 138, 170, 59, 123, 152, 50, 110, 132, 41, 96, 113, 29, 80, 94, 16, 66, 75, 0, 49, 50, 0, 27, 0, 0, 0,
- 191, 69, 126, 170, 59, 112, 151, 52, 100, 132, 42, 86, 113, 30, 73, 95, 17, 58, 75, 0, 42, 50, 0, 21, 0, 0, 0,
- 192, 70, 113, 170, 61, 100, 151, 52, 89, 132, 42, 77, 113, 31, 64, 94, 19, 50, 74, 1, 35, 50, 0, 14, 0, 0, 0,
- 192, 70, 100, 170, 62, 89, 151, 53, 77, 131, 43, 66, 112, 32, 54, 94, 20, 42, 74, 2, 27, 49, 0, 5, 0, 0, 0,
- 192, 71, 87, 171, 61, 77, 152, 53, 67, 131, 44, 57, 112, 33, 45, 94, 21, 33, 74, 4, 19, 49, 0, 1, 0, 0, 0,
- 193, 71, 74, 171, 62, 64, 152, 53, 55, 132, 44, 45, 113, 34, 34, 94, 22, 23, 73, 5, 7, 48, 0, 0, 0, 0, 0,
- 193, 70, 58, 172, 62, 50, 152, 54, 42, 132, 44, 32, 112, 35, 22, 93, 23, 10, 73, 6, 0, 47, 0, 0, 0, 0, 0,
- 193, 70, 45, 172, 62, 38, 153, 54, 31, 132, 44, 21, 112, 35, 9, 94, 23, 0, 73, 7, 0, 47, 0, 0, 0, 0, 0,
- 189, 26, 141, 169, 15, 126, 150, 2, 112, 131, 0, 97, 113, 0, 82, 94, 0, 67, 75, 0, 51, 50, 0, 29, 0, 0, 0,
- 190, 28, 128, 170, 18, 114, 151, 8, 101, 132, 0, 88, 113, 0, 74, 94, 0, 60, 75, 0, 44, 50, 0, 23, 0, 0, 0,
- 191, 30, 117, 170, 23, 104, 152, 11, 92, 132, 1, 79, 113, 0, 67, 95, 0, 53, 75, 0, 37, 50, 0, 17, 0, 0, 0,
- 191, 33, 105, 170, 26, 93, 151, 18, 83, 132, 6, 70, 112, 0, 58, 94, 0, 45, 75, 0, 30, 49, 0, 8, 0, 0, 0,
- 191, 34, 93, 170, 27, 82, 151, 20, 72, 131, 8, 61, 112, 0, 49, 94, 0, 38, 74, 0, 23, 49, 0, 4, 0, 0, 0,
- 191, 36, 82, 170, 29, 71, 151, 22, 63, 131, 11, 52, 112, 0, 41, 93, 0, 29, 74, 0, 14, 48, 0, 1, 0, 0, 0,
- 191, 38, 69, 170, 31, 60, 151, 24, 51, 131, 14, 41, 112, 1, 31, 93, 0, 19, 73, 0, 3, 48, 0, 0, 0, 0, 0,
- 192, 37, 56, 171, 31, 47, 152, 25, 40, 131, 17, 30, 112, 4, 19, 93, 0, 7, 73, 0, 0, 47, 0, 0, 0, 0, 0,
- 192, 38, 45, 171, 33, 36, 152, 26, 30, 131, 18, 21, 111, 7, 9, 93, 0, 0, 73, 0, 0, 47, 0, 0, 0, 0, 0,
- 149, 218, 248, 133, 194, 222, 119, 173, 198, 102, 151, 173, 86, 130, 148, 70, 108, 125, 53, 85, 100, 32, 59, 71, 0, 22, 33,
- 154, 216, 223, 137, 193, 200, 122, 172, 178, 106, 150, 156, 89, 128, 133, 73, 107, 112, 54, 85, 89, 31, 59, 63, 0, 22, 26,
- 159, 215, 202, 141, 192, 181, 126, 171, 161, 108, 149, 141, 90, 128, 121, 74, 107, 100, 55, 85, 80, 32, 58, 55, 0, 22, 19,
- 161, 213, 179, 144, 190, 160, 126, 170, 143, 109, 148, 125, 92, 127, 107, 74, 106, 89, 56, 84, 69, 32, 58, 46, 0, 21, 11,
- 163, 211, 156, 144, 189, 139, 129, 168, 125, 110, 147, 108, 93, 126, 92, 75, 105, 76, 57, 83, 58, 33, 58, 37, 0, 21, 1,
- 167, 211, 133, 147, 188, 120, 130, 167, 105, 110, 145, 92, 93, 125, 78, 76, 104, 64, 58, 83, 48, 33, 57, 27, 0, 21, 0,
- 169, 210, 108, 149, 187, 96, 131, 166, 86, 112, 144, 74, 94, 124, 62, 77, 103, 49, 58, 82, 35, 33, 57, 15, 0, 21, 0,
- 170, 209, 80, 151, 186, 71, 133, 165, 62, 114, 143, 52, 95, 123, 42, 77, 103, 32, 58, 81, 18, 33, 56, 0, 0, 21, 0,
- 173, 208, 55, 152, 186, 49, 134, 165, 41, 114, 143, 34, 95, 122, 25, 77, 102, 14, 58, 81, 0, 33, 56, 0, 0, 21, 0,
- 154, 195, 232, 137, 174, 207, 122, 156, 185, 105, 136, 163, 89, 116, 140, 73, 97, 117, 56, 76, 94, 35, 51, 66, 0, 13, 28,
- 158, 194, 209, 141, 174, 187, 125, 155, 167, 109, 135, 146, 91, 116, 125, 75, 96, 105, 57, 75, 83, 35, 50, 57, 0, 12, 21,
- 161, 193, 189, 144, 173, 169, 128, 154, 151, 110, 134, 132, 93, 115, 113, 77, 95, 94, 58, 75, 74, 35, 50, 50, 0, 12, 13,
- 164, 192, 168, 145, 171, 151, 129, 153, 134, 111, 133, 117, 94, 114, 100, 76, 95, 82, 58, 75, 64, 36, 50, 42, 0, 12, 5,
- 165, 191, 147, 147, 170, 131, 130, 152, 117, 113, 132, 102, 95, 113, 86, 77, 94, 71, 58, 74, 54, 35, 50, 33, 0, 13, 0,
- 167, 189, 126, 148, 169, 113, 132, 151, 100, 113, 131, 86, 96, 112, 73, 77, 93, 59, 59, 73, 43, 35, 49, 23, 0, 12, 0,
- 170, 189, 104, 150, 168, 91, 133, 150, 81, 114, 130, 69, 96, 111, 57, 78, 92, 46, 59, 73, 31, 35, 49, 11, 0, 13, 0,
- 171, 188, 78, 152, 168, 68, 134, 149, 60, 115, 130, 50, 96, 111, 41, 78, 92, 29, 60, 73, 15, 35, 49, 0, 0, 12, 0,
- 173, 187, 55, 153, 167, 47, 134, 149, 39, 115, 129, 33, 97, 110, 24, 79, 92, 13, 60, 72, 0, 35, 48, 0, 0, 12, 0,
- 157, 175, 217, 139, 157, 196, 125, 141, 175, 109, 122, 153, 92, 104, 132, 76, 86, 110, 59, 67, 88, 37, 43, 61, 1, 1, 23,
- 161, 174, 196, 144, 156, 176, 127, 140, 158, 110, 121, 137, 94, 104, 118, 77, 85, 98, 59, 67, 78, 37, 43, 53, 0, 2, 16,
- 163, 174, 178, 146, 156, 160, 130, 139, 143, 112, 121, 124, 95, 103, 106, 78, 85, 88, 60, 66, 69, 37, 42, 46, 0, 2, 7,
- 166, 173, 159, 147, 154, 142, 130, 138, 127, 113, 120, 111, 96, 103, 95, 78, 84, 77, 60, 66, 59, 37, 43, 37, 0, 2, 0,
- 166, 172, 139, 148, 154, 125, 131, 137, 112, 113, 120, 96, 96, 102, 81, 78, 84, 66, 60, 65, 50, 37, 42, 29, 0, 3, 0,
- 167, 171, 120, 149, 153, 107, 133, 137, 95, 114, 118, 81, 97, 101, 69, 79, 84, 56, 60, 65, 40, 37, 42, 19, 0, 3, 0,
- 170, 170, 99, 151, 152, 87, 134, 136, 77, 115, 118, 66, 97, 101, 55, 79, 83, 42, 61, 65, 28, 37, 42, 7, 0, 3, 0,
- 172, 170, 75, 152, 151, 65, 134, 135, 57, 115, 117, 48, 97, 100, 38, 79, 83, 27, 61, 64, 12, 36, 42, 0, 0, 3, 0,
- 172, 169, 55, 154, 151, 46, 135, 134, 40, 116, 116, 32, 97, 99, 21, 80, 82, 10, 61, 64, 0, 36, 41, 0, 0, 3, 0,
- 160, 154, 203, 143, 139, 182, 127, 124, 164, 111, 107, 143, 95, 91, 122, 78, 75, 103, 60, 57, 81, 39, 33, 56, 1, 0, 18,
- 163, 154, 184, 146, 138, 165, 130, 123, 148, 113, 107, 129, 96, 90, 110, 79, 74, 92, 61, 56, 72, 39, 34, 48, 2, 0, 9,
- 165, 154, 167, 147, 137, 149, 131, 122, 134, 114, 106, 117, 96, 90, 100, 79, 74, 82, 61, 56, 64, 39, 33, 40, 2, 0, 1,
- 166, 153, 150, 149, 137, 133, 132, 122, 119, 114, 106, 104, 97, 90, 88, 79, 74, 72, 61, 56, 55, 39, 34, 33, 0, 0, 0,
- 168, 152, 132, 149, 136, 117, 132, 121, 104, 114, 105, 90, 97, 89, 76, 79, 73, 62, 61, 56, 46, 38, 34, 25, 0, 0, 0,
- 169, 151, 114, 150, 135, 101, 133, 121, 90, 114, 104, 77, 97, 89, 65, 80, 73, 51, 61, 56, 36, 38, 34, 16, 0, 0, 0,
- 170, 150, 94, 151, 135, 83, 134, 120, 73, 115, 104, 62, 98, 88, 51, 80, 72, 39, 61, 56, 24, 38, 34, 3, 0, 0, 0,
- 172, 150, 72, 153, 134, 63, 135, 119, 55, 115, 103, 45, 98, 88, 36, 80, 72, 24, 61, 55, 9, 38, 34, 0, 0, 0, 0,
- 172, 150, 54, 153, 134, 47, 135, 119, 38, 116, 103, 30, 98, 87, 21, 80, 72, 8, 62, 55, 0, 37, 34, 0, 0, 0, 0,
- 162, 134, 190, 145, 120, 171, 129, 108, 153, 113, 93, 134, 97, 78, 115, 80, 63, 96, 62, 46, 75, 41, 23, 51, 0, 0, 11,
- 165, 134, 173, 147, 120, 154, 131, 107, 138, 114, 92, 120, 97, 78, 103, 80, 63, 85, 62, 46, 66, 40, 23, 43, 0, 0, 2,
- 166, 134, 157, 148, 120, 140, 132, 106, 125, 114, 92, 109, 97, 77, 93, 81, 63, 77, 62, 46, 58, 40, 24, 36, 0, 0, 0,
- 168, 133, 140, 149, 119, 125, 132, 106, 112, 115, 92, 97, 98, 77, 82, 81, 62, 67, 62, 46, 50, 40, 24, 29, 0, 0, 0,
- 168, 133, 123, 150, 119, 110, 133, 106, 97, 115, 91, 84, 98, 77, 70, 81, 62, 57, 62, 46, 41, 40, 24, 20, 0, 0, 0,
- 169, 132, 107, 150, 118, 94, 133, 105, 84, 115, 91, 72, 98, 76, 60, 80, 62, 47, 62, 46, 32, 39, 25, 11, 0, 0, 0,
- 171, 132, 89, 152, 118, 79, 135, 105, 69, 115, 90, 58, 98, 76, 47, 80, 62, 36, 62, 46, 21, 39, 25, 0, 0, 0, 0,
- 171, 132, 69, 153, 117, 60, 135, 104, 52, 116, 90, 42, 98, 76, 33, 81, 61, 21, 62, 46, 6, 38, 25, 0, 0, 0, 0,
- 172, 132, 54, 153, 118, 45, 135, 104, 38, 116, 90, 28, 98, 76, 18, 81, 61, 6, 62, 46, 0, 38, 25, 0, 0, 0, 0,
- 164, 115, 177, 146, 103, 159, 130, 91, 143, 114, 78, 125, 97, 65, 107, 81, 51, 89, 63, 34, 69, 41, 9, 46, 0, 0, 4,
- 166, 115, 161, 148, 103, 144, 132, 91, 129, 115, 78, 112, 98, 65, 96, 81, 51, 79, 63, 35, 61, 41, 11, 38, 0, 0, 0,
- 167, 115, 146, 150, 102, 131, 132, 91, 117, 115, 78, 101, 98, 65, 86, 81, 51, 71, 63, 35, 54, 41, 12, 32, 0, 0, 0,
- 168, 114, 132, 150, 103, 118, 133, 91, 105, 116, 78, 91, 98, 64, 76, 82, 51, 61, 63, 36, 46, 41, 13, 24, 0, 0, 0,
- 169, 114, 116, 150, 102, 103, 134, 90, 91, 116, 78, 79, 98, 65, 66, 81, 51, 53, 63, 36, 37, 40, 14, 17, 0, 0, 0,
- 169, 114, 101, 151, 101, 89, 134, 90, 79, 116, 77, 67, 98, 64, 56, 81, 51, 44, 63, 36, 29, 40, 15, 7, 0, 0, 0,
- 170, 114, 85, 152, 101, 75, 135, 90, 65, 116, 77, 54, 98, 64, 44, 81, 51, 32, 63, 36, 17, 39, 15, 0, 0, 0, 0,
- 172, 113, 66, 152, 101, 58, 135, 89, 49, 116, 77, 40, 99, 64, 30, 81, 51, 18, 62, 36, 3, 38, 16, 0, 0, 0, 0,
- 171, 113, 51, 153, 101, 44, 136, 89, 36, 116, 77, 28, 99, 64, 18, 81, 51, 5, 62, 36, 0, 38, 16, 0, 0, 0, 0,
- 165, 94, 166, 147, 82, 147, 132, 72, 132, 115, 61, 115, 98, 49, 99, 82, 36, 82, 64, 19, 64, 42, 0, 41, 0, 0, 0,
- 167, 93, 150, 150, 83, 134, 133, 73, 120, 116, 62, 104, 99, 49, 88, 82, 36, 72, 64, 20, 55, 41, 0, 33, 0, 0, 0,
- 169, 93, 137, 150, 83, 122, 134, 73, 109, 116, 61, 94, 99, 50, 80, 82, 37, 65, 64, 21, 49, 41, 0, 27, 0, 0, 0,
- 169, 94, 123, 150, 83, 110, 133, 73, 97, 116, 61, 83, 99, 50, 70, 82, 38, 57, 63, 23, 42, 41, 0, 20, 0, 0, 0,
- 169, 94, 109, 150, 84, 97, 134, 73, 85, 116, 62, 73, 99, 51, 61, 81, 38, 48, 63, 23, 33, 41, 1, 11, 0, 0, 0,
- 170, 94, 96, 150, 83, 84, 134, 73, 74, 116, 61, 62, 99, 50, 51, 82, 38, 39, 64, 23, 24, 40, 3, 4, 0, 0, 0,
- 171, 93, 79, 152, 82, 70, 135, 73, 61, 116, 62, 51, 98, 51, 40, 81, 38, 28, 63, 24, 14, 39, 4, 0, 0, 0, 0,
- 171, 94, 64, 152, 83, 55, 135, 73, 47, 116, 62, 37, 98, 50, 27, 81, 38, 15, 63, 24, 1, 39, 4, 0, 0, 0, 0,
- 172, 93, 51, 153, 82, 42, 135, 73, 35, 117, 62, 26, 99, 51, 16, 81, 39, 3, 63, 25, 0, 38, 5, 0, 0, 0, 0,
- 166, 68, 153, 148, 59, 137, 133, 49, 121, 115, 39, 106, 99, 28, 91, 82, 13, 75, 65, 0, 58, 42, 0, 36, 0, 0, 0,
- 168, 68, 139, 150, 59, 124, 134, 50, 110, 116, 40, 96, 99, 30, 81, 82, 16, 66, 64, 0, 50, 41, 0, 29, 0, 0, 0,
- 169, 69, 126, 150, 59, 113, 134, 51, 101, 117, 42, 87, 100, 30, 73, 82, 17, 59, 65, 0, 43, 41, 0, 23, 0, 0, 0,
- 169, 70, 115, 150, 61, 102, 134, 52, 89, 116, 42, 77, 99, 32, 65, 82, 19, 52, 64, 0, 36, 41, 0, 15, 0, 0, 0,
- 169, 70, 101, 150, 61, 90, 134, 52, 79, 116, 43, 68, 99, 32, 55, 82, 21, 43, 64, 2, 28, 41, 0, 6, 0, 0, 0,
- 170, 70, 89, 151, 62, 79, 134, 53, 69, 116, 44, 58, 99, 33, 46, 81, 21, 34, 64, 3, 20, 41, 0, 2, 0, 0, 0,
- 170, 71, 76, 152, 62, 66, 134, 53, 57, 116, 43, 46, 99, 33, 36, 82, 22, 24, 64, 5, 10, 40, 0, 0, 0, 0, 0,
- 171, 70, 61, 152, 62, 52, 135, 53, 44, 116, 44, 35, 99, 34, 24, 82, 22, 12, 63, 6, 0, 39, 0, 0, 0, 0, 0,
- 171, 71, 49, 153, 62, 41, 135, 54, 33, 117, 45, 25, 98, 34, 13, 81, 23, 0, 63, 7, 0, 39, 0, 0, 0, 0, 0,
- 167, 33, 142, 149, 24, 127, 134, 10, 113, 116, 0, 97, 100, 0, 83, 83, 0, 68, 65, 0, 52, 40, 0, 30, 0, 0, 0,
- 169, 33, 129, 150, 26, 115, 134, 17, 102, 116, 3, 89, 100, 0, 75, 83, 0, 60, 65, 0, 45, 40, 0, 24, 0, 0, 0,
- 169, 36, 118, 151, 27, 104, 134, 19, 93, 116, 7, 80, 100, 0, 67, 83, 0, 54, 65, 0, 38, 41, 0, 17, 0, 0, 0,
- 169, 39, 107, 150, 30, 94, 134, 22, 84, 116, 11, 71, 99, 0, 59, 83, 0, 46, 64, 0, 31, 40, 0, 9, 0, 0, 0,
- 169, 39, 95, 151, 31, 83, 134, 24, 73, 116, 15, 62, 100, 1, 51, 83, 0, 38, 64, 0, 24, 40, 0, 5, 0, 0, 0,
- 169, 41, 83, 151, 33, 73, 134, 26, 64, 117, 17, 54, 99, 4, 42, 82, 0, 30, 64, 0, 16, 40, 0, 1, 0, 0, 0,
- 170, 42, 71, 152, 34, 62, 134, 28, 53, 117, 19, 44, 99, 6, 33, 82, 0, 21, 63, 0, 4, 39, 0, 0, 0, 0, 0,
- 171, 42, 59, 152, 35, 50, 134, 29, 42, 117, 21, 32, 99, 9, 22, 82, 0, 9, 63, 0, 0, 38, 0, 0, 0, 0, 0,
- 172, 42, 48, 152, 36, 40, 135, 29, 32, 117, 21, 23, 99, 10, 12, 82, 0, 0, 63, 0, 0, 38, 0, 0, 0, 0, 0,
- 107, 207, 246, 96, 185, 220, 86, 165, 196, 73, 144, 171, 60, 123, 147, 46, 103, 125, 32, 82, 100, 9, 56, 71, 0, 20, 33,
- 115, 206, 221, 104, 184, 198, 92, 164, 178, 78, 143, 154, 64, 123, 133, 51, 102, 111, 34, 81, 89, 10, 56, 63, 0, 20, 27,
- 122, 204, 200, 108, 183, 180, 95, 163, 161, 82, 142, 140, 68, 122, 120, 54, 102, 101, 36, 81, 79, 11, 56, 55, 0, 20, 20,
- 125, 203, 179, 111, 181, 160, 97, 162, 143, 85, 141, 124, 70, 121, 107, 55, 101, 89, 38, 80, 69, 14, 55, 46, 0, 19, 10,
- 128, 202, 156, 113, 180, 140, 102, 161, 125, 87, 140, 108, 71, 120, 92, 56, 100, 76, 39, 79, 59, 14, 55, 38, 0, 20, 3,
- 132, 200, 135, 117, 179, 121, 103, 159, 106, 88, 139, 93, 73, 119, 79, 57, 100, 65, 41, 79, 49, 15, 54, 28, 0, 19, 0,
- 134, 200, 111, 119, 178, 98, 105, 158, 87, 89, 138, 76, 74, 118, 64, 58, 99, 51, 41, 78, 37, 16, 54, 17, 0, 19, 0,
- 137, 199, 85, 122, 177, 75, 108, 158, 66, 91, 137, 56, 75, 118, 46, 59, 98, 35, 42, 78, 22, 16, 54, 3, 0, 19, 0,
- 140, 198, 62, 125, 177, 55, 109, 158, 47, 92, 137, 40, 76, 117, 32, 59, 98, 21, 42, 78, 6, 16, 54, 0, 0, 18, 0,
- 118, 186, 231, 106, 167, 206, 93, 149, 184, 81, 130, 161, 67, 111, 139, 54, 92, 117, 39, 72, 93, 17, 48, 66, 0, 10, 29,
- 123, 185, 207, 110, 166, 186, 98, 148, 167, 85, 129, 145, 71, 111, 125, 56, 92, 104, 40, 72, 83, 18, 48, 57, 0, 10, 22,
- 128, 184, 188, 113, 165, 168, 102, 147, 151, 88, 128, 131, 73, 110, 113, 58, 91, 94, 42, 71, 74, 19, 48, 50, 0, 9, 15,
- 131, 183, 168, 116, 164, 151, 104, 146, 134, 89, 127, 117, 73, 109, 100, 58, 90, 83, 42, 71, 65, 20, 48, 42, 0, 9, 5,
- 134, 182, 148, 120, 163, 131, 105, 145, 118, 90, 126, 102, 75, 108, 86, 59, 90, 72, 43, 71, 55, 19, 47, 34, 0, 9, 0,
- 136, 181, 128, 122, 162, 115, 107, 144, 102, 92, 125, 87, 76, 107, 74, 61, 89, 60, 44, 70, 45, 20, 47, 24, 0, 8, 0,
- 139, 180, 106, 124, 161, 95, 109, 144, 83, 93, 124, 71, 77, 107, 60, 61, 89, 47, 44, 70, 33, 20, 47, 13, 0, 8, 0,
- 142, 179, 82, 125, 160, 72, 111, 143, 63, 94, 124, 54, 77, 106, 44, 61, 88, 32, 44, 69, 18, 20, 46, 0, 0, 8, 0,
- 143, 179, 62, 127, 160, 54, 111, 142, 47, 94, 124, 39, 78, 106, 29, 62, 88, 18, 45, 69, 3, 20, 46, 0, 0, 8, 0,
- 124, 167, 216, 112, 150, 194, 99, 134, 174, 87, 117, 153, 73, 100, 131, 58, 82, 110, 43, 64, 88, 23, 40, 61, 0, 0, 24,
- 129, 166, 195, 116, 150, 175, 103, 134, 158, 89, 116, 137, 75, 99, 118, 60, 82, 98, 44, 63, 78, 23, 40, 53, 0, 0, 17,
- 132, 166, 177, 119, 149, 160, 106, 133, 143, 90, 115, 124, 76, 99, 107, 61, 81, 88, 45, 63, 69, 24, 40, 46, 0, 0, 9,
- 136, 166, 159, 121, 148, 143, 107, 132, 126, 92, 115, 111, 77, 98, 94, 62, 81, 78, 46, 63, 60, 23, 40, 38, 0, 0, 0,
- 138, 164, 140, 122, 147, 125, 108, 131, 111, 93, 114, 97, 79, 98, 82, 63, 80, 67, 46, 62, 50, 24, 40, 29, 0, 0, 0,
- 139, 163, 122, 124, 146, 109, 110, 131, 96, 94, 114, 83, 79, 97, 70, 63, 81, 57, 46, 62, 41, 24, 40, 21, 0, 0, 0,
- 141, 163, 101, 126, 145, 90, 111, 130, 79, 95, 113, 68, 79, 96, 56, 63, 80, 44, 47, 62, 30, 23, 40, 10, 0, 0, 0,
- 144, 162, 79, 127, 145, 70, 112, 129, 60, 95, 112, 51, 79, 96, 41, 64, 79, 30, 47, 61, 15, 23, 40, 0, 0, 0, 0,
- 145, 162, 60, 129, 145, 52, 113, 129, 46, 96, 112, 37, 79, 95, 27, 64, 79, 16, 47, 61, 1, 23, 39, 0, 0, 0, 0,
- 131, 147, 202, 117, 133, 181, 105, 119, 162, 91, 103, 142, 77, 87, 122, 62, 71, 102, 47, 54, 81, 26, 31, 56, 0, 0, 18,
- 135, 147, 183, 120, 132, 164, 107, 118, 147, 93, 102, 128, 78, 87, 110, 63, 71, 92, 47, 54, 72, 26, 31, 48, 0, 0, 10,
- 138, 147, 166, 123, 131, 149, 108, 118, 133, 94, 102, 116, 79, 86, 100, 64, 71, 82, 48, 54, 64, 27, 31, 41, 0, 0, 2,
- 139, 146, 149, 124, 131, 134, 111, 117, 119, 94, 101, 103, 79, 86, 88, 64, 70, 72, 48, 53, 55, 27, 31, 33, 0, 0, 0,
- 141, 146, 132, 125, 131, 117, 111, 117, 104, 95, 101, 91, 80, 86, 77, 65, 70, 62, 48, 53, 46, 26, 31, 25, 0, 0, 0,
- 143, 145, 115, 126, 130, 101, 112, 116, 90, 96, 100, 78, 80, 85, 65, 65, 70, 52, 49, 53, 37, 27, 32, 17, 0, 0, 0,
- 144, 144, 96, 128, 129, 85, 112, 115, 75, 97, 100, 64, 81, 85, 52, 65, 69, 40, 49, 53, 26, 26, 31, 5, 0, 0, 0,
- 146, 144, 76, 129, 129, 67, 114, 115, 58, 97, 99, 48, 82, 84, 38, 66, 69, 27, 49, 53, 12, 26, 32, 0, 0, 0, 0,
- 146, 144, 59, 130, 128, 51, 114, 114, 43, 98, 99, 35, 82, 84, 25, 66, 69, 13, 49, 53, 0, 26, 32, 0, 0, 0, 0,
- 135, 129, 189, 122, 115, 170, 107, 103, 152, 94, 89, 133, 79, 74, 114, 64, 60, 95, 49, 43, 75, 29, 20, 51, 0, 0, 12,
- 138, 129, 171, 124, 115, 153, 110, 103, 138, 95, 89, 120, 81, 74, 103, 66, 60, 86, 50, 44, 67, 28, 21, 43, 0, 0, 3,
- 140, 129, 156, 125, 115, 140, 111, 103, 125, 96, 89, 109, 81, 74, 93, 67, 60, 76, 50, 44, 59, 29, 22, 36, 0, 0, 0,
- 142, 128, 140, 127, 115, 125, 112, 102, 112, 97, 88, 97, 82, 74, 83, 67, 60, 67, 50, 44, 51, 29, 22, 29, 0, 0, 0,
- 142, 128, 124, 127, 114, 111, 113, 102, 98, 98, 88, 85, 82, 74, 71, 66, 60, 58, 50, 44, 42, 29, 22, 21, 0, 0, 0,
- 144, 127, 108, 128, 114, 96, 113, 101, 85, 98, 87, 73, 82, 74, 61, 67, 60, 48, 50, 44, 33, 28, 23, 12, 0, 0, 0,
- 145, 127, 91, 129, 114, 81, 115, 101, 71, 98, 87, 60, 82, 73, 48, 67, 59, 37, 50, 44, 22, 29, 23, 1, 0, 0, 0,
- 147, 127, 73, 130, 113, 63, 115, 101, 55, 98, 87, 45, 83, 73, 35, 67, 59, 24, 50, 44, 10, 28, 24, 0, 0, 0, 0,
- 147, 127, 58, 131, 113, 49, 115, 100, 42, 99, 86, 33, 83, 73, 23, 67, 59, 10, 50, 44, 0, 27, 24, 0, 0, 0, 0,
- 138, 110, 177, 124, 99, 159, 110, 88, 142, 96, 75, 125, 82, 62, 107, 66, 48, 89, 51, 33, 70, 30, 8, 46, 0, 0, 5,
- 142, 111, 160, 127, 99, 144, 113, 88, 130, 98, 75, 112, 82, 62, 96, 68, 49, 80, 51, 33, 61, 30, 10, 39, 0, 0, 0,
- 143, 111, 146, 128, 99, 131, 114, 88, 118, 98, 75, 101, 83, 62, 86, 68, 49, 71, 52, 33, 54, 30, 11, 32, 0, 0, 0,
- 144, 111, 132, 128, 99, 118, 113, 88, 106, 99, 75, 91, 83, 62, 77, 68, 49, 62, 52, 34, 46, 30, 12, 25, 0, 0, 0,
- 144, 111, 117, 129, 98, 104, 114, 87, 92, 99, 75, 80, 83, 62, 67, 68, 49, 53, 51, 34, 38, 30, 13, 18, 0, 0, 0,
- 145, 111, 103, 130, 98, 91, 114, 87, 80, 99, 75, 68, 83, 63, 57, 68, 50, 45, 51, 34, 30, 30, 14, 8, 0, 0, 0,
- 146, 110, 87, 131, 98, 76, 115, 87, 67, 99, 75, 56, 83, 62, 45, 68, 49, 33, 52, 35, 19, 30, 15, 2, 0, 0, 0,
- 148, 110, 70, 131, 98, 60, 116, 86, 52, 99, 74, 43, 84, 62, 33, 69, 49, 21, 52, 35, 6, 29, 15, 0, 0, 0, 0,
- 148, 110, 56, 132, 97, 48, 117, 87, 40, 100, 75, 31, 84, 62, 22, 68, 49, 9, 51, 35, 0, 28, 15, 0, 0, 0, 0,
- 142, 91, 166, 126, 80, 148, 113, 71, 132, 98, 59, 115, 83, 47, 99, 69, 34, 82, 53, 17, 64, 32, 0, 41, 0, 0, 0,
- 143, 91, 150, 128, 81, 135, 114, 71, 120, 99, 60, 104, 85, 48, 89, 69, 35, 73, 53, 19, 56, 32, 0, 34, 0, 0, 0,
- 145, 91, 137, 129, 81, 122, 115, 71, 109, 100, 60, 94, 85, 48, 81, 69, 35, 65, 53, 19, 49, 32, 0, 28, 0, 0, 0,
- 146, 92, 124, 130, 81, 110, 115, 71, 98, 100, 60, 84, 85, 49, 71, 69, 36, 57, 53, 21, 42, 32, 0, 21, 0, 0, 0,
- 147, 91, 110, 130, 81, 97, 115, 71, 86, 100, 60, 74, 84, 49, 62, 69, 36, 48, 53, 22, 34, 32, 0, 13, 0, 0, 0,
- 147, 92, 97, 130, 81, 85, 116, 72, 76, 100, 60, 63, 85, 49, 52, 69, 37, 40, 53, 22, 26, 31, 1, 5, 0, 0, 0,
- 148, 92, 82, 131, 81, 71, 116, 71, 62, 100, 60, 53, 84, 49, 42, 69, 37, 30, 52, 23, 16, 31, 2, 0, 0, 0, 0,
- 148, 91, 67, 132, 81, 57, 117, 71, 49, 100, 60, 39, 84, 49, 30, 69, 37, 18, 52, 23, 2, 30, 2, 0, 0, 0, 0,
- 149, 91, 54, 132, 81, 46, 118, 71, 39, 101, 60, 29, 85, 49, 19, 69, 37, 6, 52, 23, 0, 29, 3, 0, 0, 0, 0,
- 143, 68, 153, 128, 59, 137, 115, 49, 122, 99, 39, 107, 85, 28, 91, 70, 13, 75, 54, 0, 58, 32, 0, 36, 0, 0, 0,
- 146, 68, 140, 131, 59, 125, 116, 51, 111, 100, 40, 97, 85, 29, 82, 70, 15, 67, 54, 0, 50, 32, 0, 29, 0, 0, 0,
- 147, 68, 127, 131, 59, 114, 117, 51, 102, 101, 41, 88, 86, 30, 74, 70, 17, 60, 54, 0, 44, 32, 0, 23, 0, 0, 0,
- 147, 70, 115, 131, 60, 103, 116, 52, 91, 100, 42, 78, 85, 32, 65, 70, 19, 53, 54, 1, 38, 32, 0, 17, 0, 0, 0,
- 147, 70, 103, 131, 61, 91, 117, 53, 81, 101, 43, 69, 86, 32, 57, 70, 20, 44, 54, 2, 30, 32, 0, 7, 0, 0, 0,
- 148, 70, 91, 132, 61, 80, 117, 52, 70, 101, 43, 59, 85, 33, 48, 70, 21, 36, 53, 4, 22, 32, 0, 3, 0, 0, 0,
- 148, 70, 78, 132, 62, 68, 117, 53, 58, 101, 43, 48, 85, 34, 38, 70, 22, 26, 53, 6, 12, 31, 0, 0, 0, 0, 0,
- 149, 71, 64, 132, 62, 54, 118, 54, 46, 101, 44, 37, 85, 34, 27, 69, 23, 15, 53, 7, 1, 30, 0, 0, 0, 0, 0,
- 150, 70, 53, 134, 61, 44, 118, 54, 36, 101, 44, 28, 85, 35, 17, 69, 23, 4, 52, 8, 0, 30, 0, 0, 0, 0, 0,
- 145, 38, 143, 130, 29, 128, 117, 18, 114, 101, 3, 98, 87, 0, 84, 72, 0, 69, 54, 0, 53, 30, 0, 31, 0, 0, 0,
- 147, 38, 130, 132, 30, 116, 117, 22, 103, 101, 8, 89, 87, 0, 76, 72, 0, 62, 54, 0, 46, 30, 0, 24, 0, 0, 0,
- 148, 40, 119, 132, 31, 105, 117, 23, 94, 101, 13, 81, 87, 0, 68, 71, 0, 55, 54, 0, 39, 30, 0, 18, 0, 0, 0,
- 148, 42, 108, 132, 34, 96, 117, 25, 85, 102, 15, 73, 86, 2, 60, 71, 0, 47, 54, 0, 33, 30, 0, 11, 0, 0, 0,
- 148, 43, 96, 133, 35, 85, 117, 28, 75, 102, 18, 64, 87, 5, 52, 71, 0, 40, 54, 0, 25, 30, 0, 5, 0, 0, 0,
- 149, 44, 85, 132, 36, 75, 118, 29, 66, 101, 20, 55, 86, 8, 44, 70, 0, 32, 53, 0, 18, 29, 0, 2, 0, 0, 0,
- 149, 45, 74, 133, 37, 64, 118, 31, 55, 102, 21, 45, 85, 10, 34, 70, 0, 22, 53, 0, 6, 28, 0, 0, 0, 0, 0,
- 150, 46, 61, 133, 39, 52, 118, 31, 44, 102, 23, 34, 85, 12, 24, 70, 0, 12, 52, 0, 0, 28, 0, 0, 0, 0, 0,
- 150, 46, 51, 133, 40, 42, 119, 32, 35, 102, 24, 25, 85, 13, 14, 70, 0, 1, 52, 0, 0, 27, 0, 0, 0, 0, 0,
- 53, 198, 244, 49, 177, 218, 41, 158, 195, 32, 138, 171, 22, 118, 147, 11, 98, 124, 0, 78, 100, 0, 54, 71, 0, 18, 34,
- 69, 196, 220, 64, 175, 196, 54, 157, 176, 45, 137, 154, 32, 117, 133, 19, 98, 111, 0, 78, 89, 0, 53, 63, 0, 17, 27,
- 80, 195, 198, 69, 175, 179, 60, 156, 159, 50, 136, 139, 38, 116, 120, 25, 98, 101, 4, 77, 80, 0, 53, 55, 0, 17, 21,
- 84, 193, 177, 75, 173, 159, 64, 155, 142, 55, 135, 124, 41, 116, 107, 27, 97, 89, 9, 76, 70, 0, 53, 47, 0, 17, 11,
- 89, 193, 157, 79, 172, 140, 70, 154, 125, 57, 134, 109, 44, 115, 92, 32, 96, 76, 13, 76, 59, 0, 52, 39, 0, 16, 4,
- 94, 191, 135, 85, 171, 121, 72, 152, 108, 60, 133, 94, 47, 114, 80, 32, 95, 65, 15, 76, 49, 0, 52, 29, 0, 16, 0,
- 98, 190, 113, 87, 170, 100, 76, 152, 89, 62, 132, 77, 49, 113, 65, 35, 95, 52, 18, 75, 37, 0, 52, 18, 0, 15, 0,
- 103, 190, 89, 90, 169, 80, 78, 151, 70, 64, 132, 60, 51, 113, 49, 37, 94, 38, 20, 75, 25, 0, 52, 5, 0, 15, 0,
- 106, 189, 69, 93, 169, 61, 80, 151, 53, 66, 131, 45, 52, 113, 36, 37, 94, 25, 19, 74, 11, 0, 51, 0, 0, 15, 0,
- 76, 178, 229, 68, 159, 205, 61, 142, 183, 50, 124, 160, 40, 106, 138, 28, 88, 116, 12, 69, 93, 0, 45, 66, 0, 5, 29,
- 86, 177, 207, 78, 158, 184, 67, 142, 166, 56, 123, 145, 45, 106, 125, 31, 88, 105, 16, 69, 83, 0, 45, 58, 0, 6, 22,
- 93, 176, 187, 81, 158, 168, 71, 141, 150, 61, 123, 131, 47, 105, 113, 35, 87, 94, 20, 68, 74, 0, 45, 51, 0, 5, 16,
- 98, 175, 168, 84, 157, 150, 75, 140, 134, 63, 122, 117, 50, 104, 100, 37, 87, 83, 21, 68, 65, 0, 45, 42, 0, 4, 7,
- 100, 174, 149, 89, 155, 132, 76, 139, 117, 65, 121, 102, 53, 104, 87, 39, 86, 72, 23, 67, 55, 0, 45, 34, 0, 3, 0,
- 103, 173, 130, 92, 155, 115, 80, 138, 102, 68, 120, 88, 53, 103, 75, 40, 86, 61, 24, 67, 45, 0, 45, 25, 0, 3, 0,
- 107, 172, 108, 95, 154, 96, 82, 137, 85, 70, 119, 73, 55, 102, 61, 42, 85, 49, 25, 67, 34, 0, 45, 14, 0, 3, 0,
- 110, 172, 86, 97, 153, 76, 85, 137, 67, 70, 119, 57, 56, 102, 46, 42, 84, 35, 26, 66, 21, 0, 44, 1, 0, 3, 0,
- 112, 171, 67, 98, 153, 59, 86, 137, 52, 71, 119, 44, 58, 102, 34, 44, 85, 22, 27, 66, 7, 0, 44, 0, 0, 3, 0,
- 90, 160, 215, 81, 144, 193, 70, 129, 173, 61, 112, 151, 49, 95, 131, 37, 79, 109, 22, 61, 87, 0, 38, 61, 0, 0, 25,
- 96, 160, 194, 86, 143, 174, 75, 128, 157, 65, 112, 137, 53, 95, 117, 40, 78, 98, 25, 60, 78, 0, 38, 53, 0, 0, 17,
- 100, 159, 177, 89, 143, 159, 79, 128, 143, 67, 111, 124, 55, 95, 107, 42, 78, 89, 27, 60, 70, 2, 38, 46, 0, 0, 9,
- 104, 158, 159, 92, 142, 143, 81, 127, 127, 69, 110, 110, 56, 94, 94, 43, 78, 78, 28, 60, 60, 2, 38, 38, 0, 0, 1,
- 107, 157, 140, 94, 141, 125, 82, 126, 112, 71, 110, 97, 59, 94, 82, 45, 77, 67, 29, 59, 51, 4, 37, 30, 0, 0, 0,
- 110, 156, 122, 97, 140, 109, 85, 125, 97, 72, 109, 83, 58, 93, 71, 45, 77, 57, 29, 60, 42, 5, 38, 22, 0, 0, 0,
- 111, 156, 103, 99, 139, 91, 87, 125, 81, 73, 108, 69, 60, 92, 58, 46, 77, 45, 30, 59, 31, 5, 38, 12, 0, 0, 0,
- 115, 156, 82, 101, 140, 73, 88, 124, 63, 74, 108, 53, 60, 92, 44, 46, 76, 32, 31, 59, 18, 6, 37, 0, 0, 0, 0,
- 116, 155, 65, 102, 139, 58, 89, 124, 49, 75, 108, 41, 61, 92, 32, 48, 76, 21, 31, 59, 6, 5, 37, 0, 0, 0, 0,
- 100, 141, 201, 88, 127, 181, 79, 114, 162, 69, 99, 142, 57, 83, 122, 44, 68, 102, 30, 51, 81, 7, 28, 56, 0, 0, 19,
- 105, 141, 182, 94, 127, 163, 83, 114, 146, 71, 98, 128, 59, 83, 110, 46, 68, 91, 31, 51, 72, 10, 28, 48, 0, 0, 11,
- 108, 141, 166, 96, 127, 149, 85, 113, 133, 73, 98, 116, 60, 83, 99, 46, 68, 82, 32, 51, 64, 11, 29, 41, 0, 0, 2,
- 111, 141, 149, 98, 126, 134, 88, 112, 119, 74, 97, 103, 61, 83, 88, 48, 67, 72, 33, 51, 56, 11, 29, 34, 0, 0, 0,
- 112, 140, 132, 100, 125, 118, 89, 112, 105, 75, 97, 91, 62, 82, 77, 49, 68, 62, 33, 51, 47, 12, 29, 26, 0, 0, 0,
- 115, 140, 116, 102, 125, 103, 90, 111, 91, 76, 96, 78, 62, 82, 65, 49, 67, 52, 34, 51, 38, 13, 29, 18, 0, 0, 0,
- 117, 139, 97, 103, 124, 87, 91, 111, 77, 78, 96, 65, 63, 81, 54, 49, 67, 41, 34, 51, 27, 12, 29, 7, 0, 0, 0,
- 119, 138, 78, 105, 124, 69, 92, 110, 60, 78, 95, 50, 65, 81, 40, 50, 67, 29, 34, 51, 15, 13, 30, 0, 0, 0, 0,
- 120, 138, 64, 106, 124, 54, 93, 110, 47, 78, 95, 38, 65, 81, 29, 50, 66, 17, 34, 50, 2, 13, 29, 0, 0, 0, 0,
- 107, 124, 189, 96, 111, 169, 85, 99, 152, 73, 85, 132, 61, 71, 114, 48, 57, 95, 34, 41, 75, 14, 18, 51, 0, 0, 13,
- 111, 124, 171, 100, 111, 153, 88, 99, 137, 75, 85, 120, 63, 72, 103, 50, 58, 85, 36, 41, 66, 15, 19, 43, 0, 0, 4,
- 113, 124, 156, 101, 111, 139, 90, 99, 125, 77, 85, 109, 64, 71, 93, 51, 57, 77, 36, 42, 59, 17, 20, 37, 0, 0, 0,
- 115, 124, 140, 103, 111, 125, 90, 99, 112, 78, 85, 97, 64, 71, 82, 52, 57, 67, 36, 42, 50, 16, 20, 30, 0, 0, 0,
- 117, 123, 125, 104, 110, 111, 92, 98, 99, 79, 85, 86, 65, 71, 72, 51, 58, 59, 37, 42, 43, 17, 21, 22, 0, 0, 0,
- 118, 123, 110, 105, 110, 97, 93, 98, 86, 78, 84, 74, 66, 71, 62, 52, 57, 49, 37, 42, 34, 17, 22, 14, 0, 0, 0,
- 120, 123, 93, 106, 109, 82, 94, 97, 72, 80, 84, 61, 66, 71, 50, 52, 57, 38, 37, 42, 24, 17, 22, 2, 0, 0, 0,
- 121, 122, 75, 108, 109, 66, 95, 97, 58, 80, 84, 48, 66, 71, 37, 52, 57, 26, 37, 42, 12, 16, 22, 0, 0, 0, 0,
- 122, 123, 62, 108, 109, 52, 95, 97, 45, 81, 84, 36, 67, 70, 26, 52, 57, 14, 37, 42, 0, 15, 22, 0, 0, 0, 0,
- 113, 107, 177, 102, 96, 159, 89, 85, 141, 78, 72, 124, 65, 60, 107, 52, 46, 89, 37, 30, 70, 18, 5, 46, 0, 0, 6,
- 116, 107, 160, 104, 96, 144, 92, 85, 129, 80, 72, 112, 67, 60, 96, 53, 47, 80, 38, 31, 62, 19, 7, 39, 0, 0, 0,
- 118, 107, 147, 105, 96, 131, 93, 85, 118, 80, 72, 101, 67, 60, 87, 54, 47, 71, 39, 31, 54, 19, 8, 32, 0, 0, 0,
- 119, 107, 132, 106, 96, 118, 94, 85, 106, 81, 73, 91, 67, 60, 77, 54, 47, 63, 39, 32, 47, 20, 9, 25, 0, 0, 0,
- 119, 107, 118, 106, 95, 105, 94, 85, 93, 81, 72, 80, 68, 60, 68, 54, 47, 54, 39, 32, 39, 20, 11, 18, 0, 0, 0,
- 121, 107, 104, 107, 96, 92, 95, 84, 80, 81, 72, 69, 68, 61, 58, 54, 48, 46, 39, 33, 31, 20, 12, 9, 0, 0, 0,
- 123, 107, 88, 108, 95, 77, 96, 84, 68, 82, 72, 57, 68, 60, 46, 54, 47, 35, 39, 33, 20, 19, 13, 2, 0, 0, 0,
- 123, 106, 72, 110, 95, 63, 96, 84, 54, 82, 72, 45, 69, 60, 35, 55, 48, 23, 39, 33, 9, 18, 14, 0, 0, 0, 0,
- 125, 106, 60, 110, 94, 50, 98, 84, 42, 83, 72, 34, 69, 60, 25, 55, 48, 12, 39, 33, 0, 17, 13, 0, 0, 0, 0,
- 118, 89, 165, 105, 79, 148, 93, 69, 132, 81, 57, 115, 68, 45, 99, 55, 32, 82, 41, 15, 64, 21, 0, 41, 0, 0, 0,
- 120, 89, 150, 107, 79, 135, 96, 69, 121, 82, 58, 105, 70, 46, 89, 56, 34, 73, 41, 17, 56, 21, 0, 34, 0, 0, 0,
- 121, 89, 137, 108, 79, 123, 96, 69, 109, 82, 58, 95, 70, 47, 81, 56, 34, 66, 41, 18, 49, 21, 0, 28, 0, 0, 0,
- 122, 90, 124, 109, 79, 110, 96, 69, 99, 83, 58, 85, 70, 47, 72, 56, 35, 58, 41, 19, 42, 21, 0, 22, 0, 0, 0,
- 123, 90, 111, 110, 79, 98, 97, 69, 87, 83, 59, 75, 70, 47, 63, 56, 35, 50, 41, 20, 35, 21, 0, 14, 0, 0, 0,
- 123, 90, 98, 110, 79, 87, 97, 70, 76, 84, 58, 64, 70, 48, 53, 56, 36, 41, 40, 21, 26, 21, 0, 5, 0, 0, 0,
- 125, 89, 84, 111, 79, 73, 97, 69, 64, 84, 59, 54, 70, 48, 43, 56, 36, 31, 40, 22, 17, 20, 1, 1, 0, 0, 0,
- 125, 89, 69, 112, 79, 60, 98, 70, 51, 84, 59, 42, 70, 48, 32, 56, 36, 20, 41, 22, 5, 19, 2, 0, 0, 0, 0,
- 126, 89, 57, 112, 79, 49, 99, 70, 41, 84, 59, 32, 70, 48, 22, 56, 36, 10, 40, 22, 0, 18, 2, 0, 0, 0, 0,
- 121, 67, 154, 108, 58, 138, 97, 50, 124, 84, 39, 107, 71, 28, 92, 58, 12, 76, 43, 0, 59, 20, 0, 37, 0, 0, 0,
- 124, 68, 140, 111, 59, 126, 98, 50, 112, 84, 40, 98, 71, 29, 83, 58, 15, 67, 42, 0, 51, 20, 0, 30, 0, 0, 0,
- 124, 68, 129, 111, 59, 114, 99, 51, 102, 86, 41, 88, 71, 30, 75, 58, 17, 60, 42, 0, 45, 20, 0, 24, 0, 0, 0,
- 125, 70, 116, 111, 60, 103, 99, 51, 92, 85, 41, 79, 71, 31, 66, 58, 19, 53, 42, 3, 38, 20, 0, 17, 0, 0, 0,
- 125, 70, 104, 111, 61, 93, 99, 52, 81, 85, 43, 69, 72, 32, 58, 58, 20, 45, 42, 4, 31, 20, 0, 8, 0, 0, 0,
- 126, 70, 92, 111, 61, 81, 99, 52, 71, 85, 42, 60, 71, 33, 49, 57, 21, 37, 42, 6, 23, 20, 0, 3, 0, 0, 0,
- 126, 70, 79, 112, 61, 70, 99, 53, 60, 85, 43, 50, 71, 33, 39, 57, 22, 28, 41, 7, 13, 19, 0, 0, 0, 0, 0,
- 127, 71, 66, 113, 62, 56, 100, 53, 48, 86, 44, 39, 71, 34, 29, 57, 23, 18, 41, 8, 2, 18, 0, 0, 0, 0, 0,
- 128, 70, 55, 114, 62, 46, 100, 54, 39, 86, 44, 30, 71, 34, 20, 57, 23, 7, 41, 9, 0, 18, 0, 0, 0, 0, 0,
- 124, 41, 145, 111, 32, 128, 99, 23, 114, 86, 10, 100, 73, 0, 85, 60, 0, 71, 43, 0, 54, 17, 0, 32, 0, 0, 0,
- 126, 42, 131, 113, 33, 117, 100, 25, 104, 86, 14, 90, 73, 0, 77, 60, 0, 63, 44, 0, 47, 18, 0, 25, 0, 0, 0,
- 127, 43, 120, 113, 34, 106, 101, 26, 95, 86, 17, 82, 73, 2, 69, 59, 0, 56, 43, 0, 41, 18, 0, 19, 0, 0, 0,
- 127, 45, 109, 113, 37, 97, 101, 28, 85, 86, 19, 74, 73, 5, 61, 59, 0, 48, 43, 0, 34, 19, 0, 11, 0, 0, 0,
- 127, 46, 98, 114, 38, 86, 100, 30, 76, 87, 21, 65, 73, 9, 54, 59, 0, 41, 43, 0, 26, 18, 0, 5, 0, 0, 0,
- 127, 47, 87, 113, 39, 76, 101, 31, 67, 86, 22, 56, 72, 11, 45, 59, 0, 33, 43, 0, 19, 18, 0, 2, 0, 0, 0,
- 128, 48, 75, 114, 39, 65, 101, 33, 56, 86, 23, 46, 72, 12, 36, 58, 0, 24, 42, 0, 9, 17, 0, 0, 0, 0, 0,
- 129, 48, 63, 114, 41, 54, 102, 33, 46, 87, 24, 36, 72, 14, 26, 58, 1, 14, 42, 0, 2, 16, 0, 0, 0, 0, 0,
- 128, 48, 53, 114, 41, 44, 102, 34, 37, 87, 25, 27, 72, 15, 17, 58, 1, 3, 41, 0, 0, 15, 0, 0, 0, 0, 0,
- 0, 189, 242, 0, 169, 217, 0, 151, 194, 0, 132, 170, 0, 113, 147, 0, 94, 123, 0, 74, 99, 0, 51, 71, 0, 15, 34,
- 1, 187, 219, 1, 167, 195, 0, 150, 175, 0, 131, 153, 0, 113, 132, 0, 94, 111, 0, 74, 89, 0, 50, 63, 0, 13, 28,
- 1, 186, 198, 1, 167, 178, 0, 149, 158, 0, 130, 139, 0, 111, 119, 0, 93, 100, 0, 74, 80, 0, 50, 55, 0, 13, 22,
- 1, 185, 176, 1, 165, 159, 1, 148, 142, 0, 129, 123, 0, 111, 106, 0, 93, 89, 0, 73, 70, 0, 50, 47, 0, 13, 13,
- 1, 184, 157, 1, 164, 141, 1, 147, 125, 0, 128, 110, 0, 110, 93, 0, 92, 77, 0, 73, 60, 0, 50, 39, 0, 12, 5,
- 25, 182, 137, 25, 163, 122, 17, 146, 109, 0, 128, 96, 0, 110, 81, 0, 92, 66, 0, 73, 51, 0, 50, 30, 0, 10, 0,
- 42, 181, 114, 35, 163, 102, 30, 145, 91, 14, 127, 80, 0, 109, 67, 0, 91, 53, 0, 72, 39, 0, 50, 19, 0, 10, 0,
- 52, 181, 92, 43, 162, 83, 32, 145, 73, 19, 126, 63, 0, 108, 52, 0, 90, 40, 0, 72, 27, 0, 50, 7, 0, 10, 0,
- 57, 181, 74, 48, 162, 66, 37, 144, 57, 24, 126, 49, 7, 108, 40, 0, 90, 29, 0, 72, 15, 0, 49, 0, 0, 10, 0,
- 1, 170, 227, 1, 152, 203, 0, 136, 182, 0, 119, 159, 0, 101, 137, 0, 84, 115, 0, 65, 92, 0, 43, 66, 0, 1, 29,
- 1, 169, 206, 1, 151, 184, 1, 136, 165, 0, 118, 144, 0, 102, 125, 0, 84, 105, 0, 65, 83, 0, 43, 58, 0, 0, 22,
- 29, 168, 186, 21, 151, 167, 14, 135, 150, 4, 118, 131, 0, 101, 112, 0, 83, 94, 0, 65, 75, 0, 43, 51, 0, 0, 16,
- 41, 167, 167, 33, 150, 150, 31, 134, 134, 19, 117, 117, 4, 100, 100, 0, 83, 83, 0, 65, 65, 0, 42, 43, 0, 0, 8,
- 48, 167, 149, 41, 149, 133, 33, 133, 118, 25, 116, 103, 13, 99, 88, 0, 83, 73, 0, 65, 56, 0, 42, 35, 0, 0, 0,
- 58, 165, 130, 49, 148, 115, 42, 132, 103, 31, 115, 89, 18, 99, 75, 0, 82, 61, 0, 64, 46, 0, 42, 26, 0, 0, 0,
- 62, 164, 110, 55, 147, 97, 45, 132, 87, 35, 115, 75, 22, 98, 63, 5, 82, 50, 0, 64, 36, 0, 42, 16, 0, 0, 0,
- 69, 164, 89, 60, 147, 78, 50, 131, 70, 37, 114, 59, 26, 98, 49, 10, 81, 37, 0, 64, 24, 0, 42, 4, 0, 0, 0,
- 71, 164, 71, 63, 147, 63, 53, 131, 55, 40, 114, 47, 28, 98, 38, 13, 81, 26, 0, 64, 12, 0, 42, 0, 0, 0, 0,
- 28, 153, 214, 24, 138, 193, 23, 123, 171, 16, 107, 150, 0, 91, 130, 0, 75, 109, 0, 58, 87, 0, 35, 61, 0, 0, 25,
- 48, 153, 194, 41, 138, 174, 34, 123, 156, 27, 107, 136, 16, 91, 117, 1, 75, 98, 0, 57, 78, 0, 35, 53, 0, 0, 17,
- 55, 153, 177, 47, 137, 158, 42, 122, 142, 33, 107, 124, 22, 91, 106, 6, 75, 88, 0, 57, 70, 0, 35, 46, 0, 0, 9,
- 61, 152, 158, 53, 136, 143, 45, 122, 127, 36, 106, 111, 24, 90, 94, 10, 74, 78, 0, 57, 61, 0, 35, 39, 0, 0, 2,
- 67, 151, 141, 59, 135, 126, 49, 121, 112, 39, 105, 98, 29, 90, 83, 14, 74, 68, 0, 57, 52, 0, 35, 31, 0, 0, 0,
- 71, 150, 123, 62, 135, 110, 54, 120, 98, 42, 105, 84, 31, 89, 71, 16, 74, 58, 0, 57, 43, 0, 35, 22, 0, 0, 0,
- 74, 150, 105, 64, 134, 92, 55, 120, 83, 45, 104, 71, 34, 89, 59, 20, 73, 47, 0, 57, 32, 0, 35, 13, 0, 0, 0,
- 78, 149, 84, 69, 134, 75, 59, 120, 66, 47, 103, 56, 34, 88, 46, 22, 73, 34, 1, 57, 20, 0, 35, 1, 0, 0, 0,
- 80, 149, 69, 70, 133, 61, 60, 119, 53, 49, 103, 44, 36, 88, 35, 23, 73, 24, 2, 56, 10, 0, 35, 0, 0, 0, 0,
- 58, 136, 200, 50, 122, 180, 45, 109, 162, 38, 94, 141, 27, 80, 121, 15, 65, 102, 0, 48, 81, 0, 26, 56, 0, 0, 19,
- 66, 136, 182, 59, 122, 163, 52, 109, 146, 42, 94, 128, 32, 80, 109, 20, 65, 91, 2, 48, 72, 0, 26, 49, 0, 0, 11,
- 70, 136, 165, 62, 122, 149, 55, 108, 133, 46, 94, 116, 35, 80, 99, 21, 65, 82, 4, 49, 64, 0, 26, 41, 0, 0, 3,
- 76, 135, 149, 66, 121, 133, 58, 108, 119, 48, 94, 103, 36, 79, 88, 23, 65, 73, 7, 49, 56, 0, 27, 34, 0, 0, 0,
- 78, 135, 133, 69, 120, 118, 60, 107, 106, 50, 93, 92, 39, 79, 77, 26, 65, 63, 8, 49, 47, 0, 27, 26, 0, 0, 0,
- 82, 134, 117, 71, 120, 104, 62, 107, 92, 51, 93, 79, 39, 78, 66, 27, 64, 53, 10, 48, 39, 0, 27, 18, 0, 0, 0,
- 84, 134, 99, 73, 119, 87, 64, 106, 77, 53, 92, 66, 42, 78, 55, 28, 64, 42, 11, 48, 29, 0, 28, 9, 0, 0, 0,
- 87, 133, 81, 76, 119, 72, 66, 106, 62, 55, 92, 52, 43, 78, 42, 29, 64, 31, 12, 48, 17, 0, 28, 0, 0, 0, 0,
- 88, 134, 67, 77, 119, 58, 68, 106, 51, 56, 92, 42, 44, 78, 32, 30, 64, 20, 12, 48, 6, 0, 28, 0, 0, 0, 0,
- 73, 120, 189, 64, 107, 168, 57, 96, 151, 47, 82, 133, 38, 69, 114, 26, 55, 95, 11, 39, 75, 0, 16, 51, 0, 0, 14,
- 78, 120, 171, 69, 107, 153, 62, 95, 137, 51, 82, 119, 40, 69, 102, 29, 55, 85, 15, 39, 66, 0, 17, 44, 0, 0, 4,
- 81, 120, 156, 71, 107, 140, 64, 95, 125, 53, 82, 109, 42, 69, 93, 31, 55, 77, 16, 39, 59, 0, 18, 37, 0, 0, 0,
- 85, 120, 141, 74, 107, 126, 65, 95, 112, 54, 82, 97, 43, 69, 82, 32, 55, 67, 17, 39, 51, 0, 19, 30, 0, 0, 0,
- 86, 119, 126, 76, 106, 112, 66, 95, 100, 56, 81, 85, 45, 69, 72, 33, 55, 59, 18, 40, 43, 0, 19, 22, 0, 0, 0,
- 89, 119, 110, 78, 106, 98, 69, 94, 87, 56, 81, 75, 46, 68, 62, 33, 55, 49, 18, 40, 35, 0, 20, 15, 0, 0, 0,
- 89, 119, 95, 80, 106, 83, 70, 94, 73, 58, 81, 63, 46, 68, 51, 34, 55, 39, 19, 40, 25, 0, 20, 4, 0, 0, 0,
- 92, 118, 78, 82, 106, 68, 70, 93, 59, 59, 81, 49, 47, 68, 39, 34, 55, 28, 19, 40, 14, 0, 20, 0, 0, 0, 0,
- 93, 118, 65, 82, 105, 55, 72, 93, 48, 60, 81, 39, 47, 68, 29, 34, 55, 18, 20, 40, 2, 0, 20, 0, 0, 0, 0,
- 83, 104, 177, 74, 93, 159, 65, 82, 142, 56, 70, 124, 45, 57, 106, 33, 44, 89, 20, 28, 70, 1, 4, 46, 0, 0, 8,
- 86, 104, 161, 78, 93, 145, 68, 82, 128, 58, 70, 112, 48, 58, 96, 35, 45, 80, 21, 29, 62, 1, 6, 40, 0, 0, 0,
- 89, 104, 147, 79, 93, 131, 69, 82, 118, 59, 70, 102, 47, 58, 87, 36, 45, 72, 23, 29, 55, 3, 7, 33, 0, 0, 0,
- 90, 104, 132, 80, 93, 119, 71, 82, 106, 60, 70, 91, 48, 58, 77, 37, 45, 62, 23, 30, 47, 4, 7, 26, 0, 0, 0,
- 92, 104, 118, 82, 93, 105, 72, 82, 93, 61, 69, 80, 50, 58, 68, 37, 45, 55, 23, 30, 39, 4, 8, 18, 0, 0, 0,
- 94, 104, 105, 82, 92, 93, 72, 82, 82, 61, 70, 70, 50, 58, 58, 38, 46, 46, 23, 31, 31, 6, 10, 11, 0, 0, 0,
- 95, 104, 90, 84, 92, 79, 74, 82, 70, 62, 70, 58, 50, 58, 48, 37, 46, 36, 23, 31, 22, 4, 11, 3, 0, 0, 0,
- 96, 103, 74, 85, 92, 65, 75, 81, 56, 63, 70, 47, 50, 58, 37, 38, 46, 25, 24, 31, 11, 3, 11, 0, 0, 0, 0,
- 97, 103, 62, 86, 92, 53, 76, 81, 45, 63, 69, 36, 51, 58, 27, 38, 46, 15, 23, 31, 0, 3, 11, 0, 0, 0, 0,
- 90, 87, 165, 81, 77, 148, 72, 67, 132, 62, 55, 116, 50, 44, 99, 39, 31, 82, 25, 14, 64, 1, 0, 42, 0, 0, 1,
- 93, 87, 150, 83, 77, 135, 74, 67, 121, 63, 56, 105, 52, 45, 90, 40, 32, 74, 25, 16, 57, 3, 0, 35, 0, 0, 0,
- 95, 87, 138, 85, 77, 123, 75, 67, 109, 63, 57, 95, 53, 45, 81, 41, 33, 66, 26, 17, 50, 4, 0, 28, 0, 0, 0,
- 95, 88, 124, 85, 77, 111, 75, 67, 99, 63, 57, 86, 53, 45, 72, 41, 33, 58, 26, 18, 43, 5, 1, 22, 0, 0, 0,
- 97, 88, 112, 87, 77, 100, 76, 68, 88, 64, 57, 76, 53, 46, 63, 41, 34, 50, 26, 19, 35, 5, 2, 14, 0, 0, 0,
- 99, 87, 99, 87, 78, 88, 76, 68, 77, 65, 57, 65, 53, 46, 54, 41, 35, 42, 27, 20, 27, 6, 2, 5, 0, 0, 0,
- 100, 87, 85, 88, 77, 75, 77, 68, 65, 65, 57, 54, 53, 46, 44, 41, 35, 32, 27, 21, 19, 5, 3, 2, 0, 0, 0,
- 100, 88, 71, 89, 77, 61, 78, 68, 53, 66, 57, 44, 53, 47, 33, 41, 35, 22, 27, 21, 7, 5, 3, 0, 0, 0, 0,
- 101, 87, 60, 90, 77, 52, 79, 68, 44, 66, 58, 34, 53, 47, 25, 41, 35, 13, 26, 22, 0, 5, 3, 0, 0, 0, 0,
- 97, 67, 155, 86, 58, 138, 77, 50, 125, 66, 39, 108, 55, 28, 92, 43, 12, 76, 29, 0, 59, 2, 0, 37, 0, 0, 0,
- 99, 67, 141, 88, 59, 127, 78, 50, 113, 68, 40, 98, 56, 29, 83, 44, 15, 68, 29, 0, 52, 3, 0, 30, 0, 0, 0,
- 100, 68, 129, 89, 59, 115, 80, 51, 103, 68, 41, 89, 56, 30, 75, 44, 16, 61, 30, 0, 45, 4, 0, 24, 0, 0, 0,
- 100, 69, 118, 90, 60, 104, 80, 51, 92, 67, 41, 79, 56, 31, 66, 44, 18, 53, 29, 2, 38, 4, 0, 18, 0, 0, 0,
- 101, 69, 104, 90, 61, 93, 79, 51, 82, 67, 42, 70, 56, 32, 59, 44, 20, 46, 29, 4, 31, 6, 0, 9, 0, 0, 0,
- 102, 69, 93, 90, 61, 83, 80, 52, 72, 68, 42, 61, 56, 33, 50, 43, 20, 38, 29, 5, 23, 7, 0, 4, 0, 0, 0,
- 102, 70, 80, 91, 61, 71, 80, 52, 61, 68, 43, 51, 56, 32, 40, 44, 21, 29, 30, 6, 14, 7, 0, 0, 0, 0, 0,
- 103, 70, 68, 92, 61, 58, 81, 53, 50, 69, 43, 41, 56, 34, 31, 43, 22, 19, 29, 7, 3, 7, 0, 0, 0, 0, 0,
- 104, 70, 57, 92, 61, 48, 82, 53, 40, 69, 43, 32, 56, 34, 22, 43, 23, 10, 29, 8, 0, 6, 0, 0, 0, 0, 0,
- 101, 45, 145, 91, 35, 129, 80, 26, 116, 69, 15, 101, 59, 0, 86, 46, 0, 71, 31, 0, 55, 0, 0, 33, 0, 0, 0,
- 104, 44, 132, 92, 36, 118, 82, 28, 105, 71, 17, 91, 58, 3, 77, 46, 0, 63, 31, 0, 48, 2, 0, 26, 0, 0, 0,
- 104, 46, 121, 93, 37, 107, 82, 30, 96, 70, 20, 83, 58, 6, 70, 46, 0, 57, 32, 0, 41, 4, 0, 20, 0, 0, 0,
- 104, 48, 110, 93, 40, 98, 82, 31, 87, 70, 22, 74, 59, 9, 62, 45, 0, 49, 31, 0, 35, 6, 0, 13, 0, 0, 0,
- 104, 48, 99, 92, 41, 88, 82, 32, 77, 70, 23, 65, 58, 11, 54, 46, 0, 42, 32, 0, 27, 7, 0, 5, 0, 0, 0,
- 105, 50, 88, 93, 41, 77, 82, 34, 68, 71, 24, 57, 58, 13, 46, 45, 1, 35, 31, 0, 21, 7, 0, 2, 0, 0, 0,
- 105, 50, 76, 94, 41, 66, 83, 34, 57, 71, 25, 47, 58, 15, 37, 45, 2, 25, 32, 0, 11, 7, 0, 0, 0, 0, 0,
- 106, 50, 64, 94, 42, 55, 83, 35, 47, 71, 26, 38, 58, 16, 27, 45, 4, 17, 31, 0, 4, 7, 0, 0, 0, 0, 0,
- 106, 51, 54, 95, 42, 45, 83, 35, 38, 71, 27, 30, 58, 16, 19, 45, 5, 7, 30, 0, 0, 6, 0, 0, 0, 0, 0,
- 0, 181, 240, 0, 162, 216, 0, 144, 193, 0, 126, 168, 0, 109, 146, 0, 91, 123, 0, 71, 98, 0, 48, 71, 0, 9, 34,
- 0, 179, 218, 0, 161, 195, 0, 144, 174, 0, 126, 153, 0, 108, 132, 0, 90, 110, 0, 71, 88, 0, 48, 63, 0, 8, 29,
- 0, 178, 197, 0, 159, 177, 0, 143, 159, 0, 125, 139, 0, 107, 119, 0, 90, 99, 0, 71, 79, 0, 48, 55, 0, 8, 22,
- 0, 177, 177, 0, 158, 158, 0, 142, 141, 0, 124, 123, 0, 107, 106, 0, 89, 88, 0, 71, 70, 0, 48, 47, 0, 8, 14,
- 0, 176, 157, 0, 158, 141, 0, 141, 126, 0, 123, 109, 0, 106, 93, 0, 89, 78, 0, 70, 60, 0, 47, 39, 0, 7, 5,
- 0, 175, 138, 0, 157, 123, 0, 141, 110, 0, 123, 96, 0, 105, 81, 0, 88, 67, 0, 70, 51, 0, 48, 30, 0, 6, 0,
- 0, 173, 115, 0, 155, 104, 0, 140, 92, 0, 122, 80, 0, 105, 67, 0, 88, 55, 0, 69, 40, 0, 47, 20, 0, 6, 0,
- 0, 173, 94, 0, 155, 85, 0, 139, 75, 0, 121, 64, 0, 104, 53, 0, 88, 42, 0, 70, 28, 0, 47, 9, 0, 6, 0,
- 0, 173, 76, 0, 155, 70, 0, 138, 61, 0, 122, 53, 0, 104, 44, 0, 87, 32, 0, 69, 18, 0, 47, 0, 0, 6, 0,
- 0, 164, 226, 0, 147, 203, 0, 131, 181, 0, 114, 158, 0, 97, 136, 0, 80, 115, 0, 63, 92, 0, 40, 65, 0, 0, 30,
- 0, 162, 205, 0, 145, 184, 0, 130, 164, 0, 114, 143, 0, 97, 124, 0, 81, 104, 0, 63, 83, 0, 40, 58, 0, 0, 23,
- 0, 162, 187, 0, 145, 167, 0, 130, 150, 0, 113, 131, 0, 96, 112, 0, 80, 93, 0, 62, 74, 0, 40, 50, 0, 0, 16,
- 0, 160, 167, 0, 144, 150, 0, 129, 134, 0, 112, 116, 0, 96, 100, 0, 80, 82, 0, 62, 65, 0, 40, 43, 0, 0, 7,
- 0, 160, 148, 0, 143, 133, 0, 128, 118, 0, 111, 103, 0, 96, 88, 0, 80, 73, 0, 62, 56, 0, 40, 35, 0, 0, 0,
- 0, 158, 130, 0, 142, 117, 0, 127, 104, 0, 111, 89, 0, 95, 76, 0, 79, 62, 0, 62, 46, 0, 40, 26, 0, 0, 0,
- 0, 158, 111, 0, 141, 99, 0, 127, 88, 0, 111, 76, 0, 95, 63, 0, 79, 51, 0, 62, 37, 0, 40, 18, 0, 0, 0,
- 0, 158, 91, 0, 141, 81, 0, 126, 72, 0, 110, 62, 0, 94, 50, 0, 79, 39, 0, 62, 25, 0, 40, 5, 0, 0, 0,
- 0, 157, 74, 0, 141, 66, 0, 126, 59, 0, 110, 49, 0, 94, 40, 0, 78, 29, 0, 61, 15, 0, 40, 0, 0, 0, 0,
- 0, 148, 214, 0, 133, 192, 0, 119, 171, 0, 103, 150, 0, 87, 129, 0, 72, 108, 0, 55, 86, 0, 32, 61, 0, 0, 25,
- 0, 147, 193, 0, 132, 173, 0, 118, 155, 0, 103, 136, 0, 87, 116, 0, 72, 98, 0, 55, 78, 0, 32, 53, 0, 0, 17,
- 0, 147, 176, 0, 132, 158, 0, 118, 142, 0, 102, 124, 0, 87, 106, 0, 72, 88, 0, 55, 69, 0, 33, 46, 0, 0, 9,
- 0, 146, 159, 0, 131, 142, 0, 117, 127, 0, 102, 111, 0, 87, 95, 0, 71, 79, 0, 55, 61, 0, 33, 39, 0, 0, 2,
- 0, 145, 140, 0, 130, 126, 0, 117, 112, 0, 101, 98, 0, 86, 83, 0, 71, 68, 0, 55, 52, 0, 33, 31, 0, 0, 0,
- 0, 144, 124, 0, 130, 111, 0, 116, 99, 0, 101, 84, 0, 86, 72, 0, 71, 59, 0, 55, 43, 0, 33, 23, 0, 0, 0,
- 0, 144, 106, 0, 129, 94, 0, 115, 83, 0, 101, 72, 0, 85, 60, 0, 71, 48, 0, 55, 34, 0, 33, 14, 0, 0, 0,
- 3, 143, 86, 0, 129, 77, 0, 115, 68, 0, 100, 58, 0, 85, 48, 0, 70, 36, 0, 54, 22, 0, 33, 3, 0, 0, 0,
- 18, 143, 72, 13, 128, 63, 0, 115, 57, 0, 100, 47, 0, 85, 37, 0, 70, 26, 0, 54, 13, 0, 33, 0, 0, 0, 0,
- 0, 132, 200, 0, 118, 179, 0, 105, 161, 0, 91, 140, 0, 76, 121, 0, 62, 101, 0, 46, 81, 0, 24, 56, 0, 0, 19,
- 0, 131, 182, 0, 118, 163, 0, 105, 146, 0, 91, 128, 0, 77, 110, 0, 62, 91, 0, 46, 72, 0, 25, 48, 0, 0, 11,
- 0, 131, 165, 0, 117, 149, 0, 104, 133, 0, 91, 116, 0, 77, 99, 0, 62, 82, 0, 46, 64, 0, 25, 41, 0, 0, 4,
- 0, 131, 149, 0, 116, 134, 0, 104, 119, 0, 91, 104, 0, 77, 89, 0, 62, 73, 0, 46, 56, 0, 25, 34, 0, 0, 0,
- 10, 130, 133, 2, 116, 119, 0, 104, 106, 0, 90, 91, 0, 76, 78, 0, 62, 64, 0, 46, 48, 0, 26, 27, 0, 0, 0,
- 23, 130, 118, 20, 116, 104, 13, 103, 93, 3, 89, 79, 0, 76, 67, 0, 62, 54, 0, 46, 39, 0, 26, 19, 0, 0, 0,
- 33, 129, 101, 27, 115, 89, 19, 103, 79, 9, 89, 67, 0, 75, 56, 0, 61, 43, 0, 46, 29, 0, 26, 10, 0, 0, 0,
- 41, 128, 83, 35, 115, 73, 27, 102, 64, 15, 89, 55, 0, 76, 45, 0, 62, 33, 0, 46, 18, 0, 26, 0, 0, 0, 0,
- 43, 129, 69, 38, 115, 61, 30, 102, 54, 17, 89, 45, 2, 75, 34, 0, 61, 23, 0, 46, 9, 0, 26, 0, 0, 0, 0,
- 1, 116, 188, 1, 104, 168, 0, 92, 151, 0, 79, 132, 0, 66, 113, 0, 52, 94, 0, 36, 75, 0, 14, 52, 0, 0, 14,
- 17, 116, 171, 16, 104, 153, 14, 92, 137, 8, 79, 119, 0, 67, 102, 0, 53, 85, 0, 37, 67, 0, 16, 44, 0, 0, 4,
- 31, 116, 155, 27, 104, 140, 21, 92, 125, 13, 79, 109, 3, 66, 93, 0, 53, 77, 0, 37, 59, 0, 16, 38, 0, 0, 0,
- 37, 115, 141, 30, 103, 126, 26, 92, 112, 16, 79, 98, 5, 66, 83, 0, 53, 67, 0, 38, 51, 0, 17, 31, 0, 0, 0,
- 41, 115, 126, 37, 103, 112, 31, 92, 100, 22, 79, 86, 10, 66, 72, 0, 53, 59, 0, 38, 44, 0, 17, 23, 0, 0, 0,
- 48, 115, 111, 41, 102, 99, 34, 91, 88, 24, 78, 76, 14, 66, 63, 0, 53, 50, 0, 38, 36, 0, 18, 15, 0, 0, 0,
- 51, 115, 95, 46, 102, 85, 37, 91, 74, 26, 78, 63, 16, 66, 52, 0, 53, 40, 0, 38, 26, 0, 18, 5, 0, 0, 0,
- 55, 114, 80, 47, 102, 69, 40, 90, 60, 30, 78, 51, 19, 66, 41, 3, 53, 29, 0, 38, 15, 0, 17, 0, 0, 0, 0,
- 56, 114, 66, 50, 102, 58, 40, 91, 50, 32, 78, 41, 18, 66, 32, 4, 53, 21, 0, 38, 5, 0, 17, 0, 0, 0, 0,
- 39, 102, 178, 37, 90, 159, 30, 79, 142, 21, 68, 124, 14, 55, 106, 0, 42, 89, 0, 26, 70, 0, 4, 46, 0, 0, 8,
- 48, 102, 161, 42, 90, 145, 35, 79, 128, 26, 68, 112, 19, 55, 96, 3, 43, 79, 0, 27, 62, 0, 6, 40, 0, 0, 0,
- 50, 102, 147, 44, 90, 132, 37, 79, 118, 30, 68, 102, 20, 56, 87, 7, 43, 72, 0, 28, 55, 0, 6, 34, 0, 0, 0,
- 53, 101, 133, 47, 90, 118, 41, 79, 106, 32, 68, 91, 21, 56, 78, 9, 43, 63, 0, 28, 47, 0, 6, 26, 0, 0, 0,
- 57, 101, 119, 50, 89, 106, 42, 79, 94, 34, 67, 81, 24, 56, 68, 9, 44, 55, 0, 29, 40, 0, 6, 19, 0, 0, 0,
- 60, 100, 105, 50, 90, 94, 45, 80, 83, 36, 68, 71, 24, 56, 59, 11, 44, 46, 0, 29, 32, 0, 7, 12, 0, 0, 0,
- 63, 101, 91, 55, 90, 80, 46, 79, 70, 37, 68, 59, 26, 56, 49, 12, 44, 37, 1, 29, 23, 0, 7, 3, 0, 0, 0,
- 64, 101, 75, 56, 89, 67, 48, 79, 57, 37, 68, 48, 27, 56, 37, 15, 44, 26, 0, 29, 12, 0, 7, 0, 0, 0, 0,
- 66, 101, 64, 58, 89, 55, 49, 79, 47, 39, 68, 38, 27, 56, 29, 14, 44, 18, 1, 30, 2, 0, 7, 0, 0, 0, 0,
- 57, 86, 165, 51, 75, 148, 45, 65, 133, 38, 54, 116, 28, 43, 100, 16, 29, 83, 0, 13, 64, 0, 0, 42, 0, 0, 3,
- 60, 86, 151, 55, 75, 135, 47, 66, 121, 39, 55, 105, 30, 44, 90, 18, 31, 74, 3, 16, 57, 0, 1, 35, 0, 0, 0,
- 62, 86, 139, 56, 75, 123, 49, 66, 110, 40, 55, 95, 30, 44, 81, 19, 31, 66, 4, 17, 51, 0, 1, 29, 0, 0, 0,
- 65, 86, 125, 56, 76, 112, 49, 66, 99, 39, 55, 86, 31, 44, 72, 19, 32, 59, 5, 18, 44, 0, 1, 23, 0, 0, 0,
- 67, 86, 113, 58, 75, 100, 51, 66, 88, 41, 56, 77, 31, 45, 64, 20, 32, 51, 6, 18, 35, 0, 1, 14, 0, 0, 0,
- 69, 86, 99, 61, 76, 88, 52, 66, 78, 43, 56, 66, 32, 45, 55, 20, 33, 42, 7, 19, 27, 0, 1, 6, 0, 0, 0,
- 69, 86, 86, 61, 76, 75, 53, 67, 66, 43, 56, 55, 33, 45, 45, 21, 34, 34, 8, 20, 20, 0, 2, 2, 0, 0, 0,
- 71, 86, 72, 63, 75, 62, 54, 66, 55, 45, 56, 45, 33, 45, 35, 22, 34, 23, 7, 20, 8, 0, 2, 0, 0, 0, 0,
- 71, 86, 62, 64, 75, 53, 55, 66, 46, 45, 56, 36, 33, 46, 27, 22, 34, 15, 8, 20, 0, 0, 2, 0, 0, 0, 0,
- 69, 67, 156, 61, 58, 140, 53, 50, 125, 45, 39, 108, 35, 28, 93, 25, 12, 77, 12, 0, 59, 0, 0, 37, 0, 0, 0,
- 71, 68, 142, 63, 59, 126, 56, 50, 114, 47, 40, 98, 37, 28, 84, 26, 15, 68, 12, 0, 53, 0, 0, 30, 0, 0, 0,
- 72, 68, 130, 63, 59, 116, 56, 50, 104, 47, 40, 90, 38, 30, 75, 27, 16, 61, 13, 0, 46, 0, 0, 24, 0, 0, 0,
- 73, 69, 118, 65, 59, 105, 57, 51, 92, 47, 41, 80, 37, 30, 67, 26, 18, 53, 14, 1, 39, 0, 0, 18, 0, 0, 0,
- 74, 69, 106, 65, 60, 93, 57, 51, 82, 48, 41, 70, 38, 31, 59, 26, 19, 46, 13, 2, 32, 0, 0, 10, 0, 0, 0,
- 76, 69, 95, 66, 61, 84, 58, 52, 73, 48, 42, 61, 37, 32, 50, 26, 20, 38, 14, 4, 24, 0, 0, 4, 0, 0, 0,
- 76, 69, 81, 68, 60, 72, 58, 52, 62, 48, 42, 51, 38, 32, 41, 27, 21, 30, 14, 4, 16, 0, 0, 1, 0, 0, 0,
- 76, 69, 68, 68, 61, 60, 60, 52, 51, 49, 43, 41, 38, 33, 32, 27, 21, 20, 14, 5, 5, 0, 0, 0, 0, 0, 0,
- 78, 70, 59, 69, 61, 50, 60, 52, 42, 49, 43, 34, 39, 33, 24, 27, 22, 13, 14, 7, 1, 0, 0, 0, 0, 0, 0,
- 75, 46, 146, 68, 38, 131, 60, 30, 117, 50, 19, 102, 41, 4, 87, 29, 0, 72, 13, 0, 55, 0, 0, 33, 0, 0, 0,
- 78, 47, 132, 70, 39, 119, 61, 30, 105, 53, 20, 92, 42, 5, 78, 30, 0, 64, 13, 0, 49, 0, 0, 27, 0, 0, 0,
- 79, 48, 122, 70, 40, 108, 62, 32, 96, 52, 22, 84, 42, 9, 71, 30, 0, 58, 14, 0, 42, 0, 0, 20, 0, 0, 0,
- 79, 50, 111, 70, 42, 99, 62, 33, 88, 52, 23, 74, 41, 11, 63, 29, 0, 50, 14, 0, 36, 0, 0, 14, 0, 0, 0,
- 80, 50, 99, 70, 42, 89, 61, 34, 78, 52, 25, 67, 41, 14, 55, 30, 0, 42, 15, 0, 28, 0, 0, 6, 0, 0, 0,
- 81, 51, 89, 71, 43, 78, 62, 35, 69, 52, 25, 58, 42, 15, 47, 30, 3, 36, 15, 0, 22, 0, 0, 3, 0, 0, 0,
- 81, 51, 77, 71, 44, 68, 63, 36, 59, 53, 26, 49, 41, 16, 38, 31, 4, 27, 16, 0, 12, 0, 0, 0, 0, 0, 0,
- 81, 52, 65, 72, 43, 56, 63, 36, 48, 53, 27, 39, 41, 17, 29, 30, 4, 18, 14, 0, 3, 0, 0, 0, 0, 0, 0,
- 81, 52, 55, 73, 44, 47, 64, 36, 39, 53, 28, 32, 42, 18, 21, 31, 6, 9, 14, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 174, 239, 0, 156, 214, 0, 139, 192, 0, 121, 168, 0, 105, 145, 0, 87, 123, 0, 68, 98, 0, 46, 70, 0, 3, 35,
- 0, 172, 217, 0, 155, 194, 0, 139, 173, 0, 121, 152, 0, 104, 130, 0, 87, 110, 0, 69, 88, 0, 46, 63, 0, 4, 28,
- 0, 171, 197, 0, 153, 175, 0, 138, 158, 0, 121, 139, 0, 103, 118, 0, 86, 100, 0, 68, 79, 0, 46, 55, 0, 4, 22,
- 0, 170, 177, 0, 152, 158, 0, 136, 141, 0, 119, 124, 0, 103, 106, 0, 86, 88, 0, 68, 70, 0, 45, 47, 0, 3, 14,
- 0, 169, 157, 0, 152, 141, 0, 136, 126, 0, 119, 109, 0, 102, 94, 0, 86, 78, 0, 68, 60, 0, 46, 39, 0, 3, 5,
- 0, 167, 138, 0, 150, 124, 0, 135, 111, 0, 118, 97, 0, 102, 82, 0, 85, 68, 0, 68, 52, 0, 46, 31, 0, 3, 0,
- 0, 167, 118, 0, 150, 104, 0, 135, 94, 0, 118, 81, 0, 101, 69, 0, 84, 56, 0, 67, 41, 0, 45, 21, 0, 3, 0,
- 0, 166, 97, 0, 149, 87, 0, 134, 77, 0, 117, 67, 0, 101, 56, 0, 85, 44, 0, 67, 30, 0, 45, 10, 0, 3, 0,
- 0, 165, 79, 0, 149, 73, 0, 133, 64, 0, 117, 56, 0, 101, 46, 0, 85, 34, 0, 68, 21, 0, 46, 1, 0, 3, 0,
- 0, 158, 225, 0, 141, 201, 0, 126, 180, 0, 109, 158, 0, 94, 136, 0, 78, 114, 0, 60, 91, 0, 38, 66, 0, 0, 30,
- 0, 156, 203, 0, 140, 183, 0, 125, 164, 0, 109, 143, 0, 94, 124, 0, 78, 104, 0, 61, 83, 0, 38, 57, 0, 0, 23,
- 0, 156, 186, 0, 140, 166, 0, 125, 150, 0, 109, 130, 0, 93, 111, 0, 77, 93, 0, 60, 74, 0, 38, 50, 0, 0, 17,
- 0, 155, 167, 0, 138, 149, 0, 124, 134, 0, 109, 117, 0, 93, 100, 0, 76, 83, 0, 60, 65, 0, 38, 43, 0, 0, 9,
- 0, 153, 147, 0, 138, 134, 0, 124, 120, 0, 107, 103, 0, 92, 88, 0, 77, 73, 0, 60, 56, 0, 38, 35, 0, 0, 0,
- 0, 153, 131, 0, 137, 118, 0, 122, 105, 0, 107, 90, 0, 91, 76, 0, 76, 63, 0, 60, 47, 0, 39, 28, 0, 0, 0,
- 0, 153, 111, 0, 136, 100, 0, 123, 90, 0, 107, 77, 0, 92, 65, 0, 76, 52, 0, 60, 37, 0, 38, 18, 0, 0, 0,
- 0, 152, 93, 0, 136, 82, 0, 122, 74, 0, 106, 63, 0, 91, 52, 0, 76, 40, 0, 59, 26, 0, 38, 6, 0, 0, 0,
- 0, 151, 78, 0, 136, 69, 0, 121, 61, 0, 106, 52, 0, 91, 43, 0, 76, 32, 0, 59, 17, 0, 38, 0, 0, 0, 0,
- 0, 143, 213, 0, 128, 191, 0, 115, 171, 0, 100, 149, 0, 84, 128, 0, 69, 108, 0, 52, 86, 0, 30, 61, 0, 0, 25,
- 0, 142, 193, 0, 127, 173, 0, 114, 154, 0, 99, 134, 0, 84, 116, 0, 69, 98, 0, 52, 77, 0, 31, 53, 0, 0, 18,
- 0, 141, 176, 0, 127, 158, 0, 114, 141, 0, 98, 122, 0, 84, 105, 0, 69, 88, 0, 53, 69, 0, 31, 46, 0, 0, 9,
- 0, 141, 159, 0, 126, 142, 0, 113, 127, 0, 98, 110, 0, 83, 95, 0, 69, 78, 0, 53, 60, 0, 32, 39, 0, 0, 2,
- 0, 140, 140, 0, 126, 126, 0, 112, 112, 0, 98, 98, 0, 83, 83, 0, 68, 69, 0, 52, 52, 0, 31, 31, 0, 0, 0,
- 0, 140, 124, 0, 125, 112, 0, 112, 100, 0, 97, 86, 0, 83, 72, 0, 68, 59, 0, 52, 44, 0, 31, 23, 0, 0, 0,
- 0, 139, 106, 0, 125, 96, 0, 111, 85, 0, 97, 72, 0, 83, 62, 0, 68, 49, 0, 52, 35, 0, 31, 15, 0, 0, 0,
- 0, 138, 88, 0, 124, 79, 0, 111, 70, 0, 96, 59, 0, 82, 48, 0, 68, 38, 0, 52, 24, 0, 31, 4, 0, 0, 0,
- 0, 139, 76, 0, 124, 66, 0, 111, 58, 0, 96, 50, 0, 82, 40, 0, 68, 29, 0, 52, 15, 0, 31, 0, 0, 0, 0,
- 0, 129, 200, 0, 114, 179, 0, 102, 160, 0, 87, 139, 0, 74, 120, 0, 60, 101, 0, 44, 81, 0, 22, 56, 0, 0, 19,
- 0, 127, 181, 0, 114, 163, 0, 102, 146, 0, 88, 127, 0, 74, 109, 0, 60, 91, 0, 44, 72, 0, 23, 48, 0, 0, 11,
- 0, 127, 166, 0, 113, 148, 0, 101, 133, 0, 87, 115, 0, 74, 99, 0, 60, 82, 0, 44, 64, 0, 23, 42, 0, 0, 4,
- 0, 127, 150, 0, 113, 134, 0, 101, 119, 0, 87, 104, 0, 74, 89, 0, 60, 73, 0, 44, 56, 0, 23, 35, 0, 0, 0,
- 0, 125, 134, 0, 112, 118, 0, 100, 106, 0, 87, 92, 0, 73, 78, 0, 60, 64, 0, 44, 48, 0, 23, 27, 0, 0, 0,
- 0, 125, 118, 0, 112, 105, 0, 100, 94, 0, 86, 80, 0, 73, 68, 0, 60, 54, 0, 44, 39, 0, 23, 20, 0, 0, 0,
- 0, 125, 101, 0, 111, 90, 0, 99, 80, 0, 86, 69, 0, 73, 58, 0, 59, 45, 0, 44, 30, 0, 23, 11, 0, 0, 0,
- 0, 124, 85, 0, 111, 75, 0, 99, 66, 0, 86, 56, 0, 73, 45, 0, 59, 34, 0, 44, 20, 0, 23, 1, 0, 0, 0,
- 0, 125, 72, 0, 111, 62, 0, 99, 56, 0, 86, 46, 0, 73, 36, 0, 60, 26, 0, 44, 12, 0, 23, 0, 0, 0, 0,
- 0, 114, 188, 0, 101, 167, 0, 89, 150, 0, 77, 131, 0, 64, 113, 0, 50, 95, 0, 34, 75, 0, 12, 52, 0, 0, 14,
- 0, 113, 170, 0, 101, 153, 0, 89, 137, 0, 77, 120, 0, 64, 102, 0, 50, 85, 0, 35, 67, 0, 12, 44, 0, 0, 4,
- 0, 113, 156, 0, 100, 139, 0, 89, 125, 0, 77, 109, 0, 64, 92, 0, 51, 77, 0, 35, 60, 0, 12, 38, 0, 0, 0,
- 0, 112, 141, 0, 100, 126, 0, 89, 113, 0, 77, 98, 0, 64, 83, 0, 51, 68, 0, 35, 51, 0, 12, 30, 0, 0, 0,
- 0, 112, 127, 0, 100, 112, 0, 89, 100, 0, 76, 87, 0, 64, 74, 0, 51, 59, 0, 35, 44, 0, 13, 24, 0, 0, 0,
- 0, 112, 111, 0, 100, 100, 0, 88, 88, 0, 76, 76, 0, 64, 64, 0, 51, 52, 0, 36, 37, 0, 13, 17, 0, 0, 0,
- 0, 111, 96, 0, 99, 85, 0, 88, 76, 0, 76, 64, 0, 64, 53, 0, 51, 41, 0, 36, 27, 0, 13, 6, 0, 0, 0,
- 0, 111, 81, 0, 99, 71, 0, 88, 62, 0, 76, 52, 0, 64, 43, 0, 51, 31, 0, 36, 17, 0, 13, 0, 0, 0, 0,
- 0, 111, 69, 0, 99, 60, 0, 88, 52, 0, 75, 43, 0, 63, 34, 0, 51, 21, 0, 36, 7, 0, 13, 0, 0, 0, 0,
- 0, 99, 177, 0, 88, 158, 0, 77, 141, 0, 66, 123, 0, 53, 106, 0, 40, 89, 0, 25, 71, 0, 5, 47, 0, 0, 8,
- 0, 99, 160, 0, 88, 144, 0, 77, 129, 0, 66, 112, 0, 54, 97, 0, 41, 80, 0, 26, 62, 0, 5, 40, 0, 0, 0,
- 0, 99, 147, 0, 87, 132, 0, 78, 117, 0, 66, 102, 0, 54, 87, 0, 42, 72, 0, 26, 55, 0, 5, 34, 0, 0, 0,
- 0, 99, 134, 0, 88, 119, 0, 77, 107, 0, 66, 92, 0, 54, 78, 0, 42, 64, 0, 27, 48, 0, 5, 27, 0, 0, 0,
- 0, 99, 120, 0, 87, 107, 0, 78, 94, 0, 66, 81, 0, 54, 68, 0, 42, 55, 0, 27, 40, 0, 6, 20, 0, 0, 0,
- 0, 98, 105, 0, 87, 94, 0, 77, 84, 0, 65, 71, 0, 55, 59, 0, 42, 47, 0, 28, 33, 0, 6, 12, 0, 0, 0,
- 0, 98, 93, 0, 87, 81, 0, 77, 72, 0, 66, 61, 0, 54, 49, 0, 42, 37, 0, 28, 24, 0, 6, 4, 0, 0, 0,
- 0, 98, 77, 0, 87, 68, 0, 77, 59, 0, 65, 49, 0, 54, 39, 0, 42, 27, 0, 29, 14, 0, 6, 0, 0, 0, 0,
- 1, 98, 65, 7, 87, 56, 0, 77, 49, 0, 66, 41, 0, 54, 30, 0, 42, 19, 0, 29, 3, 0, 6, 0, 0, 0, 0,
- 0, 84, 166, 0, 74, 149, 0, 64, 134, 0, 53, 117, 0, 41, 100, 0, 28, 83, 0, 11, 64, 0, 0, 42, 0, 0, 3,
- 0, 84, 151, 0, 74, 135, 0, 64, 121, 0, 53, 105, 0, 42, 90, 0, 30, 75, 0, 14, 58, 0, 0, 36, 0, 0, 0,
- 0, 84, 138, 0, 74, 124, 1, 64, 110, 0, 54, 95, 0, 43, 81, 0, 30, 67, 0, 15, 51, 0, 1, 29, 0, 0, 0,
- 14, 84, 126, 12, 74, 112, 2, 65, 99, 0, 54, 85, 0, 44, 73, 0, 31, 59, 0, 16, 44, 0, 1, 23, 0, 0, 0,
- 16, 84, 113, 13, 74, 100, 6, 65, 89, 0, 54, 77, 0, 44, 65, 0, 31, 51, 0, 17, 36, 0, 1, 16, 0, 0, 0,
- 24, 84, 100, 18, 74, 88, 13, 65, 78, 2, 55, 68, 0, 44, 55, 0, 32, 43, 0, 18, 28, 0, 1, 6, 0, 0, 0,
- 26, 84, 87, 24, 74, 76, 17, 65, 67, 7, 54, 57, 0, 44, 46, 0, 32, 35, 0, 19, 21, 0, 2, 3, 0, 0, 0,
- 30, 84, 74, 28, 74, 64, 20, 65, 55, 12, 55, 46, 0, 44, 35, 0, 32, 24, 0, 18, 9, 0, 1, 0, 0, 0, 0,
- 32, 84, 63, 28, 74, 54, 21, 65, 47, 13, 54, 38, 0, 44, 28, 0, 32, 16, 0, 18, 1, 0, 1, 0, 0, 0, 0,
- 30, 67, 155, 20, 58, 139, 20, 49, 126, 12, 39, 110, 0, 27, 94, 0, 13, 77, 0, 0, 60, 0, 0, 37, 0, 0, 0,
- 35, 67, 142, 30, 58, 126, 23, 50, 114, 16, 40, 99, 7, 29, 85, 0, 15, 69, 0, 0, 52, 0, 0, 30, 0, 0, 0,
- 35, 68, 131, 30, 59, 116, 27, 50, 104, 18, 40, 90, 9, 29, 76, 0, 17, 62, 0, 2, 46, 0, 0, 24, 0, 0, 0,
- 37, 69, 119, 33, 59, 106, 27, 51, 94, 21, 41, 80, 9, 30, 67, 0, 18, 54, 0, 3, 39, 0, 0, 18, 0, 0, 0,
- 40, 69, 107, 36, 59, 94, 28, 51, 84, 18, 41, 72, 10, 31, 60, 0, 19, 47, 0, 4, 32, 0, 0, 10, 0, 0, 0,
- 42, 69, 95, 36, 59, 84, 29, 51, 74, 19, 41, 63, 10, 31, 52, 0, 20, 39, 0, 4, 25, 0, 0, 4, 0, 0, 0,
- 43, 69, 83, 38, 60, 73, 32, 51, 62, 23, 42, 53, 11, 31, 42, 0, 20, 31, 0, 5, 17, 0, 0, 1, 0, 0, 0,
- 45, 69, 70, 39, 60, 60, 33, 51, 52, 24, 42, 43, 13, 32, 33, 0, 21, 21, 0, 5, 6, 0, 0, 0, 0, 0, 0,
- 47, 69, 59, 41, 60, 51, 34, 51, 43, 24, 42, 35, 12, 33, 26, 1, 22, 14, 0, 5, 1, 0, 0, 0, 0, 0, 0,
- 46, 48, 146, 42, 40, 131, 36, 32, 118, 27, 22, 103, 17, 6, 88, 5, 0, 73, 0, 0, 55, 0, 0, 33, 0, 0, 0,
- 48, 48, 133, 44, 40, 119, 37, 32, 107, 28, 22, 93, 20, 8, 79, 7, 0, 65, 0, 0, 49, 0, 0, 27, 0, 0, 0,
- 48, 50, 123, 44, 41, 109, 37, 33, 97, 30, 23, 83, 21, 11, 71, 8, 0, 58, 0, 0, 42, 0, 0, 21, 0, 0, 0,
- 49, 51, 111, 45, 42, 99, 38, 34, 87, 29, 25, 75, 20, 13, 63, 8, 0, 51, 0, 0, 36, 0, 0, 14, 0, 0, 0,
- 52, 52, 100, 44, 43, 89, 38, 35, 79, 29, 26, 68, 19, 15, 56, 10, 1, 43, 0, 0, 28, 0, 0, 6, 0, 0, 0,
- 52, 52, 90, 47, 44, 79, 39, 36, 70, 30, 27, 59, 20, 16, 47, 9, 2, 36, 0, 0, 22, 0, 0, 2, 0, 0, 0,
- 52, 53, 78, 46, 44, 68, 39, 37, 60, 32, 27, 49, 22, 17, 39, 10, 3, 28, 0, 0, 12, 0, 0, 0, 0, 0, 0,
- 53, 53, 66, 47, 44, 57, 40, 36, 48, 32, 27, 39, 22, 18, 30, 9, 4, 18, 0, 0, 3, 0, 0, 0, 0, 0, 0,
- 54, 53, 57, 48, 45, 49, 41, 37, 41, 33, 28, 32, 22, 19, 23, 11, 6, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0,
-};
-void AdobeCMYK_to_sRGB1(FX_BYTE c, FX_BYTE m, FX_BYTE y, FX_BYTE k, FX_BYTE& R, FX_BYTE& G, FX_BYTE& B)
-{
- int fix_c = c << 8;
- int fix_m = m << 8;
- int fix_y = y << 8;
- int fix_k = k << 8;
- int c_index = (fix_c + 4096) >> 13;
- int m_index = (fix_m + 4096) >> 13;
- int y_index = (fix_y + 4096) >> 13;
- int k_index = (fix_k + 4096) >> 13;
- int pos = (c_index * 9 * 9 * 9 + m_index * 9 * 9 + y_index * 9 + k_index) * 3;
- int fix_r = g_CMYKSamples[pos] << 8;
- int fix_g = g_CMYKSamples[pos + 1] << 8;
- int fix_b = g_CMYKSamples[pos + 2] << 8;
- int c1_index = fix_c >> 13;
- if (c1_index == c_index) {
- c1_index = c1_index == 8 ? c1_index - 1 : c1_index + 1;
- }
- int m1_index = fix_m >> 13;
- if (m1_index == m_index) {
- m1_index = m1_index == 8 ? m1_index - 1 : m1_index + 1;
- }
- int y1_index = fix_y >> 13;
- if (y1_index == y_index) {
- y1_index = y1_index == 8 ? y1_index - 1 : y1_index + 1;
- }
- int k1_index = fix_k >> 13;
- if (k1_index == k_index) {
- k1_index = k1_index == 8 ? k1_index - 1 : k1_index + 1;
- }
- int c1_pos = pos + (c1_index - c_index) * 9 * 9 * 9 * 3;
- int m1_pos = pos + (m1_index - m_index) * 9 * 9 * 3;
- int y1_pos = pos + (y1_index - y_index) * 9 * 3;
- int k1_pos = pos + (k1_index - k_index) * 3;
- int c_r_delta = g_CMYKSamples[pos] - g_CMYKSamples[c1_pos];
- int c_g_delta = g_CMYKSamples[pos + 1] - g_CMYKSamples[c1_pos + 1];
- int c_b_delta = g_CMYKSamples[pos + 2] - g_CMYKSamples[c1_pos + 2];
- int m_r_delta = g_CMYKSamples[pos] - g_CMYKSamples[m1_pos];
- int m_g_delta = g_CMYKSamples[pos + 1] - g_CMYKSamples[m1_pos + 1];
- int m_b_delta = g_CMYKSamples[pos + 2] - g_CMYKSamples[m1_pos + 2];
- int y_r_delta = g_CMYKSamples[pos] - g_CMYKSamples[y1_pos];
- int y_g_delta = g_CMYKSamples[pos + 1] - g_CMYKSamples[y1_pos + 1];
- int y_b_delta = g_CMYKSamples[pos + 2] - g_CMYKSamples[y1_pos + 2];
- int k_r_delta = g_CMYKSamples[pos] - g_CMYKSamples[k1_pos];
- int k_g_delta = g_CMYKSamples[pos + 1] - g_CMYKSamples[k1_pos + 1];
- int k_b_delta = g_CMYKSamples[pos + 2] - g_CMYKSamples[k1_pos + 2];
- int c_rate = (fix_c - (c_index << 13)) * (c_index - c1_index);
- fix_r += c_r_delta * c_rate / 32;
- fix_g += c_g_delta * c_rate / 32;
- fix_b += c_b_delta * c_rate / 32;
- int m_rate = (fix_m - (m_index << 13)) * (m_index - m1_index);
- fix_r += m_r_delta * m_rate / 32;
- fix_g += m_g_delta * m_rate / 32;
- fix_b += m_b_delta * m_rate / 32;
- int y_rate = (fix_y - (y_index << 13)) * (y_index - y1_index);
- fix_r += y_r_delta * y_rate / 32;
- fix_g += y_g_delta * y_rate / 32;
- fix_b += y_b_delta * y_rate / 32;
- int k_rate = (fix_k - (k_index << 13)) * (k_index - k1_index);
- fix_r += k_r_delta * k_rate / 32;
- fix_g += k_g_delta * k_rate / 32;
- fix_b += k_b_delta * k_rate / 32;
- if (fix_r < 0) {
- fix_r = 0;
- }
- if (fix_g < 0) {
- fix_g = 0;
- }
- if (fix_b < 0) {
- fix_b = 0;
- }
- R = fix_r >> 8;
- G = fix_g >> 8;
- B = fix_b >> 8;
-}
-void AdobeCMYK_to_sRGB(FX_FLOAT c, FX_FLOAT m, FX_FLOAT y, FX_FLOAT k, FX_FLOAT& R, FX_FLOAT& G, FX_FLOAT& B)
-{
- FX_BYTE c1 = FXSYS_round(c * 255);
- FX_BYTE m1 = FXSYS_round(m * 255);
- FX_BYTE y1 = FXSYS_round(y * 255);
- FX_BYTE k1 = FXSYS_round(k * 255);
- FX_BYTE r, g, b;
- AdobeCMYK_to_sRGB1(c1, m1, y1, k1, r, g, b);
- R = 1.0f * r / 255;
- G = 1.0f * g / 255;
- B = 1.0f * b / 255;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../../include/fxcodec/fx_codec.h"
+#include "codec_int.h"
+#include "../lcms2/include/fx_lcms2.h"
+FX_BOOL MD5ComputeID( FX_LPCVOID buf, FX_DWORD dwSize, FX_BYTE ID[16] )
+{
+ return cmsMD5computeIDExt(buf, dwSize, ID);
+}
+struct CLcmsCmm : public CFX_Object {
+ cmsHTRANSFORM m_hTransform;
+ int m_nSrcComponents;
+ int m_nDstComponents;
+ FX_BOOL m_bLab;
+};
+extern "C" {
+ int ourHandler(int ErrorCode, const char *ErrorText)
+ {
+ return TRUE;
+ }
+};
+FX_BOOL CheckComponents(cmsColorSpaceSignature cs, int nComponents, FX_BOOL bDst)
+{
+ if (nComponents <= 0 || nComponents > 15) {
+ return FALSE;
+ }
+ switch(cs) {
+ case cmsSigLabData:
+ if (nComponents < 3) {
+ return FALSE;
+ }
+ break;
+ case cmsSigGrayData:
+ if (bDst && nComponents != 1) {
+ return FALSE;
+ } else if (!bDst && nComponents > 2) {
+ return FALSE;
+ }
+ break;
+ case cmsSigRgbData:
+ if (bDst && nComponents != 3) {
+ return FALSE;
+ }
+ break;
+ case cmsSigCmykData:
+ if (bDst && nComponents != 4) {
+ return FALSE;
+ }
+ break;
+ default:
+ if (nComponents != 3) {
+ return FALSE;
+ }
+ break;
+ }
+ return TRUE;
+}
+void* IccLib_CreateTransform(const unsigned char* pSrcProfileData, unsigned int dwSrcProfileSize, int nSrcComponents,
+ const unsigned char* pDstProfileData, unsigned int dwDstProfileSize, int nDstComponents,
+ int intent, FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT, FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT)
+{
+ cmsHPROFILE srcProfile = NULL;
+ cmsHPROFILE dstProfile = NULL;
+ cmsHTRANSFORM hTransform = NULL;
+ CLcmsCmm* pCmm = NULL;
+ srcProfile = cmsOpenProfileFromMem((void*)pSrcProfileData, dwSrcProfileSize);
+ if (srcProfile == NULL) {
+ return NULL;
+ }
+ if(pDstProfileData == NULL && dwDstProfileSize == 0 && nDstComponents == 3) {
+ dstProfile = cmsCreate_sRGBProfile();
+ } else {
+ dstProfile = cmsOpenProfileFromMem((void*)pDstProfileData, dwDstProfileSize);
+ }
+ if (dstProfile == NULL) {
+ cmsCloseProfile(srcProfile);
+ return NULL;
+ }
+ int srcFormat;
+ FX_BOOL bLab = FALSE;
+ cmsColorSpaceSignature srcCS = cmsGetColorSpace(srcProfile);
+ if (!CheckComponents(srcCS, nSrcComponents, FALSE)) {
+ cmsCloseProfile(srcProfile);
+ cmsCloseProfile(dstProfile);
+ return NULL;
+ }
+ if (srcCS == cmsSigLabData) {
+ srcFormat = COLORSPACE_SH(PT_Lab) | CHANNELS_SH(nSrcComponents) | BYTES_SH(0);
+ bLab = TRUE;
+ } else {
+ srcFormat = COLORSPACE_SH(PT_ANY) | CHANNELS_SH(nSrcComponents) | BYTES_SH(1);
+ if (srcCS == cmsSigRgbData && T_DOSWAP(dwSrcFormat)) {
+ srcFormat |= DOSWAP_SH(1);
+ }
+ }
+ cmsColorSpaceSignature dstCS = cmsGetColorSpace(dstProfile);
+ if (!CheckComponents(dstCS, nDstComponents, TRUE)) {
+ cmsCloseProfile(srcProfile);
+ cmsCloseProfile(dstProfile);
+ return NULL;
+ }
+ switch(dstCS) {
+ case cmsSigGrayData:
+ hTransform = cmsCreateTransform(srcProfile, srcFormat, dstProfile, TYPE_GRAY_8, intent, 0);
+ break;
+ case cmsSigRgbData:
+ hTransform = cmsCreateTransform(srcProfile, srcFormat, dstProfile, TYPE_BGR_8, intent, 0);
+ break;
+ case cmsSigCmykData:
+ hTransform = cmsCreateTransform(srcProfile, srcFormat, dstProfile,
+ T_DOSWAP(dwDstFormat) ? TYPE_KYMC_8 : TYPE_CMYK_8,
+ intent, 0);
+ break;
+ default:
+ break;
+ }
+ if (hTransform == NULL) {
+ cmsCloseProfile(srcProfile);
+ cmsCloseProfile(dstProfile);
+ return NULL;
+ }
+ pCmm = FX_NEW CLcmsCmm;
+ if (pCmm == NULL) {
+ return NULL;
+ }
+ pCmm->m_nSrcComponents = nSrcComponents;
+ pCmm->m_nDstComponents = nDstComponents;
+ pCmm->m_hTransform = hTransform;
+ pCmm->m_bLab = bLab;
+ cmsCloseProfile(srcProfile);
+ cmsCloseProfile(dstProfile);
+ return pCmm;
+}
+void* IccLib_CreateTransform_sRGB(const unsigned char* pProfileData, unsigned int dwProfileSize, int nComponents, int intent, FX_DWORD dwSrcFormat)
+{
+ return IccLib_CreateTransform(pProfileData, dwProfileSize, nComponents, NULL, 0, 3, intent, dwSrcFormat);
+}
+void IccLib_DestroyTransform(void* pTransform)
+{
+ if (pTransform == NULL) {
+ return;
+ }
+ cmsDeleteTransform(((CLcmsCmm*)pTransform)->m_hTransform);
+ delete (CLcmsCmm*)pTransform;
+}
+void IccLib_Translate(void* pTransform, FX_FLOAT* pSrcValues, FX_FLOAT* pDestValues)
+{
+ if (pTransform == NULL) {
+ return;
+ }
+ CLcmsCmm* p = (CLcmsCmm*)pTransform;
+ FX_BYTE output[4];
+ if (p->m_bLab) {
+ CFX_FixedBufGrow<double, 16> inputs(p->m_nSrcComponents);
+ double* input = inputs;
+ for (int i = 0; i < p->m_nSrcComponents; i ++) {
+ input[i] = pSrcValues[i];
+ }
+ cmsDoTransform(p->m_hTransform, input, output, 1);
+ } else {
+ CFX_FixedBufGrow<FX_BYTE, 16> inputs(p->m_nSrcComponents);
+ FX_BYTE* input = inputs;
+ for (int i = 0; i < p->m_nSrcComponents; i ++) {
+ if (pSrcValues[i] > 1.0f) {
+ input[i] = 255;
+ } else if (pSrcValues[i] < 0) {
+ input[i] = 0;
+ } else {
+ input[i] = (int)(pSrcValues[i] * 255.0f);
+ }
+ }
+ cmsDoTransform(p->m_hTransform, input, output, 1);
+ }
+ switch(p->m_nDstComponents) {
+ case 1:
+ pDestValues[0] = output[0] / 255.0f;
+ break;
+ case 3:
+ pDestValues[0] = output[2] / 255.0f;
+ pDestValues[1] = output[1] / 255.0f;
+ pDestValues[2] = output[0] / 255.0f;
+ break;
+ case 4:
+ pDestValues[0] = output[0] / 255.0f;
+ pDestValues[1] = output[1] / 255.0f;
+ pDestValues[2] = output[2] / 255.0f;
+ pDestValues[3] = output[3] / 255.0f;
+ break;
+ }
+}
+void IccLib_TranslateImage(void* pTransform, unsigned char* pDest, const unsigned char* pSrc, int pixels)
+{
+ cmsDoTransform(((CLcmsCmm*)pTransform)->m_hTransform, (void*)pSrc, pDest, pixels);
+}
+FX_LPVOID CreateProfile_Gray(double gamma)
+{
+ cmsCIExyY* D50 = (cmsCIExyY*)cmsD50_xyY();
+ if (!cmsWhitePointFromTemp(D50, 6504)) {
+ return NULL;
+ }
+ cmsToneCurve* curve = cmsBuildGamma(NULL, gamma);
+ if (curve == NULL) {
+ return NULL;
+ }
+ FX_LPVOID profile = cmsCreateGrayProfile(D50, curve);
+ cmsFreeToneCurve(curve);
+ return profile;
+}
+ICodec_IccModule::IccCS GetProfileCSFromHandle(FX_LPVOID pProfile)
+{
+ if (pProfile == NULL) {
+ return ICodec_IccModule::IccCS_Unknown;
+ }
+ switch (cmsGetColorSpace(pProfile)) {
+ case cmsSigXYZData:
+ return ICodec_IccModule::IccCS_XYZ;
+ case cmsSigLabData:
+ return ICodec_IccModule::IccCS_Lab;
+ case cmsSigLuvData:
+ return ICodec_IccModule::IccCS_Luv;
+ case cmsSigYCbCrData:
+ return ICodec_IccModule::IccCS_YCbCr;
+ case cmsSigYxyData:
+ return ICodec_IccModule::IccCS_Yxy;
+ case cmsSigRgbData:
+ return ICodec_IccModule::IccCS_Rgb;
+ case cmsSigGrayData:
+ return ICodec_IccModule::IccCS_Gray;
+ case cmsSigHsvData:
+ return ICodec_IccModule::IccCS_Hsv;
+ case cmsSigHlsData:
+ return ICodec_IccModule::IccCS_Hls;
+ case cmsSigCmykData:
+ return ICodec_IccModule::IccCS_Cmyk;
+ case cmsSigCmyData:
+ return ICodec_IccModule::IccCS_Cmy;
+ default:
+ return ICodec_IccModule::IccCS_Unknown;
+ }
+}
+ICodec_IccModule::IccCS CCodec_IccModule::GetProfileCS(FX_LPCBYTE pProfileData, unsigned int dwProfileSize)
+{
+ ICodec_IccModule::IccCS cs;
+ cmsHPROFILE hProfile = cmsOpenProfileFromMem((void*)pProfileData, dwProfileSize);
+ if (hProfile == NULL) {
+ return IccCS_Unknown;
+ }
+ cs = GetProfileCSFromHandle(hProfile);
+ if (hProfile) {
+ cmsCloseProfile(hProfile);
+ }
+ return cs;
+}
+ICodec_IccModule::IccCS CCodec_IccModule::GetProfileCS(IFX_FileRead* pFile)
+{
+ if (pFile == NULL) {
+ return IccCS_Unknown;
+ }
+ ICodec_IccModule::IccCS cs;
+ FX_DWORD dwSize = (FX_DWORD)pFile->GetSize();
+ FX_LPBYTE pBuf = FX_Alloc(FX_BYTE, dwSize);
+ if (pBuf == NULL) {
+ return IccCS_Unknown;
+ }
+ pFile->ReadBlock(pBuf, 0, dwSize);
+ cs = GetProfileCS(pBuf, dwSize);
+ FX_Free(pBuf);
+ return cs;
+}
+FX_DWORD TransferProfileType(FX_LPVOID pProfile, FX_DWORD dwFormat)
+{
+ cmsColorSpaceSignature cs = cmsGetColorSpace(pProfile);
+ switch (cs) {
+ case cmsSigXYZData:
+ return TYPE_XYZ_16;
+ case cmsSigLabData:
+ return TYPE_Lab_DBL;
+ case cmsSigLuvData:
+ return TYPE_YUV_8;
+ case cmsSigYCbCrData:
+ return TYPE_YCbCr_8;
+ case cmsSigYxyData:
+ return TYPE_Yxy_16;
+ case cmsSigRgbData:
+ return T_DOSWAP(dwFormat) ? TYPE_RGB_8 : TYPE_BGR_8;
+ case cmsSigGrayData:
+ return TYPE_GRAY_8;
+ case cmsSigHsvData:
+ return TYPE_HSV_8;
+ case cmsSigHlsData:
+ return TYPE_HLS_8;
+ case cmsSigCmykData:
+ return T_DOSWAP(dwFormat) ? TYPE_KYMC_8 : TYPE_CMYK_8;
+ case cmsSigCmyData:
+ return TYPE_CMY_8;
+ case cmsSigMCH5Data:
+ return T_DOSWAP(dwFormat) ? TYPE_KYMC5_8 : TYPE_CMYK5_8;
+ case cmsSigMCH6Data:
+ return TYPE_CMYK6_8;
+ case cmsSigMCH7Data:
+ return T_DOSWAP(dwFormat) ? TYPE_KYMC7_8 : TYPE_CMYK7_8;
+ case cmsSigMCH8Data:
+ return T_DOSWAP(dwFormat) ? TYPE_KYMC8_8 : TYPE_CMYK8_8;
+ case cmsSigMCH9Data:
+ return T_DOSWAP(dwFormat) ? TYPE_KYMC9_8 : TYPE_CMYK9_8;
+ case cmsSigMCHAData:
+ return T_DOSWAP(dwFormat) ? TYPE_KYMC10_8 : TYPE_CMYK10_8;
+ case cmsSigMCHBData:
+ return T_DOSWAP(dwFormat) ? TYPE_KYMC11_8 : TYPE_CMYK11_8;
+ case cmsSigMCHCData:
+ return T_DOSWAP(dwFormat) ? TYPE_KYMC12_8 : TYPE_CMYK12_8;
+ default:
+ return 0;
+ }
+}
+class CFX_IccProfileCache : public CFX_Object
+{
+public:
+ CFX_IccProfileCache();
+ ~CFX_IccProfileCache();
+ FX_LPVOID m_pProfile;
+ FX_DWORD m_dwRate;
+protected:
+ void Purge();
+};
+CFX_IccProfileCache::CFX_IccProfileCache()
+{
+ m_pProfile = NULL;
+ m_dwRate = 1;
+}
+CFX_IccProfileCache::~CFX_IccProfileCache()
+{
+ if (m_pProfile) {
+ cmsCloseProfile(m_pProfile);
+ }
+}
+void CFX_IccProfileCache::Purge()
+{
+}
+class CFX_IccTransformCache : public CFX_Object
+{
+public:
+ CFX_IccTransformCache(CLcmsCmm* pCmm = NULL);
+ ~CFX_IccTransformCache();
+ FX_LPVOID m_pIccTransform;
+ FX_DWORD m_dwRate;
+ CLcmsCmm* m_pCmm;
+protected:
+ void Purge();
+};
+CFX_IccTransformCache::CFX_IccTransformCache(CLcmsCmm* pCmm)
+{
+ m_pIccTransform = NULL;
+ m_dwRate = 1;
+ m_pCmm = pCmm;
+}
+CFX_IccTransformCache::~CFX_IccTransformCache()
+{
+ if (m_pIccTransform) {
+ cmsDeleteTransform(m_pIccTransform);
+ }
+ if (m_pCmm) {
+ FX_Free(m_pCmm);
+ }
+}
+void CFX_IccTransformCache::Purge()
+{
+}
+class CFX_ByteStringKey : public CFX_BinaryBuf
+{
+public:
+ CFX_ByteStringKey() : CFX_BinaryBuf() {}
+ CFX_ByteStringKey& operator << (FX_DWORD i);
+};
+CFX_ByteStringKey& CFX_ByteStringKey::operator << (FX_DWORD i)
+{
+ AppendBlock(&i, sizeof(FX_DWORD));
+ return *this;
+}
+FX_LPVOID CCodec_IccModule::CreateProfile(ICodec_IccModule::IccParam* pIccParam, Icc_CLASS ic, CFX_BinaryBuf* pTransformKey)
+{
+ CFX_IccProfileCache* pCache = NULL;
+ CFX_ByteStringKey key;
+ CFX_ByteString text;
+ key << pIccParam->ColorSpace << (pIccParam->dwProfileType | ic << 8);
+ FX_BYTE ID[16];
+ switch (pIccParam->dwProfileType) {
+ case Icc_PARAMTYPE_NONE:
+ return NULL;
+ case Icc_PARAMTYPE_BUFFER:
+ MD5ComputeID(pIccParam->pProfileData, pIccParam->dwProfileSize, ID);
+ break;
+ case Icc_PARAMTYPE_PARAM:
+ FXSYS_memset32(ID, 0, 16);
+ switch (pIccParam->ColorSpace) {
+ case IccCS_Gray:
+ text.Format("%lf", pIccParam->Gamma);
+ break;
+ default:
+ ;
+ }
+ MD5ComputeID(text.GetBuffer(0), text.GetLength(), ID);
+ break;
+ default:
+ ;
+ }
+ key.AppendBlock(ID, 16);
+ CFX_ByteString ProfileKey(key.GetBuffer(), key.GetSize());
+ ASSERT(pTransformKey);
+ pTransformKey->AppendBlock(ProfileKey.GetBuffer(0), ProfileKey.GetLength());
+ if (!m_MapProfile.Lookup(ProfileKey, (FX_LPVOID&)pCache)) {
+ pCache = FX_NEW CFX_IccProfileCache;
+ if (pCache == NULL) {
+ return NULL;
+ }
+ switch (pIccParam->dwProfileType) {
+ case Icc_PARAMTYPE_BUFFER:
+ pCache->m_pProfile = cmsOpenProfileFromMem(pIccParam->pProfileData, pIccParam->dwProfileSize);
+ break;
+ case Icc_PARAMTYPE_PARAM:
+ switch (pIccParam->ColorSpace) {
+ case IccCS_Rgb:
+ pCache->m_pProfile = cmsCreate_sRGBProfile();
+ break;
+ case IccCS_Gray:
+ pCache->m_pProfile = CreateProfile_Gray(pIccParam->Gamma);
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+ m_MapProfile.SetAt(ProfileKey, pCache);
+ } else {
+ pCache->m_dwRate++;
+ }
+ return pCache->m_pProfile;
+}
+FX_LPVOID CCodec_IccModule::CreateTransform(ICodec_IccModule::IccParam* pInputParam,
+ ICodec_IccModule::IccParam* pOutputParam,
+ ICodec_IccModule::IccParam* pProofParam,
+ FX_DWORD dwIntent, FX_DWORD dwFlag, FX_DWORD dwPrfIntent, FX_DWORD dwPrfFlag)
+{
+ CLcmsCmm* pCmm = NULL;
+ ASSERT(pInputParam && pOutputParam);
+ CFX_ByteStringKey key;
+ FX_LPVOID pInputProfile = CreateProfile(pInputParam, Icc_CLASS_INPUT, &key);
+ if (pInputProfile == NULL) {
+ return NULL;
+ }
+ FX_LPVOID pOutputProfile = CreateProfile(pOutputParam, Icc_CLASS_OUTPUT, &key);
+ if (pOutputProfile == NULL) {
+ return NULL;
+ }
+ FX_DWORD dwInputProfileType = TransferProfileType(pInputProfile, pInputParam->dwFormat);
+ FX_DWORD dwOutputProfileType = TransferProfileType(pOutputProfile, pOutputParam->dwFormat);
+ if (dwInputProfileType == 0 || dwOutputProfileType == 0) {
+ return NULL;
+ }
+ FX_LPVOID pProofProfile = NULL;
+ if (pProofParam) {
+ pProofProfile = CreateProfile(pProofParam, Icc_CLASS_PROOF, &key);
+ }
+ key << dwInputProfileType << dwOutputProfileType << dwIntent << dwFlag << (pProofProfile != NULL) << dwPrfIntent << dwPrfFlag;
+ CFX_ByteStringC TransformKey(key.GetBuffer(), key.GetSize());
+ CFX_IccTransformCache* pTransformCache;
+ if (!m_MapTranform.Lookup(TransformKey, (FX_LPVOID&)pTransformCache)) {
+ pCmm = FX_Alloc(CLcmsCmm, 1);
+ if (pCmm == NULL) {
+ return NULL;
+ }
+ pCmm->m_nSrcComponents = T_CHANNELS(dwInputProfileType);
+ pCmm->m_nDstComponents = T_CHANNELS(dwOutputProfileType);
+ pCmm->m_bLab = T_COLORSPACE(pInputParam->dwFormat) == PT_Lab;
+ pTransformCache = FX_NEW CFX_IccTransformCache(pCmm);
+ if (pTransformCache == NULL) {
+ FX_Free(pCmm);
+ return NULL;
+ }
+ if (pProofProfile) {
+ pTransformCache->m_pIccTransform = cmsCreateProofingTransform(pInputProfile, dwInputProfileType, pOutputProfile, dwOutputProfileType,
+ pProofProfile, dwIntent, dwPrfIntent, dwPrfFlag);
+ } else {
+ pTransformCache->m_pIccTransform = cmsCreateTransform(pInputProfile, dwInputProfileType, pOutputProfile, dwOutputProfileType,
+ dwIntent, dwFlag);
+ }
+ pCmm->m_hTransform = pTransformCache->m_pIccTransform;
+ m_MapTranform.SetAt(TransformKey, pTransformCache);
+ } else {
+ pTransformCache->m_dwRate++;
+ }
+ return pTransformCache->m_pCmm;
+}
+CCodec_IccModule::~CCodec_IccModule()
+{
+ FX_POSITION pos = m_MapProfile.GetStartPosition();
+ CFX_ByteString key;
+ CFX_IccProfileCache* pProfileCache;
+ while (pos) {
+ m_MapProfile.GetNextAssoc(pos, key, (FX_LPVOID&)pProfileCache);
+ if (pProfileCache) {
+ delete pProfileCache;
+ }
+ }
+ pos = m_MapTranform.GetStartPosition();
+ CFX_IccTransformCache* pTransformCache;
+ while (pos) {
+ m_MapTranform.GetNextAssoc(pos, key, (FX_LPVOID&)pTransformCache);
+ if (pTransformCache) {
+ delete pTransformCache;
+ }
+ }
+}
+void* CCodec_IccModule::CreateTransform_sRGB(FX_LPCBYTE pProfileData, unsigned int dwProfileSize, int nComponents, int intent, FX_DWORD dwSrcFormat)
+{
+ return IccLib_CreateTransform_sRGB(pProfileData, dwProfileSize, nComponents, intent, dwSrcFormat);
+}
+void* CCodec_IccModule::CreateTransform_CMYK(FX_LPCBYTE pSrcProfileData, unsigned int dwSrcProfileSize, int nSrcComponents,
+ FX_LPCBYTE pDstProfileData, unsigned int dwDstProfileSize, int intent,
+ FX_DWORD dwSrcFormat , FX_DWORD dwDstFormat)
+{
+ return IccLib_CreateTransform(pSrcProfileData, dwSrcProfileSize, nSrcComponents,
+ pDstProfileData, dwDstProfileSize, 4, intent, dwSrcFormat, dwDstFormat);
+}
+void CCodec_IccModule::DestroyTransform(void* pTransform)
+{
+ IccLib_DestroyTransform(pTransform);
+}
+void CCodec_IccModule::Translate(void* pTransform, FX_FLOAT* pSrcValues, FX_FLOAT* pDestValues)
+{
+ IccLib_Translate(pTransform, pSrcValues, pDestValues);
+}
+void CCodec_IccModule::TranslateScanline(void* pTransform, FX_LPBYTE pDest, FX_LPCBYTE pSrc, int pixels)
+{
+ IccLib_TranslateImage(pTransform, pDest, pSrc, pixels);
+}
+const FX_BYTE g_CMYKSamples[81 * 81 * 3] = {
+ 255, 255, 255, 225, 226, 228, 199, 200, 202, 173, 174, 178, 147, 149, 152, 123, 125, 128, 99, 99, 102, 69, 70, 71, 34, 30, 31,
+ 255, 253, 229, 226, 224, 203, 200, 199, 182, 173, 173, 158, 149, 148, 135, 125, 124, 113, 99, 99, 90, 70, 69, 63, 33, 29, 24,
+ 255, 251, 204, 228, 223, 182, 201, 198, 163, 174, 172, 142, 150, 147, 122, 125, 123, 101, 99, 98, 80, 70, 68, 54, 32, 28, 16,
+ 255, 249, 179, 230, 222, 160, 203, 197, 144, 174, 170, 124, 150, 145, 105, 125, 122, 88, 99, 97, 69, 70, 68, 46, 31, 28, 6,
+ 255, 247, 154, 229, 220, 138, 203, 195, 122, 176, 169, 107, 150, 145, 91, 125, 121, 74, 100, 96, 57, 70, 67, 35, 29, 26, 0,
+ 255, 246, 128, 231, 217, 114, 205, 194, 101, 176, 167, 88, 150, 144, 75, 125, 120, 60, 100, 96, 44, 70, 66, 24, 28, 26, 0,
+ 255, 244, 96, 231, 217, 87, 203, 192, 78, 175, 167, 66, 150, 143, 56, 125, 119, 43, 100, 95, 29, 69, 66, 7, 26, 26, 0,
+ 255, 243, 51, 232, 215, 51, 204, 191, 43, 176, 165, 38, 150, 142, 28, 125, 118, 17, 99, 94, 0, 68, 65, 0, 24, 25, 0,
+ 255, 241, 0, 231, 215, 0, 203, 190, 0, 176, 164, 0, 150, 141, 0, 126, 117, 0, 99, 93, 0, 68, 65, 0, 24, 25, 0,
+ 252, 228, 238, 222, 201, 211, 197, 180, 190, 171, 156, 166, 147, 133, 143, 123, 111, 119, 99, 88, 94, 71, 61, 66, 34, 22, 26,
+ 254, 226, 213, 224, 201, 191, 199, 179, 171, 172, 155, 148, 147, 133, 128, 123, 110, 106, 98, 87, 83, 70, 59, 57, 33, 21, 18,
+ 254, 224, 191, 224, 199, 172, 200, 177, 153, 173, 154, 133, 147, 132, 115, 123, 109, 94, 98, 86, 74, 70, 59, 49, 32, 21, 9,
+ 255, 222, 168, 227, 198, 150, 200, 175, 135, 173, 153, 118, 148, 130, 99, 123, 109, 82, 98, 86, 64, 69, 58, 40, 31, 19, 0,
+ 255, 221, 145, 227, 196, 129, 201, 174, 115, 173, 151, 99, 148, 129, 85, 124, 108, 69, 98, 85, 52, 69, 58, 30, 30, 19, 0,
+ 255, 219, 121, 227, 195, 109, 201, 174, 97, 174, 150, 83, 148, 129, 70, 124, 107, 55, 98, 84, 40, 69, 58, 19, 28, 18, 0,
+ 255, 218, 92, 229, 194, 82, 202, 173, 75, 174, 150, 63, 149, 128, 51, 124, 106, 39, 98, 84, 24, 68, 57, 3, 26, 18, 0,
+ 255, 217, 54, 228, 193, 52, 201, 172, 46, 174, 148, 36, 148, 127, 27, 123, 105, 14, 98, 83, 0, 68, 56, 0, 25, 18, 0,
+ 255, 216, 0, 229, 192, 2, 202, 171, 4, 173, 148, 0, 148, 126, 0, 124, 105, 0, 98, 83, 0, 68, 56, 0, 24, 17, 0,
+ 249, 204, 223, 219, 181, 199, 195, 160, 178, 170, 140, 156, 146, 119, 134, 123, 99, 112, 98, 77, 88, 70, 52, 61, 34, 11, 20,
+ 250, 201, 200, 221, 180, 178, 197, 159, 161, 171, 139, 139, 147, 119, 120, 123, 98, 99, 98, 77, 78, 69, 51, 52, 34, 11, 10,
+ 252, 201, 180, 223, 179, 162, 197, 159, 144, 170, 138, 125, 146, 117, 107, 122, 97, 89, 98, 76, 69, 69, 50, 44, 32, 11, 2,
+ 252, 199, 158, 222, 177, 143, 199, 158, 127, 171, 137, 110, 147, 117, 93, 122, 96, 76, 97, 75, 58, 69, 50, 36, 32, 10, 0,
+ 253, 198, 137, 223, 177, 123, 198, 156, 110, 171, 136, 95, 146, 116, 80, 122, 96, 65, 97, 75, 47, 69, 50, 25, 30, 10, 0,
+ 254, 197, 115, 225, 175, 104, 198, 156, 92, 172, 135, 79, 147, 115, 66, 123, 95, 52, 98, 74, 37, 69, 49, 15, 29, 10, 0,
+ 254, 196, 89, 224, 175, 80, 199, 154, 70, 172, 134, 59, 146, 114, 48, 122, 95, 36, 97, 74, 21, 68, 49, 0, 27, 9, 0,
+ 255, 195, 57, 225, 173, 52, 198, 154, 44, 172, 133, 36, 147, 113, 26, 123, 94, 14, 98, 74, 0, 68, 49, 0, 26, 10, 0,
+ 254, 194, 15, 225, 172, 12, 198, 153, 7, 172, 132, 3, 146, 113, 0, 123, 93, 0, 98, 73, 0, 68, 49, 0, 26, 9, 0,
+ 246, 178, 209, 218, 159, 186, 194, 140, 166, 168, 122, 145, 144, 104, 125, 121, 85, 103, 97, 65, 81, 69, 41, 55, 34, 0, 12,
+ 248, 176, 186, 219, 157, 166, 195, 139, 149, 168, 121, 130, 144, 103, 111, 121, 85, 91, 97, 65, 71, 69, 41, 46, 34, 0, 4,
+ 249, 175, 168, 220, 156, 150, 196, 139, 135, 169, 121, 116, 144, 103, 100, 122, 84, 83, 98, 65, 63, 70, 41, 39, 33, 0, 0,
+ 249, 175, 148, 220, 155, 133, 196, 138, 119, 169, 120, 103, 145, 101, 87, 121, 83, 71, 97, 65, 54, 69, 41, 31, 32, 0, 0,
+ 249, 173, 128, 222, 154, 115, 195, 137, 102, 170, 119, 88, 145, 101, 74, 122, 83, 59, 97, 64, 43, 68, 40, 20, 30, 0, 0,
+ 250, 172, 108, 221, 154, 98, 195, 136, 86, 170, 118, 73, 145, 100, 61, 122, 82, 48, 97, 63, 32, 69, 40, 11, 28, 0, 0,
+ 250, 171, 85, 221, 153, 76, 196, 136, 67, 170, 117, 56, 145, 99, 44, 121, 82, 33, 97, 63, 17, 68, 40, 0, 28, 0, 0,
+ 251, 171, 58, 222, 152, 50, 197, 135, 43, 169, 117, 34, 146, 99, 25, 121, 81, 10, 96, 63, 0, 68, 40, 0, 27, 0, 0,
+ 250, 170, 26, 222, 151, 19, 196, 134, 13, 169, 116, 4, 145, 99, 0, 122, 81, 0, 97, 63, 0, 67, 40, 0, 26, 0, 0,
+ 244, 153, 194, 215, 136, 173, 192, 121, 155, 167, 104, 135, 143, 89, 115, 121, 72, 96, 97, 54, 75, 70, 31, 49, 34, 0, 6,
+ 245, 153, 173, 216, 136, 155, 192, 120, 138, 167, 104, 121, 144, 88, 103, 121, 71, 85, 97, 54, 66, 69, 31, 42, 34, 0, 0,
+ 246, 152, 157, 217, 135, 140, 193, 120, 126, 167, 103, 109, 143, 88, 92, 121, 72, 76, 97, 54, 58, 69, 31, 35, 33, 0, 0,
+ 245, 150, 139, 218, 134, 125, 193, 119, 111, 167, 103, 96, 144, 87, 80, 121, 71, 66, 96, 53, 49, 68, 31, 26, 32, 0, 0,
+ 246, 151, 122, 218, 133, 108, 194, 118, 96, 168, 102, 81, 144, 86, 69, 120, 71, 55, 95, 53, 39, 68, 30, 17, 31, 0, 0,
+ 248, 150, 103, 218, 133, 91, 193, 118, 81, 168, 102, 69, 143, 86, 56, 120, 70, 43, 96, 53, 28, 68, 31, 6, 29, 0, 0,
+ 247, 149, 81, 218, 132, 72, 194, 117, 62, 168, 101, 52, 144, 86, 42, 121, 70, 29, 96, 52, 13, 68, 30, 0, 28, 0, 0,
+ 247, 148, 55, 219, 131, 50, 194, 117, 43, 167, 101, 32, 144, 85, 22, 120, 69, 8, 96, 52, 0, 67, 30, 0, 27, 0, 0,
+ 247, 147, 29, 218, 131, 24, 194, 116, 20, 168, 100, 11, 144, 85, 0, 120, 69, 0, 96, 52, 0, 67, 30, 0, 26, 0, 0,
+ 242, 130, 179, 214, 114, 160, 190, 101, 143, 166, 87, 125, 143, 72, 107, 120, 58, 88, 96, 42, 68, 69, 17, 44, 35, 0, 0,
+ 243, 129, 161, 215, 114, 143, 191, 101, 128, 166, 87, 113, 143, 73, 96, 120, 58, 79, 96, 41, 60, 69, 18, 37, 33, 0, 0,
+ 243, 129, 146, 216, 114, 130, 192, 101, 117, 166, 87, 101, 143, 72, 86, 121, 58, 69, 96, 42, 52, 69, 18, 29, 31, 0, 0,
+ 243, 128, 130, 216, 114, 115, 191, 101, 102, 165, 86, 88, 142, 72, 75, 120, 58, 60, 95, 42, 43, 68, 19, 21, 30, 0, 0,
+ 244, 127, 112, 217, 113, 101, 192, 99, 89, 166, 85, 75, 142, 72, 63, 119, 57, 50, 96, 41, 35, 68, 19, 13, 30, 0, 0,
+ 244, 127, 96, 216, 112, 86, 191, 99, 75, 166, 86, 64, 143, 72, 52, 120, 57, 40, 95, 41, 24, 67, 20, 1, 29, 0, 0,
+ 245, 126, 77, 216, 113, 68, 191, 100, 59, 166, 85, 49, 142, 71, 38, 119, 57, 26, 95, 41, 10, 67, 20, 0, 28, 0, 0,
+ 244, 126, 55, 216, 112, 48, 191, 99, 40, 166, 85, 31, 143, 71, 20, 119, 57, 6, 95, 42, 0, 67, 20, 0, 28, 0, 0,
+ 245, 126, 33, 217, 112, 26, 192, 99, 22, 166, 84, 11, 142, 70, 0, 119, 57, 0, 95, 41, 0, 66, 20, 0, 27, 0, 0,
+ 241, 102, 167, 213, 90, 149, 189, 79, 133, 165, 66, 115, 141, 54, 98, 119, 41, 81, 96, 25, 63, 69, 0, 38, 30, 0, 0,
+ 241, 102, 149, 213, 90, 133, 189, 79, 119, 165, 66, 103, 142, 55, 88, 119, 41, 71, 96, 25, 53, 69, 0, 31, 28, 0, 0,
+ 241, 102, 135, 214, 90, 121, 190, 79, 108, 165, 66, 92, 141, 55, 78, 119, 42, 63, 96, 26, 46, 69, 0, 24, 28, 0, 0,
+ 241, 101, 120, 214, 90, 107, 189, 79, 95, 165, 67, 83, 141, 54, 68, 118, 41, 54, 95, 27, 39, 68, 0, 16, 27, 0, 0,
+ 241, 102, 106, 213, 90, 93, 189, 78, 82, 164, 67, 70, 141, 55, 58, 118, 42, 45, 94, 27, 29, 67, 2, 6, 27, 0, 0,
+ 242, 101, 90, 214, 89, 79, 190, 79, 69, 166, 67, 59, 141, 55, 47, 118, 41, 35, 95, 27, 19, 67, 3, 0, 26, 0, 0,
+ 242, 102, 72, 213, 89, 63, 191, 79, 56, 164, 67, 45, 141, 55, 34, 118, 42, 22, 94, 28, 6, 67, 3, 0, 26, 0, 0,
+ 242, 100, 51, 214, 89, 45, 190, 78, 38, 164, 67, 30, 141, 55, 18, 118, 42, 3, 95, 28, 0, 66, 4, 0, 26, 0, 0,
+ 243, 100, 33, 214, 90, 27, 190, 78, 22, 165, 67, 13, 141, 55, 0, 118, 43, 0, 94, 29, 0, 66, 5, 0, 26, 0, 0,
+ 237, 69, 153, 211, 58, 135, 187, 51, 121, 163, 41, 105, 141, 28, 90, 118, 15, 73, 96, 0, 56, 68, 0, 33, 25, 0, 0,
+ 239, 67, 137, 212, 60, 123, 189, 50, 110, 163, 41, 94, 141, 29, 79, 118, 17, 65, 95, 0, 48, 69, 0, 26, 25, 0, 0,
+ 240, 69, 124, 211, 60, 111, 188, 50, 98, 163, 42, 85, 141, 31, 72, 118, 18, 57, 94, 0, 41, 68, 0, 19, 25, 0, 0,
+ 240, 70, 112, 212, 61, 99, 188, 52, 87, 163, 41, 74, 140, 31, 62, 118, 20, 48, 94, 2, 32, 68, 0, 11, 24, 0, 0,
+ 239, 70, 98, 212, 62, 86, 188, 53, 77, 164, 42, 64, 140, 32, 52, 118, 20, 40, 94, 3, 24, 67, 0, 3, 23, 0, 0,
+ 239, 71, 85, 212, 61, 74, 187, 53, 65, 163, 44, 54, 140, 34, 43, 118, 22, 30, 95, 3, 14, 67, 0, 0, 23, 0, 0,
+ 239, 70, 67, 212, 62, 59, 188, 53, 51, 163, 45, 42, 141, 34, 31, 117, 22, 17, 94, 5, 2, 66, 0, 0, 23, 0, 0,
+ 239, 71, 50, 213, 62, 43, 188, 54, 37, 164, 45, 28, 139, 34, 16, 117, 22, 2, 94, 7, 0, 65, 0, 0, 23, 0, 0,
+ 240, 71, 34, 212, 63, 29, 189, 54, 24, 163, 46, 15, 139, 36, 2, 117, 25, 0, 94, 8, 0, 66, 0, 0, 23, 0, 0,
+ 237, 0, 140, 209, 0, 124, 186, 0, 112, 162, 0, 97, 141, 0, 82, 118, 0, 67, 95, 0, 49, 68, 0, 27, 20, 0, 0,
+ 237, 0, 126, 210, 0, 113, 187, 0, 99, 163, 0, 86, 139, 0, 72, 118, 0, 58, 95, 0, 42, 67, 0, 20, 20, 0, 0,
+ 237, 1, 114, 209, 1, 102, 187, 0, 90, 163, 0, 78, 139, 0, 64, 118, 0, 50, 95, 0, 35, 67, 0, 13, 20, 0, 0,
+ 236, 16, 102, 209, 7, 91, 186, 0, 80, 162, 0, 68, 139, 0, 56, 117, 0, 43, 94, 0, 27, 67, 0, 6, 20, 0, 0,
+ 238, 15, 89, 209, 13, 79, 186, 6, 69, 162, 0, 58, 139, 0, 47, 117, 0, 34, 93, 0, 20, 66, 0, 2, 20, 0, 0,
+ 237, 20, 78, 210, 12, 68, 187, 4, 59, 163, 0, 49, 139, 0, 38, 116, 0, 26, 94, 0, 11, 66, 0, 0, 20, 0, 0,
+ 237, 25, 64, 210, 18, 56, 186, 11, 48, 162, 4, 39, 138, 0, 27, 117, 0, 14, 93, 0, 0, 66, 0, 0, 20, 0, 0,
+ 238, 25, 48, 210, 22, 43, 186, 15, 35, 162, 8, 26, 140, 0, 14, 117, 0, 0, 93, 0, 0, 65, 0, 0, 20, 0, 0,
+ 238, 28, 35, 210, 21, 30, 187, 15, 24, 162, 8, 16, 139, 1, 2, 117, 0, 0, 93, 0, 0, 65, 0, 0, 22, 0, 0,
+ 219, 242, 252, 195, 214, 225, 172, 191, 201, 148, 165, 175, 127, 142, 150, 106, 119, 126, 84, 95, 101, 58, 66, 72, 24, 27, 32,
+ 222, 239, 226, 196, 213, 202, 173, 189, 180, 150, 165, 158, 129, 141, 135, 107, 118, 113, 85, 94, 90, 58, 66, 63, 21, 26, 24,
+ 223, 237, 203, 198, 211, 182, 175, 188, 163, 152, 164, 141, 129, 140, 121, 107, 117, 101, 85, 93, 80, 58, 64, 54, 21, 26, 18,
+ 226, 236, 179, 201, 210, 160, 177, 187, 143, 153, 162, 125, 130, 139, 106, 108, 116, 89, 85, 92, 69, 58, 64, 45, 20, 25, 8,
+ 227, 234, 153, 201, 208, 139, 178, 185, 124, 154, 161, 107, 131, 138, 91, 108, 115, 75, 85, 91, 58, 58, 63, 35, 17, 25, 0,
+ 229, 233, 130, 203, 207, 116, 178, 184, 104, 154, 160, 90, 131, 137, 76, 109, 114, 62, 85, 90, 46, 58, 63, 25, 16, 24, 0,
+ 230, 231, 100, 202, 205, 90, 179, 183, 80, 154, 159, 69, 131, 136, 57, 109, 113, 46, 86, 90, 32, 58, 63, 10, 14, 24, 0,
+ 230, 230, 65, 204, 204, 58, 180, 182, 52, 155, 157, 44, 132, 135, 35, 110, 113, 24, 86, 89, 9, 57, 62, 0, 11, 24, 0,
+ 232, 230, 19, 204, 204, 19, 180, 181, 17, 155, 157, 10, 131, 134, 2, 109, 112, 0, 85, 89, 0, 57, 62, 0, 10, 23, 0,
+ 218, 216, 236, 194, 192, 211, 172, 171, 188, 149, 149, 164, 128, 127, 141, 106, 106, 119, 84, 84, 94, 59, 57, 66, 25, 18, 26,
+ 221, 214, 211, 196, 191, 190, 174, 170, 170, 150, 148, 148, 128, 126, 127, 107, 105, 106, 85, 83, 84, 59, 56, 58, 23, 17, 18,
+ 222, 213, 190, 197, 189, 170, 175, 169, 153, 151, 147, 133, 129, 126, 113, 108, 105, 94, 85, 82, 74, 59, 56, 49, 22, 17, 11,
+ 224, 211, 168, 199, 188, 151, 175, 168, 135, 152, 146, 117, 129, 124, 99, 107, 103, 82, 84, 82, 64, 59, 55, 41, 21, 17, 1,
+ 224, 210, 145, 199, 187, 130, 176, 166, 117, 152, 145, 101, 129, 123, 86, 107, 103, 70, 85, 81, 53, 58, 55, 31, 19, 17, 0,
+ 227, 208, 123, 200, 186, 110, 177, 165, 98, 153, 143, 84, 130, 122, 70, 108, 102, 57, 85, 80, 41, 58, 54, 20, 18, 16, 0,
+ 227, 208, 97, 202, 185, 86, 177, 164, 77, 153, 142, 65, 130, 122, 54, 108, 101, 42, 85, 80, 27, 58, 54, 7, 16, 16, 0,
+ 228, 206, 66, 202, 184, 58, 178, 163, 50, 154, 141, 42, 131, 121, 33, 109, 101, 21, 86, 79, 5, 58, 54, 0, 13, 16, 0,
+ 228, 206, 29, 202, 183, 25, 178, 163, 20, 154, 141, 15, 131, 121, 5, 108, 100, 0, 85, 79, 0, 58, 53, 0, 13, 16, 0,
+ 217, 193, 221, 193, 172, 198, 172, 153, 178, 149, 133, 154, 128, 114, 132, 107, 94, 111, 85, 74, 89, 59, 49, 61, 25, 8, 22,
+ 219, 191, 198, 195, 171, 178, 173, 153, 159, 149, 132, 139, 128, 113, 119, 107, 94, 100, 85, 73, 79, 59, 48, 52, 25, 7, 14,
+ 221, 191, 180, 196, 170, 160, 174, 152, 144, 150, 132, 125, 129, 113, 107, 107, 93, 89, 85, 73, 69, 59, 48, 45, 23, 7, 4,
+ 222, 189, 159, 197, 169, 142, 174, 151, 127, 151, 131, 110, 129, 112, 94, 108, 93, 78, 85, 72, 60, 58, 47, 37, 22, 7, 0,
+ 223, 188, 138, 197, 168, 123, 175, 150, 109, 151, 130, 95, 130, 111, 81, 108, 92, 65, 85, 72, 49, 59, 47, 27, 21, 7, 0,
+ 224, 187, 118, 198, 167, 105, 176, 149, 93, 152, 129, 79, 130, 110, 68, 108, 91, 54, 85, 71, 38, 59, 47, 17, 18, 7, 0,
+ 224, 187, 93, 199, 166, 83, 176, 148, 73, 152, 128, 62, 129, 109, 51, 108, 90, 39, 85, 71, 25, 58, 46, 3, 16, 8, 0,
+ 226, 186, 64, 200, 165, 57, 177, 147, 50, 153, 127, 40, 130, 108, 31, 108, 90, 19, 85, 70, 3, 58, 46, 0, 16, 8, 0,
+ 227, 185, 35, 200, 165, 30, 176, 146, 25, 152, 127, 18, 130, 108, 7, 108, 89, 0, 85, 70, 0, 57, 46, 0, 14, 8, 0,
+ 216, 169, 205, 192, 150, 184, 171, 134, 164, 149, 116, 144, 128, 99, 124, 107, 81, 103, 85, 63, 81, 60, 39, 55, 26, 0, 15,
+ 217, 168, 186, 193, 150, 165, 172, 134, 149, 150, 116, 130, 128, 99, 111, 107, 81, 92, 85, 62, 72, 59, 39, 47, 25, 0, 6,
+ 219, 168, 168, 194, 149, 150, 173, 133, 135, 150, 116, 117, 128, 98, 99, 107, 80, 82, 86, 62, 63, 59, 38, 39, 24, 0, 0,
+ 219, 166, 148, 195, 149, 133, 173, 133, 119, 150, 115, 103, 128, 98, 88, 107, 80, 72, 85, 61, 54, 59, 38, 32, 23, 0, 0,
+ 220, 166, 129, 196, 148, 116, 174, 132, 103, 151, 114, 89, 129, 97, 75, 107, 79, 60, 85, 61, 44, 59, 38, 22, 21, 0, 0,
+ 222, 164, 110, 197, 147, 99, 175, 131, 87, 151, 113, 75, 129, 96, 63, 107, 79, 49, 85, 61, 33, 58, 38, 12, 19, 0, 0,
+ 222, 164, 88, 197, 146, 79, 174, 130, 69, 151, 113, 58, 129, 95, 47, 107, 78, 35, 85, 60, 20, 58, 38, 0, 18, 0, 0,
+ 223, 164, 63, 198, 145, 55, 175, 129, 48, 151, 112, 39, 129, 95, 29, 107, 78, 16, 85, 60, 1, 58, 38, 0, 17, 0, 0,
+ 223, 163, 36, 198, 145, 32, 174, 129, 26, 151, 111, 17, 129, 95, 7, 107, 78, 0, 84, 60, 0, 57, 37, 0, 15, 0, 0,
+ 215, 147, 192, 191, 130, 172, 170, 116, 153, 148, 100, 133, 127, 85, 115, 107, 69, 96, 85, 51, 75, 60, 28, 50, 25, 0, 8,
+ 217, 146, 173, 192, 130, 154, 171, 115, 138, 149, 100, 121, 128, 84, 103, 107, 68, 85, 85, 51, 66, 60, 28, 42, 25, 0, 0,
+ 217, 145, 157, 193, 129, 140, 173, 115, 125, 149, 100, 109, 128, 84, 92, 107, 68, 76, 85, 51, 58, 59, 28, 35, 23, 0, 0,
+ 218, 145, 140, 193, 129, 125, 172, 114, 110, 149, 99, 96, 128, 83, 81, 107, 67, 65, 84, 51, 49, 59, 29, 27, 22, 0, 0,
+ 219, 144, 121, 194, 128, 108, 172, 113, 96, 149, 98, 83, 128, 83, 69, 107, 68, 55, 85, 50, 40, 59, 28, 18, 20, 0, 0,
+ 220, 143, 104, 195, 128, 93, 173, 114, 82, 150, 98, 69, 127, 82, 58, 107, 67, 45, 85, 50, 30, 59, 28, 7, 19, 0, 0,
+ 220, 143, 84, 195, 127, 74, 173, 113, 65, 149, 97, 55, 128, 82, 44, 106, 67, 32, 84, 50, 16, 58, 28, 0, 18, 0, 0,
+ 221, 142, 62, 196, 126, 53, 173, 112, 46, 150, 97, 37, 128, 82, 26, 107, 66, 14, 84, 50, 0, 58, 28, 0, 16, 0, 0,
+ 222, 142, 38, 196, 126, 34, 174, 112, 27, 150, 96, 17, 128, 82, 6, 106, 66, 0, 84, 50, 0, 57, 29, 0, 16, 0, 0,
+ 214, 123, 179, 191, 110, 159, 169, 98, 143, 147, 84, 124, 126, 70, 106, 107, 55, 88, 85, 39, 69, 60, 15, 45, 23, 0, 2,
+ 216, 123, 161, 192, 110, 144, 170, 98, 129, 148, 84, 112, 127, 70, 95, 107, 55, 79, 85, 39, 61, 60, 15, 37, 20, 0, 0,
+ 217, 122, 145, 192, 110, 130, 170, 97, 116, 149, 84, 101, 127, 70, 85, 106, 55, 70, 85, 39, 53, 59, 16, 30, 19, 0, 0,
+ 217, 123, 131, 192, 109, 116, 171, 96, 103, 149, 83, 89, 127, 70, 75, 106, 55, 60, 85, 40, 45, 59, 16, 23, 17, 0, 0,
+ 217, 122, 114, 193, 109, 101, 172, 96, 91, 149, 82, 77, 128, 69, 64, 106, 55, 50, 84, 39, 35, 59, 17, 14, 17, 0, 0,
+ 218, 122, 98, 194, 108, 87, 171, 96, 77, 149, 82, 65, 127, 69, 52, 106, 55, 40, 84, 40, 25, 59, 18, 3, 15, 0, 0,
+ 219, 122, 80, 193, 108, 70, 172, 95, 61, 149, 82, 51, 127, 69, 40, 106, 55, 28, 84, 39, 12, 58, 17, 0, 13, 0, 0,
+ 219, 121, 59, 194, 108, 52, 172, 96, 44, 149, 82, 35, 127, 68, 24, 106, 55, 11, 84, 40, 0, 57, 18, 0, 13, 0, 0,
+ 219, 121, 40, 193, 108, 33, 172, 95, 26, 149, 81, 19, 128, 68, 6, 106, 54, 0, 84, 39, 0, 57, 18, 0, 13, 0, 0,
+ 213, 99, 165, 189, 87, 148, 169, 76, 132, 147, 64, 115, 126, 52, 98, 106, 39, 81, 85, 23, 63, 60, 0, 39, 16, 0, 0,
+ 214, 98, 149, 191, 87, 133, 170, 76, 119, 148, 65, 103, 127, 53, 88, 106, 39, 72, 85, 24, 55, 60, 0, 32, 15, 0, 0,
+ 215, 99, 136, 191, 87, 121, 170, 77, 108, 148, 65, 93, 126, 53, 79, 106, 40, 64, 85, 24, 47, 59, 0, 25, 14, 0, 0,
+ 215, 99, 121, 192, 87, 108, 170, 77, 96, 148, 65, 82, 126, 53, 69, 106, 40, 55, 85, 25, 39, 59, 0, 18, 13, 0, 0,
+ 216, 99, 106, 191, 87, 95, 170, 76, 83, 148, 65, 71, 126, 53, 58, 106, 41, 45, 85, 26, 30, 59, 0, 8, 11, 0, 0,
+ 216, 98, 91, 192, 88, 82, 170, 77, 71, 148, 65, 60, 127, 53, 48, 105, 41, 36, 83, 26, 21, 58, 1, 2, 11, 0, 0,
+ 217, 99, 75, 192, 87, 66, 170, 76, 57, 148, 65, 47, 126, 53, 36, 105, 41, 24, 83, 26, 8, 57, 2, 0, 9, 0, 0,
+ 217, 98, 57, 192, 87, 49, 171, 77, 41, 147, 65, 32, 126, 53, 21, 105, 41, 8, 84, 27, 0, 57, 3, 0, 9, 0, 0,
+ 217, 98, 40, 193, 87, 34, 171, 76, 27, 148, 65, 19, 126, 53, 6, 105, 41, 0, 83, 27, 0, 57, 4, 0, 9, 0, 0,
+ 211, 67, 152, 189, 58, 136, 168, 50, 122, 147, 39, 105, 127, 28, 89, 106, 14, 74, 85, 0, 56, 59, 0, 33, 9, 0, 0,
+ 213, 68, 138, 190, 59, 123, 169, 51, 109, 148, 40, 95, 126, 30, 80, 106, 16, 65, 85, 0, 48, 59, 0, 27, 9, 0, 0,
+ 214, 69, 125, 190, 59, 111, 168, 51, 99, 148, 41, 86, 126, 31, 72, 106, 18, 58, 85, 0, 41, 59, 0, 20, 7, 0, 0,
+ 215, 70, 112, 190, 61, 100, 169, 52, 88, 147, 42, 76, 126, 32, 63, 106, 19, 49, 84, 1, 34, 58, 0, 13, 7, 0, 0,
+ 214, 70, 99, 190, 62, 88, 169, 53, 77, 147, 43, 65, 125, 32, 53, 106, 20, 40, 84, 3, 26, 58, 0, 4, 7, 0, 0,
+ 214, 71, 86, 190, 61, 75, 169, 53, 65, 146, 43, 54, 126, 33, 44, 105, 21, 31, 83, 4, 17, 57, 0, 0, 7, 0, 0,
+ 215, 71, 71, 191, 62, 62, 169, 53, 53, 147, 44, 44, 126, 34, 33, 105, 22, 20, 83, 5, 4, 57, 0, 0, 7, 0, 0,
+ 215, 71, 54, 191, 62, 47, 169, 54, 39, 147, 44, 30, 126, 35, 20, 105, 23, 6, 83, 6, 0, 56, 0, 0, 5, 0, 0,
+ 215, 71, 41, 191, 63, 34, 170, 54, 27, 147, 45, 17, 126, 35, 6, 105, 23, 0, 83, 8, 0, 56, 0, 0, 5, 0, 0,
+ 210, 13, 140, 189, 1, 125, 167, 0, 110, 146, 0, 96, 126, 0, 81, 106, 0, 67, 85, 0, 51, 59, 0, 28, 4, 0, 0,
+ 212, 18, 126, 190, 7, 113, 168, 0, 100, 146, 0, 86, 126, 0, 73, 106, 0, 59, 84, 0, 43, 59, 0, 22, 4, 0, 0,
+ 212, 21, 115, 190, 13, 103, 168, 3, 91, 146, 0, 78, 125, 0, 65, 105, 0, 52, 84, 0, 36, 58, 0, 16, 4, 0, 0,
+ 213, 24, 103, 189, 19, 91, 168, 9, 82, 146, 0, 69, 125, 0, 57, 105, 0, 44, 84, 0, 29, 58, 0, 7, 4, 0, 0,
+ 213, 27, 92, 188, 21, 81, 168, 14, 71, 146, 1, 59, 125, 0, 48, 105, 0, 36, 84, 0, 21, 58, 0, 4, 4, 0, 0,
+ 213, 30, 80, 189, 22, 69, 168, 17, 61, 146, 5, 50, 125, 0, 39, 104, 0, 27, 83, 0, 12, 57, 0, 0, 4, 0, 0,
+ 214, 30, 67, 189, 25, 57, 168, 20, 50, 146, 9, 40, 125, 0, 29, 104, 0, 17, 83, 0, 2, 56, 0, 0, 4, 0, 0,
+ 214, 32, 53, 189, 27, 44, 169, 20, 38, 146, 13, 28, 124, 2, 17, 104, 0, 4, 83, 0, 0, 56, 0, 0, 4, 0, 0,
+ 214, 33, 41, 190, 27, 33, 168, 23, 27, 146, 13, 18, 125, 3, 5, 105, 0, 0, 83, 0, 0, 56, 0, 0, 4, 0, 0,
+ 185, 229, 250, 164, 204, 223, 146, 182, 199, 127, 158, 174, 108, 136, 149, 89, 113, 125, 70, 90, 100, 46, 62, 71, 10, 25, 33,
+ 189, 227, 225, 168, 202, 201, 148, 181, 179, 129, 157, 156, 109, 135, 134, 90, 113, 113, 70, 89, 90, 46, 62, 62, 8, 24, 25,
+ 192, 226, 202, 170, 202, 182, 151, 179, 162, 130, 156, 141, 110, 133, 121, 91, 112, 101, 71, 89, 80, 46, 61, 54, 7, 24, 19,
+ 194, 224, 179, 173, 200, 160, 153, 178, 144, 132, 155, 125, 112, 133, 107, 92, 111, 89, 71, 88, 69, 46, 61, 45, 6, 23, 10,
+ 196, 223, 155, 174, 198, 139, 154, 176, 124, 132, 153, 107, 113, 131, 91, 92, 110, 75, 72, 87, 58, 47, 60, 37, 4, 23, 0,
+ 198, 221, 131, 175, 197, 117, 155, 175, 105, 133, 152, 91, 113, 130, 76, 92, 109, 63, 72, 86, 47, 46, 60, 26, 3, 23, 0,
+ 200, 220, 104, 176, 196, 94, 156, 175, 84, 134, 151, 72, 113, 129, 59, 93, 108, 47, 72, 85, 33, 46, 59, 13, 0, 23, 0,
+ 201, 219, 73, 179, 195, 65, 157, 173, 57, 135, 150, 48, 114, 129, 39, 94, 108, 28, 72, 85, 15, 47, 59, 0, 0, 22, 0,
+ 203, 219, 42, 178, 195, 37, 157, 173, 32, 135, 150, 26, 114, 128, 16, 94, 107, 6, 73, 85, 0, 46, 58, 0, 0, 22, 0,
+ 186, 205, 233, 165, 183, 209, 148, 163, 187, 128, 142, 163, 109, 121, 140, 91, 101, 118, 71, 80, 94, 48, 54, 66, 12, 15, 27,
+ 189, 204, 211, 169, 182, 189, 151, 163, 169, 131, 141, 147, 111, 121, 126, 92, 101, 105, 72, 79, 84, 48, 54, 58, 11, 15, 19,
+ 192, 202, 190, 171, 181, 170, 152, 161, 152, 131, 141, 133, 112, 120, 113, 93, 100, 94, 72, 79, 74, 48, 53, 50, 10, 15, 11,
+ 195, 201, 169, 172, 179, 151, 153, 160, 135, 132, 139, 117, 113, 119, 100, 93, 99, 82, 72, 78, 64, 48, 53, 41, 9, 14, 3,
+ 195, 200, 146, 174, 179, 131, 154, 159, 117, 133, 138, 101, 113, 118, 86, 93, 98, 70, 73, 77, 53, 48, 52, 32, 8, 15, 0,
+ 198, 199, 125, 175, 177, 111, 155, 158, 100, 133, 137, 85, 113, 117, 71, 93, 97, 57, 72, 77, 42, 47, 52, 22, 5, 14, 0,
+ 199, 198, 101, 176, 177, 89, 155, 157, 79, 134, 136, 68, 113, 116, 56, 94, 97, 44, 73, 76, 30, 47, 52, 10, 2, 15, 0,
+ 200, 197, 72, 178, 176, 63, 157, 156, 56, 135, 136, 46, 114, 116, 37, 94, 96, 26, 73, 76, 11, 47, 51, 0, 0, 14, 0,
+ 201, 197, 45, 177, 175, 38, 156, 155, 31, 135, 135, 25, 114, 115, 17, 94, 96, 5, 73, 75, 0, 46, 51, 0, 0, 14, 0,
+ 187, 183, 218, 167, 165, 197, 149, 147, 176, 129, 127, 153, 111, 109, 132, 92, 90, 111, 73, 70, 89, 49, 46, 62, 15, 4, 22,
+ 190, 183, 197, 170, 164, 177, 151, 146, 159, 130, 127, 139, 112, 109, 119, 93, 90, 99, 72, 70, 78, 49, 45, 53, 14, 4, 15,
+ 192, 182, 179, 171, 163, 161, 153, 145, 144, 132, 126, 125, 113, 108, 107, 93, 89, 88, 73, 70, 69, 49, 45, 45, 13, 5, 6,
+ 195, 181, 159, 172, 162, 142, 152, 145, 127, 132, 125, 111, 113, 107, 94, 93, 88, 77, 73, 69, 59, 48, 45, 37, 11, 5, 0,
+ 195, 180, 139, 173, 161, 124, 153, 143, 110, 133, 125, 96, 113, 106, 81, 94, 88, 66, 73, 68, 49, 49, 44, 28, 9, 6, 0,
+ 196, 179, 118, 174, 160, 106, 154, 142, 94, 133, 124, 81, 113, 105, 68, 94, 87, 54, 73, 68, 39, 48, 44, 18, 5, 5, 0,
+ 197, 178, 96, 176, 159, 86, 155, 141, 75, 134, 123, 64, 114, 105, 53, 94, 87, 40, 73, 68, 26, 48, 44, 5, 2, 6, 0,
+ 199, 178, 70, 176, 158, 62, 156, 141, 54, 134, 122, 44, 114, 104, 35, 94, 86, 23, 73, 67, 8, 47, 44, 0, 2, 6, 0,
+ 199, 177, 45, 178, 158, 40, 156, 140, 32, 135, 122, 26, 114, 104, 16, 94, 86, 4, 73, 67, 0, 47, 44, 0, 0, 7, 0,
+ 188, 161, 204, 168, 144, 183, 149, 129, 164, 130, 112, 144, 112, 95, 123, 93, 78, 103, 74, 60, 81, 50, 36, 56, 16, 0, 16,
+ 190, 160, 185, 170, 144, 165, 151, 128, 148, 132, 111, 130, 112, 95, 110, 93, 78, 92, 74, 59, 72, 50, 36, 48, 16, 0, 8,
+ 192, 160, 167, 171, 143, 150, 153, 128, 134, 132, 111, 117, 112, 94, 100, 94, 77, 82, 74, 59, 63, 50, 36, 40, 14, 0, 0,
+ 193, 159, 149, 172, 143, 134, 153, 127, 119, 133, 110, 103, 113, 94, 87, 93, 77, 72, 73, 59, 54, 50, 36, 32, 12, 0, 0,
+ 195, 159, 131, 173, 142, 117, 153, 127, 104, 132, 110, 90, 113, 93, 76, 93, 76, 61, 74, 59, 45, 49, 36, 23, 9, 0, 0,
+ 196, 158, 113, 174, 141, 101, 155, 126, 89, 133, 109, 76, 113, 93, 64, 94, 76, 51, 74, 58, 35, 49, 36, 14, 6, 0, 0,
+ 197, 157, 92, 174, 141, 80, 154, 125, 71, 134, 108, 60, 114, 92, 50, 94, 75, 37, 73, 58, 22, 48, 36, 1, 5, 0, 0,
+ 197, 157, 68, 175, 140, 59, 155, 124, 51, 134, 108, 41, 113, 91, 32, 94, 75, 21, 73, 57, 5, 48, 35, 0, 5, 0, 0,
+ 198, 156, 46, 176, 140, 40, 155, 124, 32, 134, 107, 24, 114, 91, 14, 94, 75, 2, 73, 57, 0, 48, 36, 0, 3, 0, 0,
+ 189, 140, 191, 168, 126, 172, 150, 112, 154, 131, 97, 134, 112, 82, 115, 94, 66, 96, 74, 49, 75, 51, 25, 50, 12, 0, 10,
+ 191, 139, 173, 170, 125, 154, 152, 111, 138, 132, 96, 121, 113, 81, 103, 94, 66, 85, 74, 48, 66, 50, 26, 42, 12, 0, 1,
+ 192, 139, 157, 171, 125, 140, 152, 111, 125, 132, 96, 109, 113, 81, 92, 94, 65, 76, 74, 48, 58, 50, 26, 35, 9, 0, 0,
+ 193, 139, 140, 172, 124, 125, 153, 110, 112, 133, 95, 96, 113, 80, 82, 94, 65, 66, 74, 49, 50, 50, 26, 28, 7, 0, 0,
+ 194, 138, 123, 172, 123, 109, 153, 110, 97, 133, 95, 84, 113, 80, 70, 94, 65, 56, 74, 48, 40, 50, 26, 20, 6, 0, 0,
+ 194, 138, 105, 173, 123, 94, 153, 109, 83, 133, 94, 70, 112, 79, 59, 94, 64, 46, 74, 48, 31, 50, 26, 9, 4, 0, 0,
+ 196, 138, 87, 174, 122, 77, 153, 109, 67, 133, 93, 56, 113, 79, 46, 94, 64, 34, 73, 48, 18, 49, 27, 0, 4, 0, 0,
+ 196, 137, 65, 174, 122, 57, 154, 108, 49, 133, 93, 39, 113, 79, 29, 94, 64, 18, 74, 48, 3, 49, 27, 0, 2, 0, 0,
+ 197, 137, 47, 175, 122, 40, 155, 108, 32, 133, 93, 23, 114, 79, 14, 94, 64, 1, 73, 48, 0, 48, 27, 0, 2, 0, 0,
+ 189, 119, 177, 168, 106, 159, 150, 94, 142, 131, 81, 124, 113, 67, 107, 94, 53, 89, 74, 37, 69, 51, 11, 45, 6, 0, 3,
+ 191, 119, 161, 170, 106, 144, 152, 94, 129, 132, 81, 112, 113, 67, 96, 94, 53, 79, 74, 37, 61, 51, 13, 38, 6, 0, 0,
+ 192, 119, 146, 170, 106, 131, 152, 94, 117, 132, 80, 101, 112, 67, 85, 94, 53, 70, 74, 37, 53, 50, 14, 31, 4, 0, 0,
+ 192, 119, 131, 171, 106, 117, 153, 94, 105, 132, 80, 89, 113, 67, 75, 94, 54, 61, 74, 38, 45, 51, 14, 23, 3, 0, 0,
+ 193, 118, 114, 171, 106, 102, 153, 93, 90, 132, 80, 78, 113, 67, 65, 94, 53, 52, 74, 37, 36, 50, 15, 16, 1, 0, 0,
+ 194, 118, 99, 172, 105, 89, 153, 93, 78, 132, 80, 66, 113, 67, 54, 94, 53, 42, 74, 38, 27, 50, 16, 5, 1, 0, 0,
+ 194, 118, 82, 173, 105, 72, 153, 93, 63, 132, 79, 53, 113, 67, 42, 94, 53, 30, 74, 38, 15, 49, 16, 0, 0, 0, 0,
+ 195, 117, 63, 173, 105, 55, 154, 93, 47, 133, 79, 37, 113, 66, 27, 94, 53, 15, 73, 38, 0, 48, 16, 0, 0, 0, 0,
+ 195, 117, 46, 173, 104, 39, 154, 92, 32, 133, 79, 22, 113, 66, 13, 94, 53, 0, 73, 38, 0, 48, 17, 0, 0, 0, 0,
+ 189, 96, 166, 168, 85, 147, 150, 74, 132, 131, 62, 115, 113, 51, 99, 94, 38, 82, 74, 21, 63, 51, 0, 40, 1, 0, 0,
+ 190, 96, 150, 170, 85, 133, 152, 75, 119, 132, 63, 104, 113, 51, 88, 94, 38, 72, 75, 22, 55, 51, 0, 33, 1, 0, 0,
+ 192, 96, 137, 170, 85, 121, 152, 74, 108, 132, 64, 94, 113, 52, 79, 94, 39, 64, 74, 23, 48, 50, 0, 26, 0, 0, 0,
+ 192, 96, 122, 171, 86, 109, 152, 75, 96, 132, 63, 83, 113, 52, 69, 94, 39, 56, 74, 24, 41, 50, 0, 19, 0, 0, 0,
+ 193, 96, 107, 171, 85, 96, 152, 75, 84, 132, 64, 72, 113, 52, 60, 94, 39, 47, 74, 24, 32, 50, 1, 10, 0, 0, 0,
+ 193, 96, 93, 172, 85, 82, 152, 75, 72, 133, 63, 61, 113, 51, 49, 94, 39, 37, 73, 25, 23, 49, 2, 2, 0, 0, 0,
+ 194, 96, 78, 172, 85, 68, 152, 75, 59, 132, 63, 49, 113, 52, 39, 94, 40, 26, 73, 25, 11, 48, 3, 0, 0, 0, 0,
+ 194, 96, 60, 173, 85, 52, 153, 75, 44, 132, 64, 35, 112, 52, 25, 94, 40, 12, 73, 26, 0, 48, 4, 0, 0, 0, 0,
+ 195, 96, 46, 173, 85, 38, 154, 74, 31, 133, 63, 22, 113, 52, 11, 93, 40, 0, 73, 26, 0, 47, 5, 0, 0, 0, 0,
+ 188, 67, 153, 168, 58, 137, 151, 49, 122, 131, 39, 106, 113, 28, 90, 94, 13, 75, 75, 0, 57, 51, 0, 35, 0, 0, 0,
+ 190, 68, 138, 170, 59, 123, 152, 50, 110, 132, 41, 96, 113, 29, 80, 94, 16, 66, 75, 0, 49, 50, 0, 27, 0, 0, 0,
+ 191, 69, 126, 170, 59, 112, 151, 52, 100, 132, 42, 86, 113, 30, 73, 95, 17, 58, 75, 0, 42, 50, 0, 21, 0, 0, 0,
+ 192, 70, 113, 170, 61, 100, 151, 52, 89, 132, 42, 77, 113, 31, 64, 94, 19, 50, 74, 1, 35, 50, 0, 14, 0, 0, 0,
+ 192, 70, 100, 170, 62, 89, 151, 53, 77, 131, 43, 66, 112, 32, 54, 94, 20, 42, 74, 2, 27, 49, 0, 5, 0, 0, 0,
+ 192, 71, 87, 171, 61, 77, 152, 53, 67, 131, 44, 57, 112, 33, 45, 94, 21, 33, 74, 4, 19, 49, 0, 1, 0, 0, 0,
+ 193, 71, 74, 171, 62, 64, 152, 53, 55, 132, 44, 45, 113, 34, 34, 94, 22, 23, 73, 5, 7, 48, 0, 0, 0, 0, 0,
+ 193, 70, 58, 172, 62, 50, 152, 54, 42, 132, 44, 32, 112, 35, 22, 93, 23, 10, 73, 6, 0, 47, 0, 0, 0, 0, 0,
+ 193, 70, 45, 172, 62, 38, 153, 54, 31, 132, 44, 21, 112, 35, 9, 94, 23, 0, 73, 7, 0, 47, 0, 0, 0, 0, 0,
+ 189, 26, 141, 169, 15, 126, 150, 2, 112, 131, 0, 97, 113, 0, 82, 94, 0, 67, 75, 0, 51, 50, 0, 29, 0, 0, 0,
+ 190, 28, 128, 170, 18, 114, 151, 8, 101, 132, 0, 88, 113, 0, 74, 94, 0, 60, 75, 0, 44, 50, 0, 23, 0, 0, 0,
+ 191, 30, 117, 170, 23, 104, 152, 11, 92, 132, 1, 79, 113, 0, 67, 95, 0, 53, 75, 0, 37, 50, 0, 17, 0, 0, 0,
+ 191, 33, 105, 170, 26, 93, 151, 18, 83, 132, 6, 70, 112, 0, 58, 94, 0, 45, 75, 0, 30, 49, 0, 8, 0, 0, 0,
+ 191, 34, 93, 170, 27, 82, 151, 20, 72, 131, 8, 61, 112, 0, 49, 94, 0, 38, 74, 0, 23, 49, 0, 4, 0, 0, 0,
+ 191, 36, 82, 170, 29, 71, 151, 22, 63, 131, 11, 52, 112, 0, 41, 93, 0, 29, 74, 0, 14, 48, 0, 1, 0, 0, 0,
+ 191, 38, 69, 170, 31, 60, 151, 24, 51, 131, 14, 41, 112, 1, 31, 93, 0, 19, 73, 0, 3, 48, 0, 0, 0, 0, 0,
+ 192, 37, 56, 171, 31, 47, 152, 25, 40, 131, 17, 30, 112, 4, 19, 93, 0, 7, 73, 0, 0, 47, 0, 0, 0, 0, 0,
+ 192, 38, 45, 171, 33, 36, 152, 26, 30, 131, 18, 21, 111, 7, 9, 93, 0, 0, 73, 0, 0, 47, 0, 0, 0, 0, 0,
+ 149, 218, 248, 133, 194, 222, 119, 173, 198, 102, 151, 173, 86, 130, 148, 70, 108, 125, 53, 85, 100, 32, 59, 71, 0, 22, 33,
+ 154, 216, 223, 137, 193, 200, 122, 172, 178, 106, 150, 156, 89, 128, 133, 73, 107, 112, 54, 85, 89, 31, 59, 63, 0, 22, 26,
+ 159, 215, 202, 141, 192, 181, 126, 171, 161, 108, 149, 141, 90, 128, 121, 74, 107, 100, 55, 85, 80, 32, 58, 55, 0, 22, 19,
+ 161, 213, 179, 144, 190, 160, 126, 170, 143, 109, 148, 125, 92, 127, 107, 74, 106, 89, 56, 84, 69, 32, 58, 46, 0, 21, 11,
+ 163, 211, 156, 144, 189, 139, 129, 168, 125, 110, 147, 108, 93, 126, 92, 75, 105, 76, 57, 83, 58, 33, 58, 37, 0, 21, 1,
+ 167, 211, 133, 147, 188, 120, 130, 167, 105, 110, 145, 92, 93, 125, 78, 76, 104, 64, 58, 83, 48, 33, 57, 27, 0, 21, 0,
+ 169, 210, 108, 149, 187, 96, 131, 166, 86, 112, 144, 74, 94, 124, 62, 77, 103, 49, 58, 82, 35, 33, 57, 15, 0, 21, 0,
+ 170, 209, 80, 151, 186, 71, 133, 165, 62, 114, 143, 52, 95, 123, 42, 77, 103, 32, 58, 81, 18, 33, 56, 0, 0, 21, 0,
+ 173, 208, 55, 152, 186, 49, 134, 165, 41, 114, 143, 34, 95, 122, 25, 77, 102, 14, 58, 81, 0, 33, 56, 0, 0, 21, 0,
+ 154, 195, 232, 137, 174, 207, 122, 156, 185, 105, 136, 163, 89, 116, 140, 73, 97, 117, 56, 76, 94, 35, 51, 66, 0, 13, 28,
+ 158, 194, 209, 141, 174, 187, 125, 155, 167, 109, 135, 146, 91, 116, 125, 75, 96, 105, 57, 75, 83, 35, 50, 57, 0, 12, 21,
+ 161, 193, 189, 144, 173, 169, 128, 154, 151, 110, 134, 132, 93, 115, 113, 77, 95, 94, 58, 75, 74, 35, 50, 50, 0, 12, 13,
+ 164, 192, 168, 145, 171, 151, 129, 153, 134, 111, 133, 117, 94, 114, 100, 76, 95, 82, 58, 75, 64, 36, 50, 42, 0, 12, 5,
+ 165, 191, 147, 147, 170, 131, 130, 152, 117, 113, 132, 102, 95, 113, 86, 77, 94, 71, 58, 74, 54, 35, 50, 33, 0, 13, 0,
+ 167, 189, 126, 148, 169, 113, 132, 151, 100, 113, 131, 86, 96, 112, 73, 77, 93, 59, 59, 73, 43, 35, 49, 23, 0, 12, 0,
+ 170, 189, 104, 150, 168, 91, 133, 150, 81, 114, 130, 69, 96, 111, 57, 78, 92, 46, 59, 73, 31, 35, 49, 11, 0, 13, 0,
+ 171, 188, 78, 152, 168, 68, 134, 149, 60, 115, 130, 50, 96, 111, 41, 78, 92, 29, 60, 73, 15, 35, 49, 0, 0, 12, 0,
+ 173, 187, 55, 153, 167, 47, 134, 149, 39, 115, 129, 33, 97, 110, 24, 79, 92, 13, 60, 72, 0, 35, 48, 0, 0, 12, 0,
+ 157, 175, 217, 139, 157, 196, 125, 141, 175, 109, 122, 153, 92, 104, 132, 76, 86, 110, 59, 67, 88, 37, 43, 61, 1, 1, 23,
+ 161, 174, 196, 144, 156, 176, 127, 140, 158, 110, 121, 137, 94, 104, 118, 77, 85, 98, 59, 67, 78, 37, 43, 53, 0, 2, 16,
+ 163, 174, 178, 146, 156, 160, 130, 139, 143, 112, 121, 124, 95, 103, 106, 78, 85, 88, 60, 66, 69, 37, 42, 46, 0, 2, 7,
+ 166, 173, 159, 147, 154, 142, 130, 138, 127, 113, 120, 111, 96, 103, 95, 78, 84, 77, 60, 66, 59, 37, 43, 37, 0, 2, 0,
+ 166, 172, 139, 148, 154, 125, 131, 137, 112, 113, 120, 96, 96, 102, 81, 78, 84, 66, 60, 65, 50, 37, 42, 29, 0, 3, 0,
+ 167, 171, 120, 149, 153, 107, 133, 137, 95, 114, 118, 81, 97, 101, 69, 79, 84, 56, 60, 65, 40, 37, 42, 19, 0, 3, 0,
+ 170, 170, 99, 151, 152, 87, 134, 136, 77, 115, 118, 66, 97, 101, 55, 79, 83, 42, 61, 65, 28, 37, 42, 7, 0, 3, 0,
+ 172, 170, 75, 152, 151, 65, 134, 135, 57, 115, 117, 48, 97, 100, 38, 79, 83, 27, 61, 64, 12, 36, 42, 0, 0, 3, 0,
+ 172, 169, 55, 154, 151, 46, 135, 134, 40, 116, 116, 32, 97, 99, 21, 80, 82, 10, 61, 64, 0, 36, 41, 0, 0, 3, 0,
+ 160, 154, 203, 143, 139, 182, 127, 124, 164, 111, 107, 143, 95, 91, 122, 78, 75, 103, 60, 57, 81, 39, 33, 56, 1, 0, 18,
+ 163, 154, 184, 146, 138, 165, 130, 123, 148, 113, 107, 129, 96, 90, 110, 79, 74, 92, 61, 56, 72, 39, 34, 48, 2, 0, 9,
+ 165, 154, 167, 147, 137, 149, 131, 122, 134, 114, 106, 117, 96, 90, 100, 79, 74, 82, 61, 56, 64, 39, 33, 40, 2, 0, 1,
+ 166, 153, 150, 149, 137, 133, 132, 122, 119, 114, 106, 104, 97, 90, 88, 79, 74, 72, 61, 56, 55, 39, 34, 33, 0, 0, 0,
+ 168, 152, 132, 149, 136, 117, 132, 121, 104, 114, 105, 90, 97, 89, 76, 79, 73, 62, 61, 56, 46, 38, 34, 25, 0, 0, 0,
+ 169, 151, 114, 150, 135, 101, 133, 121, 90, 114, 104, 77, 97, 89, 65, 80, 73, 51, 61, 56, 36, 38, 34, 16, 0, 0, 0,
+ 170, 150, 94, 151, 135, 83, 134, 120, 73, 115, 104, 62, 98, 88, 51, 80, 72, 39, 61, 56, 24, 38, 34, 3, 0, 0, 0,
+ 172, 150, 72, 153, 134, 63, 135, 119, 55, 115, 103, 45, 98, 88, 36, 80, 72, 24, 61, 55, 9, 38, 34, 0, 0, 0, 0,
+ 172, 150, 54, 153, 134, 47, 135, 119, 38, 116, 103, 30, 98, 87, 21, 80, 72, 8, 62, 55, 0, 37, 34, 0, 0, 0, 0,
+ 162, 134, 190, 145, 120, 171, 129, 108, 153, 113, 93, 134, 97, 78, 115, 80, 63, 96, 62, 46, 75, 41, 23, 51, 0, 0, 11,
+ 165, 134, 173, 147, 120, 154, 131, 107, 138, 114, 92, 120, 97, 78, 103, 80, 63, 85, 62, 46, 66, 40, 23, 43, 0, 0, 2,
+ 166, 134, 157, 148, 120, 140, 132, 106, 125, 114, 92, 109, 97, 77, 93, 81, 63, 77, 62, 46, 58, 40, 24, 36, 0, 0, 0,
+ 168, 133, 140, 149, 119, 125, 132, 106, 112, 115, 92, 97, 98, 77, 82, 81, 62, 67, 62, 46, 50, 40, 24, 29, 0, 0, 0,
+ 168, 133, 123, 150, 119, 110, 133, 106, 97, 115, 91, 84, 98, 77, 70, 81, 62, 57, 62, 46, 41, 40, 24, 20, 0, 0, 0,
+ 169, 132, 107, 150, 118, 94, 133, 105, 84, 115, 91, 72, 98, 76, 60, 80, 62, 47, 62, 46, 32, 39, 25, 11, 0, 0, 0,
+ 171, 132, 89, 152, 118, 79, 135, 105, 69, 115, 90, 58, 98, 76, 47, 80, 62, 36, 62, 46, 21, 39, 25, 0, 0, 0, 0,
+ 171, 132, 69, 153, 117, 60, 135, 104, 52, 116, 90, 42, 98, 76, 33, 81, 61, 21, 62, 46, 6, 38, 25, 0, 0, 0, 0,
+ 172, 132, 54, 153, 118, 45, 135, 104, 38, 116, 90, 28, 98, 76, 18, 81, 61, 6, 62, 46, 0, 38, 25, 0, 0, 0, 0,
+ 164, 115, 177, 146, 103, 159, 130, 91, 143, 114, 78, 125, 97, 65, 107, 81, 51, 89, 63, 34, 69, 41, 9, 46, 0, 0, 4,
+ 166, 115, 161, 148, 103, 144, 132, 91, 129, 115, 78, 112, 98, 65, 96, 81, 51, 79, 63, 35, 61, 41, 11, 38, 0, 0, 0,
+ 167, 115, 146, 150, 102, 131, 132, 91, 117, 115, 78, 101, 98, 65, 86, 81, 51, 71, 63, 35, 54, 41, 12, 32, 0, 0, 0,
+ 168, 114, 132, 150, 103, 118, 133, 91, 105, 116, 78, 91, 98, 64, 76, 82, 51, 61, 63, 36, 46, 41, 13, 24, 0, 0, 0,
+ 169, 114, 116, 150, 102, 103, 134, 90, 91, 116, 78, 79, 98, 65, 66, 81, 51, 53, 63, 36, 37, 40, 14, 17, 0, 0, 0,
+ 169, 114, 101, 151, 101, 89, 134, 90, 79, 116, 77, 67, 98, 64, 56, 81, 51, 44, 63, 36, 29, 40, 15, 7, 0, 0, 0,
+ 170, 114, 85, 152, 101, 75, 135, 90, 65, 116, 77, 54, 98, 64, 44, 81, 51, 32, 63, 36, 17, 39, 15, 0, 0, 0, 0,
+ 172, 113, 66, 152, 101, 58, 135, 89, 49, 116, 77, 40, 99, 64, 30, 81, 51, 18, 62, 36, 3, 38, 16, 0, 0, 0, 0,
+ 171, 113, 51, 153, 101, 44, 136, 89, 36, 116, 77, 28, 99, 64, 18, 81, 51, 5, 62, 36, 0, 38, 16, 0, 0, 0, 0,
+ 165, 94, 166, 147, 82, 147, 132, 72, 132, 115, 61, 115, 98, 49, 99, 82, 36, 82, 64, 19, 64, 42, 0, 41, 0, 0, 0,
+ 167, 93, 150, 150, 83, 134, 133, 73, 120, 116, 62, 104, 99, 49, 88, 82, 36, 72, 64, 20, 55, 41, 0, 33, 0, 0, 0,
+ 169, 93, 137, 150, 83, 122, 134, 73, 109, 116, 61, 94, 99, 50, 80, 82, 37, 65, 64, 21, 49, 41, 0, 27, 0, 0, 0,
+ 169, 94, 123, 150, 83, 110, 133, 73, 97, 116, 61, 83, 99, 50, 70, 82, 38, 57, 63, 23, 42, 41, 0, 20, 0, 0, 0,
+ 169, 94, 109, 150, 84, 97, 134, 73, 85, 116, 62, 73, 99, 51, 61, 81, 38, 48, 63, 23, 33, 41, 1, 11, 0, 0, 0,
+ 170, 94, 96, 150, 83, 84, 134, 73, 74, 116, 61, 62, 99, 50, 51, 82, 38, 39, 64, 23, 24, 40, 3, 4, 0, 0, 0,
+ 171, 93, 79, 152, 82, 70, 135, 73, 61, 116, 62, 51, 98, 51, 40, 81, 38, 28, 63, 24, 14, 39, 4, 0, 0, 0, 0,
+ 171, 94, 64, 152, 83, 55, 135, 73, 47, 116, 62, 37, 98, 50, 27, 81, 38, 15, 63, 24, 1, 39, 4, 0, 0, 0, 0,
+ 172, 93, 51, 153, 82, 42, 135, 73, 35, 117, 62, 26, 99, 51, 16, 81, 39, 3, 63, 25, 0, 38, 5, 0, 0, 0, 0,
+ 166, 68, 153, 148, 59, 137, 133, 49, 121, 115, 39, 106, 99, 28, 91, 82, 13, 75, 65, 0, 58, 42, 0, 36, 0, 0, 0,
+ 168, 68, 139, 150, 59, 124, 134, 50, 110, 116, 40, 96, 99, 30, 81, 82, 16, 66, 64, 0, 50, 41, 0, 29, 0, 0, 0,
+ 169, 69, 126, 150, 59, 113, 134, 51, 101, 117, 42, 87, 100, 30, 73, 82, 17, 59, 65, 0, 43, 41, 0, 23, 0, 0, 0,
+ 169, 70, 115, 150, 61, 102, 134, 52, 89, 116, 42, 77, 99, 32, 65, 82, 19, 52, 64, 0, 36, 41, 0, 15, 0, 0, 0,
+ 169, 70, 101, 150, 61, 90, 134, 52, 79, 116, 43, 68, 99, 32, 55, 82, 21, 43, 64, 2, 28, 41, 0, 6, 0, 0, 0,
+ 170, 70, 89, 151, 62, 79, 134, 53, 69, 116, 44, 58, 99, 33, 46, 81, 21, 34, 64, 3, 20, 41, 0, 2, 0, 0, 0,
+ 170, 71, 76, 152, 62, 66, 134, 53, 57, 116, 43, 46, 99, 33, 36, 82, 22, 24, 64, 5, 10, 40, 0, 0, 0, 0, 0,
+ 171, 70, 61, 152, 62, 52, 135, 53, 44, 116, 44, 35, 99, 34, 24, 82, 22, 12, 63, 6, 0, 39, 0, 0, 0, 0, 0,
+ 171, 71, 49, 153, 62, 41, 135, 54, 33, 117, 45, 25, 98, 34, 13, 81, 23, 0, 63, 7, 0, 39, 0, 0, 0, 0, 0,
+ 167, 33, 142, 149, 24, 127, 134, 10, 113, 116, 0, 97, 100, 0, 83, 83, 0, 68, 65, 0, 52, 40, 0, 30, 0, 0, 0,
+ 169, 33, 129, 150, 26, 115, 134, 17, 102, 116, 3, 89, 100, 0, 75, 83, 0, 60, 65, 0, 45, 40, 0, 24, 0, 0, 0,
+ 169, 36, 118, 151, 27, 104, 134, 19, 93, 116, 7, 80, 100, 0, 67, 83, 0, 54, 65, 0, 38, 41, 0, 17, 0, 0, 0,
+ 169, 39, 107, 150, 30, 94, 134, 22, 84, 116, 11, 71, 99, 0, 59, 83, 0, 46, 64, 0, 31, 40, 0, 9, 0, 0, 0,
+ 169, 39, 95, 151, 31, 83, 134, 24, 73, 116, 15, 62, 100, 1, 51, 83, 0, 38, 64, 0, 24, 40, 0, 5, 0, 0, 0,
+ 169, 41, 83, 151, 33, 73, 134, 26, 64, 117, 17, 54, 99, 4, 42, 82, 0, 30, 64, 0, 16, 40, 0, 1, 0, 0, 0,
+ 170, 42, 71, 152, 34, 62, 134, 28, 53, 117, 19, 44, 99, 6, 33, 82, 0, 21, 63, 0, 4, 39, 0, 0, 0, 0, 0,
+ 171, 42, 59, 152, 35, 50, 134, 29, 42, 117, 21, 32, 99, 9, 22, 82, 0, 9, 63, 0, 0, 38, 0, 0, 0, 0, 0,
+ 172, 42, 48, 152, 36, 40, 135, 29, 32, 117, 21, 23, 99, 10, 12, 82, 0, 0, 63, 0, 0, 38, 0, 0, 0, 0, 0,
+ 107, 207, 246, 96, 185, 220, 86, 165, 196, 73, 144, 171, 60, 123, 147, 46, 103, 125, 32, 82, 100, 9, 56, 71, 0, 20, 33,
+ 115, 206, 221, 104, 184, 198, 92, 164, 178, 78, 143, 154, 64, 123, 133, 51, 102, 111, 34, 81, 89, 10, 56, 63, 0, 20, 27,
+ 122, 204, 200, 108, 183, 180, 95, 163, 161, 82, 142, 140, 68, 122, 120, 54, 102, 101, 36, 81, 79, 11, 56, 55, 0, 20, 20,
+ 125, 203, 179, 111, 181, 160, 97, 162, 143, 85, 141, 124, 70, 121, 107, 55, 101, 89, 38, 80, 69, 14, 55, 46, 0, 19, 10,
+ 128, 202, 156, 113, 180, 140, 102, 161, 125, 87, 140, 108, 71, 120, 92, 56, 100, 76, 39, 79, 59, 14, 55, 38, 0, 20, 3,
+ 132, 200, 135, 117, 179, 121, 103, 159, 106, 88, 139, 93, 73, 119, 79, 57, 100, 65, 41, 79, 49, 15, 54, 28, 0, 19, 0,
+ 134, 200, 111, 119, 178, 98, 105, 158, 87, 89, 138, 76, 74, 118, 64, 58, 99, 51, 41, 78, 37, 16, 54, 17, 0, 19, 0,
+ 137, 199, 85, 122, 177, 75, 108, 158, 66, 91, 137, 56, 75, 118, 46, 59, 98, 35, 42, 78, 22, 16, 54, 3, 0, 19, 0,
+ 140, 198, 62, 125, 177, 55, 109, 158, 47, 92, 137, 40, 76, 117, 32, 59, 98, 21, 42, 78, 6, 16, 54, 0, 0, 18, 0,
+ 118, 186, 231, 106, 167, 206, 93, 149, 184, 81, 130, 161, 67, 111, 139, 54, 92, 117, 39, 72, 93, 17, 48, 66, 0, 10, 29,
+ 123, 185, 207, 110, 166, 186, 98, 148, 167, 85, 129, 145, 71, 111, 125, 56, 92, 104, 40, 72, 83, 18, 48, 57, 0, 10, 22,
+ 128, 184, 188, 113, 165, 168, 102, 147, 151, 88, 128, 131, 73, 110, 113, 58, 91, 94, 42, 71, 74, 19, 48, 50, 0, 9, 15,
+ 131, 183, 168, 116, 164, 151, 104, 146, 134, 89, 127, 117, 73, 109, 100, 58, 90, 83, 42, 71, 65, 20, 48, 42, 0, 9, 5,
+ 134, 182, 148, 120, 163, 131, 105, 145, 118, 90, 126, 102, 75, 108, 86, 59, 90, 72, 43, 71, 55, 19, 47, 34, 0, 9, 0,
+ 136, 181, 128, 122, 162, 115, 107, 144, 102, 92, 125, 87, 76, 107, 74, 61, 89, 60, 44, 70, 45, 20, 47, 24, 0, 8, 0,
+ 139, 180, 106, 124, 161, 95, 109, 144, 83, 93, 124, 71, 77, 107, 60, 61, 89, 47, 44, 70, 33, 20, 47, 13, 0, 8, 0,
+ 142, 179, 82, 125, 160, 72, 111, 143, 63, 94, 124, 54, 77, 106, 44, 61, 88, 32, 44, 69, 18, 20, 46, 0, 0, 8, 0,
+ 143, 179, 62, 127, 160, 54, 111, 142, 47, 94, 124, 39, 78, 106, 29, 62, 88, 18, 45, 69, 3, 20, 46, 0, 0, 8, 0,
+ 124, 167, 216, 112, 150, 194, 99, 134, 174, 87, 117, 153, 73, 100, 131, 58, 82, 110, 43, 64, 88, 23, 40, 61, 0, 0, 24,
+ 129, 166, 195, 116, 150, 175, 103, 134, 158, 89, 116, 137, 75, 99, 118, 60, 82, 98, 44, 63, 78, 23, 40, 53, 0, 0, 17,
+ 132, 166, 177, 119, 149, 160, 106, 133, 143, 90, 115, 124, 76, 99, 107, 61, 81, 88, 45, 63, 69, 24, 40, 46, 0, 0, 9,
+ 136, 166, 159, 121, 148, 143, 107, 132, 126, 92, 115, 111, 77, 98, 94, 62, 81, 78, 46, 63, 60, 23, 40, 38, 0, 0, 0,
+ 138, 164, 140, 122, 147, 125, 108, 131, 111, 93, 114, 97, 79, 98, 82, 63, 80, 67, 46, 62, 50, 24, 40, 29, 0, 0, 0,
+ 139, 163, 122, 124, 146, 109, 110, 131, 96, 94, 114, 83, 79, 97, 70, 63, 81, 57, 46, 62, 41, 24, 40, 21, 0, 0, 0,
+ 141, 163, 101, 126, 145, 90, 111, 130, 79, 95, 113, 68, 79, 96, 56, 63, 80, 44, 47, 62, 30, 23, 40, 10, 0, 0, 0,
+ 144, 162, 79, 127, 145, 70, 112, 129, 60, 95, 112, 51, 79, 96, 41, 64, 79, 30, 47, 61, 15, 23, 40, 0, 0, 0, 0,
+ 145, 162, 60, 129, 145, 52, 113, 129, 46, 96, 112, 37, 79, 95, 27, 64, 79, 16, 47, 61, 1, 23, 39, 0, 0, 0, 0,
+ 131, 147, 202, 117, 133, 181, 105, 119, 162, 91, 103, 142, 77, 87, 122, 62, 71, 102, 47, 54, 81, 26, 31, 56, 0, 0, 18,
+ 135, 147, 183, 120, 132, 164, 107, 118, 147, 93, 102, 128, 78, 87, 110, 63, 71, 92, 47, 54, 72, 26, 31, 48, 0, 0, 10,
+ 138, 147, 166, 123, 131, 149, 108, 118, 133, 94, 102, 116, 79, 86, 100, 64, 71, 82, 48, 54, 64, 27, 31, 41, 0, 0, 2,
+ 139, 146, 149, 124, 131, 134, 111, 117, 119, 94, 101, 103, 79, 86, 88, 64, 70, 72, 48, 53, 55, 27, 31, 33, 0, 0, 0,
+ 141, 146, 132, 125, 131, 117, 111, 117, 104, 95, 101, 91, 80, 86, 77, 65, 70, 62, 48, 53, 46, 26, 31, 25, 0, 0, 0,
+ 143, 145, 115, 126, 130, 101, 112, 116, 90, 96, 100, 78, 80, 85, 65, 65, 70, 52, 49, 53, 37, 27, 32, 17, 0, 0, 0,
+ 144, 144, 96, 128, 129, 85, 112, 115, 75, 97, 100, 64, 81, 85, 52, 65, 69, 40, 49, 53, 26, 26, 31, 5, 0, 0, 0,
+ 146, 144, 76, 129, 129, 67, 114, 115, 58, 97, 99, 48, 82, 84, 38, 66, 69, 27, 49, 53, 12, 26, 32, 0, 0, 0, 0,
+ 146, 144, 59, 130, 128, 51, 114, 114, 43, 98, 99, 35, 82, 84, 25, 66, 69, 13, 49, 53, 0, 26, 32, 0, 0, 0, 0,
+ 135, 129, 189, 122, 115, 170, 107, 103, 152, 94, 89, 133, 79, 74, 114, 64, 60, 95, 49, 43, 75, 29, 20, 51, 0, 0, 12,
+ 138, 129, 171, 124, 115, 153, 110, 103, 138, 95, 89, 120, 81, 74, 103, 66, 60, 86, 50, 44, 67, 28, 21, 43, 0, 0, 3,
+ 140, 129, 156, 125, 115, 140, 111, 103, 125, 96, 89, 109, 81, 74, 93, 67, 60, 76, 50, 44, 59, 29, 22, 36, 0, 0, 0,
+ 142, 128, 140, 127, 115, 125, 112, 102, 112, 97, 88, 97, 82, 74, 83, 67, 60, 67, 50, 44, 51, 29, 22, 29, 0, 0, 0,
+ 142, 128, 124, 127, 114, 111, 113, 102, 98, 98, 88, 85, 82, 74, 71, 66, 60, 58, 50, 44, 42, 29, 22, 21, 0, 0, 0,
+ 144, 127, 108, 128, 114, 96, 113, 101, 85, 98, 87, 73, 82, 74, 61, 67, 60, 48, 50, 44, 33, 28, 23, 12, 0, 0, 0,
+ 145, 127, 91, 129, 114, 81, 115, 101, 71, 98, 87, 60, 82, 73, 48, 67, 59, 37, 50, 44, 22, 29, 23, 1, 0, 0, 0,
+ 147, 127, 73, 130, 113, 63, 115, 101, 55, 98, 87, 45, 83, 73, 35, 67, 59, 24, 50, 44, 10, 28, 24, 0, 0, 0, 0,
+ 147, 127, 58, 131, 113, 49, 115, 100, 42, 99, 86, 33, 83, 73, 23, 67, 59, 10, 50, 44, 0, 27, 24, 0, 0, 0, 0,
+ 138, 110, 177, 124, 99, 159, 110, 88, 142, 96, 75, 125, 82, 62, 107, 66, 48, 89, 51, 33, 70, 30, 8, 46, 0, 0, 5,
+ 142, 111, 160, 127, 99, 144, 113, 88, 130, 98, 75, 112, 82, 62, 96, 68, 49, 80, 51, 33, 61, 30, 10, 39, 0, 0, 0,
+ 143, 111, 146, 128, 99, 131, 114, 88, 118, 98, 75, 101, 83, 62, 86, 68, 49, 71, 52, 33, 54, 30, 11, 32, 0, 0, 0,
+ 144, 111, 132, 128, 99, 118, 113, 88, 106, 99, 75, 91, 83, 62, 77, 68, 49, 62, 52, 34, 46, 30, 12, 25, 0, 0, 0,
+ 144, 111, 117, 129, 98, 104, 114, 87, 92, 99, 75, 80, 83, 62, 67, 68, 49, 53, 51, 34, 38, 30, 13, 18, 0, 0, 0,
+ 145, 111, 103, 130, 98, 91, 114, 87, 80, 99, 75, 68, 83, 63, 57, 68, 50, 45, 51, 34, 30, 30, 14, 8, 0, 0, 0,
+ 146, 110, 87, 131, 98, 76, 115, 87, 67, 99, 75, 56, 83, 62, 45, 68, 49, 33, 52, 35, 19, 30, 15, 2, 0, 0, 0,
+ 148, 110, 70, 131, 98, 60, 116, 86, 52, 99, 74, 43, 84, 62, 33, 69, 49, 21, 52, 35, 6, 29, 15, 0, 0, 0, 0,
+ 148, 110, 56, 132, 97, 48, 117, 87, 40, 100, 75, 31, 84, 62, 22, 68, 49, 9, 51, 35, 0, 28, 15, 0, 0, 0, 0,
+ 142, 91, 166, 126, 80, 148, 113, 71, 132, 98, 59, 115, 83, 47, 99, 69, 34, 82, 53, 17, 64, 32, 0, 41, 0, 0, 0,
+ 143, 91, 150, 128, 81, 135, 114, 71, 120, 99, 60, 104, 85, 48, 89, 69, 35, 73, 53, 19, 56, 32, 0, 34, 0, 0, 0,
+ 145, 91, 137, 129, 81, 122, 115, 71, 109, 100, 60, 94, 85, 48, 81, 69, 35, 65, 53, 19, 49, 32, 0, 28, 0, 0, 0,
+ 146, 92, 124, 130, 81, 110, 115, 71, 98, 100, 60, 84, 85, 49, 71, 69, 36, 57, 53, 21, 42, 32, 0, 21, 0, 0, 0,
+ 147, 91, 110, 130, 81, 97, 115, 71, 86, 100, 60, 74, 84, 49, 62, 69, 36, 48, 53, 22, 34, 32, 0, 13, 0, 0, 0,
+ 147, 92, 97, 130, 81, 85, 116, 72, 76, 100, 60, 63, 85, 49, 52, 69, 37, 40, 53, 22, 26, 31, 1, 5, 0, 0, 0,
+ 148, 92, 82, 131, 81, 71, 116, 71, 62, 100, 60, 53, 84, 49, 42, 69, 37, 30, 52, 23, 16, 31, 2, 0, 0, 0, 0,
+ 148, 91, 67, 132, 81, 57, 117, 71, 49, 100, 60, 39, 84, 49, 30, 69, 37, 18, 52, 23, 2, 30, 2, 0, 0, 0, 0,
+ 149, 91, 54, 132, 81, 46, 118, 71, 39, 101, 60, 29, 85, 49, 19, 69, 37, 6, 52, 23, 0, 29, 3, 0, 0, 0, 0,
+ 143, 68, 153, 128, 59, 137, 115, 49, 122, 99, 39, 107, 85, 28, 91, 70, 13, 75, 54, 0, 58, 32, 0, 36, 0, 0, 0,
+ 146, 68, 140, 131, 59, 125, 116, 51, 111, 100, 40, 97, 85, 29, 82, 70, 15, 67, 54, 0, 50, 32, 0, 29, 0, 0, 0,
+ 147, 68, 127, 131, 59, 114, 117, 51, 102, 101, 41, 88, 86, 30, 74, 70, 17, 60, 54, 0, 44, 32, 0, 23, 0, 0, 0,
+ 147, 70, 115, 131, 60, 103, 116, 52, 91, 100, 42, 78, 85, 32, 65, 70, 19, 53, 54, 1, 38, 32, 0, 17, 0, 0, 0,
+ 147, 70, 103, 131, 61, 91, 117, 53, 81, 101, 43, 69, 86, 32, 57, 70, 20, 44, 54, 2, 30, 32, 0, 7, 0, 0, 0,
+ 148, 70, 91, 132, 61, 80, 117, 52, 70, 101, 43, 59, 85, 33, 48, 70, 21, 36, 53, 4, 22, 32, 0, 3, 0, 0, 0,
+ 148, 70, 78, 132, 62, 68, 117, 53, 58, 101, 43, 48, 85, 34, 38, 70, 22, 26, 53, 6, 12, 31, 0, 0, 0, 0, 0,
+ 149, 71, 64, 132, 62, 54, 118, 54, 46, 101, 44, 37, 85, 34, 27, 69, 23, 15, 53, 7, 1, 30, 0, 0, 0, 0, 0,
+ 150, 70, 53, 134, 61, 44, 118, 54, 36, 101, 44, 28, 85, 35, 17, 69, 23, 4, 52, 8, 0, 30, 0, 0, 0, 0, 0,
+ 145, 38, 143, 130, 29, 128, 117, 18, 114, 101, 3, 98, 87, 0, 84, 72, 0, 69, 54, 0, 53, 30, 0, 31, 0, 0, 0,
+ 147, 38, 130, 132, 30, 116, 117, 22, 103, 101, 8, 89, 87, 0, 76, 72, 0, 62, 54, 0, 46, 30, 0, 24, 0, 0, 0,
+ 148, 40, 119, 132, 31, 105, 117, 23, 94, 101, 13, 81, 87, 0, 68, 71, 0, 55, 54, 0, 39, 30, 0, 18, 0, 0, 0,
+ 148, 42, 108, 132, 34, 96, 117, 25, 85, 102, 15, 73, 86, 2, 60, 71, 0, 47, 54, 0, 33, 30, 0, 11, 0, 0, 0,
+ 148, 43, 96, 133, 35, 85, 117, 28, 75, 102, 18, 64, 87, 5, 52, 71, 0, 40, 54, 0, 25, 30, 0, 5, 0, 0, 0,
+ 149, 44, 85, 132, 36, 75, 118, 29, 66, 101, 20, 55, 86, 8, 44, 70, 0, 32, 53, 0, 18, 29, 0, 2, 0, 0, 0,
+ 149, 45, 74, 133, 37, 64, 118, 31, 55, 102, 21, 45, 85, 10, 34, 70, 0, 22, 53, 0, 6, 28, 0, 0, 0, 0, 0,
+ 150, 46, 61, 133, 39, 52, 118, 31, 44, 102, 23, 34, 85, 12, 24, 70, 0, 12, 52, 0, 0, 28, 0, 0, 0, 0, 0,
+ 150, 46, 51, 133, 40, 42, 119, 32, 35, 102, 24, 25, 85, 13, 14, 70, 0, 1, 52, 0, 0, 27, 0, 0, 0, 0, 0,
+ 53, 198, 244, 49, 177, 218, 41, 158, 195, 32, 138, 171, 22, 118, 147, 11, 98, 124, 0, 78, 100, 0, 54, 71, 0, 18, 34,
+ 69, 196, 220, 64, 175, 196, 54, 157, 176, 45, 137, 154, 32, 117, 133, 19, 98, 111, 0, 78, 89, 0, 53, 63, 0, 17, 27,
+ 80, 195, 198, 69, 175, 179, 60, 156, 159, 50, 136, 139, 38, 116, 120, 25, 98, 101, 4, 77, 80, 0, 53, 55, 0, 17, 21,
+ 84, 193, 177, 75, 173, 159, 64, 155, 142, 55, 135, 124, 41, 116, 107, 27, 97, 89, 9, 76, 70, 0, 53, 47, 0, 17, 11,
+ 89, 193, 157, 79, 172, 140, 70, 154, 125, 57, 134, 109, 44, 115, 92, 32, 96, 76, 13, 76, 59, 0, 52, 39, 0, 16, 4,
+ 94, 191, 135, 85, 171, 121, 72, 152, 108, 60, 133, 94, 47, 114, 80, 32, 95, 65, 15, 76, 49, 0, 52, 29, 0, 16, 0,
+ 98, 190, 113, 87, 170, 100, 76, 152, 89, 62, 132, 77, 49, 113, 65, 35, 95, 52, 18, 75, 37, 0, 52, 18, 0, 15, 0,
+ 103, 190, 89, 90, 169, 80, 78, 151, 70, 64, 132, 60, 51, 113, 49, 37, 94, 38, 20, 75, 25, 0, 52, 5, 0, 15, 0,
+ 106, 189, 69, 93, 169, 61, 80, 151, 53, 66, 131, 45, 52, 113, 36, 37, 94, 25, 19, 74, 11, 0, 51, 0, 0, 15, 0,
+ 76, 178, 229, 68, 159, 205, 61, 142, 183, 50, 124, 160, 40, 106, 138, 28, 88, 116, 12, 69, 93, 0, 45, 66, 0, 5, 29,
+ 86, 177, 207, 78, 158, 184, 67, 142, 166, 56, 123, 145, 45, 106, 125, 31, 88, 105, 16, 69, 83, 0, 45, 58, 0, 6, 22,
+ 93, 176, 187, 81, 158, 168, 71, 141, 150, 61, 123, 131, 47, 105, 113, 35, 87, 94, 20, 68, 74, 0, 45, 51, 0, 5, 16,
+ 98, 175, 168, 84, 157, 150, 75, 140, 134, 63, 122, 117, 50, 104, 100, 37, 87, 83, 21, 68, 65, 0, 45, 42, 0, 4, 7,
+ 100, 174, 149, 89, 155, 132, 76, 139, 117, 65, 121, 102, 53, 104, 87, 39, 86, 72, 23, 67, 55, 0, 45, 34, 0, 3, 0,
+ 103, 173, 130, 92, 155, 115, 80, 138, 102, 68, 120, 88, 53, 103, 75, 40, 86, 61, 24, 67, 45, 0, 45, 25, 0, 3, 0,
+ 107, 172, 108, 95, 154, 96, 82, 137, 85, 70, 119, 73, 55, 102, 61, 42, 85, 49, 25, 67, 34, 0, 45, 14, 0, 3, 0,
+ 110, 172, 86, 97, 153, 76, 85, 137, 67, 70, 119, 57, 56, 102, 46, 42, 84, 35, 26, 66, 21, 0, 44, 1, 0, 3, 0,
+ 112, 171, 67, 98, 153, 59, 86, 137, 52, 71, 119, 44, 58, 102, 34, 44, 85, 22, 27, 66, 7, 0, 44, 0, 0, 3, 0,
+ 90, 160, 215, 81, 144, 193, 70, 129, 173, 61, 112, 151, 49, 95, 131, 37, 79, 109, 22, 61, 87, 0, 38, 61, 0, 0, 25,
+ 96, 160, 194, 86, 143, 174, 75, 128, 157, 65, 112, 137, 53, 95, 117, 40, 78, 98, 25, 60, 78, 0, 38, 53, 0, 0, 17,
+ 100, 159, 177, 89, 143, 159, 79, 128, 143, 67, 111, 124, 55, 95, 107, 42, 78, 89, 27, 60, 70, 2, 38, 46, 0, 0, 9,
+ 104, 158, 159, 92, 142, 143, 81, 127, 127, 69, 110, 110, 56, 94, 94, 43, 78, 78, 28, 60, 60, 2, 38, 38, 0, 0, 1,
+ 107, 157, 140, 94, 141, 125, 82, 126, 112, 71, 110, 97, 59, 94, 82, 45, 77, 67, 29, 59, 51, 4, 37, 30, 0, 0, 0,
+ 110, 156, 122, 97, 140, 109, 85, 125, 97, 72, 109, 83, 58, 93, 71, 45, 77, 57, 29, 60, 42, 5, 38, 22, 0, 0, 0,
+ 111, 156, 103, 99, 139, 91, 87, 125, 81, 73, 108, 69, 60, 92, 58, 46, 77, 45, 30, 59, 31, 5, 38, 12, 0, 0, 0,
+ 115, 156, 82, 101, 140, 73, 88, 124, 63, 74, 108, 53, 60, 92, 44, 46, 76, 32, 31, 59, 18, 6, 37, 0, 0, 0, 0,
+ 116, 155, 65, 102, 139, 58, 89, 124, 49, 75, 108, 41, 61, 92, 32, 48, 76, 21, 31, 59, 6, 5, 37, 0, 0, 0, 0,
+ 100, 141, 201, 88, 127, 181, 79, 114, 162, 69, 99, 142, 57, 83, 122, 44, 68, 102, 30, 51, 81, 7, 28, 56, 0, 0, 19,
+ 105, 141, 182, 94, 127, 163, 83, 114, 146, 71, 98, 128, 59, 83, 110, 46, 68, 91, 31, 51, 72, 10, 28, 48, 0, 0, 11,
+ 108, 141, 166, 96, 127, 149, 85, 113, 133, 73, 98, 116, 60, 83, 99, 46, 68, 82, 32, 51, 64, 11, 29, 41, 0, 0, 2,
+ 111, 141, 149, 98, 126, 134, 88, 112, 119, 74, 97, 103, 61, 83, 88, 48, 67, 72, 33, 51, 56, 11, 29, 34, 0, 0, 0,
+ 112, 140, 132, 100, 125, 118, 89, 112, 105, 75, 97, 91, 62, 82, 77, 49, 68, 62, 33, 51, 47, 12, 29, 26, 0, 0, 0,
+ 115, 140, 116, 102, 125, 103, 90, 111, 91, 76, 96, 78, 62, 82, 65, 49, 67, 52, 34, 51, 38, 13, 29, 18, 0, 0, 0,
+ 117, 139, 97, 103, 124, 87, 91, 111, 77, 78, 96, 65, 63, 81, 54, 49, 67, 41, 34, 51, 27, 12, 29, 7, 0, 0, 0,
+ 119, 138, 78, 105, 124, 69, 92, 110, 60, 78, 95, 50, 65, 81, 40, 50, 67, 29, 34, 51, 15, 13, 30, 0, 0, 0, 0,
+ 120, 138, 64, 106, 124, 54, 93, 110, 47, 78, 95, 38, 65, 81, 29, 50, 66, 17, 34, 50, 2, 13, 29, 0, 0, 0, 0,
+ 107, 124, 189, 96, 111, 169, 85, 99, 152, 73, 85, 132, 61, 71, 114, 48, 57, 95, 34, 41, 75, 14, 18, 51, 0, 0, 13,
+ 111, 124, 171, 100, 111, 153, 88, 99, 137, 75, 85, 120, 63, 72, 103, 50, 58, 85, 36, 41, 66, 15, 19, 43, 0, 0, 4,
+ 113, 124, 156, 101, 111, 139, 90, 99, 125, 77, 85, 109, 64, 71, 93, 51, 57, 77, 36, 42, 59, 17, 20, 37, 0, 0, 0,
+ 115, 124, 140, 103, 111, 125, 90, 99, 112, 78, 85, 97, 64, 71, 82, 52, 57, 67, 36, 42, 50, 16, 20, 30, 0, 0, 0,
+ 117, 123, 125, 104, 110, 111, 92, 98, 99, 79, 85, 86, 65, 71, 72, 51, 58, 59, 37, 42, 43, 17, 21, 22, 0, 0, 0,
+ 118, 123, 110, 105, 110, 97, 93, 98, 86, 78, 84, 74, 66, 71, 62, 52, 57, 49, 37, 42, 34, 17, 22, 14, 0, 0, 0,
+ 120, 123, 93, 106, 109, 82, 94, 97, 72, 80, 84, 61, 66, 71, 50, 52, 57, 38, 37, 42, 24, 17, 22, 2, 0, 0, 0,
+ 121, 122, 75, 108, 109, 66, 95, 97, 58, 80, 84, 48, 66, 71, 37, 52, 57, 26, 37, 42, 12, 16, 22, 0, 0, 0, 0,
+ 122, 123, 62, 108, 109, 52, 95, 97, 45, 81, 84, 36, 67, 70, 26, 52, 57, 14, 37, 42, 0, 15, 22, 0, 0, 0, 0,
+ 113, 107, 177, 102, 96, 159, 89, 85, 141, 78, 72, 124, 65, 60, 107, 52, 46, 89, 37, 30, 70, 18, 5, 46, 0, 0, 6,
+ 116, 107, 160, 104, 96, 144, 92, 85, 129, 80, 72, 112, 67, 60, 96, 53, 47, 80, 38, 31, 62, 19, 7, 39, 0, 0, 0,
+ 118, 107, 147, 105, 96, 131, 93, 85, 118, 80, 72, 101, 67, 60, 87, 54, 47, 71, 39, 31, 54, 19, 8, 32, 0, 0, 0,
+ 119, 107, 132, 106, 96, 118, 94, 85, 106, 81, 73, 91, 67, 60, 77, 54, 47, 63, 39, 32, 47, 20, 9, 25, 0, 0, 0,
+ 119, 107, 118, 106, 95, 105, 94, 85, 93, 81, 72, 80, 68, 60, 68, 54, 47, 54, 39, 32, 39, 20, 11, 18, 0, 0, 0,
+ 121, 107, 104, 107, 96, 92, 95, 84, 80, 81, 72, 69, 68, 61, 58, 54, 48, 46, 39, 33, 31, 20, 12, 9, 0, 0, 0,
+ 123, 107, 88, 108, 95, 77, 96, 84, 68, 82, 72, 57, 68, 60, 46, 54, 47, 35, 39, 33, 20, 19, 13, 2, 0, 0, 0,
+ 123, 106, 72, 110, 95, 63, 96, 84, 54, 82, 72, 45, 69, 60, 35, 55, 48, 23, 39, 33, 9, 18, 14, 0, 0, 0, 0,
+ 125, 106, 60, 110, 94, 50, 98, 84, 42, 83, 72, 34, 69, 60, 25, 55, 48, 12, 39, 33, 0, 17, 13, 0, 0, 0, 0,
+ 118, 89, 165, 105, 79, 148, 93, 69, 132, 81, 57, 115, 68, 45, 99, 55, 32, 82, 41, 15, 64, 21, 0, 41, 0, 0, 0,
+ 120, 89, 150, 107, 79, 135, 96, 69, 121, 82, 58, 105, 70, 46, 89, 56, 34, 73, 41, 17, 56, 21, 0, 34, 0, 0, 0,
+ 121, 89, 137, 108, 79, 123, 96, 69, 109, 82, 58, 95, 70, 47, 81, 56, 34, 66, 41, 18, 49, 21, 0, 28, 0, 0, 0,
+ 122, 90, 124, 109, 79, 110, 96, 69, 99, 83, 58, 85, 70, 47, 72, 56, 35, 58, 41, 19, 42, 21, 0, 22, 0, 0, 0,
+ 123, 90, 111, 110, 79, 98, 97, 69, 87, 83, 59, 75, 70, 47, 63, 56, 35, 50, 41, 20, 35, 21, 0, 14, 0, 0, 0,
+ 123, 90, 98, 110, 79, 87, 97, 70, 76, 84, 58, 64, 70, 48, 53, 56, 36, 41, 40, 21, 26, 21, 0, 5, 0, 0, 0,
+ 125, 89, 84, 111, 79, 73, 97, 69, 64, 84, 59, 54, 70, 48, 43, 56, 36, 31, 40, 22, 17, 20, 1, 1, 0, 0, 0,
+ 125, 89, 69, 112, 79, 60, 98, 70, 51, 84, 59, 42, 70, 48, 32, 56, 36, 20, 41, 22, 5, 19, 2, 0, 0, 0, 0,
+ 126, 89, 57, 112, 79, 49, 99, 70, 41, 84, 59, 32, 70, 48, 22, 56, 36, 10, 40, 22, 0, 18, 2, 0, 0, 0, 0,
+ 121, 67, 154, 108, 58, 138, 97, 50, 124, 84, 39, 107, 71, 28, 92, 58, 12, 76, 43, 0, 59, 20, 0, 37, 0, 0, 0,
+ 124, 68, 140, 111, 59, 126, 98, 50, 112, 84, 40, 98, 71, 29, 83, 58, 15, 67, 42, 0, 51, 20, 0, 30, 0, 0, 0,
+ 124, 68, 129, 111, 59, 114, 99, 51, 102, 86, 41, 88, 71, 30, 75, 58, 17, 60, 42, 0, 45, 20, 0, 24, 0, 0, 0,
+ 125, 70, 116, 111, 60, 103, 99, 51, 92, 85, 41, 79, 71, 31, 66, 58, 19, 53, 42, 3, 38, 20, 0, 17, 0, 0, 0,
+ 125, 70, 104, 111, 61, 93, 99, 52, 81, 85, 43, 69, 72, 32, 58, 58, 20, 45, 42, 4, 31, 20, 0, 8, 0, 0, 0,
+ 126, 70, 92, 111, 61, 81, 99, 52, 71, 85, 42, 60, 71, 33, 49, 57, 21, 37, 42, 6, 23, 20, 0, 3, 0, 0, 0,
+ 126, 70, 79, 112, 61, 70, 99, 53, 60, 85, 43, 50, 71, 33, 39, 57, 22, 28, 41, 7, 13, 19, 0, 0, 0, 0, 0,
+ 127, 71, 66, 113, 62, 56, 100, 53, 48, 86, 44, 39, 71, 34, 29, 57, 23, 18, 41, 8, 2, 18, 0, 0, 0, 0, 0,
+ 128, 70, 55, 114, 62, 46, 100, 54, 39, 86, 44, 30, 71, 34, 20, 57, 23, 7, 41, 9, 0, 18, 0, 0, 0, 0, 0,
+ 124, 41, 145, 111, 32, 128, 99, 23, 114, 86, 10, 100, 73, 0, 85, 60, 0, 71, 43, 0, 54, 17, 0, 32, 0, 0, 0,
+ 126, 42, 131, 113, 33, 117, 100, 25, 104, 86, 14, 90, 73, 0, 77, 60, 0, 63, 44, 0, 47, 18, 0, 25, 0, 0, 0,
+ 127, 43, 120, 113, 34, 106, 101, 26, 95, 86, 17, 82, 73, 2, 69, 59, 0, 56, 43, 0, 41, 18, 0, 19, 0, 0, 0,
+ 127, 45, 109, 113, 37, 97, 101, 28, 85, 86, 19, 74, 73, 5, 61, 59, 0, 48, 43, 0, 34, 19, 0, 11, 0, 0, 0,
+ 127, 46, 98, 114, 38, 86, 100, 30, 76, 87, 21, 65, 73, 9, 54, 59, 0, 41, 43, 0, 26, 18, 0, 5, 0, 0, 0,
+ 127, 47, 87, 113, 39, 76, 101, 31, 67, 86, 22, 56, 72, 11, 45, 59, 0, 33, 43, 0, 19, 18, 0, 2, 0, 0, 0,
+ 128, 48, 75, 114, 39, 65, 101, 33, 56, 86, 23, 46, 72, 12, 36, 58, 0, 24, 42, 0, 9, 17, 0, 0, 0, 0, 0,
+ 129, 48, 63, 114, 41, 54, 102, 33, 46, 87, 24, 36, 72, 14, 26, 58, 1, 14, 42, 0, 2, 16, 0, 0, 0, 0, 0,
+ 128, 48, 53, 114, 41, 44, 102, 34, 37, 87, 25, 27, 72, 15, 17, 58, 1, 3, 41, 0, 0, 15, 0, 0, 0, 0, 0,
+ 0, 189, 242, 0, 169, 217, 0, 151, 194, 0, 132, 170, 0, 113, 147, 0, 94, 123, 0, 74, 99, 0, 51, 71, 0, 15, 34,
+ 1, 187, 219, 1, 167, 195, 0, 150, 175, 0, 131, 153, 0, 113, 132, 0, 94, 111, 0, 74, 89, 0, 50, 63, 0, 13, 28,
+ 1, 186, 198, 1, 167, 178, 0, 149, 158, 0, 130, 139, 0, 111, 119, 0, 93, 100, 0, 74, 80, 0, 50, 55, 0, 13, 22,
+ 1, 185, 176, 1, 165, 159, 1, 148, 142, 0, 129, 123, 0, 111, 106, 0, 93, 89, 0, 73, 70, 0, 50, 47, 0, 13, 13,
+ 1, 184, 157, 1, 164, 141, 1, 147, 125, 0, 128, 110, 0, 110, 93, 0, 92, 77, 0, 73, 60, 0, 50, 39, 0, 12, 5,
+ 25, 182, 137, 25, 163, 122, 17, 146, 109, 0, 128, 96, 0, 110, 81, 0, 92, 66, 0, 73, 51, 0, 50, 30, 0, 10, 0,
+ 42, 181, 114, 35, 163, 102, 30, 145, 91, 14, 127, 80, 0, 109, 67, 0, 91, 53, 0, 72, 39, 0, 50, 19, 0, 10, 0,
+ 52, 181, 92, 43, 162, 83, 32, 145, 73, 19, 126, 63, 0, 108, 52, 0, 90, 40, 0, 72, 27, 0, 50, 7, 0, 10, 0,
+ 57, 181, 74, 48, 162, 66, 37, 144, 57, 24, 126, 49, 7, 108, 40, 0, 90, 29, 0, 72, 15, 0, 49, 0, 0, 10, 0,
+ 1, 170, 227, 1, 152, 203, 0, 136, 182, 0, 119, 159, 0, 101, 137, 0, 84, 115, 0, 65, 92, 0, 43, 66, 0, 1, 29,
+ 1, 169, 206, 1, 151, 184, 1, 136, 165, 0, 118, 144, 0, 102, 125, 0, 84, 105, 0, 65, 83, 0, 43, 58, 0, 0, 22,
+ 29, 168, 186, 21, 151, 167, 14, 135, 150, 4, 118, 131, 0, 101, 112, 0, 83, 94, 0, 65, 75, 0, 43, 51, 0, 0, 16,
+ 41, 167, 167, 33, 150, 150, 31, 134, 134, 19, 117, 117, 4, 100, 100, 0, 83, 83, 0, 65, 65, 0, 42, 43, 0, 0, 8,
+ 48, 167, 149, 41, 149, 133, 33, 133, 118, 25, 116, 103, 13, 99, 88, 0, 83, 73, 0, 65, 56, 0, 42, 35, 0, 0, 0,
+ 58, 165, 130, 49, 148, 115, 42, 132, 103, 31, 115, 89, 18, 99, 75, 0, 82, 61, 0, 64, 46, 0, 42, 26, 0, 0, 0,
+ 62, 164, 110, 55, 147, 97, 45, 132, 87, 35, 115, 75, 22, 98, 63, 5, 82, 50, 0, 64, 36, 0, 42, 16, 0, 0, 0,
+ 69, 164, 89, 60, 147, 78, 50, 131, 70, 37, 114, 59, 26, 98, 49, 10, 81, 37, 0, 64, 24, 0, 42, 4, 0, 0, 0,
+ 71, 164, 71, 63, 147, 63, 53, 131, 55, 40, 114, 47, 28, 98, 38, 13, 81, 26, 0, 64, 12, 0, 42, 0, 0, 0, 0,
+ 28, 153, 214, 24, 138, 193, 23, 123, 171, 16, 107, 150, 0, 91, 130, 0, 75, 109, 0, 58, 87, 0, 35, 61, 0, 0, 25,
+ 48, 153, 194, 41, 138, 174, 34, 123, 156, 27, 107, 136, 16, 91, 117, 1, 75, 98, 0, 57, 78, 0, 35, 53, 0, 0, 17,
+ 55, 153, 177, 47, 137, 158, 42, 122, 142, 33, 107, 124, 22, 91, 106, 6, 75, 88, 0, 57, 70, 0, 35, 46, 0, 0, 9,
+ 61, 152, 158, 53, 136, 143, 45, 122, 127, 36, 106, 111, 24, 90, 94, 10, 74, 78, 0, 57, 61, 0, 35, 39, 0, 0, 2,
+ 67, 151, 141, 59, 135, 126, 49, 121, 112, 39, 105, 98, 29, 90, 83, 14, 74, 68, 0, 57, 52, 0, 35, 31, 0, 0, 0,
+ 71, 150, 123, 62, 135, 110, 54, 120, 98, 42, 105, 84, 31, 89, 71, 16, 74, 58, 0, 57, 43, 0, 35, 22, 0, 0, 0,
+ 74, 150, 105, 64, 134, 92, 55, 120, 83, 45, 104, 71, 34, 89, 59, 20, 73, 47, 0, 57, 32, 0, 35, 13, 0, 0, 0,
+ 78, 149, 84, 69, 134, 75, 59, 120, 66, 47, 103, 56, 34, 88, 46, 22, 73, 34, 1, 57, 20, 0, 35, 1, 0, 0, 0,
+ 80, 149, 69, 70, 133, 61, 60, 119, 53, 49, 103, 44, 36, 88, 35, 23, 73, 24, 2, 56, 10, 0, 35, 0, 0, 0, 0,
+ 58, 136, 200, 50, 122, 180, 45, 109, 162, 38, 94, 141, 27, 80, 121, 15, 65, 102, 0, 48, 81, 0, 26, 56, 0, 0, 19,
+ 66, 136, 182, 59, 122, 163, 52, 109, 146, 42, 94, 128, 32, 80, 109, 20, 65, 91, 2, 48, 72, 0, 26, 49, 0, 0, 11,
+ 70, 136, 165, 62, 122, 149, 55, 108, 133, 46, 94, 116, 35, 80, 99, 21, 65, 82, 4, 49, 64, 0, 26, 41, 0, 0, 3,
+ 76, 135, 149, 66, 121, 133, 58, 108, 119, 48, 94, 103, 36, 79, 88, 23, 65, 73, 7, 49, 56, 0, 27, 34, 0, 0, 0,
+ 78, 135, 133, 69, 120, 118, 60, 107, 106, 50, 93, 92, 39, 79, 77, 26, 65, 63, 8, 49, 47, 0, 27, 26, 0, 0, 0,
+ 82, 134, 117, 71, 120, 104, 62, 107, 92, 51, 93, 79, 39, 78, 66, 27, 64, 53, 10, 48, 39, 0, 27, 18, 0, 0, 0,
+ 84, 134, 99, 73, 119, 87, 64, 106, 77, 53, 92, 66, 42, 78, 55, 28, 64, 42, 11, 48, 29, 0, 28, 9, 0, 0, 0,
+ 87, 133, 81, 76, 119, 72, 66, 106, 62, 55, 92, 52, 43, 78, 42, 29, 64, 31, 12, 48, 17, 0, 28, 0, 0, 0, 0,
+ 88, 134, 67, 77, 119, 58, 68, 106, 51, 56, 92, 42, 44, 78, 32, 30, 64, 20, 12, 48, 6, 0, 28, 0, 0, 0, 0,
+ 73, 120, 189, 64, 107, 168, 57, 96, 151, 47, 82, 133, 38, 69, 114, 26, 55, 95, 11, 39, 75, 0, 16, 51, 0, 0, 14,
+ 78, 120, 171, 69, 107, 153, 62, 95, 137, 51, 82, 119, 40, 69, 102, 29, 55, 85, 15, 39, 66, 0, 17, 44, 0, 0, 4,
+ 81, 120, 156, 71, 107, 140, 64, 95, 125, 53, 82, 109, 42, 69, 93, 31, 55, 77, 16, 39, 59, 0, 18, 37, 0, 0, 0,
+ 85, 120, 141, 74, 107, 126, 65, 95, 112, 54, 82, 97, 43, 69, 82, 32, 55, 67, 17, 39, 51, 0, 19, 30, 0, 0, 0,
+ 86, 119, 126, 76, 106, 112, 66, 95, 100, 56, 81, 85, 45, 69, 72, 33, 55, 59, 18, 40, 43, 0, 19, 22, 0, 0, 0,
+ 89, 119, 110, 78, 106, 98, 69, 94, 87, 56, 81, 75, 46, 68, 62, 33, 55, 49, 18, 40, 35, 0, 20, 15, 0, 0, 0,
+ 89, 119, 95, 80, 106, 83, 70, 94, 73, 58, 81, 63, 46, 68, 51, 34, 55, 39, 19, 40, 25, 0, 20, 4, 0, 0, 0,
+ 92, 118, 78, 82, 106, 68, 70, 93, 59, 59, 81, 49, 47, 68, 39, 34, 55, 28, 19, 40, 14, 0, 20, 0, 0, 0, 0,
+ 93, 118, 65, 82, 105, 55, 72, 93, 48, 60, 81, 39, 47, 68, 29, 34, 55, 18, 20, 40, 2, 0, 20, 0, 0, 0, 0,
+ 83, 104, 177, 74, 93, 159, 65, 82, 142, 56, 70, 124, 45, 57, 106, 33, 44, 89, 20, 28, 70, 1, 4, 46, 0, 0, 8,
+ 86, 104, 161, 78, 93, 145, 68, 82, 128, 58, 70, 112, 48, 58, 96, 35, 45, 80, 21, 29, 62, 1, 6, 40, 0, 0, 0,
+ 89, 104, 147, 79, 93, 131, 69, 82, 118, 59, 70, 102, 47, 58, 87, 36, 45, 72, 23, 29, 55, 3, 7, 33, 0, 0, 0,
+ 90, 104, 132, 80, 93, 119, 71, 82, 106, 60, 70, 91, 48, 58, 77, 37, 45, 62, 23, 30, 47, 4, 7, 26, 0, 0, 0,
+ 92, 104, 118, 82, 93, 105, 72, 82, 93, 61, 69, 80, 50, 58, 68, 37, 45, 55, 23, 30, 39, 4, 8, 18, 0, 0, 0,
+ 94, 104, 105, 82, 92, 93, 72, 82, 82, 61, 70, 70, 50, 58, 58, 38, 46, 46, 23, 31, 31, 6, 10, 11, 0, 0, 0,
+ 95, 104, 90, 84, 92, 79, 74, 82, 70, 62, 70, 58, 50, 58, 48, 37, 46, 36, 23, 31, 22, 4, 11, 3, 0, 0, 0,
+ 96, 103, 74, 85, 92, 65, 75, 81, 56, 63, 70, 47, 50, 58, 37, 38, 46, 25, 24, 31, 11, 3, 11, 0, 0, 0, 0,
+ 97, 103, 62, 86, 92, 53, 76, 81, 45, 63, 69, 36, 51, 58, 27, 38, 46, 15, 23, 31, 0, 3, 11, 0, 0, 0, 0,
+ 90, 87, 165, 81, 77, 148, 72, 67, 132, 62, 55, 116, 50, 44, 99, 39, 31, 82, 25, 14, 64, 1, 0, 42, 0, 0, 1,
+ 93, 87, 150, 83, 77, 135, 74, 67, 121, 63, 56, 105, 52, 45, 90, 40, 32, 74, 25, 16, 57, 3, 0, 35, 0, 0, 0,
+ 95, 87, 138, 85, 77, 123, 75, 67, 109, 63, 57, 95, 53, 45, 81, 41, 33, 66, 26, 17, 50, 4, 0, 28, 0, 0, 0,
+ 95, 88, 124, 85, 77, 111, 75, 67, 99, 63, 57, 86, 53, 45, 72, 41, 33, 58, 26, 18, 43, 5, 1, 22, 0, 0, 0,
+ 97, 88, 112, 87, 77, 100, 76, 68, 88, 64, 57, 76, 53, 46, 63, 41, 34, 50, 26, 19, 35, 5, 2, 14, 0, 0, 0,
+ 99, 87, 99, 87, 78, 88, 76, 68, 77, 65, 57, 65, 53, 46, 54, 41, 35, 42, 27, 20, 27, 6, 2, 5, 0, 0, 0,
+ 100, 87, 85, 88, 77, 75, 77, 68, 65, 65, 57, 54, 53, 46, 44, 41, 35, 32, 27, 21, 19, 5, 3, 2, 0, 0, 0,
+ 100, 88, 71, 89, 77, 61, 78, 68, 53, 66, 57, 44, 53, 47, 33, 41, 35, 22, 27, 21, 7, 5, 3, 0, 0, 0, 0,
+ 101, 87, 60, 90, 77, 52, 79, 68, 44, 66, 58, 34, 53, 47, 25, 41, 35, 13, 26, 22, 0, 5, 3, 0, 0, 0, 0,
+ 97, 67, 155, 86, 58, 138, 77, 50, 125, 66, 39, 108, 55, 28, 92, 43, 12, 76, 29, 0, 59, 2, 0, 37, 0, 0, 0,
+ 99, 67, 141, 88, 59, 127, 78, 50, 113, 68, 40, 98, 56, 29, 83, 44, 15, 68, 29, 0, 52, 3, 0, 30, 0, 0, 0,
+ 100, 68, 129, 89, 59, 115, 80, 51, 103, 68, 41, 89, 56, 30, 75, 44, 16, 61, 30, 0, 45, 4, 0, 24, 0, 0, 0,
+ 100, 69, 118, 90, 60, 104, 80, 51, 92, 67, 41, 79, 56, 31, 66, 44, 18, 53, 29, 2, 38, 4, 0, 18, 0, 0, 0,
+ 101, 69, 104, 90, 61, 93, 79, 51, 82, 67, 42, 70, 56, 32, 59, 44, 20, 46, 29, 4, 31, 6, 0, 9, 0, 0, 0,
+ 102, 69, 93, 90, 61, 83, 80, 52, 72, 68, 42, 61, 56, 33, 50, 43, 20, 38, 29, 5, 23, 7, 0, 4, 0, 0, 0,
+ 102, 70, 80, 91, 61, 71, 80, 52, 61, 68, 43, 51, 56, 32, 40, 44, 21, 29, 30, 6, 14, 7, 0, 0, 0, 0, 0,
+ 103, 70, 68, 92, 61, 58, 81, 53, 50, 69, 43, 41, 56, 34, 31, 43, 22, 19, 29, 7, 3, 7, 0, 0, 0, 0, 0,
+ 104, 70, 57, 92, 61, 48, 82, 53, 40, 69, 43, 32, 56, 34, 22, 43, 23, 10, 29, 8, 0, 6, 0, 0, 0, 0, 0,
+ 101, 45, 145, 91, 35, 129, 80, 26, 116, 69, 15, 101, 59, 0, 86, 46, 0, 71, 31, 0, 55, 0, 0, 33, 0, 0, 0,
+ 104, 44, 132, 92, 36, 118, 82, 28, 105, 71, 17, 91, 58, 3, 77, 46, 0, 63, 31, 0, 48, 2, 0, 26, 0, 0, 0,
+ 104, 46, 121, 93, 37, 107, 82, 30, 96, 70, 20, 83, 58, 6, 70, 46, 0, 57, 32, 0, 41, 4, 0, 20, 0, 0, 0,
+ 104, 48, 110, 93, 40, 98, 82, 31, 87, 70, 22, 74, 59, 9, 62, 45, 0, 49, 31, 0, 35, 6, 0, 13, 0, 0, 0,
+ 104, 48, 99, 92, 41, 88, 82, 32, 77, 70, 23, 65, 58, 11, 54, 46, 0, 42, 32, 0, 27, 7, 0, 5, 0, 0, 0,
+ 105, 50, 88, 93, 41, 77, 82, 34, 68, 71, 24, 57, 58, 13, 46, 45, 1, 35, 31, 0, 21, 7, 0, 2, 0, 0, 0,
+ 105, 50, 76, 94, 41, 66, 83, 34, 57, 71, 25, 47, 58, 15, 37, 45, 2, 25, 32, 0, 11, 7, 0, 0, 0, 0, 0,
+ 106, 50, 64, 94, 42, 55, 83, 35, 47, 71, 26, 38, 58, 16, 27, 45, 4, 17, 31, 0, 4, 7, 0, 0, 0, 0, 0,
+ 106, 51, 54, 95, 42, 45, 83, 35, 38, 71, 27, 30, 58, 16, 19, 45, 5, 7, 30, 0, 0, 6, 0, 0, 0, 0, 0,
+ 0, 181, 240, 0, 162, 216, 0, 144, 193, 0, 126, 168, 0, 109, 146, 0, 91, 123, 0, 71, 98, 0, 48, 71, 0, 9, 34,
+ 0, 179, 218, 0, 161, 195, 0, 144, 174, 0, 126, 153, 0, 108, 132, 0, 90, 110, 0, 71, 88, 0, 48, 63, 0, 8, 29,
+ 0, 178, 197, 0, 159, 177, 0, 143, 159, 0, 125, 139, 0, 107, 119, 0, 90, 99, 0, 71, 79, 0, 48, 55, 0, 8, 22,
+ 0, 177, 177, 0, 158, 158, 0, 142, 141, 0, 124, 123, 0, 107, 106, 0, 89, 88, 0, 71, 70, 0, 48, 47, 0, 8, 14,
+ 0, 176, 157, 0, 158, 141, 0, 141, 126, 0, 123, 109, 0, 106, 93, 0, 89, 78, 0, 70, 60, 0, 47, 39, 0, 7, 5,
+ 0, 175, 138, 0, 157, 123, 0, 141, 110, 0, 123, 96, 0, 105, 81, 0, 88, 67, 0, 70, 51, 0, 48, 30, 0, 6, 0,
+ 0, 173, 115, 0, 155, 104, 0, 140, 92, 0, 122, 80, 0, 105, 67, 0, 88, 55, 0, 69, 40, 0, 47, 20, 0, 6, 0,
+ 0, 173, 94, 0, 155, 85, 0, 139, 75, 0, 121, 64, 0, 104, 53, 0, 88, 42, 0, 70, 28, 0, 47, 9, 0, 6, 0,
+ 0, 173, 76, 0, 155, 70, 0, 138, 61, 0, 122, 53, 0, 104, 44, 0, 87, 32, 0, 69, 18, 0, 47, 0, 0, 6, 0,
+ 0, 164, 226, 0, 147, 203, 0, 131, 181, 0, 114, 158, 0, 97, 136, 0, 80, 115, 0, 63, 92, 0, 40, 65, 0, 0, 30,
+ 0, 162, 205, 0, 145, 184, 0, 130, 164, 0, 114, 143, 0, 97, 124, 0, 81, 104, 0, 63, 83, 0, 40, 58, 0, 0, 23,
+ 0, 162, 187, 0, 145, 167, 0, 130, 150, 0, 113, 131, 0, 96, 112, 0, 80, 93, 0, 62, 74, 0, 40, 50, 0, 0, 16,
+ 0, 160, 167, 0, 144, 150, 0, 129, 134, 0, 112, 116, 0, 96, 100, 0, 80, 82, 0, 62, 65, 0, 40, 43, 0, 0, 7,
+ 0, 160, 148, 0, 143, 133, 0, 128, 118, 0, 111, 103, 0, 96, 88, 0, 80, 73, 0, 62, 56, 0, 40, 35, 0, 0, 0,
+ 0, 158, 130, 0, 142, 117, 0, 127, 104, 0, 111, 89, 0, 95, 76, 0, 79, 62, 0, 62, 46, 0, 40, 26, 0, 0, 0,
+ 0, 158, 111, 0, 141, 99, 0, 127, 88, 0, 111, 76, 0, 95, 63, 0, 79, 51, 0, 62, 37, 0, 40, 18, 0, 0, 0,
+ 0, 158, 91, 0, 141, 81, 0, 126, 72, 0, 110, 62, 0, 94, 50, 0, 79, 39, 0, 62, 25, 0, 40, 5, 0, 0, 0,
+ 0, 157, 74, 0, 141, 66, 0, 126, 59, 0, 110, 49, 0, 94, 40, 0, 78, 29, 0, 61, 15, 0, 40, 0, 0, 0, 0,
+ 0, 148, 214, 0, 133, 192, 0, 119, 171, 0, 103, 150, 0, 87, 129, 0, 72, 108, 0, 55, 86, 0, 32, 61, 0, 0, 25,
+ 0, 147, 193, 0, 132, 173, 0, 118, 155, 0, 103, 136, 0, 87, 116, 0, 72, 98, 0, 55, 78, 0, 32, 53, 0, 0, 17,
+ 0, 147, 176, 0, 132, 158, 0, 118, 142, 0, 102, 124, 0, 87, 106, 0, 72, 88, 0, 55, 69, 0, 33, 46, 0, 0, 9,
+ 0, 146, 159, 0, 131, 142, 0, 117, 127, 0, 102, 111, 0, 87, 95, 0, 71, 79, 0, 55, 61, 0, 33, 39, 0, 0, 2,
+ 0, 145, 140, 0, 130, 126, 0, 117, 112, 0, 101, 98, 0, 86, 83, 0, 71, 68, 0, 55, 52, 0, 33, 31, 0, 0, 0,
+ 0, 144, 124, 0, 130, 111, 0, 116, 99, 0, 101, 84, 0, 86, 72, 0, 71, 59, 0, 55, 43, 0, 33, 23, 0, 0, 0,
+ 0, 144, 106, 0, 129, 94, 0, 115, 83, 0, 101, 72, 0, 85, 60, 0, 71, 48, 0, 55, 34, 0, 33, 14, 0, 0, 0,
+ 3, 143, 86, 0, 129, 77, 0, 115, 68, 0, 100, 58, 0, 85, 48, 0, 70, 36, 0, 54, 22, 0, 33, 3, 0, 0, 0,
+ 18, 143, 72, 13, 128, 63, 0, 115, 57, 0, 100, 47, 0, 85, 37, 0, 70, 26, 0, 54, 13, 0, 33, 0, 0, 0, 0,
+ 0, 132, 200, 0, 118, 179, 0, 105, 161, 0, 91, 140, 0, 76, 121, 0, 62, 101, 0, 46, 81, 0, 24, 56, 0, 0, 19,
+ 0, 131, 182, 0, 118, 163, 0, 105, 146, 0, 91, 128, 0, 77, 110, 0, 62, 91, 0, 46, 72, 0, 25, 48, 0, 0, 11,
+ 0, 131, 165, 0, 117, 149, 0, 104, 133, 0, 91, 116, 0, 77, 99, 0, 62, 82, 0, 46, 64, 0, 25, 41, 0, 0, 4,
+ 0, 131, 149, 0, 116, 134, 0, 104, 119, 0, 91, 104, 0, 77, 89, 0, 62, 73, 0, 46, 56, 0, 25, 34, 0, 0, 0,
+ 10, 130, 133, 2, 116, 119, 0, 104, 106, 0, 90, 91, 0, 76, 78, 0, 62, 64, 0, 46, 48, 0, 26, 27, 0, 0, 0,
+ 23, 130, 118, 20, 116, 104, 13, 103, 93, 3, 89, 79, 0, 76, 67, 0, 62, 54, 0, 46, 39, 0, 26, 19, 0, 0, 0,
+ 33, 129, 101, 27, 115, 89, 19, 103, 79, 9, 89, 67, 0, 75, 56, 0, 61, 43, 0, 46, 29, 0, 26, 10, 0, 0, 0,
+ 41, 128, 83, 35, 115, 73, 27, 102, 64, 15, 89, 55, 0, 76, 45, 0, 62, 33, 0, 46, 18, 0, 26, 0, 0, 0, 0,
+ 43, 129, 69, 38, 115, 61, 30, 102, 54, 17, 89, 45, 2, 75, 34, 0, 61, 23, 0, 46, 9, 0, 26, 0, 0, 0, 0,
+ 1, 116, 188, 1, 104, 168, 0, 92, 151, 0, 79, 132, 0, 66, 113, 0, 52, 94, 0, 36, 75, 0, 14, 52, 0, 0, 14,
+ 17, 116, 171, 16, 104, 153, 14, 92, 137, 8, 79, 119, 0, 67, 102, 0, 53, 85, 0, 37, 67, 0, 16, 44, 0, 0, 4,
+ 31, 116, 155, 27, 104, 140, 21, 92, 125, 13, 79, 109, 3, 66, 93, 0, 53, 77, 0, 37, 59, 0, 16, 38, 0, 0, 0,
+ 37, 115, 141, 30, 103, 126, 26, 92, 112, 16, 79, 98, 5, 66, 83, 0, 53, 67, 0, 38, 51, 0, 17, 31, 0, 0, 0,
+ 41, 115, 126, 37, 103, 112, 31, 92, 100, 22, 79, 86, 10, 66, 72, 0, 53, 59, 0, 38, 44, 0, 17, 23, 0, 0, 0,
+ 48, 115, 111, 41, 102, 99, 34, 91, 88, 24, 78, 76, 14, 66, 63, 0, 53, 50, 0, 38, 36, 0, 18, 15, 0, 0, 0,
+ 51, 115, 95, 46, 102, 85, 37, 91, 74, 26, 78, 63, 16, 66, 52, 0, 53, 40, 0, 38, 26, 0, 18, 5, 0, 0, 0,
+ 55, 114, 80, 47, 102, 69, 40, 90, 60, 30, 78, 51, 19, 66, 41, 3, 53, 29, 0, 38, 15, 0, 17, 0, 0, 0, 0,
+ 56, 114, 66, 50, 102, 58, 40, 91, 50, 32, 78, 41, 18, 66, 32, 4, 53, 21, 0, 38, 5, 0, 17, 0, 0, 0, 0,
+ 39, 102, 178, 37, 90, 159, 30, 79, 142, 21, 68, 124, 14, 55, 106, 0, 42, 89, 0, 26, 70, 0, 4, 46, 0, 0, 8,
+ 48, 102, 161, 42, 90, 145, 35, 79, 128, 26, 68, 112, 19, 55, 96, 3, 43, 79, 0, 27, 62, 0, 6, 40, 0, 0, 0,
+ 50, 102, 147, 44, 90, 132, 37, 79, 118, 30, 68, 102, 20, 56, 87, 7, 43, 72, 0, 28, 55, 0, 6, 34, 0, 0, 0,
+ 53, 101, 133, 47, 90, 118, 41, 79, 106, 32, 68, 91, 21, 56, 78, 9, 43, 63, 0, 28, 47, 0, 6, 26, 0, 0, 0,
+ 57, 101, 119, 50, 89, 106, 42, 79, 94, 34, 67, 81, 24, 56, 68, 9, 44, 55, 0, 29, 40, 0, 6, 19, 0, 0, 0,
+ 60, 100, 105, 50, 90, 94, 45, 80, 83, 36, 68, 71, 24, 56, 59, 11, 44, 46, 0, 29, 32, 0, 7, 12, 0, 0, 0,
+ 63, 101, 91, 55, 90, 80, 46, 79, 70, 37, 68, 59, 26, 56, 49, 12, 44, 37, 1, 29, 23, 0, 7, 3, 0, 0, 0,
+ 64, 101, 75, 56, 89, 67, 48, 79, 57, 37, 68, 48, 27, 56, 37, 15, 44, 26, 0, 29, 12, 0, 7, 0, 0, 0, 0,
+ 66, 101, 64, 58, 89, 55, 49, 79, 47, 39, 68, 38, 27, 56, 29, 14, 44, 18, 1, 30, 2, 0, 7, 0, 0, 0, 0,
+ 57, 86, 165, 51, 75, 148, 45, 65, 133, 38, 54, 116, 28, 43, 100, 16, 29, 83, 0, 13, 64, 0, 0, 42, 0, 0, 3,
+ 60, 86, 151, 55, 75, 135, 47, 66, 121, 39, 55, 105, 30, 44, 90, 18, 31, 74, 3, 16, 57, 0, 1, 35, 0, 0, 0,
+ 62, 86, 139, 56, 75, 123, 49, 66, 110, 40, 55, 95, 30, 44, 81, 19, 31, 66, 4, 17, 51, 0, 1, 29, 0, 0, 0,
+ 65, 86, 125, 56, 76, 112, 49, 66, 99, 39, 55, 86, 31, 44, 72, 19, 32, 59, 5, 18, 44, 0, 1, 23, 0, 0, 0,
+ 67, 86, 113, 58, 75, 100, 51, 66, 88, 41, 56, 77, 31, 45, 64, 20, 32, 51, 6, 18, 35, 0, 1, 14, 0, 0, 0,
+ 69, 86, 99, 61, 76, 88, 52, 66, 78, 43, 56, 66, 32, 45, 55, 20, 33, 42, 7, 19, 27, 0, 1, 6, 0, 0, 0,
+ 69, 86, 86, 61, 76, 75, 53, 67, 66, 43, 56, 55, 33, 45, 45, 21, 34, 34, 8, 20, 20, 0, 2, 2, 0, 0, 0,
+ 71, 86, 72, 63, 75, 62, 54, 66, 55, 45, 56, 45, 33, 45, 35, 22, 34, 23, 7, 20, 8, 0, 2, 0, 0, 0, 0,
+ 71, 86, 62, 64, 75, 53, 55, 66, 46, 45, 56, 36, 33, 46, 27, 22, 34, 15, 8, 20, 0, 0, 2, 0, 0, 0, 0,
+ 69, 67, 156, 61, 58, 140, 53, 50, 125, 45, 39, 108, 35, 28, 93, 25, 12, 77, 12, 0, 59, 0, 0, 37, 0, 0, 0,
+ 71, 68, 142, 63, 59, 126, 56, 50, 114, 47, 40, 98, 37, 28, 84, 26, 15, 68, 12, 0, 53, 0, 0, 30, 0, 0, 0,
+ 72, 68, 130, 63, 59, 116, 56, 50, 104, 47, 40, 90, 38, 30, 75, 27, 16, 61, 13, 0, 46, 0, 0, 24, 0, 0, 0,
+ 73, 69, 118, 65, 59, 105, 57, 51, 92, 47, 41, 80, 37, 30, 67, 26, 18, 53, 14, 1, 39, 0, 0, 18, 0, 0, 0,
+ 74, 69, 106, 65, 60, 93, 57, 51, 82, 48, 41, 70, 38, 31, 59, 26, 19, 46, 13, 2, 32, 0, 0, 10, 0, 0, 0,
+ 76, 69, 95, 66, 61, 84, 58, 52, 73, 48, 42, 61, 37, 32, 50, 26, 20, 38, 14, 4, 24, 0, 0, 4, 0, 0, 0,
+ 76, 69, 81, 68, 60, 72, 58, 52, 62, 48, 42, 51, 38, 32, 41, 27, 21, 30, 14, 4, 16, 0, 0, 1, 0, 0, 0,
+ 76, 69, 68, 68, 61, 60, 60, 52, 51, 49, 43, 41, 38, 33, 32, 27, 21, 20, 14, 5, 5, 0, 0, 0, 0, 0, 0,
+ 78, 70, 59, 69, 61, 50, 60, 52, 42, 49, 43, 34, 39, 33, 24, 27, 22, 13, 14, 7, 1, 0, 0, 0, 0, 0, 0,
+ 75, 46, 146, 68, 38, 131, 60, 30, 117, 50, 19, 102, 41, 4, 87, 29, 0, 72, 13, 0, 55, 0, 0, 33, 0, 0, 0,
+ 78, 47, 132, 70, 39, 119, 61, 30, 105, 53, 20, 92, 42, 5, 78, 30, 0, 64, 13, 0, 49, 0, 0, 27, 0, 0, 0,
+ 79, 48, 122, 70, 40, 108, 62, 32, 96, 52, 22, 84, 42, 9, 71, 30, 0, 58, 14, 0, 42, 0, 0, 20, 0, 0, 0,
+ 79, 50, 111, 70, 42, 99, 62, 33, 88, 52, 23, 74, 41, 11, 63, 29, 0, 50, 14, 0, 36, 0, 0, 14, 0, 0, 0,
+ 80, 50, 99, 70, 42, 89, 61, 34, 78, 52, 25, 67, 41, 14, 55, 30, 0, 42, 15, 0, 28, 0, 0, 6, 0, 0, 0,
+ 81, 51, 89, 71, 43, 78, 62, 35, 69, 52, 25, 58, 42, 15, 47, 30, 3, 36, 15, 0, 22, 0, 0, 3, 0, 0, 0,
+ 81, 51, 77, 71, 44, 68, 63, 36, 59, 53, 26, 49, 41, 16, 38, 31, 4, 27, 16, 0, 12, 0, 0, 0, 0, 0, 0,
+ 81, 52, 65, 72, 43, 56, 63, 36, 48, 53, 27, 39, 41, 17, 29, 30, 4, 18, 14, 0, 3, 0, 0, 0, 0, 0, 0,
+ 81, 52, 55, 73, 44, 47, 64, 36, 39, 53, 28, 32, 42, 18, 21, 31, 6, 9, 14, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 174, 239, 0, 156, 214, 0, 139, 192, 0, 121, 168, 0, 105, 145, 0, 87, 123, 0, 68, 98, 0, 46, 70, 0, 3, 35,
+ 0, 172, 217, 0, 155, 194, 0, 139, 173, 0, 121, 152, 0, 104, 130, 0, 87, 110, 0, 69, 88, 0, 46, 63, 0, 4, 28,
+ 0, 171, 197, 0, 153, 175, 0, 138, 158, 0, 121, 139, 0, 103, 118, 0, 86, 100, 0, 68, 79, 0, 46, 55, 0, 4, 22,
+ 0, 170, 177, 0, 152, 158, 0, 136, 141, 0, 119, 124, 0, 103, 106, 0, 86, 88, 0, 68, 70, 0, 45, 47, 0, 3, 14,
+ 0, 169, 157, 0, 152, 141, 0, 136, 126, 0, 119, 109, 0, 102, 94, 0, 86, 78, 0, 68, 60, 0, 46, 39, 0, 3, 5,
+ 0, 167, 138, 0, 150, 124, 0, 135, 111, 0, 118, 97, 0, 102, 82, 0, 85, 68, 0, 68, 52, 0, 46, 31, 0, 3, 0,
+ 0, 167, 118, 0, 150, 104, 0, 135, 94, 0, 118, 81, 0, 101, 69, 0, 84, 56, 0, 67, 41, 0, 45, 21, 0, 3, 0,
+ 0, 166, 97, 0, 149, 87, 0, 134, 77, 0, 117, 67, 0, 101, 56, 0, 85, 44, 0, 67, 30, 0, 45, 10, 0, 3, 0,
+ 0, 165, 79, 0, 149, 73, 0, 133, 64, 0, 117, 56, 0, 101, 46, 0, 85, 34, 0, 68, 21, 0, 46, 1, 0, 3, 0,
+ 0, 158, 225, 0, 141, 201, 0, 126, 180, 0, 109, 158, 0, 94, 136, 0, 78, 114, 0, 60, 91, 0, 38, 66, 0, 0, 30,
+ 0, 156, 203, 0, 140, 183, 0, 125, 164, 0, 109, 143, 0, 94, 124, 0, 78, 104, 0, 61, 83, 0, 38, 57, 0, 0, 23,
+ 0, 156, 186, 0, 140, 166, 0, 125, 150, 0, 109, 130, 0, 93, 111, 0, 77, 93, 0, 60, 74, 0, 38, 50, 0, 0, 17,
+ 0, 155, 167, 0, 138, 149, 0, 124, 134, 0, 109, 117, 0, 93, 100, 0, 76, 83, 0, 60, 65, 0, 38, 43, 0, 0, 9,
+ 0, 153, 147, 0, 138, 134, 0, 124, 120, 0, 107, 103, 0, 92, 88, 0, 77, 73, 0, 60, 56, 0, 38, 35, 0, 0, 0,
+ 0, 153, 131, 0, 137, 118, 0, 122, 105, 0, 107, 90, 0, 91, 76, 0, 76, 63, 0, 60, 47, 0, 39, 28, 0, 0, 0,
+ 0, 153, 111, 0, 136, 100, 0, 123, 90, 0, 107, 77, 0, 92, 65, 0, 76, 52, 0, 60, 37, 0, 38, 18, 0, 0, 0,
+ 0, 152, 93, 0, 136, 82, 0, 122, 74, 0, 106, 63, 0, 91, 52, 0, 76, 40, 0, 59, 26, 0, 38, 6, 0, 0, 0,
+ 0, 151, 78, 0, 136, 69, 0, 121, 61, 0, 106, 52, 0, 91, 43, 0, 76, 32, 0, 59, 17, 0, 38, 0, 0, 0, 0,
+ 0, 143, 213, 0, 128, 191, 0, 115, 171, 0, 100, 149, 0, 84, 128, 0, 69, 108, 0, 52, 86, 0, 30, 61, 0, 0, 25,
+ 0, 142, 193, 0, 127, 173, 0, 114, 154, 0, 99, 134, 0, 84, 116, 0, 69, 98, 0, 52, 77, 0, 31, 53, 0, 0, 18,
+ 0, 141, 176, 0, 127, 158, 0, 114, 141, 0, 98, 122, 0, 84, 105, 0, 69, 88, 0, 53, 69, 0, 31, 46, 0, 0, 9,
+ 0, 141, 159, 0, 126, 142, 0, 113, 127, 0, 98, 110, 0, 83, 95, 0, 69, 78, 0, 53, 60, 0, 32, 39, 0, 0, 2,
+ 0, 140, 140, 0, 126, 126, 0, 112, 112, 0, 98, 98, 0, 83, 83, 0, 68, 69, 0, 52, 52, 0, 31, 31, 0, 0, 0,
+ 0, 140, 124, 0, 125, 112, 0, 112, 100, 0, 97, 86, 0, 83, 72, 0, 68, 59, 0, 52, 44, 0, 31, 23, 0, 0, 0,
+ 0, 139, 106, 0, 125, 96, 0, 111, 85, 0, 97, 72, 0, 83, 62, 0, 68, 49, 0, 52, 35, 0, 31, 15, 0, 0, 0,
+ 0, 138, 88, 0, 124, 79, 0, 111, 70, 0, 96, 59, 0, 82, 48, 0, 68, 38, 0, 52, 24, 0, 31, 4, 0, 0, 0,
+ 0, 139, 76, 0, 124, 66, 0, 111, 58, 0, 96, 50, 0, 82, 40, 0, 68, 29, 0, 52, 15, 0, 31, 0, 0, 0, 0,
+ 0, 129, 200, 0, 114, 179, 0, 102, 160, 0, 87, 139, 0, 74, 120, 0, 60, 101, 0, 44, 81, 0, 22, 56, 0, 0, 19,
+ 0, 127, 181, 0, 114, 163, 0, 102, 146, 0, 88, 127, 0, 74, 109, 0, 60, 91, 0, 44, 72, 0, 23, 48, 0, 0, 11,
+ 0, 127, 166, 0, 113, 148, 0, 101, 133, 0, 87, 115, 0, 74, 99, 0, 60, 82, 0, 44, 64, 0, 23, 42, 0, 0, 4,
+ 0, 127, 150, 0, 113, 134, 0, 101, 119, 0, 87, 104, 0, 74, 89, 0, 60, 73, 0, 44, 56, 0, 23, 35, 0, 0, 0,
+ 0, 125, 134, 0, 112, 118, 0, 100, 106, 0, 87, 92, 0, 73, 78, 0, 60, 64, 0, 44, 48, 0, 23, 27, 0, 0, 0,
+ 0, 125, 118, 0, 112, 105, 0, 100, 94, 0, 86, 80, 0, 73, 68, 0, 60, 54, 0, 44, 39, 0, 23, 20, 0, 0, 0,
+ 0, 125, 101, 0, 111, 90, 0, 99, 80, 0, 86, 69, 0, 73, 58, 0, 59, 45, 0, 44, 30, 0, 23, 11, 0, 0, 0,
+ 0, 124, 85, 0, 111, 75, 0, 99, 66, 0, 86, 56, 0, 73, 45, 0, 59, 34, 0, 44, 20, 0, 23, 1, 0, 0, 0,
+ 0, 125, 72, 0, 111, 62, 0, 99, 56, 0, 86, 46, 0, 73, 36, 0, 60, 26, 0, 44, 12, 0, 23, 0, 0, 0, 0,
+ 0, 114, 188, 0, 101, 167, 0, 89, 150, 0, 77, 131, 0, 64, 113, 0, 50, 95, 0, 34, 75, 0, 12, 52, 0, 0, 14,
+ 0, 113, 170, 0, 101, 153, 0, 89, 137, 0, 77, 120, 0, 64, 102, 0, 50, 85, 0, 35, 67, 0, 12, 44, 0, 0, 4,
+ 0, 113, 156, 0, 100, 139, 0, 89, 125, 0, 77, 109, 0, 64, 92, 0, 51, 77, 0, 35, 60, 0, 12, 38, 0, 0, 0,
+ 0, 112, 141, 0, 100, 126, 0, 89, 113, 0, 77, 98, 0, 64, 83, 0, 51, 68, 0, 35, 51, 0, 12, 30, 0, 0, 0,
+ 0, 112, 127, 0, 100, 112, 0, 89, 100, 0, 76, 87, 0, 64, 74, 0, 51, 59, 0, 35, 44, 0, 13, 24, 0, 0, 0,
+ 0, 112, 111, 0, 100, 100, 0, 88, 88, 0, 76, 76, 0, 64, 64, 0, 51, 52, 0, 36, 37, 0, 13, 17, 0, 0, 0,
+ 0, 111, 96, 0, 99, 85, 0, 88, 76, 0, 76, 64, 0, 64, 53, 0, 51, 41, 0, 36, 27, 0, 13, 6, 0, 0, 0,
+ 0, 111, 81, 0, 99, 71, 0, 88, 62, 0, 76, 52, 0, 64, 43, 0, 51, 31, 0, 36, 17, 0, 13, 0, 0, 0, 0,
+ 0, 111, 69, 0, 99, 60, 0, 88, 52, 0, 75, 43, 0, 63, 34, 0, 51, 21, 0, 36, 7, 0, 13, 0, 0, 0, 0,
+ 0, 99, 177, 0, 88, 158, 0, 77, 141, 0, 66, 123, 0, 53, 106, 0, 40, 89, 0, 25, 71, 0, 5, 47, 0, 0, 8,
+ 0, 99, 160, 0, 88, 144, 0, 77, 129, 0, 66, 112, 0, 54, 97, 0, 41, 80, 0, 26, 62, 0, 5, 40, 0, 0, 0,
+ 0, 99, 147, 0, 87, 132, 0, 78, 117, 0, 66, 102, 0, 54, 87, 0, 42, 72, 0, 26, 55, 0, 5, 34, 0, 0, 0,
+ 0, 99, 134, 0, 88, 119, 0, 77, 107, 0, 66, 92, 0, 54, 78, 0, 42, 64, 0, 27, 48, 0, 5, 27, 0, 0, 0,
+ 0, 99, 120, 0, 87, 107, 0, 78, 94, 0, 66, 81, 0, 54, 68, 0, 42, 55, 0, 27, 40, 0, 6, 20, 0, 0, 0,
+ 0, 98, 105, 0, 87, 94, 0, 77, 84, 0, 65, 71, 0, 55, 59, 0, 42, 47, 0, 28, 33, 0, 6, 12, 0, 0, 0,
+ 0, 98, 93, 0, 87, 81, 0, 77, 72, 0, 66, 61, 0, 54, 49, 0, 42, 37, 0, 28, 24, 0, 6, 4, 0, 0, 0,
+ 0, 98, 77, 0, 87, 68, 0, 77, 59, 0, 65, 49, 0, 54, 39, 0, 42, 27, 0, 29, 14, 0, 6, 0, 0, 0, 0,
+ 1, 98, 65, 7, 87, 56, 0, 77, 49, 0, 66, 41, 0, 54, 30, 0, 42, 19, 0, 29, 3, 0, 6, 0, 0, 0, 0,
+ 0, 84, 166, 0, 74, 149, 0, 64, 134, 0, 53, 117, 0, 41, 100, 0, 28, 83, 0, 11, 64, 0, 0, 42, 0, 0, 3,
+ 0, 84, 151, 0, 74, 135, 0, 64, 121, 0, 53, 105, 0, 42, 90, 0, 30, 75, 0, 14, 58, 0, 0, 36, 0, 0, 0,
+ 0, 84, 138, 0, 74, 124, 1, 64, 110, 0, 54, 95, 0, 43, 81, 0, 30, 67, 0, 15, 51, 0, 1, 29, 0, 0, 0,
+ 14, 84, 126, 12, 74, 112, 2, 65, 99, 0, 54, 85, 0, 44, 73, 0, 31, 59, 0, 16, 44, 0, 1, 23, 0, 0, 0,
+ 16, 84, 113, 13, 74, 100, 6, 65, 89, 0, 54, 77, 0, 44, 65, 0, 31, 51, 0, 17, 36, 0, 1, 16, 0, 0, 0,
+ 24, 84, 100, 18, 74, 88, 13, 65, 78, 2, 55, 68, 0, 44, 55, 0, 32, 43, 0, 18, 28, 0, 1, 6, 0, 0, 0,
+ 26, 84, 87, 24, 74, 76, 17, 65, 67, 7, 54, 57, 0, 44, 46, 0, 32, 35, 0, 19, 21, 0, 2, 3, 0, 0, 0,
+ 30, 84, 74, 28, 74, 64, 20, 65, 55, 12, 55, 46, 0, 44, 35, 0, 32, 24, 0, 18, 9, 0, 1, 0, 0, 0, 0,
+ 32, 84, 63, 28, 74, 54, 21, 65, 47, 13, 54, 38, 0, 44, 28, 0, 32, 16, 0, 18, 1, 0, 1, 0, 0, 0, 0,
+ 30, 67, 155, 20, 58, 139, 20, 49, 126, 12, 39, 110, 0, 27, 94, 0, 13, 77, 0, 0, 60, 0, 0, 37, 0, 0, 0,
+ 35, 67, 142, 30, 58, 126, 23, 50, 114, 16, 40, 99, 7, 29, 85, 0, 15, 69, 0, 0, 52, 0, 0, 30, 0, 0, 0,
+ 35, 68, 131, 30, 59, 116, 27, 50, 104, 18, 40, 90, 9, 29, 76, 0, 17, 62, 0, 2, 46, 0, 0, 24, 0, 0, 0,
+ 37, 69, 119, 33, 59, 106, 27, 51, 94, 21, 41, 80, 9, 30, 67, 0, 18, 54, 0, 3, 39, 0, 0, 18, 0, 0, 0,
+ 40, 69, 107, 36, 59, 94, 28, 51, 84, 18, 41, 72, 10, 31, 60, 0, 19, 47, 0, 4, 32, 0, 0, 10, 0, 0, 0,
+ 42, 69, 95, 36, 59, 84, 29, 51, 74, 19, 41, 63, 10, 31, 52, 0, 20, 39, 0, 4, 25, 0, 0, 4, 0, 0, 0,
+ 43, 69, 83, 38, 60, 73, 32, 51, 62, 23, 42, 53, 11, 31, 42, 0, 20, 31, 0, 5, 17, 0, 0, 1, 0, 0, 0,
+ 45, 69, 70, 39, 60, 60, 33, 51, 52, 24, 42, 43, 13, 32, 33, 0, 21, 21, 0, 5, 6, 0, 0, 0, 0, 0, 0,
+ 47, 69, 59, 41, 60, 51, 34, 51, 43, 24, 42, 35, 12, 33, 26, 1, 22, 14, 0, 5, 1, 0, 0, 0, 0, 0, 0,
+ 46, 48, 146, 42, 40, 131, 36, 32, 118, 27, 22, 103, 17, 6, 88, 5, 0, 73, 0, 0, 55, 0, 0, 33, 0, 0, 0,
+ 48, 48, 133, 44, 40, 119, 37, 32, 107, 28, 22, 93, 20, 8, 79, 7, 0, 65, 0, 0, 49, 0, 0, 27, 0, 0, 0,
+ 48, 50, 123, 44, 41, 109, 37, 33, 97, 30, 23, 83, 21, 11, 71, 8, 0, 58, 0, 0, 42, 0, 0, 21, 0, 0, 0,
+ 49, 51, 111, 45, 42, 99, 38, 34, 87, 29, 25, 75, 20, 13, 63, 8, 0, 51, 0, 0, 36, 0, 0, 14, 0, 0, 0,
+ 52, 52, 100, 44, 43, 89, 38, 35, 79, 29, 26, 68, 19, 15, 56, 10, 1, 43, 0, 0, 28, 0, 0, 6, 0, 0, 0,
+ 52, 52, 90, 47, 44, 79, 39, 36, 70, 30, 27, 59, 20, 16, 47, 9, 2, 36, 0, 0, 22, 0, 0, 2, 0, 0, 0,
+ 52, 53, 78, 46, 44, 68, 39, 37, 60, 32, 27, 49, 22, 17, 39, 10, 3, 28, 0, 0, 12, 0, 0, 0, 0, 0, 0,
+ 53, 53, 66, 47, 44, 57, 40, 36, 48, 32, 27, 39, 22, 18, 30, 9, 4, 18, 0, 0, 3, 0, 0, 0, 0, 0, 0,
+ 54, 53, 57, 48, 45, 49, 41, 37, 41, 33, 28, 32, 22, 19, 23, 11, 6, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0,
+};
+void AdobeCMYK_to_sRGB1(FX_BYTE c, FX_BYTE m, FX_BYTE y, FX_BYTE k, FX_BYTE& R, FX_BYTE& G, FX_BYTE& B)
+{
+ int fix_c = c << 8;
+ int fix_m = m << 8;
+ int fix_y = y << 8;
+ int fix_k = k << 8;
+ int c_index = (fix_c + 4096) >> 13;
+ int m_index = (fix_m + 4096) >> 13;
+ int y_index = (fix_y + 4096) >> 13;
+ int k_index = (fix_k + 4096) >> 13;
+ int pos = (c_index * 9 * 9 * 9 + m_index * 9 * 9 + y_index * 9 + k_index) * 3;
+ int fix_r = g_CMYKSamples[pos] << 8;
+ int fix_g = g_CMYKSamples[pos + 1] << 8;
+ int fix_b = g_CMYKSamples[pos + 2] << 8;
+ int c1_index = fix_c >> 13;
+ if (c1_index == c_index) {
+ c1_index = c1_index == 8 ? c1_index - 1 : c1_index + 1;
+ }
+ int m1_index = fix_m >> 13;
+ if (m1_index == m_index) {
+ m1_index = m1_index == 8 ? m1_index - 1 : m1_index + 1;
+ }
+ int y1_index = fix_y >> 13;
+ if (y1_index == y_index) {
+ y1_index = y1_index == 8 ? y1_index - 1 : y1_index + 1;
+ }
+ int k1_index = fix_k >> 13;
+ if (k1_index == k_index) {
+ k1_index = k1_index == 8 ? k1_index - 1 : k1_index + 1;
+ }
+ int c1_pos = pos + (c1_index - c_index) * 9 * 9 * 9 * 3;
+ int m1_pos = pos + (m1_index - m_index) * 9 * 9 * 3;
+ int y1_pos = pos + (y1_index - y_index) * 9 * 3;
+ int k1_pos = pos + (k1_index - k_index) * 3;
+ int c_r_delta = g_CMYKSamples[pos] - g_CMYKSamples[c1_pos];
+ int c_g_delta = g_CMYKSamples[pos + 1] - g_CMYKSamples[c1_pos + 1];
+ int c_b_delta = g_CMYKSamples[pos + 2] - g_CMYKSamples[c1_pos + 2];
+ int m_r_delta = g_CMYKSamples[pos] - g_CMYKSamples[m1_pos];
+ int m_g_delta = g_CMYKSamples[pos + 1] - g_CMYKSamples[m1_pos + 1];
+ int m_b_delta = g_CMYKSamples[pos + 2] - g_CMYKSamples[m1_pos + 2];
+ int y_r_delta = g_CMYKSamples[pos] - g_CMYKSamples[y1_pos];
+ int y_g_delta = g_CMYKSamples[pos + 1] - g_CMYKSamples[y1_pos + 1];
+ int y_b_delta = g_CMYKSamples[pos + 2] - g_CMYKSamples[y1_pos + 2];
+ int k_r_delta = g_CMYKSamples[pos] - g_CMYKSamples[k1_pos];
+ int k_g_delta = g_CMYKSamples[pos + 1] - g_CMYKSamples[k1_pos + 1];
+ int k_b_delta = g_CMYKSamples[pos + 2] - g_CMYKSamples[k1_pos + 2];
+ int c_rate = (fix_c - (c_index << 13)) * (c_index - c1_index);
+ fix_r += c_r_delta * c_rate / 32;
+ fix_g += c_g_delta * c_rate / 32;
+ fix_b += c_b_delta * c_rate / 32;
+ int m_rate = (fix_m - (m_index << 13)) * (m_index - m1_index);
+ fix_r += m_r_delta * m_rate / 32;
+ fix_g += m_g_delta * m_rate / 32;
+ fix_b += m_b_delta * m_rate / 32;
+ int y_rate = (fix_y - (y_index << 13)) * (y_index - y1_index);
+ fix_r += y_r_delta * y_rate / 32;
+ fix_g += y_g_delta * y_rate / 32;
+ fix_b += y_b_delta * y_rate / 32;
+ int k_rate = (fix_k - (k_index << 13)) * (k_index - k1_index);
+ fix_r += k_r_delta * k_rate / 32;
+ fix_g += k_g_delta * k_rate / 32;
+ fix_b += k_b_delta * k_rate / 32;
+ if (fix_r < 0) {
+ fix_r = 0;
+ }
+ if (fix_g < 0) {
+ fix_g = 0;
+ }
+ if (fix_b < 0) {
+ fix_b = 0;
+ }
+ R = fix_r >> 8;
+ G = fix_g >> 8;
+ B = fix_b >> 8;
+}
+void AdobeCMYK_to_sRGB(FX_FLOAT c, FX_FLOAT m, FX_FLOAT y, FX_FLOAT k, FX_FLOAT& R, FX_FLOAT& G, FX_FLOAT& B)
+{
+ FX_BYTE c1 = FXSYS_round(c * 255);
+ FX_BYTE m1 = FXSYS_round(m * 255);
+ FX_BYTE y1 = FXSYS_round(y * 255);
+ FX_BYTE k1 = FXSYS_round(k * 255);
+ FX_BYTE r, g, b;
+ AdobeCMYK_to_sRGB1(c1, m1, y1, k1, r, g, b);
+ R = 1.0f * r / 255;
+ G = 1.0f * g / 255;
+ B = 1.0f * b / 255;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_jbig.cpp b/core/src/fxcodec/codec/fx_codec_jbig.cpp
index a644e99c5d..996ad60f8a 100644
--- a/core/src/fxcodec/codec/fx_codec_jbig.cpp
+++ b/core/src/fxcodec/codec/fx_codec_jbig.cpp
@@ -1,212 +1,212 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../../include/fxcodec/fx_codec.h"
-#include "codec_int.h"
-CCodec_Jbig2Context::CCodec_Jbig2Context()
-{
- FXSYS_memset32(this, 0, sizeof(CCodec_Jbig2Context));
-}
-CCodec_Jbig2Module::~CCodec_Jbig2Module()
-{
-}
-void* CCodec_Jbig2Module::CreateJbig2Context()
-{
- return FX_NEW CCodec_Jbig2Context();
-}
-void CCodec_Jbig2Module::DestroyJbig2Context(void* pJbig2Content)
-{
- if(pJbig2Content) {
- CJBig2_Context::DestroyContext(((CCodec_Jbig2Context*)pJbig2Content)->m_pContext);
- delete (CCodec_Jbig2Context*)pJbig2Content;
- }
- pJbig2Content = NULL;
-}
-FX_BOOL CCodec_Jbig2Module::Decode(FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size,
- FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch)
-{
- FXSYS_memset32(dest_buf, 0, height * dest_pitch);
- CJBig2_Context* pContext = CJBig2_Context::CreateContext(&m_Module,
- (FX_LPBYTE)global_data, global_size, (FX_LPBYTE)src_buf, src_size, JBIG2_EMBED_STREAM);
- if (pContext == NULL) {
- return FALSE;
- }
- int ret = pContext->getFirstPage(dest_buf, width, height, dest_pitch, NULL);
- CJBig2_Context::DestroyContext(pContext);
- if (ret != JBIG2_SUCCESS) {
- return FALSE;
- }
- int dword_size = height * dest_pitch / 4;
- FX_DWORD* dword_buf = (FX_DWORD*)dest_buf;
- for (int i = 0; i < dword_size; i ++) {
- dword_buf[i] = ~dword_buf[i];
- }
- return TRUE;
-}
-FX_BOOL CCodec_Jbig2Module::Decode(IFX_FileRead* file_ptr,
- FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf)
-{
- CJBig2_Context* pContext = NULL;
- CJBig2_Image* dest_image = NULL;
- FX_DWORD src_size = (FX_DWORD)file_ptr->GetSize();
- FX_LPBYTE src_buf = FX_Alloc(FX_BYTE, src_size);
- if (src_buf == NULL) {
- return FALSE;
- }
- int ret = 0;
- if(!file_ptr->ReadBlock(src_buf, 0, src_size)) {
- goto failed;
- }
- pContext = CJBig2_Context::CreateContext(&m_Module, NULL, 0, src_buf, src_size, JBIG2_FILE_STREAM);
- if(pContext == NULL) {
- goto failed;
- }
- ret = pContext->getFirstPage(&dest_image, NULL);
- CJBig2_Context::DestroyContext(pContext);
- if (ret != JBIG2_SUCCESS) {
- goto failed;
- }
- width = (FX_DWORD)dest_image->m_nWidth;
- height = (FX_DWORD)dest_image->m_nHeight;
- pitch = (FX_DWORD)dest_image->m_nStride;
- dest_buf = dest_image->m_pData;
- dest_image->m_bNeedFree = FALSE;
- delete dest_image;
- FX_Free(src_buf);
- return TRUE;
-failed:
- if(src_buf) {
- FX_Free(src_buf);
- }
- return FALSE;
-}
-FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context, FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size,
- FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch, IFX_Pause* pPause)
-{
- if(!pJbig2Context) {
- return FXCODEC_STATUS_ERR_PARAMS;
- }
- CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
- m_pJbig2Context->m_width = width;
- m_pJbig2Context->m_height = height;
- m_pJbig2Context->m_src_buf = (unsigned char *)src_buf;
- m_pJbig2Context->m_src_size = src_size;
- m_pJbig2Context->m_global_data = global_data;
- m_pJbig2Context->m_global_size = global_size;
- m_pJbig2Context->m_dest_buf = dest_buf;
- m_pJbig2Context->m_dest_pitch = dest_pitch;
- m_pJbig2Context->m_pPause = pPause;
- m_pJbig2Context->m_bFileReader = FALSE;
- FXSYS_memset32(dest_buf, 0, height * dest_pitch);
- m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext(&m_Module,
- (FX_LPBYTE)global_data, global_size, (FX_LPBYTE)src_buf, src_size, JBIG2_EMBED_STREAM, pPause);
- if(!m_pJbig2Context->m_pContext) {
- return FXCODEC_STATUS_ERROR;
- }
- int ret = m_pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height, dest_pitch, pPause);
- if(m_pJbig2Context->m_pContext->GetProcessiveStatus() == FXCODEC_STATUS_DECODE_FINISH) {
- CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
- m_pJbig2Context->m_pContext = NULL;
- if (ret != JBIG2_SUCCESS) {
- return FXCODEC_STATUS_ERROR;
- }
- int dword_size = height * dest_pitch / 4;
- FX_DWORD* dword_buf = (FX_DWORD*)dest_buf;
- for (int i = 0; i < dword_size; i ++) {
- dword_buf[i] = ~dword_buf[i];
- }
- return FXCODEC_STATUS_DECODE_FINISH;
- }
- return m_pJbig2Context->m_pContext->GetProcessiveStatus();
-}
-FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context, IFX_FileRead* file_ptr,
- FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf, IFX_Pause* pPause)
-{
- if(!pJbig2Context) {
- return FXCODEC_STATUS_ERR_PARAMS;
- }
- CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
- m_pJbig2Context->m_bFileReader = TRUE;
- m_pJbig2Context->m_dest_image = NULL;
- m_pJbig2Context->m_src_size = (FX_DWORD)file_ptr->GetSize();
- m_pJbig2Context->m_src_buf = FX_Alloc(FX_BYTE, m_pJbig2Context->m_src_size);
- if (m_pJbig2Context->m_src_buf == NULL) {
- return FXCODEC_STATUS_ERR_MEMORY;
- }
- int ret = 0;
- if(!file_ptr->ReadBlock((void*)m_pJbig2Context->m_src_buf, 0, m_pJbig2Context->m_src_size)) {
- goto failed;
- }
- m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext(&m_Module, NULL, 0, m_pJbig2Context->m_src_buf, m_pJbig2Context->m_src_size, JBIG2_FILE_STREAM, pPause);
- if(m_pJbig2Context->m_pContext == NULL) {
- goto failed;
- }
- ret = m_pJbig2Context->m_pContext->getFirstPage(&m_pJbig2Context->m_dest_image, pPause);
- if(m_pJbig2Context->m_pContext->GetProcessiveStatus() == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- width = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nWidth;
- height = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nHeight;
- pitch = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nStride;
- dest_buf = m_pJbig2Context->m_dest_image->m_pData;
- m_pJbig2Context->m_dest_image->m_bNeedFree = FALSE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
- m_pJbig2Context->m_pContext = NULL;
- if (ret != JBIG2_SUCCESS) {
- goto failed;
- }
- width = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nWidth;
- height = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nHeight;
- pitch = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nStride;
- dest_buf = m_pJbig2Context->m_dest_image->m_pData;
- m_pJbig2Context->m_dest_image->m_bNeedFree = FALSE;
- delete m_pJbig2Context->m_dest_image;
- FX_Free(m_pJbig2Context->m_src_buf);
- return FXCODEC_STATUS_DECODE_FINISH;
-failed:
- if(m_pJbig2Context->m_src_buf) {
- FX_Free(m_pJbig2Context->m_src_buf);
- }
- m_pJbig2Context->m_src_buf = NULL;
- return FXCODEC_STATUS_ERROR;
-}
-FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(void* pJbig2Context, IFX_Pause* pPause)
-{
- CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
- int ret = m_pJbig2Context->m_pContext->Continue(pPause);
- if(m_pJbig2Context->m_pContext->GetProcessiveStatus() == FXCODEC_STATUS_DECODE_FINISH) {
- if(m_pJbig2Context->m_bFileReader) {
- CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
- m_pJbig2Context->m_pContext = NULL;
- if (ret != JBIG2_SUCCESS) {
- if(m_pJbig2Context->m_src_buf) {
- FX_Free(m_pJbig2Context->m_src_buf);
- }
- m_pJbig2Context->m_src_buf = NULL;
- return FXCODEC_STATUS_ERROR;
- }
- delete m_pJbig2Context->m_dest_image;
- FX_Free(m_pJbig2Context->m_src_buf);
- return FXCODEC_STATUS_DECODE_FINISH;
- } else {
- CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
- m_pJbig2Context->m_pContext = NULL;
- if (ret != JBIG2_SUCCESS) {
- return FXCODEC_STATUS_ERROR;
- }
- int dword_size = m_pJbig2Context->m_height * m_pJbig2Context->m_dest_pitch / 4;
- FX_DWORD* dword_buf = (FX_DWORD*)m_pJbig2Context->m_dest_buf;
- for (int i = 0; i < dword_size; i ++) {
- dword_buf[i] = ~dword_buf[i];
- }
- return FXCODEC_STATUS_DECODE_FINISH;
- }
- }
- return m_pJbig2Context->m_pContext->GetProcessiveStatus();
-}
-
-
-
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../../include/fxcodec/fx_codec.h"
+#include "codec_int.h"
+CCodec_Jbig2Context::CCodec_Jbig2Context()
+{
+ FXSYS_memset32(this, 0, sizeof(CCodec_Jbig2Context));
+}
+CCodec_Jbig2Module::~CCodec_Jbig2Module()
+{
+}
+void* CCodec_Jbig2Module::CreateJbig2Context()
+{
+ return FX_NEW CCodec_Jbig2Context();
+}
+void CCodec_Jbig2Module::DestroyJbig2Context(void* pJbig2Content)
+{
+ if(pJbig2Content) {
+ CJBig2_Context::DestroyContext(((CCodec_Jbig2Context*)pJbig2Content)->m_pContext);
+ delete (CCodec_Jbig2Context*)pJbig2Content;
+ }
+ pJbig2Content = NULL;
+}
+FX_BOOL CCodec_Jbig2Module::Decode(FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size,
+ FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch)
+{
+ FXSYS_memset32(dest_buf, 0, height * dest_pitch);
+ CJBig2_Context* pContext = CJBig2_Context::CreateContext(&m_Module,
+ (FX_LPBYTE)global_data, global_size, (FX_LPBYTE)src_buf, src_size, JBIG2_EMBED_STREAM);
+ if (pContext == NULL) {
+ return FALSE;
+ }
+ int ret = pContext->getFirstPage(dest_buf, width, height, dest_pitch, NULL);
+ CJBig2_Context::DestroyContext(pContext);
+ if (ret != JBIG2_SUCCESS) {
+ return FALSE;
+ }
+ int dword_size = height * dest_pitch / 4;
+ FX_DWORD* dword_buf = (FX_DWORD*)dest_buf;
+ for (int i = 0; i < dword_size; i ++) {
+ dword_buf[i] = ~dword_buf[i];
+ }
+ return TRUE;
+}
+FX_BOOL CCodec_Jbig2Module::Decode(IFX_FileRead* file_ptr,
+ FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf)
+{
+ CJBig2_Context* pContext = NULL;
+ CJBig2_Image* dest_image = NULL;
+ FX_DWORD src_size = (FX_DWORD)file_ptr->GetSize();
+ FX_LPBYTE src_buf = FX_Alloc(FX_BYTE, src_size);
+ if (src_buf == NULL) {
+ return FALSE;
+ }
+ int ret = 0;
+ if(!file_ptr->ReadBlock(src_buf, 0, src_size)) {
+ goto failed;
+ }
+ pContext = CJBig2_Context::CreateContext(&m_Module, NULL, 0, src_buf, src_size, JBIG2_FILE_STREAM);
+ if(pContext == NULL) {
+ goto failed;
+ }
+ ret = pContext->getFirstPage(&dest_image, NULL);
+ CJBig2_Context::DestroyContext(pContext);
+ if (ret != JBIG2_SUCCESS) {
+ goto failed;
+ }
+ width = (FX_DWORD)dest_image->m_nWidth;
+ height = (FX_DWORD)dest_image->m_nHeight;
+ pitch = (FX_DWORD)dest_image->m_nStride;
+ dest_buf = dest_image->m_pData;
+ dest_image->m_bNeedFree = FALSE;
+ delete dest_image;
+ FX_Free(src_buf);
+ return TRUE;
+failed:
+ if(src_buf) {
+ FX_Free(src_buf);
+ }
+ return FALSE;
+}
+FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context, FX_DWORD width, FX_DWORD height, FX_LPCBYTE src_buf, FX_DWORD src_size,
+ FX_LPCBYTE global_data, FX_DWORD global_size, FX_LPBYTE dest_buf, FX_DWORD dest_pitch, IFX_Pause* pPause)
+{
+ if(!pJbig2Context) {
+ return FXCODEC_STATUS_ERR_PARAMS;
+ }
+ CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
+ m_pJbig2Context->m_width = width;
+ m_pJbig2Context->m_height = height;
+ m_pJbig2Context->m_src_buf = (unsigned char *)src_buf;
+ m_pJbig2Context->m_src_size = src_size;
+ m_pJbig2Context->m_global_data = global_data;
+ m_pJbig2Context->m_global_size = global_size;
+ m_pJbig2Context->m_dest_buf = dest_buf;
+ m_pJbig2Context->m_dest_pitch = dest_pitch;
+ m_pJbig2Context->m_pPause = pPause;
+ m_pJbig2Context->m_bFileReader = FALSE;
+ FXSYS_memset32(dest_buf, 0, height * dest_pitch);
+ m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext(&m_Module,
+ (FX_LPBYTE)global_data, global_size, (FX_LPBYTE)src_buf, src_size, JBIG2_EMBED_STREAM, pPause);
+ if(!m_pJbig2Context->m_pContext) {
+ return FXCODEC_STATUS_ERROR;
+ }
+ int ret = m_pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height, dest_pitch, pPause);
+ if(m_pJbig2Context->m_pContext->GetProcessiveStatus() == FXCODEC_STATUS_DECODE_FINISH) {
+ CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
+ m_pJbig2Context->m_pContext = NULL;
+ if (ret != JBIG2_SUCCESS) {
+ return FXCODEC_STATUS_ERROR;
+ }
+ int dword_size = height * dest_pitch / 4;
+ FX_DWORD* dword_buf = (FX_DWORD*)dest_buf;
+ for (int i = 0; i < dword_size; i ++) {
+ dword_buf[i] = ~dword_buf[i];
+ }
+ return FXCODEC_STATUS_DECODE_FINISH;
+ }
+ return m_pJbig2Context->m_pContext->GetProcessiveStatus();
+}
+FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context, IFX_FileRead* file_ptr,
+ FX_DWORD& width, FX_DWORD& height, FX_DWORD& pitch, FX_LPBYTE& dest_buf, IFX_Pause* pPause)
+{
+ if(!pJbig2Context) {
+ return FXCODEC_STATUS_ERR_PARAMS;
+ }
+ CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
+ m_pJbig2Context->m_bFileReader = TRUE;
+ m_pJbig2Context->m_dest_image = NULL;
+ m_pJbig2Context->m_src_size = (FX_DWORD)file_ptr->GetSize();
+ m_pJbig2Context->m_src_buf = FX_Alloc(FX_BYTE, m_pJbig2Context->m_src_size);
+ if (m_pJbig2Context->m_src_buf == NULL) {
+ return FXCODEC_STATUS_ERR_MEMORY;
+ }
+ int ret = 0;
+ if(!file_ptr->ReadBlock((void*)m_pJbig2Context->m_src_buf, 0, m_pJbig2Context->m_src_size)) {
+ goto failed;
+ }
+ m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext(&m_Module, NULL, 0, m_pJbig2Context->m_src_buf, m_pJbig2Context->m_src_size, JBIG2_FILE_STREAM, pPause);
+ if(m_pJbig2Context->m_pContext == NULL) {
+ goto failed;
+ }
+ ret = m_pJbig2Context->m_pContext->getFirstPage(&m_pJbig2Context->m_dest_image, pPause);
+ if(m_pJbig2Context->m_pContext->GetProcessiveStatus() == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ width = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nWidth;
+ height = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nHeight;
+ pitch = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nStride;
+ dest_buf = m_pJbig2Context->m_dest_image->m_pData;
+ m_pJbig2Context->m_dest_image->m_bNeedFree = FALSE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
+ m_pJbig2Context->m_pContext = NULL;
+ if (ret != JBIG2_SUCCESS) {
+ goto failed;
+ }
+ width = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nWidth;
+ height = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nHeight;
+ pitch = (FX_DWORD)m_pJbig2Context->m_dest_image->m_nStride;
+ dest_buf = m_pJbig2Context->m_dest_image->m_pData;
+ m_pJbig2Context->m_dest_image->m_bNeedFree = FALSE;
+ delete m_pJbig2Context->m_dest_image;
+ FX_Free(m_pJbig2Context->m_src_buf);
+ return FXCODEC_STATUS_DECODE_FINISH;
+failed:
+ if(m_pJbig2Context->m_src_buf) {
+ FX_Free(m_pJbig2Context->m_src_buf);
+ }
+ m_pJbig2Context->m_src_buf = NULL;
+ return FXCODEC_STATUS_ERROR;
+}
+FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(void* pJbig2Context, IFX_Pause* pPause)
+{
+ CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
+ int ret = m_pJbig2Context->m_pContext->Continue(pPause);
+ if(m_pJbig2Context->m_pContext->GetProcessiveStatus() == FXCODEC_STATUS_DECODE_FINISH) {
+ if(m_pJbig2Context->m_bFileReader) {
+ CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
+ m_pJbig2Context->m_pContext = NULL;
+ if (ret != JBIG2_SUCCESS) {
+ if(m_pJbig2Context->m_src_buf) {
+ FX_Free(m_pJbig2Context->m_src_buf);
+ }
+ m_pJbig2Context->m_src_buf = NULL;
+ return FXCODEC_STATUS_ERROR;
+ }
+ delete m_pJbig2Context->m_dest_image;
+ FX_Free(m_pJbig2Context->m_src_buf);
+ return FXCODEC_STATUS_DECODE_FINISH;
+ } else {
+ CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
+ m_pJbig2Context->m_pContext = NULL;
+ if (ret != JBIG2_SUCCESS) {
+ return FXCODEC_STATUS_ERROR;
+ }
+ int dword_size = m_pJbig2Context->m_height * m_pJbig2Context->m_dest_pitch / 4;
+ FX_DWORD* dword_buf = (FX_DWORD*)m_pJbig2Context->m_dest_buf;
+ for (int i = 0; i < dword_size; i ++) {
+ dword_buf[i] = ~dword_buf[i];
+ }
+ return FXCODEC_STATUS_DECODE_FINISH;
+ }
+ }
+ return m_pJbig2Context->m_pContext->GetProcessiveStatus();
+}
+
+
+
diff --git a/core/src/fxcodec/codec/fx_codec_jpeg.cpp b/core/src/fxcodec/codec/fx_codec_jpeg.cpp
index fc91a6c162..5e78e134aa 100644
--- a/core/src/fxcodec/codec/fx_codec_jpeg.cpp
+++ b/core/src/fxcodec/codec/fx_codec_jpeg.cpp
@@ -1,735 +1,735 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../../include/fxcodec/fx_codec.h"
-#include "../../../include/fxge/fx_dib.h"
-#include "codec_int.h"
-extern "C" {
- static void _JpegScanSOI(const FX_BYTE*& src_buf, FX_DWORD& src_size)
- {
- if (src_size == 0) {
- return;
- }
- FX_DWORD offset = 0;
- while (offset < src_size - 1) {
- if (src_buf[offset] == 0xff && src_buf[offset + 1] == 0xd8) {
- src_buf += offset;
- src_size -= offset;
- return;
- }
- offset ++;
- }
- }
-};
-extern "C" {
-#undef FAR
-#include "../../fx_jpeglib.h"
-}
-extern "C" {
- static void _src_do_nothing(struct jpeg_decompress_struct* cinfo) {}
-};
-extern "C" {
- static void _error_fatal(j_common_ptr cinfo)
- {
- longjmp(*(jmp_buf*)cinfo->client_data, -1);
- }
-};
-extern "C" {
- static void _src_skip_data(struct jpeg_decompress_struct* cinfo, long num)
- {
- if (num > (long)cinfo->src->bytes_in_buffer) {
- _error_fatal((j_common_ptr)cinfo);
- }
- cinfo->src->next_input_byte += num;
- cinfo->src->bytes_in_buffer -= num;
- }
-};
-extern "C" {
- static boolean _src_fill_buffer(j_decompress_ptr cinfo)
- {
- return 0;
- }
-};
-extern "C" {
- static boolean _src_resync(j_decompress_ptr cinfo, int desired)
- {
- return 0;
- }
-};
-extern "C" {
- static void _error_do_nothing(j_common_ptr cinfo) {}
-};
-extern "C" {
- static void _error_do_nothing1(j_common_ptr cinfo, int) {}
-};
-extern "C" {
- static void _error_do_nothing2(j_common_ptr cinfo, char*) {}
-};
-#define JPEG_MARKER_EXIF (JPEG_APP0 + 1)
-#define JPEG_MARKER_ICC (JPEG_APP0 + 2)
-#define JPEG_MARKER_AUTHORTIME (JPEG_APP0 + 3)
-#define JPEG_MARKER_MAXSIZE 0xFFFF
-#define JPEG_OVERHEAD_LEN 14
-static FX_BOOL _JpegIsIccMarker(jpeg_saved_marker_ptr marker)
-{
- if (marker->marker == JPEG_MARKER_ICC &&
- marker->data_length >= JPEG_OVERHEAD_LEN &&
- (FXSYS_memcmp32(marker->data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00", 12) == 0)) {
- return TRUE;
- }
- return FALSE;
-}
-static FX_BOOL _JpegLoadIccProfile(j_decompress_ptr cinfo, FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length)
-{
- if(icc_buf_ptr == NULL || icc_length == NULL) {
- return FALSE;
- }
- *icc_buf_ptr = NULL;
- *icc_length = 0;
- FX_LPBYTE icc_data_ptr = NULL;
- FX_DWORD icc_data_len = 0;
- FX_BYTE count_icc_marker = 0;
- FX_BYTE num_icc_marker = 0;
- jpeg_saved_marker_ptr marker_list[256] = {NULL};
- for (jpeg_saved_marker_ptr cur_marker = cinfo->marker_list;
- cur_marker != NULL;
- cur_marker = cur_marker->next) {
- if(_JpegIsIccMarker(cur_marker)) {
- if(count_icc_marker == 0) {
- num_icc_marker = cur_marker->data[13];
- } else if(num_icc_marker != cur_marker->data[13]) {
- return FALSE;
- }
- int sn = cur_marker->data[12] - 1;
- if(sn < 0 || sn >= num_icc_marker) {
- return FALSE;
- }
- if(marker_list[sn] == NULL) {
- marker_list[sn] = cur_marker;
- } else {
- return FALSE;
- }
- count_icc_marker ++;
- icc_data_len += (cur_marker->data_length - JPEG_OVERHEAD_LEN);
- }
- }
- if(count_icc_marker != num_icc_marker) {
- return FALSE;
- }
- if(num_icc_marker == 0) {
- return TRUE;
- }
- icc_data_ptr = FX_Alloc(FX_BYTE, icc_data_len);
- if(icc_buf_ptr == NULL) {
- return FALSE;
- }
- *icc_buf_ptr = icc_data_ptr;
- *icc_length = icc_data_len;
- for (int idx = 0; idx < num_icc_marker; idx++) {
- icc_data_len = marker_list[idx]->data_length - JPEG_OVERHEAD_LEN;
- FXSYS_memcpy32(icc_data_ptr, marker_list[idx]->data + JPEG_OVERHEAD_LEN, icc_data_len);
- icc_data_ptr += icc_data_len;
- }
- return TRUE;
-}
-static FX_BOOL _JpegEmbedIccProfile(j_compress_ptr cinfo, FX_LPCBYTE icc_buf_ptr, FX_DWORD icc_length)
-{
- if(icc_buf_ptr == NULL || icc_length == 0) {
- return FALSE;
- }
- FX_DWORD icc_segment_size = (JPEG_MARKER_MAXSIZE - 2 - JPEG_OVERHEAD_LEN);
- FX_DWORD icc_segment_num = (icc_length / icc_segment_size) + 1;
- if (icc_segment_num > 255) {
- return FALSE;
- }
- FX_DWORD icc_data_length = JPEG_OVERHEAD_LEN + (icc_segment_num > 1 ? icc_segment_size : icc_length);
- FX_LPBYTE icc_data = FX_Alloc(FX_BYTE, icc_data_length);
- if (icc_data == NULL) {
- return FALSE;
- }
- FXSYS_memcpy32(icc_data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00", 12);
- icc_data[13] = (FX_BYTE)icc_segment_num;
- for (FX_BYTE i = 0; i < (icc_segment_num - 1); i++) {
- icc_data[12] = i + 1;
- FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + i * icc_segment_size, icc_segment_size);
- jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, icc_data_length);
- }
- icc_data[12] = (FX_BYTE)icc_segment_num;
- FX_DWORD icc_size = (icc_segment_num - 1) * icc_segment_size;
- FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + icc_size, icc_length - icc_size);
- jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, JPEG_OVERHEAD_LEN + icc_length - icc_size);
- FX_Free(icc_data);
- return TRUE;
-}
-extern "C" {
- static void _dest_do_nothing(j_compress_ptr cinfo) {}
-};
-extern "C" {
- static boolean _dest_empty(j_compress_ptr cinfo)
- {
- return FALSE;
- }
-};
-#define JPEG_BLOCK_SIZE 1048576
-static void _JpegEncode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length)
-{
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
- jerr.error_exit = _error_do_nothing;
- jerr.emit_message = _error_do_nothing1;
- jerr.output_message = _error_do_nothing;
- jerr.format_message = _error_do_nothing2;
- jerr.reset_error_mgr = _error_do_nothing;
- cinfo.err = &jerr;
- jpeg_create_compress(&cinfo);
- int Bpp = pSource->GetBPP() / 8;
- int nComponents = Bpp >= 3 ? (pSource->IsCmykImage() ? 4 : 3) : 1;
- int pitch = pSource->GetPitch();
- int width = pSource->GetWidth();
- int height = pSource->GetHeight();
- FX_DWORD dest_buf_length = width * height * nComponents + 1024 + (icc_length ? (icc_length + 255 * 18) : 0);
- dest_buf = FX_Alloc(FX_BYTE, dest_buf_length);
- while (dest_buf == NULL) {
- dest_buf_length >>= 1;
- dest_buf = FX_Alloc(FX_BYTE, dest_buf_length);
- }
- FXSYS_memset32(dest_buf, 0, dest_buf_length);
- struct jpeg_destination_mgr dest;
- dest.init_destination = _dest_do_nothing;
- dest.term_destination = _dest_do_nothing;
- dest.empty_output_buffer = _dest_empty;
- dest.next_output_byte = dest_buf;
- dest.free_in_buffer = dest_buf_length;
- cinfo.dest = &dest;
- cinfo.image_width = width;
- cinfo.image_height = height;
- cinfo.input_components = nComponents;
- if (nComponents == 1) {
- cinfo.in_color_space = JCS_GRAYSCALE;
- } else if (nComponents == 3) {
- cinfo.in_color_space = JCS_RGB;
- } else {
- cinfo.in_color_space = JCS_CMYK;
- }
- FX_LPBYTE line_buf = NULL;
- if (nComponents > 1) {
- line_buf = FX_Alloc(FX_BYTE, width * nComponents);
- if (line_buf == NULL) {
- return;
- }
- }
- jpeg_set_defaults(&cinfo);
- if(quality != 75) {
- jpeg_set_quality(&cinfo, quality, TRUE);
- }
- jpeg_start_compress(&cinfo, TRUE);
- _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length);
- JSAMPROW row_pointer[1];
- JDIMENSION row;
- while (cinfo.next_scanline < cinfo.image_height) {
- FX_LPCBYTE src_scan = pSource->GetScanline(cinfo.next_scanline);
- if (nComponents > 1) {
- FX_LPBYTE dest_scan = line_buf;
- if (nComponents == 3) {
- for (int i = 0; i < width; i ++) {
- dest_scan[0] = src_scan[2];
- dest_scan[1] = src_scan[1];
- dest_scan[2] = src_scan[0];
- dest_scan += 3;
- src_scan += Bpp;
- }
- } else {
- for (int i = 0; i < pitch; i ++) {
- *dest_scan++ = ~*src_scan++;
- }
- }
- row_pointer[0] = line_buf;
- } else {
- row_pointer[0] = (FX_LPBYTE)src_scan;
- }
- row = cinfo.next_scanline;
- jpeg_write_scanlines(&cinfo, row_pointer, 1);
- if (cinfo.next_scanline == row) {
- dest_buf = FX_Realloc(FX_BYTE, dest_buf, dest_buf_length + JPEG_BLOCK_SIZE);
- if (dest_buf == NULL) {
- FX_Free(line_buf);
- return;
- }
- dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_buffer;
- dest_buf_length += JPEG_BLOCK_SIZE;
- dest.free_in_buffer += JPEG_BLOCK_SIZE;
- }
- }
- jpeg_finish_compress(&cinfo);
- jpeg_destroy_compress(&cinfo);
- if (line_buf) {
- FX_Free(line_buf);
- }
- dest_size = dest_buf_length - (FX_STRSIZE)dest.free_in_buffer;
-}
-static FX_BOOL _JpegLoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
- int& num_components, int& bits_per_components, FX_BOOL& color_transform,
- FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length)
-{
- _JpegScanSOI(src_buf, src_size);
- struct jpeg_decompress_struct cinfo;
- struct jpeg_error_mgr jerr;
- jerr.error_exit = _error_fatal;
- jerr.emit_message = _error_do_nothing1;
- jerr.output_message = _error_do_nothing;
- jerr.format_message = _error_do_nothing2;
- jerr.reset_error_mgr = _error_do_nothing;
- cinfo.err = &jerr;
- jmp_buf mark;
- cinfo.client_data = &mark;
- if (setjmp(mark) == -1) {
- return FALSE;
- }
- jpeg_create_decompress(&cinfo);
- struct jpeg_source_mgr src;
- src.init_source = _src_do_nothing;
- src.term_source = _src_do_nothing;
- src.skip_input_data = _src_skip_data;
- src.fill_input_buffer = _src_fill_buffer;
- src.resync_to_restart = _src_resync;
- src.bytes_in_buffer = src_size;
- src.next_input_byte = src_buf;
- cinfo.src = &src;
- if (setjmp(mark) == -1) {
- jpeg_destroy_decompress(&cinfo);
- return FALSE;
- }
- if(icc_buf_ptr && icc_length) {
- jpeg_save_markers(&cinfo, JPEG_MARKER_ICC, JPEG_MARKER_MAXSIZE);
- }
- int ret = jpeg_read_header(&cinfo, TRUE);
- if (ret != JPEG_HEADER_OK) {
- jpeg_destroy_decompress(&cinfo);
- return FALSE;
- }
- width = cinfo.image_width;
- height = cinfo.image_height;
- num_components = cinfo.num_components;
- color_transform = cinfo.jpeg_color_space == JCS_YCbCr || cinfo.jpeg_color_space == JCS_YCCK;
- bits_per_components = cinfo.data_precision;
- if(icc_buf_ptr != NULL) {
- *icc_buf_ptr = NULL;
- }
- if(icc_length != NULL) {
- *icc_length = 0;
- }
- jpeg_destroy_decompress(&cinfo);
- return TRUE;
-}
-class CCodec_JpegDecoder : public CCodec_ScanlineDecoder
-{
-public:
- CCodec_JpegDecoder();
- ~CCodec_JpegDecoder();
- FX_BOOL Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps,
- FX_BOOL ColorTransform, IFX_JpegProvider* pJP);
- virtual void Destroy()
- {
- delete this;
- }
- virtual void v_DownScale(int dest_width, int dest_height);
- virtual FX_BOOL v_Rewind();
- virtual FX_LPBYTE v_GetNextLine();
- virtual FX_DWORD GetSrcOffset();
- jmp_buf m_JmpBuf;
- struct jpeg_decompress_struct cinfo;
- struct jpeg_error_mgr jerr;
- struct jpeg_source_mgr src;
- FX_LPCBYTE m_SrcBuf;
- FX_DWORD m_SrcSize;
- FX_LPBYTE m_pScanlineBuf;
- FX_BOOL InitDecode();
- FX_BOOL m_bInited, m_bStarted, m_bJpegTransform;
-protected:
- IFX_JpegProvider* m_pExtProvider;
- void* m_pExtContext;
- FX_DWORD m_nDefaultScaleDenom;
-};
-CCodec_JpegDecoder::CCodec_JpegDecoder()
-{
- m_pScanlineBuf = NULL;
- m_DownScale = 1;
- m_bStarted = FALSE;
- m_bInited = FALSE;
- m_pExtProvider = NULL;
- m_pExtContext = NULL;
- FXSYS_memset32(&cinfo, 0, sizeof(cinfo));
- FXSYS_memset32(&jerr, 0, sizeof(jerr));
- FXSYS_memset32(&src, 0, sizeof(src));
- m_nDefaultScaleDenom = 1;
-}
-CCodec_JpegDecoder::~CCodec_JpegDecoder()
-{
- if (m_pExtProvider) {
- m_pExtProvider->DestroyDecoder(m_pExtContext);
- return;
- }
- if (m_pScanlineBuf) {
- FX_Free(m_pScanlineBuf);
- }
- if (m_bInited) {
- jpeg_destroy_decompress(&cinfo);
- }
-}
-FX_BOOL CCodec_JpegDecoder::InitDecode()
-{
- cinfo.err = &jerr;
- cinfo.client_data = &m_JmpBuf;
- if (setjmp(m_JmpBuf) == -1) {
- return FALSE;
- }
- jpeg_create_decompress(&cinfo);
- m_bInited = TRUE;
- cinfo.src = &src;
- src.bytes_in_buffer = m_SrcSize;
- src.next_input_byte = m_SrcBuf;
- if (setjmp(m_JmpBuf) == -1) {
- jpeg_destroy_decompress(&cinfo);
- m_bInited = FALSE;
- return FALSE;
- }
- cinfo.image_width = m_OrigWidth;
- cinfo.image_height = m_OrigHeight;
- int ret = jpeg_read_header(&cinfo, TRUE);
- if (ret != JPEG_HEADER_OK) {
- return FALSE;
- }
- if (cinfo.saw_Adobe_marker) {
- m_bJpegTransform = TRUE;
- }
- if (cinfo.num_components == 3 && !m_bJpegTransform) {
- cinfo.out_color_space = cinfo.jpeg_color_space;
- }
- m_OrigWidth = cinfo.image_width;
- m_OrigHeight = cinfo.image_height;
- m_OutputWidth = m_OrigWidth;
- m_OutputHeight = m_OutputHeight;
- m_nDefaultScaleDenom = cinfo.scale_denom;
- return TRUE;
-}
-FX_BOOL CCodec_JpegDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
- int nComps, FX_BOOL ColorTransform, IFX_JpegProvider* pJP)
-{
- if (pJP) {
- m_pExtProvider = pJP;
- m_pExtContext = m_pExtProvider->CreateDecoder(src_buf, src_size, width, height, nComps, ColorTransform);
- return m_pExtContext != NULL;
- }
- _JpegScanSOI(src_buf, src_size);
- m_SrcBuf = src_buf;
- m_SrcSize = src_size;
- jerr.error_exit = _error_fatal;
- jerr.emit_message = _error_do_nothing1;
- jerr.output_message = _error_do_nothing;
- jerr.format_message = _error_do_nothing2;
- jerr.reset_error_mgr = _error_do_nothing;
- src.init_source = _src_do_nothing;
- src.term_source = _src_do_nothing;
- src.skip_input_data = _src_skip_data;
- src.fill_input_buffer = _src_fill_buffer;
- src.resync_to_restart = _src_resync;
- m_bJpegTransform = ColorTransform;
- if(src_size > 1 && FXSYS_memcmp32(src_buf + src_size - 2, "\xFF\xD9", 2) != 0) {
- ((FX_LPBYTE)src_buf)[src_size - 2] = 0xFF;
- ((FX_LPBYTE)src_buf)[src_size - 1] = 0xD9;
- }
- m_OutputWidth = m_OrigWidth = width;
- m_OutputHeight = m_OrigHeight = height;
- if (!InitDecode()) {
- return FALSE;
- }
- if (cinfo.num_components < nComps) {
- return FALSE;
- }
- if ((int)cinfo.image_width < width) {
- return FALSE;
- }
- m_Pitch = (cinfo.image_width * cinfo.num_components + 3) / 4 * 4;
- m_pScanlineBuf = FX_Alloc(FX_BYTE, m_Pitch);
- if (m_pScanlineBuf == NULL) {
- return FALSE;
- }
- m_nComps = cinfo.num_components;
- m_bpc = 8;
- m_bColorTransformed = FALSE;
- m_bStarted = FALSE;
- return TRUE;
-}
-extern "C" {
- FX_INT32 FX_GetDownsampleRatio(FX_INT32 originWidth, FX_INT32 originHeight, FX_INT32 downsampleWidth, FX_INT32 downsampleHeight)
- {
- int iratio_w = originWidth / downsampleWidth;
- int iratio_h = originHeight / downsampleHeight;
- int ratio = (iratio_w > iratio_h) ? iratio_h : iratio_w;
- if (ratio >= 8) {
- return 8;
- } else if (ratio >= 4) {
- return 4;
- } else if (ratio >= 2) {
- return 2;
- }
- return 1;
- }
-}
-void CCodec_JpegDecoder::v_DownScale(int dest_width, int dest_height)
-{
- if (m_pExtProvider) {
- m_pExtProvider->DownScale(m_pExtContext, dest_width, dest_height);
- return;
- }
- int old_scale = m_DownScale;
- m_DownScale = FX_GetDownsampleRatio(m_OrigWidth, m_OrigHeight, dest_width, dest_height);
- m_OutputWidth = (m_OrigWidth + m_DownScale - 1) / m_DownScale;
- m_OutputHeight = (m_OrigHeight + m_DownScale - 1) / m_DownScale;
- m_Pitch = (m_OutputWidth * m_nComps + 3) / 4 * 4;
- if (old_scale != m_DownScale) {
- m_NextLine = -1;
- }
-}
-FX_BOOL CCodec_JpegDecoder::v_Rewind()
-{
- if (m_pExtProvider) {
- return m_pExtProvider->Rewind(m_pExtContext);
- }
- if (m_bStarted) {
- jpeg_destroy_decompress(&cinfo);
- if (!InitDecode()) {
- return FALSE;
- }
- }
- if (setjmp(m_JmpBuf) == -1) {
- return FALSE;
- }
- cinfo.scale_denom = m_nDefaultScaleDenom * m_DownScale;
- m_OutputWidth = (m_OrigWidth + m_DownScale - 1) / m_DownScale;
- m_OutputHeight = (m_OrigHeight + m_DownScale - 1) / m_DownScale;
- if (!jpeg_start_decompress(&cinfo)) {
- jpeg_destroy_decompress(&cinfo);
- return FALSE;
- }
- if ((int)cinfo.output_width > m_OrigWidth) {
- FXSYS_assert(FALSE);
- return FALSE;
- }
- m_bStarted = TRUE;
- return TRUE;
-}
-FX_LPBYTE CCodec_JpegDecoder::v_GetNextLine()
-{
- if (m_pExtProvider) {
- return m_pExtProvider->GetNextLine(m_pExtContext);
- }
- int nlines = jpeg_read_scanlines(&cinfo, &m_pScanlineBuf, 1);
- if (nlines < 1) {
- return NULL;
- }
- return m_pScanlineBuf;
-}
-FX_DWORD CCodec_JpegDecoder::GetSrcOffset()
-{
- if (m_pExtProvider) {
- return m_pExtProvider->GetSrcOffset(m_pExtContext);
- }
- return (FX_DWORD)(m_SrcSize - src.bytes_in_buffer);
-}
-ICodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size,
- int width, int height, int nComps, FX_BOOL ColorTransform)
-{
- if (src_buf == NULL || src_size == 0) {
- return NULL;
- }
- CCodec_JpegDecoder* pDecoder = FX_NEW CCodec_JpegDecoder;
- if (pDecoder == NULL) {
- return NULL;
- }
- if (!pDecoder->Create(src_buf, src_size, width, height, nComps, ColorTransform, m_pExtProvider)) {
- delete pDecoder;
- return NULL;
- }
- return pDecoder;
-}
-FX_BOOL CCodec_JpegModule::LoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
- int& num_components, int& bits_per_components, FX_BOOL& color_transform,
- FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length)
-{
- if (m_pExtProvider) {
- return m_pExtProvider->LoadInfo(src_buf, src_size, width, height,
- num_components, bits_per_components, color_transform,
- icc_buf_ptr, icc_length);
- }
- return _JpegLoadInfo(src_buf, src_size, width, height, num_components, bits_per_components, color_transform, icc_buf_ptr, icc_length);
-}
-FX_BOOL CCodec_JpegModule::Encode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length)
-{
- if (m_pExtProvider) {
- return m_pExtProvider->Encode(pSource, dest_buf, dest_size, quality, icc_buf, icc_length);
- }
- if(pSource->GetBPP() < 8 || pSource->GetPalette() != NULL) {
- ASSERT(pSource->GetBPP() >= 8 && pSource->GetPalette() == NULL);
- return FALSE;
- }
- _JpegEncode(pSource, dest_buf, dest_size, quality, icc_buf, icc_length);
- return TRUE;
-}
-struct FXJPEG_Context {
- jmp_buf m_JumpMark;
- jpeg_decompress_struct m_Info;
- jpeg_error_mgr m_ErrMgr;
- jpeg_source_mgr m_SrcMgr;
- unsigned int m_SkipSize;
- void* (*m_AllocFunc)(unsigned int);
- void (*m_FreeFunc)(void*);
-};
-extern "C" {
- static void _error_fatal1(j_common_ptr cinfo)
- {
- longjmp(((FXJPEG_Context*)cinfo->client_data)->m_JumpMark, -1);
- }
-};
-extern "C" {
- static void _src_skip_data1(struct jpeg_decompress_struct* cinfo, long num)
- {
- if (cinfo->src->bytes_in_buffer < (size_t)num) {
- ((FXJPEG_Context*)cinfo->client_data)->m_SkipSize = (unsigned int)(num - cinfo->src->bytes_in_buffer);
- cinfo->src->bytes_in_buffer = 0;
- } else {
- cinfo->src->next_input_byte += num;
- cinfo->src->bytes_in_buffer -= num;
- }
- }
-};
-static void* jpeg_alloc_func(unsigned int size)
-{
- return FX_Alloc(char, size);
-}
-static void jpeg_free_func(void* p)
-{
- FX_Free(p);
-}
-void* CCodec_JpegModule::Start()
-{
- if (m_pExtProvider) {
- return m_pExtProvider->Start();
- }
- FXJPEG_Context* p = (FXJPEG_Context*)FX_Alloc(FX_BYTE, sizeof(FXJPEG_Context));
- if (p == NULL) {
- return NULL;
- }
- p->m_AllocFunc = jpeg_alloc_func;
- p->m_FreeFunc = jpeg_free_func;
- p->m_ErrMgr.error_exit = _error_fatal1;
- p->m_ErrMgr.emit_message = _error_do_nothing1;
- p->m_ErrMgr.output_message = _error_do_nothing;
- p->m_ErrMgr.format_message = _error_do_nothing2;
- p->m_ErrMgr.reset_error_mgr = _error_do_nothing;
- p->m_SrcMgr.init_source = _src_do_nothing;
- p->m_SrcMgr.term_source = _src_do_nothing;
- p->m_SrcMgr.skip_input_data = _src_skip_data1;
- p->m_SrcMgr.fill_input_buffer = _src_fill_buffer;
- p->m_SrcMgr.resync_to_restart = _src_resync;
- p->m_Info.client_data = p;
- p->m_Info.err = &p->m_ErrMgr;
- if (setjmp(p->m_JumpMark) == -1) {
- return 0;
- }
- jpeg_create_decompress(&p->m_Info);
- p->m_Info.src = &p->m_SrcMgr;
- p->m_SkipSize = 0;
- return p;
-}
-void CCodec_JpegModule::Finish(void* pContext)
-{
- if (m_pExtProvider) {
- m_pExtProvider->Finish(pContext);
- return;
- }
- FXJPEG_Context* p = (FXJPEG_Context*)pContext;
- jpeg_destroy_decompress(&p->m_Info);
- p->m_FreeFunc(p);
-}
-void CCodec_JpegModule::Input(void* pContext, const unsigned char* src_buf, FX_DWORD src_size)
-{
- if (m_pExtProvider) {
- m_pExtProvider->Input(pContext, src_buf, src_size);
- return;
- }
- FXJPEG_Context* p = (FXJPEG_Context*)pContext;
- if (p->m_SkipSize) {
- if (p->m_SkipSize > src_size) {
- p->m_SrcMgr.bytes_in_buffer = 0;
- p->m_SkipSize -= src_size;
- return;
- }
- src_size -= p->m_SkipSize;
- src_buf += p->m_SkipSize;
- p->m_SkipSize = 0;
- }
- p->m_SrcMgr.next_input_byte = src_buf;
- p->m_SrcMgr.bytes_in_buffer = src_size;
-}
-int CCodec_JpegModule::ReadHeader(void* pContext, int* width, int* height, int* nComps)
-{
- if (m_pExtProvider) {
- return m_pExtProvider->ReadHeader(pContext, width, height, nComps);
- }
- FXJPEG_Context* p = (FXJPEG_Context*)pContext;
- if (setjmp(p->m_JumpMark) == -1) {
- return 1;
- }
- int ret = jpeg_read_header(&p->m_Info, true);
- if (ret == JPEG_SUSPENDED) {
- return 2;
- }
- if (ret != JPEG_HEADER_OK) {
- return 1;
- }
- *width = p->m_Info.image_width;
- *height = p->m_Info.image_height;
- *nComps = p->m_Info.num_components;
- return 0;
-}
-FX_BOOL CCodec_JpegModule::StartScanline(void* pContext, int down_scale)
-{
- if (m_pExtProvider) {
- return m_pExtProvider->StartScanline(pContext, down_scale);
- }
- FXJPEG_Context* p = (FXJPEG_Context*)pContext;
- if (setjmp(p->m_JumpMark) == -1) {
- return FALSE;
- }
- p->m_Info.scale_denom = down_scale;
- return jpeg_start_decompress(&p->m_Info);
-}
-FX_BOOL CCodec_JpegModule::ReadScanline(void* pContext, unsigned char* dest_buf)
-{
- if (m_pExtProvider) {
- return m_pExtProvider->ReadScanline(pContext, dest_buf);
- }
- FXJPEG_Context* p = (FXJPEG_Context*)pContext;
- if (setjmp(p->m_JumpMark) == -1) {
- return FALSE;
- }
- int nlines = jpeg_read_scanlines(&p->m_Info, &dest_buf, 1);
- return nlines == 1;
-}
-FX_DWORD CCodec_JpegModule::GetAvailInput(void* pContext, FX_LPBYTE* avail_buf_ptr)
-{
- if (m_pExtProvider) {
- return m_pExtProvider->GetAvailInput(pContext, avail_buf_ptr);
- }
- if(avail_buf_ptr != NULL) {
- *avail_buf_ptr = NULL;
- if(((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer > 0) {
- *avail_buf_ptr = (FX_LPBYTE)((FXJPEG_Context*)pContext)->m_SrcMgr.next_input_byte;
- }
- }
- return (FX_DWORD)((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../../include/fxcodec/fx_codec.h"
+#include "../../../include/fxge/fx_dib.h"
+#include "codec_int.h"
+extern "C" {
+ static void _JpegScanSOI(const FX_BYTE*& src_buf, FX_DWORD& src_size)
+ {
+ if (src_size == 0) {
+ return;
+ }
+ FX_DWORD offset = 0;
+ while (offset < src_size - 1) {
+ if (src_buf[offset] == 0xff && src_buf[offset + 1] == 0xd8) {
+ src_buf += offset;
+ src_size -= offset;
+ return;
+ }
+ offset ++;
+ }
+ }
+};
+extern "C" {
+#undef FAR
+#include "../../fx_jpeglib.h"
+}
+extern "C" {
+ static void _src_do_nothing(struct jpeg_decompress_struct* cinfo) {}
+};
+extern "C" {
+ static void _error_fatal(j_common_ptr cinfo)
+ {
+ longjmp(*(jmp_buf*)cinfo->client_data, -1);
+ }
+};
+extern "C" {
+ static void _src_skip_data(struct jpeg_decompress_struct* cinfo, long num)
+ {
+ if (num > (long)cinfo->src->bytes_in_buffer) {
+ _error_fatal((j_common_ptr)cinfo);
+ }
+ cinfo->src->next_input_byte += num;
+ cinfo->src->bytes_in_buffer -= num;
+ }
+};
+extern "C" {
+ static boolean _src_fill_buffer(j_decompress_ptr cinfo)
+ {
+ return 0;
+ }
+};
+extern "C" {
+ static boolean _src_resync(j_decompress_ptr cinfo, int desired)
+ {
+ return 0;
+ }
+};
+extern "C" {
+ static void _error_do_nothing(j_common_ptr cinfo) {}
+};
+extern "C" {
+ static void _error_do_nothing1(j_common_ptr cinfo, int) {}
+};
+extern "C" {
+ static void _error_do_nothing2(j_common_ptr cinfo, char*) {}
+};
+#define JPEG_MARKER_EXIF (JPEG_APP0 + 1)
+#define JPEG_MARKER_ICC (JPEG_APP0 + 2)
+#define JPEG_MARKER_AUTHORTIME (JPEG_APP0 + 3)
+#define JPEG_MARKER_MAXSIZE 0xFFFF
+#define JPEG_OVERHEAD_LEN 14
+static FX_BOOL _JpegIsIccMarker(jpeg_saved_marker_ptr marker)
+{
+ if (marker->marker == JPEG_MARKER_ICC &&
+ marker->data_length >= JPEG_OVERHEAD_LEN &&
+ (FXSYS_memcmp32(marker->data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00", 12) == 0)) {
+ return TRUE;
+ }
+ return FALSE;
+}
+static FX_BOOL _JpegLoadIccProfile(j_decompress_ptr cinfo, FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length)
+{
+ if(icc_buf_ptr == NULL || icc_length == NULL) {
+ return FALSE;
+ }
+ *icc_buf_ptr = NULL;
+ *icc_length = 0;
+ FX_LPBYTE icc_data_ptr = NULL;
+ FX_DWORD icc_data_len = 0;
+ FX_BYTE count_icc_marker = 0;
+ FX_BYTE num_icc_marker = 0;
+ jpeg_saved_marker_ptr marker_list[256] = {NULL};
+ for (jpeg_saved_marker_ptr cur_marker = cinfo->marker_list;
+ cur_marker != NULL;
+ cur_marker = cur_marker->next) {
+ if(_JpegIsIccMarker(cur_marker)) {
+ if(count_icc_marker == 0) {
+ num_icc_marker = cur_marker->data[13];
+ } else if(num_icc_marker != cur_marker->data[13]) {
+ return FALSE;
+ }
+ int sn = cur_marker->data[12] - 1;
+ if(sn < 0 || sn >= num_icc_marker) {
+ return FALSE;
+ }
+ if(marker_list[sn] == NULL) {
+ marker_list[sn] = cur_marker;
+ } else {
+ return FALSE;
+ }
+ count_icc_marker ++;
+ icc_data_len += (cur_marker->data_length - JPEG_OVERHEAD_LEN);
+ }
+ }
+ if(count_icc_marker != num_icc_marker) {
+ return FALSE;
+ }
+ if(num_icc_marker == 0) {
+ return TRUE;
+ }
+ icc_data_ptr = FX_Alloc(FX_BYTE, icc_data_len);
+ if(icc_buf_ptr == NULL) {
+ return FALSE;
+ }
+ *icc_buf_ptr = icc_data_ptr;
+ *icc_length = icc_data_len;
+ for (int idx = 0; idx < num_icc_marker; idx++) {
+ icc_data_len = marker_list[idx]->data_length - JPEG_OVERHEAD_LEN;
+ FXSYS_memcpy32(icc_data_ptr, marker_list[idx]->data + JPEG_OVERHEAD_LEN, icc_data_len);
+ icc_data_ptr += icc_data_len;
+ }
+ return TRUE;
+}
+static FX_BOOL _JpegEmbedIccProfile(j_compress_ptr cinfo, FX_LPCBYTE icc_buf_ptr, FX_DWORD icc_length)
+{
+ if(icc_buf_ptr == NULL || icc_length == 0) {
+ return FALSE;
+ }
+ FX_DWORD icc_segment_size = (JPEG_MARKER_MAXSIZE - 2 - JPEG_OVERHEAD_LEN);
+ FX_DWORD icc_segment_num = (icc_length / icc_segment_size) + 1;
+ if (icc_segment_num > 255) {
+ return FALSE;
+ }
+ FX_DWORD icc_data_length = JPEG_OVERHEAD_LEN + (icc_segment_num > 1 ? icc_segment_size : icc_length);
+ FX_LPBYTE icc_data = FX_Alloc(FX_BYTE, icc_data_length);
+ if (icc_data == NULL) {
+ return FALSE;
+ }
+ FXSYS_memcpy32(icc_data, "\x49\x43\x43\x5f\x50\x52\x4f\x46\x49\x4c\x45\x00", 12);
+ icc_data[13] = (FX_BYTE)icc_segment_num;
+ for (FX_BYTE i = 0; i < (icc_segment_num - 1); i++) {
+ icc_data[12] = i + 1;
+ FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + i * icc_segment_size, icc_segment_size);
+ jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, icc_data_length);
+ }
+ icc_data[12] = (FX_BYTE)icc_segment_num;
+ FX_DWORD icc_size = (icc_segment_num - 1) * icc_segment_size;
+ FXSYS_memcpy32(icc_data + JPEG_OVERHEAD_LEN, icc_buf_ptr + icc_size, icc_length - icc_size);
+ jpeg_write_marker(cinfo, JPEG_MARKER_ICC, icc_data, JPEG_OVERHEAD_LEN + icc_length - icc_size);
+ FX_Free(icc_data);
+ return TRUE;
+}
+extern "C" {
+ static void _dest_do_nothing(j_compress_ptr cinfo) {}
+};
+extern "C" {
+ static boolean _dest_empty(j_compress_ptr cinfo)
+ {
+ return FALSE;
+ }
+};
+#define JPEG_BLOCK_SIZE 1048576
+static void _JpegEncode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length)
+{
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ jerr.error_exit = _error_do_nothing;
+ jerr.emit_message = _error_do_nothing1;
+ jerr.output_message = _error_do_nothing;
+ jerr.format_message = _error_do_nothing2;
+ jerr.reset_error_mgr = _error_do_nothing;
+ cinfo.err = &jerr;
+ jpeg_create_compress(&cinfo);
+ int Bpp = pSource->GetBPP() / 8;
+ int nComponents = Bpp >= 3 ? (pSource->IsCmykImage() ? 4 : 3) : 1;
+ int pitch = pSource->GetPitch();
+ int width = pSource->GetWidth();
+ int height = pSource->GetHeight();
+ FX_DWORD dest_buf_length = width * height * nComponents + 1024 + (icc_length ? (icc_length + 255 * 18) : 0);
+ dest_buf = FX_Alloc(FX_BYTE, dest_buf_length);
+ while (dest_buf == NULL) {
+ dest_buf_length >>= 1;
+ dest_buf = FX_Alloc(FX_BYTE, dest_buf_length);
+ }
+ FXSYS_memset32(dest_buf, 0, dest_buf_length);
+ struct jpeg_destination_mgr dest;
+ dest.init_destination = _dest_do_nothing;
+ dest.term_destination = _dest_do_nothing;
+ dest.empty_output_buffer = _dest_empty;
+ dest.next_output_byte = dest_buf;
+ dest.free_in_buffer = dest_buf_length;
+ cinfo.dest = &dest;
+ cinfo.image_width = width;
+ cinfo.image_height = height;
+ cinfo.input_components = nComponents;
+ if (nComponents == 1) {
+ cinfo.in_color_space = JCS_GRAYSCALE;
+ } else if (nComponents == 3) {
+ cinfo.in_color_space = JCS_RGB;
+ } else {
+ cinfo.in_color_space = JCS_CMYK;
+ }
+ FX_LPBYTE line_buf = NULL;
+ if (nComponents > 1) {
+ line_buf = FX_Alloc(FX_BYTE, width * nComponents);
+ if (line_buf == NULL) {
+ return;
+ }
+ }
+ jpeg_set_defaults(&cinfo);
+ if(quality != 75) {
+ jpeg_set_quality(&cinfo, quality, TRUE);
+ }
+ jpeg_start_compress(&cinfo, TRUE);
+ _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length);
+ JSAMPROW row_pointer[1];
+ JDIMENSION row;
+ while (cinfo.next_scanline < cinfo.image_height) {
+ FX_LPCBYTE src_scan = pSource->GetScanline(cinfo.next_scanline);
+ if (nComponents > 1) {
+ FX_LPBYTE dest_scan = line_buf;
+ if (nComponents == 3) {
+ for (int i = 0; i < width; i ++) {
+ dest_scan[0] = src_scan[2];
+ dest_scan[1] = src_scan[1];
+ dest_scan[2] = src_scan[0];
+ dest_scan += 3;
+ src_scan += Bpp;
+ }
+ } else {
+ for (int i = 0; i < pitch; i ++) {
+ *dest_scan++ = ~*src_scan++;
+ }
+ }
+ row_pointer[0] = line_buf;
+ } else {
+ row_pointer[0] = (FX_LPBYTE)src_scan;
+ }
+ row = cinfo.next_scanline;
+ jpeg_write_scanlines(&cinfo, row_pointer, 1);
+ if (cinfo.next_scanline == row) {
+ dest_buf = FX_Realloc(FX_BYTE, dest_buf, dest_buf_length + JPEG_BLOCK_SIZE);
+ if (dest_buf == NULL) {
+ FX_Free(line_buf);
+ return;
+ }
+ dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_buffer;
+ dest_buf_length += JPEG_BLOCK_SIZE;
+ dest.free_in_buffer += JPEG_BLOCK_SIZE;
+ }
+ }
+ jpeg_finish_compress(&cinfo);
+ jpeg_destroy_compress(&cinfo);
+ if (line_buf) {
+ FX_Free(line_buf);
+ }
+ dest_size = dest_buf_length - (FX_STRSIZE)dest.free_in_buffer;
+}
+static FX_BOOL _JpegLoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
+ int& num_components, int& bits_per_components, FX_BOOL& color_transform,
+ FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length)
+{
+ _JpegScanSOI(src_buf, src_size);
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ jerr.error_exit = _error_fatal;
+ jerr.emit_message = _error_do_nothing1;
+ jerr.output_message = _error_do_nothing;
+ jerr.format_message = _error_do_nothing2;
+ jerr.reset_error_mgr = _error_do_nothing;
+ cinfo.err = &jerr;
+ jmp_buf mark;
+ cinfo.client_data = &mark;
+ if (setjmp(mark) == -1) {
+ return FALSE;
+ }
+ jpeg_create_decompress(&cinfo);
+ struct jpeg_source_mgr src;
+ src.init_source = _src_do_nothing;
+ src.term_source = _src_do_nothing;
+ src.skip_input_data = _src_skip_data;
+ src.fill_input_buffer = _src_fill_buffer;
+ src.resync_to_restart = _src_resync;
+ src.bytes_in_buffer = src_size;
+ src.next_input_byte = src_buf;
+ cinfo.src = &src;
+ if (setjmp(mark) == -1) {
+ jpeg_destroy_decompress(&cinfo);
+ return FALSE;
+ }
+ if(icc_buf_ptr && icc_length) {
+ jpeg_save_markers(&cinfo, JPEG_MARKER_ICC, JPEG_MARKER_MAXSIZE);
+ }
+ int ret = jpeg_read_header(&cinfo, TRUE);
+ if (ret != JPEG_HEADER_OK) {
+ jpeg_destroy_decompress(&cinfo);
+ return FALSE;
+ }
+ width = cinfo.image_width;
+ height = cinfo.image_height;
+ num_components = cinfo.num_components;
+ color_transform = cinfo.jpeg_color_space == JCS_YCbCr || cinfo.jpeg_color_space == JCS_YCCK;
+ bits_per_components = cinfo.data_precision;
+ if(icc_buf_ptr != NULL) {
+ *icc_buf_ptr = NULL;
+ }
+ if(icc_length != NULL) {
+ *icc_length = 0;
+ }
+ jpeg_destroy_decompress(&cinfo);
+ return TRUE;
+}
+class CCodec_JpegDecoder : public CCodec_ScanlineDecoder
+{
+public:
+ CCodec_JpegDecoder();
+ ~CCodec_JpegDecoder();
+ FX_BOOL Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps,
+ FX_BOOL ColorTransform, IFX_JpegProvider* pJP);
+ virtual void Destroy()
+ {
+ delete this;
+ }
+ virtual void v_DownScale(int dest_width, int dest_height);
+ virtual FX_BOOL v_Rewind();
+ virtual FX_LPBYTE v_GetNextLine();
+ virtual FX_DWORD GetSrcOffset();
+ jmp_buf m_JmpBuf;
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ struct jpeg_source_mgr src;
+ FX_LPCBYTE m_SrcBuf;
+ FX_DWORD m_SrcSize;
+ FX_LPBYTE m_pScanlineBuf;
+ FX_BOOL InitDecode();
+ FX_BOOL m_bInited, m_bStarted, m_bJpegTransform;
+protected:
+ IFX_JpegProvider* m_pExtProvider;
+ void* m_pExtContext;
+ FX_DWORD m_nDefaultScaleDenom;
+};
+CCodec_JpegDecoder::CCodec_JpegDecoder()
+{
+ m_pScanlineBuf = NULL;
+ m_DownScale = 1;
+ m_bStarted = FALSE;
+ m_bInited = FALSE;
+ m_pExtProvider = NULL;
+ m_pExtContext = NULL;
+ FXSYS_memset32(&cinfo, 0, sizeof(cinfo));
+ FXSYS_memset32(&jerr, 0, sizeof(jerr));
+ FXSYS_memset32(&src, 0, sizeof(src));
+ m_nDefaultScaleDenom = 1;
+}
+CCodec_JpegDecoder::~CCodec_JpegDecoder()
+{
+ if (m_pExtProvider) {
+ m_pExtProvider->DestroyDecoder(m_pExtContext);
+ return;
+ }
+ if (m_pScanlineBuf) {
+ FX_Free(m_pScanlineBuf);
+ }
+ if (m_bInited) {
+ jpeg_destroy_decompress(&cinfo);
+ }
+}
+FX_BOOL CCodec_JpegDecoder::InitDecode()
+{
+ cinfo.err = &jerr;
+ cinfo.client_data = &m_JmpBuf;
+ if (setjmp(m_JmpBuf) == -1) {
+ return FALSE;
+ }
+ jpeg_create_decompress(&cinfo);
+ m_bInited = TRUE;
+ cinfo.src = &src;
+ src.bytes_in_buffer = m_SrcSize;
+ src.next_input_byte = m_SrcBuf;
+ if (setjmp(m_JmpBuf) == -1) {
+ jpeg_destroy_decompress(&cinfo);
+ m_bInited = FALSE;
+ return FALSE;
+ }
+ cinfo.image_width = m_OrigWidth;
+ cinfo.image_height = m_OrigHeight;
+ int ret = jpeg_read_header(&cinfo, TRUE);
+ if (ret != JPEG_HEADER_OK) {
+ return FALSE;
+ }
+ if (cinfo.saw_Adobe_marker) {
+ m_bJpegTransform = TRUE;
+ }
+ if (cinfo.num_components == 3 && !m_bJpegTransform) {
+ cinfo.out_color_space = cinfo.jpeg_color_space;
+ }
+ m_OrigWidth = cinfo.image_width;
+ m_OrigHeight = cinfo.image_height;
+ m_OutputWidth = m_OrigWidth;
+ m_OutputHeight = m_OutputHeight;
+ m_nDefaultScaleDenom = cinfo.scale_denom;
+ return TRUE;
+}
+FX_BOOL CCodec_JpegDecoder::Create(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height,
+ int nComps, FX_BOOL ColorTransform, IFX_JpegProvider* pJP)
+{
+ if (pJP) {
+ m_pExtProvider = pJP;
+ m_pExtContext = m_pExtProvider->CreateDecoder(src_buf, src_size, width, height, nComps, ColorTransform);
+ return m_pExtContext != NULL;
+ }
+ _JpegScanSOI(src_buf, src_size);
+ m_SrcBuf = src_buf;
+ m_SrcSize = src_size;
+ jerr.error_exit = _error_fatal;
+ jerr.emit_message = _error_do_nothing1;
+ jerr.output_message = _error_do_nothing;
+ jerr.format_message = _error_do_nothing2;
+ jerr.reset_error_mgr = _error_do_nothing;
+ src.init_source = _src_do_nothing;
+ src.term_source = _src_do_nothing;
+ src.skip_input_data = _src_skip_data;
+ src.fill_input_buffer = _src_fill_buffer;
+ src.resync_to_restart = _src_resync;
+ m_bJpegTransform = ColorTransform;
+ if(src_size > 1 && FXSYS_memcmp32(src_buf + src_size - 2, "\xFF\xD9", 2) != 0) {
+ ((FX_LPBYTE)src_buf)[src_size - 2] = 0xFF;
+ ((FX_LPBYTE)src_buf)[src_size - 1] = 0xD9;
+ }
+ m_OutputWidth = m_OrigWidth = width;
+ m_OutputHeight = m_OrigHeight = height;
+ if (!InitDecode()) {
+ return FALSE;
+ }
+ if (cinfo.num_components < nComps) {
+ return FALSE;
+ }
+ if ((int)cinfo.image_width < width) {
+ return FALSE;
+ }
+ m_Pitch = (cinfo.image_width * cinfo.num_components + 3) / 4 * 4;
+ m_pScanlineBuf = FX_Alloc(FX_BYTE, m_Pitch);
+ if (m_pScanlineBuf == NULL) {
+ return FALSE;
+ }
+ m_nComps = cinfo.num_components;
+ m_bpc = 8;
+ m_bColorTransformed = FALSE;
+ m_bStarted = FALSE;
+ return TRUE;
+}
+extern "C" {
+ FX_INT32 FX_GetDownsampleRatio(FX_INT32 originWidth, FX_INT32 originHeight, FX_INT32 downsampleWidth, FX_INT32 downsampleHeight)
+ {
+ int iratio_w = originWidth / downsampleWidth;
+ int iratio_h = originHeight / downsampleHeight;
+ int ratio = (iratio_w > iratio_h) ? iratio_h : iratio_w;
+ if (ratio >= 8) {
+ return 8;
+ } else if (ratio >= 4) {
+ return 4;
+ } else if (ratio >= 2) {
+ return 2;
+ }
+ return 1;
+ }
+}
+void CCodec_JpegDecoder::v_DownScale(int dest_width, int dest_height)
+{
+ if (m_pExtProvider) {
+ m_pExtProvider->DownScale(m_pExtContext, dest_width, dest_height);
+ return;
+ }
+ int old_scale = m_DownScale;
+ m_DownScale = FX_GetDownsampleRatio(m_OrigWidth, m_OrigHeight, dest_width, dest_height);
+ m_OutputWidth = (m_OrigWidth + m_DownScale - 1) / m_DownScale;
+ m_OutputHeight = (m_OrigHeight + m_DownScale - 1) / m_DownScale;
+ m_Pitch = (m_OutputWidth * m_nComps + 3) / 4 * 4;
+ if (old_scale != m_DownScale) {
+ m_NextLine = -1;
+ }
+}
+FX_BOOL CCodec_JpegDecoder::v_Rewind()
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->Rewind(m_pExtContext);
+ }
+ if (m_bStarted) {
+ jpeg_destroy_decompress(&cinfo);
+ if (!InitDecode()) {
+ return FALSE;
+ }
+ }
+ if (setjmp(m_JmpBuf) == -1) {
+ return FALSE;
+ }
+ cinfo.scale_denom = m_nDefaultScaleDenom * m_DownScale;
+ m_OutputWidth = (m_OrigWidth + m_DownScale - 1) / m_DownScale;
+ m_OutputHeight = (m_OrigHeight + m_DownScale - 1) / m_DownScale;
+ if (!jpeg_start_decompress(&cinfo)) {
+ jpeg_destroy_decompress(&cinfo);
+ return FALSE;
+ }
+ if ((int)cinfo.output_width > m_OrigWidth) {
+ FXSYS_assert(FALSE);
+ return FALSE;
+ }
+ m_bStarted = TRUE;
+ return TRUE;
+}
+FX_LPBYTE CCodec_JpegDecoder::v_GetNextLine()
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->GetNextLine(m_pExtContext);
+ }
+ int nlines = jpeg_read_scanlines(&cinfo, &m_pScanlineBuf, 1);
+ if (nlines < 1) {
+ return NULL;
+ }
+ return m_pScanlineBuf;
+}
+FX_DWORD CCodec_JpegDecoder::GetSrcOffset()
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->GetSrcOffset(m_pExtContext);
+ }
+ return (FX_DWORD)(m_SrcSize - src.bytes_in_buffer);
+}
+ICodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size,
+ int width, int height, int nComps, FX_BOOL ColorTransform)
+{
+ if (src_buf == NULL || src_size == 0) {
+ return NULL;
+ }
+ CCodec_JpegDecoder* pDecoder = FX_NEW CCodec_JpegDecoder;
+ if (pDecoder == NULL) {
+ return NULL;
+ }
+ if (!pDecoder->Create(src_buf, src_size, width, height, nComps, ColorTransform, m_pExtProvider)) {
+ delete pDecoder;
+ return NULL;
+ }
+ return pDecoder;
+}
+FX_BOOL CCodec_JpegModule::LoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
+ int& num_components, int& bits_per_components, FX_BOOL& color_transform,
+ FX_LPBYTE* icc_buf_ptr, FX_DWORD* icc_length)
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->LoadInfo(src_buf, src_size, width, height,
+ num_components, bits_per_components, color_transform,
+ icc_buf_ptr, icc_length);
+ }
+ return _JpegLoadInfo(src_buf, src_size, width, height, num_components, bits_per_components, color_transform, icc_buf_ptr, icc_length);
+}
+FX_BOOL CCodec_JpegModule::Encode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length)
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->Encode(pSource, dest_buf, dest_size, quality, icc_buf, icc_length);
+ }
+ if(pSource->GetBPP() < 8 || pSource->GetPalette() != NULL) {
+ ASSERT(pSource->GetBPP() >= 8 && pSource->GetPalette() == NULL);
+ return FALSE;
+ }
+ _JpegEncode(pSource, dest_buf, dest_size, quality, icc_buf, icc_length);
+ return TRUE;
+}
+struct FXJPEG_Context {
+ jmp_buf m_JumpMark;
+ jpeg_decompress_struct m_Info;
+ jpeg_error_mgr m_ErrMgr;
+ jpeg_source_mgr m_SrcMgr;
+ unsigned int m_SkipSize;
+ void* (*m_AllocFunc)(unsigned int);
+ void (*m_FreeFunc)(void*);
+};
+extern "C" {
+ static void _error_fatal1(j_common_ptr cinfo)
+ {
+ longjmp(((FXJPEG_Context*)cinfo->client_data)->m_JumpMark, -1);
+ }
+};
+extern "C" {
+ static void _src_skip_data1(struct jpeg_decompress_struct* cinfo, long num)
+ {
+ if (cinfo->src->bytes_in_buffer < (size_t)num) {
+ ((FXJPEG_Context*)cinfo->client_data)->m_SkipSize = (unsigned int)(num - cinfo->src->bytes_in_buffer);
+ cinfo->src->bytes_in_buffer = 0;
+ } else {
+ cinfo->src->next_input_byte += num;
+ cinfo->src->bytes_in_buffer -= num;
+ }
+ }
+};
+static void* jpeg_alloc_func(unsigned int size)
+{
+ return FX_Alloc(char, size);
+}
+static void jpeg_free_func(void* p)
+{
+ FX_Free(p);
+}
+void* CCodec_JpegModule::Start()
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->Start();
+ }
+ FXJPEG_Context* p = (FXJPEG_Context*)FX_Alloc(FX_BYTE, sizeof(FXJPEG_Context));
+ if (p == NULL) {
+ return NULL;
+ }
+ p->m_AllocFunc = jpeg_alloc_func;
+ p->m_FreeFunc = jpeg_free_func;
+ p->m_ErrMgr.error_exit = _error_fatal1;
+ p->m_ErrMgr.emit_message = _error_do_nothing1;
+ p->m_ErrMgr.output_message = _error_do_nothing;
+ p->m_ErrMgr.format_message = _error_do_nothing2;
+ p->m_ErrMgr.reset_error_mgr = _error_do_nothing;
+ p->m_SrcMgr.init_source = _src_do_nothing;
+ p->m_SrcMgr.term_source = _src_do_nothing;
+ p->m_SrcMgr.skip_input_data = _src_skip_data1;
+ p->m_SrcMgr.fill_input_buffer = _src_fill_buffer;
+ p->m_SrcMgr.resync_to_restart = _src_resync;
+ p->m_Info.client_data = p;
+ p->m_Info.err = &p->m_ErrMgr;
+ if (setjmp(p->m_JumpMark) == -1) {
+ return 0;
+ }
+ jpeg_create_decompress(&p->m_Info);
+ p->m_Info.src = &p->m_SrcMgr;
+ p->m_SkipSize = 0;
+ return p;
+}
+void CCodec_JpegModule::Finish(void* pContext)
+{
+ if (m_pExtProvider) {
+ m_pExtProvider->Finish(pContext);
+ return;
+ }
+ FXJPEG_Context* p = (FXJPEG_Context*)pContext;
+ jpeg_destroy_decompress(&p->m_Info);
+ p->m_FreeFunc(p);
+}
+void CCodec_JpegModule::Input(void* pContext, const unsigned char* src_buf, FX_DWORD src_size)
+{
+ if (m_pExtProvider) {
+ m_pExtProvider->Input(pContext, src_buf, src_size);
+ return;
+ }
+ FXJPEG_Context* p = (FXJPEG_Context*)pContext;
+ if (p->m_SkipSize) {
+ if (p->m_SkipSize > src_size) {
+ p->m_SrcMgr.bytes_in_buffer = 0;
+ p->m_SkipSize -= src_size;
+ return;
+ }
+ src_size -= p->m_SkipSize;
+ src_buf += p->m_SkipSize;
+ p->m_SkipSize = 0;
+ }
+ p->m_SrcMgr.next_input_byte = src_buf;
+ p->m_SrcMgr.bytes_in_buffer = src_size;
+}
+int CCodec_JpegModule::ReadHeader(void* pContext, int* width, int* height, int* nComps)
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->ReadHeader(pContext, width, height, nComps);
+ }
+ FXJPEG_Context* p = (FXJPEG_Context*)pContext;
+ if (setjmp(p->m_JumpMark) == -1) {
+ return 1;
+ }
+ int ret = jpeg_read_header(&p->m_Info, true);
+ if (ret == JPEG_SUSPENDED) {
+ return 2;
+ }
+ if (ret != JPEG_HEADER_OK) {
+ return 1;
+ }
+ *width = p->m_Info.image_width;
+ *height = p->m_Info.image_height;
+ *nComps = p->m_Info.num_components;
+ return 0;
+}
+FX_BOOL CCodec_JpegModule::StartScanline(void* pContext, int down_scale)
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->StartScanline(pContext, down_scale);
+ }
+ FXJPEG_Context* p = (FXJPEG_Context*)pContext;
+ if (setjmp(p->m_JumpMark) == -1) {
+ return FALSE;
+ }
+ p->m_Info.scale_denom = down_scale;
+ return jpeg_start_decompress(&p->m_Info);
+}
+FX_BOOL CCodec_JpegModule::ReadScanline(void* pContext, unsigned char* dest_buf)
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->ReadScanline(pContext, dest_buf);
+ }
+ FXJPEG_Context* p = (FXJPEG_Context*)pContext;
+ if (setjmp(p->m_JumpMark) == -1) {
+ return FALSE;
+ }
+ int nlines = jpeg_read_scanlines(&p->m_Info, &dest_buf, 1);
+ return nlines == 1;
+}
+FX_DWORD CCodec_JpegModule::GetAvailInput(void* pContext, FX_LPBYTE* avail_buf_ptr)
+{
+ if (m_pExtProvider) {
+ return m_pExtProvider->GetAvailInput(pContext, avail_buf_ptr);
+ }
+ if(avail_buf_ptr != NULL) {
+ *avail_buf_ptr = NULL;
+ if(((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer > 0) {
+ *avail_buf_ptr = (FX_LPBYTE)((FXJPEG_Context*)pContext)->m_SrcMgr.next_input_byte;
+ }
+ }
+ return (FX_DWORD)((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer;
+}
diff --git a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
index 423819f69f..32cb10076d 100644
--- a/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
+++ b/core/src/fxcodec/codec/fx_codec_jpx_opj.cpp
@@ -1,799 +1,799 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../../../include/fxcodec/fx_codec.h"
-#include "codec_int.h"
-#include "../fx_libopenjpeg/libopenjpeg20/openjpeg.h"
-#include "../lcms2/include/fx_lcms2.h"
-static void fx_error_callback(const char *msg, void *client_data)
-{
- (void)client_data;
-}
-static void fx_warning_callback(const char *msg, void *client_data)
-{
- (void)client_data;
-}
-static void fx_info_callback(const char *msg, void *client_data)
-{
- (void)client_data;
-}
-typedef struct {
- const unsigned char* src_data;
- int src_size;
- int offset;
-} decodeData;
-static OPJ_SIZE_T opj_read_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, decodeData* srcData)
-{
- if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
- return -1;
- }
- OPJ_SIZE_T readlength = p_nb_bytes;
- OPJ_SIZE_T bufferLength = (OPJ_SIZE_T)(srcData->src_size - srcData->offset);
- if(bufferLength <= 0) {
- return 0;
- }
- if(bufferLength <= p_nb_bytes) {
- readlength = bufferLength;
- }
- memcpy(p_buffer, &(srcData->src_data[srcData->offset]), readlength);
- srcData->offset += (int)readlength;
- return readlength;
-}
-static OPJ_SIZE_T opj_write_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, decodeData* srcData)
-{
- if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
- return -1;
- }
- OPJ_SIZE_T writeLength = p_nb_bytes;
- OPJ_SIZE_T bufferLength = (OPJ_SIZE_T)(srcData->src_size - srcData->offset);
- if(bufferLength <= p_nb_bytes) {
- writeLength = bufferLength;
- }
- memcpy((void*&)(srcData->src_data[srcData->offset]), p_buffer, writeLength);
- srcData->offset += (int)writeLength;
- return writeLength;
-}
-static OPJ_OFF_T opj_skip_from_memory (OPJ_OFF_T p_nb_bytes, decodeData* srcData)
-{
- if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
- return -1;
- }
- OPJ_OFF_T postion = srcData->offset + p_nb_bytes;
- if(postion < 0 ) {
- postion = 0;
- } else if (postion > srcData->src_size) {
- }
- srcData->offset = (int)postion;
- return p_nb_bytes;
-}
-static OPJ_BOOL opj_seek_from_memory (OPJ_OFF_T p_nb_bytes, decodeData * srcData)
-{
- if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
- return -1;
- }
- srcData->offset = (int)p_nb_bytes;
- if(srcData->offset < 0) {
- srcData->offset = 0;
- } else if(srcData->offset > srcData->src_size) {
- srcData->offset = srcData->src_size;
- }
- return OPJ_TRUE;
-}
-opj_stream_t* fx_opj_stream_create_memory_stream (decodeData* data, OPJ_SIZE_T p_size, OPJ_BOOL p_is_read_stream)
-{
- opj_stream_t* l_stream = 00;
- if (!data || ! data->src_data || data->src_size <= 0 ) {
- return NULL;
- }
- l_stream = opj_stream_create(p_size, p_is_read_stream);
- if (! l_stream) {
- return NULL;
- }
- opj_stream_set_user_data_v3(l_stream, data, NULL);
- opj_stream_set_user_data_length(l_stream, data->src_size);
- opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_memory);
- opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_memory);
- opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_memory);
- opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_memory);
- return l_stream;
-}
-static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
- int *out_r, int *out_g, int *out_b)
-{
- int r, g, b;
- cb -= offset;
- cr -= offset;
- r = y + (int)(1.402 * (float)cr);
- if(r < 0) {
- r = 0;
- } else if(r > upb) {
- r = upb;
- } *out_r = r;
- g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
- if(g < 0) {
- g = 0;
- } else if(g > upb) {
- g = upb;
- } *out_g = g;
- b = y + (int)(1.772 * (float)cb);
- if(b < 0) {
- b = 0;
- } else if(b > upb) {
- b = upb;
- } *out_b = b;
-}
-static void sycc444_to_rgb(opj_image_t *img)
-{
- int *d0, *d1, *d2, *r, *g, *b;
- const int *y, *cb, *cr;
- int maxw, maxh, max, i, offset, upb;
- i = (int)img->comps[0].prec;
- offset = 1 << (i - 1);
- upb = (1 << i) - 1;
- maxw = (int)img->comps[0].w;
- maxh = (int)img->comps[0].h;
- max = maxw * maxh;
- y = img->comps[0].data;
- cb = img->comps[1].data;
- cr = img->comps[2].data;
- d0 = r = FX_Alloc(int, (size_t)max);
- d1 = g = FX_Alloc(int, (size_t)max);
- d2 = b = FX_Alloc(int, (size_t)max);
- for(i = 0; i < max; ++i) {
- sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++cb;
- ++cr;
- ++r;
- ++g;
- ++b;
- }
- FX_Free(img->comps[0].data);
- img->comps[0].data = d0;
- FX_Free(img->comps[1].data);
- img->comps[1].data = d1;
- FX_Free(img->comps[2].data);
- img->comps[2].data = d2;
-}
-static void sycc422_to_rgb(opj_image_t *img)
-{
- int *d0, *d1, *d2, *r, *g, *b;
- const int *y, *cb, *cr;
- int maxw, maxh, max, offset, upb;
- int i, j;
- i = (int)img->comps[0].prec;
- offset = 1 << (i - 1);
- upb = (1 << i) - 1;
- maxw = (int)img->comps[0].w;
- maxh = (int)img->comps[0].h;
- max = maxw * maxh;
- y = img->comps[0].data;
- cb = img->comps[1].data;
- cr = img->comps[2].data;
- d0 = r = FX_Alloc(int, (size_t)max);
- d1 = g = FX_Alloc(int, (size_t)max);
- d2 = b = FX_Alloc(int, (size_t)max);
- for(i = 0; i < maxh; ++i) {
- for(j = 0; j < maxw; j += 2) {
- sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++r;
- ++g;
- ++b;
- sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++r;
- ++g;
- ++b;
- ++cb;
- ++cr;
- }
- }
- FX_Free(img->comps[0].data);
- img->comps[0].data = d0;
- FX_Free(img->comps[1].data);
- img->comps[1].data = d1;
- FX_Free(img->comps[2].data);
- img->comps[2].data = d2;
- img->comps[1].w = maxw;
- img->comps[1].h = maxh;
- img->comps[2].w = maxw;
- img->comps[2].h = maxh;
- img->comps[1].w = (OPJ_UINT32)maxw;
- img->comps[1].h = (OPJ_UINT32)maxh;
- img->comps[2].w = (OPJ_UINT32)maxw;
- img->comps[2].h = (OPJ_UINT32)maxh;
- img->comps[1].dx = img->comps[0].dx;
- img->comps[2].dx = img->comps[0].dx;
- img->comps[1].dy = img->comps[0].dy;
- img->comps[2].dy = img->comps[0].dy;
-}
-static void sycc420_to_rgb(opj_image_t *img)
-{
- int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
- const int *y, *cb, *cr, *ny;
- int maxw, maxh, max, offset, upb;
- int i, j;
- i = (int)img->comps[0].prec;
- offset = 1 << (i - 1);
- upb = (1 << i) - 1;
- maxw = (int)img->comps[0].w;
- maxh = (int)img->comps[0].h;
- max = maxw * maxh;
- y = img->comps[0].data;
- cb = img->comps[1].data;
- cr = img->comps[2].data;
- d0 = r = FX_Alloc(int, (size_t)max);
- d1 = g = FX_Alloc(int, (size_t)max);
- d2 = b = FX_Alloc(int, (size_t)max);
- for(i = 0; i < maxh; i += 2) {
- ny = y + maxw;
- nr = r + maxw;
- ng = g + maxw;
- nb = b + maxw;
- for(j = 0; j < maxw; j += 2) {
- sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++r;
- ++g;
- ++b;
- sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
- ++y;
- ++r;
- ++g;
- ++b;
- sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
- ++ny;
- ++nr;
- ++ng;
- ++nb;
- sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
- ++ny;
- ++nr;
- ++ng;
- ++nb;
- ++cb;
- ++cr;
- }
- y += maxw;
- r += maxw;
- g += maxw;
- b += maxw;
- }
- FX_Free(img->comps[0].data);
- img->comps[0].data = d0;
- FX_Free(img->comps[1].data);
- img->comps[1].data = d1;
- FX_Free(img->comps[2].data);
- img->comps[2].data = d2;
- img->comps[1].w = maxw;
- img->comps[1].h = maxh;
- img->comps[2].w = maxw;
- img->comps[2].h = maxh;
- img->comps[1].w = (OPJ_UINT32)maxw;
- img->comps[1].h = (OPJ_UINT32)maxh;
- img->comps[2].w = (OPJ_UINT32)maxw;
- img->comps[2].h = (OPJ_UINT32)maxh;
- img->comps[1].dx = img->comps[0].dx;
- img->comps[2].dx = img->comps[0].dx;
- img->comps[1].dy = img->comps[0].dy;
- img->comps[2].dy = img->comps[0].dy;
-}
-void color_sycc_to_rgb(opj_image_t *img)
-{
- if(img->numcomps < 3) {
- img->color_space = OPJ_CLRSPC_GRAY;
- return;
- }
- if((img->comps[0].dx == 1)
- && (img->comps[1].dx == 2)
- && (img->comps[2].dx == 2)
- && (img->comps[0].dy == 1)
- && (img->comps[1].dy == 2)
- && (img->comps[2].dy == 2)) {
- sycc420_to_rgb(img);
- } else if((img->comps[0].dx == 1)
- && (img->comps[1].dx == 2)
- && (img->comps[2].dx == 2)
- && (img->comps[0].dy == 1)
- && (img->comps[1].dy == 1)
- && (img->comps[2].dy == 1)) {
- sycc422_to_rgb(img);
- } else if((img->comps[0].dx == 1)
- && (img->comps[1].dx == 1)
- && (img->comps[2].dx == 1)
- && (img->comps[0].dy == 1)
- && (img->comps[1].dy == 1)
- && (img->comps[2].dy == 1)) {
- sycc444_to_rgb(img);
- } else {
- return;
- }
- img->color_space = OPJ_CLRSPC_SRGB;
-}
-void color_apply_icc_profile(opj_image_t *image)
-{
- cmsHPROFILE in_prof, out_prof;
- cmsHTRANSFORM transform;
- cmsColorSpaceSignature in_space, out_space;
- cmsUInt32Number intent, in_type, out_type, nr_samples;
- int *r, *g, *b;
- int prec, i, max, max_w, max_h;
- OPJ_COLOR_SPACE oldspace;
- in_prof =
- cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
- if(in_prof == NULL) {
- return;
- }
- in_space = cmsGetPCS(in_prof);
- out_space = cmsGetColorSpace(in_prof);
- intent = cmsGetHeaderRenderingIntent(in_prof);
- max_w = (int)image->comps[0].w;
- max_h = (int)image->comps[0].h;
- prec = (int)image->comps[0].prec;
- oldspace = image->color_space;
- if(out_space == cmsSigRgbData) {
- if( prec <= 8 ) {
- in_type = TYPE_RGB_8;
- out_type = TYPE_RGB_8;
- } else {
- in_type = TYPE_RGB_16;
- out_type = TYPE_RGB_16;
- }
- out_prof = cmsCreate_sRGBProfile();
- image->color_space = OPJ_CLRSPC_SRGB;
- } else if(out_space == cmsSigGrayData) {
- if( prec <= 8 ) {
- in_type = TYPE_GRAY_8;
- out_type = TYPE_RGB_8;
- } else {
- in_type = TYPE_GRAY_16;
- out_type = TYPE_RGB_16;
- }
- out_prof = cmsCreate_sRGBProfile();
- image->color_space = OPJ_CLRSPC_SRGB;
- } else if(out_space == cmsSigYCbCrData) {
- in_type = TYPE_YCbCr_16;
- out_type = TYPE_RGB_16;
- out_prof = cmsCreate_sRGBProfile();
- image->color_space = OPJ_CLRSPC_SRGB;
- } else {
- return;
- }
- transform = cmsCreateTransform(in_prof, in_type,
- out_prof, out_type, intent, 0);
- cmsCloseProfile(in_prof);
- cmsCloseProfile(out_prof);
- if(transform == NULL) {
- image->color_space = oldspace;
- return;
- }
- if(image->numcomps > 2) {
- if( prec <= 8 ) {
- unsigned char *inbuf, *outbuf, *in, *out;
- max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
- in = inbuf = FX_Alloc(unsigned char, nr_samples);
- out = outbuf = FX_Alloc(unsigned char, nr_samples);
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
- *in++ = (unsigned char) * r++;
- *in++ = (unsigned char) * g++;
- *in++ = (unsigned char) * b++;
- }
- cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
- *r++ = (int) * out++;
- *g++ = (int) * out++;
- *b++ = (int) * out++;
- }
- FX_Free(inbuf);
- FX_Free(outbuf);
- } else {
- unsigned short *inbuf, *outbuf, *in, *out;
- max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
- in = inbuf = FX_Alloc(unsigned short, nr_samples);
- out = outbuf = FX_Alloc(unsigned short, nr_samples);
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
- *in++ = (unsigned short) * r++;
- *in++ = (unsigned short) * g++;
- *in++ = (unsigned short) * b++;
- }
- cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
- *r++ = (int) * out++;
- *g++ = (int) * out++;
- *b++ = (int) * out++;
- }
- FX_Free(inbuf);
- FX_Free(outbuf);
- }
- } else {
- unsigned char *in, *inbuf, *out, *outbuf;
- max = max_w * max_h;
- nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
- in = inbuf = FX_Alloc(unsigned char, nr_samples);
- out = outbuf = FX_Alloc(unsigned char, nr_samples);
- image->comps = (opj_image_comp_t*)
- realloc(image->comps, (image->numcomps + 2) * sizeof(opj_image_comp_t));
- if(image->numcomps == 2) {
- image->comps[3] = image->comps[1];
- }
- image->comps[1] = image->comps[0];
- image->comps[2] = image->comps[0];
- image->comps[1].data = FX_Alloc(int, (size_t)max);
- FXSYS_memset8(image->comps[1].data, 0, sizeof(int) * (size_t)max);
- image->comps[2].data = FX_Alloc(int, (size_t)max);
- FXSYS_memset8(image->comps[2].data, 0, sizeof(int) * (size_t)max);
- image->numcomps += 2;
- r = image->comps[0].data;
- for(i = 0; i < max; ++i) {
- *in++ = (unsigned char) * r++;
- }
- cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
- r = image->comps[0].data;
- g = image->comps[1].data;
- b = image->comps[2].data;
- for(i = 0; i < max; ++i) {
- *r++ = (int) * out++;
- *g++ = (int) * out++;
- *b++ = (int) * out++;
- }
- FX_Free(inbuf);
- FX_Free(outbuf);
- }
- cmsDeleteTransform(transform);
-}
-void color_apply_conversion(opj_image_t *image)
-{
- int *row;
- int enumcs, numcomps;
- numcomps = image->numcomps;
- if(numcomps < 3) {
- return;
- }
- row = (int*)image->icc_profile_buf;
- enumcs = row[0];
- if(enumcs == 14) {
- int *L, *a, *b, *red, *green, *blue, *src0, *src1, *src2;
- double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
- double minL, maxL, mina, maxa, minb, maxb;
- unsigned int default_type, il;
- unsigned int i, max, illu;
- cmsHPROFILE in, out;
- cmsHTRANSFORM transform;
- cmsUInt16Number RGB[3];
- cmsCIELab Lab;
- illu = 0;
- il = 0;
- in = cmsCreateLab4Profile(NULL);
- out = cmsCreate_sRGBProfile();
- transform =
- cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16,
- INTENT_PERCEPTUAL, 0);
- cmsCloseProfile(in);
- cmsCloseProfile(out);
- if(transform == NULL) {
- return;
- }
- prec0 = (double)image->comps[0].prec;
- prec1 = (double)image->comps[1].prec;
- prec2 = (double)image->comps[2].prec;
- default_type = row[1];
- if(default_type == 0x44454600) {
- rl = 100;
- ra = 170;
- rb = 200;
- ol = 0;
- oa = pow(2, prec1 - 1);
- ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
- } else {
- rl = row[2];
- ra = row[4];
- rb = row[6];
- ol = row[3];
- oa = row[5];
- ob = row[7];
- }
- L = src0 = image->comps[0].data;
- a = src1 = image->comps[1].data;
- b = src2 = image->comps[2].data;
- max = image->comps[0].w * image->comps[0].h;
- red = FX_Alloc(int, max);
- image->comps[0].data = red;
- green = FX_Alloc(int, max);
- image->comps[1].data = green;
- blue = FX_Alloc(int, max);
- image->comps[2].data = blue;
- minL = -(rl * ol) / (pow(2, prec0) - 1);
- maxL = minL + rl;
- mina = -(ra * oa) / (pow(2, prec1) - 1);
- maxa = mina + ra;
- minb = -(rb * ob) / (pow(2, prec2) - 1);
- maxb = minb + rb;
- for(i = 0; i < max; ++i) {
- Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1);
- ++L;
- Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1);
- ++a;
- Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1);
- ++b;
- cmsDoTransform(transform, &Lab, RGB, 1);
- *red++ = RGB[0];
- *green++ = RGB[1];
- *blue++ = RGB[2];
- }
- cmsDeleteTransform(transform);
- FX_Free(src0);
- FX_Free(src1);
- FX_Free(src2);
- image->color_space = OPJ_CLRSPC_SRGB;
- image->comps[0].prec = 16;
- image->comps[1].prec = 16;
- image->comps[2].prec = 16;
- return;
- }
-}
-class CJPX_Decoder : public CFX_Object
-{
-public:
- CJPX_Decoder();
- ~CJPX_Decoder();
- FX_BOOL Init(const unsigned char* src_data, int src_size);
- void GetInfo(FX_DWORD& width, FX_DWORD& height, FX_DWORD& codestream_nComps, FX_DWORD& output_nComps);
- FX_BOOL Decode(FX_LPBYTE dest_buf, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets);
- FX_LPCBYTE m_SrcData;
- int m_SrcSize;
- opj_image_t *image;
- opj_codec_t* l_codec;
- opj_stream_t *l_stream;
- FX_BOOL m_useColorSpace;
-};
-CJPX_Decoder::CJPX_Decoder(): image(NULL), l_codec(NULL), l_stream(NULL), m_useColorSpace(FALSE)
-{
-}
-CJPX_Decoder::~CJPX_Decoder()
-{
- if(l_codec) {
- opj_destroy_codec(l_codec);
- }
- if(l_stream) {
- opj_stream_destroy(l_stream);
- }
- if(image) {
- opj_image_destroy(image);
- }
-}
-FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, int src_size)
-{
- opj_dparameters_t parameters;
- try {
- image = NULL;
- m_SrcData = src_data;
- m_SrcSize = src_size;
- decodeData srcData;
- srcData.offset = 0;
- srcData.src_size = src_size;
- srcData.src_data = src_data;
- l_stream = fx_opj_stream_create_memory_stream(&srcData, OPJ_J2K_STREAM_CHUNK_SIZE, 1);
- if (l_stream == NULL) {
- return FALSE;
- }
- opj_set_default_decoder_parameters(&parameters);
- parameters.decod_format = 0;
- parameters.cod_format = 3;
- if(FXSYS_memcmp32(m_SrcData, "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a", 12) == 0) {
- l_codec = opj_create_decompress(OPJ_CODEC_JP2);
- parameters.decod_format = 1;
- } else {
- l_codec = opj_create_decompress(OPJ_CODEC_J2K);
- }
- if(!l_codec) {
- return FALSE;
- }
- opj_set_info_handler(l_codec, fx_info_callback, 00);
- opj_set_warning_handler(l_codec, fx_warning_callback, 00);
- opj_set_error_handler(l_codec, fx_error_callback, 00);
- if ( !opj_setup_decoder(l_codec, &parameters) ) {
- return FALSE;
- }
- if(! opj_read_header(l_stream, l_codec, &image)) {
- image = NULL;
- return FALSE;
- }
- if(this->m_useColorSpace) {
- image->useColorSpace = 1;
- } else {
- image->useColorSpace = 0;
- }
- if (!parameters.nb_tile_to_decode) {
- if (!opj_set_decode_area(l_codec, image, parameters.DA_x0,
- parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)) {
- opj_image_destroy(image);
- image = NULL;
- return FALSE;
- }
- if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) {
- opj_image_destroy(image);
- image = NULL;
- return FALSE;
- }
- } else {
- if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
- return FALSE;
- }
- }
- opj_stream_destroy(l_stream);
- l_stream = NULL;
- if( image->color_space != OPJ_CLRSPC_SYCC
- && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
- && image->comps[1].dx != 1 ) {
- image->color_space = OPJ_CLRSPC_SYCC;
- } else if (image->numcomps <= 2) {
- image->color_space = OPJ_CLRSPC_GRAY;
- }
- if(image->color_space == OPJ_CLRSPC_SYCC) {
- color_sycc_to_rgb(image);
- }
- if(image->icc_profile_buf && !image->useColorSpace) {
- FX_Free(image->icc_profile_buf);
- image->icc_profile_buf = NULL;
- image->icc_profile_len = 0;
- }
- if(!image) {
- return FALSE;
- }
- } catch (...) {
- return FALSE;
- }
- return TRUE;
-}
-void CJPX_Decoder::GetInfo(FX_DWORD& width, FX_DWORD& height, FX_DWORD& codestream_nComps, FX_DWORD& output_nComps)
-{
- width = (FX_DWORD)image->x1;
- height = (FX_DWORD)image->y1;
- output_nComps = codestream_nComps = (FX_DWORD)image->numcomps;
-}
-FX_BOOL CJPX_Decoder::Decode(FX_LPBYTE dest_buf, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets)
-{
- FX_BYTE** channel_bufs;
- int* adjust_comps;
- int i, wid, hei, row, col, channel, src;
- FX_BOOL flag;
- FX_LPBYTE pChannel, pScanline, pPixel;
- try {
- if(image->comps[0].w != image->x1 || image->comps[0].h != image->y1) {
- return FALSE;
- }
- if(pitch < (int)(image->comps[0].w * 8 * image->numcomps + 31) >> 5 << 2) {
- return FALSE;
- }
- FXSYS_memset8(dest_buf, 0xff, image->y1 * pitch);
- channel_bufs = FX_Alloc(FX_BYTE*, image->numcomps);
- if (channel_bufs == NULL) {
- return FALSE;
- }
- adjust_comps = FX_Alloc(int, image->numcomps);
- if (adjust_comps == NULL) {
- FX_Free(channel_bufs);
- return FALSE;
- }
- flag = TRUE;
- for (i = 0; i < (int)image->numcomps; i ++) {
- channel_bufs[i] = dest_buf + offsets[i];
- adjust_comps[i] = image->comps[i].prec - 8;
- if(i > 0) {
- if(image->comps[i].dx != image->comps[i - 1].dx
- || image->comps[i].dy != image->comps[i - 1].dy
- || image->comps[i].prec != image->comps[i - 1].prec) {
- flag = FALSE;
- goto failed;
- }
- }
- }
- wid = image->comps[0].w;
- hei = image->comps[0].h;
- for (channel = 0; channel < (int)image->numcomps; channel++) {
- pChannel = channel_bufs[channel];
- if(adjust_comps[channel] < 0) {
- for(row = 0; row < hei; row++) {
- pScanline = pChannel + row * pitch;
- for (col = 0; col < wid; col++) {
- pPixel = pScanline + col * image->numcomps;
- src = image->comps[channel].data[row * wid + col];
- src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
- if (adjust_comps[channel] > 0) {
- *pPixel = 0;
- } else {
- *pPixel = (FX_BYTE)(src << -adjust_comps[channel]);
- }
- }
- }
- } else {
- for(row = 0; row < hei; row++) {
- pScanline = pChannel + row * pitch;
- for (col = 0; col < wid; col++) {
- pPixel = pScanline + col * image->numcomps;
- if (!image->comps[channel].data) continue;
- src = image->comps[channel].data[row * wid + col];
- src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
- if (adjust_comps[channel] - 1 < 0) {
- *pPixel = (FX_BYTE)((src >> adjust_comps[channel]));
- } else {
- int tmpPixel = (src >> adjust_comps[channel]) + ((src >> (adjust_comps[channel] - 1)) % 2);
- if (tmpPixel > 255) {
- tmpPixel = 255;
- } else if (tmpPixel < 0) {
- tmpPixel = 0;
- }
- *pPixel = (FX_BYTE)tmpPixel;
- }
- }
- }
- }
- }
- } catch (...) {
- if (channel_bufs) {
- FX_Free(channel_bufs);
- }
- FX_Free(adjust_comps);
- return FALSE;
- }
- FX_Free(channel_bufs);
- FX_Free(adjust_comps);
- return TRUE;
-failed:
- FX_Free(channel_bufs);
- FX_Free(adjust_comps);
- return FALSE;
-}
-void initialize_transition_table();
-void initialize_significance_luts();
-void initialize_sign_lut();
-CCodec_JpxModule::CCodec_JpxModule()
-{
-}
-void* CCodec_JpxModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size , FX_BOOL useColorSpace)
-{
- CJPX_Decoder* pDecoder = FX_NEW CJPX_Decoder;
- if (pDecoder == NULL) {
- return NULL;
- }
- pDecoder->m_useColorSpace = useColorSpace;
- if (!pDecoder->Init(src_buf, src_size)) {
- delete pDecoder;
- return NULL;
- }
- return pDecoder;
-}
-void CCodec_JpxModule::GetImageInfo(FX_LPVOID ctx, FX_DWORD& width, FX_DWORD& height,
- FX_DWORD& codestream_nComps, FX_DWORD& output_nComps)
-{
- CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;
- pDecoder->GetInfo(width, height, codestream_nComps, output_nComps);
-}
-FX_BOOL CCodec_JpxModule::Decode(void* ctx, FX_LPBYTE dest_data, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets)
-{
- CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;
- return pDecoder->Decode(dest_data, pitch, bTranslateColor, offsets);
-}
-void CCodec_JpxModule::DestroyDecoder(void* ctx)
-{
- CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;
- delete pDecoder;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../../include/fxcodec/fx_codec.h"
+#include "codec_int.h"
+#include "../fx_libopenjpeg/libopenjpeg20/openjpeg.h"
+#include "../lcms2/include/fx_lcms2.h"
+static void fx_error_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+}
+static void fx_warning_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+}
+static void fx_info_callback(const char *msg, void *client_data)
+{
+ (void)client_data;
+}
+typedef struct {
+ const unsigned char* src_data;
+ int src_size;
+ int offset;
+} decodeData;
+static OPJ_SIZE_T opj_read_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, decodeData* srcData)
+{
+ if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
+ return -1;
+ }
+ OPJ_SIZE_T readlength = p_nb_bytes;
+ OPJ_SIZE_T bufferLength = (OPJ_SIZE_T)(srcData->src_size - srcData->offset);
+ if(bufferLength <= 0) {
+ return 0;
+ }
+ if(bufferLength <= p_nb_bytes) {
+ readlength = bufferLength;
+ }
+ memcpy(p_buffer, &(srcData->src_data[srcData->offset]), readlength);
+ srcData->offset += (int)readlength;
+ return readlength;
+}
+static OPJ_SIZE_T opj_write_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, decodeData* srcData)
+{
+ if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
+ return -1;
+ }
+ OPJ_SIZE_T writeLength = p_nb_bytes;
+ OPJ_SIZE_T bufferLength = (OPJ_SIZE_T)(srcData->src_size - srcData->offset);
+ if(bufferLength <= p_nb_bytes) {
+ writeLength = bufferLength;
+ }
+ memcpy((void*&)(srcData->src_data[srcData->offset]), p_buffer, writeLength);
+ srcData->offset += (int)writeLength;
+ return writeLength;
+}
+static OPJ_OFF_T opj_skip_from_memory (OPJ_OFF_T p_nb_bytes, decodeData* srcData)
+{
+ if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
+ return -1;
+ }
+ OPJ_OFF_T postion = srcData->offset + p_nb_bytes;
+ if(postion < 0 ) {
+ postion = 0;
+ } else if (postion > srcData->src_size) {
+ }
+ srcData->offset = (int)postion;
+ return p_nb_bytes;
+}
+static OPJ_BOOL opj_seek_from_memory (OPJ_OFF_T p_nb_bytes, decodeData * srcData)
+{
+ if(srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL || srcData->offset >= srcData->src_size) {
+ return -1;
+ }
+ srcData->offset = (int)p_nb_bytes;
+ if(srcData->offset < 0) {
+ srcData->offset = 0;
+ } else if(srcData->offset > srcData->src_size) {
+ srcData->offset = srcData->src_size;
+ }
+ return OPJ_TRUE;
+}
+opj_stream_t* fx_opj_stream_create_memory_stream (decodeData* data, OPJ_SIZE_T p_size, OPJ_BOOL p_is_read_stream)
+{
+ opj_stream_t* l_stream = 00;
+ if (!data || ! data->src_data || data->src_size <= 0 ) {
+ return NULL;
+ }
+ l_stream = opj_stream_create(p_size, p_is_read_stream);
+ if (! l_stream) {
+ return NULL;
+ }
+ opj_stream_set_user_data_v3(l_stream, data, NULL);
+ opj_stream_set_user_data_length(l_stream, data->src_size);
+ opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_memory);
+ opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_memory);
+ opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_memory);
+ opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_memory);
+ return l_stream;
+}
+static void sycc_to_rgb(int offset, int upb, int y, int cb, int cr,
+ int *out_r, int *out_g, int *out_b)
+{
+ int r, g, b;
+ cb -= offset;
+ cr -= offset;
+ r = y + (int)(1.402 * (float)cr);
+ if(r < 0) {
+ r = 0;
+ } else if(r > upb) {
+ r = upb;
+ } *out_r = r;
+ g = y - (int)(0.344 * (float)cb + 0.714 * (float)cr);
+ if(g < 0) {
+ g = 0;
+ } else if(g > upb) {
+ g = upb;
+ } *out_g = g;
+ b = y + (int)(1.772 * (float)cb);
+ if(b < 0) {
+ b = 0;
+ } else if(b > upb) {
+ b = upb;
+ } *out_b = b;
+}
+static void sycc444_to_rgb(opj_image_t *img)
+{
+ int *d0, *d1, *d2, *r, *g, *b;
+ const int *y, *cb, *cr;
+ int maxw, maxh, max, i, offset, upb;
+ i = (int)img->comps[0].prec;
+ offset = 1 << (i - 1);
+ upb = (1 << i) - 1;
+ maxw = (int)img->comps[0].w;
+ maxh = (int)img->comps[0].h;
+ max = maxw * maxh;
+ y = img->comps[0].data;
+ cb = img->comps[1].data;
+ cr = img->comps[2].data;
+ d0 = r = FX_Alloc(int, (size_t)max);
+ d1 = g = FX_Alloc(int, (size_t)max);
+ d2 = b = FX_Alloc(int, (size_t)max);
+ for(i = 0; i < max; ++i) {
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y;
+ ++cb;
+ ++cr;
+ ++r;
+ ++g;
+ ++b;
+ }
+ FX_Free(img->comps[0].data);
+ img->comps[0].data = d0;
+ FX_Free(img->comps[1].data);
+ img->comps[1].data = d1;
+ FX_Free(img->comps[2].data);
+ img->comps[2].data = d2;
+}
+static void sycc422_to_rgb(opj_image_t *img)
+{
+ int *d0, *d1, *d2, *r, *g, *b;
+ const int *y, *cb, *cr;
+ int maxw, maxh, max, offset, upb;
+ int i, j;
+ i = (int)img->comps[0].prec;
+ offset = 1 << (i - 1);
+ upb = (1 << i) - 1;
+ maxw = (int)img->comps[0].w;
+ maxh = (int)img->comps[0].h;
+ max = maxw * maxh;
+ y = img->comps[0].data;
+ cb = img->comps[1].data;
+ cr = img->comps[2].data;
+ d0 = r = FX_Alloc(int, (size_t)max);
+ d1 = g = FX_Alloc(int, (size_t)max);
+ d2 = b = FX_Alloc(int, (size_t)max);
+ for(i = 0; i < maxh; ++i) {
+ for(j = 0; j < maxw; j += 2) {
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y;
+ ++r;
+ ++g;
+ ++b;
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y;
+ ++r;
+ ++g;
+ ++b;
+ ++cb;
+ ++cr;
+ }
+ }
+ FX_Free(img->comps[0].data);
+ img->comps[0].data = d0;
+ FX_Free(img->comps[1].data);
+ img->comps[1].data = d1;
+ FX_Free(img->comps[2].data);
+ img->comps[2].data = d2;
+ img->comps[1].w = maxw;
+ img->comps[1].h = maxh;
+ img->comps[2].w = maxw;
+ img->comps[2].h = maxh;
+ img->comps[1].w = (OPJ_UINT32)maxw;
+ img->comps[1].h = (OPJ_UINT32)maxh;
+ img->comps[2].w = (OPJ_UINT32)maxw;
+ img->comps[2].h = (OPJ_UINT32)maxh;
+ img->comps[1].dx = img->comps[0].dx;
+ img->comps[2].dx = img->comps[0].dx;
+ img->comps[1].dy = img->comps[0].dy;
+ img->comps[2].dy = img->comps[0].dy;
+}
+static void sycc420_to_rgb(opj_image_t *img)
+{
+ int *d0, *d1, *d2, *r, *g, *b, *nr, *ng, *nb;
+ const int *y, *cb, *cr, *ny;
+ int maxw, maxh, max, offset, upb;
+ int i, j;
+ i = (int)img->comps[0].prec;
+ offset = 1 << (i - 1);
+ upb = (1 << i) - 1;
+ maxw = (int)img->comps[0].w;
+ maxh = (int)img->comps[0].h;
+ max = maxw * maxh;
+ y = img->comps[0].data;
+ cb = img->comps[1].data;
+ cr = img->comps[2].data;
+ d0 = r = FX_Alloc(int, (size_t)max);
+ d1 = g = FX_Alloc(int, (size_t)max);
+ d2 = b = FX_Alloc(int, (size_t)max);
+ for(i = 0; i < maxh; i += 2) {
+ ny = y + maxw;
+ nr = r + maxw;
+ ng = g + maxw;
+ nb = b + maxw;
+ for(j = 0; j < maxw; j += 2) {
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y;
+ ++r;
+ ++g;
+ ++b;
+ sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b);
+ ++y;
+ ++r;
+ ++g;
+ ++b;
+ sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
+ ++ny;
+ ++nr;
+ ++ng;
+ ++nb;
+ sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb);
+ ++ny;
+ ++nr;
+ ++ng;
+ ++nb;
+ ++cb;
+ ++cr;
+ }
+ y += maxw;
+ r += maxw;
+ g += maxw;
+ b += maxw;
+ }
+ FX_Free(img->comps[0].data);
+ img->comps[0].data = d0;
+ FX_Free(img->comps[1].data);
+ img->comps[1].data = d1;
+ FX_Free(img->comps[2].data);
+ img->comps[2].data = d2;
+ img->comps[1].w = maxw;
+ img->comps[1].h = maxh;
+ img->comps[2].w = maxw;
+ img->comps[2].h = maxh;
+ img->comps[1].w = (OPJ_UINT32)maxw;
+ img->comps[1].h = (OPJ_UINT32)maxh;
+ img->comps[2].w = (OPJ_UINT32)maxw;
+ img->comps[2].h = (OPJ_UINT32)maxh;
+ img->comps[1].dx = img->comps[0].dx;
+ img->comps[2].dx = img->comps[0].dx;
+ img->comps[1].dy = img->comps[0].dy;
+ img->comps[2].dy = img->comps[0].dy;
+}
+void color_sycc_to_rgb(opj_image_t *img)
+{
+ if(img->numcomps < 3) {
+ img->color_space = OPJ_CLRSPC_GRAY;
+ return;
+ }
+ if((img->comps[0].dx == 1)
+ && (img->comps[1].dx == 2)
+ && (img->comps[2].dx == 2)
+ && (img->comps[0].dy == 1)
+ && (img->comps[1].dy == 2)
+ && (img->comps[2].dy == 2)) {
+ sycc420_to_rgb(img);
+ } else if((img->comps[0].dx == 1)
+ && (img->comps[1].dx == 2)
+ && (img->comps[2].dx == 2)
+ && (img->comps[0].dy == 1)
+ && (img->comps[1].dy == 1)
+ && (img->comps[2].dy == 1)) {
+ sycc422_to_rgb(img);
+ } else if((img->comps[0].dx == 1)
+ && (img->comps[1].dx == 1)
+ && (img->comps[2].dx == 1)
+ && (img->comps[0].dy == 1)
+ && (img->comps[1].dy == 1)
+ && (img->comps[2].dy == 1)) {
+ sycc444_to_rgb(img);
+ } else {
+ return;
+ }
+ img->color_space = OPJ_CLRSPC_SRGB;
+}
+void color_apply_icc_profile(opj_image_t *image)
+{
+ cmsHPROFILE in_prof, out_prof;
+ cmsHTRANSFORM transform;
+ cmsColorSpaceSignature in_space, out_space;
+ cmsUInt32Number intent, in_type, out_type, nr_samples;
+ int *r, *g, *b;
+ int prec, i, max, max_w, max_h;
+ OPJ_COLOR_SPACE oldspace;
+ in_prof =
+ cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len);
+ if(in_prof == NULL) {
+ return;
+ }
+ in_space = cmsGetPCS(in_prof);
+ out_space = cmsGetColorSpace(in_prof);
+ intent = cmsGetHeaderRenderingIntent(in_prof);
+ max_w = (int)image->comps[0].w;
+ max_h = (int)image->comps[0].h;
+ prec = (int)image->comps[0].prec;
+ oldspace = image->color_space;
+ if(out_space == cmsSigRgbData) {
+ if( prec <= 8 ) {
+ in_type = TYPE_RGB_8;
+ out_type = TYPE_RGB_8;
+ } else {
+ in_type = TYPE_RGB_16;
+ out_type = TYPE_RGB_16;
+ }
+ out_prof = cmsCreate_sRGBProfile();
+ image->color_space = OPJ_CLRSPC_SRGB;
+ } else if(out_space == cmsSigGrayData) {
+ if( prec <= 8 ) {
+ in_type = TYPE_GRAY_8;
+ out_type = TYPE_RGB_8;
+ } else {
+ in_type = TYPE_GRAY_16;
+ out_type = TYPE_RGB_16;
+ }
+ out_prof = cmsCreate_sRGBProfile();
+ image->color_space = OPJ_CLRSPC_SRGB;
+ } else if(out_space == cmsSigYCbCrData) {
+ in_type = TYPE_YCbCr_16;
+ out_type = TYPE_RGB_16;
+ out_prof = cmsCreate_sRGBProfile();
+ image->color_space = OPJ_CLRSPC_SRGB;
+ } else {
+ return;
+ }
+ transform = cmsCreateTransform(in_prof, in_type,
+ out_prof, out_type, intent, 0);
+ cmsCloseProfile(in_prof);
+ cmsCloseProfile(out_prof);
+ if(transform == NULL) {
+ image->color_space = oldspace;
+ return;
+ }
+ if(image->numcomps > 2) {
+ if( prec <= 8 ) {
+ unsigned char *inbuf, *outbuf, *in, *out;
+ max = max_w * max_h;
+ nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned char);
+ in = inbuf = FX_Alloc(unsigned char, nr_samples);
+ out = outbuf = FX_Alloc(unsigned char, nr_samples);
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+ for(i = 0; i < max; ++i) {
+ *in++ = (unsigned char) * r++;
+ *in++ = (unsigned char) * g++;
+ *in++ = (unsigned char) * b++;
+ }
+ cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+ for(i = 0; i < max; ++i) {
+ *r++ = (int) * out++;
+ *g++ = (int) * out++;
+ *b++ = (int) * out++;
+ }
+ FX_Free(inbuf);
+ FX_Free(outbuf);
+ } else {
+ unsigned short *inbuf, *outbuf, *in, *out;
+ max = max_w * max_h;
+ nr_samples = (cmsUInt32Number)max * 3 * (cmsUInt32Number)sizeof(unsigned short);
+ in = inbuf = FX_Alloc(unsigned short, nr_samples);
+ out = outbuf = FX_Alloc(unsigned short, nr_samples);
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+ for(i = 0; i < max; ++i) {
+ *in++ = (unsigned short) * r++;
+ *in++ = (unsigned short) * g++;
+ *in++ = (unsigned short) * b++;
+ }
+ cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+ for(i = 0; i < max; ++i) {
+ *r++ = (int) * out++;
+ *g++ = (int) * out++;
+ *b++ = (int) * out++;
+ }
+ FX_Free(inbuf);
+ FX_Free(outbuf);
+ }
+ } else {
+ unsigned char *in, *inbuf, *out, *outbuf;
+ max = max_w * max_h;
+ nr_samples = (cmsUInt32Number)max * 3 * sizeof(unsigned char);
+ in = inbuf = FX_Alloc(unsigned char, nr_samples);
+ out = outbuf = FX_Alloc(unsigned char, nr_samples);
+ image->comps = (opj_image_comp_t*)
+ realloc(image->comps, (image->numcomps + 2) * sizeof(opj_image_comp_t));
+ if(image->numcomps == 2) {
+ image->comps[3] = image->comps[1];
+ }
+ image->comps[1] = image->comps[0];
+ image->comps[2] = image->comps[0];
+ image->comps[1].data = FX_Alloc(int, (size_t)max);
+ FXSYS_memset8(image->comps[1].data, 0, sizeof(int) * (size_t)max);
+ image->comps[2].data = FX_Alloc(int, (size_t)max);
+ FXSYS_memset8(image->comps[2].data, 0, sizeof(int) * (size_t)max);
+ image->numcomps += 2;
+ r = image->comps[0].data;
+ for(i = 0; i < max; ++i) {
+ *in++ = (unsigned char) * r++;
+ }
+ cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max);
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+ for(i = 0; i < max; ++i) {
+ *r++ = (int) * out++;
+ *g++ = (int) * out++;
+ *b++ = (int) * out++;
+ }
+ FX_Free(inbuf);
+ FX_Free(outbuf);
+ }
+ cmsDeleteTransform(transform);
+}
+void color_apply_conversion(opj_image_t *image)
+{
+ int *row;
+ int enumcs, numcomps;
+ numcomps = image->numcomps;
+ if(numcomps < 3) {
+ return;
+ }
+ row = (int*)image->icc_profile_buf;
+ enumcs = row[0];
+ if(enumcs == 14) {
+ int *L, *a, *b, *red, *green, *blue, *src0, *src1, *src2;
+ double rl, ol, ra, oa, rb, ob, prec0, prec1, prec2;
+ double minL, maxL, mina, maxa, minb, maxb;
+ unsigned int default_type, il;
+ unsigned int i, max, illu;
+ cmsHPROFILE in, out;
+ cmsHTRANSFORM transform;
+ cmsUInt16Number RGB[3];
+ cmsCIELab Lab;
+ illu = 0;
+ il = 0;
+ in = cmsCreateLab4Profile(NULL);
+ out = cmsCreate_sRGBProfile();
+ transform =
+ cmsCreateTransform(in, TYPE_Lab_DBL, out, TYPE_RGB_16,
+ INTENT_PERCEPTUAL, 0);
+ cmsCloseProfile(in);
+ cmsCloseProfile(out);
+ if(transform == NULL) {
+ return;
+ }
+ prec0 = (double)image->comps[0].prec;
+ prec1 = (double)image->comps[1].prec;
+ prec2 = (double)image->comps[2].prec;
+ default_type = row[1];
+ if(default_type == 0x44454600) {
+ rl = 100;
+ ra = 170;
+ rb = 200;
+ ol = 0;
+ oa = pow(2, prec1 - 1);
+ ob = pow(2, prec2 - 2) + pow(2, prec2 - 3);
+ } else {
+ rl = row[2];
+ ra = row[4];
+ rb = row[6];
+ ol = row[3];
+ oa = row[5];
+ ob = row[7];
+ }
+ L = src0 = image->comps[0].data;
+ a = src1 = image->comps[1].data;
+ b = src2 = image->comps[2].data;
+ max = image->comps[0].w * image->comps[0].h;
+ red = FX_Alloc(int, max);
+ image->comps[0].data = red;
+ green = FX_Alloc(int, max);
+ image->comps[1].data = green;
+ blue = FX_Alloc(int, max);
+ image->comps[2].data = blue;
+ minL = -(rl * ol) / (pow(2, prec0) - 1);
+ maxL = minL + rl;
+ mina = -(ra * oa) / (pow(2, prec1) - 1);
+ maxa = mina + ra;
+ minb = -(rb * ob) / (pow(2, prec2) - 1);
+ maxb = minb + rb;
+ for(i = 0; i < max; ++i) {
+ Lab.L = minL + (double)(*L) * (maxL - minL) / (pow(2, prec0) - 1);
+ ++L;
+ Lab.a = mina + (double)(*a) * (maxa - mina) / (pow(2, prec1) - 1);
+ ++a;
+ Lab.b = minb + (double)(*b) * (maxb - minb) / (pow(2, prec2) - 1);
+ ++b;
+ cmsDoTransform(transform, &Lab, RGB, 1);
+ *red++ = RGB[0];
+ *green++ = RGB[1];
+ *blue++ = RGB[2];
+ }
+ cmsDeleteTransform(transform);
+ FX_Free(src0);
+ FX_Free(src1);
+ FX_Free(src2);
+ image->color_space = OPJ_CLRSPC_SRGB;
+ image->comps[0].prec = 16;
+ image->comps[1].prec = 16;
+ image->comps[2].prec = 16;
+ return;
+ }
+}
+class CJPX_Decoder : public CFX_Object
+{
+public:
+ CJPX_Decoder();
+ ~CJPX_Decoder();
+ FX_BOOL Init(const unsigned char* src_data, int src_size);
+ void GetInfo(FX_DWORD& width, FX_DWORD& height, FX_DWORD& codestream_nComps, FX_DWORD& output_nComps);
+ FX_BOOL Decode(FX_LPBYTE dest_buf, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets);
+ FX_LPCBYTE m_SrcData;
+ int m_SrcSize;
+ opj_image_t *image;
+ opj_codec_t* l_codec;
+ opj_stream_t *l_stream;
+ FX_BOOL m_useColorSpace;
+};
+CJPX_Decoder::CJPX_Decoder(): image(NULL), l_codec(NULL), l_stream(NULL), m_useColorSpace(FALSE)
+{
+}
+CJPX_Decoder::~CJPX_Decoder()
+{
+ if(l_codec) {
+ opj_destroy_codec(l_codec);
+ }
+ if(l_stream) {
+ opj_stream_destroy(l_stream);
+ }
+ if(image) {
+ opj_image_destroy(image);
+ }
+}
+FX_BOOL CJPX_Decoder::Init(const unsigned char* src_data, int src_size)
+{
+ opj_dparameters_t parameters;
+ try {
+ image = NULL;
+ m_SrcData = src_data;
+ m_SrcSize = src_size;
+ decodeData srcData;
+ srcData.offset = 0;
+ srcData.src_size = src_size;
+ srcData.src_data = src_data;
+ l_stream = fx_opj_stream_create_memory_stream(&srcData, OPJ_J2K_STREAM_CHUNK_SIZE, 1);
+ if (l_stream == NULL) {
+ return FALSE;
+ }
+ opj_set_default_decoder_parameters(&parameters);
+ parameters.decod_format = 0;
+ parameters.cod_format = 3;
+ if(FXSYS_memcmp32(m_SrcData, "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a", 12) == 0) {
+ l_codec = opj_create_decompress(OPJ_CODEC_JP2);
+ parameters.decod_format = 1;
+ } else {
+ l_codec = opj_create_decompress(OPJ_CODEC_J2K);
+ }
+ if(!l_codec) {
+ return FALSE;
+ }
+ opj_set_info_handler(l_codec, fx_info_callback, 00);
+ opj_set_warning_handler(l_codec, fx_warning_callback, 00);
+ opj_set_error_handler(l_codec, fx_error_callback, 00);
+ if ( !opj_setup_decoder(l_codec, &parameters) ) {
+ return FALSE;
+ }
+ if(! opj_read_header(l_stream, l_codec, &image)) {
+ image = NULL;
+ return FALSE;
+ }
+ if(this->m_useColorSpace) {
+ image->useColorSpace = 1;
+ } else {
+ image->useColorSpace = 0;
+ }
+ if (!parameters.nb_tile_to_decode) {
+ if (!opj_set_decode_area(l_codec, image, parameters.DA_x0,
+ parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)) {
+ opj_image_destroy(image);
+ image = NULL;
+ return FALSE;
+ }
+ if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) {
+ opj_image_destroy(image);
+ image = NULL;
+ return FALSE;
+ }
+ } else {
+ if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
+ return FALSE;
+ }
+ }
+ opj_stream_destroy(l_stream);
+ l_stream = NULL;
+ if( image->color_space != OPJ_CLRSPC_SYCC
+ && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
+ && image->comps[1].dx != 1 ) {
+ image->color_space = OPJ_CLRSPC_SYCC;
+ } else if (image->numcomps <= 2) {
+ image->color_space = OPJ_CLRSPC_GRAY;
+ }
+ if(image->color_space == OPJ_CLRSPC_SYCC) {
+ color_sycc_to_rgb(image);
+ }
+ if(image->icc_profile_buf && !image->useColorSpace) {
+ FX_Free(image->icc_profile_buf);
+ image->icc_profile_buf = NULL;
+ image->icc_profile_len = 0;
+ }
+ if(!image) {
+ return FALSE;
+ }
+ } catch (...) {
+ return FALSE;
+ }
+ return TRUE;
+}
+void CJPX_Decoder::GetInfo(FX_DWORD& width, FX_DWORD& height, FX_DWORD& codestream_nComps, FX_DWORD& output_nComps)
+{
+ width = (FX_DWORD)image->x1;
+ height = (FX_DWORD)image->y1;
+ output_nComps = codestream_nComps = (FX_DWORD)image->numcomps;
+}
+FX_BOOL CJPX_Decoder::Decode(FX_LPBYTE dest_buf, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets)
+{
+ FX_BYTE** channel_bufs;
+ int* adjust_comps;
+ int i, wid, hei, row, col, channel, src;
+ FX_BOOL flag;
+ FX_LPBYTE pChannel, pScanline, pPixel;
+ try {
+ if(image->comps[0].w != image->x1 || image->comps[0].h != image->y1) {
+ return FALSE;
+ }
+ if(pitch < (int)(image->comps[0].w * 8 * image->numcomps + 31) >> 5 << 2) {
+ return FALSE;
+ }
+ FXSYS_memset8(dest_buf, 0xff, image->y1 * pitch);
+ channel_bufs = FX_Alloc(FX_BYTE*, image->numcomps);
+ if (channel_bufs == NULL) {
+ return FALSE;
+ }
+ adjust_comps = FX_Alloc(int, image->numcomps);
+ if (adjust_comps == NULL) {
+ FX_Free(channel_bufs);
+ return FALSE;
+ }
+ flag = TRUE;
+ for (i = 0; i < (int)image->numcomps; i ++) {
+ channel_bufs[i] = dest_buf + offsets[i];
+ adjust_comps[i] = image->comps[i].prec - 8;
+ if(i > 0) {
+ if(image->comps[i].dx != image->comps[i - 1].dx
+ || image->comps[i].dy != image->comps[i - 1].dy
+ || image->comps[i].prec != image->comps[i - 1].prec) {
+ flag = FALSE;
+ goto failed;
+ }
+ }
+ }
+ wid = image->comps[0].w;
+ hei = image->comps[0].h;
+ for (channel = 0; channel < (int)image->numcomps; channel++) {
+ pChannel = channel_bufs[channel];
+ if(adjust_comps[channel] < 0) {
+ for(row = 0; row < hei; row++) {
+ pScanline = pChannel + row * pitch;
+ for (col = 0; col < wid; col++) {
+ pPixel = pScanline + col * image->numcomps;
+ src = image->comps[channel].data[row * wid + col];
+ src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
+ if (adjust_comps[channel] > 0) {
+ *pPixel = 0;
+ } else {
+ *pPixel = (FX_BYTE)(src << -adjust_comps[channel]);
+ }
+ }
+ }
+ } else {
+ for(row = 0; row < hei; row++) {
+ pScanline = pChannel + row * pitch;
+ for (col = 0; col < wid; col++) {
+ pPixel = pScanline + col * image->numcomps;
+ if (!image->comps[channel].data) continue;
+ src = image->comps[channel].data[row * wid + col];
+ src += image->comps[channel].sgnd ? 1 << (image->comps[channel].prec - 1) : 0;
+ if (adjust_comps[channel] - 1 < 0) {
+ *pPixel = (FX_BYTE)((src >> adjust_comps[channel]));
+ } else {
+ int tmpPixel = (src >> adjust_comps[channel]) + ((src >> (adjust_comps[channel] - 1)) % 2);
+ if (tmpPixel > 255) {
+ tmpPixel = 255;
+ } else if (tmpPixel < 0) {
+ tmpPixel = 0;
+ }
+ *pPixel = (FX_BYTE)tmpPixel;
+ }
+ }
+ }
+ }
+ }
+ } catch (...) {
+ if (channel_bufs) {
+ FX_Free(channel_bufs);
+ }
+ FX_Free(adjust_comps);
+ return FALSE;
+ }
+ FX_Free(channel_bufs);
+ FX_Free(adjust_comps);
+ return TRUE;
+failed:
+ FX_Free(channel_bufs);
+ FX_Free(adjust_comps);
+ return FALSE;
+}
+void initialize_transition_table();
+void initialize_significance_luts();
+void initialize_sign_lut();
+CCodec_JpxModule::CCodec_JpxModule()
+{
+}
+void* CCodec_JpxModule::CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size , FX_BOOL useColorSpace)
+{
+ CJPX_Decoder* pDecoder = FX_NEW CJPX_Decoder;
+ if (pDecoder == NULL) {
+ return NULL;
+ }
+ pDecoder->m_useColorSpace = useColorSpace;
+ if (!pDecoder->Init(src_buf, src_size)) {
+ delete pDecoder;
+ return NULL;
+ }
+ return pDecoder;
+}
+void CCodec_JpxModule::GetImageInfo(FX_LPVOID ctx, FX_DWORD& width, FX_DWORD& height,
+ FX_DWORD& codestream_nComps, FX_DWORD& output_nComps)
+{
+ CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;
+ pDecoder->GetInfo(width, height, codestream_nComps, output_nComps);
+}
+FX_BOOL CCodec_JpxModule::Decode(void* ctx, FX_LPBYTE dest_data, int pitch, FX_BOOL bTranslateColor, FX_LPBYTE offsets)
+{
+ CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;
+ return pDecoder->Decode(dest_data, pitch, bTranslateColor, offsets);
+}
+void CCodec_JpxModule::DestroyDecoder(void* ctx)
+{
+ CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx;
+ delete pDecoder;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h b/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h
index 1664257411..214688d3b2 100644
--- a/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h
+++ b/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h
@@ -1,126 +1,126 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_ARITHMETIC_DECODER_H_
-#define _JBIG2_ARITHMETIC_DECODER_H_
-#include "JBig2_Define.h"
-#include "JBig2_BitStream.h"
-#include "JBig2_ArithQe.h"
-typedef struct {
- unsigned int MPS;
- unsigned int I;
-} JBig2ArithCtx;
-class CJBig2_ArithDecoder : public CJBig2_Object
-{
-public:
-
- CJBig2_ArithDecoder(CJBig2_BitStream *pStream);
-
- ~CJBig2_ArithDecoder();
-
- int DECODE(JBig2ArithCtx *pCX);
-private:
-
- void INITDEC();
-
- void BYTEIN();
- unsigned char B;
- unsigned int C;
- unsigned int A;
- unsigned int CT;
- CJBig2_BitStream *m_pStream;
-};
-inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream *pStream)
-{
- m_pStream = pStream;
- INITDEC();
-}
-inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder()
-{
-}
-inline void CJBig2_ArithDecoder::INITDEC()
-{
- B = m_pStream->getCurByte_arith();
- C = (B ^ 0xff) << 16;;
- BYTEIN();
- C = C << 7;
- CT = CT - 7;
- A = 0x8000;
-}
-inline void CJBig2_ArithDecoder::BYTEIN()
-{
- unsigned char B1;
- if(B == 0xff) {
- B1 = m_pStream->getNextByte_arith();
- if(B1 > 0x8f) {
- CT = 8;
- } else {
- m_pStream->incByteIdx();
- B = B1;
- C = C + 0xfe00 - (B << 9);
- CT = 7;
- }
- } else {
- m_pStream->incByteIdx();
- B = m_pStream->getCurByte_arith();
- C = C + 0xff00 - (B << 8);
- CT = 8;
- }
-}
-inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx *pCX)
-{
- int D;
- const JBig2ArithQe * qe = &QeTable[pCX->I];
- A = A - qe->Qe;
- if((C >> 16) < A) {
- if(A & 0x8000) {
- D = pCX->MPS;
- } else {
- if(A < qe->Qe) {
- D = 1 - pCX->MPS;
- if(qe->nSwitch == 1) {
- pCX->MPS = 1 - pCX->MPS;
- }
- pCX->I = qe->NLPS;
- } else {
- D = pCX->MPS;
- pCX->I = qe->NMPS;
- }
- do {
- if (CT == 0) {
- BYTEIN();
- }
- A <<= 1;
- C <<= 1;
- CT--;
- } while ((A & 0x8000) == 0);
- }
- } else {
- C -= A << 16;
- if(A < qe->Qe) {
- A = qe->Qe;
- D = pCX->MPS;
- pCX->I = qe->NMPS;
- } else {
- A = qe->Qe;
- D = 1 - pCX->MPS;
- if(qe->nSwitch == 1) {
- pCX->MPS = 1 - pCX->MPS;
- }
- pCX->I = qe->NLPS;
- }
- do {
- if (CT == 0) {
- BYTEIN();
- }
- A <<= 1;
- C <<= 1;
- CT--;
- } while ((A & 0x8000) == 0);
- }
- return D;
-}
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_ARITHMETIC_DECODER_H_
+#define _JBIG2_ARITHMETIC_DECODER_H_
+#include "JBig2_Define.h"
+#include "JBig2_BitStream.h"
+#include "JBig2_ArithQe.h"
+typedef struct {
+ unsigned int MPS;
+ unsigned int I;
+} JBig2ArithCtx;
+class CJBig2_ArithDecoder : public CJBig2_Object
+{
+public:
+
+ CJBig2_ArithDecoder(CJBig2_BitStream *pStream);
+
+ ~CJBig2_ArithDecoder();
+
+ int DECODE(JBig2ArithCtx *pCX);
+private:
+
+ void INITDEC();
+
+ void BYTEIN();
+ unsigned char B;
+ unsigned int C;
+ unsigned int A;
+ unsigned int CT;
+ CJBig2_BitStream *m_pStream;
+};
+inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream *pStream)
+{
+ m_pStream = pStream;
+ INITDEC();
+}
+inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder()
+{
+}
+inline void CJBig2_ArithDecoder::INITDEC()
+{
+ B = m_pStream->getCurByte_arith();
+ C = (B ^ 0xff) << 16;;
+ BYTEIN();
+ C = C << 7;
+ CT = CT - 7;
+ A = 0x8000;
+}
+inline void CJBig2_ArithDecoder::BYTEIN()
+{
+ unsigned char B1;
+ if(B == 0xff) {
+ B1 = m_pStream->getNextByte_arith();
+ if(B1 > 0x8f) {
+ CT = 8;
+ } else {
+ m_pStream->incByteIdx();
+ B = B1;
+ C = C + 0xfe00 - (B << 9);
+ CT = 7;
+ }
+ } else {
+ m_pStream->incByteIdx();
+ B = m_pStream->getCurByte_arith();
+ C = C + 0xff00 - (B << 8);
+ CT = 8;
+ }
+}
+inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx *pCX)
+{
+ int D;
+ const JBig2ArithQe * qe = &QeTable[pCX->I];
+ A = A - qe->Qe;
+ if((C >> 16) < A) {
+ if(A & 0x8000) {
+ D = pCX->MPS;
+ } else {
+ if(A < qe->Qe) {
+ D = 1 - pCX->MPS;
+ if(qe->nSwitch == 1) {
+ pCX->MPS = 1 - pCX->MPS;
+ }
+ pCX->I = qe->NLPS;
+ } else {
+ D = pCX->MPS;
+ pCX->I = qe->NMPS;
+ }
+ do {
+ if (CT == 0) {
+ BYTEIN();
+ }
+ A <<= 1;
+ C <<= 1;
+ CT--;
+ } while ((A & 0x8000) == 0);
+ }
+ } else {
+ C -= A << 16;
+ if(A < qe->Qe) {
+ A = qe->Qe;
+ D = pCX->MPS;
+ pCX->I = qe->NMPS;
+ } else {
+ A = qe->Qe;
+ D = 1 - pCX->MPS;
+ if(qe->nSwitch == 1) {
+ pCX->MPS = 1 - pCX->MPS;
+ }
+ pCX->I = qe->NLPS;
+ }
+ do {
+ if (CT == 0) {
+ BYTEIN();
+ }
+ A <<= 1;
+ C <<= 1;
+ CT--;
+ } while ((A & 0x8000) == 0);
+ }
+ return D;
+}
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
index bb4e0bba9f..1e115848a3 100644
--- a/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
@@ -1,105 +1,105 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_ArithIntDecoder.h"
-CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder()
-{
- IAx = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), 512);
- JBIG2_memset(IAx, 0, sizeof(JBig2ArithCtx) * 512);
-}
-CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder()
-{
- m_pModule->JBig2_Free(IAx);
-}
-int CJBig2_ArithIntDecoder::decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult)
-{
- int PREV, V;
- int S, D;
- int nNeedBits, nTemp, i;
- PREV = 1;
- S = pArithDecoder->DECODE(IAx + PREV);
- PREV = (PREV << 1) | S;
- D = pArithDecoder->DECODE(IAx + PREV);
- PREV = (PREV << 1) | D;
- if(D) {
- D = pArithDecoder->DECODE(IAx + PREV);
- PREV = (PREV << 1) | D;
- if(D) {
- D = pArithDecoder->DECODE(IAx + PREV);
- PREV = (PREV << 1) | D;
- if(D) {
- D = pArithDecoder->DECODE(IAx + PREV);
- PREV = (PREV << 1) | D;
- if(D) {
- D = pArithDecoder->DECODE(IAx + PREV);
- PREV = (PREV << 1) | D;
- if(D) {
- nNeedBits = 32;
- V = 4436;
- } else {
- nNeedBits = 12;
- V = 340;
- }
- } else {
- nNeedBits = 8;
- V = 84;
- }
- } else {
- nNeedBits = 6;
- V = 20;
- }
- } else {
- nNeedBits = 4;
- V = 4;
- }
- } else {
- nNeedBits = 2;
- V = 0;
- }
- nTemp = 0;
- for(i = 0; i < nNeedBits; i++) {
- D = pArithDecoder->DECODE(IAx + PREV);
- if(PREV < 256) {
- PREV = (PREV << 1) | D;
- } else {
- PREV = (((PREV << 1) | D) & 511) | 256;
- }
- nTemp = (nTemp << 1) | D;
- }
- V += nTemp;
- if(S == 1 && V > 0) {
- V = -V;
- }
- *nResult = V;
- if(S == 1 && V == 0) {
- return JBIG2_OOB;
- }
- return 0;
-}
-CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA)
-{
- SBSYMCODELEN = SBSYMCODELENA;
- IAID = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), (1 << SBSYMCODELEN));
- JBIG2_memset(IAID, 0, sizeof(JBig2ArithCtx) * (int)(1 << SBSYMCODELEN));
-}
-CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder()
-{
- m_pModule->JBig2_Free(IAID);
-}
-int CJBig2_ArithIaidDecoder::decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult)
-{
- int PREV;
- int D;
- int i;
- PREV = 1;
- for(i = 0; i < SBSYMCODELEN; i++) {
- D = pArithDecoder->DECODE(IAID + PREV);
- PREV = (PREV << 1) | D;
- }
- PREV = PREV - (1 << SBSYMCODELEN);
- *nResult = PREV;
- return 0;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_ArithIntDecoder.h"
+CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder()
+{
+ IAx = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), 512);
+ JBIG2_memset(IAx, 0, sizeof(JBig2ArithCtx) * 512);
+}
+CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder()
+{
+ m_pModule->JBig2_Free(IAx);
+}
+int CJBig2_ArithIntDecoder::decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult)
+{
+ int PREV, V;
+ int S, D;
+ int nNeedBits, nTemp, i;
+ PREV = 1;
+ S = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | S;
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ PREV = (PREV << 1) | D;
+ if(D) {
+ nNeedBits = 32;
+ V = 4436;
+ } else {
+ nNeedBits = 12;
+ V = 340;
+ }
+ } else {
+ nNeedBits = 8;
+ V = 84;
+ }
+ } else {
+ nNeedBits = 6;
+ V = 20;
+ }
+ } else {
+ nNeedBits = 4;
+ V = 4;
+ }
+ } else {
+ nNeedBits = 2;
+ V = 0;
+ }
+ nTemp = 0;
+ for(i = 0; i < nNeedBits; i++) {
+ D = pArithDecoder->DECODE(IAx + PREV);
+ if(PREV < 256) {
+ PREV = (PREV << 1) | D;
+ } else {
+ PREV = (((PREV << 1) | D) & 511) | 256;
+ }
+ nTemp = (nTemp << 1) | D;
+ }
+ V += nTemp;
+ if(S == 1 && V > 0) {
+ V = -V;
+ }
+ *nResult = V;
+ if(S == 1 && V == 0) {
+ return JBIG2_OOB;
+ }
+ return 0;
+}
+CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA)
+{
+ SBSYMCODELEN = SBSYMCODELENA;
+ IAID = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), (1 << SBSYMCODELEN));
+ JBIG2_memset(IAID, 0, sizeof(JBig2ArithCtx) * (int)(1 << SBSYMCODELEN));
+}
+CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder()
+{
+ m_pModule->JBig2_Free(IAID);
+}
+int CJBig2_ArithIaidDecoder::decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult)
+{
+ int PREV;
+ int D;
+ int i;
+ PREV = 1;
+ for(i = 0; i < SBSYMCODELEN; i++) {
+ D = pArithDecoder->DECODE(IAID + PREV);
+ PREV = (PREV << 1) | D;
+ }
+ PREV = PREV - (1 << SBSYMCODELEN);
+ *nResult = PREV;
+ return 0;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h
index ad4656f54d..4ea104cf65 100644
--- a/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h
+++ b/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h
@@ -1,39 +1,39 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_ARITH_INT_DECODER_H_
-#define _JBIG2_ARITH_INT_DECODER_H_
-#include "JBig2_Module.h"
-#include "JBig2_ArithDecoder.h"
-class CJBig2_ArithIntDecoder : public CJBig2_Object
-{
-public:
-
- CJBig2_ArithIntDecoder();
-
- ~CJBig2_ArithIntDecoder();
-
- int decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult);
-private:
-
- JBig2ArithCtx *IAx;
-};
-class CJBig2_ArithIaidDecoder : public CJBig2_Object
-{
-public:
-
- CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA);
-
- ~CJBig2_ArithIaidDecoder();
-
- int decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult);
-private:
-
- JBig2ArithCtx *IAID;
-
- unsigned char SBSYMCODELEN;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_ARITH_INT_DECODER_H_
+#define _JBIG2_ARITH_INT_DECODER_H_
+#include "JBig2_Module.h"
+#include "JBig2_ArithDecoder.h"
+class CJBig2_ArithIntDecoder : public CJBig2_Object
+{
+public:
+
+ CJBig2_ArithIntDecoder();
+
+ ~CJBig2_ArithIntDecoder();
+
+ int decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult);
+private:
+
+ JBig2ArithCtx *IAx;
+};
+class CJBig2_ArithIaidDecoder : public CJBig2_Object
+{
+public:
+
+ CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA);
+
+ ~CJBig2_ArithIaidDecoder();
+
+ int decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult);
+private:
+
+ JBig2ArithCtx *IAID;
+
+ unsigned char SBSYMCODELEN;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_ArithQe.h b/core/src/fxcodec/jbig2/JBig2_ArithQe.h
index 17bc18aa03..60fbbc9ac3 100644
--- a/core/src/fxcodec/jbig2/JBig2_ArithQe.h
+++ b/core/src/fxcodec/jbig2/JBig2_ArithQe.h
@@ -1,64 +1,64 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_ARITH_QE_H_
-#define _JBIG2_ARITH_QE_H_
-typedef struct {
- unsigned int Qe;
- unsigned int NMPS;
- unsigned int NLPS;
- unsigned int nSwitch;
-} JBig2ArithQe;
-const JBig2ArithQe QeTable[] = {
- { 0x5601, 1, 1, 1 },
- { 0x3401, 2, 6, 0 },
- { 0x1801, 3, 9, 0 },
- { 0x0AC1, 4, 12, 0 },
- { 0x0521, 5, 29, 0 },
- { 0x0221, 38, 33, 0 },
- { 0x5601, 7, 6, 1 },
- { 0x5401, 8, 14, 0 },
- { 0x4801, 9, 14, 0 },
- { 0x3801, 10, 14, 0 },
- { 0x3001, 11, 17, 0 },
- { 0x2401, 12, 18, 0 },
- { 0x1C01, 13, 20, 0 },
- { 0x1601, 29, 21, 0 },
- { 0x5601, 15, 14, 1 },
- { 0x5401, 16, 14, 0 },
- { 0x5101, 17, 15, 0 },
- { 0x4801, 18, 16, 0 },
- { 0x3801, 19, 17, 0 },
- { 0x3401, 20, 18, 0 },
- { 0x3001, 21, 19, 0 },
- { 0x2801, 22, 19, 0 },
- { 0x2401, 23, 20, 0 },
- { 0x2201, 24, 21, 0 },
- { 0x1C01, 25, 22, 0 },
- { 0x1801, 26, 23, 0 },
- { 0x1601, 27, 24, 0 },
- { 0x1401, 28, 25, 0 },
- { 0x1201, 29, 26, 0 },
- { 0x1101, 30, 27, 0 },
- { 0x0AC1, 31, 28, 0 },
- { 0x09C1, 32, 29, 0 },
- { 0x08A1, 33, 30, 0 },
- { 0x0521, 34, 31, 0 },
- { 0x0441, 35, 32, 0 },
- { 0x02A1, 36, 33, 0 },
- { 0x0221, 37, 34, 0 },
- { 0x0141, 38, 35, 0 },
- { 0x0111, 39, 36, 0 },
- { 0x0085, 40, 37, 0 },
- { 0x0049, 41, 38, 0 },
- { 0x0025, 42, 39, 0 },
- { 0x0015, 43, 40, 0 },
- { 0x0009, 44, 41, 0 },
- { 0x0005, 45, 42, 0 },
- { 0x0001, 45, 43, 0 },
- { 0x5601, 46, 46, 0 }
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_ARITH_QE_H_
+#define _JBIG2_ARITH_QE_H_
+typedef struct {
+ unsigned int Qe;
+ unsigned int NMPS;
+ unsigned int NLPS;
+ unsigned int nSwitch;
+} JBig2ArithQe;
+const JBig2ArithQe QeTable[] = {
+ { 0x5601, 1, 1, 1 },
+ { 0x3401, 2, 6, 0 },
+ { 0x1801, 3, 9, 0 },
+ { 0x0AC1, 4, 12, 0 },
+ { 0x0521, 5, 29, 0 },
+ { 0x0221, 38, 33, 0 },
+ { 0x5601, 7, 6, 1 },
+ { 0x5401, 8, 14, 0 },
+ { 0x4801, 9, 14, 0 },
+ { 0x3801, 10, 14, 0 },
+ { 0x3001, 11, 17, 0 },
+ { 0x2401, 12, 18, 0 },
+ { 0x1C01, 13, 20, 0 },
+ { 0x1601, 29, 21, 0 },
+ { 0x5601, 15, 14, 1 },
+ { 0x5401, 16, 14, 0 },
+ { 0x5101, 17, 15, 0 },
+ { 0x4801, 18, 16, 0 },
+ { 0x3801, 19, 17, 0 },
+ { 0x3401, 20, 18, 0 },
+ { 0x3001, 21, 19, 0 },
+ { 0x2801, 22, 19, 0 },
+ { 0x2401, 23, 20, 0 },
+ { 0x2201, 24, 21, 0 },
+ { 0x1C01, 25, 22, 0 },
+ { 0x1801, 26, 23, 0 },
+ { 0x1601, 27, 24, 0 },
+ { 0x1401, 28, 25, 0 },
+ { 0x1201, 29, 26, 0 },
+ { 0x1101, 30, 27, 0 },
+ { 0x0AC1, 31, 28, 0 },
+ { 0x09C1, 32, 29, 0 },
+ { 0x08A1, 33, 30, 0 },
+ { 0x0521, 34, 31, 0 },
+ { 0x0441, 35, 32, 0 },
+ { 0x02A1, 36, 33, 0 },
+ { 0x0221, 37, 34, 0 },
+ { 0x0141, 38, 35, 0 },
+ { 0x0111, 39, 36, 0 },
+ { 0x0085, 40, 37, 0 },
+ { 0x0049, 41, 38, 0 },
+ { 0x0025, 42, 39, 0 },
+ { 0x0015, 43, 40, 0 },
+ { 0x0009, 44, 41, 0 },
+ { 0x0005, 45, 42, 0 },
+ { 0x0001, 45, 43, 0 },
+ { 0x5601, 46, 46, 0 }
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_BitStream.h b/core/src/fxcodec/jbig2/JBig2_BitStream.h
index 8ed473a150..9d5d29468f 100644
--- a/core/src/fxcodec/jbig2/JBig2_BitStream.h
+++ b/core/src/fxcodec/jbig2/JBig2_BitStream.h
@@ -1,316 +1,316 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_BIT_STREAM_H_
-#define _JBIG2_BIT_STREAM_H_
-#include "JBig2_Define.h"
-class CJBig2_BitStream : public CJBig2_Object
-{
-public:
-
- CJBig2_BitStream(FX_BYTE *pBuffer, FX_DWORD dwLength);
-
- CJBig2_BitStream(CJBig2_BitStream &bs);
-
- ~CJBig2_BitStream();
-
- FX_INT32 readNBits(FX_DWORD nBits, FX_DWORD *dwResult);
-
- FX_INT32 readNBits(FX_DWORD nBits, FX_INT32 *nResult);
-
- FX_INT32 read1Bit(FX_DWORD *dwResult);
-
- FX_INT32 read1Bit(FX_BOOL *bResult);
-
- FX_INT32 read1Byte(FX_BYTE *cResult);
-
- FX_INT32 readInteger(FX_DWORD *dwResult);
-
- FX_INT32 readShortInteger(FX_WORD *wResult);
-
- void alignByte();
-
- void align4Byte();
-
- FX_BYTE getAt(FX_DWORD dwOffset);
-
- FX_BYTE getCurByte();
-
- FX_BYTE getNextByte();
-
- FX_INT32 incByteIdx();
-
- FX_BYTE getCurByte_arith();
-
- FX_BYTE getNextByte_arith();
-
- FX_DWORD getOffset();
-
- void setOffset(FX_DWORD dwOffset);
-
- FX_DWORD getBitPos();
-
- void setBitPos(FX_DWORD dwBitPos);
-
- FX_BYTE *getBuf();
-
- FX_DWORD getLength()
- {
- return m_dwLength;
- }
-
- FX_BYTE *getPointer();
-
- void offset(FX_DWORD dwOffset);
-
- FX_DWORD getByteLeft();
-private:
-
- FX_BYTE *m_pBuf;
-
- FX_DWORD m_dwLength;
-
- FX_DWORD m_dwByteIdx;
-
- FX_DWORD m_dwBitIdx;
-};
-inline CJBig2_BitStream::CJBig2_BitStream(FX_BYTE *pBuffer, FX_DWORD dwLength)
-{
- m_pBuf = pBuffer;
- m_dwLength = dwLength;
- m_dwByteIdx = 0;
- m_dwBitIdx = 0;
- if (m_dwLength > 256 * 1024 * 1024) {
- m_dwLength = 0;
- m_pBuf = NULL;
- }
-}
-inline CJBig2_BitStream::CJBig2_BitStream(CJBig2_BitStream &bs)
-{
- m_pBuf = bs.m_pBuf;
- m_dwLength = bs.m_dwLength;
- m_dwByteIdx = bs.m_dwByteIdx;
- m_dwBitIdx = bs.m_dwBitIdx;
-}
-inline CJBig2_BitStream::~CJBig2_BitStream()
-{
-}
-inline FX_INT32 CJBig2_BitStream::readNBits(FX_DWORD dwBits, FX_DWORD *dwResult)
-{
- FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx;
- if(dwTemp <= (m_dwLength << 3)) {
- *dwResult = 0;
- if(dwTemp + dwBits <= (m_dwLength << 3)) {
- dwTemp = dwBits;
- } else {
- dwTemp = (m_dwLength << 3) - dwTemp;
- }
- while(dwTemp > 0) {
- *dwResult = (*dwResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
- if(m_dwBitIdx == 7) {
- m_dwByteIdx ++;
- m_dwBitIdx = 0;
- } else {
- m_dwBitIdx ++;
- }
- dwTemp --;
- }
- return 0;
- } else {
- return -1;
- }
-}
-inline FX_INT32 CJBig2_BitStream::readNBits(FX_DWORD dwBits, FX_INT32 *nResult)
-{
- FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx;
- if(dwTemp <= (m_dwLength << 3)) {
- *nResult = 0;
- if(dwTemp + dwBits <= (m_dwLength << 3)) {
- dwTemp = dwBits;
- } else {
- dwTemp = (m_dwLength << 3) - dwTemp;
- }
- while(dwTemp > 0) {
- *nResult = (*nResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
- if(m_dwBitIdx == 7) {
- m_dwByteIdx ++;
- m_dwBitIdx = 0;
- } else {
- m_dwBitIdx ++;
- }
- dwTemp --;
- }
- return 0;
- } else {
- return -1;
- }
-}
-
-inline FX_INT32 CJBig2_BitStream::read1Bit(FX_DWORD *dwResult)
-{
- if(m_dwByteIdx < m_dwLength) {
- *dwResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
- if(m_dwBitIdx == 7) {
- m_dwByteIdx ++;
- m_dwBitIdx = 0;
- } else {
- m_dwBitIdx ++;
- }
- return 0;
- } else {
- return -1;
- }
-}
-
-inline FX_INT32 CJBig2_BitStream::read1Bit(FX_BOOL *bResult)
-{
- if(m_dwByteIdx < m_dwLength) {
- *bResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
- if(m_dwBitIdx == 7) {
- m_dwByteIdx ++;
- m_dwBitIdx = 0;
- } else {
- m_dwBitIdx ++;
- }
- return 0;
- } else {
- return -1;
- }
-}
-inline FX_INT32 CJBig2_BitStream::read1Byte(FX_BYTE *cResult)
-{
- if(m_dwByteIdx < m_dwLength) {
- *cResult = m_pBuf[m_dwByteIdx];
- m_dwByteIdx ++;
- return 0;
- } else {
- return -1;
- }
-}
-
-inline FX_INT32 CJBig2_BitStream::readInteger(FX_DWORD *dwResult)
-{
- if(m_dwByteIdx + 3 < m_dwLength) {
- *dwResult = (m_pBuf[m_dwByteIdx] << 24) | (m_pBuf[m_dwByteIdx + 1] << 16)
- | (m_pBuf[m_dwByteIdx + 2] << 8) | m_pBuf[m_dwByteIdx + 3];
- m_dwByteIdx += 4;
- return 0;
- } else {
- return -1;
- }
-}
-
-inline FX_INT32 CJBig2_BitStream::readShortInteger(FX_WORD *dwResult)
-{
- if(m_dwByteIdx + 1 < m_dwLength) {
- *dwResult = (m_pBuf[m_dwByteIdx] << 8) | m_pBuf[m_dwByteIdx + 1];
- m_dwByteIdx += 2;
- return 0;
- } else {
- return -1;
- }
-}
-inline void CJBig2_BitStream::alignByte()
-{
- if(m_dwBitIdx != 0) {
- m_dwByteIdx ++;
- m_dwBitIdx = 0;
- }
-}
-inline void CJBig2_BitStream::align4Byte()
-{
- if(m_dwBitIdx != 0) {
- m_dwByteIdx ++;
- m_dwBitIdx = 0;
- }
- m_dwByteIdx = (m_dwByteIdx + 3) & -4;
-}
-inline FX_BYTE CJBig2_BitStream::getAt(FX_DWORD dwOffset)
-{
- if(dwOffset < m_dwLength) {
- return m_pBuf[dwOffset];
- } else {
- return 0;
- }
-}
-inline FX_BYTE CJBig2_BitStream::getCurByte()
-{
- if(m_dwByteIdx < m_dwLength) {
- return m_pBuf[m_dwByteIdx];
- } else {
- return 0;
- }
-}
-inline FX_BYTE CJBig2_BitStream::getNextByte()
-{
- if(m_dwByteIdx + 1 < m_dwLength) {
- return m_pBuf[m_dwByteIdx + 1];
- } else {
- return 0;
- }
-}
-inline FX_INT32 CJBig2_BitStream::incByteIdx()
-{
- if(m_dwByteIdx < m_dwLength) {
- m_dwByteIdx ++;
- return 0;
- } else {
- return -1;
- }
-}
-inline FX_BYTE CJBig2_BitStream::getCurByte_arith()
-{
- if(m_dwByteIdx < m_dwLength) {
- return m_pBuf[m_dwByteIdx];
- } else {
- return 0xff;
- }
-}
-inline FX_BYTE CJBig2_BitStream::getNextByte_arith()
-{
- if(m_dwByteIdx + 1 < m_dwLength) {
- return m_pBuf[m_dwByteIdx + 1];
- } else {
- return 0xff;
- }
-}
-inline FX_DWORD CJBig2_BitStream::getOffset()
-{
- return m_dwByteIdx;
-}
-inline void CJBig2_BitStream::setOffset(FX_DWORD dwOffset)
-{
- if (dwOffset > m_dwLength) {
- dwOffset = m_dwLength;
- }
- m_dwByteIdx = dwOffset;
-}
-inline FX_DWORD CJBig2_BitStream::getBitPos()
-{
- return (m_dwByteIdx << 3) + m_dwBitIdx;
-}
-inline void CJBig2_BitStream::setBitPos(FX_DWORD dwBitPos)
-{
- m_dwByteIdx = dwBitPos >> 3;
- m_dwBitIdx = dwBitPos & 7;
-}
-inline FX_BYTE *CJBig2_BitStream::getBuf()
-{
- return m_pBuf;
-}
-inline FX_BYTE *CJBig2_BitStream::getPointer()
-{
- return m_pBuf + m_dwByteIdx;
-}
-inline void CJBig2_BitStream::offset(FX_DWORD dwOffset)
-{
- m_dwByteIdx += dwOffset;
-}
-inline FX_DWORD CJBig2_BitStream::getByteLeft()
-{
- return m_dwLength - m_dwByteIdx;
-}
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_BIT_STREAM_H_
+#define _JBIG2_BIT_STREAM_H_
+#include "JBig2_Define.h"
+class CJBig2_BitStream : public CJBig2_Object
+{
+public:
+
+ CJBig2_BitStream(FX_BYTE *pBuffer, FX_DWORD dwLength);
+
+ CJBig2_BitStream(CJBig2_BitStream &bs);
+
+ ~CJBig2_BitStream();
+
+ FX_INT32 readNBits(FX_DWORD nBits, FX_DWORD *dwResult);
+
+ FX_INT32 readNBits(FX_DWORD nBits, FX_INT32 *nResult);
+
+ FX_INT32 read1Bit(FX_DWORD *dwResult);
+
+ FX_INT32 read1Bit(FX_BOOL *bResult);
+
+ FX_INT32 read1Byte(FX_BYTE *cResult);
+
+ FX_INT32 readInteger(FX_DWORD *dwResult);
+
+ FX_INT32 readShortInteger(FX_WORD *wResult);
+
+ void alignByte();
+
+ void align4Byte();
+
+ FX_BYTE getAt(FX_DWORD dwOffset);
+
+ FX_BYTE getCurByte();
+
+ FX_BYTE getNextByte();
+
+ FX_INT32 incByteIdx();
+
+ FX_BYTE getCurByte_arith();
+
+ FX_BYTE getNextByte_arith();
+
+ FX_DWORD getOffset();
+
+ void setOffset(FX_DWORD dwOffset);
+
+ FX_DWORD getBitPos();
+
+ void setBitPos(FX_DWORD dwBitPos);
+
+ FX_BYTE *getBuf();
+
+ FX_DWORD getLength()
+ {
+ return m_dwLength;
+ }
+
+ FX_BYTE *getPointer();
+
+ void offset(FX_DWORD dwOffset);
+
+ FX_DWORD getByteLeft();
+private:
+
+ FX_BYTE *m_pBuf;
+
+ FX_DWORD m_dwLength;
+
+ FX_DWORD m_dwByteIdx;
+
+ FX_DWORD m_dwBitIdx;
+};
+inline CJBig2_BitStream::CJBig2_BitStream(FX_BYTE *pBuffer, FX_DWORD dwLength)
+{
+ m_pBuf = pBuffer;
+ m_dwLength = dwLength;
+ m_dwByteIdx = 0;
+ m_dwBitIdx = 0;
+ if (m_dwLength > 256 * 1024 * 1024) {
+ m_dwLength = 0;
+ m_pBuf = NULL;
+ }
+}
+inline CJBig2_BitStream::CJBig2_BitStream(CJBig2_BitStream &bs)
+{
+ m_pBuf = bs.m_pBuf;
+ m_dwLength = bs.m_dwLength;
+ m_dwByteIdx = bs.m_dwByteIdx;
+ m_dwBitIdx = bs.m_dwBitIdx;
+}
+inline CJBig2_BitStream::~CJBig2_BitStream()
+{
+}
+inline FX_INT32 CJBig2_BitStream::readNBits(FX_DWORD dwBits, FX_DWORD *dwResult)
+{
+ FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx;
+ if(dwTemp <= (m_dwLength << 3)) {
+ *dwResult = 0;
+ if(dwTemp + dwBits <= (m_dwLength << 3)) {
+ dwTemp = dwBits;
+ } else {
+ dwTemp = (m_dwLength << 3) - dwTemp;
+ }
+ while(dwTemp > 0) {
+ *dwResult = (*dwResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
+ if(m_dwBitIdx == 7) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ } else {
+ m_dwBitIdx ++;
+ }
+ dwTemp --;
+ }
+ return 0;
+ } else {
+ return -1;
+ }
+}
+inline FX_INT32 CJBig2_BitStream::readNBits(FX_DWORD dwBits, FX_INT32 *nResult)
+{
+ FX_DWORD dwTemp = (m_dwByteIdx << 3) + m_dwBitIdx;
+ if(dwTemp <= (m_dwLength << 3)) {
+ *nResult = 0;
+ if(dwTemp + dwBits <= (m_dwLength << 3)) {
+ dwTemp = dwBits;
+ } else {
+ dwTemp = (m_dwLength << 3) - dwTemp;
+ }
+ while(dwTemp > 0) {
+ *nResult = (*nResult << 1) | ((m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01);
+ if(m_dwBitIdx == 7) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ } else {
+ m_dwBitIdx ++;
+ }
+ dwTemp --;
+ }
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+inline FX_INT32 CJBig2_BitStream::read1Bit(FX_DWORD *dwResult)
+{
+ if(m_dwByteIdx < m_dwLength) {
+ *dwResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
+ if(m_dwBitIdx == 7) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ } else {
+ m_dwBitIdx ++;
+ }
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+inline FX_INT32 CJBig2_BitStream::read1Bit(FX_BOOL *bResult)
+{
+ if(m_dwByteIdx < m_dwLength) {
+ *bResult = (m_pBuf[m_dwByteIdx] >> (7 - m_dwBitIdx)) & 0x01;
+ if(m_dwBitIdx == 7) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ } else {
+ m_dwBitIdx ++;
+ }
+ return 0;
+ } else {
+ return -1;
+ }
+}
+inline FX_INT32 CJBig2_BitStream::read1Byte(FX_BYTE *cResult)
+{
+ if(m_dwByteIdx < m_dwLength) {
+ *cResult = m_pBuf[m_dwByteIdx];
+ m_dwByteIdx ++;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+inline FX_INT32 CJBig2_BitStream::readInteger(FX_DWORD *dwResult)
+{
+ if(m_dwByteIdx + 3 < m_dwLength) {
+ *dwResult = (m_pBuf[m_dwByteIdx] << 24) | (m_pBuf[m_dwByteIdx + 1] << 16)
+ | (m_pBuf[m_dwByteIdx + 2] << 8) | m_pBuf[m_dwByteIdx + 3];
+ m_dwByteIdx += 4;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
+inline FX_INT32 CJBig2_BitStream::readShortInteger(FX_WORD *dwResult)
+{
+ if(m_dwByteIdx + 1 < m_dwLength) {
+ *dwResult = (m_pBuf[m_dwByteIdx] << 8) | m_pBuf[m_dwByteIdx + 1];
+ m_dwByteIdx += 2;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+inline void CJBig2_BitStream::alignByte()
+{
+ if(m_dwBitIdx != 0) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ }
+}
+inline void CJBig2_BitStream::align4Byte()
+{
+ if(m_dwBitIdx != 0) {
+ m_dwByteIdx ++;
+ m_dwBitIdx = 0;
+ }
+ m_dwByteIdx = (m_dwByteIdx + 3) & -4;
+}
+inline FX_BYTE CJBig2_BitStream::getAt(FX_DWORD dwOffset)
+{
+ if(dwOffset < m_dwLength) {
+ return m_pBuf[dwOffset];
+ } else {
+ return 0;
+ }
+}
+inline FX_BYTE CJBig2_BitStream::getCurByte()
+{
+ if(m_dwByteIdx < m_dwLength) {
+ return m_pBuf[m_dwByteIdx];
+ } else {
+ return 0;
+ }
+}
+inline FX_BYTE CJBig2_BitStream::getNextByte()
+{
+ if(m_dwByteIdx + 1 < m_dwLength) {
+ return m_pBuf[m_dwByteIdx + 1];
+ } else {
+ return 0;
+ }
+}
+inline FX_INT32 CJBig2_BitStream::incByteIdx()
+{
+ if(m_dwByteIdx < m_dwLength) {
+ m_dwByteIdx ++;
+ return 0;
+ } else {
+ return -1;
+ }
+}
+inline FX_BYTE CJBig2_BitStream::getCurByte_arith()
+{
+ if(m_dwByteIdx < m_dwLength) {
+ return m_pBuf[m_dwByteIdx];
+ } else {
+ return 0xff;
+ }
+}
+inline FX_BYTE CJBig2_BitStream::getNextByte_arith()
+{
+ if(m_dwByteIdx + 1 < m_dwLength) {
+ return m_pBuf[m_dwByteIdx + 1];
+ } else {
+ return 0xff;
+ }
+}
+inline FX_DWORD CJBig2_BitStream::getOffset()
+{
+ return m_dwByteIdx;
+}
+inline void CJBig2_BitStream::setOffset(FX_DWORD dwOffset)
+{
+ if (dwOffset > m_dwLength) {
+ dwOffset = m_dwLength;
+ }
+ m_dwByteIdx = dwOffset;
+}
+inline FX_DWORD CJBig2_BitStream::getBitPos()
+{
+ return (m_dwByteIdx << 3) + m_dwBitIdx;
+}
+inline void CJBig2_BitStream::setBitPos(FX_DWORD dwBitPos)
+{
+ m_dwByteIdx = dwBitPos >> 3;
+ m_dwBitIdx = dwBitPos & 7;
+}
+inline FX_BYTE *CJBig2_BitStream::getBuf()
+{
+ return m_pBuf;
+}
+inline FX_BYTE *CJBig2_BitStream::getPointer()
+{
+ return m_pBuf + m_dwByteIdx;
+}
+inline void CJBig2_BitStream::offset(FX_DWORD dwOffset)
+{
+ m_dwByteIdx += dwOffset;
+}
+inline FX_DWORD CJBig2_BitStream::getByteLeft()
+{
+ return m_dwLength - m_dwByteIdx;
+}
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Context.cpp b/core/src/fxcodec/jbig2/JBig2_Context.cpp
index 856c0c3bda..543f9abf63 100644
--- a/core/src/fxcodec/jbig2/JBig2_Context.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_Context.cpp
@@ -1,1812 +1,1812 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_Context.h"
-void OutputBitmap(CJBig2_Image* pImage)
-{
- if(!pImage) {
- return;
- }
-}
-CJBig2_Context *CJBig2_Context::CreateContext(CJBig2_Module *pModule, FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
- FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause)
-{
- return new(pModule) CJBig2_Context(pGlobalData, dwGlobalLength, pData, dwLength, nStreamType, pPause);
-}
-void CJBig2_Context::DestroyContext(CJBig2_Context *pContext)
-{
- if(pContext) {
- delete pContext;
- }
-}
-CJBig2_Context::CJBig2_Context(FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
- FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause)
-{
- if(pGlobalData && (dwGlobalLength > 0)) {
- JBIG2_ALLOC(m_pGlobalContext, CJBig2_Context(NULL, 0, pGlobalData, dwGlobalLength,
- JBIG2_EMBED_STREAM, pPause));
- } else {
- m_pGlobalContext = NULL;
- }
- JBIG2_ALLOC(m_pStream, CJBig2_BitStream(pData, dwLength));
- m_nStreamType = nStreamType;
- m_nState = JBIG2_OUT_OF_PAGE;
- JBIG2_ALLOC(m_pSegmentList, CJBig2_List<CJBig2_Segment>);
- JBIG2_ALLOC(m_pPageInfoList, CJBig2_List<JBig2PageInfo>(1));
- m_pPage = NULL;
- m_bBufSpecified = FALSE;
- m_pPause = pPause;
- m_nSegmentDecoded = 0;
- m_PauseStep = 10;
- m_pArithDecoder = NULL;
- m_pGRD = NULL;
- m_gbContext = NULL;
- m_pSegment = NULL;
- m_dwOffset = 0;
- m_ProcessiveStatus = FXCODEC_STATUS_FRAME_READY;
-}
-CJBig2_Context::~CJBig2_Context()
-{
- if(m_pArithDecoder) {
- delete m_pArithDecoder;
- }
- m_pArithDecoder = NULL;
- if(m_pGRD) {
- delete m_pGRD;
- }
- m_pGRD = NULL;
- if(m_gbContext) {
- delete m_gbContext;
- }
- m_gbContext = NULL;
- if(m_pGlobalContext) {
- delete m_pGlobalContext;
- }
- m_pGlobalContext = NULL;
- if(m_pPageInfoList) {
- delete m_pPageInfoList;
- }
- m_pPageInfoList = NULL;
- if(m_bBufSpecified && m_pPage) {
- delete m_pPage;
- }
- m_pPage = NULL;
- if(m_pStream) {
- delete m_pStream;
- }
- m_pStream = NULL;
- if(m_pSegmentList) {
- delete m_pSegmentList;
- }
- m_pSegmentList = NULL;
-}
-FX_INT32 CJBig2_Context::decodeFile(IFX_Pause* pPause)
-{
- FX_BYTE cFlags;
- FX_DWORD dwTemp;
- const FX_BYTE fileID[] = {0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A};
- FX_INT32 nRet;
- if(m_pStream->getByteLeft() < 8) {
- m_pModule->JBig2_Error("file header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- if(JBIG2_memcmp(m_pStream->getPointer(), fileID, 8) != 0) {
- m_pModule->JBig2_Error("not jbig2 file");
- nRet = JBIG2_ERROR_FILE_FORMAT;
- goto failed;
- }
- m_pStream->offset(8);
- if(m_pStream->read1Byte(&cFlags) != 0) {
- m_pModule->JBig2_Error("file header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- if(!(cFlags & 0x02)) {
- if(m_pStream->readInteger(&dwTemp) != 0) {
- m_pModule->JBig2_Error("file header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- if(dwTemp > 0) {
- delete m_pPageInfoList;
- JBIG2_ALLOC(m_pPageInfoList, CJBig2_List<JBig2PageInfo>(dwTemp));
- }
- }
- if(cFlags & 0x01) {
- m_nStreamType = JBIG2_SQUENTIAL_STREAM;
- return decode_SquentialOrgnazation(pPause);
- } else {
- m_nStreamType = JBIG2_RANDOM_STREAM;
- return decode_RandomOrgnazation_FirstPage(pPause);
- }
-failed:
- return nRet;
-}
-FX_INT32 CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause)
-{
- FX_INT32 nRet;
- if(m_pStream->getByteLeft() > 0) {
- while(m_pStream->getByteLeft() >= JBIG2_MIN_SEGMENT_SIZE) {
- if(m_pSegment == NULL) {
- JBIG2_ALLOC(m_pSegment, CJBig2_Segment());
- nRet = parseSegmentHeader(m_pSegment);
- if(nRet != JBIG2_SUCCESS) {
- delete m_pSegment;
- m_pSegment = NULL;
- return nRet;
- }
- m_dwOffset = m_pStream->getOffset();
- }
- nRet = parseSegmentData(m_pSegment, pPause);
- if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- m_PauseStep = 2;
- return JBIG2_SUCCESS;
- }
- if((nRet == JBIG2_END_OF_PAGE) || (nRet == JBIG2_END_OF_FILE)) {
- delete m_pSegment;
- m_pSegment = NULL;
- break;
- } else if(nRet != JBIG2_SUCCESS) {
- delete m_pSegment;
- m_pSegment = NULL;
- return nRet;
- }
- m_pSegmentList->addItem(m_pSegment);
- if(m_pSegment->m_dwData_length != 0xffffffff) {
- m_dwOffset = m_dwOffset + m_pSegment->m_dwData_length;
- m_pStream->setOffset(m_dwOffset);
- } else {
- m_pStream->offset(4);
- }
- OutputBitmap(m_pPage);
- m_pSegment = NULL;
- if(m_pStream->getByteLeft() > 0 && m_pPage && pPause && pPause->NeedToPauseNow()) {
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- m_PauseStep = 2;
- return JBIG2_SUCCESS;
- }
- }
- } else {
- return JBIG2_END_OF_FILE;
- }
- return JBIG2_SUCCESS;
-}
-FX_INT32 CJBig2_Context::decode_EmbedOrgnazation(IFX_Pause* pPause)
-{
- return decode_SquentialOrgnazation(pPause);
-}
-FX_INT32 CJBig2_Context::decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause)
-{
- CJBig2_Segment *pSegment;
- FX_INT32 nRet;
- while(m_pStream->getByteLeft() > JBIG2_MIN_SEGMENT_SIZE) {
- JBIG2_ALLOC(pSegment, CJBig2_Segment());
- nRet = parseSegmentHeader(pSegment);
- if(nRet != JBIG2_SUCCESS) {
- delete pSegment;
- return nRet;
- } else if(pSegment->m_cFlags.s.type == 51) {
- delete pSegment;
- break;
- }
- m_pSegmentList->addItem(pSegment);
- if(pPause && m_pPause && pPause->NeedToPauseNow()) {
- m_PauseStep = 3;
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return JBIG2_SUCCESS;
- }
- }
- m_nSegmentDecoded = 0;
- return decode_RandomOrgnazation(pPause);
-}
-FX_INT32 CJBig2_Context::decode_RandomOrgnazation(IFX_Pause* pPause)
-{
- FX_INT32 nRet;
- for(; m_nSegmentDecoded < m_pSegmentList->getLength(); m_nSegmentDecoded++) {
- nRet = parseSegmentData(m_pSegmentList->getAt(m_nSegmentDecoded), pPause);
- if((nRet == JBIG2_END_OF_PAGE) || (nRet == JBIG2_END_OF_FILE)) {
- break;
- } else if(nRet != JBIG2_SUCCESS) {
- return nRet;
- }
- if(m_pPage && pPause && pPause->NeedToPauseNow()) {
- m_PauseStep = 4;
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return JBIG2_SUCCESS;
- }
- }
- return JBIG2_SUCCESS;
-}
-FX_INT32 CJBig2_Context::getFirstPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause)
-{
- FX_INT32 nRet = 0;
- if(m_pGlobalContext) {
- nRet = m_pGlobalContext->decode_EmbedOrgnazation(pPause);
- if(nRet != JBIG2_SUCCESS) {
- m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
- return nRet;
- }
- }
- m_bFirstPage = TRUE;
- m_PauseStep = 0;
- if(m_pPage) {
- delete m_pPage;
- }
- JBIG2_ALLOC(m_pPage, CJBig2_Image(width, height, stride, pBuf));
- m_bBufSpecified = TRUE;
- if(m_pPage && pPause && pPause->NeedToPauseNow()) {
- m_PauseStep = 1;
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return nRet;
- }
- int ret = Continue(pPause);
- return ret;
-}
-FX_INT32 CJBig2_Context::Continue(IFX_Pause* pPause)
-{
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_READY;
- FX_INT32 nRet;
- if(m_PauseStep <= 1) {
- switch(m_nStreamType) {
- case JBIG2_FILE_STREAM:
- nRet = decodeFile(pPause);
- break;
- case JBIG2_SQUENTIAL_STREAM:
- nRet = decode_SquentialOrgnazation(pPause);
- break;
- case JBIG2_RANDOM_STREAM:
- if(m_bFirstPage) {
- nRet = decode_RandomOrgnazation_FirstPage(pPause);
- } else {
- nRet = decode_RandomOrgnazation(pPause);
- }
- break;
- case JBIG2_EMBED_STREAM:
- nRet = decode_EmbedOrgnazation(pPause);
- break;
- default:
- m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
- return JBIG2_ERROR_STREAM_TYPE;
- }
- } else if(m_PauseStep == 2) {
- nRet = decode_SquentialOrgnazation(pPause);
- } else if(m_PauseStep == 3) {
- nRet = decode_RandomOrgnazation_FirstPage(pPause);
- } else if(m_PauseStep == 4) {
- nRet = decode_RandomOrgnazation(pPause);
- } else if(m_PauseStep == 5) {
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return JBIG2_SUCCESS;
- }
- if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- return nRet;
- }
- m_PauseStep = 5;
- if(!m_bBufSpecified && nRet == JBIG2_SUCCESS) {
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return JBIG2_SUCCESS;
- }
- if(nRet == JBIG2_SUCCESS) {
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- } else {
- m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
- }
- return nRet;
-}
-FX_INT32 CJBig2_Context::getNextPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause)
-{
- FX_INT32 nRet = JBIG2_ERROR_STREAM_TYPE;
- m_bFirstPage = FALSE;
- m_PauseStep = 0;
- if(m_pPage) {
- delete m_pPage;
- }
- JBIG2_ALLOC(m_pPage, CJBig2_Image(width, height, stride, pBuf));
- m_bBufSpecified = TRUE;
- if(m_pPage && pPause && pPause->NeedToPauseNow()) {
- m_PauseStep = 1;
- m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return nRet;
- }
- return Continue(pPause);
- switch(m_nStreamType) {
- case JBIG2_FILE_STREAM:
- nRet = decodeFile(pPause);
- break;
- case JBIG2_SQUENTIAL_STREAM:
- nRet = decode_SquentialOrgnazation(pPause);
- break;
- case JBIG2_RANDOM_STREAM:
- nRet = decode_RandomOrgnazation(pPause);
- break;
- case JBIG2_EMBED_STREAM:
- nRet = decode_EmbedOrgnazation(pPause);
- break;
- default:
- return JBIG2_ERROR_STREAM_TYPE;
- }
- return nRet;
-}
-FX_INT32 CJBig2_Context::getFirstPage(CJBig2_Image **image, IFX_Pause* pPause)
-{
- FX_INT32 nRet;
- m_bFirstPage = TRUE;
- m_PauseStep = 0;
- if(m_pGlobalContext) {
- nRet = m_pGlobalContext->decode_EmbedOrgnazation(pPause);
- if(nRet != JBIG2_SUCCESS) {
- return nRet;
- }
- }
- m_bBufSpecified = FALSE;
- return Continue(pPause);
-}
-FX_INT32 CJBig2_Context::getNextPage(CJBig2_Image **image, IFX_Pause* pPause)
-{
- FX_INT32 nRet;
- m_bBufSpecified = FALSE;
- m_bFirstPage = FALSE;
- m_PauseStep = 0;
- switch(m_nStreamType) {
- case JBIG2_FILE_STREAM:
- nRet = decodeFile(pPause);
- break;
- case JBIG2_SQUENTIAL_STREAM:
- nRet = decode_SquentialOrgnazation(pPause);
- break;
- case JBIG2_RANDOM_STREAM:
- nRet = decode_RandomOrgnazation(pPause);
- break;
- case JBIG2_EMBED_STREAM:
- nRet = decode_EmbedOrgnazation(pPause);
- break;
- default:
- return JBIG2_ERROR_STREAM_TYPE;
- }
- if(nRet == JBIG2_SUCCESS) {
- *image = m_pPage;
- m_pPage = NULL;
- return JBIG2_SUCCESS;
- }
- return nRet;
-}
-CJBig2_Segment *CJBig2_Context::findSegmentByNumber(FX_DWORD dwNumber)
-{
- CJBig2_Segment *pSeg;
- FX_INT32 i;
- if(m_pGlobalContext) {
- pSeg = m_pGlobalContext->findSegmentByNumber(dwNumber);
- if(pSeg) {
- return pSeg;
- }
- }
- for(i = 0; i < m_pSegmentList->getLength(); i++) {
- pSeg = m_pSegmentList->getAt(i);
- if(pSeg->m_dwNumber == dwNumber) {
- return pSeg;
- }
- }
- return NULL;
-}
-CJBig2_Segment *CJBig2_Context::findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment,
- FX_BYTE cType, FX_INT32 nIndex)
-{
- CJBig2_Segment *pSeg;
- FX_INT32 i, count;
- count = 0;
- for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
- if(pSeg && pSeg->m_cFlags.s.type == cType) {
- if(count == nIndex) {
- return pSeg;
- } else {
- count ++;
- }
- }
- }
- return NULL;
-}
-FX_INT32 CJBig2_Context::parseSegmentHeader(CJBig2_Segment *pSegment)
-{
- FX_BYTE cSSize, cPSize;
- FX_BYTE cTemp;
- FX_WORD wTemp;
- FX_DWORD dwTemp;
- if((m_pStream->readInteger(&pSegment->m_dwNumber) != 0)
- || (m_pStream->read1Byte(&pSegment->m_cFlags.c) != 0)) {
- goto failed;
- }
- cTemp = m_pStream->getCurByte();
- if((cTemp >> 5) == 7) {
- if(m_pStream->readInteger((FX_DWORD*)&pSegment->m_nReferred_to_segment_count) != 0) {
- goto failed;
- }
- pSegment->m_nReferred_to_segment_count &= 0x1fffffff;
- if (pSegment->m_nReferred_to_segment_count > JBIG2_MAX_REFERRED_SEGMENT_COUNT) {
- m_pModule->JBig2_Error("Too many referred segments.");
- return JBIG2_ERROR_LIMIT;
- }
- dwTemp = 5 + 4 + (pSegment->m_nReferred_to_segment_count + 1) / 8;
- } else {
- if(m_pStream->read1Byte(&cTemp) != 0) {
- goto failed;
- }
- pSegment->m_nReferred_to_segment_count = cTemp >> 5;
- dwTemp = 5 + 1;
- }
- cSSize = pSegment->m_dwNumber > 65536 ? 4 : pSegment->m_dwNumber > 256 ? 2 : 1;
- cPSize = pSegment->m_cFlags.s.page_association_size ? 4 : 1;
- if(pSegment->m_nReferred_to_segment_count) {
- pSegment->m_pReferred_to_segment_numbers = (FX_DWORD*)m_pModule->JBig2_Malloc2(
- sizeof(FX_DWORD), pSegment->m_nReferred_to_segment_count);
- for(FX_INT32 i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- switch(cSSize) {
- case 1:
- if(m_pStream->read1Byte(&cTemp) != 0) {
- goto failed;
- }
- pSegment->m_pReferred_to_segment_numbers[i] = cTemp;
- break;
- case 2:
- if(m_pStream->readShortInteger(&wTemp) != 0) {
- goto failed;
- }
- pSegment->m_pReferred_to_segment_numbers[i] = wTemp;
- break;
- case 4:
- if(m_pStream->readInteger(&dwTemp) != 0) {
- goto failed;
- }
- pSegment->m_pReferred_to_segment_numbers[i] = dwTemp;
- break;
- }
- if (pSegment->m_pReferred_to_segment_numbers[i] >= pSegment->m_dwNumber) {
- m_pModule->JBig2_Error("The referred segment number is greater than this segment number.");
- goto failed;
- }
- }
- }
- if(cPSize == 1) {
- if(m_pStream->read1Byte(&cTemp) != 0) {
- goto failed;
- }
- pSegment->m_dwPage_association = cTemp;
- } else {
- if(m_pStream->readInteger(&pSegment->m_dwPage_association) != 0) {
- goto failed;
- }
- }
- if(m_pStream->readInteger(&pSegment->m_dwData_length) != 0) {
- goto failed;
- }
- pSegment->m_pData = m_pStream->getPointer();
- pSegment->m_State = JBIG2_SEGMENT_DATA_UNPARSED;
- return JBIG2_SUCCESS;
-failed:
- m_pModule->JBig2_Error("header too short.");
- return JBIG2_ERROR_TOO_SHORT;
-}
-FX_INT32 CJBig2_Context::parseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause)
-{
- FX_INT32 ret = ProcessiveParseSegmentData(pSegment, pPause);
- while(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE && m_pStream->getByteLeft() > 0) {
- ret = ProcessiveParseSegmentData(pSegment, pPause);
- }
- return ret;
-}
-FX_INT32 CJBig2_Context::ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause)
-{
- switch(pSegment->m_cFlags.s.type) {
- case 0:
- return parseSymbolDict(pSegment, pPause);
- case 4:
- case 6:
- case 7:
- if(m_nState == JBIG2_OUT_OF_PAGE) {
- goto failed2;
- } else {
- return parseTextRegion(pSegment);
- }
- case 16:
- return parsePatternDict(pSegment, pPause);
- case 20:
- case 22:
- case 23:
- if(m_nState == JBIG2_OUT_OF_PAGE) {
- goto failed2;
- } else {
- return parseHalftoneRegion(pSegment, pPause);
- }
- case 36:
- case 38:
- case 39:
- if(m_nState == JBIG2_OUT_OF_PAGE) {
- goto failed2;
- } else {
- return parseGenericRegion(pSegment, pPause);
- }
- case 40:
- case 42:
- case 43:
- if(m_nState == JBIG2_OUT_OF_PAGE) {
- goto failed2;
- } else {
- return parseGenericRefinementRegion(pSegment);
- }
- case 48: {
- FX_WORD wTemp;
- JBig2PageInfo *pPageInfo;
- JBIG2_ALLOC(pPageInfo, JBig2PageInfo);
- if((m_pStream->readInteger(&pPageInfo->m_dwWidth) != 0)
- || (m_pStream->readInteger(&pPageInfo->m_dwHeight) != 0)
- || (m_pStream->readInteger(&pPageInfo->m_dwResolutionX) != 0)
- || (m_pStream->readInteger(&pPageInfo->m_dwResolutionY) != 0)
- || (m_pStream->read1Byte(&pPageInfo->m_cFlags) != 0)
- || (m_pStream->readShortInteger(&wTemp) != 0)) {
- delete pPageInfo;
- goto failed1;
- }
- pPageInfo->m_bIsStriped = ((wTemp >> 15) & 1) ? 1 : 0;
- pPageInfo->m_wMaxStripeSize = wTemp & 0x7fff;
- if((pPageInfo->m_dwHeight == 0xffffffff) && (pPageInfo->m_bIsStriped != 1)) {
- m_pModule->JBig2_Warn("page height = 0xffffffff buf stripe field is 0");
- pPageInfo->m_bIsStriped = 1;
- }
- if(!m_bBufSpecified) {
- if(m_pPage) {
- delete m_pPage;
- }
- if(pPageInfo->m_dwHeight == 0xffffffff) {
- JBIG2_ALLOC(m_pPage, CJBig2_Image(pPageInfo->m_dwWidth, pPageInfo->m_wMaxStripeSize));
- } else {
- JBIG2_ALLOC(m_pPage, CJBig2_Image(pPageInfo->m_dwWidth, pPageInfo->m_dwHeight));
- }
- }
- m_pPage->fill((pPageInfo->m_cFlags & 4) ? 1 : 0);
- m_pPageInfoList->addItem(pPageInfo);
- m_nState = JBIG2_IN_PAGE;
- }
- break;
- case 49:
- m_nState = JBIG2_OUT_OF_PAGE;
- return JBIG2_END_OF_PAGE;
- break;
- case 50:
- m_pStream->offset(pSegment->m_dwData_length);
- break;
- case 51:
- return JBIG2_END_OF_FILE;
- case 52:
- m_pStream->offset(pSegment->m_dwData_length);
- break;
- case 53:
- return parseTable(pSegment);
- case 62:
- m_pStream->offset(pSegment->m_dwData_length);
- break;
- default:
- break;
- }
- return JBIG2_SUCCESS;
-failed1:
- m_pModule->JBig2_Error("segment data too short.");
- return JBIG2_ERROR_TOO_SHORT;
-failed2:
- m_pModule->JBig2_Error("segment syntax error.");
- return JBIG2_ERROR_FETAL;
-}
-FX_INT32 CJBig2_Context::parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause)
-{
- FX_DWORD dwTemp;
- FX_WORD wFlags;
- FX_BYTE cSDHUFFDH, cSDHUFFDW, cSDHUFFBMSIZE, cSDHUFFAGGINST;
- CJBig2_HuffmanTable *Table_B1 = NULL, *Table_B2 = NULL, *Table_B3 = NULL, *Table_B4 = NULL, *Table_B5 = NULL;
- FX_INT32 i, nIndex, nRet;
- CJBig2_Segment *pSeg = NULL, *pLRSeg = NULL;
- FX_BOOL bUsed;
- CJBig2_Image ** SDINSYMS = NULL;
- CJBig2_SDDProc *pSymbolDictDecoder;
- JBig2ArithCtx *gbContext = NULL, *grContext = NULL;
- CJBig2_ArithDecoder *pArithDecoder;
- JBIG2_ALLOC(pSymbolDictDecoder, CJBig2_SDDProc());
- if(m_pStream->readShortInteger(&wFlags) != 0) {
- m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- pSymbolDictDecoder->SDHUFF = wFlags & 0x0001;
- pSymbolDictDecoder->SDREFAGG = (wFlags >> 1) & 0x0001;
- pSymbolDictDecoder->SDTEMPLATE = (wFlags >> 10) & 0x0003;
- pSymbolDictDecoder->SDRTEMPLATE = (wFlags >> 12) & 0x0003;
- cSDHUFFDH = (wFlags >> 2) & 0x0003;
- cSDHUFFDW = (wFlags >> 4) & 0x0003;
- cSDHUFFBMSIZE = (wFlags >> 6) & 0x0001;
- cSDHUFFAGGINST = (wFlags >> 7) & 0x0001;
- if(pSymbolDictDecoder->SDHUFF == 0) {
- if(pSymbolDictDecoder->SDTEMPLATE == 0) {
- dwTemp = 8;
- } else {
- dwTemp = 2;
- }
- for(i = 0; i < (FX_INT32)dwTemp; i++) {
- if(m_pStream->read1Byte((FX_BYTE*)&pSymbolDictDecoder->SDAT[i]) != 0) {
- m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- }
- }
- if((pSymbolDictDecoder->SDREFAGG == 1) && (pSymbolDictDecoder->SDRTEMPLATE == 0)) {
- for(i = 0; i < 4; i++) {
- if(m_pStream->read1Byte((FX_BYTE*)&pSymbolDictDecoder->SDRAT[i]) != 0) {
- m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- }
- }
- if((m_pStream->readInteger(&pSymbolDictDecoder->SDNUMEXSYMS) != 0)
- || (m_pStream->readInteger(&pSymbolDictDecoder->SDNUMNEWSYMS) != 0)) {
- m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- if (pSymbolDictDecoder->SDNUMEXSYMS > JBIG2_MAX_EXPORT_SYSMBOLS
- || pSymbolDictDecoder->SDNUMNEWSYMS > JBIG2_MAX_NEW_SYSMBOLS) {
- m_pModule->JBig2_Error("symbol dictionary segment : too many export/new symbols.");
- nRet = JBIG2_ERROR_LIMIT;
- goto failed;
- }
- for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- if(!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) {
- m_pModule->JBig2_Error("symbol dictionary segment : can't find refered to segments");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- }
- pSymbolDictDecoder->SDNUMINSYMS = 0;
- for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
- if(pSeg->m_cFlags.s.type == 0) {
- pSymbolDictDecoder->SDNUMINSYMS += pSeg->m_Result.sd->SDNUMEXSYMS;
- pLRSeg = pSeg;
- }
- }
- if(pSymbolDictDecoder->SDNUMINSYMS == 0) {
- SDINSYMS = NULL;
- } else {
- SDINSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(
- sizeof(CJBig2_Image*), pSymbolDictDecoder->SDNUMINSYMS);
- dwTemp = 0;
- for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
- if(pSeg->m_cFlags.s.type == 0) {
- JBIG2_memcpy(SDINSYMS + dwTemp, pSeg->m_Result.sd->SDEXSYMS,
- pSeg->m_Result.sd->SDNUMEXSYMS * sizeof(CJBig2_Image*));
- dwTemp += pSeg->m_Result.sd->SDNUMEXSYMS;
- }
- }
- }
- pSymbolDictDecoder->SDINSYMS = SDINSYMS;
- if(pSymbolDictDecoder->SDHUFF == 1) {
- if((cSDHUFFDH == 2) || (cSDHUFFDW == 2)) {
- m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDH=2 or SDHUFFDW=2 is not permitted.");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- nIndex = 0;
- if(cSDHUFFDH == 0) {
- JBIG2_ALLOC(Table_B4, CJBig2_HuffmanTable(HuffmanTable_B4,
- sizeof(HuffmanTable_B4) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B4));
- pSymbolDictDecoder->SDHUFFDH = Table_B4;
- } else if(cSDHUFFDH == 1) {
- JBIG2_ALLOC(Table_B5, CJBig2_HuffmanTable(HuffmanTable_B5,
- sizeof(HuffmanTable_B5) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B5));
- pSymbolDictDecoder->SDHUFFDH = Table_B5;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDH can't find user supplied table.");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pSymbolDictDecoder->SDHUFFDH = pSeg->m_Result.ht;
- }
- if(cSDHUFFDW == 0) {
- JBIG2_ALLOC(Table_B2, CJBig2_HuffmanTable(HuffmanTable_B2,
- sizeof(HuffmanTable_B2) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B2));
- pSymbolDictDecoder->SDHUFFDW = Table_B2;
- } else if(cSDHUFFDW == 1) {
- JBIG2_ALLOC(Table_B3, CJBig2_HuffmanTable(HuffmanTable_B3,
- sizeof(HuffmanTable_B3) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B3));
- pSymbolDictDecoder->SDHUFFDW = Table_B3;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDW can't find user supplied table.");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pSymbolDictDecoder->SDHUFFDW = pSeg->m_Result.ht;
- }
- if(cSDHUFFBMSIZE == 0) {
- JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
- sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
- pSymbolDictDecoder->SDHUFFBMSIZE = Table_B1;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFBMSIZE can't find user supplied table.");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pSymbolDictDecoder->SDHUFFBMSIZE = pSeg->m_Result.ht;
- }
- if(pSymbolDictDecoder->SDREFAGG == 1) {
- if(cSDHUFFAGGINST == 0) {
- if(!Table_B1) {
- JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
- sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
- }
- pSymbolDictDecoder->SDHUFFAGGINST = Table_B1;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFAGGINST can't find user supplied table.");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pSymbolDictDecoder->SDHUFFAGGINST = pSeg->m_Result.ht;
- }
- }
- }
- if((wFlags & 0x0100) && pLRSeg && pLRSeg->m_Result.sd->m_bContextRetained) {
- if (pSymbolDictDecoder->SDHUFF == 0) {
- dwTemp = pSymbolDictDecoder->SDTEMPLATE == 0 ? 65536 : pSymbolDictDecoder->SDTEMPLATE == 1 ?
- 8192 : 1024;
- gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
- JBIG2_memcpy(gbContext, pLRSeg->m_Result.sd->m_gbContext, sizeof(JBig2ArithCtx)*dwTemp);
- }
- if (pSymbolDictDecoder->SDREFAGG == 1) {
- dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13;
- grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
- JBIG2_memcpy(grContext, pLRSeg->m_Result.sd->m_grContext, sizeof(JBig2ArithCtx)*dwTemp);
- }
- } else {
- if (pSymbolDictDecoder->SDHUFF == 0) {
- dwTemp = pSymbolDictDecoder->SDTEMPLATE == 0 ? 65536 : pSymbolDictDecoder->SDTEMPLATE == 1 ?
- 8192 : 1024;
- gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
- JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
- }
- if (pSymbolDictDecoder->SDREFAGG == 1) {
- dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13;
- grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
- JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
- }
- }
- pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER;
- if(pSymbolDictDecoder->SDHUFF == 0) {
- JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
- pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecoder, gbContext, grContext);
- delete pArithDecoder;
- if(pSegment->m_Result.sd == NULL) {
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pStream->alignByte();
- m_pStream->offset(2);
- } else {
- pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream, gbContext, grContext, pPause);
- if(pSegment->m_Result.sd == NULL) {
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pStream->alignByte();
- }
- if(wFlags & 0x0200) {
- pSegment->m_Result.sd->m_bContextRetained = TRUE;
- if(pSymbolDictDecoder->SDHUFF == 0) {
- pSegment->m_Result.sd->m_gbContext = gbContext;
- }
- if(pSymbolDictDecoder->SDREFAGG == 1) {
- pSegment->m_Result.sd->m_grContext = grContext;
- }
- bUsed = TRUE;
- } else {
- bUsed = FALSE;
- }
- delete pSymbolDictDecoder;
- if(SDINSYMS) {
- m_pModule->JBig2_Free(SDINSYMS);
- }
- if(Table_B1) {
- delete Table_B1;
- }
- if(Table_B2) {
- delete Table_B2;
- }
- if(Table_B3) {
- delete Table_B3;
- }
- if(Table_B4) {
- delete Table_B4;
- }
- if(Table_B5) {
- delete Table_B5;
- }
- if(bUsed == FALSE) {
- if(gbContext) {
- m_pModule->JBig2_Free(gbContext);
- }
- if(grContext) {
- m_pModule->JBig2_Free(grContext);
- }
- }
- return JBIG2_SUCCESS;
-failed:
- delete pSymbolDictDecoder;
- if(SDINSYMS) {
- m_pModule->JBig2_Free(SDINSYMS);
- }
- if(Table_B1) {
- delete Table_B1;
- }
- if(Table_B2) {
- delete Table_B2;
- }
- if(Table_B3) {
- delete Table_B3;
- }
- if(Table_B4) {
- delete Table_B4;
- }
- if(Table_B5) {
- delete Table_B5;
- }
- if(gbContext) {
- m_pModule->JBig2_Free(gbContext);
- }
- if(grContext) {
- m_pModule->JBig2_Free(grContext);
- }
- return nRet;
-}
-
-FX_BOOL CJBig2_Context::parseTextRegion(CJBig2_Segment *pSegment)
-{
- FX_DWORD dwTemp;
- FX_WORD wFlags;
- FX_INT32 i, nIndex, nRet;
- JBig2RegionInfo ri;
- CJBig2_Segment *pSeg;
- CJBig2_Image **SBSYMS = NULL;
- JBig2HuffmanCode *SBSYMCODES = NULL;
- FX_BYTE cSBHUFFFS, cSBHUFFDS, cSBHUFFDT, cSBHUFFRDW, cSBHUFFRDH, cSBHUFFRDX, cSBHUFFRDY, cSBHUFFRSIZE;
- CJBig2_HuffmanTable *Table_B1 = NULL,
- *Table_B6 = NULL,
- *Table_B7 = NULL,
- *Table_B8 = NULL,
- *Table_B9 = NULL,
- *Table_B10 = NULL,
- *Table_B11 = NULL,
- *Table_B12 = NULL,
- *Table_B13 = NULL,
- *Table_B14 = NULL,
- *Table_B15 = NULL;
- JBig2ArithCtx *grContext = NULL;
- CJBig2_ArithDecoder *pArithDecoder;
- CJBig2_TRDProc *pTRD;
- JBIG2_ALLOC(pTRD, CJBig2_TRDProc());
- if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
- || (m_pStream->readShortInteger(&wFlags) != 0)) {
- m_pModule->JBig2_Error("text region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- pTRD->SBW = ri.width;
- pTRD->SBH = ri.height;
- pTRD->SBHUFF = wFlags & 0x0001;
- pTRD->SBREFINE = (wFlags >> 1) & 0x0001;
- dwTemp = (wFlags >> 2) & 0x0003;
- pTRD->SBSTRIPS = 1 << dwTemp;
- pTRD->REFCORNER = (JBig2Corner)((wFlags >> 4) & 0x0003);
- pTRD->TRANSPOSED = (wFlags >> 6) & 0x0001;
- pTRD->SBCOMBOP = (JBig2ComposeOp)((wFlags >> 7) & 0x0003);
- pTRD->SBDEFPIXEL = (wFlags >> 9) & 0x0001;
- pTRD->SBDSOFFSET = (wFlags >> 10) & 0x001f;
- if(pTRD->SBDSOFFSET >= 0x0010) {
- pTRD->SBDSOFFSET = pTRD->SBDSOFFSET - 0x0020;
- }
- pTRD->SBRTEMPLATE = (wFlags >> 15) & 0x0001;
- if(pTRD->SBHUFF == 1) {
- if(m_pStream->readShortInteger(&wFlags) != 0) {
- m_pModule->JBig2_Error("text region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- cSBHUFFFS = wFlags & 0x0003;
- cSBHUFFDS = (wFlags >> 2) & 0x0003;
- cSBHUFFDT = (wFlags >> 4) & 0x0003;
- cSBHUFFRDW = (wFlags >> 6) & 0x0003;
- cSBHUFFRDH = (wFlags >> 8) & 0x0003;
- cSBHUFFRDX = (wFlags >> 10) & 0x0003;
- cSBHUFFRDY = (wFlags >> 12) & 0x0003;
- cSBHUFFRSIZE = (wFlags >> 14) & 0x0001;
- }
- if((pTRD->SBREFINE == 1) && (pTRD->SBRTEMPLATE == 0)) {
- for(i = 0; i < 4; i++) {
- if(m_pStream->read1Byte((FX_BYTE*)&pTRD->SBRAT[i]) != 0) {
- m_pModule->JBig2_Error("text region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- }
- }
- if(m_pStream->readInteger(&pTRD->SBNUMINSTANCES) != 0) {
- m_pModule->JBig2_Error("text region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- if(!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) {
- m_pModule->JBig2_Error("text region segment : can't find refered to segments");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- }
- pTRD->SBNUMSYMS = 0;
- for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
- if(pSeg->m_cFlags.s.type == 0) {
- pTRD->SBNUMSYMS += pSeg->m_Result.sd->SDNUMEXSYMS;
- }
- }
- if (pTRD->SBNUMSYMS > 0) {
- SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(
- sizeof(CJBig2_Image*), pTRD->SBNUMSYMS);
- dwTemp = 0;
- for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
- if(pSeg->m_cFlags.s.type == 0) {
- JBIG2_memcpy(SBSYMS + dwTemp, pSeg->m_Result.sd->SDEXSYMS,
- pSeg->m_Result.sd->SDNUMEXSYMS * sizeof(CJBig2_Image*));
- dwTemp += pSeg->m_Result.sd->SDNUMEXSYMS;
- }
- }
- pTRD->SBSYMS = SBSYMS;
- } else {
- pTRD->SBSYMS = NULL;
- }
- if(pTRD->SBHUFF == 1) {
- SBSYMCODES = decodeSymbolIDHuffmanTable(m_pStream, pTRD->SBNUMSYMS);
- if(SBSYMCODES == NULL) {
- m_pModule->JBig2_Error("text region segment: symbol ID huffman table decode failure!");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pStream->alignByte();
- pTRD->SBSYMCODES = SBSYMCODES;
- } else {
- dwTemp = 0;
- while((FX_DWORD)(1 << dwTemp) < pTRD->SBNUMSYMS) {
- dwTemp ++;
- }
- pTRD->SBSYMCODELEN = (FX_BYTE)dwTemp;
- }
- if(pTRD->SBHUFF == 1) {
- if((cSBHUFFFS == 2) || (cSBHUFFRDW == 2) || (cSBHUFFRDH == 2)
- || (cSBHUFFRDX == 2) || (cSBHUFFRDY == 2)) {
- m_pModule->JBig2_Error("text region segment : SBHUFFFS=2 or SBHUFFRDW=2 or "
- "SBHUFFRDH=2 or SBHUFFRDX=2 or SBHUFFRDY=2 is not permitted");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- nIndex = 0;
- if(cSBHUFFFS == 0) {
- JBIG2_ALLOC(Table_B6, CJBig2_HuffmanTable(HuffmanTable_B6,
- sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
- pTRD->SBHUFFFS = Table_B6;
- } else if(cSBHUFFFS == 1) {
- JBIG2_ALLOC(Table_B7, CJBig2_HuffmanTable(HuffmanTable_B7,
- sizeof(HuffmanTable_B7) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B7));
- pTRD->SBHUFFFS = Table_B7;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("text region segment : SBHUFFFS can't find user supplied table");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pTRD->SBHUFFFS = pSeg->m_Result.ht;
- }
- if(cSBHUFFDS == 0) {
- JBIG2_ALLOC(Table_B8, CJBig2_HuffmanTable(HuffmanTable_B8,
- sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
- pTRD->SBHUFFDS = Table_B8;
- } else if(cSBHUFFDS == 1) {
- JBIG2_ALLOC(Table_B9, CJBig2_HuffmanTable(HuffmanTable_B9,
- sizeof(HuffmanTable_B9) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B9));
- pTRD->SBHUFFDS = Table_B9;
- } else if(cSBHUFFDS == 2) {
- JBIG2_ALLOC(Table_B10, CJBig2_HuffmanTable(HuffmanTable_B10,
- sizeof(HuffmanTable_B10) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B10));
- pTRD->SBHUFFDS = Table_B10;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("text region segment : SBHUFFDS can't find user supplied table");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pTRD->SBHUFFDS = pSeg->m_Result.ht;
- }
- if(cSBHUFFDT == 0) {
- JBIG2_ALLOC(Table_B11, CJBig2_HuffmanTable(HuffmanTable_B11,
- sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
- pTRD->SBHUFFDT = Table_B11;
- } else if(cSBHUFFDT == 1) {
- JBIG2_ALLOC(Table_B12, CJBig2_HuffmanTable(HuffmanTable_B12,
- sizeof(HuffmanTable_B12) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B12));
- pTRD->SBHUFFDT = Table_B12;
- } else if(cSBHUFFDT == 2) {
- JBIG2_ALLOC(Table_B13, CJBig2_HuffmanTable(HuffmanTable_B13,
- sizeof(HuffmanTable_B13) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B13));
- pTRD->SBHUFFDT = Table_B13;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("text region segment : SBHUFFDT can't find user supplied table");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pTRD->SBHUFFDT = pSeg->m_Result.ht;
- }
- if(cSBHUFFRDW == 0) {
- JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
- sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
- pTRD->SBHUFFRDW = Table_B14;
- } else if(cSBHUFFRDW == 1) {
- JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- pTRD->SBHUFFRDW = Table_B15;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("text region segment : SBHUFFRDW can't find user supplied table");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pTRD->SBHUFFRDW = pSeg->m_Result.ht;
- }
- if(cSBHUFFRDH == 0) {
- if(!Table_B14) {
- JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
- sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
- }
- pTRD->SBHUFFRDH = Table_B14;
- } else if(cSBHUFFRDH == 1) {
- if(!Table_B15) {
- JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- }
- pTRD->SBHUFFRDH = Table_B15;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("text region segment : SBHUFFRDH can't find user supplied table");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pTRD->SBHUFFRDH = pSeg->m_Result.ht;
- }
- if(cSBHUFFRDX == 0) {
- if(!Table_B14) {
- JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
- sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
- }
- pTRD->SBHUFFRDX = Table_B14;
- } else if(cSBHUFFRDX == 1) {
- if(!Table_B15) {
- JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- }
- pTRD->SBHUFFRDX = Table_B15;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("text region segment : SBHUFFRDX can't find user supplied table");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pTRD->SBHUFFRDX = pSeg->m_Result.ht;
- }
- if(cSBHUFFRDY == 0) {
- if(!Table_B14) {
- JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
- sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
- }
- pTRD->SBHUFFRDY = Table_B14;
- } else if(cSBHUFFRDY == 1) {
- if(!Table_B15) {
- JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- }
- pTRD->SBHUFFRDY = Table_B15;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("text region segment : SBHUFFRDY can't find user supplied table");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pTRD->SBHUFFRDY = pSeg->m_Result.ht;
- }
- if(cSBHUFFRSIZE == 0) {
- JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
- sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
- pTRD->SBHUFFRSIZE = Table_B1;
- } else {
- pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
- if(!pSeg) {
- m_pModule->JBig2_Error("text region segment : SBHUFFRSIZE can't find user supplied table");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pTRD->SBHUFFRSIZE = pSeg->m_Result.ht;
- }
- }
- if(pTRD->SBREFINE == 1) {
- dwTemp = pTRD->SBRTEMPLATE ? 1 << 10 : 1 << 13;
- grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
- JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
- }
- if(pTRD->SBHUFF == 0) {
- JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
- pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
- pSegment->m_Result.im = pTRD->decode_Arith(pArithDecoder, grContext);
- delete pArithDecoder;
- if(pSegment->m_Result.im == NULL) {
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pStream->alignByte();
- m_pStream->offset(2);
- } else {
- pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
- pSegment->m_Result.im = pTRD->decode_Huffman(m_pStream, grContext);
- if(pSegment->m_Result.im == NULL) {
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pStream->alignByte();
- }
- if(pSegment->m_cFlags.s.type != 4) {
- if(!m_bBufSpecified) {
- JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
- if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
- m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
- }
- }
- m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
- delete pSegment->m_Result.im;
- pSegment->m_Result.im = NULL;
- }
- delete pTRD;
- if(SBSYMS) {
- m_pModule->JBig2_Free(SBSYMS);
- }
- if(SBSYMCODES) {
- m_pModule->JBig2_Free(SBSYMCODES);
- }
- if(grContext) {
- m_pModule->JBig2_Free(grContext);
- }
- if(Table_B1) {
- delete Table_B1;
- }
- if(Table_B6) {
- delete Table_B6;
- }
- if(Table_B7) {
- delete Table_B7;
- }
- if(Table_B8) {
- delete Table_B8;
- }
- if(Table_B9) {
- delete Table_B9;
- }
- if(Table_B10) {
- delete Table_B10;
- }
- if(Table_B11) {
- delete Table_B11;
- }
- if(Table_B12) {
- delete Table_B12;
- }
- if(Table_B13) {
- delete Table_B13;
- }
- if(Table_B14) {
- delete Table_B14;
- }
- if(Table_B15) {
- delete Table_B15;
- }
- return JBIG2_SUCCESS;
-failed:
- delete pTRD;
- if(SBSYMS) {
- m_pModule->JBig2_Free(SBSYMS);
- }
- if(SBSYMCODES) {
- m_pModule->JBig2_Free(SBSYMCODES);
- }
- if(grContext) {
- m_pModule->JBig2_Free(grContext);
- }
- if(Table_B1) {
- delete Table_B1;
- }
- if(Table_B6) {
- delete Table_B6;
- }
- if(Table_B7) {
- delete Table_B7;
- }
- if(Table_B8) {
- delete Table_B8;
- }
- if(Table_B9) {
- delete Table_B9;
- }
- if(Table_B10) {
- delete Table_B10;
- }
- if(Table_B11) {
- delete Table_B11;
- }
- if(Table_B12) {
- delete Table_B12;
- }
- if(Table_B13) {
- delete Table_B13;
- }
- if(Table_B14) {
- delete Table_B14;
- }
- if(Table_B15) {
- delete Table_B15;
- }
- return nRet;
-}
-
-FX_BOOL CJBig2_Context::parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause)
-{
- FX_DWORD dwTemp;
- FX_BYTE cFlags;
- JBig2ArithCtx *gbContext;
- CJBig2_ArithDecoder *pArithDecoder;
- CJBig2_PDDProc *pPDD;
- FX_INT32 nRet;
- JBIG2_ALLOC(pPDD, CJBig2_PDDProc());
- if((m_pStream->read1Byte(&cFlags) != 0)
- || (m_pStream->read1Byte(&pPDD->HDPW) != 0)
- || (m_pStream->read1Byte(&pPDD->HDPH) != 0)
- || (m_pStream->readInteger(&pPDD->GRAYMAX) != 0)) {
- m_pModule->JBig2_Error("pattern dictionary segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- if (pPDD->GRAYMAX > JBIG2_MAX_PATTERN_INDEX) {
- m_pModule->JBig2_Error("pattern dictionary segment : too max gray max.");
- nRet = JBIG2_ERROR_LIMIT;
- goto failed;
- }
- pPDD->HDMMR = cFlags & 0x01;
- pPDD->HDTEMPLATE = (cFlags >> 1) & 0x03;
- pSegment->m_nResultType = JBIG2_PATTERN_DICT_POINTER;
- if(pPDD->HDMMR == 0) {
- dwTemp = pPDD->HDTEMPLATE == 0 ? 65536 : pPDD->HDTEMPLATE == 1 ? 8192 : 1024;
- gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
- JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
- JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
- pSegment->m_Result.pd = pPDD->decode_Arith(pArithDecoder, gbContext, pPause);
- delete pArithDecoder;
- if(pSegment->m_Result.pd == NULL) {
- m_pModule->JBig2_Free(gbContext);
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pModule->JBig2_Free(gbContext);
- m_pStream->alignByte();
- m_pStream->offset(2);
- } else {
- pSegment->m_Result.pd = pPDD->decode_MMR(m_pStream, pPause);
- if(pSegment->m_Result.pd == NULL) {
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pStream->alignByte();
- }
- delete pPDD;
- return JBIG2_SUCCESS;
-failed:
- delete pPDD;
- return nRet;
-}
-FX_BOOL CJBig2_Context::parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause)
-{
- FX_DWORD dwTemp;
- FX_BYTE cFlags;
- JBig2RegionInfo ri;
- CJBig2_Segment *pSeg;
- CJBig2_PatternDict *pPatternDict;
- JBig2ArithCtx *gbContext;
- CJBig2_ArithDecoder *pArithDecoder;
- CJBig2_HTRDProc *pHRD;
- FX_INT32 nRet;
- JBIG2_ALLOC(pHRD, CJBig2_HTRDProc());
- if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
- || (m_pStream->read1Byte(&cFlags) != 0)
- || (m_pStream->readInteger(&pHRD->HGW) != 0)
- || (m_pStream->readInteger(&pHRD->HGH) != 0)
- || (m_pStream->readInteger((FX_DWORD*)&pHRD->HGX) != 0)
- || (m_pStream->readInteger((FX_DWORD*)&pHRD->HGY) != 0)
- || (m_pStream->readShortInteger(&pHRD->HRX) != 0)
- || (m_pStream->readShortInteger(&pHRD->HRY) != 0)) {
- m_pModule->JBig2_Error("halftone region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- pHRD->HBW = ri.width;
- pHRD->HBH = ri.height;
- pHRD->HMMR = cFlags & 0x01;
- pHRD->HTEMPLATE = (cFlags >> 1) & 0x03;
- pHRD->HENABLESKIP = (cFlags >> 3) & 0x01;
- pHRD->HCOMBOP = (JBig2ComposeOp)((cFlags >> 4) & 0x07);
- pHRD->HDEFPIXEL = (cFlags >> 7) & 0x01;
- if(pSegment->m_nReferred_to_segment_count != 1) {
- m_pModule->JBig2_Error("halftone region segment : refered to segment count not equals 1");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]);
- if( (pSeg == NULL) || (pSeg->m_cFlags.s.type != 16)) {
- m_pModule->JBig2_Error("halftone region segment : refered to segment is not pattern dict");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pPatternDict = pSeg->m_Result.pd;
- if((pPatternDict == NULL) || (pPatternDict->NUMPATS == 0)) {
- m_pModule->JBig2_Error("halftone region segment : has no patterns input");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pHRD->HNUMPATS = pPatternDict->NUMPATS;
- pHRD->HPATS = pPatternDict->HDPATS;
- pHRD->HPW = pPatternDict->HDPATS[0]->m_nWidth;
- pHRD->HPH = pPatternDict->HDPATS[0]->m_nHeight;
- pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
- if(pHRD->HMMR == 0) {
- dwTemp = pHRD->HTEMPLATE == 0 ? 65536 : pHRD->HTEMPLATE == 1 ? 8192 : 1024;
- gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
- JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
- JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
- pSegment->m_Result.im = pHRD->decode_Arith(pArithDecoder, gbContext, pPause);
- delete pArithDecoder;
- if(pSegment->m_Result.im == NULL) {
- m_pModule->JBig2_Free(gbContext);
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pModule->JBig2_Free(gbContext);
- m_pStream->alignByte();
- m_pStream->offset(2);
- } else {
- pSegment->m_Result.im = pHRD->decode_MMR(m_pStream, pPause);
- if(pSegment->m_Result.im == NULL) {
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pStream->alignByte();
- }
- if(pSegment->m_cFlags.s.type != 20) {
- if(!m_bBufSpecified) {
- JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
- if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
- m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
- }
- }
- m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
- delete pSegment->m_Result.im;
- pSegment->m_Result.im = NULL;
- }
- delete pHRD;
- return JBIG2_SUCCESS;
-failed:
- delete pHRD;
- return nRet;
-}
-
-FX_BOOL CJBig2_Context::parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause)
-{
- FX_DWORD dwTemp;
- FX_BYTE cFlags;
- FX_INT32 i, nRet;
- if(m_pGRD == NULL) {
- JBIG2_ALLOC(m_pGRD, CJBig2_GRDProc());
- if((parseRegionInfo(&m_ri) != JBIG2_SUCCESS)
- || (m_pStream->read1Byte(&cFlags) != 0)) {
- m_pModule->JBig2_Error("generic region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- if (m_ri.height < 0 || m_ri.width < 0) {
- m_pModule->JBig2_Error("generic region segment : wrong data.");
- nRet = JBIG2_FAILED;
- goto failed;
- }
- m_pGRD->GBW = m_ri.width;
- m_pGRD->GBH = m_ri.height;
- m_pGRD->MMR = cFlags & 0x01;
- m_pGRD->GBTEMPLATE = (cFlags >> 1) & 0x03;
- m_pGRD->TPGDON = (cFlags >> 3) & 0x01;
- if(m_pGRD->MMR == 0) {
- if(m_pGRD->GBTEMPLATE == 0) {
- for(i = 0; i < 8; i++) {
- if(m_pStream->read1Byte((FX_BYTE*)&m_pGRD->GBAT[i]) != 0) {
- m_pModule->JBig2_Error("generic region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- }
- } else {
- for(i = 0; i < 2; i++) {
- if(m_pStream->read1Byte((FX_BYTE*)&m_pGRD->GBAT[i]) != 0) {
- m_pModule->JBig2_Error("generic region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- }
- }
- }
- m_pGRD->USESKIP = 0;
- }
- pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
- if(m_pGRD->MMR == 0) {
- dwTemp = m_pGRD->GBTEMPLATE == 0 ? 65536 : m_pGRD->GBTEMPLATE == 1 ? 8192 : 1024;
- if(m_gbContext == NULL) {
- m_gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc(sizeof(JBig2ArithCtx) * dwTemp);
- JBIG2_memset(m_gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
- }
- if(m_pArithDecoder == NULL) {
- JBIG2_ALLOC(m_pArithDecoder, CJBig2_ArithDecoder(m_pStream));
- m_ProcessiveStatus = m_pGRD->Start_decode_Arith(&pSegment->m_Result.im, m_pArithDecoder, m_gbContext, pPause);
- } else {
- m_ProcessiveStatus = m_pGRD->Continue_decode(pPause);
- }
- OutputBitmap(pSegment->m_Result.im);
- if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- if(pSegment->m_cFlags.s.type != 36) {
- if(!m_bBufSpecified) {
- JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
- if ((pPageInfo->m_bIsStriped == 1) && (m_ri.y + m_ri.height > m_pPage->m_nHeight)) {
- m_pPage->expand(m_ri.y + m_ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
- }
- }
- FX_RECT Rect = m_pGRD->GetReplaceRect();
- m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, pSegment->m_Result.im, (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect);
- }
- return JBIG2_SUCCESS;
- } else {
- delete m_pArithDecoder;
- m_pArithDecoder = NULL;
- if(pSegment->m_Result.im == NULL) {
- m_pModule->JBig2_Free(m_gbContext);
- nRet = JBIG2_ERROR_FETAL;
- m_gbContext = NULL;
- m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
- goto failed;
- }
- m_pModule->JBig2_Free(m_gbContext);
- m_gbContext = NULL;
- m_pStream->alignByte();
- m_pStream->offset(2);
- }
- } else {
- FXCODEC_STATUS status = m_pGRD->Start_decode_MMR(&pSegment->m_Result.im, m_pStream, pPause);
- while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- m_pGRD->Continue_decode(pPause);
- }
- if(pSegment->m_Result.im == NULL) {
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pStream->alignByte();
- }
- if(pSegment->m_cFlags.s.type != 36) {
- if(!m_bBufSpecified) {
- JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
- if ((pPageInfo->m_bIsStriped == 1) && (m_ri.y + m_ri.height > m_pPage->m_nHeight)) {
- m_pPage->expand(m_ri.y + m_ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
- }
- }
- FX_RECT Rect = m_pGRD->GetReplaceRect();
- m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, pSegment->m_Result.im, (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect);
- delete pSegment->m_Result.im;
- pSegment->m_Result.im = NULL;
- }
- delete m_pGRD;
- m_pGRD = NULL;
- return JBIG2_SUCCESS;
-failed:
- delete m_pGRD;
- m_pGRD = NULL;
- return nRet;
-}
-
-FX_BOOL CJBig2_Context::parseGenericRefinementRegion(CJBig2_Segment *pSegment)
-{
- FX_DWORD dwTemp;
- JBig2RegionInfo ri;
- CJBig2_Segment *pSeg;
- FX_INT32 i, nRet;
- FX_BYTE cFlags;
- JBig2ArithCtx *grContext;
- CJBig2_GRRDProc *pGRRD;
- CJBig2_ArithDecoder *pArithDecoder;
- JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
- if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
- || (m_pStream->read1Byte(&cFlags) != 0)) {
- m_pModule->JBig2_Error("generic refinement region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- pGRRD->GRW = ri.width;
- pGRRD->GRH = ri.height;
- pGRRD->GRTEMPLATE = cFlags & 0x01;
- pGRRD->TPGRON = (cFlags >> 1) & 0x01;
- if(pGRRD->GRTEMPLATE == 0) {
- for(i = 0; i < 4; i++) {
- if(m_pStream->read1Byte((FX_BYTE*)&pGRRD->GRAT[i]) != 0) {
- m_pModule->JBig2_Error("generic refinement region segment : data header too short.");
- nRet = JBIG2_ERROR_TOO_SHORT;
- goto failed;
- }
- }
- }
- pSeg = NULL;
- if(pSegment->m_nReferred_to_segment_count > 0) {
- for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
- pSeg = this->findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]);
- if(pSeg == NULL) {
- m_pModule->JBig2_Error("generic refinement region segment : can't find refered to segments");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- if((pSeg->m_cFlags.s.type == 4) || (pSeg->m_cFlags.s.type == 20)
- || (pSeg->m_cFlags.s.type == 36) || (pSeg->m_cFlags.s.type == 40)) {
- break;
- }
- }
- if(i >= pSegment->m_nReferred_to_segment_count) {
- m_pModule->JBig2_Error("generic refinement region segment : can't find refered to intermediate region");
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- pGRRD->GRREFERENCE = pSeg->m_Result.im;
- } else {
- pGRRD->GRREFERENCE = m_pPage;
- }
- pGRRD->GRREFERENCEDX = 0;
- pGRRD->GRREFERENCEDY = 0;
- dwTemp = pGRRD->GRTEMPLATE ? 1 << 10 : 1 << 13;
- grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
- JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
- JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
- pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
- pSegment->m_Result.im = pGRRD->decode(pArithDecoder, grContext);
- delete pArithDecoder;
- if(pSegment->m_Result.im == NULL) {
- m_pModule->JBig2_Free(grContext);
- nRet = JBIG2_ERROR_FETAL;
- goto failed;
- }
- m_pModule->JBig2_Free(grContext);
- m_pStream->alignByte();
- m_pStream->offset(2);
- if(pSegment->m_cFlags.s.type != 40) {
- if(!m_bBufSpecified) {
- JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
- if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
- m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
- }
- }
- m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
- delete pSegment->m_Result.im;
- pSegment->m_Result.im = NULL;
- }
- delete pGRRD;
- return JBIG2_SUCCESS;
-failed:
- delete pGRRD;
- return nRet;
-}
-FX_BOOL CJBig2_Context::parseTable(CJBig2_Segment *pSegment)
-{
- pSegment->m_nResultType = JBIG2_HUFFMAN_TABLE_POINTER;
- JBIG2_ALLOC(pSegment->m_Result.ht, CJBig2_HuffmanTable(m_pStream));
- if(!pSegment->m_Result.ht->isOK()) {
- delete pSegment->m_Result.ht;
- pSegment->m_Result.ht = NULL;
- return JBIG2_ERROR_FETAL;
- }
- m_pStream->alignByte();
- return JBIG2_SUCCESS;
-}
-FX_INT32 CJBig2_Context::parseRegionInfo(JBig2RegionInfo *pRI)
-{
- if((m_pStream->readInteger((FX_DWORD*)&pRI->width) != 0)
- || (m_pStream->readInteger((FX_DWORD*)&pRI->height) != 0)
- || (m_pStream->readInteger((FX_DWORD*)&pRI->x) != 0)
- || (m_pStream->readInteger((FX_DWORD*)&pRI->y) != 0)
- || (m_pStream->read1Byte(&pRI->flags) != 0)) {
- return JBIG2_ERROR_TOO_SHORT;
- }
- return JBIG2_SUCCESS;
-}
-JBig2HuffmanCode *CJBig2_Context::decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream,
- FX_DWORD SBNUMSYMS)
-{
- JBig2HuffmanCode *SBSYMCODES;
- FX_INT32 runcodes[35], runcodes_len[35], runcode;
- FX_INT32 i, j, nTemp, nVal, nBits;
- FX_INT32 run;
- SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(sizeof(JBig2HuffmanCode), SBNUMSYMS);
- for (i = 0; i < 35; i ++) {
- if(pStream->readNBits(4, &runcodes_len[i]) != 0) {
- goto failed;
- }
- }
- huffman_assign_code(runcodes, runcodes_len, 35);
- i = 0;
- while(i < (int)SBNUMSYMS) {
- nVal = 0;
- nBits = 0;
- for(;;) {
- if(pStream->read1Bit(&nTemp) != 0) {
- goto failed;
- }
- nVal = (nVal << 1) | nTemp;
- nBits ++;
- for(j = 0; j < 35; j++) {
- if((nBits == runcodes_len[j]) && (nVal == runcodes[j])) {
- break;
- }
- }
- if(j < 35) {
- break;
- }
- }
- runcode = j;
- if(runcode < 32) {
- SBSYMCODES[i].codelen = runcode;
- run = 0;
- } else if(runcode == 32) {
- if(pStream->readNBits(2, &nTemp) != 0) {
- goto failed;
- }
- run = nTemp + 3;
- } else if(runcode == 33) {
- if(pStream->readNBits(3, &nTemp) != 0) {
- goto failed;
- }
- run = nTemp + 3;
- } else if(runcode == 34) {
- if(pStream->readNBits(7, &nTemp) != 0) {
- goto failed;
- }
- run = nTemp + 11;
- }
- if(run > 0) {
- if (i + run > (int)SBNUMSYMS) {
- goto failed;
- }
- for(j = 0; j < run; j++) {
- if(runcode == 32 && i > 0) {
- SBSYMCODES[i + j].codelen = SBSYMCODES[i - 1].codelen;
- } else {
- SBSYMCODES[i + j].codelen = 0;
- }
- }
- i += run;
- } else {
- i ++;
- }
- }
- huffman_assign_code(SBSYMCODES, SBNUMSYMS);
- return SBSYMCODES;
-failed:
- m_pModule->JBig2_Free(SBSYMCODES);
- return NULL;
-}
-void CJBig2_Context::huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP)
-{
- int CURLEN, LENMAX, CURCODE, CURTEMP, i;
- int *LENCOUNT;
- int *FIRSTCODE;
- LENMAX = 0;
- for(i = 0; i < NTEMP; i++) {
- if(PREFLEN[i] > LENMAX) {
- LENMAX = PREFLEN[i];
- }
- }
- LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
- JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
- FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
- for(i = 0; i < NTEMP; i++) {
- LENCOUNT[PREFLEN[i]] ++;
- }
- CURLEN = 1;
- FIRSTCODE[0] = 0;
- LENCOUNT[0] = 0;
- while(CURLEN <= LENMAX) {
- FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
- CURCODE = FIRSTCODE[CURLEN];
- CURTEMP = 0;
- while(CURTEMP < NTEMP) {
- if(PREFLEN[CURTEMP] == CURLEN) {
- CODES[CURTEMP] = CURCODE;
- CURCODE = CURCODE + 1;
- }
- CURTEMP = CURTEMP + 1;
- }
- CURLEN = CURLEN + 1;
- }
- m_pModule->JBig2_Free(LENCOUNT);
- m_pModule->JBig2_Free(FIRSTCODE);
-}
-void CJBig2_Context::huffman_assign_code(JBig2HuffmanCode *SBSYMCODES, int NTEMP)
-{
- int CURLEN, LENMAX, CURCODE, CURTEMP, i;
- int *LENCOUNT;
- int *FIRSTCODE;
- LENMAX = 0;
- for(i = 0; i < NTEMP; i++) {
- if(SBSYMCODES[i].codelen > LENMAX) {
- LENMAX = SBSYMCODES[i].codelen;
- }
- }
- LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
- JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
- FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
- for(i = 0; i < NTEMP; i++) {
- LENCOUNT[SBSYMCODES[i].codelen] ++;
- }
- CURLEN = 1;
- FIRSTCODE[0] = 0;
- LENCOUNT[0] = 0;
- while(CURLEN <= LENMAX) {
- FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
- CURCODE = FIRSTCODE[CURLEN];
- CURTEMP = 0;
- while(CURTEMP < NTEMP) {
- if(SBSYMCODES[CURTEMP].codelen == CURLEN) {
- SBSYMCODES[CURTEMP].code = CURCODE;
- CURCODE = CURCODE + 1;
- }
- CURTEMP = CURTEMP + 1;
- }
- CURLEN = CURLEN + 1;
- }
- m_pModule->JBig2_Free(LENCOUNT);
- m_pModule->JBig2_Free(FIRSTCODE);
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_Context.h"
+void OutputBitmap(CJBig2_Image* pImage)
+{
+ if(!pImage) {
+ return;
+ }
+}
+CJBig2_Context *CJBig2_Context::CreateContext(CJBig2_Module *pModule, FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
+ FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause)
+{
+ return new(pModule) CJBig2_Context(pGlobalData, dwGlobalLength, pData, dwLength, nStreamType, pPause);
+}
+void CJBig2_Context::DestroyContext(CJBig2_Context *pContext)
+{
+ if(pContext) {
+ delete pContext;
+ }
+}
+CJBig2_Context::CJBig2_Context(FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
+ FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause)
+{
+ if(pGlobalData && (dwGlobalLength > 0)) {
+ JBIG2_ALLOC(m_pGlobalContext, CJBig2_Context(NULL, 0, pGlobalData, dwGlobalLength,
+ JBIG2_EMBED_STREAM, pPause));
+ } else {
+ m_pGlobalContext = NULL;
+ }
+ JBIG2_ALLOC(m_pStream, CJBig2_BitStream(pData, dwLength));
+ m_nStreamType = nStreamType;
+ m_nState = JBIG2_OUT_OF_PAGE;
+ JBIG2_ALLOC(m_pSegmentList, CJBig2_List<CJBig2_Segment>);
+ JBIG2_ALLOC(m_pPageInfoList, CJBig2_List<JBig2PageInfo>(1));
+ m_pPage = NULL;
+ m_bBufSpecified = FALSE;
+ m_pPause = pPause;
+ m_nSegmentDecoded = 0;
+ m_PauseStep = 10;
+ m_pArithDecoder = NULL;
+ m_pGRD = NULL;
+ m_gbContext = NULL;
+ m_pSegment = NULL;
+ m_dwOffset = 0;
+ m_ProcessiveStatus = FXCODEC_STATUS_FRAME_READY;
+}
+CJBig2_Context::~CJBig2_Context()
+{
+ if(m_pArithDecoder) {
+ delete m_pArithDecoder;
+ }
+ m_pArithDecoder = NULL;
+ if(m_pGRD) {
+ delete m_pGRD;
+ }
+ m_pGRD = NULL;
+ if(m_gbContext) {
+ delete m_gbContext;
+ }
+ m_gbContext = NULL;
+ if(m_pGlobalContext) {
+ delete m_pGlobalContext;
+ }
+ m_pGlobalContext = NULL;
+ if(m_pPageInfoList) {
+ delete m_pPageInfoList;
+ }
+ m_pPageInfoList = NULL;
+ if(m_bBufSpecified && m_pPage) {
+ delete m_pPage;
+ }
+ m_pPage = NULL;
+ if(m_pStream) {
+ delete m_pStream;
+ }
+ m_pStream = NULL;
+ if(m_pSegmentList) {
+ delete m_pSegmentList;
+ }
+ m_pSegmentList = NULL;
+}
+FX_INT32 CJBig2_Context::decodeFile(IFX_Pause* pPause)
+{
+ FX_BYTE cFlags;
+ FX_DWORD dwTemp;
+ const FX_BYTE fileID[] = {0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A};
+ FX_INT32 nRet;
+ if(m_pStream->getByteLeft() < 8) {
+ m_pModule->JBig2_Error("file header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if(JBIG2_memcmp(m_pStream->getPointer(), fileID, 8) != 0) {
+ m_pModule->JBig2_Error("not jbig2 file");
+ nRet = JBIG2_ERROR_FILE_FORMAT;
+ goto failed;
+ }
+ m_pStream->offset(8);
+ if(m_pStream->read1Byte(&cFlags) != 0) {
+ m_pModule->JBig2_Error("file header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if(!(cFlags & 0x02)) {
+ if(m_pStream->readInteger(&dwTemp) != 0) {
+ m_pModule->JBig2_Error("file header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if(dwTemp > 0) {
+ delete m_pPageInfoList;
+ JBIG2_ALLOC(m_pPageInfoList, CJBig2_List<JBig2PageInfo>(dwTemp));
+ }
+ }
+ if(cFlags & 0x01) {
+ m_nStreamType = JBIG2_SQUENTIAL_STREAM;
+ return decode_SquentialOrgnazation(pPause);
+ } else {
+ m_nStreamType = JBIG2_RANDOM_STREAM;
+ return decode_RandomOrgnazation_FirstPage(pPause);
+ }
+failed:
+ return nRet;
+}
+FX_INT32 CJBig2_Context::decode_SquentialOrgnazation(IFX_Pause* pPause)
+{
+ FX_INT32 nRet;
+ if(m_pStream->getByteLeft() > 0) {
+ while(m_pStream->getByteLeft() >= JBIG2_MIN_SEGMENT_SIZE) {
+ if(m_pSegment == NULL) {
+ JBIG2_ALLOC(m_pSegment, CJBig2_Segment());
+ nRet = parseSegmentHeader(m_pSegment);
+ if(nRet != JBIG2_SUCCESS) {
+ delete m_pSegment;
+ m_pSegment = NULL;
+ return nRet;
+ }
+ m_dwOffset = m_pStream->getOffset();
+ }
+ nRet = parseSegmentData(m_pSegment, pPause);
+ if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ m_PauseStep = 2;
+ return JBIG2_SUCCESS;
+ }
+ if((nRet == JBIG2_END_OF_PAGE) || (nRet == JBIG2_END_OF_FILE)) {
+ delete m_pSegment;
+ m_pSegment = NULL;
+ break;
+ } else if(nRet != JBIG2_SUCCESS) {
+ delete m_pSegment;
+ m_pSegment = NULL;
+ return nRet;
+ }
+ m_pSegmentList->addItem(m_pSegment);
+ if(m_pSegment->m_dwData_length != 0xffffffff) {
+ m_dwOffset = m_dwOffset + m_pSegment->m_dwData_length;
+ m_pStream->setOffset(m_dwOffset);
+ } else {
+ m_pStream->offset(4);
+ }
+ OutputBitmap(m_pPage);
+ m_pSegment = NULL;
+ if(m_pStream->getByteLeft() > 0 && m_pPage && pPause && pPause->NeedToPauseNow()) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ m_PauseStep = 2;
+ return JBIG2_SUCCESS;
+ }
+ }
+ } else {
+ return JBIG2_END_OF_FILE;
+ }
+ return JBIG2_SUCCESS;
+}
+FX_INT32 CJBig2_Context::decode_EmbedOrgnazation(IFX_Pause* pPause)
+{
+ return decode_SquentialOrgnazation(pPause);
+}
+FX_INT32 CJBig2_Context::decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause)
+{
+ CJBig2_Segment *pSegment;
+ FX_INT32 nRet;
+ while(m_pStream->getByteLeft() > JBIG2_MIN_SEGMENT_SIZE) {
+ JBIG2_ALLOC(pSegment, CJBig2_Segment());
+ nRet = parseSegmentHeader(pSegment);
+ if(nRet != JBIG2_SUCCESS) {
+ delete pSegment;
+ return nRet;
+ } else if(pSegment->m_cFlags.s.type == 51) {
+ delete pSegment;
+ break;
+ }
+ m_pSegmentList->addItem(pSegment);
+ if(pPause && m_pPause && pPause->NeedToPauseNow()) {
+ m_PauseStep = 3;
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return JBIG2_SUCCESS;
+ }
+ }
+ m_nSegmentDecoded = 0;
+ return decode_RandomOrgnazation(pPause);
+}
+FX_INT32 CJBig2_Context::decode_RandomOrgnazation(IFX_Pause* pPause)
+{
+ FX_INT32 nRet;
+ for(; m_nSegmentDecoded < m_pSegmentList->getLength(); m_nSegmentDecoded++) {
+ nRet = parseSegmentData(m_pSegmentList->getAt(m_nSegmentDecoded), pPause);
+ if((nRet == JBIG2_END_OF_PAGE) || (nRet == JBIG2_END_OF_FILE)) {
+ break;
+ } else if(nRet != JBIG2_SUCCESS) {
+ return nRet;
+ }
+ if(m_pPage && pPause && pPause->NeedToPauseNow()) {
+ m_PauseStep = 4;
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return JBIG2_SUCCESS;
+ }
+ }
+ return JBIG2_SUCCESS;
+}
+FX_INT32 CJBig2_Context::getFirstPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause)
+{
+ FX_INT32 nRet = 0;
+ if(m_pGlobalContext) {
+ nRet = m_pGlobalContext->decode_EmbedOrgnazation(pPause);
+ if(nRet != JBIG2_SUCCESS) {
+ m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
+ return nRet;
+ }
+ }
+ m_bFirstPage = TRUE;
+ m_PauseStep = 0;
+ if(m_pPage) {
+ delete m_pPage;
+ }
+ JBIG2_ALLOC(m_pPage, CJBig2_Image(width, height, stride, pBuf));
+ m_bBufSpecified = TRUE;
+ if(m_pPage && pPause && pPause->NeedToPauseNow()) {
+ m_PauseStep = 1;
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return nRet;
+ }
+ int ret = Continue(pPause);
+ return ret;
+}
+FX_INT32 CJBig2_Context::Continue(IFX_Pause* pPause)
+{
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_READY;
+ FX_INT32 nRet;
+ if(m_PauseStep <= 1) {
+ switch(m_nStreamType) {
+ case JBIG2_FILE_STREAM:
+ nRet = decodeFile(pPause);
+ break;
+ case JBIG2_SQUENTIAL_STREAM:
+ nRet = decode_SquentialOrgnazation(pPause);
+ break;
+ case JBIG2_RANDOM_STREAM:
+ if(m_bFirstPage) {
+ nRet = decode_RandomOrgnazation_FirstPage(pPause);
+ } else {
+ nRet = decode_RandomOrgnazation(pPause);
+ }
+ break;
+ case JBIG2_EMBED_STREAM:
+ nRet = decode_EmbedOrgnazation(pPause);
+ break;
+ default:
+ m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
+ return JBIG2_ERROR_STREAM_TYPE;
+ }
+ } else if(m_PauseStep == 2) {
+ nRet = decode_SquentialOrgnazation(pPause);
+ } else if(m_PauseStep == 3) {
+ nRet = decode_RandomOrgnazation_FirstPage(pPause);
+ } else if(m_PauseStep == 4) {
+ nRet = decode_RandomOrgnazation(pPause);
+ } else if(m_PauseStep == 5) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return JBIG2_SUCCESS;
+ }
+ if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ return nRet;
+ }
+ m_PauseStep = 5;
+ if(!m_bBufSpecified && nRet == JBIG2_SUCCESS) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return JBIG2_SUCCESS;
+ }
+ if(nRet == JBIG2_SUCCESS) {
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ } else {
+ m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
+ }
+ return nRet;
+}
+FX_INT32 CJBig2_Context::getNextPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause)
+{
+ FX_INT32 nRet = JBIG2_ERROR_STREAM_TYPE;
+ m_bFirstPage = FALSE;
+ m_PauseStep = 0;
+ if(m_pPage) {
+ delete m_pPage;
+ }
+ JBIG2_ALLOC(m_pPage, CJBig2_Image(width, height, stride, pBuf));
+ m_bBufSpecified = TRUE;
+ if(m_pPage && pPause && pPause->NeedToPauseNow()) {
+ m_PauseStep = 1;
+ m_ProcessiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return nRet;
+ }
+ return Continue(pPause);
+ switch(m_nStreamType) {
+ case JBIG2_FILE_STREAM:
+ nRet = decodeFile(pPause);
+ break;
+ case JBIG2_SQUENTIAL_STREAM:
+ nRet = decode_SquentialOrgnazation(pPause);
+ break;
+ case JBIG2_RANDOM_STREAM:
+ nRet = decode_RandomOrgnazation(pPause);
+ break;
+ case JBIG2_EMBED_STREAM:
+ nRet = decode_EmbedOrgnazation(pPause);
+ break;
+ default:
+ return JBIG2_ERROR_STREAM_TYPE;
+ }
+ return nRet;
+}
+FX_INT32 CJBig2_Context::getFirstPage(CJBig2_Image **image, IFX_Pause* pPause)
+{
+ FX_INT32 nRet;
+ m_bFirstPage = TRUE;
+ m_PauseStep = 0;
+ if(m_pGlobalContext) {
+ nRet = m_pGlobalContext->decode_EmbedOrgnazation(pPause);
+ if(nRet != JBIG2_SUCCESS) {
+ return nRet;
+ }
+ }
+ m_bBufSpecified = FALSE;
+ return Continue(pPause);
+}
+FX_INT32 CJBig2_Context::getNextPage(CJBig2_Image **image, IFX_Pause* pPause)
+{
+ FX_INT32 nRet;
+ m_bBufSpecified = FALSE;
+ m_bFirstPage = FALSE;
+ m_PauseStep = 0;
+ switch(m_nStreamType) {
+ case JBIG2_FILE_STREAM:
+ nRet = decodeFile(pPause);
+ break;
+ case JBIG2_SQUENTIAL_STREAM:
+ nRet = decode_SquentialOrgnazation(pPause);
+ break;
+ case JBIG2_RANDOM_STREAM:
+ nRet = decode_RandomOrgnazation(pPause);
+ break;
+ case JBIG2_EMBED_STREAM:
+ nRet = decode_EmbedOrgnazation(pPause);
+ break;
+ default:
+ return JBIG2_ERROR_STREAM_TYPE;
+ }
+ if(nRet == JBIG2_SUCCESS) {
+ *image = m_pPage;
+ m_pPage = NULL;
+ return JBIG2_SUCCESS;
+ }
+ return nRet;
+}
+CJBig2_Segment *CJBig2_Context::findSegmentByNumber(FX_DWORD dwNumber)
+{
+ CJBig2_Segment *pSeg;
+ FX_INT32 i;
+ if(m_pGlobalContext) {
+ pSeg = m_pGlobalContext->findSegmentByNumber(dwNumber);
+ if(pSeg) {
+ return pSeg;
+ }
+ }
+ for(i = 0; i < m_pSegmentList->getLength(); i++) {
+ pSeg = m_pSegmentList->getAt(i);
+ if(pSeg->m_dwNumber == dwNumber) {
+ return pSeg;
+ }
+ }
+ return NULL;
+}
+CJBig2_Segment *CJBig2_Context::findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment,
+ FX_BYTE cType, FX_INT32 nIndex)
+{
+ CJBig2_Segment *pSeg;
+ FX_INT32 i, count;
+ count = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg && pSeg->m_cFlags.s.type == cType) {
+ if(count == nIndex) {
+ return pSeg;
+ } else {
+ count ++;
+ }
+ }
+ }
+ return NULL;
+}
+FX_INT32 CJBig2_Context::parseSegmentHeader(CJBig2_Segment *pSegment)
+{
+ FX_BYTE cSSize, cPSize;
+ FX_BYTE cTemp;
+ FX_WORD wTemp;
+ FX_DWORD dwTemp;
+ if((m_pStream->readInteger(&pSegment->m_dwNumber) != 0)
+ || (m_pStream->read1Byte(&pSegment->m_cFlags.c) != 0)) {
+ goto failed;
+ }
+ cTemp = m_pStream->getCurByte();
+ if((cTemp >> 5) == 7) {
+ if(m_pStream->readInteger((FX_DWORD*)&pSegment->m_nReferred_to_segment_count) != 0) {
+ goto failed;
+ }
+ pSegment->m_nReferred_to_segment_count &= 0x1fffffff;
+ if (pSegment->m_nReferred_to_segment_count > JBIG2_MAX_REFERRED_SEGMENT_COUNT) {
+ m_pModule->JBig2_Error("Too many referred segments.");
+ return JBIG2_ERROR_LIMIT;
+ }
+ dwTemp = 5 + 4 + (pSegment->m_nReferred_to_segment_count + 1) / 8;
+ } else {
+ if(m_pStream->read1Byte(&cTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_nReferred_to_segment_count = cTemp >> 5;
+ dwTemp = 5 + 1;
+ }
+ cSSize = pSegment->m_dwNumber > 65536 ? 4 : pSegment->m_dwNumber > 256 ? 2 : 1;
+ cPSize = pSegment->m_cFlags.s.page_association_size ? 4 : 1;
+ if(pSegment->m_nReferred_to_segment_count) {
+ pSegment->m_pReferred_to_segment_numbers = (FX_DWORD*)m_pModule->JBig2_Malloc2(
+ sizeof(FX_DWORD), pSegment->m_nReferred_to_segment_count);
+ for(FX_INT32 i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ switch(cSSize) {
+ case 1:
+ if(m_pStream->read1Byte(&cTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_pReferred_to_segment_numbers[i] = cTemp;
+ break;
+ case 2:
+ if(m_pStream->readShortInteger(&wTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_pReferred_to_segment_numbers[i] = wTemp;
+ break;
+ case 4:
+ if(m_pStream->readInteger(&dwTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_pReferred_to_segment_numbers[i] = dwTemp;
+ break;
+ }
+ if (pSegment->m_pReferred_to_segment_numbers[i] >= pSegment->m_dwNumber) {
+ m_pModule->JBig2_Error("The referred segment number is greater than this segment number.");
+ goto failed;
+ }
+ }
+ }
+ if(cPSize == 1) {
+ if(m_pStream->read1Byte(&cTemp) != 0) {
+ goto failed;
+ }
+ pSegment->m_dwPage_association = cTemp;
+ } else {
+ if(m_pStream->readInteger(&pSegment->m_dwPage_association) != 0) {
+ goto failed;
+ }
+ }
+ if(m_pStream->readInteger(&pSegment->m_dwData_length) != 0) {
+ goto failed;
+ }
+ pSegment->m_pData = m_pStream->getPointer();
+ pSegment->m_State = JBIG2_SEGMENT_DATA_UNPARSED;
+ return JBIG2_SUCCESS;
+failed:
+ m_pModule->JBig2_Error("header too short.");
+ return JBIG2_ERROR_TOO_SHORT;
+}
+FX_INT32 CJBig2_Context::parseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_INT32 ret = ProcessiveParseSegmentData(pSegment, pPause);
+ while(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE && m_pStream->getByteLeft() > 0) {
+ ret = ProcessiveParseSegmentData(pSegment, pPause);
+ }
+ return ret;
+}
+FX_INT32 CJBig2_Context::ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ switch(pSegment->m_cFlags.s.type) {
+ case 0:
+ return parseSymbolDict(pSegment, pPause);
+ case 4:
+ case 6:
+ case 7:
+ if(m_nState == JBIG2_OUT_OF_PAGE) {
+ goto failed2;
+ } else {
+ return parseTextRegion(pSegment);
+ }
+ case 16:
+ return parsePatternDict(pSegment, pPause);
+ case 20:
+ case 22:
+ case 23:
+ if(m_nState == JBIG2_OUT_OF_PAGE) {
+ goto failed2;
+ } else {
+ return parseHalftoneRegion(pSegment, pPause);
+ }
+ case 36:
+ case 38:
+ case 39:
+ if(m_nState == JBIG2_OUT_OF_PAGE) {
+ goto failed2;
+ } else {
+ return parseGenericRegion(pSegment, pPause);
+ }
+ case 40:
+ case 42:
+ case 43:
+ if(m_nState == JBIG2_OUT_OF_PAGE) {
+ goto failed2;
+ } else {
+ return parseGenericRefinementRegion(pSegment);
+ }
+ case 48: {
+ FX_WORD wTemp;
+ JBig2PageInfo *pPageInfo;
+ JBIG2_ALLOC(pPageInfo, JBig2PageInfo);
+ if((m_pStream->readInteger(&pPageInfo->m_dwWidth) != 0)
+ || (m_pStream->readInteger(&pPageInfo->m_dwHeight) != 0)
+ || (m_pStream->readInteger(&pPageInfo->m_dwResolutionX) != 0)
+ || (m_pStream->readInteger(&pPageInfo->m_dwResolutionY) != 0)
+ || (m_pStream->read1Byte(&pPageInfo->m_cFlags) != 0)
+ || (m_pStream->readShortInteger(&wTemp) != 0)) {
+ delete pPageInfo;
+ goto failed1;
+ }
+ pPageInfo->m_bIsStriped = ((wTemp >> 15) & 1) ? 1 : 0;
+ pPageInfo->m_wMaxStripeSize = wTemp & 0x7fff;
+ if((pPageInfo->m_dwHeight == 0xffffffff) && (pPageInfo->m_bIsStriped != 1)) {
+ m_pModule->JBig2_Warn("page height = 0xffffffff buf stripe field is 0");
+ pPageInfo->m_bIsStriped = 1;
+ }
+ if(!m_bBufSpecified) {
+ if(m_pPage) {
+ delete m_pPage;
+ }
+ if(pPageInfo->m_dwHeight == 0xffffffff) {
+ JBIG2_ALLOC(m_pPage, CJBig2_Image(pPageInfo->m_dwWidth, pPageInfo->m_wMaxStripeSize));
+ } else {
+ JBIG2_ALLOC(m_pPage, CJBig2_Image(pPageInfo->m_dwWidth, pPageInfo->m_dwHeight));
+ }
+ }
+ m_pPage->fill((pPageInfo->m_cFlags & 4) ? 1 : 0);
+ m_pPageInfoList->addItem(pPageInfo);
+ m_nState = JBIG2_IN_PAGE;
+ }
+ break;
+ case 49:
+ m_nState = JBIG2_OUT_OF_PAGE;
+ return JBIG2_END_OF_PAGE;
+ break;
+ case 50:
+ m_pStream->offset(pSegment->m_dwData_length);
+ break;
+ case 51:
+ return JBIG2_END_OF_FILE;
+ case 52:
+ m_pStream->offset(pSegment->m_dwData_length);
+ break;
+ case 53:
+ return parseTable(pSegment);
+ case 62:
+ m_pStream->offset(pSegment->m_dwData_length);
+ break;
+ default:
+ break;
+ }
+ return JBIG2_SUCCESS;
+failed1:
+ m_pModule->JBig2_Error("segment data too short.");
+ return JBIG2_ERROR_TOO_SHORT;
+failed2:
+ m_pModule->JBig2_Error("segment syntax error.");
+ return JBIG2_ERROR_FETAL;
+}
+FX_INT32 CJBig2_Context::parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_DWORD dwTemp;
+ FX_WORD wFlags;
+ FX_BYTE cSDHUFFDH, cSDHUFFDW, cSDHUFFBMSIZE, cSDHUFFAGGINST;
+ CJBig2_HuffmanTable *Table_B1 = NULL, *Table_B2 = NULL, *Table_B3 = NULL, *Table_B4 = NULL, *Table_B5 = NULL;
+ FX_INT32 i, nIndex, nRet;
+ CJBig2_Segment *pSeg = NULL, *pLRSeg = NULL;
+ FX_BOOL bUsed;
+ CJBig2_Image ** SDINSYMS = NULL;
+ CJBig2_SDDProc *pSymbolDictDecoder;
+ JBig2ArithCtx *gbContext = NULL, *grContext = NULL;
+ CJBig2_ArithDecoder *pArithDecoder;
+ JBIG2_ALLOC(pSymbolDictDecoder, CJBig2_SDDProc());
+ if(m_pStream->readShortInteger(&wFlags) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFF = wFlags & 0x0001;
+ pSymbolDictDecoder->SDREFAGG = (wFlags >> 1) & 0x0001;
+ pSymbolDictDecoder->SDTEMPLATE = (wFlags >> 10) & 0x0003;
+ pSymbolDictDecoder->SDRTEMPLATE = (wFlags >> 12) & 0x0003;
+ cSDHUFFDH = (wFlags >> 2) & 0x0003;
+ cSDHUFFDW = (wFlags >> 4) & 0x0003;
+ cSDHUFFBMSIZE = (wFlags >> 6) & 0x0001;
+ cSDHUFFAGGINST = (wFlags >> 7) & 0x0001;
+ if(pSymbolDictDecoder->SDHUFF == 0) {
+ if(pSymbolDictDecoder->SDTEMPLATE == 0) {
+ dwTemp = 8;
+ } else {
+ dwTemp = 2;
+ }
+ for(i = 0; i < (FX_INT32)dwTemp; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&pSymbolDictDecoder->SDAT[i]) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ if((pSymbolDictDecoder->SDREFAGG == 1) && (pSymbolDictDecoder->SDRTEMPLATE == 0)) {
+ for(i = 0; i < 4; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&pSymbolDictDecoder->SDRAT[i]) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ if((m_pStream->readInteger(&pSymbolDictDecoder->SDNUMEXSYMS) != 0)
+ || (m_pStream->readInteger(&pSymbolDictDecoder->SDNUMNEWSYMS) != 0)) {
+ m_pModule->JBig2_Error("symbol dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if (pSymbolDictDecoder->SDNUMEXSYMS > JBIG2_MAX_EXPORT_SYSMBOLS
+ || pSymbolDictDecoder->SDNUMNEWSYMS > JBIG2_MAX_NEW_SYSMBOLS) {
+ m_pModule->JBig2_Error("symbol dictionary segment : too many export/new symbols.");
+ nRet = JBIG2_ERROR_LIMIT;
+ goto failed;
+ }
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ if(!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) {
+ m_pModule->JBig2_Error("symbol dictionary segment : can't find refered to segments");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ }
+ pSymbolDictDecoder->SDNUMINSYMS = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg->m_cFlags.s.type == 0) {
+ pSymbolDictDecoder->SDNUMINSYMS += pSeg->m_Result.sd->SDNUMEXSYMS;
+ pLRSeg = pSeg;
+ }
+ }
+ if(pSymbolDictDecoder->SDNUMINSYMS == 0) {
+ SDINSYMS = NULL;
+ } else {
+ SDINSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(
+ sizeof(CJBig2_Image*), pSymbolDictDecoder->SDNUMINSYMS);
+ dwTemp = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg->m_cFlags.s.type == 0) {
+ JBIG2_memcpy(SDINSYMS + dwTemp, pSeg->m_Result.sd->SDEXSYMS,
+ pSeg->m_Result.sd->SDNUMEXSYMS * sizeof(CJBig2_Image*));
+ dwTemp += pSeg->m_Result.sd->SDNUMEXSYMS;
+ }
+ }
+ }
+ pSymbolDictDecoder->SDINSYMS = SDINSYMS;
+ if(pSymbolDictDecoder->SDHUFF == 1) {
+ if((cSDHUFFDH == 2) || (cSDHUFFDW == 2)) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDH=2 or SDHUFFDW=2 is not permitted.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ nIndex = 0;
+ if(cSDHUFFDH == 0) {
+ JBIG2_ALLOC(Table_B4, CJBig2_HuffmanTable(HuffmanTable_B4,
+ sizeof(HuffmanTable_B4) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B4));
+ pSymbolDictDecoder->SDHUFFDH = Table_B4;
+ } else if(cSDHUFFDH == 1) {
+ JBIG2_ALLOC(Table_B5, CJBig2_HuffmanTable(HuffmanTable_B5,
+ sizeof(HuffmanTable_B5) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B5));
+ pSymbolDictDecoder->SDHUFFDH = Table_B5;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDH can't find user supplied table.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFFDH = pSeg->m_Result.ht;
+ }
+ if(cSDHUFFDW == 0) {
+ JBIG2_ALLOC(Table_B2, CJBig2_HuffmanTable(HuffmanTable_B2,
+ sizeof(HuffmanTable_B2) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B2));
+ pSymbolDictDecoder->SDHUFFDW = Table_B2;
+ } else if(cSDHUFFDW == 1) {
+ JBIG2_ALLOC(Table_B3, CJBig2_HuffmanTable(HuffmanTable_B3,
+ sizeof(HuffmanTable_B3) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B3));
+ pSymbolDictDecoder->SDHUFFDW = Table_B3;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFDW can't find user supplied table.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFFDW = pSeg->m_Result.ht;
+ }
+ if(cSDHUFFBMSIZE == 0) {
+ JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ pSymbolDictDecoder->SDHUFFBMSIZE = Table_B1;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFBMSIZE can't find user supplied table.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFFBMSIZE = pSeg->m_Result.ht;
+ }
+ if(pSymbolDictDecoder->SDREFAGG == 1) {
+ if(cSDHUFFAGGINST == 0) {
+ if(!Table_B1) {
+ JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ }
+ pSymbolDictDecoder->SDHUFFAGGINST = Table_B1;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("symbol dictionary segment : SDHUFFAGGINST can't find user supplied table.");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSymbolDictDecoder->SDHUFFAGGINST = pSeg->m_Result.ht;
+ }
+ }
+ }
+ if((wFlags & 0x0100) && pLRSeg && pLRSeg->m_Result.sd->m_bContextRetained) {
+ if (pSymbolDictDecoder->SDHUFF == 0) {
+ dwTemp = pSymbolDictDecoder->SDTEMPLATE == 0 ? 65536 : pSymbolDictDecoder->SDTEMPLATE == 1 ?
+ 8192 : 1024;
+ gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memcpy(gbContext, pLRSeg->m_Result.sd->m_gbContext, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ if (pSymbolDictDecoder->SDREFAGG == 1) {
+ dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13;
+ grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memcpy(grContext, pLRSeg->m_Result.sd->m_grContext, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ } else {
+ if (pSymbolDictDecoder->SDHUFF == 0) {
+ dwTemp = pSymbolDictDecoder->SDTEMPLATE == 0 ? 65536 : pSymbolDictDecoder->SDTEMPLATE == 1 ?
+ 8192 : 1024;
+ gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ if (pSymbolDictDecoder->SDREFAGG == 1) {
+ dwTemp = pSymbolDictDecoder->SDRTEMPLATE ? 1 << 10 : 1 << 13;
+ grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ }
+ pSegment->m_nResultType = JBIG2_SYMBOL_DICT_POINTER;
+ if(pSymbolDictDecoder->SDHUFF == 0) {
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_Result.sd = pSymbolDictDecoder->decode_Arith(pArithDecoder, gbContext, grContext);
+ delete pArithDecoder;
+ if(pSegment->m_Result.sd == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ } else {
+ pSegment->m_Result.sd = pSymbolDictDecoder->decode_Huffman(m_pStream, gbContext, grContext, pPause);
+ if(pSegment->m_Result.sd == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ if(wFlags & 0x0200) {
+ pSegment->m_Result.sd->m_bContextRetained = TRUE;
+ if(pSymbolDictDecoder->SDHUFF == 0) {
+ pSegment->m_Result.sd->m_gbContext = gbContext;
+ }
+ if(pSymbolDictDecoder->SDREFAGG == 1) {
+ pSegment->m_Result.sd->m_grContext = grContext;
+ }
+ bUsed = TRUE;
+ } else {
+ bUsed = FALSE;
+ }
+ delete pSymbolDictDecoder;
+ if(SDINSYMS) {
+ m_pModule->JBig2_Free(SDINSYMS);
+ }
+ if(Table_B1) {
+ delete Table_B1;
+ }
+ if(Table_B2) {
+ delete Table_B2;
+ }
+ if(Table_B3) {
+ delete Table_B3;
+ }
+ if(Table_B4) {
+ delete Table_B4;
+ }
+ if(Table_B5) {
+ delete Table_B5;
+ }
+ if(bUsed == FALSE) {
+ if(gbContext) {
+ m_pModule->JBig2_Free(gbContext);
+ }
+ if(grContext) {
+ m_pModule->JBig2_Free(grContext);
+ }
+ }
+ return JBIG2_SUCCESS;
+failed:
+ delete pSymbolDictDecoder;
+ if(SDINSYMS) {
+ m_pModule->JBig2_Free(SDINSYMS);
+ }
+ if(Table_B1) {
+ delete Table_B1;
+ }
+ if(Table_B2) {
+ delete Table_B2;
+ }
+ if(Table_B3) {
+ delete Table_B3;
+ }
+ if(Table_B4) {
+ delete Table_B4;
+ }
+ if(Table_B5) {
+ delete Table_B5;
+ }
+ if(gbContext) {
+ m_pModule->JBig2_Free(gbContext);
+ }
+ if(grContext) {
+ m_pModule->JBig2_Free(grContext);
+ }
+ return nRet;
+}
+
+FX_BOOL CJBig2_Context::parseTextRegion(CJBig2_Segment *pSegment)
+{
+ FX_DWORD dwTemp;
+ FX_WORD wFlags;
+ FX_INT32 i, nIndex, nRet;
+ JBig2RegionInfo ri;
+ CJBig2_Segment *pSeg;
+ CJBig2_Image **SBSYMS = NULL;
+ JBig2HuffmanCode *SBSYMCODES = NULL;
+ FX_BYTE cSBHUFFFS, cSBHUFFDS, cSBHUFFDT, cSBHUFFRDW, cSBHUFFRDH, cSBHUFFRDX, cSBHUFFRDY, cSBHUFFRSIZE;
+ CJBig2_HuffmanTable *Table_B1 = NULL,
+ *Table_B6 = NULL,
+ *Table_B7 = NULL,
+ *Table_B8 = NULL,
+ *Table_B9 = NULL,
+ *Table_B10 = NULL,
+ *Table_B11 = NULL,
+ *Table_B12 = NULL,
+ *Table_B13 = NULL,
+ *Table_B14 = NULL,
+ *Table_B15 = NULL;
+ JBig2ArithCtx *grContext = NULL;
+ CJBig2_ArithDecoder *pArithDecoder;
+ CJBig2_TRDProc *pTRD;
+ JBIG2_ALLOC(pTRD, CJBig2_TRDProc());
+ if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
+ || (m_pStream->readShortInteger(&wFlags) != 0)) {
+ m_pModule->JBig2_Error("text region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ pTRD->SBW = ri.width;
+ pTRD->SBH = ri.height;
+ pTRD->SBHUFF = wFlags & 0x0001;
+ pTRD->SBREFINE = (wFlags >> 1) & 0x0001;
+ dwTemp = (wFlags >> 2) & 0x0003;
+ pTRD->SBSTRIPS = 1 << dwTemp;
+ pTRD->REFCORNER = (JBig2Corner)((wFlags >> 4) & 0x0003);
+ pTRD->TRANSPOSED = (wFlags >> 6) & 0x0001;
+ pTRD->SBCOMBOP = (JBig2ComposeOp)((wFlags >> 7) & 0x0003);
+ pTRD->SBDEFPIXEL = (wFlags >> 9) & 0x0001;
+ pTRD->SBDSOFFSET = (wFlags >> 10) & 0x001f;
+ if(pTRD->SBDSOFFSET >= 0x0010) {
+ pTRD->SBDSOFFSET = pTRD->SBDSOFFSET - 0x0020;
+ }
+ pTRD->SBRTEMPLATE = (wFlags >> 15) & 0x0001;
+ if(pTRD->SBHUFF == 1) {
+ if(m_pStream->readShortInteger(&wFlags) != 0) {
+ m_pModule->JBig2_Error("text region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ cSBHUFFFS = wFlags & 0x0003;
+ cSBHUFFDS = (wFlags >> 2) & 0x0003;
+ cSBHUFFDT = (wFlags >> 4) & 0x0003;
+ cSBHUFFRDW = (wFlags >> 6) & 0x0003;
+ cSBHUFFRDH = (wFlags >> 8) & 0x0003;
+ cSBHUFFRDX = (wFlags >> 10) & 0x0003;
+ cSBHUFFRDY = (wFlags >> 12) & 0x0003;
+ cSBHUFFRSIZE = (wFlags >> 14) & 0x0001;
+ }
+ if((pTRD->SBREFINE == 1) && (pTRD->SBRTEMPLATE == 0)) {
+ for(i = 0; i < 4; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&pTRD->SBRAT[i]) != 0) {
+ m_pModule->JBig2_Error("text region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ if(m_pStream->readInteger(&pTRD->SBNUMINSTANCES) != 0) {
+ m_pModule->JBig2_Error("text region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ if(!findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i])) {
+ m_pModule->JBig2_Error("text region segment : can't find refered to segments");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ }
+ pTRD->SBNUMSYMS = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg->m_cFlags.s.type == 0) {
+ pTRD->SBNUMSYMS += pSeg->m_Result.sd->SDNUMEXSYMS;
+ }
+ }
+ if (pTRD->SBNUMSYMS > 0) {
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(
+ sizeof(CJBig2_Image*), pTRD->SBNUMSYMS);
+ dwTemp = 0;
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[i]);
+ if(pSeg->m_cFlags.s.type == 0) {
+ JBIG2_memcpy(SBSYMS + dwTemp, pSeg->m_Result.sd->SDEXSYMS,
+ pSeg->m_Result.sd->SDNUMEXSYMS * sizeof(CJBig2_Image*));
+ dwTemp += pSeg->m_Result.sd->SDNUMEXSYMS;
+ }
+ }
+ pTRD->SBSYMS = SBSYMS;
+ } else {
+ pTRD->SBSYMS = NULL;
+ }
+ if(pTRD->SBHUFF == 1) {
+ SBSYMCODES = decodeSymbolIDHuffmanTable(m_pStream, pTRD->SBNUMSYMS);
+ if(SBSYMCODES == NULL) {
+ m_pModule->JBig2_Error("text region segment: symbol ID huffman table decode failure!");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ pTRD->SBSYMCODES = SBSYMCODES;
+ } else {
+ dwTemp = 0;
+ while((FX_DWORD)(1 << dwTemp) < pTRD->SBNUMSYMS) {
+ dwTemp ++;
+ }
+ pTRD->SBSYMCODELEN = (FX_BYTE)dwTemp;
+ }
+ if(pTRD->SBHUFF == 1) {
+ if((cSBHUFFFS == 2) || (cSBHUFFRDW == 2) || (cSBHUFFRDH == 2)
+ || (cSBHUFFRDX == 2) || (cSBHUFFRDY == 2)) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFFS=2 or SBHUFFRDW=2 or "
+ "SBHUFFRDH=2 or SBHUFFRDX=2 or SBHUFFRDY=2 is not permitted");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ nIndex = 0;
+ if(cSBHUFFFS == 0) {
+ JBIG2_ALLOC(Table_B6, CJBig2_HuffmanTable(HuffmanTable_B6,
+ sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
+ pTRD->SBHUFFFS = Table_B6;
+ } else if(cSBHUFFFS == 1) {
+ JBIG2_ALLOC(Table_B7, CJBig2_HuffmanTable(HuffmanTable_B7,
+ sizeof(HuffmanTable_B7) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B7));
+ pTRD->SBHUFFFS = Table_B7;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFFS can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFFS = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFDS == 0) {
+ JBIG2_ALLOC(Table_B8, CJBig2_HuffmanTable(HuffmanTable_B8,
+ sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
+ pTRD->SBHUFFDS = Table_B8;
+ } else if(cSBHUFFDS == 1) {
+ JBIG2_ALLOC(Table_B9, CJBig2_HuffmanTable(HuffmanTable_B9,
+ sizeof(HuffmanTable_B9) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B9));
+ pTRD->SBHUFFDS = Table_B9;
+ } else if(cSBHUFFDS == 2) {
+ JBIG2_ALLOC(Table_B10, CJBig2_HuffmanTable(HuffmanTable_B10,
+ sizeof(HuffmanTable_B10) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B10));
+ pTRD->SBHUFFDS = Table_B10;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFDS can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFDS = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFDT == 0) {
+ JBIG2_ALLOC(Table_B11, CJBig2_HuffmanTable(HuffmanTable_B11,
+ sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
+ pTRD->SBHUFFDT = Table_B11;
+ } else if(cSBHUFFDT == 1) {
+ JBIG2_ALLOC(Table_B12, CJBig2_HuffmanTable(HuffmanTable_B12,
+ sizeof(HuffmanTable_B12) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B12));
+ pTRD->SBHUFFDT = Table_B12;
+ } else if(cSBHUFFDT == 2) {
+ JBIG2_ALLOC(Table_B13, CJBig2_HuffmanTable(HuffmanTable_B13,
+ sizeof(HuffmanTable_B13) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B13));
+ pTRD->SBHUFFDT = Table_B13;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFDT can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFDT = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRDW == 0) {
+ JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
+ sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
+ pTRD->SBHUFFRDW = Table_B14;
+ } else if(cSBHUFFRDW == 1) {
+ JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ pTRD->SBHUFFRDW = Table_B15;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRDW can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRDW = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRDH == 0) {
+ if(!Table_B14) {
+ JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
+ sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
+ }
+ pTRD->SBHUFFRDH = Table_B14;
+ } else if(cSBHUFFRDH == 1) {
+ if(!Table_B15) {
+ JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ }
+ pTRD->SBHUFFRDH = Table_B15;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRDH can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRDH = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRDX == 0) {
+ if(!Table_B14) {
+ JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
+ sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
+ }
+ pTRD->SBHUFFRDX = Table_B14;
+ } else if(cSBHUFFRDX == 1) {
+ if(!Table_B15) {
+ JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ }
+ pTRD->SBHUFFRDX = Table_B15;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRDX can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRDX = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRDY == 0) {
+ if(!Table_B14) {
+ JBIG2_ALLOC(Table_B14, CJBig2_HuffmanTable(HuffmanTable_B14,
+ sizeof(HuffmanTable_B14) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B14));
+ }
+ pTRD->SBHUFFRDY = Table_B14;
+ } else if(cSBHUFFRDY == 1) {
+ if(!Table_B15) {
+ JBIG2_ALLOC(Table_B15, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ }
+ pTRD->SBHUFFRDY = Table_B15;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRDY can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRDY = pSeg->m_Result.ht;
+ }
+ if(cSBHUFFRSIZE == 0) {
+ JBIG2_ALLOC(Table_B1, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ pTRD->SBHUFFRSIZE = Table_B1;
+ } else {
+ pSeg = findReferredSegmentByTypeAndIndex(pSegment, 53, nIndex++);
+ if(!pSeg) {
+ m_pModule->JBig2_Error("text region segment : SBHUFFRSIZE can't find user supplied table");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pTRD->SBHUFFRSIZE = pSeg->m_Result.ht;
+ }
+ }
+ if(pTRD->SBREFINE == 1) {
+ dwTemp = pTRD->SBRTEMPLATE ? 1 << 10 : 1 << 13;
+ grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ if(pTRD->SBHUFF == 0) {
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ pSegment->m_Result.im = pTRD->decode_Arith(pArithDecoder, grContext);
+ delete pArithDecoder;
+ if(pSegment->m_Result.im == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ } else {
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ pSegment->m_Result.im = pTRD->decode_Huffman(m_pStream, grContext);
+ if(pSegment->m_Result.im == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ if(pSegment->m_cFlags.s.type != 4) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
+ delete pSegment->m_Result.im;
+ pSegment->m_Result.im = NULL;
+ }
+ delete pTRD;
+ if(SBSYMS) {
+ m_pModule->JBig2_Free(SBSYMS);
+ }
+ if(SBSYMCODES) {
+ m_pModule->JBig2_Free(SBSYMCODES);
+ }
+ if(grContext) {
+ m_pModule->JBig2_Free(grContext);
+ }
+ if(Table_B1) {
+ delete Table_B1;
+ }
+ if(Table_B6) {
+ delete Table_B6;
+ }
+ if(Table_B7) {
+ delete Table_B7;
+ }
+ if(Table_B8) {
+ delete Table_B8;
+ }
+ if(Table_B9) {
+ delete Table_B9;
+ }
+ if(Table_B10) {
+ delete Table_B10;
+ }
+ if(Table_B11) {
+ delete Table_B11;
+ }
+ if(Table_B12) {
+ delete Table_B12;
+ }
+ if(Table_B13) {
+ delete Table_B13;
+ }
+ if(Table_B14) {
+ delete Table_B14;
+ }
+ if(Table_B15) {
+ delete Table_B15;
+ }
+ return JBIG2_SUCCESS;
+failed:
+ delete pTRD;
+ if(SBSYMS) {
+ m_pModule->JBig2_Free(SBSYMS);
+ }
+ if(SBSYMCODES) {
+ m_pModule->JBig2_Free(SBSYMCODES);
+ }
+ if(grContext) {
+ m_pModule->JBig2_Free(grContext);
+ }
+ if(Table_B1) {
+ delete Table_B1;
+ }
+ if(Table_B6) {
+ delete Table_B6;
+ }
+ if(Table_B7) {
+ delete Table_B7;
+ }
+ if(Table_B8) {
+ delete Table_B8;
+ }
+ if(Table_B9) {
+ delete Table_B9;
+ }
+ if(Table_B10) {
+ delete Table_B10;
+ }
+ if(Table_B11) {
+ delete Table_B11;
+ }
+ if(Table_B12) {
+ delete Table_B12;
+ }
+ if(Table_B13) {
+ delete Table_B13;
+ }
+ if(Table_B14) {
+ delete Table_B14;
+ }
+ if(Table_B15) {
+ delete Table_B15;
+ }
+ return nRet;
+}
+
+FX_BOOL CJBig2_Context::parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_DWORD dwTemp;
+ FX_BYTE cFlags;
+ JBig2ArithCtx *gbContext;
+ CJBig2_ArithDecoder *pArithDecoder;
+ CJBig2_PDDProc *pPDD;
+ FX_INT32 nRet;
+ JBIG2_ALLOC(pPDD, CJBig2_PDDProc());
+ if((m_pStream->read1Byte(&cFlags) != 0)
+ || (m_pStream->read1Byte(&pPDD->HDPW) != 0)
+ || (m_pStream->read1Byte(&pPDD->HDPH) != 0)
+ || (m_pStream->readInteger(&pPDD->GRAYMAX) != 0)) {
+ m_pModule->JBig2_Error("pattern dictionary segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if (pPDD->GRAYMAX > JBIG2_MAX_PATTERN_INDEX) {
+ m_pModule->JBig2_Error("pattern dictionary segment : too max gray max.");
+ nRet = JBIG2_ERROR_LIMIT;
+ goto failed;
+ }
+ pPDD->HDMMR = cFlags & 0x01;
+ pPDD->HDTEMPLATE = (cFlags >> 1) & 0x03;
+ pSegment->m_nResultType = JBIG2_PATTERN_DICT_POINTER;
+ if(pPDD->HDMMR == 0) {
+ dwTemp = pPDD->HDTEMPLATE == 0 ? 65536 : pPDD->HDTEMPLATE == 1 ? 8192 : 1024;
+ gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_Result.pd = pPDD->decode_Arith(pArithDecoder, gbContext, pPause);
+ delete pArithDecoder;
+ if(pSegment->m_Result.pd == NULL) {
+ m_pModule->JBig2_Free(gbContext);
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(gbContext);
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ } else {
+ pSegment->m_Result.pd = pPDD->decode_MMR(m_pStream, pPause);
+ if(pSegment->m_Result.pd == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ delete pPDD;
+ return JBIG2_SUCCESS;
+failed:
+ delete pPDD;
+ return nRet;
+}
+FX_BOOL CJBig2_Context::parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_DWORD dwTemp;
+ FX_BYTE cFlags;
+ JBig2RegionInfo ri;
+ CJBig2_Segment *pSeg;
+ CJBig2_PatternDict *pPatternDict;
+ JBig2ArithCtx *gbContext;
+ CJBig2_ArithDecoder *pArithDecoder;
+ CJBig2_HTRDProc *pHRD;
+ FX_INT32 nRet;
+ JBIG2_ALLOC(pHRD, CJBig2_HTRDProc());
+ if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
+ || (m_pStream->read1Byte(&cFlags) != 0)
+ || (m_pStream->readInteger(&pHRD->HGW) != 0)
+ || (m_pStream->readInteger(&pHRD->HGH) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pHRD->HGX) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pHRD->HGY) != 0)
+ || (m_pStream->readShortInteger(&pHRD->HRX) != 0)
+ || (m_pStream->readShortInteger(&pHRD->HRY) != 0)) {
+ m_pModule->JBig2_Error("halftone region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ pHRD->HBW = ri.width;
+ pHRD->HBH = ri.height;
+ pHRD->HMMR = cFlags & 0x01;
+ pHRD->HTEMPLATE = (cFlags >> 1) & 0x03;
+ pHRD->HENABLESKIP = (cFlags >> 3) & 0x01;
+ pHRD->HCOMBOP = (JBig2ComposeOp)((cFlags >> 4) & 0x07);
+ pHRD->HDEFPIXEL = (cFlags >> 7) & 0x01;
+ if(pSegment->m_nReferred_to_segment_count != 1) {
+ m_pModule->JBig2_Error("halftone region segment : refered to segment count not equals 1");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pSeg = findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]);
+ if( (pSeg == NULL) || (pSeg->m_cFlags.s.type != 16)) {
+ m_pModule->JBig2_Error("halftone region segment : refered to segment is not pattern dict");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pPatternDict = pSeg->m_Result.pd;
+ if((pPatternDict == NULL) || (pPatternDict->NUMPATS == 0)) {
+ m_pModule->JBig2_Error("halftone region segment : has no patterns input");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pHRD->HNUMPATS = pPatternDict->NUMPATS;
+ pHRD->HPATS = pPatternDict->HDPATS;
+ pHRD->HPW = pPatternDict->HDPATS[0]->m_nWidth;
+ pHRD->HPH = pPatternDict->HDPATS[0]->m_nHeight;
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ if(pHRD->HMMR == 0) {
+ dwTemp = pHRD->HTEMPLATE == 0 ? 65536 : pHRD->HTEMPLATE == 1 ? 8192 : 1024;
+ gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_Result.im = pHRD->decode_Arith(pArithDecoder, gbContext, pPause);
+ delete pArithDecoder;
+ if(pSegment->m_Result.im == NULL) {
+ m_pModule->JBig2_Free(gbContext);
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(gbContext);
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ } else {
+ pSegment->m_Result.im = pHRD->decode_MMR(m_pStream, pPause);
+ if(pSegment->m_Result.im == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ if(pSegment->m_cFlags.s.type != 20) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
+ delete pSegment->m_Result.im;
+ pSegment->m_Result.im = NULL;
+ }
+ delete pHRD;
+ return JBIG2_SUCCESS;
+failed:
+ delete pHRD;
+ return nRet;
+}
+
+FX_BOOL CJBig2_Context::parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause)
+{
+ FX_DWORD dwTemp;
+ FX_BYTE cFlags;
+ FX_INT32 i, nRet;
+ if(m_pGRD == NULL) {
+ JBIG2_ALLOC(m_pGRD, CJBig2_GRDProc());
+ if((parseRegionInfo(&m_ri) != JBIG2_SUCCESS)
+ || (m_pStream->read1Byte(&cFlags) != 0)) {
+ m_pModule->JBig2_Error("generic region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ if (m_ri.height < 0 || m_ri.width < 0) {
+ m_pModule->JBig2_Error("generic region segment : wrong data.");
+ nRet = JBIG2_FAILED;
+ goto failed;
+ }
+ m_pGRD->GBW = m_ri.width;
+ m_pGRD->GBH = m_ri.height;
+ m_pGRD->MMR = cFlags & 0x01;
+ m_pGRD->GBTEMPLATE = (cFlags >> 1) & 0x03;
+ m_pGRD->TPGDON = (cFlags >> 3) & 0x01;
+ if(m_pGRD->MMR == 0) {
+ if(m_pGRD->GBTEMPLATE == 0) {
+ for(i = 0; i < 8; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&m_pGRD->GBAT[i]) != 0) {
+ m_pModule->JBig2_Error("generic region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ } else {
+ for(i = 0; i < 2; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&m_pGRD->GBAT[i]) != 0) {
+ m_pModule->JBig2_Error("generic region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ }
+ m_pGRD->USESKIP = 0;
+ }
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ if(m_pGRD->MMR == 0) {
+ dwTemp = m_pGRD->GBTEMPLATE == 0 ? 65536 : m_pGRD->GBTEMPLATE == 1 ? 8192 : 1024;
+ if(m_gbContext == NULL) {
+ m_gbContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc(sizeof(JBig2ArithCtx) * dwTemp);
+ JBIG2_memset(m_gbContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ }
+ if(m_pArithDecoder == NULL) {
+ JBIG2_ALLOC(m_pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ m_ProcessiveStatus = m_pGRD->Start_decode_Arith(&pSegment->m_Result.im, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProcessiveStatus = m_pGRD->Continue_decode(pPause);
+ }
+ OutputBitmap(pSegment->m_Result.im);
+ if(m_ProcessiveStatus == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ if(pSegment->m_cFlags.s.type != 36) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (m_ri.y + m_ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(m_ri.y + m_ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ FX_RECT Rect = m_pGRD->GetReplaceRect();
+ m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, pSegment->m_Result.im, (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect);
+ }
+ return JBIG2_SUCCESS;
+ } else {
+ delete m_pArithDecoder;
+ m_pArithDecoder = NULL;
+ if(pSegment->m_Result.im == NULL) {
+ m_pModule->JBig2_Free(m_gbContext);
+ nRet = JBIG2_ERROR_FETAL;
+ m_gbContext = NULL;
+ m_ProcessiveStatus = FXCODEC_STATUS_ERROR;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(m_gbContext);
+ m_gbContext = NULL;
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ }
+ } else {
+ FXCODEC_STATUS status = m_pGRD->Start_decode_MMR(&pSegment->m_Result.im, m_pStream, pPause);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ m_pGRD->Continue_decode(pPause);
+ }
+ if(pSegment->m_Result.im == NULL) {
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pStream->alignByte();
+ }
+ if(pSegment->m_cFlags.s.type != 36) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (m_ri.y + m_ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(m_ri.y + m_ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ FX_RECT Rect = m_pGRD->GetReplaceRect();
+ m_pPage->composeFrom(m_ri.x + Rect.left, m_ri.y + Rect.top, pSegment->m_Result.im, (JBig2ComposeOp)(m_ri.flags & 0x03), &Rect);
+ delete pSegment->m_Result.im;
+ pSegment->m_Result.im = NULL;
+ }
+ delete m_pGRD;
+ m_pGRD = NULL;
+ return JBIG2_SUCCESS;
+failed:
+ delete m_pGRD;
+ m_pGRD = NULL;
+ return nRet;
+}
+
+FX_BOOL CJBig2_Context::parseGenericRefinementRegion(CJBig2_Segment *pSegment)
+{
+ FX_DWORD dwTemp;
+ JBig2RegionInfo ri;
+ CJBig2_Segment *pSeg;
+ FX_INT32 i, nRet;
+ FX_BYTE cFlags;
+ JBig2ArithCtx *grContext;
+ CJBig2_GRRDProc *pGRRD;
+ CJBig2_ArithDecoder *pArithDecoder;
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ if((parseRegionInfo(&ri) != JBIG2_SUCCESS)
+ || (m_pStream->read1Byte(&cFlags) != 0)) {
+ m_pModule->JBig2_Error("generic refinement region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ pGRRD->GRW = ri.width;
+ pGRRD->GRH = ri.height;
+ pGRRD->GRTEMPLATE = cFlags & 0x01;
+ pGRRD->TPGRON = (cFlags >> 1) & 0x01;
+ if(pGRRD->GRTEMPLATE == 0) {
+ for(i = 0; i < 4; i++) {
+ if(m_pStream->read1Byte((FX_BYTE*)&pGRRD->GRAT[i]) != 0) {
+ m_pModule->JBig2_Error("generic refinement region segment : data header too short.");
+ nRet = JBIG2_ERROR_TOO_SHORT;
+ goto failed;
+ }
+ }
+ }
+ pSeg = NULL;
+ if(pSegment->m_nReferred_to_segment_count > 0) {
+ for(i = 0; i < pSegment->m_nReferred_to_segment_count; i++) {
+ pSeg = this->findSegmentByNumber(pSegment->m_pReferred_to_segment_numbers[0]);
+ if(pSeg == NULL) {
+ m_pModule->JBig2_Error("generic refinement region segment : can't find refered to segments");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ if((pSeg->m_cFlags.s.type == 4) || (pSeg->m_cFlags.s.type == 20)
+ || (pSeg->m_cFlags.s.type == 36) || (pSeg->m_cFlags.s.type == 40)) {
+ break;
+ }
+ }
+ if(i >= pSegment->m_nReferred_to_segment_count) {
+ m_pModule->JBig2_Error("generic refinement region segment : can't find refered to intermediate region");
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ pGRRD->GRREFERENCE = pSeg->m_Result.im;
+ } else {
+ pGRRD->GRREFERENCE = m_pPage;
+ }
+ pGRRD->GRREFERENCEDX = 0;
+ pGRRD->GRREFERENCEDY = 0;
+ dwTemp = pGRRD->GRTEMPLATE ? 1 << 10 : 1 << 13;
+ grContext = (JBig2ArithCtx*)m_pModule->JBig2_Malloc2(sizeof(JBig2ArithCtx), dwTemp);
+ JBIG2_memset(grContext, 0, sizeof(JBig2ArithCtx)*dwTemp);
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(m_pStream));
+ pSegment->m_nResultType = JBIG2_IMAGE_POINTER;
+ pSegment->m_Result.im = pGRRD->decode(pArithDecoder, grContext);
+ delete pArithDecoder;
+ if(pSegment->m_Result.im == NULL) {
+ m_pModule->JBig2_Free(grContext);
+ nRet = JBIG2_ERROR_FETAL;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(grContext);
+ m_pStream->alignByte();
+ m_pStream->offset(2);
+ if(pSegment->m_cFlags.s.type != 40) {
+ if(!m_bBufSpecified) {
+ JBig2PageInfo *pPageInfo = m_pPageInfoList->getLast();
+ if ((pPageInfo->m_bIsStriped == 1) && (ri.y + ri.height > m_pPage->m_nHeight)) {
+ m_pPage->expand(ri.y + ri.height, (pPageInfo->m_cFlags & 4) ? 1 : 0);
+ }
+ }
+ m_pPage->composeFrom(ri.x, ri.y, pSegment->m_Result.im, (JBig2ComposeOp)(ri.flags & 0x03));
+ delete pSegment->m_Result.im;
+ pSegment->m_Result.im = NULL;
+ }
+ delete pGRRD;
+ return JBIG2_SUCCESS;
+failed:
+ delete pGRRD;
+ return nRet;
+}
+FX_BOOL CJBig2_Context::parseTable(CJBig2_Segment *pSegment)
+{
+ pSegment->m_nResultType = JBIG2_HUFFMAN_TABLE_POINTER;
+ JBIG2_ALLOC(pSegment->m_Result.ht, CJBig2_HuffmanTable(m_pStream));
+ if(!pSegment->m_Result.ht->isOK()) {
+ delete pSegment->m_Result.ht;
+ pSegment->m_Result.ht = NULL;
+ return JBIG2_ERROR_FETAL;
+ }
+ m_pStream->alignByte();
+ return JBIG2_SUCCESS;
+}
+FX_INT32 CJBig2_Context::parseRegionInfo(JBig2RegionInfo *pRI)
+{
+ if((m_pStream->readInteger((FX_DWORD*)&pRI->width) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pRI->height) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pRI->x) != 0)
+ || (m_pStream->readInteger((FX_DWORD*)&pRI->y) != 0)
+ || (m_pStream->read1Byte(&pRI->flags) != 0)) {
+ return JBIG2_ERROR_TOO_SHORT;
+ }
+ return JBIG2_SUCCESS;
+}
+JBig2HuffmanCode *CJBig2_Context::decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream,
+ FX_DWORD SBNUMSYMS)
+{
+ JBig2HuffmanCode *SBSYMCODES;
+ FX_INT32 runcodes[35], runcodes_len[35], runcode;
+ FX_INT32 i, j, nTemp, nVal, nBits;
+ FX_INT32 run;
+ SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(sizeof(JBig2HuffmanCode), SBNUMSYMS);
+ for (i = 0; i < 35; i ++) {
+ if(pStream->readNBits(4, &runcodes_len[i]) != 0) {
+ goto failed;
+ }
+ }
+ huffman_assign_code(runcodes, runcodes_len, 35);
+ i = 0;
+ while(i < (int)SBNUMSYMS) {
+ nVal = 0;
+ nBits = 0;
+ for(;;) {
+ if(pStream->read1Bit(&nTemp) != 0) {
+ goto failed;
+ }
+ nVal = (nVal << 1) | nTemp;
+ nBits ++;
+ for(j = 0; j < 35; j++) {
+ if((nBits == runcodes_len[j]) && (nVal == runcodes[j])) {
+ break;
+ }
+ }
+ if(j < 35) {
+ break;
+ }
+ }
+ runcode = j;
+ if(runcode < 32) {
+ SBSYMCODES[i].codelen = runcode;
+ run = 0;
+ } else if(runcode == 32) {
+ if(pStream->readNBits(2, &nTemp) != 0) {
+ goto failed;
+ }
+ run = nTemp + 3;
+ } else if(runcode == 33) {
+ if(pStream->readNBits(3, &nTemp) != 0) {
+ goto failed;
+ }
+ run = nTemp + 3;
+ } else if(runcode == 34) {
+ if(pStream->readNBits(7, &nTemp) != 0) {
+ goto failed;
+ }
+ run = nTemp + 11;
+ }
+ if(run > 0) {
+ if (i + run > (int)SBNUMSYMS) {
+ goto failed;
+ }
+ for(j = 0; j < run; j++) {
+ if(runcode == 32 && i > 0) {
+ SBSYMCODES[i + j].codelen = SBSYMCODES[i - 1].codelen;
+ } else {
+ SBSYMCODES[i + j].codelen = 0;
+ }
+ }
+ i += run;
+ } else {
+ i ++;
+ }
+ }
+ huffman_assign_code(SBSYMCODES, SBNUMSYMS);
+ return SBSYMCODES;
+failed:
+ m_pModule->JBig2_Free(SBSYMCODES);
+ return NULL;
+}
+void CJBig2_Context::huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP)
+{
+ int CURLEN, LENMAX, CURCODE, CURTEMP, i;
+ int *LENCOUNT;
+ int *FIRSTCODE;
+ LENMAX = 0;
+ for(i = 0; i < NTEMP; i++) {
+ if(PREFLEN[i] > LENMAX) {
+ LENMAX = PREFLEN[i];
+ }
+ }
+ LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
+ FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ for(i = 0; i < NTEMP; i++) {
+ LENCOUNT[PREFLEN[i]] ++;
+ }
+ CURLEN = 1;
+ FIRSTCODE[0] = 0;
+ LENCOUNT[0] = 0;
+ while(CURLEN <= LENMAX) {
+ FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = FIRSTCODE[CURLEN];
+ CURTEMP = 0;
+ while(CURTEMP < NTEMP) {
+ if(PREFLEN[CURTEMP] == CURLEN) {
+ CODES[CURTEMP] = CURCODE;
+ CURCODE = CURCODE + 1;
+ }
+ CURTEMP = CURTEMP + 1;
+ }
+ CURLEN = CURLEN + 1;
+ }
+ m_pModule->JBig2_Free(LENCOUNT);
+ m_pModule->JBig2_Free(FIRSTCODE);
+}
+void CJBig2_Context::huffman_assign_code(JBig2HuffmanCode *SBSYMCODES, int NTEMP)
+{
+ int CURLEN, LENMAX, CURCODE, CURTEMP, i;
+ int *LENCOUNT;
+ int *FIRSTCODE;
+ LENMAX = 0;
+ for(i = 0; i < NTEMP; i++) {
+ if(SBSYMCODES[i].codelen > LENMAX) {
+ LENMAX = SBSYMCODES[i].codelen;
+ }
+ }
+ LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
+ FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ for(i = 0; i < NTEMP; i++) {
+ LENCOUNT[SBSYMCODES[i].codelen] ++;
+ }
+ CURLEN = 1;
+ FIRSTCODE[0] = 0;
+ LENCOUNT[0] = 0;
+ while(CURLEN <= LENMAX) {
+ FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = FIRSTCODE[CURLEN];
+ CURTEMP = 0;
+ while(CURTEMP < NTEMP) {
+ if(SBSYMCODES[CURTEMP].codelen == CURLEN) {
+ SBSYMCODES[CURTEMP].code = CURCODE;
+ CURCODE = CURCODE + 1;
+ }
+ CURTEMP = CURTEMP + 1;
+ }
+ CURLEN = CURLEN + 1;
+ }
+ m_pModule->JBig2_Free(LENCOUNT);
+ m_pModule->JBig2_Free(FIRSTCODE);
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Context.h b/core/src/fxcodec/jbig2/JBig2_Context.h
index 369dba3512..90bd7ddbbe 100644
--- a/core/src/fxcodec/jbig2/JBig2_Context.h
+++ b/core/src/fxcodec/jbig2/JBig2_Context.h
@@ -1,135 +1,135 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_CONTEXT_H_
-#define _JBIG2_CONTEXT_H_
-#include "JBig2_Module.h"
-#include "JBig2_List.h"
-#include "JBig2_Segment.h"
-#include "JBig2_Page.h"
-#include "JBig2_GeneralDecoder.h"
-#include "../../../include/fxcodec/fx_codec_def.h"
-#include "../../../include/fxcrt/fx_basic.h"
-typedef enum {
- JBIG2_OUT_OF_PAGE = 0,
- JBIG2_IN_PAGE,
-} JBig2State;
-#define JBIG2_SUCCESS 0
-#define JBIG2_FAILED -1
-#define JBIG2_ERROR_TOO_SHORT -2
-#define JBIG2_ERROR_FETAL -3
-#define JBIG2_END_OF_PAGE 2
-#define JBIG2_END_OF_FILE 3
-#define JBIG2_ERROR_FILE_FORMAT -4
-#define JBIG2_ERROR_STREAM_TYPE -5
-#define JBIG2_ERROR_LIMIT -6
-#define JBIG2_FILE_STREAM 0
-#define JBIG2_SQUENTIAL_STREAM 1
-#define JBIG2_RANDOM_STREAM 2
-#define JBIG2_EMBED_STREAM 3
-#define JBIG2_MIN_SEGMENT_SIZE 11
-class CJBig2_Context : public CJBig2_Object
-{
-public:
-
- static CJBig2_Context *CreateContext(CJBig2_Module *pModule, FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
- FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause = NULL);
-
- static void DestroyContext(CJBig2_Context *pContext);
-
- FX_INT32 getFirstPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause);
-
- FX_INT32 getNextPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause);
-
- FX_INT32 getFirstPage(CJBig2_Image **image, IFX_Pause* pPause);
-
- FX_INT32 getNextPage(CJBig2_Image **image, IFX_Pause* pPause);
- FX_INT32 Continue(IFX_Pause* pPause);
- FXCODEC_STATUS GetProcessiveStatus()
- {
- return m_ProcessiveStatus;
- };
-private:
-
- CJBig2_Context(FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
- FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause);
-
- ~CJBig2_Context();
-
- FX_INT32 decodeFile(IFX_Pause* pPause);
-
- FX_INT32 decode_SquentialOrgnazation(IFX_Pause* pPause);
-
- FX_INT32 decode_EmbedOrgnazation(IFX_Pause* pPause);
-
- FX_INT32 decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
-
- FX_INT32 decode_RandomOrgnazation(IFX_Pause* pPause);
-
- CJBig2_Segment *findSegmentByNumber(FX_DWORD dwNumber);
-
- CJBig2_Segment *findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment, FX_BYTE cType, FX_INT32 nIndex);
-
- FX_INT32 parseSegmentHeader(CJBig2_Segment *pSegment);
-
- FX_INT32 parseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
- FX_INT32 ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
-
- FX_INT32 parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
-
- FX_INT32 parseTextRegion(CJBig2_Segment *pSegment);
-
- FX_INT32 parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
-
- FX_INT32 parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
-
- FX_INT32 parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
-
- FX_INT32 parseGenericRefinementRegion(CJBig2_Segment *pSegment);
-
- FX_INT32 parseTable(CJBig2_Segment *pSegment);
-
- FX_INT32 parseRegionInfo(JBig2RegionInfo *pRI);
-
-
-
- JBig2HuffmanCode *decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream, FX_DWORD SBNUMSYMS);
-
- void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
-
- void huffman_assign_code(JBig2HuffmanCode *SBSYMCODES, int NTEMP);
-
-private:
-
- CJBig2_Context *m_pGlobalContext;
-
- FX_INT32 m_nStreamType;
-
- CJBig2_BitStream *m_pStream;
-
- FX_INT32 m_nState;
-
- CJBig2_List<CJBig2_Segment> *m_pSegmentList;
-
- CJBig2_List<JBig2PageInfo> *m_pPageInfoList;
-
- CJBig2_Image *m_pPage;
-
- FX_BOOL m_bBufSpecified;
-
- FX_INT32 m_nSegmentDecoded;
- IFX_Pause* m_pPause;
- FX_INT32 m_PauseStep;
- FXCODEC_STATUS m_ProcessiveStatus;
- FX_BOOL m_bFirstPage;
- CJBig2_ArithDecoder *m_pArithDecoder;
- CJBig2_GRDProc *m_pGRD;
- JBig2ArithCtx *m_gbContext;
- CJBig2_Segment *m_pSegment;
- FX_DWORD m_dwOffset;
- JBig2RegionInfo m_ri;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_CONTEXT_H_
+#define _JBIG2_CONTEXT_H_
+#include "JBig2_Module.h"
+#include "JBig2_List.h"
+#include "JBig2_Segment.h"
+#include "JBig2_Page.h"
+#include "JBig2_GeneralDecoder.h"
+#include "../../../include/fxcodec/fx_codec_def.h"
+#include "../../../include/fxcrt/fx_basic.h"
+typedef enum {
+ JBIG2_OUT_OF_PAGE = 0,
+ JBIG2_IN_PAGE,
+} JBig2State;
+#define JBIG2_SUCCESS 0
+#define JBIG2_FAILED -1
+#define JBIG2_ERROR_TOO_SHORT -2
+#define JBIG2_ERROR_FETAL -3
+#define JBIG2_END_OF_PAGE 2
+#define JBIG2_END_OF_FILE 3
+#define JBIG2_ERROR_FILE_FORMAT -4
+#define JBIG2_ERROR_STREAM_TYPE -5
+#define JBIG2_ERROR_LIMIT -6
+#define JBIG2_FILE_STREAM 0
+#define JBIG2_SQUENTIAL_STREAM 1
+#define JBIG2_RANDOM_STREAM 2
+#define JBIG2_EMBED_STREAM 3
+#define JBIG2_MIN_SEGMENT_SIZE 11
+class CJBig2_Context : public CJBig2_Object
+{
+public:
+
+ static CJBig2_Context *CreateContext(CJBig2_Module *pModule, FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
+ FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause = NULL);
+
+ static void DestroyContext(CJBig2_Context *pContext);
+
+ FX_INT32 getFirstPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause);
+
+ FX_INT32 getNextPage(FX_BYTE *pBuf, FX_INT32 width, FX_INT32 height, FX_INT32 stride, IFX_Pause* pPause);
+
+ FX_INT32 getFirstPage(CJBig2_Image **image, IFX_Pause* pPause);
+
+ FX_INT32 getNextPage(CJBig2_Image **image, IFX_Pause* pPause);
+ FX_INT32 Continue(IFX_Pause* pPause);
+ FXCODEC_STATUS GetProcessiveStatus()
+ {
+ return m_ProcessiveStatus;
+ };
+private:
+
+ CJBig2_Context(FX_BYTE *pGlobalData, FX_DWORD dwGlobalLength,
+ FX_BYTE *pData, FX_DWORD dwLength, FX_INT32 nStreamType, IFX_Pause* pPause);
+
+ ~CJBig2_Context();
+
+ FX_INT32 decodeFile(IFX_Pause* pPause);
+
+ FX_INT32 decode_SquentialOrgnazation(IFX_Pause* pPause);
+
+ FX_INT32 decode_EmbedOrgnazation(IFX_Pause* pPause);
+
+ FX_INT32 decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
+
+ FX_INT32 decode_RandomOrgnazation(IFX_Pause* pPause);
+
+ CJBig2_Segment *findSegmentByNumber(FX_DWORD dwNumber);
+
+ CJBig2_Segment *findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment, FX_BYTE cType, FX_INT32 nIndex);
+
+ FX_INT32 parseSegmentHeader(CJBig2_Segment *pSegment);
+
+ FX_INT32 parseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+ FX_INT32 ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseTextRegion(CJBig2_Segment *pSegment);
+
+ FX_INT32 parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
+
+ FX_INT32 parseGenericRefinementRegion(CJBig2_Segment *pSegment);
+
+ FX_INT32 parseTable(CJBig2_Segment *pSegment);
+
+ FX_INT32 parseRegionInfo(JBig2RegionInfo *pRI);
+
+
+
+ JBig2HuffmanCode *decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream, FX_DWORD SBNUMSYMS);
+
+ void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
+
+ void huffman_assign_code(JBig2HuffmanCode *SBSYMCODES, int NTEMP);
+
+private:
+
+ CJBig2_Context *m_pGlobalContext;
+
+ FX_INT32 m_nStreamType;
+
+ CJBig2_BitStream *m_pStream;
+
+ FX_INT32 m_nState;
+
+ CJBig2_List<CJBig2_Segment> *m_pSegmentList;
+
+ CJBig2_List<JBig2PageInfo> *m_pPageInfoList;
+
+ CJBig2_Image *m_pPage;
+
+ FX_BOOL m_bBufSpecified;
+
+ FX_INT32 m_nSegmentDecoded;
+ IFX_Pause* m_pPause;
+ FX_INT32 m_PauseStep;
+ FXCODEC_STATUS m_ProcessiveStatus;
+ FX_BOOL m_bFirstPage;
+ CJBig2_ArithDecoder *m_pArithDecoder;
+ CJBig2_GRDProc *m_pGRD;
+ JBig2ArithCtx *m_gbContext;
+ CJBig2_Segment *m_pSegment;
+ FX_DWORD m_dwOffset;
+ JBig2RegionInfo m_ri;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Define.h b/core/src/fxcodec/jbig2/JBig2_Define.h
index 1019b6af5d..ccf15d07ba 100644
--- a/core/src/fxcodec/jbig2/JBig2_Define.h
+++ b/core/src/fxcodec/jbig2/JBig2_Define.h
@@ -1,34 +1,34 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_DEFINE_H_
-#define _JBIG2_DEFINE_H_
-#include "../../../include/fxcrt/fx_system.h"
-#define JBIG2_memset FXSYS_memset8
-#define JBIG2_memcmp FXSYS_memcmp32
-#define JBIG2_memcpy FXSYS_memcpy32
-#include "JBig2_Object.h"
-#define JBIG2_OOB 1
-typedef struct {
- FX_INT32 width,
- height;
- FX_INT32 x,
- y;
- FX_BYTE flags;
-} JBig2RegionInfo;
-typedef struct {
- FX_INT32 codelen;
- FX_INT32 code;
-} JBig2HuffmanCode;
-extern "C" {
- void _FaxG4Decode(void *pModule, FX_LPCBYTE src_buf, FX_DWORD src_size, int* pbitpos, FX_LPBYTE dest_buf, int width, int height, int pitch = 0);
-};
-#define JBIG2_MAX_REFERRED_SEGMENT_COUNT 64
-#define JBIG2_MAX_EXPORT_SYSMBOLS 65535
-#define JBIG2_MAX_NEW_SYSMBOLS 65535
-#define JBIG2_MAX_PATTERN_INDEX 65535
-#define JBIG2_MAX_IMAGE_SIZE 65535
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_DEFINE_H_
+#define _JBIG2_DEFINE_H_
+#include "../../../include/fxcrt/fx_system.h"
+#define JBIG2_memset FXSYS_memset8
+#define JBIG2_memcmp FXSYS_memcmp32
+#define JBIG2_memcpy FXSYS_memcpy32
+#include "JBig2_Object.h"
+#define JBIG2_OOB 1
+typedef struct {
+ FX_INT32 width,
+ height;
+ FX_INT32 x,
+ y;
+ FX_BYTE flags;
+} JBig2RegionInfo;
+typedef struct {
+ FX_INT32 codelen;
+ FX_INT32 code;
+} JBig2HuffmanCode;
+extern "C" {
+ void _FaxG4Decode(void *pModule, FX_LPCBYTE src_buf, FX_DWORD src_size, int* pbitpos, FX_LPBYTE dest_buf, int width, int height, int pitch = 0);
+};
+#define JBIG2_MAX_REFERRED_SEGMENT_COUNT 64
+#define JBIG2_MAX_EXPORT_SYSMBOLS 65535
+#define JBIG2_MAX_NEW_SYSMBOLS 65535
+#define JBIG2_MAX_PATTERN_INDEX 65535
+#define JBIG2_MAX_IMAGE_SIZE 65535
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp
index 9aa714a228..5847b20a93 100644
--- a/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.cpp
@@ -1,4290 +1,4290 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_GeneralDecoder.h"
-#include "JBig2_ArithDecoder.h"
-#include "JBig2_ArithIntDecoder.h"
-#include "JBig2_HuffmanDecoder.h"
-#include "JBig2_HuffmanTable.h"
-#include "JBig2_PatternDict.h"
-CJBig2_Image *CJBig2_GRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- if (GBW == 0 || GBH == 0) {
- CJBig2_Image* pImage;
- JBIG2_ALLOC(pImage, CJBig2_Image(GBW, GBH));
- return pImage;
- }
- if(GBTEMPLATE == 0) {
- if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)
- && (GBAT[2] == (signed char) - 3) && (GBAT[3] == (signed char) - 1)
- && (GBAT[4] == 2) && (GBAT[5] == (signed char) - 2)
- && (GBAT[6] == (signed char) - 2) && (GBAT[7] == (signed char) - 2)) {
- return decode_Arith_Template0_opt3(pArithDecoder, gbContext);
- } else {
- return decode_Arith_Template0_unopt(pArithDecoder, gbContext);
- }
- } else if(GBTEMPLATE == 1) {
- if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)) {
- return decode_Arith_Template1_opt3(pArithDecoder, gbContext);
- } else {
- return decode_Arith_Template1_unopt(pArithDecoder, gbContext);
- }
- } else if(GBTEMPLATE == 2) {
- if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
- return decode_Arith_Template2_opt3(pArithDecoder, gbContext);
- } else {
- return decode_Arith_Template2_unopt(pArithDecoder, gbContext);
- }
- } else {
- if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
- return decode_Arith_Template3_opt3(pArithDecoder, gbContext);
- } else {
- return decode_Arith_Template3_unopt(pArithDecoder, gbContext);
- }
- }
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2, line3;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = GBREG->getPixel(2, h - 2);
- line1 |= GBREG->getPixel(1, h - 2) << 1;
- line1 |= GBREG->getPixel(0, h - 2) << 2;
- line2 = GBREG->getPixel(3, h - 1);
- line2 |= GBREG->getPixel(2, h - 1) << 1;
- line2 |= GBREG->getPixel(1, h - 1) << 2;
- line2 |= GBREG->getPixel(0, h - 1) << 3;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= line2 << 4;
- CONTEXT |= line1 << 11;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x1f;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 4, h - 1)) & 0x7f;
- line3 = ((line3 << 1) | bVal) & 0x0f;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2;
- FX_BYTE *pLine, cVal;
- FX_INTPTR nStride, nStride2;
- FX_INT32 nBits, k;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- if (GBREG->m_pData == NULL) {
- delete GBREG;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- pLine = GBREG->m_pData;
- nStride = GBREG->m_nStride;
- nStride2 = nStride << 1;
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = (h > 1) ? pLine[-nStride2] << 6 : 0;
- line2 = (h > 0) ? pLine[-nStride] : 0;
- CONTEXT = (line1 & 0xf800) | (line2 & 0x07f0);
- for(FX_DWORD w = 0; w < GBW; w += 8) {
- if(w + 8 < GBW) {
- nBits = 8;
- if(h > 1) {
- line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 6);
- }
- if(h > 0) {
- line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
- }
- } else {
- nBits = GBW - w;
- if(h > 1) {
- line1 <<= 8;
- }
- if(h > 0) {
- line2 <<= 8;
- }
- }
- cVal = 0;
- for(k = 0; k < nBits; k++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0800)
- | ((line2 >> (7 - k)) & 0x0010);
- }
- pLine[w >> 3] = cVal;
- }
- }
- pLine += nStride;
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2;
- FX_BYTE *pLine, *pLine1, *pLine2, cVal;
- FX_INT32 nStride, nStride2, k;
- FX_INT32 nLineBytes, nBitsLeft, cc;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- if (GBREG->m_pData == NULL) {
- delete GBREG;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- pLine = GBREG->m_pData;
- nStride = GBREG->m_nStride;
- nStride2 = nStride << 1;
- nLineBytes = ((GBW + 7) >> 3) - 1;
- nBitsLeft = GBW - (nLineBytes << 3);
- FX_DWORD height = GBH & 0x7fffffff;
- for(FX_DWORD h = 0; h < height; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- if(h > 1) {
- pLine1 = pLine - nStride2;
- pLine2 = pLine - nStride;
- line1 = (*pLine1++) << 6;
- line2 = *pLine2++;
- CONTEXT = ((line1 & 0xf800) | (line2 & 0x07f0));
- for(cc = 0; cc < nLineBytes; cc++) {
- line1 = (line1 << 8) | ((*pLine1++) << 6);
- line2 = (line2 << 8) | (*pLine2++);
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
- | ((line1 >> k) & 0x0800)
- | ((line2 >> k) & 0x0010));
- }
- pLine[cc] = cVal;
- }
- line1 <<= 8;
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0800)
- | ((line2 >> (7 - k)) & 0x0010));
- }
- pLine[nLineBytes] = cVal;
- } else {
- pLine2 = pLine - nStride;
- line2 = (h & 1) ? (*pLine2++) : 0;
- CONTEXT = (line2 & 0x07f0);
- for(cc = 0; cc < nLineBytes; cc++) {
- if(h & 1) {
- line2 = (line2 << 8) | (*pLine2++);
- }
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
- | ((line2 >> k) & 0x0010));
- }
- pLine[cc] = cVal;
- }
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
- | (((line2 >> (7 - k))) & 0x0010));
- }
- pLine[nLineBytes] = cVal;
- }
- }
- pLine += nStride;
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2, line3;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = GBREG->getPixel(1, h - 2);
- line1 |= GBREG->getPixel(0, h - 2) << 1;
- line2 = GBREG->getPixel(2, h - 1);
- line2 |= GBREG->getPixel(1, h - 1) << 1;
- line2 |= GBREG->getPixel(0, h - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
- CONTEXT |= line2 << 5;
- CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
- CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
- CONTEXT |= line1 << 12;
- CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x0f;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2, line3;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = GBREG->getPixel(2, h - 2);
- line1 |= GBREG->getPixel(1, h - 2) << 1;
- line1 |= GBREG->getPixel(0, h - 2) << 2;
- line2 = GBREG->getPixel(3, h - 1);
- line2 |= GBREG->getPixel(2, h - 1) << 1;
- line2 |= GBREG->getPixel(1, h - 1) << 2;
- line2 |= GBREG->getPixel(0, h - 1) << 3;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= line2 << 3;
- CONTEXT |= line1 << 9;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 4, h - 1)) & 0x3f;
- line3 = ((line3 << 1) | bVal) & 0x07;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2;
- FX_BYTE *pLine, cVal;
- FX_INTPTR nStride, nStride2;
- FX_INT32 nBits, k;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- if (GBREG->m_pData == NULL) {
- delete GBREG;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- pLine = GBREG->m_pData;
- nStride = GBREG->m_nStride;
- nStride2 = nStride << 1;
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = (h > 1) ? pLine[-nStride2] << 4 : 0;
- line2 = (h > 0) ? pLine[-nStride] : 0;
- CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
- for(FX_DWORD w = 0; w < GBW; w += 8) {
- if(w + 8 < GBW) {
- nBits = 8;
- if(h > 1) {
- line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 4);
- }
- if(h > 0) {
- line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
- }
- } else {
- nBits = GBW - w;
- if(h > 1) {
- line1 <<= 8;
- }
- if(h > 0) {
- line2 <<= 8;
- }
- }
- cVal = 0;
- for(k = 0; k < nBits; k++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0200)
- | ((line2 >> (8 - k)) & 0x0008);
- }
- pLine[w >> 3] = cVal;
- }
- }
- pLine += nStride;
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2;
- FX_BYTE *pLine, *pLine1, *pLine2, cVal;
- FX_INT32 nStride, nStride2, k;
- FX_INT32 nLineBytes, nBitsLeft, cc;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- if (GBREG->m_pData == NULL) {
- delete GBREG;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- pLine = GBREG->m_pData;
- nStride = GBREG->m_nStride;
- nStride2 = nStride << 1;
- nLineBytes = ((GBW + 7) >> 3) - 1;
- nBitsLeft = GBW - (nLineBytes << 3);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- if(h > 1) {
- pLine1 = pLine - nStride2;
- pLine2 = pLine - nStride;
- line1 = (*pLine1++) << 4;
- line2 = *pLine2++;
- CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
- for(cc = 0; cc < nLineBytes; cc++) {
- line1 = (line1 << 8) | ((*pLine1++) << 4);
- line2 = (line2 << 8) | (*pLine2++);
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line1 >> k) & 0x0200)
- | ((line2 >> (k + 1)) & 0x0008);
- }
- pLine[cc] = cVal;
- }
- line1 <<= 8;
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0200)
- | ((line2 >> (8 - k)) & 0x0008);
- }
- pLine[nLineBytes] = cVal;
- } else {
- pLine2 = pLine - nStride;
- line2 = (h & 1) ? (*pLine2++) : 0;
- CONTEXT = (line2 >> 1) & 0x01f8;
- for(cc = 0; cc < nLineBytes; cc++) {
- if(h & 1) {
- line2 = (line2 << 8) | (*pLine2++);
- }
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line2 >> (k + 1)) & 0x0008);
- }
- pLine[cc] = cVal;
- }
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line2 >> (8 - k)) & 0x0008);
- }
- pLine[nLineBytes] = cVal;
- }
- }
- pLine += nStride;
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2, line3;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = GBREG->getPixel(2, h - 2);
- line1 |= GBREG->getPixel(1, h - 2) << 1;
- line1 |= GBREG->getPixel(0, h - 2) << 2;
- line2 = GBREG->getPixel(2, h - 1);
- line2 |= GBREG->getPixel(1, h - 1) << 1;
- line2 |= GBREG->getPixel(0, h - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
- CONTEXT |= line2 << 4;
- CONTEXT |= line1 << 9;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x07;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2, line3;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = GBREG->getPixel(1, h - 2);
- line1 |= GBREG->getPixel(0, h - 2) << 1;
- line2 = GBREG->getPixel(2, h - 1);
- line2 |= GBREG->getPixel(1, h - 1) << 1;
- line2 |= GBREG->getPixel(0, h - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= line2 << 2;
- CONTEXT |= line1 << 7;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x03;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2;
- FX_BYTE *pLine, cVal;
- FX_INTPTR nStride, nStride2;
- FX_INT32 nBits, k;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- if (GBREG->m_pData == NULL) {
- delete GBREG;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- pLine = GBREG->m_pData;
- nStride = GBREG->m_nStride;
- nStride2 = nStride << 1;
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = (h > 1) ? pLine[-nStride2] << 1 : 0;
- line2 = (h > 0) ? pLine[-nStride] : 0;
- CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
- for(FX_DWORD w = 0; w < GBW; w += 8) {
- if(w + 8 < GBW) {
- nBits = 8;
- if(h > 1) {
- line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 1);
- }
- if(h > 0) {
- line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
- }
- } else {
- nBits = GBW - w;
- if(h > 1) {
- line1 <<= 8;
- }
- if(h > 0) {
- line2 <<= 8;
- }
- }
- cVal = 0;
- for(k = 0; k < nBits; k++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0080)
- | ((line2 >> (10 - k)) & 0x0004);
- }
- pLine[w >> 3] = cVal;
- }
- }
- pLine += nStride;
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2;
- FX_BYTE *pLine, *pLine1, *pLine2, cVal;
- FX_INT32 nStride, nStride2, k;
- FX_INT32 nLineBytes, nBitsLeft, cc;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- if (GBREG->m_pData == NULL) {
- delete GBREG;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- pLine = GBREG->m_pData;
- nStride = GBREG->m_nStride;
- nStride2 = nStride << 1;
- nLineBytes = ((GBW + 7) >> 3) - 1;
- nBitsLeft = GBW - (nLineBytes << 3);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- if(h > 1) {
- pLine1 = pLine - nStride2;
- pLine2 = pLine - nStride;
- line1 = (*pLine1++) << 1;
- line2 = *pLine2++;
- CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
- for(cc = 0; cc < nLineBytes; cc++) {
- line1 = (line1 << 8) | ((*pLine1++) << 1);
- line2 = (line2 << 8) | (*pLine2++);
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | ((line1 >> k) & 0x0080)
- | ((line2 >> (k + 3)) & 0x0004);
- }
- pLine[cc] = cVal;
- }
- line1 <<= 8;
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0080)
- | ((line2 >> (10 - k)) & 0x0004);
- }
- pLine[nLineBytes] = cVal;
- } else {
- pLine2 = pLine - nStride;
- line2 = (h & 1) ? (*pLine2++) : 0;
- CONTEXT = (line2 >> 3) & 0x007c;
- for(cc = 0; cc < nLineBytes; cc++) {
- if(h & 1) {
- line2 = (line2 << 8) | (*pLine2++);
- }
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | ((line2 >> (k + 3)) & 0x0004);
- }
- pLine[cc] = cVal;
- }
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | (((line2 >> (10 - k))) & 0x0004);
- }
- pLine[nLineBytes] = cVal;
- }
- }
- pLine += nStride;
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2, line3;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = GBREG->getPixel(1, h - 2);
- line1 |= GBREG->getPixel(0, h - 2) << 1;
- line2 = GBREG->getPixel(1, h - 1);
- line2 |= GBREG->getPixel(0, h - 1) << 1;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
- CONTEXT |= line2 << 3;
- CONTEXT |= line1 << 7;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x0f;
- line3 = ((line3 << 1) | bVal) & 0x03;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = GBREG->getPixel(2, h - 1);
- line1 |= GBREG->getPixel(1, h - 1) << 1;
- line1 |= GBREG->getPixel(0, h - 1) << 2;
- line2 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line2;
- CONTEXT |= line1 << 4;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x3f;
- line2 = ((line2 << 1) | bVal) & 0x0f;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1;
- FX_BYTE *pLine, cVal;
- FX_INTPTR nStride, nStride2;
- FX_INT32 nBits, k;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- if (GBREG->m_pData == NULL) {
- delete GBREG;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- pLine = GBREG->m_pData;
- nStride = GBREG->m_nStride;
- nStride2 = nStride << 1;
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = (h > 0) ? pLine[-nStride] : 0;
- CONTEXT = (line1 >> 1) & 0x03f0;
- for(FX_DWORD w = 0; w < GBW; w += 8) {
- if(w + 8 < GBW) {
- nBits = 8;
- if(h > 0) {
- line1 = (line1 << 8) | (pLine[-nStride + (w >> 3) + 1]);
- }
- } else {
- nBits = GBW - w;
- if(h > 0) {
- line1 <<= 8;
- }
- }
- cVal = 0;
- for(k = 0; k < nBits; k++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
- | ((line1 >> (8 - k)) & 0x0010);
- }
- pLine[w >> 3] = cVal;
- }
- }
- pLine += nStride;
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1;
- FX_BYTE *pLine, *pLine1, cVal;
- FX_INT32 nStride, k;
- FX_INT32 nLineBytes, nBitsLeft, cc;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- if (GBREG->m_pData == NULL) {
- delete GBREG;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- pLine = GBREG->m_pData;
- nStride = GBREG->m_nStride;
- nLineBytes = ((GBW + 7) >> 3) - 1;
- nBitsLeft = GBW - (nLineBytes << 3);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- if(h > 0) {
- pLine1 = pLine - nStride;
- line1 = *pLine1++;
- CONTEXT = (line1 >> 1) & 0x03f0;
- for(cc = 0; cc < nLineBytes; cc++) {
- line1 = (line1 << 8) | (*pLine1++);
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
- | ((line1 >> (k + 1)) & 0x0010);
- }
- pLine[cc] = cVal;
- }
- line1 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
- | ((line1 >> (8 - k)) & 0x0010);
- }
- pLine[nLineBytes] = cVal;
- } else {
- CONTEXT = 0;
- for(cc = 0; cc < nLineBytes; cc++) {
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
- }
- pLine[cc] = cVal;
- }
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
- }
- pLine[nLineBytes] = cVal;
- }
- }
- pLine += nStride;
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- line1 = GBREG->getPixel(1, h - 1);
- line1 |= GBREG->getPixel(0, h - 1) << 1;
- line2 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line2;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
- CONTEXT |= line1 << 5;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x1f;
- line2 = ((line2 << 1) | bVal) & 0x0f;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_V2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG;
- FX_DWORD line1, line2, line3;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- switch(GBTEMPLATE) {
- case 0:
- CONTEXT = 0x9b25;
- break;
- case 1:
- CONTEXT = 0x0795;
- break;
- case 2:
- CONTEXT = 0x00e5;
- break;
- case 3:
- CONTEXT = 0x0195;
- break;
- }
- SLTP = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(h, h - 1);
- } else {
- switch(GBTEMPLATE) {
- case 0: {
- line1 = GBREG->getPixel(1, h - 2);
- line1 |= GBREG->getPixel(0, h - 2) << 1;
- line2 = GBREG->getPixel(2, h - 1);
- line2 |= GBREG->getPixel(1, h - 1) << 1;
- line2 |= GBREG->getPixel(0, h - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
- CONTEXT |= line2 << 5;
- CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
- CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
- CONTEXT |= line1 << 12;
- CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x0f;
- }
- }
- break;
- case 1: {
- line1 = GBREG->getPixel(2, h - 2);
- line1 |= GBREG->getPixel(1, h - 2) << 1;
- line1 |= GBREG->getPixel(0, h - 2) << 2;
- line2 = GBREG->getPixel(2, h - 1);
- line2 |= GBREG->getPixel(1, h - 1) << 1;
- line2 |= GBREG->getPixel(0, h - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
- CONTEXT |= line2 << 4;
- CONTEXT |= line1 << 9;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x07;
- }
- }
- break;
- case 2: {
- line1 = GBREG->getPixel(1, h - 2);
- line1 |= GBREG->getPixel(0, h - 2) << 1;
- line2 = GBREG->getPixel(1, h - 1);
- line2 |= GBREG->getPixel(0, h - 1) << 1;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
- CONTEXT |= line2 << 3;
- CONTEXT |= line1 << 7;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x0f;
- line3 = ((line3 << 1) | bVal) & 0x03;
- }
- }
- break;
- case 3: {
- line1 = GBREG->getPixel(1, h - 1);
- line1 |= GBREG->getPixel(0, h - 1) << 1;
- line2 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line2;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
- CONTEXT |= line1 << 5;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x1f;
- line2 = ((line2 << 1) | bVal) & 0x0f;
- }
- }
- break;
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_Arith_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT = 0;
- CJBig2_Image *GBREG;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- switch(GBTEMPLATE) {
- case 0:
- CONTEXT = 0x9b25;
- break;
- case 1:
- CONTEXT = 0x0795;
- break;
- case 2:
- CONTEXT = 0x00e5;
- break;
- case 3:
- CONTEXT = 0x0195;
- break;
- }
- SLTP = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- for(FX_DWORD w = 0; w < GBW; w++) {
- GBREG->setPixel(w, h, GBREG->getPixel(w, h - 1));
- }
- } else {
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- GBREG->setPixel(w, h, 0);
- } else {
- CONTEXT = 0;
- switch(GBTEMPLATE) {
- case 0:
- CONTEXT |= GBREG->getPixel(w - 1, h);
- CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
- CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
- CONTEXT |= GBREG->getPixel(w - 4, h) << 3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
- CONTEXT |= GBREG->getPixel(w + 2, h - 1) << 5;
- CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 6;
- CONTEXT |= GBREG->getPixel(w, h - 1) << 7;
- CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 8;
- CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 9;
- CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
- CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
- CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 12;
- CONTEXT |= GBREG->getPixel(w, h - 2) << 13;
- CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 14;
- CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
- break;
- case 1:
- CONTEXT |= GBREG->getPixel(w - 1, h);
- CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
- CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
- CONTEXT |= GBREG->getPixel(w + 2, h - 1) << 4;
- CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 5;
- CONTEXT |= GBREG->getPixel(w, h - 1) << 6;
- CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 7;
- CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 8;
- CONTEXT |= GBREG->getPixel(w + 2, h - 2) << 9;
- CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 10;
- CONTEXT |= GBREG->getPixel(w, h - 2) << 11;
- CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 12;
- break;
- case 2:
- CONTEXT |= GBREG->getPixel(w - 1, h);
- CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
- CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 3;
- CONTEXT |= GBREG->getPixel(w, h - 1) << 4;
- CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 5;
- CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 6;
- CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 7;
- CONTEXT |= GBREG->getPixel(w, h - 2) << 8;
- CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 9;
- break;
- case 3:
- CONTEXT |= GBREG->getPixel(w - 1, h);
- CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
- CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
- CONTEXT |= GBREG->getPixel(w - 4, h) << 3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
- CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 5;
- CONTEXT |= GBREG->getPixel(w, h - 1) << 6;
- CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 7;
- CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 8;
- CONTEXT |= GBREG->getPixel(w - 3, h - 1) << 9;
- break;
- }
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- GBREG->setPixel(w, h, bVal);
- }
- }
- }
- }
- return GBREG;
-}
-CJBig2_Image *CJBig2_GRDProc::decode_MMR(CJBig2_BitStream *pStream)
-{
- int bitpos, i;
- CJBig2_Image *pImage;
- JBIG2_ALLOC(pImage, CJBig2_Image(GBW, GBH));
- if (pImage->m_pData == NULL) {
- delete pImage;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- return NULL;
- }
- bitpos = (int)pStream->getBitPos();
- _FaxG4Decode(m_pModule, pStream->getBuf(), pStream->getLength(), &bitpos, pImage->m_pData, GBW, GBH, pImage->m_nStride);
- pStream->setBitPos(bitpos);
- for(i = 0; (FX_DWORD)i < pImage->m_nStride * GBH; i++) {
- pImage->m_pData[i] = ~pImage->m_pData[i];
- }
- return pImage;
-}
-CJBig2_Image *CJBig2_GRRDProc::decode(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
-{
- if (GRW == 0 || GRH == 0) {
- CJBig2_Image* pImage;
- JBIG2_ALLOC(pImage, CJBig2_Image(GRW, GRH));
- return pImage;
- }
- if(GRTEMPLATE == 0) {
- if((GRAT[0] == (signed char) - 1) && (GRAT[1] == (signed char) - 1)
- && (GRAT[2] == (signed char) - 1) && (GRAT[3] == (signed char) - 1)
- && (GRREFERENCEDX == 0) && (GRW == (FX_DWORD)GRREFERENCE->m_nWidth)) {
- return decode_Template0_opt(pArithDecoder, grContext);
- } else {
- return decode_Template0_unopt(pArithDecoder, grContext);
- }
- } else {
- if((GRREFERENCEDX == 0) && (GRW == (FX_DWORD)GRREFERENCE->m_nWidth)) {
- return decode_Template1_opt(pArithDecoder, grContext);
- } else {
- return decode_Template1_unopt(pArithDecoder, grContext);
- }
- }
-}
-CJBig2_Image *CJBig2_GRRDProc::decode_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GRREG;
- FX_DWORD line1, line2, line3, line4, line5;
- LTP = 0;
- JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
- GRREG->fill(0);
- for(FX_DWORD h = 0; h < GRH; h++) {
- if(TPGRON) {
- SLTP = pArithDecoder->DECODE(&grContext[0x0010]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 0) {
- line1 = GRREG->getPixel(1, h - 1);
- line1 |= GRREG->getPixel(0, h - 1) << 1;
- line2 = 0;
- line3 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
- line3 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1) << 1;
- line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
- line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
- line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
- line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
- line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
- line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
- for(FX_DWORD w = 0; w < GRW; w++) {
- CONTEXT = line5;
- CONTEXT |= line4 << 3;
- CONTEXT |= line3 << 6;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
- CONTEXT |= line2 << 9;
- CONTEXT |= line1 << 10;
- CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- GRREG->setPixel(w, h, bVal);
- line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x03;
- line2 = ((line2 << 1) | bVal) & 0x01;
- line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY - 1)) & 0x03;
- line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
- line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x07;
- }
- } else {
- line1 = GRREG->getPixel(1, h - 1);
- line1 |= GRREG->getPixel(0, h - 1) << 1;
- line2 = 0;
- line3 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
- line3 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1) << 1;
- line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
- line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
- line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
- line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
- line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
- line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
- for(FX_DWORD w = 0; w < GRW; w++) {
- bVal = GRREFERENCE->getPixel(w, h);
- if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w - 1, h))
- && (bVal == GRREFERENCE->getPixel(w + 1, h))
- && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
- && (bVal == GRREFERENCE->getPixel(w, h + 1))
- && (bVal == GRREFERENCE->getPixel(w + 1, h + 1)))) {
- CONTEXT = line5;
- CONTEXT |= line4 << 3;
- CONTEXT |= line3 << 6;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
- CONTEXT |= line2 << 9;
- CONTEXT |= line1 << 10;
- CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- }
- GRREG->setPixel(w, h, bVal);
- line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x03;
- line2 = ((line2 << 1) | bVal) & 0x01;
- line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY - 1)) & 0x03;
- line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
- line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x07;
- }
- }
- }
- return GRREG;
-}
-CJBig2_Image *CJBig2_GRRDProc::decode_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GRREG;
- FX_DWORD line1, line1_r, line2_r, line3_r;
- FX_BYTE *pLine, *pLineR, cVal;
- FX_INTPTR nStride, nStrideR, nOffset;
- FX_INT32 k, nBits;
- FX_INT32 GRWR, GRHR;
- FX_INT32 GRW, GRH;
- GRW = (FX_INT32)CJBig2_GRRDProc::GRW;
- GRH = (FX_INT32)CJBig2_GRRDProc::GRH;
- LTP = 0;
- JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
- if (GRREG->m_pData == NULL) {
- delete GRREG;
- m_pModule->JBig2_Error("Generic refinement region decoding procedure: Create Image Failed with width = %d, height = %d\n", GRW, GRH);
- return NULL;
- }
- pLine = GRREG->m_pData;
- pLineR = GRREFERENCE->m_pData;
- nStride = GRREG->m_nStride;
- nStrideR = GRREFERENCE->m_nStride;
- GRWR = (FX_INT32)GRREFERENCE->m_nWidth;
- GRHR = (FX_INT32)GRREFERENCE->m_nHeight;
- if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
- GRREFERENCEDY = 0;
- }
- nOffset = -GRREFERENCEDY * nStrideR;
- for (FX_INT32 h = 0; h < GRH; h++) {
- if(TPGRON) {
- SLTP = pArithDecoder->DECODE(&grContext[0x0010]);
- LTP = LTP ^ SLTP;
- }
- line1 = (h > 0) ? pLine[-nStride] << 4 : 0;
- FX_INT32 reference_h = h - GRREFERENCEDY;
- FX_BOOL line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
- FX_BOOL line2_r_ok = (reference_h > -1 && reference_h < GRHR);
- FX_BOOL line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
- line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
- line2_r = line2_r_ok ? pLineR[nOffset] : 0;
- line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
- if(LTP == 0) {
- CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0)
- | ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
- for (FX_INT32 w = 0; w < GRW; w += 8) {
- nBits = GRW - w > 8 ? 8 : GRW - w;
- if (h > 0)
- line1 = (line1 << 8) |
- (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
- if (h > GRHR + GRREFERENCEDY + 1) {
- line1_r = 0;
- line2_r = 0;
- line3_r = 0;
- } else {
- if(line1_r_ok)
- line1_r = (line1_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
- if(line2_r_ok)
- line2_r = (line2_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
- if(line3_r_ok)
- line3_r = (line3_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
- else {
- line3_r = 0;
- }
- }
- cVal = 0;
- for (k = 0; k < nBits; k++) {
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
- ((line1 >> (7 - k)) & 0x0400) |
- ((line1_r >> (7 - k)) & 0x0040) |
- ((line2_r >> (10 - k)) & 0x0008) |
- ((line3_r >> (13 - k)) & 0x0001);
- }
- pLine[w >> 3] = cVal;
- }
- } else {
- CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0)
- | ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
- for (FX_INT32 w = 0; w < GRW; w += 8) {
- nBits = GRW - w > 8 ? 8 : GRW - w;
- if (h > 0)
- line1 = (line1 << 8) |
- (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
- if(line1_r_ok)
- line1_r = (line1_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
- if(line2_r_ok)
- line2_r = (line2_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
- if(line3_r_ok)
- line3_r = (line3_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
- else {
- line3_r = 0;
- }
- cVal = 0;
- for (k = 0; k < nBits; k++) {
- bVal = GRREFERENCE->getPixel(w + k, h);
- if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w + k - 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + k, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + k + 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + k - 1, h))
- && (bVal == GRREFERENCE->getPixel(w + k + 1, h))
- && (bVal == GRREFERENCE->getPixel(w + k - 1, h + 1))
- && (bVal == GRREFERENCE->getPixel(w + k, h + 1))
- && (bVal == GRREFERENCE->getPixel(w + k + 1, h + 1)))) {
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- }
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
- ((line1 >> (7 - k)) & 0x0400) |
- ((line1_r >> (7 - k)) & 0x0040) |
- ((line2_r >> (10 - k)) & 0x0008) |
- ((line3_r >> (13 - k)) & 0x0001);
- }
- pLine[w >> 3] = cVal;
- }
- }
- pLine += nStride;
- if (h < GRHR + GRREFERENCEDY) {
- pLineR += nStrideR;
- }
- }
- return GRREG;
-}
-CJBig2_Image *CJBig2_GRRDProc::decode_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GRREG;
- FX_DWORD line1, line2, line3, line4, line5;
- LTP = 0;
- JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
- GRREG->fill(0);
- for(FX_DWORD h = 0; h < GRH; h++) {
- if(TPGRON) {
- SLTP = pArithDecoder->DECODE(&grContext[0x0008]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 0) {
- line1 = GRREG->getPixel(1, h - 1);
- line1 |= GRREG->getPixel(0, h - 1) << 1;
- line1 |= GRREG->getPixel(-1, h - 1) << 2;
- line2 = 0;
- line3 = GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
- line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
- line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
- line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
- line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
- line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
- for(FX_DWORD w = 0; w < GRW; w++) {
- CONTEXT = line5;
- CONTEXT |= line4 << 2;
- CONTEXT |= line3 << 5;
- CONTEXT |= line2 << 6;
- CONTEXT |= line1 << 7;
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- GRREG->setPixel(w, h, bVal);
- line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x07;
- line2 = ((line2 << 1) | bVal) & 0x01;
- line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1)) & 0x01;
- line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
- line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x03;
- }
- } else {
- line1 = GRREG->getPixel(1, h - 1);
- line1 |= GRREG->getPixel(0, h - 1) << 1;
- line1 |= GRREG->getPixel(-1, h - 1) << 2;
- line2 = 0;
- line3 = GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
- line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
- line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
- line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
- line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
- line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
- for(FX_DWORD w = 0; w < GRW; w++) {
- bVal = GRREFERENCE->getPixel(w, h);
- if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w - 1, h))
- && (bVal == GRREFERENCE->getPixel(w + 1, h))
- && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
- && (bVal == GRREFERENCE->getPixel(w, h + 1))
- && (bVal == GRREFERENCE->getPixel(w + 1, h + 1)))) {
- CONTEXT = line5;
- CONTEXT |= line4 << 2;
- CONTEXT |= line3 << 5;
- CONTEXT |= line2 << 6;
- CONTEXT |= line1 << 7;
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- }
- GRREG->setPixel(w, h, bVal);
- line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x07;
- line2 = ((line2 << 1) | bVal) & 0x01;
- line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1)) & 0x01;
- line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
- line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x03;
- }
- }
- }
- return GRREG;
-}
-CJBig2_Image *CJBig2_GRRDProc::decode_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GRREG;
- FX_DWORD line1, line1_r, line2_r, line3_r;
- FX_BYTE *pLine, *pLineR, cVal;
- FX_INTPTR nStride, nStrideR, nOffset;
- FX_INT32 k, nBits;
- FX_INT32 GRWR, GRHR;
- FX_INT32 GRW, GRH;
- GRW = (FX_INT32)CJBig2_GRRDProc::GRW;
- GRH = (FX_INT32)CJBig2_GRRDProc::GRH;
- LTP = 0;
- JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
- if (GRREG->m_pData == NULL) {
- delete GRREG;
- m_pModule->JBig2_Error("Generic refinement region decoding procedure: Create Image Failed with width = %d, height = %d\n", GRW, GRH);
- return NULL;
- }
- pLine = GRREG->m_pData;
- pLineR = GRREFERENCE->m_pData;
- nStride = GRREG->m_nStride;
- nStrideR = GRREFERENCE->m_nStride;
- GRWR = (FX_INT32)GRREFERENCE->m_nWidth;
- GRHR = (FX_INT32)GRREFERENCE->m_nHeight;
- if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
- GRREFERENCEDY = 0;
- }
- nOffset = -GRREFERENCEDY * nStrideR;
- for (FX_INT32 h = 0; h < GRH; h++) {
- if(TPGRON) {
- SLTP = pArithDecoder->DECODE(&grContext[0x0008]);
- LTP = LTP ^ SLTP;
- }
- line1 = (h > 0) ? pLine[-nStride] << 1 : 0;
- FX_INT32 reference_h = h - GRREFERENCEDY;
- FX_BOOL line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
- FX_BOOL line2_r_ok = (reference_h > -1 && reference_h < GRHR);
- FX_BOOL line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
- line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
- line2_r = line2_r_ok ? pLineR[nOffset] : 0;
- line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
- if(LTP == 0) {
- CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020)
- | ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
- for (FX_INT32 w = 0; w < GRW; w += 8) {
- nBits = GRW - w > 8 ? 8 : GRW - w;
- if (h > 0)
- line1 = (line1 << 8) |
- (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
- if(line1_r_ok)
- line1_r = (line1_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
- if(line2_r_ok)
- line2_r = (line2_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
- if(line3_r_ok)
- line3_r = (line3_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
- else {
- line3_r = 0;
- }
- cVal = 0;
- for (k = 0; k < nBits; k++) {
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
- ((line1 >> (7 - k)) & 0x0080) |
- ((line1_r >> (9 - k)) & 0x0020) |
- ((line2_r >> (11 - k)) & 0x0004) |
- ((line3_r >> (13 - k)) & 0x0001);
- }
- pLine[w >> 3] = cVal;
- }
- } else {
- CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020)
- | ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
- for (FX_INT32 w = 0; w < GRW; w += 8) {
- nBits = GRW - w > 8 ? 8 : GRW - w;
- if (h > 0)
- line1 = (line1 << 8) |
- (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
- if(line1_r_ok)
- line1_r = (line1_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
- if(line2_r_ok)
- line2_r = (line2_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
- if(line3_r_ok)
- line3_r = (line3_r << 8) |
- (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
- else {
- line3_r = 0;
- }
- cVal = 0;
- for (k = 0; k < nBits; k++) {
- bVal = GRREFERENCE->getPixel(w + k, h);
- if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w + k - 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + k, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + k + 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + k - 1, h))
- && (bVal == GRREFERENCE->getPixel(w + k + 1, h))
- && (bVal == GRREFERENCE->getPixel(w + k - 1, h + 1))
- && (bVal == GRREFERENCE->getPixel(w + k, h + 1))
- && (bVal == GRREFERENCE->getPixel(w + k + 1, h + 1)))) {
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- }
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
- ((line1 >> (7 - k)) & 0x0080) |
- ((line1_r >> (9 - k)) & 0x0020) |
- ((line2_r >> (11 - k)) & 0x0004) |
- ((line3_r >> (13 - k)) & 0x0001);
- }
- pLine[w >> 3] = cVal;
- }
- }
- pLine += nStride;
- if (h < GRHR + GRREFERENCEDY) {
- pLineR += nStrideR;
- }
- }
- return GRREG;
-}
-CJBig2_Image *CJBig2_GRRDProc::decode_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
-{
- FX_BOOL LTP, SLTP, bVal;
- FX_BOOL TPGRPIX, TPGRVAL;
- FX_DWORD CONTEXT;
- CJBig2_Image *GRREG;
- LTP = 0;
- JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
- GRREG->fill(0);
- for(FX_DWORD h = 0; h < GRH; h++) {
- if(TPGRON) {
- switch(GRTEMPLATE) {
- case 0:
- CONTEXT = 0x0010;
- break;
- case 1:
- CONTEXT = 0x0008;
- break;
- }
- SLTP = pArithDecoder->DECODE(&grContext[CONTEXT]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 0) {
- for(FX_DWORD w = 0; w < GRW; w++) {
- CONTEXT = 0;
- switch(GRTEMPLATE) {
- case 0:
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 3;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 4;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 5;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1) << 6;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 7;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
- CONTEXT |= GRREG->getPixel(w - 1, h) << 9;
- CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 10;
- CONTEXT |= GRREG->getPixel(w, h - 1) << 11;
- CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
- break;
- case 1:
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 2;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 3;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 4;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 5;
- CONTEXT |= GRREG->getPixel(w - 1, h) << 6;
- CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 7;
- CONTEXT |= GRREG->getPixel(w, h - 1) << 8;
- CONTEXT |= GRREG->getPixel(w - 1, h - 1) << 9;
- break;
- }
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- GRREG->setPixel(w, h, bVal);
- }
- } else {
- for(FX_DWORD w = 0; w < GRW; w++) {
- bVal = GRREFERENCE->getPixel(w, h);
- if(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w, h - 1))
- && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
- && (bVal == GRREFERENCE->getPixel(w - 1, h))
- && (bVal == GRREFERENCE->getPixel(w + 1, h))
- && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
- && (bVal == GRREFERENCE->getPixel(w, h + 1))
- && (bVal == GRREFERENCE->getPixel(w + 1, h + 1))) {
- TPGRPIX = 1;
- TPGRVAL = bVal;
- } else {
- TPGRPIX = 0;
- }
- if(TPGRPIX) {
- GRREG->setPixel(w, h, TPGRVAL);
- } else {
- CONTEXT = 0;
- switch(GRTEMPLATE) {
- case 0:
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 3;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 4;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 5;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1) << 6;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 7;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
- CONTEXT |= GRREG->getPixel(w - 1, h) << 9;
- CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 10;
- CONTEXT |= GRREG->getPixel(w, h - 1) << 11;
- CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
- break;
- case 1:
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 2;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 3;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 4;
- CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 5;
- CONTEXT |= GRREG->getPixel(w - 1, h) << 6;
- CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 7;
- CONTEXT |= GRREG->getPixel(w, h - 1) << 8;
- CONTEXT |= GRREG->getPixel(w - 1, h - 1) << 9;
- break;
- }
- bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
- GRREG->setPixel(w, h, bVal);
- }
- }
- }
- }
- return GRREG;
-}
-CJBig2_Image *CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *grContext)
-{
- FX_INT32 STRIPT, FIRSTS;
- FX_DWORD NINSTANCES;
- FX_INT32 DT, DFS, CURS;
- FX_BYTE CURT;
- FX_INT32 SI, TI;
- FX_DWORD IDI;
- CJBig2_Image *IBI;
- FX_DWORD WI, HI;
- FX_INT32 IDS;
- FX_BOOL RI;
- FX_INT32 RDWI, RDHI, RDXI, RDYI;
- CJBig2_Image *IBOI;
- FX_DWORD WOI, HOI;
- CJBig2_Image *SBREG;
- FX_BOOL bFirst;
- FX_DWORD nTmp;
- FX_INT32 nVal, nBits;
- CJBig2_HuffmanDecoder *pHuffmanDecoder;
- CJBig2_GRRDProc *pGRRD;
- CJBig2_ArithDecoder *pArithDecoder;
- JBIG2_ALLOC(pHuffmanDecoder, CJBig2_HuffmanDecoder(pStream));
- JBIG2_ALLOC(SBREG, CJBig2_Image(SBW, SBH));
- SBREG->fill(SBDEFPIXEL);
- if(pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
- goto failed;
- }
- STRIPT *= SBSTRIPS;
- STRIPT = -STRIPT;
- FIRSTS = 0;
- NINSTANCES = 0;
- while(NINSTANCES < SBNUMINSTANCES) {
- if(pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
- goto failed;
- }
- DT *= SBSTRIPS;
- STRIPT = STRIPT + DT;
- bFirst = TRUE;
- for(;;) {
- if(bFirst) {
- if(pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
- goto failed;
- }
- FIRSTS = FIRSTS + DFS;
- CURS = FIRSTS;
- bFirst = FALSE;
- } else {
- nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS);
- if(nVal == JBIG2_OOB) {
- break;
- } else if(nVal != 0) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
- goto failed;
- } else {
- CURS = CURS + IDS + SBDSOFFSET;
- }
- }
- if(SBSTRIPS == 1) {
- CURT = 0;
- } else {
- nTmp = 1;
- while((FX_DWORD)(1 << nTmp) < SBSTRIPS) {
- nTmp ++;
- }
- if(pStream->readNBits(nTmp, &nVal) != 0) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
- goto failed;
- }
- CURT = nVal;
- }
- TI = STRIPT + CURT;
- nVal = 0;
- nBits = 0;
- for(;;) {
- if(pStream->read1Bit(&nTmp) != 0) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
- goto failed;
- }
- nVal = (nVal << 1) | nTmp;
- nBits ++;
- for(IDI = 0; IDI < SBNUMSYMS; IDI++) {
- if((nBits == SBSYMCODES[IDI].codelen) && (nVal == SBSYMCODES[IDI].code)) {
- break;
- }
- }
- if(IDI < SBNUMSYMS) {
- break;
- }
- }
- if(SBREFINE == 0) {
- RI = 0;
- } else {
- if(pStream->read1Bit(&RI) != 0) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
- goto failed;
- }
- }
- if(RI == 0) {
- IBI = SBSYMS[IDI];
- } else {
- if((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0)
- || (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0)
- || (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0)
- || (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0)
- || (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
- goto failed;
- }
- pStream->alignByte();
- nTmp = pStream->getOffset();
- IBOI = SBSYMS[IDI];
- if (!IBOI) {
- goto failed;
- }
- WOI = IBOI->m_nWidth;
- HOI = IBOI->m_nHeight;
- if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) {
- m_pModule->JBig2_Error("text region decoding procedure (huffman): Invalid RDWI or RDHI value.");
- goto failed;
- }
- JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
- pGRRD->GRW = WOI + RDWI;
- pGRRD->GRH = HOI + RDHI;
- pGRRD->GRTEMPLATE = SBRTEMPLATE;
- pGRRD->GRREFERENCE = IBOI;
- pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI;
- pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI;
- pGRRD->TPGRON = 0;
- pGRRD->GRAT[0] = SBRAT[0];
- pGRRD->GRAT[1] = SBRAT[1];
- pGRRD->GRAT[2] = SBRAT[2];
- pGRRD->GRAT[3] = SBRAT[3];
- JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(pStream));
- IBI = pGRRD->decode(pArithDecoder, grContext);
- if(IBI == NULL) {
- delete pGRRD;
- delete pArithDecoder;
- goto failed;
- }
- delete pArithDecoder;
- pStream->alignByte();
- pStream->offset(2);
- if((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
- delete IBI;
- delete pGRRD;
- m_pModule->JBig2_Error("text region decoding procedure (huffman):"
- "bytes processed by generic refinement region decoding procedure doesn't equal SBHUFFRSIZE.");
- goto failed;
- }
- delete pGRRD;
- }
- if (!IBI) {
- continue;
- }
- WI = IBI->m_nWidth;
- HI = IBI->m_nHeight;
- if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT)
- || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
- CURS = CURS + WI - 1;
- } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT)
- || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
- CURS = CURS + HI - 1;
- }
- SI = CURS;
- if(TRANSPOSED == 0) {
- switch(REFCORNER) {
- case JBIG2_CORNER_TOPLEFT:
- SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_TOPRIGHT:
- SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_BOTTOMLEFT:
- SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_BOTTOMRIGHT:
- SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
- break;
- }
- } else {
- switch(REFCORNER) {
- case JBIG2_CORNER_TOPLEFT:
- SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_TOPRIGHT:
- SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_BOTTOMLEFT:
- SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_BOTTOMRIGHT:
- SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
- break;
- }
- }
- if(RI != 0) {
- delete IBI;
- }
- if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
- || (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
- CURS = CURS + WI - 1;
- } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
- || (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
- CURS = CURS + HI - 1;
- }
- NINSTANCES = NINSTANCES + 1;
- }
- }
- delete pHuffmanDecoder;
- return SBREG;
-failed:
- delete pHuffmanDecoder;
- delete SBREG;
- return NULL;
-}
-CJBig2_Image *CJBig2_TRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext,
- JBig2IntDecoderState *pIDS)
-{
- FX_INT32 STRIPT, FIRSTS;
- FX_DWORD NINSTANCES;
- FX_INT32 DT, DFS, CURS;
- FX_INT32 CURT;
- FX_INT32 SI, TI;
- FX_DWORD IDI;
- CJBig2_Image *IBI;
- FX_DWORD WI, HI;
- FX_INT32 IDS;
- FX_BOOL RI;
- FX_INT32 RDWI, RDHI, RDXI, RDYI;
- CJBig2_Image *IBOI;
- FX_DWORD WOI, HOI;
- CJBig2_Image *SBREG;
- FX_BOOL bFirst;
- FX_INT32 nRet, nVal;
- FX_INT32 bRetained;
- CJBig2_ArithIntDecoder *IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH, *IARDX, *IARDY;
- CJBig2_ArithIaidDecoder *IAID;
- CJBig2_GRRDProc *pGRRD;
- if(pIDS) {
- IADT = pIDS->IADT;
- IAFS = pIDS->IAFS;
- IADS = pIDS->IADS;
- IAIT = pIDS->IAIT;
- IARI = pIDS->IARI;
- IARDW = pIDS->IARDW;
- IARDH = pIDS->IARDH;
- IARDX = pIDS->IARDX;
- IARDY = pIDS->IARDY;
- IAID = pIDS->IAID;
- bRetained = TRUE;
- } else {
- JBIG2_ALLOC(IADT, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IAFS, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IADS, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IAIT, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARI, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARDW, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARDH, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARDX, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARDY, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IAID , CJBig2_ArithIaidDecoder(SBSYMCODELEN));
- bRetained = FALSE;
- }
- JBIG2_ALLOC(SBREG, CJBig2_Image(SBW, SBH));
- SBREG->fill(SBDEFPIXEL);
- if(IADT->decode(pArithDecoder, &STRIPT) == -1) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
- goto failed;
- }
- STRIPT *= SBSTRIPS;
- STRIPT = -STRIPT;
- FIRSTS = 0;
- NINSTANCES = 0;
- while(NINSTANCES < SBNUMINSTANCES) {
- if(IADT->decode(pArithDecoder, &DT) == -1) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
- goto failed;
- }
- DT *= SBSTRIPS;
- STRIPT = STRIPT + DT;
- bFirst = TRUE;
- for(;;) {
- if(bFirst) {
- if(IAFS->decode(pArithDecoder, &DFS) == -1) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
- goto failed;
- }
- FIRSTS = FIRSTS + DFS;
- CURS = FIRSTS;
- bFirst = FALSE;
- } else {
- nRet = IADS->decode(pArithDecoder, &IDS);
- if(nRet == JBIG2_OOB) {
- break;
- } else if(nRet != 0) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
- goto failed;
- } else {
- CURS = CURS + IDS + SBDSOFFSET;
- }
- }
- if (NINSTANCES >= SBNUMINSTANCES) {
- break;
- }
- if(SBSTRIPS == 1) {
- CURT = 0;
- } else {
- if(IAIT->decode(pArithDecoder, &nVal) == -1) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
- goto failed;
- }
- CURT = nVal;
- }
- TI = STRIPT + CURT;
- if(IAID->decode(pArithDecoder, &nVal) == -1) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
- goto failed;
- }
- IDI = nVal;
- if(IDI >= SBNUMSYMS) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): symbol id out of range.(%d/%d)",
- IDI, SBNUMSYMS);
- goto failed;
- }
- if(SBREFINE == 0) {
- RI = 0;
- } else {
- if(IARI->decode(pArithDecoder, &RI) == -1) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
- goto failed;
- }
- }
- if (!SBSYMS[IDI]) {
- goto failed;
- }
- if(RI == 0) {
- IBI = SBSYMS[IDI];
- } else {
- if((IARDW->decode(pArithDecoder, &RDWI) == -1)
- || (IARDH->decode(pArithDecoder, &RDHI) == -1)
- || (IARDX->decode(pArithDecoder, &RDXI) == -1)
- || (IARDY->decode(pArithDecoder, &RDYI) == -1)) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
- goto failed;
- }
- IBOI = SBSYMS[IDI];
- WOI = IBOI->m_nWidth;
- HOI = IBOI->m_nHeight;
- if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) {
- m_pModule->JBig2_Error("text region decoding procedure (arith): Invalid RDWI or RDHI value.");
- goto failed;
- }
- JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
- pGRRD->GRW = WOI + RDWI;
- pGRRD->GRH = HOI + RDHI;
- pGRRD->GRTEMPLATE = SBRTEMPLATE;
- pGRRD->GRREFERENCE = IBOI;
- pGRRD->GRREFERENCEDX = (RDWI >> 1) + RDXI;
- pGRRD->GRREFERENCEDY = (RDHI >> 1) + RDYI;
- pGRRD->TPGRON = 0;
- pGRRD->GRAT[0] = SBRAT[0];
- pGRRD->GRAT[1] = SBRAT[1];
- pGRRD->GRAT[2] = SBRAT[2];
- pGRRD->GRAT[3] = SBRAT[3];
- IBI = pGRRD->decode(pArithDecoder, grContext);
- if(IBI == NULL) {
- delete pGRRD;
- goto failed;
- }
- delete pGRRD;
- }
- WI = IBI->m_nWidth;
- HI = IBI->m_nHeight;
- if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT)
- || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
- CURS = CURS + WI - 1;
- } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT)
- || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
- CURS = CURS + HI - 1;
- }
- SI = CURS;
- if(TRANSPOSED == 0) {
- switch(REFCORNER) {
- case JBIG2_CORNER_TOPLEFT:
- SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_TOPRIGHT:
- SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_BOTTOMLEFT:
- SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_BOTTOMRIGHT:
- SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
- break;
- }
- } else {
- switch(REFCORNER) {
- case JBIG2_CORNER_TOPLEFT:
- SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_TOPRIGHT:
- SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_BOTTOMLEFT:
- SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
- break;
- case JBIG2_CORNER_BOTTOMRIGHT:
- SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
- break;
- }
- }
- if(RI != 0) {
- delete IBI;
- }
- if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
- || (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
- CURS = CURS + WI - 1;
- } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
- || (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
- CURS = CURS + HI - 1;
- }
- NINSTANCES = NINSTANCES + 1;
- }
- }
- if(bRetained == FALSE) {
- delete IADT;
- delete IAFS;
- delete IADS;
- delete IAIT;
- delete IARI;
- delete IARDW;
- delete IARDH;
- delete IARDX;
- delete IARDY;
- delete IAID;
- }
- return SBREG;
-failed:
- if(bRetained == FALSE) {
- delete IADT;
- delete IAFS;
- delete IADS;
- delete IAIT;
- delete IARI;
- delete IARDW;
- delete IARDH;
- delete IARDX;
- delete IARDY;
- delete IAID;
- }
- delete SBREG;
- return NULL;
-}
-CJBig2_SymbolDict *CJBig2_SDDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
- JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext)
-{
- CJBig2_Image **SDNEWSYMS;
- FX_DWORD HCHEIGHT, NSYMSDECODED;
- FX_INT32 HCDH;
- FX_DWORD SYMWIDTH, TOTWIDTH, HCFIRSTSYM;
- FX_INT32 DW;
- CJBig2_Image *BS;
- FX_DWORD I, J, REFAGGNINST;
- FX_BOOL *EXFLAGS;
- FX_DWORD EXINDEX;
- FX_BOOL CUREXFLAG;
- FX_DWORD EXRUNLENGTH;
- FX_INT32 nVal;
- FX_DWORD nTmp;
- FX_BOOL SBHUFF;
- FX_DWORD SBNUMSYMS;
- FX_BYTE SBSYMCODELEN;
- FX_DWORD IDI;
- FX_INT32 RDXI, RDYI;
- CJBig2_Image **SBSYMS;
- CJBig2_HuffmanTable *SBHUFFFS, *SBHUFFDS, *SBHUFFDT, *SBHUFFRDW, *SBHUFFRDH, *SBHUFFRDX, *SBHUFFRDY,
- *SBHUFFRSIZE;
- CJBig2_GRRDProc *pGRRD;
- CJBig2_GRDProc *pGRD;
- CJBig2_ArithIntDecoder *IADH, *IADW, *IAAI, *IARDX, *IARDY, *IAEX,
- *IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH;
- CJBig2_ArithIaidDecoder *IAID;
- CJBig2_SymbolDict *pDict;
- JBIG2_ALLOC(IADH, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IADW, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IAAI, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARDX, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARDY, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IAEX, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IADT, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IAFS, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IADS, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IAIT, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARI, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARDW, CJBig2_ArithIntDecoder());
- JBIG2_ALLOC(IARDH, CJBig2_ArithIntDecoder());
- nTmp = 0;
- while((FX_DWORD)(1 << nTmp) < (SDNUMINSYMS + SDNUMNEWSYMS)) {
- nTmp ++;
- }
- JBIG2_ALLOC(IAID, CJBig2_ArithIaidDecoder((FX_BYTE)nTmp));
- SDNEWSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(CJBig2_Image*));
- FXSYS_memset32(SDNEWSYMS, 0 , SDNUMNEWSYMS * sizeof(CJBig2_Image*));
- HCHEIGHT = 0;
- NSYMSDECODED = 0;
- while(NSYMSDECODED < SDNUMNEWSYMS) {
- BS = NULL;
- if(IADH->decode(pArithDecoder, &HCDH) == -1) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
- goto failed;
- }
- HCHEIGHT = HCHEIGHT + HCDH;
- if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): invalid HCHEIGHT value.");
- goto failed;
- }
- SYMWIDTH = 0;
- TOTWIDTH = 0;
- HCFIRSTSYM = NSYMSDECODED;
- for(;;) {
- nVal = IADW->decode(pArithDecoder, &DW);
- if(nVal == JBIG2_OOB) {
- break;
- } else if(nVal != 0) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
- goto failed;
- } else {
- if (NSYMSDECODED >= SDNUMNEWSYMS) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): NSYMSDECODED >= SDNUMNEWSYMS.");
- goto failed;
- }
- SYMWIDTH = SYMWIDTH + DW;
- if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): invalid SYMWIDTH value.");
- goto failed;
- } else if (HCHEIGHT == 0 || SYMWIDTH == 0) {
- TOTWIDTH = TOTWIDTH + SYMWIDTH;
- SDNEWSYMS[NSYMSDECODED] = NULL;
- NSYMSDECODED = NSYMSDECODED + 1;
- continue;
- }
- TOTWIDTH = TOTWIDTH + SYMWIDTH;
- }
- if(SDREFAGG == 0) {
- JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
- pGRD->MMR = 0;
- pGRD->GBW = SYMWIDTH;
- pGRD->GBH = HCHEIGHT;
- pGRD->GBTEMPLATE = SDTEMPLATE;
- pGRD->TPGDON = 0;
- pGRD->USESKIP = 0;
- pGRD->GBAT[0] = SDAT[0];
- pGRD->GBAT[1] = SDAT[1];
- pGRD->GBAT[2] = SDAT[2];
- pGRD->GBAT[3] = SDAT[3];
- pGRD->GBAT[4] = SDAT[4];
- pGRD->GBAT[5] = SDAT[5];
- pGRD->GBAT[6] = SDAT[6];
- pGRD->GBAT[7] = SDAT[7];
- BS = pGRD->decode_Arith(pArithDecoder, gbContext);
- if(BS == NULL) {
- delete pGRD;
- goto failed;
- }
- delete pGRD;
- } else {
- if(IAAI->decode(pArithDecoder, (int*)&REFAGGNINST) == -1) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
- goto failed;
- }
- if(REFAGGNINST > 1) {
- CJBig2_TRDProc *pDecoder;
- JBIG2_ALLOC(pDecoder, CJBig2_TRDProc());
- pDecoder->SBHUFF = SDHUFF;
- pDecoder->SBREFINE = 1;
- pDecoder->SBW = SYMWIDTH;
- pDecoder->SBH = HCHEIGHT;
- pDecoder->SBNUMINSTANCES = REFAGGNINST;
- pDecoder->SBSTRIPS = 1;
- pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
- SBNUMSYMS = pDecoder->SBNUMSYMS;
- nTmp = 0;
- while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
- nTmp ++;
- }
- SBSYMCODELEN = (FX_BYTE)nTmp;
- pDecoder->SBSYMCODELEN = SBSYMCODELEN;
- SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
- JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
- JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
- pDecoder->SBSYMS = SBSYMS;
- pDecoder->SBDEFPIXEL = 0;
- pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
- pDecoder->TRANSPOSED = 0;
- pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
- pDecoder->SBDSOFFSET = 0;
- JBIG2_ALLOC(SBHUFFFS, CJBig2_HuffmanTable(HuffmanTable_B6,
- sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
- JBIG2_ALLOC(SBHUFFDS, CJBig2_HuffmanTable(HuffmanTable_B8,
- sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
- JBIG2_ALLOC(SBHUFFDT, CJBig2_HuffmanTable(HuffmanTable_B11,
- sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
- JBIG2_ALLOC(SBHUFFRDW, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRDH, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRDY, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
- sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
- pDecoder->SBHUFFFS = SBHUFFFS;
- pDecoder->SBHUFFDS = SBHUFFDS;
- pDecoder->SBHUFFDT = SBHUFFDT;
- pDecoder->SBHUFFRDW = SBHUFFRDW;
- pDecoder->SBHUFFRDH = SBHUFFRDH;
- pDecoder->SBHUFFRDX = SBHUFFRDX;
- pDecoder->SBHUFFRDY = SBHUFFRDY;
- pDecoder->SBHUFFRSIZE = SBHUFFRSIZE;
- pDecoder->SBRTEMPLATE = SDRTEMPLATE;
- pDecoder->SBRAT[0] = SDRAT[0];
- pDecoder->SBRAT[1] = SDRAT[1];
- pDecoder->SBRAT[2] = SDRAT[2];
- pDecoder->SBRAT[3] = SDRAT[3];
- JBig2IntDecoderState ids;
- ids.IADT = IADT;
- ids.IAFS = IAFS;
- ids.IADS = IADS;
- ids.IAIT = IAIT;
- ids.IARI = IARI;
- ids.IARDW = IARDW;
- ids.IARDH = IARDH;
- ids.IARDX = IARDX;
- ids.IARDY = IARDY;
- ids.IAID = IAID;
- BS = pDecoder->decode_Arith(pArithDecoder, grContext, &ids);
- if(BS == NULL) {
- m_pModule->JBig2_Free(SBSYMS);
- delete SBHUFFFS;
- delete SBHUFFDS;
- delete SBHUFFDT;
- delete SBHUFFRDW;
- delete SBHUFFRDH;
- delete SBHUFFRDX;
- delete SBHUFFRDY;
- delete SBHUFFRSIZE;
- delete pDecoder;
- goto failed;
- }
- m_pModule->JBig2_Free(SBSYMS);
- delete SBHUFFFS;
- delete SBHUFFDS;
- delete SBHUFFDT;
- delete SBHUFFRDW;
- delete SBHUFFRDH;
- delete SBHUFFRDX;
- delete SBHUFFRDY;
- delete SBHUFFRSIZE;
- delete pDecoder;
- } else if(REFAGGNINST == 1) {
- SBHUFF = SDHUFF;
- SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
- if(IAID->decode(pArithDecoder, (int*)&IDI) == -1) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
- goto failed;
- }
- if((IARDX->decode(pArithDecoder, &RDXI) == -1)
- || (IARDY->decode(pArithDecoder, &RDYI) == -1)) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
- goto failed;
- }
- if (IDI >= SBNUMSYMS) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith):"
- " refinement references unknown symbol %d", IDI);
- goto failed;
- }
- SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
- JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
- JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
- if (!SBSYMS[IDI]) {
- m_pModule->JBig2_Free(SBSYMS);
- goto failed;
- }
- JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
- pGRRD->GRW = SYMWIDTH;
- pGRRD->GRH = HCHEIGHT;
- pGRRD->GRTEMPLATE = SDRTEMPLATE;
- pGRRD->GRREFERENCE = SBSYMS[IDI];
- pGRRD->GRREFERENCEDX = RDXI;
- pGRRD->GRREFERENCEDY = RDYI;
- pGRRD->TPGRON = 0;
- pGRRD->GRAT[0] = SDRAT[0];
- pGRRD->GRAT[1] = SDRAT[1];
- pGRRD->GRAT[2] = SDRAT[2];
- pGRRD->GRAT[3] = SDRAT[3];
- BS = pGRRD->decode(pArithDecoder, grContext);
- if(BS == NULL) {
- m_pModule->JBig2_Free(SBSYMS);
- delete pGRRD;
- goto failed;
- }
- m_pModule->JBig2_Free(SBSYMS);
- delete pGRRD;
- }
- }
- SDNEWSYMS[NSYMSDECODED] = BS;
- BS = NULL;
- NSYMSDECODED = NSYMSDECODED + 1;
- }
- }
- EXINDEX = 0;
- CUREXFLAG = 0;
- EXFLAGS = (FX_BOOL*)m_pModule->JBig2_Malloc2(sizeof(FX_BOOL), (SDNUMINSYMS + SDNUMNEWSYMS));
- while(EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
- if(IAEX->decode(pArithDecoder, (int*)&EXRUNLENGTH) == -1) {
- m_pModule->JBig2_Free(EXFLAGS);
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
- goto failed;
- }
- if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS) {
- m_pModule->JBig2_Free(EXFLAGS);
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): Invalid EXRUNLENGTH value.");
- goto failed;
- }
- if(EXRUNLENGTH != 0) {
- for(I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
- EXFLAGS[I] = CUREXFLAG;
- }
- }
- EXINDEX = EXINDEX + EXRUNLENGTH;
- CUREXFLAG = !CUREXFLAG;
- }
- JBIG2_ALLOC(pDict, CJBig2_SymbolDict());
- pDict->SDNUMEXSYMS = SDNUMEXSYMS;
- pDict->SDEXSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), SDNUMEXSYMS);
- I = J = 0;
- for(I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
- if(EXFLAGS[I] && J < SDNUMEXSYMS) {
- if(I < SDNUMINSYMS) {
- JBIG2_ALLOC(pDict->SDEXSYMS[J], CJBig2_Image(*SDINSYMS[I]));
- } else {
- pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS];
- }
- J = J + 1;
- } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) {
- delete SDNEWSYMS[I - SDNUMINSYMS];
- }
- }
- if (J < SDNUMEXSYMS) {
- pDict->SDNUMEXSYMS = J;
- }
- m_pModule->JBig2_Free(EXFLAGS);
- m_pModule->JBig2_Free(SDNEWSYMS);
- delete IADH;
- delete IADW;
- delete IAAI;
- delete IARDX;
- delete IARDY;
- delete IAEX;
- delete IAID;
- delete IADT;
- delete IAFS;
- delete IADS;
- delete IAIT;
- delete IARI;
- delete IARDW;
- delete IARDH;
- return pDict;
-failed:
- for(I = 0; I < NSYMSDECODED; I++) {
- if (SDNEWSYMS[I]) {
- delete SDNEWSYMS[I];
- SDNEWSYMS[I] = NULL;
- }
- }
- m_pModule->JBig2_Free(SDNEWSYMS);
- delete IADH;
- delete IADW;
- delete IAAI;
- delete IARDX;
- delete IARDY;
- delete IAEX;
- delete IAID;
- delete IADT;
- delete IAFS;
- delete IADS;
- delete IAIT;
- delete IARI;
- delete IARDW;
- delete IARDH;
- return NULL;
-}
-CJBig2_SymbolDict *CJBig2_SDDProc::decode_Huffman(CJBig2_BitStream *pStream,
- JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext, IFX_Pause* pPause)
-{
- CJBig2_Image **SDNEWSYMS;
- FX_DWORD *SDNEWSYMWIDTHS;
- FX_DWORD HCHEIGHT, NSYMSDECODED;
- FX_INT32 HCDH;
- FX_DWORD SYMWIDTH, TOTWIDTH, HCFIRSTSYM;
- FX_INT32 DW;
- CJBig2_Image *BS, *BHC;
- FX_DWORD I, J, REFAGGNINST;
- FX_BOOL *EXFLAGS;
- FX_DWORD EXINDEX;
- FX_BOOL CUREXFLAG;
- FX_DWORD EXRUNLENGTH;
- FX_INT32 nVal, nBits;
- FX_DWORD nTmp;
- FX_BOOL SBHUFF;
- FX_DWORD SBNUMSYMS;
- FX_BYTE SBSYMCODELEN;
- JBig2HuffmanCode *SBSYMCODES;
- FX_DWORD IDI;
- FX_INT32 RDXI, RDYI;
- FX_DWORD BMSIZE;
- FX_DWORD stride;
- CJBig2_Image **SBSYMS;
- CJBig2_HuffmanTable *SBHUFFFS, *SBHUFFDS, *SBHUFFDT, *SBHUFFRDW, *SBHUFFRDH, *SBHUFFRDX, *SBHUFFRDY,
- *SBHUFFRSIZE, *pTable;
- CJBig2_HuffmanDecoder *pHuffmanDecoder;
- CJBig2_GRRDProc *pGRRD;
- CJBig2_ArithDecoder *pArithDecoder;
- CJBig2_GRDProc *pGRD;
- CJBig2_SymbolDict *pDict;
- JBIG2_ALLOC(pHuffmanDecoder, CJBig2_HuffmanDecoder(pStream));
- SDNEWSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(CJBig2_Image*));
- FXSYS_memset32(SDNEWSYMS, 0 , SDNUMNEWSYMS * sizeof(CJBig2_Image*));
- SDNEWSYMWIDTHS = NULL;
- BHC = NULL;
- if(SDREFAGG == 0) {
- SDNEWSYMWIDTHS = (FX_DWORD *)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(FX_DWORD));
- FXSYS_memset32(SDNEWSYMWIDTHS, 0 , SDNUMNEWSYMS * sizeof(FX_DWORD));
- }
- HCHEIGHT = 0;
- NSYMSDECODED = 0;
- BS = NULL;
- while(NSYMSDECODED < SDNUMNEWSYMS) {
- if(pHuffmanDecoder->decodeAValue(SDHUFFDH, &HCDH) != 0) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
- goto failed;
- }
- HCHEIGHT = HCHEIGHT + HCDH;
- if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): invalid HCHEIGHT value.");
- goto failed;
- }
- SYMWIDTH = 0;
- TOTWIDTH = 0;
- HCFIRSTSYM = NSYMSDECODED;
- for(;;) {
- nVal = pHuffmanDecoder->decodeAValue(SDHUFFDW, &DW);
- if(nVal == JBIG2_OOB) {
- break;
- } else if(nVal != 0) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
- goto failed;
- } else {
- if (NSYMSDECODED >= SDNUMNEWSYMS) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): NSYMSDECODED >= SDNUMNEWSYMS.");
- goto failed;
- }
- SYMWIDTH = SYMWIDTH + DW;
- if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): invalid SYMWIDTH value.");
- goto failed;
- } else if (HCHEIGHT == 0 || SYMWIDTH == 0) {
- TOTWIDTH = TOTWIDTH + SYMWIDTH;
- SDNEWSYMS[NSYMSDECODED] = NULL;
- NSYMSDECODED = NSYMSDECODED + 1;
- continue;
- }
- TOTWIDTH = TOTWIDTH + SYMWIDTH;
- }
- if(SDREFAGG == 1) {
- if(pHuffmanDecoder->decodeAValue(SDHUFFAGGINST, (int*)&REFAGGNINST) != 0) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
- goto failed;
- }
- BS = NULL;
- if(REFAGGNINST > 1) {
- CJBig2_TRDProc *pDecoder;
- JBIG2_ALLOC(pDecoder, CJBig2_TRDProc());
- pDecoder->SBHUFF = SDHUFF;
- pDecoder->SBREFINE = 1;
- pDecoder->SBW = SYMWIDTH;
- pDecoder->SBH = HCHEIGHT;
- pDecoder->SBNUMINSTANCES = REFAGGNINST;
- pDecoder->SBSTRIPS = 1;
- pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
- SBNUMSYMS = pDecoder->SBNUMSYMS;
- SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(JBig2HuffmanCode));
- nTmp = 1;
- while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
- nTmp ++;
- }
- for(I = 0; I < SBNUMSYMS; I++) {
- SBSYMCODES[I].codelen = nTmp;
- SBSYMCODES[I].code = I;
- }
- pDecoder->SBSYMCODES = SBSYMCODES;
- SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
- JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
- JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
- pDecoder->SBSYMS = SBSYMS;
- pDecoder->SBDEFPIXEL = 0;
- pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
- pDecoder->TRANSPOSED = 0;
- pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
- pDecoder->SBDSOFFSET = 0;
- JBIG2_ALLOC(SBHUFFFS, CJBig2_HuffmanTable(HuffmanTable_B6,
- sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
- JBIG2_ALLOC(SBHUFFDS, CJBig2_HuffmanTable(HuffmanTable_B8,
- sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
- JBIG2_ALLOC(SBHUFFDT, CJBig2_HuffmanTable(HuffmanTable_B11,
- sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
- JBIG2_ALLOC(SBHUFFRDW, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRDH, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRDY, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
- sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
- pDecoder->SBHUFFFS = SBHUFFFS;
- pDecoder->SBHUFFDS = SBHUFFDS;
- pDecoder->SBHUFFDT = SBHUFFDT;
- pDecoder->SBHUFFRDW = SBHUFFRDW;
- pDecoder->SBHUFFRDH = SBHUFFRDH;
- pDecoder->SBHUFFRDX = SBHUFFRDX;
- pDecoder->SBHUFFRDY = SBHUFFRDY;
- pDecoder->SBHUFFRSIZE = SBHUFFRSIZE;
- pDecoder->SBRTEMPLATE = SDRTEMPLATE;
- pDecoder->SBRAT[0] = SDRAT[0];
- pDecoder->SBRAT[1] = SDRAT[1];
- pDecoder->SBRAT[2] = SDRAT[2];
- pDecoder->SBRAT[3] = SDRAT[3];
- BS = pDecoder->decode_Huffman(pStream, grContext);
- if(BS == NULL) {
- m_pModule->JBig2_Free(SBSYMCODES);
- m_pModule->JBig2_Free(SBSYMS);
- delete SBHUFFFS;
- delete SBHUFFDS;
- delete SBHUFFDT;
- delete SBHUFFRDW;
- delete SBHUFFRDH;
- delete SBHUFFRDX;
- delete SBHUFFRDY;
- delete SBHUFFRSIZE;
- delete pDecoder;
- goto failed;
- }
- m_pModule->JBig2_Free(SBSYMCODES);
- m_pModule->JBig2_Free(SBSYMS);
- delete SBHUFFFS;
- delete SBHUFFDS;
- delete SBHUFFDT;
- delete SBHUFFRDW;
- delete SBHUFFRDH;
- delete SBHUFFRDX;
- delete SBHUFFRDY;
- delete SBHUFFRSIZE;
- delete pDecoder;
- } else if(REFAGGNINST == 1) {
- SBHUFF = SDHUFF;
- SBNUMSYMS = SDNUMINSYMS + SDNUMNEWSYMS;
- nTmp = 1;
- while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
- nTmp ++;
- }
- SBSYMCODELEN = (FX_BYTE)nTmp;
- SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(JBig2HuffmanCode));
- for(I = 0; I < SBNUMSYMS; I++) {
- SBSYMCODES[I].codelen = SBSYMCODELEN;
- SBSYMCODES[I].code = I;
- }
- nVal = 0;
- nBits = 0;
- for(;;) {
- if(pStream->read1Bit(&nTmp) != 0) {
- m_pModule->JBig2_Free(SBSYMCODES);
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
- goto failed;
- }
- nVal = (nVal << 1) | nTmp;
- for(IDI = 0; IDI < SBNUMSYMS; IDI++) {
- if((nVal == SBSYMCODES[IDI].code)
- && (nBits == SBSYMCODES[IDI].codelen)) {
- break;
- }
- }
- if(IDI < SBNUMSYMS) {
- break;
- }
- }
- m_pModule->JBig2_Free(SBSYMCODES);
- JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
- sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
- JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
- sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
- if((pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0)
- || (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDYI) != 0)
- || (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) {
- delete SBHUFFRDX;
- delete SBHUFFRSIZE;
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
- goto failed;
- }
- delete SBHUFFRDX;
- delete SBHUFFRSIZE;
- pStream->alignByte();
- nTmp = pStream->getOffset();
- SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
- JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
- JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
- JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
- pGRRD->GRW = SYMWIDTH;
- pGRRD->GRH = HCHEIGHT;
- pGRRD->GRTEMPLATE = SDRTEMPLATE;
- pGRRD->GRREFERENCE = SBSYMS[IDI];
- pGRRD->GRREFERENCEDX = RDXI;
- pGRRD->GRREFERENCEDY = RDYI;
- pGRRD->TPGRON = 0;
- pGRRD->GRAT[0] = SDRAT[0];
- pGRRD->GRAT[1] = SDRAT[1];
- pGRRD->GRAT[2] = SDRAT[2];
- pGRRD->GRAT[3] = SDRAT[3];
- JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(pStream));
- BS = pGRRD->decode(pArithDecoder, grContext);
- if(BS == NULL) {
- m_pModule->JBig2_Free(SBSYMS);
- delete pGRRD;
- delete pArithDecoder;
- goto failed;
- }
- pStream->alignByte();
- pStream->offset(2);
- if((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
- delete BS;
- m_pModule->JBig2_Free(SBSYMS);
- delete pGRRD;
- delete pArithDecoder;
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman):"
- "bytes processed by generic refinement region decoding procedure doesn't equal SBHUFFRSIZE.");
- goto failed;
- }
- m_pModule->JBig2_Free(SBSYMS);
- delete pGRRD;
- delete pArithDecoder;
- }
- SDNEWSYMS[NSYMSDECODED] = BS;
- }
- if(SDREFAGG == 0) {
- SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
- }
- NSYMSDECODED = NSYMSDECODED + 1;
- }
- if(SDREFAGG == 0) {
- if(pHuffmanDecoder->decodeAValue(SDHUFFBMSIZE, (FX_INT32*)&BMSIZE) != 0) {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
- goto failed;
- }
- pStream->alignByte();
- if(BMSIZE == 0) {
- stride = (TOTWIDTH + 7) >> 3;
- if(pStream->getByteLeft() >= stride * HCHEIGHT) {
- JBIG2_ALLOC(BHC, CJBig2_Image(TOTWIDTH, HCHEIGHT));
- for(I = 0; I < HCHEIGHT; I ++) {
- JBIG2_memcpy(BHC->m_pData + I * BHC->m_nStride, pStream->getPointer(), stride);
- pStream->offset(stride);
- }
- } else {
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
- goto failed;
- }
- } else {
- JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
- pGRD->MMR = 1;
- pGRD->GBW = TOTWIDTH;
- pGRD->GBH = HCHEIGHT;
- FXCODEC_STATUS status = pGRD->Start_decode_MMR(&BHC, pStream);
- while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- pGRD->Continue_decode(pPause);
- }
- delete pGRD;
- pStream->alignByte();
- }
- nTmp = 0;
- if (!BHC) {
- continue;
- }
- for(I = HCFIRSTSYM; I < NSYMSDECODED; I++) {
- SDNEWSYMS[I] = BHC->subImage(nTmp, 0, SDNEWSYMWIDTHS[I], HCHEIGHT);
- nTmp += SDNEWSYMWIDTHS[I];
- }
- delete BHC;
- BHC = NULL;
- }
- }
- EXINDEX = 0;
- CUREXFLAG = 0;
- JBIG2_ALLOC(pTable, CJBig2_HuffmanTable(HuffmanTable_B1,
- sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
- EXFLAGS = (FX_BOOL*)m_pModule->JBig2_Malloc2(sizeof(FX_BOOL), (SDNUMINSYMS + SDNUMNEWSYMS));
- while(EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
- if(pHuffmanDecoder->decodeAValue(pTable, (int*)&EXRUNLENGTH) != 0) {
- delete pTable;
- m_pModule->JBig2_Free(EXFLAGS);
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
- goto failed;
- }
- if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS) {
- delete pTable;
- m_pModule->JBig2_Free(EXFLAGS);
- m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): Invalid EXRUNLENGTH value.");
- goto failed;
- }
- if(EXRUNLENGTH != 0) {
- for(I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
- EXFLAGS[I] = CUREXFLAG;
- }
- }
- EXINDEX = EXINDEX + EXRUNLENGTH;
- CUREXFLAG = !CUREXFLAG;
- }
- delete pTable;
- JBIG2_ALLOC(pDict, CJBig2_SymbolDict());
- pDict->SDNUMEXSYMS = SDNUMEXSYMS;
- pDict->SDEXSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), SDNUMEXSYMS);
- I = J = 0;
- for(I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
- if(EXFLAGS[I] && J < SDNUMEXSYMS) {
- if(I < SDNUMINSYMS) {
- JBIG2_ALLOC(pDict->SDEXSYMS[J], CJBig2_Image(*SDINSYMS[I]));
- } else {
- pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS];
- }
- J = J + 1;
- } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) {
- delete SDNEWSYMS[I - SDNUMINSYMS];
- }
- }
- if (J < SDNUMEXSYMS) {
- pDict->SDNUMEXSYMS = J;
- }
- m_pModule->JBig2_Free(EXFLAGS);
- m_pModule->JBig2_Free(SDNEWSYMS);
- if(SDREFAGG == 0) {
- m_pModule->JBig2_Free(SDNEWSYMWIDTHS);
- }
- delete pHuffmanDecoder;
- return pDict;
-failed:
- for(I = 0; I < NSYMSDECODED; I++) {
- if (SDNEWSYMS[I]) {
- delete SDNEWSYMS[I];
- }
- }
- m_pModule->JBig2_Free(SDNEWSYMS);
- if(SDREFAGG == 0) {
- m_pModule->JBig2_Free(SDNEWSYMWIDTHS);
- }
- delete pHuffmanDecoder;
- return NULL;
-}
-CJBig2_Image *CJBig2_HTRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
- JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_DWORD ng, mg;
- FX_INT32 x, y;
- CJBig2_Image *HSKIP;
- FX_DWORD HBPP;
- FX_DWORD *GI;
- CJBig2_Image *HTREG;
- CJBig2_GSIDProc *pGID;
- JBIG2_ALLOC(HTREG, CJBig2_Image(HBW, HBH));
- HTREG->fill(HDEFPIXEL);
- HSKIP = NULL;
- if(HENABLESKIP == 1) {
- JBIG2_ALLOC(HSKIP, CJBig2_Image(HGW, HGH));
- for(mg = 0; mg < HGH; mg++) {
- for(ng = 0; ng < HGW; ng++) {
- x = (HGX + mg * HRY + ng * HRX) >> 8;
- y = (HGY + mg * HRX - ng * HRY) >> 8;
- if((x + HPW <= 0) | (x >= (FX_INT32)HBW)
- | (y + HPH <= 0) | (y >= (FX_INT32)HPH)) {
- HSKIP->setPixel(ng, mg, 1);
- } else {
- HSKIP->setPixel(ng, mg, 0);
- }
- }
- }
- }
- HBPP = 1;
- while((FX_DWORD)(1 << HBPP) < HNUMPATS) {
- HBPP ++;
- }
- JBIG2_ALLOC(pGID, CJBig2_GSIDProc());
- pGID->GSMMR = HMMR;
- pGID->GSW = HGW;
- pGID->GSH = HGH;
- pGID->GSBPP = (FX_BYTE)HBPP;
- pGID->GSUSESKIP = HENABLESKIP;
- pGID->GSKIP = HSKIP;
- pGID->GSTEMPLATE = HTEMPLATE;
- GI = pGID->decode_Arith(pArithDecoder, gbContext, pPause);
- if(GI == NULL) {
- goto failed;
- }
- for(mg = 0; mg < HGH; mg++) {
- for(ng = 0; ng < HGW; ng++) {
- x = (HGX + mg * HRY + ng * HRX) >> 8;
- y = (HGY + mg * HRX - ng * HRY) >> 8;
- FX_DWORD pat_index = GI[mg * HGW + ng];
- if (pat_index >= HNUMPATS) {
- pat_index = HNUMPATS - 1;
- }
- HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
- }
- }
- m_pModule->JBig2_Free(GI);
- if(HSKIP) {
- delete HSKIP;
- }
- delete pGID;
- return HTREG;
-failed:
- if(HSKIP) {
- delete HSKIP;
- }
- delete pGID;
- delete HTREG;
- return NULL;
-}
-CJBig2_Image *CJBig2_HTRDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
-{
- FX_DWORD ng, mg;
- FX_INT32 x, y;
- FX_DWORD HBPP;
- FX_DWORD *GI;
- CJBig2_Image *HTREG;
- CJBig2_GSIDProc *pGID;
- JBIG2_ALLOC(HTREG, CJBig2_Image(HBW, HBH));
- HTREG->fill(HDEFPIXEL);
- HBPP = 1;
- while((FX_DWORD)(1 << HBPP) < HNUMPATS) {
- HBPP ++;
- }
- JBIG2_ALLOC(pGID, CJBig2_GSIDProc());
- pGID->GSMMR = HMMR;
- pGID->GSW = HGW;
- pGID->GSH = HGH;
- pGID->GSBPP = (FX_BYTE)HBPP;
- pGID->GSUSESKIP = 0;
- GI = pGID->decode_MMR(pStream, pPause);
- if(GI == NULL) {
- goto failed;
- }
- for(mg = 0; mg < HGH; mg++) {
- for(ng = 0; ng < HGW; ng++) {
- x = (HGX + mg * HRY + ng * HRX) >> 8;
- y = (HGY + mg * HRX - ng * HRY) >> 8;
- FX_DWORD pat_index = GI[mg * HGW + ng];
- if (pat_index >= HNUMPATS) {
- pat_index = HNUMPATS - 1;
- }
- HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
- }
- }
- m_pModule->JBig2_Free(GI);
- delete pGID;
- return HTREG;
-failed:
- delete pGID;
- delete HTREG;
- return NULL;
-}
-CJBig2_PatternDict *CJBig2_PDDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
- JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_DWORD GRAY;
- CJBig2_Image *BHDC = NULL;
- CJBig2_PatternDict *pDict;
- CJBig2_GRDProc *pGRD;
- JBIG2_ALLOC(pDict, CJBig2_PatternDict());
- pDict->NUMPATS = GRAYMAX + 1;
- pDict->HDPATS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), pDict->NUMPATS);
- JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*)*pDict->NUMPATS);
- JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
- pGRD->MMR = HDMMR;
- pGRD->GBW = (GRAYMAX + 1) * HDPW;
- pGRD->GBH = HDPH;
- pGRD->GBTEMPLATE = HDTEMPLATE;
- pGRD->TPGDON = 0;
- pGRD->USESKIP = 0;
- pGRD->GBAT[0] = -(FX_INT32)HDPW;
- pGRD->GBAT[1] = 0;
- if(pGRD->GBTEMPLATE == 0) {
- pGRD->GBAT[2] = -3;
- pGRD->GBAT[3] = -1;
- pGRD->GBAT[4] = 2;
- pGRD->GBAT[5] = -2;
- pGRD->GBAT[6] = -2;
- pGRD->GBAT[7] = -2;
- }
- FXCODEC_STATUS status = pGRD->Start_decode_Arith(&BHDC, pArithDecoder, gbContext);
- while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- pGRD->Continue_decode(pPause);
- }
- if(BHDC == NULL) {
- delete pGRD;
- goto failed;
- }
- delete pGRD;
- GRAY = 0;
- while(GRAY <= GRAYMAX) {
- pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
- GRAY = GRAY + 1;
- }
- delete BHDC;
- return pDict;
-failed:
- delete pDict;
- return NULL;
-}
-
-CJBig2_PatternDict *CJBig2_PDDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
-{
- FX_DWORD GRAY;
- CJBig2_Image *BHDC = NULL;
- CJBig2_PatternDict *pDict;
- CJBig2_GRDProc *pGRD;
- JBIG2_ALLOC(pDict, CJBig2_PatternDict());
- pDict->NUMPATS = GRAYMAX + 1;
- pDict->HDPATS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), pDict->NUMPATS);
- JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*)*pDict->NUMPATS);
- JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
- pGRD->MMR = HDMMR;
- pGRD->GBW = (GRAYMAX + 1) * HDPW;
- pGRD->GBH = HDPH;
- FXCODEC_STATUS status = pGRD->Start_decode_MMR(&BHDC, pStream);
- while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- pGRD->Continue_decode(pPause);
- }
- if(BHDC == NULL) {
- delete pGRD;
- goto failed;
- }
- delete pGRD;
- GRAY = 0;
- while(GRAY <= GRAYMAX) {
- pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
- GRAY = GRAY + 1;
- }
- delete BHDC;
- return pDict;
-failed:
- delete pDict;
- return NULL;
-}
-FX_DWORD *CJBig2_GSIDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
- JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- CJBig2_Image **GSPLANES;
- FX_INT32 J, K;
- FX_DWORD x, y;
- FX_DWORD *GSVALS;
- CJBig2_GRDProc *pGRD;
- GSPLANES = (CJBig2_Image **)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), GSBPP);
- if (!GSPLANES) {
- return NULL;
- }
- GSVALS = (FX_DWORD*)m_pModule->JBig2_Malloc3(sizeof(FX_DWORD), GSW, GSH);
- if (!GSVALS) {
- m_pModule->JBig2_Free(GSPLANES);
- return NULL;
- }
- JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*)*GSBPP);
- JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD)*GSW * GSH);
- JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
- pGRD->MMR = GSMMR;
- pGRD->GBW = GSW;
- pGRD->GBH = GSH;
- pGRD->GBTEMPLATE = GSTEMPLATE;
- pGRD->TPGDON = 0;
- pGRD->USESKIP = GSUSESKIP;
- pGRD->SKIP = GSKIP;
- if(GSTEMPLATE <= 1) {
- pGRD->GBAT[0] = 3;
- } else {
- pGRD->GBAT[0] = 2;
- }
- pGRD->GBAT[1] = -1;
- if(pGRD->GBTEMPLATE == 0) {
- pGRD->GBAT[2] = -3;
- pGRD->GBAT[3] = -1;
- pGRD->GBAT[4] = 2;
- pGRD->GBAT[5] = -2;
- pGRD->GBAT[6] = -2;
- pGRD->GBAT[7] = -2;
- }
- FXCODEC_STATUS status = pGRD->Start_decode_Arith(&GSPLANES[GSBPP - 1], pArithDecoder, gbContext);
- while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- pGRD->Continue_decode(pPause);
- }
- if(GSPLANES[GSBPP - 1] == NULL) {
- goto failed;
- }
- J = GSBPP - 2;
- while(J >= 0) {
- FXCODEC_STATUS status = pGRD->Start_decode_Arith(&GSPLANES[J], pArithDecoder, gbContext);
- while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- pGRD->Continue_decode(pPause);
- }
- if(GSPLANES[J] == NULL) {
- for(K = GSBPP - 1; K > J; K--) {
- delete GSPLANES[K];
- goto failed;
- }
- }
- GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR);
- J = J - 1;
- }
- for(y = 0; y < GSH; y++) {
- for(x = 0; x < GSW; x++) {
- for(J = 0; J < GSBPP; J++) {
- GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J;
- }
- }
- }
- for(J = 0; J < GSBPP; J++) {
- delete GSPLANES[J];
- }
- m_pModule->JBig2_Free(GSPLANES);
- delete pGRD;
- return GSVALS;
-failed:
- m_pModule->JBig2_Free(GSPLANES);
- delete pGRD;
- m_pModule->JBig2_Free(GSVALS);
- return NULL;
-}
-FX_DWORD *CJBig2_GSIDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
-{
- CJBig2_Image **GSPLANES;
- FX_INT32 J, K;
- FX_DWORD x, y;
- FX_DWORD *GSVALS;
- CJBig2_GRDProc *pGRD;
- GSPLANES = (CJBig2_Image **)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), GSBPP);
- if (!GSPLANES) {
- return NULL;
- }
- GSVALS = (FX_DWORD*)m_pModule->JBig2_Malloc3(sizeof(FX_DWORD), GSW, GSH);
- if (!GSVALS) {
- if (GSPLANES) {
- m_pModule->JBig2_Free(GSPLANES);
- }
- return NULL;
- }
- JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*)*GSBPP);
- JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD)*GSW * GSH);
- JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
- pGRD->MMR = GSMMR;
- pGRD->GBW = GSW;
- pGRD->GBH = GSH;
- FXCODEC_STATUS status = pGRD->Start_decode_MMR(&GSPLANES[GSBPP - 1], pStream);
- while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- pGRD->Continue_decode(pPause);
- }
- if(GSPLANES[GSBPP - 1] == NULL) {
- goto failed;
- }
- pStream->alignByte();
- pStream->offset(3);
- J = GSBPP - 2;
- while(J >= 0) {
- FXCODEC_STATUS status = pGRD->Start_decode_MMR(&GSPLANES[J], pStream);
- while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- pGRD->Continue_decode(pPause);
- }
- if(GSPLANES[J] == NULL) {
- for(K = GSBPP - 1; K > J; K--) {
- delete GSPLANES[K];
- goto failed;
- }
- }
- pStream->alignByte();
- pStream->offset(3);
- GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR);
- J = J - 1;
- }
- for(y = 0; y < GSH; y++) {
- for(x = 0; x < GSW; x++) {
- for(J = 0; J < GSBPP; J++) {
- GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J;
- }
- }
- }
- for(J = 0; J < GSBPP; J++) {
- delete GSPLANES[J];
- }
- m_pModule->JBig2_Free(GSPLANES);
- delete pGRD;
- return GSVALS;
-failed:
- m_pModule->JBig2_Free(GSPLANES);
- delete pGRD;
- m_pModule->JBig2_Free(GSVALS);
- return NULL;
-}
-FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- if (GBW == 0 || GBH == 0) {
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
- m_pPause = pPause;
- if(*pImage == NULL) {
- JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
- }
- if ((*pImage)->m_pData == NULL) {
- delete *pImage;
- *pImage = NULL;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- m_ProssiveStatus = FXCODEC_STATUS_ERROR;
- return FXCODEC_STATUS_ERROR;
- }
- m_DecodeType = 1;
- m_pImage = pImage;
- (*m_pImage)->fill(0);
- m_pArithDecoder = pArithDecoder;
- m_gbContext = gbContext;
- LTP = 0;
- m_pLine = NULL;
- m_loopIndex = 0;
- return decode_Arith(pPause);
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith(IFX_Pause* pPause)
-{
- int iline = m_loopIndex;
- CJBig2_Image* pImage = *m_pImage;
- if(GBTEMPLATE == 0) {
- if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)
- && (GBAT[2] == (signed char) - 3) && (GBAT[3] == (signed char) - 1)
- && (GBAT[4] == 2) && (GBAT[5] == (signed char) - 2)
- && (GBAT[6] == (signed char) - 2) && (GBAT[7] == (signed char) - 2)) {
- m_ProssiveStatus = decode_Arith_Template0_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
- } else {
- m_ProssiveStatus = decode_Arith_Template0_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
- }
- } else if(GBTEMPLATE == 1) {
- if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)) {
- m_ProssiveStatus = decode_Arith_Template1_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
- } else {
- m_ProssiveStatus = decode_Arith_Template1_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
- }
- } else if(GBTEMPLATE == 2) {
- if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
- m_ProssiveStatus = decode_Arith_Template2_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
- } else {
- m_ProssiveStatus = decode_Arith_Template2_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
- }
- } else {
- if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
- m_ProssiveStatus = decode_Arith_Template3_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
- } else {
- m_ProssiveStatus = decode_Arith_Template3_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
- }
- }
- m_ReplaceRect.left = 0;
- m_ReplaceRect.right = pImage->m_nWidth;
- m_ReplaceRect.top = iline;
- m_ReplaceRect.bottom = m_loopIndex;
- if(m_ProssiveStatus == FXCODEC_STATUS_DECODE_FINISH) {
- m_loopIndex = 0;
- }
- return m_ProssiveStatus;
-}
-FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith_V2(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- if(GBW == 0 || GBH == 0) {
- * pImage = NULL;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
- }
- if(*pImage == NULL) {
- JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
- }
- if ((*pImage)->m_pData == NULL) {
- delete *pImage;
- *pImage = NULL;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- m_ProssiveStatus = FXCODEC_STATUS_ERROR;
- return FXCODEC_STATUS_ERROR;
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
- m_DecodeType = 2;
- m_pPause = pPause;
- m_pImage = pImage;
- (*m_pImage)->fill(0);
- LTP = 0;
- m_loopIndex = 0;
- m_pArithDecoder = pArithDecoder;
- m_gbContext = gbContext;
- return decode_Arith_V2(pPause);
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_V2(IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- CJBig2_Image *GBREG = *m_pImage;
- FX_DWORD line1, line2, line3;
- LTP = 0;
- JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
- GBREG->fill(0);
- for(; m_loopIndex < GBH; m_loopIndex++) {
- if(TPGDON) {
- switch(GBTEMPLATE) {
- case 0:
- CONTEXT = 0x9b25;
- break;
- case 1:
- CONTEXT = 0x0795;
- break;
- case 2:
- CONTEXT = 0x00e5;
- break;
- case 3:
- CONTEXT = 0x0195;
- break;
- }
- SLTP = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- GBREG->copyLine(m_loopIndex, m_loopIndex - 1);
- } else {
- switch(GBTEMPLATE) {
- case 0: {
- line1 = GBREG->getPixel(1, m_loopIndex - 2);
- line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 1;
- line2 = GBREG->getPixel(2, m_loopIndex - 1);
- line2 |= GBREG->getPixel(1, m_loopIndex - 1) << 1;
- line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
- CONTEXT |= line2 << 5;
- CONTEXT |= GBREG->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
- CONTEXT |= GBREG->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
- CONTEXT |= line1 << 12;
- CONTEXT |= GBREG->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
- bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, m_loopIndex, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x0f;
- }
- }
- break;
- case 1: {
- line1 = GBREG->getPixel(2, m_loopIndex - 2);
- line1 |= GBREG->getPixel(1, m_loopIndex - 2) << 1;
- line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 2;
- line2 = GBREG->getPixel(2, m_loopIndex - 1);
- line2 |= GBREG->getPixel(1, m_loopIndex - 1) << 1;
- line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 3;
- CONTEXT |= line2 << 4;
- CONTEXT |= line1 << 9;
- bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, m_loopIndex, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 2)) & 0x0f;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x07;
- }
- }
- break;
- case 2: {
- line1 = GBREG->getPixel(1, m_loopIndex - 2);
- line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 1;
- line2 = GBREG->getPixel(1, m_loopIndex - 1);
- line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 1;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
- CONTEXT |= line2 << 3;
- CONTEXT |= line1 << 7;
- bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, m_loopIndex, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
- line2 = ((line2 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 1)) & 0x0f;
- line3 = ((line3 << 1) | bVal) & 0x03;
- }
- }
- break;
- case 3: {
- line1 = GBREG->getPixel(1, m_loopIndex - 1);
- line1 |= GBREG->getPixel(0, m_loopIndex - 1) << 1;
- line2 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
- bVal = 0;
- } else {
- CONTEXT = line2;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
- CONTEXT |= line1 << 5;
- bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
- }
- if(bVal) {
- GBREG->setPixel(w, m_loopIndex, bVal);
- }
- line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 1)) & 0x1f;
- line2 = ((line2 << 1) | bVal) & 0x0f;
- }
- }
- break;
- }
- }
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex ++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith_V1(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- if(GBW == 0 || GBH == 0) {
- * pImage = NULL;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
- }
- if(*pImage == NULL) {
- JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
- }
- if ((*pImage)->m_pData == NULL) {
- delete *pImage;
- *pImage = NULL;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- m_ProssiveStatus = FXCODEC_STATUS_ERROR;
- return FXCODEC_STATUS_ERROR;
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
- m_pPause = pPause;
- m_pImage = pImage;
- m_DecodeType = 3;
- (*m_pImage)->fill(0);
- LTP = 0;
- m_loopIndex = 0;
- m_pArithDecoder = pArithDecoder;
- m_gbContext = gbContext;
- return decode_Arith_V1(pPause);
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_V1(IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT = 0;
- CJBig2_Image *GBREG = (*m_pImage);
- for(; m_loopIndex < GBH; m_loopIndex++) {
- if(TPGDON) {
- switch(GBTEMPLATE) {
- case 0:
- CONTEXT = 0x9b25;
- break;
- case 1:
- CONTEXT = 0x0795;
- break;
- case 2:
- CONTEXT = 0x00e5;
- break;
- case 3:
- CONTEXT = 0x0195;
- break;
- }
- SLTP = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- for(FX_DWORD w = 0; w < GBW; w++) {
- GBREG->setPixel(w, m_loopIndex, GBREG->getPixel(w, m_loopIndex - 1));
- }
- } else {
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
- GBREG->setPixel(w, m_loopIndex, 0);
- } else {
- CONTEXT = 0;
- switch(GBTEMPLATE) {
- case 0:
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
- CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
- CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
- CONTEXT |= GBREG->getPixel(w - 4, m_loopIndex) << 3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
- CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 1) << 5;
- CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 6;
- CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 7;
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 8;
- CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 9;
- CONTEXT |= GBREG->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
- CONTEXT |= GBREG->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
- CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 12;
- CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 13;
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 14;
- CONTEXT |= GBREG->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
- break;
- case 1:
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
- CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
- CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 3;
- CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 1) << 4;
- CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 5;
- CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 6;
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 7;
- CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 8;
- CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 2) << 9;
- CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 10;
- CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 11;
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 12;
- break;
- case 2:
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
- CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
- CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 3;
- CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 4;
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 5;
- CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 6;
- CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 7;
- CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 8;
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 9;
- break;
- case 3:
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
- CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
- CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
- CONTEXT |= GBREG->getPixel(w - 4, m_loopIndex) << 3;
- CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
- CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 5;
- CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 6;
- CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 7;
- CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 8;
- CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex - 1) << 9;
- break;
- }
- bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
- GBREG->setPixel(w, m_loopIndex, bVal);
- }
- }
- }
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex ++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::Start_decode_MMR(CJBig2_Image** pImage, CJBig2_BitStream *pStream, IFX_Pause* pPause)
-{
- int bitpos, i;
- JBIG2_ALLOC((* pImage), CJBig2_Image(GBW, GBH));
- if ((* pImage)->m_pData == NULL) {
- delete (* pImage);
- (* pImage) = NULL;
- m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
- m_ProssiveStatus = FXCODEC_STATUS_ERROR;
- return m_ProssiveStatus;
- }
- bitpos = (int)pStream->getBitPos();
- _FaxG4Decode(m_pModule, pStream->getBuf(), pStream->getLength(), &bitpos, (* pImage)->m_pData, GBW, GBH, (* pImage)->m_nStride);
- pStream->setBitPos(bitpos);
- for(i = 0; (FX_DWORD)i < (* pImage)->m_nStride * GBH; i++) {
- (* pImage)->m_pData[i] = ~(* pImage)->m_pData[i];
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return m_ProssiveStatus;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_MMR()
-{
- return m_ProssiveStatus;
-}
-FXCODEC_STATUS CJBig2_GRDProc::Continue_decode(IFX_Pause* pPause)
-{
- if(m_ProssiveStatus != FXCODEC_STATUS_DECODE_TOBECONTINUE) {
- return m_ProssiveStatus;
- }
- switch (m_DecodeType) {
- case 1:
- return decode_Arith(pPause);
- case 2:
- return decode_Arith_V2(pPause);
- case 3:
- return decode_Arith_V1(pPause);
- case 4:
- return decode_MMR();
- }
- m_ProssiveStatus = FXCODEC_STATUS_ERROR;
- return m_ProssiveStatus;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- FX_DWORD line1, line2;
- FX_BYTE *pLine1, *pLine2, cVal;
- FX_INT32 nStride, nStride2, k;
- FX_INT32 nLineBytes, nBitsLeft, cc;
- if(m_pLine == NULL) {
- m_pLine = pImage->m_pData;
- }
- nStride = pImage->m_nStride;
- nStride2 = nStride << 1;
- nLineBytes = ((GBW + 7) >> 3) - 1;
- nBitsLeft = GBW - (nLineBytes << 3);
- FX_DWORD height = GBH & 0x7fffffff;
- for(; m_loopIndex < height; m_loopIndex++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- pImage->copyLine(m_loopIndex, m_loopIndex - 1);
- } else {
- if(m_loopIndex > 1) {
- pLine1 = m_pLine - nStride2;
- pLine2 = m_pLine - nStride;
- line1 = (*pLine1++) << 6;
- line2 = *pLine2++;
- CONTEXT = ((line1 & 0xf800) | (line2 & 0x07f0));
- for(cc = 0; cc < nLineBytes; cc++) {
- line1 = (line1 << 8) | ((*pLine1++) << 6);
- line2 = (line2 << 8) | (*pLine2++);
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
- | ((line1 >> k) & 0x0800)
- | ((line2 >> k) & 0x0010));
- }
- m_pLine[cc] = cVal;
- }
- line1 <<= 8;
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0800)
- | ((line2 >> (7 - k)) & 0x0010));
- }
- m_pLine[nLineBytes] = cVal;
- } else {
- pLine2 = m_pLine - nStride;
- line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
- CONTEXT = (line2 & 0x07f0);
- for(cc = 0; cc < nLineBytes; cc++) {
- if(m_loopIndex & 1) {
- line2 = (line2 << 8) | (*pLine2++);
- }
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
- | ((line2 >> k) & 0x0010));
- }
- m_pLine[cc] = cVal;
- }
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
- | ((line2 >> (7 - k)) & 0x0010));
- }
- m_pLine[nLineBytes] = cVal;
- }
- }
- m_pLine += nStride;
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- FX_DWORD line1, line2, line3;
- for(; m_loopIndex < GBH; m_loopIndex++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- pImage->copyLine(m_loopIndex, m_loopIndex - 1);
- } else {
- line1 = pImage->getPixel(1, m_loopIndex - 2);
- line1 |= pImage->getPixel(0, m_loopIndex - 2) << 1;
- line2 = pImage->getPixel(2, m_loopIndex - 1);
- line2 |= pImage->getPixel(1, m_loopIndex - 1) << 1;
- line2 |= pImage->getPixel(0, m_loopIndex - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
- CONTEXT |= line2 << 5;
- CONTEXT |= pImage->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
- CONTEXT |= pImage->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
- CONTEXT |= line1 << 12;
- CONTEXT |= pImage->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- pImage->setPixel(w, m_loopIndex, bVal);
- }
- line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
- line2 = ((line2 << 1) | pImage->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x0f;
- }
- }
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- FX_DWORD line1, line2;
- FX_BYTE *pLine1, *pLine2, cVal;
- FX_INT32 nStride, nStride2, k;
- FX_INT32 nLineBytes, nBitsLeft, cc;
- if (!m_pLine) {
- m_pLine = pImage->m_pData;
- }
- nStride = pImage->m_nStride;
- nStride2 = nStride << 1;
- nLineBytes = ((GBW + 7) >> 3) - 1;
- nBitsLeft = GBW - (nLineBytes << 3);
- for(; m_loopIndex < GBH; m_loopIndex++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- pImage->copyLine(m_loopIndex, m_loopIndex - 1);
- } else {
- if(m_loopIndex > 1) {
- pLine1 = m_pLine - nStride2;
- pLine2 = m_pLine - nStride;
- line1 = (*pLine1++) << 4;
- line2 = *pLine2++;
- CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
- for(cc = 0; cc < nLineBytes; cc++) {
- line1 = (line1 << 8) | ((*pLine1++) << 4);
- line2 = (line2 << 8) | (*pLine2++);
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line1 >> k) & 0x0200)
- | ((line2 >> (k + 1)) & 0x0008);
- }
- m_pLine[cc] = cVal;
- }
- line1 <<= 8;
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0200)
- | ((line2 >> (8 - k)) & 0x0008);
- }
- m_pLine[nLineBytes] = cVal;
- } else {
- pLine2 = m_pLine - nStride;
- line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
- CONTEXT = (line2 >> 1) & 0x01f8;
- for(cc = 0; cc < nLineBytes; cc++) {
- if(m_loopIndex & 1) {
- line2 = (line2 << 8) | (*pLine2++);
- }
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line2 >> (k + 1)) & 0x0008);
- }
- m_pLine[cc] = cVal;
- }
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
- | ((line2 >> (8 - k)) & 0x0008);
- }
- m_pLine[nLineBytes] = cVal;
- }
- }
- m_pLine += nStride;
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- FX_DWORD line1, line2, line3;
- for(FX_DWORD h = 0; h < GBH; h++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- pImage->copyLine(h, h - 1);
- } else {
- line1 = pImage->getPixel(2, h - 2);
- line1 |= pImage->getPixel(1, h - 2) << 1;
- line1 |= pImage->getPixel(0, h - 2) << 2;
- line2 = pImage->getPixel(2, h - 1);
- line2 |= pImage->getPixel(1, h - 1) << 1;
- line2 |= pImage->getPixel(0, h - 1) << 2;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, h)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= pImage->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
- CONTEXT |= line2 << 4;
- CONTEXT |= line1 << 9;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- pImage->setPixel(w, h, bVal);
- }
- line1 = ((line1 << 1) | pImage->getPixel(w + 3, h - 2)) & 0x0f;
- line2 = ((line2 << 1) | pImage->getPixel(w + 3, h - 1)) & 0x1f;
- line3 = ((line3 << 1) | bVal) & 0x07;
- }
- }
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- FX_DWORD line1, line2;
- FX_BYTE *pLine1, *pLine2, cVal;
- FX_INT32 nStride, nStride2, k;
- FX_INT32 nLineBytes, nBitsLeft, cc;
- if(!m_pLine) {
- m_pLine = pImage->m_pData;
- }
- nStride = pImage->m_nStride;
- nStride2 = nStride << 1;
- nLineBytes = ((GBW + 7) >> 3) - 1;
- nBitsLeft = GBW - (nLineBytes << 3);
- for(; m_loopIndex < GBH; m_loopIndex++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- pImage->copyLine(m_loopIndex, m_loopIndex - 1);
- } else {
- if(m_loopIndex > 1) {
- pLine1 = m_pLine - nStride2;
- pLine2 = m_pLine - nStride;
- line1 = (*pLine1++) << 1;
- line2 = *pLine2++;
- CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
- for(cc = 0; cc < nLineBytes; cc++) {
- line1 = (line1 << 8) | ((*pLine1++) << 1);
- line2 = (line2 << 8) | (*pLine2++);
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | ((line1 >> k) & 0x0080)
- | ((line2 >> (k + 3)) & 0x0004);
- }
- m_pLine[cc] = cVal;
- }
- line1 <<= 8;
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | ((line1 >> (7 - k)) & 0x0080)
- | ((line2 >> (10 - k)) & 0x0004);
- }
- m_pLine[nLineBytes] = cVal;
- } else {
- pLine2 = m_pLine - nStride;
- line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
- CONTEXT = (line2 >> 3) & 0x007c;
- for(cc = 0; cc < nLineBytes; cc++) {
- if(m_loopIndex & 1) {
- line2 = (line2 << 8) | (*pLine2++);
- }
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | ((line2 >> (k + 3)) & 0x0004);
- }
- m_pLine[cc] = cVal;
- }
- line2 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
- | (((line2 >> (10 - k))) & 0x0004);
- }
- m_pLine[nLineBytes] = cVal;
- }
- }
- m_pLine += nStride;
- if(pPause && m_loopIndex % 50 == 0 && pPause->NeedToPauseNow()) {
- m_loopIndex++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- FX_DWORD line1, line2, line3;
- for(; m_loopIndex < GBH; m_loopIndex++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- pImage->copyLine(m_loopIndex, m_loopIndex - 1);
- } else {
- line1 = pImage->getPixel(1, m_loopIndex - 2);
- line1 |= pImage->getPixel(0, m_loopIndex - 2) << 1;
- line2 = pImage->getPixel(1, m_loopIndex - 1);
- line2 |= pImage->getPixel(0, m_loopIndex - 1) << 1;
- line3 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
- bVal = 0;
- } else {
- CONTEXT = line3;
- CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
- CONTEXT |= line2 << 3;
- CONTEXT |= line1 << 7;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- pImage->setPixel(w, m_loopIndex, bVal);
- }
- line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
- line2 = ((line2 << 1) | pImage->getPixel(w + 2, m_loopIndex - 1)) & 0x0f;
- line3 = ((line3 << 1) | bVal) & 0x03;
- }
- }
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- FX_DWORD line1;
- FX_BYTE *pLine1, cVal;
- FX_INT32 nStride, k;
- FX_INT32 nLineBytes, nBitsLeft, cc;
- if (!m_pLine) {
- m_pLine = pImage->m_pData;
- }
- nStride = pImage->m_nStride;
- nLineBytes = ((GBW + 7) >> 3) - 1;
- nBitsLeft = GBW - (nLineBytes << 3);
- for(; m_loopIndex < GBH; m_loopIndex++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- pImage->copyLine(m_loopIndex, m_loopIndex - 1);
- } else {
- if(m_loopIndex > 0) {
- pLine1 = m_pLine - nStride;
- line1 = *pLine1++;
- CONTEXT = (line1 >> 1) & 0x03f0;
- for(cc = 0; cc < nLineBytes; cc++) {
- line1 = (line1 << 8) | (*pLine1++);
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
- | ((line1 >> (k + 1)) & 0x0010);
- }
- m_pLine[cc] = cVal;
- }
- line1 <<= 8;
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
- | ((line1 >> (8 - k)) & 0x0010);
- }
- m_pLine[nLineBytes] = cVal;
- } else {
- CONTEXT = 0;
- for(cc = 0; cc < nLineBytes; cc++) {
- cVal = 0;
- for(k = 7; k >= 0; k--) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << k;
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
- }
- m_pLine[cc] = cVal;
- }
- cVal = 0;
- for(k = 0; k < nBitsLeft; k++) {
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- cVal |= bVal << (7 - k);
- CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
- }
- m_pLine[nLineBytes] = cVal;
- }
- }
- m_pLine += nStride;
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
-FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
-{
- FX_BOOL SLTP, bVal;
- FX_DWORD CONTEXT;
- FX_DWORD line1, line2;
- for(; m_loopIndex < GBH; m_loopIndex++) {
- if(TPGDON) {
- SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
- LTP = LTP ^ SLTP;
- }
- if(LTP == 1) {
- pImage->copyLine(m_loopIndex, m_loopIndex - 1);
- } else {
- line1 = pImage->getPixel(1, m_loopIndex - 1);
- line1 |= pImage->getPixel(0, m_loopIndex - 1) << 1;
- line2 = 0;
- for(FX_DWORD w = 0; w < GBW; w++) {
- if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
- bVal = 0;
- } else {
- CONTEXT = line2;
- CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
- CONTEXT |= line1 << 5;
- bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
- }
- if(bVal) {
- pImage->setPixel(w, m_loopIndex, bVal);
- }
- line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 1)) & 0x1f;
- line2 = ((line2 << 1) | bVal) & 0x0f;
- }
- }
- if(pPause && pPause->NeedToPauseNow()) {
- m_loopIndex++;
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
- return FXCODEC_STATUS_DECODE_TOBECONTINUE;
- }
- }
- m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
- return FXCODEC_STATUS_DECODE_FINISH;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_GeneralDecoder.h"
+#include "JBig2_ArithDecoder.h"
+#include "JBig2_ArithIntDecoder.h"
+#include "JBig2_HuffmanDecoder.h"
+#include "JBig2_HuffmanTable.h"
+#include "JBig2_PatternDict.h"
+CJBig2_Image *CJBig2_GRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ if (GBW == 0 || GBH == 0) {
+ CJBig2_Image* pImage;
+ JBIG2_ALLOC(pImage, CJBig2_Image(GBW, GBH));
+ return pImage;
+ }
+ if(GBTEMPLATE == 0) {
+ if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)
+ && (GBAT[2] == (signed char) - 3) && (GBAT[3] == (signed char) - 1)
+ && (GBAT[4] == 2) && (GBAT[5] == (signed char) - 2)
+ && (GBAT[6] == (signed char) - 2) && (GBAT[7] == (signed char) - 2)) {
+ return decode_Arith_Template0_opt3(pArithDecoder, gbContext);
+ } else {
+ return decode_Arith_Template0_unopt(pArithDecoder, gbContext);
+ }
+ } else if(GBTEMPLATE == 1) {
+ if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)) {
+ return decode_Arith_Template1_opt3(pArithDecoder, gbContext);
+ } else {
+ return decode_Arith_Template1_unopt(pArithDecoder, gbContext);
+ }
+ } else if(GBTEMPLATE == 2) {
+ if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
+ return decode_Arith_Template2_opt3(pArithDecoder, gbContext);
+ } else {
+ return decode_Arith_Template2_unopt(pArithDecoder, gbContext);
+ }
+ } else {
+ if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
+ return decode_Arith_Template3_opt3(pArithDecoder, gbContext);
+ } else {
+ return decode_Arith_Template3_unopt(pArithDecoder, gbContext);
+ }
+ }
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(2, h - 2);
+ line1 |= GBREG->getPixel(1, h - 2) << 1;
+ line1 |= GBREG->getPixel(0, h - 2) << 2;
+ line2 = GBREG->getPixel(3, h - 1);
+ line2 |= GBREG->getPixel(2, h - 1) << 1;
+ line2 |= GBREG->getPixel(1, h - 1) << 2;
+ line2 |= GBREG->getPixel(0, h - 1) << 3;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 11;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x1f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 4, h - 1)) & 0x7f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, cVal;
+ FX_INTPTR nStride, nStride2;
+ FX_INT32 nBits, k;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = (h > 1) ? pLine[-nStride2] << 6 : 0;
+ line2 = (h > 0) ? pLine[-nStride] : 0;
+ CONTEXT = (line1 & 0xf800) | (line2 & 0x07f0);
+ for(FX_DWORD w = 0; w < GBW; w += 8) {
+ if(w + 8 < GBW) {
+ nBits = 8;
+ if(h > 1) {
+ line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 6);
+ }
+ if(h > 0) {
+ line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
+ }
+ } else {
+ nBits = GBW - w;
+ if(h > 1) {
+ line1 <<= 8;
+ }
+ if(h > 0) {
+ line2 <<= 8;
+ }
+ }
+ cVal = 0;
+ for(k = 0; k < nBits; k++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0800)
+ | ((line2 >> (7 - k)) & 0x0010);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ FX_DWORD height = GBH & 0x7fffffff;
+ for(FX_DWORD h = 0; h < height; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ if(h > 1) {
+ pLine1 = pLine - nStride2;
+ pLine2 = pLine - nStride;
+ line1 = (*pLine1++) << 6;
+ line2 = *pLine2++;
+ CONTEXT = ((line1 & 0xf800) | (line2 & 0x07f0));
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 6);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> k) & 0x0800)
+ | ((line2 >> k) & 0x0010));
+ }
+ pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0800)
+ | ((line2 >> (7 - k)) & 0x0010));
+ }
+ pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = pLine - nStride;
+ line2 = (h & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 & 0x07f0);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(h & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line2 >> k) & 0x0010));
+ }
+ pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | (((line2 >> (7 - k))) & 0x0010));
+ }
+ pLine[nLineBytes] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= line2 << 5;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
+ CONTEXT |= line1 << 12;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(2, h - 2);
+ line1 |= GBREG->getPixel(1, h - 2) << 1;
+ line1 |= GBREG->getPixel(0, h - 2) << 2;
+ line2 = GBREG->getPixel(3, h - 1);
+ line2 |= GBREG->getPixel(2, h - 1) << 1;
+ line2 |= GBREG->getPixel(1, h - 1) << 2;
+ line2 |= GBREG->getPixel(0, h - 1) << 3;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 9;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 4, h - 1)) & 0x3f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, cVal;
+ FX_INTPTR nStride, nStride2;
+ FX_INT32 nBits, k;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = (h > 1) ? pLine[-nStride2] << 4 : 0;
+ line2 = (h > 0) ? pLine[-nStride] : 0;
+ CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
+ for(FX_DWORD w = 0; w < GBW; w += 8) {
+ if(w + 8 < GBW) {
+ nBits = 8;
+ if(h > 1) {
+ line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 4);
+ }
+ if(h > 0) {
+ line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
+ }
+ } else {
+ nBits = GBW - w;
+ if(h > 1) {
+ line1 <<= 8;
+ }
+ if(h > 0) {
+ line2 <<= 8;
+ }
+ }
+ cVal = 0;
+ for(k = 0; k < nBits; k++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0200)
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ if(h > 1) {
+ pLine1 = pLine - nStride2;
+ pLine2 = pLine - nStride;
+ line1 = (*pLine1++) << 4;
+ line2 = *pLine2++;
+ CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 4);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> k) & 0x0200)
+ | ((line2 >> (k + 1)) & 0x0008);
+ }
+ pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0200)
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = pLine - nStride;
+ line2 = (h & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 >> 1) & 0x01f8;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(h & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line2 >> (k + 1)) & 0x0008);
+ }
+ pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ pLine[nLineBytes] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(2, h - 2);
+ line1 |= GBREG->getPixel(1, h - 2) << 1;
+ line1 |= GBREG->getPixel(0, h - 2) << 2;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 9;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= line2 << 2;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, cVal;
+ FX_INTPTR nStride, nStride2;
+ FX_INT32 nBits, k;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = (h > 1) ? pLine[-nStride2] << 1 : 0;
+ line2 = (h > 0) ? pLine[-nStride] : 0;
+ CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
+ for(FX_DWORD w = 0; w < GBW; w += 8) {
+ if(w + 8 < GBW) {
+ nBits = 8;
+ if(h > 1) {
+ line1 = (line1 << 8) | (pLine[-nStride2 + (w >> 3) + 1] << 1);
+ }
+ if(h > 0) {
+ line2 = (line2 << 8) | (pLine[-nStride + (w >> 3) + 1]);
+ }
+ } else {
+ nBits = GBW - w;
+ if(h > 1) {
+ line1 <<= 8;
+ }
+ if(h > 0) {
+ line2 <<= 8;
+ }
+ }
+ cVal = 0;
+ for(k = 0; k < nBits; k++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0080)
+ | ((line2 >> (10 - k)) & 0x0004);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine, *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ if(h > 1) {
+ pLine1 = pLine - nStride2;
+ pLine2 = pLine - nStride;
+ line1 = (*pLine1++) << 1;
+ line2 = *pLine2++;
+ CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 1);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> k) & 0x0080)
+ | ((line2 >> (k + 3)) & 0x0004);
+ }
+ pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0080)
+ | ((line2 >> (10 - k)) & 0x0004);
+ }
+ pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = pLine - nStride;
+ line2 = (h & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 >> 3) & 0x007c;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(h & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line2 >> (k + 3)) & 0x0004);
+ }
+ pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | (((line2 >> (10 - k))) & 0x0004);
+ }
+ pLine[nLineBytes] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template2_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(1, h - 1);
+ line2 |= GBREG->getPixel(0, h - 1) << 1;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x0f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(2, h - 1);
+ line1 |= GBREG->getPixel(1, h - 1) << 1;
+ line1 |= GBREG->getPixel(0, h - 1) << 2;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= line1 << 4;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x3f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1;
+ FX_BYTE *pLine, cVal;
+ FX_INTPTR nStride, nStride2;
+ FX_INT32 nBits, k;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nStride2 = nStride << 1;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = (h > 0) ? pLine[-nStride] : 0;
+ CONTEXT = (line1 >> 1) & 0x03f0;
+ for(FX_DWORD w = 0; w < GBW; w += 8) {
+ if(w + 8 < GBW) {
+ nBits = 8;
+ if(h > 0) {
+ line1 = (line1 << 8) | (pLine[-nStride + (w >> 3) + 1]);
+ }
+ } else {
+ nBits = GBW - w;
+ if(h > 0) {
+ line1 <<= 8;
+ }
+ }
+ cVal = 0;
+ for(k = 0; k < nBits; k++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (8 - k)) & 0x0010);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1;
+ FX_BYTE *pLine, *pLine1, cVal;
+ FX_INT32 nStride, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ if (GBREG->m_pData == NULL) {
+ delete GBREG;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ pLine = GBREG->m_pData;
+ nStride = GBREG->m_nStride;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ if(h > 0) {
+ pLine1 = pLine - nStride;
+ line1 = *pLine1++;
+ CONTEXT = (line1 >> 1) & 0x03f0;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | (*pLine1++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (k + 1)) & 0x0010);
+ }
+ pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (8 - k)) & 0x0010);
+ }
+ pLine[nLineBytes] = cVal;
+ } else {
+ CONTEXT = 0;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
+ }
+ pLine[cc] = cVal;
+ }
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
+ }
+ pLine[nLineBytes] = cVal;
+ }
+ }
+ pLine += nStride;
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_Template3_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ line1 = GBREG->getPixel(1, h - 1);
+ line1 |= GBREG->getPixel(0, h - 1) << 1;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= line1 << 5;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x1f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_V2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT = 0x9b25;
+ break;
+ case 1:
+ CONTEXT = 0x0795;
+ break;
+ case 2:
+ CONTEXT = 0x00e5;
+ break;
+ case 3:
+ CONTEXT = 0x0195;
+ break;
+ }
+ SLTP = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(h, h - 1);
+ } else {
+ switch(GBTEMPLATE) {
+ case 0: {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= line2 << 5;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
+ CONTEXT |= line1 << 12;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ break;
+ case 1: {
+ line1 = GBREG->getPixel(2, h - 2);
+ line1 |= GBREG->getPixel(1, h - 2) << 1;
+ line1 |= GBREG->getPixel(0, h - 2) << 2;
+ line2 = GBREG->getPixel(2, h - 1);
+ line2 |= GBREG->getPixel(1, h - 1) << 1;
+ line2 |= GBREG->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 9;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, h - 2)) & 0x0f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ break;
+ case 2: {
+ line1 = GBREG->getPixel(1, h - 2);
+ line1 |= GBREG->getPixel(0, h - 2) << 1;
+ line2 = GBREG->getPixel(1, h - 1);
+ line2 |= GBREG->getPixel(0, h - 1) << 1;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x0f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ break;
+ case 3: {
+ line1 = GBREG->getPixel(1, h - 1);
+ line1 |= GBREG->getPixel(0, h - 1) << 1;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= line1 << 5;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, h - 1)) & 0x1f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ break;
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_Arith_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT = 0;
+ CJBig2_Image *GBREG;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT = 0x9b25;
+ break;
+ case 1:
+ CONTEXT = 0x0795;
+ break;
+ case 2:
+ CONTEXT = 0x00e5;
+ break;
+ case 3:
+ CONTEXT = 0x0195;
+ break;
+ }
+ SLTP = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ GBREG->setPixel(w, h, GBREG->getPixel(w, h - 1));
+ }
+ } else {
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ GBREG->setPixel(w, h, 0);
+ } else {
+ CONTEXT = 0;
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT |= GBREG->getPixel(w - 1, h);
+ CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
+ CONTEXT |= GBREG->getPixel(w - 4, h) << 3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= GBREG->getPixel(w + 2, h - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w, h - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 9;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], h + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], h + GBAT[5]) << 11;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 12;
+ CONTEXT |= GBREG->getPixel(w, h - 2) << 13;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 14;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], h + GBAT[7]) << 15;
+ break;
+ case 1:
+ CONTEXT |= GBREG->getPixel(w - 1, h);
+ CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
+ CONTEXT |= GBREG->getPixel(w + 2, h - 1) << 4;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w, h - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w + 2, h - 2) << 9;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 10;
+ CONTEXT |= GBREG->getPixel(w, h - 2) << 11;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 12;
+ break;
+ case 2:
+ CONTEXT |= GBREG->getPixel(w - 1, h);
+ CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 2;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 3;
+ CONTEXT |= GBREG->getPixel(w, h - 1) << 4;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 2) << 7;
+ CONTEXT |= GBREG->getPixel(w, h - 2) << 8;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 2) << 9;
+ break;
+ case 3:
+ CONTEXT |= GBREG->getPixel(w - 1, h);
+ CONTEXT |= GBREG->getPixel(w - 2, h) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, h) << 2;
+ CONTEXT |= GBREG->getPixel(w - 4, h) << 3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], h + GBAT[1]) << 4;
+ CONTEXT |= GBREG->getPixel(w + 1, h - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w, h - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w - 1, h - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 2, h - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w - 3, h - 1) << 9;
+ break;
+ }
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ GBREG->setPixel(w, h, bVal);
+ }
+ }
+ }
+ }
+ return GBREG;
+}
+CJBig2_Image *CJBig2_GRDProc::decode_MMR(CJBig2_BitStream *pStream)
+{
+ int bitpos, i;
+ CJBig2_Image *pImage;
+ JBIG2_ALLOC(pImage, CJBig2_Image(GBW, GBH));
+ if (pImage->m_pData == NULL) {
+ delete pImage;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ return NULL;
+ }
+ bitpos = (int)pStream->getBitPos();
+ _FaxG4Decode(m_pModule, pStream->getBuf(), pStream->getLength(), &bitpos, pImage->m_pData, GBW, GBH, pImage->m_nStride);
+ pStream->setBitPos(bitpos);
+ for(i = 0; (FX_DWORD)i < pImage->m_nStride * GBH; i++) {
+ pImage->m_pData[i] = ~pImage->m_pData[i];
+ }
+ return pImage;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ if (GRW == 0 || GRH == 0) {
+ CJBig2_Image* pImage;
+ JBIG2_ALLOC(pImage, CJBig2_Image(GRW, GRH));
+ return pImage;
+ }
+ if(GRTEMPLATE == 0) {
+ if((GRAT[0] == (signed char) - 1) && (GRAT[1] == (signed char) - 1)
+ && (GRAT[2] == (signed char) - 1) && (GRAT[3] == (signed char) - 1)
+ && (GRREFERENCEDX == 0) && (GRW == (FX_DWORD)GRREFERENCE->m_nWidth)) {
+ return decode_Template0_opt(pArithDecoder, grContext);
+ } else {
+ return decode_Template0_unopt(pArithDecoder, grContext);
+ }
+ } else {
+ if((GRREFERENCEDX == 0) && (GRW == (FX_DWORD)GRREFERENCE->m_nWidth)) {
+ return decode_Template1_opt(pArithDecoder, grContext);
+ } else {
+ return decode_Template1_unopt(pArithDecoder, grContext);
+ }
+ }
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ FX_DWORD line1, line2, line3, line4, line5;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ GRREG->fill(0);
+ for(FX_DWORD h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ SLTP = pArithDecoder->DECODE(&grContext[0x0010]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 0) {
+ line1 = GRREG->getPixel(1, h - 1);
+ line1 |= GRREG->getPixel(0, h - 1) << 1;
+ line2 = 0;
+ line3 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
+ line3 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1) << 1;
+ line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
+ line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ CONTEXT = line5;
+ CONTEXT |= line4 << 3;
+ CONTEXT |= line3 << 6;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
+ CONTEXT |= line2 << 9;
+ CONTEXT |= line1 << 10;
+ CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ GRREG->setPixel(w, h, bVal);
+ line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x03;
+ line2 = ((line2 << 1) | bVal) & 0x01;
+ line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY - 1)) & 0x03;
+ line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
+ line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x07;
+ }
+ } else {
+ line1 = GRREG->getPixel(1, h - 1);
+ line1 |= GRREG->getPixel(0, h - 1) << 1;
+ line2 = 0;
+ line3 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY - 1);
+ line3 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1) << 1;
+ line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
+ line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ bVal = GRREFERENCE->getPixel(w, h);
+ if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h + 1)))) {
+ CONTEXT = line5;
+ CONTEXT |= line4 << 3;
+ CONTEXT |= line3 << 6;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
+ CONTEXT |= line2 << 9;
+ CONTEXT |= line1 << 10;
+ CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ }
+ GRREG->setPixel(w, h, bVal);
+ line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x03;
+ line2 = ((line2 << 1) | bVal) & 0x01;
+ line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY - 1)) & 0x03;
+ line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
+ line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x07;
+ }
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ FX_DWORD line1, line1_r, line2_r, line3_r;
+ FX_BYTE *pLine, *pLineR, cVal;
+ FX_INTPTR nStride, nStrideR, nOffset;
+ FX_INT32 k, nBits;
+ FX_INT32 GRWR, GRHR;
+ FX_INT32 GRW, GRH;
+ GRW = (FX_INT32)CJBig2_GRRDProc::GRW;
+ GRH = (FX_INT32)CJBig2_GRRDProc::GRH;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ if (GRREG->m_pData == NULL) {
+ delete GRREG;
+ m_pModule->JBig2_Error("Generic refinement region decoding procedure: Create Image Failed with width = %d, height = %d\n", GRW, GRH);
+ return NULL;
+ }
+ pLine = GRREG->m_pData;
+ pLineR = GRREFERENCE->m_pData;
+ nStride = GRREG->m_nStride;
+ nStrideR = GRREFERENCE->m_nStride;
+ GRWR = (FX_INT32)GRREFERENCE->m_nWidth;
+ GRHR = (FX_INT32)GRREFERENCE->m_nHeight;
+ if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
+ GRREFERENCEDY = 0;
+ }
+ nOffset = -GRREFERENCEDY * nStrideR;
+ for (FX_INT32 h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ SLTP = pArithDecoder->DECODE(&grContext[0x0010]);
+ LTP = LTP ^ SLTP;
+ }
+ line1 = (h > 0) ? pLine[-nStride] << 4 : 0;
+ FX_INT32 reference_h = h - GRREFERENCEDY;
+ FX_BOOL line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
+ FX_BOOL line2_r_ok = (reference_h > -1 && reference_h < GRHR);
+ FX_BOOL line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
+ line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
+ line2_r = line2_r_ok ? pLineR[nOffset] : 0;
+ line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
+ if(LTP == 0) {
+ CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0)
+ | ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
+ for (FX_INT32 w = 0; w < GRW; w += 8) {
+ nBits = GRW - w > 8 ? 8 : GRW - w;
+ if (h > 0)
+ line1 = (line1 << 8) |
+ (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
+ if (h > GRHR + GRREFERENCEDY + 1) {
+ line1_r = 0;
+ line2_r = 0;
+ line3_r = 0;
+ } else {
+ if(line1_r_ok)
+ line1_r = (line1_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
+ if(line2_r_ok)
+ line2_r = (line2_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
+ if(line3_r_ok)
+ line3_r = (line3_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
+ else {
+ line3_r = 0;
+ }
+ }
+ cVal = 0;
+ for (k = 0; k < nBits; k++) {
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
+ ((line1 >> (7 - k)) & 0x0400) |
+ ((line1_r >> (7 - k)) & 0x0040) |
+ ((line2_r >> (10 - k)) & 0x0008) |
+ ((line3_r >> (13 - k)) & 0x0001);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ } else {
+ CONTEXT = (line1 & 0x1c00) | (line1_r & 0x01c0)
+ | ((line2_r >> 3) & 0x0038) | ((line3_r >> 6) & 0x0007);
+ for (FX_INT32 w = 0; w < GRW; w += 8) {
+ nBits = GRW - w > 8 ? 8 : GRW - w;
+ if (h > 0)
+ line1 = (line1 << 8) |
+ (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 4 : 0);
+ if(line1_r_ok)
+ line1_r = (line1_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
+ if(line2_r_ok)
+ line2_r = (line2_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
+ if(line3_r_ok)
+ line3_r = (line3_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
+ else {
+ line3_r = 0;
+ }
+ cVal = 0;
+ for (k = 0; k < nBits; k++) {
+ bVal = GRREFERENCE->getPixel(w + k, h);
+ if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w + k - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + k - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + k, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h + 1)))) {
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0cdb) << 1) | (bVal << 9) |
+ ((line1 >> (7 - k)) & 0x0400) |
+ ((line1_r >> (7 - k)) & 0x0040) |
+ ((line2_r >> (10 - k)) & 0x0008) |
+ ((line3_r >> (13 - k)) & 0x0001);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ if (h < GRHR + GRREFERENCEDY) {
+ pLineR += nStrideR;
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ FX_DWORD line1, line2, line3, line4, line5;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ GRREG->fill(0);
+ for(FX_DWORD h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ SLTP = pArithDecoder->DECODE(&grContext[0x0008]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 0) {
+ line1 = GRREG->getPixel(1, h - 1);
+ line1 |= GRREG->getPixel(0, h - 1) << 1;
+ line1 |= GRREG->getPixel(-1, h - 1) << 2;
+ line2 = 0;
+ line3 = GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
+ line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
+ line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ CONTEXT = line5;
+ CONTEXT |= line4 << 2;
+ CONTEXT |= line3 << 5;
+ CONTEXT |= line2 << 6;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ GRREG->setPixel(w, h, bVal);
+ line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x07;
+ line2 = ((line2 << 1) | bVal) & 0x01;
+ line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1)) & 0x01;
+ line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
+ line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x03;
+ }
+ } else {
+ line1 = GRREG->getPixel(1, h - 1);
+ line1 |= GRREG->getPixel(0, h - 1) << 1;
+ line1 |= GRREG->getPixel(-1, h - 1) << 2;
+ line2 = 0;
+ line3 = GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY - 1);
+ line4 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY);
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY) << 1;
+ line4 |= GRREFERENCE->getPixel(-GRREFERENCEDX - 1, h - GRREFERENCEDY) << 2;
+ line5 = GRREFERENCE->getPixel(-GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ line5 |= GRREFERENCE->getPixel(-GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ bVal = GRREFERENCE->getPixel(w, h);
+ if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h + 1)))) {
+ CONTEXT = line5;
+ CONTEXT |= line4 << 2;
+ CONTEXT |= line3 << 5;
+ CONTEXT |= line2 << 6;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ }
+ GRREG->setPixel(w, h, bVal);
+ line1 = ((line1 << 1) | GRREG->getPixel(w + 2, h - 1)) & 0x07;
+ line2 = ((line2 << 1) | bVal) & 0x01;
+ line3 = ((line3 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1)) & 0x01;
+ line4 = ((line4 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY)) & 0x07;
+ line5 = ((line5 << 1) | GRREFERENCE->getPixel(w - GRREFERENCEDX + 2, h - GRREFERENCEDY + 1)) & 0x03;
+ }
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ FX_DWORD line1, line1_r, line2_r, line3_r;
+ FX_BYTE *pLine, *pLineR, cVal;
+ FX_INTPTR nStride, nStrideR, nOffset;
+ FX_INT32 k, nBits;
+ FX_INT32 GRWR, GRHR;
+ FX_INT32 GRW, GRH;
+ GRW = (FX_INT32)CJBig2_GRRDProc::GRW;
+ GRH = (FX_INT32)CJBig2_GRRDProc::GRH;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ if (GRREG->m_pData == NULL) {
+ delete GRREG;
+ m_pModule->JBig2_Error("Generic refinement region decoding procedure: Create Image Failed with width = %d, height = %d\n", GRW, GRH);
+ return NULL;
+ }
+ pLine = GRREG->m_pData;
+ pLineR = GRREFERENCE->m_pData;
+ nStride = GRREG->m_nStride;
+ nStrideR = GRREFERENCE->m_nStride;
+ GRWR = (FX_INT32)GRREFERENCE->m_nWidth;
+ GRHR = (FX_INT32)GRREFERENCE->m_nHeight;
+ if (GRREFERENCEDY < -GRHR + 1 || GRREFERENCEDY > GRHR - 1) {
+ GRREFERENCEDY = 0;
+ }
+ nOffset = -GRREFERENCEDY * nStrideR;
+ for (FX_INT32 h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ SLTP = pArithDecoder->DECODE(&grContext[0x0008]);
+ LTP = LTP ^ SLTP;
+ }
+ line1 = (h > 0) ? pLine[-nStride] << 1 : 0;
+ FX_INT32 reference_h = h - GRREFERENCEDY;
+ FX_BOOL line1_r_ok = (reference_h > 0 && reference_h < GRHR + 1);
+ FX_BOOL line2_r_ok = (reference_h > -1 && reference_h < GRHR);
+ FX_BOOL line3_r_ok = (reference_h > -2 && reference_h < GRHR - 1);
+ line1_r = line1_r_ok ? pLineR[nOffset - nStrideR] : 0;
+ line2_r = line2_r_ok ? pLineR[nOffset] : 0;
+ line3_r = line3_r_ok ? pLineR[nOffset + nStrideR] : 0;
+ if(LTP == 0) {
+ CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020)
+ | ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
+ for (FX_INT32 w = 0; w < GRW; w += 8) {
+ nBits = GRW - w > 8 ? 8 : GRW - w;
+ if (h > 0)
+ line1 = (line1 << 8) |
+ (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
+ if(line1_r_ok)
+ line1_r = (line1_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
+ if(line2_r_ok)
+ line2_r = (line2_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
+ if(line3_r_ok)
+ line3_r = (line3_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
+ else {
+ line3_r = 0;
+ }
+ cVal = 0;
+ for (k = 0; k < nBits; k++) {
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
+ ((line1 >> (7 - k)) & 0x0080) |
+ ((line1_r >> (9 - k)) & 0x0020) |
+ ((line2_r >> (11 - k)) & 0x0004) |
+ ((line3_r >> (13 - k)) & 0x0001);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ } else {
+ CONTEXT = (line1 & 0x0380) | ((line1_r >> 2) & 0x0020)
+ | ((line2_r >> 4) & 0x001c) | ((line3_r >> 6) & 0x0003);
+ for (FX_INT32 w = 0; w < GRW; w += 8) {
+ nBits = GRW - w > 8 ? 8 : GRW - w;
+ if (h > 0)
+ line1 = (line1 << 8) |
+ (w + 8 < GRW ? pLine[-nStride + (w >> 3) + 1] << 1 : 0);
+ if(line1_r_ok)
+ line1_r = (line1_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset - nStrideR + (w >> 3) + 1] : 0);
+ if(line2_r_ok)
+ line2_r = (line2_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + (w >> 3) + 1] : 0);
+ if(line3_r_ok)
+ line3_r = (line3_r << 8) |
+ (w + 8 < GRWR ? pLineR[nOffset + nStrideR + (w >> 3) + 1] : 0);
+ else {
+ line3_r = 0;
+ }
+ cVal = 0;
+ for (k = 0; k < nBits; k++) {
+ bVal = GRREFERENCE->getPixel(w + k, h);
+ if(!(TPGRON && (bVal == GRREFERENCE->getPixel(w + k - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + k - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + k - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + k, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + k + 1, h + 1)))) {
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ }
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x018d) << 1) | (bVal << 6) |
+ ((line1 >> (7 - k)) & 0x0080) |
+ ((line1_r >> (9 - k)) & 0x0020) |
+ ((line2_r >> (11 - k)) & 0x0004) |
+ ((line3_r >> (13 - k)) & 0x0001);
+ }
+ pLine[w >> 3] = cVal;
+ }
+ }
+ pLine += nStride;
+ if (h < GRHR + GRREFERENCEDY) {
+ pLineR += nStrideR;
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_GRRDProc::decode_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext)
+{
+ FX_BOOL LTP, SLTP, bVal;
+ FX_BOOL TPGRPIX, TPGRVAL;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GRREG;
+ LTP = 0;
+ JBIG2_ALLOC(GRREG, CJBig2_Image(GRW, GRH));
+ GRREG->fill(0);
+ for(FX_DWORD h = 0; h < GRH; h++) {
+ if(TPGRON) {
+ switch(GRTEMPLATE) {
+ case 0:
+ CONTEXT = 0x0010;
+ break;
+ case 1:
+ CONTEXT = 0x0008;
+ break;
+ }
+ SLTP = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 0) {
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ CONTEXT = 0;
+ switch(GRTEMPLATE) {
+ case 0:
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 3;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 4;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 5;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1) << 6;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 7;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
+ CONTEXT |= GRREG->getPixel(w - 1, h) << 9;
+ CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 10;
+ CONTEXT |= GRREG->getPixel(w, h - 1) << 11;
+ CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
+ break;
+ case 1:
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 2;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 3;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 4;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 5;
+ CONTEXT |= GRREG->getPixel(w - 1, h) << 6;
+ CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 7;
+ CONTEXT |= GRREG->getPixel(w, h - 1) << 8;
+ CONTEXT |= GRREG->getPixel(w - 1, h - 1) << 9;
+ break;
+ }
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ GRREG->setPixel(w, h, bVal);
+ }
+ } else {
+ for(FX_DWORD w = 0; w < GRW; w++) {
+ bVal = GRREFERENCE->getPixel(w, h);
+ if(TPGRON && (bVal == GRREFERENCE->getPixel(w - 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h - 1))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h))
+ && (bVal == GRREFERENCE->getPixel(w - 1, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w, h + 1))
+ && (bVal == GRREFERENCE->getPixel(w + 1, h + 1))) {
+ TPGRPIX = 1;
+ TPGRVAL = bVal;
+ } else {
+ TPGRPIX = 0;
+ }
+ if(TPGRPIX) {
+ GRREG->setPixel(w, h, TPGRVAL);
+ } else {
+ CONTEXT = 0;
+ switch(GRTEMPLATE) {
+ case 0:
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY + 1) << 2;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 3;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 4;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 5;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY - 1) << 6;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 7;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + GRAT[2], h - GRREFERENCEDY + GRAT[3]) << 8;
+ CONTEXT |= GRREG->getPixel(w - 1, h) << 9;
+ CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 10;
+ CONTEXT |= GRREG->getPixel(w, h - 1) << 11;
+ CONTEXT |= GRREG->getPixel(w + GRAT[0], h + GRAT[1]) << 12;
+ break;
+ case 1:
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY + 1);
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY + 1) << 1;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX + 1, h - GRREFERENCEDY) << 2;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY) << 3;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX - 1, h - GRREFERENCEDY) << 4;
+ CONTEXT |= GRREFERENCE->getPixel(w - GRREFERENCEDX, h - GRREFERENCEDY - 1) << 5;
+ CONTEXT |= GRREG->getPixel(w - 1, h) << 6;
+ CONTEXT |= GRREG->getPixel(w + 1, h - 1) << 7;
+ CONTEXT |= GRREG->getPixel(w, h - 1) << 8;
+ CONTEXT |= GRREG->getPixel(w - 1, h - 1) << 9;
+ break;
+ }
+ bVal = pArithDecoder->DECODE(&grContext[CONTEXT]);
+ GRREG->setPixel(w, h, bVal);
+ }
+ }
+ }
+ }
+ return GRREG;
+}
+CJBig2_Image *CJBig2_TRDProc::decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *grContext)
+{
+ FX_INT32 STRIPT, FIRSTS;
+ FX_DWORD NINSTANCES;
+ FX_INT32 DT, DFS, CURS;
+ FX_BYTE CURT;
+ FX_INT32 SI, TI;
+ FX_DWORD IDI;
+ CJBig2_Image *IBI;
+ FX_DWORD WI, HI;
+ FX_INT32 IDS;
+ FX_BOOL RI;
+ FX_INT32 RDWI, RDHI, RDXI, RDYI;
+ CJBig2_Image *IBOI;
+ FX_DWORD WOI, HOI;
+ CJBig2_Image *SBREG;
+ FX_BOOL bFirst;
+ FX_DWORD nTmp;
+ FX_INT32 nVal, nBits;
+ CJBig2_HuffmanDecoder *pHuffmanDecoder;
+ CJBig2_GRRDProc *pGRRD;
+ CJBig2_ArithDecoder *pArithDecoder;
+ JBIG2_ALLOC(pHuffmanDecoder, CJBig2_HuffmanDecoder(pStream));
+ JBIG2_ALLOC(SBREG, CJBig2_Image(SBW, SBH));
+ SBREG->fill(SBDEFPIXEL);
+ if(pHuffmanDecoder->decodeAValue(SBHUFFDT, &STRIPT) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ STRIPT *= SBSTRIPS;
+ STRIPT = -STRIPT;
+ FIRSTS = 0;
+ NINSTANCES = 0;
+ while(NINSTANCES < SBNUMINSTANCES) {
+ if(pHuffmanDecoder->decodeAValue(SBHUFFDT, &DT) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ DT *= SBSTRIPS;
+ STRIPT = STRIPT + DT;
+ bFirst = TRUE;
+ for(;;) {
+ if(bFirst) {
+ if(pHuffmanDecoder->decodeAValue(SBHUFFFS, &DFS) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ FIRSTS = FIRSTS + DFS;
+ CURS = FIRSTS;
+ bFirst = FALSE;
+ } else {
+ nVal = pHuffmanDecoder->decodeAValue(SBHUFFDS, &IDS);
+ if(nVal == JBIG2_OOB) {
+ break;
+ } else if(nVal != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ } else {
+ CURS = CURS + IDS + SBDSOFFSET;
+ }
+ }
+ if(SBSTRIPS == 1) {
+ CURT = 0;
+ } else {
+ nTmp = 1;
+ while((FX_DWORD)(1 << nTmp) < SBSTRIPS) {
+ nTmp ++;
+ }
+ if(pStream->readNBits(nTmp, &nVal) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ CURT = nVal;
+ }
+ TI = STRIPT + CURT;
+ nVal = 0;
+ nBits = 0;
+ for(;;) {
+ if(pStream->read1Bit(&nTmp) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ nVal = (nVal << 1) | nTmp;
+ nBits ++;
+ for(IDI = 0; IDI < SBNUMSYMS; IDI++) {
+ if((nBits == SBSYMCODES[IDI].codelen) && (nVal == SBSYMCODES[IDI].code)) {
+ break;
+ }
+ }
+ if(IDI < SBNUMSYMS) {
+ break;
+ }
+ }
+ if(SBREFINE == 0) {
+ RI = 0;
+ } else {
+ if(pStream->read1Bit(&RI) != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ }
+ if(RI == 0) {
+ IBI = SBSYMS[IDI];
+ } else {
+ if((pHuffmanDecoder->decodeAValue(SBHUFFRDW, &RDWI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRDH, &RDHI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRDY, &RDYI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ pStream->alignByte();
+ nTmp = pStream->getOffset();
+ IBOI = SBSYMS[IDI];
+ if (!IBOI) {
+ goto failed;
+ }
+ WOI = IBOI->m_nWidth;
+ HOI = IBOI->m_nHeight;
+ if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (huffman): Invalid RDWI or RDHI value.");
+ goto failed;
+ }
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ pGRRD->GRW = WOI + RDWI;
+ pGRRD->GRH = HOI + RDHI;
+ pGRRD->GRTEMPLATE = SBRTEMPLATE;
+ pGRRD->GRREFERENCE = IBOI;
+ pGRRD->GRREFERENCEDX = (RDWI >> 2) + RDXI;
+ pGRRD->GRREFERENCEDY = (RDHI >> 2) + RDYI;
+ pGRRD->TPGRON = 0;
+ pGRRD->GRAT[0] = SBRAT[0];
+ pGRRD->GRAT[1] = SBRAT[1];
+ pGRRD->GRAT[2] = SBRAT[2];
+ pGRRD->GRAT[3] = SBRAT[3];
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(pStream));
+ IBI = pGRRD->decode(pArithDecoder, grContext);
+ if(IBI == NULL) {
+ delete pGRRD;
+ delete pArithDecoder;
+ goto failed;
+ }
+ delete pArithDecoder;
+ pStream->alignByte();
+ pStream->offset(2);
+ if((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
+ delete IBI;
+ delete pGRRD;
+ m_pModule->JBig2_Error("text region decoding procedure (huffman):"
+ "bytes processed by generic refinement region decoding procedure doesn't equal SBHUFFRSIZE.");
+ goto failed;
+ }
+ delete pGRRD;
+ }
+ if (!IBI) {
+ continue;
+ }
+ WI = IBI->m_nWidth;
+ HI = IBI->m_nHeight;
+ if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
+ CURS = CURS + WI - 1;
+ } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
+ CURS = CURS + HI - 1;
+ }
+ SI = CURS;
+ if(TRANSPOSED == 0) {
+ switch(REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
+ break;
+ }
+ } else {
+ switch(REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
+ break;
+ }
+ }
+ if(RI != 0) {
+ delete IBI;
+ }
+ if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
+ CURS = CURS + WI - 1;
+ } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
+ || (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
+ CURS = CURS + HI - 1;
+ }
+ NINSTANCES = NINSTANCES + 1;
+ }
+ }
+ delete pHuffmanDecoder;
+ return SBREG;
+failed:
+ delete pHuffmanDecoder;
+ delete SBREG;
+ return NULL;
+}
+CJBig2_Image *CJBig2_TRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext,
+ JBig2IntDecoderState *pIDS)
+{
+ FX_INT32 STRIPT, FIRSTS;
+ FX_DWORD NINSTANCES;
+ FX_INT32 DT, DFS, CURS;
+ FX_INT32 CURT;
+ FX_INT32 SI, TI;
+ FX_DWORD IDI;
+ CJBig2_Image *IBI;
+ FX_DWORD WI, HI;
+ FX_INT32 IDS;
+ FX_BOOL RI;
+ FX_INT32 RDWI, RDHI, RDXI, RDYI;
+ CJBig2_Image *IBOI;
+ FX_DWORD WOI, HOI;
+ CJBig2_Image *SBREG;
+ FX_BOOL bFirst;
+ FX_INT32 nRet, nVal;
+ FX_INT32 bRetained;
+ CJBig2_ArithIntDecoder *IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH, *IARDX, *IARDY;
+ CJBig2_ArithIaidDecoder *IAID;
+ CJBig2_GRRDProc *pGRRD;
+ if(pIDS) {
+ IADT = pIDS->IADT;
+ IAFS = pIDS->IAFS;
+ IADS = pIDS->IADS;
+ IAIT = pIDS->IAIT;
+ IARI = pIDS->IARI;
+ IARDW = pIDS->IARDW;
+ IARDH = pIDS->IARDH;
+ IARDX = pIDS->IARDX;
+ IARDY = pIDS->IARDY;
+ IAID = pIDS->IAID;
+ bRetained = TRUE;
+ } else {
+ JBIG2_ALLOC(IADT, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAFS, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IADS, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAIT, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARI, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDW, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDH, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDX, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDY, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAID , CJBig2_ArithIaidDecoder(SBSYMCODELEN));
+ bRetained = FALSE;
+ }
+ JBIG2_ALLOC(SBREG, CJBig2_Image(SBW, SBH));
+ SBREG->fill(SBDEFPIXEL);
+ if(IADT->decode(pArithDecoder, &STRIPT) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ STRIPT *= SBSTRIPS;
+ STRIPT = -STRIPT;
+ FIRSTS = 0;
+ NINSTANCES = 0;
+ while(NINSTANCES < SBNUMINSTANCES) {
+ if(IADT->decode(pArithDecoder, &DT) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ DT *= SBSTRIPS;
+ STRIPT = STRIPT + DT;
+ bFirst = TRUE;
+ for(;;) {
+ if(bFirst) {
+ if(IAFS->decode(pArithDecoder, &DFS) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ FIRSTS = FIRSTS + DFS;
+ CURS = FIRSTS;
+ bFirst = FALSE;
+ } else {
+ nRet = IADS->decode(pArithDecoder, &IDS);
+ if(nRet == JBIG2_OOB) {
+ break;
+ } else if(nRet != 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ } else {
+ CURS = CURS + IDS + SBDSOFFSET;
+ }
+ }
+ if (NINSTANCES >= SBNUMINSTANCES) {
+ break;
+ }
+ if(SBSTRIPS == 1) {
+ CURT = 0;
+ } else {
+ if(IAIT->decode(pArithDecoder, &nVal) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ CURT = nVal;
+ }
+ TI = STRIPT + CURT;
+ if(IAID->decode(pArithDecoder, &nVal) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ IDI = nVal;
+ if(IDI >= SBNUMSYMS) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): symbol id out of range.(%d/%d)",
+ IDI, SBNUMSYMS);
+ goto failed;
+ }
+ if(SBREFINE == 0) {
+ RI = 0;
+ } else {
+ if(IARI->decode(pArithDecoder, &RI) == -1) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ }
+ if (!SBSYMS[IDI]) {
+ goto failed;
+ }
+ if(RI == 0) {
+ IBI = SBSYMS[IDI];
+ } else {
+ if((IARDW->decode(pArithDecoder, &RDWI) == -1)
+ || (IARDH->decode(pArithDecoder, &RDHI) == -1)
+ || (IARDX->decode(pArithDecoder, &RDXI) == -1)
+ || (IARDY->decode(pArithDecoder, &RDYI) == -1)) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): too short.");
+ goto failed;
+ }
+ IBOI = SBSYMS[IDI];
+ WOI = IBOI->m_nWidth;
+ HOI = IBOI->m_nHeight;
+ if ((int)(WOI + RDWI) < 0 || (int)(HOI + RDHI) < 0) {
+ m_pModule->JBig2_Error("text region decoding procedure (arith): Invalid RDWI or RDHI value.");
+ goto failed;
+ }
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ pGRRD->GRW = WOI + RDWI;
+ pGRRD->GRH = HOI + RDHI;
+ pGRRD->GRTEMPLATE = SBRTEMPLATE;
+ pGRRD->GRREFERENCE = IBOI;
+ pGRRD->GRREFERENCEDX = (RDWI >> 1) + RDXI;
+ pGRRD->GRREFERENCEDY = (RDHI >> 1) + RDYI;
+ pGRRD->TPGRON = 0;
+ pGRRD->GRAT[0] = SBRAT[0];
+ pGRRD->GRAT[1] = SBRAT[1];
+ pGRRD->GRAT[2] = SBRAT[2];
+ pGRRD->GRAT[3] = SBRAT[3];
+ IBI = pGRRD->decode(pArithDecoder, grContext);
+ if(IBI == NULL) {
+ delete pGRRD;
+ goto failed;
+ }
+ delete pGRRD;
+ }
+ WI = IBI->m_nWidth;
+ HI = IBI->m_nHeight;
+ if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPRIGHT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
+ CURS = CURS + WI - 1;
+ } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_BOTTOMLEFT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMRIGHT))) {
+ CURS = CURS + HI - 1;
+ }
+ SI = CURS;
+ if(TRANSPOSED == 0) {
+ switch(REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ SBREG->composeFrom(SI, TI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ SBREG->composeFrom(SI - WI + 1, TI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ SBREG->composeFrom(SI, TI - HI + 1, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ SBREG->composeFrom(SI - WI + 1, TI - HI + 1, IBI, SBCOMBOP);
+ break;
+ }
+ } else {
+ switch(REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ SBREG->composeFrom(TI, SI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ SBREG->composeFrom(TI - WI + 1, SI, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ SBREG->composeFrom(TI, SI - HI + 1, IBI, SBCOMBOP);
+ break;
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ SBREG->composeFrom(TI - WI + 1, SI - HI + 1, IBI, SBCOMBOP);
+ break;
+ }
+ }
+ if(RI != 0) {
+ delete IBI;
+ }
+ if(TRANSPOSED == 0 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
+ || (REFCORNER == JBIG2_CORNER_BOTTOMLEFT))) {
+ CURS = CURS + WI - 1;
+ } else if(TRANSPOSED == 1 && ((REFCORNER == JBIG2_CORNER_TOPLEFT)
+ || (REFCORNER == JBIG2_CORNER_TOPRIGHT))) {
+ CURS = CURS + HI - 1;
+ }
+ NINSTANCES = NINSTANCES + 1;
+ }
+ }
+ if(bRetained == FALSE) {
+ delete IADT;
+ delete IAFS;
+ delete IADS;
+ delete IAIT;
+ delete IARI;
+ delete IARDW;
+ delete IARDH;
+ delete IARDX;
+ delete IARDY;
+ delete IAID;
+ }
+ return SBREG;
+failed:
+ if(bRetained == FALSE) {
+ delete IADT;
+ delete IAFS;
+ delete IADS;
+ delete IAIT;
+ delete IARI;
+ delete IARDW;
+ delete IARDH;
+ delete IARDX;
+ delete IARDY;
+ delete IAID;
+ }
+ delete SBREG;
+ return NULL;
+}
+CJBig2_SymbolDict *CJBig2_SDDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
+ JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext)
+{
+ CJBig2_Image **SDNEWSYMS;
+ FX_DWORD HCHEIGHT, NSYMSDECODED;
+ FX_INT32 HCDH;
+ FX_DWORD SYMWIDTH, TOTWIDTH, HCFIRSTSYM;
+ FX_INT32 DW;
+ CJBig2_Image *BS;
+ FX_DWORD I, J, REFAGGNINST;
+ FX_BOOL *EXFLAGS;
+ FX_DWORD EXINDEX;
+ FX_BOOL CUREXFLAG;
+ FX_DWORD EXRUNLENGTH;
+ FX_INT32 nVal;
+ FX_DWORD nTmp;
+ FX_BOOL SBHUFF;
+ FX_DWORD SBNUMSYMS;
+ FX_BYTE SBSYMCODELEN;
+ FX_DWORD IDI;
+ FX_INT32 RDXI, RDYI;
+ CJBig2_Image **SBSYMS;
+ CJBig2_HuffmanTable *SBHUFFFS, *SBHUFFDS, *SBHUFFDT, *SBHUFFRDW, *SBHUFFRDH, *SBHUFFRDX, *SBHUFFRDY,
+ *SBHUFFRSIZE;
+ CJBig2_GRRDProc *pGRRD;
+ CJBig2_GRDProc *pGRD;
+ CJBig2_ArithIntDecoder *IADH, *IADW, *IAAI, *IARDX, *IARDY, *IAEX,
+ *IADT, *IAFS, *IADS, *IAIT, *IARI, *IARDW, *IARDH;
+ CJBig2_ArithIaidDecoder *IAID;
+ CJBig2_SymbolDict *pDict;
+ JBIG2_ALLOC(IADH, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IADW, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAAI, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDX, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDY, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAEX, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IADT, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAFS, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IADS, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IAIT, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARI, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDW, CJBig2_ArithIntDecoder());
+ JBIG2_ALLOC(IARDH, CJBig2_ArithIntDecoder());
+ nTmp = 0;
+ while((FX_DWORD)(1 << nTmp) < (SDNUMINSYMS + SDNUMNEWSYMS)) {
+ nTmp ++;
+ }
+ JBIG2_ALLOC(IAID, CJBig2_ArithIaidDecoder((FX_BYTE)nTmp));
+ SDNEWSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(CJBig2_Image*));
+ FXSYS_memset32(SDNEWSYMS, 0 , SDNUMNEWSYMS * sizeof(CJBig2_Image*));
+ HCHEIGHT = 0;
+ NSYMSDECODED = 0;
+ while(NSYMSDECODED < SDNUMNEWSYMS) {
+ BS = NULL;
+ if(IADH->decode(pArithDecoder, &HCDH) == -1) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ HCHEIGHT = HCHEIGHT + HCDH;
+ if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): invalid HCHEIGHT value.");
+ goto failed;
+ }
+ SYMWIDTH = 0;
+ TOTWIDTH = 0;
+ HCFIRSTSYM = NSYMSDECODED;
+ for(;;) {
+ nVal = IADW->decode(pArithDecoder, &DW);
+ if(nVal == JBIG2_OOB) {
+ break;
+ } else if(nVal != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ } else {
+ if (NSYMSDECODED >= SDNUMNEWSYMS) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): NSYMSDECODED >= SDNUMNEWSYMS.");
+ goto failed;
+ }
+ SYMWIDTH = SYMWIDTH + DW;
+ if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): invalid SYMWIDTH value.");
+ goto failed;
+ } else if (HCHEIGHT == 0 || SYMWIDTH == 0) {
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ SDNEWSYMS[NSYMSDECODED] = NULL;
+ NSYMSDECODED = NSYMSDECODED + 1;
+ continue;
+ }
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ }
+ if(SDREFAGG == 0) {
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = 0;
+ pGRD->GBW = SYMWIDTH;
+ pGRD->GBH = HCHEIGHT;
+ pGRD->GBTEMPLATE = SDTEMPLATE;
+ pGRD->TPGDON = 0;
+ pGRD->USESKIP = 0;
+ pGRD->GBAT[0] = SDAT[0];
+ pGRD->GBAT[1] = SDAT[1];
+ pGRD->GBAT[2] = SDAT[2];
+ pGRD->GBAT[3] = SDAT[3];
+ pGRD->GBAT[4] = SDAT[4];
+ pGRD->GBAT[5] = SDAT[5];
+ pGRD->GBAT[6] = SDAT[6];
+ pGRD->GBAT[7] = SDAT[7];
+ BS = pGRD->decode_Arith(pArithDecoder, gbContext);
+ if(BS == NULL) {
+ delete pGRD;
+ goto failed;
+ }
+ delete pGRD;
+ } else {
+ if(IAAI->decode(pArithDecoder, (int*)&REFAGGNINST) == -1) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ if(REFAGGNINST > 1) {
+ CJBig2_TRDProc *pDecoder;
+ JBIG2_ALLOC(pDecoder, CJBig2_TRDProc());
+ pDecoder->SBHUFF = SDHUFF;
+ pDecoder->SBREFINE = 1;
+ pDecoder->SBW = SYMWIDTH;
+ pDecoder->SBH = HCHEIGHT;
+ pDecoder->SBNUMINSTANCES = REFAGGNINST;
+ pDecoder->SBSTRIPS = 1;
+ pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
+ SBNUMSYMS = pDecoder->SBNUMSYMS;
+ nTmp = 0;
+ while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
+ nTmp ++;
+ }
+ SBSYMCODELEN = (FX_BYTE)nTmp;
+ pDecoder->SBSYMCODELEN = SBSYMCODELEN;
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
+ pDecoder->SBSYMS = SBSYMS;
+ pDecoder->SBDEFPIXEL = 0;
+ pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
+ pDecoder->TRANSPOSED = 0;
+ pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
+ pDecoder->SBDSOFFSET = 0;
+ JBIG2_ALLOC(SBHUFFFS, CJBig2_HuffmanTable(HuffmanTable_B6,
+ sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
+ JBIG2_ALLOC(SBHUFFDS, CJBig2_HuffmanTable(HuffmanTable_B8,
+ sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
+ JBIG2_ALLOC(SBHUFFDT, CJBig2_HuffmanTable(HuffmanTable_B11,
+ sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
+ JBIG2_ALLOC(SBHUFFRDW, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDH, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDY, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ pDecoder->SBHUFFFS = SBHUFFFS;
+ pDecoder->SBHUFFDS = SBHUFFDS;
+ pDecoder->SBHUFFDT = SBHUFFDT;
+ pDecoder->SBHUFFRDW = SBHUFFRDW;
+ pDecoder->SBHUFFRDH = SBHUFFRDH;
+ pDecoder->SBHUFFRDX = SBHUFFRDX;
+ pDecoder->SBHUFFRDY = SBHUFFRDY;
+ pDecoder->SBHUFFRSIZE = SBHUFFRSIZE;
+ pDecoder->SBRTEMPLATE = SDRTEMPLATE;
+ pDecoder->SBRAT[0] = SDRAT[0];
+ pDecoder->SBRAT[1] = SDRAT[1];
+ pDecoder->SBRAT[2] = SDRAT[2];
+ pDecoder->SBRAT[3] = SDRAT[3];
+ JBig2IntDecoderState ids;
+ ids.IADT = IADT;
+ ids.IAFS = IAFS;
+ ids.IADS = IADS;
+ ids.IAIT = IAIT;
+ ids.IARI = IARI;
+ ids.IARDW = IARDW;
+ ids.IARDH = IARDH;
+ ids.IARDX = IARDX;
+ ids.IARDY = IARDY;
+ ids.IAID = IAID;
+ BS = pDecoder->decode_Arith(pArithDecoder, grContext, &ids);
+ if(BS == NULL) {
+ m_pModule->JBig2_Free(SBSYMS);
+ delete SBHUFFFS;
+ delete SBHUFFDS;
+ delete SBHUFFDT;
+ delete SBHUFFRDW;
+ delete SBHUFFRDH;
+ delete SBHUFFRDX;
+ delete SBHUFFRDY;
+ delete SBHUFFRSIZE;
+ delete pDecoder;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(SBSYMS);
+ delete SBHUFFFS;
+ delete SBHUFFDS;
+ delete SBHUFFDT;
+ delete SBHUFFRDW;
+ delete SBHUFFRDH;
+ delete SBHUFFRDX;
+ delete SBHUFFRDY;
+ delete SBHUFFRSIZE;
+ delete pDecoder;
+ } else if(REFAGGNINST == 1) {
+ SBHUFF = SDHUFF;
+ SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
+ if(IAID->decode(pArithDecoder, (int*)&IDI) == -1) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ if((IARDX->decode(pArithDecoder, &RDXI) == -1)
+ || (IARDY->decode(pArithDecoder, &RDYI) == -1)) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ if (IDI >= SBNUMSYMS) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith):"
+ " refinement references unknown symbol %d", IDI);
+ goto failed;
+ }
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
+ if (!SBSYMS[IDI]) {
+ m_pModule->JBig2_Free(SBSYMS);
+ goto failed;
+ }
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ pGRRD->GRW = SYMWIDTH;
+ pGRRD->GRH = HCHEIGHT;
+ pGRRD->GRTEMPLATE = SDRTEMPLATE;
+ pGRRD->GRREFERENCE = SBSYMS[IDI];
+ pGRRD->GRREFERENCEDX = RDXI;
+ pGRRD->GRREFERENCEDY = RDYI;
+ pGRRD->TPGRON = 0;
+ pGRRD->GRAT[0] = SDRAT[0];
+ pGRRD->GRAT[1] = SDRAT[1];
+ pGRRD->GRAT[2] = SDRAT[2];
+ pGRRD->GRAT[3] = SDRAT[3];
+ BS = pGRRD->decode(pArithDecoder, grContext);
+ if(BS == NULL) {
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ }
+ }
+ SDNEWSYMS[NSYMSDECODED] = BS;
+ BS = NULL;
+ NSYMSDECODED = NSYMSDECODED + 1;
+ }
+ }
+ EXINDEX = 0;
+ CUREXFLAG = 0;
+ EXFLAGS = (FX_BOOL*)m_pModule->JBig2_Malloc2(sizeof(FX_BOOL), (SDNUMINSYMS + SDNUMNEWSYMS));
+ while(EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
+ if(IAEX->decode(pArithDecoder, (int*)&EXRUNLENGTH) == -1) {
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): too short.");
+ goto failed;
+ }
+ if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS) {
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): Invalid EXRUNLENGTH value.");
+ goto failed;
+ }
+ if(EXRUNLENGTH != 0) {
+ for(I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
+ EXFLAGS[I] = CUREXFLAG;
+ }
+ }
+ EXINDEX = EXINDEX + EXRUNLENGTH;
+ CUREXFLAG = !CUREXFLAG;
+ }
+ JBIG2_ALLOC(pDict, CJBig2_SymbolDict());
+ pDict->SDNUMEXSYMS = SDNUMEXSYMS;
+ pDict->SDEXSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), SDNUMEXSYMS);
+ I = J = 0;
+ for(I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
+ if(EXFLAGS[I] && J < SDNUMEXSYMS) {
+ if(I < SDNUMINSYMS) {
+ JBIG2_ALLOC(pDict->SDEXSYMS[J], CJBig2_Image(*SDINSYMS[I]));
+ } else {
+ pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS];
+ }
+ J = J + 1;
+ } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) {
+ delete SDNEWSYMS[I - SDNUMINSYMS];
+ }
+ }
+ if (J < SDNUMEXSYMS) {
+ pDict->SDNUMEXSYMS = J;
+ }
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Free(SDNEWSYMS);
+ delete IADH;
+ delete IADW;
+ delete IAAI;
+ delete IARDX;
+ delete IARDY;
+ delete IAEX;
+ delete IAID;
+ delete IADT;
+ delete IAFS;
+ delete IADS;
+ delete IAIT;
+ delete IARI;
+ delete IARDW;
+ delete IARDH;
+ return pDict;
+failed:
+ for(I = 0; I < NSYMSDECODED; I++) {
+ if (SDNEWSYMS[I]) {
+ delete SDNEWSYMS[I];
+ SDNEWSYMS[I] = NULL;
+ }
+ }
+ m_pModule->JBig2_Free(SDNEWSYMS);
+ delete IADH;
+ delete IADW;
+ delete IAAI;
+ delete IARDX;
+ delete IARDY;
+ delete IAEX;
+ delete IAID;
+ delete IADT;
+ delete IAFS;
+ delete IADS;
+ delete IAIT;
+ delete IARI;
+ delete IARDW;
+ delete IARDH;
+ return NULL;
+}
+CJBig2_SymbolDict *CJBig2_SDDProc::decode_Huffman(CJBig2_BitStream *pStream,
+ JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext, IFX_Pause* pPause)
+{
+ CJBig2_Image **SDNEWSYMS;
+ FX_DWORD *SDNEWSYMWIDTHS;
+ FX_DWORD HCHEIGHT, NSYMSDECODED;
+ FX_INT32 HCDH;
+ FX_DWORD SYMWIDTH, TOTWIDTH, HCFIRSTSYM;
+ FX_INT32 DW;
+ CJBig2_Image *BS, *BHC;
+ FX_DWORD I, J, REFAGGNINST;
+ FX_BOOL *EXFLAGS;
+ FX_DWORD EXINDEX;
+ FX_BOOL CUREXFLAG;
+ FX_DWORD EXRUNLENGTH;
+ FX_INT32 nVal, nBits;
+ FX_DWORD nTmp;
+ FX_BOOL SBHUFF;
+ FX_DWORD SBNUMSYMS;
+ FX_BYTE SBSYMCODELEN;
+ JBig2HuffmanCode *SBSYMCODES;
+ FX_DWORD IDI;
+ FX_INT32 RDXI, RDYI;
+ FX_DWORD BMSIZE;
+ FX_DWORD stride;
+ CJBig2_Image **SBSYMS;
+ CJBig2_HuffmanTable *SBHUFFFS, *SBHUFFDS, *SBHUFFDT, *SBHUFFRDW, *SBHUFFRDH, *SBHUFFRDX, *SBHUFFRDY,
+ *SBHUFFRSIZE, *pTable;
+ CJBig2_HuffmanDecoder *pHuffmanDecoder;
+ CJBig2_GRRDProc *pGRRD;
+ CJBig2_ArithDecoder *pArithDecoder;
+ CJBig2_GRDProc *pGRD;
+ CJBig2_SymbolDict *pDict;
+ JBIG2_ALLOC(pHuffmanDecoder, CJBig2_HuffmanDecoder(pStream));
+ SDNEWSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(CJBig2_Image*));
+ FXSYS_memset32(SDNEWSYMS, 0 , SDNUMNEWSYMS * sizeof(CJBig2_Image*));
+ SDNEWSYMWIDTHS = NULL;
+ BHC = NULL;
+ if(SDREFAGG == 0) {
+ SDNEWSYMWIDTHS = (FX_DWORD *)m_pModule->JBig2_Malloc2(SDNUMNEWSYMS, sizeof(FX_DWORD));
+ FXSYS_memset32(SDNEWSYMWIDTHS, 0 , SDNUMNEWSYMS * sizeof(FX_DWORD));
+ }
+ HCHEIGHT = 0;
+ NSYMSDECODED = 0;
+ BS = NULL;
+ while(NSYMSDECODED < SDNUMNEWSYMS) {
+ if(pHuffmanDecoder->decodeAValue(SDHUFFDH, &HCDH) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ HCHEIGHT = HCHEIGHT + HCDH;
+ if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): invalid HCHEIGHT value.");
+ goto failed;
+ }
+ SYMWIDTH = 0;
+ TOTWIDTH = 0;
+ HCFIRSTSYM = NSYMSDECODED;
+ for(;;) {
+ nVal = pHuffmanDecoder->decodeAValue(SDHUFFDW, &DW);
+ if(nVal == JBIG2_OOB) {
+ break;
+ } else if(nVal != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ } else {
+ if (NSYMSDECODED >= SDNUMNEWSYMS) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): NSYMSDECODED >= SDNUMNEWSYMS.");
+ goto failed;
+ }
+ SYMWIDTH = SYMWIDTH + DW;
+ if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): invalid SYMWIDTH value.");
+ goto failed;
+ } else if (HCHEIGHT == 0 || SYMWIDTH == 0) {
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ SDNEWSYMS[NSYMSDECODED] = NULL;
+ NSYMSDECODED = NSYMSDECODED + 1;
+ continue;
+ }
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ }
+ if(SDREFAGG == 1) {
+ if(pHuffmanDecoder->decodeAValue(SDHUFFAGGINST, (int*)&REFAGGNINST) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ BS = NULL;
+ if(REFAGGNINST > 1) {
+ CJBig2_TRDProc *pDecoder;
+ JBIG2_ALLOC(pDecoder, CJBig2_TRDProc());
+ pDecoder->SBHUFF = SDHUFF;
+ pDecoder->SBREFINE = 1;
+ pDecoder->SBW = SYMWIDTH;
+ pDecoder->SBH = HCHEIGHT;
+ pDecoder->SBNUMINSTANCES = REFAGGNINST;
+ pDecoder->SBSTRIPS = 1;
+ pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED;
+ SBNUMSYMS = pDecoder->SBNUMSYMS;
+ SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(JBig2HuffmanCode));
+ nTmp = 1;
+ while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
+ nTmp ++;
+ }
+ for(I = 0; I < SBNUMSYMS; I++) {
+ SBSYMCODES[I].codelen = nTmp;
+ SBSYMCODES[I].code = I;
+ }
+ pDecoder->SBSYMCODES = SBSYMCODES;
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
+ pDecoder->SBSYMS = SBSYMS;
+ pDecoder->SBDEFPIXEL = 0;
+ pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR;
+ pDecoder->TRANSPOSED = 0;
+ pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT;
+ pDecoder->SBDSOFFSET = 0;
+ JBIG2_ALLOC(SBHUFFFS, CJBig2_HuffmanTable(HuffmanTable_B6,
+ sizeof(HuffmanTable_B6) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B6));
+ JBIG2_ALLOC(SBHUFFDS, CJBig2_HuffmanTable(HuffmanTable_B8,
+ sizeof(HuffmanTable_B8) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B8));
+ JBIG2_ALLOC(SBHUFFDT, CJBig2_HuffmanTable(HuffmanTable_B11,
+ sizeof(HuffmanTable_B11) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B11));
+ JBIG2_ALLOC(SBHUFFRDW, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDH, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRDY, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ pDecoder->SBHUFFFS = SBHUFFFS;
+ pDecoder->SBHUFFDS = SBHUFFDS;
+ pDecoder->SBHUFFDT = SBHUFFDT;
+ pDecoder->SBHUFFRDW = SBHUFFRDW;
+ pDecoder->SBHUFFRDH = SBHUFFRDH;
+ pDecoder->SBHUFFRDX = SBHUFFRDX;
+ pDecoder->SBHUFFRDY = SBHUFFRDY;
+ pDecoder->SBHUFFRSIZE = SBHUFFRSIZE;
+ pDecoder->SBRTEMPLATE = SDRTEMPLATE;
+ pDecoder->SBRAT[0] = SDRAT[0];
+ pDecoder->SBRAT[1] = SDRAT[1];
+ pDecoder->SBRAT[2] = SDRAT[2];
+ pDecoder->SBRAT[3] = SDRAT[3];
+ BS = pDecoder->decode_Huffman(pStream, grContext);
+ if(BS == NULL) {
+ m_pModule->JBig2_Free(SBSYMCODES);
+ m_pModule->JBig2_Free(SBSYMS);
+ delete SBHUFFFS;
+ delete SBHUFFDS;
+ delete SBHUFFDT;
+ delete SBHUFFRDW;
+ delete SBHUFFRDH;
+ delete SBHUFFRDX;
+ delete SBHUFFRDY;
+ delete SBHUFFRSIZE;
+ delete pDecoder;
+ goto failed;
+ }
+ m_pModule->JBig2_Free(SBSYMCODES);
+ m_pModule->JBig2_Free(SBSYMS);
+ delete SBHUFFFS;
+ delete SBHUFFDS;
+ delete SBHUFFDT;
+ delete SBHUFFRDW;
+ delete SBHUFFRDH;
+ delete SBHUFFRDX;
+ delete SBHUFFRDY;
+ delete SBHUFFRSIZE;
+ delete pDecoder;
+ } else if(REFAGGNINST == 1) {
+ SBHUFF = SDHUFF;
+ SBNUMSYMS = SDNUMINSYMS + SDNUMNEWSYMS;
+ nTmp = 1;
+ while((FX_DWORD)(1 << nTmp) < SBNUMSYMS) {
+ nTmp ++;
+ }
+ SBSYMCODELEN = (FX_BYTE)nTmp;
+ SBSYMCODES = (JBig2HuffmanCode*)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(JBig2HuffmanCode));
+ for(I = 0; I < SBNUMSYMS; I++) {
+ SBSYMCODES[I].codelen = SBSYMCODELEN;
+ SBSYMCODES[I].code = I;
+ }
+ nVal = 0;
+ nBits = 0;
+ for(;;) {
+ if(pStream->read1Bit(&nTmp) != 0) {
+ m_pModule->JBig2_Free(SBSYMCODES);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ nVal = (nVal << 1) | nTmp;
+ for(IDI = 0; IDI < SBNUMSYMS; IDI++) {
+ if((nVal == SBSYMCODES[IDI].code)
+ && (nBits == SBSYMCODES[IDI].codelen)) {
+ break;
+ }
+ }
+ if(IDI < SBNUMSYMS) {
+ break;
+ }
+ }
+ m_pModule->JBig2_Free(SBSYMCODES);
+ JBIG2_ALLOC(SBHUFFRDX, CJBig2_HuffmanTable(HuffmanTable_B15,
+ sizeof(HuffmanTable_B15) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B15));
+ JBIG2_ALLOC(SBHUFFRSIZE, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ if((pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDXI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRDX, &RDYI) != 0)
+ || (pHuffmanDecoder->decodeAValue(SBHUFFRSIZE, &nVal) != 0)) {
+ delete SBHUFFRDX;
+ delete SBHUFFRSIZE;
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ delete SBHUFFRDX;
+ delete SBHUFFRSIZE;
+ pStream->alignByte();
+ nTmp = pStream->getOffset();
+ SBSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(SBNUMSYMS, sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*));
+ JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*));
+ JBIG2_ALLOC(pGRRD, CJBig2_GRRDProc());
+ pGRRD->GRW = SYMWIDTH;
+ pGRRD->GRH = HCHEIGHT;
+ pGRRD->GRTEMPLATE = SDRTEMPLATE;
+ pGRRD->GRREFERENCE = SBSYMS[IDI];
+ pGRRD->GRREFERENCEDX = RDXI;
+ pGRRD->GRREFERENCEDY = RDYI;
+ pGRRD->TPGRON = 0;
+ pGRRD->GRAT[0] = SDRAT[0];
+ pGRRD->GRAT[1] = SDRAT[1];
+ pGRRD->GRAT[2] = SDRAT[2];
+ pGRRD->GRAT[3] = SDRAT[3];
+ JBIG2_ALLOC(pArithDecoder, CJBig2_ArithDecoder(pStream));
+ BS = pGRRD->decode(pArithDecoder, grContext);
+ if(BS == NULL) {
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ delete pArithDecoder;
+ goto failed;
+ }
+ pStream->alignByte();
+ pStream->offset(2);
+ if((FX_DWORD)nVal != (pStream->getOffset() - nTmp)) {
+ delete BS;
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ delete pArithDecoder;
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman):"
+ "bytes processed by generic refinement region decoding procedure doesn't equal SBHUFFRSIZE.");
+ goto failed;
+ }
+ m_pModule->JBig2_Free(SBSYMS);
+ delete pGRRD;
+ delete pArithDecoder;
+ }
+ SDNEWSYMS[NSYMSDECODED] = BS;
+ }
+ if(SDREFAGG == 0) {
+ SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
+ }
+ NSYMSDECODED = NSYMSDECODED + 1;
+ }
+ if(SDREFAGG == 0) {
+ if(pHuffmanDecoder->decodeAValue(SDHUFFBMSIZE, (FX_INT32*)&BMSIZE) != 0) {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ pStream->alignByte();
+ if(BMSIZE == 0) {
+ stride = (TOTWIDTH + 7) >> 3;
+ if(pStream->getByteLeft() >= stride * HCHEIGHT) {
+ JBIG2_ALLOC(BHC, CJBig2_Image(TOTWIDTH, HCHEIGHT));
+ for(I = 0; I < HCHEIGHT; I ++) {
+ JBIG2_memcpy(BHC->m_pData + I * BHC->m_nStride, pStream->getPointer(), stride);
+ pStream->offset(stride);
+ }
+ } else {
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ } else {
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = 1;
+ pGRD->GBW = TOTWIDTH;
+ pGRD->GBH = HCHEIGHT;
+ FXCODEC_STATUS status = pGRD->Start_decode_MMR(&BHC, pStream);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ delete pGRD;
+ pStream->alignByte();
+ }
+ nTmp = 0;
+ if (!BHC) {
+ continue;
+ }
+ for(I = HCFIRSTSYM; I < NSYMSDECODED; I++) {
+ SDNEWSYMS[I] = BHC->subImage(nTmp, 0, SDNEWSYMWIDTHS[I], HCHEIGHT);
+ nTmp += SDNEWSYMWIDTHS[I];
+ }
+ delete BHC;
+ BHC = NULL;
+ }
+ }
+ EXINDEX = 0;
+ CUREXFLAG = 0;
+ JBIG2_ALLOC(pTable, CJBig2_HuffmanTable(HuffmanTable_B1,
+ sizeof(HuffmanTable_B1) / sizeof(JBig2TableLine), HuffmanTable_HTOOB_B1));
+ EXFLAGS = (FX_BOOL*)m_pModule->JBig2_Malloc2(sizeof(FX_BOOL), (SDNUMINSYMS + SDNUMNEWSYMS));
+ while(EXINDEX < SDNUMINSYMS + SDNUMNEWSYMS) {
+ if(pHuffmanDecoder->decodeAValue(pTable, (int*)&EXRUNLENGTH) != 0) {
+ delete pTable;
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (huffman): too short.");
+ goto failed;
+ }
+ if (EXINDEX + EXRUNLENGTH > SDNUMINSYMS + SDNUMNEWSYMS) {
+ delete pTable;
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Error("symbol dictionary decoding procedure (arith): Invalid EXRUNLENGTH value.");
+ goto failed;
+ }
+ if(EXRUNLENGTH != 0) {
+ for(I = EXINDEX; I < EXINDEX + EXRUNLENGTH; I++) {
+ EXFLAGS[I] = CUREXFLAG;
+ }
+ }
+ EXINDEX = EXINDEX + EXRUNLENGTH;
+ CUREXFLAG = !CUREXFLAG;
+ }
+ delete pTable;
+ JBIG2_ALLOC(pDict, CJBig2_SymbolDict());
+ pDict->SDNUMEXSYMS = SDNUMEXSYMS;
+ pDict->SDEXSYMS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), SDNUMEXSYMS);
+ I = J = 0;
+ for(I = 0; I < SDNUMINSYMS + SDNUMNEWSYMS; I++) {
+ if(EXFLAGS[I] && J < SDNUMEXSYMS) {
+ if(I < SDNUMINSYMS) {
+ JBIG2_ALLOC(pDict->SDEXSYMS[J], CJBig2_Image(*SDINSYMS[I]));
+ } else {
+ pDict->SDEXSYMS[J] = SDNEWSYMS[I - SDNUMINSYMS];
+ }
+ J = J + 1;
+ } else if (!EXFLAGS[I] && I >= SDNUMINSYMS) {
+ delete SDNEWSYMS[I - SDNUMINSYMS];
+ }
+ }
+ if (J < SDNUMEXSYMS) {
+ pDict->SDNUMEXSYMS = J;
+ }
+ m_pModule->JBig2_Free(EXFLAGS);
+ m_pModule->JBig2_Free(SDNEWSYMS);
+ if(SDREFAGG == 0) {
+ m_pModule->JBig2_Free(SDNEWSYMWIDTHS);
+ }
+ delete pHuffmanDecoder;
+ return pDict;
+failed:
+ for(I = 0; I < NSYMSDECODED; I++) {
+ if (SDNEWSYMS[I]) {
+ delete SDNEWSYMS[I];
+ }
+ }
+ m_pModule->JBig2_Free(SDNEWSYMS);
+ if(SDREFAGG == 0) {
+ m_pModule->JBig2_Free(SDNEWSYMWIDTHS);
+ }
+ delete pHuffmanDecoder;
+ return NULL;
+}
+CJBig2_Image *CJBig2_HTRDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
+ JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_DWORD ng, mg;
+ FX_INT32 x, y;
+ CJBig2_Image *HSKIP;
+ FX_DWORD HBPP;
+ FX_DWORD *GI;
+ CJBig2_Image *HTREG;
+ CJBig2_GSIDProc *pGID;
+ JBIG2_ALLOC(HTREG, CJBig2_Image(HBW, HBH));
+ HTREG->fill(HDEFPIXEL);
+ HSKIP = NULL;
+ if(HENABLESKIP == 1) {
+ JBIG2_ALLOC(HSKIP, CJBig2_Image(HGW, HGH));
+ for(mg = 0; mg < HGH; mg++) {
+ for(ng = 0; ng < HGW; ng++) {
+ x = (HGX + mg * HRY + ng * HRX) >> 8;
+ y = (HGY + mg * HRX - ng * HRY) >> 8;
+ if((x + HPW <= 0) | (x >= (FX_INT32)HBW)
+ | (y + HPH <= 0) | (y >= (FX_INT32)HPH)) {
+ HSKIP->setPixel(ng, mg, 1);
+ } else {
+ HSKIP->setPixel(ng, mg, 0);
+ }
+ }
+ }
+ }
+ HBPP = 1;
+ while((FX_DWORD)(1 << HBPP) < HNUMPATS) {
+ HBPP ++;
+ }
+ JBIG2_ALLOC(pGID, CJBig2_GSIDProc());
+ pGID->GSMMR = HMMR;
+ pGID->GSW = HGW;
+ pGID->GSH = HGH;
+ pGID->GSBPP = (FX_BYTE)HBPP;
+ pGID->GSUSESKIP = HENABLESKIP;
+ pGID->GSKIP = HSKIP;
+ pGID->GSTEMPLATE = HTEMPLATE;
+ GI = pGID->decode_Arith(pArithDecoder, gbContext, pPause);
+ if(GI == NULL) {
+ goto failed;
+ }
+ for(mg = 0; mg < HGH; mg++) {
+ for(ng = 0; ng < HGW; ng++) {
+ x = (HGX + mg * HRY + ng * HRX) >> 8;
+ y = (HGY + mg * HRX - ng * HRY) >> 8;
+ FX_DWORD pat_index = GI[mg * HGW + ng];
+ if (pat_index >= HNUMPATS) {
+ pat_index = HNUMPATS - 1;
+ }
+ HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
+ }
+ }
+ m_pModule->JBig2_Free(GI);
+ if(HSKIP) {
+ delete HSKIP;
+ }
+ delete pGID;
+ return HTREG;
+failed:
+ if(HSKIP) {
+ delete HSKIP;
+ }
+ delete pGID;
+ delete HTREG;
+ return NULL;
+}
+CJBig2_Image *CJBig2_HTRDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
+{
+ FX_DWORD ng, mg;
+ FX_INT32 x, y;
+ FX_DWORD HBPP;
+ FX_DWORD *GI;
+ CJBig2_Image *HTREG;
+ CJBig2_GSIDProc *pGID;
+ JBIG2_ALLOC(HTREG, CJBig2_Image(HBW, HBH));
+ HTREG->fill(HDEFPIXEL);
+ HBPP = 1;
+ while((FX_DWORD)(1 << HBPP) < HNUMPATS) {
+ HBPP ++;
+ }
+ JBIG2_ALLOC(pGID, CJBig2_GSIDProc());
+ pGID->GSMMR = HMMR;
+ pGID->GSW = HGW;
+ pGID->GSH = HGH;
+ pGID->GSBPP = (FX_BYTE)HBPP;
+ pGID->GSUSESKIP = 0;
+ GI = pGID->decode_MMR(pStream, pPause);
+ if(GI == NULL) {
+ goto failed;
+ }
+ for(mg = 0; mg < HGH; mg++) {
+ for(ng = 0; ng < HGW; ng++) {
+ x = (HGX + mg * HRY + ng * HRX) >> 8;
+ y = (HGY + mg * HRX - ng * HRY) >> 8;
+ FX_DWORD pat_index = GI[mg * HGW + ng];
+ if (pat_index >= HNUMPATS) {
+ pat_index = HNUMPATS - 1;
+ }
+ HTREG->composeFrom(x, y, HPATS[pat_index], HCOMBOP);
+ }
+ }
+ m_pModule->JBig2_Free(GI);
+ delete pGID;
+ return HTREG;
+failed:
+ delete pGID;
+ delete HTREG;
+ return NULL;
+}
+CJBig2_PatternDict *CJBig2_PDDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
+ JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_DWORD GRAY;
+ CJBig2_Image *BHDC = NULL;
+ CJBig2_PatternDict *pDict;
+ CJBig2_GRDProc *pGRD;
+ JBIG2_ALLOC(pDict, CJBig2_PatternDict());
+ pDict->NUMPATS = GRAYMAX + 1;
+ pDict->HDPATS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), pDict->NUMPATS);
+ JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*)*pDict->NUMPATS);
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = HDMMR;
+ pGRD->GBW = (GRAYMAX + 1) * HDPW;
+ pGRD->GBH = HDPH;
+ pGRD->GBTEMPLATE = HDTEMPLATE;
+ pGRD->TPGDON = 0;
+ pGRD->USESKIP = 0;
+ pGRD->GBAT[0] = -(FX_INT32)HDPW;
+ pGRD->GBAT[1] = 0;
+ if(pGRD->GBTEMPLATE == 0) {
+ pGRD->GBAT[2] = -3;
+ pGRD->GBAT[3] = -1;
+ pGRD->GBAT[4] = 2;
+ pGRD->GBAT[5] = -2;
+ pGRD->GBAT[6] = -2;
+ pGRD->GBAT[7] = -2;
+ }
+ FXCODEC_STATUS status = pGRD->Start_decode_Arith(&BHDC, pArithDecoder, gbContext);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(BHDC == NULL) {
+ delete pGRD;
+ goto failed;
+ }
+ delete pGRD;
+ GRAY = 0;
+ while(GRAY <= GRAYMAX) {
+ pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
+ GRAY = GRAY + 1;
+ }
+ delete BHDC;
+ return pDict;
+failed:
+ delete pDict;
+ return NULL;
+}
+
+CJBig2_PatternDict *CJBig2_PDDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
+{
+ FX_DWORD GRAY;
+ CJBig2_Image *BHDC = NULL;
+ CJBig2_PatternDict *pDict;
+ CJBig2_GRDProc *pGRD;
+ JBIG2_ALLOC(pDict, CJBig2_PatternDict());
+ pDict->NUMPATS = GRAYMAX + 1;
+ pDict->HDPATS = (CJBig2_Image**)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), pDict->NUMPATS);
+ JBIG2_memset(pDict->HDPATS, 0, sizeof(CJBig2_Image*)*pDict->NUMPATS);
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = HDMMR;
+ pGRD->GBW = (GRAYMAX + 1) * HDPW;
+ pGRD->GBH = HDPH;
+ FXCODEC_STATUS status = pGRD->Start_decode_MMR(&BHDC, pStream);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(BHDC == NULL) {
+ delete pGRD;
+ goto failed;
+ }
+ delete pGRD;
+ GRAY = 0;
+ while(GRAY <= GRAYMAX) {
+ pDict->HDPATS[GRAY] = BHDC->subImage(HDPW * GRAY, 0, HDPW, HDPH);
+ GRAY = GRAY + 1;
+ }
+ delete BHDC;
+ return pDict;
+failed:
+ delete pDict;
+ return NULL;
+}
+FX_DWORD *CJBig2_GSIDProc::decode_Arith(CJBig2_ArithDecoder *pArithDecoder,
+ JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ CJBig2_Image **GSPLANES;
+ FX_INT32 J, K;
+ FX_DWORD x, y;
+ FX_DWORD *GSVALS;
+ CJBig2_GRDProc *pGRD;
+ GSPLANES = (CJBig2_Image **)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), GSBPP);
+ if (!GSPLANES) {
+ return NULL;
+ }
+ GSVALS = (FX_DWORD*)m_pModule->JBig2_Malloc3(sizeof(FX_DWORD), GSW, GSH);
+ if (!GSVALS) {
+ m_pModule->JBig2_Free(GSPLANES);
+ return NULL;
+ }
+ JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*)*GSBPP);
+ JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD)*GSW * GSH);
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = GSMMR;
+ pGRD->GBW = GSW;
+ pGRD->GBH = GSH;
+ pGRD->GBTEMPLATE = GSTEMPLATE;
+ pGRD->TPGDON = 0;
+ pGRD->USESKIP = GSUSESKIP;
+ pGRD->SKIP = GSKIP;
+ if(GSTEMPLATE <= 1) {
+ pGRD->GBAT[0] = 3;
+ } else {
+ pGRD->GBAT[0] = 2;
+ }
+ pGRD->GBAT[1] = -1;
+ if(pGRD->GBTEMPLATE == 0) {
+ pGRD->GBAT[2] = -3;
+ pGRD->GBAT[3] = -1;
+ pGRD->GBAT[4] = 2;
+ pGRD->GBAT[5] = -2;
+ pGRD->GBAT[6] = -2;
+ pGRD->GBAT[7] = -2;
+ }
+ FXCODEC_STATUS status = pGRD->Start_decode_Arith(&GSPLANES[GSBPP - 1], pArithDecoder, gbContext);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(GSPLANES[GSBPP - 1] == NULL) {
+ goto failed;
+ }
+ J = GSBPP - 2;
+ while(J >= 0) {
+ FXCODEC_STATUS status = pGRD->Start_decode_Arith(&GSPLANES[J], pArithDecoder, gbContext);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(GSPLANES[J] == NULL) {
+ for(K = GSBPP - 1; K > J; K--) {
+ delete GSPLANES[K];
+ goto failed;
+ }
+ }
+ GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR);
+ J = J - 1;
+ }
+ for(y = 0; y < GSH; y++) {
+ for(x = 0; x < GSW; x++) {
+ for(J = 0; J < GSBPP; J++) {
+ GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J;
+ }
+ }
+ }
+ for(J = 0; J < GSBPP; J++) {
+ delete GSPLANES[J];
+ }
+ m_pModule->JBig2_Free(GSPLANES);
+ delete pGRD;
+ return GSVALS;
+failed:
+ m_pModule->JBig2_Free(GSPLANES);
+ delete pGRD;
+ m_pModule->JBig2_Free(GSVALS);
+ return NULL;
+}
+FX_DWORD *CJBig2_GSIDProc::decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause)
+{
+ CJBig2_Image **GSPLANES;
+ FX_INT32 J, K;
+ FX_DWORD x, y;
+ FX_DWORD *GSVALS;
+ CJBig2_GRDProc *pGRD;
+ GSPLANES = (CJBig2_Image **)m_pModule->JBig2_Malloc2(sizeof(CJBig2_Image*), GSBPP);
+ if (!GSPLANES) {
+ return NULL;
+ }
+ GSVALS = (FX_DWORD*)m_pModule->JBig2_Malloc3(sizeof(FX_DWORD), GSW, GSH);
+ if (!GSVALS) {
+ if (GSPLANES) {
+ m_pModule->JBig2_Free(GSPLANES);
+ }
+ return NULL;
+ }
+ JBIG2_memset(GSPLANES, 0, sizeof(CJBig2_Image*)*GSBPP);
+ JBIG2_memset(GSVALS, 0, sizeof(FX_DWORD)*GSW * GSH);
+ JBIG2_ALLOC(pGRD, CJBig2_GRDProc());
+ pGRD->MMR = GSMMR;
+ pGRD->GBW = GSW;
+ pGRD->GBH = GSH;
+ FXCODEC_STATUS status = pGRD->Start_decode_MMR(&GSPLANES[GSBPP - 1], pStream);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(GSPLANES[GSBPP - 1] == NULL) {
+ goto failed;
+ }
+ pStream->alignByte();
+ pStream->offset(3);
+ J = GSBPP - 2;
+ while(J >= 0) {
+ FXCODEC_STATUS status = pGRD->Start_decode_MMR(&GSPLANES[J], pStream);
+ while(status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ pGRD->Continue_decode(pPause);
+ }
+ if(GSPLANES[J] == NULL) {
+ for(K = GSBPP - 1; K > J; K--) {
+ delete GSPLANES[K];
+ goto failed;
+ }
+ }
+ pStream->alignByte();
+ pStream->offset(3);
+ GSPLANES[J]->composeFrom(0, 0, GSPLANES[J + 1], JBIG2_COMPOSE_XOR);
+ J = J - 1;
+ }
+ for(y = 0; y < GSH; y++) {
+ for(x = 0; x < GSW; x++) {
+ for(J = 0; J < GSBPP; J++) {
+ GSVALS[y * GSW + x] |= GSPLANES[J]->getPixel(x, y) << J;
+ }
+ }
+ }
+ for(J = 0; J < GSBPP; J++) {
+ delete GSPLANES[J];
+ }
+ m_pModule->JBig2_Free(GSPLANES);
+ delete pGRD;
+ return GSVALS;
+failed:
+ m_pModule->JBig2_Free(GSPLANES);
+ delete pGRD;
+ m_pModule->JBig2_Free(GSVALS);
+ return NULL;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ if (GBW == 0 || GBH == 0) {
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
+ m_pPause = pPause;
+ if(*pImage == NULL) {
+ JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
+ }
+ if ((*pImage)->m_pData == NULL) {
+ delete *pImage;
+ *pImage = NULL;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return FXCODEC_STATUS_ERROR;
+ }
+ m_DecodeType = 1;
+ m_pImage = pImage;
+ (*m_pImage)->fill(0);
+ m_pArithDecoder = pArithDecoder;
+ m_gbContext = gbContext;
+ LTP = 0;
+ m_pLine = NULL;
+ m_loopIndex = 0;
+ return decode_Arith(pPause);
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith(IFX_Pause* pPause)
+{
+ int iline = m_loopIndex;
+ CJBig2_Image* pImage = *m_pImage;
+ if(GBTEMPLATE == 0) {
+ if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)
+ && (GBAT[2] == (signed char) - 3) && (GBAT[3] == (signed char) - 1)
+ && (GBAT[4] == 2) && (GBAT[5] == (signed char) - 2)
+ && (GBAT[6] == (signed char) - 2) && (GBAT[7] == (signed char) - 2)) {
+ m_ProssiveStatus = decode_Arith_Template0_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProssiveStatus = decode_Arith_Template0_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
+ }
+ } else if(GBTEMPLATE == 1) {
+ if((GBAT[0] == 3) && (GBAT[1] == (signed char) - 1)) {
+ m_ProssiveStatus = decode_Arith_Template1_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProssiveStatus = decode_Arith_Template1_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
+ }
+ } else if(GBTEMPLATE == 2) {
+ if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
+ m_ProssiveStatus = decode_Arith_Template2_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProssiveStatus = decode_Arith_Template2_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
+ }
+ } else {
+ if((GBAT[0] == 2) && (GBAT[1] == (signed char) - 1)) {
+ m_ProssiveStatus = decode_Arith_Template3_opt3(pImage, m_pArithDecoder, m_gbContext, pPause);
+ } else {
+ m_ProssiveStatus = decode_Arith_Template3_unopt(pImage, m_pArithDecoder, m_gbContext, pPause);
+ }
+ }
+ m_ReplaceRect.left = 0;
+ m_ReplaceRect.right = pImage->m_nWidth;
+ m_ReplaceRect.top = iline;
+ m_ReplaceRect.bottom = m_loopIndex;
+ if(m_ProssiveStatus == FXCODEC_STATUS_DECODE_FINISH) {
+ m_loopIndex = 0;
+ }
+ return m_ProssiveStatus;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith_V2(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ if(GBW == 0 || GBH == 0) {
+ * pImage = NULL;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+ }
+ if(*pImage == NULL) {
+ JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
+ }
+ if ((*pImage)->m_pData == NULL) {
+ delete *pImage;
+ *pImage = NULL;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return FXCODEC_STATUS_ERROR;
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
+ m_DecodeType = 2;
+ m_pPause = pPause;
+ m_pImage = pImage;
+ (*m_pImage)->fill(0);
+ LTP = 0;
+ m_loopIndex = 0;
+ m_pArithDecoder = pArithDecoder;
+ m_gbContext = gbContext;
+ return decode_Arith_V2(pPause);
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_V2(IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ CJBig2_Image *GBREG = *m_pImage;
+ FX_DWORD line1, line2, line3;
+ LTP = 0;
+ JBIG2_ALLOC(GBREG, CJBig2_Image(GBW, GBH));
+ GBREG->fill(0);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT = 0x9b25;
+ break;
+ case 1:
+ CONTEXT = 0x0795;
+ break;
+ case 2:
+ CONTEXT = 0x00e5;
+ break;
+ case 3:
+ CONTEXT = 0x0195;
+ break;
+ }
+ SLTP = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ GBREG->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ switch(GBTEMPLATE) {
+ case 0: {
+ line1 = GBREG->getPixel(1, m_loopIndex - 2);
+ line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 1;
+ line2 = GBREG->getPixel(2, m_loopIndex - 1);
+ line2 |= GBREG->getPixel(1, m_loopIndex - 1) << 1;
+ line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= line2 << 5;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
+ CONTEXT |= line1 << 12;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ break;
+ case 1: {
+ line1 = GBREG->getPixel(2, m_loopIndex - 2);
+ line1 |= GBREG->getPixel(1, m_loopIndex - 2) << 1;
+ line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 2;
+ line2 = GBREG->getPixel(2, m_loopIndex - 1);
+ line2 |= GBREG->getPixel(1, m_loopIndex - 1) << 1;
+ line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 9;
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 2)) & 0x0f;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ break;
+ case 2: {
+ line1 = GBREG->getPixel(1, m_loopIndex - 2);
+ line1 |= GBREG->getPixel(0, m_loopIndex - 2) << 1;
+ line2 = GBREG->getPixel(1, m_loopIndex - 1);
+ line2 |= GBREG->getPixel(0, m_loopIndex - 1) << 1;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 7;
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
+ line2 = ((line2 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 1)) & 0x0f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ break;
+ case 3: {
+ line1 = GBREG->getPixel(1, m_loopIndex - 1);
+ line1 |= GBREG->getPixel(0, m_loopIndex - 1) << 1;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= line1 << 5;
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | GBREG->getPixel(w + 2, m_loopIndex - 1)) & 0x1f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ break;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex ++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Start_decode_Arith_V1(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ if(GBW == 0 || GBH == 0) {
+ * pImage = NULL;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+ }
+ if(*pImage == NULL) {
+ JBIG2_ALLOC((*pImage), CJBig2_Image(GBW, GBH));
+ }
+ if ((*pImage)->m_pData == NULL) {
+ delete *pImage;
+ *pImage = NULL;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return FXCODEC_STATUS_ERROR;
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_READY;
+ m_pPause = pPause;
+ m_pImage = pImage;
+ m_DecodeType = 3;
+ (*m_pImage)->fill(0);
+ LTP = 0;
+ m_loopIndex = 0;
+ m_pArithDecoder = pArithDecoder;
+ m_gbContext = gbContext;
+ return decode_Arith_V1(pPause);
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_V1(IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT = 0;
+ CJBig2_Image *GBREG = (*m_pImage);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT = 0x9b25;
+ break;
+ case 1:
+ CONTEXT = 0x0795;
+ break;
+ case 2:
+ CONTEXT = 0x00e5;
+ break;
+ case 3:
+ CONTEXT = 0x0195;
+ break;
+ }
+ SLTP = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ GBREG->setPixel(w, m_loopIndex, GBREG->getPixel(w, m_loopIndex - 1));
+ }
+ } else {
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ GBREG->setPixel(w, m_loopIndex, 0);
+ } else {
+ CONTEXT = 0;
+ switch(GBTEMPLATE) {
+ case 0:
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
+ CONTEXT |= GBREG->getPixel(w - 4, m_loopIndex) << 3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 9;
+ CONTEXT |= GBREG->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
+ CONTEXT |= GBREG->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 12;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 13;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 14;
+ CONTEXT |= GBREG->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
+ break;
+ case 1:
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 3;
+ CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 1) << 4;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w + 2, m_loopIndex - 2) << 9;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 10;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 11;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 12;
+ break;
+ case 2:
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 3;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 4;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 2) << 7;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 2) << 8;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 2) << 9;
+ break;
+ case 3:
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex);
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex) << 1;
+ CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex) << 2;
+ CONTEXT |= GBREG->getPixel(w - 4, m_loopIndex) << 3;
+ CONTEXT |= GBREG->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= GBREG->getPixel(w + 1, m_loopIndex - 1) << 5;
+ CONTEXT |= GBREG->getPixel(w, m_loopIndex - 1) << 6;
+ CONTEXT |= GBREG->getPixel(w - 1, m_loopIndex - 1) << 7;
+ CONTEXT |= GBREG->getPixel(w - 2, m_loopIndex - 1) << 8;
+ CONTEXT |= GBREG->getPixel(w - 3, m_loopIndex - 1) << 9;
+ break;
+ }
+ bVal = m_pArithDecoder->DECODE(&m_gbContext[CONTEXT]);
+ GBREG->setPixel(w, m_loopIndex, bVal);
+ }
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex ++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Start_decode_MMR(CJBig2_Image** pImage, CJBig2_BitStream *pStream, IFX_Pause* pPause)
+{
+ int bitpos, i;
+ JBIG2_ALLOC((* pImage), CJBig2_Image(GBW, GBH));
+ if ((* pImage)->m_pData == NULL) {
+ delete (* pImage);
+ (* pImage) = NULL;
+ m_pModule->JBig2_Error("Generic region decoding procedure: Create Image Failed with width = %d, height = %d\n", GBW, GBH);
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return m_ProssiveStatus;
+ }
+ bitpos = (int)pStream->getBitPos();
+ _FaxG4Decode(m_pModule, pStream->getBuf(), pStream->getLength(), &bitpos, (* pImage)->m_pData, GBW, GBH, (* pImage)->m_nStride);
+ pStream->setBitPos(bitpos);
+ for(i = 0; (FX_DWORD)i < (* pImage)->m_nStride * GBH; i++) {
+ (* pImage)->m_pData[i] = ~(* pImage)->m_pData[i];
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return m_ProssiveStatus;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_MMR()
+{
+ return m_ProssiveStatus;
+}
+FXCODEC_STATUS CJBig2_GRDProc::Continue_decode(IFX_Pause* pPause)
+{
+ if(m_ProssiveStatus != FXCODEC_STATUS_DECODE_TOBECONTINUE) {
+ return m_ProssiveStatus;
+ }
+ switch (m_DecodeType) {
+ case 1:
+ return decode_Arith(pPause);
+ case 2:
+ return decode_Arith_V2(pPause);
+ case 3:
+ return decode_Arith_V1(pPause);
+ case 4:
+ return decode_MMR();
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_ERROR;
+ return m_ProssiveStatus;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ if(m_pLine == NULL) {
+ m_pLine = pImage->m_pData;
+ }
+ nStride = pImage->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ FX_DWORD height = GBH & 0x7fffffff;
+ for(; m_loopIndex < height; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ if(m_loopIndex > 1) {
+ pLine1 = m_pLine - nStride2;
+ pLine2 = m_pLine - nStride;
+ line1 = (*pLine1++) << 6;
+ line2 = *pLine2++;
+ CONTEXT = ((line1 & 0xf800) | (line2 & 0x07f0));
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 6);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> k) & 0x0800)
+ | ((line2 >> k) & 0x0010));
+ }
+ m_pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0800)
+ | ((line2 >> (7 - k)) & 0x0010));
+ }
+ m_pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = m_pLine - nStride;
+ line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 & 0x07f0);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(m_loopIndex & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line2 >> k) & 0x0010));
+ }
+ m_pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = (((CONTEXT & 0x7bf7) << 1) | bVal
+ | ((line2 >> (7 - k)) & 0x0010));
+ }
+ m_pLine[nLineBytes] = cVal;
+ }
+ }
+ m_pLine += nStride;
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template0_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2, line3;
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x9b25]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ line1 = pImage->getPixel(1, m_loopIndex - 2);
+ line1 |= pImage->getPixel(0, m_loopIndex - 2) << 1;
+ line2 = pImage->getPixel(2, m_loopIndex - 1);
+ line2 |= pImage->getPixel(1, m_loopIndex - 1) << 1;
+ line2 |= pImage->getPixel(0, m_loopIndex - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= line2 << 5;
+ CONTEXT |= pImage->getPixel(w + GBAT[2], m_loopIndex + GBAT[3]) << 10;
+ CONTEXT |= pImage->getPixel(w + GBAT[4], m_loopIndex + GBAT[5]) << 11;
+ CONTEXT |= line1 << 12;
+ CONTEXT |= pImage->getPixel(w + GBAT[6], m_loopIndex + GBAT[7]) << 15;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ pImage->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
+ line2 = ((line2 << 1) | pImage->getPixel(w + 3, m_loopIndex - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x0f;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ if (!m_pLine) {
+ m_pLine = pImage->m_pData;
+ }
+ nStride = pImage->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ if(m_loopIndex > 1) {
+ pLine1 = m_pLine - nStride2;
+ pLine2 = m_pLine - nStride;
+ line1 = (*pLine1++) << 4;
+ line2 = *pLine2++;
+ CONTEXT = (line1 & 0x1e00) | ((line2 >> 1) & 0x01f8);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 4);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> k) & 0x0200)
+ | ((line2 >> (k + 1)) & 0x0008);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0200)
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ m_pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = m_pLine - nStride;
+ line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 >> 1) & 0x01f8;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(m_loopIndex & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line2 >> (k + 1)) & 0x0008);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x0efb) << 1) | bVal
+ | ((line2 >> (8 - k)) & 0x0008);
+ }
+ m_pLine[nLineBytes] = cVal;
+ }
+ }
+ m_pLine += nStride;
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template1_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2, line3;
+ for(FX_DWORD h = 0; h < GBH; h++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0795]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(h, h - 1);
+ } else {
+ line1 = pImage->getPixel(2, h - 2);
+ line1 |= pImage->getPixel(1, h - 2) << 1;
+ line1 |= pImage->getPixel(0, h - 2) << 2;
+ line2 = pImage->getPixel(2, h - 1);
+ line2 |= pImage->getPixel(1, h - 1) << 1;
+ line2 |= pImage->getPixel(0, h - 1) << 2;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, h)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= pImage->getPixel(w + GBAT[0], h + GBAT[1]) << 3;
+ CONTEXT |= line2 << 4;
+ CONTEXT |= line1 << 9;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ pImage->setPixel(w, h, bVal);
+ }
+ line1 = ((line1 << 1) | pImage->getPixel(w + 3, h - 2)) & 0x0f;
+ line2 = ((line2 << 1) | pImage->getPixel(w + 3, h - 1)) & 0x1f;
+ line3 = ((line3 << 1) | bVal) & 0x07;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2;
+ FX_BYTE *pLine1, *pLine2, cVal;
+ FX_INT32 nStride, nStride2, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ if(!m_pLine) {
+ m_pLine = pImage->m_pData;
+ }
+ nStride = pImage->m_nStride;
+ nStride2 = nStride << 1;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ if(m_loopIndex > 1) {
+ pLine1 = m_pLine - nStride2;
+ pLine2 = m_pLine - nStride;
+ line1 = (*pLine1++) << 1;
+ line2 = *pLine2++;
+ CONTEXT = (line1 & 0x0380) | ((line2 >> 3) & 0x007c);
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | ((*pLine1++) << 1);
+ line2 = (line2 << 8) | (*pLine2++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> k) & 0x0080)
+ | ((line2 >> (k + 3)) & 0x0004);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line1 >> (7 - k)) & 0x0080)
+ | ((line2 >> (10 - k)) & 0x0004);
+ }
+ m_pLine[nLineBytes] = cVal;
+ } else {
+ pLine2 = m_pLine - nStride;
+ line2 = (m_loopIndex & 1) ? (*pLine2++) : 0;
+ CONTEXT = (line2 >> 3) & 0x007c;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ if(m_loopIndex & 1) {
+ line2 = (line2 << 8) | (*pLine2++);
+ }
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | ((line2 >> (k + 3)) & 0x0004);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line2 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01bd) << 1) | bVal
+ | (((line2 >> (10 - k))) & 0x0004);
+ }
+ m_pLine[nLineBytes] = cVal;
+ }
+ }
+ m_pLine += nStride;
+ if(pPause && m_loopIndex % 50 == 0 && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template2_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2, line3;
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x00e5]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ line1 = pImage->getPixel(1, m_loopIndex - 2);
+ line1 |= pImage->getPixel(0, m_loopIndex - 2) << 1;
+ line2 = pImage->getPixel(1, m_loopIndex - 1);
+ line2 |= pImage->getPixel(0, m_loopIndex - 1) << 1;
+ line3 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line3;
+ CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 2;
+ CONTEXT |= line2 << 3;
+ CONTEXT |= line1 << 7;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ pImage->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 2)) & 0x07;
+ line2 = ((line2 << 1) | pImage->getPixel(w + 2, m_loopIndex - 1)) & 0x0f;
+ line3 = ((line3 << 1) | bVal) & 0x03;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1;
+ FX_BYTE *pLine1, cVal;
+ FX_INT32 nStride, k;
+ FX_INT32 nLineBytes, nBitsLeft, cc;
+ if (!m_pLine) {
+ m_pLine = pImage->m_pData;
+ }
+ nStride = pImage->m_nStride;
+ nLineBytes = ((GBW + 7) >> 3) - 1;
+ nBitsLeft = GBW - (nLineBytes << 3);
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ if(m_loopIndex > 0) {
+ pLine1 = m_pLine - nStride;
+ line1 = *pLine1++;
+ CONTEXT = (line1 >> 1) & 0x03f0;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ line1 = (line1 << 8) | (*pLine1++);
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (k + 1)) & 0x0010);
+ }
+ m_pLine[cc] = cVal;
+ }
+ line1 <<= 8;
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal
+ | ((line1 >> (8 - k)) & 0x0010);
+ }
+ m_pLine[nLineBytes] = cVal;
+ } else {
+ CONTEXT = 0;
+ for(cc = 0; cc < nLineBytes; cc++) {
+ cVal = 0;
+ for(k = 7; k >= 0; k--) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << k;
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
+ }
+ m_pLine[cc] = cVal;
+ }
+ cVal = 0;
+ for(k = 0; k < nBitsLeft; k++) {
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ cVal |= bVal << (7 - k);
+ CONTEXT = ((CONTEXT & 0x01f7) << 1) | bVal;
+ }
+ m_pLine[nLineBytes] = cVal;
+ }
+ }
+ m_pLine += nStride;
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
+FXCODEC_STATUS CJBig2_GRDProc::decode_Arith_Template3_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause)
+{
+ FX_BOOL SLTP, bVal;
+ FX_DWORD CONTEXT;
+ FX_DWORD line1, line2;
+ for(; m_loopIndex < GBH; m_loopIndex++) {
+ if(TPGDON) {
+ SLTP = pArithDecoder->DECODE(&gbContext[0x0195]);
+ LTP = LTP ^ SLTP;
+ }
+ if(LTP == 1) {
+ pImage->copyLine(m_loopIndex, m_loopIndex - 1);
+ } else {
+ line1 = pImage->getPixel(1, m_loopIndex - 1);
+ line1 |= pImage->getPixel(0, m_loopIndex - 1) << 1;
+ line2 = 0;
+ for(FX_DWORD w = 0; w < GBW; w++) {
+ if(USESKIP && SKIP->getPixel(w, m_loopIndex)) {
+ bVal = 0;
+ } else {
+ CONTEXT = line2;
+ CONTEXT |= pImage->getPixel(w + GBAT[0], m_loopIndex + GBAT[1]) << 4;
+ CONTEXT |= line1 << 5;
+ bVal = pArithDecoder->DECODE(&gbContext[CONTEXT]);
+ }
+ if(bVal) {
+ pImage->setPixel(w, m_loopIndex, bVal);
+ }
+ line1 = ((line1 << 1) | pImage->getPixel(w + 2, m_loopIndex - 1)) & 0x1f;
+ line2 = ((line2 << 1) | bVal) & 0x0f;
+ }
+ }
+ if(pPause && pPause->NeedToPauseNow()) {
+ m_loopIndex++;
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ return FXCODEC_STATUS_DECODE_TOBECONTINUE;
+ }
+ }
+ m_ProssiveStatus = FXCODEC_STATUS_DECODE_FINISH;
+ return FXCODEC_STATUS_DECODE_FINISH;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h
index a89d0d62ef..3f43c979a5 100644
--- a/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h
+++ b/core/src/fxcodec/jbig2/JBig2_GeneralDecoder.h
@@ -1,278 +1,278 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_GENERAL_DECODER_H_
-#define _JBIG2_GENERAL_DECODER_H_
-#include "../../../include/fxcodec/fx_codec_def.h"
-#include "../../../include/fxcrt/fx_basic.h"
-#include "JBig2_Define.h"
-#include "JBig2_SymbolDict.h"
-#include "JBig2_ArithDecoder.h"
-#include "JBig2_ArithIntDecoder.h"
-#include "../../../include/fxcrt/fx_coordinates.h"
-class CJBig2_HuffmanTable;
-class CJBig2_Image;
-class CJBig2_PatternDict;
-typedef enum {
- JBIG2_CORNER_BOTTOMLEFT = 0,
- JBIG2_CORNER_TOPLEFT = 1,
- JBIG2_CORNER_BOTTOMRIGHT = 2,
- JBIG2_CORNER_TOPRIGHT = 3
-} JBig2Corner;
-class CJBig2_GRDProc : public CJBig2_Object
-{
-public:
- CJBig2_GRDProc()
- {
- m_loopIndex = 0;
- m_pLine = NULL;
- m_pPause = NULL;
- m_DecodeType = 0;
- LTP = 0;
- m_ReplaceRect.left = 0;
- m_ReplaceRect.bottom = 0;
- m_ReplaceRect.top = 0;
- m_ReplaceRect.right = 0;
- }
-
- CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_V2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_MMR(CJBig2_BitStream *pStream);
- FXCODEC_STATUS Start_decode_Arith(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
- FXCODEC_STATUS Start_decode_Arith_V2(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
- FXCODEC_STATUS Start_decode_Arith_V1(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
- FXCODEC_STATUS Start_decode_MMR(CJBig2_Image** pImage, CJBig2_BitStream *pStream, IFX_Pause* pPause = NULL);
- FXCODEC_STATUS Continue_decode(IFX_Pause* pPause);
- FX_RECT GetReplaceRect()
- {
- return m_ReplaceRect;
- };
-private:
- FXCODEC_STATUS decode_Arith(IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_V2(IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_V1(IFX_Pause* pPause);
- FXCODEC_STATUS decode_MMR();
- FXCODEC_STATUS decode_Arith_Template0_opt3(CJBig2_Image*pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_Template0_unopt(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_Template1_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_Template1_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_Template2_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_Template2_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_Template3_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
- FXCODEC_STATUS decode_Arith_Template3_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
- FX_DWORD m_loopIndex;
- FX_BYTE * m_pLine;
- IFX_Pause* m_pPause;
- FXCODEC_STATUS m_ProssiveStatus;
- CJBig2_Image** m_pImage;
- CJBig2_ArithDecoder *m_pArithDecoder;
- JBig2ArithCtx *m_gbContext;
- FX_WORD m_DecodeType;
- FX_BOOL LTP;
- FX_RECT m_ReplaceRect;
-private:
-
- CJBig2_Image *decode_Arith_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template0_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template0_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template1_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template1_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template2_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template2_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template2_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template2_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template3_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template3_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template3_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-
- CJBig2_Image *decode_Arith_Template3_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
-public:
- FX_BOOL MMR;
- FX_DWORD GBW;
- FX_DWORD GBH;
- FX_BYTE GBTEMPLATE;
- FX_BOOL TPGDON;
- FX_BOOL USESKIP;
- CJBig2_Image * SKIP;
- signed char GBAT[8];
-};
-class CJBig2_GRRDProc : public CJBig2_Object
-{
-public:
-
- CJBig2_Image *decode(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
-
- CJBig2_Image *decode_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
-
- CJBig2_Image *decode_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
-
- CJBig2_Image *decode_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
-
- CJBig2_Image *decode_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
-
- CJBig2_Image *decode_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
-public:
- FX_DWORD GRW;
- FX_DWORD GRH;
- FX_BOOL GRTEMPLATE;
- CJBig2_Image *GRREFERENCE;
- FX_INT32 GRREFERENCEDX;
- FX_INT32 GRREFERENCEDY;
- FX_BOOL TPGRON;
- signed char GRAT[4];
-};
-typedef struct {
- CJBig2_ArithIntDecoder *IADT,
- *IAFS,
- *IADS,
- *IAIT,
- *IARI,
- *IARDW,
- *IARDH,
- *IARDX,
- *IARDY;
- CJBig2_ArithIaidDecoder *IAID;
-} JBig2IntDecoderState;
-class CJBig2_TRDProc : public CJBig2_Object
-{
-public:
-
- CJBig2_Image *decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *grContext);
-
- CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext,
- JBig2IntDecoderState *pIDS = NULL);
-public:
- FX_BOOL SBHUFF;
- FX_BOOL SBREFINE;
- FX_DWORD SBW;
- FX_DWORD SBH;
- FX_DWORD SBNUMINSTANCES;
- FX_DWORD SBSTRIPS;
- FX_DWORD SBNUMSYMS;
-
- JBig2HuffmanCode *SBSYMCODES;
- FX_BYTE SBSYMCODELEN;
-
- CJBig2_Image **SBSYMS;
- FX_BOOL SBDEFPIXEL;
-
- JBig2ComposeOp SBCOMBOP;
- FX_BOOL TRANSPOSED;
-
- JBig2Corner REFCORNER;
- signed char SBDSOFFSET;
- CJBig2_HuffmanTable *SBHUFFFS,
- *SBHUFFDS,
- *SBHUFFDT,
- *SBHUFFRDW,
- *SBHUFFRDH,
- *SBHUFFRDX,
- *SBHUFFRDY,
- *SBHUFFRSIZE;
- FX_BOOL SBRTEMPLATE;
- signed char SBRAT[4];
-};
-class CJBig2_SDDProc : public CJBig2_Object
-{
-public:
-
- CJBig2_SymbolDict *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext);
-
- CJBig2_SymbolDict *decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext, IFX_Pause* pPause);
-public:
- FX_BOOL SDHUFF;
- FX_BOOL SDREFAGG;
- FX_DWORD SDNUMINSYMS;
- CJBig2_Image ** SDINSYMS;
- FX_DWORD SDNUMNEWSYMS;
- FX_DWORD SDNUMEXSYMS;
- CJBig2_HuffmanTable *SDHUFFDH,
- *SDHUFFDW,
- *SDHUFFBMSIZE,
- *SDHUFFAGGINST;
- FX_BYTE SDTEMPLATE;
- signed char SDAT[8];
- FX_BOOL SDRTEMPLATE;
- signed char SDRAT[4];
-};
-class CJBig2_HTRDProc : public CJBig2_Object
-{
-public:
-
- CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
-
- CJBig2_Image *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
-public:
- FX_DWORD HBW,
- HBH;
- FX_BOOL HMMR;
- FX_BYTE HTEMPLATE;
- FX_DWORD HNUMPATS;
- CJBig2_Image **HPATS;
- FX_BOOL HDEFPIXEL;
- JBig2ComposeOp HCOMBOP;
- FX_BOOL HENABLESKIP;
- FX_DWORD HGW,
- HGH;
- FX_INT32 HGX,
- HGY;
- FX_WORD HRX,
- HRY;
- FX_BYTE HPW,
- HPH;
-};
-class CJBig2_PDDProc : public CJBig2_Object
-{
-public:
-
- CJBig2_PatternDict *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
-
- CJBig2_PatternDict *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
-public:
- FX_BOOL HDMMR;
- FX_BYTE HDPW,
- HDPH;
- FX_DWORD GRAYMAX;
- FX_BYTE HDTEMPLATE;
-};
-class CJBig2_GSIDProc : public CJBig2_Object
-{
-public:
-
- FX_DWORD *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
-
- FX_DWORD *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
-public:
- FX_BOOL GSMMR;
- FX_BOOL GSUSESKIP;
- FX_BYTE GSBPP;
- FX_DWORD GSW,
- GSH;
- FX_BYTE GSTEMPLATE;
- CJBig2_Image *GSKIP;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_GENERAL_DECODER_H_
+#define _JBIG2_GENERAL_DECODER_H_
+#include "../../../include/fxcodec/fx_codec_def.h"
+#include "../../../include/fxcrt/fx_basic.h"
+#include "JBig2_Define.h"
+#include "JBig2_SymbolDict.h"
+#include "JBig2_ArithDecoder.h"
+#include "JBig2_ArithIntDecoder.h"
+#include "../../../include/fxcrt/fx_coordinates.h"
+class CJBig2_HuffmanTable;
+class CJBig2_Image;
+class CJBig2_PatternDict;
+typedef enum {
+ JBIG2_CORNER_BOTTOMLEFT = 0,
+ JBIG2_CORNER_TOPLEFT = 1,
+ JBIG2_CORNER_BOTTOMRIGHT = 2,
+ JBIG2_CORNER_TOPRIGHT = 3
+} JBig2Corner;
+class CJBig2_GRDProc : public CJBig2_Object
+{
+public:
+ CJBig2_GRDProc()
+ {
+ m_loopIndex = 0;
+ m_pLine = NULL;
+ m_pPause = NULL;
+ m_DecodeType = 0;
+ LTP = 0;
+ m_ReplaceRect.left = 0;
+ m_ReplaceRect.bottom = 0;
+ m_ReplaceRect.top = 0;
+ m_ReplaceRect.right = 0;
+ }
+
+ CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_V2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_MMR(CJBig2_BitStream *pStream);
+ FXCODEC_STATUS Start_decode_Arith(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
+ FXCODEC_STATUS Start_decode_Arith_V2(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
+ FXCODEC_STATUS Start_decode_Arith_V1(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
+ FXCODEC_STATUS Start_decode_MMR(CJBig2_Image** pImage, CJBig2_BitStream *pStream, IFX_Pause* pPause = NULL);
+ FXCODEC_STATUS Continue_decode(IFX_Pause* pPause);
+ FX_RECT GetReplaceRect()
+ {
+ return m_ReplaceRect;
+ };
+private:
+ FXCODEC_STATUS decode_Arith(IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_V2(IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_V1(IFX_Pause* pPause);
+ FXCODEC_STATUS decode_MMR();
+ FXCODEC_STATUS decode_Arith_Template0_opt3(CJBig2_Image*pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template0_unopt(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template1_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template1_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template2_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template2_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template3_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FXCODEC_STATUS decode_Arith_Template3_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+ FX_DWORD m_loopIndex;
+ FX_BYTE * m_pLine;
+ IFX_Pause* m_pPause;
+ FXCODEC_STATUS m_ProssiveStatus;
+ CJBig2_Image** m_pImage;
+ CJBig2_ArithDecoder *m_pArithDecoder;
+ JBig2ArithCtx *m_gbContext;
+ FX_WORD m_DecodeType;
+ FX_BOOL LTP;
+ FX_RECT m_ReplaceRect;
+private:
+
+ CJBig2_Image *decode_Arith_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template0_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template0_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template1_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template1_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template2_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template2_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template2_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template2_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template3_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template3_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template3_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+
+ CJBig2_Image *decode_Arith_Template3_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
+public:
+ FX_BOOL MMR;
+ FX_DWORD GBW;
+ FX_DWORD GBH;
+ FX_BYTE GBTEMPLATE;
+ FX_BOOL TPGDON;
+ FX_BOOL USESKIP;
+ CJBig2_Image * SKIP;
+ signed char GBAT[8];
+};
+class CJBig2_GRRDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_Image *decode(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
+public:
+ FX_DWORD GRW;
+ FX_DWORD GRH;
+ FX_BOOL GRTEMPLATE;
+ CJBig2_Image *GRREFERENCE;
+ FX_INT32 GRREFERENCEDX;
+ FX_INT32 GRREFERENCEDY;
+ FX_BOOL TPGRON;
+ signed char GRAT[4];
+};
+typedef struct {
+ CJBig2_ArithIntDecoder *IADT,
+ *IAFS,
+ *IADS,
+ *IAIT,
+ *IARI,
+ *IARDW,
+ *IARDH,
+ *IARDX,
+ *IARDY;
+ CJBig2_ArithIaidDecoder *IAID;
+} JBig2IntDecoderState;
+class CJBig2_TRDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_Image *decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *grContext);
+
+ CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext,
+ JBig2IntDecoderState *pIDS = NULL);
+public:
+ FX_BOOL SBHUFF;
+ FX_BOOL SBREFINE;
+ FX_DWORD SBW;
+ FX_DWORD SBH;
+ FX_DWORD SBNUMINSTANCES;
+ FX_DWORD SBSTRIPS;
+ FX_DWORD SBNUMSYMS;
+
+ JBig2HuffmanCode *SBSYMCODES;
+ FX_BYTE SBSYMCODELEN;
+
+ CJBig2_Image **SBSYMS;
+ FX_BOOL SBDEFPIXEL;
+
+ JBig2ComposeOp SBCOMBOP;
+ FX_BOOL TRANSPOSED;
+
+ JBig2Corner REFCORNER;
+ signed char SBDSOFFSET;
+ CJBig2_HuffmanTable *SBHUFFFS,
+ *SBHUFFDS,
+ *SBHUFFDT,
+ *SBHUFFRDW,
+ *SBHUFFRDH,
+ *SBHUFFRDX,
+ *SBHUFFRDY,
+ *SBHUFFRSIZE;
+ FX_BOOL SBRTEMPLATE;
+ signed char SBRAT[4];
+};
+class CJBig2_SDDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_SymbolDict *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext);
+
+ CJBig2_SymbolDict *decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext, IFX_Pause* pPause);
+public:
+ FX_BOOL SDHUFF;
+ FX_BOOL SDREFAGG;
+ FX_DWORD SDNUMINSYMS;
+ CJBig2_Image ** SDINSYMS;
+ FX_DWORD SDNUMNEWSYMS;
+ FX_DWORD SDNUMEXSYMS;
+ CJBig2_HuffmanTable *SDHUFFDH,
+ *SDHUFFDW,
+ *SDHUFFBMSIZE,
+ *SDHUFFAGGINST;
+ FX_BYTE SDTEMPLATE;
+ signed char SDAT[8];
+ FX_BOOL SDRTEMPLATE;
+ signed char SDRAT[4];
+};
+class CJBig2_HTRDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+
+ CJBig2_Image *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
+public:
+ FX_DWORD HBW,
+ HBH;
+ FX_BOOL HMMR;
+ FX_BYTE HTEMPLATE;
+ FX_DWORD HNUMPATS;
+ CJBig2_Image **HPATS;
+ FX_BOOL HDEFPIXEL;
+ JBig2ComposeOp HCOMBOP;
+ FX_BOOL HENABLESKIP;
+ FX_DWORD HGW,
+ HGH;
+ FX_INT32 HGX,
+ HGY;
+ FX_WORD HRX,
+ HRY;
+ FX_BYTE HPW,
+ HPH;
+};
+class CJBig2_PDDProc : public CJBig2_Object
+{
+public:
+
+ CJBig2_PatternDict *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+
+ CJBig2_PatternDict *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
+public:
+ FX_BOOL HDMMR;
+ FX_BYTE HDPW,
+ HDPH;
+ FX_DWORD GRAYMAX;
+ FX_BYTE HDTEMPLATE;
+};
+class CJBig2_GSIDProc : public CJBig2_Object
+{
+public:
+
+ FX_DWORD *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
+
+ FX_DWORD *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
+public:
+ FX_BOOL GSMMR;
+ FX_BOOL GSUSESKIP;
+ FX_BYTE GSBPP;
+ FX_DWORD GSW,
+ GSH;
+ FX_BYTE GSTEMPLATE;
+ CJBig2_Image *GSKIP;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
index 8199fe7c21..dd820ae4ca 100644
--- a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp
@@ -1,55 +1,55 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_HuffmanDecoder.h"
-CJBig2_HuffmanDecoder::CJBig2_HuffmanDecoder(CJBig2_BitStream *pStream)
-{
- m_pStream = pStream;
-}
-CJBig2_HuffmanDecoder::~CJBig2_HuffmanDecoder()
-{
-}
-int CJBig2_HuffmanDecoder::decodeAValue(CJBig2_HuffmanTable *pTable, int *nResult)
-{
- int nVal, nTmp, i, nBits;
- nVal = 0;
- nBits = 0;
- while(1) {
- if(m_pStream->read1Bit(&nTmp) == -1) {
- return -1;
- }
- nVal = (nVal << 1) | nTmp;
- nBits ++;
- for(i = 0; i < pTable->NTEMP; i++) {
- if((pTable->PREFLEN[i] == nBits) && (pTable->CODES[i] == nVal)) {
- if((pTable->HTOOB == 1) && (i == pTable->NTEMP - 1)) {
- return JBIG2_OOB;
- }
- if(m_pStream->readNBits(pTable->RANGELEN[i], &nTmp) == -1) {
- return -1;
- }
- if(pTable->HTOOB) {
- if(i == pTable->NTEMP - 3) {
- *nResult = pTable->RANGELOW[i] - nTmp;
- return 0;
- } else {
- *nResult = pTable->RANGELOW[i] + nTmp;
- return 0;
- }
- } else {
- if(i == pTable->NTEMP - 2) {
- *nResult = pTable->RANGELOW[i] - nTmp;
- return 0;
- } else {
- *nResult = pTable->RANGELOW[i] + nTmp;
- return 0;
- }
- }
- }
- }
- }
- return -2;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_HuffmanDecoder.h"
+CJBig2_HuffmanDecoder::CJBig2_HuffmanDecoder(CJBig2_BitStream *pStream)
+{
+ m_pStream = pStream;
+}
+CJBig2_HuffmanDecoder::~CJBig2_HuffmanDecoder()
+{
+}
+int CJBig2_HuffmanDecoder::decodeAValue(CJBig2_HuffmanTable *pTable, int *nResult)
+{
+ int nVal, nTmp, i, nBits;
+ nVal = 0;
+ nBits = 0;
+ while(1) {
+ if(m_pStream->read1Bit(&nTmp) == -1) {
+ return -1;
+ }
+ nVal = (nVal << 1) | nTmp;
+ nBits ++;
+ for(i = 0; i < pTable->NTEMP; i++) {
+ if((pTable->PREFLEN[i] == nBits) && (pTable->CODES[i] == nVal)) {
+ if((pTable->HTOOB == 1) && (i == pTable->NTEMP - 1)) {
+ return JBIG2_OOB;
+ }
+ if(m_pStream->readNBits(pTable->RANGELEN[i], &nTmp) == -1) {
+ return -1;
+ }
+ if(pTable->HTOOB) {
+ if(i == pTable->NTEMP - 3) {
+ *nResult = pTable->RANGELOW[i] - nTmp;
+ return 0;
+ } else {
+ *nResult = pTable->RANGELOW[i] + nTmp;
+ return 0;
+ }
+ } else {
+ if(i == pTable->NTEMP - 2) {
+ *nResult = pTable->RANGELOW[i] - nTmp;
+ return 0;
+ } else {
+ *nResult = pTable->RANGELOW[i] + nTmp;
+ return 0;
+ }
+ }
+ }
+ }
+ }
+ return -2;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h
index 38b2021d20..60ec2900d3 100644
--- a/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanDecoder.h
@@ -1,24 +1,24 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_HUFFMAN_DECODER_H_
-#define _JBIG2_HUFFMAN_DECODER_H_
-#include "JBig2_BitStream.h"
-#include "JBig2_HuffmanTable.h"
-class CJBig2_HuffmanDecoder : public CJBig2_Object
-{
-public:
-
- CJBig2_HuffmanDecoder(CJBig2_BitStream *pStream);
-
- ~CJBig2_HuffmanDecoder();
-
- int decodeAValue(CJBig2_HuffmanTable *pTable, int *nResult);
-private:
-
- CJBig2_BitStream *m_pStream;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_HUFFMAN_DECODER_H_
+#define _JBIG2_HUFFMAN_DECODER_H_
+#include "JBig2_BitStream.h"
+#include "JBig2_HuffmanTable.h"
+class CJBig2_HuffmanDecoder : public CJBig2_Object
+{
+public:
+
+ CJBig2_HuffmanDecoder(CJBig2_BitStream *pStream);
+
+ ~CJBig2_HuffmanDecoder();
+
+ int decodeAValue(CJBig2_HuffmanTable *pTable, int *nResult);
+private:
+
+ CJBig2_BitStream *m_pStream;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
index af4a54988d..0a5bc8e645 100644
--- a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.cpp
@@ -1,193 +1,193 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_HuffmanTable.h"
-#include "JBig2_BitStream.h"
-#include <string.h>
-
-CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine *pTable, int nLines,
- FX_BOOL bHTOOB)
-{
- init();
- m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB);
-}
-
-CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream *pStream)
-{
- init();
- m_bOK = parseFromCodedBuffer(pStream);
-}
-
-CJBig2_HuffmanTable::~CJBig2_HuffmanTable()
-{
- if(CODES) {
- m_pModule->JBig2_Free(CODES);
- }
- if(PREFLEN) {
- m_pModule->JBig2_Free(PREFLEN);
- }
- if(RANGELEN) {
- m_pModule->JBig2_Free(RANGELEN);
- }
- if(RANGELOW) {
- m_pModule->JBig2_Free(RANGELOW);
- }
-}
-void CJBig2_HuffmanTable::init()
-{
- HTOOB = FALSE;
- NTEMP = 0;
- CODES = NULL;
- PREFLEN = NULL;
- RANGELEN = NULL;
- RANGELOW = NULL;
-}
-int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB)
-{
- int CURLEN, LENMAX, CURCODE, CURTEMP, i;
- int *LENCOUNT;
- int *FIRSTCODE;
- HTOOB = bHTOOB;
- NTEMP = nLines;
- CODES = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
- PREFLEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
- RANGELEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
- RANGELOW = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
- LENMAX = 0;
- for(i = 0; i < NTEMP; i++) {
- PREFLEN[i] = pTable[i].PREFLEN;
- RANGELEN[i] = pTable[i].RANDELEN;
- RANGELOW[i] = pTable[i].RANGELOW;
- if(PREFLEN[i] > LENMAX) {
- LENMAX = PREFLEN[i];
- }
- }
- LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
- JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
- FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
- for(i = 0; i < NTEMP; i++) {
- LENCOUNT[PREFLEN[i]] ++;
- }
- CURLEN = 1;
- FIRSTCODE[0] = 0;
- LENCOUNT[0] = 0;
- while(CURLEN <= LENMAX) {
- FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
- CURCODE = FIRSTCODE[CURLEN];
- CURTEMP = 0;
- while(CURTEMP < NTEMP) {
- if(PREFLEN[CURTEMP] == CURLEN) {
- CODES[CURTEMP] = CURCODE;
- CURCODE = CURCODE + 1;
- }
- CURTEMP = CURTEMP + 1;
- }
- CURLEN = CURLEN + 1;
- }
- m_pModule->JBig2_Free(LENCOUNT);
- m_pModule->JBig2_Free(FIRSTCODE);
- return 1;
-}
-
-#define HT_CHECK_MEMORY_ADJUST \
- if(NTEMP >= nSize) \
- { \
- nSize += 16; \
- PREFLEN = (int*)m_pModule->JBig2_Realloc(PREFLEN,sizeof(int)*nSize); \
- RANGELEN = (int*)m_pModule->JBig2_Realloc(RANGELEN,sizeof(int)*nSize); \
- RANGELOW = (int*)m_pModule->JBig2_Realloc(RANGELOW,sizeof(int)*nSize); \
- }
-int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream *pStream)
-{
- unsigned char HTPS, HTRS;
- int HTLOW, HTHIGH;
- int CURRANGELOW;
- int nSize = 16;
- int CURLEN, LENMAX, CURCODE, CURTEMP, i;
- int *LENCOUNT;
- int *FIRSTCODE;
- unsigned char cTemp;
- if(pStream->read1Byte(&cTemp) == -1) {
- goto failed;
- }
- HTOOB = cTemp & 0x01;
- HTPS = ((cTemp >> 1) & 0x07) + 1;
- HTRS = ((cTemp >> 4) & 0x07) + 1;
- if(pStream->readInteger((FX_DWORD*)&HTLOW) == -1 ||
- pStream->readInteger((FX_DWORD*)&HTHIGH) == -1) {
- goto failed;
- }
- PREFLEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
- RANGELEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
- RANGELOW = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
- CURRANGELOW = HTLOW;
- NTEMP = 0;
- do {
- HT_CHECK_MEMORY_ADJUST
- if((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
- || (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) {
- goto failed;
- }
- RANGELOW[NTEMP] = CURRANGELOW;
- CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]);
- NTEMP = NTEMP + 1;
- } while(CURRANGELOW < HTHIGH);
- HT_CHECK_MEMORY_ADJUST
- if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
- goto failed;
- }
- RANGELEN[NTEMP] = 32;
- RANGELOW[NTEMP] = HTLOW - 1;
- NTEMP = NTEMP + 1;
- HT_CHECK_MEMORY_ADJUST
- if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
- goto failed;
- }
- RANGELEN[NTEMP] = 32;
- RANGELOW[NTEMP] = HTHIGH;
- NTEMP = NTEMP + 1;
- if(HTOOB) {
- HT_CHECK_MEMORY_ADJUST
- if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
- goto failed;
- }
- NTEMP = NTEMP + 1;
- }
- CODES = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
- LENMAX = 0;
- for(i = 0; i < NTEMP; i++) {
- if(PREFLEN[i] > LENMAX) {
- LENMAX = PREFLEN[i];
- }
- }
- LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
- JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
- FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
- for(i = 0; i < NTEMP; i++) {
- LENCOUNT[PREFLEN[i]] ++;
- }
- CURLEN = 1;
- FIRSTCODE[0] = 0;
- LENCOUNT[0] = 0;
- while(CURLEN <= LENMAX) {
- FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
- CURCODE = FIRSTCODE[CURLEN];
- CURTEMP = 0;
- while(CURTEMP < NTEMP) {
- if(PREFLEN[CURTEMP] == CURLEN) {
- CODES[CURTEMP] = CURCODE;
- CURCODE = CURCODE + 1;
- }
- CURTEMP = CURTEMP + 1;
- }
- CURLEN = CURLEN + 1;
- }
- m_pModule->JBig2_Free(LENCOUNT);
- m_pModule->JBig2_Free(FIRSTCODE);
- return TRUE;
-failed:
- return FALSE;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_HuffmanTable.h"
+#include "JBig2_BitStream.h"
+#include <string.h>
+
+CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine *pTable, int nLines,
+ FX_BOOL bHTOOB)
+{
+ init();
+ m_bOK = parseFromStandardTable(pTable, nLines, bHTOOB);
+}
+
+CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream *pStream)
+{
+ init();
+ m_bOK = parseFromCodedBuffer(pStream);
+}
+
+CJBig2_HuffmanTable::~CJBig2_HuffmanTable()
+{
+ if(CODES) {
+ m_pModule->JBig2_Free(CODES);
+ }
+ if(PREFLEN) {
+ m_pModule->JBig2_Free(PREFLEN);
+ }
+ if(RANGELEN) {
+ m_pModule->JBig2_Free(RANGELEN);
+ }
+ if(RANGELOW) {
+ m_pModule->JBig2_Free(RANGELOW);
+ }
+}
+void CJBig2_HuffmanTable::init()
+{
+ HTOOB = FALSE;
+ NTEMP = 0;
+ CODES = NULL;
+ PREFLEN = NULL;
+ RANGELEN = NULL;
+ RANGELOW = NULL;
+}
+int CJBig2_HuffmanTable::parseFromStandardTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB)
+{
+ int CURLEN, LENMAX, CURCODE, CURTEMP, i;
+ int *LENCOUNT;
+ int *FIRSTCODE;
+ HTOOB = bHTOOB;
+ NTEMP = nLines;
+ CODES = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ PREFLEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ RANGELEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ RANGELOW = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ LENMAX = 0;
+ for(i = 0; i < NTEMP; i++) {
+ PREFLEN[i] = pTable[i].PREFLEN;
+ RANGELEN[i] = pTable[i].RANDELEN;
+ RANGELOW[i] = pTable[i].RANGELOW;
+ if(PREFLEN[i] > LENMAX) {
+ LENMAX = PREFLEN[i];
+ }
+ }
+ LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
+ FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ for(i = 0; i < NTEMP; i++) {
+ LENCOUNT[PREFLEN[i]] ++;
+ }
+ CURLEN = 1;
+ FIRSTCODE[0] = 0;
+ LENCOUNT[0] = 0;
+ while(CURLEN <= LENMAX) {
+ FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = FIRSTCODE[CURLEN];
+ CURTEMP = 0;
+ while(CURTEMP < NTEMP) {
+ if(PREFLEN[CURTEMP] == CURLEN) {
+ CODES[CURTEMP] = CURCODE;
+ CURCODE = CURCODE + 1;
+ }
+ CURTEMP = CURTEMP + 1;
+ }
+ CURLEN = CURLEN + 1;
+ }
+ m_pModule->JBig2_Free(LENCOUNT);
+ m_pModule->JBig2_Free(FIRSTCODE);
+ return 1;
+}
+
+#define HT_CHECK_MEMORY_ADJUST \
+ if(NTEMP >= nSize) \
+ { \
+ nSize += 16; \
+ PREFLEN = (int*)m_pModule->JBig2_Realloc(PREFLEN,sizeof(int)*nSize); \
+ RANGELEN = (int*)m_pModule->JBig2_Realloc(RANGELEN,sizeof(int)*nSize); \
+ RANGELOW = (int*)m_pModule->JBig2_Realloc(RANGELOW,sizeof(int)*nSize); \
+ }
+int CJBig2_HuffmanTable::parseFromCodedBuffer(CJBig2_BitStream *pStream)
+{
+ unsigned char HTPS, HTRS;
+ int HTLOW, HTHIGH;
+ int CURRANGELOW;
+ int nSize = 16;
+ int CURLEN, LENMAX, CURCODE, CURTEMP, i;
+ int *LENCOUNT;
+ int *FIRSTCODE;
+ unsigned char cTemp;
+ if(pStream->read1Byte(&cTemp) == -1) {
+ goto failed;
+ }
+ HTOOB = cTemp & 0x01;
+ HTPS = ((cTemp >> 1) & 0x07) + 1;
+ HTRS = ((cTemp >> 4) & 0x07) + 1;
+ if(pStream->readInteger((FX_DWORD*)&HTLOW) == -1 ||
+ pStream->readInteger((FX_DWORD*)&HTHIGH) == -1) {
+ goto failed;
+ }
+ PREFLEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
+ RANGELEN = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
+ RANGELOW = (int*)m_pModule->JBig2_Malloc2(sizeof(int), nSize);
+ CURRANGELOW = HTLOW;
+ NTEMP = 0;
+ do {
+ HT_CHECK_MEMORY_ADJUST
+ if((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
+ || (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1)) {
+ goto failed;
+ }
+ RANGELOW[NTEMP] = CURRANGELOW;
+ CURRANGELOW = CURRANGELOW + (1 << RANGELEN[NTEMP]);
+ NTEMP = NTEMP + 1;
+ } while(CURRANGELOW < HTHIGH);
+ HT_CHECK_MEMORY_ADJUST
+ if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
+ goto failed;
+ }
+ RANGELEN[NTEMP] = 32;
+ RANGELOW[NTEMP] = HTLOW - 1;
+ NTEMP = NTEMP + 1;
+ HT_CHECK_MEMORY_ADJUST
+ if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
+ goto failed;
+ }
+ RANGELEN[NTEMP] = 32;
+ RANGELOW[NTEMP] = HTHIGH;
+ NTEMP = NTEMP + 1;
+ if(HTOOB) {
+ HT_CHECK_MEMORY_ADJUST
+ if(pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) {
+ goto failed;
+ }
+ NTEMP = NTEMP + 1;
+ }
+ CODES = (int*)m_pModule->JBig2_Malloc2(sizeof(int), NTEMP);
+ LENMAX = 0;
+ for(i = 0; i < NTEMP; i++) {
+ if(PREFLEN[i] > LENMAX) {
+ LENMAX = PREFLEN[i];
+ }
+ }
+ LENCOUNT = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ JBIG2_memset(LENCOUNT, 0, sizeof(int) * (LENMAX + 1));
+ FIRSTCODE = (int*)m_pModule->JBig2_Malloc2(sizeof(int), (LENMAX + 1));
+ for(i = 0; i < NTEMP; i++) {
+ LENCOUNT[PREFLEN[i]] ++;
+ }
+ CURLEN = 1;
+ FIRSTCODE[0] = 0;
+ LENCOUNT[0] = 0;
+ while(CURLEN <= LENMAX) {
+ FIRSTCODE[CURLEN] = (FIRSTCODE[CURLEN - 1] + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = FIRSTCODE[CURLEN];
+ CURTEMP = 0;
+ while(CURTEMP < NTEMP) {
+ if(PREFLEN[CURTEMP] == CURLEN) {
+ CODES[CURTEMP] = CURCODE;
+ CURCODE = CURCODE + 1;
+ }
+ CURTEMP = CURTEMP + 1;
+ }
+ CURLEN = CURLEN + 1;
+ }
+ m_pModule->JBig2_Free(LENCOUNT);
+ m_pModule->JBig2_Free(FIRSTCODE);
+ return TRUE;
+failed:
+ return FALSE;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h
index df051c28d4..d68ced2fca 100644
--- a/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable.h
@@ -1,42 +1,42 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_HUFFMAN_TABLE_H_
-#define _JBIG2_HUFFMAN_TABLE_H_
-#include "JBig2_Module.h"
-#include "JBig2_HuffmanTable_Standard.h"
-#include "JBig2_BitStream.h"
-class CJBig2_HuffmanTable : public CJBig2_Object
-{
-public:
-
- CJBig2_HuffmanTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB);
-
- CJBig2_HuffmanTable(CJBig2_BitStream *pStream);
-
- ~CJBig2_HuffmanTable();
-
- void init();
-
- int parseFromStandardTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB);
-
- int parseFromCodedBuffer(CJBig2_BitStream *pStream);
-
- FX_BOOL isOK()
- {
- return m_bOK;
- }
-private:
- FX_BOOL HTOOB;
- int NTEMP;
- int *CODES;
- int *PREFLEN;
- int *RANGELEN;
- int *RANGELOW;
- FX_BOOL m_bOK;
- friend class CJBig2_HuffmanDecoder;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_HUFFMAN_TABLE_H_
+#define _JBIG2_HUFFMAN_TABLE_H_
+#include "JBig2_Module.h"
+#include "JBig2_HuffmanTable_Standard.h"
+#include "JBig2_BitStream.h"
+class CJBig2_HuffmanTable : public CJBig2_Object
+{
+public:
+
+ CJBig2_HuffmanTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB);
+
+ CJBig2_HuffmanTable(CJBig2_BitStream *pStream);
+
+ ~CJBig2_HuffmanTable();
+
+ void init();
+
+ int parseFromStandardTable(const JBig2TableLine *pTable, int nLines, FX_BOOL bHTOOB);
+
+ int parseFromCodedBuffer(CJBig2_BitStream *pStream);
+
+ FX_BOOL isOK()
+ {
+ return m_bOK;
+ }
+private:
+ FX_BOOL HTOOB;
+ int NTEMP;
+ int *CODES;
+ int *PREFLEN;
+ int *RANGELEN;
+ int *RANGELOW;
+ FX_BOOL m_bOK;
+ friend class CJBig2_HuffmanDecoder;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h b/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h
index f6114364f1..428b2c3671 100644
--- a/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h
+++ b/core/src/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h
@@ -1,251 +1,251 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_HUFFMAN_TABLE_STANDARD_H_
-#define _JBIG2_HUFFMAN_TABLE_STANDARD_H_
-typedef struct {
- int PREFLEN;
- int RANDELEN;
- int RANGELOW;
-} JBig2TableLine;
-const FX_BOOL HuffmanTable_HTOOB_B1 = FALSE;
-const JBig2TableLine HuffmanTable_B1[] = {
- { 1, 4, 0 },
- { 2, 8, 16 },
- { 3, 16, 272 },
- { 0, 32, -1 },
- { 3, 32, 65808 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B2 = TRUE;
-const JBig2TableLine HuffmanTable_B2[] = {
- { 1, 0, 0 },
- { 2, 0, 1 },
- { 3, 0, 2 },
- { 4, 3, 3 },
- { 5, 6, 11 },
- { 0, 32, -1 },
- { 6, 32, 75 },
- { 6, 0, 0 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B3 = TRUE;
-const JBig2TableLine HuffmanTable_B3[] = {
- { 8, 8, -256 },
- { 1, 0, 0 },
- { 2, 0, 1 },
- { 3, 0, 2 },
- { 4, 3, 3 },
- { 5, 6, 11 },
- { 8, 32, -257 },
- { 7, 32, 75 },
- { 6, 0, 0 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B4 = FALSE;
-const JBig2TableLine HuffmanTable_B4[] = {
- { 1, 0, 1 },
- { 2, 0, 2 },
- { 3, 0, 3 },
- { 4, 3, 4 },
- { 5, 6, 12 },
- { 0, 32, -1 },
- { 5, 32, 76 },
-};
-const FX_BOOL HuffmanTable_HTOOB_B5 = FALSE;
-const JBig2TableLine HuffmanTable_B5[] = {
- { 7, 8, -255 },
- { 1, 0, 1 },
- { 2, 0, 2 },
- { 3, 0, 3 },
- { 4, 3, 4 },
- { 5, 6, 12 },
- { 7, 32, -256 },
- { 6, 32, 76 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B6 = FALSE;
-const JBig2TableLine HuffmanTable_B6[] = {
- { 5, 10, -2048 },
- { 4, 9, -1024 },
- { 4, 8, -512 },
- { 4, 7, -256 },
- { 5, 6, -128 },
- { 5, 5, -64 },
- { 4, 5, -32 },
- { 2, 7, 0 },
- { 3, 7, 128 },
- { 3, 8, 256 },
- { 4, 9, 512 },
- { 4, 10, 1024 },
- { 6, 32, -2049 },
- { 6, 32, 2048 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B7 = FALSE;
-const JBig2TableLine HuffmanTable_B7[] = {
- { 4, 9, -1024 },
- { 3, 8, -512 },
- { 4, 7, -256 },
- { 5, 6, -128 },
- { 5, 5, -64 },
- { 4, 5, -32 },
- { 4, 5, 0 },
- { 5, 5, 32 },
- { 5, 6, 64 },
- { 4, 7, 128 },
- { 3, 8, 256 },
- { 3, 9, 512 },
- { 3, 10, 1024 },
- { 5, 32, -1025 },
- { 5, 32, 2048 },
-};
-const FX_BOOL HuffmanTable_HTOOB_B8 = TRUE;
-const JBig2TableLine HuffmanTable_B8[] = {
- { 8, 3, -15 },
- { 9, 1, -7 },
- { 8, 1, -5 },
- { 9, 0, -3 },
- { 7, 0, -2 },
- { 4, 0, -1 },
- { 2, 1, 0 },
- { 5, 0, 2 },
- { 6, 0, 3 },
- { 3, 4, 4 },
- { 6, 1, 20 },
- { 4, 4, 22 },
- { 4, 5, 38 },
- { 5, 6, 70 },
- { 5, 7, 134 },
- { 6, 7, 262 },
- { 7, 8, 390 },
- { 6, 10, 646 },
- { 9, 32, -16 },
- { 9, 32, 1670 },
- { 2, 0, 0 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B9 = TRUE;
-const JBig2TableLine HuffmanTable_B9[] = {
- { 8, 4, -31 },
- { 9, 2, -15 },
- { 8, 2, -11 },
- { 9, 1, -7 },
- { 7, 1, -5 },
- { 4, 1, -3 },
- { 3, 1, -1 },
- { 3, 1, 1 },
- { 5, 1, 3 },
- { 6, 1, 5 },
- { 3, 5, 7 },
- { 6, 2, 39 },
- { 4, 5, 43 },
- { 4, 6, 75 },
- { 5, 7, 139 },
- { 5, 8, 267 },
- { 6, 8, 523 },
- { 7, 9, 779 },
- { 6, 11, 1291 },
- { 9, 32, -32 },
- { 9, 32, 3339 },
- { 2, 0, 0 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B10 = TRUE;
-const JBig2TableLine HuffmanTable_B10[] = {
- { 7, 4, -21 },
- { 8, 0, -5 },
- { 7, 0, -4 },
- { 5, 0, -3 },
- { 2, 2, -2 },
- { 5, 0, 2 },
- { 6, 0, 3 },
- { 7, 0, 4 },
- { 8, 0, 5 },
- { 2, 6, 6 },
- { 5, 5, 70 },
- { 6, 5, 102 },
- { 6, 6, 134 },
- { 6, 7, 198 },
- { 6, 8, 326 },
- { 6, 9, 582 },
- { 6, 10, 1094 },
- { 7, 11, 2118 },
- { 8, 32, -22 },
- { 8, 32, 4166 },
- { 2, 0, 0 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B11 = FALSE;
-const JBig2TableLine HuffmanTable_B11[] = {
- { 1, 0, 1 },
- { 2, 1, 2 },
- { 4, 0, 4 },
- { 4, 1, 5 },
- { 5, 1, 7 },
- { 5, 2, 9 },
- { 6, 2, 13 },
- { 7, 2, 17 },
- { 7, 3, 21 },
- { 7, 4, 29 },
- { 7, 5, 45 },
- { 7, 6, 77 },
- { 0, 32, 0 },
- { 7, 32, 141 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B12 = FALSE;
-const JBig2TableLine HuffmanTable_B12[] = {
- { 1, 0, 1 },
- { 2, 0, 2 },
- { 3, 1, 3 },
- { 5, 0, 5 },
- { 5, 1, 6 },
- { 6, 1, 8 },
- { 7, 0, 10 },
- { 7, 1, 11 },
- { 7, 2, 13 },
- { 7, 3, 17 },
- { 7, 4, 25 },
- { 8, 5, 41 },
- { 0, 32, 0 },
- { 8, 32, 73 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B13 = FALSE;
-const JBig2TableLine HuffmanTable_B13[] = {
- { 1, 0, 1 },
- { 3, 0, 2 },
- { 4, 0, 3 },
- { 5, 0, 4 },
- { 4, 1, 5 },
- { 3, 3, 7 },
- { 6, 1, 15 },
- { 6, 2, 17 },
- { 6, 3, 21 },
- { 6, 4, 29 },
- { 6, 5, 45 },
- { 7, 6, 77 },
- { 0, 32, 0 },
- { 7, 32, 141 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B14 = FALSE;
-const JBig2TableLine HuffmanTable_B14[] = {
- { 3, 0, -2 },
- { 3, 0, -1 },
- { 1, 0, 0 },
- { 3, 0, 1 },
- { 3, 0, 2 },
- { 0, 32, -3 },
- { 0, 32, 3 }
-};
-const FX_BOOL HuffmanTable_HTOOB_B15 = FALSE;
-const JBig2TableLine HuffmanTable_B15[] = {
- { 7, 4, -24 },
- { 6, 2, -8 },
- { 5, 1, -4 },
- { 4, 0, -2 },
- { 3, 0, -1 },
- { 1, 0, 0 },
- { 3, 0, 1 },
- { 4, 0, 2 },
- { 5, 1, 3 },
- { 6, 2, 5 },
- { 7, 4, 9 },
- { 7, 32, -25 },
- { 7, 32, 25 }
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_HUFFMAN_TABLE_STANDARD_H_
+#define _JBIG2_HUFFMAN_TABLE_STANDARD_H_
+typedef struct {
+ int PREFLEN;
+ int RANDELEN;
+ int RANGELOW;
+} JBig2TableLine;
+const FX_BOOL HuffmanTable_HTOOB_B1 = FALSE;
+const JBig2TableLine HuffmanTable_B1[] = {
+ { 1, 4, 0 },
+ { 2, 8, 16 },
+ { 3, 16, 272 },
+ { 0, 32, -1 },
+ { 3, 32, 65808 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B2 = TRUE;
+const JBig2TableLine HuffmanTable_B2[] = {
+ { 1, 0, 0 },
+ { 2, 0, 1 },
+ { 3, 0, 2 },
+ { 4, 3, 3 },
+ { 5, 6, 11 },
+ { 0, 32, -1 },
+ { 6, 32, 75 },
+ { 6, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B3 = TRUE;
+const JBig2TableLine HuffmanTable_B3[] = {
+ { 8, 8, -256 },
+ { 1, 0, 0 },
+ { 2, 0, 1 },
+ { 3, 0, 2 },
+ { 4, 3, 3 },
+ { 5, 6, 11 },
+ { 8, 32, -257 },
+ { 7, 32, 75 },
+ { 6, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B4 = FALSE;
+const JBig2TableLine HuffmanTable_B4[] = {
+ { 1, 0, 1 },
+ { 2, 0, 2 },
+ { 3, 0, 3 },
+ { 4, 3, 4 },
+ { 5, 6, 12 },
+ { 0, 32, -1 },
+ { 5, 32, 76 },
+};
+const FX_BOOL HuffmanTable_HTOOB_B5 = FALSE;
+const JBig2TableLine HuffmanTable_B5[] = {
+ { 7, 8, -255 },
+ { 1, 0, 1 },
+ { 2, 0, 2 },
+ { 3, 0, 3 },
+ { 4, 3, 4 },
+ { 5, 6, 12 },
+ { 7, 32, -256 },
+ { 6, 32, 76 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B6 = FALSE;
+const JBig2TableLine HuffmanTable_B6[] = {
+ { 5, 10, -2048 },
+ { 4, 9, -1024 },
+ { 4, 8, -512 },
+ { 4, 7, -256 },
+ { 5, 6, -128 },
+ { 5, 5, -64 },
+ { 4, 5, -32 },
+ { 2, 7, 0 },
+ { 3, 7, 128 },
+ { 3, 8, 256 },
+ { 4, 9, 512 },
+ { 4, 10, 1024 },
+ { 6, 32, -2049 },
+ { 6, 32, 2048 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B7 = FALSE;
+const JBig2TableLine HuffmanTable_B7[] = {
+ { 4, 9, -1024 },
+ { 3, 8, -512 },
+ { 4, 7, -256 },
+ { 5, 6, -128 },
+ { 5, 5, -64 },
+ { 4, 5, -32 },
+ { 4, 5, 0 },
+ { 5, 5, 32 },
+ { 5, 6, 64 },
+ { 4, 7, 128 },
+ { 3, 8, 256 },
+ { 3, 9, 512 },
+ { 3, 10, 1024 },
+ { 5, 32, -1025 },
+ { 5, 32, 2048 },
+};
+const FX_BOOL HuffmanTable_HTOOB_B8 = TRUE;
+const JBig2TableLine HuffmanTable_B8[] = {
+ { 8, 3, -15 },
+ { 9, 1, -7 },
+ { 8, 1, -5 },
+ { 9, 0, -3 },
+ { 7, 0, -2 },
+ { 4, 0, -1 },
+ { 2, 1, 0 },
+ { 5, 0, 2 },
+ { 6, 0, 3 },
+ { 3, 4, 4 },
+ { 6, 1, 20 },
+ { 4, 4, 22 },
+ { 4, 5, 38 },
+ { 5, 6, 70 },
+ { 5, 7, 134 },
+ { 6, 7, 262 },
+ { 7, 8, 390 },
+ { 6, 10, 646 },
+ { 9, 32, -16 },
+ { 9, 32, 1670 },
+ { 2, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B9 = TRUE;
+const JBig2TableLine HuffmanTable_B9[] = {
+ { 8, 4, -31 },
+ { 9, 2, -15 },
+ { 8, 2, -11 },
+ { 9, 1, -7 },
+ { 7, 1, -5 },
+ { 4, 1, -3 },
+ { 3, 1, -1 },
+ { 3, 1, 1 },
+ { 5, 1, 3 },
+ { 6, 1, 5 },
+ { 3, 5, 7 },
+ { 6, 2, 39 },
+ { 4, 5, 43 },
+ { 4, 6, 75 },
+ { 5, 7, 139 },
+ { 5, 8, 267 },
+ { 6, 8, 523 },
+ { 7, 9, 779 },
+ { 6, 11, 1291 },
+ { 9, 32, -32 },
+ { 9, 32, 3339 },
+ { 2, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B10 = TRUE;
+const JBig2TableLine HuffmanTable_B10[] = {
+ { 7, 4, -21 },
+ { 8, 0, -5 },
+ { 7, 0, -4 },
+ { 5, 0, -3 },
+ { 2, 2, -2 },
+ { 5, 0, 2 },
+ { 6, 0, 3 },
+ { 7, 0, 4 },
+ { 8, 0, 5 },
+ { 2, 6, 6 },
+ { 5, 5, 70 },
+ { 6, 5, 102 },
+ { 6, 6, 134 },
+ { 6, 7, 198 },
+ { 6, 8, 326 },
+ { 6, 9, 582 },
+ { 6, 10, 1094 },
+ { 7, 11, 2118 },
+ { 8, 32, -22 },
+ { 8, 32, 4166 },
+ { 2, 0, 0 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B11 = FALSE;
+const JBig2TableLine HuffmanTable_B11[] = {
+ { 1, 0, 1 },
+ { 2, 1, 2 },
+ { 4, 0, 4 },
+ { 4, 1, 5 },
+ { 5, 1, 7 },
+ { 5, 2, 9 },
+ { 6, 2, 13 },
+ { 7, 2, 17 },
+ { 7, 3, 21 },
+ { 7, 4, 29 },
+ { 7, 5, 45 },
+ { 7, 6, 77 },
+ { 0, 32, 0 },
+ { 7, 32, 141 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B12 = FALSE;
+const JBig2TableLine HuffmanTable_B12[] = {
+ { 1, 0, 1 },
+ { 2, 0, 2 },
+ { 3, 1, 3 },
+ { 5, 0, 5 },
+ { 5, 1, 6 },
+ { 6, 1, 8 },
+ { 7, 0, 10 },
+ { 7, 1, 11 },
+ { 7, 2, 13 },
+ { 7, 3, 17 },
+ { 7, 4, 25 },
+ { 8, 5, 41 },
+ { 0, 32, 0 },
+ { 8, 32, 73 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B13 = FALSE;
+const JBig2TableLine HuffmanTable_B13[] = {
+ { 1, 0, 1 },
+ { 3, 0, 2 },
+ { 4, 0, 3 },
+ { 5, 0, 4 },
+ { 4, 1, 5 },
+ { 3, 3, 7 },
+ { 6, 1, 15 },
+ { 6, 2, 17 },
+ { 6, 3, 21 },
+ { 6, 4, 29 },
+ { 6, 5, 45 },
+ { 7, 6, 77 },
+ { 0, 32, 0 },
+ { 7, 32, 141 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B14 = FALSE;
+const JBig2TableLine HuffmanTable_B14[] = {
+ { 3, 0, -2 },
+ { 3, 0, -1 },
+ { 1, 0, 0 },
+ { 3, 0, 1 },
+ { 3, 0, 2 },
+ { 0, 32, -3 },
+ { 0, 32, 3 }
+};
+const FX_BOOL HuffmanTable_HTOOB_B15 = FALSE;
+const JBig2TableLine HuffmanTable_B15[] = {
+ { 7, 4, -24 },
+ { 6, 2, -8 },
+ { 5, 1, -4 },
+ { 4, 0, -2 },
+ { 3, 0, -1 },
+ { 1, 0, 0 },
+ { 3, 0, 1 },
+ { 4, 0, 2 },
+ { 5, 1, 3 },
+ { 6, 2, 5 },
+ { 7, 4, 9 },
+ { 7, 32, -25 },
+ { 7, 32, 25 }
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Image.cpp b/core/src/fxcodec/jbig2/JBig2_Image.cpp
index 1719028176..09353c0e81 100644
--- a/core/src/fxcodec/jbig2/JBig2_Image.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_Image.cpp
@@ -1,1619 +1,1619 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_Image.h"
-#include "../../../include/fxcrt/fx_basic.h"
-#include "../../../include/fxcrt/fx_coordinates.h"
-#include <limits.h>
-CJBig2_Image::CJBig2_Image(FX_INT32 w, FX_INT32 h)
-{
- m_nWidth = w;
- m_nHeight = h;
- if (m_nWidth <= 0 || m_nHeight <= 0 || m_nWidth > INT_MAX - 31) {
- m_pData = NULL;
- m_bNeedFree = FALSE;
- return;
- }
- m_nStride = ((w + 31) >> 5) << 2;
- if (m_nStride * m_nHeight > 0 && 104857600 / (int)m_nStride > m_nHeight) {
- m_pData = (FX_BYTE *)m_pModule->JBig2_Malloc2(m_nStride, m_nHeight);
- } else {
- m_pData = NULL;
- }
- m_bNeedFree = TRUE;
-}
-CJBig2_Image::CJBig2_Image(FX_INT32 w, FX_INT32 h, FX_INT32 stride, FX_BYTE*pBuf)
-{
- m_nWidth = w;
- m_nHeight = h;
- m_nStride = stride;
- m_pData = pBuf;
- m_bNeedFree = FALSE;
-}
-CJBig2_Image::CJBig2_Image(CJBig2_Image &im)
-{
- m_pModule = im.m_pModule;
- m_nWidth = im.m_nWidth;
- m_nHeight = im.m_nHeight;
- m_nStride = im.m_nStride;
- if (im.m_pData) {
- m_pData = (FX_BYTE*)m_pModule->JBig2_Malloc2(m_nStride, m_nHeight);
- JBIG2_memcpy(m_pData, im.m_pData, m_nStride * m_nHeight);
- } else {
- m_pData = NULL;
- }
- m_bNeedFree = TRUE;
-}
-CJBig2_Image::~CJBig2_Image()
-{
- if(m_bNeedFree && m_pData) {
- m_pModule->JBig2_Free(m_pData);
- }
-}
-FX_BOOL CJBig2_Image::getPixel(FX_INT32 x, FX_INT32 y)
-{
- if (!m_pData) {
- return 0;
- }
- FX_INT32 m, n;
- if(x < 0 || x >= m_nWidth) {
- return 0;
- }
- if(y < 0 || y >= m_nHeight) {
- return 0;
- }
- m = y * m_nStride + (x >> 3);
- n = x & 7;
- return ((m_pData[m] >> (7 - n)) & 1);
-}
-
-FX_INT32 CJBig2_Image::setPixel(FX_INT32 x, FX_INT32 y, FX_BOOL v)
-{
- if (!m_pData) {
- return 0;
- }
- FX_INT32 m, n;
- if(x < 0 || x >= m_nWidth) {
- return 0;
- }
- if(y < 0 || y >= m_nHeight) {
- return 0;
- }
- m = y * m_nStride + (x >> 3);
- n = x & 7;
- if(v) {
- m_pData[m] |= 1 << (7 - n);
- } else {
- m_pData[m] &= ~(1 << (7 - n));
- }
- return 1;
-}
-void CJBig2_Image::copyLine(FX_INT32 hTo, FX_INT32 hFrom)
-{
- if (!m_pData) {
- return;
- }
- if(hFrom < 0 || hFrom >= m_nHeight) {
- JBIG2_memset(m_pData + hTo * m_nStride, 0, m_nStride);
- } else {
- JBIG2_memcpy(m_pData + hTo * m_nStride, m_pData + hFrom * m_nStride, m_nStride);
- }
-}
-void CJBig2_Image::fill(FX_BOOL v)
-{
- if (!m_pData) {
- return;
- }
- JBIG2_memset(m_pData, v ? 0xff : 0, m_nStride * m_nHeight);
-}
-FX_BOOL CJBig2_Image::composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
-{
- if (!m_pData) {
- return FALSE;
- }
- return composeTo_opt2(pDst, x, y, op);
-}
-FX_BOOL CJBig2_Image::composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect)
-{
- if (!m_pData) {
- return FALSE;
- }
- if (NULL == pSrcRect || *pSrcRect == FX_RECT(0, 0, m_nWidth, m_nHeight)) {
- return composeTo_opt2(pDst, x, y, op);
- }
- return composeTo_opt2(pDst, x, y, op, pSrcRect);
-}
-FX_BOOL CJBig2_Image::composeTo_unopt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
-{
- FX_INT32 w, h, dx, dy;
- FX_INT32 i, j;
- w = m_nWidth;
- h = m_nHeight;
- dx = dy = 0;
- if(x < 0) {
- dx += -x;
- w -= -x;
- x = 0;
- }
- if(y < 0) {
- dy += -y;
- h -= -y;
- y = 0;
- }
- if(x + w > pDst->m_nWidth) {
- w = pDst->m_nWidth - x;
- }
- if(y + h > pDst->m_nHeight) {
- h = pDst->m_nHeight - y;
- }
- switch(op) {
- case JBIG2_COMPOSE_OR:
- for(j = 0; j < h; j++) {
- for(i = 0; i < w; i++) {
- pDst->setPixel(x + i, y + j,
- (getPixel(i + dx, j + dy) | pDst->getPixel(x + i, y + j)) & 1);
- }
- }
- break;
- case JBIG2_COMPOSE_AND:
- for(j = 0; j < h; j++) {
- for(i = 0; i < w; i++) {
- pDst->setPixel(x + i, y + j,
- (getPixel(i + dx, j + dy) & pDst->getPixel(x + i, y + j)) & 1);
- }
- }
- break;
- case JBIG2_COMPOSE_XOR:
- for(j = 0; j < h; j++) {
- for(i = 0; i < w; i++) {
- pDst->setPixel(x + i, y + j,
- (getPixel(i + dx, j + dy) ^ pDst->getPixel(x + i, y + j)) & 1);
- }
- }
- break;
- case JBIG2_COMPOSE_XNOR:
- for(j = 0; j < h; j++) {
- for(i = 0; i < w; i++) {
- pDst->setPixel(x + i, y + j,
- (~(getPixel(i + dx, j + dy) ^ pDst->getPixel(x + i, y + j))) & 1);
- }
- }
- break;
- case JBIG2_COMPOSE_REPLACE:
- for(j = 0; j < h; j++) {
- for(i = 0; i < w; i++) {
- pDst->setPixel(x + i, y + j, getPixel(i + dx, j + dy));
- }
- }
- break;
- }
- return TRUE;
-}
-
-FX_BOOL CJBig2_Image::composeTo_opt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
-{
- FX_INT32 x0, x1, y0, y1, xx, yy;
- FX_BYTE *pLineSrc, *pLineDst, *srcPtr, *destPtr;
- FX_DWORD src0, src1, src, dest, s1, s2, m1, m2, m3;
- FX_BOOL oneByte;
- if (!m_pData) {
- return FALSE;
- }
- if (y < 0) {
- y0 = -y;
- } else {
- y0 = 0;
- }
- if (y + m_nHeight > pDst->m_nHeight) {
- y1 = pDst->m_nHeight - y;
- } else {
- y1 = m_nHeight;
- }
- if (y0 >= y1) {
- return FALSE;
- }
- if (x >= 0) {
- x0 = x & ~7;
- } else {
- x0 = 0;
- }
- x1 = x + m_nWidth;
- if (x1 > pDst->m_nWidth) {
- x1 = pDst->m_nWidth;
- }
- if (x0 >= x1) {
- return FALSE;
- }
- s1 = x & 7;
- s2 = 8 - s1;
- m1 = 0xff >> (x1 & 7);
- m2 = 0xff << (((x1 & 7) == 0) ? 0 : 8 - (x1 & 7));
- m3 = (0xff >> s1) & m2;
- oneByte = x0 == ((x1 - 1) & ~7);
- pLineDst = pDst->m_pData + y * pDst->m_nStride;
- pLineSrc = m_pData + y0 * m_nStride;
- if(oneByte) {
- if(x >= 0) {
- switch(op) {
- case JBIG2_COMPOSE_OR: {
- for (yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- dest = *destPtr;
- dest |= (*srcPtr >> s1) & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_AND: {
- for (yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- dest = *destPtr;
- dest &= ((0xff00 | *srcPtr) >> s1) | m1;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_XOR: {
- for (yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- dest = *destPtr;
- dest ^= (*srcPtr >> s1) & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_XNOR: {
- for (yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- dest = *destPtr;
- dest ^= ((*srcPtr ^ 0xff) >> s1) & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_REPLACE: {
- for (yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- dest = *destPtr;
- dest = (dest & ~m3) | ((*srcPtr >> s1) & m3);
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- }
- } else {
- switch(op) {
- case JBIG2_COMPOSE_OR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- dest = *destPtr;
- dest |= *srcPtr & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_AND: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- dest = *destPtr;
- dest &= *srcPtr | m1;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_XOR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- dest = *destPtr;
- dest ^= *srcPtr & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_XNOR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- dest = *destPtr;
- dest ^= (*srcPtr ^ 0xff) & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_REPLACE: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- dest = *destPtr;
- dest = (*srcPtr & m2) | (dest & m1);
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- }
- }
- } else {
- if(x >= 0) {
- switch(op) {
- case JBIG2_COMPOSE_OR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- src1 = *srcPtr++;
- dest = *destPtr;
- dest |= src1 >> s1;
- *destPtr++ = (FX_BYTE)dest;
- xx = x0 + 8;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest |= src;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest |= src & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_AND: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- src1 = *srcPtr++;
- dest = *destPtr;
- dest &= (0xff00 | src1) >> s1;
- *destPtr++ = (FX_BYTE)dest;
- xx = x0 + 8;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest &= src;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest &= src | m1;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_XOR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- src1 = *srcPtr++;
- dest = *destPtr;
- dest ^= src1 >> s1;
- *destPtr++ = (FX_BYTE)dest;
- xx = x0 + 8;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest ^= src;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest ^= src & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_XNOR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- src1 = *srcPtr++;
- dest = *destPtr;
- dest ^= (src1 ^ 0xff) >> s1;
- *destPtr++ = (FX_BYTE)dest;
- xx = x0 + 8;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest ^= src ^ 0xff;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest ^= (src ^ 0xff) & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_REPLACE: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst + (x >> 3);
- srcPtr = pLineSrc;
- src1 = *srcPtr++;
- dest = *destPtr;
- dest = (dest & (0xff << s2)) | (src1 >> s1);
- *destPtr++ = (FX_BYTE)dest;
- xx = x0 + 8;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest = src;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest = (src & m2) | (dest & m1);
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- }
- } else {
- switch(op) {
- case JBIG2_COMPOSE_OR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- src1 = *srcPtr++;
- xx = x0;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest |= src;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest |= src & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_AND: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- src1 = *srcPtr++;
- xx = x0;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest &= src;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest &= src | m1;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_XOR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- src1 = *srcPtr++;
- xx = x0;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest ^= src;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest ^= src & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_XNOR: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- src1 = *srcPtr++;
- xx = x0;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest ^= src ^ 0xff;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest ^= (src ^ 0xff) & m2;
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- case JBIG2_COMPOSE_REPLACE: {
- for(yy = y0; yy < y1; ++yy) {
- destPtr = pLineDst;
- srcPtr = pLineSrc + (-x >> 3);
- src1 = *srcPtr++;
- xx = x0;
- for (; xx < x1 - 8; xx += 8) {
- dest = *destPtr;
- src0 = src1;
- src1 = *srcPtr++;
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest = src;
- *destPtr++ = (FX_BYTE)dest;
- }
- dest = *destPtr;
- src0 = src1;
- if(srcPtr - pLineSrc < m_nStride) {
- src1 = *srcPtr++;
- } else {
- src1 = 0;
- }
- src = (((src0 << 8) | src1) >> s1) & 0xff;
- dest = (src & m2) | (dest & m1);
- *destPtr = (FX_BYTE)dest;
- pLineDst += pDst->m_nStride;
- pLineSrc += m_nStride;
- }
- }
- break;
- }
- }
- }
- return TRUE;
-}
-FX_BOOL CJBig2_Image::composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op)
-{
- if (!m_pData) {
- return FALSE;
- }
- return pSrc->composeTo(this, x, y, op);
-}
-FX_BOOL CJBig2_Image::composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op, const FX_RECT* pSrcRect)
-{
- if (!m_pData) {
- return FALSE;
- }
- return pSrc->composeTo(this, x, y, op, pSrcRect);
-}
-CJBig2_Image *CJBig2_Image::subImage_unopt(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h)
-{
- CJBig2_Image *pImage;
- FX_INT32 i, j;
- JBIG2_ALLOC(pImage, CJBig2_Image(w, h));
- for(j = 0; j < h; j++) {
- for(i = 0; i < w; i++) {
- pImage->setPixel(i, j, getPixel(x + i, y + j));
- }
- }
- return pImage;
-}
-#define JBIG2_GETDWORD(buf) ((FX_DWORD)(((buf)[0] << 24) | ((buf)[1] << 16) | ((buf)[2] << 8) | (buf)[3]))
-CJBig2_Image *CJBig2_Image::subImage(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h)
-{
- CJBig2_Image *pImage;
- FX_INT32 m, n, j;
- FX_BYTE *pLineSrc, *pLineDst;
- FX_DWORD wTmp;
- FX_BYTE *pSrc, *pSrcEnd, *pDst, *pDstEnd;
- if (w == 0 || h == 0) {
- return NULL;
- }
- JBIG2_ALLOC(pImage, CJBig2_Image(w, h));
- if (!m_pData) {
- pImage->fill(0);
- return pImage;
- }
- if (!pImage->m_pData) {
- return pImage;
- }
- pLineSrc = m_pData + m_nStride * y;
- pLineDst = pImage->m_pData;
- m = (x >> 5) << 2;
- n = x & 31;
- if(n == 0) {
- for(j = 0; j < h; j++) {
- pSrc = pLineSrc + m;
- pSrcEnd = pLineSrc + m_nStride;
- pDst = pLineDst;
- pDstEnd = pLineDst + pImage->m_nStride;
- for(; pDst < pDstEnd; pSrc += 4, pDst += 4) {
- *((FX_DWORD *)pDst) = *((FX_DWORD *)pSrc);
- }
- pLineSrc += m_nStride;
- pLineDst += pImage->m_nStride;
- }
- } else {
- for(j = 0; j < h; j++) {
- pSrc = pLineSrc + m;
- pSrcEnd = pLineSrc + m_nStride;
- pDst = pLineDst;
- pDstEnd = pLineDst + pImage->m_nStride;
- for(; pDst < pDstEnd; pSrc += 4, pDst += 4) {
- if(pSrc + 4 < pSrcEnd) {
- wTmp = (JBIG2_GETDWORD(pSrc) << n) | (JBIG2_GETDWORD(pSrc + 4) >> (32 - n));
- } else {
- wTmp = JBIG2_GETDWORD(pSrc) << n;
- }
- pDst[0] = (FX_BYTE)(wTmp >> 24);
- pDst[1] = (FX_BYTE)(wTmp >> 16);
- pDst[2] = (FX_BYTE)(wTmp >> 8);
- pDst[3] = (FX_BYTE)wTmp;
- }
- pLineSrc += m_nStride;
- pLineDst += pImage->m_nStride;
- }
- }
- return pImage;
-}
-void CJBig2_Image::expand(FX_INT32 h, FX_BOOL v)
-{
- if (!m_pData) {
- return;
- }
- m_pData = (FX_BYTE*)m_pModule->JBig2_Realloc(m_pData, h * m_nStride);
- if(h > m_nHeight) {
- JBIG2_memset(m_pData + m_nHeight * m_nStride, v ? 0xff : 0, (h - m_nHeight)*m_nStride);
- }
- m_nHeight = h;
-}
-FX_BOOL CJBig2_Image::composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
-{
- FX_INT32 xs0, ys0, xs1, ys1, xd0, yd0, xd1, yd1, xx, yy, w, h, middleDwords, lineLeft;
- FX_DWORD s1, d1, d2, shift, shift1, shift2, tmp, tmp1, tmp2, maskL, maskR, maskM;
- FX_BYTE *lineSrc, *lineDst, *sp, *dp;
- if (!m_pData) {
- return FALSE;
- }
- if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) {
- return FALSE;
- }
- if(y < 0) {
- ys0 = -y;
- } else {
- ys0 = 0;
- }
- if(y + m_nHeight > pDst->m_nHeight) {
- ys1 = pDst->m_nHeight - y;
- } else {
- ys1 = m_nHeight;
- }
- if(x < 0) {
- xs0 = -x;
- } else {
- xs0 = 0;
- }
- if(x + m_nWidth > pDst->m_nWidth) {
- xs1 = pDst->m_nWidth - x;
- } else {
- xs1 = m_nWidth;
- }
- if((ys0 >= ys1) || (xs0 >= xs1)) {
- return 0;
- }
- w = xs1 - xs0;
- h = ys1 - ys0;
- if(y < 0) {
- yd0 = 0;
- } else {
- yd0 = y;
- }
- if(x < 0) {
- xd0 = 0;
- } else {
- xd0 = x;
- }
- xd1 = xd0 + w;
- yd1 = yd0 + h;
- d1 = xd0 & 31;
- d2 = xd1 & 31;
- s1 = xs0 & 31;
- maskL = 0xffffffff >> d1;
- maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32);
- maskM = maskL & maskR;
- lineSrc = m_pData + ys0 * m_nStride + ((xs0 >> 5) << 2);
- lineLeft = m_nStride - ((xs0 >> 5) << 2);
- lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2);
- if((xd0 & ~31) == ((xd1 - 1) & ~31)) {
- if((xs0 & ~31) == ((xs1 - 1) & ~31)) {
- if(s1 > d1) {
- shift = s1 - d1;
- for(yy = yd0; yy < yd1; yy++) {
- tmp1 = JBIG2_GETDWORD(lineSrc) << shift;
- tmp2 = JBIG2_GETDWORD(lineDst);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
- break;
- }
- lineDst[0] = (FX_BYTE)(tmp >> 24);
- lineDst[1] = (FX_BYTE)(tmp >> 16);
- lineDst[2] = (FX_BYTE)(tmp >> 8);
- lineDst[3] = (FX_BYTE)tmp;
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- } else {
- shift = d1 - s1;
- for(yy = yd0; yy < yd1; yy++) {
- tmp1 = JBIG2_GETDWORD(lineSrc) >> shift;
- tmp2 = JBIG2_GETDWORD(lineDst);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
- break;
- }
- lineDst[0] = (FX_BYTE)(tmp >> 24);
- lineDst[1] = (FX_BYTE)(tmp >> 16);
- lineDst[2] = (FX_BYTE)(tmp >> 8);
- lineDst[3] = (FX_BYTE)tmp;
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- }
- } else {
- shift1 = s1 - d1;
- shift2 = 32 - shift1;
- for(yy = yd0; yy < yd1; yy++) {
- tmp1 = (JBIG2_GETDWORD(lineSrc) << shift1) | (JBIG2_GETDWORD(lineSrc + 4) >> shift2);
- tmp2 = JBIG2_GETDWORD(lineDst);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
- break;
- }
- lineDst[0] = (FX_BYTE)(tmp >> 24);
- lineDst[1] = (FX_BYTE)(tmp >> 16);
- lineDst[2] = (FX_BYTE)(tmp >> 8);
- lineDst[3] = (FX_BYTE)tmp;
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- }
- } else {
- if(s1 > d1) {
- shift1 = s1 - d1;
- shift2 = 32 - shift1;
- middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
- for(yy = yd0; yy < yd1; yy++) {
- sp = lineSrc;
- dp = lineDst;
- if(d1 != 0) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- for(xx = 0; xx < middleDwords; xx++) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = tmp1 | tmp2;
- break;
- case JBIG2_COMPOSE_AND:
- tmp = tmp1 & tmp2;
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = tmp1 ^ tmp2;
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = ~(tmp1 ^ tmp2);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = tmp1;
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- if(d2 != 0) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (
- ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift2);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- }
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- } else if(s1 == d1) {
- middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
- for(yy = yd0; yy < yd1; yy++) {
- sp = lineSrc;
- dp = lineDst;
- if(d1 != 0) {
- tmp1 = JBIG2_GETDWORD(sp);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- for(xx = 0; xx < middleDwords; xx++) {
- tmp1 = JBIG2_GETDWORD(sp);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = tmp1 | tmp2;
- break;
- case JBIG2_COMPOSE_AND:
- tmp = tmp1 & tmp2;
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = tmp1 ^ tmp2;
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = ~(tmp1 ^ tmp2);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = tmp1;
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- if(d2 != 0) {
- tmp1 = JBIG2_GETDWORD(sp);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- }
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- } else {
- shift1 = d1 - s1;
- shift2 = 32 - shift1;
- middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
- for(yy = yd0; yy < yd1; yy++) {
- sp = lineSrc;
- dp = lineDst;
- if(d1 != 0) {
- tmp1 = JBIG2_GETDWORD(sp) >> shift1;
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- dp += 4;
- }
- for(xx = 0; xx < middleDwords; xx++) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift2) | ((JBIG2_GETDWORD(sp + 4)) >> shift1);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = tmp1 | tmp2;
- break;
- case JBIG2_COMPOSE_AND:
- tmp = tmp1 & tmp2;
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = tmp1 ^ tmp2;
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = ~(tmp1 ^ tmp2);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = tmp1;
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- if(d2 != 0) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift2) | (
- ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift1);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- }
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- }
- }
- return 1;
-}
-FX_BOOL CJBig2_Image::composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect)
-{
- FX_INT32 xs0, ys0, xs1, ys1, xd0, yd0, xd1, yd1, xx, yy, w, h, middleDwords, lineLeft;
- FX_DWORD s1, d1, d2, shift, shift1, shift2, tmp, tmp1, tmp2, maskL, maskR, maskM;
- FX_BYTE *lineSrc, *lineDst, *sp, *dp;
- FX_INT32 sw, sh;
- if (!m_pData) {
- return FALSE;
- }
- if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) {
- return FALSE;
- }
- sw = pSrcRect->Width();
- sh = pSrcRect->Height();
- if(y < 0) {
- ys0 = -y;
- } else {
- ys0 = 0;
- }
- if(y + sh > pDst->m_nHeight) {
- ys1 = pDst->m_nHeight - y;
- } else {
- ys1 = sh;
- }
- if(x < 0) {
- xs0 = -x;
- } else {
- xs0 = 0;
- }
- if(x + sw > pDst->m_nWidth) {
- xs1 = pDst->m_nWidth - x;
- } else {
- xs1 = sw;
- }
- if((ys0 >= ys1) || (xs0 >= xs1)) {
- return 0;
- }
- w = xs1 - xs0;
- h = ys1 - ys0;
- if(y < 0) {
- yd0 = 0;
- } else {
- yd0 = y;
- }
- if(x < 0) {
- xd0 = 0;
- } else {
- xd0 = x;
- }
- xd1 = xd0 + w;
- yd1 = yd0 + h;
- d1 = xd0 & 31;
- d2 = xd1 & 31;
- s1 = xs0 & 31;
- maskL = 0xffffffff >> d1;
- maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32);
- maskM = maskL & maskR;
- lineSrc = m_pData + (pSrcRect->top + ys0) * m_nStride + (((xs0 + pSrcRect->left) >> 5) << 2);
- lineLeft = m_nStride - ((xs0 >> 5) << 2);
- lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2);
- if((xd0 & ~31) == ((xd1 - 1) & ~31)) {
- if((xs0 & ~31) == ((xs1 - 1) & ~31)) {
- if(s1 > d1) {
- shift = s1 - d1;
- for(yy = yd0; yy < yd1; yy++) {
- tmp1 = JBIG2_GETDWORD(lineSrc) << shift;
- tmp2 = JBIG2_GETDWORD(lineDst);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
- break;
- }
- lineDst[0] = (FX_BYTE)(tmp >> 24);
- lineDst[1] = (FX_BYTE)(tmp >> 16);
- lineDst[2] = (FX_BYTE)(tmp >> 8);
- lineDst[3] = (FX_BYTE)tmp;
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- } else {
- shift = d1 - s1;
- for(yy = yd0; yy < yd1; yy++) {
- tmp1 = JBIG2_GETDWORD(lineSrc) >> shift;
- tmp2 = JBIG2_GETDWORD(lineDst);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
- break;
- }
- lineDst[0] = (FX_BYTE)(tmp >> 24);
- lineDst[1] = (FX_BYTE)(tmp >> 16);
- lineDst[2] = (FX_BYTE)(tmp >> 8);
- lineDst[3] = (FX_BYTE)tmp;
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- }
- } else {
- shift1 = s1 - d1;
- shift2 = 32 - shift1;
- for(yy = yd0; yy < yd1; yy++) {
- tmp1 = (JBIG2_GETDWORD(lineSrc) << shift1) | (JBIG2_GETDWORD(lineSrc + 4) >> shift2);
- tmp2 = JBIG2_GETDWORD(lineDst);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
- break;
- }
- lineDst[0] = (FX_BYTE)(tmp >> 24);
- lineDst[1] = (FX_BYTE)(tmp >> 16);
- lineDst[2] = (FX_BYTE)(tmp >> 8);
- lineDst[3] = (FX_BYTE)tmp;
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- }
- } else {
- if(s1 > d1) {
- shift1 = s1 - d1;
- shift2 = 32 - shift1;
- middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
- for(yy = yd0; yy < yd1; yy++) {
- sp = lineSrc;
- dp = lineDst;
- if(d1 != 0) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- for(xx = 0; xx < middleDwords; xx++) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = tmp1 | tmp2;
- break;
- case JBIG2_COMPOSE_AND:
- tmp = tmp1 & tmp2;
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = tmp1 ^ tmp2;
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = ~(tmp1 ^ tmp2);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = tmp1;
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- if(d2 != 0) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (
- ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift2);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- }
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- } else if(s1 == d1) {
- middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
- for(yy = yd0; yy < yd1; yy++) {
- sp = lineSrc;
- dp = lineDst;
- if(d1 != 0) {
- tmp1 = JBIG2_GETDWORD(sp);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- for(xx = 0; xx < middleDwords; xx++) {
- tmp1 = JBIG2_GETDWORD(sp);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = tmp1 | tmp2;
- break;
- case JBIG2_COMPOSE_AND:
- tmp = tmp1 & tmp2;
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = tmp1 ^ tmp2;
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = ~(tmp1 ^ tmp2);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = tmp1;
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- if(d2 != 0) {
- tmp1 = JBIG2_GETDWORD(sp);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- }
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- } else {
- shift1 = d1 - s1;
- shift2 = 32 - shift1;
- middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
- for(yy = yd0; yy < yd1; yy++) {
- sp = lineSrc;
- dp = lineDst;
- if(d1 != 0) {
- tmp1 = JBIG2_GETDWORD(sp) >> shift1;
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- dp += 4;
- }
- for(xx = 0; xx < middleDwords; xx++) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift2) | ((JBIG2_GETDWORD(sp + 4)) >> shift1);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = tmp1 | tmp2;
- break;
- case JBIG2_COMPOSE_AND:
- tmp = tmp1 & tmp2;
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = tmp1 ^ tmp2;
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = ~(tmp1 ^ tmp2);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = tmp1;
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- sp += 4;
- dp += 4;
- }
- if(d2 != 0) {
- tmp1 = (JBIG2_GETDWORD(sp) << shift2) | (
- ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift1);
- tmp2 = JBIG2_GETDWORD(dp);
- switch(op) {
- case JBIG2_COMPOSE_OR:
- tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_AND:
- tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XOR:
- tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
- break;
- case JBIG2_COMPOSE_XNOR:
- tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
- break;
- case JBIG2_COMPOSE_REPLACE:
- tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
- break;
- }
- dp[0] = (FX_BYTE)(tmp >> 24);
- dp[1] = (FX_BYTE)(tmp >> 16);
- dp[2] = (FX_BYTE)(tmp >> 8);
- dp[3] = (FX_BYTE)tmp;
- }
- lineSrc += m_nStride;
- lineDst += pDst->m_nStride;
- }
- }
- }
- return 1;
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_Image.h"
+#include "../../../include/fxcrt/fx_basic.h"
+#include "../../../include/fxcrt/fx_coordinates.h"
+#include <limits.h>
+CJBig2_Image::CJBig2_Image(FX_INT32 w, FX_INT32 h)
+{
+ m_nWidth = w;
+ m_nHeight = h;
+ if (m_nWidth <= 0 || m_nHeight <= 0 || m_nWidth > INT_MAX - 31) {
+ m_pData = NULL;
+ m_bNeedFree = FALSE;
+ return;
+ }
+ m_nStride = ((w + 31) >> 5) << 2;
+ if (m_nStride * m_nHeight > 0 && 104857600 / (int)m_nStride > m_nHeight) {
+ m_pData = (FX_BYTE *)m_pModule->JBig2_Malloc2(m_nStride, m_nHeight);
+ } else {
+ m_pData = NULL;
+ }
+ m_bNeedFree = TRUE;
+}
+CJBig2_Image::CJBig2_Image(FX_INT32 w, FX_INT32 h, FX_INT32 stride, FX_BYTE*pBuf)
+{
+ m_nWidth = w;
+ m_nHeight = h;
+ m_nStride = stride;
+ m_pData = pBuf;
+ m_bNeedFree = FALSE;
+}
+CJBig2_Image::CJBig2_Image(CJBig2_Image &im)
+{
+ m_pModule = im.m_pModule;
+ m_nWidth = im.m_nWidth;
+ m_nHeight = im.m_nHeight;
+ m_nStride = im.m_nStride;
+ if (im.m_pData) {
+ m_pData = (FX_BYTE*)m_pModule->JBig2_Malloc2(m_nStride, m_nHeight);
+ JBIG2_memcpy(m_pData, im.m_pData, m_nStride * m_nHeight);
+ } else {
+ m_pData = NULL;
+ }
+ m_bNeedFree = TRUE;
+}
+CJBig2_Image::~CJBig2_Image()
+{
+ if(m_bNeedFree && m_pData) {
+ m_pModule->JBig2_Free(m_pData);
+ }
+}
+FX_BOOL CJBig2_Image::getPixel(FX_INT32 x, FX_INT32 y)
+{
+ if (!m_pData) {
+ return 0;
+ }
+ FX_INT32 m, n;
+ if(x < 0 || x >= m_nWidth) {
+ return 0;
+ }
+ if(y < 0 || y >= m_nHeight) {
+ return 0;
+ }
+ m = y * m_nStride + (x >> 3);
+ n = x & 7;
+ return ((m_pData[m] >> (7 - n)) & 1);
+}
+
+FX_INT32 CJBig2_Image::setPixel(FX_INT32 x, FX_INT32 y, FX_BOOL v)
+{
+ if (!m_pData) {
+ return 0;
+ }
+ FX_INT32 m, n;
+ if(x < 0 || x >= m_nWidth) {
+ return 0;
+ }
+ if(y < 0 || y >= m_nHeight) {
+ return 0;
+ }
+ m = y * m_nStride + (x >> 3);
+ n = x & 7;
+ if(v) {
+ m_pData[m] |= 1 << (7 - n);
+ } else {
+ m_pData[m] &= ~(1 << (7 - n));
+ }
+ return 1;
+}
+void CJBig2_Image::copyLine(FX_INT32 hTo, FX_INT32 hFrom)
+{
+ if (!m_pData) {
+ return;
+ }
+ if(hFrom < 0 || hFrom >= m_nHeight) {
+ JBIG2_memset(m_pData + hTo * m_nStride, 0, m_nStride);
+ } else {
+ JBIG2_memcpy(m_pData + hTo * m_nStride, m_pData + hFrom * m_nStride, m_nStride);
+ }
+}
+void CJBig2_Image::fill(FX_BOOL v)
+{
+ if (!m_pData) {
+ return;
+ }
+ JBIG2_memset(m_pData, v ? 0xff : 0, m_nStride * m_nHeight);
+}
+FX_BOOL CJBig2_Image::composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
+{
+ if (!m_pData) {
+ return FALSE;
+ }
+ return composeTo_opt2(pDst, x, y, op);
+}
+FX_BOOL CJBig2_Image::composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect)
+{
+ if (!m_pData) {
+ return FALSE;
+ }
+ if (NULL == pSrcRect || *pSrcRect == FX_RECT(0, 0, m_nWidth, m_nHeight)) {
+ return composeTo_opt2(pDst, x, y, op);
+ }
+ return composeTo_opt2(pDst, x, y, op, pSrcRect);
+}
+FX_BOOL CJBig2_Image::composeTo_unopt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
+{
+ FX_INT32 w, h, dx, dy;
+ FX_INT32 i, j;
+ w = m_nWidth;
+ h = m_nHeight;
+ dx = dy = 0;
+ if(x < 0) {
+ dx += -x;
+ w -= -x;
+ x = 0;
+ }
+ if(y < 0) {
+ dy += -y;
+ h -= -y;
+ y = 0;
+ }
+ if(x + w > pDst->m_nWidth) {
+ w = pDst->m_nWidth - x;
+ }
+ if(y + h > pDst->m_nHeight) {
+ h = pDst->m_nHeight - y;
+ }
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j,
+ (getPixel(i + dx, j + dy) | pDst->getPixel(x + i, y + j)) & 1);
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j,
+ (getPixel(i + dx, j + dy) & pDst->getPixel(x + i, y + j)) & 1);
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j,
+ (getPixel(i + dx, j + dy) ^ pDst->getPixel(x + i, y + j)) & 1);
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j,
+ (~(getPixel(i + dx, j + dy) ^ pDst->getPixel(x + i, y + j))) & 1);
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pDst->setPixel(x + i, y + j, getPixel(i + dx, j + dy));
+ }
+ }
+ break;
+ }
+ return TRUE;
+}
+
+FX_BOOL CJBig2_Image::composeTo_opt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
+{
+ FX_INT32 x0, x1, y0, y1, xx, yy;
+ FX_BYTE *pLineSrc, *pLineDst, *srcPtr, *destPtr;
+ FX_DWORD src0, src1, src, dest, s1, s2, m1, m2, m3;
+ FX_BOOL oneByte;
+ if (!m_pData) {
+ return FALSE;
+ }
+ if (y < 0) {
+ y0 = -y;
+ } else {
+ y0 = 0;
+ }
+ if (y + m_nHeight > pDst->m_nHeight) {
+ y1 = pDst->m_nHeight - y;
+ } else {
+ y1 = m_nHeight;
+ }
+ if (y0 >= y1) {
+ return FALSE;
+ }
+ if (x >= 0) {
+ x0 = x & ~7;
+ } else {
+ x0 = 0;
+ }
+ x1 = x + m_nWidth;
+ if (x1 > pDst->m_nWidth) {
+ x1 = pDst->m_nWidth;
+ }
+ if (x0 >= x1) {
+ return FALSE;
+ }
+ s1 = x & 7;
+ s2 = 8 - s1;
+ m1 = 0xff >> (x1 & 7);
+ m2 = 0xff << (((x1 & 7) == 0) ? 0 : 8 - (x1 & 7));
+ m3 = (0xff >> s1) & m2;
+ oneByte = x0 == ((x1 - 1) & ~7);
+ pLineDst = pDst->m_pData + y * pDst->m_nStride;
+ pLineSrc = m_pData + y0 * m_nStride;
+ if(oneByte) {
+ if(x >= 0) {
+ switch(op) {
+ case JBIG2_COMPOSE_OR: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest |= (*srcPtr >> s1) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest &= ((0xff00 | *srcPtr) >> s1) | m1;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest ^= (*srcPtr >> s1) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest ^= ((*srcPtr ^ 0xff) >> s1) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE: {
+ for (yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ dest = *destPtr;
+ dest = (dest & ~m3) | ((*srcPtr >> s1) & m3);
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ }
+ } else {
+ switch(op) {
+ case JBIG2_COMPOSE_OR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest |= *srcPtr & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest &= *srcPtr | m1;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest ^= *srcPtr & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest ^= (*srcPtr ^ 0xff) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ dest = *destPtr;
+ dest = (*srcPtr & m2) | (dest & m1);
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ }
+ }
+ } else {
+ if(x >= 0) {
+ switch(op) {
+ case JBIG2_COMPOSE_OR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest |= src1 >> s1;
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest |= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest |= src & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest &= (0xff00 | src1) >> s1;
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest &= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest &= src | m1;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest ^= src1 >> s1;
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest ^= (src1 ^ 0xff) >> s1;
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src ^ 0xff;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= (src ^ 0xff) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst + (x >> 3);
+ srcPtr = pLineSrc;
+ src1 = *srcPtr++;
+ dest = *destPtr;
+ dest = (dest & (0xff << s2)) | (src1 >> s1);
+ *destPtr++ = (FX_BYTE)dest;
+ xx = x0 + 8;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest = src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest = (src & m2) | (dest & m1);
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ }
+ } else {
+ switch(op) {
+ case JBIG2_COMPOSE_OR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest |= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest |= src & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest &= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest &= src | m1;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= src ^ 0xff;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest ^= (src ^ 0xff) & m2;
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE: {
+ for(yy = y0; yy < y1; ++yy) {
+ destPtr = pLineDst;
+ srcPtr = pLineSrc + (-x >> 3);
+ src1 = *srcPtr++;
+ xx = x0;
+ for (; xx < x1 - 8; xx += 8) {
+ dest = *destPtr;
+ src0 = src1;
+ src1 = *srcPtr++;
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest = src;
+ *destPtr++ = (FX_BYTE)dest;
+ }
+ dest = *destPtr;
+ src0 = src1;
+ if(srcPtr - pLineSrc < m_nStride) {
+ src1 = *srcPtr++;
+ } else {
+ src1 = 0;
+ }
+ src = (((src0 << 8) | src1) >> s1) & 0xff;
+ dest = (src & m2) | (dest & m1);
+ *destPtr = (FX_BYTE)dest;
+ pLineDst += pDst->m_nStride;
+ pLineSrc += m_nStride;
+ }
+ }
+ break;
+ }
+ }
+ }
+ return TRUE;
+}
+FX_BOOL CJBig2_Image::composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op)
+{
+ if (!m_pData) {
+ return FALSE;
+ }
+ return pSrc->composeTo(this, x, y, op);
+}
+FX_BOOL CJBig2_Image::composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op, const FX_RECT* pSrcRect)
+{
+ if (!m_pData) {
+ return FALSE;
+ }
+ return pSrc->composeTo(this, x, y, op, pSrcRect);
+}
+CJBig2_Image *CJBig2_Image::subImage_unopt(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h)
+{
+ CJBig2_Image *pImage;
+ FX_INT32 i, j;
+ JBIG2_ALLOC(pImage, CJBig2_Image(w, h));
+ for(j = 0; j < h; j++) {
+ for(i = 0; i < w; i++) {
+ pImage->setPixel(i, j, getPixel(x + i, y + j));
+ }
+ }
+ return pImage;
+}
+#define JBIG2_GETDWORD(buf) ((FX_DWORD)(((buf)[0] << 24) | ((buf)[1] << 16) | ((buf)[2] << 8) | (buf)[3]))
+CJBig2_Image *CJBig2_Image::subImage(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h)
+{
+ CJBig2_Image *pImage;
+ FX_INT32 m, n, j;
+ FX_BYTE *pLineSrc, *pLineDst;
+ FX_DWORD wTmp;
+ FX_BYTE *pSrc, *pSrcEnd, *pDst, *pDstEnd;
+ if (w == 0 || h == 0) {
+ return NULL;
+ }
+ JBIG2_ALLOC(pImage, CJBig2_Image(w, h));
+ if (!m_pData) {
+ pImage->fill(0);
+ return pImage;
+ }
+ if (!pImage->m_pData) {
+ return pImage;
+ }
+ pLineSrc = m_pData + m_nStride * y;
+ pLineDst = pImage->m_pData;
+ m = (x >> 5) << 2;
+ n = x & 31;
+ if(n == 0) {
+ for(j = 0; j < h; j++) {
+ pSrc = pLineSrc + m;
+ pSrcEnd = pLineSrc + m_nStride;
+ pDst = pLineDst;
+ pDstEnd = pLineDst + pImage->m_nStride;
+ for(; pDst < pDstEnd; pSrc += 4, pDst += 4) {
+ *((FX_DWORD *)pDst) = *((FX_DWORD *)pSrc);
+ }
+ pLineSrc += m_nStride;
+ pLineDst += pImage->m_nStride;
+ }
+ } else {
+ for(j = 0; j < h; j++) {
+ pSrc = pLineSrc + m;
+ pSrcEnd = pLineSrc + m_nStride;
+ pDst = pLineDst;
+ pDstEnd = pLineDst + pImage->m_nStride;
+ for(; pDst < pDstEnd; pSrc += 4, pDst += 4) {
+ if(pSrc + 4 < pSrcEnd) {
+ wTmp = (JBIG2_GETDWORD(pSrc) << n) | (JBIG2_GETDWORD(pSrc + 4) >> (32 - n));
+ } else {
+ wTmp = JBIG2_GETDWORD(pSrc) << n;
+ }
+ pDst[0] = (FX_BYTE)(wTmp >> 24);
+ pDst[1] = (FX_BYTE)(wTmp >> 16);
+ pDst[2] = (FX_BYTE)(wTmp >> 8);
+ pDst[3] = (FX_BYTE)wTmp;
+ }
+ pLineSrc += m_nStride;
+ pLineDst += pImage->m_nStride;
+ }
+ }
+ return pImage;
+}
+void CJBig2_Image::expand(FX_INT32 h, FX_BOOL v)
+{
+ if (!m_pData) {
+ return;
+ }
+ m_pData = (FX_BYTE*)m_pModule->JBig2_Realloc(m_pData, h * m_nStride);
+ if(h > m_nHeight) {
+ JBIG2_memset(m_pData + m_nHeight * m_nStride, v ? 0xff : 0, (h - m_nHeight)*m_nStride);
+ }
+ m_nHeight = h;
+}
+FX_BOOL CJBig2_Image::composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op)
+{
+ FX_INT32 xs0, ys0, xs1, ys1, xd0, yd0, xd1, yd1, xx, yy, w, h, middleDwords, lineLeft;
+ FX_DWORD s1, d1, d2, shift, shift1, shift2, tmp, tmp1, tmp2, maskL, maskR, maskM;
+ FX_BYTE *lineSrc, *lineDst, *sp, *dp;
+ if (!m_pData) {
+ return FALSE;
+ }
+ if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) {
+ return FALSE;
+ }
+ if(y < 0) {
+ ys0 = -y;
+ } else {
+ ys0 = 0;
+ }
+ if(y + m_nHeight > pDst->m_nHeight) {
+ ys1 = pDst->m_nHeight - y;
+ } else {
+ ys1 = m_nHeight;
+ }
+ if(x < 0) {
+ xs0 = -x;
+ } else {
+ xs0 = 0;
+ }
+ if(x + m_nWidth > pDst->m_nWidth) {
+ xs1 = pDst->m_nWidth - x;
+ } else {
+ xs1 = m_nWidth;
+ }
+ if((ys0 >= ys1) || (xs0 >= xs1)) {
+ return 0;
+ }
+ w = xs1 - xs0;
+ h = ys1 - ys0;
+ if(y < 0) {
+ yd0 = 0;
+ } else {
+ yd0 = y;
+ }
+ if(x < 0) {
+ xd0 = 0;
+ } else {
+ xd0 = x;
+ }
+ xd1 = xd0 + w;
+ yd1 = yd0 + h;
+ d1 = xd0 & 31;
+ d2 = xd1 & 31;
+ s1 = xs0 & 31;
+ maskL = 0xffffffff >> d1;
+ maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32);
+ maskM = maskL & maskR;
+ lineSrc = m_pData + ys0 * m_nStride + ((xs0 >> 5) << 2);
+ lineLeft = m_nStride - ((xs0 >> 5) << 2);
+ lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2);
+ if((xd0 & ~31) == ((xd1 - 1) & ~31)) {
+ if((xs0 & ~31) == ((xs1 - 1) & ~31)) {
+ if(s1 > d1) {
+ shift = s1 - d1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = JBIG2_GETDWORD(lineSrc) << shift;
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else {
+ shift = d1 - s1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = JBIG2_GETDWORD(lineSrc) >> shift;
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ } else {
+ shift1 = s1 - d1;
+ shift2 = 32 - shift1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = (JBIG2_GETDWORD(lineSrc) << shift1) | (JBIG2_GETDWORD(lineSrc + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ } else {
+ if(s1 > d1) {
+ shift1 = s1 - d1;
+ shift2 = 32 - shift1;
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (
+ ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else if(s1 == d1) {
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else {
+ shift1 = d1 - s1;
+ shift2 = 32 - shift1;
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp) >> shift1;
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift2) | ((JBIG2_GETDWORD(sp + 4)) >> shift1);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift2) | (
+ ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift1);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ }
+ return 1;
+}
+FX_BOOL CJBig2_Image::composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect)
+{
+ FX_INT32 xs0, ys0, xs1, ys1, xd0, yd0, xd1, yd1, xx, yy, w, h, middleDwords, lineLeft;
+ FX_DWORD s1, d1, d2, shift, shift1, shift2, tmp, tmp1, tmp2, maskL, maskR, maskM;
+ FX_BYTE *lineSrc, *lineDst, *sp, *dp;
+ FX_INT32 sw, sh;
+ if (!m_pData) {
+ return FALSE;
+ }
+ if (x < -1048576 || x > 1048576 || y < -1048576 || y > 1048576) {
+ return FALSE;
+ }
+ sw = pSrcRect->Width();
+ sh = pSrcRect->Height();
+ if(y < 0) {
+ ys0 = -y;
+ } else {
+ ys0 = 0;
+ }
+ if(y + sh > pDst->m_nHeight) {
+ ys1 = pDst->m_nHeight - y;
+ } else {
+ ys1 = sh;
+ }
+ if(x < 0) {
+ xs0 = -x;
+ } else {
+ xs0 = 0;
+ }
+ if(x + sw > pDst->m_nWidth) {
+ xs1 = pDst->m_nWidth - x;
+ } else {
+ xs1 = sw;
+ }
+ if((ys0 >= ys1) || (xs0 >= xs1)) {
+ return 0;
+ }
+ w = xs1 - xs0;
+ h = ys1 - ys0;
+ if(y < 0) {
+ yd0 = 0;
+ } else {
+ yd0 = y;
+ }
+ if(x < 0) {
+ xd0 = 0;
+ } else {
+ xd0 = x;
+ }
+ xd1 = xd0 + w;
+ yd1 = yd0 + h;
+ d1 = xd0 & 31;
+ d2 = xd1 & 31;
+ s1 = xs0 & 31;
+ maskL = 0xffffffff >> d1;
+ maskR = 0xffffffff << ((32 - (xd1 & 31)) % 32);
+ maskM = maskL & maskR;
+ lineSrc = m_pData + (pSrcRect->top + ys0) * m_nStride + (((xs0 + pSrcRect->left) >> 5) << 2);
+ lineLeft = m_nStride - ((xs0 >> 5) << 2);
+ lineDst = pDst->m_pData + yd0 * pDst->m_nStride + ((xd0 >> 5) << 2);
+ if((xd0 & ~31) == ((xd1 - 1) & ~31)) {
+ if((xs0 & ~31) == ((xs1 - 1) & ~31)) {
+ if(s1 > d1) {
+ shift = s1 - d1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = JBIG2_GETDWORD(lineSrc) << shift;
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else {
+ shift = d1 - s1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = JBIG2_GETDWORD(lineSrc) >> shift;
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ } else {
+ shift1 = s1 - d1;
+ shift2 = 32 - shift1;
+ for(yy = yd0; yy < yd1; yy++) {
+ tmp1 = (JBIG2_GETDWORD(lineSrc) << shift1) | (JBIG2_GETDWORD(lineSrc + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(lineDst);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 | tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskM) | ((tmp1 & tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskM) | ((tmp1 ^ tmp2) & maskM);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskM) | ((~(tmp1 ^ tmp2)) & maskM);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskM) | (tmp1 & maskM);
+ break;
+ }
+ lineDst[0] = (FX_BYTE)(tmp >> 24);
+ lineDst[1] = (FX_BYTE)(tmp >> 16);
+ lineDst[2] = (FX_BYTE)(tmp >> 8);
+ lineDst[3] = (FX_BYTE)tmp;
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ } else {
+ if(s1 > d1) {
+ shift1 = s1 - d1;
+ shift2 = 32 - shift1;
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (JBIG2_GETDWORD(sp + 4) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift1) | (
+ ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift2);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else if(s1 == d1) {
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ } else {
+ shift1 = d1 - s1;
+ shift2 = 32 - shift1;
+ middleDwords = (xd1 >> 5) - ((xd0 + 31) >> 5);
+ for(yy = yd0; yy < yd1; yy++) {
+ sp = lineSrc;
+ dp = lineDst;
+ if(d1 != 0) {
+ tmp1 = JBIG2_GETDWORD(sp) >> shift1;
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 | tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskL) | ((tmp1 & tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskL) | ((tmp1 ^ tmp2) & maskL);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskL) | ((~(tmp1 ^ tmp2)) & maskL);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskL) | (tmp1 & maskL);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ dp += 4;
+ }
+ for(xx = 0; xx < middleDwords; xx++) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift2) | ((JBIG2_GETDWORD(sp + 4)) >> shift1);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = tmp1 | tmp2;
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = tmp1 & tmp2;
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = tmp1 ^ tmp2;
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = ~(tmp1 ^ tmp2);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = tmp1;
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ sp += 4;
+ dp += 4;
+ }
+ if(d2 != 0) {
+ tmp1 = (JBIG2_GETDWORD(sp) << shift2) | (
+ ((sp + 4) < lineSrc + lineLeft ? JBIG2_GETDWORD(sp + 4) : 0) >> shift1);
+ tmp2 = JBIG2_GETDWORD(dp);
+ switch(op) {
+ case JBIG2_COMPOSE_OR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 | tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_AND:
+ tmp = (tmp2 & ~maskR) | ((tmp1 & tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XOR:
+ tmp = (tmp2 & ~maskR) | ((tmp1 ^ tmp2) & maskR);
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ tmp = (tmp2 & ~maskR) | ((~(tmp1 ^ tmp2)) & maskR);
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ tmp = (tmp2 & ~maskR) | (tmp1 & maskR);
+ break;
+ }
+ dp[0] = (FX_BYTE)(tmp >> 24);
+ dp[1] = (FX_BYTE)(tmp >> 16);
+ dp[2] = (FX_BYTE)(tmp >> 8);
+ dp[3] = (FX_BYTE)tmp;
+ }
+ lineSrc += m_nStride;
+ lineDst += pDst->m_nStride;
+ }
+ }
+ }
+ return 1;
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Image.h b/core/src/fxcodec/jbig2/JBig2_Image.h
index 3c69f647e3..5d06695c7c 100644
--- a/core/src/fxcodec/jbig2/JBig2_Image.h
+++ b/core/src/fxcodec/jbig2/JBig2_Image.h
@@ -1,68 +1,68 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_IMAGE_H_
-#define _JBIG2_IMAGE_H_
-#include "JBig2_Define.h"
-#include "JBig2_Module.h"
-typedef enum {
- JBIG2_COMPOSE_OR = 0,
- JBIG2_COMPOSE_AND = 1,
- JBIG2_COMPOSE_XOR = 2,
- JBIG2_COMPOSE_XNOR = 3,
- JBIG2_COMPOSE_REPLACE = 4
-} JBig2ComposeOp;
-struct FX_RECT;
-class CJBig2_Image : public CJBig2_Object
-{
-public:
-
- CJBig2_Image(FX_INT32 w, FX_INT32 h);
-
- CJBig2_Image(FX_INT32 w, FX_INT32 h, FX_INT32 stride, FX_BYTE*pBuf);
-
- CJBig2_Image(CJBig2_Image &im);
-
- ~CJBig2_Image();
-
- FX_BOOL getPixel(FX_INT32 x, FX_INT32 y);
-
- FX_INT32 setPixel(FX_INT32 x, FX_INT32 y, FX_BOOL v);
-
- void copyLine(FX_INT32 hTo, FX_INT32 hFrom);
-
- void fill(FX_BOOL v);
-
- FX_BOOL composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
- FX_BOOL composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect);
-
- FX_BOOL composeTo_unopt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
-
- FX_BOOL composeTo_opt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
-
- FX_BOOL composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
- FX_BOOL composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect);
-
- FX_BOOL composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op);
- FX_BOOL composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op, const FX_RECT* pSrcRect);
- CJBig2_Image *subImage_unopt(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h);
-
- CJBig2_Image *subImage(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h);
-
- void expand(FX_INT32 h, FX_BOOL v);
-public:
-
- FX_INT32 m_nWidth;
-
- FX_INT32 m_nHeight;
-
- FX_INT32 m_nStride;
-
- FX_BYTE *m_pData;
-
- FX_BOOL m_bNeedFree;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_IMAGE_H_
+#define _JBIG2_IMAGE_H_
+#include "JBig2_Define.h"
+#include "JBig2_Module.h"
+typedef enum {
+ JBIG2_COMPOSE_OR = 0,
+ JBIG2_COMPOSE_AND = 1,
+ JBIG2_COMPOSE_XOR = 2,
+ JBIG2_COMPOSE_XNOR = 3,
+ JBIG2_COMPOSE_REPLACE = 4
+} JBig2ComposeOp;
+struct FX_RECT;
+class CJBig2_Image : public CJBig2_Object
+{
+public:
+
+ CJBig2_Image(FX_INT32 w, FX_INT32 h);
+
+ CJBig2_Image(FX_INT32 w, FX_INT32 h, FX_INT32 stride, FX_BYTE*pBuf);
+
+ CJBig2_Image(CJBig2_Image &im);
+
+ ~CJBig2_Image();
+
+ FX_BOOL getPixel(FX_INT32 x, FX_INT32 y);
+
+ FX_INT32 setPixel(FX_INT32 x, FX_INT32 y, FX_BOOL v);
+
+ void copyLine(FX_INT32 hTo, FX_INT32 hFrom);
+
+ void fill(FX_BOOL v);
+
+ FX_BOOL composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
+ FX_BOOL composeTo(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect);
+
+ FX_BOOL composeTo_unopt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
+
+ FX_BOOL composeTo_opt(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
+
+ FX_BOOL composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op);
+ FX_BOOL composeTo_opt2(CJBig2_Image *pDst, FX_INT32 x, FX_INT32 y, JBig2ComposeOp op, const FX_RECT* pSrcRect);
+
+ FX_BOOL composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op);
+ FX_BOOL composeFrom(FX_INT32 x, FX_INT32 y, CJBig2_Image *pSrc, JBig2ComposeOp op, const FX_RECT* pSrcRect);
+ CJBig2_Image *subImage_unopt(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h);
+
+ CJBig2_Image *subImage(FX_INT32 x, FX_INT32 y, FX_INT32 w, FX_INT32 h);
+
+ void expand(FX_INT32 h, FX_BOOL v);
+public:
+
+ FX_INT32 m_nWidth;
+
+ FX_INT32 m_nHeight;
+
+ FX_INT32 m_nStride;
+
+ FX_BYTE *m_pData;
+
+ FX_BOOL m_bNeedFree;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_List.h b/core/src/fxcodec/jbig2/JBig2_List.h
index a737d9e112..9292724b1e 100644
--- a/core/src/fxcodec/jbig2/JBig2_List.h
+++ b/core/src/fxcodec/jbig2/JBig2_List.h
@@ -1,67 +1,67 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_LIST_H_
-#define _JBIG2_LIST_H_
-#include "JBig2_Define.h"
-#include "JBig2_Object.h"
-template <class TYPE>
-class CJBig2_List : public CJBig2_Object
-{
-public:
-
- CJBig2_List(FX_INT32 nSize = 8)
- {
- m_nSize = nSize;
- m_pArray = (TYPE**)m_pModule->JBig2_Malloc2(sizeof(TYPE*), nSize);
- m_nLength = 0;
- }
-
- ~CJBig2_List()
- {
- clear();
- m_pModule->JBig2_Free(m_pArray);
- }
-
- void clear()
- {
- FX_INT32 i;
- for(i = 0; i < m_nLength; i++) {
- delete m_pArray[i];
- }
- m_nLength = 0;
- }
-
- void addItem(TYPE *pItem)
- {
- if(m_nLength >= m_nSize) {
- m_nSize += 8;
- m_pArray = (TYPE**)m_pModule->JBig2_Realloc(m_pArray, sizeof(TYPE*)*m_nSize);
- }
- m_pArray[m_nLength++] = pItem;
- }
-
-
- FX_INT32 getLength()
- {
- return m_nLength;
- }
-
- TYPE *getAt(FX_INT32 nIndex)
- {
- return m_pArray[nIndex];
- }
-
- TYPE *getLast()
- {
- return m_pArray[m_nLength - 1];
- }
-private:
- FX_INT32 m_nSize;
- TYPE **m_pArray;
- FX_INT32 m_nLength;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_LIST_H_
+#define _JBIG2_LIST_H_
+#include "JBig2_Define.h"
+#include "JBig2_Object.h"
+template <class TYPE>
+class CJBig2_List : public CJBig2_Object
+{
+public:
+
+ CJBig2_List(FX_INT32 nSize = 8)
+ {
+ m_nSize = nSize;
+ m_pArray = (TYPE**)m_pModule->JBig2_Malloc2(sizeof(TYPE*), nSize);
+ m_nLength = 0;
+ }
+
+ ~CJBig2_List()
+ {
+ clear();
+ m_pModule->JBig2_Free(m_pArray);
+ }
+
+ void clear()
+ {
+ FX_INT32 i;
+ for(i = 0; i < m_nLength; i++) {
+ delete m_pArray[i];
+ }
+ m_nLength = 0;
+ }
+
+ void addItem(TYPE *pItem)
+ {
+ if(m_nLength >= m_nSize) {
+ m_nSize += 8;
+ m_pArray = (TYPE**)m_pModule->JBig2_Realloc(m_pArray, sizeof(TYPE*)*m_nSize);
+ }
+ m_pArray[m_nLength++] = pItem;
+ }
+
+
+ FX_INT32 getLength()
+ {
+ return m_nLength;
+ }
+
+ TYPE *getAt(FX_INT32 nIndex)
+ {
+ return m_pArray[nIndex];
+ }
+
+ TYPE *getLast()
+ {
+ return m_pArray[m_nLength - 1];
+ }
+private:
+ FX_INT32 m_nSize;
+ TYPE **m_pArray;
+ FX_INT32 m_nLength;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Module.h b/core/src/fxcodec/jbig2/JBig2_Module.h
index ccde5ade02..3a01eb094c 100644
--- a/core/src/fxcodec/jbig2/JBig2_Module.h
+++ b/core/src/fxcodec/jbig2/JBig2_Module.h
@@ -1,32 +1,32 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_MODULE_H_
-#define _JBIG2_MODULE_H_
-#include "JBig2_Define.h"
-class CJBig2_Module
-{
-public:
-
- virtual void *JBig2_Malloc(FX_DWORD dwSize) = 0;
-
- virtual void *JBig2_Malloc2(FX_DWORD num, FX_DWORD dwSize) = 0;
-
- virtual void *JBig2_Malloc3(FX_DWORD num, FX_DWORD dwSize, FX_DWORD dwSize2) = 0;
-
- virtual void *JBig2_Realloc(FX_LPVOID pMem, FX_DWORD dwSize) = 0;
-
- virtual void JBig2_Free(FX_LPVOID pMem) = 0;
-
- virtual void JBig2_Assert(FX_INT32 nExpression) {};
-
- virtual void JBig2_Error(FX_LPCSTR format, ...) {};
-
- virtual void JBig2_Warn(FX_LPCSTR format, ...) {};
-
- virtual void JBig2_Log(FX_LPCSTR format, ...) {};
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_MODULE_H_
+#define _JBIG2_MODULE_H_
+#include "JBig2_Define.h"
+class CJBig2_Module
+{
+public:
+
+ virtual void *JBig2_Malloc(FX_DWORD dwSize) = 0;
+
+ virtual void *JBig2_Malloc2(FX_DWORD num, FX_DWORD dwSize) = 0;
+
+ virtual void *JBig2_Malloc3(FX_DWORD num, FX_DWORD dwSize, FX_DWORD dwSize2) = 0;
+
+ virtual void *JBig2_Realloc(FX_LPVOID pMem, FX_DWORD dwSize) = 0;
+
+ virtual void JBig2_Free(FX_LPVOID pMem) = 0;
+
+ virtual void JBig2_Assert(FX_INT32 nExpression) {};
+
+ virtual void JBig2_Error(FX_LPCSTR format, ...) {};
+
+ virtual void JBig2_Warn(FX_LPCSTR format, ...) {};
+
+ virtual void JBig2_Log(FX_LPCSTR format, ...) {};
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Object.cpp b/core/src/fxcodec/jbig2/JBig2_Object.cpp
index 3a94f0e65a..2bb267b085 100644
--- a/core/src/fxcodec/jbig2/JBig2_Object.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_Object.cpp
@@ -1,72 +1,72 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_Object.h"
-#include "JBig2_Module.h"
-void *CJBig2_Object::operator new(size_t size, CJBig2_Module *pModule, FX_LPCSTR filename, int line)
-{
- CJBig2_Object *p;
- p = (CJBig2_Object *)pModule->JBig2_Malloc((FX_DWORD)size);
- p->m_pModule = pModule;
- return p;
-}
-void CJBig2_Object::operator delete(void *p, CJBig2_Module *pModule, FX_LPCSTR filename, int line)
-{
- pModule->JBig2_Free(p);
-}
-void *CJBig2_Object::operator new(size_t size, CJBig2_Module *pModule)
-{
- CJBig2_Object *p;
- p = (CJBig2_Object *)pModule->JBig2_Malloc((FX_DWORD)size);
- p->m_pModule = pModule;
- return p;
-}
-void CJBig2_Object::operator delete(void *p)
-{
- ((CJBig2_Object *)p)->m_pModule->JBig2_Free(p);
-}
-void CJBig2_Object::operator delete(void *p, CJBig2_Module *pModule)
-{
- pModule->JBig2_Free(p);
-}
-void *CJBig2_Object::operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size,
- FX_LPCSTR filename, int line)
-{
- void *p;
- FX_BYTE *pCur, *pEnd;
- p = (FX_BYTE *)pModule->JBig2_Malloc((FX_DWORD)size);
- pCur = (FX_BYTE *)p;
- pEnd = pCur + size;
- for(; pCur < pEnd; pCur += unit_size) {
- ((CJBig2_Object *)pCur)->m_pModule = pModule;
- }
- return p;
-}
-void CJBig2_Object::operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size,
- FX_LPCSTR filename, int line)
-{
- pModule->JBig2_Free(p);
-}
-void *CJBig2_Object::operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size)
-{
- void *p;
- FX_BYTE *pCur, *pEnd;
- p = (FX_BYTE *)pModule->JBig2_Malloc((FX_DWORD)size);
- pCur = (FX_BYTE *)p;
- pEnd = pCur + size;
- for(; pCur < pEnd; pCur += unit_size) {
- ((CJBig2_Object *)pCur)->m_pModule = pModule;
- }
- return p;
-}
-void CJBig2_Object::operator delete[](void* p)
-{
- ((CJBig2_Object *)p)->m_pModule->JBig2_Free(p);
-}
-void CJBig2_Object::operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size)
-{
- pModule->JBig2_Free(p);
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_Object.h"
+#include "JBig2_Module.h"
+void *CJBig2_Object::operator new(size_t size, CJBig2_Module *pModule, FX_LPCSTR filename, int line)
+{
+ CJBig2_Object *p;
+ p = (CJBig2_Object *)pModule->JBig2_Malloc((FX_DWORD)size);
+ p->m_pModule = pModule;
+ return p;
+}
+void CJBig2_Object::operator delete(void *p, CJBig2_Module *pModule, FX_LPCSTR filename, int line)
+{
+ pModule->JBig2_Free(p);
+}
+void *CJBig2_Object::operator new(size_t size, CJBig2_Module *pModule)
+{
+ CJBig2_Object *p;
+ p = (CJBig2_Object *)pModule->JBig2_Malloc((FX_DWORD)size);
+ p->m_pModule = pModule;
+ return p;
+}
+void CJBig2_Object::operator delete(void *p)
+{
+ ((CJBig2_Object *)p)->m_pModule->JBig2_Free(p);
+}
+void CJBig2_Object::operator delete(void *p, CJBig2_Module *pModule)
+{
+ pModule->JBig2_Free(p);
+}
+void *CJBig2_Object::operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size,
+ FX_LPCSTR filename, int line)
+{
+ void *p;
+ FX_BYTE *pCur, *pEnd;
+ p = (FX_BYTE *)pModule->JBig2_Malloc((FX_DWORD)size);
+ pCur = (FX_BYTE *)p;
+ pEnd = pCur + size;
+ for(; pCur < pEnd; pCur += unit_size) {
+ ((CJBig2_Object *)pCur)->m_pModule = pModule;
+ }
+ return p;
+}
+void CJBig2_Object::operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size,
+ FX_LPCSTR filename, int line)
+{
+ pModule->JBig2_Free(p);
+}
+void *CJBig2_Object::operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size)
+{
+ void *p;
+ FX_BYTE *pCur, *pEnd;
+ p = (FX_BYTE *)pModule->JBig2_Malloc((FX_DWORD)size);
+ pCur = (FX_BYTE *)p;
+ pEnd = pCur + size;
+ for(; pCur < pEnd; pCur += unit_size) {
+ ((CJBig2_Object *)pCur)->m_pModule = pModule;
+ }
+ return p;
+}
+void CJBig2_Object::operator delete[](void* p)
+{
+ ((CJBig2_Object *)p)->m_pModule->JBig2_Free(p);
+}
+void CJBig2_Object::operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size)
+{
+ pModule->JBig2_Free(p);
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Object.h b/core/src/fxcodec/jbig2/JBig2_Object.h
index 7fef1a21a9..23eecbf171 100644
--- a/core/src/fxcodec/jbig2/JBig2_Object.h
+++ b/core/src/fxcodec/jbig2/JBig2_Object.h
@@ -1,43 +1,43 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_OBJECT_H_
-#define _JBIG2_OBJECT_H_
-#include "JBig2_Define.h"
-class CJBig2_Module;
-#define _JBIG2_NO_EXPECTION_
-class CJBig2_Object
-{
-public:
-
- void *operator new(size_t size, CJBig2_Module *pModule, FX_LPCSTR filename, int line);
-
- void operator delete(void *p, CJBig2_Module *pModule, FX_LPCSTR filename, int line);
-
- void *operator new(size_t size, CJBig2_Module *pModule);
-
- void operator delete(void *p);
-
- void operator delete(void *p, CJBig2_Module *pModule);
-
- void *operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size,
- FX_LPCSTR filename, int line);
-
- void operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size,
- FX_LPCSTR filename, int line);
-
- void *operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size);
-
- void operator delete[](void* p);
-
- void operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size);
-public:
-
- CJBig2_Module *m_pModule;
-};
-#define JBIG2_NEW new(m_pModule)
-#define JBIG2_ALLOC(p, a) p = JBIG2_NEW a; p->m_pModule = m_pModule;
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_OBJECT_H_
+#define _JBIG2_OBJECT_H_
+#include "JBig2_Define.h"
+class CJBig2_Module;
+#define _JBIG2_NO_EXPECTION_
+class CJBig2_Object
+{
+public:
+
+ void *operator new(size_t size, CJBig2_Module *pModule, FX_LPCSTR filename, int line);
+
+ void operator delete(void *p, CJBig2_Module *pModule, FX_LPCSTR filename, int line);
+
+ void *operator new(size_t size, CJBig2_Module *pModule);
+
+ void operator delete(void *p);
+
+ void operator delete(void *p, CJBig2_Module *pModule);
+
+ void *operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size,
+ FX_LPCSTR filename, int line);
+
+ void operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size,
+ FX_LPCSTR filename, int line);
+
+ void *operator new[](size_t size, CJBig2_Module *pModule, size_t unit_size);
+
+ void operator delete[](void* p);
+
+ void operator delete[](void *p, CJBig2_Module *pModule, size_t unit_size);
+public:
+
+ CJBig2_Module *m_pModule;
+};
+#define JBIG2_NEW new(m_pModule)
+#define JBIG2_ALLOC(p, a) p = JBIG2_NEW a; p->m_pModule = m_pModule;
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Page.h b/core/src/fxcodec/jbig2/JBig2_Page.h
index 490705448c..d7399bd452 100644
--- a/core/src/fxcodec/jbig2/JBig2_Page.h
+++ b/core/src/fxcodec/jbig2/JBig2_Page.h
@@ -1,19 +1,19 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_PAGE_H_
-#define _JBIG2_PAGE_H_
-#include "JBig2_Image.h"
-struct JBig2PageInfo : public CJBig2_Object {
- FX_DWORD m_dwWidth,
- m_dwHeight;
- FX_DWORD m_dwResolutionX,
- m_dwResolutionY;
- FX_BYTE m_cFlags;
- FX_BOOL m_bIsStriped;
- FX_WORD m_wMaxStripeSize;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_PAGE_H_
+#define _JBIG2_PAGE_H_
+#include "JBig2_Image.h"
+struct JBig2PageInfo : public CJBig2_Object {
+ FX_DWORD m_dwWidth,
+ m_dwHeight;
+ FX_DWORD m_dwResolutionX,
+ m_dwResolutionY;
+ FX_BYTE m_cFlags;
+ FX_BOOL m_bIsStriped;
+ FX_WORD m_wMaxStripeSize;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp b/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp
index 58c52a5848..0c6e5be8ce 100644
--- a/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_PatternDict.cpp
@@ -1,24 +1,24 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_PatternDict.h"
-CJBig2_PatternDict::CJBig2_PatternDict()
-{
- NUMPATS = 0;
- HDPATS = NULL;
-}
-
-CJBig2_PatternDict::~CJBig2_PatternDict()
-{
- if(HDPATS) {
- for(FX_DWORD i = 0; i < NUMPATS; i++) {
- if(HDPATS[i]) {
- delete HDPATS[i];
- }
- }
- m_pModule->JBig2_Free(HDPATS);
- }
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_PatternDict.h"
+CJBig2_PatternDict::CJBig2_PatternDict()
+{
+ NUMPATS = 0;
+ HDPATS = NULL;
+}
+
+CJBig2_PatternDict::~CJBig2_PatternDict()
+{
+ if(HDPATS) {
+ for(FX_DWORD i = 0; i < NUMPATS; i++) {
+ if(HDPATS[i]) {
+ delete HDPATS[i];
+ }
+ }
+ m_pModule->JBig2_Free(HDPATS);
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_PatternDict.h b/core/src/fxcodec/jbig2/JBig2_PatternDict.h
index c8686507d9..b75cbde999 100644
--- a/core/src/fxcodec/jbig2/JBig2_PatternDict.h
+++ b/core/src/fxcodec/jbig2/JBig2_PatternDict.h
@@ -1,22 +1,22 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_PATTERN_DICT_H_
-#define _JBIG2_PATTERN_DICT_H_
-#include "JBig2_Define.h"
-#include "JBig2_Image.h"
-class CJBig2_PatternDict : public CJBig2_Object
-{
-public:
-
- CJBig2_PatternDict();
-
- ~CJBig2_PatternDict();
-public:
- FX_DWORD NUMPATS;
- CJBig2_Image **HDPATS;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_PATTERN_DICT_H_
+#define _JBIG2_PATTERN_DICT_H_
+#include "JBig2_Define.h"
+#include "JBig2_Image.h"
+class CJBig2_PatternDict : public CJBig2_Object
+{
+public:
+
+ CJBig2_PatternDict();
+
+ ~CJBig2_PatternDict();
+public:
+ FX_DWORD NUMPATS;
+ CJBig2_Image **HDPATS;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_Segment.cpp b/core/src/fxcodec/jbig2/JBig2_Segment.cpp
index 788207dc6e..b7eed35987 100644
--- a/core/src/fxcodec/jbig2/JBig2_Segment.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_Segment.cpp
@@ -1,53 +1,53 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_Segment.h"
-CJBig2_Segment::CJBig2_Segment()
-{
- init();
-}
-CJBig2_Segment::~CJBig2_Segment()
-{
- clean();
-}
-void CJBig2_Segment::init()
-{
- m_dwNumber = 0;
- m_cFlags.c = 0;
- m_nReferred_to_segment_count = 0;
- m_pReferred_to_segment_numbers = NULL;
- m_dwPage_association = 0;
- m_dwData_length = 0;
- m_dwHeader_Length = 0;
- m_pData = NULL;
- m_State = JBIG2_SEGMENT_HEADER_UNPARSED;
- m_nResultType = JBIG2_VOID_POINTER;
- m_Result.vd = NULL;
-}
-void CJBig2_Segment::clean()
-{
- if(m_pReferred_to_segment_numbers) {
- m_pModule->JBig2_Free(m_pReferred_to_segment_numbers);
- }
- if(m_Result.vd) {
- switch(m_nResultType) {
- case JBIG2_IMAGE_POINTER:
- delete m_Result.im;
- break;
- case JBIG2_SYMBOL_DICT_POINTER:
- delete m_Result.sd;
- break;
- case JBIG2_PATTERN_DICT_POINTER:
- delete m_Result.pd;
- break;
- case JBIG2_HUFFMAN_TABLE_POINTER:
- delete m_Result.ht;
- break;
- default:
- m_pModule->JBig2_Free(m_Result.vd);
- }
- }
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_Segment.h"
+CJBig2_Segment::CJBig2_Segment()
+{
+ init();
+}
+CJBig2_Segment::~CJBig2_Segment()
+{
+ clean();
+}
+void CJBig2_Segment::init()
+{
+ m_dwNumber = 0;
+ m_cFlags.c = 0;
+ m_nReferred_to_segment_count = 0;
+ m_pReferred_to_segment_numbers = NULL;
+ m_dwPage_association = 0;
+ m_dwData_length = 0;
+ m_dwHeader_Length = 0;
+ m_pData = NULL;
+ m_State = JBIG2_SEGMENT_HEADER_UNPARSED;
+ m_nResultType = JBIG2_VOID_POINTER;
+ m_Result.vd = NULL;
+}
+void CJBig2_Segment::clean()
+{
+ if(m_pReferred_to_segment_numbers) {
+ m_pModule->JBig2_Free(m_pReferred_to_segment_numbers);
+ }
+ if(m_Result.vd) {
+ switch(m_nResultType) {
+ case JBIG2_IMAGE_POINTER:
+ delete m_Result.im;
+ break;
+ case JBIG2_SYMBOL_DICT_POINTER:
+ delete m_Result.sd;
+ break;
+ case JBIG2_PATTERN_DICT_POINTER:
+ delete m_Result.pd;
+ break;
+ case JBIG2_HUFFMAN_TABLE_POINTER:
+ delete m_Result.ht;
+ break;
+ default:
+ m_pModule->JBig2_Free(m_Result.vd);
+ }
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_Segment.h b/core/src/fxcodec/jbig2/JBig2_Segment.h
index 48c31b17bb..33ee721a1b 100644
--- a/core/src/fxcodec/jbig2/JBig2_Segment.h
+++ b/core/src/fxcodec/jbig2/JBig2_Segment.h
@@ -1,68 +1,68 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_SEGMENT_H_
-#define _JBIG2_SEGMENT_H_
-#include "JBig2_Define.h"
-#include "JBig2_SymbolDict.h"
-#include "JBig2_PatternDict.h"
-#include "JBig2_Module.h"
-#include "JBig2_HuffmanTable.h"
-#define JBIG2_GET_INT32(buf) (((buf)[0]<<24) | ((buf)[1]<<16) | ((buf)[2]<<8) | (buf)[3])
-#define JBIG2_GET_INT16(buf) (((buf)[0]<<8) | (buf)[1])
-typedef enum {
- JBIG2_SEGMENT_HEADER_UNPARSED,
- JBIG2_SEGMENT_DATA_UNPARSED,
- JBIG2_SEGMENT_PARSE_COMPLETE,
- JBIG2_SEGMENT_PAUSED,
- JBIG2_SEGMENT_ERROR
-} JBig2_SegmentState;
-typedef enum {
- JBIG2_VOID_POINTER = 0,
- JBIG2_IMAGE_POINTER,
- JBIG2_SYMBOL_DICT_POINTER,
- JBIG2_PATTERN_DICT_POINTER,
- JBIG2_HUFFMAN_TABLE_POINTER
-} JBig2_ResultType;
-class CJBig2_Segment : public CJBig2_Object
-{
-public:
-
- CJBig2_Segment();
-
- ~CJBig2_Segment();
-
- void init();
-
- void clean();
-public:
- FX_DWORD m_dwNumber;
- union {
- struct {
- FX_BYTE type : 6;
- FX_BYTE page_association_size : 1;
- FX_BYTE deferred_non_retain : 1;
- } s;
- FX_BYTE c;
- } m_cFlags;
- FX_INT32 m_nReferred_to_segment_count;
- FX_DWORD * m_pReferred_to_segment_numbers;
- FX_DWORD m_dwPage_association;
- FX_DWORD m_dwData_length;
-
- FX_DWORD m_dwHeader_Length;
- FX_BYTE *m_pData;
- JBig2_SegmentState m_State;
- JBig2_ResultType m_nResultType;
- union {
- CJBig2_SymbolDict *sd;
- CJBig2_PatternDict *pd;
- CJBig2_Image *im;
- CJBig2_HuffmanTable *ht;
- FX_LPVOID vd;
- } m_Result;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_SEGMENT_H_
+#define _JBIG2_SEGMENT_H_
+#include "JBig2_Define.h"
+#include "JBig2_SymbolDict.h"
+#include "JBig2_PatternDict.h"
+#include "JBig2_Module.h"
+#include "JBig2_HuffmanTable.h"
+#define JBIG2_GET_INT32(buf) (((buf)[0]<<24) | ((buf)[1]<<16) | ((buf)[2]<<8) | (buf)[3])
+#define JBIG2_GET_INT16(buf) (((buf)[0]<<8) | (buf)[1])
+typedef enum {
+ JBIG2_SEGMENT_HEADER_UNPARSED,
+ JBIG2_SEGMENT_DATA_UNPARSED,
+ JBIG2_SEGMENT_PARSE_COMPLETE,
+ JBIG2_SEGMENT_PAUSED,
+ JBIG2_SEGMENT_ERROR
+} JBig2_SegmentState;
+typedef enum {
+ JBIG2_VOID_POINTER = 0,
+ JBIG2_IMAGE_POINTER,
+ JBIG2_SYMBOL_DICT_POINTER,
+ JBIG2_PATTERN_DICT_POINTER,
+ JBIG2_HUFFMAN_TABLE_POINTER
+} JBig2_ResultType;
+class CJBig2_Segment : public CJBig2_Object
+{
+public:
+
+ CJBig2_Segment();
+
+ ~CJBig2_Segment();
+
+ void init();
+
+ void clean();
+public:
+ FX_DWORD m_dwNumber;
+ union {
+ struct {
+ FX_BYTE type : 6;
+ FX_BYTE page_association_size : 1;
+ FX_BYTE deferred_non_retain : 1;
+ } s;
+ FX_BYTE c;
+ } m_cFlags;
+ FX_INT32 m_nReferred_to_segment_count;
+ FX_DWORD * m_pReferred_to_segment_numbers;
+ FX_DWORD m_dwPage_association;
+ FX_DWORD m_dwData_length;
+
+ FX_DWORD m_dwHeader_Length;
+ FX_BYTE *m_pData;
+ JBig2_SegmentState m_State;
+ JBig2_ResultType m_nResultType;
+ union {
+ CJBig2_SymbolDict *sd;
+ CJBig2_PatternDict *pd;
+ CJBig2_Image *im;
+ CJBig2_HuffmanTable *ht;
+ FX_LPVOID vd;
+ } m_Result;
+};
+#endif
diff --git a/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp b/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp
index 8516c045c6..67e54c0cea 100644
--- a/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp
+++ b/core/src/fxcodec/jbig2/JBig2_SymbolDict.cpp
@@ -1,34 +1,34 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "JBig2_SymbolDict.h"
-CJBig2_SymbolDict::CJBig2_SymbolDict()
-{
- SDNUMEXSYMS = 0;
- SDEXSYMS = NULL;
- m_bContextRetained = FALSE;
- m_gbContext = m_grContext = NULL;
-}
-
-CJBig2_SymbolDict::~CJBig2_SymbolDict()
-{
- if(SDEXSYMS) {
- for(FX_DWORD i = 0; i < SDNUMEXSYMS; i++) {
- if(SDEXSYMS[i]) {
- delete SDEXSYMS[i];
- }
- }
- m_pModule->JBig2_Free(SDEXSYMS);
- }
- if(m_bContextRetained) {
- if(m_gbContext) {
- m_pModule->JBig2_Free(m_gbContext);
- }
- if(m_grContext) {
- m_pModule->JBig2_Free(m_grContext);
- }
- }
-}
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "JBig2_SymbolDict.h"
+CJBig2_SymbolDict::CJBig2_SymbolDict()
+{
+ SDNUMEXSYMS = 0;
+ SDEXSYMS = NULL;
+ m_bContextRetained = FALSE;
+ m_gbContext = m_grContext = NULL;
+}
+
+CJBig2_SymbolDict::~CJBig2_SymbolDict()
+{
+ if(SDEXSYMS) {
+ for(FX_DWORD i = 0; i < SDNUMEXSYMS; i++) {
+ if(SDEXSYMS[i]) {
+ delete SDEXSYMS[i];
+ }
+ }
+ m_pModule->JBig2_Free(SDEXSYMS);
+ }
+ if(m_bContextRetained) {
+ if(m_gbContext) {
+ m_pModule->JBig2_Free(m_gbContext);
+ }
+ if(m_grContext) {
+ m_pModule->JBig2_Free(m_grContext);
+ }
+ }
+}
diff --git a/core/src/fxcodec/jbig2/JBig2_SymbolDict.h b/core/src/fxcodec/jbig2/JBig2_SymbolDict.h
index cb97a167d1..cfe75db4c9 100644
--- a/core/src/fxcodec/jbig2/JBig2_SymbolDict.h
+++ b/core/src/fxcodec/jbig2/JBig2_SymbolDict.h
@@ -1,26 +1,26 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _JBIG2_SYMBOL_DICT_H_
-#define _JBIG2_SYMBOL_DICT_H_
-#include "JBig2_Define.h"
-#include "JBig2_ArithDecoder.h"
-#include "JBig2_Image.h"
-class CJBig2_SymbolDict : public CJBig2_Object
-{
-public:
-
- CJBig2_SymbolDict();
-
- ~CJBig2_SymbolDict();
-public:
- FX_DWORD SDNUMEXSYMS;
- CJBig2_Image **SDEXSYMS;
- FX_BOOL m_bContextRetained;
- JBig2ArithCtx *m_gbContext,
- *m_grContext;
-};
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _JBIG2_SYMBOL_DICT_H_
+#define _JBIG2_SYMBOL_DICT_H_
+#include "JBig2_Define.h"
+#include "JBig2_ArithDecoder.h"
+#include "JBig2_Image.h"
+class CJBig2_SymbolDict : public CJBig2_Object
+{
+public:
+
+ CJBig2_SymbolDict();
+
+ ~CJBig2_SymbolDict();
+public:
+ FX_DWORD SDNUMEXSYMS;
+ CJBig2_Image **SDEXSYMS;
+ FX_BOOL m_bContextRetained;
+ JBig2ArithCtx *m_gbContext,
+ *m_grContext;
+};
+#endif
diff --git a/core/src/fxcodec/lcms2/include/fx_lcms2.h b/core/src/fxcodec/lcms2/include/fx_lcms2.h
index 53038322d3..e6413188d8 100644
--- a/core/src/fxcodec/lcms2/include/fx_lcms2.h
+++ b/core/src/fxcodec/lcms2/include/fx_lcms2.h
@@ -1,10 +1,10 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _FX_LCMS2_H_
-#define _FX_LCMS2_H_
-#include "../lcms2-2.6/include/lcms2.h"
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _FX_LCMS2_H_
+#define _FX_LCMS2_H_
+#include "../lcms2-2.6/include/lcms2.h"
+#endif
diff --git a/core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h b/core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h
index 1103b7f698..4ab5775718 100644
--- a/core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h
+++ b/core/src/fxcodec/lcms2/include/fx_lcms2_plugin.h
@@ -1,10 +1,10 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef _FX_LCMS2_H_
-#define _FX_LCMS2_H_
-#include "../lcms2-2.6/include/lcms2_plugin.h"
-#endif
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#ifndef _FX_LCMS2_H_
+#define _FX_LCMS2_H_
+#include "../lcms2-2.6/include/lcms2_plugin.h"
+#endif
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c
index 745238c6e2..87c6320cff 100644
--- a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmserr.c
@@ -260,50 +260,50 @@ cmsBool _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugi
}
// Generic allocate
-void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size)
-{
- return FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
-}
-
-// Generic allocate & zero
-void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size)
-{
- void* p = FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
- if (p) FXSYS_memset32(p, 0, size);
- return p;
-}
-
-// Generic calloc
-void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
-{
- cmsUInt32Number total = num * size;
- if (total == 0 || total / size != num || total >= 512 * 1024 * 1024)
- return NULL;
-
- return _cmsMallocZero(ContextID, num * size);
-}
-
-// Generic reallocate
-void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
-{
- return FXMEM_DefaultRealloc(Ptr, size, FXMEM_NONLEAVE);
-}
-
-// Generic free memory
-void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr)
-{
- if (Ptr != NULL) FXMEM_DefaultFree(Ptr, 0);
-}
-
-// Generic block duplication
-void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size)
-{
- void* p = FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
- FXSYS_memmove32(p, Org, size);
- return p;
-}
-
-_cmsMemPluginChunkType _cmsMemPluginChunk = {_cmsMalloc, _cmsMallocZero, _cmsFree,
+void* CMSEXPORT _cmsMalloc(cmsContext ContextID, cmsUInt32Number size)
+{
+ return FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
+}
+
+// Generic allocate & zero
+void* CMSEXPORT _cmsMallocZero(cmsContext ContextID, cmsUInt32Number size)
+{
+ void* p = FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
+ if (p) FXSYS_memset32(p, 0, size);
+ return p;
+}
+
+// Generic calloc
+void* CMSEXPORT _cmsCalloc(cmsContext ContextID, cmsUInt32Number num, cmsUInt32Number size)
+{
+ cmsUInt32Number total = num * size;
+ if (total == 0 || total / size != num || total >= 512 * 1024 * 1024)
+ return NULL;
+
+ return _cmsMallocZero(ContextID, num * size);
+}
+
+// Generic reallocate
+void* CMSEXPORT _cmsRealloc(cmsContext ContextID, void* Ptr, cmsUInt32Number size)
+{
+ return FXMEM_DefaultRealloc(Ptr, size, FXMEM_NONLEAVE);
+}
+
+// Generic free memory
+void CMSEXPORT _cmsFree(cmsContext ContextID, void* Ptr)
+{
+ if (Ptr != NULL) FXMEM_DefaultFree(Ptr, 0);
+}
+
+// Generic block duplication
+void* CMSEXPORT _cmsDupMem(cmsContext ContextID, const void* Org, cmsUInt32Number size)
+{
+ void* p = FXMEM_DefaultAlloc(size, FXMEM_NONLEAVE);
+ FXSYS_memmove32(p, Org, size);
+ return p;
+}
+
+_cmsMemPluginChunkType _cmsMemPluginChunk = {_cmsMalloc, _cmsMallocZero, _cmsFree,
_cmsRealloc, _cmsCalloc, _cmsDupMem
};
diff --git a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c
index 09427650c9..1103363a78 100644
--- a/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c
+++ b/core/src/fxcodec/lcms2/lcms2-2.6/src/cmsgmt.c
@@ -1,590 +1,590 @@
-//---------------------------------------------------------------------------------
-//
-// Little Color Management System
-// Copyright (c) 1998-2012 Marti Maria Saguer
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the "Software"),
-// to deal in the Software without restriction, including without limitation
-// the rights to use, copy, modify, merge, publish, distribute, sublicense,
-// and/or sell copies of the Software, and to permit persons to whom the Software
-// is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
-// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-//---------------------------------------------------------------------------------
-//
-
-#include "lcms2_internal.h"
-
-
-// Auxiliar: append a Lab identity after the given sequence of profiles
-// and return the transform. Lab profile is closed, rest of profiles are kept open.
-cmsHTRANSFORM _cmsChain2Lab(cmsContext ContextID,
- cmsUInt32Number nProfiles,
- cmsUInt32Number InputFormat,
- cmsUInt32Number OutputFormat,
- const cmsUInt32Number Intents[],
- const cmsHPROFILE hProfiles[],
- const cmsBool BPC[],
- const cmsFloat64Number AdaptationStates[],
- cmsUInt32Number dwFlags)
-{
- cmsHTRANSFORM xform;
- cmsHPROFILE hLab;
- cmsHPROFILE ProfileList[256];
- cmsBool BPCList[256];
- cmsFloat64Number AdaptationList[256];
- cmsUInt32Number IntentList[256];
- cmsUInt32Number i;
-
- // This is a rather big number and there is no need of dynamic memory
- // since we are adding a profile, 254 + 1 = 255 and this is the limit
- if (nProfiles > 254) return NULL;
-
- // The output space
- hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
- if (hLab == NULL) return NULL;
-
- // Create a copy of parameters
- for (i=0; i < nProfiles; i++) {
-
- ProfileList[i] = hProfiles[i];
- BPCList[i] = BPC[i];
- AdaptationList[i] = AdaptationStates[i];
- IntentList[i] = Intents[i];
- }
-
- // Place Lab identity at chain's end.
- ProfileList[nProfiles] = hLab;
- BPCList[nProfiles] = 0;
- AdaptationList[nProfiles] = 1.0;
- IntentList[nProfiles] = INTENT_RELATIVE_COLORIMETRIC;
-
- // Create the transform
- xform = cmsCreateExtendedTransform(ContextID, nProfiles + 1, ProfileList,
- BPCList,
- IntentList,
- AdaptationList,
- NULL, 0,
- InputFormat,
- OutputFormat,
- dwFlags);
-
- cmsCloseProfile(hLab);
-
- return xform;
-}
-
-
-// Compute K -> L* relationship. Flags may include black point compensation. In this case,
-// the relationship is assumed from the profile with BPC to a black point zero.
-static
-cmsToneCurve* ComputeKToLstar(cmsContext ContextID,
- cmsUInt32Number nPoints,
- cmsUInt32Number nProfiles,
- const cmsUInt32Number Intents[],
- const cmsHPROFILE hProfiles[],
- const cmsBool BPC[],
- const cmsFloat64Number AdaptationStates[],
- cmsUInt32Number dwFlags)
-{
- cmsToneCurve* out = NULL;
- cmsUInt32Number i;
- cmsHTRANSFORM xform;
- cmsCIELab Lab;
- cmsFloat32Number cmyk[4];
- cmsFloat32Number* SampledPoints;
-
- xform = _cmsChain2Lab(ContextID, nProfiles, TYPE_CMYK_FLT, TYPE_Lab_DBL, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
- if (xform == NULL) return NULL;
-
- SampledPoints = (cmsFloat32Number*) _cmsCalloc(ContextID, nPoints, sizeof(cmsFloat32Number));
- if (SampledPoints == NULL) goto Error;
-
- for (i=0; i < nPoints; i++) {
-
- cmyk[0] = 0;
- cmyk[1] = 0;
- cmyk[2] = 0;
- cmyk[3] = (cmsFloat32Number) ((i * 100.0) / (nPoints-1));
-
- cmsDoTransform(xform, cmyk, &Lab, 1);
- SampledPoints[i]= (cmsFloat32Number) (1.0 - Lab.L / 100.0); // Negate K for easier operation
- }
-
- out = cmsBuildTabulatedToneCurveFloat(ContextID, nPoints, SampledPoints);
-
-Error:
-
- cmsDeleteTransform(xform);
- if (SampledPoints) _cmsFree(ContextID, SampledPoints);
-
- return out;
-}
-
-
-// Compute Black tone curve on a CMYK -> CMYK transform. This is done by
-// using the proof direction on both profiles to find K->L* relationship
-// then joining both curves. dwFlags may include black point compensation.
-cmsToneCurve* _cmsBuildKToneCurve(cmsContext ContextID,
- cmsUInt32Number nPoints,
- cmsUInt32Number nProfiles,
- const cmsUInt32Number Intents[],
- const cmsHPROFILE hProfiles[],
- const cmsBool BPC[],
- const cmsFloat64Number AdaptationStates[],
- cmsUInt32Number dwFlags)
-{
- cmsToneCurve *in, *out, *KTone;
-
- // Make sure CMYK -> CMYK
- if (cmsGetColorSpace(hProfiles[0]) != cmsSigCmykData ||
- cmsGetColorSpace(hProfiles[nProfiles-1])!= cmsSigCmykData) return NULL;
-
-
- // Make sure last is an output profile
- if (cmsGetDeviceClass(hProfiles[nProfiles - 1]) != cmsSigOutputClass) return NULL;
-
- // Create individual curves. BPC works also as each K to L* is
- // computed as a BPC to zero black point in case of L*
- in = ComputeKToLstar(ContextID, nPoints, nProfiles - 1, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
- if (in == NULL) return NULL;
-
- out = ComputeKToLstar(ContextID, nPoints, 1,
- Intents + (nProfiles - 1),
- &hProfiles [nProfiles - 1],
- BPC + (nProfiles - 1),
- AdaptationStates + (nProfiles - 1),
- dwFlags);
- if (out == NULL) {
- cmsFreeToneCurve(in);
- return NULL;
- }
-
- // Build the relationship. This effectively limits the maximum accuracy to 16 bits, but
- // since this is used on black-preserving LUTs, we are not loosing accuracy in any case
- KTone = cmsJoinToneCurve(ContextID, in, out, nPoints);
-
- // Get rid of components
- cmsFreeToneCurve(in); cmsFreeToneCurve(out);
-
- // Something went wrong...
- if (KTone == NULL) return NULL;
-
- // Make sure it is monotonic
- if (!cmsIsToneCurveMonotonic(KTone)) {
- cmsFreeToneCurve(KTone);
- return NULL;
- }
-
- return KTone;
-}
-
-
-// Gamut LUT Creation -----------------------------------------------------------------------------------------
-
-// Used by gamut & softproofing
-
-typedef struct {
-
- cmsHTRANSFORM hInput; // From whatever input color space. 16 bits to DBL
- cmsHTRANSFORM hForward, hReverse; // Transforms going from Lab to colorant and back
- cmsFloat64Number Thereshold; // The thereshold after which is considered out of gamut
-
- } GAMUTCHAIN;
-
-// This sampler does compute gamut boundaries by comparing original
-// values with a transform going back and forth. Values above ERR_THERESHOLD
-// of maximum are considered out of gamut.
-
-#define ERR_THERESHOLD 5
-
-
-static
-int GamutSampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
-{
- GAMUTCHAIN* t = (GAMUTCHAIN* ) Cargo;
- cmsCIELab LabIn1, LabOut1;
- cmsCIELab LabIn2, LabOut2;
- cmsUInt16Number Proof[cmsMAXCHANNELS], Proof2[cmsMAXCHANNELS];
- cmsFloat64Number dE1, dE2, ErrorRatio;
-
- // Assume in-gamut by default.
- ErrorRatio = 1.0;
-
- // Convert input to Lab
- cmsDoTransform(t -> hInput, In, &LabIn1, 1);
-
- // converts from PCS to colorant. This always
- // does return in-gamut values,
- cmsDoTransform(t -> hForward, &LabIn1, Proof, 1);
-
- // Now, do the inverse, from colorant to PCS.
- cmsDoTransform(t -> hReverse, Proof, &LabOut1, 1);
-
- memmove(&LabIn2, &LabOut1, sizeof(cmsCIELab));
-
- // Try again, but this time taking Check as input
- cmsDoTransform(t -> hForward, &LabOut1, Proof2, 1);
- cmsDoTransform(t -> hReverse, Proof2, &LabOut2, 1);
-
- // Take difference of direct value
- dE1 = cmsDeltaE(&LabIn1, &LabOut1);
-
- // Take difference of converted value
- dE2 = cmsDeltaE(&LabIn2, &LabOut2);
-
-
- // if dE1 is small and dE2 is small, value is likely to be in gamut
- if (dE1 < t->Thereshold && dE2 < t->Thereshold)
- Out[0] = 0;
- else {
-
- // if dE1 is small and dE2 is big, undefined. Assume in gamut
- if (dE1 < t->Thereshold && dE2 > t->Thereshold)
- Out[0] = 0;
- else
- // dE1 is big and dE2 is small, clearly out of gamut
- if (dE1 > t->Thereshold && dE2 < t->Thereshold)
- Out[0] = (cmsUInt16Number) _cmsQuickFloor((dE1 - t->Thereshold) + .5);
- else {
-
- // dE1 is big and dE2 is also big, could be due to perceptual mapping
- // so take error ratio
- if (dE2 == 0.0)
- ErrorRatio = dE1;
- else
- ErrorRatio = dE1 / dE2;
-
- if (ErrorRatio > t->Thereshold)
- Out[0] = (cmsUInt16Number) _cmsQuickFloor((ErrorRatio - t->Thereshold) + .5);
- else
- Out[0] = 0;
- }
- }
-
-
- return TRUE;
-}
-
-// Does compute a gamut LUT going back and forth across pcs -> relativ. colorimetric intent -> pcs
-// the dE obtained is then annotated on the LUT. Values truely out of gamut are clipped to dE = 0xFFFE
-// and values changed are supposed to be handled by any gamut remapping, so, are out of gamut as well.
-//
-// **WARNING: This algorithm does assume that gamut remapping algorithms does NOT move in-gamut colors,
-// of course, many perceptual and saturation intents does not work in such way, but relativ. ones should.
-
-cmsPipeline* _cmsCreateGamutCheckPipeline(cmsContext ContextID,
- cmsHPROFILE hProfiles[],
- cmsBool BPC[],
- cmsUInt32Number Intents[],
- cmsFloat64Number AdaptationStates[],
- cmsUInt32Number nGamutPCSposition,
- cmsHPROFILE hGamut)
-{
- cmsHPROFILE hLab;
- cmsPipeline* Gamut;
- cmsStage* CLUT;
- cmsUInt32Number dwFormat;
- GAMUTCHAIN Chain;
- int nChannels, nGridpoints;
- cmsColorSpaceSignature ColorSpace;
- cmsUInt32Number i;
- cmsHPROFILE ProfileList[256];
- cmsBool BPCList[256];
- cmsFloat64Number AdaptationList[256];
- cmsUInt32Number IntentList[256];
-
- memset(&Chain, 0, sizeof(GAMUTCHAIN));
-
-
- if (nGamutPCSposition <= 0 || nGamutPCSposition > 255) {
- cmsSignalError(ContextID, cmsERROR_RANGE, "Wrong position of PCS. 1..255 expected, %d found.", nGamutPCSposition);
- return NULL;
- }
-
- hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
- if (hLab == NULL) return NULL;
-
-
- // The figure of merit. On matrix-shaper profiles, should be almost zero as
- // the conversion is pretty exact. On LUT based profiles, different resolutions
- // of input and output CLUT may result in differences.
-
- if (cmsIsMatrixShaper(hGamut)) {
-
- Chain.Thereshold = 1.0;
- }
- else {
- Chain.Thereshold = ERR_THERESHOLD;
- }
-
-
- // Create a copy of parameters
- for (i=0; i < nGamutPCSposition; i++) {
- ProfileList[i] = hProfiles[i];
- BPCList[i] = BPC[i];
- AdaptationList[i] = AdaptationStates[i];
- IntentList[i] = Intents[i];
- }
-
- // Fill Lab identity
- ProfileList[nGamutPCSposition] = hLab;
- BPCList[nGamutPCSposition] = 0;
- AdaptationList[nGamutPCSposition] = 1.0;
- IntentList[nGamutPCSposition] = INTENT_RELATIVE_COLORIMETRIC;
-
-
- ColorSpace = cmsGetColorSpace(hGamut);
-
- nChannels = cmsChannelsOf(ColorSpace);
- nGridpoints = _cmsReasonableGridpointsByColorspace(ColorSpace, cmsFLAGS_HIGHRESPRECALC);
- dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2));
-
- // 16 bits to Lab double
- Chain.hInput = cmsCreateExtendedTransform(ContextID,
- nGamutPCSposition + 1,
- ProfileList,
- BPCList,
- IntentList,
- AdaptationList,
- NULL, 0,
- dwFormat, TYPE_Lab_DBL,
- cmsFLAGS_NOCACHE);
-
-
- // Does create the forward step. Lab double to device
- dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2));
- Chain.hForward = cmsCreateTransformTHR(ContextID,
- hLab, TYPE_Lab_DBL,
- hGamut, dwFormat,
- INTENT_RELATIVE_COLORIMETRIC,
- cmsFLAGS_NOCACHE);
-
- // Does create the backwards step
- Chain.hReverse = cmsCreateTransformTHR(ContextID, hGamut, dwFormat,
- hLab, TYPE_Lab_DBL,
- INTENT_RELATIVE_COLORIMETRIC,
- cmsFLAGS_NOCACHE);
-
-
- // All ok?
- if (Chain.hInput && Chain.hForward && Chain.hReverse) {
-
- // Go on, try to compute gamut LUT from PCS. This consist on a single channel containing
- // dE when doing a transform back and forth on the colorimetric intent.
-
- Gamut = cmsPipelineAlloc(ContextID, 3, 1);
- if (Gamut != NULL) {
-
- CLUT = cmsStageAllocCLut16bit(ContextID, nGridpoints, nChannels, 1, NULL);
- if (!cmsPipelineInsertStage(Gamut, cmsAT_BEGIN, CLUT)) {
- cmsPipelineFree(Gamut);
- Gamut = NULL;
- }
- else {
- cmsStageSampleCLut16bit(CLUT, GamutSampler, (void*) &Chain, 0);
- }
- }
- }
- else
- Gamut = NULL; // Didn't work...
-
- // Free all needed stuff.
- if (Chain.hInput) cmsDeleteTransform(Chain.hInput);
- if (Chain.hForward) cmsDeleteTransform(Chain.hForward);
- if (Chain.hReverse) cmsDeleteTransform(Chain.hReverse);
- if (hLab) cmsCloseProfile(hLab);
-
- // And return computed hull
- return Gamut;
-}
-
-// Total Area Coverage estimation ----------------------------------------------------------------
-
-typedef struct {
- cmsUInt32Number nOutputChans;
- cmsHTRANSFORM hRoundTrip;
- cmsFloat32Number MaxTAC;
- cmsFloat32Number MaxInput[cmsMAXCHANNELS];
-
-} cmsTACestimator;
-
-
-// This callback just accounts the maximum ink dropped in the given node. It does not populate any
-// memory, as the destination table is NULL. Its only purpose it to know the global maximum.
-static
-int EstimateTAC(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void * Cargo)
-{
- cmsTACestimator* bp = (cmsTACestimator*) Cargo;
- cmsFloat32Number RoundTrip[cmsMAXCHANNELS];
- cmsUInt32Number i;
- cmsFloat32Number Sum;
-
-
- // Evaluate the xform
- cmsDoTransform(bp->hRoundTrip, In, RoundTrip, 1);
-
- // All all amounts of ink
- for (Sum=0, i=0; i < bp ->nOutputChans; i++)
- Sum += RoundTrip[i];
-
- // If above maximum, keep track of input values
- if (Sum > bp ->MaxTAC) {
-
- bp ->MaxTAC = Sum;
-
- for (i=0; i < bp ->nOutputChans; i++) {
- bp ->MaxInput[i] = In[i];
- }
- }
-
- return TRUE;
-
- cmsUNUSED_PARAMETER(Out);
-}
-
-
-// Detect Total area coverage of the profile
-cmsFloat64Number CMSEXPORT cmsDetectTAC(cmsHPROFILE hProfile)
-{
- cmsTACestimator bp;
- cmsUInt32Number dwFormatter;
- cmsUInt32Number GridPoints[MAX_INPUT_DIMENSIONS];
- cmsHPROFILE hLab;
- cmsContext ContextID = cmsGetProfileContextID(hProfile);
-
- // TAC only works on output profiles
- if (cmsGetDeviceClass(hProfile) != cmsSigOutputClass) {
- return 0;
- }
-
- // Create a fake formatter for result
- dwFormatter = cmsFormatterForColorspaceOfProfile(hProfile, 4, TRUE);
-
- bp.nOutputChans = T_CHANNELS(dwFormatter);
- bp.MaxTAC = 0; // Initial TAC is 0
-
- // for safety
- if (bp.nOutputChans >= cmsMAXCHANNELS) return 0;
-
- hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
- if (hLab == NULL) return 0;
- // Setup a roundtrip on perceptual intent in output profile for TAC estimation
- bp.hRoundTrip = cmsCreateTransformTHR(ContextID, hLab, TYPE_Lab_16,
- hProfile, dwFormatter, INTENT_PERCEPTUAL, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
-
- cmsCloseProfile(hLab);
- if (bp.hRoundTrip == NULL) return 0;
-
- // For L* we only need black and white. For C* we need many points
- GridPoints[0] = 6;
- GridPoints[1] = 74;
- GridPoints[2] = 74;
-
-
- if (!cmsSliceSpace16(3, GridPoints, EstimateTAC, &bp)) {
- bp.MaxTAC = 0;
- }
-
- cmsDeleteTransform(bp.hRoundTrip);
-
- // Results in %
- return bp.MaxTAC;
-}
-
-
-// Carefully, clamp on CIELab space.
-
-cmsBool CMSEXPORT cmsDesaturateLab(cmsCIELab* Lab,
- double amax, double amin,
- double bmax, double bmin)
-{
-
- // Whole Luma surface to zero
-
- if (Lab -> L < 0) {
-
- Lab-> L = Lab->a = Lab-> b = 0.0;
- return FALSE;
- }
-
- // Clamp white, DISCARD HIGHLIGHTS. This is done
- // in such way because icc spec doesn't allow the
- // use of L>100 as a highlight means.
-
- if (Lab->L > 100)
- Lab -> L = 100;
-
- // Check out gamut prism, on a, b faces
-
- if (Lab -> a < amin || Lab->a > amax||
- Lab -> b < bmin || Lab->b > bmax) {
-
- cmsCIELCh LCh;
- double h, slope;
-
- // Falls outside a, b limits. Transports to LCh space,
- // and then do the clipping
-
-
- if (Lab -> a == 0.0) { // Is hue exactly 90?
-
- // atan will not work, so clamp here
- Lab -> b = Lab->b < 0 ? bmin : bmax;
- return TRUE;
- }
-
- cmsLab2LCh(&LCh, Lab);
-
- slope = Lab -> b / Lab -> a;
- h = LCh.h;
-
- // There are 4 zones
-
- if ((h >= 0. && h < 45.) ||
- (h >= 315 && h <= 360.)) {
-
- // clip by amax
- Lab -> a = amax;
- Lab -> b = amax * slope;
- }
- else
- if (h >= 45. && h < 135.)
- {
- // clip by bmax
- Lab -> b = bmax;
- Lab -> a = bmax / slope;
- }
- else
- if (h >= 135. && h < 225.) {
- // clip by amin
- Lab -> a = amin;
- Lab -> b = amin * slope;
-
- }
- else
- if (h >= 225. && h < 315.) {
- // clip by bmin
- Lab -> b = bmin;
- Lab -> a = bmin / slope;
- }
- else {
- cmsSignalError(0, cmsERROR_RANGE, "Invalid angle");
- return FALSE;
- }
-
- }
-
- return TRUE;
-}
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2012 Marti Maria Saguer
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the Software
+// is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
+// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+//---------------------------------------------------------------------------------
+//
+
+#include "lcms2_internal.h"
+
+
+// Auxiliar: append a Lab identity after the given sequence of profiles
+// and return the transform. Lab profile is closed, rest of profiles are kept open.
+cmsHTRANSFORM _cmsChain2Lab(cmsContext ContextID,
+ cmsUInt32Number nProfiles,
+ cmsUInt32Number InputFormat,
+ cmsUInt32Number OutputFormat,
+ const cmsUInt32Number Intents[],
+ const cmsHPROFILE hProfiles[],
+ const cmsBool BPC[],
+ const cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags)
+{
+ cmsHTRANSFORM xform;
+ cmsHPROFILE hLab;
+ cmsHPROFILE ProfileList[256];
+ cmsBool BPCList[256];
+ cmsFloat64Number AdaptationList[256];
+ cmsUInt32Number IntentList[256];
+ cmsUInt32Number i;
+
+ // This is a rather big number and there is no need of dynamic memory
+ // since we are adding a profile, 254 + 1 = 255 and this is the limit
+ if (nProfiles > 254) return NULL;
+
+ // The output space
+ hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
+ if (hLab == NULL) return NULL;
+
+ // Create a copy of parameters
+ for (i=0; i < nProfiles; i++) {
+
+ ProfileList[i] = hProfiles[i];
+ BPCList[i] = BPC[i];
+ AdaptationList[i] = AdaptationStates[i];
+ IntentList[i] = Intents[i];
+ }
+
+ // Place Lab identity at chain's end.
+ ProfileList[nProfiles] = hLab;
+ BPCList[nProfiles] = 0;
+ AdaptationList[nProfiles] = 1.0;
+ IntentList[nProfiles] = INTENT_RELATIVE_COLORIMETRIC;
+
+ // Create the transform
+ xform = cmsCreateExtendedTransform(ContextID, nProfiles + 1, ProfileList,
+ BPCList,
+ IntentList,
+ AdaptationList,
+ NULL, 0,
+ InputFormat,
+ OutputFormat,
+ dwFlags);
+
+ cmsCloseProfile(hLab);
+
+ return xform;
+}
+
+
+// Compute K -> L* relationship. Flags may include black point compensation. In this case,
+// the relationship is assumed from the profile with BPC to a black point zero.
+static
+cmsToneCurve* ComputeKToLstar(cmsContext ContextID,
+ cmsUInt32Number nPoints,
+ cmsUInt32Number nProfiles,
+ const cmsUInt32Number Intents[],
+ const cmsHPROFILE hProfiles[],
+ const cmsBool BPC[],
+ const cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags)
+{
+ cmsToneCurve* out = NULL;
+ cmsUInt32Number i;
+ cmsHTRANSFORM xform;
+ cmsCIELab Lab;
+ cmsFloat32Number cmyk[4];
+ cmsFloat32Number* SampledPoints;
+
+ xform = _cmsChain2Lab(ContextID, nProfiles, TYPE_CMYK_FLT, TYPE_Lab_DBL, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
+ if (xform == NULL) return NULL;
+
+ SampledPoints = (cmsFloat32Number*) _cmsCalloc(ContextID, nPoints, sizeof(cmsFloat32Number));
+ if (SampledPoints == NULL) goto Error;
+
+ for (i=0; i < nPoints; i++) {
+
+ cmyk[0] = 0;
+ cmyk[1] = 0;
+ cmyk[2] = 0;
+ cmyk[3] = (cmsFloat32Number) ((i * 100.0) / (nPoints-1));
+
+ cmsDoTransform(xform, cmyk, &Lab, 1);
+ SampledPoints[i]= (cmsFloat32Number) (1.0 - Lab.L / 100.0); // Negate K for easier operation
+ }
+
+ out = cmsBuildTabulatedToneCurveFloat(ContextID, nPoints, SampledPoints);
+
+Error:
+
+ cmsDeleteTransform(xform);
+ if (SampledPoints) _cmsFree(ContextID, SampledPoints);
+
+ return out;
+}
+
+
+// Compute Black tone curve on a CMYK -> CMYK transform. This is done by
+// using the proof direction on both profiles to find K->L* relationship
+// then joining both curves. dwFlags may include black point compensation.
+cmsToneCurve* _cmsBuildKToneCurve(cmsContext ContextID,
+ cmsUInt32Number nPoints,
+ cmsUInt32Number nProfiles,
+ const cmsUInt32Number Intents[],
+ const cmsHPROFILE hProfiles[],
+ const cmsBool BPC[],
+ const cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number dwFlags)
+{
+ cmsToneCurve *in, *out, *KTone;
+
+ // Make sure CMYK -> CMYK
+ if (cmsGetColorSpace(hProfiles[0]) != cmsSigCmykData ||
+ cmsGetColorSpace(hProfiles[nProfiles-1])!= cmsSigCmykData) return NULL;
+
+
+ // Make sure last is an output profile
+ if (cmsGetDeviceClass(hProfiles[nProfiles - 1]) != cmsSigOutputClass) return NULL;
+
+ // Create individual curves. BPC works also as each K to L* is
+ // computed as a BPC to zero black point in case of L*
+ in = ComputeKToLstar(ContextID, nPoints, nProfiles - 1, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
+ if (in == NULL) return NULL;
+
+ out = ComputeKToLstar(ContextID, nPoints, 1,
+ Intents + (nProfiles - 1),
+ &hProfiles [nProfiles - 1],
+ BPC + (nProfiles - 1),
+ AdaptationStates + (nProfiles - 1),
+ dwFlags);
+ if (out == NULL) {
+ cmsFreeToneCurve(in);
+ return NULL;
+ }
+
+ // Build the relationship. This effectively limits the maximum accuracy to 16 bits, but
+ // since this is used on black-preserving LUTs, we are not loosing accuracy in any case
+ KTone = cmsJoinToneCurve(ContextID, in, out, nPoints);
+
+ // Get rid of components
+ cmsFreeToneCurve(in); cmsFreeToneCurve(out);
+
+ // Something went wrong...
+ if (KTone == NULL) return NULL;
+
+ // Make sure it is monotonic
+ if (!cmsIsToneCurveMonotonic(KTone)) {
+ cmsFreeToneCurve(KTone);
+ return NULL;
+ }
+
+ return KTone;
+}
+
+
+// Gamut LUT Creation -----------------------------------------------------------------------------------------
+
+// Used by gamut & softproofing
+
+typedef struct {
+
+ cmsHTRANSFORM hInput; // From whatever input color space. 16 bits to DBL
+ cmsHTRANSFORM hForward, hReverse; // Transforms going from Lab to colorant and back
+ cmsFloat64Number Thereshold; // The thereshold after which is considered out of gamut
+
+ } GAMUTCHAIN;
+
+// This sampler does compute gamut boundaries by comparing original
+// values with a transform going back and forth. Values above ERR_THERESHOLD
+// of maximum are considered out of gamut.
+
+#define ERR_THERESHOLD 5
+
+
+static
+int GamutSampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
+{
+ GAMUTCHAIN* t = (GAMUTCHAIN* ) Cargo;
+ cmsCIELab LabIn1, LabOut1;
+ cmsCIELab LabIn2, LabOut2;
+ cmsUInt16Number Proof[cmsMAXCHANNELS], Proof2[cmsMAXCHANNELS];
+ cmsFloat64Number dE1, dE2, ErrorRatio;
+
+ // Assume in-gamut by default.
+ ErrorRatio = 1.0;
+
+ // Convert input to Lab
+ cmsDoTransform(t -> hInput, In, &LabIn1, 1);
+
+ // converts from PCS to colorant. This always
+ // does return in-gamut values,
+ cmsDoTransform(t -> hForward, &LabIn1, Proof, 1);
+
+ // Now, do the inverse, from colorant to PCS.
+ cmsDoTransform(t -> hReverse, Proof, &LabOut1, 1);
+
+ memmove(&LabIn2, &LabOut1, sizeof(cmsCIELab));
+
+ // Try again, but this time taking Check as input
+ cmsDoTransform(t -> hForward, &LabOut1, Proof2, 1);
+ cmsDoTransform(t -> hReverse, Proof2, &LabOut2, 1);
+
+ // Take difference of direct value
+ dE1 = cmsDeltaE(&LabIn1, &LabOut1);
+
+ // Take difference of converted value
+ dE2 = cmsDeltaE(&LabIn2, &LabOut2);
+
+
+ // if dE1 is small and dE2 is small, value is likely to be in gamut
+ if (dE1 < t->Thereshold && dE2 < t->Thereshold)
+ Out[0] = 0;
+ else {
+
+ // if dE1 is small and dE2 is big, undefined. Assume in gamut
+ if (dE1 < t->Thereshold && dE2 > t->Thereshold)
+ Out[0] = 0;
+ else
+ // dE1 is big and dE2 is small, clearly out of gamut
+ if (dE1 > t->Thereshold && dE2 < t->Thereshold)
+ Out[0] = (cmsUInt16Number) _cmsQuickFloor((dE1 - t->Thereshold) + .5);
+ else {
+
+ // dE1 is big and dE2 is also big, could be due to perceptual mapping
+ // so take error ratio
+ if (dE2 == 0.0)
+ ErrorRatio = dE1;
+ else
+ ErrorRatio = dE1 / dE2;
+
+ if (ErrorRatio > t->Thereshold)
+ Out[0] = (cmsUInt16Number) _cmsQuickFloor((ErrorRatio - t->Thereshold) + .5);
+ else
+ Out[0] = 0;
+ }
+ }
+
+
+ return TRUE;
+}
+
+// Does compute a gamut LUT going back and forth across pcs -> relativ. colorimetric intent -> pcs
+// the dE obtained is then annotated on the LUT. Values truely out of gamut are clipped to dE = 0xFFFE
+// and values changed are supposed to be handled by any gamut remapping, so, are out of gamut as well.
+//
+// **WARNING: This algorithm does assume that gamut remapping algorithms does NOT move in-gamut colors,
+// of course, many perceptual and saturation intents does not work in such way, but relativ. ones should.
+
+cmsPipeline* _cmsCreateGamutCheckPipeline(cmsContext ContextID,
+ cmsHPROFILE hProfiles[],
+ cmsBool BPC[],
+ cmsUInt32Number Intents[],
+ cmsFloat64Number AdaptationStates[],
+ cmsUInt32Number nGamutPCSposition,
+ cmsHPROFILE hGamut)
+{
+ cmsHPROFILE hLab;
+ cmsPipeline* Gamut;
+ cmsStage* CLUT;
+ cmsUInt32Number dwFormat;
+ GAMUTCHAIN Chain;
+ int nChannels, nGridpoints;
+ cmsColorSpaceSignature ColorSpace;
+ cmsUInt32Number i;
+ cmsHPROFILE ProfileList[256];
+ cmsBool BPCList[256];
+ cmsFloat64Number AdaptationList[256];
+ cmsUInt32Number IntentList[256];
+
+ memset(&Chain, 0, sizeof(GAMUTCHAIN));
+
+
+ if (nGamutPCSposition <= 0 || nGamutPCSposition > 255) {
+ cmsSignalError(ContextID, cmsERROR_RANGE, "Wrong position of PCS. 1..255 expected, %d found.", nGamutPCSposition);
+ return NULL;
+ }
+
+ hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
+ if (hLab == NULL) return NULL;
+
+
+ // The figure of merit. On matrix-shaper profiles, should be almost zero as
+ // the conversion is pretty exact. On LUT based profiles, different resolutions
+ // of input and output CLUT may result in differences.
+
+ if (cmsIsMatrixShaper(hGamut)) {
+
+ Chain.Thereshold = 1.0;
+ }
+ else {
+ Chain.Thereshold = ERR_THERESHOLD;
+ }
+
+
+ // Create a copy of parameters
+ for (i=0; i < nGamutPCSposition; i++) {
+ ProfileList[i] = hProfiles[i];
+ BPCList[i] = BPC[i];
+ AdaptationList[i] = AdaptationStates[i];
+ IntentList[i] = Intents[i];
+ }
+
+ // Fill Lab identity
+ ProfileList[nGamutPCSposition] = hLab;
+ BPCList[nGamutPCSposition] = 0;
+ AdaptationList[nGamutPCSposition] = 1.0;
+ IntentList[nGamutPCSposition] = INTENT_RELATIVE_COLORIMETRIC;
+
+
+ ColorSpace = cmsGetColorSpace(hGamut);
+
+ nChannels = cmsChannelsOf(ColorSpace);
+ nGridpoints = _cmsReasonableGridpointsByColorspace(ColorSpace, cmsFLAGS_HIGHRESPRECALC);
+ dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2));
+
+ // 16 bits to Lab double
+ Chain.hInput = cmsCreateExtendedTransform(ContextID,
+ nGamutPCSposition + 1,
+ ProfileList,
+ BPCList,
+ IntentList,
+ AdaptationList,
+ NULL, 0,
+ dwFormat, TYPE_Lab_DBL,
+ cmsFLAGS_NOCACHE);
+
+
+ // Does create the forward step. Lab double to device
+ dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2));
+ Chain.hForward = cmsCreateTransformTHR(ContextID,
+ hLab, TYPE_Lab_DBL,
+ hGamut, dwFormat,
+ INTENT_RELATIVE_COLORIMETRIC,
+ cmsFLAGS_NOCACHE);
+
+ // Does create the backwards step
+ Chain.hReverse = cmsCreateTransformTHR(ContextID, hGamut, dwFormat,
+ hLab, TYPE_Lab_DBL,
+ INTENT_RELATIVE_COLORIMETRIC,
+ cmsFLAGS_NOCACHE);
+
+
+ // All ok?
+ if (Chain.hInput && Chain.hForward && Chain.hReverse) {
+
+ // Go on, try to compute gamut LUT from PCS. This consist on a single channel containing
+ // dE when doing a transform back and forth on the colorimetric intent.
+
+ Gamut = cmsPipelineAlloc(ContextID, 3, 1);
+ if (Gamut != NULL) {
+
+ CLUT = cmsStageAllocCLut16bit(ContextID, nGridpoints, nChannels, 1, NULL);
+ if (!cmsPipelineInsertStage(Gamut, cmsAT_BEGIN, CLUT)) {
+ cmsPipelineFree(Gamut);
+ Gamut = NULL;
+ }
+ else {
+ cmsStageSampleCLut16bit(CLUT, GamutSampler, (void*) &Chain, 0);
+ }
+ }
+ }
+ else
+ Gamut = NULL; // Didn't work...
+
+ // Free all needed stuff.
+ if (Chain.hInput) cmsDeleteTransform(Chain.hInput);
+ if (Chain.hForward) cmsDeleteTransform(Chain.hForward);
+ if (Chain.hReverse) cmsDeleteTransform(Chain.hReverse);
+ if (hLab) cmsCloseProfile(hLab);
+
+ // And return computed hull
+ return Gamut;
+}
+
+// Total Area Coverage estimation ----------------------------------------------------------------
+
+typedef struct {
+ cmsUInt32Number nOutputChans;
+ cmsHTRANSFORM hRoundTrip;
+ cmsFloat32Number MaxTAC;
+ cmsFloat32Number MaxInput[cmsMAXCHANNELS];
+
+} cmsTACestimator;
+
+
+// This callback just accounts the maximum ink dropped in the given node. It does not populate any
+// memory, as the destination table is NULL. Its only purpose it to know the global maximum.
+static
+int EstimateTAC(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void * Cargo)
+{
+ cmsTACestimator* bp = (cmsTACestimator*) Cargo;
+ cmsFloat32Number RoundTrip[cmsMAXCHANNELS];
+ cmsUInt32Number i;
+ cmsFloat32Number Sum;
+
+
+ // Evaluate the xform
+ cmsDoTransform(bp->hRoundTrip, In, RoundTrip, 1);
+
+ // All all amounts of ink
+ for (Sum=0, i=0; i < bp ->nOutputChans; i++)
+ Sum += RoundTrip[i];
+
+ // If above maximum, keep track of input values
+ if (Sum > bp ->MaxTAC) {
+
+ bp ->MaxTAC = Sum;
+
+ for (i=0; i < bp ->nOutputChans; i++) {
+ bp ->MaxInput[i] = In[i];
+ }
+ }
+
+ return TRUE;
+
+ cmsUNUSED_PARAMETER(Out);
+}
+
+
+// Detect Total area coverage of the profile
+cmsFloat64Number CMSEXPORT cmsDetectTAC(cmsHPROFILE hProfile)
+{
+ cmsTACestimator bp;
+ cmsUInt32Number dwFormatter;
+ cmsUInt32Number GridPoints[MAX_INPUT_DIMENSIONS];
+ cmsHPROFILE hLab;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ // TAC only works on output profiles
+ if (cmsGetDeviceClass(hProfile) != cmsSigOutputClass) {
+ return 0;
+ }
+
+ // Create a fake formatter for result
+ dwFormatter = cmsFormatterForColorspaceOfProfile(hProfile, 4, TRUE);
+
+ bp.nOutputChans = T_CHANNELS(dwFormatter);
+ bp.MaxTAC = 0; // Initial TAC is 0
+
+ // for safety
+ if (bp.nOutputChans >= cmsMAXCHANNELS) return 0;
+
+ hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
+ if (hLab == NULL) return 0;
+ // Setup a roundtrip on perceptual intent in output profile for TAC estimation
+ bp.hRoundTrip = cmsCreateTransformTHR(ContextID, hLab, TYPE_Lab_16,
+ hProfile, dwFormatter, INTENT_PERCEPTUAL, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
+
+ cmsCloseProfile(hLab);
+ if (bp.hRoundTrip == NULL) return 0;
+
+ // For L* we only need black and white. For C* we need many points
+ GridPoints[0] = 6;
+ GridPoints[1] = 74;
+ GridPoints[2] = 74;
+
+
+ if (!cmsSliceSpace16(3, GridPoints, EstimateTAC, &bp)) {
+ bp.MaxTAC = 0;
+ }
+
+ cmsDeleteTransform(bp.hRoundTrip);
+
+ // Results in %
+ return bp.MaxTAC;
+}
+
+
+// Carefully, clamp on CIELab space.
+
+cmsBool CMSEXPORT cmsDesaturateLab(cmsCIELab* Lab,
+ double amax, double amin,
+ double bmax, double bmin)
+{
+
+ // Whole Luma surface to zero
+
+ if (Lab -> L < 0) {
+
+ Lab-> L = Lab->a = Lab-> b = 0.0;
+ return FALSE;
+ }
+
+ // Clamp white, DISCARD HIGHLIGHTS. This is done
+ // in such way because icc spec doesn't allow the
+ // use of L>100 as a highlight means.
+
+ if (Lab->L > 100)
+ Lab -> L = 100;
+
+ // Check out gamut prism, on a, b faces
+
+ if (Lab -> a < amin || Lab->a > amax||
+ Lab -> b < bmin || Lab->b > bmax) {
+
+ cmsCIELCh LCh;
+ double h, slope;
+
+ // Falls outside a, b limits. Transports to LCh space,
+ // and then do the clipping
+
+
+ if (Lab -> a == 0.0) { // Is hue exactly 90?
+
+ // atan will not work, so clamp here
+ Lab -> b = Lab->b < 0 ? bmin : bmax;
+ return TRUE;
+ }
+
+ cmsLab2LCh(&LCh, Lab);
+
+ slope = Lab -> b / Lab -> a;
+ h = LCh.h;
+
+ // There are 4 zones
+
+ if ((h >= 0. && h < 45.) ||
+ (h >= 315 && h <= 360.)) {
+
+ // clip by amax
+ Lab -> a = amax;
+ Lab -> b = amax * slope;
+ }
+ else
+ if (h >= 45. && h < 135.)
+ {
+ // clip by bmax
+ Lab -> b = bmax;
+ Lab -> a = bmax / slope;
+ }
+ else
+ if (h >= 135. && h < 225.) {
+ // clip by amin
+ Lab -> a = amin;
+ Lab -> b = amin * slope;
+
+ }
+ else
+ if (h >= 225. && h < 315.) {
+ // clip by bmin
+ Lab -> b = bmin;
+ Lab -> a = bmin / slope;
+ }
+ else {
+ cmsSignalError(0, cmsERROR_RANGE, "Invalid angle");
+ return FALSE;
+ }
+
+ }
+
+ return TRUE;
+}
diff --git a/core/src/fxcodec/lcms2/src/fx_cmscam02.c b/core/src/fxcodec/lcms2/src/fx_cmscam02.c
index 05584aa8f8..3268424870 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmscam02.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmscam02.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmscam02.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmscam02.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmscgats.c b/core/src/fxcodec/lcms2/src/fx_cmscgats.c
index 80dcba7830..9e00fe6c3e 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmscgats.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmscgats.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmscgats.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmscgats.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmscnvrt.c b/core/src/fxcodec/lcms2/src/fx_cmscnvrt.c
index 9b8a292f7b..9b9eb99c70 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmscnvrt.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmscnvrt.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmscnvrt.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmscnvrt.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmserr.c b/core/src/fxcodec/lcms2/src/fx_cmserr.c
index 6929e7b34c..27d0696c85 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmserr.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmserr.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmserr.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmserr.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsgamma.c b/core/src/fxcodec/lcms2/src/fx_cmsgamma.c
index f5bb690220..9936c62b0f 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsgamma.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsgamma.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsgamma.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsgamma.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsgmt.c b/core/src/fxcodec/lcms2/src/fx_cmsgmt.c
index ea82b15be0..dfe4cc86f0 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsgmt.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsgmt.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsgmt.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsgmt.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmshalf.c b/core/src/fxcodec/lcms2/src/fx_cmshalf.c
index 6ed6b2b075..9c7e07c580 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmshalf.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmshalf.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmshalf.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmshalf.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsintrp.c b/core/src/fxcodec/lcms2/src/fx_cmsintrp.c
index affca53ca9..8eacd55bc0 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsintrp.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsintrp.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsintrp.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsintrp.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsio0.c b/core/src/fxcodec/lcms2/src/fx_cmsio0.c
index 2b6bc689cc..a4aed23c94 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsio0.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsio0.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsio0.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsio0.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsio1.c b/core/src/fxcodec/lcms2/src/fx_cmsio1.c
index 192e879063..34ce927c4f 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsio1.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsio1.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsio1.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsio1.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmslut.c b/core/src/fxcodec/lcms2/src/fx_cmslut.c
index d9a319c774..d032e3b0cb 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmslut.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmslut.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmslut.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmslut.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsmd5.c b/core/src/fxcodec/lcms2/src/fx_cmsmd5.c
index 3a3a99719c..028db4a8eb 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsmd5.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsmd5.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsmd5.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsmd5.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsmtrx.c b/core/src/fxcodec/lcms2/src/fx_cmsmtrx.c
index 667ac1f09e..ad04dcc307 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsmtrx.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsmtrx.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsmtrx.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsmtrx.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsnamed.c b/core/src/fxcodec/lcms2/src/fx_cmsnamed.c
index 5e1b028e94..93a0132aa7 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsnamed.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsnamed.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsnamed.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsnamed.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsopt.c b/core/src/fxcodec/lcms2/src/fx_cmsopt.c
index f12429c551..e16c735cbb 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsopt.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsopt.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsopt.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsopt.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmspack.c b/core/src/fxcodec/lcms2/src/fx_cmspack.c
index 6e0640905d..d125d33a32 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmspack.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmspack.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmspack.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmspack.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmspcs.c b/core/src/fxcodec/lcms2/src/fx_cmspcs.c
index 48ce39de52..2a9d6dde99 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmspcs.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmspcs.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmspcs.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmspcs.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsplugin.c b/core/src/fxcodec/lcms2/src/fx_cmsplugin.c
index 39266c2222..ab7e6c31ee 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsplugin.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsplugin.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsplugin.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsplugin.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsps2.c b/core/src/fxcodec/lcms2/src/fx_cmsps2.c
index dcf55409e4..b6a1ea9f97 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsps2.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsps2.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsps2.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsps2.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmssamp.c b/core/src/fxcodec/lcms2/src/fx_cmssamp.c
index f1468fe16d..5e5c0316b2 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmssamp.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmssamp.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmssamp.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmssamp.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmssm.c b/core/src/fxcodec/lcms2/src/fx_cmssm.c
index a53ad71f42..b8c716041e 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmssm.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmssm.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmssm.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmssm.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmstypes.c b/core/src/fxcodec/lcms2/src/fx_cmstypes.c
index 19f9e706c1..cf64a61dd9 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmstypes.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmstypes.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmstypes.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmstypes.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsvirt.c b/core/src/fxcodec/lcms2/src/fx_cmsvirt.c
index 3e5397f11b..cc60d1f9f1 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsvirt.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsvirt.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsvirt.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsvirt.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmswtpnt.c b/core/src/fxcodec/lcms2/src/fx_cmswtpnt.c
index c11afd6016..d46c60a985 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmswtpnt.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmswtpnt.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmswtpnt.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmswtpnt.c"
diff --git a/core/src/fxcodec/lcms2/src/fx_cmsxform.c b/core/src/fxcodec/lcms2/src/fx_cmsxform.c
index a4b6d85536..e8cd83af42 100644
--- a/core/src/fxcodec/lcms2/src/fx_cmsxform.c
+++ b/core/src/fxcodec/lcms2/src/fx_cmsxform.c
@@ -1,7 +1,7 @@
-// Copyright 2014 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "../lcms2-2.6/src/cmsxform.c"
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../lcms2-2.6/src/cmsxform.c"
diff --git a/core/src/fxcodec/libjpeg/cderror.h b/core/src/fxcodec/libjpeg/cderror.h
index c19d38fb4a..70435e161c 100644
--- a/core/src/fxcodec/libjpeg/cderror.h
+++ b/core/src/fxcodec/libjpeg/cderror.h
@@ -1,132 +1,132 @@
-/*
- * cderror.h
- *
- * Copyright (C) 1994-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 defines the error and message codes for the cjpeg/djpeg
- * applications. These strings are not needed as part of the JPEG library
- * proper.
- * Edit this file to add new codes, or to translate the message strings to
- * some other language.
- */
-
-/*
- * To define the enum list of message codes, include this file without
- * defining macro JMESSAGE. To create a message string table, include it
- * again with a suitable JMESSAGE definition (see jerror.c for an example).
- */
-#ifndef JMESSAGE
-#ifndef CDERROR_H
-#define CDERROR_H
-/* First time through, define the enum list */
-#define JMAKE_ENUM_LIST
-#else
-/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
-#define JMESSAGE(code,string)
-#endif /* CDERROR_H */
-#endif /* JMESSAGE */
-
-#ifdef JMAKE_ENUM_LIST
-
-typedef enum {
-
-#define JMESSAGE(code,string) code ,
-
-#endif /* JMAKE_ENUM_LIST */
-
-JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */
-
-#ifdef BMP_SUPPORTED
-JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format")
-JMESSAGE(JERR_BMP_BADDEPTH, "Only 8- and 24-bit BMP files are supported")
-JMESSAGE(JERR_BMP_BADHEADER, "Invalid BMP file: bad header length")
-JMESSAGE(JERR_BMP_BADPLANES, "Invalid BMP file: biPlanes not equal to 1")
-JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB")
-JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported")
-JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM")
-JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image")
-JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image")
-JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image")
-JMESSAGE(JTRC_BMP_OS2_MAPPED, "%ux%u 8-bit colormapped OS2 BMP image")
-#endif /* BMP_SUPPORTED */
-
-#ifdef GIF_SUPPORTED
-JMESSAGE(JERR_GIF_BUG, "GIF output got confused")
-JMESSAGE(JERR_GIF_CODESIZE, "Bogus GIF codesize %d")
-JMESSAGE(JERR_GIF_COLORSPACE, "GIF output must be grayscale or RGB")
-JMESSAGE(JERR_GIF_IMAGENOTFOUND, "Too few images in GIF file")
-JMESSAGE(JERR_GIF_NOT, "Not a GIF file")
-JMESSAGE(JTRC_GIF, "%ux%ux%d GIF image")
-JMESSAGE(JTRC_GIF_BADVERSION,
- "Warning: unexpected GIF version number '%c%c%c'")
-JMESSAGE(JTRC_GIF_EXTENSION, "Ignoring GIF extension block of type 0x%02x")
-JMESSAGE(JTRC_GIF_NONSQUARE, "Caution: nonsquare pixels in input")
-JMESSAGE(JWRN_GIF_BADDATA, "Corrupt data in GIF file")
-JMESSAGE(JWRN_GIF_CHAR, "Bogus char 0x%02x in GIF file, ignoring")
-JMESSAGE(JWRN_GIF_ENDCODE, "Premature end of GIF image")
-JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits")
-#endif /* GIF_SUPPORTED */
-
-#ifdef PPM_SUPPORTED
-JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB")
-JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file")
-JMESSAGE(JERR_PPM_NOT, "Not a PPM/PGM file")
-JMESSAGE(JTRC_PGM, "%ux%u PGM image")
-JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image")
-JMESSAGE(JTRC_PPM, "%ux%u PPM image")
-JMESSAGE(JTRC_PPM_TEXT, "%ux%u text PPM image")
-#endif /* PPM_SUPPORTED */
-
-#ifdef RLE_SUPPORTED
-JMESSAGE(JERR_RLE_BADERROR, "Bogus error code from RLE library")
-JMESSAGE(JERR_RLE_COLORSPACE, "RLE output must be grayscale or RGB")
-JMESSAGE(JERR_RLE_DIMENSIONS, "Image dimensions (%ux%u) too large for RLE")
-JMESSAGE(JERR_RLE_EMPTY, "Empty RLE file")
-JMESSAGE(JERR_RLE_EOF, "Premature EOF in RLE header")
-JMESSAGE(JERR_RLE_MEM, "Insufficient memory for RLE header")
-JMESSAGE(JERR_RLE_NOT, "Not an RLE file")
-JMESSAGE(JERR_RLE_TOOMANYCHANNELS, "Cannot handle %d output channels for RLE")
-JMESSAGE(JERR_RLE_UNSUPPORTED, "Cannot handle this RLE setup")
-JMESSAGE(JTRC_RLE, "%ux%u full-color RLE file")
-JMESSAGE(JTRC_RLE_FULLMAP, "%ux%u full-color RLE file with map of length %d")
-JMESSAGE(JTRC_RLE_GRAY, "%ux%u grayscale RLE file")
-JMESSAGE(JTRC_RLE_MAPGRAY, "%ux%u grayscale RLE file with map of length %d")
-JMESSAGE(JTRC_RLE_MAPPED, "%ux%u colormapped RLE file with map of length %d")
-#endif /* RLE_SUPPORTED */
-
-#ifdef TARGA_SUPPORTED
-JMESSAGE(JERR_TGA_BADCMAP, "Unsupported Targa colormap format")
-JMESSAGE(JERR_TGA_BADPARMS, "Invalid or unsupported Targa file")
-JMESSAGE(JERR_TGA_COLORSPACE, "Targa output must be grayscale or RGB")
-JMESSAGE(JTRC_TGA, "%ux%u RGB Targa image")
-JMESSAGE(JTRC_TGA_GRAY, "%ux%u grayscale Targa image")
-JMESSAGE(JTRC_TGA_MAPPED, "%ux%u colormapped Targa image")
-#else
-JMESSAGE(JERR_TGA_NOTCOMP, "Targa support was not compiled")
-#endif /* TARGA_SUPPORTED */
-
-JMESSAGE(JERR_BAD_CMAP_FILE,
- "Color map file is invalid or of unsupported format")
-JMESSAGE(JERR_TOO_MANY_COLORS,
- "Output file format cannot handle %d colormap entries")
-JMESSAGE(JERR_UNGETC_FAILED, "ungetc failed")
-#ifdef TARGA_SUPPORTED
-JMESSAGE(JERR_UNKNOWN_FORMAT,
- "Unrecognized input file format --- perhaps you need -targa")
-#else
-JMESSAGE(JERR_UNKNOWN_FORMAT, "Unrecognized input file format")
-#endif
-JMESSAGE(JERR_UNSUPPORTED_FORMAT, "Unsupported output file format")
-
-#ifdef JMAKE_ENUM_LIST
-
- JMSG_LASTADDONCODE
-} ADDON_MESSAGE_CODE;
-
-#undef JMAKE_ENUM_LIST
-#endif /* JMAKE_ENUM_LIST */
-
-/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
-#undef JMESSAGE
+/*
+ * cderror.h
+ *
+ * Copyright (C) 1994-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 defines the error and message codes for the cjpeg/djpeg
+ * applications. These strings are not needed as part of the JPEG library
+ * proper.
+ * Edit this file to add new codes, or to translate the message strings to
+ * some other language.
+ */
+
+/*
+ * To define the enum list of message codes, include this file without
+ * defining macro JMESSAGE. To create a message string table, include it
+ * again with a suitable JMESSAGE definition (see jerror.c for an example).
+ */
+#ifndef JMESSAGE
+#ifndef CDERROR_H
+#define CDERROR_H
+/* First time through, define the enum list */
+#define JMAKE_ENUM_LIST
+#else
+/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
+#define JMESSAGE(code,string)
+#endif /* CDERROR_H */
+#endif /* JMESSAGE */
+
+#ifdef JMAKE_ENUM_LIST
+
+typedef enum {
+
+#define JMESSAGE(code,string) code ,
+
+#endif /* JMAKE_ENUM_LIST */
+
+JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */
+
+#ifdef BMP_SUPPORTED
+JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format")
+JMESSAGE(JERR_BMP_BADDEPTH, "Only 8- and 24-bit BMP files are supported")
+JMESSAGE(JERR_BMP_BADHEADER, "Invalid BMP file: bad header length")
+JMESSAGE(JERR_BMP_BADPLANES, "Invalid BMP file: biPlanes not equal to 1")
+JMESSAGE(JERR_BMP_COLORSPACE, "BMP output must be grayscale or RGB")
+JMESSAGE(JERR_BMP_COMPRESSED, "Sorry, compressed BMPs not yet supported")
+JMESSAGE(JERR_BMP_NOT, "Not a BMP file - does not start with BM")
+JMESSAGE(JTRC_BMP, "%ux%u 24-bit BMP image")
+JMESSAGE(JTRC_BMP_MAPPED, "%ux%u 8-bit colormapped BMP image")
+JMESSAGE(JTRC_BMP_OS2, "%ux%u 24-bit OS2 BMP image")
+JMESSAGE(JTRC_BMP_OS2_MAPPED, "%ux%u 8-bit colormapped OS2 BMP image")
+#endif /* BMP_SUPPORTED */
+
+#ifdef GIF_SUPPORTED
+JMESSAGE(JERR_GIF_BUG, "GIF output got confused")
+JMESSAGE(JERR_GIF_CODESIZE, "Bogus GIF codesize %d")
+JMESSAGE(JERR_GIF_COLORSPACE, "GIF output must be grayscale or RGB")
+JMESSAGE(JERR_GIF_IMAGENOTFOUND, "Too few images in GIF file")
+JMESSAGE(JERR_GIF_NOT, "Not a GIF file")
+JMESSAGE(JTRC_GIF, "%ux%ux%d GIF image")
+JMESSAGE(JTRC_GIF_BADVERSION,
+ "Warning: unexpected GIF version number '%c%c%c'")
+JMESSAGE(JTRC_GIF_EXTENSION, "Ignoring GIF extension block of type 0x%02x")
+JMESSAGE(JTRC_GIF_NONSQUARE, "Caution: nonsquare pixels in input")
+JMESSAGE(JWRN_GIF_BADDATA, "Corrupt data in GIF file")
+JMESSAGE(JWRN_GIF_CHAR, "Bogus char 0x%02x in GIF file, ignoring")
+JMESSAGE(JWRN_GIF_ENDCODE, "Premature end of GIF image")
+JMESSAGE(JWRN_GIF_NOMOREDATA, "Ran out of GIF bits")
+#endif /* GIF_SUPPORTED */
+
+#ifdef PPM_SUPPORTED
+JMESSAGE(JERR_PPM_COLORSPACE, "PPM output must be grayscale or RGB")
+JMESSAGE(JERR_PPM_NONNUMERIC, "Nonnumeric data in PPM file")
+JMESSAGE(JERR_PPM_NOT, "Not a PPM/PGM file")
+JMESSAGE(JTRC_PGM, "%ux%u PGM image")
+JMESSAGE(JTRC_PGM_TEXT, "%ux%u text PGM image")
+JMESSAGE(JTRC_PPM, "%ux%u PPM image")
+JMESSAGE(JTRC_PPM_TEXT, "%ux%u text PPM image")
+#endif /* PPM_SUPPORTED */
+
+#ifdef RLE_SUPPORTED
+JMESSAGE(JERR_RLE_BADERROR, "Bogus error code from RLE library")
+JMESSAGE(JERR_RLE_COLORSPACE, "RLE output must be grayscale or RGB")
+JMESSAGE(JERR_RLE_DIMENSIONS, "Image dimensions (%ux%u) too large for RLE")
+JMESSAGE(JERR_RLE_EMPTY, "Empty RLE file")
+JMESSAGE(JERR_RLE_EOF, "Premature EOF in RLE header")
+JMESSAGE(JERR_RLE_MEM, "Insufficient memory for RLE header")
+JMESSAGE(JERR_RLE_NOT, "Not an RLE file")
+JMESSAGE(JERR_RLE_TOOMANYCHANNELS, "Cannot handle %d output channels for RLE")
+JMESSAGE(JERR_RLE_UNSUPPORTED, "Cannot handle this RLE setup")
+JMESSAGE(JTRC_RLE, "%ux%u full-color RLE file")
+JMESSAGE(JTRC_RLE_FULLMAP, "%ux%u full-color RLE file with map of length %d")
+JMESSAGE(JTRC_RLE_GRAY, "%ux%u grayscale RLE file")
+JMESSAGE(JTRC_RLE_MAPGRAY, "%ux%u grayscale RLE file with map of length %d")
+JMESSAGE(JTRC_RLE_MAPPED, "%ux%u colormapped RLE file with map of length %d")
+#endif /* RLE_SUPPORTED */
+
+#ifdef TARGA_SUPPORTED
+JMESSAGE(JERR_TGA_BADCMAP, "Unsupported Targa colormap format")
+JMESSAGE(JERR_TGA_BADPARMS, "Invalid or unsupported Targa file")
+JMESSAGE(JERR_TGA_COLORSPACE, "Targa output must be grayscale or RGB")
+JMESSAGE(JTRC_TGA, "%ux%u RGB Targa image")
+JMESSAGE(JTRC_TGA_GRAY, "%ux%u grayscale Targa image")
+JMESSAGE(JTRC_TGA_MAPPED, "%ux%u colormapped Targa image")
+#else
+JMESSAGE(JERR_TGA_NOTCOMP, "Targa support was not compiled")
+#endif /* TARGA_SUPPORTED */
+
+JMESSAGE(JERR_BAD_CMAP_FILE,
+ "Color map file is invalid or of unsupported format")
+JMESSAGE(JERR_TOO_MANY_COLORS,
+ "Output file format cannot handle %d colormap entries")
+JMESSAGE(JERR_UNGETC_FAILED, "ungetc failed")
+#ifdef TARGA_SUPPORTED
+JMESSAGE(JERR_UNKNOWN_FORMAT,
+ "Unrecognized input file format --- perhaps you need -targa")
+#else
+JMESSAGE(JERR_UNKNOWN_FORMAT, "Unrecognized input file format")
+#endif
+JMESSAGE(JERR_UNSUPPORTED_FORMAT, "Unsupported output file format")
+
+#ifdef JMAKE_ENUM_LIST
+
+ JMSG_LASTADDONCODE
+} ADDON_MESSAGE_CODE;
+
+#undef JMAKE_ENUM_LIST
+#endif /* JMAKE_ENUM_LIST */
+
+/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
+#undef JMESSAGE
diff --git a/core/src/fxcodec/libjpeg/cdjpeg.h b/core/src/fxcodec/libjpeg/cdjpeg.h
index be12278810..3d728ee9c7 100644
--- a/core/src/fxcodec/libjpeg/cdjpeg.h
+++ b/core/src/fxcodec/libjpeg/cdjpeg.h
@@ -1,184 +1,184 @@
-/*
- * cdjpeg.h
- *
- * Copyright (C) 1994-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 common declarations for the sample applications
- * cjpeg and djpeg. It is NOT used by the core JPEG library.
- */
-
-#define JPEG_CJPEG_DJPEG /* define proper options in jconfig.h */
-#define JPEG_INTERNAL_OPTIONS /* cjpeg.c,djpeg.c need to see xxx_SUPPORTED */
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jerror.h" /* get library error codes too */
-#include "cderror.h" /* get application-specific error codes */
-
-
-/*
- * Object interface for cjpeg's source file decoding modules
- */
-
-typedef struct cjpeg_source_struct * cjpeg_source_ptr;
-
-struct cjpeg_source_struct {
- JMETHOD(void, start_input, (j_compress_ptr cinfo,
- cjpeg_source_ptr sinfo));
- JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
- cjpeg_source_ptr sinfo));
- JMETHOD(void, finish_input, (j_compress_ptr cinfo,
- cjpeg_source_ptr sinfo));
-
- FXSYS_FILE *input_file;
-
- JSAMPARRAY buffer;
- JDIMENSION buffer_height;
-};
-
-
-/*
- * Object interface for djpeg's output file encoding modules
- */
-
-typedef struct djpeg_dest_struct * djpeg_dest_ptr;
-
-struct djpeg_dest_struct {
- /* start_output is called after jpeg_start_decompress finishes.
- * The color map will be ready at this time, if one is needed.
- */
- JMETHOD(void, start_output, (j_decompress_ptr cinfo,
- djpeg_dest_ptr dinfo));
- /* Emit the specified number of pixel rows from the buffer. */
- JMETHOD(void, put_pixel_rows, (j_decompress_ptr cinfo,
- djpeg_dest_ptr dinfo,
- JDIMENSION rows_supplied));
- /* Finish up at the end of the image. */
- JMETHOD(void, finish_output, (j_decompress_ptr cinfo,
- djpeg_dest_ptr dinfo));
-
- /* Target file spec; filled in by djpeg.c after object is created. */
- FXSYS_FILE * output_file;
-
- /* Output pixel-row buffer. Created by module init or start_output.
- * Width is cinfo->output_width * cinfo->output_components;
- * height is buffer_height.
- */
- JSAMPARRAY buffer;
- JDIMENSION buffer_height;
-};
-
-
-/*
- * cjpeg/djpeg may need to perform extra passes to convert to or from
- * the source/destination file format. The JPEG library does not know
- * about these passes, but we'd like them to be counted by the progress
- * monitor. We use an expanded progress monitor object to hold the
- * additional pass count.
- */
-
-struct cdjpeg_progress_mgr {
- struct jpeg_progress_mgr pub; /* fields known to JPEG library */
- int completed_extra_passes; /* extra passes completed */
- int total_extra_passes; /* total extra */
- /* last printed percentage stored here to avoid multiple printouts */
- int percent_done;
-};
-
-typedef struct cdjpeg_progress_mgr * cd_progress_ptr;
-
-
-/* Short forms of external names for systems with brain-damaged linkers. */
-
-#ifdef NEED_SHORT_EXTERNAL_NAMES
-#define jinit_read_bmp jIRdBMP
-#define jinit_write_bmp jIWrBMP
-#define jinit_read_gif jIRdGIF
-#define jinit_write_gif jIWrGIF
-#define jinit_read_ppm jIRdPPM
-#define jinit_write_ppm jIWrPPM
-#define jinit_read_rle jIRdRLE
-#define jinit_write_rle jIWrRLE
-#define jinit_read_targa jIRdTarga
-#define jinit_write_targa jIWrTarga
-#define read_quant_tables RdQTables
-#define read_scan_script RdScnScript
-#define set_quant_slots SetQSlots
-#define set_sample_factors SetSFacts
-#define read_color_map RdCMap
-#define enable_signal_catcher EnSigCatcher
-#define start_progress_monitor StProgMon
-#define end_progress_monitor EnProgMon
-#define read_stdin RdStdin
-#define write_stdout WrStdout
-#endif /* NEED_SHORT_EXTERNAL_NAMES */
-
-/* Module selection routines for I/O modules. */
-
-EXTERN(cjpeg_source_ptr) jinit_read_bmp JPP((j_compress_ptr cinfo));
-EXTERN(djpeg_dest_ptr) jinit_write_bmp JPP((j_decompress_ptr cinfo,
- boolean is_os2));
-EXTERN(cjpeg_source_ptr) jinit_read_gif JPP((j_compress_ptr cinfo));
-EXTERN(djpeg_dest_ptr) jinit_write_gif JPP((j_decompress_ptr cinfo));
-EXTERN(cjpeg_source_ptr) jinit_read_ppm JPP((j_compress_ptr cinfo));
-EXTERN(djpeg_dest_ptr) jinit_write_ppm JPP((j_decompress_ptr cinfo));
-EXTERN(cjpeg_source_ptr) jinit_read_rle JPP((j_compress_ptr cinfo));
-EXTERN(djpeg_dest_ptr) jinit_write_rle JPP((j_decompress_ptr cinfo));
-EXTERN(cjpeg_source_ptr) jinit_read_targa JPP((j_compress_ptr cinfo));
-EXTERN(djpeg_dest_ptr) jinit_write_targa JPP((j_decompress_ptr cinfo));
-
-/* cjpeg support routines (in rdswitch.c) */
-
-EXTERN(boolean) read_quant_tables JPP((j_compress_ptr cinfo, char * filename,
- int scale_factor, boolean force_baseline));
-EXTERN(boolean) read_scan_script JPP((j_compress_ptr cinfo, char * filename));
-EXTERN(boolean) set_quant_slots JPP((j_compress_ptr cinfo, char *arg));
-EXTERN(boolean) set_sample_factors JPP((j_compress_ptr cinfo, char *arg));
-
-/* djpeg support routines (in rdcolmap.c) */
-
-EXTERN(void) read_color_map JPP((j_decompress_ptr cinfo, FXSYS_FILE * infile));
-
-/* common support routines (in cdjpeg.c) */
-
-EXTERN(void) enable_signal_catcher JPP((j_common_ptr cinfo));
-EXTERN(void) start_progress_monitor JPP((j_common_ptr cinfo,
- cd_progress_ptr progress));
-EXTERN(void) end_progress_monitor JPP((j_common_ptr cinfo));
-EXTERN(boolean) keymatch JPP((char * arg, const char * keyword, int minchars));
-EXTERN(FXSYS_FILE *) read_stdin JPP((void));
-EXTERN(FXSYS_FILE *) write_stdout JPP((void));
-
-/* miscellaneous useful macros */
-
-#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
-#define READ_BINARY "r"
-#define WRITE_BINARY "w"
-#else
-#ifdef VMS /* VMS is very nonstandard */
-#define READ_BINARY "rb", "ctx=stm"
-#define WRITE_BINARY "wb", "ctx=stm"
-#else /* standard ANSI-compliant case */
-#define READ_BINARY "rb"
-#define WRITE_BINARY "wb"
-#endif
-#endif
-
-#ifndef EXIT_FAILURE /* define exit() codes if not provided */
-#define EXIT_FAILURE 1
-#endif
-#ifndef EXIT_SUCCESS
-#ifdef VMS
-#define EXIT_SUCCESS 1 /* VMS is very nonstandard */
-#else
-#define EXIT_SUCCESS 0
-#endif
-#endif
-#ifndef EXIT_WARNING
-#ifdef VMS
-#define EXIT_WARNING 1 /* VMS is very nonstandard */
-#else
-#define EXIT_WARNING 2
-#endif
-#endif
+/*
+ * cdjpeg.h
+ *
+ * Copyright (C) 1994-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 common declarations for the sample applications
+ * cjpeg and djpeg. It is NOT used by the core JPEG library.
+ */
+
+#define JPEG_CJPEG_DJPEG /* define proper options in jconfig.h */
+#define JPEG_INTERNAL_OPTIONS /* cjpeg.c,djpeg.c need to see xxx_SUPPORTED */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jerror.h" /* get library error codes too */
+#include "cderror.h" /* get application-specific error codes */
+
+
+/*
+ * Object interface for cjpeg's source file decoding modules
+ */
+
+typedef struct cjpeg_source_struct * cjpeg_source_ptr;
+
+struct cjpeg_source_struct {
+ JMETHOD(void, start_input, (j_compress_ptr cinfo,
+ cjpeg_source_ptr sinfo));
+ JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
+ cjpeg_source_ptr sinfo));
+ JMETHOD(void, finish_input, (j_compress_ptr cinfo,
+ cjpeg_source_ptr sinfo));
+
+ FXSYS_FILE *input_file;
+
+ JSAMPARRAY buffer;
+ JDIMENSION buffer_height;
+};
+
+
+/*
+ * Object interface for djpeg's output file encoding modules
+ */
+
+typedef struct djpeg_dest_struct * djpeg_dest_ptr;
+
+struct djpeg_dest_struct {
+ /* start_output is called after jpeg_start_decompress finishes.
+ * The color map will be ready at this time, if one is needed.
+ */
+ JMETHOD(void, start_output, (j_decompress_ptr cinfo,
+ djpeg_dest_ptr dinfo));
+ /* Emit the specified number of pixel rows from the buffer. */
+ JMETHOD(void, put_pixel_rows, (j_decompress_ptr cinfo,
+ djpeg_dest_ptr dinfo,
+ JDIMENSION rows_supplied));
+ /* Finish up at the end of the image. */
+ JMETHOD(void, finish_output, (j_decompress_ptr cinfo,
+ djpeg_dest_ptr dinfo));
+
+ /* Target file spec; filled in by djpeg.c after object is created. */
+ FXSYS_FILE * output_file;
+
+ /* Output pixel-row buffer. Created by module init or start_output.
+ * Width is cinfo->output_width * cinfo->output_components;
+ * height is buffer_height.
+ */
+ JSAMPARRAY buffer;
+ JDIMENSION buffer_height;
+};
+
+
+/*
+ * cjpeg/djpeg may need to perform extra passes to convert to or from
+ * the source/destination file format. The JPEG library does not know
+ * about these passes, but we'd like them to be counted by the progress
+ * monitor. We use an expanded progress monitor object to hold the
+ * additional pass count.
+ */
+
+struct cdjpeg_progress_mgr {
+ struct jpeg_progress_mgr pub; /* fields known to JPEG library */
+ int completed_extra_passes; /* extra passes completed */
+ int total_extra_passes; /* total extra */
+ /* last printed percentage stored here to avoid multiple printouts */
+ int percent_done;
+};
+
+typedef struct cdjpeg_progress_mgr * cd_progress_ptr;
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jinit_read_bmp jIRdBMP
+#define jinit_write_bmp jIWrBMP
+#define jinit_read_gif jIRdGIF
+#define jinit_write_gif jIWrGIF
+#define jinit_read_ppm jIRdPPM
+#define jinit_write_ppm jIWrPPM
+#define jinit_read_rle jIRdRLE
+#define jinit_write_rle jIWrRLE
+#define jinit_read_targa jIRdTarga
+#define jinit_write_targa jIWrTarga
+#define read_quant_tables RdQTables
+#define read_scan_script RdScnScript
+#define set_quant_slots SetQSlots
+#define set_sample_factors SetSFacts
+#define read_color_map RdCMap
+#define enable_signal_catcher EnSigCatcher
+#define start_progress_monitor StProgMon
+#define end_progress_monitor EnProgMon
+#define read_stdin RdStdin
+#define write_stdout WrStdout
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+/* Module selection routines for I/O modules. */
+
+EXTERN(cjpeg_source_ptr) jinit_read_bmp JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_bmp JPP((j_decompress_ptr cinfo,
+ boolean is_os2));
+EXTERN(cjpeg_source_ptr) jinit_read_gif JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_gif JPP((j_decompress_ptr cinfo));
+EXTERN(cjpeg_source_ptr) jinit_read_ppm JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_ppm JPP((j_decompress_ptr cinfo));
+EXTERN(cjpeg_source_ptr) jinit_read_rle JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_rle JPP((j_decompress_ptr cinfo));
+EXTERN(cjpeg_source_ptr) jinit_read_targa JPP((j_compress_ptr cinfo));
+EXTERN(djpeg_dest_ptr) jinit_write_targa JPP((j_decompress_ptr cinfo));
+
+/* cjpeg support routines (in rdswitch.c) */
+
+EXTERN(boolean) read_quant_tables JPP((j_compress_ptr cinfo, char * filename,
+ int scale_factor, boolean force_baseline));
+EXTERN(boolean) read_scan_script JPP((j_compress_ptr cinfo, char * filename));
+EXTERN(boolean) set_quant_slots JPP((j_compress_ptr cinfo, char *arg));
+EXTERN(boolean) set_sample_factors JPP((j_compress_ptr cinfo, char *arg));
+
+/* djpeg support routines (in rdcolmap.c) */
+
+EXTERN(void) read_color_map JPP((j_decompress_ptr cinfo, FXSYS_FILE * infile));
+
+/* common support routines (in cdjpeg.c) */
+
+EXTERN(void) enable_signal_catcher JPP((j_common_ptr cinfo));
+EXTERN(void) start_progress_monitor JPP((j_common_ptr cinfo,
+ cd_progress_ptr progress));
+EXTERN(void) end_progress_monitor JPP((j_common_ptr cinfo));
+EXTERN(boolean) keymatch JPP((char * arg, const char * keyword, int minchars));
+EXTERN(FXSYS_FILE *) read_stdin JPP((void));
+EXTERN(FXSYS_FILE *) write_stdout JPP((void));
+
+/* miscellaneous useful macros */
+
+#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
+#define READ_BINARY "r"
+#define WRITE_BINARY "w"
+#else
+#ifdef VMS /* VMS is very nonstandard */
+#define READ_BINARY "rb", "ctx=stm"
+#define WRITE_BINARY "wb", "ctx=stm"
+#else /* standard ANSI-compliant case */
+#define READ_BINARY "rb"
+#define WRITE_BINARY "wb"
+#endif
+#endif
+
+#ifndef EXIT_FAILURE /* define exit() codes if not provided */
+#define EXIT_FAILURE 1
+#endif
+#ifndef EXIT_SUCCESS
+#ifdef VMS
+#define EXIT_SUCCESS 1 /* VMS is very nonstandard */
+#else
+#define EXIT_SUCCESS 0
+#endif
+#endif
+#ifndef EXIT_WARNING
+#ifdef VMS
+#define EXIT_WARNING 1 /* VMS is very nonstandard */
+#else
+#define EXIT_WARNING 2
+#endif
+#endif
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c b/core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c
index ec88cff26d..ec04fd2f15 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcapimin.c
@@ -1,283 +1,283 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcapimin.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 application interface code for the compression half
- * of the JPEG library. These are the "minimum" API routines that may be
- * needed in either the normal full-compression case or the transcoding-only
- * case.
- *
- * Most of the routines intended to be called directly by an application
- * are in this file or in jcapistd.c. But also see jcparam.c for
- * parameter-setup helper routines, jcomapi.c for routines shared by
- * compression and decompression, and jctrans.c for the transcoding case.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/*
- * Initialization of a JPEG compression object.
- * The error manager must already be set up (in case memory manager fails).
- */
-
-GLOBAL(void)
-jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
-{
- int i;
-
- /* Guard against version mismatches between library and caller. */
- cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
- if (version != JPEG_LIB_VERSION)
- ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
- if (structsize != SIZEOF(struct jpeg_compress_struct))
- ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
- (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
-
- /* For debugging purposes, we zero the whole master structure.
- * But the application has already set the err pointer, and may have set
- * client_data, so we have to save and restore those fields.
- * Note: if application hasn't set client_data, tools like Purify may
- * complain here.
- */
- {
- struct jpeg_error_mgr * err = cinfo->err;
- void * client_data = cinfo->client_data; /* ignore Purify complaint here */
- MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
- cinfo->err = err;
- cinfo->client_data = client_data;
- }
- cinfo->is_decompressor = FALSE;
-
- /* Initialize a memory manager instance for this object */
- jinit_memory_mgr((j_common_ptr) cinfo);
-
- /* Zero out pointers to permanent structures. */
- cinfo->progress = NULL;
- cinfo->dest = NULL;
-
- cinfo->comp_info = NULL;
-
- for (i = 0; i < NUM_QUANT_TBLS; i++)
- cinfo->quant_tbl_ptrs[i] = NULL;
-
- for (i = 0; i < NUM_HUFF_TBLS; i++) {
- cinfo->dc_huff_tbl_ptrs[i] = NULL;
- cinfo->ac_huff_tbl_ptrs[i] = NULL;
- }
-
- cinfo->script_space = NULL;
-
- cinfo->input_gamma = 1.0; /* in case application forgets */
-
- /* OK, I'm ready */
- cinfo->global_state = CSTATE_START;
-}
-
-
-/*
- * Destruction of a JPEG compression object
- */
-
-GLOBAL(void)
-jpeg_destroy_compress (j_compress_ptr cinfo)
-{
- jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
-}
-
-
-/*
- * Abort processing of a JPEG compression operation,
- * but don't destroy the object itself.
- */
-
-GLOBAL(void)
-jpeg_abort_compress (j_compress_ptr cinfo)
-{
- jpeg_abort((j_common_ptr) cinfo); /* use common routine */
-}
-
-
-/*
- * Forcibly suppress or un-suppress all quantization and Huffman tables.
- * Marks all currently defined tables as already written (if suppress)
- * or not written (if !suppress). This will control whether they get emitted
- * by a subsequent jpeg_start_compress call.
- *
- * This routine is exported for use by applications that want to produce
- * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
- * since it is called by jpeg_start_compress, we put it here --- otherwise
- * jcparam.o would be linked whether the application used it or not.
- */
-
-GLOBAL(void)
-jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
-{
- int i;
- JQUANT_TBL * qtbl;
- JHUFF_TBL * htbl;
-
- for (i = 0; i < NUM_QUANT_TBLS; i++) {
- if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
- qtbl->sent_table = suppress;
- }
-
- for (i = 0; i < NUM_HUFF_TBLS; i++) {
- if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
- htbl->sent_table = suppress;
- if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
- htbl->sent_table = suppress;
- }
-}
-
-
-/*
- * Finish JPEG compression.
- *
- * If a multipass operating mode was selected, this may do a great deal of
- * work including most of the actual output.
- */
-
-GLOBAL(void)
-jpeg_finish_compress (j_compress_ptr cinfo)
-{
- JDIMENSION iMCU_row;
-
- if (cinfo->global_state == CSTATE_SCANNING ||
- cinfo->global_state == CSTATE_RAW_OK) {
- /* Terminate first pass */
- if (cinfo->next_scanline < cinfo->image_height)
- ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
- (*cinfo->master->finish_pass) (cinfo);
- } else if (cinfo->global_state != CSTATE_WRCOEFS)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- /* Perform any remaining passes */
- while (! cinfo->master->is_last_pass) {
- (*cinfo->master->prepare_for_pass) (cinfo);
- for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
- if (cinfo->progress != NULL) {
- cinfo->progress->pass_counter = (long) iMCU_row;
- cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
- (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
- }
- /* We bypass the main controller and invoke coef controller directly;
- * all work is being done from the coefficient buffer.
- */
- if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
- ERREXIT(cinfo, JERR_CANT_SUSPEND);
- }
- (*cinfo->master->finish_pass) (cinfo);
- }
- /* Write EOI, do final cleanup */
- (*cinfo->marker->write_file_trailer) (cinfo);
- (*cinfo->dest->term_destination) (cinfo);
- /* We can use jpeg_abort to release memory and reset global_state */
- jpeg_abort((j_common_ptr) cinfo);
-}
-
-
-/*
- * Write a special marker.
- * This is only recommended for writing COM or APPn markers.
- * Must be called after jpeg_start_compress() and before
- * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
- */
-
-GLOBAL(void)
-jpeg_write_marker (j_compress_ptr cinfo, int marker,
- const JOCTET *dataptr, unsigned int datalen)
-{
- JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
-
- if (cinfo->next_scanline != 0 ||
- (cinfo->global_state != CSTATE_SCANNING &&
- cinfo->global_state != CSTATE_RAW_OK &&
- cinfo->global_state != CSTATE_WRCOEFS))
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
- write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
- while (datalen--) {
- (*write_marker_byte) (cinfo, *dataptr);
- dataptr++;
- }
-}
-
-/* Same, but piecemeal. */
-
-GLOBAL(void)
-jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
-{
- if (cinfo->next_scanline != 0 ||
- (cinfo->global_state != CSTATE_SCANNING &&
- cinfo->global_state != CSTATE_RAW_OK &&
- cinfo->global_state != CSTATE_WRCOEFS))
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
-}
-
-GLOBAL(void)
-jpeg_write_m_byte (j_compress_ptr cinfo, int val)
-{
- (*cinfo->marker->write_marker_byte) (cinfo, val);
-}
-
-
-/*
- * Alternate compression function: just write an abbreviated table file.
- * Before calling this, all parameters and a data destination must be set up.
- *
- * To produce a pair of files containing abbreviated tables and abbreviated
- * image data, one would proceed as follows:
- *
- * initialize JPEG object
- * set JPEG parameters
- * set destination to table file
- * jpeg_write_tables(cinfo);
- * set destination to image file
- * jpeg_start_compress(cinfo, FALSE);
- * write data...
- * jpeg_finish_compress(cinfo);
- *
- * jpeg_write_tables has the side effect of marking all tables written
- * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
- * will not re-emit the tables unless it is passed write_all_tables=TRUE.
- */
-
-GLOBAL(void)
-jpeg_write_tables (j_compress_ptr cinfo)
-{
- if (cinfo->global_state != CSTATE_START)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- /* (Re)initialize error mgr and destination modules */
- (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
- (*cinfo->dest->init_destination) (cinfo);
- /* Initialize the marker writer ... bit of a crock to do it here. */
- jinit_marker_writer(cinfo);
- /* Write them tables! */
- (*cinfo->marker->write_tables_only) (cinfo);
- /* And clean up. */
- (*cinfo->dest->term_destination) (cinfo);
- /*
- * In library releases up through v6a, we called jpeg_abort() here to free
- * any working memory allocated by the destination manager and marker
- * writer. Some applications had a problem with that: they allocated space
- * of their own from the library memory manager, and didn't want it to go
- * away during write_tables. So now we do nothing. This will cause a
- * memory leak if an app calls write_tables repeatedly without doing a full
- * compression cycle or otherwise resetting the JPEG object. However, that
- * seems less bad than unexpectedly freeing memory in the normal case.
- * An app that prefers the old behavior can call jpeg_abort for itself after
- * each call to jpeg_write_tables().
- */
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcapimin.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 application interface code for the compression half
+ * of the JPEG library. These are the "minimum" API routines that may be
+ * needed in either the normal full-compression case or the transcoding-only
+ * case.
+ *
+ * Most of the routines intended to be called directly by an application
+ * are in this file or in jcapistd.c. But also see jcparam.c for
+ * parameter-setup helper routines, jcomapi.c for routines shared by
+ * compression and decompression, and jctrans.c for the transcoding case.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Initialization of a JPEG compression object.
+ * The error manager must already be set up (in case memory manager fails).
+ */
+
+GLOBAL(void)
+jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
+{
+ int i;
+
+ /* Guard against version mismatches between library and caller. */
+ cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
+ if (version != JPEG_LIB_VERSION)
+ ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
+ if (structsize != SIZEOF(struct jpeg_compress_struct))
+ ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
+ (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
+
+ /* For debugging purposes, we zero the whole master structure.
+ * But the application has already set the err pointer, and may have set
+ * client_data, so we have to save and restore those fields.
+ * Note: if application hasn't set client_data, tools like Purify may
+ * complain here.
+ */
+ {
+ struct jpeg_error_mgr * err = cinfo->err;
+ void * client_data = cinfo->client_data; /* ignore Purify complaint here */
+ MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
+ cinfo->err = err;
+ cinfo->client_data = client_data;
+ }
+ cinfo->is_decompressor = FALSE;
+
+ /* Initialize a memory manager instance for this object */
+ jinit_memory_mgr((j_common_ptr) cinfo);
+
+ /* Zero out pointers to permanent structures. */
+ cinfo->progress = NULL;
+ cinfo->dest = NULL;
+
+ cinfo->comp_info = NULL;
+
+ for (i = 0; i < NUM_QUANT_TBLS; i++)
+ cinfo->quant_tbl_ptrs[i] = NULL;
+
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ cinfo->dc_huff_tbl_ptrs[i] = NULL;
+ cinfo->ac_huff_tbl_ptrs[i] = NULL;
+ }
+
+ cinfo->script_space = NULL;
+
+ cinfo->input_gamma = 1.0; /* in case application forgets */
+
+ /* OK, I'm ready */
+ cinfo->global_state = CSTATE_START;
+}
+
+
+/*
+ * Destruction of a JPEG compression object
+ */
+
+GLOBAL(void)
+jpeg_destroy_compress (j_compress_ptr cinfo)
+{
+ jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Abort processing of a JPEG compression operation,
+ * but don't destroy the object itself.
+ */
+
+GLOBAL(void)
+jpeg_abort_compress (j_compress_ptr cinfo)
+{
+ jpeg_abort((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Forcibly suppress or un-suppress all quantization and Huffman tables.
+ * Marks all currently defined tables as already written (if suppress)
+ * or not written (if !suppress). This will control whether they get emitted
+ * by a subsequent jpeg_start_compress call.
+ *
+ * This routine is exported for use by applications that want to produce
+ * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
+ * since it is called by jpeg_start_compress, we put it here --- otherwise
+ * jcparam.o would be linked whether the application used it or not.
+ */
+
+GLOBAL(void)
+jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
+{
+ int i;
+ JQUANT_TBL * qtbl;
+ JHUFF_TBL * htbl;
+
+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
+ if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
+ qtbl->sent_table = suppress;
+ }
+
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
+ htbl->sent_table = suppress;
+ if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
+ htbl->sent_table = suppress;
+ }
+}
+
+
+/*
+ * Finish JPEG compression.
+ *
+ * If a multipass operating mode was selected, this may do a great deal of
+ * work including most of the actual output.
+ */
+
+GLOBAL(void)
+jpeg_finish_compress (j_compress_ptr cinfo)
+{
+ JDIMENSION iMCU_row;
+
+ if (cinfo->global_state == CSTATE_SCANNING ||
+ cinfo->global_state == CSTATE_RAW_OK) {
+ /* Terminate first pass */
+ if (cinfo->next_scanline < cinfo->image_height)
+ ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
+ (*cinfo->master->finish_pass) (cinfo);
+ } else if (cinfo->global_state != CSTATE_WRCOEFS)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ /* Perform any remaining passes */
+ while (! cinfo->master->is_last_pass) {
+ (*cinfo->master->prepare_for_pass) (cinfo);
+ for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) iMCU_row;
+ cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+ /* We bypass the main controller and invoke coef controller directly;
+ * all work is being done from the coefficient buffer.
+ */
+ if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
+ }
+ (*cinfo->master->finish_pass) (cinfo);
+ }
+ /* Write EOI, do final cleanup */
+ (*cinfo->marker->write_file_trailer) (cinfo);
+ (*cinfo->dest->term_destination) (cinfo);
+ /* We can use jpeg_abort to release memory and reset global_state */
+ jpeg_abort((j_common_ptr) cinfo);
+}
+
+
+/*
+ * Write a special marker.
+ * This is only recommended for writing COM or APPn markers.
+ * Must be called after jpeg_start_compress() and before
+ * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
+ */
+
+GLOBAL(void)
+jpeg_write_marker (j_compress_ptr cinfo, int marker,
+ const JOCTET *dataptr, unsigned int datalen)
+{
+ JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
+
+ if (cinfo->next_scanline != 0 ||
+ (cinfo->global_state != CSTATE_SCANNING &&
+ cinfo->global_state != CSTATE_RAW_OK &&
+ cinfo->global_state != CSTATE_WRCOEFS))
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
+ write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
+ while (datalen--) {
+ (*write_marker_byte) (cinfo, *dataptr);
+ dataptr++;
+ }
+}
+
+/* Same, but piecemeal. */
+
+GLOBAL(void)
+jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
+{
+ if (cinfo->next_scanline != 0 ||
+ (cinfo->global_state != CSTATE_SCANNING &&
+ cinfo->global_state != CSTATE_RAW_OK &&
+ cinfo->global_state != CSTATE_WRCOEFS))
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
+}
+
+GLOBAL(void)
+jpeg_write_m_byte (j_compress_ptr cinfo, int val)
+{
+ (*cinfo->marker->write_marker_byte) (cinfo, val);
+}
+
+
+/*
+ * Alternate compression function: just write an abbreviated table file.
+ * Before calling this, all parameters and a data destination must be set up.
+ *
+ * To produce a pair of files containing abbreviated tables and abbreviated
+ * image data, one would proceed as follows:
+ *
+ * initialize JPEG object
+ * set JPEG parameters
+ * set destination to table file
+ * jpeg_write_tables(cinfo);
+ * set destination to image file
+ * jpeg_start_compress(cinfo, FALSE);
+ * write data...
+ * jpeg_finish_compress(cinfo);
+ *
+ * jpeg_write_tables has the side effect of marking all tables written
+ * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
+ * will not re-emit the tables unless it is passed write_all_tables=TRUE.
+ */
+
+GLOBAL(void)
+jpeg_write_tables (j_compress_ptr cinfo)
+{
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ /* (Re)initialize error mgr and destination modules */
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+ (*cinfo->dest->init_destination) (cinfo);
+ /* Initialize the marker writer ... bit of a crock to do it here. */
+ jinit_marker_writer(cinfo);
+ /* Write them tables! */
+ (*cinfo->marker->write_tables_only) (cinfo);
+ /* And clean up. */
+ (*cinfo->dest->term_destination) (cinfo);
+ /*
+ * In library releases up through v6a, we called jpeg_abort() here to free
+ * any working memory allocated by the destination manager and marker
+ * writer. Some applications had a problem with that: they allocated space
+ * of their own from the library memory manager, and didn't want it to go
+ * away during write_tables. So now we do nothing. This will cause a
+ * memory leak if an app calls write_tables repeatedly without doing a full
+ * compression cycle or otherwise resetting the JPEG object. However, that
+ * seems less bad than unexpectedly freeing memory in the normal case.
+ * An app that prefers the old behavior can call jpeg_abort for itself after
+ * each call to jpeg_write_tables().
+ */
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c b/core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c
index bf13542539..9d765e6f01 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcapistd.c
@@ -1,164 +1,164 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcapistd.c
- *
- * Copyright (C) 1994-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 contains application interface code for the compression half
- * of the JPEG library. These are the "standard" API routines that are
- * used in the normal full-compression case. They are not used by a
- * transcoding-only application. Note that if an application links in
- * jpeg_start_compress, it will end up linking in the entire compressor.
- * We thus must separate this file from jcapimin.c to avoid linking the
- * whole compression library into a transcoder.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/*
- * Compression initialization.
- * Before calling this, all parameters and a data destination must be set up.
- *
- * We require a write_all_tables parameter as a failsafe check when writing
- * multiple datastreams from the same compression object. Since prior runs
- * will have left all the tables marked sent_table=TRUE, a subsequent run
- * would emit an abbreviated stream (no tables) by default. This may be what
- * is wanted, but for safety's sake it should not be the default behavior:
- * programmers should have to make a deliberate choice to emit abbreviated
- * images. Therefore the documentation and examples should encourage people
- * to pass write_all_tables=TRUE; then it will take active thought to do the
- * wrong thing.
- */
-
-GLOBAL(void)
-jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
-{
- if (cinfo->global_state != CSTATE_START)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- if (write_all_tables)
- jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
-
- /* (Re)initialize error mgr and destination modules */
- (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
- (*cinfo->dest->init_destination) (cinfo);
- /* Perform master selection of active modules */
- jinit_compress_master(cinfo);
- /* Set up for the first pass */
- (*cinfo->master->prepare_for_pass) (cinfo);
- /* Ready for application to drive first pass through jpeg_write_scanlines
- * or jpeg_write_raw_data.
- */
- cinfo->next_scanline = 0;
- cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
-}
-
-
-/*
- * Write some scanlines of data to the JPEG compressor.
- *
- * The return value will be the number of lines actually written.
- * This should be less than the supplied num_lines only in case that
- * the data destination module has requested suspension of the compressor,
- * or if more than image_height scanlines are passed in.
- *
- * Note: we warn about excess calls to jpeg_write_scanlines() since
- * this likely signals an application programmer error. However,
- * excess scanlines passed in the last valid call are *silently* ignored,
- * so that the application need not adjust num_lines for end-of-image
- * when using a multiple-scanline buffer.
- */
-
-GLOBAL(JDIMENSION)
-jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
- JDIMENSION num_lines)
-{
- JDIMENSION row_ctr, rows_left;
-
- if (cinfo->global_state != CSTATE_SCANNING)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- if (cinfo->next_scanline >= cinfo->image_height)
- WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
-
- /* Call progress monitor hook if present */
- if (cinfo->progress != NULL) {
- cinfo->progress->pass_counter = (long) cinfo->next_scanline;
- cinfo->progress->pass_limit = (long) cinfo->image_height;
- (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
- }
-
- /* Give master control module another chance if this is first call to
- * jpeg_write_scanlines. This lets output of the frame/scan headers be
- * delayed so that application can write COM, etc, markers between
- * jpeg_start_compress and jpeg_write_scanlines.
- */
- if (cinfo->master->call_pass_startup)
- (*cinfo->master->pass_startup) (cinfo);
-
- /* Ignore any extra scanlines at bottom of image. */
- rows_left = cinfo->image_height - cinfo->next_scanline;
- if (num_lines > rows_left)
- num_lines = rows_left;
-
- row_ctr = 0;
- (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
- cinfo->next_scanline += row_ctr;
- return row_ctr;
-}
-
-
-/*
- * Alternate entry point to write raw data.
- * Processes exactly one iMCU row per call, unless suspended.
- */
-
-GLOBAL(JDIMENSION)
-jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
- JDIMENSION num_lines)
-{
- JDIMENSION lines_per_iMCU_row;
-
- if (cinfo->global_state != CSTATE_RAW_OK)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- if (cinfo->next_scanline >= cinfo->image_height) {
- WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
- return 0;
- }
-
- /* Call progress monitor hook if present */
- if (cinfo->progress != NULL) {
- cinfo->progress->pass_counter = (long) cinfo->next_scanline;
- cinfo->progress->pass_limit = (long) cinfo->image_height;
- (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
- }
-
- /* Give master control module another chance if this is first call to
- * jpeg_write_raw_data. This lets output of the frame/scan headers be
- * delayed so that application can write COM, etc, markers between
- * jpeg_start_compress and jpeg_write_raw_data.
- */
- if (cinfo->master->call_pass_startup)
- (*cinfo->master->pass_startup) (cinfo);
-
- /* Verify that at least one iMCU row has been passed. */
- lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
- if (num_lines < lines_per_iMCU_row)
- ERREXIT(cinfo, JERR_BUFFER_SIZE);
-
- /* Directly compress the row. */
- if (! (*cinfo->coef->compress_data) (cinfo, data)) {
- /* If compressor did not consume the whole row, suspend processing. */
- return 0;
- }
-
- /* OK, we processed one iMCU row. */
- cinfo->next_scanline += lines_per_iMCU_row;
- return lines_per_iMCU_row;
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcapistd.c
+ *
+ * Copyright (C) 1994-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 contains application interface code for the compression half
+ * of the JPEG library. These are the "standard" API routines that are
+ * used in the normal full-compression case. They are not used by a
+ * transcoding-only application. Note that if an application links in
+ * jpeg_start_compress, it will end up linking in the entire compressor.
+ * We thus must separate this file from jcapimin.c to avoid linking the
+ * whole compression library into a transcoder.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Compression initialization.
+ * Before calling this, all parameters and a data destination must be set up.
+ *
+ * We require a write_all_tables parameter as a failsafe check when writing
+ * multiple datastreams from the same compression object. Since prior runs
+ * will have left all the tables marked sent_table=TRUE, a subsequent run
+ * would emit an abbreviated stream (no tables) by default. This may be what
+ * is wanted, but for safety's sake it should not be the default behavior:
+ * programmers should have to make a deliberate choice to emit abbreviated
+ * images. Therefore the documentation and examples should encourage people
+ * to pass write_all_tables=TRUE; then it will take active thought to do the
+ * wrong thing.
+ */
+
+GLOBAL(void)
+jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
+{
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ if (write_all_tables)
+ jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
+
+ /* (Re)initialize error mgr and destination modules */
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+ (*cinfo->dest->init_destination) (cinfo);
+ /* Perform master selection of active modules */
+ jinit_compress_master(cinfo);
+ /* Set up for the first pass */
+ (*cinfo->master->prepare_for_pass) (cinfo);
+ /* Ready for application to drive first pass through jpeg_write_scanlines
+ * or jpeg_write_raw_data.
+ */
+ cinfo->next_scanline = 0;
+ cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
+}
+
+
+/*
+ * Write some scanlines of data to the JPEG compressor.
+ *
+ * The return value will be the number of lines actually written.
+ * This should be less than the supplied num_lines only in case that
+ * the data destination module has requested suspension of the compressor,
+ * or if more than image_height scanlines are passed in.
+ *
+ * Note: we warn about excess calls to jpeg_write_scanlines() since
+ * this likely signals an application programmer error. However,
+ * excess scanlines passed in the last valid call are *silently* ignored,
+ * so that the application need not adjust num_lines for end-of-image
+ * when using a multiple-scanline buffer.
+ */
+
+GLOBAL(JDIMENSION)
+jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
+ JDIMENSION num_lines)
+{
+ JDIMENSION row_ctr, rows_left;
+
+ if (cinfo->global_state != CSTATE_SCANNING)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ if (cinfo->next_scanline >= cinfo->image_height)
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->next_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->image_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+
+ /* Give master control module another chance if this is first call to
+ * jpeg_write_scanlines. This lets output of the frame/scan headers be
+ * delayed so that application can write COM, etc, markers between
+ * jpeg_start_compress and jpeg_write_scanlines.
+ */
+ if (cinfo->master->call_pass_startup)
+ (*cinfo->master->pass_startup) (cinfo);
+
+ /* Ignore any extra scanlines at bottom of image. */
+ rows_left = cinfo->image_height - cinfo->next_scanline;
+ if (num_lines > rows_left)
+ num_lines = rows_left;
+
+ row_ctr = 0;
+ (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
+ cinfo->next_scanline += row_ctr;
+ return row_ctr;
+}
+
+
+/*
+ * Alternate entry point to write raw data.
+ * Processes exactly one iMCU row per call, unless suspended.
+ */
+
+GLOBAL(JDIMENSION)
+jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
+ JDIMENSION num_lines)
+{
+ JDIMENSION lines_per_iMCU_row;
+
+ if (cinfo->global_state != CSTATE_RAW_OK)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ if (cinfo->next_scanline >= cinfo->image_height) {
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+ return 0;
+ }
+
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->next_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->image_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+
+ /* Give master control module another chance if this is first call to
+ * jpeg_write_raw_data. This lets output of the frame/scan headers be
+ * delayed so that application can write COM, etc, markers between
+ * jpeg_start_compress and jpeg_write_raw_data.
+ */
+ if (cinfo->master->call_pass_startup)
+ (*cinfo->master->pass_startup) (cinfo);
+
+ /* Verify that at least one iMCU row has been passed. */
+ lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
+ if (num_lines < lines_per_iMCU_row)
+ ERREXIT(cinfo, JERR_BUFFER_SIZE);
+
+ /* Directly compress the row. */
+ if (! (*cinfo->coef->compress_data) (cinfo, data)) {
+ /* If compressor did not consume the whole row, suspend processing. */
+ return 0;
+ }
+
+ /* OK, we processed one iMCU row. */
+ cinfo->next_scanline += lines_per_iMCU_row;
+ return lines_per_iMCU_row;
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c b/core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c
index ea2a4a6cbb..08910ef6f3 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jccoefct.c
@@ -1,452 +1,452 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jccoefct.c
- *
- * Copyright (C) 1994-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 the coefficient buffer controller for compression.
- * This controller is the top level of the JPEG compressor proper.
- * The coefficient buffer lies between forward-DCT and entropy encoding steps.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* We use a full-image coefficient buffer when doing Huffman optimization,
- * and also for writing multiple-scan JPEG files. In all cases, the DCT
- * step is run during the first pass, and subsequent passes need only read
- * the buffered coefficients.
- */
-#ifdef ENTROPY_OPT_SUPPORTED
-#define FULL_COEF_BUFFER_SUPPORTED
-#else
-#ifdef C_MULTISCAN_FILES_SUPPORTED
-#define FULL_COEF_BUFFER_SUPPORTED
-#endif
-#endif
-
-
-/* Private buffer controller object */
-
-typedef struct {
- struct jpeg_c_coef_controller pub; /* public fields */
-
- JDIMENSION iMCU_row_num; /* iMCU row # within image */
- JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
- int MCU_vert_offset; /* counts MCU rows within iMCU row */
- int MCU_rows_per_iMCU_row; /* number of such rows needed */
-
- /* For single-pass compression, it's sufficient to buffer just one MCU
- * (although this may prove a bit slow in practice). We allocate a
- * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
- * MCU constructed and sent. (On 80x86, the workspace is FAR even though
- * it's not really very big; this is to keep the module interfaces unchanged
- * when a large coefficient buffer is necessary.)
- * In multi-pass modes, this array points to the current MCU's blocks
- * within the virtual arrays.
- */
- JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
-
- /* In multi-pass modes, we need a virtual block array for each component. */
- jvirt_barray_ptr whole_image[MAX_COMPONENTS];
-} my_coef_controller;
-
-typedef my_coef_controller * my_coef_ptr;
-
-
-/* Forward declarations */
-METHODDEF(boolean) compress_data
- JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
-#ifdef FULL_COEF_BUFFER_SUPPORTED
-METHODDEF(boolean) compress_first_pass
- JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
-METHODDEF(boolean) compress_output
- JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
-#endif
-
-
-LOCAL(void)
-start_iMCU_row (j_compress_ptr cinfo)
-/* Reset within-iMCU-row counters for a new row */
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
-
- /* In an interleaved scan, an MCU row is the same as an iMCU row.
- * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
- * But at the bottom of the image, process only what's left.
- */
- if (cinfo->comps_in_scan > 1) {
- coef->MCU_rows_per_iMCU_row = 1;
- } else {
- if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
- coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
- else
- coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
- }
-
- coef->mcu_ctr = 0;
- coef->MCU_vert_offset = 0;
-}
-
-
-/*
- * Initialize for a processing pass.
- */
-
-METHODDEF(void)
-start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
-
- coef->iMCU_row_num = 0;
- start_iMCU_row(cinfo);
-
- switch (pass_mode) {
- case JBUF_PASS_THRU:
- if (coef->whole_image[0] != NULL)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- coef->pub.compress_data = compress_data;
- break;
-#ifdef FULL_COEF_BUFFER_SUPPORTED
- case JBUF_SAVE_AND_PASS:
- if (coef->whole_image[0] == NULL)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- coef->pub.compress_data = compress_first_pass;
- break;
- case JBUF_CRANK_DEST:
- if (coef->whole_image[0] == NULL)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- coef->pub.compress_data = compress_output;
- break;
-#endif
- default:
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- break;
- }
-}
-
-
-/*
- * Process some data in the single-pass case.
- * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
- * per call, ie, v_samp_factor block rows for each component in the image.
- * Returns TRUE if the iMCU row is completed, FALSE if suspended.
- *
- * NB: input_buf contains a plane for each component in image,
- * which we index according to the component's SOF position.
- */
-
-METHODDEF(boolean)
-compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- JDIMENSION MCU_col_num; /* index of current MCU within row */
- JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
- JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
- int blkn, bi, ci, yindex, yoffset, blockcnt;
- JDIMENSION ypos, xpos;
- jpeg_component_info *compptr;
-
- /* Loop to write as much as one whole iMCU row */
- for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
- yoffset++) {
- for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
- MCU_col_num++) {
- /* Determine where data comes from in input_buf and do the DCT thing.
- * Each call on forward_DCT processes a horizontal row of DCT blocks
- * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
- * sequentially. Dummy blocks at the right or bottom edge are filled in
- * specially. The data in them does not matter for image reconstruction,
- * so we fill them with values that will encode to the smallest amount of
- * data, viz: all zeroes in the AC entries, DC entries equal to previous
- * block's DC value. (Thanks to Thomas Kinsman for this idea.)
- */
- blkn = 0;
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
- : compptr->last_col_width;
- xpos = MCU_col_num * compptr->MCU_sample_width;
- ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
- for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
- if (coef->iMCU_row_num < last_iMCU_row ||
- yoffset+yindex < compptr->last_row_height) {
- (*cinfo->fdct->forward_DCT) (cinfo, compptr,
- input_buf[compptr->component_index],
- coef->MCU_buffer[blkn],
- ypos, xpos, (JDIMENSION) blockcnt);
- if (blockcnt < compptr->MCU_width) {
- /* Create some dummy blocks at the right edge of the image. */
- jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
- (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
- for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
- coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
- }
- }
- } else {
- /* Create a row of dummy blocks at the bottom of the image. */
- jzero_far((void FAR *) coef->MCU_buffer[blkn],
- compptr->MCU_width * SIZEOF(JBLOCK));
- for (bi = 0; bi < compptr->MCU_width; bi++) {
- coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
- }
- }
- blkn += compptr->MCU_width;
- ypos += DCTSIZE;
- }
- }
- /* Try to write the MCU. In event of a suspension failure, we will
- * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
- */
- if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
- /* Suspension forced; update state counters and exit */
- coef->MCU_vert_offset = yoffset;
- coef->mcu_ctr = MCU_col_num;
- return FALSE;
- }
- }
- /* Completed an MCU row, but perhaps not an iMCU row */
- coef->mcu_ctr = 0;
- }
- /* Completed the iMCU row, advance counters for next one */
- coef->iMCU_row_num++;
- start_iMCU_row(cinfo);
- return TRUE;
-}
-
-
-#ifdef FULL_COEF_BUFFER_SUPPORTED
-
-/*
- * Process some data in the first pass of a multi-pass case.
- * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
- * per call, ie, v_samp_factor block rows for each component in the image.
- * This amount of data is read from the source buffer, DCT'd and quantized,
- * and saved into the virtual arrays. We also generate suitable dummy blocks
- * as needed at the right and lower edges. (The dummy blocks are constructed
- * in the virtual arrays, which have been padded appropriately.) This makes
- * it possible for subsequent passes not to worry about real vs. dummy blocks.
- *
- * We must also emit the data to the entropy encoder. This is conveniently
- * done by calling compress_output() after we've loaded the current strip
- * of the virtual arrays.
- *
- * NB: input_buf contains a plane for each component in image. All
- * components are DCT'd and loaded into the virtual arrays in this pass.
- * However, it may be that only a subset of the components are emitted to
- * the entropy encoder during this first pass; be careful about looking
- * at the scan-dependent variables (MCU dimensions, etc).
- */
-
-METHODDEF(boolean)
-compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
- JDIMENSION blocks_across, MCUs_across, MCUindex;
- int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
- JCOEF lastDC;
- jpeg_component_info *compptr;
- JBLOCKARRAY buffer;
- JBLOCKROW thisblockrow, lastblockrow;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Align the virtual buffer for this component. */
- buffer = (*cinfo->mem->access_virt_barray)
- ((j_common_ptr) cinfo, coef->whole_image[ci],
- coef->iMCU_row_num * compptr->v_samp_factor,
- (JDIMENSION) compptr->v_samp_factor, TRUE);
- /* Count non-dummy DCT block rows in this iMCU row. */
- if (coef->iMCU_row_num < last_iMCU_row)
- block_rows = compptr->v_samp_factor;
- else {
- /* NB: can't use last_row_height here, since may not be set! */
- block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
- if (block_rows == 0) block_rows = compptr->v_samp_factor;
- }
- blocks_across = compptr->width_in_blocks;
- h_samp_factor = compptr->h_samp_factor;
- /* Count number of dummy blocks to be added at the right margin. */
- ndummy = (int) (blocks_across % h_samp_factor);
- if (ndummy > 0)
- ndummy = h_samp_factor - ndummy;
- /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
- * on forward_DCT processes a complete horizontal row of DCT blocks.
- */
- for (block_row = 0; block_row < block_rows; block_row++) {
- thisblockrow = buffer[block_row];
- (*cinfo->fdct->forward_DCT) (cinfo, compptr,
- input_buf[ci], thisblockrow,
- (JDIMENSION) (block_row * DCTSIZE),
- (JDIMENSION) 0, blocks_across);
- if (ndummy > 0) {
- /* Create dummy blocks at the right edge of the image. */
- thisblockrow += blocks_across; /* => first dummy block */
- jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
- lastDC = thisblockrow[-1][0];
- for (bi = 0; bi < ndummy; bi++) {
- thisblockrow[bi][0] = lastDC;
- }
- }
- }
- /* If at end of image, create dummy block rows as needed.
- * The tricky part here is that within each MCU, we want the DC values
- * of the dummy blocks to match the last real block's DC value.
- * This squeezes a few more bytes out of the resulting file...
- */
- if (coef->iMCU_row_num == last_iMCU_row) {
- blocks_across += ndummy; /* include lower right corner */
- MCUs_across = blocks_across / h_samp_factor;
- for (block_row = block_rows; block_row < compptr->v_samp_factor;
- block_row++) {
- thisblockrow = buffer[block_row];
- lastblockrow = buffer[block_row-1];
- jzero_far((void FAR *) thisblockrow,
- (size_t) (blocks_across * SIZEOF(JBLOCK)));
- for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
- lastDC = lastblockrow[h_samp_factor-1][0];
- for (bi = 0; bi < h_samp_factor; bi++) {
- thisblockrow[bi][0] = lastDC;
- }
- thisblockrow += h_samp_factor; /* advance to next MCU in row */
- lastblockrow += h_samp_factor;
- }
- }
- }
- }
- /* NB: compress_output will increment iMCU_row_num if successful.
- * A suspension return will result in redoing all the work above next time.
- */
-
- /* Emit data to the entropy encoder, sharing code with subsequent passes */
- return compress_output(cinfo, input_buf);
-}
-
-
-/*
- * Process some data in subsequent passes of a multi-pass case.
- * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
- * per call, ie, v_samp_factor block rows for each component in the scan.
- * The data is obtained from the virtual arrays and fed to the entropy coder.
- * Returns TRUE if the iMCU row is completed, FALSE if suspended.
- *
- * NB: input_buf is ignored; it is likely to be a NULL pointer.
- */
-
-METHODDEF(boolean)
-compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- JDIMENSION MCU_col_num; /* index of current MCU within row */
- int blkn, ci, xindex, yindex, yoffset;
- JDIMENSION start_col;
- JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
- JBLOCKROW buffer_ptr;
- jpeg_component_info *compptr;
-
- /* Align the virtual buffers for the components used in this scan.
- * NB: during first pass, this is safe only because the buffers will
- * already be aligned properly, so jmemmgr.c won't need to do any I/O.
- */
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- buffer[ci] = (*cinfo->mem->access_virt_barray)
- ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
- coef->iMCU_row_num * compptr->v_samp_factor,
- (JDIMENSION) compptr->v_samp_factor, FALSE);
- }
-
- /* Loop to process one whole iMCU row */
- for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
- yoffset++) {
- for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
- MCU_col_num++) {
- /* Construct list of pointers to DCT blocks belonging to this MCU */
- blkn = 0; /* index of current DCT block within MCU */
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- start_col = MCU_col_num * compptr->MCU_width;
- for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
- buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
- for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
- coef->MCU_buffer[blkn++] = buffer_ptr++;
- }
- }
- }
- /* Try to write the MCU. */
- if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
- /* Suspension forced; update state counters and exit */
- coef->MCU_vert_offset = yoffset;
- coef->mcu_ctr = MCU_col_num;
- return FALSE;
- }
- }
- /* Completed an MCU row, but perhaps not an iMCU row */
- coef->mcu_ctr = 0;
- }
- /* Completed the iMCU row, advance counters for next one */
- coef->iMCU_row_num++;
- start_iMCU_row(cinfo);
- return TRUE;
-}
-
-#endif /* FULL_COEF_BUFFER_SUPPORTED */
-
-
-/*
- * Initialize coefficient buffer controller.
- */
-
-GLOBAL(void)
-jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
-{
- my_coef_ptr coef;
-
- coef = (my_coef_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_coef_controller));
- cinfo->coef = (struct jpeg_c_coef_controller *) coef;
- coef->pub.start_pass = start_pass_coef;
-
- /* Create the coefficient buffer. */
- if (need_full_buffer) {
-#ifdef FULL_COEF_BUFFER_SUPPORTED
- /* Allocate a full-image virtual array for each component, */
- /* padded to a multiple of samp_factor DCT blocks in each direction. */
- int ci;
- jpeg_component_info *compptr;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
- (JDIMENSION) jround_up((long) compptr->width_in_blocks,
- (long) compptr->h_samp_factor),
- (JDIMENSION) jround_up((long) compptr->height_in_blocks,
- (long) compptr->v_samp_factor),
- (JDIMENSION) compptr->v_samp_factor);
- }
-#else
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
-#endif
- } else {
- /* We only need a single-MCU buffer. */
- JBLOCKROW buffer;
- int i;
-
- buffer = (JBLOCKROW)
- (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
- for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
- coef->MCU_buffer[i] = buffer + i;
- }
- coef->whole_image[0] = NULL; /* flag for no virtual arrays */
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jccoefct.c
+ *
+ * Copyright (C) 1994-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 the coefficient buffer controller for compression.
+ * This controller is the top level of the JPEG compressor proper.
+ * The coefficient buffer lies between forward-DCT and entropy encoding steps.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* We use a full-image coefficient buffer when doing Huffman optimization,
+ * and also for writing multiple-scan JPEG files. In all cases, the DCT
+ * step is run during the first pass, and subsequent passes need only read
+ * the buffered coefficients.
+ */
+#ifdef ENTROPY_OPT_SUPPORTED
+#define FULL_COEF_BUFFER_SUPPORTED
+#else
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+#define FULL_COEF_BUFFER_SUPPORTED
+#endif
+#endif
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_c_coef_controller pub; /* public fields */
+
+ JDIMENSION iMCU_row_num; /* iMCU row # within image */
+ JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
+ int MCU_vert_offset; /* counts MCU rows within iMCU row */
+ int MCU_rows_per_iMCU_row; /* number of such rows needed */
+
+ /* For single-pass compression, it's sufficient to buffer just one MCU
+ * (although this may prove a bit slow in practice). We allocate a
+ * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
+ * MCU constructed and sent. (On 80x86, the workspace is FAR even though
+ * it's not really very big; this is to keep the module interfaces unchanged
+ * when a large coefficient buffer is necessary.)
+ * In multi-pass modes, this array points to the current MCU's blocks
+ * within the virtual arrays.
+ */
+ JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
+
+ /* In multi-pass modes, we need a virtual block array for each component. */
+ jvirt_barray_ptr whole_image[MAX_COMPONENTS];
+} my_coef_controller;
+
+typedef my_coef_controller * my_coef_ptr;
+
+
+/* Forward declarations */
+METHODDEF(boolean) compress_data
+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+METHODDEF(boolean) compress_first_pass
+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
+METHODDEF(boolean) compress_output
+ JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
+#endif
+
+
+LOCAL(void)
+start_iMCU_row (j_compress_ptr cinfo)
+/* Reset within-iMCU-row counters for a new row */
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ /* In an interleaved scan, an MCU row is the same as an iMCU row.
+ * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
+ * But at the bottom of the image, process only what's left.
+ */
+ if (cinfo->comps_in_scan > 1) {
+ coef->MCU_rows_per_iMCU_row = 1;
+ } else {
+ if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
+ else
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
+ }
+
+ coef->mcu_ctr = 0;
+ coef->MCU_vert_offset = 0;
+}
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ coef->iMCU_row_num = 0;
+ start_iMCU_row(cinfo);
+
+ switch (pass_mode) {
+ case JBUF_PASS_THRU:
+ if (coef->whole_image[0] != NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ coef->pub.compress_data = compress_data;
+ break;
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+ case JBUF_SAVE_AND_PASS:
+ if (coef->whole_image[0] == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ coef->pub.compress_data = compress_first_pass;
+ break;
+ case JBUF_CRANK_DEST:
+ if (coef->whole_image[0] == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ coef->pub.compress_data = compress_output;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ break;
+ }
+}
+
+
+/*
+ * Process some data in the single-pass case.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the image.
+ * Returns TRUE if the iMCU row is completed, FALSE if suspended.
+ *
+ * NB: input_buf contains a plane for each component in image,
+ * which we index according to the component's SOF position.
+ */
+
+METHODDEF(boolean)
+compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ int blkn, bi, ci, yindex, yoffset, blockcnt;
+ JDIMENSION ypos, xpos;
+ jpeg_component_info *compptr;
+
+ /* Loop to write as much as one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
+ MCU_col_num++) {
+ /* Determine where data comes from in input_buf and do the DCT thing.
+ * Each call on forward_DCT processes a horizontal row of DCT blocks
+ * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
+ * sequentially. Dummy blocks at the right or bottom edge are filled in
+ * specially. The data in them does not matter for image reconstruction,
+ * so we fill them with values that will encode to the smallest amount of
+ * data, viz: all zeroes in the AC entries, DC entries equal to previous
+ * block's DC value. (Thanks to Thomas Kinsman for this idea.)
+ */
+ blkn = 0;
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+ : compptr->last_col_width;
+ xpos = MCU_col_num * compptr->MCU_sample_width;
+ ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ if (coef->iMCU_row_num < last_iMCU_row ||
+ yoffset+yindex < compptr->last_row_height) {
+ (*cinfo->fdct->forward_DCT) (cinfo, compptr,
+ input_buf[compptr->component_index],
+ coef->MCU_buffer[blkn],
+ ypos, xpos, (JDIMENSION) blockcnt);
+ if (blockcnt < compptr->MCU_width) {
+ /* Create some dummy blocks at the right edge of the image. */
+ jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
+ (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
+ for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
+ coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
+ }
+ }
+ } else {
+ /* Create a row of dummy blocks at the bottom of the image. */
+ jzero_far((void FAR *) coef->MCU_buffer[blkn],
+ compptr->MCU_width * SIZEOF(JBLOCK));
+ for (bi = 0; bi < compptr->MCU_width; bi++) {
+ coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
+ }
+ }
+ blkn += compptr->MCU_width;
+ ypos += DCTSIZE;
+ }
+ }
+ /* Try to write the MCU. In event of a suspension failure, we will
+ * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
+ */
+ if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->mcu_ctr = MCU_col_num;
+ return FALSE;
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->mcu_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ coef->iMCU_row_num++;
+ start_iMCU_row(cinfo);
+ return TRUE;
+}
+
+
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+
+/*
+ * Process some data in the first pass of a multi-pass case.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the image.
+ * This amount of data is read from the source buffer, DCT'd and quantized,
+ * and saved into the virtual arrays. We also generate suitable dummy blocks
+ * as needed at the right and lower edges. (The dummy blocks are constructed
+ * in the virtual arrays, which have been padded appropriately.) This makes
+ * it possible for subsequent passes not to worry about real vs. dummy blocks.
+ *
+ * We must also emit the data to the entropy encoder. This is conveniently
+ * done by calling compress_output() after we've loaded the current strip
+ * of the virtual arrays.
+ *
+ * NB: input_buf contains a plane for each component in image. All
+ * components are DCT'd and loaded into the virtual arrays in this pass.
+ * However, it may be that only a subset of the components are emitted to
+ * the entropy encoder during this first pass; be careful about looking
+ * at the scan-dependent variables (MCU dimensions, etc).
+ */
+
+METHODDEF(boolean)
+compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ JDIMENSION blocks_across, MCUs_across, MCUindex;
+ int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
+ JCOEF lastDC;
+ jpeg_component_info *compptr;
+ JBLOCKARRAY buffer;
+ JBLOCKROW thisblockrow, lastblockrow;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Align the virtual buffer for this component. */
+ buffer = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[ci],
+ coef->iMCU_row_num * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ /* Count non-dummy DCT block rows in this iMCU row. */
+ if (coef->iMCU_row_num < last_iMCU_row)
+ block_rows = compptr->v_samp_factor;
+ else {
+ /* NB: can't use last_row_height here, since may not be set! */
+ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (block_rows == 0) block_rows = compptr->v_samp_factor;
+ }
+ blocks_across = compptr->width_in_blocks;
+ h_samp_factor = compptr->h_samp_factor;
+ /* Count number of dummy blocks to be added at the right margin. */
+ ndummy = (int) (blocks_across % h_samp_factor);
+ if (ndummy > 0)
+ ndummy = h_samp_factor - ndummy;
+ /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
+ * on forward_DCT processes a complete horizontal row of DCT blocks.
+ */
+ for (block_row = 0; block_row < block_rows; block_row++) {
+ thisblockrow = buffer[block_row];
+ (*cinfo->fdct->forward_DCT) (cinfo, compptr,
+ input_buf[ci], thisblockrow,
+ (JDIMENSION) (block_row * DCTSIZE),
+ (JDIMENSION) 0, blocks_across);
+ if (ndummy > 0) {
+ /* Create dummy blocks at the right edge of the image. */
+ thisblockrow += blocks_across; /* => first dummy block */
+ jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
+ lastDC = thisblockrow[-1][0];
+ for (bi = 0; bi < ndummy; bi++) {
+ thisblockrow[bi][0] = lastDC;
+ }
+ }
+ }
+ /* If at end of image, create dummy block rows as needed.
+ * The tricky part here is that within each MCU, we want the DC values
+ * of the dummy blocks to match the last real block's DC value.
+ * This squeezes a few more bytes out of the resulting file...
+ */
+ if (coef->iMCU_row_num == last_iMCU_row) {
+ blocks_across += ndummy; /* include lower right corner */
+ MCUs_across = blocks_across / h_samp_factor;
+ for (block_row = block_rows; block_row < compptr->v_samp_factor;
+ block_row++) {
+ thisblockrow = buffer[block_row];
+ lastblockrow = buffer[block_row-1];
+ jzero_far((void FAR *) thisblockrow,
+ (size_t) (blocks_across * SIZEOF(JBLOCK)));
+ for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
+ lastDC = lastblockrow[h_samp_factor-1][0];
+ for (bi = 0; bi < h_samp_factor; bi++) {
+ thisblockrow[bi][0] = lastDC;
+ }
+ thisblockrow += h_samp_factor; /* advance to next MCU in row */
+ lastblockrow += h_samp_factor;
+ }
+ }
+ }
+ }
+ /* NB: compress_output will increment iMCU_row_num if successful.
+ * A suspension return will result in redoing all the work above next time.
+ */
+
+ /* Emit data to the entropy encoder, sharing code with subsequent passes */
+ return compress_output(cinfo, input_buf);
+}
+
+
+/*
+ * Process some data in subsequent passes of a multi-pass case.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the scan.
+ * The data is obtained from the virtual arrays and fed to the entropy coder.
+ * Returns TRUE if the iMCU row is completed, FALSE if suspended.
+ *
+ * NB: input_buf is ignored; it is likely to be a NULL pointer.
+ */
+
+METHODDEF(boolean)
+compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ int blkn, ci, xindex, yindex, yoffset;
+ JDIMENSION start_col;
+ JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
+ JBLOCKROW buffer_ptr;
+ jpeg_component_info *compptr;
+
+ /* Align the virtual buffers for the components used in this scan.
+ * NB: during first pass, this is safe only because the buffers will
+ * already be aligned properly, so jmemmgr.c won't need to do any I/O.
+ */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ buffer[ci] = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
+ coef->iMCU_row_num * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ }
+
+ /* Loop to process one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
+ MCU_col_num++) {
+ /* Construct list of pointers to DCT blocks belonging to this MCU */
+ blkn = 0; /* index of current DCT block within MCU */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ start_col = MCU_col_num * compptr->MCU_width;
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+ for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
+ coef->MCU_buffer[blkn++] = buffer_ptr++;
+ }
+ }
+ }
+ /* Try to write the MCU. */
+ if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->mcu_ctr = MCU_col_num;
+ return FALSE;
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->mcu_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ coef->iMCU_row_num++;
+ start_iMCU_row(cinfo);
+ return TRUE;
+}
+
+#endif /* FULL_COEF_BUFFER_SUPPORTED */
+
+
+/*
+ * Initialize coefficient buffer controller.
+ */
+
+GLOBAL(void)
+jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+ my_coef_ptr coef;
+
+ coef = (my_coef_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_coef_controller));
+ cinfo->coef = (struct jpeg_c_coef_controller *) coef;
+ coef->pub.start_pass = start_pass_coef;
+
+ /* Create the coefficient buffer. */
+ if (need_full_buffer) {
+#ifdef FULL_COEF_BUFFER_SUPPORTED
+ /* Allocate a full-image virtual array for each component, */
+ /* padded to a multiple of samp_factor DCT blocks in each direction. */
+ int ci;
+ jpeg_component_info *compptr;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+ (long) compptr->h_samp_factor),
+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+ (long) compptr->v_samp_factor),
+ (JDIMENSION) compptr->v_samp_factor);
+ }
+#else
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+ } else {
+ /* We only need a single-MCU buffer. */
+ JBLOCKROW buffer;
+ int i;
+
+ buffer = (JBLOCKROW)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+ for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
+ coef->MCU_buffer[i] = buffer + i;
+ }
+ coef->whole_image[0] = NULL; /* flag for no virtual arrays */
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jccolor.c b/core/src/fxcodec/libjpeg/fpdfapi_jccolor.c
index 8ff863a14b..809a05b5a1 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jccolor.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jccolor.c
@@ -1,462 +1,462 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jccolor.c
- *
- * Copyright (C) 1991-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 contains input colorspace conversion routines.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Private subobject */
-
-typedef struct {
- struct jpeg_color_converter pub; /* public fields */
-
- /* Private state for RGB->YCC conversion */
- INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
-} my_color_converter;
-
-typedef my_color_converter * my_cconvert_ptr;
-
-
-/**************** RGB -> YCbCr conversion: most common case **************/
-
-/*
- * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
- * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
- * The conversion equations to be implemented are therefore
- * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
- * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
- * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
- * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
- * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
- * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
- * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
- * were not represented exactly. Now we sacrifice exact representation of
- * maximum red and maximum blue in order to get exact grayscales.
- *
- * To avoid floating-point arithmetic, we represent the fractional constants
- * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
- * the products by 2^16, with appropriate rounding, to get the correct answer.
- *
- * For even more speed, we avoid doing any multiplications in the inner loop
- * by precalculating the constants times R,G,B for all possible values.
- * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
- * for 12-bit samples it is still acceptable. It's not very reasonable for
- * 16-bit samples, but if you want lossless storage you shouldn't be changing
- * colorspace anyway.
- * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
- * in the tables to save adding them separately in the inner loop.
- */
-
-#define SCALEBITS 16 /* speediest right-shift on some machines */
-#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
-#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
-#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
-
-/* We allocate one big table and divide it up into eight parts, instead of
- * doing eight alloc_small requests. This lets us use a single table base
- * address, which can be held in a register in the inner loops on many
- * machines (more than can hold all eight addresses, anyway).
- */
-
-#define R_Y_OFF 0 /* offset to R => Y section */
-#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
-#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
-#define R_CB_OFF (3*(MAXJSAMPLE+1))
-#define G_CB_OFF (4*(MAXJSAMPLE+1))
-#define B_CB_OFF (5*(MAXJSAMPLE+1))
-#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
-#define G_CR_OFF (6*(MAXJSAMPLE+1))
-#define B_CR_OFF (7*(MAXJSAMPLE+1))
-#define TABLE_SIZE (8*(MAXJSAMPLE+1))
-
-
-/*
- * Initialize for RGB->YCC colorspace conversion.
- */
-
-METHODDEF(void)
-rgb_ycc_start (j_compress_ptr cinfo)
-{
- my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
- INT32 * rgb_ycc_tab;
- INT32 i;
-
- /* Allocate and fill in the conversion tables. */
- cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (TABLE_SIZE * SIZEOF(INT32)));
-
- for (i = 0; i <= MAXJSAMPLE; i++) {
- rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
- rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
- rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
- rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
- rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
- /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
- * This ensures that the maximum output will round to MAXJSAMPLE
- * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
- */
- rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
-/* B=>Cb and R=>Cr tables are the same
- rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
-*/
- rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
- rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
- }
-}
-
-
-/*
- * Convert some rows of samples to the JPEG colorspace.
- *
- * Note that we change from the application's interleaved-pixel format
- * to our internal noninterleaved, one-plane-per-component format.
- * The input buffer is therefore three times as wide as the output buffer.
- *
- * A starting row offset is provided only for the output buffer. The caller
- * can easily adjust the passed input_buf value to accommodate any row
- * offset required on that side.
- */
-
-METHODDEF(void)
-rgb_ycc_convert (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
- JDIMENSION output_row, int num_rows)
-{
- my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
- register int r, g, b;
- register INT32 * ctab = cconvert->rgb_ycc_tab;
- register JSAMPROW inptr;
- register JSAMPROW outptr0, outptr1, outptr2;
- register JDIMENSION col;
- JDIMENSION num_cols = cinfo->image_width;
-
- while (--num_rows >= 0) {
- inptr = *input_buf++;
- outptr0 = output_buf[0][output_row];
- outptr1 = output_buf[1][output_row];
- outptr2 = output_buf[2][output_row];
- output_row++;
- for (col = 0; col < num_cols; col++) {
- r = GETJSAMPLE(inptr[RGB_RED]);
- g = GETJSAMPLE(inptr[RGB_GREEN]);
- b = GETJSAMPLE(inptr[RGB_BLUE]);
- inptr += RGB_PIXELSIZE;
- /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
- * must be too; we do not need an explicit range-limiting operation.
- * Hence the value being shifted is never negative, and we don't
- * need the general RIGHT_SHIFT macro.
- */
- /* Y */
- outptr0[col] = (JSAMPLE)
- ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
- >> SCALEBITS);
- /* Cb */
- outptr1[col] = (JSAMPLE)
- ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
- >> SCALEBITS);
- /* Cr */
- outptr2[col] = (JSAMPLE)
- ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
- >> SCALEBITS);
- }
- }
-}
-
-
-/**************** Cases other than RGB -> YCbCr **************/
-
-
-/*
- * Convert some rows of samples to the JPEG colorspace.
- * This version handles RGB->grayscale conversion, which is the same
- * as the RGB->Y portion of RGB->YCbCr.
- * We assume rgb_ycc_start has been called (we only use the Y tables).
- */
-
-METHODDEF(void)
-rgb_gray_convert (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
- JDIMENSION output_row, int num_rows)
-{
- my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
- register int r, g, b;
- register INT32 * ctab = cconvert->rgb_ycc_tab;
- register JSAMPROW inptr;
- register JSAMPROW outptr;
- register JDIMENSION col;
- JDIMENSION num_cols = cinfo->image_width;
-
- while (--num_rows >= 0) {
- inptr = *input_buf++;
- outptr = output_buf[0][output_row];
- output_row++;
- for (col = 0; col < num_cols; col++) {
- r = GETJSAMPLE(inptr[RGB_RED]);
- g = GETJSAMPLE(inptr[RGB_GREEN]);
- b = GETJSAMPLE(inptr[RGB_BLUE]);
- inptr += RGB_PIXELSIZE;
- /* Y */
- outptr[col] = (JSAMPLE)
- ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
- >> SCALEBITS);
- }
- }
-}
-
-
-/*
- * Convert some rows of samples to the JPEG colorspace.
- * This version handles Adobe-style CMYK->YCCK conversion,
- * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
- * conversion as above, while passing K (black) unchanged.
- * We assume rgb_ycc_start has been called.
- */
-
-METHODDEF(void)
-cmyk_ycck_convert (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
- JDIMENSION output_row, int num_rows)
-{
- my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
- register int r, g, b;
- register INT32 * ctab = cconvert->rgb_ycc_tab;
- register JSAMPROW inptr;
- register JSAMPROW outptr0, outptr1, outptr2, outptr3;
- register JDIMENSION col;
- JDIMENSION num_cols = cinfo->image_width;
-
- while (--num_rows >= 0) {
- inptr = *input_buf++;
- outptr0 = output_buf[0][output_row];
- outptr1 = output_buf[1][output_row];
- outptr2 = output_buf[2][output_row];
- outptr3 = output_buf[3][output_row];
- output_row++;
- for (col = 0; col < num_cols; col++) {
- r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
- g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
- b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
- /* K passes through as-is */
- outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
- inptr += 4;
- /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
- * must be too; we do not need an explicit range-limiting operation.
- * Hence the value being shifted is never negative, and we don't
- * need the general RIGHT_SHIFT macro.
- */
- /* Y */
- outptr0[col] = (JSAMPLE)
- ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
- >> SCALEBITS);
- /* Cb */
- outptr1[col] = (JSAMPLE)
- ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
- >> SCALEBITS);
- /* Cr */
- outptr2[col] = (JSAMPLE)
- ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
- >> SCALEBITS);
- }
- }
-}
-
-
-/*
- * Convert some rows of samples to the JPEG colorspace.
- * This version handles grayscale output with no conversion.
- * The source can be either plain grayscale or YCbCr (since Y == gray).
- */
-
-METHODDEF(void)
-grayscale_convert (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
- JDIMENSION output_row, int num_rows)
-{
- register JSAMPROW inptr;
- register JSAMPROW outptr;
- register JDIMENSION col;
- JDIMENSION num_cols = cinfo->image_width;
- int instride = cinfo->input_components;
-
- while (--num_rows >= 0) {
- inptr = *input_buf++;
- outptr = output_buf[0][output_row];
- output_row++;
- for (col = 0; col < num_cols; col++) {
- outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
- inptr += instride;
- }
- }
-}
-
-
-/*
- * Convert some rows of samples to the JPEG colorspace.
- * This version handles multi-component colorspaces without conversion.
- * We assume input_components == num_components.
- */
-
-METHODDEF(void)
-null_convert (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
- JDIMENSION output_row, int num_rows)
-{
- register JSAMPROW inptr;
- register JSAMPROW outptr;
- register JDIMENSION col;
- register int ci;
- int nc = cinfo->num_components;
- JDIMENSION num_cols = cinfo->image_width;
-
- while (--num_rows >= 0) {
- /* It seems fastest to make a separate pass for each component. */
- for (ci = 0; ci < nc; ci++) {
- inptr = *input_buf;
- outptr = output_buf[ci][output_row];
- for (col = 0; col < num_cols; col++) {
- outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
- inptr += nc;
- }
- }
- input_buf++;
- output_row++;
- }
-}
-
-
-/*
- * Empty method for start_pass.
- */
-
-METHODDEF(void)
-null_method (j_compress_ptr cinfo)
-{
- /* no work needed */
-}
-
-
-/*
- * Module initialization routine for input colorspace conversion.
- */
-
-GLOBAL(void)
-jinit_color_converter (j_compress_ptr cinfo)
-{
- my_cconvert_ptr cconvert;
-
- cconvert = (my_cconvert_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_color_converter));
- cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
- /* set start_pass to null method until we find out differently */
- cconvert->pub.start_pass = null_method;
-
- /* Make sure input_components agrees with in_color_space */
- switch (cinfo->in_color_space) {
- case JCS_GRAYSCALE:
- if (cinfo->input_components != 1)
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- break;
-
- case JCS_RGB:
-#if RGB_PIXELSIZE != 3
- if (cinfo->input_components != RGB_PIXELSIZE)
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- break;
-#endif /* else share code with YCbCr */
-
- case JCS_YCbCr:
- if (cinfo->input_components != 3)
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- break;
-
- case JCS_CMYK:
- case JCS_YCCK:
- if (cinfo->input_components != 4)
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- break;
-
- default: /* JCS_UNKNOWN can be anything */
- if (cinfo->input_components < 1)
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- break;
- }
-
- /* Check num_components, set conversion method based on requested space */
- switch (cinfo->jpeg_color_space) {
- case JCS_GRAYSCALE:
- if (cinfo->num_components != 1)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- if (cinfo->in_color_space == JCS_GRAYSCALE)
- cconvert->pub.color_convert = grayscale_convert;
- else if (cinfo->in_color_space == JCS_RGB) {
- cconvert->pub.start_pass = rgb_ycc_start;
- cconvert->pub.color_convert = rgb_gray_convert;
- } else if (cinfo->in_color_space == JCS_YCbCr)
- cconvert->pub.color_convert = grayscale_convert;
- else
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
-
- case JCS_RGB:
- if (cinfo->num_components != 3)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
- cconvert->pub.color_convert = null_convert;
- else
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
-
- case JCS_YCbCr:
- if (cinfo->num_components != 3)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- if (cinfo->in_color_space == JCS_RGB) {
- cconvert->pub.start_pass = rgb_ycc_start;
- cconvert->pub.color_convert = rgb_ycc_convert;
- } else if (cinfo->in_color_space == JCS_YCbCr)
- cconvert->pub.color_convert = null_convert;
- else
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
-
- case JCS_CMYK:
- if (cinfo->num_components != 4)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- if (cinfo->in_color_space == JCS_CMYK)
- cconvert->pub.color_convert = null_convert;
- else
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
-
- case JCS_YCCK:
- if (cinfo->num_components != 4)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- if (cinfo->in_color_space == JCS_CMYK) {
- cconvert->pub.start_pass = rgb_ycc_start;
- cconvert->pub.color_convert = cmyk_ycck_convert;
- } else if (cinfo->in_color_space == JCS_YCCK)
- cconvert->pub.color_convert = null_convert;
- else
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
-
- default: /* allow null conversion of JCS_UNKNOWN */
- if (cinfo->jpeg_color_space != cinfo->in_color_space ||
- cinfo->num_components != cinfo->input_components)
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- cconvert->pub.color_convert = null_convert;
- break;
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jccolor.c
+ *
+ * Copyright (C) 1991-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 contains input colorspace conversion routines.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_color_converter pub; /* public fields */
+
+ /* Private state for RGB->YCC conversion */
+ INT32 * rgb_ycc_tab; /* => table for RGB to YCbCr conversion */
+} my_color_converter;
+
+typedef my_color_converter * my_cconvert_ptr;
+
+
+/**************** RGB -> YCbCr conversion: most common case **************/
+
+/*
+ * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
+ * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
+ * The conversion equations to be implemented are therefore
+ * Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
+ * Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
+ * Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
+ * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
+ * Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
+ * rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
+ * negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
+ * were not represented exactly. Now we sacrifice exact representation of
+ * maximum red and maximum blue in order to get exact grayscales.
+ *
+ * To avoid floating-point arithmetic, we represent the fractional constants
+ * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
+ * the products by 2^16, with appropriate rounding, to get the correct answer.
+ *
+ * For even more speed, we avoid doing any multiplications in the inner loop
+ * by precalculating the constants times R,G,B for all possible values.
+ * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
+ * for 12-bit samples it is still acceptable. It's not very reasonable for
+ * 16-bit samples, but if you want lossless storage you shouldn't be changing
+ * colorspace anyway.
+ * The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
+ * in the tables to save adding them separately in the inner loop.
+ */
+
+#define SCALEBITS 16 /* speediest right-shift on some machines */
+#define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS)
+#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
+#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
+
+/* We allocate one big table and divide it up into eight parts, instead of
+ * doing eight alloc_small requests. This lets us use a single table base
+ * address, which can be held in a register in the inner loops on many
+ * machines (more than can hold all eight addresses, anyway).
+ */
+
+#define R_Y_OFF 0 /* offset to R => Y section */
+#define G_Y_OFF (1*(MAXJSAMPLE+1)) /* offset to G => Y section */
+#define B_Y_OFF (2*(MAXJSAMPLE+1)) /* etc. */
+#define R_CB_OFF (3*(MAXJSAMPLE+1))
+#define G_CB_OFF (4*(MAXJSAMPLE+1))
+#define B_CB_OFF (5*(MAXJSAMPLE+1))
+#define R_CR_OFF B_CB_OFF /* B=>Cb, R=>Cr are the same */
+#define G_CR_OFF (6*(MAXJSAMPLE+1))
+#define B_CR_OFF (7*(MAXJSAMPLE+1))
+#define TABLE_SIZE (8*(MAXJSAMPLE+1))
+
+
+/*
+ * Initialize for RGB->YCC colorspace conversion.
+ */
+
+METHODDEF(void)
+rgb_ycc_start (j_compress_ptr cinfo)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ INT32 * rgb_ycc_tab;
+ INT32 i;
+
+ /* Allocate and fill in the conversion tables. */
+ cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (TABLE_SIZE * SIZEOF(INT32)));
+
+ for (i = 0; i <= MAXJSAMPLE; i++) {
+ rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
+ rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
+ rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
+ rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
+ rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
+ /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
+ * This ensures that the maximum output will round to MAXJSAMPLE
+ * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
+ */
+ rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
+/* B=>Cb and R=>Cr tables are the same
+ rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1;
+*/
+ rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
+ rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ *
+ * Note that we change from the application's interleaved-pixel format
+ * to our internal noninterleaved, one-plane-per-component format.
+ * The input buffer is therefore three times as wide as the output buffer.
+ *
+ * A starting row offset is provided only for the output buffer. The caller
+ * can easily adjust the passed input_buf value to accommodate any row
+ * offset required on that side.
+ */
+
+METHODDEF(void)
+rgb_ycc_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int r, g, b;
+ register INT32 * ctab = cconvert->rgb_ycc_tab;
+ register JSAMPROW inptr;
+ register JSAMPROW outptr0, outptr1, outptr2;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->image_width;
+
+ while (--num_rows >= 0) {
+ inptr = *input_buf++;
+ outptr0 = output_buf[0][output_row];
+ outptr1 = output_buf[1][output_row];
+ outptr2 = output_buf[2][output_row];
+ output_row++;
+ for (col = 0; col < num_cols; col++) {
+ r = GETJSAMPLE(inptr[RGB_RED]);
+ g = GETJSAMPLE(inptr[RGB_GREEN]);
+ b = GETJSAMPLE(inptr[RGB_BLUE]);
+ inptr += RGB_PIXELSIZE;
+ /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
+ * must be too; we do not need an explicit range-limiting operation.
+ * Hence the value being shifted is never negative, and we don't
+ * need the general RIGHT_SHIFT macro.
+ */
+ /* Y */
+ outptr0[col] = (JSAMPLE)
+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+ >> SCALEBITS);
+ /* Cb */
+ outptr1[col] = (JSAMPLE)
+ ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
+ >> SCALEBITS);
+ /* Cr */
+ outptr2[col] = (JSAMPLE)
+ ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
+ >> SCALEBITS);
+ }
+ }
+}
+
+
+/**************** Cases other than RGB -> YCbCr **************/
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles RGB->grayscale conversion, which is the same
+ * as the RGB->Y portion of RGB->YCbCr.
+ * We assume rgb_ycc_start has been called (we only use the Y tables).
+ */
+
+METHODDEF(void)
+rgb_gray_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int r, g, b;
+ register INT32 * ctab = cconvert->rgb_ycc_tab;
+ register JSAMPROW inptr;
+ register JSAMPROW outptr;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->image_width;
+
+ while (--num_rows >= 0) {
+ inptr = *input_buf++;
+ outptr = output_buf[0][output_row];
+ output_row++;
+ for (col = 0; col < num_cols; col++) {
+ r = GETJSAMPLE(inptr[RGB_RED]);
+ g = GETJSAMPLE(inptr[RGB_GREEN]);
+ b = GETJSAMPLE(inptr[RGB_BLUE]);
+ inptr += RGB_PIXELSIZE;
+ /* Y */
+ outptr[col] = (JSAMPLE)
+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+ >> SCALEBITS);
+ }
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles Adobe-style CMYK->YCCK conversion,
+ * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
+ * conversion as above, while passing K (black) unchanged.
+ * We assume rgb_ycc_start has been called.
+ */
+
+METHODDEF(void)
+cmyk_ycck_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int r, g, b;
+ register INT32 * ctab = cconvert->rgb_ycc_tab;
+ register JSAMPROW inptr;
+ register JSAMPROW outptr0, outptr1, outptr2, outptr3;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->image_width;
+
+ while (--num_rows >= 0) {
+ inptr = *input_buf++;
+ outptr0 = output_buf[0][output_row];
+ outptr1 = output_buf[1][output_row];
+ outptr2 = output_buf[2][output_row];
+ outptr3 = output_buf[3][output_row];
+ output_row++;
+ for (col = 0; col < num_cols; col++) {
+ r = MAXJSAMPLE - GETJSAMPLE(inptr[0]);
+ g = MAXJSAMPLE - GETJSAMPLE(inptr[1]);
+ b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
+ /* K passes through as-is */
+ outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
+ inptr += 4;
+ /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
+ * must be too; we do not need an explicit range-limiting operation.
+ * Hence the value being shifted is never negative, and we don't
+ * need the general RIGHT_SHIFT macro.
+ */
+ /* Y */
+ outptr0[col] = (JSAMPLE)
+ ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
+ >> SCALEBITS);
+ /* Cb */
+ outptr1[col] = (JSAMPLE)
+ ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
+ >> SCALEBITS);
+ /* Cr */
+ outptr2[col] = (JSAMPLE)
+ ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
+ >> SCALEBITS);
+ }
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles grayscale output with no conversion.
+ * The source can be either plain grayscale or YCbCr (since Y == gray).
+ */
+
+METHODDEF(void)
+grayscale_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ register JSAMPROW inptr;
+ register JSAMPROW outptr;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->image_width;
+ int instride = cinfo->input_components;
+
+ while (--num_rows >= 0) {
+ inptr = *input_buf++;
+ outptr = output_buf[0][output_row];
+ output_row++;
+ for (col = 0; col < num_cols; col++) {
+ outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */
+ inptr += instride;
+ }
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the JPEG colorspace.
+ * This version handles multi-component colorspaces without conversion.
+ * We assume input_components == num_components.
+ */
+
+METHODDEF(void)
+null_convert (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows)
+{
+ register JSAMPROW inptr;
+ register JSAMPROW outptr;
+ register JDIMENSION col;
+ register int ci;
+ int nc = cinfo->num_components;
+ JDIMENSION num_cols = cinfo->image_width;
+
+ while (--num_rows >= 0) {
+ /* It seems fastest to make a separate pass for each component. */
+ for (ci = 0; ci < nc; ci++) {
+ inptr = *input_buf;
+ outptr = output_buf[ci][output_row];
+ for (col = 0; col < num_cols; col++) {
+ outptr[col] = inptr[ci]; /* don't need GETJSAMPLE() here */
+ inptr += nc;
+ }
+ }
+ input_buf++;
+ output_row++;
+ }
+}
+
+
+/*
+ * Empty method for start_pass.
+ */
+
+METHODDEF(void)
+null_method (j_compress_ptr cinfo)
+{
+ /* no work needed */
+}
+
+
+/*
+ * Module initialization routine for input colorspace conversion.
+ */
+
+GLOBAL(void)
+jinit_color_converter (j_compress_ptr cinfo)
+{
+ my_cconvert_ptr cconvert;
+
+ cconvert = (my_cconvert_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_color_converter));
+ cinfo->cconvert = (struct jpeg_color_converter *) cconvert;
+ /* set start_pass to null method until we find out differently */
+ cconvert->pub.start_pass = null_method;
+
+ /* Make sure input_components agrees with in_color_space */
+ switch (cinfo->in_color_space) {
+ case JCS_GRAYSCALE:
+ if (cinfo->input_components != 1)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+
+ case JCS_RGB:
+#if RGB_PIXELSIZE != 3
+ if (cinfo->input_components != RGB_PIXELSIZE)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+#endif /* else share code with YCbCr */
+
+ case JCS_YCbCr:
+ if (cinfo->input_components != 3)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+
+ case JCS_CMYK:
+ case JCS_YCCK:
+ if (cinfo->input_components != 4)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+
+ default: /* JCS_UNKNOWN can be anything */
+ if (cinfo->input_components < 1)
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ break;
+ }
+
+ /* Check num_components, set conversion method based on requested space */
+ switch (cinfo->jpeg_color_space) {
+ case JCS_GRAYSCALE:
+ if (cinfo->num_components != 1)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_GRAYSCALE)
+ cconvert->pub.color_convert = grayscale_convert;
+ else if (cinfo->in_color_space == JCS_RGB) {
+ cconvert->pub.start_pass = rgb_ycc_start;
+ cconvert->pub.color_convert = rgb_gray_convert;
+ } else if (cinfo->in_color_space == JCS_YCbCr)
+ cconvert->pub.color_convert = grayscale_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_RGB:
+ if (cinfo->num_components != 3)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_RGB && RGB_PIXELSIZE == 3)
+ cconvert->pub.color_convert = null_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_YCbCr:
+ if (cinfo->num_components != 3)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_RGB) {
+ cconvert->pub.start_pass = rgb_ycc_start;
+ cconvert->pub.color_convert = rgb_ycc_convert;
+ } else if (cinfo->in_color_space == JCS_YCbCr)
+ cconvert->pub.color_convert = null_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_CMYK:
+ if (cinfo->num_components != 4)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_CMYK)
+ cconvert->pub.color_convert = null_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_YCCK:
+ if (cinfo->num_components != 4)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ if (cinfo->in_color_space == JCS_CMYK) {
+ cconvert->pub.start_pass = rgb_ycc_start;
+ cconvert->pub.color_convert = cmyk_ycck_convert;
+ } else if (cinfo->in_color_space == JCS_YCCK)
+ cconvert->pub.color_convert = null_convert;
+ else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ default: /* allow null conversion of JCS_UNKNOWN */
+ if (cinfo->jpeg_color_space != cinfo->in_color_space ||
+ cinfo->num_components != cinfo->input_components)
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ cconvert->pub.color_convert = null_convert;
+ break;
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c b/core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c
index d4082043a5..cbd7d11207 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcdctmgr.c
@@ -1,390 +1,390 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcdctmgr.c
- *
- * Copyright (C) 1994-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 contains the forward-DCT management logic.
- * This code selects a particular DCT implementation to be used,
- * and it performs related housekeeping chores including coefficient
- * quantization.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jdct.h" /* Private declarations for DCT subsystem */
-
-
-/* Private subobject for this module */
-
-typedef struct {
- struct jpeg_forward_dct pub; /* public fields */
-
- /* Pointer to the DCT routine actually in use */
- forward_DCT_method_ptr do_dct;
-
- /* The actual post-DCT divisors --- not identical to the quant table
- * entries, because of scaling (especially for an unnormalized DCT).
- * Each table is given in normal array order.
- */
- DCTELEM * divisors[NUM_QUANT_TBLS];
-
-#ifdef DCT_FLOAT_SUPPORTED
- /* Same as above for the floating-point case. */
- float_DCT_method_ptr do_float_dct;
- FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
-#endif
-} my_fdct_controller;
-
-typedef my_fdct_controller * my_fdct_ptr;
-
-
-/*
- * Initialize for a processing pass.
- * Verify that all referenced Q-tables are present, and set up
- * the divisor table for each one.
- * In the current implementation, DCT of all components is done during
- * the first pass, even if only some components will be output in the
- * first scan. Hence all components should be examined here.
- */
-
-METHODDEF(void)
-start_pass_fdctmgr (j_compress_ptr cinfo)
-{
- my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
- int ci, qtblno, i;
- jpeg_component_info *compptr;
- JQUANT_TBL * qtbl;
- DCTELEM * dtbl;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- qtblno = compptr->quant_tbl_no;
- /* Make sure specified quantization table is present */
- if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
- cinfo->quant_tbl_ptrs[qtblno] == NULL)
- ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
- qtbl = cinfo->quant_tbl_ptrs[qtblno];
- /* Compute divisors for this quant table */
- /* We may do this more than once for same table, but it's not a big deal */
- switch (cinfo->dct_method) {
-#ifdef DCT_ISLOW_SUPPORTED
- case JDCT_ISLOW:
- /* For LL&M IDCT method, divisors are equal to raw quantization
- * coefficients multiplied by 8 (to counteract scaling).
- */
- if (fdct->divisors[qtblno] == NULL) {
- fdct->divisors[qtblno] = (DCTELEM *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- DCTSIZE2 * SIZEOF(DCTELEM));
- }
- dtbl = fdct->divisors[qtblno];
- for (i = 0; i < DCTSIZE2; i++) {
- dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
- }
- break;
-#endif
-#ifdef DCT_IFAST_SUPPORTED
- case JDCT_IFAST:
- {
- /* For AA&N IDCT method, divisors are equal to quantization
- * coefficients scaled by scalefactor[row]*scalefactor[col], where
- * scalefactor[0] = 1
- * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
- * We apply a further scale factor of 8.
- */
-#define CONST_BITS 14
- static const INT16 aanscales[DCTSIZE2] = {
- /* precomputed values scaled up by 14 bits */
- 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
- 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
- 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
- 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
- 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
- 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
- 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
- 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
- };
- SHIFT_TEMPS
-
- if (fdct->divisors[qtblno] == NULL) {
- fdct->divisors[qtblno] = (DCTELEM *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- DCTSIZE2 * SIZEOF(DCTELEM));
- }
- dtbl = fdct->divisors[qtblno];
- for (i = 0; i < DCTSIZE2; i++) {
- dtbl[i] = (DCTELEM)
- DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
- (INT32) aanscales[i]),
- CONST_BITS-3);
- }
- }
- break;
-#endif
-#ifdef DCT_FLOAT_SUPPORTED
- case JDCT_FLOAT:
- {
- /* For float AA&N IDCT method, divisors are equal to quantization
- * coefficients scaled by scalefactor[row]*scalefactor[col], where
- * scalefactor[0] = 1
- * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
- * We apply a further scale factor of 8.
- * What's actually stored is 1/divisor so that the inner loop can
- * use a multiplication rather than a division.
- */
- FAST_FLOAT * fdtbl;
- int row, col;
- static const double aanscalefactor[DCTSIZE] = {
- 1.0, 1.387039845, 1.306562965, 1.175875602,
- 1.0, 0.785694958, 0.541196100, 0.275899379
- };
-
- if (fdct->float_divisors[qtblno] == NULL) {
- fdct->float_divisors[qtblno] = (FAST_FLOAT *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- DCTSIZE2 * SIZEOF(FAST_FLOAT));
- }
- fdtbl = fdct->float_divisors[qtblno];
- i = 0;
- for (row = 0; row < DCTSIZE; row++) {
- for (col = 0; col < DCTSIZE; col++) {
- fdtbl[i] = (FAST_FLOAT)
- (1.0 / (((double) qtbl->quantval[i] *
- aanscalefactor[row] * aanscalefactor[col] * 8.0)));
- i++;
- }
- }
- }
- break;
-#endif
- default:
- ERREXIT(cinfo, JERR_NOT_COMPILED);
- break;
- }
- }
-}
-
-
-/*
- * Perform forward DCT on one or more blocks of a component.
- *
- * The input samples are taken from the sample_data[] array starting at
- * position start_row/start_col, and moving to the right for any additional
- * blocks. The quantized coefficients are returned in coef_blocks[].
- */
-
-METHODDEF(void)
-forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
- JDIMENSION start_row, JDIMENSION start_col,
- JDIMENSION num_blocks)
-/* This version is used for integer DCT implementations. */
-{
- /* This routine is heavily used, so it's worth coding it tightly. */
- my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
- forward_DCT_method_ptr do_dct = fdct->do_dct;
- DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
- DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
- JDIMENSION bi;
-
- sample_data += start_row; /* fold in the vertical offset once */
-
- for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
- /* Load data into workspace, applying unsigned->signed conversion */
- { register DCTELEM *workspaceptr;
- register JSAMPROW elemptr;
- register int elemr;
-
- workspaceptr = workspace;
- for (elemr = 0; elemr < DCTSIZE; elemr++) {
- elemptr = sample_data[elemr] + start_col;
-#if DCTSIZE == 8 /* unroll the inner loop */
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
-#else
- { register int elemc;
- for (elemc = DCTSIZE; elemc > 0; elemc--) {
- *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
- }
- }
-#endif
- }
- }
-
- /* Perform the DCT */
- (*do_dct) (workspace);
-
- /* Quantize/descale the coefficients, and store into coef_blocks[] */
- { register DCTELEM temp, qval;
- register int i;
- register JCOEFPTR output_ptr = coef_blocks[bi];
-
- for (i = 0; i < DCTSIZE2; i++) {
- qval = divisors[i];
- temp = workspace[i];
- /* Divide the coefficient value by qval, ensuring proper rounding.
- * Since C does not specify the direction of rounding for negative
- * quotients, we have to force the dividend positive for portability.
- *
- * In most files, at least half of the output values will be zero
- * (at default quantization settings, more like three-quarters...)
- * so we should ensure that this case is fast. On many machines,
- * a comparison is enough cheaper than a divide to make a special test
- * a win. Since both inputs will be nonnegative, we need only test
- * for a < b to discover whether a/b is 0.
- * If your machine's division is fast enough, define FAST_DIVIDE.
- */
-#ifdef FAST_DIVIDE
-#define DIVIDE_BY(a,b) a /= b
-#else
-#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
-#endif
- if (temp < 0) {
- temp = -temp;
- temp += qval>>1; /* for rounding */
- DIVIDE_BY(temp, qval);
- temp = -temp;
- } else {
- temp += qval>>1; /* for rounding */
- DIVIDE_BY(temp, qval);
- }
- output_ptr[i] = (JCOEF) temp;
- }
- }
- }
-}
-
-
-#ifdef DCT_FLOAT_SUPPORTED
-
-METHODDEF(void)
-forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
- JDIMENSION start_row, JDIMENSION start_col,
- JDIMENSION num_blocks)
-/* This version is used for floating-point DCT implementations. */
-{
- /* This routine is heavily used, so it's worth coding it tightly. */
- my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
- float_DCT_method_ptr do_dct = fdct->do_float_dct;
- FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
- FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
- JDIMENSION bi;
-
- sample_data += start_row; /* fold in the vertical offset once */
-
- for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
- /* Load data into workspace, applying unsigned->signed conversion */
- { register FAST_FLOAT *workspaceptr;
- register JSAMPROW elemptr;
- register int elemr;
-
- workspaceptr = workspace;
- for (elemr = 0; elemr < DCTSIZE; elemr++) {
- elemptr = sample_data[elemr] + start_col;
-#if DCTSIZE == 8 /* unroll the inner loop */
- *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
- *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
- *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
- *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
- *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
- *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
- *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
- *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
-#else
- { register int elemc;
- for (elemc = DCTSIZE; elemc > 0; elemc--) {
- *workspaceptr++ = (FAST_FLOAT)
- (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
- }
- }
-#endif
- }
- }
-
- /* Perform the DCT */
- (*do_dct) (workspace);
-
- /* Quantize/descale the coefficients, and store into coef_blocks[] */
- { register FAST_FLOAT temp;
- register int i;
- register JCOEFPTR output_ptr = coef_blocks[bi];
-
- for (i = 0; i < DCTSIZE2; i++) {
- /* Apply the quantization and scaling factor */
- temp = workspace[i] * divisors[i];
- /* Round to nearest integer.
- * Since C does not specify the direction of rounding for negative
- * quotients, we have to force the dividend positive for portability.
- * The maximum coefficient size is +-16K (for 12-bit data), so this
- * code should work for either 16-bit or 32-bit ints.
- */
- output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
- }
- }
- }
-}
-
-#endif /* DCT_FLOAT_SUPPORTED */
-
-
-/*
- * Initialize FDCT manager.
- */
-
-GLOBAL(void)
-jinit_forward_dct (j_compress_ptr cinfo)
-{
- my_fdct_ptr fdct;
- int i;
-
- fdct = (my_fdct_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_fdct_controller));
- cinfo->fdct = (struct jpeg_forward_dct *) fdct;
- fdct->pub.start_pass = start_pass_fdctmgr;
-
- switch (cinfo->dct_method) {
-#ifdef DCT_ISLOW_SUPPORTED
- case JDCT_ISLOW:
- fdct->pub.forward_DCT = forward_DCT;
- fdct->do_dct = jpeg_fdct_islow;
- break;
-#endif
-#ifdef DCT_IFAST_SUPPORTED
- case JDCT_IFAST:
- fdct->pub.forward_DCT = forward_DCT;
- fdct->do_dct = jpeg_fdct_ifast;
- break;
-#endif
-#ifdef DCT_FLOAT_SUPPORTED
- case JDCT_FLOAT:
- fdct->pub.forward_DCT = forward_DCT_float;
- fdct->do_float_dct = jpeg_fdct_float;
- break;
-#endif
- default:
- ERREXIT(cinfo, JERR_NOT_COMPILED);
- break;
- }
-
- /* Mark divisor tables unallocated */
- for (i = 0; i < NUM_QUANT_TBLS; i++) {
- fdct->divisors[i] = NULL;
-#ifdef DCT_FLOAT_SUPPORTED
- fdct->float_divisors[i] = NULL;
-#endif
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcdctmgr.c
+ *
+ * Copyright (C) 1994-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 contains the forward-DCT management logic.
+ * This code selects a particular DCT implementation to be used,
+ * and it performs related housekeeping chores including coefficient
+ * quantization.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+
+/* Private subobject for this module */
+
+typedef struct {
+ struct jpeg_forward_dct pub; /* public fields */
+
+ /* Pointer to the DCT routine actually in use */
+ forward_DCT_method_ptr do_dct;
+
+ /* The actual post-DCT divisors --- not identical to the quant table
+ * entries, because of scaling (especially for an unnormalized DCT).
+ * Each table is given in normal array order.
+ */
+ DCTELEM * divisors[NUM_QUANT_TBLS];
+
+#ifdef DCT_FLOAT_SUPPORTED
+ /* Same as above for the floating-point case. */
+ float_DCT_method_ptr do_float_dct;
+ FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
+#endif
+} my_fdct_controller;
+
+typedef my_fdct_controller * my_fdct_ptr;
+
+
+/*
+ * Initialize for a processing pass.
+ * Verify that all referenced Q-tables are present, and set up
+ * the divisor table for each one.
+ * In the current implementation, DCT of all components is done during
+ * the first pass, even if only some components will be output in the
+ * first scan. Hence all components should be examined here.
+ */
+
+METHODDEF(void)
+start_pass_fdctmgr (j_compress_ptr cinfo)
+{
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+ int ci, qtblno, i;
+ jpeg_component_info *compptr;
+ JQUANT_TBL * qtbl;
+ DCTELEM * dtbl;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ qtblno = compptr->quant_tbl_no;
+ /* Make sure specified quantization table is present */
+ if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
+ cinfo->quant_tbl_ptrs[qtblno] == NULL)
+ ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
+ qtbl = cinfo->quant_tbl_ptrs[qtblno];
+ /* Compute divisors for this quant table */
+ /* We may do this more than once for same table, but it's not a big deal */
+ switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+ case JDCT_ISLOW:
+ /* For LL&M IDCT method, divisors are equal to raw quantization
+ * coefficients multiplied by 8 (to counteract scaling).
+ */
+ if (fdct->divisors[qtblno] == NULL) {
+ fdct->divisors[qtblno] = (DCTELEM *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ DCTSIZE2 * SIZEOF(DCTELEM));
+ }
+ dtbl = fdct->divisors[qtblno];
+ for (i = 0; i < DCTSIZE2; i++) {
+ dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
+ }
+ break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ case JDCT_IFAST:
+ {
+ /* For AA&N IDCT method, divisors are equal to quantization
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
+ * scalefactor[0] = 1
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
+ * We apply a further scale factor of 8.
+ */
+#define CONST_BITS 14
+ static const INT16 aanscales[DCTSIZE2] = {
+ /* precomputed values scaled up by 14 bits */
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
+ 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
+ 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
+ 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
+ 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
+ 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
+ 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
+ };
+ SHIFT_TEMPS
+
+ if (fdct->divisors[qtblno] == NULL) {
+ fdct->divisors[qtblno] = (DCTELEM *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ DCTSIZE2 * SIZEOF(DCTELEM));
+ }
+ dtbl = fdct->divisors[qtblno];
+ for (i = 0; i < DCTSIZE2; i++) {
+ dtbl[i] = (DCTELEM)
+ DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
+ (INT32) aanscales[i]),
+ CONST_BITS-3);
+ }
+ }
+ break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ case JDCT_FLOAT:
+ {
+ /* For float AA&N IDCT method, divisors are equal to quantization
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
+ * scalefactor[0] = 1
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
+ * We apply a further scale factor of 8.
+ * What's actually stored is 1/divisor so that the inner loop can
+ * use a multiplication rather than a division.
+ */
+ FAST_FLOAT * fdtbl;
+ int row, col;
+ static const double aanscalefactor[DCTSIZE] = {
+ 1.0, 1.387039845, 1.306562965, 1.175875602,
+ 1.0, 0.785694958, 0.541196100, 0.275899379
+ };
+
+ if (fdct->float_divisors[qtblno] == NULL) {
+ fdct->float_divisors[qtblno] = (FAST_FLOAT *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ DCTSIZE2 * SIZEOF(FAST_FLOAT));
+ }
+ fdtbl = fdct->float_divisors[qtblno];
+ i = 0;
+ for (row = 0; row < DCTSIZE; row++) {
+ for (col = 0; col < DCTSIZE; col++) {
+ fdtbl[i] = (FAST_FLOAT)
+ (1.0 / (((double) qtbl->quantval[i] *
+ aanscalefactor[row] * aanscalefactor[col] * 8.0)));
+ i++;
+ }
+ }
+ }
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+ }
+}
+
+
+/*
+ * Perform forward DCT on one or more blocks of a component.
+ *
+ * The input samples are taken from the sample_data[] array starting at
+ * position start_row/start_col, and moving to the right for any additional
+ * blocks. The quantized coefficients are returned in coef_blocks[].
+ */
+
+METHODDEF(void)
+forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+ JDIMENSION start_row, JDIMENSION start_col,
+ JDIMENSION num_blocks)
+/* This version is used for integer DCT implementations. */
+{
+ /* This routine is heavily used, so it's worth coding it tightly. */
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+ forward_DCT_method_ptr do_dct = fdct->do_dct;
+ DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
+ DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
+ JDIMENSION bi;
+
+ sample_data += start_row; /* fold in the vertical offset once */
+
+ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
+ /* Load data into workspace, applying unsigned->signed conversion */
+ { register DCTELEM *workspaceptr;
+ register JSAMPROW elemptr;
+ register int elemr;
+
+ workspaceptr = workspace;
+ for (elemr = 0; elemr < DCTSIZE; elemr++) {
+ elemptr = sample_data[elemr] + start_col;
+#if DCTSIZE == 8 /* unroll the inner loop */
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+#else
+ { register int elemc;
+ for (elemc = DCTSIZE; elemc > 0; elemc--) {
+ *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
+ }
+ }
+#endif
+ }
+ }
+
+ /* Perform the DCT */
+ (*do_dct) (workspace);
+
+ /* Quantize/descale the coefficients, and store into coef_blocks[] */
+ { register DCTELEM temp, qval;
+ register int i;
+ register JCOEFPTR output_ptr = coef_blocks[bi];
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ qval = divisors[i];
+ temp = workspace[i];
+ /* Divide the coefficient value by qval, ensuring proper rounding.
+ * Since C does not specify the direction of rounding for negative
+ * quotients, we have to force the dividend positive for portability.
+ *
+ * In most files, at least half of the output values will be zero
+ * (at default quantization settings, more like three-quarters...)
+ * so we should ensure that this case is fast. On many machines,
+ * a comparison is enough cheaper than a divide to make a special test
+ * a win. Since both inputs will be nonnegative, we need only test
+ * for a < b to discover whether a/b is 0.
+ * If your machine's division is fast enough, define FAST_DIVIDE.
+ */
+#ifdef FAST_DIVIDE
+#define DIVIDE_BY(a,b) a /= b
+#else
+#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
+#endif
+ if (temp < 0) {
+ temp = -temp;
+ temp += qval>>1; /* for rounding */
+ DIVIDE_BY(temp, qval);
+ temp = -temp;
+ } else {
+ temp += qval>>1; /* for rounding */
+ DIVIDE_BY(temp, qval);
+ }
+ output_ptr[i] = (JCOEF) temp;
+ }
+ }
+ }
+}
+
+
+#ifdef DCT_FLOAT_SUPPORTED
+
+METHODDEF(void)
+forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+ JDIMENSION start_row, JDIMENSION start_col,
+ JDIMENSION num_blocks)
+/* This version is used for floating-point DCT implementations. */
+{
+ /* This routine is heavily used, so it's worth coding it tightly. */
+ my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
+ float_DCT_method_ptr do_dct = fdct->do_float_dct;
+ FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
+ FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
+ JDIMENSION bi;
+
+ sample_data += start_row; /* fold in the vertical offset once */
+
+ for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
+ /* Load data into workspace, applying unsigned->signed conversion */
+ { register FAST_FLOAT *workspaceptr;
+ register JSAMPROW elemptr;
+ register int elemr;
+
+ workspaceptr = workspace;
+ for (elemr = 0; elemr < DCTSIZE; elemr++) {
+ elemptr = sample_data[elemr] + start_col;
+#if DCTSIZE == 8 /* unroll the inner loop */
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+#else
+ { register int elemc;
+ for (elemc = DCTSIZE; elemc > 0; elemc--) {
+ *workspaceptr++ = (FAST_FLOAT)
+ (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
+ }
+ }
+#endif
+ }
+ }
+
+ /* Perform the DCT */
+ (*do_dct) (workspace);
+
+ /* Quantize/descale the coefficients, and store into coef_blocks[] */
+ { register FAST_FLOAT temp;
+ register int i;
+ register JCOEFPTR output_ptr = coef_blocks[bi];
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ /* Apply the quantization and scaling factor */
+ temp = workspace[i] * divisors[i];
+ /* Round to nearest integer.
+ * Since C does not specify the direction of rounding for negative
+ * quotients, we have to force the dividend positive for portability.
+ * The maximum coefficient size is +-16K (for 12-bit data), so this
+ * code should work for either 16-bit or 32-bit ints.
+ */
+ output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
+ }
+ }
+ }
+}
+
+#endif /* DCT_FLOAT_SUPPORTED */
+
+
+/*
+ * Initialize FDCT manager.
+ */
+
+GLOBAL(void)
+jinit_forward_dct (j_compress_ptr cinfo)
+{
+ my_fdct_ptr fdct;
+ int i;
+
+ fdct = (my_fdct_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_fdct_controller));
+ cinfo->fdct = (struct jpeg_forward_dct *) fdct;
+ fdct->pub.start_pass = start_pass_fdctmgr;
+
+ switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+ case JDCT_ISLOW:
+ fdct->pub.forward_DCT = forward_DCT;
+ fdct->do_dct = jpeg_fdct_islow;
+ break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ case JDCT_IFAST:
+ fdct->pub.forward_DCT = forward_DCT;
+ fdct->do_dct = jpeg_fdct_ifast;
+ break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ case JDCT_FLOAT:
+ fdct->pub.forward_DCT = forward_DCT_float;
+ fdct->do_float_dct = jpeg_fdct_float;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+
+ /* Mark divisor tables unallocated */
+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
+ fdct->divisors[i] = NULL;
+#ifdef DCT_FLOAT_SUPPORTED
+ fdct->float_divisors[i] = NULL;
+#endif
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jchuff.c b/core/src/fxcodec/libjpeg/fpdfapi_jchuff.c
index 9ed1fe6da2..2a65aa253b 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jchuff.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jchuff.c
@@ -1,915 +1,915 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jchuff.c
- *
- * 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 Huffman entropy encoding routines.
- *
- * Much of the complexity here has to do with supporting output suspension.
- * If the data destination 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 JPEG objects only upon successful completion of an MCU.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jchuff.h" /* Declarations shared with jcphuff.c */
-
-#ifdef _FX_MANAGED_CODE_
-#define savable_state savable_state_c
-#endif
-
-/* Expanded entropy encoder object for Huffman encoding.
- *
- * The savable_state subrecord contains fields that change within an MCU,
- * but must not be updated permanently until we complete the MCU.
- */
-
-typedef struct {
- INT32 put_buffer; /* current bit-accumulation buffer */
- int put_bits; /* # of bits now in it */
- 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).put_buffer = (src).put_buffer, \
- (dest).put_bits = (src).put_bits, \
- (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_encoder pub; /* public fields */
-
- savable_state saved; /* Bit buffer & DC 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 */
- int next_restart_num; /* next restart number to write (0-7) */
-
- /* Pointers to derived tables (these workspaces have image lifespan) */
- c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
- c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
-
-#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
- long * dc_count_ptrs[NUM_HUFF_TBLS];
- long * ac_count_ptrs[NUM_HUFF_TBLS];
-#endif
-} huff_entropy_encoder;
-
-typedef huff_entropy_encoder * huff_entropy_ptr;
-
-/* Working state while writing an MCU.
- * This struct contains all the fields that are needed by subroutines.
- */
-
-typedef struct {
- JOCTET * next_output_byte; /* => next byte to write in buffer */
- size_t free_in_buffer; /* # of byte spaces remaining in buffer */
- savable_state cur; /* Current bit buffer & DC state */
- j_compress_ptr cinfo; /* dump_buffer needs access to this */
-} working_state;
-
-
-/* Forward declarations */
-METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
- JBLOCKROW *MCU_data));
-METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
-#ifdef ENTROPY_OPT_SUPPORTED
-METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
- JBLOCKROW *MCU_data));
-METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
-#endif
-
-
-/*
- * Initialize for a Huffman-compressed scan.
- * If gather_statistics is TRUE, we do not output anything during the scan,
- * just count the Huffman symbols used and generate Huffman code tables.
- */
-
-METHODDEF(void)
-start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
-{
- huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
- int ci, dctbl, actbl;
- jpeg_component_info * compptr;
-
- if (gather_statistics) {
-#ifdef ENTROPY_OPT_SUPPORTED
- entropy->pub.encode_mcu = encode_mcu_gather;
- entropy->pub.finish_pass = finish_pass_gather;
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else {
- entropy->pub.encode_mcu = encode_mcu_huff;
- entropy->pub.finish_pass = finish_pass_huff;
- }
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- dctbl = compptr->dc_tbl_no;
- actbl = compptr->ac_tbl_no;
- if (gather_statistics) {
-#ifdef ENTROPY_OPT_SUPPORTED
- /* Check for invalid table indexes */
- /* (make_c_derived_tbl does this in the other path) */
- if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
- ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
- if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
- ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
- /* Allocate and zero the statistics tables */
- /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
- if (entropy->dc_count_ptrs[dctbl] == NULL)
- entropy->dc_count_ptrs[dctbl] = (long *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- 257 * SIZEOF(long));
- MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
- if (entropy->ac_count_ptrs[actbl] == NULL)
- entropy->ac_count_ptrs[actbl] = (long *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- 257 * SIZEOF(long));
- MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
-#endif
- } else {
- /* Compute derived values for Huffman tables */
- /* We may do this more than once for a table, but it's not expensive */
- jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
- & entropy->dc_derived_tbls[dctbl]);
- jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
- & entropy->ac_derived_tbls[actbl]);
- }
- /* Initialize DC predictions to 0 */
- entropy->saved.last_dc_val[ci] = 0;
- }
-
- /* Initialize bit buffer to empty */
- entropy->saved.put_buffer = 0;
- entropy->saved.put_bits = 0;
-
- /* Initialize restart stuff */
- entropy->restarts_to_go = cinfo->restart_interval;
- entropy->next_restart_num = 0;
-}
-
-
-/*
- * Compute the derived values for a Huffman table.
- * This routine also performs some validation checks on the table.
- *
- * Note this is also used by jcphuff.c.
- */
-
-GLOBAL(void)
-jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
- c_derived_tbl ** pdtbl)
-{
- JHUFF_TBL *htbl;
- c_derived_tbl *dtbl;
- int p, i, l, lastp, _si, maxsymbol;
- char huffsize[257];
- unsigned int huffcode[257];
- unsigned int code;
-
- /* Note that huffsize[] and huffcode[] are filled in code-length order,
- * paralleling the order of the symbols themselves in htbl->huffval[].
- */
-
- /* Find the input Huffman table */
- if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
- ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
- htbl =
- isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
- if (htbl == NULL)
- ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
-
- /* Allocate a workspace if we haven't already done so. */
- if (*pdtbl == NULL)
- *pdtbl = (c_derived_tbl *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(c_derived_tbl));
- dtbl = *pdtbl;
-
- /* Figure C.1: make table of Huffman code length for each symbol */
-
- p = 0;
- for (l = 1; l <= 16; l++) {
- i = (int) htbl->bits[l];
- if (i < 0 || p + i > 256) /* protect against table overrun */
- ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
- while (i--)
- huffsize[p++] = (char) l;
- }
- huffsize[p] = 0;
- lastp = p;
-
- /* Figure C.2: generate the codes themselves */
- /* We also validate that the counts represent a legal Huffman code tree. */
-
- code = 0;
- _si = huffsize[0];
- p = 0;
- while (huffsize[p]) {
- while (((int) huffsize[p]) == _si) {
- huffcode[p++] = code;
- code++;
- }
- /* code is now 1 more than the last code used for codelength si; but
- * it must still fit in si bits, since no code is allowed to be all ones.
- */
- if (((INT32) code) >= (((INT32) 1) << _si))
- ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
- code <<= 1;
- _si++;
- }
-
- /* Figure C.3: generate encoding tables */
- /* These are code and size indexed by symbol value */
-
- /* Set all codeless symbols to have code length 0;
- * this lets us detect duplicate VAL entries here, and later
- * allows emit_bits to detect any attempt to emit such symbols.
- */
- MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
-
- /* This is also a convenient place to check for out-of-range
- * and duplicated VAL entries. We allow 0..255 for AC symbols
- * but only 0..15 for DC. (We could constrain them further
- * based on data depth and mode, but this seems enough.)
- */
- maxsymbol = isDC ? 15 : 255;
-
- for (p = 0; p < lastp; p++) {
- i = htbl->huffval[p];
- if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
- ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
- dtbl->ehufco[i] = huffcode[p];
- dtbl->ehufsi[i] = huffsize[p];
- }
-}
-
-
-/* Outputting bytes to the file */
-
-/* Emit a byte, taking 'action' if must suspend. */
-#define emit_byte(state,val,action) \
- { *(state)->next_output_byte++ = (JOCTET) (val); \
- if (--(state)->free_in_buffer == 0) \
- if (! dump_buffer(state)) \
- { action; } }
-
-
-LOCAL(boolean)
-dump_buffer (working_state * state)
-/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
-{
- struct jpeg_destination_mgr * dest = state->cinfo->dest;
-
- if (! (*dest->empty_output_buffer) (state->cinfo))
- return FALSE;
- /* After a successful buffer dump, must reset buffer pointers */
- state->next_output_byte = dest->next_output_byte;
- state->free_in_buffer = dest->free_in_buffer;
- return TRUE;
-}
-
-
-/* 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(boolean)
-emit_bits (working_state * state, unsigned int code, int size)
-/* Emit some bits; return TRUE if successful, FALSE if must suspend */
-{
- /* This routine is heavily used, so it's worth coding tightly. */
- register INT32 put_buffer = (INT32) code;
- register int put_bits = state->cur.put_bits;
-
- /* if size is 0, caller used an invalid Huffman table entry */
- if (size == 0)
- ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
-
- 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 |= state->cur.put_buffer; /* and merge with old buffer contents */
-
- while (put_bits >= 8) {
- int c = (int) ((put_buffer >> 16) & 0xFF);
-
- emit_byte(state, c, return FALSE);
- if (c == 0xFF) { /* need to stuff a zero byte? */
- emit_byte(state, 0, return FALSE);
- }
- put_buffer <<= 8;
- put_bits -= 8;
- }
-
- state->cur.put_buffer = put_buffer; /* update state variables */
- state->cur.put_bits = put_bits;
-
- return TRUE;
-}
-
-
-LOCAL(boolean)
-flush_bits (working_state * state)
-{
- if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
- return FALSE;
- state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
- state->cur.put_bits = 0;
- return TRUE;
-}
-
-
-/* Encode a single block's worth of coefficients */
-
-LOCAL(boolean)
-encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
- c_derived_tbl *dctbl, c_derived_tbl *actbl)
-{
- register int temp, temp2;
- register int nbits;
- register int k, r, i;
-
- /* Encode the DC coefficient difference per section F.1.2.1 */
-
- temp = temp2 = block[0] - last_dc_val;
-
- 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(state->cinfo, JERR_BAD_DCT_COEF);
-
- /* Emit the Huffman-coded symbol for the number of bits */
- if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
- return FALSE;
-
- /* 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 */
- if (! emit_bits(state, (unsigned int) temp2, nbits))
- return FALSE;
-
- /* Encode the AC coefficients per section F.1.2.2 */
-
- r = 0; /* r = run length of zeros */
-
- for (k = 1; k < DCTSIZE2; k++) {
- if ((temp = block[jpeg_natural_order[k]]) == 0) {
- r++;
- } else {
- /* if run length > 15, must emit special run-length-16 codes (0xF0) */
- while (r > 15) {
- if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
- return FALSE;
- r -= 16;
- }
-
- temp2 = temp;
- if (temp < 0) {
- temp = -temp; /* temp is abs value of 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 = 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(state->cinfo, JERR_BAD_DCT_COEF);
-
- /* Emit Huffman symbol for run length / number of bits */
- i = (r << 4) + nbits;
- if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
- return FALSE;
-
- /* Emit that number of bits of the value, if positive, */
- /* or the complement of its magnitude, if negative. */
- if (! emit_bits(state, (unsigned int) temp2, nbits))
- return FALSE;
-
- r = 0;
- }
- }
-
- /* If the last coef(s) were zero, emit an end-of-block code */
- if (r > 0)
- if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
- return FALSE;
-
- return TRUE;
-}
-
-
-/*
- * Emit a restart marker & resynchronize predictions.
- */
-
-LOCAL(boolean)
-emit_restart (working_state * state, int restart_num)
-{
- int ci;
-
- if (! flush_bits(state))
- return FALSE;
-
- emit_byte(state, 0xFF, return FALSE);
- emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
-
- /* Re-initialize DC predictions to 0 */
- for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
- state->cur.last_dc_val[ci] = 0;
-
- /* The restart counter is not updated until we successfully write the MCU. */
-
- return TRUE;
-}
-
-
-/*
- * Encode and output one MCU's worth of Huffman-compressed coefficients.
- */
-
-METHODDEF(boolean)
-encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
-{
- huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
- working_state state;
- int blkn, ci;
- jpeg_component_info * compptr;
-
- /* Load up working state */
- state.next_output_byte = cinfo->dest->next_output_byte;
- state.free_in_buffer = cinfo->dest->free_in_buffer;
- ASSIGN_STATE(state.cur, entropy->saved);
- state.cinfo = cinfo;
-
- /* Emit restart marker if needed */
- if (cinfo->restart_interval) {
- if (entropy->restarts_to_go == 0)
- if (! emit_restart(&state, entropy->next_restart_num))
- return FALSE;
- }
-
- /* Encode the MCU data blocks */
- for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
- ci = cinfo->MCU_membership[blkn];
- compptr = cinfo->cur_comp_info[ci];
- if (! encode_one_block(&state,
- MCU_data[blkn][0], state.cur.last_dc_val[ci],
- entropy->dc_derived_tbls[compptr->dc_tbl_no],
- entropy->ac_derived_tbls[compptr->ac_tbl_no]))
- return FALSE;
- /* Update last_dc_val */
- state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
- }
-
- /* Completed MCU, so update state */
- cinfo->dest->next_output_byte = state.next_output_byte;
- cinfo->dest->free_in_buffer = state.free_in_buffer;
- ASSIGN_STATE(entropy->saved, state.cur);
-
- /* 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 scan.
- */
-
-METHODDEF(void)
-finish_pass_huff (j_compress_ptr cinfo)
-{
- huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
- working_state state;
-
- /* Load up working state ... flush_bits needs it */
- state.next_output_byte = cinfo->dest->next_output_byte;
- state.free_in_buffer = cinfo->dest->free_in_buffer;
- ASSIGN_STATE(state.cur, entropy->saved);
- state.cinfo = cinfo;
-
- /* Flush out the last data */
- if (! flush_bits(&state))
- ERREXIT(cinfo, JERR_CANT_SUSPEND);
-
- /* Update state */
- cinfo->dest->next_output_byte = state.next_output_byte;
- cinfo->dest->free_in_buffer = state.free_in_buffer;
- ASSIGN_STATE(entropy->saved, state.cur);
-}
-
-
-/*
- * Huffman coding optimization.
- *
- * We first scan the supplied data and count the number of uses of each symbol
- * that is to be Huffman-coded. (This process MUST agree with the code above.)
- * Then we build a Huffman coding tree for the observed counts.
- * Symbols which are not needed at all for the particular image are not
- * assigned any code, which saves space in the DHT marker as well as in
- * the compressed data.
- */
-
-#ifdef ENTROPY_OPT_SUPPORTED
-
-
-/* Process a single block's worth of coefficients */
-
-LOCAL(void)
-htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
- long dc_counts[], long ac_counts[])
-{
- register int temp;
- register int nbits;
- register int k, r;
-
- /* Encode the DC coefficient difference per section F.1.2.1 */
-
- temp = block[0] - last_dc_val;
- if (temp < 0)
- temp = -temp;
-
- /* 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 the Huffman symbol for the number of bits */
- dc_counts[nbits]++;
-
- /* Encode the AC coefficients per section F.1.2.2 */
-
- r = 0; /* r = run length of zeros */
-
- for (k = 1; k < DCTSIZE2; k++) {
- if ((temp = block[jpeg_natural_order[k]]) == 0) {
- r++;
- } else {
- /* if run length > 15, must emit special run-length-16 codes (0xF0) */
- while (r > 15) {
- ac_counts[0xF0]++;
- r -= 16;
- }
-
- /* Find the number of bits needed for the magnitude of the coefficient */
- if (temp < 0)
- temp = -temp;
-
- /* 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 Huffman symbol for run length / number of bits */
- ac_counts[(r << 4) + nbits]++;
-
- r = 0;
- }
- }
-
- /* If the last coef(s) were zero, emit an end-of-block code */
- if (r > 0)
- ac_counts[0]++;
-}
-
-
-/*
- * Trial-encode one MCU's worth of Huffman-compressed coefficients.
- * No data is actually output, so no suspension return is possible.
- */
-
-METHODDEF(boolean)
-encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
-{
- huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
- int blkn, ci;
- jpeg_component_info * compptr;
-
- /* Take care of restart intervals if needed */
- if (cinfo->restart_interval) {
- if (entropy->restarts_to_go == 0) {
- /* Re-initialize DC predictions to 0 */
- for (ci = 0; ci < cinfo->comps_in_scan; ci++)
- entropy->saved.last_dc_val[ci] = 0;
- /* Update restart state */
- entropy->restarts_to_go = cinfo->restart_interval;
- }
- entropy->restarts_to_go--;
- }
-
- for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
- ci = cinfo->MCU_membership[blkn];
- compptr = cinfo->cur_comp_info[ci];
- htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
- entropy->dc_count_ptrs[compptr->dc_tbl_no],
- entropy->ac_count_ptrs[compptr->ac_tbl_no]);
- entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
- }
-
- return TRUE;
-}
-
-
-/*
- * Generate the best Huffman code table for the given counts, fill htbl.
- * Note this is also used by jcphuff.c.
- *
- * The JPEG standard requires that no symbol be assigned a codeword of all
- * one bits (so that padding bits added at the end of a compressed segment
- * can't look like a valid code). Because of the canonical ordering of
- * codewords, this just means that there must be an unused slot in the
- * longest codeword length category. Section K.2 of the JPEG spec suggests
- * reserving such a slot by pretending that symbol 256 is a valid symbol
- * with count 1. In theory that's not optimal; giving it count zero but
- * including it in the symbol set anyway should give a better Huffman code.
- * But the theoretically better code actually seems to come out worse in
- * practice, because it produces more all-ones bytes (which incur stuffed
- * zero bytes in the final file). In any case the difference is tiny.
- *
- * The JPEG standard requires Huffman codes to be no more than 16 bits long.
- * If some symbols have a very small but nonzero probability, the Huffman tree
- * must be adjusted to meet the code length restriction. We currently use
- * the adjustment method suggested in JPEG section K.2. This method is *not*
- * optimal; it may not choose the best possible limited-length code. But
- * typically only very-low-frequency symbols will be given less-than-optimal
- * lengths, so the code is almost optimal. Experimental comparisons against
- * an optimal limited-length-code algorithm indicate that the difference is
- * microscopic --- usually less than a hundredth of a percent of total size.
- * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
- */
-
-GLOBAL(void)
-jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
-{
-#define MAX_CLEN 32 /* assumed maximum initial code length */
- UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
- int codesize[257]; /* codesize[k] = code length of symbol k */
- int others[257]; /* next symbol in current branch of tree */
- int c1, c2;
- int p, i, j;
- long v;
-
- /* This algorithm is explained in section K.2 of the JPEG standard */
-
- MEMZERO(bits, SIZEOF(bits));
- MEMZERO(codesize, SIZEOF(codesize));
- for (i = 0; i < 257; i++)
- others[i] = -1; /* init links to empty */
-
- freq[256] = 1; /* make sure 256 has a nonzero count */
- /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
- * that no real symbol is given code-value of all ones, because 256
- * will be placed last in the largest codeword category.
- */
-
- /* Huffman's basic algorithm to assign optimal code lengths to symbols */
-
- for (;;) {
- /* Find the smallest nonzero frequency, set c1 = its symbol */
- /* In case of ties, take the larger symbol number */
- c1 = -1;
- v = 1000000000L;
- for (i = 0; i <= 256; i++) {
- if (freq[i] && freq[i] <= v) {
- v = freq[i];
- c1 = i;
- }
- }
-
- /* Find the next smallest nonzero frequency, set c2 = its symbol */
- /* In case of ties, take the larger symbol number */
- c2 = -1;
- v = 1000000000L;
- for (i = 0; i <= 256; i++) {
- if (freq[i] && freq[i] <= v && i != c1) {
- v = freq[i];
- c2 = i;
- }
- }
-
- /* Done if we've merged everything into one frequency */
- if (c2 < 0)
- break;
-
- /* Else merge the two counts/trees */
- freq[c1] += freq[c2];
- freq[c2] = 0;
-
- /* Increment the codesize of everything in c1's tree branch */
- codesize[c1]++;
- while (others[c1] >= 0) {
- c1 = others[c1];
- codesize[c1]++;
- }
-
- others[c1] = c2; /* chain c2 onto c1's tree branch */
-
- /* Increment the codesize of everything in c2's tree branch */
- codesize[c2]++;
- while (others[c2] >= 0) {
- c2 = others[c2];
- codesize[c2]++;
- }
- }
-
- /* Now count the number of symbols of each code length */
- for (i = 0; i <= 256; i++) {
- if (codesize[i]) {
- /* The JPEG standard seems to think that this can't happen, */
- /* but I'm paranoid... */
- if (codesize[i] > MAX_CLEN)
- ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
-
- bits[codesize[i]]++;
- }
- }
-
- /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
- * Huffman procedure assigned any such lengths, we must adjust the coding.
- * Here is what the JPEG spec says about how this next bit works:
- * Since symbols are paired for the longest Huffman code, the symbols are
- * removed from this length category two at a time. The prefix for the pair
- * (which is one bit shorter) is allocated to one of the pair; then,
- * skipping the BITS entry for that prefix length, a code word from the next
- * shortest nonzero BITS entry is converted into a prefix for two code words
- * one bit longer.
- */
-
- for (i = MAX_CLEN; i > 16; i--) {
- while (bits[i] > 0) {
- j = i - 2; /* find length of new prefix to be used */
- while (bits[j] == 0)
- j--;
-
- bits[i] -= 2; /* remove two symbols */
- bits[i-1]++; /* one goes in this length */
- bits[j+1] += 2; /* two new symbols in this length */
- bits[j]--; /* symbol of this length is now a prefix */
- }
- }
-
- /* Remove the count for the pseudo-symbol 256 from the largest codelength */
- while (bits[i] == 0) /* find largest codelength still in use */
- i--;
- bits[i]--;
-
- /* Return final symbol counts (only for lengths 0..16) */
- MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
-
- /* Return a list of the symbols sorted by code length */
- /* It's not real clear to me why we don't need to consider the codelength
- * changes made above, but the JPEG spec seems to think this works.
- */
- p = 0;
- for (i = 1; i <= MAX_CLEN; i++) {
- for (j = 0; j <= 255; j++) {
- if (codesize[j] == i) {
- htbl->huffval[p] = (UINT8) j;
- p++;
- }
- }
- }
-
- /* Set sent_table FALSE so updated table will be written to JPEG file. */
- htbl->sent_table = FALSE;
-}
-
-
-/*
- * Finish up a statistics-gathering pass and create the new Huffman tables.
- */
-
-METHODDEF(void)
-finish_pass_gather (j_compress_ptr cinfo)
-{
- huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
- int ci, dctbl, actbl;
- jpeg_component_info * compptr;
- JHUFF_TBL **htblptr;
- boolean did_dc[NUM_HUFF_TBLS];
- boolean did_ac[NUM_HUFF_TBLS];
-
- /* It's important not to apply jpeg_gen_optimal_table more than once
- * per table, because it clobbers the input frequency counts!
- */
- MEMZERO(did_dc, SIZEOF(did_dc));
- MEMZERO(did_ac, SIZEOF(did_ac));
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- dctbl = compptr->dc_tbl_no;
- actbl = compptr->ac_tbl_no;
- if (! did_dc[dctbl]) {
- htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
- if (*htblptr == NULL)
- *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
- jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
- did_dc[dctbl] = TRUE;
- }
- if (! did_ac[actbl]) {
- htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
- if (*htblptr == NULL)
- *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
- jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
- did_ac[actbl] = TRUE;
- }
- }
-}
-
-
-#endif /* ENTROPY_OPT_SUPPORTED */
-
-
-/*
- * Module initialization routine for Huffman entropy encoding.
- */
-
-GLOBAL(void)
-jinit_huff_encoder (j_compress_ptr cinfo)
-{
- huff_entropy_ptr entropy;
- int i;
-
- entropy = (huff_entropy_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(huff_entropy_encoder));
- cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
- entropy->pub.start_pass = start_pass_huff;
-
- /* Mark tables unallocated */
- for (i = 0; i < NUM_HUFF_TBLS; i++) {
- entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
-#ifdef ENTROPY_OPT_SUPPORTED
- entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
-#endif
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jchuff.c
+ *
+ * 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 Huffman entropy encoding routines.
+ *
+ * Much of the complexity here has to do with supporting output suspension.
+ * If the data destination 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 JPEG objects only upon successful completion of an MCU.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jchuff.h" /* Declarations shared with jcphuff.c */
+
+#ifdef _FX_MANAGED_CODE_
+#define savable_state savable_state_c
+#endif
+
+/* Expanded entropy encoder object for Huffman encoding.
+ *
+ * The savable_state subrecord contains fields that change within an MCU,
+ * but must not be updated permanently until we complete the MCU.
+ */
+
+typedef struct {
+ INT32 put_buffer; /* current bit-accumulation buffer */
+ int put_bits; /* # of bits now in it */
+ 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).put_buffer = (src).put_buffer, \
+ (dest).put_bits = (src).put_bits, \
+ (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_encoder pub; /* public fields */
+
+ savable_state saved; /* Bit buffer & DC 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 */
+ int next_restart_num; /* next restart number to write (0-7) */
+
+ /* Pointers to derived tables (these workspaces have image lifespan) */
+ c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
+ c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
+
+#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
+ long * dc_count_ptrs[NUM_HUFF_TBLS];
+ long * ac_count_ptrs[NUM_HUFF_TBLS];
+#endif
+} huff_entropy_encoder;
+
+typedef huff_entropy_encoder * huff_entropy_ptr;
+
+/* Working state while writing an MCU.
+ * This struct contains all the fields that are needed by subroutines.
+ */
+
+typedef struct {
+ JOCTET * next_output_byte; /* => next byte to write in buffer */
+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
+ savable_state cur; /* Current bit buffer & DC state */
+ j_compress_ptr cinfo; /* dump_buffer needs access to this */
+} working_state;
+
+
+/* Forward declarations */
+METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
+#ifdef ENTROPY_OPT_SUPPORTED
+METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
+#endif
+
+
+/*
+ * Initialize for a Huffman-compressed scan.
+ * If gather_statistics is TRUE, we do not output anything during the scan,
+ * just count the Huffman symbols used and generate Huffman code tables.
+ */
+
+METHODDEF(void)
+start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci, dctbl, actbl;
+ jpeg_component_info * compptr;
+
+ if (gather_statistics) {
+#ifdef ENTROPY_OPT_SUPPORTED
+ entropy->pub.encode_mcu = encode_mcu_gather;
+ entropy->pub.finish_pass = finish_pass_gather;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ entropy->pub.encode_mcu = encode_mcu_huff;
+ entropy->pub.finish_pass = finish_pass_huff;
+ }
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ dctbl = compptr->dc_tbl_no;
+ actbl = compptr->ac_tbl_no;
+ if (gather_statistics) {
+#ifdef ENTROPY_OPT_SUPPORTED
+ /* Check for invalid table indexes */
+ /* (make_c_derived_tbl does this in the other path) */
+ if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
+ if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
+ /* Allocate and zero the statistics tables */
+ /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
+ if (entropy->dc_count_ptrs[dctbl] == NULL)
+ entropy->dc_count_ptrs[dctbl] = (long *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ 257 * SIZEOF(long));
+ MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
+ if (entropy->ac_count_ptrs[actbl] == NULL)
+ entropy->ac_count_ptrs[actbl] = (long *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ 257 * SIZEOF(long));
+ MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
+#endif
+ } else {
+ /* Compute derived values for Huffman tables */
+ /* We may do this more than once for a table, but it's not expensive */
+ jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
+ & entropy->dc_derived_tbls[dctbl]);
+ jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
+ & entropy->ac_derived_tbls[actbl]);
+ }
+ /* Initialize DC predictions to 0 */
+ entropy->saved.last_dc_val[ci] = 0;
+ }
+
+ /* Initialize bit buffer to empty */
+ entropy->saved.put_buffer = 0;
+ entropy->saved.put_bits = 0;
+
+ /* Initialize restart stuff */
+ entropy->restarts_to_go = cinfo->restart_interval;
+ entropy->next_restart_num = 0;
+}
+
+
+/*
+ * Compute the derived values for a Huffman table.
+ * This routine also performs some validation checks on the table.
+ *
+ * Note this is also used by jcphuff.c.
+ */
+
+GLOBAL(void)
+jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
+ c_derived_tbl ** pdtbl)
+{
+ JHUFF_TBL *htbl;
+ c_derived_tbl *dtbl;
+ int p, i, l, lastp, _si, maxsymbol;
+ char huffsize[257];
+ unsigned int huffcode[257];
+ unsigned int code;
+
+ /* Note that huffsize[] and huffcode[] are filled in code-length order,
+ * paralleling the order of the symbols themselves in htbl->huffval[].
+ */
+
+ /* Find the input Huffman table */
+ if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+ htbl =
+ isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
+ if (htbl == NULL)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+
+ /* Allocate a workspace if we haven't already done so. */
+ if (*pdtbl == NULL)
+ *pdtbl = (c_derived_tbl *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(c_derived_tbl));
+ dtbl = *pdtbl;
+
+ /* Figure C.1: make table of Huffman code length for each symbol */
+
+ p = 0;
+ for (l = 1; l <= 16; l++) {
+ i = (int) htbl->bits[l];
+ if (i < 0 || p + i > 256) /* protect against table overrun */
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ while (i--)
+ huffsize[p++] = (char) l;
+ }
+ huffsize[p] = 0;
+ lastp = p;
+
+ /* Figure C.2: generate the codes themselves */
+ /* We also validate that the counts represent a legal Huffman code tree. */
+
+ code = 0;
+ _si = huffsize[0];
+ p = 0;
+ while (huffsize[p]) {
+ while (((int) huffsize[p]) == _si) {
+ huffcode[p++] = code;
+ code++;
+ }
+ /* code is now 1 more than the last code used for codelength si; but
+ * it must still fit in si bits, since no code is allowed to be all ones.
+ */
+ if (((INT32) code) >= (((INT32) 1) << _si))
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ code <<= 1;
+ _si++;
+ }
+
+ /* Figure C.3: generate encoding tables */
+ /* These are code and size indexed by symbol value */
+
+ /* Set all codeless symbols to have code length 0;
+ * this lets us detect duplicate VAL entries here, and later
+ * allows emit_bits to detect any attempt to emit such symbols.
+ */
+ MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
+
+ /* This is also a convenient place to check for out-of-range
+ * and duplicated VAL entries. We allow 0..255 for AC symbols
+ * but only 0..15 for DC. (We could constrain them further
+ * based on data depth and mode, but this seems enough.)
+ */
+ maxsymbol = isDC ? 15 : 255;
+
+ for (p = 0; p < lastp; p++) {
+ i = htbl->huffval[p];
+ if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ dtbl->ehufco[i] = huffcode[p];
+ dtbl->ehufsi[i] = huffsize[p];
+ }
+}
+
+
+/* Outputting bytes to the file */
+
+/* Emit a byte, taking 'action' if must suspend. */
+#define emit_byte(state,val,action) \
+ { *(state)->next_output_byte++ = (JOCTET) (val); \
+ if (--(state)->free_in_buffer == 0) \
+ if (! dump_buffer(state)) \
+ { action; } }
+
+
+LOCAL(boolean)
+dump_buffer (working_state * state)
+/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
+{
+ struct jpeg_destination_mgr * dest = state->cinfo->dest;
+
+ if (! (*dest->empty_output_buffer) (state->cinfo))
+ return FALSE;
+ /* After a successful buffer dump, must reset buffer pointers */
+ state->next_output_byte = dest->next_output_byte;
+ state->free_in_buffer = dest->free_in_buffer;
+ return TRUE;
+}
+
+
+/* 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(boolean)
+emit_bits (working_state * state, unsigned int code, int size)
+/* Emit some bits; return TRUE if successful, FALSE if must suspend */
+{
+ /* This routine is heavily used, so it's worth coding tightly. */
+ register INT32 put_buffer = (INT32) code;
+ register int put_bits = state->cur.put_bits;
+
+ /* if size is 0, caller used an invalid Huffman table entry */
+ if (size == 0)
+ ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
+
+ 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 |= state->cur.put_buffer; /* and merge with old buffer contents */
+
+ while (put_bits >= 8) {
+ int c = (int) ((put_buffer >> 16) & 0xFF);
+
+ emit_byte(state, c, return FALSE);
+ if (c == 0xFF) { /* need to stuff a zero byte? */
+ emit_byte(state, 0, return FALSE);
+ }
+ put_buffer <<= 8;
+ put_bits -= 8;
+ }
+
+ state->cur.put_buffer = put_buffer; /* update state variables */
+ state->cur.put_bits = put_bits;
+
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+flush_bits (working_state * state)
+{
+ if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
+ return FALSE;
+ state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
+ state->cur.put_bits = 0;
+ return TRUE;
+}
+
+
+/* Encode a single block's worth of coefficients */
+
+LOCAL(boolean)
+encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
+ c_derived_tbl *dctbl, c_derived_tbl *actbl)
+{
+ register int temp, temp2;
+ register int nbits;
+ register int k, r, i;
+
+ /* Encode the DC coefficient difference per section F.1.2.1 */
+
+ temp = temp2 = block[0] - last_dc_val;
+
+ 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(state->cinfo, JERR_BAD_DCT_COEF);
+
+ /* Emit the Huffman-coded symbol for the number of bits */
+ if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
+ return FALSE;
+
+ /* 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 */
+ if (! emit_bits(state, (unsigned int) temp2, nbits))
+ return FALSE;
+
+ /* Encode the AC coefficients per section F.1.2.2 */
+
+ r = 0; /* r = run length of zeros */
+
+ for (k = 1; k < DCTSIZE2; k++) {
+ if ((temp = block[jpeg_natural_order[k]]) == 0) {
+ r++;
+ } else {
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+ while (r > 15) {
+ if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
+ return FALSE;
+ r -= 16;
+ }
+
+ temp2 = temp;
+ if (temp < 0) {
+ temp = -temp; /* temp is abs value of 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 = 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(state->cinfo, JERR_BAD_DCT_COEF);
+
+ /* Emit Huffman symbol for run length / number of bits */
+ i = (r << 4) + nbits;
+ if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
+ return FALSE;
+
+ /* Emit that number of bits of the value, if positive, */
+ /* or the complement of its magnitude, if negative. */
+ if (! emit_bits(state, (unsigned int) temp2, nbits))
+ return FALSE;
+
+ r = 0;
+ }
+ }
+
+ /* If the last coef(s) were zero, emit an end-of-block code */
+ if (r > 0)
+ if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
+ return FALSE;
+
+ return TRUE;
+}
+
+
+/*
+ * Emit a restart marker & resynchronize predictions.
+ */
+
+LOCAL(boolean)
+emit_restart (working_state * state, int restart_num)
+{
+ int ci;
+
+ if (! flush_bits(state))
+ return FALSE;
+
+ emit_byte(state, 0xFF, return FALSE);
+ emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
+
+ /* Re-initialize DC predictions to 0 */
+ for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
+ state->cur.last_dc_val[ci] = 0;
+
+ /* The restart counter is not updated until we successfully write the MCU. */
+
+ return TRUE;
+}
+
+
+/*
+ * Encode and output one MCU's worth of Huffman-compressed coefficients.
+ */
+
+METHODDEF(boolean)
+encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ working_state state;
+ int blkn, ci;
+ jpeg_component_info * compptr;
+
+ /* Load up working state */
+ state.next_output_byte = cinfo->dest->next_output_byte;
+ state.free_in_buffer = cinfo->dest->free_in_buffer;
+ ASSIGN_STATE(state.cur, entropy->saved);
+ state.cinfo = cinfo;
+
+ /* Emit restart marker if needed */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0)
+ if (! emit_restart(&state, entropy->next_restart_num))
+ return FALSE;
+ }
+
+ /* Encode the MCU data blocks */
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ ci = cinfo->MCU_membership[blkn];
+ compptr = cinfo->cur_comp_info[ci];
+ if (! encode_one_block(&state,
+ MCU_data[blkn][0], state.cur.last_dc_val[ci],
+ entropy->dc_derived_tbls[compptr->dc_tbl_no],
+ entropy->ac_derived_tbls[compptr->ac_tbl_no]))
+ return FALSE;
+ /* Update last_dc_val */
+ state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
+ }
+
+ /* Completed MCU, so update state */
+ cinfo->dest->next_output_byte = state.next_output_byte;
+ cinfo->dest->free_in_buffer = state.free_in_buffer;
+ ASSIGN_STATE(entropy->saved, state.cur);
+
+ /* 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 scan.
+ */
+
+METHODDEF(void)
+finish_pass_huff (j_compress_ptr cinfo)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ working_state state;
+
+ /* Load up working state ... flush_bits needs it */
+ state.next_output_byte = cinfo->dest->next_output_byte;
+ state.free_in_buffer = cinfo->dest->free_in_buffer;
+ ASSIGN_STATE(state.cur, entropy->saved);
+ state.cinfo = cinfo;
+
+ /* Flush out the last data */
+ if (! flush_bits(&state))
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
+
+ /* Update state */
+ cinfo->dest->next_output_byte = state.next_output_byte;
+ cinfo->dest->free_in_buffer = state.free_in_buffer;
+ ASSIGN_STATE(entropy->saved, state.cur);
+}
+
+
+/*
+ * Huffman coding optimization.
+ *
+ * We first scan the supplied data and count the number of uses of each symbol
+ * that is to be Huffman-coded. (This process MUST agree with the code above.)
+ * Then we build a Huffman coding tree for the observed counts.
+ * Symbols which are not needed at all for the particular image are not
+ * assigned any code, which saves space in the DHT marker as well as in
+ * the compressed data.
+ */
+
+#ifdef ENTROPY_OPT_SUPPORTED
+
+
+/* Process a single block's worth of coefficients */
+
+LOCAL(void)
+htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
+ long dc_counts[], long ac_counts[])
+{
+ register int temp;
+ register int nbits;
+ register int k, r;
+
+ /* Encode the DC coefficient difference per section F.1.2.1 */
+
+ temp = block[0] - last_dc_val;
+ if (temp < 0)
+ temp = -temp;
+
+ /* 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 the Huffman symbol for the number of bits */
+ dc_counts[nbits]++;
+
+ /* Encode the AC coefficients per section F.1.2.2 */
+
+ r = 0; /* r = run length of zeros */
+
+ for (k = 1; k < DCTSIZE2; k++) {
+ if ((temp = block[jpeg_natural_order[k]]) == 0) {
+ r++;
+ } else {
+ /* if run length > 15, must emit special run-length-16 codes (0xF0) */
+ while (r > 15) {
+ ac_counts[0xF0]++;
+ r -= 16;
+ }
+
+ /* Find the number of bits needed for the magnitude of the coefficient */
+ if (temp < 0)
+ temp = -temp;
+
+ /* 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 Huffman symbol for run length / number of bits */
+ ac_counts[(r << 4) + nbits]++;
+
+ r = 0;
+ }
+ }
+
+ /* If the last coef(s) were zero, emit an end-of-block code */
+ if (r > 0)
+ ac_counts[0]++;
+}
+
+
+/*
+ * Trial-encode one MCU's worth of Huffman-compressed coefficients.
+ * No data is actually output, so no suspension return is possible.
+ */
+
+METHODDEF(boolean)
+encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int blkn, ci;
+ jpeg_component_info * compptr;
+
+ /* Take care of restart intervals if needed */
+ if (cinfo->restart_interval) {
+ if (entropy->restarts_to_go == 0) {
+ /* Re-initialize DC predictions to 0 */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++)
+ entropy->saved.last_dc_val[ci] = 0;
+ /* Update restart state */
+ entropy->restarts_to_go = cinfo->restart_interval;
+ }
+ entropy->restarts_to_go--;
+ }
+
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ ci = cinfo->MCU_membership[blkn];
+ compptr = cinfo->cur_comp_info[ci];
+ htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
+ entropy->dc_count_ptrs[compptr->dc_tbl_no],
+ entropy->ac_count_ptrs[compptr->ac_tbl_no]);
+ entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
+ }
+
+ return TRUE;
+}
+
+
+/*
+ * Generate the best Huffman code table for the given counts, fill htbl.
+ * Note this is also used by jcphuff.c.
+ *
+ * The JPEG standard requires that no symbol be assigned a codeword of all
+ * one bits (so that padding bits added at the end of a compressed segment
+ * can't look like a valid code). Because of the canonical ordering of
+ * codewords, this just means that there must be an unused slot in the
+ * longest codeword length category. Section K.2 of the JPEG spec suggests
+ * reserving such a slot by pretending that symbol 256 is a valid symbol
+ * with count 1. In theory that's not optimal; giving it count zero but
+ * including it in the symbol set anyway should give a better Huffman code.
+ * But the theoretically better code actually seems to come out worse in
+ * practice, because it produces more all-ones bytes (which incur stuffed
+ * zero bytes in the final file). In any case the difference is tiny.
+ *
+ * The JPEG standard requires Huffman codes to be no more than 16 bits long.
+ * If some symbols have a very small but nonzero probability, the Huffman tree
+ * must be adjusted to meet the code length restriction. We currently use
+ * the adjustment method suggested in JPEG section K.2. This method is *not*
+ * optimal; it may not choose the best possible limited-length code. But
+ * typically only very-low-frequency symbols will be given less-than-optimal
+ * lengths, so the code is almost optimal. Experimental comparisons against
+ * an optimal limited-length-code algorithm indicate that the difference is
+ * microscopic --- usually less than a hundredth of a percent of total size.
+ * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
+ */
+
+GLOBAL(void)
+jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
+{
+#define MAX_CLEN 32 /* assumed maximum initial code length */
+ UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
+ int codesize[257]; /* codesize[k] = code length of symbol k */
+ int others[257]; /* next symbol in current branch of tree */
+ int c1, c2;
+ int p, i, j;
+ long v;
+
+ /* This algorithm is explained in section K.2 of the JPEG standard */
+
+ MEMZERO(bits, SIZEOF(bits));
+ MEMZERO(codesize, SIZEOF(codesize));
+ for (i = 0; i < 257; i++)
+ others[i] = -1; /* init links to empty */
+
+ freq[256] = 1; /* make sure 256 has a nonzero count */
+ /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
+ * that no real symbol is given code-value of all ones, because 256
+ * will be placed last in the largest codeword category.
+ */
+
+ /* Huffman's basic algorithm to assign optimal code lengths to symbols */
+
+ for (;;) {
+ /* Find the smallest nonzero frequency, set c1 = its symbol */
+ /* In case of ties, take the larger symbol number */
+ c1 = -1;
+ v = 1000000000L;
+ for (i = 0; i <= 256; i++) {
+ if (freq[i] && freq[i] <= v) {
+ v = freq[i];
+ c1 = i;
+ }
+ }
+
+ /* Find the next smallest nonzero frequency, set c2 = its symbol */
+ /* In case of ties, take the larger symbol number */
+ c2 = -1;
+ v = 1000000000L;
+ for (i = 0; i <= 256; i++) {
+ if (freq[i] && freq[i] <= v && i != c1) {
+ v = freq[i];
+ c2 = i;
+ }
+ }
+
+ /* Done if we've merged everything into one frequency */
+ if (c2 < 0)
+ break;
+
+ /* Else merge the two counts/trees */
+ freq[c1] += freq[c2];
+ freq[c2] = 0;
+
+ /* Increment the codesize of everything in c1's tree branch */
+ codesize[c1]++;
+ while (others[c1] >= 0) {
+ c1 = others[c1];
+ codesize[c1]++;
+ }
+
+ others[c1] = c2; /* chain c2 onto c1's tree branch */
+
+ /* Increment the codesize of everything in c2's tree branch */
+ codesize[c2]++;
+ while (others[c2] >= 0) {
+ c2 = others[c2];
+ codesize[c2]++;
+ }
+ }
+
+ /* Now count the number of symbols of each code length */
+ for (i = 0; i <= 256; i++) {
+ if (codesize[i]) {
+ /* The JPEG standard seems to think that this can't happen, */
+ /* but I'm paranoid... */
+ if (codesize[i] > MAX_CLEN)
+ ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
+
+ bits[codesize[i]]++;
+ }
+ }
+
+ /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
+ * Huffman procedure assigned any such lengths, we must adjust the coding.
+ * Here is what the JPEG spec says about how this next bit works:
+ * Since symbols are paired for the longest Huffman code, the symbols are
+ * removed from this length category two at a time. The prefix for the pair
+ * (which is one bit shorter) is allocated to one of the pair; then,
+ * skipping the BITS entry for that prefix length, a code word from the next
+ * shortest nonzero BITS entry is converted into a prefix for two code words
+ * one bit longer.
+ */
+
+ for (i = MAX_CLEN; i > 16; i--) {
+ while (bits[i] > 0) {
+ j = i - 2; /* find length of new prefix to be used */
+ while (bits[j] == 0)
+ j--;
+
+ bits[i] -= 2; /* remove two symbols */
+ bits[i-1]++; /* one goes in this length */
+ bits[j+1] += 2; /* two new symbols in this length */
+ bits[j]--; /* symbol of this length is now a prefix */
+ }
+ }
+
+ /* Remove the count for the pseudo-symbol 256 from the largest codelength */
+ while (bits[i] == 0) /* find largest codelength still in use */
+ i--;
+ bits[i]--;
+
+ /* Return final symbol counts (only for lengths 0..16) */
+ MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
+
+ /* Return a list of the symbols sorted by code length */
+ /* It's not real clear to me why we don't need to consider the codelength
+ * changes made above, but the JPEG spec seems to think this works.
+ */
+ p = 0;
+ for (i = 1; i <= MAX_CLEN; i++) {
+ for (j = 0; j <= 255; j++) {
+ if (codesize[j] == i) {
+ htbl->huffval[p] = (UINT8) j;
+ p++;
+ }
+ }
+ }
+
+ /* Set sent_table FALSE so updated table will be written to JPEG file. */
+ htbl->sent_table = FALSE;
+}
+
+
+/*
+ * Finish up a statistics-gathering pass and create the new Huffman tables.
+ */
+
+METHODDEF(void)
+finish_pass_gather (j_compress_ptr cinfo)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci, dctbl, actbl;
+ jpeg_component_info * compptr;
+ JHUFF_TBL **htblptr;
+ boolean did_dc[NUM_HUFF_TBLS];
+ boolean did_ac[NUM_HUFF_TBLS];
+
+ /* It's important not to apply jpeg_gen_optimal_table more than once
+ * per table, because it clobbers the input frequency counts!
+ */
+ MEMZERO(did_dc, SIZEOF(did_dc));
+ MEMZERO(did_ac, SIZEOF(did_ac));
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ dctbl = compptr->dc_tbl_no;
+ actbl = compptr->ac_tbl_no;
+ if (! did_dc[dctbl]) {
+ htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+ jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
+ did_dc[dctbl] = TRUE;
+ }
+ if (! did_ac[actbl]) {
+ htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+ jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
+ did_ac[actbl] = TRUE;
+ }
+ }
+}
+
+
+#endif /* ENTROPY_OPT_SUPPORTED */
+
+
+/*
+ * Module initialization routine for Huffman entropy encoding.
+ */
+
+GLOBAL(void)
+jinit_huff_encoder (j_compress_ptr cinfo)
+{
+ huff_entropy_ptr entropy;
+ int i;
+
+ entropy = (huff_entropy_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(huff_entropy_encoder));
+ cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
+ entropy->pub.start_pass = start_pass_huff;
+
+ /* Mark tables unallocated */
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
+#ifdef ENTROPY_OPT_SUPPORTED
+ entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
+#endif
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcinit.c b/core/src/fxcodec/libjpeg/fpdfapi_jcinit.c
index a0d8f8148f..58e5d18764 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcinit.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcinit.c
@@ -1,75 +1,75 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcinit.c
- *
- * 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 initialization logic for the JPEG compressor.
- * This routine is in charge of selecting the modules to be executed and
- * making an initialization call to each one.
- *
- * Logically, this code belongs in jcmaster.c. It's split out because
- * linking this routine implies linking the entire compression library.
- * For a transcoding-only application, we want to be able to use jcmaster.c
- * without linking in the whole library.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/*
- * Master selection of compression modules.
- * This is done once at the start of processing an image. We determine
- * which modules will be used and give them appropriate initialization calls.
- */
-
-GLOBAL(void)
-jinit_compress_master (j_compress_ptr cinfo)
-{
- /* Initialize master control (includes parameter checking/processing) */
- jinit_c_master_control(cinfo, FALSE /* full compression */);
-
- /* Preprocessing */
- if (! cinfo->raw_data_in) {
- jinit_color_converter(cinfo);
- jinit_downsampler(cinfo);
- jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
- }
- /* Forward DCT */
- jinit_forward_dct(cinfo);
- /* Entropy encoding: either Huffman or arithmetic coding. */
- if (cinfo->arith_code) {
- ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
- } else {
- if (cinfo->progressive_mode) {
-#ifdef C_PROGRESSIVE_SUPPORTED
- jinit_phuff_encoder(cinfo);
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else
- jinit_huff_encoder(cinfo);
- }
-
- /* Need a full-image coefficient buffer in any multi-pass mode. */
- jinit_c_coef_controller(cinfo,
- (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
- jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
-
- jinit_marker_writer(cinfo);
-
- /* We can now tell the memory manager to allocate virtual arrays. */
- (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
-
- /* Write the datastream header (SOI) immediately.
- * Frame and scan headers are postponed till later.
- * This lets application insert special markers after the SOI.
- */
- (*cinfo->marker->write_file_header) (cinfo);
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcinit.c
+ *
+ * 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 initialization logic for the JPEG compressor.
+ * This routine is in charge of selecting the modules to be executed and
+ * making an initialization call to each one.
+ *
+ * Logically, this code belongs in jcmaster.c. It's split out because
+ * linking this routine implies linking the entire compression library.
+ * For a transcoding-only application, we want to be able to use jcmaster.c
+ * without linking in the whole library.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Master selection of compression modules.
+ * This is done once at the start of processing an image. We determine
+ * which modules will be used and give them appropriate initialization calls.
+ */
+
+GLOBAL(void)
+jinit_compress_master (j_compress_ptr cinfo)
+{
+ /* Initialize master control (includes parameter checking/processing) */
+ jinit_c_master_control(cinfo, FALSE /* full compression */);
+
+ /* Preprocessing */
+ if (! cinfo->raw_data_in) {
+ jinit_color_converter(cinfo);
+ jinit_downsampler(cinfo);
+ jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
+ }
+ /* Forward DCT */
+ jinit_forward_dct(cinfo);
+ /* Entropy encoding: either Huffman or arithmetic coding. */
+ if (cinfo->arith_code) {
+ ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+ } else {
+ if (cinfo->progressive_mode) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ jinit_phuff_encoder(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else
+ jinit_huff_encoder(cinfo);
+ }
+
+ /* Need a full-image coefficient buffer in any multi-pass mode. */
+ jinit_c_coef_controller(cinfo,
+ (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
+ jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
+
+ jinit_marker_writer(cinfo);
+
+ /* We can now tell the memory manager to allocate virtual arrays. */
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+ /* Write the datastream header (SOI) immediately.
+ * Frame and scan headers are postponed till later.
+ * This lets application insert special markers after the SOI.
+ */
+ (*cinfo->marker->write_file_header) (cinfo);
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c b/core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c
index 25d61f2284..4bf2c403ea 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcmainct.c
@@ -1,296 +1,296 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcmainct.c
- *
- * Copyright (C) 1994-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 contains the main buffer controller for compression.
- * The main buffer lies between the pre-processor and the JPEG
- * compressor proper; it holds downsampled data in the JPEG colorspace.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Note: currently, there is no operating mode in which a full-image buffer
- * is needed at this step. If there were, that mode could not be used with
- * "raw data" input, since this module is bypassed in that case. However,
- * we've left the code here for possible use in special applications.
- */
-#undef FULL_MAIN_BUFFER_SUPPORTED
-
-
-/* Private buffer controller object */
-
-typedef struct {
- struct jpeg_c_main_controller pub; /* public fields */
-
- JDIMENSION cur_iMCU_row; /* number of current iMCU row */
- JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
- boolean suspended; /* remember if we suspended output */
- J_BUF_MODE pass_mode; /* current operating mode */
-
- /* If using just a strip buffer, this points to the entire set of buffers
- * (we allocate one for each component). In the full-image case, this
- * points to the currently accessible strips of the virtual arrays.
- */
- JSAMPARRAY buffer[MAX_COMPONENTS];
-
-#ifdef FULL_MAIN_BUFFER_SUPPORTED
- /* If using full-image storage, this array holds pointers to virtual-array
- * control blocks for each component. Unused if not full-image storage.
- */
- jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
-#endif
-} my_main_controller;
-
-typedef my_main_controller * my_main_ptr;
-
-
-/* Forward declarations */
-METHODDEF(void) process_data_simple_main
- JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
- JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
-#ifdef FULL_MAIN_BUFFER_SUPPORTED
-METHODDEF(void) process_data_buffer_main
- JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
- JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
-#endif
-
-
-/*
- * Initialize for a processing pass.
- */
-
-METHODDEF(void)
-start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
-
- /* Do nothing in raw-data mode. */
- if (cinfo->raw_data_in)
- return;
-
- main->cur_iMCU_row = 0; /* initialize counters */
- main->rowgroup_ctr = 0;
- main->suspended = FALSE;
- main->pass_mode = pass_mode; /* save mode for use by process_data */
-
- switch (pass_mode) {
- case JBUF_PASS_THRU:
-#ifdef FULL_MAIN_BUFFER_SUPPORTED
- if (main->whole_image[0] != NULL)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
-#endif
- main->pub.process_data = process_data_simple_main;
- break;
-#ifdef FULL_MAIN_BUFFER_SUPPORTED
- case JBUF_SAVE_SOURCE:
- case JBUF_CRANK_DEST:
- case JBUF_SAVE_AND_PASS:
- if (main->whole_image[0] == NULL)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- main->pub.process_data = process_data_buffer_main;
- break;
-#endif
- default:
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- break;
- }
-}
-
-
-/*
- * Process some data.
- * This routine handles the simple pass-through mode,
- * where we have only a strip buffer.
- */
-
-METHODDEF(void)
-process_data_simple_main (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
- JDIMENSION in_rows_avail)
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
-
- while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
- /* Read input data if we haven't filled the main buffer yet */
- if (main->rowgroup_ctr < DCTSIZE)
- (*cinfo->prep->pre_process_data) (cinfo,
- input_buf, in_row_ctr, in_rows_avail,
- main->buffer, &main->rowgroup_ctr,
- (JDIMENSION) DCTSIZE);
-
- /* If we don't have a full iMCU row buffered, return to application for
- * more data. Note that preprocessor will always pad to fill the iMCU row
- * at the bottom of the image.
- */
- if (main->rowgroup_ctr != DCTSIZE)
- return;
-
- /* Send the completed row to the compressor */
- if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
- /* If compressor did not consume the whole row, then we must need to
- * suspend processing and return to the application. In this situation
- * we pretend we didn't yet consume the last input row; otherwise, if
- * it happened to be the last row of the image, the application would
- * think we were done.
- */
- if (! main->suspended) {
- (*in_row_ctr)--;
- main->suspended = TRUE;
- }
- return;
- }
- /* We did finish the row. Undo our little suspension hack if a previous
- * call suspended; then mark the main buffer empty.
- */
- if (main->suspended) {
- (*in_row_ctr)++;
- main->suspended = FALSE;
- }
- main->rowgroup_ctr = 0;
- main->cur_iMCU_row++;
- }
-}
-
-
-#ifdef FULL_MAIN_BUFFER_SUPPORTED
-
-/*
- * Process some data.
- * This routine handles all of the modes that use a full-size buffer.
- */
-
-METHODDEF(void)
-process_data_buffer_main (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
- JDIMENSION in_rows_avail)
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci;
- jpeg_component_info *compptr;
- boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
-
- while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
- /* Realign the virtual buffers if at the start of an iMCU row. */
- if (main->rowgroup_ctr == 0) {
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr) cinfo, main->whole_image[ci],
- main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
- (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
- }
- /* In a read pass, pretend we just read some source data. */
- if (! writing) {
- *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
- main->rowgroup_ctr = DCTSIZE;
- }
- }
-
- /* If a write pass, read input data until the current iMCU row is full. */
- /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
- if (writing) {
- (*cinfo->prep->pre_process_data) (cinfo,
- input_buf, in_row_ctr, in_rows_avail,
- main->buffer, &main->rowgroup_ctr,
- (JDIMENSION) DCTSIZE);
- /* Return to application if we need more data to fill the iMCU row. */
- if (main->rowgroup_ctr < DCTSIZE)
- return;
- }
-
- /* Emit data, unless this is a sink-only pass. */
- if (main->pass_mode != JBUF_SAVE_SOURCE) {
- if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
- /* If compressor did not consume the whole row, then we must need to
- * suspend processing and return to the application. In this situation
- * we pretend we didn't yet consume the last input row; otherwise, if
- * it happened to be the last row of the image, the application would
- * think we were done.
- */
- if (! main->suspended) {
- (*in_row_ctr)--;
- main->suspended = TRUE;
- }
- return;
- }
- /* We did finish the row. Undo our little suspension hack if a previous
- * call suspended; then mark the main buffer empty.
- */
- if (main->suspended) {
- (*in_row_ctr)++;
- main->suspended = FALSE;
- }
- }
-
- /* If get here, we are done with this iMCU row. Mark buffer empty. */
- main->rowgroup_ctr = 0;
- main->cur_iMCU_row++;
- }
-}
-
-#endif /* FULL_MAIN_BUFFER_SUPPORTED */
-
-
-/*
- * Initialize main buffer controller.
- */
-
-GLOBAL(void)
-jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
-{
- my_main_ptr main;
- int ci;
- jpeg_component_info *compptr;
-
- main = (my_main_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_main_controller));
- cinfo->main = (struct jpeg_c_main_controller *) main;
- main->pub.start_pass = start_pass_main;
-
- /* We don't need to create a buffer in raw-data mode. */
- if (cinfo->raw_data_in)
- return;
-
- /* Create the buffer. It holds downsampled data, so each component
- * may be of a different size.
- */
- if (need_full_buffer) {
-#ifdef FULL_MAIN_BUFFER_SUPPORTED
- /* Allocate a full-image virtual array for each component */
- /* Note we pad the bottom to a multiple of the iMCU height */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
- compptr->width_in_blocks * DCTSIZE,
- (JDIMENSION) jround_up((long) compptr->height_in_blocks,
- (long) compptr->v_samp_factor) * DCTSIZE,
- (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
- }
-#else
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
-#endif
- } else {
-#ifdef FULL_MAIN_BUFFER_SUPPORTED
- main->whole_image[0] = NULL; /* flag for no virtual arrays */
-#endif
- /* Allocate a strip buffer for each component */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- main->buffer[ci] = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- compptr->width_in_blocks * DCTSIZE,
- (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
- }
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcmainct.c
+ *
+ * Copyright (C) 1994-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 contains the main buffer controller for compression.
+ * The main buffer lies between the pre-processor and the JPEG
+ * compressor proper; it holds downsampled data in the JPEG colorspace.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Note: currently, there is no operating mode in which a full-image buffer
+ * is needed at this step. If there were, that mode could not be used with
+ * "raw data" input, since this module is bypassed in that case. However,
+ * we've left the code here for possible use in special applications.
+ */
+#undef FULL_MAIN_BUFFER_SUPPORTED
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_c_main_controller pub; /* public fields */
+
+ JDIMENSION cur_iMCU_row; /* number of current iMCU row */
+ JDIMENSION rowgroup_ctr; /* counts row groups received in iMCU row */
+ boolean suspended; /* remember if we suspended output */
+ J_BUF_MODE pass_mode; /* current operating mode */
+
+ /* If using just a strip buffer, this points to the entire set of buffers
+ * (we allocate one for each component). In the full-image case, this
+ * points to the currently accessible strips of the virtual arrays.
+ */
+ JSAMPARRAY buffer[MAX_COMPONENTS];
+
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ /* If using full-image storage, this array holds pointers to virtual-array
+ * control blocks for each component. Unused if not full-image storage.
+ */
+ jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
+#endif
+} my_main_controller;
+
+typedef my_main_controller * my_main_ptr;
+
+
+/* Forward declarations */
+METHODDEF(void) process_data_simple_main
+ JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
+ JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+METHODDEF(void) process_data_buffer_main
+ JPP((j_compress_ptr cinfo, JSAMPARRAY input_buf,
+ JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail));
+#endif
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_main (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+
+ /* Do nothing in raw-data mode. */
+ if (cinfo->raw_data_in)
+ return;
+
+ main->cur_iMCU_row = 0; /* initialize counters */
+ main->rowgroup_ctr = 0;
+ main->suspended = FALSE;
+ main->pass_mode = pass_mode; /* save mode for use by process_data */
+
+ switch (pass_mode) {
+ case JBUF_PASS_THRU:
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ if (main->whole_image[0] != NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+ main->pub.process_data = process_data_simple_main;
+ break;
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ case JBUF_SAVE_SOURCE:
+ case JBUF_CRANK_DEST:
+ case JBUF_SAVE_AND_PASS:
+ if (main->whole_image[0] == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ main->pub.process_data = process_data_buffer_main;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ break;
+ }
+}
+
+
+/*
+ * Process some data.
+ * This routine handles the simple pass-through mode,
+ * where we have only a strip buffer.
+ */
+
+METHODDEF(void)
+process_data_simple_main (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+
+ while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
+ /* Read input data if we haven't filled the main buffer yet */
+ if (main->rowgroup_ctr < DCTSIZE)
+ (*cinfo->prep->pre_process_data) (cinfo,
+ input_buf, in_row_ctr, in_rows_avail,
+ main->buffer, &main->rowgroup_ctr,
+ (JDIMENSION) DCTSIZE);
+
+ /* If we don't have a full iMCU row buffered, return to application for
+ * more data. Note that preprocessor will always pad to fill the iMCU row
+ * at the bottom of the image.
+ */
+ if (main->rowgroup_ctr != DCTSIZE)
+ return;
+
+ /* Send the completed row to the compressor */
+ if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
+ /* If compressor did not consume the whole row, then we must need to
+ * suspend processing and return to the application. In this situation
+ * we pretend we didn't yet consume the last input row; otherwise, if
+ * it happened to be the last row of the image, the application would
+ * think we were done.
+ */
+ if (! main->suspended) {
+ (*in_row_ctr)--;
+ main->suspended = TRUE;
+ }
+ return;
+ }
+ /* We did finish the row. Undo our little suspension hack if a previous
+ * call suspended; then mark the main buffer empty.
+ */
+ if (main->suspended) {
+ (*in_row_ctr)++;
+ main->suspended = FALSE;
+ }
+ main->rowgroup_ctr = 0;
+ main->cur_iMCU_row++;
+ }
+}
+
+
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+
+/*
+ * Process some data.
+ * This routine handles all of the modes that use a full-size buffer.
+ */
+
+METHODDEF(void)
+process_data_buffer_main (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci;
+ jpeg_component_info *compptr;
+ boolean writing = (main->pass_mode != JBUF_CRANK_DEST);
+
+ while (main->cur_iMCU_row < cinfo->total_iMCU_rows) {
+ /* Realign the virtual buffers if at the start of an iMCU row. */
+ if (main->rowgroup_ctr == 0) {
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ main->buffer[ci] = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, main->whole_image[ci],
+ main->cur_iMCU_row * (compptr->v_samp_factor * DCTSIZE),
+ (JDIMENSION) (compptr->v_samp_factor * DCTSIZE), writing);
+ }
+ /* In a read pass, pretend we just read some source data. */
+ if (! writing) {
+ *in_row_ctr += cinfo->max_v_samp_factor * DCTSIZE;
+ main->rowgroup_ctr = DCTSIZE;
+ }
+ }
+
+ /* If a write pass, read input data until the current iMCU row is full. */
+ /* Note: preprocessor will pad if necessary to fill the last iMCU row. */
+ if (writing) {
+ (*cinfo->prep->pre_process_data) (cinfo,
+ input_buf, in_row_ctr, in_rows_avail,
+ main->buffer, &main->rowgroup_ctr,
+ (JDIMENSION) DCTSIZE);
+ /* Return to application if we need more data to fill the iMCU row. */
+ if (main->rowgroup_ctr < DCTSIZE)
+ return;
+ }
+
+ /* Emit data, unless this is a sink-only pass. */
+ if (main->pass_mode != JBUF_SAVE_SOURCE) {
+ if (! (*cinfo->coef->compress_data) (cinfo, main->buffer)) {
+ /* If compressor did not consume the whole row, then we must need to
+ * suspend processing and return to the application. In this situation
+ * we pretend we didn't yet consume the last input row; otherwise, if
+ * it happened to be the last row of the image, the application would
+ * think we were done.
+ */
+ if (! main->suspended) {
+ (*in_row_ctr)--;
+ main->suspended = TRUE;
+ }
+ return;
+ }
+ /* We did finish the row. Undo our little suspension hack if a previous
+ * call suspended; then mark the main buffer empty.
+ */
+ if (main->suspended) {
+ (*in_row_ctr)++;
+ main->suspended = FALSE;
+ }
+ }
+
+ /* If get here, we are done with this iMCU row. Mark buffer empty. */
+ main->rowgroup_ctr = 0;
+ main->cur_iMCU_row++;
+ }
+}
+
+#endif /* FULL_MAIN_BUFFER_SUPPORTED */
+
+
+/*
+ * Initialize main buffer controller.
+ */
+
+GLOBAL(void)
+jinit_c_main_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+ my_main_ptr main;
+ int ci;
+ jpeg_component_info *compptr;
+
+ main = (my_main_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_main_controller));
+ cinfo->main = (struct jpeg_c_main_controller *) main;
+ main->pub.start_pass = start_pass_main;
+
+ /* We don't need to create a buffer in raw-data mode. */
+ if (cinfo->raw_data_in)
+ return;
+
+ /* Create the buffer. It holds downsampled data, so each component
+ * may be of a different size.
+ */
+ if (need_full_buffer) {
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ /* Allocate a full-image virtual array for each component */
+ /* Note we pad the bottom to a multiple of the iMCU height */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ main->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ compptr->width_in_blocks * DCTSIZE,
+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+ (long) compptr->v_samp_factor) * DCTSIZE,
+ (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
+ }
+#else
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif
+ } else {
+#ifdef FULL_MAIN_BUFFER_SUPPORTED
+ main->whole_image[0] = NULL; /* flag for no virtual arrays */
+#endif
+ /* Allocate a strip buffer for each component */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ main->buffer[ci] = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ compptr->width_in_blocks * DCTSIZE,
+ (JDIMENSION) (compptr->v_samp_factor * DCTSIZE));
+ }
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c b/core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c
index d0a9e87650..f1c89d92a9 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcmarker.c
@@ -1,667 +1,667 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcmarker.c
- *
- * Copyright (C) 1991-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 routines to write JPEG datastream markers.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-typedef enum { /* JPEG marker codes */
- M_SOF0 = 0xc0,
- M_SOF1 = 0xc1,
- M_SOF2 = 0xc2,
- M_SOF3 = 0xc3,
-
- M_SOF5 = 0xc5,
- M_SOF6 = 0xc6,
- M_SOF7 = 0xc7,
-
- M_JPG = 0xc8,
- M_SOF9 = 0xc9,
- M_SOF10 = 0xca,
- M_SOF11 = 0xcb,
-
- M_SOF13 = 0xcd,
- M_SOF14 = 0xce,
- M_SOF15 = 0xcf,
-
- M_DHT = 0xc4,
-
- M_DAC = 0xcc,
-
- M_RST0 = 0xd0,
- M_RST1 = 0xd1,
- M_RST2 = 0xd2,
- M_RST3 = 0xd3,
- M_RST4 = 0xd4,
- M_RST5 = 0xd5,
- M_RST6 = 0xd6,
- M_RST7 = 0xd7,
-
- M_SOI = 0xd8,
- M_EOI = 0xd9,
- M_SOS = 0xda,
- M_DQT = 0xdb,
- M_DNL = 0xdc,
- M_DRI = 0xdd,
- M_DHP = 0xde,
- M_EXP = 0xdf,
-
- M_APP0 = 0xe0,
- M_APP1 = 0xe1,
- M_APP2 = 0xe2,
- M_APP3 = 0xe3,
- M_APP4 = 0xe4,
- M_APP5 = 0xe5,
- M_APP6 = 0xe6,
- M_APP7 = 0xe7,
- M_APP8 = 0xe8,
- M_APP9 = 0xe9,
- M_APP10 = 0xea,
- M_APP11 = 0xeb,
- M_APP12 = 0xec,
- M_APP13 = 0xed,
- M_APP14 = 0xee,
- M_APP15 = 0xef,
-
- M_JPG0 = 0xf0,
- M_JPG13 = 0xfd,
- M_COM = 0xfe,
-
- M_TEM = 0x01,
-
- M_ERROR = 0x100
-} JPEG_MARKER;
-
-
-/* Private state */
-
-typedef struct {
- struct jpeg_marker_writer pub; /* public fields */
-
- unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
-} my_marker_writer;
-
-typedef my_marker_writer * my_marker_ptr;
-
-
-/*
- * Basic output routines.
- *
- * Note that we do not support suspension while writing a marker.
- * Therefore, an application using suspension must ensure that there is
- * enough buffer space for the initial markers (typ. 600-700 bytes) before
- * calling jpeg_start_compress, and enough space to write the trailing EOI
- * (a few bytes) before calling jpeg_finish_compress. Multipass compression
- * modes are not supported at all with suspension, so those two are the only
- * points where markers will be written.
- */
-
-LOCAL(void)
-emit_byte (j_compress_ptr cinfo, int val)
-/* Emit a byte */
-{
- 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);
- }
-}
-
-
-LOCAL(void)
-emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
-/* Emit a marker code */
-{
- emit_byte(cinfo, 0xFF);
- emit_byte(cinfo, (int) mark);
-}
-
-
-LOCAL(void)
-emit_2bytes (j_compress_ptr cinfo, int value)
-/* Emit a 2-byte integer; these are always MSB first in JPEG files */
-{
- emit_byte(cinfo, (value >> 8) & 0xFF);
- emit_byte(cinfo, value & 0xFF);
-}
-
-
-/*
- * Routines to write specific marker types.
- */
-
-LOCAL(int)
-emit_dqt (j_compress_ptr cinfo, int index)
-/* Emit a DQT marker */
-/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
-{
- JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
- int prec;
- int i;
-
- if (qtbl == NULL)
- ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
-
- prec = 0;
- for (i = 0; i < DCTSIZE2; i++) {
- if (qtbl->quantval[i] > 255)
- prec = 1;
- }
-
- if (! qtbl->sent_table) {
- emit_marker(cinfo, M_DQT);
-
- emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
-
- emit_byte(cinfo, index + (prec<<4));
-
- for (i = 0; i < DCTSIZE2; i++) {
- /* The table entries must be emitted in zigzag order. */
- unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
- if (prec)
- emit_byte(cinfo, (int) (qval >> 8));
- emit_byte(cinfo, (int) (qval & 0xFF));
- }
-
- qtbl->sent_table = TRUE;
- }
-
- return prec;
-}
-
-
-LOCAL(void)
-emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
-/* Emit a DHT marker */
-{
- JHUFF_TBL * htbl;
- int length, i;
-
- if (is_ac) {
- htbl = cinfo->ac_huff_tbl_ptrs[index];
- index += 0x10; /* output index has AC bit set */
- } else {
- htbl = cinfo->dc_huff_tbl_ptrs[index];
- }
-
- if (htbl == NULL)
- ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
-
- if (! htbl->sent_table) {
- emit_marker(cinfo, M_DHT);
-
- length = 0;
- for (i = 1; i <= 16; i++)
- length += htbl->bits[i];
-
- emit_2bytes(cinfo, length + 2 + 1 + 16);
- emit_byte(cinfo, index);
-
- for (i = 1; i <= 16; i++)
- emit_byte(cinfo, htbl->bits[i]);
-
- for (i = 0; i < length; i++)
- emit_byte(cinfo, htbl->huffval[i]);
-
- htbl->sent_table = TRUE;
- }
-}
-
-
-LOCAL(void)
-emit_dac (j_compress_ptr cinfo)
-/* Emit a DAC marker */
-/* Since the useful info is so small, we want to emit all the tables in */
-/* one DAC marker. Therefore this routine does its own scan of the table. */
-{
-#ifdef C_ARITH_CODING_SUPPORTED
- char dc_in_use[NUM_ARITH_TBLS];
- char ac_in_use[NUM_ARITH_TBLS];
- int length, i;
- jpeg_component_info *compptr;
-
- for (i = 0; i < NUM_ARITH_TBLS; i++)
- dc_in_use[i] = ac_in_use[i] = 0;
-
- for (i = 0; i < cinfo->comps_in_scan; i++) {
- compptr = cinfo->cur_comp_info[i];
- dc_in_use[compptr->dc_tbl_no] = 1;
- ac_in_use[compptr->ac_tbl_no] = 1;
- }
-
- length = 0;
- for (i = 0; i < NUM_ARITH_TBLS; i++)
- length += dc_in_use[i] + ac_in_use[i];
-
- emit_marker(cinfo, M_DAC);
-
- emit_2bytes(cinfo, length*2 + 2);
-
- for (i = 0; i < NUM_ARITH_TBLS; i++) {
- if (dc_in_use[i]) {
- emit_byte(cinfo, i);
- emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
- }
- if (ac_in_use[i]) {
- emit_byte(cinfo, i + 0x10);
- emit_byte(cinfo, cinfo->arith_ac_K[i]);
- }
- }
-#endif /* C_ARITH_CODING_SUPPORTED */
-}
-
-
-LOCAL(void)
-emit_dri (j_compress_ptr cinfo)
-/* Emit a DRI marker */
-{
- emit_marker(cinfo, M_DRI);
-
- emit_2bytes(cinfo, 4); /* fixed length */
-
- emit_2bytes(cinfo, (int) cinfo->restart_interval);
-}
-
-
-LOCAL(void)
-emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
-/* Emit a SOF marker */
-{
- int ci;
- jpeg_component_info *compptr;
-
- emit_marker(cinfo, code);
-
- emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
-
- /* Make sure image isn't bigger than SOF field can handle */
- if ((long) cinfo->image_height > 65535L ||
- (long) cinfo->image_width > 65535L)
- ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
-
- emit_byte(cinfo, cinfo->data_precision);
- emit_2bytes(cinfo, (int) cinfo->image_height);
- emit_2bytes(cinfo, (int) cinfo->image_width);
-
- emit_byte(cinfo, cinfo->num_components);
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- emit_byte(cinfo, compptr->component_id);
- emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
- emit_byte(cinfo, compptr->quant_tbl_no);
- }
-}
-
-
-LOCAL(void)
-emit_sos (j_compress_ptr cinfo)
-/* Emit a SOS marker */
-{
- int i, td, ta;
- jpeg_component_info *compptr;
-
- emit_marker(cinfo, M_SOS);
-
- emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
-
- emit_byte(cinfo, cinfo->comps_in_scan);
-
- for (i = 0; i < cinfo->comps_in_scan; i++) {
- compptr = cinfo->cur_comp_info[i];
- emit_byte(cinfo, compptr->component_id);
- td = compptr->dc_tbl_no;
- ta = compptr->ac_tbl_no;
- if (cinfo->progressive_mode) {
- /* Progressive mode: only DC or only AC tables are used in one scan;
- * furthermore, Huffman coding of DC refinement uses no table at all.
- * We emit 0 for unused field(s); this is recommended by the P&M text
- * but does not seem to be specified in the standard.
- */
- if (cinfo->Ss == 0) {
- ta = 0; /* DC scan */
- if (cinfo->Ah != 0 && !cinfo->arith_code)
- td = 0; /* no DC table either */
- } else {
- td = 0; /* AC scan */
- }
- }
- emit_byte(cinfo, (td << 4) + ta);
- }
-
- emit_byte(cinfo, cinfo->Ss);
- emit_byte(cinfo, cinfo->Se);
- emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
-}
-
-
-LOCAL(void)
-emit_jfif_app0 (j_compress_ptr cinfo)
-/* Emit a JFIF-compliant APP0 marker */
-{
- /*
- * Length of APP0 block (2 bytes)
- * Block ID (4 bytes - ASCII "JFIF")
- * Zero byte (1 byte to terminate the ID string)
- * Version Major, Minor (2 bytes - major first)
- * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
- * Xdpu (2 bytes - dots per unit horizontal)
- * Ydpu (2 bytes - dots per unit vertical)
- * Thumbnail X size (1 byte)
- * Thumbnail Y size (1 byte)
- */
-
- emit_marker(cinfo, M_APP0);
-
- emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
-
- emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
- emit_byte(cinfo, 0x46);
- emit_byte(cinfo, 0x49);
- emit_byte(cinfo, 0x46);
- emit_byte(cinfo, 0);
- emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
- emit_byte(cinfo, cinfo->JFIF_minor_version);
- emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
- emit_2bytes(cinfo, (int) cinfo->X_density);
- emit_2bytes(cinfo, (int) cinfo->Y_density);
- emit_byte(cinfo, 0); /* No thumbnail image */
- emit_byte(cinfo, 0);
-}
-
-
-LOCAL(void)
-emit_adobe_app14 (j_compress_ptr cinfo)
-/* Emit an Adobe APP14 marker */
-{
- /*
- * Length of APP14 block (2 bytes)
- * Block ID (5 bytes - ASCII "Adobe")
- * Version Number (2 bytes - currently 100)
- * Flags0 (2 bytes - currently 0)
- * Flags1 (2 bytes - currently 0)
- * Color transform (1 byte)
- *
- * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
- * now in circulation seem to use Version = 100, so that's what we write.
- *
- * We write the color transform byte as 1 if the JPEG color space is
- * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
- * whether the encoder performed a transformation, which is pretty useless.
- */
-
- emit_marker(cinfo, M_APP14);
-
- emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
-
- emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
- emit_byte(cinfo, 0x64);
- emit_byte(cinfo, 0x6F);
- emit_byte(cinfo, 0x62);
- emit_byte(cinfo, 0x65);
- emit_2bytes(cinfo, 100); /* Version */
- emit_2bytes(cinfo, 0); /* Flags0 */
- emit_2bytes(cinfo, 0); /* Flags1 */
- switch (cinfo->jpeg_color_space) {
- case JCS_YCbCr:
- emit_byte(cinfo, 1); /* Color transform = 1 */
- break;
- case JCS_YCCK:
- emit_byte(cinfo, 2); /* Color transform = 2 */
- break;
- default:
- emit_byte(cinfo, 0); /* Color transform = 0 */
- break;
- }
-}
-
-
-/*
- * These routines allow writing an arbitrary marker with parameters.
- * The only intended use is to emit COM or APPn markers after calling
- * write_file_header and before calling write_frame_header.
- * Other uses are not guaranteed to produce desirable results.
- * Counting the parameter bytes properly is the caller's responsibility.
- */
-
-METHODDEF(void)
-write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
-/* Emit an arbitrary marker header */
-{
- if (datalen > (unsigned int) 65533) /* safety check */
- ERREXIT(cinfo, JERR_BAD_LENGTH);
-
- emit_marker(cinfo, (JPEG_MARKER) marker);
-
- emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
-}
-
-METHODDEF(void)
-write_marker_byte (j_compress_ptr cinfo, int val)
-/* Emit one byte of marker parameters following write_marker_header */
-{
- emit_byte(cinfo, val);
-}
-
-
-/*
- * Write datastream header.
- * This consists of an SOI and optional APPn markers.
- * We recommend use of the JFIF marker, but not the Adobe marker,
- * when using YCbCr or grayscale data. The JFIF marker should NOT
- * be used for any other JPEG colorspace. The Adobe marker is helpful
- * to distinguish RGB, CMYK, and YCCK colorspaces.
- * Note that an application can write additional header markers after
- * jpeg_start_compress returns.
- */
-
-METHODDEF(void)
-write_file_header (j_compress_ptr cinfo)
-{
- my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
-
- emit_marker(cinfo, M_SOI); /* first the SOI */
-
- /* SOI is defined to reset restart interval to 0 */
- marker->last_restart_interval = 0;
-
- if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
- emit_jfif_app0(cinfo);
- if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
- emit_adobe_app14(cinfo);
-}
-
-
-/*
- * Write frame header.
- * This consists of DQT and SOFn markers.
- * Note that we do not emit the SOF until we have emitted the DQT(s).
- * This avoids compatibility problems with incorrect implementations that
- * try to error-check the quant table numbers as soon as they see the SOF.
- */
-
-METHODDEF(void)
-write_frame_header (j_compress_ptr cinfo)
-{
- int ci, prec;
- boolean is_baseline;
- jpeg_component_info *compptr;
-
- /* Emit DQT for each quantization table.
- * Note that emit_dqt() suppresses any duplicate tables.
- */
- prec = 0;
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- prec += emit_dqt(cinfo, compptr->quant_tbl_no);
- }
- /* now prec is nonzero iff there are any 16-bit quant tables. */
-
- /* Check for a non-baseline specification.
- * Note we assume that Huffman table numbers won't be changed later.
- */
- if (cinfo->arith_code || cinfo->progressive_mode ||
- cinfo->data_precision != 8) {
- is_baseline = FALSE;
- } else {
- is_baseline = TRUE;
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
- is_baseline = FALSE;
- }
- if (prec && is_baseline) {
- is_baseline = FALSE;
- /* If it's baseline except for quantizer size, warn the user */
- TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
- }
- }
-
- /* Emit the proper SOF marker */
- if (cinfo->arith_code) {
- emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */
- } else {
- if (cinfo->progressive_mode)
- emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
- else if (is_baseline)
- emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
- else
- emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
- }
-}
-
-
-/*
- * Write scan header.
- * This consists of DHT or DAC markers, optional DRI, and SOS.
- * Compressed data will be written following the SOS.
- */
-
-METHODDEF(void)
-write_scan_header (j_compress_ptr cinfo)
-{
- my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
- int i;
- jpeg_component_info *compptr;
-
- if (cinfo->arith_code) {
- /* Emit arith conditioning info. We may have some duplication
- * if the file has multiple scans, but it's so small it's hardly
- * worth worrying about.
- */
- emit_dac(cinfo);
- } else {
- /* Emit Huffman tables.
- * Note that emit_dht() suppresses any duplicate tables.
- */
- for (i = 0; i < cinfo->comps_in_scan; i++) {
- compptr = cinfo->cur_comp_info[i];
- if (cinfo->progressive_mode) {
- /* Progressive mode: only DC or only AC tables are used in one scan */
- if (cinfo->Ss == 0) {
- if (cinfo->Ah == 0) /* DC needs no table for refinement scan */
- emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
- } else {
- emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
- }
- } else {
- /* Sequential mode: need both DC and AC tables */
- emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
- emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
- }
- }
- }
-
- /* Emit DRI if required --- note that DRI value could change for each scan.
- * We avoid wasting space with unnecessary DRIs, however.
- */
- if (cinfo->restart_interval != marker->last_restart_interval) {
- emit_dri(cinfo);
- marker->last_restart_interval = cinfo->restart_interval;
- }
-
- emit_sos(cinfo);
-}
-
-
-/*
- * Write datastream trailer.
- */
-
-METHODDEF(void)
-write_file_trailer (j_compress_ptr cinfo)
-{
- emit_marker(cinfo, M_EOI);
-}
-
-
-/*
- * Write an abbreviated table-specification datastream.
- * This consists of SOI, DQT and DHT tables, and EOI.
- * Any table that is defined and not marked sent_table = TRUE will be
- * emitted. Note that all tables will be marked sent_table = TRUE at exit.
- */
-
-METHODDEF(void)
-write_tables_only (j_compress_ptr cinfo)
-{
- int i;
-
- emit_marker(cinfo, M_SOI);
-
- for (i = 0; i < NUM_QUANT_TBLS; i++) {
- if (cinfo->quant_tbl_ptrs[i] != NULL)
- (void) emit_dqt(cinfo, i);
- }
-
- if (! cinfo->arith_code) {
- for (i = 0; i < NUM_HUFF_TBLS; i++) {
- if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
- emit_dht(cinfo, i, FALSE);
- if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
- emit_dht(cinfo, i, TRUE);
- }
- }
-
- emit_marker(cinfo, M_EOI);
-}
-
-
-/*
- * Initialize the marker writer module.
- */
-
-GLOBAL(void)
-jinit_marker_writer (j_compress_ptr cinfo)
-{
- my_marker_ptr marker;
-
- /* Create the subobject */
- marker = (my_marker_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_marker_writer));
- cinfo->marker = (struct jpeg_marker_writer *) marker;
- /* Initialize method pointers */
- marker->pub.write_file_header = write_file_header;
- marker->pub.write_frame_header = write_frame_header;
- marker->pub.write_scan_header = write_scan_header;
- marker->pub.write_file_trailer = write_file_trailer;
- marker->pub.write_tables_only = write_tables_only;
- marker->pub.write_marker_header = write_marker_header;
- marker->pub.write_marker_byte = write_marker_byte;
- /* Initialize private state */
- marker->last_restart_interval = 0;
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcmarker.c
+ *
+ * Copyright (C) 1991-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 routines to write JPEG datastream markers.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+typedef enum { /* JPEG marker codes */
+ M_SOF0 = 0xc0,
+ M_SOF1 = 0xc1,
+ M_SOF2 = 0xc2,
+ M_SOF3 = 0xc3,
+
+ M_SOF5 = 0xc5,
+ M_SOF6 = 0xc6,
+ M_SOF7 = 0xc7,
+
+ M_JPG = 0xc8,
+ M_SOF9 = 0xc9,
+ M_SOF10 = 0xca,
+ M_SOF11 = 0xcb,
+
+ M_SOF13 = 0xcd,
+ M_SOF14 = 0xce,
+ M_SOF15 = 0xcf,
+
+ M_DHT = 0xc4,
+
+ M_DAC = 0xcc,
+
+ M_RST0 = 0xd0,
+ M_RST1 = 0xd1,
+ M_RST2 = 0xd2,
+ M_RST3 = 0xd3,
+ M_RST4 = 0xd4,
+ M_RST5 = 0xd5,
+ M_RST6 = 0xd6,
+ M_RST7 = 0xd7,
+
+ M_SOI = 0xd8,
+ M_EOI = 0xd9,
+ M_SOS = 0xda,
+ M_DQT = 0xdb,
+ M_DNL = 0xdc,
+ M_DRI = 0xdd,
+ M_DHP = 0xde,
+ M_EXP = 0xdf,
+
+ M_APP0 = 0xe0,
+ M_APP1 = 0xe1,
+ M_APP2 = 0xe2,
+ M_APP3 = 0xe3,
+ M_APP4 = 0xe4,
+ M_APP5 = 0xe5,
+ M_APP6 = 0xe6,
+ M_APP7 = 0xe7,
+ M_APP8 = 0xe8,
+ M_APP9 = 0xe9,
+ M_APP10 = 0xea,
+ M_APP11 = 0xeb,
+ M_APP12 = 0xec,
+ M_APP13 = 0xed,
+ M_APP14 = 0xee,
+ M_APP15 = 0xef,
+
+ M_JPG0 = 0xf0,
+ M_JPG13 = 0xfd,
+ M_COM = 0xfe,
+
+ M_TEM = 0x01,
+
+ M_ERROR = 0x100
+} JPEG_MARKER;
+
+
+/* Private state */
+
+typedef struct {
+ struct jpeg_marker_writer pub; /* public fields */
+
+ unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
+} my_marker_writer;
+
+typedef my_marker_writer * my_marker_ptr;
+
+
+/*
+ * Basic output routines.
+ *
+ * Note that we do not support suspension while writing a marker.
+ * Therefore, an application using suspension must ensure that there is
+ * enough buffer space for the initial markers (typ. 600-700 bytes) before
+ * calling jpeg_start_compress, and enough space to write the trailing EOI
+ * (a few bytes) before calling jpeg_finish_compress. Multipass compression
+ * modes are not supported at all with suspension, so those two are the only
+ * points where markers will be written.
+ */
+
+LOCAL(void)
+emit_byte (j_compress_ptr cinfo, int val)
+/* Emit a byte */
+{
+ 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);
+ }
+}
+
+
+LOCAL(void)
+emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
+/* Emit a marker code */
+{
+ emit_byte(cinfo, 0xFF);
+ emit_byte(cinfo, (int) mark);
+}
+
+
+LOCAL(void)
+emit_2bytes (j_compress_ptr cinfo, int value)
+/* Emit a 2-byte integer; these are always MSB first in JPEG files */
+{
+ emit_byte(cinfo, (value >> 8) & 0xFF);
+ emit_byte(cinfo, value & 0xFF);
+}
+
+
+/*
+ * Routines to write specific marker types.
+ */
+
+LOCAL(int)
+emit_dqt (j_compress_ptr cinfo, int index)
+/* Emit a DQT marker */
+/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
+{
+ JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
+ int prec;
+ int i;
+
+ if (qtbl == NULL)
+ ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
+
+ prec = 0;
+ for (i = 0; i < DCTSIZE2; i++) {
+ if (qtbl->quantval[i] > 255)
+ prec = 1;
+ }
+
+ if (! qtbl->sent_table) {
+ emit_marker(cinfo, M_DQT);
+
+ emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
+
+ emit_byte(cinfo, index + (prec<<4));
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ /* The table entries must be emitted in zigzag order. */
+ unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
+ if (prec)
+ emit_byte(cinfo, (int) (qval >> 8));
+ emit_byte(cinfo, (int) (qval & 0xFF));
+ }
+
+ qtbl->sent_table = TRUE;
+ }
+
+ return prec;
+}
+
+
+LOCAL(void)
+emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
+/* Emit a DHT marker */
+{
+ JHUFF_TBL * htbl;
+ int length, i;
+
+ if (is_ac) {
+ htbl = cinfo->ac_huff_tbl_ptrs[index];
+ index += 0x10; /* output index has AC bit set */
+ } else {
+ htbl = cinfo->dc_huff_tbl_ptrs[index];
+ }
+
+ if (htbl == NULL)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
+
+ if (! htbl->sent_table) {
+ emit_marker(cinfo, M_DHT);
+
+ length = 0;
+ for (i = 1; i <= 16; i++)
+ length += htbl->bits[i];
+
+ emit_2bytes(cinfo, length + 2 + 1 + 16);
+ emit_byte(cinfo, index);
+
+ for (i = 1; i <= 16; i++)
+ emit_byte(cinfo, htbl->bits[i]);
+
+ for (i = 0; i < length; i++)
+ emit_byte(cinfo, htbl->huffval[i]);
+
+ htbl->sent_table = TRUE;
+ }
+}
+
+
+LOCAL(void)
+emit_dac (j_compress_ptr cinfo)
+/* Emit a DAC marker */
+/* Since the useful info is so small, we want to emit all the tables in */
+/* one DAC marker. Therefore this routine does its own scan of the table. */
+{
+#ifdef C_ARITH_CODING_SUPPORTED
+ char dc_in_use[NUM_ARITH_TBLS];
+ char ac_in_use[NUM_ARITH_TBLS];
+ int length, i;
+ jpeg_component_info *compptr;
+
+ for (i = 0; i < NUM_ARITH_TBLS; i++)
+ dc_in_use[i] = ac_in_use[i] = 0;
+
+ for (i = 0; i < cinfo->comps_in_scan; i++) {
+ compptr = cinfo->cur_comp_info[i];
+ dc_in_use[compptr->dc_tbl_no] = 1;
+ ac_in_use[compptr->ac_tbl_no] = 1;
+ }
+
+ length = 0;
+ for (i = 0; i < NUM_ARITH_TBLS; i++)
+ length += dc_in_use[i] + ac_in_use[i];
+
+ emit_marker(cinfo, M_DAC);
+
+ emit_2bytes(cinfo, length*2 + 2);
+
+ for (i = 0; i < NUM_ARITH_TBLS; i++) {
+ if (dc_in_use[i]) {
+ emit_byte(cinfo, i);
+ emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
+ }
+ if (ac_in_use[i]) {
+ emit_byte(cinfo, i + 0x10);
+ emit_byte(cinfo, cinfo->arith_ac_K[i]);
+ }
+ }
+#endif /* C_ARITH_CODING_SUPPORTED */
+}
+
+
+LOCAL(void)
+emit_dri (j_compress_ptr cinfo)
+/* Emit a DRI marker */
+{
+ emit_marker(cinfo, M_DRI);
+
+ emit_2bytes(cinfo, 4); /* fixed length */
+
+ emit_2bytes(cinfo, (int) cinfo->restart_interval);
+}
+
+
+LOCAL(void)
+emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
+/* Emit a SOF marker */
+{
+ int ci;
+ jpeg_component_info *compptr;
+
+ emit_marker(cinfo, code);
+
+ emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
+
+ /* Make sure image isn't bigger than SOF field can handle */
+ if ((long) cinfo->image_height > 65535L ||
+ (long) cinfo->image_width > 65535L)
+ ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
+
+ emit_byte(cinfo, cinfo->data_precision);
+ emit_2bytes(cinfo, (int) cinfo->image_height);
+ emit_2bytes(cinfo, (int) cinfo->image_width);
+
+ emit_byte(cinfo, cinfo->num_components);
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ emit_byte(cinfo, compptr->component_id);
+ emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
+ emit_byte(cinfo, compptr->quant_tbl_no);
+ }
+}
+
+
+LOCAL(void)
+emit_sos (j_compress_ptr cinfo)
+/* Emit a SOS marker */
+{
+ int i, td, ta;
+ jpeg_component_info *compptr;
+
+ emit_marker(cinfo, M_SOS);
+
+ emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
+
+ emit_byte(cinfo, cinfo->comps_in_scan);
+
+ for (i = 0; i < cinfo->comps_in_scan; i++) {
+ compptr = cinfo->cur_comp_info[i];
+ emit_byte(cinfo, compptr->component_id);
+ td = compptr->dc_tbl_no;
+ ta = compptr->ac_tbl_no;
+ if (cinfo->progressive_mode) {
+ /* Progressive mode: only DC or only AC tables are used in one scan;
+ * furthermore, Huffman coding of DC refinement uses no table at all.
+ * We emit 0 for unused field(s); this is recommended by the P&M text
+ * but does not seem to be specified in the standard.
+ */
+ if (cinfo->Ss == 0) {
+ ta = 0; /* DC scan */
+ if (cinfo->Ah != 0 && !cinfo->arith_code)
+ td = 0; /* no DC table either */
+ } else {
+ td = 0; /* AC scan */
+ }
+ }
+ emit_byte(cinfo, (td << 4) + ta);
+ }
+
+ emit_byte(cinfo, cinfo->Ss);
+ emit_byte(cinfo, cinfo->Se);
+ emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
+}
+
+
+LOCAL(void)
+emit_jfif_app0 (j_compress_ptr cinfo)
+/* Emit a JFIF-compliant APP0 marker */
+{
+ /*
+ * Length of APP0 block (2 bytes)
+ * Block ID (4 bytes - ASCII "JFIF")
+ * Zero byte (1 byte to terminate the ID string)
+ * Version Major, Minor (2 bytes - major first)
+ * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
+ * Xdpu (2 bytes - dots per unit horizontal)
+ * Ydpu (2 bytes - dots per unit vertical)
+ * Thumbnail X size (1 byte)
+ * Thumbnail Y size (1 byte)
+ */
+
+ emit_marker(cinfo, M_APP0);
+
+ emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
+
+ emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
+ emit_byte(cinfo, 0x46);
+ emit_byte(cinfo, 0x49);
+ emit_byte(cinfo, 0x46);
+ emit_byte(cinfo, 0);
+ emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
+ emit_byte(cinfo, cinfo->JFIF_minor_version);
+ emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
+ emit_2bytes(cinfo, (int) cinfo->X_density);
+ emit_2bytes(cinfo, (int) cinfo->Y_density);
+ emit_byte(cinfo, 0); /* No thumbnail image */
+ emit_byte(cinfo, 0);
+}
+
+
+LOCAL(void)
+emit_adobe_app14 (j_compress_ptr cinfo)
+/* Emit an Adobe APP14 marker */
+{
+ /*
+ * Length of APP14 block (2 bytes)
+ * Block ID (5 bytes - ASCII "Adobe")
+ * Version Number (2 bytes - currently 100)
+ * Flags0 (2 bytes - currently 0)
+ * Flags1 (2 bytes - currently 0)
+ * Color transform (1 byte)
+ *
+ * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
+ * now in circulation seem to use Version = 100, so that's what we write.
+ *
+ * We write the color transform byte as 1 if the JPEG color space is
+ * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
+ * whether the encoder performed a transformation, which is pretty useless.
+ */
+
+ emit_marker(cinfo, M_APP14);
+
+ emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
+
+ emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
+ emit_byte(cinfo, 0x64);
+ emit_byte(cinfo, 0x6F);
+ emit_byte(cinfo, 0x62);
+ emit_byte(cinfo, 0x65);
+ emit_2bytes(cinfo, 100); /* Version */
+ emit_2bytes(cinfo, 0); /* Flags0 */
+ emit_2bytes(cinfo, 0); /* Flags1 */
+ switch (cinfo->jpeg_color_space) {
+ case JCS_YCbCr:
+ emit_byte(cinfo, 1); /* Color transform = 1 */
+ break;
+ case JCS_YCCK:
+ emit_byte(cinfo, 2); /* Color transform = 2 */
+ break;
+ default:
+ emit_byte(cinfo, 0); /* Color transform = 0 */
+ break;
+ }
+}
+
+
+/*
+ * These routines allow writing an arbitrary marker with parameters.
+ * The only intended use is to emit COM or APPn markers after calling
+ * write_file_header and before calling write_frame_header.
+ * Other uses are not guaranteed to produce desirable results.
+ * Counting the parameter bytes properly is the caller's responsibility.
+ */
+
+METHODDEF(void)
+write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
+/* Emit an arbitrary marker header */
+{
+ if (datalen > (unsigned int) 65533) /* safety check */
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ emit_marker(cinfo, (JPEG_MARKER) marker);
+
+ emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
+}
+
+METHODDEF(void)
+write_marker_byte (j_compress_ptr cinfo, int val)
+/* Emit one byte of marker parameters following write_marker_header */
+{
+ emit_byte(cinfo, val);
+}
+
+
+/*
+ * Write datastream header.
+ * This consists of an SOI and optional APPn markers.
+ * We recommend use of the JFIF marker, but not the Adobe marker,
+ * when using YCbCr or grayscale data. The JFIF marker should NOT
+ * be used for any other JPEG colorspace. The Adobe marker is helpful
+ * to distinguish RGB, CMYK, and YCCK colorspaces.
+ * Note that an application can write additional header markers after
+ * jpeg_start_compress returns.
+ */
+
+METHODDEF(void)
+write_file_header (j_compress_ptr cinfo)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+
+ emit_marker(cinfo, M_SOI); /* first the SOI */
+
+ /* SOI is defined to reset restart interval to 0 */
+ marker->last_restart_interval = 0;
+
+ if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
+ emit_jfif_app0(cinfo);
+ if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
+ emit_adobe_app14(cinfo);
+}
+
+
+/*
+ * Write frame header.
+ * This consists of DQT and SOFn markers.
+ * Note that we do not emit the SOF until we have emitted the DQT(s).
+ * This avoids compatibility problems with incorrect implementations that
+ * try to error-check the quant table numbers as soon as they see the SOF.
+ */
+
+METHODDEF(void)
+write_frame_header (j_compress_ptr cinfo)
+{
+ int ci, prec;
+ boolean is_baseline;
+ jpeg_component_info *compptr;
+
+ /* Emit DQT for each quantization table.
+ * Note that emit_dqt() suppresses any duplicate tables.
+ */
+ prec = 0;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ prec += emit_dqt(cinfo, compptr->quant_tbl_no);
+ }
+ /* now prec is nonzero iff there are any 16-bit quant tables. */
+
+ /* Check for a non-baseline specification.
+ * Note we assume that Huffman table numbers won't be changed later.
+ */
+ if (cinfo->arith_code || cinfo->progressive_mode ||
+ cinfo->data_precision != 8) {
+ is_baseline = FALSE;
+ } else {
+ is_baseline = TRUE;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
+ is_baseline = FALSE;
+ }
+ if (prec && is_baseline) {
+ is_baseline = FALSE;
+ /* If it's baseline except for quantizer size, warn the user */
+ TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
+ }
+ }
+
+ /* Emit the proper SOF marker */
+ if (cinfo->arith_code) {
+ emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */
+ } else {
+ if (cinfo->progressive_mode)
+ emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
+ else if (is_baseline)
+ emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
+ else
+ emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
+ }
+}
+
+
+/*
+ * Write scan header.
+ * This consists of DHT or DAC markers, optional DRI, and SOS.
+ * Compressed data will be written following the SOS.
+ */
+
+METHODDEF(void)
+write_scan_header (j_compress_ptr cinfo)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+ int i;
+ jpeg_component_info *compptr;
+
+ if (cinfo->arith_code) {
+ /* Emit arith conditioning info. We may have some duplication
+ * if the file has multiple scans, but it's so small it's hardly
+ * worth worrying about.
+ */
+ emit_dac(cinfo);
+ } else {
+ /* Emit Huffman tables.
+ * Note that emit_dht() suppresses any duplicate tables.
+ */
+ for (i = 0; i < cinfo->comps_in_scan; i++) {
+ compptr = cinfo->cur_comp_info[i];
+ if (cinfo->progressive_mode) {
+ /* Progressive mode: only DC or only AC tables are used in one scan */
+ if (cinfo->Ss == 0) {
+ if (cinfo->Ah == 0) /* DC needs no table for refinement scan */
+ emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
+ } else {
+ emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
+ }
+ } else {
+ /* Sequential mode: need both DC and AC tables */
+ emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
+ emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
+ }
+ }
+ }
+
+ /* Emit DRI if required --- note that DRI value could change for each scan.
+ * We avoid wasting space with unnecessary DRIs, however.
+ */
+ if (cinfo->restart_interval != marker->last_restart_interval) {
+ emit_dri(cinfo);
+ marker->last_restart_interval = cinfo->restart_interval;
+ }
+
+ emit_sos(cinfo);
+}
+
+
+/*
+ * Write datastream trailer.
+ */
+
+METHODDEF(void)
+write_file_trailer (j_compress_ptr cinfo)
+{
+ emit_marker(cinfo, M_EOI);
+}
+
+
+/*
+ * Write an abbreviated table-specification datastream.
+ * This consists of SOI, DQT and DHT tables, and EOI.
+ * Any table that is defined and not marked sent_table = TRUE will be
+ * emitted. Note that all tables will be marked sent_table = TRUE at exit.
+ */
+
+METHODDEF(void)
+write_tables_only (j_compress_ptr cinfo)
+{
+ int i;
+
+ emit_marker(cinfo, M_SOI);
+
+ for (i = 0; i < NUM_QUANT_TBLS; i++) {
+ if (cinfo->quant_tbl_ptrs[i] != NULL)
+ (void) emit_dqt(cinfo, i);
+ }
+
+ if (! cinfo->arith_code) {
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
+ emit_dht(cinfo, i, FALSE);
+ if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
+ emit_dht(cinfo, i, TRUE);
+ }
+ }
+
+ emit_marker(cinfo, M_EOI);
+}
+
+
+/*
+ * Initialize the marker writer module.
+ */
+
+GLOBAL(void)
+jinit_marker_writer (j_compress_ptr cinfo)
+{
+ my_marker_ptr marker;
+
+ /* Create the subobject */
+ marker = (my_marker_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_marker_writer));
+ cinfo->marker = (struct jpeg_marker_writer *) marker;
+ /* Initialize method pointers */
+ marker->pub.write_file_header = write_file_header;
+ marker->pub.write_frame_header = write_frame_header;
+ marker->pub.write_scan_header = write_scan_header;
+ marker->pub.write_file_trailer = write_file_trailer;
+ marker->pub.write_tables_only = write_tables_only;
+ marker->pub.write_marker_header = write_marker_header;
+ marker->pub.write_marker_byte = write_marker_byte;
+ /* Initialize private state */
+ marker->last_restart_interval = 0;
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c b/core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c
index 870f775191..45322a53b3 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcmaster.c
@@ -1,593 +1,593 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcmaster.c
- *
- * 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 master control logic for the JPEG compressor.
- * These routines are concerned with parameter validation, initial setup,
- * and inter-pass control (determining the number of passes and the work
- * to be done in each pass).
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Private state */
-
-typedef enum {
- main_pass, /* input data, also do first output step */
- huff_opt_pass, /* Huffman code optimization pass */
- output_pass /* data output pass */
-} c_pass_type;
-
-typedef struct {
- struct jpeg_comp_master pub; /* public fields */
-
- c_pass_type pass_type; /* the type of the current pass */
-
- int pass_number; /* # of passes completed */
- int total_passes; /* total # of passes needed */
-
- int scan_number; /* current index in scan_info[] */
-} my_comp_master;
-
-typedef my_comp_master * my_master_ptr;
-
-
-/*
- * Support routines that do various essential calculations.
- */
-
-LOCAL(void)
-initial_setup (j_compress_ptr cinfo)
-/* Do computations that are needed before master selection phase */
-{
- int ci;
- jpeg_component_info *compptr;
- long samplesperrow;
- JDIMENSION jd_samplesperrow;
-
- /* Sanity check on image dimensions */
- if (cinfo->image_height <= 0 || cinfo->image_width <= 0
- || cinfo->num_components <= 0 || cinfo->input_components <= 0)
- ERREXIT(cinfo, JERR_EMPTY_IMAGE);
-
- /* Make sure image isn't bigger than I can handle */
- if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
- (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
- ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
-
- /* Width of an input scanline must be representable as JDIMENSION. */
- samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
- jd_samplesperrow = (JDIMENSION) samplesperrow;
- if ((long) jd_samplesperrow != samplesperrow)
- ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
-
- /* For now, precision must match compiled-in value... */
- if (cinfo->data_precision != BITS_IN_JSAMPLE)
- ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
-
- /* Check that number of components won't exceed internal array sizes */
- if (cinfo->num_components > MAX_COMPONENTS)
- ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
- MAX_COMPONENTS);
-
- /* Compute maximum sampling factors; check factor validity */
- cinfo->max_h_samp_factor = 1;
- cinfo->max_v_samp_factor = 1;
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
- compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
- ERREXIT(cinfo, JERR_BAD_SAMPLING);
- cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
- compptr->h_samp_factor);
- cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
- compptr->v_samp_factor);
- }
-
- /* Compute dimensions of components */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Fill in the correct component_index value; don't rely on application */
- compptr->component_index = ci;
- /* For compression, we never do DCT scaling. */
- compptr->DCT_scaled_size = DCTSIZE;
- /* Size in DCT blocks */
- compptr->width_in_blocks = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
- (long) (cinfo->max_h_samp_factor * DCTSIZE));
- compptr->height_in_blocks = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
- (long) (cinfo->max_v_samp_factor * DCTSIZE));
- /* Size in samples */
- compptr->downsampled_width = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
- (long) cinfo->max_h_samp_factor);
- compptr->downsampled_height = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
- (long) cinfo->max_v_samp_factor);
- /* Mark component needed (this flag isn't actually used for compression) */
- compptr->component_needed = TRUE;
- }
-
- /* Compute number of fully interleaved MCU rows (number of times that
- * main controller will call coefficient controller).
- */
- cinfo->total_iMCU_rows = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height,
- (long) (cinfo->max_v_samp_factor*DCTSIZE));
-}
-
-
-#ifdef C_MULTISCAN_FILES_SUPPORTED
-
-LOCAL(void)
-validate_script (j_compress_ptr cinfo)
-/* Verify that the scan script in cinfo->scan_info[] is valid; also
- * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
- */
-{
- const jpeg_scan_info * scanptr;
- int scanno, ncomps, ci, coefi, thisi;
- int Ss, Se, Ah, Al;
- boolean component_sent[MAX_COMPONENTS];
-#ifdef C_PROGRESSIVE_SUPPORTED
- int * last_bitpos_ptr;
- int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
- /* -1 until that coefficient has been seen; then last Al for it */
-#endif
-
- if (cinfo->num_scans <= 0)
- ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
-
- /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
- * for progressive JPEG, no scan can have this.
- */
- scanptr = cinfo->scan_info;
- if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
-#ifdef C_PROGRESSIVE_SUPPORTED
- cinfo->progressive_mode = TRUE;
- last_bitpos_ptr = & last_bitpos[0][0];
- for (ci = 0; ci < cinfo->num_components; ci++)
- for (coefi = 0; coefi < DCTSIZE2; coefi++)
- *last_bitpos_ptr++ = -1;
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else {
- cinfo->progressive_mode = FALSE;
- for (ci = 0; ci < cinfo->num_components; ci++)
- component_sent[ci] = FALSE;
- }
-
- for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
- /* Validate component indexes */
- ncomps = scanptr->comps_in_scan;
- if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
- ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
- for (ci = 0; ci < ncomps; ci++) {
- thisi = scanptr->component_index[ci];
- if (thisi < 0 || thisi >= cinfo->num_components)
- ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
- /* Components must appear in SOF order within each scan */
- if (ci > 0 && thisi <= scanptr->component_index[ci-1])
- ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
- }
- /* Validate progression parameters */
- Ss = scanptr->Ss;
- Se = scanptr->Se;
- Ah = scanptr->Ah;
- Al = scanptr->Al;
- if (cinfo->progressive_mode) {
-#ifdef C_PROGRESSIVE_SUPPORTED
- /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
- * seems wrong: the upper bound ought to depend on data precision.
- * Perhaps they really meant 0..N+1 for N-bit precision.
- * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
- * out-of-range reconstructed DC values during the first DC scan,
- * which might cause problems for some decoders.
- */
-#if BITS_IN_JSAMPLE == 8
-#define MAX_AH_AL 10
-#else
-#define MAX_AH_AL 13
-#endif
- if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
- Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
- ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
- if (Ss == 0) {
- if (Se != 0) /* DC and AC together not OK */
- ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
- } else {
- if (ncomps != 1) /* AC scans must be for only one component */
- ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
- }
- for (ci = 0; ci < ncomps; ci++) {
- last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
- if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
- ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
- for (coefi = Ss; coefi <= Se; coefi++) {
- if (last_bitpos_ptr[coefi] < 0) {
- /* first scan of this coefficient */
- if (Ah != 0)
- ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
- } else {
- /* not first scan */
- if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
- ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
- }
- last_bitpos_ptr[coefi] = Al;
- }
- }
-#endif
- } else {
- /* For sequential JPEG, all progression parameters must be these: */
- if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
- ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
- /* Make sure components are not sent twice */
- for (ci = 0; ci < ncomps; ci++) {
- thisi = scanptr->component_index[ci];
- if (component_sent[thisi])
- ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
- component_sent[thisi] = TRUE;
- }
- }
- }
-
- /* Now verify that everything got sent. */
- if (cinfo->progressive_mode) {
-#ifdef C_PROGRESSIVE_SUPPORTED
- /* For progressive mode, we only check that at least some DC data
- * got sent for each component; the spec does not require that all bits
- * of all coefficients be transmitted. Would it be wiser to enforce
- * transmission of all coefficient bits??
- */
- for (ci = 0; ci < cinfo->num_components; ci++) {
- if (last_bitpos[ci][0] < 0)
- ERREXIT(cinfo, JERR_MISSING_DATA);
- }
-#endif
- } else {
- for (ci = 0; ci < cinfo->num_components; ci++) {
- if (! component_sent[ci])
- ERREXIT(cinfo, JERR_MISSING_DATA);
- }
- }
-}
-
-#endif /* C_MULTISCAN_FILES_SUPPORTED */
-
-
-LOCAL(void)
-select_scan_parameters (j_compress_ptr cinfo)
-/* Set up the scan parameters for the current scan */
-{
- int ci;
-
-#ifdef C_MULTISCAN_FILES_SUPPORTED
- if (cinfo->scan_info != NULL) {
- /* Prepare for current scan --- the script is already validated */
- my_master_ptr master = (my_master_ptr) cinfo->master;
- const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
-
- cinfo->comps_in_scan = scanptr->comps_in_scan;
- for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
- cinfo->cur_comp_info[ci] =
- &cinfo->comp_info[scanptr->component_index[ci]];
- }
- cinfo->Ss = scanptr->Ss;
- cinfo->Se = scanptr->Se;
- cinfo->Ah = scanptr->Ah;
- cinfo->Al = scanptr->Al;
- }
- else
-#endif
- {
- /* Prepare for single sequential-JPEG scan containing all components */
- if (cinfo->num_components > MAX_COMPS_IN_SCAN)
- ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
- MAX_COMPS_IN_SCAN);
- cinfo->comps_in_scan = cinfo->num_components;
- for (ci = 0; ci < cinfo->num_components; ci++) {
- cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
- }
- cinfo->Ss = 0;
- cinfo->Se = DCTSIZE2-1;
- cinfo->Ah = 0;
- cinfo->Al = 0;
- }
-}
-
-
-LOCAL(void)
-per_scan_setup (j_compress_ptr cinfo)
-/* Do computations that are needed before processing a JPEG scan */
-/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
-{
- int ci, mcublks, tmp;
- jpeg_component_info *compptr;
-
- if (cinfo->comps_in_scan == 1) {
-
- /* Noninterleaved (single-component) scan */
- compptr = cinfo->cur_comp_info[0];
-
- /* Overall image size in MCUs */
- cinfo->MCUs_per_row = compptr->width_in_blocks;
- cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
-
- /* For noninterleaved scan, always one block per MCU */
- compptr->MCU_width = 1;
- compptr->MCU_height = 1;
- compptr->MCU_blocks = 1;
- compptr->MCU_sample_width = DCTSIZE;
- compptr->last_col_width = 1;
- /* For noninterleaved scans, it is convenient to define last_row_height
- * as the number of block rows present in the last iMCU row.
- */
- tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
- if (tmp == 0) tmp = compptr->v_samp_factor;
- compptr->last_row_height = tmp;
-
- /* Prepare array describing MCU composition */
- cinfo->blocks_in_MCU = 1;
- cinfo->MCU_membership[0] = 0;
-
- } else {
-
- /* Interleaved (multi-component) scan */
- if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
- ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
- MAX_COMPS_IN_SCAN);
-
- /* Overall image size in MCUs */
- cinfo->MCUs_per_row = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width,
- (long) (cinfo->max_h_samp_factor*DCTSIZE));
- cinfo->MCU_rows_in_scan = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height,
- (long) (cinfo->max_v_samp_factor*DCTSIZE));
-
- cinfo->blocks_in_MCU = 0;
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- /* Sampling factors give # of blocks of component in each MCU */
- compptr->MCU_width = compptr->h_samp_factor;
- compptr->MCU_height = compptr->v_samp_factor;
- compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
- compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
- /* Figure number of non-dummy blocks in last MCU column & row */
- tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
- if (tmp == 0) tmp = compptr->MCU_width;
- compptr->last_col_width = tmp;
- tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
- if (tmp == 0) tmp = compptr->MCU_height;
- compptr->last_row_height = tmp;
- /* Prepare array describing MCU composition */
- mcublks = compptr->MCU_blocks;
- if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
- ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
- while (mcublks-- > 0) {
- cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
- }
- }
-
- }
-
- /* Convert restart specified in rows to actual MCU count. */
- /* Note that count must fit in 16 bits, so we provide limiting. */
- if (cinfo->restart_in_rows > 0) {
- long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
- cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
- }
-}
-
-
-/*
- * Per-pass setup.
- * This is called at the beginning of each pass. We determine which modules
- * will be active during this pass and give them appropriate start_pass calls.
- * We also set is_last_pass to indicate whether any more passes will be
- * required.
- */
-
-METHODDEF(void)
-prepare_for_pass (j_compress_ptr cinfo)
-{
- my_master_ptr master = (my_master_ptr) cinfo->master;
-
- switch (master->pass_type) {
- case main_pass:
- /* Initial pass: will collect input data, and do either Huffman
- * optimization or data output for the first scan.
- */
- select_scan_parameters(cinfo);
- per_scan_setup(cinfo);
- if (! cinfo->raw_data_in) {
- (*cinfo->cconvert->start_pass) (cinfo);
- (*cinfo->downsample->start_pass) (cinfo);
- (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
- }
- (*cinfo->fdct->start_pass) (cinfo);
- (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
- (*cinfo->coef->start_pass) (cinfo,
- (master->total_passes > 1 ?
- JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
- (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
- if (cinfo->optimize_coding) {
- /* No immediate data output; postpone writing frame/scan headers */
- master->pub.call_pass_startup = FALSE;
- } else {
- /* Will write frame/scan headers at first jpeg_write_scanlines call */
- master->pub.call_pass_startup = TRUE;
- }
- break;
-#ifdef ENTROPY_OPT_SUPPORTED
- case huff_opt_pass:
- /* Do Huffman optimization for a scan after the first one. */
- select_scan_parameters(cinfo);
- per_scan_setup(cinfo);
- if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
- (*cinfo->entropy->start_pass) (cinfo, TRUE);
- (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
- master->pub.call_pass_startup = FALSE;
- break;
- }
- /* Special case: Huffman DC refinement scans need no Huffman table
- * and therefore we can skip the optimization pass for them.
- */
- master->pass_type = output_pass;
- master->pass_number++;
- /*FALLTHROUGH*/
-#endif
- case output_pass:
- /* Do a data-output pass. */
- /* We need not repeat per-scan setup if prior optimization pass did it. */
- if (! cinfo->optimize_coding) {
- select_scan_parameters(cinfo);
- per_scan_setup(cinfo);
- }
- (*cinfo->entropy->start_pass) (cinfo, FALSE);
- (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
- /* We emit frame/scan headers now */
- if (master->scan_number == 0)
- (*cinfo->marker->write_frame_header) (cinfo);
- (*cinfo->marker->write_scan_header) (cinfo);
- master->pub.call_pass_startup = FALSE;
- break;
- default:
- ERREXIT(cinfo, JERR_NOT_COMPILED);
- }
-
- master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
-
- /* Set up progress monitor's pass info if present */
- if (cinfo->progress != NULL) {
- cinfo->progress->completed_passes = master->pass_number;
- cinfo->progress->total_passes = master->total_passes;
- }
-}
-
-
-/*
- * Special start-of-pass hook.
- * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
- * In single-pass processing, we need this hook because we don't want to
- * write frame/scan headers during jpeg_start_compress; we want to let the
- * application write COM markers etc. between jpeg_start_compress and the
- * jpeg_write_scanlines loop.
- * In multi-pass processing, this routine is not used.
- */
-
-METHODDEF(void)
-pass_startup (j_compress_ptr cinfo)
-{
- cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
-
- (*cinfo->marker->write_frame_header) (cinfo);
- (*cinfo->marker->write_scan_header) (cinfo);
-}
-
-
-/*
- * Finish up at end of pass.
- */
-
-METHODDEF(void)
-finish_pass_master (j_compress_ptr cinfo)
-{
- my_master_ptr master = (my_master_ptr) cinfo->master;
-
- /* The entropy coder always needs an end-of-pass call,
- * either to analyze statistics or to flush its output buffer.
- */
- (*cinfo->entropy->finish_pass) (cinfo);
-
- /* Update state for next pass */
- switch (master->pass_type) {
- case main_pass:
- /* next pass is either output of scan 0 (after optimization)
- * or output of scan 1 (if no optimization).
- */
- master->pass_type = output_pass;
- if (! cinfo->optimize_coding)
- master->scan_number++;
- break;
- case huff_opt_pass:
- /* next pass is always output of current scan */
- master->pass_type = output_pass;
- break;
- case output_pass:
- /* next pass is either optimization or output of next scan */
- if (cinfo->optimize_coding)
- master->pass_type = huff_opt_pass;
- master->scan_number++;
- break;
- }
-
- master->pass_number++;
-}
-
-
-/*
- * Initialize master compression control.
- */
-
-GLOBAL(void)
-jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
-{
- my_master_ptr master;
-
- master = (my_master_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_comp_master));
- cinfo->master = (struct jpeg_comp_master *) master;
- master->pub.prepare_for_pass = prepare_for_pass;
- master->pub.pass_startup = pass_startup;
- master->pub.finish_pass = finish_pass_master;
- master->pub.is_last_pass = FALSE;
-
- /* Validate parameters, determine derived values */
- initial_setup(cinfo);
-
- if (cinfo->scan_info != NULL) {
-#ifdef C_MULTISCAN_FILES_SUPPORTED
- validate_script(cinfo);
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else {
- cinfo->progressive_mode = FALSE;
- cinfo->num_scans = 1;
- }
-
- if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */
- cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
-
- /* Initialize my private state */
- if (transcode_only) {
- /* no main pass in transcoding */
- if (cinfo->optimize_coding)
- master->pass_type = huff_opt_pass;
- else
- master->pass_type = output_pass;
- } else {
- /* for normal compression, first pass is always this type: */
- master->pass_type = main_pass;
- }
- master->scan_number = 0;
- master->pass_number = 0;
- if (cinfo->optimize_coding)
- master->total_passes = cinfo->num_scans * 2;
- else
- master->total_passes = cinfo->num_scans;
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcmaster.c
+ *
+ * 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 master control logic for the JPEG compressor.
+ * These routines are concerned with parameter validation, initial setup,
+ * and inter-pass control (determining the number of passes and the work
+ * to be done in each pass).
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private state */
+
+typedef enum {
+ main_pass, /* input data, also do first output step */
+ huff_opt_pass, /* Huffman code optimization pass */
+ output_pass /* data output pass */
+} c_pass_type;
+
+typedef struct {
+ struct jpeg_comp_master pub; /* public fields */
+
+ c_pass_type pass_type; /* the type of the current pass */
+
+ int pass_number; /* # of passes completed */
+ int total_passes; /* total # of passes needed */
+
+ int scan_number; /* current index in scan_info[] */
+} my_comp_master;
+
+typedef my_comp_master * my_master_ptr;
+
+
+/*
+ * Support routines that do various essential calculations.
+ */
+
+LOCAL(void)
+initial_setup (j_compress_ptr cinfo)
+/* Do computations that are needed before master selection phase */
+{
+ int ci;
+ jpeg_component_info *compptr;
+ long samplesperrow;
+ JDIMENSION jd_samplesperrow;
+
+ /* Sanity check on image dimensions */
+ if (cinfo->image_height <= 0 || cinfo->image_width <= 0
+ || cinfo->num_components <= 0 || cinfo->input_components <= 0)
+ ERREXIT(cinfo, JERR_EMPTY_IMAGE);
+
+ /* Make sure image isn't bigger than I can handle */
+ if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
+ (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
+ ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
+
+ /* Width of an input scanline must be representable as JDIMENSION. */
+ samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
+ jd_samplesperrow = (JDIMENSION) samplesperrow;
+ if ((long) jd_samplesperrow != samplesperrow)
+ ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+
+ /* For now, precision must match compiled-in value... */
+ if (cinfo->data_precision != BITS_IN_JSAMPLE)
+ ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
+
+ /* Check that number of components won't exceed internal array sizes */
+ if (cinfo->num_components > MAX_COMPONENTS)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+ MAX_COMPONENTS);
+
+ /* Compute maximum sampling factors; check factor validity */
+ cinfo->max_h_samp_factor = 1;
+ cinfo->max_v_samp_factor = 1;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
+ compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
+ ERREXIT(cinfo, JERR_BAD_SAMPLING);
+ cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
+ compptr->h_samp_factor);
+ cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
+ compptr->v_samp_factor);
+ }
+
+ /* Compute dimensions of components */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Fill in the correct component_index value; don't rely on application */
+ compptr->component_index = ci;
+ /* For compression, we never do DCT scaling. */
+ compptr->DCT_scaled_size = DCTSIZE;
+ /* Size in DCT blocks */
+ compptr->width_in_blocks = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+ (long) (cinfo->max_h_samp_factor * DCTSIZE));
+ compptr->height_in_blocks = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+ (long) (cinfo->max_v_samp_factor * DCTSIZE));
+ /* Size in samples */
+ compptr->downsampled_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+ (long) cinfo->max_h_samp_factor);
+ compptr->downsampled_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+ (long) cinfo->max_v_samp_factor);
+ /* Mark component needed (this flag isn't actually used for compression) */
+ compptr->component_needed = TRUE;
+ }
+
+ /* Compute number of fully interleaved MCU rows (number of times that
+ * main controller will call coefficient controller).
+ */
+ cinfo->total_iMCU_rows = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height,
+ (long) (cinfo->max_v_samp_factor*DCTSIZE));
+}
+
+
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+
+LOCAL(void)
+validate_script (j_compress_ptr cinfo)
+/* Verify that the scan script in cinfo->scan_info[] is valid; also
+ * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
+ */
+{
+ const jpeg_scan_info * scanptr;
+ int scanno, ncomps, ci, coefi, thisi;
+ int Ss, Se, Ah, Al;
+ boolean component_sent[MAX_COMPONENTS];
+#ifdef C_PROGRESSIVE_SUPPORTED
+ int * last_bitpos_ptr;
+ int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
+ /* -1 until that coefficient has been seen; then last Al for it */
+#endif
+
+ if (cinfo->num_scans <= 0)
+ ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
+
+ /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
+ * for progressive JPEG, no scan can have this.
+ */
+ scanptr = cinfo->scan_info;
+ if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ cinfo->progressive_mode = TRUE;
+ last_bitpos_ptr = & last_bitpos[0][0];
+ for (ci = 0; ci < cinfo->num_components; ci++)
+ for (coefi = 0; coefi < DCTSIZE2; coefi++)
+ *last_bitpos_ptr++ = -1;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ cinfo->progressive_mode = FALSE;
+ for (ci = 0; ci < cinfo->num_components; ci++)
+ component_sent[ci] = FALSE;
+ }
+
+ for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
+ /* Validate component indexes */
+ ncomps = scanptr->comps_in_scan;
+ if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
+ for (ci = 0; ci < ncomps; ci++) {
+ thisi = scanptr->component_index[ci];
+ if (thisi < 0 || thisi >= cinfo->num_components)
+ ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+ /* Components must appear in SOF order within each scan */
+ if (ci > 0 && thisi <= scanptr->component_index[ci-1])
+ ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+ }
+ /* Validate progression parameters */
+ Ss = scanptr->Ss;
+ Se = scanptr->Se;
+ Ah = scanptr->Ah;
+ Al = scanptr->Al;
+ if (cinfo->progressive_mode) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
+ * seems wrong: the upper bound ought to depend on data precision.
+ * Perhaps they really meant 0..N+1 for N-bit precision.
+ * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
+ * out-of-range reconstructed DC values during the first DC scan,
+ * which might cause problems for some decoders.
+ */
+#if BITS_IN_JSAMPLE == 8
+#define MAX_AH_AL 10
+#else
+#define MAX_AH_AL 13
+#endif
+ if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
+ Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ if (Ss == 0) {
+ if (Se != 0) /* DC and AC together not OK */
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ } else {
+ if (ncomps != 1) /* AC scans must be for only one component */
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ }
+ for (ci = 0; ci < ncomps; ci++) {
+ last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
+ if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ for (coefi = Ss; coefi <= Se; coefi++) {
+ if (last_bitpos_ptr[coefi] < 0) {
+ /* first scan of this coefficient */
+ if (Ah != 0)
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ } else {
+ /* not first scan */
+ if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ }
+ last_bitpos_ptr[coefi] = Al;
+ }
+ }
+#endif
+ } else {
+ /* For sequential JPEG, all progression parameters must be these: */
+ if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
+ ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
+ /* Make sure components are not sent twice */
+ for (ci = 0; ci < ncomps; ci++) {
+ thisi = scanptr->component_index[ci];
+ if (component_sent[thisi])
+ ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
+ component_sent[thisi] = TRUE;
+ }
+ }
+ }
+
+ /* Now verify that everything got sent. */
+ if (cinfo->progressive_mode) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ /* For progressive mode, we only check that at least some DC data
+ * got sent for each component; the spec does not require that all bits
+ * of all coefficients be transmitted. Would it be wiser to enforce
+ * transmission of all coefficient bits??
+ */
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ if (last_bitpos[ci][0] < 0)
+ ERREXIT(cinfo, JERR_MISSING_DATA);
+ }
+#endif
+ } else {
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ if (! component_sent[ci])
+ ERREXIT(cinfo, JERR_MISSING_DATA);
+ }
+ }
+}
+
+#endif /* C_MULTISCAN_FILES_SUPPORTED */
+
+
+LOCAL(void)
+select_scan_parameters (j_compress_ptr cinfo)
+/* Set up the scan parameters for the current scan */
+{
+ int ci;
+
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ if (cinfo->scan_info != NULL) {
+ /* Prepare for current scan --- the script is already validated */
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+ const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
+
+ cinfo->comps_in_scan = scanptr->comps_in_scan;
+ for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
+ cinfo->cur_comp_info[ci] =
+ &cinfo->comp_info[scanptr->component_index[ci]];
+ }
+ cinfo->Ss = scanptr->Ss;
+ cinfo->Se = scanptr->Se;
+ cinfo->Ah = scanptr->Ah;
+ cinfo->Al = scanptr->Al;
+ }
+ else
+#endif
+ {
+ /* Prepare for single sequential-JPEG scan containing all components */
+ if (cinfo->num_components > MAX_COMPS_IN_SCAN)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+ MAX_COMPS_IN_SCAN);
+ cinfo->comps_in_scan = cinfo->num_components;
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
+ }
+ cinfo->Ss = 0;
+ cinfo->Se = DCTSIZE2-1;
+ cinfo->Ah = 0;
+ cinfo->Al = 0;
+ }
+}
+
+
+LOCAL(void)
+per_scan_setup (j_compress_ptr cinfo)
+/* Do computations that are needed before processing a JPEG scan */
+/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
+{
+ int ci, mcublks, tmp;
+ jpeg_component_info *compptr;
+
+ if (cinfo->comps_in_scan == 1) {
+
+ /* Noninterleaved (single-component) scan */
+ compptr = cinfo->cur_comp_info[0];
+
+ /* Overall image size in MCUs */
+ cinfo->MCUs_per_row = compptr->width_in_blocks;
+ cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
+
+ /* For noninterleaved scan, always one block per MCU */
+ compptr->MCU_width = 1;
+ compptr->MCU_height = 1;
+ compptr->MCU_blocks = 1;
+ compptr->MCU_sample_width = DCTSIZE;
+ compptr->last_col_width = 1;
+ /* For noninterleaved scans, it is convenient to define last_row_height
+ * as the number of block rows present in the last iMCU row.
+ */
+ tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (tmp == 0) tmp = compptr->v_samp_factor;
+ compptr->last_row_height = tmp;
+
+ /* Prepare array describing MCU composition */
+ cinfo->blocks_in_MCU = 1;
+ cinfo->MCU_membership[0] = 0;
+
+ } else {
+
+ /* Interleaved (multi-component) scan */
+ if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
+ MAX_COMPS_IN_SCAN);
+
+ /* Overall image size in MCUs */
+ cinfo->MCUs_per_row = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width,
+ (long) (cinfo->max_h_samp_factor*DCTSIZE));
+ cinfo->MCU_rows_in_scan = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height,
+ (long) (cinfo->max_v_samp_factor*DCTSIZE));
+
+ cinfo->blocks_in_MCU = 0;
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* Sampling factors give # of blocks of component in each MCU */
+ compptr->MCU_width = compptr->h_samp_factor;
+ compptr->MCU_height = compptr->v_samp_factor;
+ compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
+ compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
+ /* Figure number of non-dummy blocks in last MCU column & row */
+ tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
+ if (tmp == 0) tmp = compptr->MCU_width;
+ compptr->last_col_width = tmp;
+ tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
+ if (tmp == 0) tmp = compptr->MCU_height;
+ compptr->last_row_height = tmp;
+ /* Prepare array describing MCU composition */
+ mcublks = compptr->MCU_blocks;
+ if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
+ ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
+ while (mcublks-- > 0) {
+ cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
+ }
+ }
+
+ }
+
+ /* Convert restart specified in rows to actual MCU count. */
+ /* Note that count must fit in 16 bits, so we provide limiting. */
+ if (cinfo->restart_in_rows > 0) {
+ long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
+ cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
+ }
+}
+
+
+/*
+ * Per-pass setup.
+ * This is called at the beginning of each pass. We determine which modules
+ * will be active during this pass and give them appropriate start_pass calls.
+ * We also set is_last_pass to indicate whether any more passes will be
+ * required.
+ */
+
+METHODDEF(void)
+prepare_for_pass (j_compress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ switch (master->pass_type) {
+ case main_pass:
+ /* Initial pass: will collect input data, and do either Huffman
+ * optimization or data output for the first scan.
+ */
+ select_scan_parameters(cinfo);
+ per_scan_setup(cinfo);
+ if (! cinfo->raw_data_in) {
+ (*cinfo->cconvert->start_pass) (cinfo);
+ (*cinfo->downsample->start_pass) (cinfo);
+ (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
+ }
+ (*cinfo->fdct->start_pass) (cinfo);
+ (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
+ (*cinfo->coef->start_pass) (cinfo,
+ (master->total_passes > 1 ?
+ JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
+ (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
+ if (cinfo->optimize_coding) {
+ /* No immediate data output; postpone writing frame/scan headers */
+ master->pub.call_pass_startup = FALSE;
+ } else {
+ /* Will write frame/scan headers at first jpeg_write_scanlines call */
+ master->pub.call_pass_startup = TRUE;
+ }
+ break;
+#ifdef ENTROPY_OPT_SUPPORTED
+ case huff_opt_pass:
+ /* Do Huffman optimization for a scan after the first one. */
+ select_scan_parameters(cinfo);
+ per_scan_setup(cinfo);
+ if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
+ (*cinfo->entropy->start_pass) (cinfo, TRUE);
+ (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
+ master->pub.call_pass_startup = FALSE;
+ break;
+ }
+ /* Special case: Huffman DC refinement scans need no Huffman table
+ * and therefore we can skip the optimization pass for them.
+ */
+ master->pass_type = output_pass;
+ master->pass_number++;
+ /*FALLTHROUGH*/
+#endif
+ case output_pass:
+ /* Do a data-output pass. */
+ /* We need not repeat per-scan setup if prior optimization pass did it. */
+ if (! cinfo->optimize_coding) {
+ select_scan_parameters(cinfo);
+ per_scan_setup(cinfo);
+ }
+ (*cinfo->entropy->start_pass) (cinfo, FALSE);
+ (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
+ /* We emit frame/scan headers now */
+ if (master->scan_number == 0)
+ (*cinfo->marker->write_frame_header) (cinfo);
+ (*cinfo->marker->write_scan_header) (cinfo);
+ master->pub.call_pass_startup = FALSE;
+ break;
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ }
+
+ master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
+
+ /* Set up progress monitor's pass info if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->completed_passes = master->pass_number;
+ cinfo->progress->total_passes = master->total_passes;
+ }
+}
+
+
+/*
+ * Special start-of-pass hook.
+ * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
+ * In single-pass processing, we need this hook because we don't want to
+ * write frame/scan headers during jpeg_start_compress; we want to let the
+ * application write COM markers etc. between jpeg_start_compress and the
+ * jpeg_write_scanlines loop.
+ * In multi-pass processing, this routine is not used.
+ */
+
+METHODDEF(void)
+pass_startup (j_compress_ptr cinfo)
+{
+ cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
+
+ (*cinfo->marker->write_frame_header) (cinfo);
+ (*cinfo->marker->write_scan_header) (cinfo);
+}
+
+
+/*
+ * Finish up at end of pass.
+ */
+
+METHODDEF(void)
+finish_pass_master (j_compress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ /* The entropy coder always needs an end-of-pass call,
+ * either to analyze statistics or to flush its output buffer.
+ */
+ (*cinfo->entropy->finish_pass) (cinfo);
+
+ /* Update state for next pass */
+ switch (master->pass_type) {
+ case main_pass:
+ /* next pass is either output of scan 0 (after optimization)
+ * or output of scan 1 (if no optimization).
+ */
+ master->pass_type = output_pass;
+ if (! cinfo->optimize_coding)
+ master->scan_number++;
+ break;
+ case huff_opt_pass:
+ /* next pass is always output of current scan */
+ master->pass_type = output_pass;
+ break;
+ case output_pass:
+ /* next pass is either optimization or output of next scan */
+ if (cinfo->optimize_coding)
+ master->pass_type = huff_opt_pass;
+ master->scan_number++;
+ break;
+ }
+
+ master->pass_number++;
+}
+
+
+/*
+ * Initialize master compression control.
+ */
+
+GLOBAL(void)
+jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
+{
+ my_master_ptr master;
+
+ master = (my_master_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_comp_master));
+ cinfo->master = (struct jpeg_comp_master *) master;
+ master->pub.prepare_for_pass = prepare_for_pass;
+ master->pub.pass_startup = pass_startup;
+ master->pub.finish_pass = finish_pass_master;
+ master->pub.is_last_pass = FALSE;
+
+ /* Validate parameters, determine derived values */
+ initial_setup(cinfo);
+
+ if (cinfo->scan_info != NULL) {
+#ifdef C_MULTISCAN_FILES_SUPPORTED
+ validate_script(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ cinfo->progressive_mode = FALSE;
+ cinfo->num_scans = 1;
+ }
+
+ if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */
+ cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
+
+ /* Initialize my private state */
+ if (transcode_only) {
+ /* no main pass in transcoding */
+ if (cinfo->optimize_coding)
+ master->pass_type = huff_opt_pass;
+ else
+ master->pass_type = output_pass;
+ } else {
+ /* for normal compression, first pass is always this type: */
+ master->pass_type = main_pass;
+ }
+ master->scan_number = 0;
+ master->pass_number = 0;
+ if (cinfo->optimize_coding)
+ master->total_passes = cinfo->num_scans * 2;
+ else
+ master->total_passes = cinfo->num_scans;
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c b/core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c
index 964909ac4e..aad37d8879 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcomapi.c
@@ -1,109 +1,109 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcomapi.c
- *
- * Copyright (C) 1994-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 application interface routines that are used for both
- * compression and decompression.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/*
- * Abort processing of a JPEG compression or decompression operation,
- * but don't destroy the object itself.
- *
- * For this, we merely clean up all the nonpermanent memory pools.
- * Note that temp files (virtual arrays) are not allowed to belong to
- * the permanent pool, so we will be able to close all temp files here.
- * Closing a data source or destination, if necessary, is the application's
- * responsibility.
- */
-
-GLOBAL(void)
-jpeg_abort (j_common_ptr cinfo)
-{
- int pool;
-
- /* Do nothing if called on a not-initialized or destroyed JPEG object. */
- if (cinfo->mem == NULL)
- return;
-
- /* Releasing pools in reverse order might help avoid fragmentation
- * with some (brain-damaged) malloc libraries.
- */
- for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
- (*cinfo->mem->free_pool) (cinfo, pool);
- }
-
- /* Reset overall state for possible reuse of object */
- if (cinfo->is_decompressor) {
- cinfo->global_state = DSTATE_START;
- /* Try to keep application from accessing now-deleted marker list.
- * A bit kludgy to do it here, but this is the most central place.
- */
- ((j_decompress_ptr) cinfo)->marker_list = NULL;
- } else {
- cinfo->global_state = CSTATE_START;
- }
-}
-
-
-/*
- * Destruction of a JPEG object.
- *
- * Everything gets deallocated except the master jpeg_compress_struct itself
- * and the error manager struct. Both of these are supplied by the application
- * and must be freed, if necessary, by the application. (Often they are on
- * the stack and so don't need to be freed anyway.)
- * Closing a data source or destination, if necessary, is the application's
- * responsibility.
- */
-
-GLOBAL(void)
-jpeg_destroy (j_common_ptr cinfo)
-{
- /* We need only tell the memory manager to release everything. */
- /* NB: mem pointer is NULL if memory mgr failed to initialize. */
- if (cinfo->mem != NULL)
- (*cinfo->mem->self_destruct) (cinfo);
- cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */
- cinfo->global_state = 0; /* mark it destroyed */
-}
-
-
-/*
- * Convenience routines for allocating quantization and Huffman tables.
- * (Would jutils.c be a more reasonable place to put these?)
- */
-
-GLOBAL(JQUANT_TBL *)
-jpeg_alloc_quant_table (j_common_ptr cinfo)
-{
- JQUANT_TBL *tbl;
-
- tbl = (JQUANT_TBL *)
- (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
- tbl->sent_table = FALSE; /* make sure this is false in any new table */
- return tbl;
-}
-
-
-GLOBAL(JHUFF_TBL *)
-jpeg_alloc_huff_table (j_common_ptr cinfo)
-{
- JHUFF_TBL *tbl;
-
- tbl = (JHUFF_TBL *)
- (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
- tbl->sent_table = FALSE; /* make sure this is false in any new table */
- return tbl;
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcomapi.c
+ *
+ * Copyright (C) 1994-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 application interface routines that are used for both
+ * compression and decompression.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Abort processing of a JPEG compression or decompression operation,
+ * but don't destroy the object itself.
+ *
+ * For this, we merely clean up all the nonpermanent memory pools.
+ * Note that temp files (virtual arrays) are not allowed to belong to
+ * the permanent pool, so we will be able to close all temp files here.
+ * Closing a data source or destination, if necessary, is the application's
+ * responsibility.
+ */
+
+GLOBAL(void)
+jpeg_abort (j_common_ptr cinfo)
+{
+ int pool;
+
+ /* Do nothing if called on a not-initialized or destroyed JPEG object. */
+ if (cinfo->mem == NULL)
+ return;
+
+ /* Releasing pools in reverse order might help avoid fragmentation
+ * with some (brain-damaged) malloc libraries.
+ */
+ for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
+ (*cinfo->mem->free_pool) (cinfo, pool);
+ }
+
+ /* Reset overall state for possible reuse of object */
+ if (cinfo->is_decompressor) {
+ cinfo->global_state = DSTATE_START;
+ /* Try to keep application from accessing now-deleted marker list.
+ * A bit kludgy to do it here, but this is the most central place.
+ */
+ ((j_decompress_ptr) cinfo)->marker_list = NULL;
+ } else {
+ cinfo->global_state = CSTATE_START;
+ }
+}
+
+
+/*
+ * Destruction of a JPEG object.
+ *
+ * Everything gets deallocated except the master jpeg_compress_struct itself
+ * and the error manager struct. Both of these are supplied by the application
+ * and must be freed, if necessary, by the application. (Often they are on
+ * the stack and so don't need to be freed anyway.)
+ * Closing a data source or destination, if necessary, is the application's
+ * responsibility.
+ */
+
+GLOBAL(void)
+jpeg_destroy (j_common_ptr cinfo)
+{
+ /* We need only tell the memory manager to release everything. */
+ /* NB: mem pointer is NULL if memory mgr failed to initialize. */
+ if (cinfo->mem != NULL)
+ (*cinfo->mem->self_destruct) (cinfo);
+ cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */
+ cinfo->global_state = 0; /* mark it destroyed */
+}
+
+
+/*
+ * Convenience routines for allocating quantization and Huffman tables.
+ * (Would jutils.c be a more reasonable place to put these?)
+ */
+
+GLOBAL(JQUANT_TBL *)
+jpeg_alloc_quant_table (j_common_ptr cinfo)
+{
+ JQUANT_TBL *tbl;
+
+ tbl = (JQUANT_TBL *)
+ (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
+ tbl->sent_table = FALSE; /* make sure this is false in any new table */
+ return tbl;
+}
+
+
+GLOBAL(JHUFF_TBL *)
+jpeg_alloc_huff_table (j_common_ptr cinfo)
+{
+ JHUFF_TBL *tbl;
+
+ tbl = (JHUFF_TBL *)
+ (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
+ tbl->sent_table = FALSE; /* make sure this is false in any new table */
+ return tbl;
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcparam.c b/core/src/fxcodec/libjpeg/fpdfapi_jcparam.c
index 8b1b9b9fb6..f3ea109878 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcparam.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcparam.c
@@ -1,613 +1,613 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcparam.c
- *
- * Copyright (C) 1991-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 optional default-setting code for the JPEG compressor.
- * Applications do not have to use this file, but those that don't use it
- * must know a lot more about the innards of the JPEG code.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/*
- * Quantization table setup routines
- */
-
-GLOBAL(void)
-jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
- const unsigned int *basic_table,
- int scale_factor, boolean force_baseline)
-/* Define a quantization table equal to the basic_table times
- * a scale factor (given as a percentage).
- * If force_baseline is TRUE, the computed quantization table entries
- * are limited to 1..255 for JPEG baseline compatibility.
- */
-{
- JQUANT_TBL ** qtblptr;
- int i;
- long temp;
-
- /* Safety check to ensure start_compress not called yet. */
- if (cinfo->global_state != CSTATE_START)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
- ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
-
- qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
-
- if (*qtblptr == NULL)
- *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
-
- for (i = 0; i < DCTSIZE2; i++) {
- temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
- /* limit the values to the valid range */
- if (temp <= 0L) temp = 1L;
- if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
- if (force_baseline && temp > 255L)
- temp = 255L; /* limit to baseline range if requested */
- (*qtblptr)->quantval[i] = (UINT16) temp;
- }
-
- /* Initialize sent_table FALSE so table will be written to JPEG file. */
- (*qtblptr)->sent_table = FALSE;
-}
-
-
-GLOBAL(void)
-jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
- boolean force_baseline)
-/* Set or change the 'quality' (quantization) setting, using default tables
- * and a straight percentage-scaling quality scale. In most cases it's better
- * to use jpeg_set_quality (below); this entry point is provided for
- * applications that insist on a linear percentage scaling.
- */
-{
- /* These are the sample quantization tables given in JPEG spec section K.1.
- * The spec says that the values given produce "good" quality, and
- * when divided by 2, "very good" quality.
- */
- static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
- 16, 11, 10, 16, 24, 40, 51, 61,
- 12, 12, 14, 19, 26, 58, 60, 55,
- 14, 13, 16, 24, 40, 57, 69, 56,
- 14, 17, 22, 29, 51, 87, 80, 62,
- 18, 22, 37, 56, 68, 109, 103, 77,
- 24, 35, 55, 64, 81, 104, 113, 92,
- 49, 64, 78, 87, 103, 121, 120, 101,
- 72, 92, 95, 98, 112, 100, 103, 99
- };
- static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
- 17, 18, 24, 47, 99, 99, 99, 99,
- 18, 21, 26, 66, 99, 99, 99, 99,
- 24, 26, 56, 99, 99, 99, 99, 99,
- 47, 66, 99, 99, 99, 99, 99, 99,
- 99, 99, 99, 99, 99, 99, 99, 99,
- 99, 99, 99, 99, 99, 99, 99, 99,
- 99, 99, 99, 99, 99, 99, 99, 99,
- 99, 99, 99, 99, 99, 99, 99, 99
- };
-
- /* Set up two quantization tables using the specified scaling */
- jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
- scale_factor, force_baseline);
- jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
- scale_factor, force_baseline);
-}
-
-
-GLOBAL(int)
-jpeg_quality_scaling (int quality)
-/* Convert a user-specified quality rating to a percentage scaling factor
- * for an underlying quantization table, using our recommended scaling curve.
- * The input 'quality' factor should be 0 (terrible) to 100 (very good).
- */
-{
- /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */
- if (quality <= 0) quality = 1;
- if (quality > 100) quality = 100;
-
- /* The basic table is used as-is (scaling 100) for a quality of 50.
- * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
- * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
- * to make all the table entries 1 (hence, minimum quantization loss).
- * Qualities 1..50 are converted to scaling percentage 5000/Q.
- */
- if (quality < 50)
- quality = 5000 / quality;
- else
- quality = 200 - quality*2;
-
- return quality;
-}
-
-
-GLOBAL(void)
-jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
-/* Set or change the 'quality' (quantization) setting, using default tables.
- * This is the standard quality-adjusting entry point for typical user
- * interfaces; only those who want detailed control over quantization tables
- * would use the preceding three routines directly.
- */
-{
- /* Convert user 0-100 rating to percentage scaling */
- quality = jpeg_quality_scaling(quality);
-
- /* Set up standard quality tables */
- jpeg_set_linear_quality(cinfo, quality, force_baseline);
-}
-
-
-/*
- * Huffman table setup routines
- */
-
-LOCAL(void)
-add_huff_table (j_compress_ptr cinfo,
- JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
-/* Define a Huffman table */
-{
- int nsymbols, len;
-
- if (*htblptr == NULL)
- *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
-
- /* Copy the number-of-symbols-of-each-code-length counts */
- MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
-
- /* Validate the counts. We do this here mainly so we can copy the right
- * number of symbols from the val[] array, without risking marching off
- * the end of memory. jchuff.c will do a more thorough test later.
- */
- nsymbols = 0;
- for (len = 1; len <= 16; len++)
- nsymbols += bits[len];
- if (nsymbols < 1 || nsymbols > 256)
- ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
-
- MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
-
- /* Initialize sent_table FALSE so table will be written to JPEG file. */
- (*htblptr)->sent_table = FALSE;
-}
-
-
-LOCAL(void)
-std_huff_tables (j_compress_ptr cinfo)
-/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
-/* IMPORTANT: these are only valid for 8-bit data precision! */
-{
- static const UINT8 bits_dc_luminance[17] =
- { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
- static const UINT8 val_dc_luminance[] =
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
-
- static const UINT8 bits_dc_chrominance[17] =
- { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
- static const UINT8 val_dc_chrominance[] =
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
-
- static const UINT8 bits_ac_luminance[17] =
- { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
- static const UINT8 val_ac_luminance[] =
- { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
- 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
- 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
- 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
- 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
- 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
- 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
- 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
- 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
- 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
- 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
- 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
- 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
- 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
- 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
- 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
- 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
- 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
- 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
- 0xf9, 0xfa };
-
- static const UINT8 bits_ac_chrominance[17] =
- { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
- static const UINT8 val_ac_chrominance[] =
- { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
- 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
- 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
- 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
- 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
- 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
- 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
- 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
- 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
- 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
- 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
- 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
- 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
- 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
- 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
- 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
- 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
- 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
- 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
- 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
- 0xf9, 0xfa };
-
- add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
- bits_dc_luminance, val_dc_luminance);
- add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
- bits_ac_luminance, val_ac_luminance);
- add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
- bits_dc_chrominance, val_dc_chrominance);
- add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
- bits_ac_chrominance, val_ac_chrominance);
-}
-
-
-/*
- * Default parameter setup for compression.
- *
- * Applications that don't choose to use this routine must do their
- * own setup of all these parameters. Alternately, you can call this
- * to establish defaults and then alter parameters selectively. This
- * is the recommended approach since, if we add any new parameters,
- * your code will still work (they'll be set to reasonable defaults).
- */
-
-GLOBAL(void)
-jpeg_set_defaults (j_compress_ptr cinfo)
-{
- int i;
-
- /* Safety check to ensure start_compress not called yet. */
- if (cinfo->global_state != CSTATE_START)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- /* Allocate comp_info array large enough for maximum component count.
- * Array is made permanent in case application wants to compress
- * multiple images at same param settings.
- */
- if (cinfo->comp_info == NULL)
- cinfo->comp_info = (jpeg_component_info *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
- MAX_COMPONENTS * SIZEOF(jpeg_component_info));
-
- /* Initialize everything not dependent on the color space */
-
- cinfo->data_precision = BITS_IN_JSAMPLE;
- /* Set up two quantization tables using default quality of 75 */
- jpeg_set_quality(cinfo, 75, TRUE);
- /* Set up two Huffman tables */
- std_huff_tables(cinfo);
-
- /* Initialize default arithmetic coding conditioning */
- for (i = 0; i < NUM_ARITH_TBLS; i++) {
- cinfo->arith_dc_L[i] = 0;
- cinfo->arith_dc_U[i] = 1;
- cinfo->arith_ac_K[i] = 5;
- }
-
- /* Default is no multiple-scan output */
- cinfo->scan_info = NULL;
- cinfo->num_scans = 0;
-
- /* Expect normal source image, not raw downsampled data */
- cinfo->raw_data_in = FALSE;
-
- /* Use Huffman coding, not arithmetic coding, by default */
- cinfo->arith_code = FALSE;
-
- /* By default, don't do extra passes to optimize entropy coding */
- cinfo->optimize_coding = FALSE;
- /* The standard Huffman tables are only valid for 8-bit data precision.
- * If the precision is higher, force optimization on so that usable
- * tables will be computed. This test can be removed if default tables
- * are supplied that are valid for the desired precision.
- */
- if (cinfo->data_precision > 8)
- cinfo->optimize_coding = TRUE;
-
- /* By default, use the simpler non-cosited sampling alignment */
- cinfo->CCIR601_sampling = FALSE;
-
- /* No input smoothing */
- cinfo->smoothing_factor = 0;
-
- /* DCT algorithm preference */
- cinfo->dct_method = JDCT_DEFAULT;
-
- /* No restart markers */
- cinfo->restart_interval = 0;
- cinfo->restart_in_rows = 0;
-
- /* Fill in default JFIF marker parameters. Note that whether the marker
- * will actually be written is determined by jpeg_set_colorspace.
- *
- * By default, the library emits JFIF version code 1.01.
- * An application that wants to emit JFIF 1.02 extension markers should set
- * JFIF_minor_version to 2. We could probably get away with just defaulting
- * to 1.02, but there may still be some decoders in use that will complain
- * about that; saying 1.01 should minimize compatibility problems.
- */
- cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
- cinfo->JFIF_minor_version = 1;
- cinfo->density_unit = 0; /* Pixel size is unknown by default */
- cinfo->X_density = 1; /* Pixel aspect ratio is square by default */
- cinfo->Y_density = 1;
-
- /* Choose JPEG colorspace based on input space, set defaults accordingly */
-
- jpeg_default_colorspace(cinfo);
-}
-
-
-/*
- * Select an appropriate JPEG colorspace for in_color_space.
- */
-
-GLOBAL(void)
-jpeg_default_colorspace (j_compress_ptr cinfo)
-{
- switch (cinfo->in_color_space) {
- case JCS_GRAYSCALE:
- jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
- break;
- case JCS_RGB:
- jpeg_set_colorspace(cinfo, JCS_YCbCr);
- break;
- case JCS_YCbCr:
- jpeg_set_colorspace(cinfo, JCS_YCbCr);
- break;
- case JCS_CMYK:
- jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
- break;
- case JCS_YCCK:
- jpeg_set_colorspace(cinfo, JCS_YCCK);
- break;
- case JCS_UNKNOWN:
- jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
- break;
- default:
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- }
-}
-
-
-/*
- * Set the JPEG colorspace, and choose colorspace-dependent default values.
- */
-
-GLOBAL(void)
-jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
-{
- jpeg_component_info * compptr;
- int ci;
-
-#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \
- (compptr = &cinfo->comp_info[index], \
- compptr->component_id = (id), \
- compptr->h_samp_factor = (hsamp), \
- compptr->v_samp_factor = (vsamp), \
- compptr->quant_tbl_no = (quant), \
- compptr->dc_tbl_no = (dctbl), \
- compptr->ac_tbl_no = (actbl) )
-
- /* Safety check to ensure start_compress not called yet. */
- if (cinfo->global_state != CSTATE_START)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
- * tables 1 for chrominance components.
- */
-
- cinfo->jpeg_color_space = colorspace;
-
- cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
- cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
-
- switch (colorspace) {
- case JCS_GRAYSCALE:
- cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
- cinfo->num_components = 1;
- /* JFIF specifies component ID 1 */
- SET_COMP(0, 1, 1,1, 0, 0,0);
- break;
- case JCS_RGB:
- cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
- cinfo->num_components = 3;
- SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
- SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
- SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
- break;
- case JCS_YCbCr:
- cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
- cinfo->num_components = 3;
- /* JFIF specifies component IDs 1,2,3 */
- /* We default to 2x2 subsamples of chrominance */
- SET_COMP(0, 1, 2,2, 0, 0,0);
- SET_COMP(1, 2, 1,1, 1, 1,1);
- SET_COMP(2, 3, 1,1, 1, 1,1);
- break;
- case JCS_CMYK:
- cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
- cinfo->num_components = 4;
- SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
- SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
- SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
- SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
- break;
- case JCS_YCCK:
- cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
- cinfo->num_components = 4;
- SET_COMP(0, 1, 2,2, 0, 0,0);
- SET_COMP(1, 2, 1,1, 1, 1,1);
- SET_COMP(2, 3, 1,1, 1, 1,1);
- SET_COMP(3, 4, 2,2, 0, 0,0);
- break;
- case JCS_UNKNOWN:
- cinfo->num_components = cinfo->input_components;
- if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
- ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
- MAX_COMPONENTS);
- for (ci = 0; ci < cinfo->num_components; ci++) {
- SET_COMP(ci, ci, 1,1, 0, 0,0);
- }
- break;
- default:
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- }
-}
-
-
-#ifdef C_PROGRESSIVE_SUPPORTED
-
-LOCAL(jpeg_scan_info *)
-fill_a_scan (jpeg_scan_info * scanptr, int ci,
- int Ss, int Se, int Ah, int Al)
-/* Support routine: generate one scan for specified component */
-{
- scanptr->comps_in_scan = 1;
- scanptr->component_index[0] = ci;
- scanptr->Ss = Ss;
- scanptr->Se = Se;
- scanptr->Ah = Ah;
- scanptr->Al = Al;
- scanptr++;
- return scanptr;
-}
-
-LOCAL(jpeg_scan_info *)
-fill_scans (jpeg_scan_info * scanptr, int ncomps,
- int Ss, int Se, int Ah, int Al)
-/* Support routine: generate one scan for each component */
-{
- int ci;
-
- for (ci = 0; ci < ncomps; ci++) {
- scanptr->comps_in_scan = 1;
- scanptr->component_index[0] = ci;
- scanptr->Ss = Ss;
- scanptr->Se = Se;
- scanptr->Ah = Ah;
- scanptr->Al = Al;
- scanptr++;
- }
- return scanptr;
-}
-
-LOCAL(jpeg_scan_info *)
-fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
-/* Support routine: generate interleaved DC scan if possible, else N scans */
-{
- int ci;
-
- if (ncomps <= MAX_COMPS_IN_SCAN) {
- /* Single interleaved DC scan */
- scanptr->comps_in_scan = ncomps;
- for (ci = 0; ci < ncomps; ci++)
- scanptr->component_index[ci] = ci;
- scanptr->Ss = scanptr->Se = 0;
- scanptr->Ah = Ah;
- scanptr->Al = Al;
- scanptr++;
- } else {
- /* Noninterleaved DC scan for each component */
- scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
- }
- return scanptr;
-}
-
-
-/*
- * Create a recommended progressive-JPEG script.
- * cinfo->num_components and cinfo->jpeg_color_space must be correct.
- */
-
-GLOBAL(void)
-jpeg_simple_progression (j_compress_ptr cinfo)
-{
- int ncomps = cinfo->num_components;
- int nscans;
- jpeg_scan_info * scanptr;
-
- /* Safety check to ensure start_compress not called yet. */
- if (cinfo->global_state != CSTATE_START)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- /* Figure space needed for script. Calculation must match code below! */
- if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
- /* Custom script for YCbCr color images. */
- nscans = 10;
- } else {
- /* All-purpose script for other color spaces. */
- if (ncomps > MAX_COMPS_IN_SCAN)
- nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */
- else
- nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */
- }
-
- /* Allocate space for script.
- * We need to put it in the permanent pool in case the application performs
- * multiple compressions without changing the settings. To avoid a memory
- * leak if jpeg_simple_progression is called repeatedly for the same JPEG
- * object, we try to re-use previously allocated space, and we allocate
- * enough space to handle YCbCr even if initially asked for grayscale.
- */
- if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
- cinfo->script_space_size = MAX(nscans, 10);
- cinfo->script_space = (jpeg_scan_info *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
- cinfo->script_space_size * SIZEOF(jpeg_scan_info));
- }
- scanptr = cinfo->script_space;
- cinfo->scan_info = scanptr;
- cinfo->num_scans = nscans;
-
- if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
- /* Custom script for YCbCr color images. */
- /* Initial DC scan */
- scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
- /* Initial AC scan: get some luma data out in a hurry */
- scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
- /* Chroma data is too small to be worth expending many scans on */
- scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
- scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
- /* Complete spectral selection for luma AC */
- scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
- /* Refine next bit of luma AC */
- scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
- /* Finish DC successive approximation */
- scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
- /* Finish AC successive approximation */
- scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
- scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
- /* Luma bottom bit comes last since it's usually largest scan */
- scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
- } else {
- /* All-purpose script for other color spaces. */
- /* Successive approximation first pass */
- scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
- scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
- scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
- /* Successive approximation second pass */
- scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
- /* Successive approximation final pass */
- scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
- scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
- }
-}
-
-#endif /* C_PROGRESSIVE_SUPPORTED */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcparam.c
+ *
+ * Copyright (C) 1991-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 optional default-setting code for the JPEG compressor.
+ * Applications do not have to use this file, but those that don't use it
+ * must know a lot more about the innards of the JPEG code.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Quantization table setup routines
+ */
+
+GLOBAL(void)
+jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
+ const unsigned int *basic_table,
+ int scale_factor, boolean force_baseline)
+/* Define a quantization table equal to the basic_table times
+ * a scale factor (given as a percentage).
+ * If force_baseline is TRUE, the computed quantization table entries
+ * are limited to 1..255 for JPEG baseline compatibility.
+ */
+{
+ JQUANT_TBL ** qtblptr;
+ int i;
+ long temp;
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ if (which_tbl < 0 || which_tbl >= NUM_QUANT_TBLS)
+ ERREXIT1(cinfo, JERR_DQT_INDEX, which_tbl);
+
+ qtblptr = & cinfo->quant_tbl_ptrs[which_tbl];
+
+ if (*qtblptr == NULL)
+ *qtblptr = jpeg_alloc_quant_table((j_common_ptr) cinfo);
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
+ /* limit the values to the valid range */
+ if (temp <= 0L) temp = 1L;
+ if (temp > 32767L) temp = 32767L; /* max quantizer needed for 12 bits */
+ if (force_baseline && temp > 255L)
+ temp = 255L; /* limit to baseline range if requested */
+ (*qtblptr)->quantval[i] = (UINT16) temp;
+ }
+
+ /* Initialize sent_table FALSE so table will be written to JPEG file. */
+ (*qtblptr)->sent_table = FALSE;
+}
+
+
+GLOBAL(void)
+jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
+ boolean force_baseline)
+/* Set or change the 'quality' (quantization) setting, using default tables
+ * and a straight percentage-scaling quality scale. In most cases it's better
+ * to use jpeg_set_quality (below); this entry point is provided for
+ * applications that insist on a linear percentage scaling.
+ */
+{
+ /* These are the sample quantization tables given in JPEG spec section K.1.
+ * The spec says that the values given produce "good" quality, and
+ * when divided by 2, "very good" quality.
+ */
+ static const unsigned int std_luminance_quant_tbl[DCTSIZE2] = {
+ 16, 11, 10, 16, 24, 40, 51, 61,
+ 12, 12, 14, 19, 26, 58, 60, 55,
+ 14, 13, 16, 24, 40, 57, 69, 56,
+ 14, 17, 22, 29, 51, 87, 80, 62,
+ 18, 22, 37, 56, 68, 109, 103, 77,
+ 24, 35, 55, 64, 81, 104, 113, 92,
+ 49, 64, 78, 87, 103, 121, 120, 101,
+ 72, 92, 95, 98, 112, 100, 103, 99
+ };
+ static const unsigned int std_chrominance_quant_tbl[DCTSIZE2] = {
+ 17, 18, 24, 47, 99, 99, 99, 99,
+ 18, 21, 26, 66, 99, 99, 99, 99,
+ 24, 26, 56, 99, 99, 99, 99, 99,
+ 47, 66, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99
+ };
+
+ /* Set up two quantization tables using the specified scaling */
+ jpeg_add_quant_table(cinfo, 0, std_luminance_quant_tbl,
+ scale_factor, force_baseline);
+ jpeg_add_quant_table(cinfo, 1, std_chrominance_quant_tbl,
+ scale_factor, force_baseline);
+}
+
+
+GLOBAL(int)
+jpeg_quality_scaling (int quality)
+/* Convert a user-specified quality rating to a percentage scaling factor
+ * for an underlying quantization table, using our recommended scaling curve.
+ * The input 'quality' factor should be 0 (terrible) to 100 (very good).
+ */
+{
+ /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */
+ if (quality <= 0) quality = 1;
+ if (quality > 100) quality = 100;
+
+ /* The basic table is used as-is (scaling 100) for a quality of 50.
+ * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
+ * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
+ * to make all the table entries 1 (hence, minimum quantization loss).
+ * Qualities 1..50 are converted to scaling percentage 5000/Q.
+ */
+ if (quality < 50)
+ quality = 5000 / quality;
+ else
+ quality = 200 - quality*2;
+
+ return quality;
+}
+
+
+GLOBAL(void)
+jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
+/* Set or change the 'quality' (quantization) setting, using default tables.
+ * This is the standard quality-adjusting entry point for typical user
+ * interfaces; only those who want detailed control over quantization tables
+ * would use the preceding three routines directly.
+ */
+{
+ /* Convert user 0-100 rating to percentage scaling */
+ quality = jpeg_quality_scaling(quality);
+
+ /* Set up standard quality tables */
+ jpeg_set_linear_quality(cinfo, quality, force_baseline);
+}
+
+
+/*
+ * Huffman table setup routines
+ */
+
+LOCAL(void)
+add_huff_table (j_compress_ptr cinfo,
+ JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
+/* Define a Huffman table */
+{
+ int nsymbols, len;
+
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+
+ /* Copy the number-of-symbols-of-each-code-length counts */
+ MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
+
+ /* Validate the counts. We do this here mainly so we can copy the right
+ * number of symbols from the val[] array, without risking marching off
+ * the end of memory. jchuff.c will do a more thorough test later.
+ */
+ nsymbols = 0;
+ for (len = 1; len <= 16; len++)
+ nsymbols += bits[len];
+ if (nsymbols < 1 || nsymbols > 256)
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+
+ MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8));
+
+ /* Initialize sent_table FALSE so table will be written to JPEG file. */
+ (*htblptr)->sent_table = FALSE;
+}
+
+
+LOCAL(void)
+std_huff_tables (j_compress_ptr cinfo)
+/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
+/* IMPORTANT: these are only valid for 8-bit data precision! */
+{
+ static const UINT8 bits_dc_luminance[17] =
+ { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
+ static const UINT8 val_dc_luminance[] =
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
+
+ static const UINT8 bits_dc_chrominance[17] =
+ { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
+ static const UINT8 val_dc_chrominance[] =
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
+
+ static const UINT8 bits_ac_luminance[17] =
+ { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
+ static const UINT8 val_ac_luminance[] =
+ { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
+ 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
+ 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
+ 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
+ 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
+ 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
+ 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+ 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
+ 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
+ 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
+ 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
+ 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
+ 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
+ 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+ 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
+ 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
+ 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
+ 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
+ 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
+ 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
+ 0xf9, 0xfa };
+
+ static const UINT8 bits_ac_chrominance[17] =
+ { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
+ static const UINT8 val_ac_chrominance[] =
+ { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
+ 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
+ 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
+ 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
+ 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
+ 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
+ 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
+ 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
+ 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
+ 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
+ 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
+ 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
+ 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
+ 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
+ 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
+ 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
+ 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
+ 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
+ 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
+ 0xf9, 0xfa };
+
+ add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
+ bits_dc_luminance, val_dc_luminance);
+ add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
+ bits_ac_luminance, val_ac_luminance);
+ add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
+ bits_dc_chrominance, val_dc_chrominance);
+ add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
+ bits_ac_chrominance, val_ac_chrominance);
+}
+
+
+/*
+ * Default parameter setup for compression.
+ *
+ * Applications that don't choose to use this routine must do their
+ * own setup of all these parameters. Alternately, you can call this
+ * to establish defaults and then alter parameters selectively. This
+ * is the recommended approach since, if we add any new parameters,
+ * your code will still work (they'll be set to reasonable defaults).
+ */
+
+GLOBAL(void)
+jpeg_set_defaults (j_compress_ptr cinfo)
+{
+ int i;
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ /* Allocate comp_info array large enough for maximum component count.
+ * Array is made permanent in case application wants to compress
+ * multiple images at same param settings.
+ */
+ if (cinfo->comp_info == NULL)
+ cinfo->comp_info = (jpeg_component_info *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ MAX_COMPONENTS * SIZEOF(jpeg_component_info));
+
+ /* Initialize everything not dependent on the color space */
+
+ cinfo->data_precision = BITS_IN_JSAMPLE;
+ /* Set up two quantization tables using default quality of 75 */
+ jpeg_set_quality(cinfo, 75, TRUE);
+ /* Set up two Huffman tables */
+ std_huff_tables(cinfo);
+
+ /* Initialize default arithmetic coding conditioning */
+ for (i = 0; i < NUM_ARITH_TBLS; i++) {
+ cinfo->arith_dc_L[i] = 0;
+ cinfo->arith_dc_U[i] = 1;
+ cinfo->arith_ac_K[i] = 5;
+ }
+
+ /* Default is no multiple-scan output */
+ cinfo->scan_info = NULL;
+ cinfo->num_scans = 0;
+
+ /* Expect normal source image, not raw downsampled data */
+ cinfo->raw_data_in = FALSE;
+
+ /* Use Huffman coding, not arithmetic coding, by default */
+ cinfo->arith_code = FALSE;
+
+ /* By default, don't do extra passes to optimize entropy coding */
+ cinfo->optimize_coding = FALSE;
+ /* The standard Huffman tables are only valid for 8-bit data precision.
+ * If the precision is higher, force optimization on so that usable
+ * tables will be computed. This test can be removed if default tables
+ * are supplied that are valid for the desired precision.
+ */
+ if (cinfo->data_precision > 8)
+ cinfo->optimize_coding = TRUE;
+
+ /* By default, use the simpler non-cosited sampling alignment */
+ cinfo->CCIR601_sampling = FALSE;
+
+ /* No input smoothing */
+ cinfo->smoothing_factor = 0;
+
+ /* DCT algorithm preference */
+ cinfo->dct_method = JDCT_DEFAULT;
+
+ /* No restart markers */
+ cinfo->restart_interval = 0;
+ cinfo->restart_in_rows = 0;
+
+ /* Fill in default JFIF marker parameters. Note that whether the marker
+ * will actually be written is determined by jpeg_set_colorspace.
+ *
+ * By default, the library emits JFIF version code 1.01.
+ * An application that wants to emit JFIF 1.02 extension markers should set
+ * JFIF_minor_version to 2. We could probably get away with just defaulting
+ * to 1.02, but there may still be some decoders in use that will complain
+ * about that; saying 1.01 should minimize compatibility problems.
+ */
+ cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
+ cinfo->JFIF_minor_version = 1;
+ cinfo->density_unit = 0; /* Pixel size is unknown by default */
+ cinfo->X_density = 1; /* Pixel aspect ratio is square by default */
+ cinfo->Y_density = 1;
+
+ /* Choose JPEG colorspace based on input space, set defaults accordingly */
+
+ jpeg_default_colorspace(cinfo);
+}
+
+
+/*
+ * Select an appropriate JPEG colorspace for in_color_space.
+ */
+
+GLOBAL(void)
+jpeg_default_colorspace (j_compress_ptr cinfo)
+{
+ switch (cinfo->in_color_space) {
+ case JCS_GRAYSCALE:
+ jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
+ break;
+ case JCS_RGB:
+ jpeg_set_colorspace(cinfo, JCS_YCbCr);
+ break;
+ case JCS_YCbCr:
+ jpeg_set_colorspace(cinfo, JCS_YCbCr);
+ break;
+ case JCS_CMYK:
+ jpeg_set_colorspace(cinfo, JCS_CMYK); /* By default, no translation */
+ break;
+ case JCS_YCCK:
+ jpeg_set_colorspace(cinfo, JCS_YCCK);
+ break;
+ case JCS_UNKNOWN:
+ jpeg_set_colorspace(cinfo, JCS_UNKNOWN);
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
+ }
+}
+
+
+/*
+ * Set the JPEG colorspace, and choose colorspace-dependent default values.
+ */
+
+GLOBAL(void)
+jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
+{
+ jpeg_component_info * compptr;
+ int ci;
+
+#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl) \
+ (compptr = &cinfo->comp_info[index], \
+ compptr->component_id = (id), \
+ compptr->h_samp_factor = (hsamp), \
+ compptr->v_samp_factor = (vsamp), \
+ compptr->quant_tbl_no = (quant), \
+ compptr->dc_tbl_no = (dctbl), \
+ compptr->ac_tbl_no = (actbl) )
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ /* For all colorspaces, we use Q and Huff tables 0 for luminance components,
+ * tables 1 for chrominance components.
+ */
+
+ cinfo->jpeg_color_space = colorspace;
+
+ cinfo->write_JFIF_header = FALSE; /* No marker for non-JFIF colorspaces */
+ cinfo->write_Adobe_marker = FALSE; /* write no Adobe marker by default */
+
+ switch (colorspace) {
+ case JCS_GRAYSCALE:
+ cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
+ cinfo->num_components = 1;
+ /* JFIF specifies component ID 1 */
+ SET_COMP(0, 1, 1,1, 0, 0,0);
+ break;
+ case JCS_RGB:
+ cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag RGB */
+ cinfo->num_components = 3;
+ SET_COMP(0, 0x52 /* 'R' */, 1,1, 0, 0,0);
+ SET_COMP(1, 0x47 /* 'G' */, 1,1, 0, 0,0);
+ SET_COMP(2, 0x42 /* 'B' */, 1,1, 0, 0,0);
+ break;
+ case JCS_YCbCr:
+ cinfo->write_JFIF_header = TRUE; /* Write a JFIF marker */
+ cinfo->num_components = 3;
+ /* JFIF specifies component IDs 1,2,3 */
+ /* We default to 2x2 subsamples of chrominance */
+ SET_COMP(0, 1, 2,2, 0, 0,0);
+ SET_COMP(1, 2, 1,1, 1, 1,1);
+ SET_COMP(2, 3, 1,1, 1, 1,1);
+ break;
+ case JCS_CMYK:
+ cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag CMYK */
+ cinfo->num_components = 4;
+ SET_COMP(0, 0x43 /* 'C' */, 1,1, 0, 0,0);
+ SET_COMP(1, 0x4D /* 'M' */, 1,1, 0, 0,0);
+ SET_COMP(2, 0x59 /* 'Y' */, 1,1, 0, 0,0);
+ SET_COMP(3, 0x4B /* 'K' */, 1,1, 0, 0,0);
+ break;
+ case JCS_YCCK:
+ cinfo->write_Adobe_marker = TRUE; /* write Adobe marker to flag YCCK */
+ cinfo->num_components = 4;
+ SET_COMP(0, 1, 2,2, 0, 0,0);
+ SET_COMP(1, 2, 1,1, 1, 1,1);
+ SET_COMP(2, 3, 1,1, 1, 1,1);
+ SET_COMP(3, 4, 2,2, 0, 0,0);
+ break;
+ case JCS_UNKNOWN:
+ cinfo->num_components = cinfo->input_components;
+ if (cinfo->num_components < 1 || cinfo->num_components > MAX_COMPONENTS)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+ MAX_COMPONENTS);
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ SET_COMP(ci, ci, 1,1, 0, 0,0);
+ }
+ break;
+ default:
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ }
+}
+
+
+#ifdef C_PROGRESSIVE_SUPPORTED
+
+LOCAL(jpeg_scan_info *)
+fill_a_scan (jpeg_scan_info * scanptr, int ci,
+ int Ss, int Se, int Ah, int Al)
+/* Support routine: generate one scan for specified component */
+{
+ scanptr->comps_in_scan = 1;
+ scanptr->component_index[0] = ci;
+ scanptr->Ss = Ss;
+ scanptr->Se = Se;
+ scanptr->Ah = Ah;
+ scanptr->Al = Al;
+ scanptr++;
+ return scanptr;
+}
+
+LOCAL(jpeg_scan_info *)
+fill_scans (jpeg_scan_info * scanptr, int ncomps,
+ int Ss, int Se, int Ah, int Al)
+/* Support routine: generate one scan for each component */
+{
+ int ci;
+
+ for (ci = 0; ci < ncomps; ci++) {
+ scanptr->comps_in_scan = 1;
+ scanptr->component_index[0] = ci;
+ scanptr->Ss = Ss;
+ scanptr->Se = Se;
+ scanptr->Ah = Ah;
+ scanptr->Al = Al;
+ scanptr++;
+ }
+ return scanptr;
+}
+
+LOCAL(jpeg_scan_info *)
+fill_dc_scans (jpeg_scan_info * scanptr, int ncomps, int Ah, int Al)
+/* Support routine: generate interleaved DC scan if possible, else N scans */
+{
+ int ci;
+
+ if (ncomps <= MAX_COMPS_IN_SCAN) {
+ /* Single interleaved DC scan */
+ scanptr->comps_in_scan = ncomps;
+ for (ci = 0; ci < ncomps; ci++)
+ scanptr->component_index[ci] = ci;
+ scanptr->Ss = scanptr->Se = 0;
+ scanptr->Ah = Ah;
+ scanptr->Al = Al;
+ scanptr++;
+ } else {
+ /* Noninterleaved DC scan for each component */
+ scanptr = fill_scans(scanptr, ncomps, 0, 0, Ah, Al);
+ }
+ return scanptr;
+}
+
+
+/*
+ * Create a recommended progressive-JPEG script.
+ * cinfo->num_components and cinfo->jpeg_color_space must be correct.
+ */
+
+GLOBAL(void)
+jpeg_simple_progression (j_compress_ptr cinfo)
+{
+ int ncomps = cinfo->num_components;
+ int nscans;
+ jpeg_scan_info * scanptr;
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ /* Figure space needed for script. Calculation must match code below! */
+ if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
+ /* Custom script for YCbCr color images. */
+ nscans = 10;
+ } else {
+ /* All-purpose script for other color spaces. */
+ if (ncomps > MAX_COMPS_IN_SCAN)
+ nscans = 6 * ncomps; /* 2 DC + 4 AC scans per component */
+ else
+ nscans = 2 + 4 * ncomps; /* 2 DC scans; 4 AC scans per component */
+ }
+
+ /* Allocate space for script.
+ * We need to put it in the permanent pool in case the application performs
+ * multiple compressions without changing the settings. To avoid a memory
+ * leak if jpeg_simple_progression is called repeatedly for the same JPEG
+ * object, we try to re-use previously allocated space, and we allocate
+ * enough space to handle YCbCr even if initially asked for grayscale.
+ */
+ if (cinfo->script_space == NULL || cinfo->script_space_size < nscans) {
+ cinfo->script_space_size = MAX(nscans, 10);
+ cinfo->script_space = (jpeg_scan_info *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ cinfo->script_space_size * SIZEOF(jpeg_scan_info));
+ }
+ scanptr = cinfo->script_space;
+ cinfo->scan_info = scanptr;
+ cinfo->num_scans = nscans;
+
+ if (ncomps == 3 && cinfo->jpeg_color_space == JCS_YCbCr) {
+ /* Custom script for YCbCr color images. */
+ /* Initial DC scan */
+ scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
+ /* Initial AC scan: get some luma data out in a hurry */
+ scanptr = fill_a_scan(scanptr, 0, 1, 5, 0, 2);
+ /* Chroma data is too small to be worth expending many scans on */
+ scanptr = fill_a_scan(scanptr, 2, 1, 63, 0, 1);
+ scanptr = fill_a_scan(scanptr, 1, 1, 63, 0, 1);
+ /* Complete spectral selection for luma AC */
+ scanptr = fill_a_scan(scanptr, 0, 6, 63, 0, 2);
+ /* Refine next bit of luma AC */
+ scanptr = fill_a_scan(scanptr, 0, 1, 63, 2, 1);
+ /* Finish DC successive approximation */
+ scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
+ /* Finish AC successive approximation */
+ scanptr = fill_a_scan(scanptr, 2, 1, 63, 1, 0);
+ scanptr = fill_a_scan(scanptr, 1, 1, 63, 1, 0);
+ /* Luma bottom bit comes last since it's usually largest scan */
+ scanptr = fill_a_scan(scanptr, 0, 1, 63, 1, 0);
+ } else {
+ /* All-purpose script for other color spaces. */
+ /* Successive approximation first pass */
+ scanptr = fill_dc_scans(scanptr, ncomps, 0, 1);
+ scanptr = fill_scans(scanptr, ncomps, 1, 5, 0, 2);
+ scanptr = fill_scans(scanptr, ncomps, 6, 63, 0, 2);
+ /* Successive approximation second pass */
+ scanptr = fill_scans(scanptr, ncomps, 1, 63, 2, 1);
+ /* Successive approximation final pass */
+ scanptr = fill_dc_scans(scanptr, ncomps, 1, 0);
+ scanptr = fill_scans(scanptr, ncomps, 1, 63, 1, 0);
+ }
+}
+
+#endif /* C_PROGRESSIVE_SUPPORTED */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c b/core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c
index c2655fbb48..6d89b6b2c0 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcphuff.c
@@ -1,836 +1,836 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * 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 */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * 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 */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c b/core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c
index 539d87bbae..57f3fc1108 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcprepct.c
@@ -1,357 +1,357 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcprepct.c
- *
- * Copyright (C) 1994-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 contains the compression preprocessing controller.
- * This controller manages the color conversion, downsampling,
- * and edge expansion steps.
- *
- * Most of the complexity here is associated with buffering input rows
- * as required by the downsampler. See the comments at the head of
- * jcsample.c for the downsampler's needs.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* At present, jcsample.c can request context rows only for smoothing.
- * In the future, we might also need context rows for CCIR601 sampling
- * or other more-complex downsampling procedures. The code to support
- * context rows should be compiled only if needed.
- */
-#ifdef INPUT_SMOOTHING_SUPPORTED
-#define CONTEXT_ROWS_SUPPORTED
-#endif
-
-
-/*
- * For the simple (no-context-row) case, we just need to buffer one
- * row group's worth of pixels for the downsampling step. At the bottom of
- * the image, we pad to a full row group by replicating the last pixel row.
- * The downsampler's last output row is then replicated if needed to pad
- * out to a full iMCU row.
- *
- * When providing context rows, we must buffer three row groups' worth of
- * pixels. Three row groups are physically allocated, but the row pointer
- * arrays are made five row groups high, with the extra pointers above and
- * below "wrapping around" to point to the last and first real row groups.
- * This allows the downsampler to access the proper context rows.
- * At the top and bottom of the image, we create dummy context rows by
- * copying the first or last real pixel row. This copying could be avoided
- * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
- * trouble on the compression side.
- */
-
-
-/* Private buffer controller object */
-
-typedef struct {
- struct jpeg_c_prep_controller pub; /* public fields */
-
- /* Downsampling input buffer. This buffer holds color-converted data
- * until we have enough to do a downsample step.
- */
- JSAMPARRAY color_buf[MAX_COMPONENTS];
-
- JDIMENSION rows_to_go; /* counts rows remaining in source image */
- int next_buf_row; /* index of next row to store in color_buf */
-
-#ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
- int this_row_group; /* starting row index of group to process */
- int next_buf_stop; /* downsample when we reach this index */
-#endif
-} my_prep_controller;
-
-typedef my_prep_controller * my_prep_ptr;
-
-
-/*
- * Initialize for a processing pass.
- */
-
-METHODDEF(void)
-start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
-{
- my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
-
- if (pass_mode != JBUF_PASS_THRU)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
-
- /* Initialize total-height counter for detecting bottom of image */
- prep->rows_to_go = cinfo->image_height;
- /* Mark the conversion buffer empty */
- prep->next_buf_row = 0;
-#ifdef CONTEXT_ROWS_SUPPORTED
- /* Preset additional state variables for context mode.
- * These aren't used in non-context mode, so we needn't test which mode.
- */
- prep->this_row_group = 0;
- /* Set next_buf_stop to stop after two row groups have been read in. */
- prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
-#endif
-}
-
-
-/*
- * Expand an image vertically from height input_rows to height output_rows,
- * by duplicating the bottom row.
- */
-
-LOCAL(void)
-expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
- int input_rows, int output_rows)
-{
- register int row;
-
- for (row = input_rows; row < output_rows; row++) {
- jcopy_sample_rows(image_data, input_rows-1, image_data, row,
- 1, num_cols);
- }
-}
-
-
-/*
- * Process some data in the simple no-context case.
- *
- * Preprocessor output data is counted in "row groups". A row group
- * is defined to be v_samp_factor sample rows of each component.
- * Downsampling will produce this much data from each max_v_samp_factor
- * input rows.
- */
-
-METHODDEF(void)
-pre_process_data (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
- JDIMENSION in_rows_avail,
- JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
- JDIMENSION out_row_groups_avail)
-{
- my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
- int numrows, ci;
- JDIMENSION inrows;
- jpeg_component_info * compptr;
-
- while (*in_row_ctr < in_rows_avail &&
- *out_row_group_ctr < out_row_groups_avail) {
- /* Do color conversion to fill the conversion buffer. */
- inrows = in_rows_avail - *in_row_ctr;
- numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
- numrows = (int) MIN((JDIMENSION) numrows, inrows);
- (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
- prep->color_buf,
- (JDIMENSION) prep->next_buf_row,
- numrows);
- *in_row_ctr += numrows;
- prep->next_buf_row += numrows;
- prep->rows_to_go -= numrows;
- /* If at bottom of image, pad to fill the conversion buffer. */
- if (prep->rows_to_go == 0 &&
- prep->next_buf_row < cinfo->max_v_samp_factor) {
- for (ci = 0; ci < cinfo->num_components; ci++) {
- expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
- prep->next_buf_row, cinfo->max_v_samp_factor);
- }
- prep->next_buf_row = cinfo->max_v_samp_factor;
- }
- /* If we've filled the conversion buffer, empty it. */
- if (prep->next_buf_row == cinfo->max_v_samp_factor) {
- (*cinfo->downsample->downsample) (cinfo,
- prep->color_buf, (JDIMENSION) 0,
- output_buf, *out_row_group_ctr);
- prep->next_buf_row = 0;
- (*out_row_group_ctr)++;
- }
- /* If at bottom of image, pad the output to a full iMCU height.
- * Note we assume the caller is providing a one-iMCU-height output buffer!
- */
- if (prep->rows_to_go == 0 &&
- *out_row_group_ctr < out_row_groups_avail) {
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- expand_bottom_edge(output_buf[ci],
- compptr->width_in_blocks * DCTSIZE,
- (int) (*out_row_group_ctr * compptr->v_samp_factor),
- (int) (out_row_groups_avail * compptr->v_samp_factor));
- }
- *out_row_group_ctr = out_row_groups_avail;
- break; /* can exit outer loop without test */
- }
- }
-}
-
-
-#ifdef CONTEXT_ROWS_SUPPORTED
-
-/*
- * Process some data in the context case.
- */
-
-METHODDEF(void)
-pre_process_context (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
- JDIMENSION in_rows_avail,
- JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
- JDIMENSION out_row_groups_avail)
-{
- my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
- int numrows, ci;
- int buf_height = cinfo->max_v_samp_factor * 3;
- JDIMENSION inrows;
-
- while (*out_row_group_ctr < out_row_groups_avail) {
- if (*in_row_ctr < in_rows_avail) {
- /* Do color conversion to fill the conversion buffer. */
- inrows = in_rows_avail - *in_row_ctr;
- numrows = prep->next_buf_stop - prep->next_buf_row;
- numrows = (int) MIN((JDIMENSION) numrows, inrows);
- (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
- prep->color_buf,
- (JDIMENSION) prep->next_buf_row,
- numrows);
- /* Pad at top of image, if first time through */
- if (prep->rows_to_go == cinfo->image_height) {
- for (ci = 0; ci < cinfo->num_components; ci++) {
- int row;
- for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
- jcopy_sample_rows(prep->color_buf[ci], 0,
- prep->color_buf[ci], -row,
- 1, cinfo->image_width);
- }
- }
- }
- *in_row_ctr += numrows;
- prep->next_buf_row += numrows;
- prep->rows_to_go -= numrows;
- } else {
- /* Return for more data, unless we are at the bottom of the image. */
- if (prep->rows_to_go != 0)
- break;
- /* When at bottom of image, pad to fill the conversion buffer. */
- if (prep->next_buf_row < prep->next_buf_stop) {
- for (ci = 0; ci < cinfo->num_components; ci++) {
- expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
- prep->next_buf_row, prep->next_buf_stop);
- }
- prep->next_buf_row = prep->next_buf_stop;
- }
- }
- /* If we've gotten enough data, downsample a row group. */
- if (prep->next_buf_row == prep->next_buf_stop) {
- (*cinfo->downsample->downsample) (cinfo,
- prep->color_buf,
- (JDIMENSION) prep->this_row_group,
- output_buf, *out_row_group_ctr);
- (*out_row_group_ctr)++;
- /* Advance pointers with wraparound as necessary. */
- prep->this_row_group += cinfo->max_v_samp_factor;
- if (prep->this_row_group >= buf_height)
- prep->this_row_group = 0;
- if (prep->next_buf_row >= buf_height)
- prep->next_buf_row = 0;
- prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
- }
- }
-}
-
-
-/*
- * Create the wrapped-around downsampling input buffer needed for context mode.
- */
-
-LOCAL(void)
-create_context_buffer (j_compress_ptr cinfo)
-{
- my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
- int rgroup_height = cinfo->max_v_samp_factor;
- int ci, i;
- jpeg_component_info * compptr;
- JSAMPARRAY true_buffer, fake_buffer;
-
- /* Grab enough space for fake row pointers for all the components;
- * we need five row groups' worth of pointers for each component.
- */
- fake_buffer = (JSAMPARRAY)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (cinfo->num_components * 5 * rgroup_height) *
- SIZEOF(JSAMPROW));
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Allocate the actual buffer space (3 row groups) for this component.
- * We make the buffer wide enough to allow the downsampler to edge-expand
- * horizontally within the buffer, if it so chooses.
- */
- true_buffer = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
- cinfo->max_h_samp_factor) / compptr->h_samp_factor),
- (JDIMENSION) (3 * rgroup_height));
- /* Copy true buffer row pointers into the middle of the fake row array */
- MEMCOPY(fake_buffer + rgroup_height, true_buffer,
- 3 * rgroup_height * SIZEOF(JSAMPROW));
- /* Fill in the above and below wraparound pointers */
- for (i = 0; i < rgroup_height; i++) {
- fake_buffer[i] = true_buffer[2 * rgroup_height + i];
- fake_buffer[4 * rgroup_height + i] = true_buffer[i];
- }
- prep->color_buf[ci] = fake_buffer + rgroup_height;
- fake_buffer += 5 * rgroup_height; /* point to space for next component */
- }
-}
-
-#endif /* CONTEXT_ROWS_SUPPORTED */
-
-
-/*
- * Initialize preprocessing controller.
- */
-
-GLOBAL(void)
-jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
-{
- my_prep_ptr prep;
- int ci;
- jpeg_component_info * compptr;
-
- if (need_full_buffer) /* safety check */
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
-
- prep = (my_prep_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_prep_controller));
- cinfo->prep = (struct jpeg_c_prep_controller *) prep;
- prep->pub.start_pass = start_pass_prep;
-
- /* Allocate the color conversion buffer.
- * We make the buffer wide enough to allow the downsampler to edge-expand
- * horizontally within the buffer, if it so chooses.
- */
- if (cinfo->downsample->need_context_rows) {
- /* Set up to provide context rows */
-#ifdef CONTEXT_ROWS_SUPPORTED
- prep->pub.pre_process_data = pre_process_context;
- create_context_buffer(cinfo);
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else {
- /* No context, just make it tall enough for one row group */
- prep->pub.pre_process_data = pre_process_data;
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
- cinfo->max_h_samp_factor) / compptr->h_samp_factor),
- (JDIMENSION) cinfo->max_v_samp_factor);
- }
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcprepct.c
+ *
+ * Copyright (C) 1994-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 contains the compression preprocessing controller.
+ * This controller manages the color conversion, downsampling,
+ * and edge expansion steps.
+ *
+ * Most of the complexity here is associated with buffering input rows
+ * as required by the downsampler. See the comments at the head of
+ * jcsample.c for the downsampler's needs.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* At present, jcsample.c can request context rows only for smoothing.
+ * In the future, we might also need context rows for CCIR601 sampling
+ * or other more-complex downsampling procedures. The code to support
+ * context rows should be compiled only if needed.
+ */
+#ifdef INPUT_SMOOTHING_SUPPORTED
+#define CONTEXT_ROWS_SUPPORTED
+#endif
+
+
+/*
+ * For the simple (no-context-row) case, we just need to buffer one
+ * row group's worth of pixels for the downsampling step. At the bottom of
+ * the image, we pad to a full row group by replicating the last pixel row.
+ * The downsampler's last output row is then replicated if needed to pad
+ * out to a full iMCU row.
+ *
+ * When providing context rows, we must buffer three row groups' worth of
+ * pixels. Three row groups are physically allocated, but the row pointer
+ * arrays are made five row groups high, with the extra pointers above and
+ * below "wrapping around" to point to the last and first real row groups.
+ * This allows the downsampler to access the proper context rows.
+ * At the top and bottom of the image, we create dummy context rows by
+ * copying the first or last real pixel row. This copying could be avoided
+ * by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
+ * trouble on the compression side.
+ */
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_c_prep_controller pub; /* public fields */
+
+ /* Downsampling input buffer. This buffer holds color-converted data
+ * until we have enough to do a downsample step.
+ */
+ JSAMPARRAY color_buf[MAX_COMPONENTS];
+
+ JDIMENSION rows_to_go; /* counts rows remaining in source image */
+ int next_buf_row; /* index of next row to store in color_buf */
+
+#ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
+ int this_row_group; /* starting row index of group to process */
+ int next_buf_stop; /* downsample when we reach this index */
+#endif
+} my_prep_controller;
+
+typedef my_prep_controller * my_prep_ptr;
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+
+ if (pass_mode != JBUF_PASS_THRU)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+ /* Initialize total-height counter for detecting bottom of image */
+ prep->rows_to_go = cinfo->image_height;
+ /* Mark the conversion buffer empty */
+ prep->next_buf_row = 0;
+#ifdef CONTEXT_ROWS_SUPPORTED
+ /* Preset additional state variables for context mode.
+ * These aren't used in non-context mode, so we needn't test which mode.
+ */
+ prep->this_row_group = 0;
+ /* Set next_buf_stop to stop after two row groups have been read in. */
+ prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
+#endif
+}
+
+
+/*
+ * Expand an image vertically from height input_rows to height output_rows,
+ * by duplicating the bottom row.
+ */
+
+LOCAL(void)
+expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
+ int input_rows, int output_rows)
+{
+ register int row;
+
+ for (row = input_rows; row < output_rows; row++) {
+ jcopy_sample_rows(image_data, input_rows-1, image_data, row,
+ 1, num_cols);
+ }
+}
+
+
+/*
+ * Process some data in the simple no-context case.
+ *
+ * Preprocessor output data is counted in "row groups". A row group
+ * is defined to be v_samp_factor sample rows of each component.
+ * Downsampling will produce this much data from each max_v_samp_factor
+ * input rows.
+ */
+
+METHODDEF(void)
+pre_process_data (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail,
+ JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
+ JDIMENSION out_row_groups_avail)
+{
+ my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+ int numrows, ci;
+ JDIMENSION inrows;
+ jpeg_component_info * compptr;
+
+ while (*in_row_ctr < in_rows_avail &&
+ *out_row_group_ctr < out_row_groups_avail) {
+ /* Do color conversion to fill the conversion buffer. */
+ inrows = in_rows_avail - *in_row_ctr;
+ numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
+ numrows = (int) MIN((JDIMENSION) numrows, inrows);
+ (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
+ prep->color_buf,
+ (JDIMENSION) prep->next_buf_row,
+ numrows);
+ *in_row_ctr += numrows;
+ prep->next_buf_row += numrows;
+ prep->rows_to_go -= numrows;
+ /* If at bottom of image, pad to fill the conversion buffer. */
+ if (prep->rows_to_go == 0 &&
+ prep->next_buf_row < cinfo->max_v_samp_factor) {
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
+ prep->next_buf_row, cinfo->max_v_samp_factor);
+ }
+ prep->next_buf_row = cinfo->max_v_samp_factor;
+ }
+ /* If we've filled the conversion buffer, empty it. */
+ if (prep->next_buf_row == cinfo->max_v_samp_factor) {
+ (*cinfo->downsample->downsample) (cinfo,
+ prep->color_buf, (JDIMENSION) 0,
+ output_buf, *out_row_group_ctr);
+ prep->next_buf_row = 0;
+ (*out_row_group_ctr)++;
+ }
+ /* If at bottom of image, pad the output to a full iMCU height.
+ * Note we assume the caller is providing a one-iMCU-height output buffer!
+ */
+ if (prep->rows_to_go == 0 &&
+ *out_row_group_ctr < out_row_groups_avail) {
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ expand_bottom_edge(output_buf[ci],
+ compptr->width_in_blocks * DCTSIZE,
+ (int) (*out_row_group_ctr * compptr->v_samp_factor),
+ (int) (out_row_groups_avail * compptr->v_samp_factor));
+ }
+ *out_row_group_ctr = out_row_groups_avail;
+ break; /* can exit outer loop without test */
+ }
+ }
+}
+
+
+#ifdef CONTEXT_ROWS_SUPPORTED
+
+/*
+ * Process some data in the context case.
+ */
+
+METHODDEF(void)
+pre_process_context (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail,
+ JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
+ JDIMENSION out_row_groups_avail)
+{
+ my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+ int numrows, ci;
+ int buf_height = cinfo->max_v_samp_factor * 3;
+ JDIMENSION inrows;
+
+ while (*out_row_group_ctr < out_row_groups_avail) {
+ if (*in_row_ctr < in_rows_avail) {
+ /* Do color conversion to fill the conversion buffer. */
+ inrows = in_rows_avail - *in_row_ctr;
+ numrows = prep->next_buf_stop - prep->next_buf_row;
+ numrows = (int) MIN((JDIMENSION) numrows, inrows);
+ (*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
+ prep->color_buf,
+ (JDIMENSION) prep->next_buf_row,
+ numrows);
+ /* Pad at top of image, if first time through */
+ if (prep->rows_to_go == cinfo->image_height) {
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ int row;
+ for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
+ jcopy_sample_rows(prep->color_buf[ci], 0,
+ prep->color_buf[ci], -row,
+ 1, cinfo->image_width);
+ }
+ }
+ }
+ *in_row_ctr += numrows;
+ prep->next_buf_row += numrows;
+ prep->rows_to_go -= numrows;
+ } else {
+ /* Return for more data, unless we are at the bottom of the image. */
+ if (prep->rows_to_go != 0)
+ break;
+ /* When at bottom of image, pad to fill the conversion buffer. */
+ if (prep->next_buf_row < prep->next_buf_stop) {
+ for (ci = 0; ci < cinfo->num_components; ci++) {
+ expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
+ prep->next_buf_row, prep->next_buf_stop);
+ }
+ prep->next_buf_row = prep->next_buf_stop;
+ }
+ }
+ /* If we've gotten enough data, downsample a row group. */
+ if (prep->next_buf_row == prep->next_buf_stop) {
+ (*cinfo->downsample->downsample) (cinfo,
+ prep->color_buf,
+ (JDIMENSION) prep->this_row_group,
+ output_buf, *out_row_group_ctr);
+ (*out_row_group_ctr)++;
+ /* Advance pointers with wraparound as necessary. */
+ prep->this_row_group += cinfo->max_v_samp_factor;
+ if (prep->this_row_group >= buf_height)
+ prep->this_row_group = 0;
+ if (prep->next_buf_row >= buf_height)
+ prep->next_buf_row = 0;
+ prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
+ }
+ }
+}
+
+
+/*
+ * Create the wrapped-around downsampling input buffer needed for context mode.
+ */
+
+LOCAL(void)
+create_context_buffer (j_compress_ptr cinfo)
+{
+ my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
+ int rgroup_height = cinfo->max_v_samp_factor;
+ int ci, i;
+ jpeg_component_info * compptr;
+ JSAMPARRAY true_buffer, fake_buffer;
+
+ /* Grab enough space for fake row pointers for all the components;
+ * we need five row groups' worth of pointers for each component.
+ */
+ fake_buffer = (JSAMPARRAY)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (cinfo->num_components * 5 * rgroup_height) *
+ SIZEOF(JSAMPROW));
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Allocate the actual buffer space (3 row groups) for this component.
+ * We make the buffer wide enough to allow the downsampler to edge-expand
+ * horizontally within the buffer, if it so chooses.
+ */
+ true_buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
+ cinfo->max_h_samp_factor) / compptr->h_samp_factor),
+ (JDIMENSION) (3 * rgroup_height));
+ /* Copy true buffer row pointers into the middle of the fake row array */
+ MEMCOPY(fake_buffer + rgroup_height, true_buffer,
+ 3 * rgroup_height * SIZEOF(JSAMPROW));
+ /* Fill in the above and below wraparound pointers */
+ for (i = 0; i < rgroup_height; i++) {
+ fake_buffer[i] = true_buffer[2 * rgroup_height + i];
+ fake_buffer[4 * rgroup_height + i] = true_buffer[i];
+ }
+ prep->color_buf[ci] = fake_buffer + rgroup_height;
+ fake_buffer += 5 * rgroup_height; /* point to space for next component */
+ }
+}
+
+#endif /* CONTEXT_ROWS_SUPPORTED */
+
+
+/*
+ * Initialize preprocessing controller.
+ */
+
+GLOBAL(void)
+jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
+{
+ my_prep_ptr prep;
+ int ci;
+ jpeg_component_info * compptr;
+
+ if (need_full_buffer) /* safety check */
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+ prep = (my_prep_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_prep_controller));
+ cinfo->prep = (struct jpeg_c_prep_controller *) prep;
+ prep->pub.start_pass = start_pass_prep;
+
+ /* Allocate the color conversion buffer.
+ * We make the buffer wide enough to allow the downsampler to edge-expand
+ * horizontally within the buffer, if it so chooses.
+ */
+ if (cinfo->downsample->need_context_rows) {
+ /* Set up to provide context rows */
+#ifdef CONTEXT_ROWS_SUPPORTED
+ prep->pub.pre_process_data = pre_process_context;
+ create_context_buffer(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ /* No context, just make it tall enough for one row group */
+ prep->pub.pre_process_data = pre_process_data;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
+ cinfo->max_h_samp_factor) / compptr->h_samp_factor),
+ (JDIMENSION) cinfo->max_v_samp_factor);
+ }
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jcsample.c b/core/src/fxcodec/libjpeg/fpdfapi_jcsample.c
index 7256bd7356..5e1e828740 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jcsample.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jcsample.c
@@ -1,522 +1,522 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jcsample.c
- *
- * Copyright (C) 1991-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 contains downsampling routines.
- *
- * Downsampling input data is counted in "row groups". A row group
- * is defined to be max_v_samp_factor pixel rows of each component,
- * from which the downsampler produces v_samp_factor sample rows.
- * A single row group is processed in each call to the downsampler module.
- *
- * The downsampler is responsible for edge-expansion of its output data
- * to fill an integral number of DCT blocks horizontally. The source buffer
- * may be modified if it is helpful for this purpose (the source buffer is
- * allocated wide enough to correspond to the desired output width).
- * The caller (the prep controller) is responsible for vertical padding.
- *
- * The downsampler may request "context rows" by setting need_context_rows
- * during startup. In this case, the input arrays will contain at least
- * one row group's worth of pixels above and below the passed-in data;
- * the caller will create dummy rows at image top and bottom by replicating
- * the first or last real pixel row.
- *
- * An excellent reference for image resampling is
- * Digital Image Warping, George Wolberg, 1990.
- * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
- *
- * The downsampling algorithm used here is a simple average of the source
- * pixels covered by the output pixel. The hi-falutin sampling literature
- * refers to this as a "box filter". In general the characteristics of a box
- * filter are not very good, but for the specific cases we normally use (1:1
- * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
- * nearly so bad. If you intend to use other sampling ratios, you'd be well
- * advised to improve this code.
- *
- * A simple input-smoothing capability is provided. This is mainly intended
- * for cleaning up color-dithered GIF input files (if you find it inadequate,
- * we suggest using an external filtering program such as pnmconvol). When
- * enabled, each input pixel P is replaced by a weighted sum of itself and its
- * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,
- * where SF = (smoothing_factor / 1024).
- * Currently, smoothing is only supported for 2h2v sampling factors.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Pointer to routine to downsample a single component */
-typedef JMETHOD(void, downsample1_ptr,
- (j_compress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY output_data));
-
-/* Private subobject */
-
-typedef struct {
- struct jpeg_downsampler pub; /* public fields */
-
- /* Downsampling method pointers, one per component */
- downsample1_ptr methods[MAX_COMPONENTS];
-} my_downsampler;
-
-typedef my_downsampler * my_downsample_ptr;
-
-
-/*
- * Initialize for a downsampling pass.
- */
-
-METHODDEF(void)
-start_pass_downsample (j_compress_ptr cinfo)
-{
- /* no work for now */
-}
-
-
-/*
- * Expand a component horizontally from width input_cols to width output_cols,
- * by duplicating the rightmost samples.
- */
-
-LOCAL(void)
-expand_right_edge (JSAMPARRAY image_data, int num_rows,
- JDIMENSION input_cols, JDIMENSION output_cols)
-{
- register JSAMPROW ptr;
- register JSAMPLE pixval;
- register int count;
- int row;
- int numcols = (int) (output_cols - input_cols);
-
- if (numcols > 0) {
- for (row = 0; row < num_rows; row++) {
- ptr = image_data[row] + input_cols;
- pixval = ptr[-1]; /* don't need GETJSAMPLE() here */
- for (count = numcols; count > 0; count--)
- *ptr++ = pixval;
- }
- }
-}
-
-
-/*
- * Do downsampling for a whole row group (all components).
- *
- * In this version we simply downsample each component independently.
- */
-
-METHODDEF(void)
-sep_downsample (j_compress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION in_row_index,
- JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
-{
- my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
- int ci;
- jpeg_component_info * compptr;
- JSAMPARRAY in_ptr, out_ptr;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- in_ptr = input_buf[ci] + in_row_index;
- out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
- (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
- }
-}
-
-
-/*
- * Downsample pixel values of a single component.
- * One row group is processed per call.
- * This version handles arbitrary integral sampling ratios, without smoothing.
- * Note that this version is not actually used for customary sampling ratios.
- */
-
-METHODDEF(void)
-int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY output_data)
-{
- int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
- JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */
- JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
- JSAMPROW inptr, outptr;
- INT32 outvalue;
-
- h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
- v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
- numpix = h_expand * v_expand;
- numpix2 = numpix/2;
-
- /* Expand input data enough to let all the output samples be generated
- * by the standard loop. Special-casing padded output would be more
- * efficient.
- */
- expand_right_edge(input_data, cinfo->max_v_samp_factor,
- cinfo->image_width, output_cols * h_expand);
-
- inrow = 0;
- for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
- outptr = output_data[outrow];
- for (outcol = 0, outcol_h = 0; outcol < output_cols;
- outcol++, outcol_h += h_expand) {
- outvalue = 0;
- for (v = 0; v < v_expand; v++) {
- inptr = input_data[inrow+v] + outcol_h;
- for (h = 0; h < h_expand; h++) {
- outvalue += (INT32) GETJSAMPLE(*inptr++);
- }
- }
- *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
- }
- inrow += v_expand;
- }
-}
-
-
-/*
- * Downsample pixel values of a single component.
- * This version handles the special case of a full-size component,
- * without smoothing.
- */
-
-METHODDEF(void)
-fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY output_data)
-{
- /* Copy the data */
- jcopy_sample_rows(input_data, 0, output_data, 0,
- cinfo->max_v_samp_factor, cinfo->image_width);
- /* Edge-expand */
- expand_right_edge(output_data, cinfo->max_v_samp_factor,
- cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
-}
-
-
-/*
- * Downsample pixel values of a single component.
- * This version handles the common case of 2:1 horizontal and 1:1 vertical,
- * without smoothing.
- *
- * A note about the "bias" calculations: when rounding fractional values to
- * integer, we do not want to always round 0.5 up to the next integer.
- * If we did that, we'd introduce a noticeable bias towards larger values.
- * Instead, this code is arranged so that 0.5 will be rounded up or down at
- * alternate pixel locations (a simple ordered dither pattern).
- */
-
-METHODDEF(void)
-h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY output_data)
-{
- int outrow;
- JDIMENSION outcol;
- JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
- register JSAMPROW inptr, outptr;
- register int bias;
-
- /* Expand input data enough to let all the output samples be generated
- * by the standard loop. Special-casing padded output would be more
- * efficient.
- */
- expand_right_edge(input_data, cinfo->max_v_samp_factor,
- cinfo->image_width, output_cols * 2);
-
- for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
- outptr = output_data[outrow];
- inptr = input_data[outrow];
- bias = 0; /* bias = 0,1,0,1,... for successive samples */
- for (outcol = 0; outcol < output_cols; outcol++) {
- *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
- + bias) >> 1);
- bias ^= 1; /* 0=>1, 1=>0 */
- inptr += 2;
- }
- }
-}
-
-
-/*
- * Downsample pixel values of a single component.
- * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
- * without smoothing.
- */
-
-METHODDEF(void)
-h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY output_data)
-{
- int inrow, outrow;
- JDIMENSION outcol;
- JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
- register JSAMPROW inptr0, inptr1, outptr;
- register int bias;
-
- /* Expand input data enough to let all the output samples be generated
- * by the standard loop. Special-casing padded output would be more
- * efficient.
- */
- expand_right_edge(input_data, cinfo->max_v_samp_factor,
- cinfo->image_width, output_cols * 2);
-
- inrow = 0;
- for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
- outptr = output_data[outrow];
- inptr0 = input_data[inrow];
- inptr1 = input_data[inrow+1];
- bias = 1; /* bias = 1,2,1,2,... for successive samples */
- for (outcol = 0; outcol < output_cols; outcol++) {
- *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
- GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
- + bias) >> 2);
- bias ^= 3; /* 1=>2, 2=>1 */
- inptr0 += 2; inptr1 += 2;
- }
- inrow += 2;
- }
-}
-
-
-#ifdef INPUT_SMOOTHING_SUPPORTED
-
-/*
- * Downsample pixel values of a single component.
- * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
- * with smoothing. One row of context is required.
- */
-
-METHODDEF(void)
-h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY output_data)
-{
- int inrow, outrow;
- JDIMENSION colctr;
- JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
- register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
- INT32 membersum, neighsum, memberscale, neighscale;
-
- /* Expand input data enough to let all the output samples be generated
- * by the standard loop. Special-casing padded output would be more
- * efficient.
- */
- expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
- cinfo->image_width, output_cols * 2);
-
- /* We don't bother to form the individual "smoothed" input pixel values;
- * we can directly compute the output which is the average of the four
- * smoothed values. Each of the four member pixels contributes a fraction
- * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
- * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
- * output. The four corner-adjacent neighbor pixels contribute a fraction
- * SF to just one smoothed pixel, or SF/4 to the final output; while the
- * eight edge-adjacent neighbors contribute SF to each of two smoothed
- * pixels, or SF/2 overall. In order to use integer arithmetic, these
- * factors are scaled by 2^16 = 65536.
- * Also recall that SF = smoothing_factor / 1024.
- */
-
- memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
- neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
-
- inrow = 0;
- for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
- outptr = output_data[outrow];
- inptr0 = input_data[inrow];
- inptr1 = input_data[inrow+1];
- above_ptr = input_data[inrow-1];
- below_ptr = input_data[inrow+2];
-
- /* Special case for first column: pretend column -1 is same as column 0 */
- membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
- GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
- neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
- GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
- GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
- GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
- neighsum += neighsum;
- neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
- GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
- membersum = membersum * memberscale + neighsum * neighscale;
- *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
- inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
-
- for (colctr = output_cols - 2; colctr > 0; colctr--) {
- /* sum of pixels directly mapped to this output element */
- membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
- GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
- /* sum of edge-neighbor pixels */
- neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
- GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
- GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
- GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
- /* The edge-neighbors count twice as much as corner-neighbors */
- neighsum += neighsum;
- /* Add in the corner-neighbors */
- neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
- GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
- /* form final output scaled up by 2^16 */
- membersum = membersum * memberscale + neighsum * neighscale;
- /* round, descale and output it */
- *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
- inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
- }
-
- /* Special case for last column */
- membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
- GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
- neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
- GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
- GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
- GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
- neighsum += neighsum;
- neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
- GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
- membersum = membersum * memberscale + neighsum * neighscale;
- *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
-
- inrow += 2;
- }
-}
-
-
-/*
- * Downsample pixel values of a single component.
- * This version handles the special case of a full-size component,
- * with smoothing. One row of context is required.
- */
-
-METHODDEF(void)
-fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
- JSAMPARRAY input_data, JSAMPARRAY output_data)
-{
- int outrow;
- JDIMENSION colctr;
- JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
- register JSAMPROW inptr, above_ptr, below_ptr, outptr;
- INT32 membersum, neighsum, memberscale, neighscale;
- int colsum, lastcolsum, nextcolsum;
-
- /* Expand input data enough to let all the output samples be generated
- * by the standard loop. Special-casing padded output would be more
- * efficient.
- */
- expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
- cinfo->image_width, output_cols);
-
- /* Each of the eight neighbor pixels contributes a fraction SF to the
- * smoothed pixel, while the main pixel contributes (1-8*SF). In order
- * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
- * Also recall that SF = smoothing_factor / 1024.
- */
-
- memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
- neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
-
- for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
- outptr = output_data[outrow];
- inptr = input_data[outrow];
- above_ptr = input_data[outrow-1];
- below_ptr = input_data[outrow+1];
-
- /* Special case for first column */
- colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
- GETJSAMPLE(*inptr);
- membersum = GETJSAMPLE(*inptr++);
- nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
- GETJSAMPLE(*inptr);
- neighsum = colsum + (colsum - membersum) + nextcolsum;
- membersum = membersum * memberscale + neighsum * neighscale;
- *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
- lastcolsum = colsum; colsum = nextcolsum;
-
- for (colctr = output_cols - 2; colctr > 0; colctr--) {
- membersum = GETJSAMPLE(*inptr++);
- above_ptr++; below_ptr++;
- nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
- GETJSAMPLE(*inptr);
- neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
- membersum = membersum * memberscale + neighsum * neighscale;
- *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
- lastcolsum = colsum; colsum = nextcolsum;
- }
-
- /* Special case for last column */
- membersum = GETJSAMPLE(*inptr);
- neighsum = lastcolsum + (colsum - membersum) + colsum;
- membersum = membersum * memberscale + neighsum * neighscale;
- *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
-
- }
-}
-
-#endif /* INPUT_SMOOTHING_SUPPORTED */
-
-
-/*
- * Module initialization routine for downsampling.
- * Note that we must select a routine for each component.
- */
-
-GLOBAL(void)
-jinit_downsampler (j_compress_ptr cinfo)
-{
- my_downsample_ptr downsample;
- int ci;
- jpeg_component_info * compptr;
- boolean smoothok = TRUE;
-
- downsample = (my_downsample_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_downsampler));
- cinfo->downsample = (struct jpeg_downsampler *) downsample;
- downsample->pub.start_pass = start_pass_downsample;
- downsample->pub.downsample = sep_downsample;
- downsample->pub.need_context_rows = FALSE;
-
- if (cinfo->CCIR601_sampling)
- ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
-
- /* Verify we can handle the sampling factors, and set up method pointers */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
- compptr->v_samp_factor == cinfo->max_v_samp_factor) {
-#ifdef INPUT_SMOOTHING_SUPPORTED
- if (cinfo->smoothing_factor) {
- downsample->methods[ci] = fullsize_smooth_downsample;
- downsample->pub.need_context_rows = TRUE;
- } else
-#endif
- downsample->methods[ci] = fullsize_downsample;
- } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
- compptr->v_samp_factor == cinfo->max_v_samp_factor) {
- smoothok = FALSE;
- downsample->methods[ci] = h2v1_downsample;
- } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
- compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
-#ifdef INPUT_SMOOTHING_SUPPORTED
- if (cinfo->smoothing_factor) {
- downsample->methods[ci] = h2v2_smooth_downsample;
- downsample->pub.need_context_rows = TRUE;
- } else
-#endif
- downsample->methods[ci] = h2v2_downsample;
- } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
- (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
- smoothok = FALSE;
- downsample->methods[ci] = int_downsample;
- } else
- ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
- }
-
-#ifdef INPUT_SMOOTHING_SUPPORTED
- if (cinfo->smoothing_factor && !smoothok)
- TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
-#endif
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jcsample.c
+ *
+ * Copyright (C) 1991-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 contains downsampling routines.
+ *
+ * Downsampling input data is counted in "row groups". A row group
+ * is defined to be max_v_samp_factor pixel rows of each component,
+ * from which the downsampler produces v_samp_factor sample rows.
+ * A single row group is processed in each call to the downsampler module.
+ *
+ * The downsampler is responsible for edge-expansion of its output data
+ * to fill an integral number of DCT blocks horizontally. The source buffer
+ * may be modified if it is helpful for this purpose (the source buffer is
+ * allocated wide enough to correspond to the desired output width).
+ * The caller (the prep controller) is responsible for vertical padding.
+ *
+ * The downsampler may request "context rows" by setting need_context_rows
+ * during startup. In this case, the input arrays will contain at least
+ * one row group's worth of pixels above and below the passed-in data;
+ * the caller will create dummy rows at image top and bottom by replicating
+ * the first or last real pixel row.
+ *
+ * An excellent reference for image resampling is
+ * Digital Image Warping, George Wolberg, 1990.
+ * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
+ *
+ * The downsampling algorithm used here is a simple average of the source
+ * pixels covered by the output pixel. The hi-falutin sampling literature
+ * refers to this as a "box filter". In general the characteristics of a box
+ * filter are not very good, but for the specific cases we normally use (1:1
+ * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
+ * nearly so bad. If you intend to use other sampling ratios, you'd be well
+ * advised to improve this code.
+ *
+ * A simple input-smoothing capability is provided. This is mainly intended
+ * for cleaning up color-dithered GIF input files (if you find it inadequate,
+ * we suggest using an external filtering program such as pnmconvol). When
+ * enabled, each input pixel P is replaced by a weighted sum of itself and its
+ * eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,
+ * where SF = (smoothing_factor / 1024).
+ * Currently, smoothing is only supported for 2h2v sampling factors.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Pointer to routine to downsample a single component */
+typedef JMETHOD(void, downsample1_ptr,
+ (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data));
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_downsampler pub; /* public fields */
+
+ /* Downsampling method pointers, one per component */
+ downsample1_ptr methods[MAX_COMPONENTS];
+} my_downsampler;
+
+typedef my_downsampler * my_downsample_ptr;
+
+
+/*
+ * Initialize for a downsampling pass.
+ */
+
+METHODDEF(void)
+start_pass_downsample (j_compress_ptr cinfo)
+{
+ /* no work for now */
+}
+
+
+/*
+ * Expand a component horizontally from width input_cols to width output_cols,
+ * by duplicating the rightmost samples.
+ */
+
+LOCAL(void)
+expand_right_edge (JSAMPARRAY image_data, int num_rows,
+ JDIMENSION input_cols, JDIMENSION output_cols)
+{
+ register JSAMPROW ptr;
+ register JSAMPLE pixval;
+ register int count;
+ int row;
+ int numcols = (int) (output_cols - input_cols);
+
+ if (numcols > 0) {
+ for (row = 0; row < num_rows; row++) {
+ ptr = image_data[row] + input_cols;
+ pixval = ptr[-1]; /* don't need GETJSAMPLE() here */
+ for (count = numcols; count > 0; count--)
+ *ptr++ = pixval;
+ }
+ }
+}
+
+
+/*
+ * Do downsampling for a whole row group (all components).
+ *
+ * In this version we simply downsample each component independently.
+ */
+
+METHODDEF(void)
+sep_downsample (j_compress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_index,
+ JSAMPIMAGE output_buf, JDIMENSION out_row_group_index)
+{
+ my_downsample_ptr downsample = (my_downsample_ptr) cinfo->downsample;
+ int ci;
+ jpeg_component_info * compptr;
+ JSAMPARRAY in_ptr, out_ptr;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ in_ptr = input_buf[ci] + in_row_index;
+ out_ptr = output_buf[ci] + (out_row_group_index * compptr->v_samp_factor);
+ (*downsample->methods[ci]) (cinfo, compptr, in_ptr, out_ptr);
+ }
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * One row group is processed per call.
+ * This version handles arbitrary integral sampling ratios, without smoothing.
+ * Note that this version is not actually used for customary sampling ratios.
+ */
+
+METHODDEF(void)
+int_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
+ JDIMENSION outcol, outcol_h; /* outcol_h == outcol*h_expand */
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ JSAMPROW inptr, outptr;
+ INT32 outvalue;
+
+ h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
+ v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
+ numpix = h_expand * v_expand;
+ numpix2 = numpix/2;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data, cinfo->max_v_samp_factor,
+ cinfo->image_width, output_cols * h_expand);
+
+ inrow = 0;
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ for (outcol = 0, outcol_h = 0; outcol < output_cols;
+ outcol++, outcol_h += h_expand) {
+ outvalue = 0;
+ for (v = 0; v < v_expand; v++) {
+ inptr = input_data[inrow+v] + outcol_h;
+ for (h = 0; h < h_expand; h++) {
+ outvalue += (INT32) GETJSAMPLE(*inptr++);
+ }
+ }
+ *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
+ }
+ inrow += v_expand;
+ }
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the special case of a full-size component,
+ * without smoothing.
+ */
+
+METHODDEF(void)
+fullsize_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ /* Copy the data */
+ jcopy_sample_rows(input_data, 0, output_data, 0,
+ cinfo->max_v_samp_factor, cinfo->image_width);
+ /* Edge-expand */
+ expand_right_edge(output_data, cinfo->max_v_samp_factor,
+ cinfo->image_width, compptr->width_in_blocks * DCTSIZE);
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the common case of 2:1 horizontal and 1:1 vertical,
+ * without smoothing.
+ *
+ * A note about the "bias" calculations: when rounding fractional values to
+ * integer, we do not want to always round 0.5 up to the next integer.
+ * If we did that, we'd introduce a noticeable bias towards larger values.
+ * Instead, this code is arranged so that 0.5 will be rounded up or down at
+ * alternate pixel locations (a simple ordered dither pattern).
+ */
+
+METHODDEF(void)
+h2v1_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int outrow;
+ JDIMENSION outcol;
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ register JSAMPROW inptr, outptr;
+ register int bias;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data, cinfo->max_v_samp_factor,
+ cinfo->image_width, output_cols * 2);
+
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ inptr = input_data[outrow];
+ bias = 0; /* bias = 0,1,0,1,... for successive samples */
+ for (outcol = 0; outcol < output_cols; outcol++) {
+ *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
+ + bias) >> 1);
+ bias ^= 1; /* 0=>1, 1=>0 */
+ inptr += 2;
+ }
+ }
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
+ * without smoothing.
+ */
+
+METHODDEF(void)
+h2v2_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int inrow, outrow;
+ JDIMENSION outcol;
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ register JSAMPROW inptr0, inptr1, outptr;
+ register int bias;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data, cinfo->max_v_samp_factor,
+ cinfo->image_width, output_cols * 2);
+
+ inrow = 0;
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ inptr0 = input_data[inrow];
+ inptr1 = input_data[inrow+1];
+ bias = 1; /* bias = 1,2,1,2,... for successive samples */
+ for (outcol = 0; outcol < output_cols; outcol++) {
+ *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
+ + bias) >> 2);
+ bias ^= 3; /* 1=>2, 2=>1 */
+ inptr0 += 2; inptr1 += 2;
+ }
+ inrow += 2;
+ }
+}
+
+
+#ifdef INPUT_SMOOTHING_SUPPORTED
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
+ * with smoothing. One row of context is required.
+ */
+
+METHODDEF(void)
+h2v2_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int inrow, outrow;
+ JDIMENSION colctr;
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
+ INT32 membersum, neighsum, memberscale, neighscale;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
+ cinfo->image_width, output_cols * 2);
+
+ /* We don't bother to form the individual "smoothed" input pixel values;
+ * we can directly compute the output which is the average of the four
+ * smoothed values. Each of the four member pixels contributes a fraction
+ * (1-8*SF) to its own smoothed image and a fraction SF to each of the three
+ * other smoothed pixels, therefore a total fraction (1-5*SF)/4 to the final
+ * output. The four corner-adjacent neighbor pixels contribute a fraction
+ * SF to just one smoothed pixel, or SF/4 to the final output; while the
+ * eight edge-adjacent neighbors contribute SF to each of two smoothed
+ * pixels, or SF/2 overall. In order to use integer arithmetic, these
+ * factors are scaled by 2^16 = 65536.
+ * Also recall that SF = smoothing_factor / 1024.
+ */
+
+ memberscale = 16384 - cinfo->smoothing_factor * 80; /* scaled (1-5*SF)/4 */
+ neighscale = cinfo->smoothing_factor * 16; /* scaled SF/4 */
+
+ inrow = 0;
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ inptr0 = input_data[inrow];
+ inptr1 = input_data[inrow+1];
+ above_ptr = input_data[inrow-1];
+ below_ptr = input_data[inrow+2];
+
+ /* Special case for first column: pretend column -1 is same as column 0 */
+ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+ neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
+ GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+ GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[2]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[2]);
+ neighsum += neighsum;
+ neighsum += GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[2]) +
+ GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[2]);
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
+ inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
+
+ for (colctr = output_cols - 2; colctr > 0; colctr--) {
+ /* sum of pixels directly mapped to this output element */
+ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+ /* sum of edge-neighbor pixels */
+ neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
+ GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+ GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[2]) +
+ GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[2]);
+ /* The edge-neighbors count twice as much as corner-neighbors */
+ neighsum += neighsum;
+ /* Add in the corner-neighbors */
+ neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[2]) +
+ GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[2]);
+ /* form final output scaled up by 2^16 */
+ membersum = membersum * memberscale + neighsum * neighscale;
+ /* round, descale and output it */
+ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
+ inptr0 += 2; inptr1 += 2; above_ptr += 2; below_ptr += 2;
+ }
+
+ /* Special case for last column */
+ membersum = GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1]);
+ neighsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(above_ptr[1]) +
+ GETJSAMPLE(*below_ptr) + GETJSAMPLE(below_ptr[1]) +
+ GETJSAMPLE(inptr0[-1]) + GETJSAMPLE(inptr0[1]) +
+ GETJSAMPLE(inptr1[-1]) + GETJSAMPLE(inptr1[1]);
+ neighsum += neighsum;
+ neighsum += GETJSAMPLE(above_ptr[-1]) + GETJSAMPLE(above_ptr[1]) +
+ GETJSAMPLE(below_ptr[-1]) + GETJSAMPLE(below_ptr[1]);
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
+
+ inrow += 2;
+ }
+}
+
+
+/*
+ * Downsample pixel values of a single component.
+ * This version handles the special case of a full-size component,
+ * with smoothing. One row of context is required.
+ */
+
+METHODDEF(void)
+fullsize_smooth_downsample (j_compress_ptr cinfo, jpeg_component_info *compptr,
+ JSAMPARRAY input_data, JSAMPARRAY output_data)
+{
+ int outrow;
+ JDIMENSION colctr;
+ JDIMENSION output_cols = compptr->width_in_blocks * DCTSIZE;
+ register JSAMPROW inptr, above_ptr, below_ptr, outptr;
+ INT32 membersum, neighsum, memberscale, neighscale;
+ int colsum, lastcolsum, nextcolsum;
+
+ /* Expand input data enough to let all the output samples be generated
+ * by the standard loop. Special-casing padded output would be more
+ * efficient.
+ */
+ expand_right_edge(input_data - 1, cinfo->max_v_samp_factor + 2,
+ cinfo->image_width, output_cols);
+
+ /* Each of the eight neighbor pixels contributes a fraction SF to the
+ * smoothed pixel, while the main pixel contributes (1-8*SF). In order
+ * to use integer arithmetic, these factors are multiplied by 2^16 = 65536.
+ * Also recall that SF = smoothing_factor / 1024.
+ */
+
+ memberscale = 65536L - cinfo->smoothing_factor * 512L; /* scaled 1-8*SF */
+ neighscale = cinfo->smoothing_factor * 64; /* scaled SF */
+
+ for (outrow = 0; outrow < compptr->v_samp_factor; outrow++) {
+ outptr = output_data[outrow];
+ inptr = input_data[outrow];
+ above_ptr = input_data[outrow-1];
+ below_ptr = input_data[outrow+1];
+
+ /* Special case for first column */
+ colsum = GETJSAMPLE(*above_ptr++) + GETJSAMPLE(*below_ptr++) +
+ GETJSAMPLE(*inptr);
+ membersum = GETJSAMPLE(*inptr++);
+ nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
+ GETJSAMPLE(*inptr);
+ neighsum = colsum + (colsum - membersum) + nextcolsum;
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
+ lastcolsum = colsum; colsum = nextcolsum;
+
+ for (colctr = output_cols - 2; colctr > 0; colctr--) {
+ membersum = GETJSAMPLE(*inptr++);
+ above_ptr++; below_ptr++;
+ nextcolsum = GETJSAMPLE(*above_ptr) + GETJSAMPLE(*below_ptr) +
+ GETJSAMPLE(*inptr);
+ neighsum = lastcolsum + (colsum - membersum) + nextcolsum;
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr++ = (JSAMPLE) ((membersum + 32768) >> 16);
+ lastcolsum = colsum; colsum = nextcolsum;
+ }
+
+ /* Special case for last column */
+ membersum = GETJSAMPLE(*inptr);
+ neighsum = lastcolsum + (colsum - membersum) + colsum;
+ membersum = membersum * memberscale + neighsum * neighscale;
+ *outptr = (JSAMPLE) ((membersum + 32768) >> 16);
+
+ }
+}
+
+#endif /* INPUT_SMOOTHING_SUPPORTED */
+
+
+/*
+ * Module initialization routine for downsampling.
+ * Note that we must select a routine for each component.
+ */
+
+GLOBAL(void)
+jinit_downsampler (j_compress_ptr cinfo)
+{
+ my_downsample_ptr downsample;
+ int ci;
+ jpeg_component_info * compptr;
+ boolean smoothok = TRUE;
+
+ downsample = (my_downsample_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_downsampler));
+ cinfo->downsample = (struct jpeg_downsampler *) downsample;
+ downsample->pub.start_pass = start_pass_downsample;
+ downsample->pub.downsample = sep_downsample;
+ downsample->pub.need_context_rows = FALSE;
+
+ if (cinfo->CCIR601_sampling)
+ ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
+
+ /* Verify we can handle the sampling factors, and set up method pointers */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (compptr->h_samp_factor == cinfo->max_h_samp_factor &&
+ compptr->v_samp_factor == cinfo->max_v_samp_factor) {
+#ifdef INPUT_SMOOTHING_SUPPORTED
+ if (cinfo->smoothing_factor) {
+ downsample->methods[ci] = fullsize_smooth_downsample;
+ downsample->pub.need_context_rows = TRUE;
+ } else
+#endif
+ downsample->methods[ci] = fullsize_downsample;
+ } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
+ compptr->v_samp_factor == cinfo->max_v_samp_factor) {
+ smoothok = FALSE;
+ downsample->methods[ci] = h2v1_downsample;
+ } else if (compptr->h_samp_factor * 2 == cinfo->max_h_samp_factor &&
+ compptr->v_samp_factor * 2 == cinfo->max_v_samp_factor) {
+#ifdef INPUT_SMOOTHING_SUPPORTED
+ if (cinfo->smoothing_factor) {
+ downsample->methods[ci] = h2v2_smooth_downsample;
+ downsample->pub.need_context_rows = TRUE;
+ } else
+#endif
+ downsample->methods[ci] = h2v2_downsample;
+ } else if ((cinfo->max_h_samp_factor % compptr->h_samp_factor) == 0 &&
+ (cinfo->max_v_samp_factor % compptr->v_samp_factor) == 0) {
+ smoothok = FALSE;
+ downsample->methods[ci] = int_downsample;
+ } else
+ ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
+ }
+
+#ifdef INPUT_SMOOTHING_SUPPORTED
+ if (cinfo->smoothing_factor && !smoothok)
+ TRACEMS(cinfo, 0, JTRC_SMOOTH_NOTIMPL);
+#endif
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jctrans.c b/core/src/fxcodec/libjpeg/fpdfapi_jctrans.c
index ed541159f1..40d166736b 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jctrans.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jctrans.c
@@ -1,391 +1,391 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jctrans.c
- *
- * Copyright (C) 1995-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 library routines for transcoding compression,
- * that is, writing raw DCT coefficient arrays to an output JPEG file.
- * The routines in jcapimin.c will also be needed by a transcoder.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Forward declarations */
-LOCAL(void) transencode_master_selection
- JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
-LOCAL(void) transencode_coef_controller
- JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
-
-
-/*
- * Compression initialization for writing raw-coefficient data.
- * Before calling this, all parameters and a data destination must be set up.
- * Call jpeg_finish_compress() to actually write the data.
- *
- * The number of passed virtual arrays must match cinfo->num_components.
- * Note that the virtual arrays need not be filled or even realized at
- * the time write_coefficients is called; indeed, if the virtual arrays
- * were requested from this compression object's memory manager, they
- * typically will be realized during this routine and filled afterwards.
- */
-
-GLOBAL(void)
-jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
-{
- if (cinfo->global_state != CSTATE_START)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- /* Mark all tables to be written */
- jpeg_suppress_tables(cinfo, FALSE);
- /* (Re)initialize error mgr and destination modules */
- (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
- (*cinfo->dest->init_destination) (cinfo);
- /* Perform master selection of active modules */
- transencode_master_selection(cinfo, coef_arrays);
- /* Wait for jpeg_finish_compress() call */
- cinfo->next_scanline = 0; /* so jpeg_write_marker works */
- cinfo->global_state = CSTATE_WRCOEFS;
-}
-
-
-/*
- * Initialize the compression object with default parameters,
- * then copy from the source object all parameters needed for lossless
- * transcoding. Parameters that can be varied without loss (such as
- * scan script and Huffman optimization) are left in their default states.
- */
-
-GLOBAL(void)
-jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
- j_compress_ptr dstinfo)
-{
- JQUANT_TBL ** qtblptr;
- jpeg_component_info *incomp, *outcomp;
- JQUANT_TBL *c_quant, *slot_quant;
- int tblno, ci, coefi;
-
- /* Safety check to ensure start_compress not called yet. */
- if (dstinfo->global_state != CSTATE_START)
- ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
- /* Copy fundamental image dimensions */
- dstinfo->image_width = srcinfo->image_width;
- dstinfo->image_height = srcinfo->image_height;
- dstinfo->input_components = srcinfo->num_components;
- dstinfo->in_color_space = srcinfo->jpeg_color_space;
- /* Initialize all parameters to default values */
- jpeg_set_defaults(dstinfo);
- /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
- * Fix it to get the right header markers for the image colorspace.
- */
- jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
- dstinfo->data_precision = srcinfo->data_precision;
- dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
- /* Copy the source's quantization tables. */
- for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
- if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
- qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
- if (*qtblptr == NULL)
- *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
- MEMCOPY((*qtblptr)->quantval,
- srcinfo->quant_tbl_ptrs[tblno]->quantval,
- SIZEOF((*qtblptr)->quantval));
- (*qtblptr)->sent_table = FALSE;
- }
- }
- /* Copy the source's per-component info.
- * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
- */
- dstinfo->num_components = srcinfo->num_components;
- if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
- ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
- MAX_COMPONENTS);
- for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
- ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
- outcomp->component_id = incomp->component_id;
- outcomp->h_samp_factor = incomp->h_samp_factor;
- outcomp->v_samp_factor = incomp->v_samp_factor;
- outcomp->quant_tbl_no = incomp->quant_tbl_no;
- /* Make sure saved quantization table for component matches the qtable
- * slot. If not, the input file re-used this qtable slot.
- * IJG encoder currently cannot duplicate this.
- */
- tblno = outcomp->quant_tbl_no;
- if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
- srcinfo->quant_tbl_ptrs[tblno] == NULL)
- ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
- slot_quant = srcinfo->quant_tbl_ptrs[tblno];
- c_quant = incomp->quant_table;
- if (c_quant != NULL) {
- for (coefi = 0; coefi < DCTSIZE2; coefi++) {
- if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
- ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
- }
- }
- /* Note: we do not copy the source's Huffman table assignments;
- * instead we rely on jpeg_set_colorspace to have made a suitable choice.
- */
- }
- /* Also copy JFIF version and resolution information, if available.
- * Strictly speaking this isn't "critical" info, but it's nearly
- * always appropriate to copy it if available. In particular,
- * if the application chooses to copy JFIF 1.02 extension markers from
- * the source file, we need to copy the version to make sure we don't
- * emit a file that has 1.02 extensions but a claimed version of 1.01.
- * We will *not*, however, copy version info from mislabeled "2.01" files.
- */
- if (srcinfo->saw_JFIF_marker) {
- if (srcinfo->JFIF_major_version == 1) {
- dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
- dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
- }
- dstinfo->density_unit = srcinfo->density_unit;
- dstinfo->X_density = srcinfo->X_density;
- dstinfo->Y_density = srcinfo->Y_density;
- }
-}
-
-
-/*
- * Master selection of compression modules for transcoding.
- * This substitutes for jcinit.c's initialization of the full compressor.
- */
-
-LOCAL(void)
-transencode_master_selection (j_compress_ptr cinfo,
- jvirt_barray_ptr * coef_arrays)
-{
- /* Although we don't actually use input_components for transcoding,
- * jcmaster.c's initial_setup will complain if input_components is 0.
- */
- cinfo->input_components = 1;
- /* Initialize master control (includes parameter checking/processing) */
- jinit_c_master_control(cinfo, TRUE /* transcode only */);
-
- /* Entropy encoding: either Huffman or arithmetic coding. */
- if (cinfo->arith_code) {
- ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
- } else {
- if (cinfo->progressive_mode) {
-#ifdef C_PROGRESSIVE_SUPPORTED
- jinit_phuff_encoder(cinfo);
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else
- jinit_huff_encoder(cinfo);
- }
-
- /* We need a special coefficient buffer controller. */
- transencode_coef_controller(cinfo, coef_arrays);
-
- jinit_marker_writer(cinfo);
-
- /* We can now tell the memory manager to allocate virtual arrays. */
- (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
-
- /* Write the datastream header (SOI, JFIF) immediately.
- * Frame and scan headers are postponed till later.
- * This lets application insert special markers after the SOI.
- */
- (*cinfo->marker->write_file_header) (cinfo);
-}
-
-
-/*
- * The rest of this file is a special implementation of the coefficient
- * buffer controller. This is similar to jccoefct.c, but it handles only
- * output from presupplied virtual arrays. Furthermore, we generate any
- * dummy padding blocks on-the-fly rather than expecting them to be present
- * in the arrays.
- */
-
-/* Private buffer controller object */
-
-typedef struct {
- struct jpeg_c_coef_controller pub; /* public fields */
-
- JDIMENSION iMCU_row_num; /* iMCU row # within image */
- JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
- int MCU_vert_offset; /* counts MCU rows within iMCU row */
- int MCU_rows_per_iMCU_row; /* number of such rows needed */
-
- /* Virtual block array for each component. */
- jvirt_barray_ptr * whole_image;
-
- /* Workspace for constructing dummy blocks at right/bottom edges. */
- JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
-} my_coef_controller;
-
-typedef my_coef_controller * my_coef_ptr;
-
-
-LOCAL(void)
-start_iMCU_row (j_compress_ptr cinfo)
-/* Reset within-iMCU-row counters for a new row */
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
-
- /* In an interleaved scan, an MCU row is the same as an iMCU row.
- * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
- * But at the bottom of the image, process only what's left.
- */
- if (cinfo->comps_in_scan > 1) {
- coef->MCU_rows_per_iMCU_row = 1;
- } else {
- if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
- coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
- else
- coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
- }
-
- coef->mcu_ctr = 0;
- coef->MCU_vert_offset = 0;
-}
-
-
-/*
- * Initialize for a processing pass.
- */
-
-METHODDEF(void)
-start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
-
- if (pass_mode != JBUF_CRANK_DEST)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
-
- coef->iMCU_row_num = 0;
- start_iMCU_row(cinfo);
-}
-
-
-/*
- * Process some data.
- * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
- * per call, ie, v_samp_factor block rows for each component in the scan.
- * The data is obtained from the virtual arrays and fed to the entropy coder.
- * Returns TRUE if the iMCU row is completed, FALSE if suspended.
- *
- * NB: input_buf is ignored; it is likely to be a NULL pointer.
- */
-
-METHODDEF(boolean)
-compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- JDIMENSION MCU_col_num; /* index of current MCU within row */
- JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
- JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
- int blkn, ci, xindex, yindex, yoffset, blockcnt;
- JDIMENSION start_col;
- JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
- JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
- JBLOCKROW buffer_ptr;
- jpeg_component_info *compptr;
-
- /* Align the virtual buffers for the components used in this scan. */
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- buffer[ci] = (*cinfo->mem->access_virt_barray)
- ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
- coef->iMCU_row_num * compptr->v_samp_factor,
- (JDIMENSION) compptr->v_samp_factor, FALSE);
- }
-
- /* Loop to process one whole iMCU row */
- for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
- yoffset++) {
- for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
- MCU_col_num++) {
- /* Construct list of pointers to DCT blocks belonging to this MCU */
- blkn = 0; /* index of current DCT block within MCU */
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- start_col = MCU_col_num * compptr->MCU_width;
- blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
- : compptr->last_col_width;
- for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
- if (coef->iMCU_row_num < last_iMCU_row ||
- yindex+yoffset < compptr->last_row_height) {
- /* Fill in pointers to real blocks in this row */
- buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
- for (xindex = 0; xindex < blockcnt; xindex++)
- MCU_buffer[blkn++] = buffer_ptr++;
- } else {
- /* At bottom of image, need a whole row of dummy blocks */
- xindex = 0;
- }
- /* Fill in any dummy blocks needed in this row.
- * Dummy blocks are filled in the same way as in jccoefct.c:
- * all zeroes in the AC entries, DC entries equal to previous
- * block's DC value. The init routine has already zeroed the
- * AC entries, so we need only set the DC entries correctly.
- */
- for (; xindex < compptr->MCU_width; xindex++) {
- MCU_buffer[blkn] = coef->dummy_buffer[blkn];
- MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
- blkn++;
- }
- }
- }
- /* Try to write the MCU. */
- if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
- /* Suspension forced; update state counters and exit */
- coef->MCU_vert_offset = yoffset;
- coef->mcu_ctr = MCU_col_num;
- return FALSE;
- }
- }
- /* Completed an MCU row, but perhaps not an iMCU row */
- coef->mcu_ctr = 0;
- }
- /* Completed the iMCU row, advance counters for next one */
- coef->iMCU_row_num++;
- start_iMCU_row(cinfo);
- return TRUE;
-}
-
-
-/*
- * Initialize coefficient buffer controller.
- *
- * Each passed coefficient array must be the right size for that
- * coefficient: width_in_blocks wide and height_in_blocks high,
- * with unitheight at least v_samp_factor.
- */
-
-LOCAL(void)
-transencode_coef_controller (j_compress_ptr cinfo,
- jvirt_barray_ptr * coef_arrays)
-{
- my_coef_ptr coef;
- JBLOCKROW buffer;
- int i;
-
- coef = (my_coef_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_coef_controller));
- cinfo->coef = (struct jpeg_c_coef_controller *) coef;
- coef->pub.start_pass = start_pass_coef;
- coef->pub.compress_data = compress_output;
-
- /* Save pointer to virtual arrays */
- coef->whole_image = coef_arrays;
-
- /* Allocate and pre-zero space for dummy DCT blocks. */
- buffer = (JBLOCKROW)
- (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
- jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
- for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
- coef->dummy_buffer[i] = buffer + i;
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jctrans.c
+ *
+ * Copyright (C) 1995-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 library routines for transcoding compression,
+ * that is, writing raw DCT coefficient arrays to an output JPEG file.
+ * The routines in jcapimin.c will also be needed by a transcoder.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Forward declarations */
+LOCAL(void) transencode_master_selection
+ JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
+LOCAL(void) transencode_coef_controller
+ JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
+
+
+/*
+ * Compression initialization for writing raw-coefficient data.
+ * Before calling this, all parameters and a data destination must be set up.
+ * Call jpeg_finish_compress() to actually write the data.
+ *
+ * The number of passed virtual arrays must match cinfo->num_components.
+ * Note that the virtual arrays need not be filled or even realized at
+ * the time write_coefficients is called; indeed, if the virtual arrays
+ * were requested from this compression object's memory manager, they
+ * typically will be realized during this routine and filled afterwards.
+ */
+
+GLOBAL(void)
+jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
+{
+ if (cinfo->global_state != CSTATE_START)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ /* Mark all tables to be written */
+ jpeg_suppress_tables(cinfo, FALSE);
+ /* (Re)initialize error mgr and destination modules */
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+ (*cinfo->dest->init_destination) (cinfo);
+ /* Perform master selection of active modules */
+ transencode_master_selection(cinfo, coef_arrays);
+ /* Wait for jpeg_finish_compress() call */
+ cinfo->next_scanline = 0; /* so jpeg_write_marker works */
+ cinfo->global_state = CSTATE_WRCOEFS;
+}
+
+
+/*
+ * Initialize the compression object with default parameters,
+ * then copy from the source object all parameters needed for lossless
+ * transcoding. Parameters that can be varied without loss (such as
+ * scan script and Huffman optimization) are left in their default states.
+ */
+
+GLOBAL(void)
+jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
+ j_compress_ptr dstinfo)
+{
+ JQUANT_TBL ** qtblptr;
+ jpeg_component_info *incomp, *outcomp;
+ JQUANT_TBL *c_quant, *slot_quant;
+ int tblno, ci, coefi;
+
+ /* Safety check to ensure start_compress not called yet. */
+ if (dstinfo->global_state != CSTATE_START)
+ ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
+ /* Copy fundamental image dimensions */
+ dstinfo->image_width = srcinfo->image_width;
+ dstinfo->image_height = srcinfo->image_height;
+ dstinfo->input_components = srcinfo->num_components;
+ dstinfo->in_color_space = srcinfo->jpeg_color_space;
+ /* Initialize all parameters to default values */
+ jpeg_set_defaults(dstinfo);
+ /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
+ * Fix it to get the right header markers for the image colorspace.
+ */
+ jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
+ dstinfo->data_precision = srcinfo->data_precision;
+ dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
+ /* Copy the source's quantization tables. */
+ for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
+ if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
+ qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
+ if (*qtblptr == NULL)
+ *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
+ MEMCOPY((*qtblptr)->quantval,
+ srcinfo->quant_tbl_ptrs[tblno]->quantval,
+ SIZEOF((*qtblptr)->quantval));
+ (*qtblptr)->sent_table = FALSE;
+ }
+ }
+ /* Copy the source's per-component info.
+ * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
+ */
+ dstinfo->num_components = srcinfo->num_components;
+ if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
+ ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
+ MAX_COMPONENTS);
+ for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
+ ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
+ outcomp->component_id = incomp->component_id;
+ outcomp->h_samp_factor = incomp->h_samp_factor;
+ outcomp->v_samp_factor = incomp->v_samp_factor;
+ outcomp->quant_tbl_no = incomp->quant_tbl_no;
+ /* Make sure saved quantization table for component matches the qtable
+ * slot. If not, the input file re-used this qtable slot.
+ * IJG encoder currently cannot duplicate this.
+ */
+ tblno = outcomp->quant_tbl_no;
+ if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
+ srcinfo->quant_tbl_ptrs[tblno] == NULL)
+ ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
+ slot_quant = srcinfo->quant_tbl_ptrs[tblno];
+ c_quant = incomp->quant_table;
+ if (c_quant != NULL) {
+ for (coefi = 0; coefi < DCTSIZE2; coefi++) {
+ if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
+ ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
+ }
+ }
+ /* Note: we do not copy the source's Huffman table assignments;
+ * instead we rely on jpeg_set_colorspace to have made a suitable choice.
+ */
+ }
+ /* Also copy JFIF version and resolution information, if available.
+ * Strictly speaking this isn't "critical" info, but it's nearly
+ * always appropriate to copy it if available. In particular,
+ * if the application chooses to copy JFIF 1.02 extension markers from
+ * the source file, we need to copy the version to make sure we don't
+ * emit a file that has 1.02 extensions but a claimed version of 1.01.
+ * We will *not*, however, copy version info from mislabeled "2.01" files.
+ */
+ if (srcinfo->saw_JFIF_marker) {
+ if (srcinfo->JFIF_major_version == 1) {
+ dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
+ dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
+ }
+ dstinfo->density_unit = srcinfo->density_unit;
+ dstinfo->X_density = srcinfo->X_density;
+ dstinfo->Y_density = srcinfo->Y_density;
+ }
+}
+
+
+/*
+ * Master selection of compression modules for transcoding.
+ * This substitutes for jcinit.c's initialization of the full compressor.
+ */
+
+LOCAL(void)
+transencode_master_selection (j_compress_ptr cinfo,
+ jvirt_barray_ptr * coef_arrays)
+{
+ /* Although we don't actually use input_components for transcoding,
+ * jcmaster.c's initial_setup will complain if input_components is 0.
+ */
+ cinfo->input_components = 1;
+ /* Initialize master control (includes parameter checking/processing) */
+ jinit_c_master_control(cinfo, TRUE /* transcode only */);
+
+ /* Entropy encoding: either Huffman or arithmetic coding. */
+ if (cinfo->arith_code) {
+ ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+ } else {
+ if (cinfo->progressive_mode) {
+#ifdef C_PROGRESSIVE_SUPPORTED
+ jinit_phuff_encoder(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else
+ jinit_huff_encoder(cinfo);
+ }
+
+ /* We need a special coefficient buffer controller. */
+ transencode_coef_controller(cinfo, coef_arrays);
+
+ jinit_marker_writer(cinfo);
+
+ /* We can now tell the memory manager to allocate virtual arrays. */
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+ /* Write the datastream header (SOI, JFIF) immediately.
+ * Frame and scan headers are postponed till later.
+ * This lets application insert special markers after the SOI.
+ */
+ (*cinfo->marker->write_file_header) (cinfo);
+}
+
+
+/*
+ * The rest of this file is a special implementation of the coefficient
+ * buffer controller. This is similar to jccoefct.c, but it handles only
+ * output from presupplied virtual arrays. Furthermore, we generate any
+ * dummy padding blocks on-the-fly rather than expecting them to be present
+ * in the arrays.
+ */
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_c_coef_controller pub; /* public fields */
+
+ JDIMENSION iMCU_row_num; /* iMCU row # within image */
+ JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
+ int MCU_vert_offset; /* counts MCU rows within iMCU row */
+ int MCU_rows_per_iMCU_row; /* number of such rows needed */
+
+ /* Virtual block array for each component. */
+ jvirt_barray_ptr * whole_image;
+
+ /* Workspace for constructing dummy blocks at right/bottom edges. */
+ JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
+} my_coef_controller;
+
+typedef my_coef_controller * my_coef_ptr;
+
+
+LOCAL(void)
+start_iMCU_row (j_compress_ptr cinfo)
+/* Reset within-iMCU-row counters for a new row */
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ /* In an interleaved scan, an MCU row is the same as an iMCU row.
+ * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
+ * But at the bottom of the image, process only what's left.
+ */
+ if (cinfo->comps_in_scan > 1) {
+ coef->MCU_rows_per_iMCU_row = 1;
+ } else {
+ if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
+ else
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
+ }
+
+ coef->mcu_ctr = 0;
+ coef->MCU_vert_offset = 0;
+}
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ if (pass_mode != JBUF_CRANK_DEST)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+ coef->iMCU_row_num = 0;
+ start_iMCU_row(cinfo);
+}
+
+
+/*
+ * Process some data.
+ * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
+ * per call, ie, v_samp_factor block rows for each component in the scan.
+ * The data is obtained from the virtual arrays and fed to the entropy coder.
+ * Returns TRUE if the iMCU row is completed, FALSE if suspended.
+ *
+ * NB: input_buf is ignored; it is likely to be a NULL pointer.
+ */
+
+METHODDEF(boolean)
+compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ int blkn, ci, xindex, yindex, yoffset, blockcnt;
+ JDIMENSION start_col;
+ JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
+ JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
+ JBLOCKROW buffer_ptr;
+ jpeg_component_info *compptr;
+
+ /* Align the virtual buffers for the components used in this scan. */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ buffer[ci] = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
+ coef->iMCU_row_num * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ }
+
+ /* Loop to process one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
+ MCU_col_num++) {
+ /* Construct list of pointers to DCT blocks belonging to this MCU */
+ blkn = 0; /* index of current DCT block within MCU */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ start_col = MCU_col_num * compptr->MCU_width;
+ blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+ : compptr->last_col_width;
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ if (coef->iMCU_row_num < last_iMCU_row ||
+ yindex+yoffset < compptr->last_row_height) {
+ /* Fill in pointers to real blocks in this row */
+ buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+ for (xindex = 0; xindex < blockcnt; xindex++)
+ MCU_buffer[blkn++] = buffer_ptr++;
+ } else {
+ /* At bottom of image, need a whole row of dummy blocks */
+ xindex = 0;
+ }
+ /* Fill in any dummy blocks needed in this row.
+ * Dummy blocks are filled in the same way as in jccoefct.c:
+ * all zeroes in the AC entries, DC entries equal to previous
+ * block's DC value. The init routine has already zeroed the
+ * AC entries, so we need only set the DC entries correctly.
+ */
+ for (; xindex < compptr->MCU_width; xindex++) {
+ MCU_buffer[blkn] = coef->dummy_buffer[blkn];
+ MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
+ blkn++;
+ }
+ }
+ }
+ /* Try to write the MCU. */
+ if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->mcu_ctr = MCU_col_num;
+ return FALSE;
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->mcu_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ coef->iMCU_row_num++;
+ start_iMCU_row(cinfo);
+ return TRUE;
+}
+
+
+/*
+ * Initialize coefficient buffer controller.
+ *
+ * Each passed coefficient array must be the right size for that
+ * coefficient: width_in_blocks wide and height_in_blocks high,
+ * with unitheight at least v_samp_factor.
+ */
+
+LOCAL(void)
+transencode_coef_controller (j_compress_ptr cinfo,
+ jvirt_barray_ptr * coef_arrays)
+{
+ my_coef_ptr coef;
+ JBLOCKROW buffer;
+ int i;
+
+ coef = (my_coef_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_coef_controller));
+ cinfo->coef = (struct jpeg_c_coef_controller *) coef;
+ coef->pub.start_pass = start_pass_coef;
+ coef->pub.compress_data = compress_output;
+
+ /* Save pointer to virtual arrays */
+ coef->whole_image = coef_arrays;
+
+ /* Allocate and pre-zero space for dummy DCT blocks. */
+ buffer = (JBLOCKROW)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+ jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+ for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
+ coef->dummy_buffer[i] = buffer + i;
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c b/core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c
index 1b24b707b2..80c52cd29f 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdapimin.c
@@ -1,398 +1,398 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdapimin.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 application interface code for the decompression half
- * of the JPEG library. These are the "minimum" API routines that may be
- * needed in either the normal full-decompression case or the
- * transcoding-only case.
- *
- * Most of the routines intended to be called directly by an application
- * are in this file or in jdapistd.c. But also see jcomapi.c for routines
- * shared by compression and decompression, and jdtrans.c for the transcoding
- * case.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/*
- * Initialization of a JPEG decompression object.
- * The error manager must already be set up (in case memory manager fails).
- */
-
-GLOBAL(void)
-jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
-{
- int i;
-
- /* Guard against version mismatches between library and caller. */
- cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
- if (version != JPEG_LIB_VERSION)
- ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
- if (structsize != SIZEOF(struct jpeg_decompress_struct))
- ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
- (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
-
- /* For debugging purposes, we zero the whole master structure.
- * But the application has already set the err pointer, and may have set
- * client_data, so we have to save and restore those fields.
- * Note: if application hasn't set client_data, tools like Purify may
- * complain here.
- */
- {
- struct jpeg_error_mgr * err = cinfo->err;
- void * client_data = cinfo->client_data; /* ignore Purify complaint here */
- MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
- cinfo->err = err;
- cinfo->client_data = client_data;
- }
- cinfo->is_decompressor = TRUE;
-
- /* Initialize a memory manager instance for this object */
- jinit_memory_mgr((j_common_ptr) cinfo);
-
- /* Zero out pointers to permanent structures. */
- cinfo->progress = NULL;
- cinfo->src = NULL;
-
- for (i = 0; i < NUM_QUANT_TBLS; i++)
- cinfo->quant_tbl_ptrs[i] = NULL;
-
- for (i = 0; i < NUM_HUFF_TBLS; i++) {
- cinfo->dc_huff_tbl_ptrs[i] = NULL;
- cinfo->ac_huff_tbl_ptrs[i] = NULL;
- }
-
- /* Initialize marker processor so application can override methods
- * for COM, APPn markers before calling jpeg_read_header.
- */
- cinfo->marker_list = NULL;
- jinit_marker_reader(cinfo);
-
- /* And initialize the overall input controller. */
- jinit_input_controller(cinfo);
-
- /* OK, I'm ready */
- cinfo->global_state = DSTATE_START;
-}
-
-
-/*
- * Destruction of a JPEG decompression object
- */
-
-GLOBAL(void)
-jpeg_destroy_decompress (j_decompress_ptr cinfo)
-{
- jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
-}
-
-
-/*
- * Abort processing of a JPEG decompression operation,
- * but don't destroy the object itself.
- */
-
-GLOBAL(void)
-jpeg_abort_decompress (j_decompress_ptr cinfo)
-{
- jpeg_abort((j_common_ptr) cinfo); /* use common routine */
-}
-
-
-/*
- * Set default decompression parameters.
- */
-
-LOCAL(void)
-default_decompress_parms (j_decompress_ptr cinfo)
-{
- /* Guess the input colorspace, and set output colorspace accordingly. */
- /* (Wish JPEG committee had provided a real way to specify this...) */
- /* Note application may override our guesses. */
- switch (cinfo->num_components) {
- case 1:
- cinfo->jpeg_color_space = JCS_GRAYSCALE;
- cinfo->out_color_space = JCS_GRAYSCALE;
- break;
-
- case 3:
- if (cinfo->saw_JFIF_marker) {
- cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
- } else if (cinfo->saw_Adobe_marker) {
- switch (cinfo->Adobe_transform) {
- case 0:
- cinfo->jpeg_color_space = JCS_RGB;
- break;
- case 1:
- cinfo->jpeg_color_space = JCS_YCbCr;
- break;
- default:
- WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
- cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
- break;
- }
- } else {
- /* Saw no special markers, try to guess from the component IDs */
- int cid0 = cinfo->comp_info[0].component_id;
- int cid1 = cinfo->comp_info[1].component_id;
- int cid2 = cinfo->comp_info[2].component_id;
-
- if (cid0 == 1 && cid1 == 2 && cid2 == 3)
- cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
- else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
- cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
- else {
- TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
- cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
- }
- }
- /* Always guess RGB is proper output colorspace. */
- cinfo->out_color_space = JCS_RGB;
- break;
-
- case 4:
- if (cinfo->saw_Adobe_marker) {
- switch (cinfo->Adobe_transform) {
- case 0:
- cinfo->jpeg_color_space = JCS_CMYK;
- break;
- case 2:
- cinfo->jpeg_color_space = JCS_YCCK;
- break;
- default:
- WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
- cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
- break;
- }
- } else {
- /* No special markers, assume straight CMYK. */
- cinfo->jpeg_color_space = JCS_CMYK;
- }
- cinfo->out_color_space = JCS_CMYK;
- break;
-
- default:
- cinfo->jpeg_color_space = JCS_UNKNOWN;
- cinfo->out_color_space = JCS_UNKNOWN;
- break;
- }
-
- /* Set defaults for other decompression parameters. */
- cinfo->scale_num = 1; /* 1:1 scaling */
- cinfo->scale_denom = 1;
- cinfo->output_gamma = 1.0;
- cinfo->buffered_image = FALSE;
- cinfo->raw_data_out = FALSE;
- cinfo->dct_method = JDCT_DEFAULT;
- cinfo->do_fancy_upsampling = TRUE;
- cinfo->do_block_smoothing = TRUE;
- cinfo->quantize_colors = FALSE;
- /* We set these in case application only sets quantize_colors. */
- cinfo->dither_mode = JDITHER_FS;
-#ifdef QUANT_2PASS_SUPPORTED
- cinfo->two_pass_quantize = TRUE;
-#else
- cinfo->two_pass_quantize = FALSE;
-#endif
- cinfo->desired_number_of_colors = 256;
- cinfo->colormap = NULL;
- /* Initialize for no mode change in buffered-image mode. */
- cinfo->enable_1pass_quant = FALSE;
- cinfo->enable_external_quant = FALSE;
- cinfo->enable_2pass_quant = FALSE;
-}
-
-
-/*
- * Decompression startup: read start of JPEG datastream to see what's there.
- * Need only initialize JPEG object and supply a data source before calling.
- *
- * This routine will read as far as the first SOS marker (ie, actual start of
- * compressed data), and will save all tables and parameters in the JPEG
- * object. It will also initialize the decompression parameters to default
- * values, and finally return JPEG_HEADER_OK. On return, the application may
- * adjust the decompression parameters and then call jpeg_start_decompress.
- * (Or, if the application only wanted to determine the image parameters,
- * the data need not be decompressed. In that case, call jpeg_abort or
- * jpeg_destroy to release any temporary space.)
- * If an abbreviated (tables only) datastream is presented, the routine will
- * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
- * re-use the JPEG object to read the abbreviated image datastream(s).
- * It is unnecessary (but OK) to call jpeg_abort in this case.
- * The JPEG_SUSPENDED return code only occurs if the data source module
- * requests suspension of the decompressor. In this case the application
- * should load more source data and then re-call jpeg_read_header to resume
- * processing.
- * If a non-suspending data source is used and require_image is TRUE, then the
- * return code need not be inspected since only JPEG_HEADER_OK is possible.
- *
- * This routine is now just a front end to jpeg_consume_input, with some
- * extra error checking.
- */
-
-GLOBAL(int)
-jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
-{
- int retcode;
-
- if (cinfo->global_state != DSTATE_START &&
- cinfo->global_state != DSTATE_INHEADER)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- retcode = jpeg_consume_input(cinfo);
-
- switch (retcode) {
- case JPEG_REACHED_SOS:
- retcode = JPEG_HEADER_OK;
- break;
- case JPEG_REACHED_EOI:
- if (require_image) /* Complain if application wanted an image */
- ERREXIT(cinfo, JERR_NO_IMAGE);
- /* Reset to start state; it would be safer to require the application to
- * call jpeg_abort, but we can't change it now for compatibility reasons.
- * A side effect is to free any temporary memory (there shouldn't be any).
- */
- jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
- retcode = JPEG_HEADER_TABLES_ONLY;
- break;
- case JPEG_SUSPENDED:
- /* no work */
- break;
- }
-
- return retcode;
-}
-
-
-/*
- * Consume data in advance of what the decompressor requires.
- * This can be called at any time once the decompressor object has
- * been created and a data source has been set up.
- *
- * This routine is essentially a state machine that handles a couple
- * of critical state-transition actions, namely initial setup and
- * transition from header scanning to ready-for-start_decompress.
- * All the actual input is done via the input controller's consume_input
- * method.
- */
-
-GLOBAL(int)
-jpeg_consume_input (j_decompress_ptr cinfo)
-{
- int retcode = JPEG_SUSPENDED;
-
- /* NB: every possible DSTATE value should be listed in this switch */
- switch (cinfo->global_state) {
- case DSTATE_START:
- /* Start-of-datastream actions: reset appropriate modules */
- (*cinfo->inputctl->reset_input_controller) (cinfo);
- /* Initialize application's data source module */
- (*cinfo->src->init_source) (cinfo);
- cinfo->global_state = DSTATE_INHEADER;
- /*FALLTHROUGH*/
- case DSTATE_INHEADER:
- retcode = (*cinfo->inputctl->consume_input) (cinfo);
- if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
- /* Set up default parameters based on header data */
- default_decompress_parms(cinfo);
- /* Set global state: ready for start_decompress */
- cinfo->global_state = DSTATE_READY;
- }
- break;
- case DSTATE_READY:
- /* Can't advance past first SOS until start_decompress is called */
- retcode = JPEG_REACHED_SOS;
- break;
- case DSTATE_PRELOAD:
- case DSTATE_PRESCAN:
- case DSTATE_SCANNING:
- case DSTATE_RAW_OK:
- case DSTATE_BUFIMAGE:
- case DSTATE_BUFPOST:
- case DSTATE_STOPPING:
- retcode = (*cinfo->inputctl->consume_input) (cinfo);
- break;
- default:
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- }
- return retcode;
-}
-
-
-/*
- * Have we finished reading the input file?
- */
-
-GLOBAL(boolean)
-jpeg_input_complete (j_decompress_ptr cinfo)
-{
- /* Check for valid jpeg object */
- if (cinfo->global_state < DSTATE_START ||
- cinfo->global_state > DSTATE_STOPPING)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- return cinfo->inputctl->eoi_reached;
-}
-
-
-/*
- * Is there more than one scan?
- */
-
-GLOBAL(boolean)
-jpeg_has_multiple_scans (j_decompress_ptr cinfo)
-{
- /* Only valid after jpeg_read_header completes */
- if (cinfo->global_state < DSTATE_READY ||
- cinfo->global_state > DSTATE_STOPPING)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- return cinfo->inputctl->has_multiple_scans;
-}
-
-
-/*
- * Finish JPEG decompression.
- *
- * This will normally just verify the file trailer and release temp storage.
- *
- * Returns FALSE if suspended. The return value need be inspected only if
- * a suspending data source is used.
- */
-
-GLOBAL(boolean)
-jpeg_finish_decompress (j_decompress_ptr cinfo)
-{
- if ((cinfo->global_state == DSTATE_SCANNING ||
- cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
- /* Terminate final pass of non-buffered mode */
- if (cinfo->output_scanline < cinfo->output_height)
- ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
- (*cinfo->master->finish_output_pass) (cinfo);
- cinfo->global_state = DSTATE_STOPPING;
- } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
- /* Finishing after a buffered-image operation */
- cinfo->global_state = DSTATE_STOPPING;
- } else if (cinfo->global_state != DSTATE_STOPPING) {
- /* STOPPING = repeat call after a suspension, anything else is error */
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- }
- /* Read until EOI */
- while (! cinfo->inputctl->eoi_reached) {
- if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
- return FALSE; /* Suspend, come back later */
- }
- /* Do final cleanup */
- (*cinfo->src->term_source) (cinfo);
- /* We can use jpeg_abort to release memory and reset global_state */
- jpeg_abort((j_common_ptr) cinfo);
- return TRUE;
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdapimin.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 application interface code for the decompression half
+ * of the JPEG library. These are the "minimum" API routines that may be
+ * needed in either the normal full-decompression case or the
+ * transcoding-only case.
+ *
+ * Most of the routines intended to be called directly by an application
+ * are in this file or in jdapistd.c. But also see jcomapi.c for routines
+ * shared by compression and decompression, and jdtrans.c for the transcoding
+ * case.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * Initialization of a JPEG decompression object.
+ * The error manager must already be set up (in case memory manager fails).
+ */
+
+GLOBAL(void)
+jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
+{
+ int i;
+
+ /* Guard against version mismatches between library and caller. */
+ cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
+ if (version != JPEG_LIB_VERSION)
+ ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
+ if (structsize != SIZEOF(struct jpeg_decompress_struct))
+ ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
+ (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
+
+ /* For debugging purposes, we zero the whole master structure.
+ * But the application has already set the err pointer, and may have set
+ * client_data, so we have to save and restore those fields.
+ * Note: if application hasn't set client_data, tools like Purify may
+ * complain here.
+ */
+ {
+ struct jpeg_error_mgr * err = cinfo->err;
+ void * client_data = cinfo->client_data; /* ignore Purify complaint here */
+ MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
+ cinfo->err = err;
+ cinfo->client_data = client_data;
+ }
+ cinfo->is_decompressor = TRUE;
+
+ /* Initialize a memory manager instance for this object */
+ jinit_memory_mgr((j_common_ptr) cinfo);
+
+ /* Zero out pointers to permanent structures. */
+ cinfo->progress = NULL;
+ cinfo->src = NULL;
+
+ for (i = 0; i < NUM_QUANT_TBLS; i++)
+ cinfo->quant_tbl_ptrs[i] = NULL;
+
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ cinfo->dc_huff_tbl_ptrs[i] = NULL;
+ cinfo->ac_huff_tbl_ptrs[i] = NULL;
+ }
+
+ /* Initialize marker processor so application can override methods
+ * for COM, APPn markers before calling jpeg_read_header.
+ */
+ cinfo->marker_list = NULL;
+ jinit_marker_reader(cinfo);
+
+ /* And initialize the overall input controller. */
+ jinit_input_controller(cinfo);
+
+ /* OK, I'm ready */
+ cinfo->global_state = DSTATE_START;
+}
+
+
+/*
+ * Destruction of a JPEG decompression object
+ */
+
+GLOBAL(void)
+jpeg_destroy_decompress (j_decompress_ptr cinfo)
+{
+ jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Abort processing of a JPEG decompression operation,
+ * but don't destroy the object itself.
+ */
+
+GLOBAL(void)
+jpeg_abort_decompress (j_decompress_ptr cinfo)
+{
+ jpeg_abort((j_common_ptr) cinfo); /* use common routine */
+}
+
+
+/*
+ * Set default decompression parameters.
+ */
+
+LOCAL(void)
+default_decompress_parms (j_decompress_ptr cinfo)
+{
+ /* Guess the input colorspace, and set output colorspace accordingly. */
+ /* (Wish JPEG committee had provided a real way to specify this...) */
+ /* Note application may override our guesses. */
+ switch (cinfo->num_components) {
+ case 1:
+ cinfo->jpeg_color_space = JCS_GRAYSCALE;
+ cinfo->out_color_space = JCS_GRAYSCALE;
+ break;
+
+ case 3:
+ if (cinfo->saw_JFIF_marker) {
+ cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
+ } else if (cinfo->saw_Adobe_marker) {
+ switch (cinfo->Adobe_transform) {
+ case 0:
+ cinfo->jpeg_color_space = JCS_RGB;
+ break;
+ case 1:
+ cinfo->jpeg_color_space = JCS_YCbCr;
+ break;
+ default:
+ WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
+ cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
+ break;
+ }
+ } else {
+ /* Saw no special markers, try to guess from the component IDs */
+ int cid0 = cinfo->comp_info[0].component_id;
+ int cid1 = cinfo->comp_info[1].component_id;
+ int cid2 = cinfo->comp_info[2].component_id;
+
+ if (cid0 == 1 && cid1 == 2 && cid2 == 3)
+ cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
+ else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
+ cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
+ else {
+ TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
+ cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
+ }
+ }
+ /* Always guess RGB is proper output colorspace. */
+ cinfo->out_color_space = JCS_RGB;
+ break;
+
+ case 4:
+ if (cinfo->saw_Adobe_marker) {
+ switch (cinfo->Adobe_transform) {
+ case 0:
+ cinfo->jpeg_color_space = JCS_CMYK;
+ break;
+ case 2:
+ cinfo->jpeg_color_space = JCS_YCCK;
+ break;
+ default:
+ WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
+ cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
+ break;
+ }
+ } else {
+ /* No special markers, assume straight CMYK. */
+ cinfo->jpeg_color_space = JCS_CMYK;
+ }
+ cinfo->out_color_space = JCS_CMYK;
+ break;
+
+ default:
+ cinfo->jpeg_color_space = JCS_UNKNOWN;
+ cinfo->out_color_space = JCS_UNKNOWN;
+ break;
+ }
+
+ /* Set defaults for other decompression parameters. */
+ cinfo->scale_num = 1; /* 1:1 scaling */
+ cinfo->scale_denom = 1;
+ cinfo->output_gamma = 1.0;
+ cinfo->buffered_image = FALSE;
+ cinfo->raw_data_out = FALSE;
+ cinfo->dct_method = JDCT_DEFAULT;
+ cinfo->do_fancy_upsampling = TRUE;
+ cinfo->do_block_smoothing = TRUE;
+ cinfo->quantize_colors = FALSE;
+ /* We set these in case application only sets quantize_colors. */
+ cinfo->dither_mode = JDITHER_FS;
+#ifdef QUANT_2PASS_SUPPORTED
+ cinfo->two_pass_quantize = TRUE;
+#else
+ cinfo->two_pass_quantize = FALSE;
+#endif
+ cinfo->desired_number_of_colors = 256;
+ cinfo->colormap = NULL;
+ /* Initialize for no mode change in buffered-image mode. */
+ cinfo->enable_1pass_quant = FALSE;
+ cinfo->enable_external_quant = FALSE;
+ cinfo->enable_2pass_quant = FALSE;
+}
+
+
+/*
+ * Decompression startup: read start of JPEG datastream to see what's there.
+ * Need only initialize JPEG object and supply a data source before calling.
+ *
+ * This routine will read as far as the first SOS marker (ie, actual start of
+ * compressed data), and will save all tables and parameters in the JPEG
+ * object. It will also initialize the decompression parameters to default
+ * values, and finally return JPEG_HEADER_OK. On return, the application may
+ * adjust the decompression parameters and then call jpeg_start_decompress.
+ * (Or, if the application only wanted to determine the image parameters,
+ * the data need not be decompressed. In that case, call jpeg_abort or
+ * jpeg_destroy to release any temporary space.)
+ * If an abbreviated (tables only) datastream is presented, the routine will
+ * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
+ * re-use the JPEG object to read the abbreviated image datastream(s).
+ * It is unnecessary (but OK) to call jpeg_abort in this case.
+ * The JPEG_SUSPENDED return code only occurs if the data source module
+ * requests suspension of the decompressor. In this case the application
+ * should load more source data and then re-call jpeg_read_header to resume
+ * processing.
+ * If a non-suspending data source is used and require_image is TRUE, then the
+ * return code need not be inspected since only JPEG_HEADER_OK is possible.
+ *
+ * This routine is now just a front end to jpeg_consume_input, with some
+ * extra error checking.
+ */
+
+GLOBAL(int)
+jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
+{
+ int retcode;
+
+ if (cinfo->global_state != DSTATE_START &&
+ cinfo->global_state != DSTATE_INHEADER)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ retcode = jpeg_consume_input(cinfo);
+
+ switch (retcode) {
+ case JPEG_REACHED_SOS:
+ retcode = JPEG_HEADER_OK;
+ break;
+ case JPEG_REACHED_EOI:
+ if (require_image) /* Complain if application wanted an image */
+ ERREXIT(cinfo, JERR_NO_IMAGE);
+ /* Reset to start state; it would be safer to require the application to
+ * call jpeg_abort, but we can't change it now for compatibility reasons.
+ * A side effect is to free any temporary memory (there shouldn't be any).
+ */
+ jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
+ retcode = JPEG_HEADER_TABLES_ONLY;
+ break;
+ case JPEG_SUSPENDED:
+ /* no work */
+ break;
+ }
+
+ return retcode;
+}
+
+
+/*
+ * Consume data in advance of what the decompressor requires.
+ * This can be called at any time once the decompressor object has
+ * been created and a data source has been set up.
+ *
+ * This routine is essentially a state machine that handles a couple
+ * of critical state-transition actions, namely initial setup and
+ * transition from header scanning to ready-for-start_decompress.
+ * All the actual input is done via the input controller's consume_input
+ * method.
+ */
+
+GLOBAL(int)
+jpeg_consume_input (j_decompress_ptr cinfo)
+{
+ int retcode = JPEG_SUSPENDED;
+
+ /* NB: every possible DSTATE value should be listed in this switch */
+ switch (cinfo->global_state) {
+ case DSTATE_START:
+ /* Start-of-datastream actions: reset appropriate modules */
+ (*cinfo->inputctl->reset_input_controller) (cinfo);
+ /* Initialize application's data source module */
+ (*cinfo->src->init_source) (cinfo);
+ cinfo->global_state = DSTATE_INHEADER;
+ /*FALLTHROUGH*/
+ case DSTATE_INHEADER:
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
+ if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
+ /* Set up default parameters based on header data */
+ default_decompress_parms(cinfo);
+ /* Set global state: ready for start_decompress */
+ cinfo->global_state = DSTATE_READY;
+ }
+ break;
+ case DSTATE_READY:
+ /* Can't advance past first SOS until start_decompress is called */
+ retcode = JPEG_REACHED_SOS;
+ break;
+ case DSTATE_PRELOAD:
+ case DSTATE_PRESCAN:
+ case DSTATE_SCANNING:
+ case DSTATE_RAW_OK:
+ case DSTATE_BUFIMAGE:
+ case DSTATE_BUFPOST:
+ case DSTATE_STOPPING:
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
+ break;
+ default:
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ }
+ return retcode;
+}
+
+
+/*
+ * Have we finished reading the input file?
+ */
+
+GLOBAL(boolean)
+jpeg_input_complete (j_decompress_ptr cinfo)
+{
+ /* Check for valid jpeg object */
+ if (cinfo->global_state < DSTATE_START ||
+ cinfo->global_state > DSTATE_STOPPING)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ return cinfo->inputctl->eoi_reached;
+}
+
+
+/*
+ * Is there more than one scan?
+ */
+
+GLOBAL(boolean)
+jpeg_has_multiple_scans (j_decompress_ptr cinfo)
+{
+ /* Only valid after jpeg_read_header completes */
+ if (cinfo->global_state < DSTATE_READY ||
+ cinfo->global_state > DSTATE_STOPPING)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ return cinfo->inputctl->has_multiple_scans;
+}
+
+
+/*
+ * Finish JPEG decompression.
+ *
+ * This will normally just verify the file trailer and release temp storage.
+ *
+ * Returns FALSE if suspended. The return value need be inspected only if
+ * a suspending data source is used.
+ */
+
+GLOBAL(boolean)
+jpeg_finish_decompress (j_decompress_ptr cinfo)
+{
+ if ((cinfo->global_state == DSTATE_SCANNING ||
+ cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
+ /* Terminate final pass of non-buffered mode */
+ if (cinfo->output_scanline < cinfo->output_height)
+ ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
+ (*cinfo->master->finish_output_pass) (cinfo);
+ cinfo->global_state = DSTATE_STOPPING;
+ } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
+ /* Finishing after a buffered-image operation */
+ cinfo->global_state = DSTATE_STOPPING;
+ } else if (cinfo->global_state != DSTATE_STOPPING) {
+ /* STOPPING = repeat call after a suspension, anything else is error */
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ }
+ /* Read until EOI */
+ while (! cinfo->inputctl->eoi_reached) {
+ if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
+ return FALSE; /* Suspend, come back later */
+ }
+ /* Do final cleanup */
+ (*cinfo->src->term_source) (cinfo);
+ /* We can use jpeg_abort to release memory and reset global_state */
+ jpeg_abort((j_common_ptr) cinfo);
+ return TRUE;
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c b/core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c
index 8c969eb309..4c31f7640c 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdapistd.c
@@ -1,279 +1,279 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdapistd.c
- *
- * Copyright (C) 1994-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 contains application interface code for the decompression half
- * of the JPEG library. These are the "standard" API routines that are
- * used in the normal full-decompression case. They are not used by a
- * transcoding-only application. Note that if an application links in
- * jpeg_start_decompress, it will end up linking in the entire decompressor.
- * We thus must separate this file from jdapimin.c to avoid linking the
- * whole decompression library into a transcoder.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Forward declarations */
-LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
-
-
-/*
- * Decompression initialization.
- * jpeg_read_header must be completed before calling this.
- *
- * If a multipass operating mode was selected, this will do all but the
- * last pass, and thus may take a great deal of time.
- *
- * Returns FALSE if suspended. The return value need be inspected only if
- * a suspending data source is used.
- */
-
-GLOBAL(boolean)
-jpeg_start_decompress (j_decompress_ptr cinfo)
-{
- if (cinfo->global_state == DSTATE_READY) {
- /* First call: initialize master control, select active modules */
- jinit_master_decompress(cinfo);
- if (cinfo->buffered_image) {
- /* No more work here; expecting jpeg_start_output next */
- cinfo->global_state = DSTATE_BUFIMAGE;
- return TRUE;
- }
- cinfo->global_state = DSTATE_PRELOAD;
- }
- if (cinfo->global_state == DSTATE_PRELOAD) {
- /* If file has multiple scans, absorb them all into the coef buffer */
- if (cinfo->inputctl->has_multiple_scans) {
-#ifdef D_MULTISCAN_FILES_SUPPORTED
- for (;;) {
- int retcode;
- /* Call progress monitor hook if present */
- if (cinfo->progress != NULL)
- (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
- /* Absorb some more input */
- retcode = (*cinfo->inputctl->consume_input) (cinfo);
- if (retcode == JPEG_SUSPENDED)
- return FALSE;
- if (retcode == JPEG_REACHED_EOI)
- break;
- /* Advance progress counter if appropriate */
- if (cinfo->progress != NULL &&
- (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
- if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
- /* jdmaster underestimated number of scans; ratchet up one scan */
- cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
- }
- }
- }
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif /* D_MULTISCAN_FILES_SUPPORTED */
- }
- cinfo->output_scan_number = cinfo->input_scan_number;
- } else if (cinfo->global_state != DSTATE_PRESCAN)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- /* Perform any dummy output passes, and set up for the final pass */
- return output_pass_setup(cinfo);
-}
-
-
-/*
- * Set up for an output pass, and perform any dummy pass(es) needed.
- * Common subroutine for jpeg_start_decompress and jpeg_start_output.
- * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
- * Exit: If done, returns TRUE and sets global_state for proper output mode.
- * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
- */
-
-LOCAL(boolean)
-output_pass_setup (j_decompress_ptr cinfo)
-{
- if (cinfo->global_state != DSTATE_PRESCAN) {
- /* First call: do pass setup */
- (*cinfo->master->prepare_for_output_pass) (cinfo);
- cinfo->output_scanline = 0;
- cinfo->global_state = DSTATE_PRESCAN;
- }
- /* Loop over any required dummy passes */
- while (cinfo->master->is_dummy_pass) {
-#ifdef QUANT_2PASS_SUPPORTED
- /* Crank through the dummy pass */
- while (cinfo->output_scanline < cinfo->output_height) {
- JDIMENSION last_scanline;
- /* Call progress monitor hook if present */
- if (cinfo->progress != NULL) {
- cinfo->progress->pass_counter = (long) cinfo->output_scanline;
- cinfo->progress->pass_limit = (long) cinfo->output_height;
- (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
- }
- /* Process some data */
- last_scanline = cinfo->output_scanline;
- (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
- &cinfo->output_scanline, (JDIMENSION) 0);
- if (cinfo->output_scanline == last_scanline)
- return FALSE; /* No progress made, must suspend */
- }
- /* Finish up dummy pass, and set up for another one */
- (*cinfo->master->finish_output_pass) (cinfo);
- (*cinfo->master->prepare_for_output_pass) (cinfo);
- cinfo->output_scanline = 0;
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif /* QUANT_2PASS_SUPPORTED */
- }
- /* Ready for application to drive output pass through
- * jpeg_read_scanlines or jpeg_read_raw_data.
- */
- cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
- return TRUE;
-}
-
-
-/*
- * Read some scanlines of data from the JPEG decompressor.
- *
- * The return value will be the number of lines actually read.
- * This may be less than the number requested in several cases,
- * including bottom of image, data source suspension, and operating
- * modes that emit multiple scanlines at a time.
- *
- * Note: we warn about excess calls to jpeg_read_scanlines() since
- * this likely signals an application programmer error. However,
- * an oversize buffer (max_lines > scanlines remaining) is not an error.
- */
-
-GLOBAL(JDIMENSION)
-jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
- JDIMENSION max_lines)
-{
- JDIMENSION row_ctr;
-
- if (cinfo->global_state != DSTATE_SCANNING)
- return 0; /* XYQ 2010-6-27: don't throw exception here */
-/* ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); */
- if (cinfo->output_scanline >= cinfo->output_height) {
- WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
- return 0;
- }
-
- /* Call progress monitor hook if present */
- if (cinfo->progress != NULL) {
- cinfo->progress->pass_counter = (long) cinfo->output_scanline;
- cinfo->progress->pass_limit = (long) cinfo->output_height;
- (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
- }
-
- /* Process some data */
- row_ctr = 0;
- (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
- cinfo->output_scanline += row_ctr;
- return row_ctr;
-}
-
-
-/*
- * Alternate entry point to read raw data.
- * Processes exactly one iMCU row per call, unless suspended.
- */
-
-GLOBAL(JDIMENSION)
-jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
- JDIMENSION max_lines)
-{
- JDIMENSION lines_per_iMCU_row;
-
- if (cinfo->global_state != DSTATE_RAW_OK)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- if (cinfo->output_scanline >= cinfo->output_height) {
- WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
- return 0;
- }
-
- /* Call progress monitor hook if present */
- if (cinfo->progress != NULL) {
- cinfo->progress->pass_counter = (long) cinfo->output_scanline;
- cinfo->progress->pass_limit = (long) cinfo->output_height;
- (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
- }
-
- /* Verify that at least one iMCU row can be returned. */
- lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
- if (max_lines < lines_per_iMCU_row)
- ERREXIT(cinfo, JERR_BUFFER_SIZE);
-
- /* Decompress directly into user's buffer. */
- if (! (*cinfo->coef->decompress_data) (cinfo, data))
- return 0; /* suspension forced, can do nothing more */
-
- /* OK, we processed one iMCU row. */
- cinfo->output_scanline += lines_per_iMCU_row;
- return lines_per_iMCU_row;
-}
-
-
-/* Additional entry points for buffered-image mode. */
-
-#ifdef D_MULTISCAN_FILES_SUPPORTED
-
-/*
- * Initialize for an output pass in buffered-image mode.
- */
-
-GLOBAL(boolean)
-jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
-{
- if (cinfo->global_state != DSTATE_BUFIMAGE &&
- cinfo->global_state != DSTATE_PRESCAN)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- /* Limit scan number to valid range */
- if (scan_number <= 0)
- scan_number = 1;
- if (cinfo->inputctl->eoi_reached &&
- scan_number > cinfo->input_scan_number)
- scan_number = cinfo->input_scan_number;
- cinfo->output_scan_number = scan_number;
- /* Perform any dummy output passes, and set up for the real pass */
- return output_pass_setup(cinfo);
-}
-
-
-/*
- * Finish up after an output pass in buffered-image mode.
- *
- * Returns FALSE if suspended. The return value need be inspected only if
- * a suspending data source is used.
- */
-
-GLOBAL(boolean)
-jpeg_finish_output (j_decompress_ptr cinfo)
-{
- if ((cinfo->global_state == DSTATE_SCANNING ||
- cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
- /* Terminate this pass. */
- /* We do not require the whole pass to have been completed. */
- (*cinfo->master->finish_output_pass) (cinfo);
- cinfo->global_state = DSTATE_BUFPOST;
- } else if (cinfo->global_state != DSTATE_BUFPOST) {
- /* BUFPOST = repeat call after a suspension, anything else is error */
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- }
- /* Read markers looking for SOS or EOI */
- while (cinfo->input_scan_number <= cinfo->output_scan_number &&
- ! cinfo->inputctl->eoi_reached) {
- if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
- return FALSE; /* Suspend, come back later */
- }
- cinfo->global_state = DSTATE_BUFIMAGE;
- return TRUE;
-}
-
-#endif /* D_MULTISCAN_FILES_SUPPORTED */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdapistd.c
+ *
+ * Copyright (C) 1994-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 contains application interface code for the decompression half
+ * of the JPEG library. These are the "standard" API routines that are
+ * used in the normal full-decompression case. They are not used by a
+ * transcoding-only application. Note that if an application links in
+ * jpeg_start_decompress, it will end up linking in the entire decompressor.
+ * We thus must separate this file from jdapimin.c to avoid linking the
+ * whole decompression library into a transcoder.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Forward declarations */
+LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
+
+
+/*
+ * Decompression initialization.
+ * jpeg_read_header must be completed before calling this.
+ *
+ * If a multipass operating mode was selected, this will do all but the
+ * last pass, and thus may take a great deal of time.
+ *
+ * Returns FALSE if suspended. The return value need be inspected only if
+ * a suspending data source is used.
+ */
+
+GLOBAL(boolean)
+jpeg_start_decompress (j_decompress_ptr cinfo)
+{
+ if (cinfo->global_state == DSTATE_READY) {
+ /* First call: initialize master control, select active modules */
+ jinit_master_decompress(cinfo);
+ if (cinfo->buffered_image) {
+ /* No more work here; expecting jpeg_start_output next */
+ cinfo->global_state = DSTATE_BUFIMAGE;
+ return TRUE;
+ }
+ cinfo->global_state = DSTATE_PRELOAD;
+ }
+ if (cinfo->global_state == DSTATE_PRELOAD) {
+ /* If file has multiple scans, absorb them all into the coef buffer */
+ if (cinfo->inputctl->has_multiple_scans) {
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+ for (;;) {
+ int retcode;
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL)
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ /* Absorb some more input */
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
+ if (retcode == JPEG_SUSPENDED)
+ return FALSE;
+ if (retcode == JPEG_REACHED_EOI)
+ break;
+ /* Advance progress counter if appropriate */
+ if (cinfo->progress != NULL &&
+ (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
+ if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
+ /* jdmaster underestimated number of scans; ratchet up one scan */
+ cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
+ }
+ }
+ }
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+ }
+ cinfo->output_scan_number = cinfo->input_scan_number;
+ } else if (cinfo->global_state != DSTATE_PRESCAN)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ /* Perform any dummy output passes, and set up for the final pass */
+ return output_pass_setup(cinfo);
+}
+
+
+/*
+ * Set up for an output pass, and perform any dummy pass(es) needed.
+ * Common subroutine for jpeg_start_decompress and jpeg_start_output.
+ * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
+ * Exit: If done, returns TRUE and sets global_state for proper output mode.
+ * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
+ */
+
+LOCAL(boolean)
+output_pass_setup (j_decompress_ptr cinfo)
+{
+ if (cinfo->global_state != DSTATE_PRESCAN) {
+ /* First call: do pass setup */
+ (*cinfo->master->prepare_for_output_pass) (cinfo);
+ cinfo->output_scanline = 0;
+ cinfo->global_state = DSTATE_PRESCAN;
+ }
+ /* Loop over any required dummy passes */
+ while (cinfo->master->is_dummy_pass) {
+#ifdef QUANT_2PASS_SUPPORTED
+ /* Crank through the dummy pass */
+ while (cinfo->output_scanline < cinfo->output_height) {
+ JDIMENSION last_scanline;
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+ /* Process some data */
+ last_scanline = cinfo->output_scanline;
+ (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
+ &cinfo->output_scanline, (JDIMENSION) 0);
+ if (cinfo->output_scanline == last_scanline)
+ return FALSE; /* No progress made, must suspend */
+ }
+ /* Finish up dummy pass, and set up for another one */
+ (*cinfo->master->finish_output_pass) (cinfo);
+ (*cinfo->master->prepare_for_output_pass) (cinfo);
+ cinfo->output_scanline = 0;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif /* QUANT_2PASS_SUPPORTED */
+ }
+ /* Ready for application to drive output pass through
+ * jpeg_read_scanlines or jpeg_read_raw_data.
+ */
+ cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
+ return TRUE;
+}
+
+
+/*
+ * Read some scanlines of data from the JPEG decompressor.
+ *
+ * The return value will be the number of lines actually read.
+ * This may be less than the number requested in several cases,
+ * including bottom of image, data source suspension, and operating
+ * modes that emit multiple scanlines at a time.
+ *
+ * Note: we warn about excess calls to jpeg_read_scanlines() since
+ * this likely signals an application programmer error. However,
+ * an oversize buffer (max_lines > scanlines remaining) is not an error.
+ */
+
+GLOBAL(JDIMENSION)
+jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
+ JDIMENSION max_lines)
+{
+ JDIMENSION row_ctr;
+
+ if (cinfo->global_state != DSTATE_SCANNING)
+ return 0; /* XYQ 2010-6-27: don't throw exception here */
+/* ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); */
+ if (cinfo->output_scanline >= cinfo->output_height) {
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+ return 0;
+ }
+
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+
+ /* Process some data */
+ row_ctr = 0;
+ (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
+ cinfo->output_scanline += row_ctr;
+ return row_ctr;
+}
+
+
+/*
+ * Alternate entry point to read raw data.
+ * Processes exactly one iMCU row per call, unless suspended.
+ */
+
+GLOBAL(JDIMENSION)
+jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
+ JDIMENSION max_lines)
+{
+ JDIMENSION lines_per_iMCU_row;
+
+ if (cinfo->global_state != DSTATE_RAW_OK)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ if (cinfo->output_scanline >= cinfo->output_height) {
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
+ return 0;
+ }
+
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ }
+
+ /* Verify that at least one iMCU row can be returned. */
+ lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
+ if (max_lines < lines_per_iMCU_row)
+ ERREXIT(cinfo, JERR_BUFFER_SIZE);
+
+ /* Decompress directly into user's buffer. */
+ if (! (*cinfo->coef->decompress_data) (cinfo, data))
+ return 0; /* suspension forced, can do nothing more */
+
+ /* OK, we processed one iMCU row. */
+ cinfo->output_scanline += lines_per_iMCU_row;
+ return lines_per_iMCU_row;
+}
+
+
+/* Additional entry points for buffered-image mode. */
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+
+/*
+ * Initialize for an output pass in buffered-image mode.
+ */
+
+GLOBAL(boolean)
+jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
+{
+ if (cinfo->global_state != DSTATE_BUFIMAGE &&
+ cinfo->global_state != DSTATE_PRESCAN)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ /* Limit scan number to valid range */
+ if (scan_number <= 0)
+ scan_number = 1;
+ if (cinfo->inputctl->eoi_reached &&
+ scan_number > cinfo->input_scan_number)
+ scan_number = cinfo->input_scan_number;
+ cinfo->output_scan_number = scan_number;
+ /* Perform any dummy output passes, and set up for the real pass */
+ return output_pass_setup(cinfo);
+}
+
+
+/*
+ * Finish up after an output pass in buffered-image mode.
+ *
+ * Returns FALSE if suspended. The return value need be inspected only if
+ * a suspending data source is used.
+ */
+
+GLOBAL(boolean)
+jpeg_finish_output (j_decompress_ptr cinfo)
+{
+ if ((cinfo->global_state == DSTATE_SCANNING ||
+ cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
+ /* Terminate this pass. */
+ /* We do not require the whole pass to have been completed. */
+ (*cinfo->master->finish_output_pass) (cinfo);
+ cinfo->global_state = DSTATE_BUFPOST;
+ } else if (cinfo->global_state != DSTATE_BUFPOST) {
+ /* BUFPOST = repeat call after a suspension, anything else is error */
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ }
+ /* Read markers looking for SOS or EOI */
+ while (cinfo->input_scan_number <= cinfo->output_scan_number &&
+ ! cinfo->inputctl->eoi_reached) {
+ if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
+ return FALSE; /* Suspend, come back later */
+ }
+ cinfo->global_state = DSTATE_BUFIMAGE;
+ return TRUE;
+}
+
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c b/core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c
index 89041f36f4..099833a8d6 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdcoefct.c
@@ -1,739 +1,739 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdcoefct.c
- *
- * Copyright (C) 1994-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 the coefficient buffer controller for decompression.
- * This controller is the top level of the JPEG decompressor proper.
- * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
- *
- * In buffered-image mode, this controller is the interface between
- * input-oriented processing and output-oriented processing.
- * Also, the input side (only) is used when reading a file for transcoding.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-/* Block smoothing is only applicable for progressive JPEG, so: */
-#ifndef D_PROGRESSIVE_SUPPORTED
-#undef BLOCK_SMOOTHING_SUPPORTED
-#endif
-
-/* Private buffer controller object */
-
-typedef struct {
- struct jpeg_d_coef_controller pub; /* public fields */
-
- /* These variables keep track of the current location of the input side. */
- /* cinfo->input_iMCU_row is also used for this. */
- JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
- int MCU_vert_offset; /* counts MCU rows within iMCU row */
- int MCU_rows_per_iMCU_row; /* number of such rows needed */
-
- /* The output side's location is represented by cinfo->output_iMCU_row. */
-
- /* In single-pass modes, it's sufficient to buffer just one MCU.
- * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
- * and let the entropy decoder write into that workspace each time.
- * (On 80x86, the workspace is FAR even though it's not really very big;
- * this is to keep the module interfaces unchanged when a large coefficient
- * buffer is necessary.)
- * In multi-pass modes, this array points to the current MCU's blocks
- * within the virtual arrays; it is used only by the input side.
- */
- JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
-
-#ifdef D_MULTISCAN_FILES_SUPPORTED
- /* In multi-pass modes, we need a virtual block array for each component. */
- jvirt_barray_ptr whole_image[MAX_COMPONENTS];
-#endif
-
-#ifdef BLOCK_SMOOTHING_SUPPORTED
- /* When doing block smoothing, we latch coefficient Al values here */
- int * coef_bits_latch;
-#define SAVED_COEFS 6 /* we save coef_bits[0..5] */
-#endif
-} my_coef_controller;
-
-typedef my_coef_controller * my_coef_ptr;
-
-/* Forward declarations */
-METHODDEF(int) decompress_onepass
- JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
-#ifdef D_MULTISCAN_FILES_SUPPORTED
-METHODDEF(int) decompress_data
- JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
-#endif
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
-METHODDEF(int) decompress_smooth_data
- JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
-#endif
-
-
-LOCAL(void)
-start_iMCU_row (j_decompress_ptr cinfo)
-/* Reset within-iMCU-row counters for a new row (input side) */
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
-
- /* In an interleaved scan, an MCU row is the same as an iMCU row.
- * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
- * But at the bottom of the image, process only what's left.
- */
- if (cinfo->comps_in_scan > 1) {
- coef->MCU_rows_per_iMCU_row = 1;
- } else {
- if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
- coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
- else
- coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
- }
-
- coef->MCU_ctr = 0;
- coef->MCU_vert_offset = 0;
-}
-
-
-/*
- * Initialize for an input processing pass.
- */
-
-METHODDEF(void)
-start_input_pass (j_decompress_ptr cinfo)
-{
- cinfo->input_iMCU_row = 0;
- start_iMCU_row(cinfo);
-}
-
-
-/*
- * Initialize for an output processing pass.
- */
-
-METHODDEF(void)
-start_output_pass (j_decompress_ptr cinfo)
-{
-#ifdef BLOCK_SMOOTHING_SUPPORTED
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
-
- /* If multipass, check to see whether to use block smoothing on this pass */
- if (coef->pub.coef_arrays != NULL) {
- if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
- coef->pub.decompress_data = decompress_smooth_data;
- else
- coef->pub.decompress_data = decompress_data;
- }
-#endif
- cinfo->output_iMCU_row = 0;
-}
-
-
-/*
- * Decompress and return some data in the single-pass case.
- * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
- * Input and output must run in lockstep since we have only a one-MCU buffer.
- * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
- *
- * NB: output_buf contains a plane for each component in image,
- * which we index according to the component's SOF position.
- */
-
-METHODDEF(int)
-decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- JDIMENSION MCU_col_num; /* index of current MCU within row */
- JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
- JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
- int blkn, ci, xindex, yindex, yoffset, useful_width;
- JSAMPARRAY output_ptr;
- JDIMENSION start_col, output_col;
- jpeg_component_info *compptr;
- inverse_DCT_method_ptr inverse_DCT;
-
- /* Loop to process as much as one whole iMCU row */
- for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
- yoffset++) {
- for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
- MCU_col_num++) {
- /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
- jzero_far((void FAR *) coef->MCU_buffer[0],
- (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
- if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
- /* Suspension forced; update state counters and exit */
- coef->MCU_vert_offset = yoffset;
- coef->MCU_ctr = MCU_col_num;
- return JPEG_SUSPENDED;
- }
- /* Determine where data should go in output_buf and do the IDCT thing.
- * We skip dummy blocks at the right and bottom edges (but blkn gets
- * incremented past them!). Note the inner loop relies on having
- * allocated the MCU_buffer[] blocks sequentially.
- */
- blkn = 0; /* index of current DCT block within MCU */
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- /* Don't bother to IDCT an uninteresting component. */
- if (! compptr->component_needed) {
- blkn += compptr->MCU_blocks;
- continue;
- }
- inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
- useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
- : compptr->last_col_width;
- output_ptr = output_buf[compptr->component_index] +
- yoffset * compptr->DCT_scaled_size;
- start_col = MCU_col_num * compptr->MCU_sample_width;
- for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
- if (cinfo->input_iMCU_row < last_iMCU_row ||
- yoffset+yindex < compptr->last_row_height) {
- output_col = start_col;
- for (xindex = 0; xindex < useful_width; xindex++) {
- (*inverse_DCT) (cinfo, compptr,
- (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
- output_ptr, output_col);
- output_col += compptr->DCT_scaled_size;
- }
- }
- blkn += compptr->MCU_width;
- output_ptr += compptr->DCT_scaled_size;
- }
- }
- }
- /* Completed an MCU row, but perhaps not an iMCU row */
- coef->MCU_ctr = 0;
- }
- /* Completed the iMCU row, advance counters for next one */
- cinfo->output_iMCU_row++;
- if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
- start_iMCU_row(cinfo);
- return JPEG_ROW_COMPLETED;
- }
- /* Completed the scan */
- (*cinfo->inputctl->finish_input_pass) (cinfo);
- return JPEG_SCAN_COMPLETED;
-}
-
-
-/*
- * Dummy consume-input routine for single-pass operation.
- */
-
-METHODDEF(int)
-dummy_consume_data (j_decompress_ptr cinfo)
-{
- return JPEG_SUSPENDED; /* Always indicate nothing was done */
-}
-
-
-#ifdef D_MULTISCAN_FILES_SUPPORTED
-
-/*
- * Consume input data and store it in the full-image coefficient buffer.
- * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
- * ie, v_samp_factor block rows for each component in the scan.
- * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
- */
-
-METHODDEF(int)
-consume_data (j_decompress_ptr cinfo)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- JDIMENSION MCU_col_num; /* index of current MCU within row */
- int blkn, ci, xindex, yindex, yoffset;
- JDIMENSION start_col;
- JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
- JBLOCKROW buffer_ptr;
- jpeg_component_info *compptr;
-
- /* Align the virtual buffers for the components used in this scan. */
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- buffer[ci] = (*cinfo->mem->access_virt_barray)
- ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
- cinfo->input_iMCU_row * compptr->v_samp_factor,
- (JDIMENSION) compptr->v_samp_factor, TRUE);
- /* Note: entropy decoder expects buffer to be zeroed,
- * but this is handled automatically by the memory manager
- * because we requested a pre-zeroed array.
- */
- }
-
- /* Loop to process one whole iMCU row */
- for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
- yoffset++) {
- for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
- MCU_col_num++) {
- /* Construct list of pointers to DCT blocks belonging to this MCU */
- blkn = 0; /* index of current DCT block within MCU */
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- start_col = MCU_col_num * compptr->MCU_width;
- for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
- buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
- for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
- coef->MCU_buffer[blkn++] = buffer_ptr++;
- }
- }
- }
- /* Try to fetch the MCU. */
- if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
- /* Suspension forced; update state counters and exit */
- coef->MCU_vert_offset = yoffset;
- coef->MCU_ctr = MCU_col_num;
- return JPEG_SUSPENDED;
- }
- }
- /* Completed an MCU row, but perhaps not an iMCU row */
- coef->MCU_ctr = 0;
- }
- /* Completed the iMCU row, advance counters for next one */
- if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
- start_iMCU_row(cinfo);
- return JPEG_ROW_COMPLETED;
- }
- /* Completed the scan */
- (*cinfo->inputctl->finish_input_pass) (cinfo);
- return JPEG_SCAN_COMPLETED;
-}
-
-
-/*
- * Decompress and return some data in the multi-pass case.
- * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
- * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
- *
- * NB: output_buf contains a plane for each component in image.
- */
-
-METHODDEF(int)
-decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
- JDIMENSION block_num;
- int ci, block_row, block_rows;
- JBLOCKARRAY buffer;
- JBLOCKROW buffer_ptr;
- JSAMPARRAY output_ptr;
- JDIMENSION output_col;
- jpeg_component_info *compptr;
- inverse_DCT_method_ptr inverse_DCT;
-
- /* Force some input to be done if we are getting ahead of the input. */
- while (cinfo->input_scan_number < cinfo->output_scan_number ||
- (cinfo->input_scan_number == cinfo->output_scan_number &&
- cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
- if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
- return JPEG_SUSPENDED;
- }
-
- /* OK, output from the virtual arrays. */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Don't bother to IDCT an uninteresting component. */
- if (! compptr->component_needed)
- continue;
- /* Align the virtual buffer for this component. */
- buffer = (*cinfo->mem->access_virt_barray)
- ((j_common_ptr) cinfo, coef->whole_image[ci],
- cinfo->output_iMCU_row * compptr->v_samp_factor,
- (JDIMENSION) compptr->v_samp_factor, FALSE);
- /* Count non-dummy DCT block rows in this iMCU row. */
- if (cinfo->output_iMCU_row < last_iMCU_row)
- block_rows = compptr->v_samp_factor;
- else {
- /* NB: can't use last_row_height here; it is input-side-dependent! */
- block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
- if (block_rows == 0) block_rows = compptr->v_samp_factor;
- }
- inverse_DCT = cinfo->idct->inverse_DCT[ci];
- output_ptr = output_buf[ci];
- /* Loop over all DCT blocks to be processed. */
- for (block_row = 0; block_row < block_rows; block_row++) {
- buffer_ptr = buffer[block_row];
- output_col = 0;
- for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
- (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
- output_ptr, output_col);
- buffer_ptr++;
- output_col += compptr->DCT_scaled_size;
- }
- output_ptr += compptr->DCT_scaled_size;
- }
- }
-
- if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
- return JPEG_ROW_COMPLETED;
- return JPEG_SCAN_COMPLETED;
-}
-
-#endif /* D_MULTISCAN_FILES_SUPPORTED */
-
-
-#ifdef BLOCK_SMOOTHING_SUPPORTED
-
-/*
- * This code applies interblock smoothing as described by section K.8
- * of the JPEG standard: the first 5 AC coefficients are estimated from
- * the DC values of a DCT block and its 8 neighboring blocks.
- * We apply smoothing only for progressive JPEG decoding, and only if
- * the coefficients it can estimate are not yet known to full precision.
- */
-
-/* Natural-order array positions of the first 5 zigzag-order coefficients */
-#define Q01_POS 1
-#define Q10_POS 8
-#define Q20_POS 16
-#define Q11_POS 9
-#define Q02_POS 2
-
-/*
- * Determine whether block smoothing is applicable and safe.
- * We also latch the current states of the coef_bits[] entries for the
- * AC coefficients; otherwise, if the input side of the decompressor
- * advances into a new scan, we might think the coefficients are known
- * more accurately than they really are.
- */
-
-LOCAL(boolean)
-smoothing_ok (j_decompress_ptr cinfo)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- boolean smoothing_useful = FALSE;
- int ci, coefi;
- jpeg_component_info *compptr;
- JQUANT_TBL * qtable;
- int * coef_bits;
- int * coef_bits_latch;
-
- if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
- return FALSE;
-
- /* Allocate latch area if not already done */
- if (coef->coef_bits_latch == NULL)
- coef->coef_bits_latch = (int *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- cinfo->num_components *
- (SAVED_COEFS * SIZEOF(int)));
- coef_bits_latch = coef->coef_bits_latch;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* All components' quantization values must already be latched. */
- if ((qtable = compptr->quant_table) == NULL)
- return FALSE;
- /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
- if (qtable->quantval[0] == 0 ||
- qtable->quantval[Q01_POS] == 0 ||
- qtable->quantval[Q10_POS] == 0 ||
- qtable->quantval[Q20_POS] == 0 ||
- qtable->quantval[Q11_POS] == 0 ||
- qtable->quantval[Q02_POS] == 0)
- return FALSE;
- /* DC values must be at least partly known for all components. */
- coef_bits = cinfo->coef_bits[ci];
- if (coef_bits[0] < 0)
- return FALSE;
- /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
- for (coefi = 1; coefi <= 5; coefi++) {
- coef_bits_latch[coefi] = coef_bits[coefi];
- if (coef_bits[coefi] != 0)
- smoothing_useful = TRUE;
- }
- coef_bits_latch += SAVED_COEFS;
- }
-
- return smoothing_useful;
-}
-
-
-/*
- * Variant of decompress_data for use when doing block smoothing.
- */
-
-METHODDEF(int)
-decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
-{
- my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
- JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
- JDIMENSION block_num, last_block_column;
- int ci, block_row, block_rows, access_rows;
- JBLOCKARRAY buffer;
- JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
- JSAMPARRAY output_ptr;
- JDIMENSION output_col;
- jpeg_component_info *compptr;
- inverse_DCT_method_ptr inverse_DCT;
- boolean first_row, last_row;
- JBLOCK workspace;
- int *coef_bits;
- JQUANT_TBL *quanttbl;
- INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
- int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
- int Al, pred;
-
- /* Force some input to be done if we are getting ahead of the input. */
- while (cinfo->input_scan_number <= cinfo->output_scan_number &&
- ! cinfo->inputctl->eoi_reached) {
- if (cinfo->input_scan_number == cinfo->output_scan_number) {
- /* If input is working on current scan, we ordinarily want it to
- * have completed the current row. But if input scan is DC,
- * we want it to keep one row ahead so that next block row's DC
- * values are up to date.
- */
- JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
- if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
- break;
- }
- if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
- return JPEG_SUSPENDED;
- }
-
- /* OK, output from the virtual arrays. */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Don't bother to IDCT an uninteresting component. */
- if (! compptr->component_needed)
- continue;
- /* Count non-dummy DCT block rows in this iMCU row. */
- if (cinfo->output_iMCU_row < last_iMCU_row) {
- block_rows = compptr->v_samp_factor;
- access_rows = block_rows * 2; /* this and next iMCU row */
- last_row = FALSE;
- } else {
- /* NB: can't use last_row_height here; it is input-side-dependent! */
- block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
- if (block_rows == 0) block_rows = compptr->v_samp_factor;
- access_rows = block_rows; /* this iMCU row only */
- last_row = TRUE;
- }
- /* Align the virtual buffer for this component. */
- if (cinfo->output_iMCU_row > 0) {
- access_rows += compptr->v_samp_factor; /* prior iMCU row too */
- buffer = (*cinfo->mem->access_virt_barray)
- ((j_common_ptr) cinfo, coef->whole_image[ci],
- (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
- (JDIMENSION) access_rows, FALSE);
- buffer += compptr->v_samp_factor; /* point to current iMCU row */
- first_row = FALSE;
- } else {
- buffer = (*cinfo->mem->access_virt_barray)
- ((j_common_ptr) cinfo, coef->whole_image[ci],
- (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
- first_row = TRUE;
- }
- /* Fetch component-dependent info */
- coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
- quanttbl = compptr->quant_table;
- Q00 = quanttbl->quantval[0];
- Q01 = quanttbl->quantval[Q01_POS];
- Q10 = quanttbl->quantval[Q10_POS];
- Q20 = quanttbl->quantval[Q20_POS];
- Q11 = quanttbl->quantval[Q11_POS];
- Q02 = quanttbl->quantval[Q02_POS];
- inverse_DCT = cinfo->idct->inverse_DCT[ci];
- output_ptr = output_buf[ci];
- /* Loop over all DCT blocks to be processed. */
- for (block_row = 0; block_row < block_rows; block_row++) {
- buffer_ptr = buffer[block_row];
- if (first_row && block_row == 0)
- prev_block_row = buffer_ptr;
- else
- prev_block_row = buffer[block_row-1];
- if (last_row && block_row == block_rows-1)
- next_block_row = buffer_ptr;
- else
- next_block_row = buffer[block_row+1];
- /* We fetch the surrounding DC values using a sliding-register approach.
- * Initialize all nine here so as to do the right thing on narrow pics.
- */
- DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
- DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
- DC7 = DC8 = DC9 = (int) next_block_row[0][0];
- output_col = 0;
- last_block_column = compptr->width_in_blocks - 1;
- for (block_num = 0; block_num <= last_block_column; block_num++) {
- /* Fetch current DCT block into workspace so we can modify it. */
- jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
- /* Update DC values */
- if (block_num < last_block_column) {
- DC3 = (int) prev_block_row[1][0];
- DC6 = (int) buffer_ptr[1][0];
- DC9 = (int) next_block_row[1][0];
- }
- /* Compute coefficient estimates per K.8.
- * An estimate is applied only if coefficient is still zero,
- * and is not known to be fully accurate.
- */
- /* AC01 */
- if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
- num = 36 * Q00 * (DC4 - DC6);
- if (num >= 0) {
- pred = (int) (((Q01<<7) + num) / (Q01<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- } else {
- pred = (int) (((Q01<<7) - num) / (Q01<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- pred = -pred;
- }
- workspace[1] = (JCOEF) pred;
- }
- /* AC10 */
- if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
- num = 36 * Q00 * (DC2 - DC8);
- if (num >= 0) {
- pred = (int) (((Q10<<7) + num) / (Q10<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- } else {
- pred = (int) (((Q10<<7) - num) / (Q10<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- pred = -pred;
- }
- workspace[8] = (JCOEF) pred;
- }
- /* AC20 */
- if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
- num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
- if (num >= 0) {
- pred = (int) (((Q20<<7) + num) / (Q20<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- } else {
- pred = (int) (((Q20<<7) - num) / (Q20<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- pred = -pred;
- }
- workspace[16] = (JCOEF) pred;
- }
- /* AC11 */
- if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
- num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
- if (num >= 0) {
- pred = (int) (((Q11<<7) + num) / (Q11<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- } else {
- pred = (int) (((Q11<<7) - num) / (Q11<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- pred = -pred;
- }
- workspace[9] = (JCOEF) pred;
- }
- /* AC02 */
- if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
- num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
- if (num >= 0) {
- pred = (int) (((Q02<<7) + num) / (Q02<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- } else {
- pred = (int) (((Q02<<7) - num) / (Q02<<8));
- if (Al > 0 && pred >= (1<<Al))
- pred = (1<<Al)-1;
- pred = -pred;
- }
- workspace[2] = (JCOEF) pred;
- }
- /* OK, do the IDCT */
- (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
- output_ptr, output_col);
- /* Advance for next column */
- DC1 = DC2; DC2 = DC3;
- DC4 = DC5; DC5 = DC6;
- DC7 = DC8; DC8 = DC9;
- buffer_ptr++, prev_block_row++, next_block_row++;
- output_col += compptr->DCT_scaled_size;
- }
- output_ptr += compptr->DCT_scaled_size;
- }
- }
-
- if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
- return JPEG_ROW_COMPLETED;
- return JPEG_SCAN_COMPLETED;
-}
-
-#endif /* BLOCK_SMOOTHING_SUPPORTED */
-
-
-/*
- * Initialize coefficient buffer controller.
- */
-
-GLOBAL(void)
-jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
-{
- my_coef_ptr coef;
-
- coef = (my_coef_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_coef_controller));
- cinfo->coef = (struct jpeg_d_coef_controller *) coef;
- coef->pub.start_input_pass = start_input_pass;
- coef->pub.start_output_pass = start_output_pass;
-#ifdef BLOCK_SMOOTHING_SUPPORTED
- coef->coef_bits_latch = NULL;
-#endif
-
- /* Create the coefficient buffer. */
- if (need_full_buffer) {
-#ifdef D_MULTISCAN_FILES_SUPPORTED
- /* Allocate a full-image virtual array for each component, */
- /* padded to a multiple of samp_factor DCT blocks in each direction. */
- /* Note we ask for a pre-zeroed array. */
- int ci, access_rows;
- jpeg_component_info *compptr;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- access_rows = compptr->v_samp_factor;
-#ifdef BLOCK_SMOOTHING_SUPPORTED
- /* If block smoothing could be used, need a bigger window */
- if (cinfo->progressive_mode)
- access_rows *= 3;
-#endif
- coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
- (JDIMENSION) jround_up((long) compptr->width_in_blocks,
- (long) compptr->h_samp_factor),
- (JDIMENSION) jround_up((long) compptr->height_in_blocks,
- (long) compptr->v_samp_factor),
- (JDIMENSION) access_rows);
- }
- coef->pub.consume_data = consume_data;
- coef->pub.decompress_data = decompress_data;
- coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else {
- /* We only need a single-MCU buffer. */
- JBLOCKROW buffer;
- int i;
-
- buffer = (JBLOCKROW)
- (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
- for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
- coef->MCU_buffer[i] = buffer + i;
- }
- coef->pub.consume_data = dummy_consume_data;
- coef->pub.decompress_data = decompress_onepass;
- coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdcoefct.c
+ *
+ * Copyright (C) 1994-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 the coefficient buffer controller for decompression.
+ * This controller is the top level of the JPEG decompressor proper.
+ * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
+ *
+ * In buffered-image mode, this controller is the interface between
+ * input-oriented processing and output-oriented processing.
+ * Also, the input side (only) is used when reading a file for transcoding.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+/* Block smoothing is only applicable for progressive JPEG, so: */
+#ifndef D_PROGRESSIVE_SUPPORTED
+#undef BLOCK_SMOOTHING_SUPPORTED
+#endif
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_d_coef_controller pub; /* public fields */
+
+ /* These variables keep track of the current location of the input side. */
+ /* cinfo->input_iMCU_row is also used for this. */
+ JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
+ int MCU_vert_offset; /* counts MCU rows within iMCU row */
+ int MCU_rows_per_iMCU_row; /* number of such rows needed */
+
+ /* The output side's location is represented by cinfo->output_iMCU_row. */
+
+ /* In single-pass modes, it's sufficient to buffer just one MCU.
+ * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
+ * and let the entropy decoder write into that workspace each time.
+ * (On 80x86, the workspace is FAR even though it's not really very big;
+ * this is to keep the module interfaces unchanged when a large coefficient
+ * buffer is necessary.)
+ * In multi-pass modes, this array points to the current MCU's blocks
+ * within the virtual arrays; it is used only by the input side.
+ */
+ JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+ /* In multi-pass modes, we need a virtual block array for each component. */
+ jvirt_barray_ptr whole_image[MAX_COMPONENTS];
+#endif
+
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+ /* When doing block smoothing, we latch coefficient Al values here */
+ int * coef_bits_latch;
+#define SAVED_COEFS 6 /* we save coef_bits[0..5] */
+#endif
+} my_coef_controller;
+
+typedef my_coef_controller * my_coef_ptr;
+
+/* Forward declarations */
+METHODDEF(int) decompress_onepass
+ JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+METHODDEF(int) decompress_data
+ JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+#endif
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
+METHODDEF(int) decompress_smooth_data
+ JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
+#endif
+
+
+LOCAL(void)
+start_iMCU_row (j_decompress_ptr cinfo)
+/* Reset within-iMCU-row counters for a new row (input side) */
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ /* In an interleaved scan, an MCU row is the same as an iMCU row.
+ * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
+ * But at the bottom of the image, process only what's left.
+ */
+ if (cinfo->comps_in_scan > 1) {
+ coef->MCU_rows_per_iMCU_row = 1;
+ } else {
+ if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
+ else
+ coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
+ }
+
+ coef->MCU_ctr = 0;
+ coef->MCU_vert_offset = 0;
+}
+
+
+/*
+ * Initialize for an input processing pass.
+ */
+
+METHODDEF(void)
+start_input_pass (j_decompress_ptr cinfo)
+{
+ cinfo->input_iMCU_row = 0;
+ start_iMCU_row(cinfo);
+}
+
+
+/*
+ * Initialize for an output processing pass.
+ */
+
+METHODDEF(void)
+start_output_pass (j_decompress_ptr cinfo)
+{
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+
+ /* If multipass, check to see whether to use block smoothing on this pass */
+ if (coef->pub.coef_arrays != NULL) {
+ if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
+ coef->pub.decompress_data = decompress_smooth_data;
+ else
+ coef->pub.decompress_data = decompress_data;
+ }
+#endif
+ cinfo->output_iMCU_row = 0;
+}
+
+
+/*
+ * Decompress and return some data in the single-pass case.
+ * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
+ * Input and output must run in lockstep since we have only a one-MCU buffer.
+ * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
+ *
+ * NB: output_buf contains a plane for each component in image,
+ * which we index according to the component's SOF position.
+ */
+
+METHODDEF(int)
+decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ int blkn, ci, xindex, yindex, yoffset, useful_width;
+ JSAMPARRAY output_ptr;
+ JDIMENSION start_col, output_col;
+ jpeg_component_info *compptr;
+ inverse_DCT_method_ptr inverse_DCT;
+
+ /* Loop to process as much as one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
+ MCU_col_num++) {
+ /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
+ jzero_far((void FAR *) coef->MCU_buffer[0],
+ (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
+ if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->MCU_ctr = MCU_col_num;
+ return JPEG_SUSPENDED;
+ }
+ /* Determine where data should go in output_buf and do the IDCT thing.
+ * We skip dummy blocks at the right and bottom edges (but blkn gets
+ * incremented past them!). Note the inner loop relies on having
+ * allocated the MCU_buffer[] blocks sequentially.
+ */
+ blkn = 0; /* index of current DCT block within MCU */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* Don't bother to IDCT an uninteresting component. */
+ if (! compptr->component_needed) {
+ blkn += compptr->MCU_blocks;
+ continue;
+ }
+ inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
+ useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
+ : compptr->last_col_width;
+ output_ptr = output_buf[compptr->component_index] +
+ yoffset * compptr->DCT_scaled_size;
+ start_col = MCU_col_num * compptr->MCU_sample_width;
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ if (cinfo->input_iMCU_row < last_iMCU_row ||
+ yoffset+yindex < compptr->last_row_height) {
+ output_col = start_col;
+ for (xindex = 0; xindex < useful_width; xindex++) {
+ (*inverse_DCT) (cinfo, compptr,
+ (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
+ output_ptr, output_col);
+ output_col += compptr->DCT_scaled_size;
+ }
+ }
+ blkn += compptr->MCU_width;
+ output_ptr += compptr->DCT_scaled_size;
+ }
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->MCU_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ cinfo->output_iMCU_row++;
+ if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
+ start_iMCU_row(cinfo);
+ return JPEG_ROW_COMPLETED;
+ }
+ /* Completed the scan */
+ (*cinfo->inputctl->finish_input_pass) (cinfo);
+ return JPEG_SCAN_COMPLETED;
+}
+
+
+/*
+ * Dummy consume-input routine for single-pass operation.
+ */
+
+METHODDEF(int)
+dummy_consume_data (j_decompress_ptr cinfo)
+{
+ return JPEG_SUSPENDED; /* Always indicate nothing was done */
+}
+
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+
+/*
+ * Consume input data and store it in the full-image coefficient buffer.
+ * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
+ * ie, v_samp_factor block rows for each component in the scan.
+ * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
+ */
+
+METHODDEF(int)
+consume_data (j_decompress_ptr cinfo)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION MCU_col_num; /* index of current MCU within row */
+ int blkn, ci, xindex, yindex, yoffset;
+ JDIMENSION start_col;
+ JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
+ JBLOCKROW buffer_ptr;
+ jpeg_component_info *compptr;
+
+ /* Align the virtual buffers for the components used in this scan. */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ buffer[ci] = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
+ cinfo->input_iMCU_row * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, TRUE);
+ /* Note: entropy decoder expects buffer to be zeroed,
+ * but this is handled automatically by the memory manager
+ * because we requested a pre-zeroed array.
+ */
+ }
+
+ /* Loop to process one whole iMCU row */
+ for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
+ yoffset++) {
+ for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
+ MCU_col_num++) {
+ /* Construct list of pointers to DCT blocks belonging to this MCU */
+ blkn = 0; /* index of current DCT block within MCU */
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ start_col = MCU_col_num * compptr->MCU_width;
+ for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
+ buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
+ for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
+ coef->MCU_buffer[blkn++] = buffer_ptr++;
+ }
+ }
+ }
+ /* Try to fetch the MCU. */
+ if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
+ /* Suspension forced; update state counters and exit */
+ coef->MCU_vert_offset = yoffset;
+ coef->MCU_ctr = MCU_col_num;
+ return JPEG_SUSPENDED;
+ }
+ }
+ /* Completed an MCU row, but perhaps not an iMCU row */
+ coef->MCU_ctr = 0;
+ }
+ /* Completed the iMCU row, advance counters for next one */
+ if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
+ start_iMCU_row(cinfo);
+ return JPEG_ROW_COMPLETED;
+ }
+ /* Completed the scan */
+ (*cinfo->inputctl->finish_input_pass) (cinfo);
+ return JPEG_SCAN_COMPLETED;
+}
+
+
+/*
+ * Decompress and return some data in the multi-pass case.
+ * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
+ * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
+ *
+ * NB: output_buf contains a plane for each component in image.
+ */
+
+METHODDEF(int)
+decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ JDIMENSION block_num;
+ int ci, block_row, block_rows;
+ JBLOCKARRAY buffer;
+ JBLOCKROW buffer_ptr;
+ JSAMPARRAY output_ptr;
+ JDIMENSION output_col;
+ jpeg_component_info *compptr;
+ inverse_DCT_method_ptr inverse_DCT;
+
+ /* Force some input to be done if we are getting ahead of the input. */
+ while (cinfo->input_scan_number < cinfo->output_scan_number ||
+ (cinfo->input_scan_number == cinfo->output_scan_number &&
+ cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
+ if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
+ return JPEG_SUSPENDED;
+ }
+
+ /* OK, output from the virtual arrays. */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Don't bother to IDCT an uninteresting component. */
+ if (! compptr->component_needed)
+ continue;
+ /* Align the virtual buffer for this component. */
+ buffer = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[ci],
+ cinfo->output_iMCU_row * compptr->v_samp_factor,
+ (JDIMENSION) compptr->v_samp_factor, FALSE);
+ /* Count non-dummy DCT block rows in this iMCU row. */
+ if (cinfo->output_iMCU_row < last_iMCU_row)
+ block_rows = compptr->v_samp_factor;
+ else {
+ /* NB: can't use last_row_height here; it is input-side-dependent! */
+ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (block_rows == 0) block_rows = compptr->v_samp_factor;
+ }
+ inverse_DCT = cinfo->idct->inverse_DCT[ci];
+ output_ptr = output_buf[ci];
+ /* Loop over all DCT blocks to be processed. */
+ for (block_row = 0; block_row < block_rows; block_row++) {
+ buffer_ptr = buffer[block_row];
+ output_col = 0;
+ for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
+ (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
+ output_ptr, output_col);
+ buffer_ptr++;
+ output_col += compptr->DCT_scaled_size;
+ }
+ output_ptr += compptr->DCT_scaled_size;
+ }
+ }
+
+ if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
+ return JPEG_ROW_COMPLETED;
+ return JPEG_SCAN_COMPLETED;
+}
+
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+
+
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+
+/*
+ * This code applies interblock smoothing as described by section K.8
+ * of the JPEG standard: the first 5 AC coefficients are estimated from
+ * the DC values of a DCT block and its 8 neighboring blocks.
+ * We apply smoothing only for progressive JPEG decoding, and only if
+ * the coefficients it can estimate are not yet known to full precision.
+ */
+
+/* Natural-order array positions of the first 5 zigzag-order coefficients */
+#define Q01_POS 1
+#define Q10_POS 8
+#define Q20_POS 16
+#define Q11_POS 9
+#define Q02_POS 2
+
+/*
+ * Determine whether block smoothing is applicable and safe.
+ * We also latch the current states of the coef_bits[] entries for the
+ * AC coefficients; otherwise, if the input side of the decompressor
+ * advances into a new scan, we might think the coefficients are known
+ * more accurately than they really are.
+ */
+
+LOCAL(boolean)
+smoothing_ok (j_decompress_ptr cinfo)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ boolean smoothing_useful = FALSE;
+ int ci, coefi;
+ jpeg_component_info *compptr;
+ JQUANT_TBL * qtable;
+ int * coef_bits;
+ int * coef_bits_latch;
+
+ if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
+ return FALSE;
+
+ /* Allocate latch area if not already done */
+ if (coef->coef_bits_latch == NULL)
+ coef->coef_bits_latch = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->num_components *
+ (SAVED_COEFS * SIZEOF(int)));
+ coef_bits_latch = coef->coef_bits_latch;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* All components' quantization values must already be latched. */
+ if ((qtable = compptr->quant_table) == NULL)
+ return FALSE;
+ /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
+ if (qtable->quantval[0] == 0 ||
+ qtable->quantval[Q01_POS] == 0 ||
+ qtable->quantval[Q10_POS] == 0 ||
+ qtable->quantval[Q20_POS] == 0 ||
+ qtable->quantval[Q11_POS] == 0 ||
+ qtable->quantval[Q02_POS] == 0)
+ return FALSE;
+ /* DC values must be at least partly known for all components. */
+ coef_bits = cinfo->coef_bits[ci];
+ if (coef_bits[0] < 0)
+ return FALSE;
+ /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
+ for (coefi = 1; coefi <= 5; coefi++) {
+ coef_bits_latch[coefi] = coef_bits[coefi];
+ if (coef_bits[coefi] != 0)
+ smoothing_useful = TRUE;
+ }
+ coef_bits_latch += SAVED_COEFS;
+ }
+
+ return smoothing_useful;
+}
+
+
+/*
+ * Variant of decompress_data for use when doing block smoothing.
+ */
+
+METHODDEF(int)
+decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
+{
+ my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
+ JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
+ JDIMENSION block_num, last_block_column;
+ int ci, block_row, block_rows, access_rows;
+ JBLOCKARRAY buffer;
+ JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
+ JSAMPARRAY output_ptr;
+ JDIMENSION output_col;
+ jpeg_component_info *compptr;
+ inverse_DCT_method_ptr inverse_DCT;
+ boolean first_row, last_row;
+ JBLOCK workspace;
+ int *coef_bits;
+ JQUANT_TBL *quanttbl;
+ INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
+ int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
+ int Al, pred;
+
+ /* Force some input to be done if we are getting ahead of the input. */
+ while (cinfo->input_scan_number <= cinfo->output_scan_number &&
+ ! cinfo->inputctl->eoi_reached) {
+ if (cinfo->input_scan_number == cinfo->output_scan_number) {
+ /* If input is working on current scan, we ordinarily want it to
+ * have completed the current row. But if input scan is DC,
+ * we want it to keep one row ahead so that next block row's DC
+ * values are up to date.
+ */
+ JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
+ if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
+ break;
+ }
+ if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
+ return JPEG_SUSPENDED;
+ }
+
+ /* OK, output from the virtual arrays. */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Don't bother to IDCT an uninteresting component. */
+ if (! compptr->component_needed)
+ continue;
+ /* Count non-dummy DCT block rows in this iMCU row. */
+ if (cinfo->output_iMCU_row < last_iMCU_row) {
+ block_rows = compptr->v_samp_factor;
+ access_rows = block_rows * 2; /* this and next iMCU row */
+ last_row = FALSE;
+ } else {
+ /* NB: can't use last_row_height here; it is input-side-dependent! */
+ block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (block_rows == 0) block_rows = compptr->v_samp_factor;
+ access_rows = block_rows; /* this iMCU row only */
+ last_row = TRUE;
+ }
+ /* Align the virtual buffer for this component. */
+ if (cinfo->output_iMCU_row > 0) {
+ access_rows += compptr->v_samp_factor; /* prior iMCU row too */
+ buffer = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[ci],
+ (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
+ (JDIMENSION) access_rows, FALSE);
+ buffer += compptr->v_samp_factor; /* point to current iMCU row */
+ first_row = FALSE;
+ } else {
+ buffer = (*cinfo->mem->access_virt_barray)
+ ((j_common_ptr) cinfo, coef->whole_image[ci],
+ (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
+ first_row = TRUE;
+ }
+ /* Fetch component-dependent info */
+ coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
+ quanttbl = compptr->quant_table;
+ Q00 = quanttbl->quantval[0];
+ Q01 = quanttbl->quantval[Q01_POS];
+ Q10 = quanttbl->quantval[Q10_POS];
+ Q20 = quanttbl->quantval[Q20_POS];
+ Q11 = quanttbl->quantval[Q11_POS];
+ Q02 = quanttbl->quantval[Q02_POS];
+ inverse_DCT = cinfo->idct->inverse_DCT[ci];
+ output_ptr = output_buf[ci];
+ /* Loop over all DCT blocks to be processed. */
+ for (block_row = 0; block_row < block_rows; block_row++) {
+ buffer_ptr = buffer[block_row];
+ if (first_row && block_row == 0)
+ prev_block_row = buffer_ptr;
+ else
+ prev_block_row = buffer[block_row-1];
+ if (last_row && block_row == block_rows-1)
+ next_block_row = buffer_ptr;
+ else
+ next_block_row = buffer[block_row+1];
+ /* We fetch the surrounding DC values using a sliding-register approach.
+ * Initialize all nine here so as to do the right thing on narrow pics.
+ */
+ DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
+ DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
+ DC7 = DC8 = DC9 = (int) next_block_row[0][0];
+ output_col = 0;
+ last_block_column = compptr->width_in_blocks - 1;
+ for (block_num = 0; block_num <= last_block_column; block_num++) {
+ /* Fetch current DCT block into workspace so we can modify it. */
+ jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
+ /* Update DC values */
+ if (block_num < last_block_column) {
+ DC3 = (int) prev_block_row[1][0];
+ DC6 = (int) buffer_ptr[1][0];
+ DC9 = (int) next_block_row[1][0];
+ }
+ /* Compute coefficient estimates per K.8.
+ * An estimate is applied only if coefficient is still zero,
+ * and is not known to be fully accurate.
+ */
+ /* AC01 */
+ if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
+ num = 36 * Q00 * (DC4 - DC6);
+ if (num >= 0) {
+ pred = (int) (((Q01<<7) + num) / (Q01<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q01<<7) - num) / (Q01<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[1] = (JCOEF) pred;
+ }
+ /* AC10 */
+ if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
+ num = 36 * Q00 * (DC2 - DC8);
+ if (num >= 0) {
+ pred = (int) (((Q10<<7) + num) / (Q10<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q10<<7) - num) / (Q10<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[8] = (JCOEF) pred;
+ }
+ /* AC20 */
+ if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
+ num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
+ if (num >= 0) {
+ pred = (int) (((Q20<<7) + num) / (Q20<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q20<<7) - num) / (Q20<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[16] = (JCOEF) pred;
+ }
+ /* AC11 */
+ if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
+ num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
+ if (num >= 0) {
+ pred = (int) (((Q11<<7) + num) / (Q11<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q11<<7) - num) / (Q11<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[9] = (JCOEF) pred;
+ }
+ /* AC02 */
+ if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
+ num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
+ if (num >= 0) {
+ pred = (int) (((Q02<<7) + num) / (Q02<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ } else {
+ pred = (int) (((Q02<<7) - num) / (Q02<<8));
+ if (Al > 0 && pred >= (1<<Al))
+ pred = (1<<Al)-1;
+ pred = -pred;
+ }
+ workspace[2] = (JCOEF) pred;
+ }
+ /* OK, do the IDCT */
+ (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
+ output_ptr, output_col);
+ /* Advance for next column */
+ DC1 = DC2; DC2 = DC3;
+ DC4 = DC5; DC5 = DC6;
+ DC7 = DC8; DC8 = DC9;
+ buffer_ptr++, prev_block_row++, next_block_row++;
+ output_col += compptr->DCT_scaled_size;
+ }
+ output_ptr += compptr->DCT_scaled_size;
+ }
+ }
+
+ if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
+ return JPEG_ROW_COMPLETED;
+ return JPEG_SCAN_COMPLETED;
+}
+
+#endif /* BLOCK_SMOOTHING_SUPPORTED */
+
+
+/*
+ * Initialize coefficient buffer controller.
+ */
+
+GLOBAL(void)
+jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+ my_coef_ptr coef;
+
+ coef = (my_coef_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_coef_controller));
+ cinfo->coef = (struct jpeg_d_coef_controller *) coef;
+ coef->pub.start_input_pass = start_input_pass;
+ coef->pub.start_output_pass = start_output_pass;
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+ coef->coef_bits_latch = NULL;
+#endif
+
+ /* Create the coefficient buffer. */
+ if (need_full_buffer) {
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+ /* Allocate a full-image virtual array for each component, */
+ /* padded to a multiple of samp_factor DCT blocks in each direction. */
+ /* Note we ask for a pre-zeroed array. */
+ int ci, access_rows;
+ jpeg_component_info *compptr;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ access_rows = compptr->v_samp_factor;
+#ifdef BLOCK_SMOOTHING_SUPPORTED
+ /* If block smoothing could be used, need a bigger window */
+ if (cinfo->progressive_mode)
+ access_rows *= 3;
+#endif
+ coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
+ (JDIMENSION) jround_up((long) compptr->width_in_blocks,
+ (long) compptr->h_samp_factor),
+ (JDIMENSION) jround_up((long) compptr->height_in_blocks,
+ (long) compptr->v_samp_factor),
+ (JDIMENSION) access_rows);
+ }
+ coef->pub.consume_data = consume_data;
+ coef->pub.decompress_data = decompress_data;
+ coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ /* We only need a single-MCU buffer. */
+ JBLOCKROW buffer;
+ int i;
+
+ buffer = (JBLOCKROW)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
+ for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
+ coef->MCU_buffer[i] = buffer + i;
+ }
+ coef->pub.consume_data = dummy_consume_data;
+ coef->pub.decompress_data = decompress_onepass;
+ coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c b/core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c
index 5cd35f5ec9..c17329b86d 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdcolor.c
@@ -1,399 +1,399 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdcolor.c
- *
- * 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 output colorspace conversion routines.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Private subobject */
-
-typedef struct {
- struct jpeg_color_deconverter pub; /* public fields */
-
- /* Private state for YCC->RGB conversion */
- int * Cr_r_tab; /* => table for Cr to R conversion */
- int * Cb_b_tab; /* => table for Cb to B conversion */
- INT32 * Cr_g_tab; /* => table for Cr to G conversion */
- INT32 * Cb_g_tab; /* => table for Cb to G conversion */
-} my_color_deconverter;
-
-typedef my_color_deconverter * my_cconvert_ptr;
-
-
-/**************** YCbCr -> RGB conversion: most common case **************/
-
-/*
- * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
- * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
- * The conversion equations to be implemented are therefore
- * R = Y + 1.40200 * Cr
- * G = Y - 0.34414 * Cb - 0.71414 * Cr
- * B = Y + 1.77200 * Cb
- * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
- * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
- *
- * To avoid floating-point arithmetic, we represent the fractional constants
- * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
- * the products by 2^16, with appropriate rounding, to get the correct answer.
- * Notice that Y, being an integral input, does not contribute any fraction
- * so it need not participate in the rounding.
- *
- * For even more speed, we avoid doing any multiplications in the inner loop
- * by precalculating the constants times Cb and Cr for all possible values.
- * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
- * for 12-bit samples it is still acceptable. It's not very reasonable for
- * 16-bit samples, but if you want lossless storage you shouldn't be changing
- * colorspace anyway.
- * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
- * values for the G calculation are left scaled up, since we must add them
- * together before rounding.
- */
-
-#define SCALEBITS 16 /* speediest right-shift on some machines */
-#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
-#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
-
-
-/*
- * Initialize tables for YCC->RGB colorspace conversion.
- */
-
-LOCAL(void)
-build_ycc_rgb_table (j_decompress_ptr cinfo)
-{
- my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
- int i;
- INT32 x;
- SHIFT_TEMPS
-
- cconvert->Cr_r_tab = (int *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (MAXJSAMPLE+1) * SIZEOF(int));
- cconvert->Cb_b_tab = (int *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (MAXJSAMPLE+1) * SIZEOF(int));
- cconvert->Cr_g_tab = (INT32 *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (MAXJSAMPLE+1) * SIZEOF(INT32));
- cconvert->Cb_g_tab = (INT32 *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (MAXJSAMPLE+1) * SIZEOF(INT32));
-
- for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
- /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
- /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
- /* Cr=>R value is nearest int to 1.40200 * x */
- cconvert->Cr_r_tab[i] = (int)
- RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
- /* Cb=>B value is nearest int to 1.77200 * x */
- cconvert->Cb_b_tab[i] = (int)
- RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
- /* Cr=>G value is scaled-up -0.71414 * x */
- cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
- /* Cb=>G value is scaled-up -0.34414 * x */
- /* We also add in ONE_HALF so that need not do it in inner loop */
- cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
- }
-}
-
-
-/*
- * Convert some rows of samples to the output colorspace.
- *
- * Note that we change from noninterleaved, one-plane-per-component format
- * to interleaved-pixel format. The output buffer is therefore three times
- * as wide as the input buffer.
- * A starting row offset is provided only for the input buffer. The caller
- * can easily adjust the passed output_buf value to accommodate any row
- * offset required on that side.
- */
-
-METHODDEF(void)
-ycc_rgb_convert (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION input_row,
- JSAMPARRAY output_buf, int num_rows)
-{
- my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
- register int y, cb, cr;
- register JSAMPROW outptr;
- register JSAMPROW inptr0, inptr1, inptr2;
- register JDIMENSION col;
- JDIMENSION num_cols = cinfo->output_width;
- /* copy these pointers into registers if possible */
- register JSAMPLE * range_limit = cinfo->sample_range_limit;
- register int * Crrtab = cconvert->Cr_r_tab;
- register int * Cbbtab = cconvert->Cb_b_tab;
- register INT32 * Crgtab = cconvert->Cr_g_tab;
- register INT32 * Cbgtab = cconvert->Cb_g_tab;
- SHIFT_TEMPS
-
- while (--num_rows >= 0) {
- inptr0 = input_buf[0][input_row];
- inptr1 = input_buf[1][input_row];
- inptr2 = input_buf[2][input_row];
- input_row++;
- outptr = *output_buf++;
- for (col = 0; col < num_cols; col++) {
- y = GETJSAMPLE(inptr0[col]);
- cb = GETJSAMPLE(inptr1[col]);
- cr = GETJSAMPLE(inptr2[col]);
- /* Range-limiting is essential due to noise introduced by DCT losses. */
- outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
- outptr[RGB_GREEN] = range_limit[y +
- ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
- SCALEBITS))];
- outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
- outptr += RGB_PIXELSIZE;
- }
- }
-}
-
-
-/**************** Cases other than YCbCr -> RGB **************/
-
-
-/*
- * Color conversion for no colorspace change: just copy the data,
- * converting from separate-planes to interleaved representation.
- */
-
-METHODDEF(void)
-null_convert (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION input_row,
- JSAMPARRAY output_buf, int num_rows)
-{
- register JSAMPROW inptr, outptr;
- register JDIMENSION count;
- register int num_components = cinfo->num_components;
- JDIMENSION num_cols = cinfo->output_width;
- int ci;
-
- while (--num_rows >= 0) {
- for (ci = 0; ci < num_components; ci++) {
- inptr = input_buf[ci][input_row];
- outptr = output_buf[0] + ci;
- for (count = num_cols; count > 0; count--) {
- *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
- outptr += num_components;
- }
- }
- input_row++;
- output_buf++;
- }
-}
-
-
-/*
- * Color conversion for grayscale: just copy the data.
- * This also works for YCbCr -> grayscale conversion, in which
- * we just copy the Y (luminance) component and ignore chrominance.
- */
-
-METHODDEF(void)
-grayscale_convert (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION input_row,
- JSAMPARRAY output_buf, int num_rows)
-{
- jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
- num_rows, cinfo->output_width);
-}
-
-
-/*
- * Convert grayscale to RGB: just duplicate the graylevel three times.
- * This is provided to support applications that don't want to cope
- * with grayscale as a separate case.
- */
-
-METHODDEF(void)
-gray_rgb_convert (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION input_row,
- JSAMPARRAY output_buf, int num_rows)
-{
- register JSAMPROW inptr, outptr;
- register JDIMENSION col;
- JDIMENSION num_cols = cinfo->output_width;
-
- while (--num_rows >= 0) {
- inptr = input_buf[0][input_row++];
- outptr = *output_buf++;
- for (col = 0; col < num_cols; col++) {
- /* We can dispense with GETJSAMPLE() here */
- outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
- outptr += RGB_PIXELSIZE;
- }
- }
-}
-
-
-/*
- * Adobe-style YCCK->CMYK conversion.
- * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
- * conversion as above, while passing K (black) unchanged.
- * We assume build_ycc_rgb_table has been called.
- */
-
-METHODDEF(void)
-ycck_cmyk_convert (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION input_row,
- JSAMPARRAY output_buf, int num_rows)
-{
- my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
- register int y, cb, cr;
- register JSAMPROW outptr;
- register JSAMPROW inptr0, inptr1, inptr2, inptr3;
- register JDIMENSION col;
- JDIMENSION num_cols = cinfo->output_width;
- /* copy these pointers into registers if possible */
- register JSAMPLE * range_limit = cinfo->sample_range_limit;
- register int * Crrtab = cconvert->Cr_r_tab;
- register int * Cbbtab = cconvert->Cb_b_tab;
- register INT32 * Crgtab = cconvert->Cr_g_tab;
- register INT32 * Cbgtab = cconvert->Cb_g_tab;
- SHIFT_TEMPS
-
- while (--num_rows >= 0) {
- inptr0 = input_buf[0][input_row];
- inptr1 = input_buf[1][input_row];
- inptr2 = input_buf[2][input_row];
- inptr3 = input_buf[3][input_row];
- input_row++;
- outptr = *output_buf++;
- for (col = 0; col < num_cols; col++) {
- y = GETJSAMPLE(inptr0[col]);
- cb = GETJSAMPLE(inptr1[col]);
- cr = GETJSAMPLE(inptr2[col]);
- /* Range-limiting is essential due to noise introduced by DCT losses. */
- outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
- outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
- ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
- SCALEBITS)))];
- outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
- /* K passes through unchanged */
- outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
- outptr += 4;
- }
- }
-}
-
-
-/*
- * Empty method for start_pass.
- */
-
-METHODDEF(void)
-start_pass_dcolor (j_decompress_ptr cinfo)
-{
- /* no work needed */
-}
-
-
-/*
- * Module initialization routine for output colorspace conversion.
- */
-
-GLOBAL(void)
-jinit_color_deconverter (j_decompress_ptr cinfo)
-{
- my_cconvert_ptr cconvert;
- int ci;
-
- cconvert = (my_cconvert_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_color_deconverter));
- cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
- cconvert->pub.start_pass = start_pass_dcolor;
-
- /* Make sure num_components agrees with jpeg_color_space */
- switch (cinfo->jpeg_color_space) {
- case JCS_GRAYSCALE:
- if (cinfo->num_components != 1)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- break;
-
- case JCS_RGB:
- case JCS_YCbCr:
- if (cinfo->num_components != 3)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- break;
-
- case JCS_CMYK:
- case JCS_YCCK:
- if (cinfo->num_components != 4)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- break;
-
- default: /* JCS_UNKNOWN can be anything */
- if (cinfo->num_components < 1)
- ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
- break;
- }
-
- /* Set out_color_components and conversion method based on requested space.
- * Also clear the component_needed flags for any unused components,
- * so that earlier pipeline stages can avoid useless computation.
- */
-
- switch (cinfo->out_color_space) {
- case JCS_GRAYSCALE:
- cinfo->out_color_components = 1;
- if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
- cinfo->jpeg_color_space == JCS_YCbCr) {
- cconvert->pub.color_convert = grayscale_convert;
- /* For color->grayscale conversion, only the Y (0) component is needed */
- for (ci = 1; ci < cinfo->num_components; ci++)
- cinfo->comp_info[ci].component_needed = FALSE;
- } else
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
-
- case JCS_RGB:
- cinfo->out_color_components = RGB_PIXELSIZE;
- if (cinfo->jpeg_color_space == JCS_YCbCr) {
- cconvert->pub.color_convert = ycc_rgb_convert;
- build_ycc_rgb_table(cinfo);
- } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
- cconvert->pub.color_convert = gray_rgb_convert;
- } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
- cconvert->pub.color_convert = null_convert;
- } else
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
-
- case JCS_CMYK:
- cinfo->out_color_components = 4;
- if (cinfo->jpeg_color_space == JCS_YCCK) {
- cconvert->pub.color_convert = ycck_cmyk_convert;
- build_ycc_rgb_table(cinfo);
- } else if (cinfo->jpeg_color_space == JCS_CMYK) {
- cconvert->pub.color_convert = null_convert;
- } else
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
-
- default:
- /* Permit null conversion to same output space */
- if (cinfo->out_color_space == cinfo->jpeg_color_space) {
- cinfo->out_color_components = cinfo->num_components;
- cconvert->pub.color_convert = null_convert;
- } else /* unsupported non-null conversion */
- ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
- break;
- }
-
- if (cinfo->quantize_colors)
- cinfo->output_components = 1; /* single colormapped output component */
- else
- cinfo->output_components = cinfo->out_color_components;
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdcolor.c
+ *
+ * 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 output colorspace conversion routines.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_color_deconverter pub; /* public fields */
+
+ /* Private state for YCC->RGB conversion */
+ int * Cr_r_tab; /* => table for Cr to R conversion */
+ int * Cb_b_tab; /* => table for Cb to B conversion */
+ INT32 * Cr_g_tab; /* => table for Cr to G conversion */
+ INT32 * Cb_g_tab; /* => table for Cb to G conversion */
+} my_color_deconverter;
+
+typedef my_color_deconverter * my_cconvert_ptr;
+
+
+/**************** YCbCr -> RGB conversion: most common case **************/
+
+/*
+ * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
+ * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
+ * The conversion equations to be implemented are therefore
+ * R = Y + 1.40200 * Cr
+ * G = Y - 0.34414 * Cb - 0.71414 * Cr
+ * B = Y + 1.77200 * Cb
+ * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
+ * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
+ *
+ * To avoid floating-point arithmetic, we represent the fractional constants
+ * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
+ * the products by 2^16, with appropriate rounding, to get the correct answer.
+ * Notice that Y, being an integral input, does not contribute any fraction
+ * so it need not participate in the rounding.
+ *
+ * For even more speed, we avoid doing any multiplications in the inner loop
+ * by precalculating the constants times Cb and Cr for all possible values.
+ * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
+ * for 12-bit samples it is still acceptable. It's not very reasonable for
+ * 16-bit samples, but if you want lossless storage you shouldn't be changing
+ * colorspace anyway.
+ * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
+ * values for the G calculation are left scaled up, since we must add them
+ * together before rounding.
+ */
+
+#define SCALEBITS 16 /* speediest right-shift on some machines */
+#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
+#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
+
+
+/*
+ * Initialize tables for YCC->RGB colorspace conversion.
+ */
+
+LOCAL(void)
+build_ycc_rgb_table (j_decompress_ptr cinfo)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ int i;
+ INT32 x;
+ SHIFT_TEMPS
+
+ cconvert->Cr_r_tab = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(int));
+ cconvert->Cb_b_tab = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(int));
+ cconvert->Cr_g_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(INT32));
+ cconvert->Cb_g_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(INT32));
+
+ for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
+ /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
+ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
+ /* Cr=>R value is nearest int to 1.40200 * x */
+ cconvert->Cr_r_tab[i] = (int)
+ RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
+ /* Cb=>B value is nearest int to 1.77200 * x */
+ cconvert->Cb_b_tab[i] = (int)
+ RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
+ /* Cr=>G value is scaled-up -0.71414 * x */
+ cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
+ /* Cb=>G value is scaled-up -0.34414 * x */
+ /* We also add in ONE_HALF so that need not do it in inner loop */
+ cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
+ }
+}
+
+
+/*
+ * Convert some rows of samples to the output colorspace.
+ *
+ * Note that we change from noninterleaved, one-plane-per-component format
+ * to interleaved-pixel format. The output buffer is therefore three times
+ * as wide as the input buffer.
+ * A starting row offset is provided only for the input buffer. The caller
+ * can easily adjust the passed output_buf value to accommodate any row
+ * offset required on that side.
+ */
+
+METHODDEF(void)
+ycc_rgb_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int y, cb, cr;
+ register JSAMPROW outptr;
+ register JSAMPROW inptr0, inptr1, inptr2;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->output_width;
+ /* copy these pointers into registers if possible */
+ register JSAMPLE * range_limit = cinfo->sample_range_limit;
+ register int * Crrtab = cconvert->Cr_r_tab;
+ register int * Cbbtab = cconvert->Cb_b_tab;
+ register INT32 * Crgtab = cconvert->Cr_g_tab;
+ register INT32 * Cbgtab = cconvert->Cb_g_tab;
+ SHIFT_TEMPS
+
+ while (--num_rows >= 0) {
+ inptr0 = input_buf[0][input_row];
+ inptr1 = input_buf[1][input_row];
+ inptr2 = input_buf[2][input_row];
+ input_row++;
+ outptr = *output_buf++;
+ for (col = 0; col < num_cols; col++) {
+ y = GETJSAMPLE(inptr0[col]);
+ cb = GETJSAMPLE(inptr1[col]);
+ cr = GETJSAMPLE(inptr2[col]);
+ /* Range-limiting is essential due to noise introduced by DCT losses. */
+ outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
+ outptr[RGB_GREEN] = range_limit[y +
+ ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
+ SCALEBITS))];
+ outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
+ outptr += RGB_PIXELSIZE;
+ }
+ }
+}
+
+
+/**************** Cases other than YCbCr -> RGB **************/
+
+
+/*
+ * Color conversion for no colorspace change: just copy the data,
+ * converting from separate-planes to interleaved representation.
+ */
+
+METHODDEF(void)
+null_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ register JSAMPROW inptr, outptr;
+ register JDIMENSION count;
+ register int num_components = cinfo->num_components;
+ JDIMENSION num_cols = cinfo->output_width;
+ int ci;
+
+ while (--num_rows >= 0) {
+ for (ci = 0; ci < num_components; ci++) {
+ inptr = input_buf[ci][input_row];
+ outptr = output_buf[0] + ci;
+ for (count = num_cols; count > 0; count--) {
+ *outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */
+ outptr += num_components;
+ }
+ }
+ input_row++;
+ output_buf++;
+ }
+}
+
+
+/*
+ * Color conversion for grayscale: just copy the data.
+ * This also works for YCbCr -> grayscale conversion, in which
+ * we just copy the Y (luminance) component and ignore chrominance.
+ */
+
+METHODDEF(void)
+grayscale_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
+ num_rows, cinfo->output_width);
+}
+
+
+/*
+ * Convert grayscale to RGB: just duplicate the graylevel three times.
+ * This is provided to support applications that don't want to cope
+ * with grayscale as a separate case.
+ */
+
+METHODDEF(void)
+gray_rgb_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ register JSAMPROW inptr, outptr;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->output_width;
+
+ while (--num_rows >= 0) {
+ inptr = input_buf[0][input_row++];
+ outptr = *output_buf++;
+ for (col = 0; col < num_cols; col++) {
+ /* We can dispense with GETJSAMPLE() here */
+ outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
+ outptr += RGB_PIXELSIZE;
+ }
+ }
+}
+
+
+/*
+ * Adobe-style YCCK->CMYK conversion.
+ * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
+ * conversion as above, while passing K (black) unchanged.
+ * We assume build_ycc_rgb_table has been called.
+ */
+
+METHODDEF(void)
+ycck_cmyk_convert (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows)
+{
+ my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
+ register int y, cb, cr;
+ register JSAMPROW outptr;
+ register JSAMPROW inptr0, inptr1, inptr2, inptr3;
+ register JDIMENSION col;
+ JDIMENSION num_cols = cinfo->output_width;
+ /* copy these pointers into registers if possible */
+ register JSAMPLE * range_limit = cinfo->sample_range_limit;
+ register int * Crrtab = cconvert->Cr_r_tab;
+ register int * Cbbtab = cconvert->Cb_b_tab;
+ register INT32 * Crgtab = cconvert->Cr_g_tab;
+ register INT32 * Cbgtab = cconvert->Cb_g_tab;
+ SHIFT_TEMPS
+
+ while (--num_rows >= 0) {
+ inptr0 = input_buf[0][input_row];
+ inptr1 = input_buf[1][input_row];
+ inptr2 = input_buf[2][input_row];
+ inptr3 = input_buf[3][input_row];
+ input_row++;
+ outptr = *output_buf++;
+ for (col = 0; col < num_cols; col++) {
+ y = GETJSAMPLE(inptr0[col]);
+ cb = GETJSAMPLE(inptr1[col]);
+ cr = GETJSAMPLE(inptr2[col]);
+ /* Range-limiting is essential due to noise introduced by DCT losses. */
+ outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])]; /* red */
+ outptr[1] = range_limit[MAXJSAMPLE - (y + /* green */
+ ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
+ SCALEBITS)))];
+ outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])]; /* blue */
+ /* K passes through unchanged */
+ outptr[3] = inptr3[col]; /* don't need GETJSAMPLE here */
+ outptr += 4;
+ }
+ }
+}
+
+
+/*
+ * Empty method for start_pass.
+ */
+
+METHODDEF(void)
+start_pass_dcolor (j_decompress_ptr cinfo)
+{
+ /* no work needed */
+}
+
+
+/*
+ * Module initialization routine for output colorspace conversion.
+ */
+
+GLOBAL(void)
+jinit_color_deconverter (j_decompress_ptr cinfo)
+{
+ my_cconvert_ptr cconvert;
+ int ci;
+
+ cconvert = (my_cconvert_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_color_deconverter));
+ cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
+ cconvert->pub.start_pass = start_pass_dcolor;
+
+ /* Make sure num_components agrees with jpeg_color_space */
+ switch (cinfo->jpeg_color_space) {
+ case JCS_GRAYSCALE:
+ if (cinfo->num_components != 1)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ break;
+
+ case JCS_RGB:
+ case JCS_YCbCr:
+ if (cinfo->num_components != 3)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ break;
+
+ case JCS_CMYK:
+ case JCS_YCCK:
+ if (cinfo->num_components != 4)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ break;
+
+ default: /* JCS_UNKNOWN can be anything */
+ if (cinfo->num_components < 1)
+ ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
+ break;
+ }
+
+ /* Set out_color_components and conversion method based on requested space.
+ * Also clear the component_needed flags for any unused components,
+ * so that earlier pipeline stages can avoid useless computation.
+ */
+
+ switch (cinfo->out_color_space) {
+ case JCS_GRAYSCALE:
+ cinfo->out_color_components = 1;
+ if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
+ cinfo->jpeg_color_space == JCS_YCbCr) {
+ cconvert->pub.color_convert = grayscale_convert;
+ /* For color->grayscale conversion, only the Y (0) component is needed */
+ for (ci = 1; ci < cinfo->num_components; ci++)
+ cinfo->comp_info[ci].component_needed = FALSE;
+ } else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_RGB:
+ cinfo->out_color_components = RGB_PIXELSIZE;
+ if (cinfo->jpeg_color_space == JCS_YCbCr) {
+ cconvert->pub.color_convert = ycc_rgb_convert;
+ build_ycc_rgb_table(cinfo);
+ } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
+ cconvert->pub.color_convert = gray_rgb_convert;
+ } else if (cinfo->jpeg_color_space == JCS_RGB && RGB_PIXELSIZE == 3) {
+ cconvert->pub.color_convert = null_convert;
+ } else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ case JCS_CMYK:
+ cinfo->out_color_components = 4;
+ if (cinfo->jpeg_color_space == JCS_YCCK) {
+ cconvert->pub.color_convert = ycck_cmyk_convert;
+ build_ycc_rgb_table(cinfo);
+ } else if (cinfo->jpeg_color_space == JCS_CMYK) {
+ cconvert->pub.color_convert = null_convert;
+ } else
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+
+ default:
+ /* Permit null conversion to same output space */
+ if (cinfo->out_color_space == cinfo->jpeg_color_space) {
+ cinfo->out_color_components = cinfo->num_components;
+ cconvert->pub.color_convert = null_convert;
+ } else /* unsupported non-null conversion */
+ ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
+ break;
+ }
+
+ if (cinfo->quantize_colors)
+ cinfo->output_components = 1; /* single colormapped output component */
+ else
+ cinfo->output_components = cinfo->out_color_components;
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c b/core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c
index 811d51d51a..5226456414 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jddctmgr.c
@@ -1,272 +1,272 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jddctmgr.c
- *
- * Copyright (C) 1994-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 contains the inverse-DCT management logic.
- * This code selects a particular IDCT implementation to be used,
- * and it performs related housekeeping chores. No code in this file
- * is executed per IDCT step, only during output pass setup.
- *
- * Note that the IDCT routines are responsible for performing coefficient
- * dequantization as well as the IDCT proper. This module sets up the
- * dequantization multiplier table needed by the IDCT routine.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jdct.h" /* Private declarations for DCT subsystem */
-
-
-/*
- * The decompressor input side (jdinput.c) saves away the appropriate
- * quantization table for each component at the start of the first scan
- * involving that component. (This is necessary in order to correctly
- * decode files that reuse Q-table slots.)
- * When we are ready to make an output pass, the saved Q-table is converted
- * to a multiplier table that will actually be used by the IDCT routine.
- * The multiplier table contents are IDCT-method-dependent. To support
- * application changes in IDCT method between scans, we can remake the
- * multiplier tables if necessary.
- * In buffered-image mode, the first output pass may occur before any data
- * has been seen for some components, and thus before their Q-tables have
- * been saved away. To handle this case, multiplier tables are preset
- * to zeroes; the result of the IDCT will be a neutral gray level.
- */
-
-
-/* Private subobject for this module */
-
-typedef struct {
- struct jpeg_inverse_dct pub; /* public fields */
-
- /* This array contains the IDCT method code that each multiplier table
- * is currently set up for, or -1 if it's not yet set up.
- * The actual multiplier tables are pointed to by dct_table in the
- * per-component comp_info structures.
- */
- int cur_method[MAX_COMPONENTS];
-} my_idct_controller;
-
-typedef my_idct_controller * my_idct_ptr;
-
-
-/* Allocated multiplier tables: big enough for any supported variant */
-
-typedef union {
- ISLOW_MULT_TYPE islow_array[DCTSIZE2];
-#ifdef DCT_IFAST_SUPPORTED
- IFAST_MULT_TYPE ifast_array[DCTSIZE2];
-#endif
-#ifdef DCT_FLOAT_SUPPORTED
- FLOAT_MULT_TYPE float_array[DCTSIZE2];
-#endif
-} multiplier_table;
-
-
-/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
- * so be sure to compile that code if either ISLOW or SCALING is requested.
- */
-#ifdef DCT_ISLOW_SUPPORTED
-#define PROVIDE_ISLOW_TABLES
-#else
-#ifdef IDCT_SCALING_SUPPORTED
-#define PROVIDE_ISLOW_TABLES
-#endif
-#endif
-
-
-/*
- * Prepare for an output pass.
- * Here we select the proper IDCT routine for each component and build
- * a matching multiplier table.
- */
-
-METHODDEF(void)
-start_pass (j_decompress_ptr cinfo)
-{
- my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
- int ci, i;
- jpeg_component_info *compptr;
- int method = 0;
- inverse_DCT_method_ptr method_ptr = NULL;
- JQUANT_TBL * qtbl;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Select the proper IDCT routine for this component's scaling */
- switch (compptr->DCT_scaled_size) {
-#ifdef IDCT_SCALING_SUPPORTED
- case 1:
- method_ptr = jpeg_idct_1x1;
- method = JDCT_ISLOW; /* jidctred uses islow-style table */
- break;
- case 2:
- method_ptr = jpeg_idct_2x2;
- method = JDCT_ISLOW; /* jidctred uses islow-style table */
- break;
- case 4:
- method_ptr = jpeg_idct_4x4;
- method = JDCT_ISLOW; /* jidctred uses islow-style table */
- break;
-#endif
- case DCTSIZE:
- switch (cinfo->dct_method) {
-#ifdef DCT_ISLOW_SUPPORTED
- case JDCT_ISLOW:
- method_ptr = jpeg_idct_islow;
- method = JDCT_ISLOW;
- break;
-#endif
-#ifdef DCT_IFAST_SUPPORTED
- case JDCT_IFAST:
- method_ptr = jpeg_idct_ifast;
- method = JDCT_IFAST;
- break;
-#endif
-#ifdef DCT_FLOAT_SUPPORTED
- case JDCT_FLOAT:
- method_ptr = jpeg_idct_float;
- method = JDCT_FLOAT;
- break;
-#endif
- default:
- ERREXIT(cinfo, JERR_NOT_COMPILED);
- break;
- }
- break;
- default:
- ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
- break;
- }
- idct->pub.inverse_DCT[ci] = method_ptr;
- /* Create multiplier table from quant table.
- * However, we can skip this if the component is uninteresting
- * or if we already built the table. Also, if no quant table
- * has yet been saved for the component, we leave the
- * multiplier table all-zero; we'll be reading zeroes from the
- * coefficient controller's buffer anyway.
- */
- if (! compptr->component_needed || idct->cur_method[ci] == method)
- continue;
- qtbl = compptr->quant_table;
- if (qtbl == NULL) /* happens if no data yet for component */
- continue;
- idct->cur_method[ci] = method;
- switch (method) {
-#ifdef PROVIDE_ISLOW_TABLES
- case JDCT_ISLOW:
- {
- /* For LL&M IDCT method, multipliers are equal to raw quantization
- * coefficients, but are stored as ints to ensure access efficiency.
- */
- ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
- for (i = 0; i < DCTSIZE2; i++) {
- ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
- }
- }
- break;
-#endif
-#ifdef DCT_IFAST_SUPPORTED
- case JDCT_IFAST:
- {
- /* For AA&N IDCT method, multipliers are equal to quantization
- * coefficients scaled by scalefactor[row]*scalefactor[col], where
- * scalefactor[0] = 1
- * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
- * For integer operation, the multiplier table is to be scaled by
- * IFAST_SCALE_BITS.
- */
- IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
-#define CONST_BITS 14
- static const INT16 aanscales[DCTSIZE2] = {
- /* precomputed values scaled up by 14 bits */
- 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
- 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
- 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
- 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
- 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
- 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
- 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
- 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
- };
- SHIFT_TEMPS
-
- for (i = 0; i < DCTSIZE2; i++) {
- ifmtbl[i] = (IFAST_MULT_TYPE)
- DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
- (INT32) aanscales[i]),
- CONST_BITS-IFAST_SCALE_BITS);
- }
- }
- break;
-#endif
-#ifdef DCT_FLOAT_SUPPORTED
- case JDCT_FLOAT:
- {
- /* For float AA&N IDCT method, multipliers are equal to quantization
- * coefficients scaled by scalefactor[row]*scalefactor[col], where
- * scalefactor[0] = 1
- * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
- */
- FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
- int row, col;
- static const double aanscalefactor[DCTSIZE] = {
- 1.0, 1.387039845, 1.306562965, 1.175875602,
- 1.0, 0.785694958, 0.541196100, 0.275899379
- };
-
- i = 0;
- for (row = 0; row < DCTSIZE; row++) {
- for (col = 0; col < DCTSIZE; col++) {
- fmtbl[i] = (FLOAT_MULT_TYPE)
- ((double) qtbl->quantval[i] *
- aanscalefactor[row] * aanscalefactor[col]);
- i++;
- }
- }
- }
- break;
-#endif
- default:
- ERREXIT(cinfo, JERR_NOT_COMPILED);
- break;
- }
- }
-}
-
-
-/*
- * Initialize IDCT manager.
- */
-
-GLOBAL(void)
-jinit_inverse_dct (j_decompress_ptr cinfo)
-{
- my_idct_ptr idct;
- int ci;
- jpeg_component_info *compptr;
-
- idct = (my_idct_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_idct_controller));
- cinfo->idct = (struct jpeg_inverse_dct *) idct;
- idct->pub.start_pass = start_pass;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Allocate and pre-zero a multiplier table for each component */
- compptr->dct_table =
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(multiplier_table));
- MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
- /* Mark multiplier table not yet set up for any method */
- idct->cur_method[ci] = -1;
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jddctmgr.c
+ *
+ * Copyright (C) 1994-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 contains the inverse-DCT management logic.
+ * This code selects a particular IDCT implementation to be used,
+ * and it performs related housekeeping chores. No code in this file
+ * is executed per IDCT step, only during output pass setup.
+ *
+ * Note that the IDCT routines are responsible for performing coefficient
+ * dequantization as well as the IDCT proper. This module sets up the
+ * dequantization multiplier table needed by the IDCT routine.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+
+/*
+ * The decompressor input side (jdinput.c) saves away the appropriate
+ * quantization table for each component at the start of the first scan
+ * involving that component. (This is necessary in order to correctly
+ * decode files that reuse Q-table slots.)
+ * When we are ready to make an output pass, the saved Q-table is converted
+ * to a multiplier table that will actually be used by the IDCT routine.
+ * The multiplier table contents are IDCT-method-dependent. To support
+ * application changes in IDCT method between scans, we can remake the
+ * multiplier tables if necessary.
+ * In buffered-image mode, the first output pass may occur before any data
+ * has been seen for some components, and thus before their Q-tables have
+ * been saved away. To handle this case, multiplier tables are preset
+ * to zeroes; the result of the IDCT will be a neutral gray level.
+ */
+
+
+/* Private subobject for this module */
+
+typedef struct {
+ struct jpeg_inverse_dct pub; /* public fields */
+
+ /* This array contains the IDCT method code that each multiplier table
+ * is currently set up for, or -1 if it's not yet set up.
+ * The actual multiplier tables are pointed to by dct_table in the
+ * per-component comp_info structures.
+ */
+ int cur_method[MAX_COMPONENTS];
+} my_idct_controller;
+
+typedef my_idct_controller * my_idct_ptr;
+
+
+/* Allocated multiplier tables: big enough for any supported variant */
+
+typedef union {
+ ISLOW_MULT_TYPE islow_array[DCTSIZE2];
+#ifdef DCT_IFAST_SUPPORTED
+ IFAST_MULT_TYPE ifast_array[DCTSIZE2];
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ FLOAT_MULT_TYPE float_array[DCTSIZE2];
+#endif
+} multiplier_table;
+
+
+/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
+ * so be sure to compile that code if either ISLOW or SCALING is requested.
+ */
+#ifdef DCT_ISLOW_SUPPORTED
+#define PROVIDE_ISLOW_TABLES
+#else
+#ifdef IDCT_SCALING_SUPPORTED
+#define PROVIDE_ISLOW_TABLES
+#endif
+#endif
+
+
+/*
+ * Prepare for an output pass.
+ * Here we select the proper IDCT routine for each component and build
+ * a matching multiplier table.
+ */
+
+METHODDEF(void)
+start_pass (j_decompress_ptr cinfo)
+{
+ my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
+ int ci, i;
+ jpeg_component_info *compptr;
+ int method = 0;
+ inverse_DCT_method_ptr method_ptr = NULL;
+ JQUANT_TBL * qtbl;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Select the proper IDCT routine for this component's scaling */
+ switch (compptr->DCT_scaled_size) {
+#ifdef IDCT_SCALING_SUPPORTED
+ case 1:
+ method_ptr = jpeg_idct_1x1;
+ method = JDCT_ISLOW; /* jidctred uses islow-style table */
+ break;
+ case 2:
+ method_ptr = jpeg_idct_2x2;
+ method = JDCT_ISLOW; /* jidctred uses islow-style table */
+ break;
+ case 4:
+ method_ptr = jpeg_idct_4x4;
+ method = JDCT_ISLOW; /* jidctred uses islow-style table */
+ break;
+#endif
+ case DCTSIZE:
+ switch (cinfo->dct_method) {
+#ifdef DCT_ISLOW_SUPPORTED
+ case JDCT_ISLOW:
+ method_ptr = jpeg_idct_islow;
+ method = JDCT_ISLOW;
+ break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ case JDCT_IFAST:
+ method_ptr = jpeg_idct_ifast;
+ method = JDCT_IFAST;
+ break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ case JDCT_FLOAT:
+ method_ptr = jpeg_idct_float;
+ method = JDCT_FLOAT;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+ break;
+ default:
+ ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
+ break;
+ }
+ idct->pub.inverse_DCT[ci] = method_ptr;
+ /* Create multiplier table from quant table.
+ * However, we can skip this if the component is uninteresting
+ * or if we already built the table. Also, if no quant table
+ * has yet been saved for the component, we leave the
+ * multiplier table all-zero; we'll be reading zeroes from the
+ * coefficient controller's buffer anyway.
+ */
+ if (! compptr->component_needed || idct->cur_method[ci] == method)
+ continue;
+ qtbl = compptr->quant_table;
+ if (qtbl == NULL) /* happens if no data yet for component */
+ continue;
+ idct->cur_method[ci] = method;
+ switch (method) {
+#ifdef PROVIDE_ISLOW_TABLES
+ case JDCT_ISLOW:
+ {
+ /* For LL&M IDCT method, multipliers are equal to raw quantization
+ * coefficients, but are stored as ints to ensure access efficiency.
+ */
+ ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
+ for (i = 0; i < DCTSIZE2; i++) {
+ ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
+ }
+ }
+ break;
+#endif
+#ifdef DCT_IFAST_SUPPORTED
+ case JDCT_IFAST:
+ {
+ /* For AA&N IDCT method, multipliers are equal to quantization
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
+ * scalefactor[0] = 1
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
+ * For integer operation, the multiplier table is to be scaled by
+ * IFAST_SCALE_BITS.
+ */
+ IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
+#define CONST_BITS 14
+ static const INT16 aanscales[DCTSIZE2] = {
+ /* precomputed values scaled up by 14 bits */
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
+ 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
+ 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
+ 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
+ 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
+ 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
+ 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
+ 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
+ };
+ SHIFT_TEMPS
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ ifmtbl[i] = (IFAST_MULT_TYPE)
+ DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
+ (INT32) aanscales[i]),
+ CONST_BITS-IFAST_SCALE_BITS);
+ }
+ }
+ break;
+#endif
+#ifdef DCT_FLOAT_SUPPORTED
+ case JDCT_FLOAT:
+ {
+ /* For float AA&N IDCT method, multipliers are equal to quantization
+ * coefficients scaled by scalefactor[row]*scalefactor[col], where
+ * scalefactor[0] = 1
+ * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
+ */
+ FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
+ int row, col;
+ static const double aanscalefactor[DCTSIZE] = {
+ 1.0, 1.387039845, 1.306562965, 1.175875602,
+ 1.0, 0.785694958, 0.541196100, 0.275899379
+ };
+
+ i = 0;
+ for (row = 0; row < DCTSIZE; row++) {
+ for (col = 0; col < DCTSIZE; col++) {
+ fmtbl[i] = (FLOAT_MULT_TYPE)
+ ((double) qtbl->quantval[i] *
+ aanscalefactor[row] * aanscalefactor[col]);
+ i++;
+ }
+ }
+ }
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+ break;
+ }
+ }
+}
+
+
+/*
+ * Initialize IDCT manager.
+ */
+
+GLOBAL(void)
+jinit_inverse_dct (j_decompress_ptr cinfo)
+{
+ my_idct_ptr idct;
+ int ci;
+ jpeg_component_info *compptr;
+
+ idct = (my_idct_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_idct_controller));
+ cinfo->idct = (struct jpeg_inverse_dct *) idct;
+ idct->pub.start_pass = start_pass;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Allocate and pre-zero a multiplier table for each component */
+ compptr->dct_table =
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(multiplier_table));
+ MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
+ /* Mark multiplier table not yet set up for any method */
+ idct->cur_method[ci] = -1;
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c b/core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c
index b724d52b95..dc37db58a9 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdhuff.c
@@ -1,657 +1,657 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdhuff.c
- *
- * 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 Huffman entropy decoding routines.
- *
- * 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 jdphuff.c */
-
-#ifdef _FX_MANAGED_CODE_
-#define savable_state savable_state_d
-#endif
-
-/*
- * Expanded entropy decoder object for 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 {
- 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).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 * dc_derived_tbls[NUM_HUFF_TBLS];
- d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
-
- /* Precalculated info set up by start_pass for use in decode_mcu: */
-
- /* Pointers to derived tables to be used for each block within an MCU */
- d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
- d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
- /* Whether we care about the DC and AC coefficient values for each block */
- boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
- boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
-} huff_entropy_decoder;
-
-typedef huff_entropy_decoder * huff_entropy_ptr;
-
-
-/*
- * Initialize for a Huffman-compressed scan.
- */
-
-METHODDEF(void)
-start_pass_huff_decoder (j_decompress_ptr cinfo)
-{
- huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
- int ci, blkn, dctbl, actbl;
- jpeg_component_info * compptr;
-
- /* 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 because
- * there are some baseline files out there with all zeroes in these bytes.
- */
- if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
- cinfo->Ah != 0 || cinfo->Al != 0)
- WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- dctbl = compptr->dc_tbl_no;
- actbl = compptr->ac_tbl_no;
- /* Compute derived values for Huffman tables */
- /* We may do this more than once for a table, but it's not expensive */
- jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
- & entropy->dc_derived_tbls[dctbl]);
- jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
- & entropy->ac_derived_tbls[actbl]);
- /* Initialize DC predictions to 0 */
- entropy->saved.last_dc_val[ci] = 0;
- }
-
- /* Precalculate decoding info for each block in an MCU of this scan */
- for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
- ci = cinfo->MCU_membership[blkn];
- compptr = cinfo->cur_comp_info[ci];
- /* Precalculate which table to use for each block */
- entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
- entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
- /* Decide whether we really care about the coefficient values */
- if (compptr->component_needed) {
- entropy->dc_needed[blkn] = TRUE;
- /* we don't need the ACs if producing a 1/8th-size image */
- entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
- } else {
- entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
- }
- }
-
- /* 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 restart counter */
- entropy->restarts_to_go = cinfo->restart_interval;
-}
-
-
-/*
- * Compute the derived values for a Huffman table.
- * This routine also performs some validation checks on the table.
- *
- * Note this is also used by jdphuff.c.
- */
-
-GLOBAL(void)
-jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
- d_derived_tbl ** pdtbl)
-{
- JHUFF_TBL *htbl;
- d_derived_tbl *dtbl;
- int p, i, l, _si, numsymbols;
- int lookbits, ctr;
- char huffsize[257];
- unsigned int huffcode[257];
- unsigned int code;
-
- /* Note that huffsize[] and huffcode[] are filled in code-length order,
- * paralleling the order of the symbols themselves in htbl->huffval[].
- */
-
- /* Find the input Huffman table */
- if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
- ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
- htbl =
- isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
- if (htbl == NULL)
- ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
-
- /* Allocate a workspace if we haven't already done so. */
- if (*pdtbl == NULL)
- *pdtbl = (d_derived_tbl *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(d_derived_tbl));
- dtbl = *pdtbl;
- dtbl->pub = htbl; /* fill in back link */
-
- /* Figure C.1: make table of Huffman code length for each symbol */
-
- p = 0;
- for (l = 1; l <= 16; l++) {
- i = (int) htbl->bits[l];
- if (i < 0 || p + i > 256) /* protect against table overrun */
- ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
- while (i--)
- huffsize[p++] = (char) l;
- }
- huffsize[p] = 0;
- numsymbols = p;
-
- /* Figure C.2: generate the codes themselves */
- /* We also validate that the counts represent a legal Huffman code tree. */
-
- code = 0;
- _si = huffsize[0];
- p = 0;
- while (huffsize[p]) {
- while (((int) huffsize[p]) == _si) {
- huffcode[p++] = code;
- code++;
- }
- /* code is now 1 more than the last code used for codelength si; but
- * it must still fit in si bits, since no code is allowed to be all ones.
- */
- if (((INT32) code) >= (((INT32) 1) << _si))
- ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
- code <<= 1;
- _si++;
- }
-
- /* Figure F.15: generate decoding tables for bit-sequential decoding */
-
- p = 0;
- for (l = 1; l <= 16; l++) {
- if (htbl->bits[l]) {
- /* valoffset[l] = huffval[] index of 1st symbol of code length l,
- * minus the minimum code of length l
- */
- dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
- p += htbl->bits[l];
- dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
- } else {
- dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
- }
- }
- dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
-
- /* Compute lookahead tables to speed up decoding.
- * First we set all the table entries to 0, indicating "too long";
- * then we iterate through the Huffman codes that are short enough and
- * fill in all the entries that correspond to bit sequences starting
- * with that code.
- */
-
- MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
-
- p = 0;
- for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
- for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
- /* l = current code's length, p = its index in huffcode[] & huffval[]. */
- /* Generate left-justified code followed by all possible bit sequences */
- lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
- for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
- dtbl->look_nbits[lookbits] = l;
- dtbl->look_sym[lookbits] = htbl->huffval[p];
- lookbits++;
- }
- }
- }
-
- /* Validate symbols as being reasonable.
- * For AC tables, we make no check, but accept all byte values 0..255.
- * For DC tables, we require the symbols to be in range 0..15.
- * (Tighter bounds could be applied depending on the data depth and mode,
- * but this is sufficient to ensure safe decoding.)
- */
- if (isDC) {
- for (i = 0; i < numsymbols; i++) {
- int sym = htbl->huffval[i];
- if (sym < 0 || sym > 15)
- ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
- }
- }
-}
-
-
-/*
- * Out-of-line code for bit fetching (shared with jdphuff.c).
- * See jdhuff.h for info about usage.
- * Note: current values of get_buffer and bits_left are passed as parameters,
- * but are returned in the corresponding fields of the state struct.
- *
- * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
- * of get_buffer to be used. (On machines with wider words, an even larger
- * buffer could be used.) However, on some machines 32-bit shifts are
- * quite slow and take time proportional to the number of places shifted.
- * (This is true with most PC compilers, for instance.) In this case it may
- * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
- * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
- */
-
-#ifdef SLOW_SHIFT_32
-#define MIN_GET_BITS 15 /* minimum allowable value */
-#else
-#define MIN_GET_BITS (BIT_BUF_SIZE-7)
-#endif
-
-
-GLOBAL(boolean)
-jpeg_fill_bit_buffer (bitread_working_state * state,
- register bit_buf_type get_buffer, register int bits_left,
- int nbits)
-/* Load up the bit buffer to a depth of at least nbits */
-{
- /* Copy heavily used state fields into locals (hopefully registers) */
- register const JOCTET * next_input_byte = state->next_input_byte;
- register size_t bytes_in_buffer = state->bytes_in_buffer;
- j_decompress_ptr cinfo = state->cinfo;
-
- /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
- /* (It is assumed that no request will be for more than that many bits.) */
- /* We fail to do so only if we hit a marker or are forced to suspend. */
-
- if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
- while (bits_left < MIN_GET_BITS) {
- register int c;
-
- /* Attempt to read a byte */
- if (bytes_in_buffer == 0) {
- if (! (*cinfo->src->fill_input_buffer) (cinfo))
- return FALSE;
- next_input_byte = cinfo->src->next_input_byte;
- bytes_in_buffer = cinfo->src->bytes_in_buffer;
- }
- bytes_in_buffer--;
- c = GETJOCTET(*next_input_byte++);
-
- /* If it's 0xFF, check and discard stuffed zero byte */
- if (c == 0xFF) {
- /* Loop here to discard any padding FF's on terminating marker,
- * so that we can save a valid unread_marker value. NOTE: we will
- * accept multiple FF's followed by a 0 as meaning a single FF data
- * byte. This data pattern is not valid according to the standard.
- */
- do {
- if (bytes_in_buffer == 0) {
- if (! (*cinfo->src->fill_input_buffer) (cinfo))
- return FALSE;
- next_input_byte = cinfo->src->next_input_byte;
- bytes_in_buffer = cinfo->src->bytes_in_buffer;
- }
- bytes_in_buffer--;
- c = GETJOCTET(*next_input_byte++);
- } while (c == 0xFF);
-
- if (c == 0) {
- /* Found FF/00, which represents an FF data byte */
- c = 0xFF;
- } else {
- /* Oops, it's actually a marker indicating end of compressed data.
- * Save the marker code for later use.
- * Fine point: it might appear that we should save the marker into
- * bitread working state, not straight into permanent state. But
- * once we have hit a marker, we cannot need to suspend within the
- * current MCU, because we will read no more bytes from the data
- * source. So it is OK to update permanent state right away.
- */
- cinfo->unread_marker = c;
- /* See if we need to insert some fake zero bits. */
- goto no_more_bytes;
- }
- }
-
- /* OK, load c into get_buffer */
- get_buffer = (get_buffer << 8) | c;
- bits_left += 8;
- } /* end while */
- } else {
- no_more_bytes:
- /* We get here if we've read the marker that terminates the compressed
- * data segment. There should be enough bits in the buffer register
- * to satisfy the request; if so, no problem.
- */
- if (nbits > bits_left) {
- /* Uh-oh. Report corrupted data to user and stuff zeroes into
- * the data stream, so that we can produce some kind of image.
- * We use a nonvolatile flag to ensure that only one warning message
- * appears per data segment.
- */
- if (! cinfo->entropy->insufficient_data) {
- WARNMS(cinfo, JWRN_HIT_MARKER);
- cinfo->entropy->insufficient_data = TRUE;
- }
- /* Fill the buffer with zero bits */
- get_buffer <<= MIN_GET_BITS - bits_left;
- bits_left = MIN_GET_BITS;
- }
- }
-
- /* Unload the local registers */
- state->next_input_byte = next_input_byte;
- state->bytes_in_buffer = bytes_in_buffer;
- state->get_buffer = get_buffer;
- state->bits_left = bits_left;
-
- return TRUE;
-}
-
-
-/*
- * Out-of-line code for Huffman code decoding.
- * See jdhuff.h for info about usage.
- */
-
-GLOBAL(int)
-jpeg_huff_decode (bitread_working_state * state,
- register bit_buf_type get_buffer, register int bits_left,
- d_derived_tbl * htbl, int min_bits)
-{
- register int l = min_bits;
- register INT32 code;
-
- /* HUFF_DECODE has determined that the code is at least min_bits */
- /* bits long, so fetch that many bits in one swoop. */
-
- CHECK_BIT_BUFFER(*state, l, return -1);
- code = GET_BITS(l);
-
- /* Collect the rest of the Huffman code one bit at a time. */
- /* This is per Figure F.16 in the JPEG spec. */
-
- while (code > htbl->maxcode[l]) {
- code <<= 1;
- CHECK_BIT_BUFFER(*state, 1, return -1);
- code |= GET_BITS(1);
- l++;
- }
-
- /* Unload the local registers */
- state->get_buffer = get_buffer;
- state->bits_left = bits_left;
-
- /* With garbage input we may reach the sentinel value l = 17. */
-
- if (l > 16) {
- WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
- return 0; /* fake a zero as the safest result */
- }
-
- return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
-}
-
-
-/*
- * 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)
-{
- huff_entropy_ptr entropy = (huff_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;
-
- /* 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;
-}
-
-
-/*
- * Decode and return 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 HAS BEEN ZEROED BY THE CALLER.
- * (Wholesale zeroing is usually a little faster than retail...)
- *
- * Returns 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
- * this module, since we'll just re-assign them on the next call.)
- */
-
-METHODDEF(boolean)
-decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
-{
- huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
- int blkn;
- BITREAD_STATE_VARS;
- savable_state state;
-
- /* 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++) {
- JBLOCKROW block = MCU_data[blkn];
- d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
- d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
- register int s, k, r;
-
- /* Decode a single block's worth of coefficients */
-
- /* Section F.2.2.1: decode the DC coefficient difference */
- HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
- if (s) {
- CHECK_BIT_BUFFER(br_state, s, return FALSE);
- r = GET_BITS(s);
- s = HUFF_EXTEND(r, s);
- }
-
- if (entropy->dc_needed[blkn]) {
- /* Convert DC difference to actual value, update last_dc_val */
- int ci = cinfo->MCU_membership[blkn];
- s += state.last_dc_val[ci];
- state.last_dc_val[ci] = s;
- /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
- (*block)[0] = (JCOEF) s;
- }
-
- if (entropy->ac_needed[blkn]) {
-
- /* Section F.2.2.2: decode the AC coefficients */
- /* Since zeroes are skipped, output area must be cleared beforehand */
- for (k = 1; k < DCTSIZE2; k++) {
- HUFF_DECODE(s, br_state, actbl, 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);
- /* Output coefficient in natural (dezigzagged) order.
- * Note: the extra entries in jpeg_natural_order[] will save us
- * if k >= DCTSIZE2, which could happen if the data is corrupted.
- */
- (*block)[jpeg_natural_order[k]] = (JCOEF) s;
- } else {
- if (r != 15)
- break;
- k += 15;
- }
- }
-
- } else {
-
- /* Section F.2.2.2: decode the AC coefficients */
- /* In this path we just discard the values */
- for (k = 1; k < DCTSIZE2; k++) {
- HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
-
- r = s >> 4;
- s &= 15;
-
- if (s) {
- k += r;
- CHECK_BIT_BUFFER(br_state, s, return FALSE);
- DROP_BITS(s);
- } else {
- if (r != 15)
- break;
- k += 15;
- }
- }
-
- }
- }
-
- /* 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;
-}
-
-
-/*
- * Module initialization routine for Huffman entropy decoding.
- */
-
-GLOBAL(void)
-jinit_huff_decoder (j_decompress_ptr cinfo)
-{
- huff_entropy_ptr entropy;
- int i;
-
- entropy = (huff_entropy_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(huff_entropy_decoder));
- cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
- entropy->pub.start_pass = start_pass_huff_decoder;
- entropy->pub.decode_mcu = decode_mcu;
-
- /* Mark tables unallocated */
- for (i = 0; i < NUM_HUFF_TBLS; i++) {
- entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdhuff.c
+ *
+ * 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 Huffman entropy decoding routines.
+ *
+ * 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 jdphuff.c */
+
+#ifdef _FX_MANAGED_CODE_
+#define savable_state savable_state_d
+#endif
+
+/*
+ * Expanded entropy decoder object for 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 {
+ 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).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 * dc_derived_tbls[NUM_HUFF_TBLS];
+ d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
+
+ /* Precalculated info set up by start_pass for use in decode_mcu: */
+
+ /* Pointers to derived tables to be used for each block within an MCU */
+ d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
+ d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
+ /* Whether we care about the DC and AC coefficient values for each block */
+ boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
+ boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
+} huff_entropy_decoder;
+
+typedef huff_entropy_decoder * huff_entropy_ptr;
+
+
+/*
+ * Initialize for a Huffman-compressed scan.
+ */
+
+METHODDEF(void)
+start_pass_huff_decoder (j_decompress_ptr cinfo)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci, blkn, dctbl, actbl;
+ jpeg_component_info * compptr;
+
+ /* 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 because
+ * there are some baseline files out there with all zeroes in these bytes.
+ */
+ if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
+ cinfo->Ah != 0 || cinfo->Al != 0)
+ WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ dctbl = compptr->dc_tbl_no;
+ actbl = compptr->ac_tbl_no;
+ /* Compute derived values for Huffman tables */
+ /* We may do this more than once for a table, but it's not expensive */
+ jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
+ & entropy->dc_derived_tbls[dctbl]);
+ jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
+ & entropy->ac_derived_tbls[actbl]);
+ /* Initialize DC predictions to 0 */
+ entropy->saved.last_dc_val[ci] = 0;
+ }
+
+ /* Precalculate decoding info for each block in an MCU of this scan */
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
+ ci = cinfo->MCU_membership[blkn];
+ compptr = cinfo->cur_comp_info[ci];
+ /* Precalculate which table to use for each block */
+ entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
+ entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
+ /* Decide whether we really care about the coefficient values */
+ if (compptr->component_needed) {
+ entropy->dc_needed[blkn] = TRUE;
+ /* we don't need the ACs if producing a 1/8th-size image */
+ entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
+ } else {
+ entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
+ }
+ }
+
+ /* 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 restart counter */
+ entropy->restarts_to_go = cinfo->restart_interval;
+}
+
+
+/*
+ * Compute the derived values for a Huffman table.
+ * This routine also performs some validation checks on the table.
+ *
+ * Note this is also used by jdphuff.c.
+ */
+
+GLOBAL(void)
+jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
+ d_derived_tbl ** pdtbl)
+{
+ JHUFF_TBL *htbl;
+ d_derived_tbl *dtbl;
+ int p, i, l, _si, numsymbols;
+ int lookbits, ctr;
+ char huffsize[257];
+ unsigned int huffcode[257];
+ unsigned int code;
+
+ /* Note that huffsize[] and huffcode[] are filled in code-length order,
+ * paralleling the order of the symbols themselves in htbl->huffval[].
+ */
+
+ /* Find the input Huffman table */
+ if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+ htbl =
+ isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
+ if (htbl == NULL)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
+
+ /* Allocate a workspace if we haven't already done so. */
+ if (*pdtbl == NULL)
+ *pdtbl = (d_derived_tbl *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(d_derived_tbl));
+ dtbl = *pdtbl;
+ dtbl->pub = htbl; /* fill in back link */
+
+ /* Figure C.1: make table of Huffman code length for each symbol */
+
+ p = 0;
+ for (l = 1; l <= 16; l++) {
+ i = (int) htbl->bits[l];
+ if (i < 0 || p + i > 256) /* protect against table overrun */
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ while (i--)
+ huffsize[p++] = (char) l;
+ }
+ huffsize[p] = 0;
+ numsymbols = p;
+
+ /* Figure C.2: generate the codes themselves */
+ /* We also validate that the counts represent a legal Huffman code tree. */
+
+ code = 0;
+ _si = huffsize[0];
+ p = 0;
+ while (huffsize[p]) {
+ while (((int) huffsize[p]) == _si) {
+ huffcode[p++] = code;
+ code++;
+ }
+ /* code is now 1 more than the last code used for codelength si; but
+ * it must still fit in si bits, since no code is allowed to be all ones.
+ */
+ if (((INT32) code) >= (((INT32) 1) << _si))
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ code <<= 1;
+ _si++;
+ }
+
+ /* Figure F.15: generate decoding tables for bit-sequential decoding */
+
+ p = 0;
+ for (l = 1; l <= 16; l++) {
+ if (htbl->bits[l]) {
+ /* valoffset[l] = huffval[] index of 1st symbol of code length l,
+ * minus the minimum code of length l
+ */
+ dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
+ p += htbl->bits[l];
+ dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
+ } else {
+ dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
+ }
+ }
+ dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
+
+ /* Compute lookahead tables to speed up decoding.
+ * First we set all the table entries to 0, indicating "too long";
+ * then we iterate through the Huffman codes that are short enough and
+ * fill in all the entries that correspond to bit sequences starting
+ * with that code.
+ */
+
+ MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
+
+ p = 0;
+ for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
+ for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
+ /* l = current code's length, p = its index in huffcode[] & huffval[]. */
+ /* Generate left-justified code followed by all possible bit sequences */
+ lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
+ for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
+ dtbl->look_nbits[lookbits] = l;
+ dtbl->look_sym[lookbits] = htbl->huffval[p];
+ lookbits++;
+ }
+ }
+ }
+
+ /* Validate symbols as being reasonable.
+ * For AC tables, we make no check, but accept all byte values 0..255.
+ * For DC tables, we require the symbols to be in range 0..15.
+ * (Tighter bounds could be applied depending on the data depth and mode,
+ * but this is sufficient to ensure safe decoding.)
+ */
+ if (isDC) {
+ for (i = 0; i < numsymbols; i++) {
+ int sym = htbl->huffval[i];
+ if (sym < 0 || sym > 15)
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+ }
+ }
+}
+
+
+/*
+ * Out-of-line code for bit fetching (shared with jdphuff.c).
+ * See jdhuff.h for info about usage.
+ * Note: current values of get_buffer and bits_left are passed as parameters,
+ * but are returned in the corresponding fields of the state struct.
+ *
+ * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
+ * of get_buffer to be used. (On machines with wider words, an even larger
+ * buffer could be used.) However, on some machines 32-bit shifts are
+ * quite slow and take time proportional to the number of places shifted.
+ * (This is true with most PC compilers, for instance.) In this case it may
+ * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
+ * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
+ */
+
+#ifdef SLOW_SHIFT_32
+#define MIN_GET_BITS 15 /* minimum allowable value */
+#else
+#define MIN_GET_BITS (BIT_BUF_SIZE-7)
+#endif
+
+
+GLOBAL(boolean)
+jpeg_fill_bit_buffer (bitread_working_state * state,
+ register bit_buf_type get_buffer, register int bits_left,
+ int nbits)
+/* Load up the bit buffer to a depth of at least nbits */
+{
+ /* Copy heavily used state fields into locals (hopefully registers) */
+ register const JOCTET * next_input_byte = state->next_input_byte;
+ register size_t bytes_in_buffer = state->bytes_in_buffer;
+ j_decompress_ptr cinfo = state->cinfo;
+
+ /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
+ /* (It is assumed that no request will be for more than that many bits.) */
+ /* We fail to do so only if we hit a marker or are forced to suspend. */
+
+ if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
+ while (bits_left < MIN_GET_BITS) {
+ register int c;
+
+ /* Attempt to read a byte */
+ if (bytes_in_buffer == 0) {
+ if (! (*cinfo->src->fill_input_buffer) (cinfo))
+ return FALSE;
+ next_input_byte = cinfo->src->next_input_byte;
+ bytes_in_buffer = cinfo->src->bytes_in_buffer;
+ }
+ bytes_in_buffer--;
+ c = GETJOCTET(*next_input_byte++);
+
+ /* If it's 0xFF, check and discard stuffed zero byte */
+ if (c == 0xFF) {
+ /* Loop here to discard any padding FF's on terminating marker,
+ * so that we can save a valid unread_marker value. NOTE: we will
+ * accept multiple FF's followed by a 0 as meaning a single FF data
+ * byte. This data pattern is not valid according to the standard.
+ */
+ do {
+ if (bytes_in_buffer == 0) {
+ if (! (*cinfo->src->fill_input_buffer) (cinfo))
+ return FALSE;
+ next_input_byte = cinfo->src->next_input_byte;
+ bytes_in_buffer = cinfo->src->bytes_in_buffer;
+ }
+ bytes_in_buffer--;
+ c = GETJOCTET(*next_input_byte++);
+ } while (c == 0xFF);
+
+ if (c == 0) {
+ /* Found FF/00, which represents an FF data byte */
+ c = 0xFF;
+ } else {
+ /* Oops, it's actually a marker indicating end of compressed data.
+ * Save the marker code for later use.
+ * Fine point: it might appear that we should save the marker into
+ * bitread working state, not straight into permanent state. But
+ * once we have hit a marker, we cannot need to suspend within the
+ * current MCU, because we will read no more bytes from the data
+ * source. So it is OK to update permanent state right away.
+ */
+ cinfo->unread_marker = c;
+ /* See if we need to insert some fake zero bits. */
+ goto no_more_bytes;
+ }
+ }
+
+ /* OK, load c into get_buffer */
+ get_buffer = (get_buffer << 8) | c;
+ bits_left += 8;
+ } /* end while */
+ } else {
+ no_more_bytes:
+ /* We get here if we've read the marker that terminates the compressed
+ * data segment. There should be enough bits in the buffer register
+ * to satisfy the request; if so, no problem.
+ */
+ if (nbits > bits_left) {
+ /* Uh-oh. Report corrupted data to user and stuff zeroes into
+ * the data stream, so that we can produce some kind of image.
+ * We use a nonvolatile flag to ensure that only one warning message
+ * appears per data segment.
+ */
+ if (! cinfo->entropy->insufficient_data) {
+ WARNMS(cinfo, JWRN_HIT_MARKER);
+ cinfo->entropy->insufficient_data = TRUE;
+ }
+ /* Fill the buffer with zero bits */
+ get_buffer <<= MIN_GET_BITS - bits_left;
+ bits_left = MIN_GET_BITS;
+ }
+ }
+
+ /* Unload the local registers */
+ state->next_input_byte = next_input_byte;
+ state->bytes_in_buffer = bytes_in_buffer;
+ state->get_buffer = get_buffer;
+ state->bits_left = bits_left;
+
+ return TRUE;
+}
+
+
+/*
+ * Out-of-line code for Huffman code decoding.
+ * See jdhuff.h for info about usage.
+ */
+
+GLOBAL(int)
+jpeg_huff_decode (bitread_working_state * state,
+ register bit_buf_type get_buffer, register int bits_left,
+ d_derived_tbl * htbl, int min_bits)
+{
+ register int l = min_bits;
+ register INT32 code;
+
+ /* HUFF_DECODE has determined that the code is at least min_bits */
+ /* bits long, so fetch that many bits in one swoop. */
+
+ CHECK_BIT_BUFFER(*state, l, return -1);
+ code = GET_BITS(l);
+
+ /* Collect the rest of the Huffman code one bit at a time. */
+ /* This is per Figure F.16 in the JPEG spec. */
+
+ while (code > htbl->maxcode[l]) {
+ code <<= 1;
+ CHECK_BIT_BUFFER(*state, 1, return -1);
+ code |= GET_BITS(1);
+ l++;
+ }
+
+ /* Unload the local registers */
+ state->get_buffer = get_buffer;
+ state->bits_left = bits_left;
+
+ /* With garbage input we may reach the sentinel value l = 17. */
+
+ if (l > 16) {
+ WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
+ return 0; /* fake a zero as the safest result */
+ }
+
+ return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
+}
+
+
+/*
+ * 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)
+{
+ huff_entropy_ptr entropy = (huff_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;
+
+ /* 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;
+}
+
+
+/*
+ * Decode and return 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 HAS BEEN ZEROED BY THE CALLER.
+ * (Wholesale zeroing is usually a little faster than retail...)
+ *
+ * Returns 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
+ * this module, since we'll just re-assign them on the next call.)
+ */
+
+METHODDEF(boolean)
+decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int blkn;
+ BITREAD_STATE_VARS;
+ savable_state state;
+
+ /* 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++) {
+ JBLOCKROW block = MCU_data[blkn];
+ d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
+ d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
+ register int s, k, r;
+
+ /* Decode a single block's worth of coefficients */
+
+ /* Section F.2.2.1: decode the DC coefficient difference */
+ HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
+ if (s) {
+ CHECK_BIT_BUFFER(br_state, s, return FALSE);
+ r = GET_BITS(s);
+ s = HUFF_EXTEND(r, s);
+ }
+
+ if (entropy->dc_needed[blkn]) {
+ /* Convert DC difference to actual value, update last_dc_val */
+ int ci = cinfo->MCU_membership[blkn];
+ s += state.last_dc_val[ci];
+ state.last_dc_val[ci] = s;
+ /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
+ (*block)[0] = (JCOEF) s;
+ }
+
+ if (entropy->ac_needed[blkn]) {
+
+ /* Section F.2.2.2: decode the AC coefficients */
+ /* Since zeroes are skipped, output area must be cleared beforehand */
+ for (k = 1; k < DCTSIZE2; k++) {
+ HUFF_DECODE(s, br_state, actbl, 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);
+ /* Output coefficient in natural (dezigzagged) order.
+ * Note: the extra entries in jpeg_natural_order[] will save us
+ * if k >= DCTSIZE2, which could happen if the data is corrupted.
+ */
+ (*block)[jpeg_natural_order[k]] = (JCOEF) s;
+ } else {
+ if (r != 15)
+ break;
+ k += 15;
+ }
+ }
+
+ } else {
+
+ /* Section F.2.2.2: decode the AC coefficients */
+ /* In this path we just discard the values */
+ for (k = 1; k < DCTSIZE2; k++) {
+ HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
+
+ r = s >> 4;
+ s &= 15;
+
+ if (s) {
+ k += r;
+ CHECK_BIT_BUFFER(br_state, s, return FALSE);
+ DROP_BITS(s);
+ } else {
+ if (r != 15)
+ break;
+ k += 15;
+ }
+ }
+
+ }
+ }
+
+ /* 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;
+}
+
+
+/*
+ * Module initialization routine for Huffman entropy decoding.
+ */
+
+GLOBAL(void)
+jinit_huff_decoder (j_decompress_ptr cinfo)
+{
+ huff_entropy_ptr entropy;
+ int i;
+
+ entropy = (huff_entropy_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(huff_entropy_decoder));
+ cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
+ entropy->pub.start_pass = start_pass_huff_decoder;
+ entropy->pub.decode_mcu = decode_mcu;
+
+ /* Mark tables unallocated */
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdinput.c b/core/src/fxcodec/libjpeg/fpdfapi_jdinput.c
index 46a7efce2e..6e714e928e 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdinput.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdinput.c
@@ -1,384 +1,384 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdinput.c
- *
- * 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 input control logic for the JPEG decompressor.
- * These routines are concerned with controlling the decompressor's input
- * processing (marker reading and coefficient decoding). The actual input
- * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Private state */
-
-typedef struct {
- struct jpeg_input_controller pub; /* public fields */
-
- boolean inheaders; /* TRUE until first SOS is reached */
-} my_input_controller;
-
-typedef my_input_controller * my_inputctl_ptr;
-
-
-/* Forward declarations */
-METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
-
-
-/*
- * Routines to calculate various quantities related to the size of the image.
- */
-
-LOCAL(void)
-initial_setup (j_decompress_ptr cinfo)
-/* Called once, when first SOS marker is reached */
-{
- int ci;
- jpeg_component_info *compptr;
-
- /* Make sure image isn't bigger than I can handle */
- if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
- (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
- ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
-
- /* For now, precision must match compiled-in value... */
- if (cinfo->data_precision != BITS_IN_JSAMPLE)
- ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
-
- /* Check that number of components won't exceed internal array sizes */
- if (cinfo->num_components > MAX_COMPONENTS)
- ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
- MAX_COMPONENTS);
-
- /* Compute maximum sampling factors; check factor validity */
- cinfo->max_h_samp_factor = 1;
- cinfo->max_v_samp_factor = 1;
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
- compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
- ERREXIT(cinfo, JERR_BAD_SAMPLING);
- cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
- compptr->h_samp_factor);
- cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
- compptr->v_samp_factor);
- }
-
- /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
- * In the full decompressor, this will be overridden by jdmaster.c;
- * but in the transcoder, jdmaster.c is not used, so we must do it here.
- */
- cinfo->min_DCT_scaled_size = DCTSIZE;
-
- /* Compute dimensions of components */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- compptr->DCT_scaled_size = DCTSIZE;
- /* Size in DCT blocks */
- compptr->width_in_blocks = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
- (long) (cinfo->max_h_samp_factor * DCTSIZE));
- compptr->height_in_blocks = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
- (long) (cinfo->max_v_samp_factor * DCTSIZE));
- /* downsampled_width and downsampled_height will also be overridden by
- * jdmaster.c if we are doing full decompression. The transcoder library
- * doesn't use these values, but the calling application might.
- */
- /* Size in samples */
- compptr->downsampled_width = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
- (long) cinfo->max_h_samp_factor);
- compptr->downsampled_height = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
- (long) cinfo->max_v_samp_factor);
- /* Mark component needed, until color conversion says otherwise */
- compptr->component_needed = TRUE;
- /* Mark no quantization table yet saved for component */
- compptr->quant_table = NULL;
- }
-
- /* Compute number of fully interleaved MCU rows. */
- cinfo->total_iMCU_rows = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height,
- (long) (cinfo->max_v_samp_factor*DCTSIZE));
-
- /* Decide whether file contains multiple scans */
- if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
- cinfo->inputctl->has_multiple_scans = TRUE;
- else
- cinfo->inputctl->has_multiple_scans = FALSE;
-}
-
-
-LOCAL(void)
-per_scan_setup (j_decompress_ptr cinfo)
-/* Do computations that are needed before processing a JPEG scan */
-/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
-{
- int ci, mcublks, tmp;
- jpeg_component_info *compptr;
-
- if (cinfo->comps_in_scan == 1) {
-
- /* Noninterleaved (single-component) scan */
- compptr = cinfo->cur_comp_info[0];
-
- /* Overall image size in MCUs */
- cinfo->MCUs_per_row = compptr->width_in_blocks;
- cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
-
- /* For noninterleaved scan, always one block per MCU */
- compptr->MCU_width = 1;
- compptr->MCU_height = 1;
- compptr->MCU_blocks = 1;
- compptr->MCU_sample_width = compptr->DCT_scaled_size;
- compptr->last_col_width = 1;
- /* For noninterleaved scans, it is convenient to define last_row_height
- * as the number of block rows present in the last iMCU row.
- */
- tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
- if (tmp == 0) tmp = compptr->v_samp_factor;
- compptr->last_row_height = tmp;
-
- /* Prepare array describing MCU composition */
- cinfo->blocks_in_MCU = 1;
- cinfo->MCU_membership[0] = 0;
-
- } else {
-
- /* Interleaved (multi-component) scan */
- if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
- ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
- MAX_COMPS_IN_SCAN);
-
- /* Overall image size in MCUs */
- cinfo->MCUs_per_row = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width,
- (long) (cinfo->max_h_samp_factor*DCTSIZE));
- cinfo->MCU_rows_in_scan = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height,
- (long) (cinfo->max_v_samp_factor*DCTSIZE));
-
- cinfo->blocks_in_MCU = 0;
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- /* Sampling factors give # of blocks of component in each MCU */
- compptr->MCU_width = compptr->h_samp_factor;
- compptr->MCU_height = compptr->v_samp_factor;
- compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
- compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
- /* Figure number of non-dummy blocks in last MCU column & row */
- tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
- if (tmp == 0) tmp = compptr->MCU_width;
- compptr->last_col_width = tmp;
- tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
- if (tmp == 0) tmp = compptr->MCU_height;
- compptr->last_row_height = tmp;
- /* Prepare array describing MCU composition */
- mcublks = compptr->MCU_blocks;
- if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
- ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
- while (mcublks-- > 0) {
- cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
- }
- }
-
- }
-}
-
-
-/*
- * Save away a copy of the Q-table referenced by each component present
- * in the current scan, unless already saved during a prior scan.
- *
- * In a multiple-scan JPEG file, the encoder could assign different components
- * the same Q-table slot number, but change table definitions between scans
- * so that each component uses a different Q-table. (The IJG encoder is not
- * currently capable of doing this, but other encoders might.) Since we want
- * to be able to dequantize all the components at the end of the file, this
- * means that we have to save away the table actually used for each component.
- * We do this by copying the table at the start of the first scan containing
- * the component.
- * The JPEG spec prohibits the encoder from changing the contents of a Q-table
- * slot between scans of a component using that slot. If the encoder does so
- * anyway, this decoder will simply use the Q-table values that were current
- * at the start of the first scan for the component.
- *
- * The decompressor output side looks only at the saved quant tables,
- * not at the current Q-table slots.
- */
-
-LOCAL(void)
-latch_quant_tables (j_decompress_ptr cinfo)
-{
- int ci, qtblno;
- jpeg_component_info *compptr;
- JQUANT_TBL * qtbl;
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- /* No work if we already saved Q-table for this component */
- if (compptr->quant_table != NULL)
- continue;
- /* Make sure specified quantization table is present */
- qtblno = compptr->quant_tbl_no;
- if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
- cinfo->quant_tbl_ptrs[qtblno] == NULL)
- ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
- /* OK, save away the quantization table */
- qtbl = (JQUANT_TBL *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(JQUANT_TBL));
- MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
- compptr->quant_table = qtbl;
- }
-}
-
-
-/*
- * Initialize the input modules to read a scan of compressed data.
- * The first call to this is done by jdmaster.c after initializing
- * the entire decompressor (during jpeg_start_decompress).
- * Subsequent calls come from consume_markers, below.
- */
-
-METHODDEF(void)
-start_input_pass (j_decompress_ptr cinfo)
-{
- per_scan_setup(cinfo);
- latch_quant_tables(cinfo);
- (*cinfo->entropy->start_pass) (cinfo);
- (*cinfo->coef->start_input_pass) (cinfo);
- cinfo->inputctl->consume_input = cinfo->coef->consume_data;
-}
-
-
-/*
- * Finish up after inputting a compressed-data scan.
- * This is called by the coefficient controller after it's read all
- * the expected data of the scan.
- */
-
-METHODDEF(void)
-finish_input_pass (j_decompress_ptr cinfo)
-{
- cinfo->inputctl->consume_input = consume_markers;
-}
-
-
-/*
- * Read JPEG markers before, between, or after compressed-data scans.
- * Change state as necessary when a new scan is reached.
- * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
- *
- * The consume_input method pointer points either here or to the
- * coefficient controller's consume_data routine, depending on whether
- * we are reading a compressed data segment or inter-segment markers.
- */
-
-METHODDEF(int)
-consume_markers (j_decompress_ptr cinfo)
-{
- my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
- int val;
-
- if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
- return JPEG_REACHED_EOI;
-
- val = (*cinfo->marker->read_markers) (cinfo);
-
- switch (val) {
- case JPEG_REACHED_SOS: /* Found SOS */
- if (inputctl->inheaders) { /* 1st SOS */
- initial_setup(cinfo);
- inputctl->inheaders = FALSE;
- /* Note: start_input_pass must be called by jdmaster.c
- * before any more input can be consumed. jdapimin.c is
- * responsible for enforcing this sequencing.
- */
- } else { /* 2nd or later SOS marker */
- if (! inputctl->pub.has_multiple_scans)
- ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
- start_input_pass(cinfo);
- }
- break;
- case JPEG_REACHED_EOI: /* Found EOI */
- inputctl->pub.eoi_reached = TRUE;
- if (inputctl->inheaders) { /* Tables-only datastream, apparently */
- if (cinfo->marker->saw_SOF)
- ERREXIT(cinfo, JERR_SOF_NO_SOS);
- } else {
- /* Prevent infinite loop in coef ctlr's decompress_data routine
- * if user set output_scan_number larger than number of scans.
- */
- if (cinfo->output_scan_number > cinfo->input_scan_number)
- cinfo->output_scan_number = cinfo->input_scan_number;
- }
- break;
- case JPEG_SUSPENDED:
- break;
- }
-
- return val;
-}
-
-
-/*
- * Reset state to begin a fresh datastream.
- */
-
-METHODDEF(void)
-reset_input_controller (j_decompress_ptr cinfo)
-{
- my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
-
- inputctl->pub.consume_input = consume_markers;
- inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
- inputctl->pub.eoi_reached = FALSE;
- inputctl->inheaders = TRUE;
- /* Reset other modules */
- (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
- (*cinfo->marker->reset_marker_reader) (cinfo);
- /* Reset progression state -- would be cleaner if entropy decoder did this */
- cinfo->coef_bits = NULL;
-}
-
-
-/*
- * Initialize the input controller module.
- * This is called only once, when the decompression object is created.
- */
-
-GLOBAL(void)
-jinit_input_controller (j_decompress_ptr cinfo)
-{
- my_inputctl_ptr inputctl;
-
- /* Create subobject in permanent pool */
- inputctl = (my_inputctl_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
- SIZEOF(my_input_controller));
- cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
- /* Initialize method pointers */
- inputctl->pub.consume_input = consume_markers;
- inputctl->pub.reset_input_controller = reset_input_controller;
- inputctl->pub.start_input_pass = start_input_pass;
- inputctl->pub.finish_input_pass = finish_input_pass;
- /* Initialize state: can't use reset_input_controller since we don't
- * want to try to reset other modules yet.
- */
- inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
- inputctl->pub.eoi_reached = FALSE;
- inputctl->inheaders = TRUE;
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdinput.c
+ *
+ * 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 input control logic for the JPEG decompressor.
+ * These routines are concerned with controlling the decompressor's input
+ * processing (marker reading and coefficient decoding). The actual input
+ * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private state */
+
+typedef struct {
+ struct jpeg_input_controller pub; /* public fields */
+
+ boolean inheaders; /* TRUE until first SOS is reached */
+} my_input_controller;
+
+typedef my_input_controller * my_inputctl_ptr;
+
+
+/* Forward declarations */
+METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
+
+
+/*
+ * Routines to calculate various quantities related to the size of the image.
+ */
+
+LOCAL(void)
+initial_setup (j_decompress_ptr cinfo)
+/* Called once, when first SOS marker is reached */
+{
+ int ci;
+ jpeg_component_info *compptr;
+
+ /* Make sure image isn't bigger than I can handle */
+ if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
+ (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
+ ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
+
+ /* For now, precision must match compiled-in value... */
+ if (cinfo->data_precision != BITS_IN_JSAMPLE)
+ ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
+
+ /* Check that number of components won't exceed internal array sizes */
+ if (cinfo->num_components > MAX_COMPONENTS)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
+ MAX_COMPONENTS);
+
+ /* Compute maximum sampling factors; check factor validity */
+ cinfo->max_h_samp_factor = 1;
+ cinfo->max_v_samp_factor = 1;
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
+ compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
+ ERREXIT(cinfo, JERR_BAD_SAMPLING);
+ cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
+ compptr->h_samp_factor);
+ cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
+ compptr->v_samp_factor);
+ }
+
+ /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
+ * In the full decompressor, this will be overridden by jdmaster.c;
+ * but in the transcoder, jdmaster.c is not used, so we must do it here.
+ */
+ cinfo->min_DCT_scaled_size = DCTSIZE;
+
+ /* Compute dimensions of components */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ compptr->DCT_scaled_size = DCTSIZE;
+ /* Size in DCT blocks */
+ compptr->width_in_blocks = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+ (long) (cinfo->max_h_samp_factor * DCTSIZE));
+ compptr->height_in_blocks = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+ (long) (cinfo->max_v_samp_factor * DCTSIZE));
+ /* downsampled_width and downsampled_height will also be overridden by
+ * jdmaster.c if we are doing full decompression. The transcoder library
+ * doesn't use these values, but the calling application might.
+ */
+ /* Size in samples */
+ compptr->downsampled_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
+ (long) cinfo->max_h_samp_factor);
+ compptr->downsampled_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
+ (long) cinfo->max_v_samp_factor);
+ /* Mark component needed, until color conversion says otherwise */
+ compptr->component_needed = TRUE;
+ /* Mark no quantization table yet saved for component */
+ compptr->quant_table = NULL;
+ }
+
+ /* Compute number of fully interleaved MCU rows. */
+ cinfo->total_iMCU_rows = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height,
+ (long) (cinfo->max_v_samp_factor*DCTSIZE));
+
+ /* Decide whether file contains multiple scans */
+ if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
+ cinfo->inputctl->has_multiple_scans = TRUE;
+ else
+ cinfo->inputctl->has_multiple_scans = FALSE;
+}
+
+
+LOCAL(void)
+per_scan_setup (j_decompress_ptr cinfo)
+/* Do computations that are needed before processing a JPEG scan */
+/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
+{
+ int ci, mcublks, tmp;
+ jpeg_component_info *compptr;
+
+ if (cinfo->comps_in_scan == 1) {
+
+ /* Noninterleaved (single-component) scan */
+ compptr = cinfo->cur_comp_info[0];
+
+ /* Overall image size in MCUs */
+ cinfo->MCUs_per_row = compptr->width_in_blocks;
+ cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
+
+ /* For noninterleaved scan, always one block per MCU */
+ compptr->MCU_width = 1;
+ compptr->MCU_height = 1;
+ compptr->MCU_blocks = 1;
+ compptr->MCU_sample_width = compptr->DCT_scaled_size;
+ compptr->last_col_width = 1;
+ /* For noninterleaved scans, it is convenient to define last_row_height
+ * as the number of block rows present in the last iMCU row.
+ */
+ tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
+ if (tmp == 0) tmp = compptr->v_samp_factor;
+ compptr->last_row_height = tmp;
+
+ /* Prepare array describing MCU composition */
+ cinfo->blocks_in_MCU = 1;
+ cinfo->MCU_membership[0] = 0;
+
+ } else {
+
+ /* Interleaved (multi-component) scan */
+ if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
+ ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
+ MAX_COMPS_IN_SCAN);
+
+ /* Overall image size in MCUs */
+ cinfo->MCUs_per_row = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width,
+ (long) (cinfo->max_h_samp_factor*DCTSIZE));
+ cinfo->MCU_rows_in_scan = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height,
+ (long) (cinfo->max_v_samp_factor*DCTSIZE));
+
+ cinfo->blocks_in_MCU = 0;
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* Sampling factors give # of blocks of component in each MCU */
+ compptr->MCU_width = compptr->h_samp_factor;
+ compptr->MCU_height = compptr->v_samp_factor;
+ compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
+ compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
+ /* Figure number of non-dummy blocks in last MCU column & row */
+ tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
+ if (tmp == 0) tmp = compptr->MCU_width;
+ compptr->last_col_width = tmp;
+ tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
+ if (tmp == 0) tmp = compptr->MCU_height;
+ compptr->last_row_height = tmp;
+ /* Prepare array describing MCU composition */
+ mcublks = compptr->MCU_blocks;
+ if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
+ ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
+ while (mcublks-- > 0) {
+ cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
+ }
+ }
+
+ }
+}
+
+
+/*
+ * Save away a copy of the Q-table referenced by each component present
+ * in the current scan, unless already saved during a prior scan.
+ *
+ * In a multiple-scan JPEG file, the encoder could assign different components
+ * the same Q-table slot number, but change table definitions between scans
+ * so that each component uses a different Q-table. (The IJG encoder is not
+ * currently capable of doing this, but other encoders might.) Since we want
+ * to be able to dequantize all the components at the end of the file, this
+ * means that we have to save away the table actually used for each component.
+ * We do this by copying the table at the start of the first scan containing
+ * the component.
+ * The JPEG spec prohibits the encoder from changing the contents of a Q-table
+ * slot between scans of a component using that slot. If the encoder does so
+ * anyway, this decoder will simply use the Q-table values that were current
+ * at the start of the first scan for the component.
+ *
+ * The decompressor output side looks only at the saved quant tables,
+ * not at the current Q-table slots.
+ */
+
+LOCAL(void)
+latch_quant_tables (j_decompress_ptr cinfo)
+{
+ int ci, qtblno;
+ jpeg_component_info *compptr;
+ JQUANT_TBL * qtbl;
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ /* No work if we already saved Q-table for this component */
+ if (compptr->quant_table != NULL)
+ continue;
+ /* Make sure specified quantization table is present */
+ qtblno = compptr->quant_tbl_no;
+ if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
+ cinfo->quant_tbl_ptrs[qtblno] == NULL)
+ ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
+ /* OK, save away the quantization table */
+ qtbl = (JQUANT_TBL *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(JQUANT_TBL));
+ MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
+ compptr->quant_table = qtbl;
+ }
+}
+
+
+/*
+ * Initialize the input modules to read a scan of compressed data.
+ * The first call to this is done by jdmaster.c after initializing
+ * the entire decompressor (during jpeg_start_decompress).
+ * Subsequent calls come from consume_markers, below.
+ */
+
+METHODDEF(void)
+start_input_pass (j_decompress_ptr cinfo)
+{
+ per_scan_setup(cinfo);
+ latch_quant_tables(cinfo);
+ (*cinfo->entropy->start_pass) (cinfo);
+ (*cinfo->coef->start_input_pass) (cinfo);
+ cinfo->inputctl->consume_input = cinfo->coef->consume_data;
+}
+
+
+/*
+ * Finish up after inputting a compressed-data scan.
+ * This is called by the coefficient controller after it's read all
+ * the expected data of the scan.
+ */
+
+METHODDEF(void)
+finish_input_pass (j_decompress_ptr cinfo)
+{
+ cinfo->inputctl->consume_input = consume_markers;
+}
+
+
+/*
+ * Read JPEG markers before, between, or after compressed-data scans.
+ * Change state as necessary when a new scan is reached.
+ * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
+ *
+ * The consume_input method pointer points either here or to the
+ * coefficient controller's consume_data routine, depending on whether
+ * we are reading a compressed data segment or inter-segment markers.
+ */
+
+METHODDEF(int)
+consume_markers (j_decompress_ptr cinfo)
+{
+ my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
+ int val;
+
+ if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
+ return JPEG_REACHED_EOI;
+
+ val = (*cinfo->marker->read_markers) (cinfo);
+
+ switch (val) {
+ case JPEG_REACHED_SOS: /* Found SOS */
+ if (inputctl->inheaders) { /* 1st SOS */
+ initial_setup(cinfo);
+ inputctl->inheaders = FALSE;
+ /* Note: start_input_pass must be called by jdmaster.c
+ * before any more input can be consumed. jdapimin.c is
+ * responsible for enforcing this sequencing.
+ */
+ } else { /* 2nd or later SOS marker */
+ if (! inputctl->pub.has_multiple_scans)
+ ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
+ start_input_pass(cinfo);
+ }
+ break;
+ case JPEG_REACHED_EOI: /* Found EOI */
+ inputctl->pub.eoi_reached = TRUE;
+ if (inputctl->inheaders) { /* Tables-only datastream, apparently */
+ if (cinfo->marker->saw_SOF)
+ ERREXIT(cinfo, JERR_SOF_NO_SOS);
+ } else {
+ /* Prevent infinite loop in coef ctlr's decompress_data routine
+ * if user set output_scan_number larger than number of scans.
+ */
+ if (cinfo->output_scan_number > cinfo->input_scan_number)
+ cinfo->output_scan_number = cinfo->input_scan_number;
+ }
+ break;
+ case JPEG_SUSPENDED:
+ break;
+ }
+
+ return val;
+}
+
+
+/*
+ * Reset state to begin a fresh datastream.
+ */
+
+METHODDEF(void)
+reset_input_controller (j_decompress_ptr cinfo)
+{
+ my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
+
+ inputctl->pub.consume_input = consume_markers;
+ inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
+ inputctl->pub.eoi_reached = FALSE;
+ inputctl->inheaders = TRUE;
+ /* Reset other modules */
+ (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
+ (*cinfo->marker->reset_marker_reader) (cinfo);
+ /* Reset progression state -- would be cleaner if entropy decoder did this */
+ cinfo->coef_bits = NULL;
+}
+
+
+/*
+ * Initialize the input controller module.
+ * This is called only once, when the decompression object is created.
+ */
+
+GLOBAL(void)
+jinit_input_controller (j_decompress_ptr cinfo)
+{
+ my_inputctl_ptr inputctl;
+
+ /* Create subobject in permanent pool */
+ inputctl = (my_inputctl_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ SIZEOF(my_input_controller));
+ cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
+ /* Initialize method pointers */
+ inputctl->pub.consume_input = consume_markers;
+ inputctl->pub.reset_input_controller = reset_input_controller;
+ inputctl->pub.start_input_pass = start_input_pass;
+ inputctl->pub.finish_input_pass = finish_input_pass;
+ /* Initialize state: can't use reset_input_controller since we don't
+ * want to try to reset other modules yet.
+ */
+ inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
+ inputctl->pub.eoi_reached = FALSE;
+ inputctl->inheaders = TRUE;
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c b/core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c
index 0fc77c7c17..1483e6fff0 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdmainct.c
@@ -1,515 +1,515 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdmainct.c
- *
- * Copyright (C) 1994-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 contains the main buffer controller for decompression.
- * The main buffer lies between the JPEG decompressor proper and the
- * post-processor; it holds downsampled data in the JPEG colorspace.
- *
- * Note that this code is bypassed in raw-data mode, since the application
- * supplies the equivalent of the main buffer in that case.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/*
- * In the current system design, the main buffer need never be a full-image
- * buffer; any full-height buffers will be found inside the coefficient or
- * postprocessing controllers. Nonetheless, the main controller is not
- * trivial. Its responsibility is to provide context rows for upsampling/
- * rescaling, and doing this in an efficient fashion is a bit tricky.
- *
- * Postprocessor input data is counted in "row groups". A row group
- * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
- * sample rows of each component. (We require DCT_scaled_size values to be
- * chosen such that these numbers are integers. In practice DCT_scaled_size
- * values will likely be powers of two, so we actually have the stronger
- * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
- * Upsampling will typically produce max_v_samp_factor pixel rows from each
- * row group (times any additional scale factor that the upsampler is
- * applying).
- *
- * The coefficient controller will deliver data to us one iMCU row at a time;
- * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
- * exactly min_DCT_scaled_size row groups. (This amount of data corresponds
- * to one row of MCUs when the image is fully interleaved.) Note that the
- * number of sample rows varies across components, but the number of row
- * groups does not. Some garbage sample rows may be included in the last iMCU
- * row at the bottom of the image.
- *
- * Depending on the vertical scaling algorithm used, the upsampler may need
- * access to the sample row(s) above and below its current input row group.
- * The upsampler is required to set need_context_rows TRUE at global selection
- * time if so. When need_context_rows is FALSE, this controller can simply
- * obtain one iMCU row at a time from the coefficient controller and dole it
- * out as row groups to the postprocessor.
- *
- * When need_context_rows is TRUE, this controller guarantees that the buffer
- * passed to postprocessing contains at least one row group's worth of samples
- * above and below the row group(s) being processed. Note that the context
- * rows "above" the first passed row group appear at negative row offsets in
- * the passed buffer. At the top and bottom of the image, the required
- * context rows are manufactured by duplicating the first or last real sample
- * row; this avoids having special cases in the upsampling inner loops.
- *
- * The amount of context is fixed at one row group just because that's a
- * convenient number for this controller to work with. The existing
- * upsamplers really only need one sample row of context. An upsampler
- * supporting arbitrary output rescaling might wish for more than one row
- * group of context when shrinking the image; tough, we don't handle that.
- * (This is justified by the assumption that downsizing will be handled mostly
- * by adjusting the DCT_scaled_size values, so that the actual scale factor at
- * the upsample step needn't be much less than one.)
- *
- * To provide the desired context, we have to retain the last two row groups
- * of one iMCU row while reading in the next iMCU row. (The last row group
- * can't be processed until we have another row group for its below-context,
- * and so we have to save the next-to-last group too for its above-context.)
- * We could do this most simply by copying data around in our buffer, but
- * that'd be very slow. We can avoid copying any data by creating a rather
- * strange pointer structure. Here's how it works. We allocate a workspace
- * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
- * of row groups per iMCU row). We create two sets of redundant pointers to
- * the workspace. Labeling the physical row groups 0 to M+1, the synthesized
- * pointer lists look like this:
- * M+1 M-1
- * master pointer --> 0 master pointer --> 0
- * 1 1
- * ... ...
- * M-3 M-3
- * M-2 M
- * M-1 M+1
- * M M-2
- * M+1 M-1
- * 0 0
- * We read alternate iMCU rows using each master pointer; thus the last two
- * row groups of the previous iMCU row remain un-overwritten in the workspace.
- * The pointer lists are set up so that the required context rows appear to
- * be adjacent to the proper places when we pass the pointer lists to the
- * upsampler.
- *
- * The above pictures describe the normal state of the pointer lists.
- * At top and bottom of the image, we diddle the pointer lists to duplicate
- * the first or last sample row as necessary (this is cheaper than copying
- * sample rows around).
- *
- * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that
- * situation each iMCU row provides only one row group so the buffering logic
- * must be different (eg, we must read two iMCU rows before we can emit the
- * first row group). For now, we simply do not support providing context
- * rows when min_DCT_scaled_size is 1. That combination seems unlikely to
- * be worth providing --- if someone wants a 1/8th-size preview, they probably
- * want it quick and dirty, so a context-free upsampler is sufficient.
- */
-
-
-/* Private buffer controller object */
-
-typedef struct {
- struct jpeg_d_main_controller pub; /* public fields */
-
- /* Pointer to allocated workspace (M or M+2 row groups). */
- JSAMPARRAY buffer[MAX_COMPONENTS];
-
- boolean buffer_full; /* Have we gotten an iMCU row from decoder? */
- JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */
-
- /* Remaining fields are only used in the context case. */
-
- /* These are the master pointers to the funny-order pointer lists. */
- JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */
-
- int whichptr; /* indicates which pointer set is now in use */
- int context_state; /* process_data state machine status */
- JDIMENSION rowgroups_avail; /* row groups available to postprocessor */
- JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */
-} my_main_controller;
-
-typedef my_main_controller * my_main_ptr;
-
-/* context_state values: */
-#define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */
-#define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */
-#define CTX_POSTPONED_ROW 2 /* feeding postponed row group */
-
-
-/* Forward declarations */
-METHODDEF(void) process_data_simple_main
- JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
- JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
-METHODDEF(void) process_data_context_main
- JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
- JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
-#ifdef QUANT_2PASS_SUPPORTED
-METHODDEF(void) process_data_crank_post
- JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
- JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
-#endif
-
-
-LOCAL(void)
-alloc_funny_pointers (j_decompress_ptr cinfo)
-/* Allocate space for the funny pointer lists.
- * This is done only once, not once per pass.
- */
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci, rgroup;
- int M = cinfo->min_DCT_scaled_size;
- jpeg_component_info *compptr;
- JSAMPARRAY xbuf;
-
- /* Get top-level space for component array pointers.
- * We alloc both arrays with one call to save a few cycles.
- */
- main->xbuffer[0] = (JSAMPIMAGE)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
- main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
- cinfo->min_DCT_scaled_size; /* height of a row group of component */
- /* Get space for pointer lists --- M+4 row groups in each list.
- * We alloc both pointer lists with one call to save a few cycles.
- */
- xbuf = (JSAMPARRAY)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
- xbuf += rgroup; /* want one row group at negative offsets */
- main->xbuffer[0][ci] = xbuf;
- xbuf += rgroup * (M + 4);
- main->xbuffer[1][ci] = xbuf;
- }
-}
-
-
-LOCAL(void)
-make_funny_pointers (j_decompress_ptr cinfo)
-/* Create the funny pointer lists discussed in the comments above.
- * The actual workspace is already allocated (in main->buffer),
- * and the space for the pointer lists is allocated too.
- * This routine just fills in the curiously ordered lists.
- * This will be repeated at the beginning of each pass.
- */
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci, i, rgroup;
- int M = cinfo->min_DCT_scaled_size;
- jpeg_component_info *compptr;
- JSAMPARRAY buf, xbuf0, xbuf1;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
- cinfo->min_DCT_scaled_size; /* height of a row group of component */
- xbuf0 = main->xbuffer[0][ci];
- xbuf1 = main->xbuffer[1][ci];
- /* First copy the workspace pointers as-is */
- buf = main->buffer[ci];
- for (i = 0; i < rgroup * (M + 2); i++) {
- xbuf0[i] = xbuf1[i] = buf[i];
- }
- /* In the second list, put the last four row groups in swapped order */
- for (i = 0; i < rgroup * 2; i++) {
- xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
- xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
- }
- /* The wraparound pointers at top and bottom will be filled later
- * (see set_wraparound_pointers, below). Initially we want the "above"
- * pointers to duplicate the first actual data line. This only needs
- * to happen in xbuffer[0].
- */
- for (i = 0; i < rgroup; i++) {
- xbuf0[i - rgroup] = xbuf0[0];
- }
- }
-}
-
-
-LOCAL(void)
-set_wraparound_pointers (j_decompress_ptr cinfo)
-/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
- * This changes the pointer list state from top-of-image to the normal state.
- */
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci, i, rgroup;
- int M = cinfo->min_DCT_scaled_size;
- jpeg_component_info *compptr;
- JSAMPARRAY xbuf0, xbuf1;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
- cinfo->min_DCT_scaled_size; /* height of a row group of component */
- xbuf0 = main->xbuffer[0][ci];
- xbuf1 = main->xbuffer[1][ci];
- for (i = 0; i < rgroup; i++) {
- xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
- xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
- xbuf0[rgroup*(M+2) + i] = xbuf0[i];
- xbuf1[rgroup*(M+2) + i] = xbuf1[i];
- }
- }
-}
-
-
-LOCAL(void)
-set_bottom_pointers (j_decompress_ptr cinfo)
-/* Change the pointer lists to duplicate the last sample row at the bottom
- * of the image. whichptr indicates which xbuffer holds the final iMCU row.
- * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
- */
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
- int ci, i, rgroup, iMCUheight, rows_left;
- jpeg_component_info *compptr;
- JSAMPARRAY xbuf;
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Count sample rows in one iMCU row and in one row group */
- iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
- rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
- /* Count nondummy sample rows remaining for this component */
- rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
- if (rows_left == 0) rows_left = iMCUheight;
- /* Count nondummy row groups. Should get same answer for each component,
- * so we need only do it once.
- */
- if (ci == 0) {
- main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
- }
- /* Duplicate the last real sample row rgroup*2 times; this pads out the
- * last partial rowgroup and ensures at least one full rowgroup of context.
- */
- xbuf = main->xbuffer[main->whichptr][ci];
- for (i = 0; i < rgroup * 2; i++) {
- xbuf[rows_left + i] = xbuf[rows_left-1];
- }
- }
-}
-
-
-/*
- * Initialize for a processing pass.
- */
-
-METHODDEF(void)
-start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
-
- switch (pass_mode) {
- case JBUF_PASS_THRU:
- if (cinfo->upsample->need_context_rows) {
- main->pub.process_data = process_data_context_main;
- make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
- main->whichptr = 0; /* Read first iMCU row into xbuffer[0] */
- main->context_state = CTX_PREPARE_FOR_IMCU;
- main->iMCU_row_ctr = 0;
- } else {
- /* Simple case with no context needed */
- main->pub.process_data = process_data_simple_main;
- }
- main->buffer_full = FALSE; /* Mark buffer empty */
- main->rowgroup_ctr = 0;
- break;
-#ifdef QUANT_2PASS_SUPPORTED
- case JBUF_CRANK_DEST:
- /* For last pass of 2-pass quantization, just crank the postprocessor */
- main->pub.process_data = process_data_crank_post;
- break;
-#endif
- default:
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- break;
- }
-}
-
-
-/*
- * Process some data.
- * This handles the simple case where no context is required.
- */
-
-METHODDEF(void)
-process_data_simple_main (j_decompress_ptr cinfo,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
- JDIMENSION rowgroups_avail;
-
- /* Read input data if we haven't filled the main buffer yet */
- if (! main->buffer_full) {
- if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
- return; /* suspension forced, can do nothing more */
- main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
- }
-
- /* There are always min_DCT_scaled_size row groups in an iMCU row. */
- rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
- /* Note: at the bottom of the image, we may pass extra garbage row groups
- * to the postprocessor. The postprocessor has to check for bottom
- * of image anyway (at row resolution), so no point in us doing it too.
- */
-
- /* Feed the postprocessor */
- (*cinfo->post->post_process_data) (cinfo, main->buffer,
- &main->rowgroup_ctr, rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail);
-
- /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
- if (main->rowgroup_ctr >= rowgroups_avail) {
- main->buffer_full = FALSE;
- main->rowgroup_ctr = 0;
- }
-}
-
-
-/*
- * Process some data.
- * This handles the case where context rows must be provided.
- */
-
-METHODDEF(void)
-process_data_context_main (j_decompress_ptr cinfo,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-{
- my_main_ptr main = (my_main_ptr) cinfo->main;
-
- /* Read input data if we haven't filled the main buffer yet */
- if (! main->buffer_full) {
- if (! (*cinfo->coef->decompress_data) (cinfo,
- main->xbuffer[main->whichptr]))
- return; /* suspension forced, can do nothing more */
- main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
- main->iMCU_row_ctr++; /* count rows received */
- }
-
- /* Postprocessor typically will not swallow all the input data it is handed
- * in one call (due to filling the output buffer first). Must be prepared
- * to exit and restart. This switch lets us keep track of how far we got.
- * Note that each case falls through to the next on successful completion.
- */
- switch (main->context_state) {
- case CTX_POSTPONED_ROW:
- /* Call postprocessor using previously set pointers for postponed row */
- (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
- &main->rowgroup_ctr, main->rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail);
- if (main->rowgroup_ctr < main->rowgroups_avail)
- return; /* Need to suspend */
- main->context_state = CTX_PREPARE_FOR_IMCU;
- if (*out_row_ctr >= out_rows_avail)
- return; /* Postprocessor exactly filled output buf */
- /*FALLTHROUGH*/
- case CTX_PREPARE_FOR_IMCU:
- /* Prepare to process first M-1 row groups of this iMCU row */
- main->rowgroup_ctr = 0;
- main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
- /* Check for bottom of image: if so, tweak pointers to "duplicate"
- * the last sample row, and adjust rowgroups_avail to ignore padding rows.
- */
- if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
- set_bottom_pointers(cinfo);
- main->context_state = CTX_PROCESS_IMCU;
- /*FALLTHROUGH*/
- case CTX_PROCESS_IMCU:
- /* Call postprocessor using previously set pointers */
- (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
- &main->rowgroup_ctr, main->rowgroups_avail,
- output_buf, out_row_ctr, out_rows_avail);
- if (main->rowgroup_ctr < main->rowgroups_avail)
- return; /* Need to suspend */
- /* After the first iMCU, change wraparound pointers to normal state */
- if (main->iMCU_row_ctr == 1)
- set_wraparound_pointers(cinfo);
- /* Prepare to load new iMCU row using other xbuffer list */
- main->whichptr ^= 1; /* 0=>1 or 1=>0 */
- main->buffer_full = FALSE;
- /* Still need to process last row group of this iMCU row, */
- /* which is saved at index M+1 of the other xbuffer */
- main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
- main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
- main->context_state = CTX_POSTPONED_ROW;
- }
-}
-
-
-/*
- * Process some data.
- * Final pass of two-pass quantization: just call the postprocessor.
- * Source data will be the postprocessor controller's internal buffer.
- */
-
-#ifdef QUANT_2PASS_SUPPORTED
-
-METHODDEF(void)
-process_data_crank_post (j_decompress_ptr cinfo,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-{
- (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
- (JDIMENSION *) NULL, (JDIMENSION) 0,
- output_buf, out_row_ctr, out_rows_avail);
-}
-
-#endif /* QUANT_2PASS_SUPPORTED */
-
-
-/*
- * Initialize main buffer controller.
- */
-
-GLOBAL(void)
-jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
-{
- my_main_ptr main;
- int ci, rgroup, ngroups;
- jpeg_component_info *compptr;
-
- main = (my_main_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_main_controller));
- cinfo->main = (struct jpeg_d_main_controller *) main;
- main->pub.start_pass = start_pass_main;
-
- if (need_full_buffer) /* shouldn't happen */
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
-
- /* Allocate the workspace.
- * ngroups is the number of row groups we need.
- */
- if (cinfo->upsample->need_context_rows) {
- if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
- ERREXIT(cinfo, JERR_NOTIMPL);
- alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
- ngroups = cinfo->min_DCT_scaled_size + 2;
- } else {
- ngroups = cinfo->min_DCT_scaled_size;
- }
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
- cinfo->min_DCT_scaled_size; /* height of a row group of component */
- main->buffer[ci] = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- compptr->width_in_blocks * compptr->DCT_scaled_size,
- (JDIMENSION) (rgroup * ngroups));
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdmainct.c
+ *
+ * Copyright (C) 1994-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 contains the main buffer controller for decompression.
+ * The main buffer lies between the JPEG decompressor proper and the
+ * post-processor; it holds downsampled data in the JPEG colorspace.
+ *
+ * Note that this code is bypassed in raw-data mode, since the application
+ * supplies the equivalent of the main buffer in that case.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * In the current system design, the main buffer need never be a full-image
+ * buffer; any full-height buffers will be found inside the coefficient or
+ * postprocessing controllers. Nonetheless, the main controller is not
+ * trivial. Its responsibility is to provide context rows for upsampling/
+ * rescaling, and doing this in an efficient fashion is a bit tricky.
+ *
+ * Postprocessor input data is counted in "row groups". A row group
+ * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
+ * sample rows of each component. (We require DCT_scaled_size values to be
+ * chosen such that these numbers are integers. In practice DCT_scaled_size
+ * values will likely be powers of two, so we actually have the stronger
+ * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
+ * Upsampling will typically produce max_v_samp_factor pixel rows from each
+ * row group (times any additional scale factor that the upsampler is
+ * applying).
+ *
+ * The coefficient controller will deliver data to us one iMCU row at a time;
+ * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
+ * exactly min_DCT_scaled_size row groups. (This amount of data corresponds
+ * to one row of MCUs when the image is fully interleaved.) Note that the
+ * number of sample rows varies across components, but the number of row
+ * groups does not. Some garbage sample rows may be included in the last iMCU
+ * row at the bottom of the image.
+ *
+ * Depending on the vertical scaling algorithm used, the upsampler may need
+ * access to the sample row(s) above and below its current input row group.
+ * The upsampler is required to set need_context_rows TRUE at global selection
+ * time if so. When need_context_rows is FALSE, this controller can simply
+ * obtain one iMCU row at a time from the coefficient controller and dole it
+ * out as row groups to the postprocessor.
+ *
+ * When need_context_rows is TRUE, this controller guarantees that the buffer
+ * passed to postprocessing contains at least one row group's worth of samples
+ * above and below the row group(s) being processed. Note that the context
+ * rows "above" the first passed row group appear at negative row offsets in
+ * the passed buffer. At the top and bottom of the image, the required
+ * context rows are manufactured by duplicating the first or last real sample
+ * row; this avoids having special cases in the upsampling inner loops.
+ *
+ * The amount of context is fixed at one row group just because that's a
+ * convenient number for this controller to work with. The existing
+ * upsamplers really only need one sample row of context. An upsampler
+ * supporting arbitrary output rescaling might wish for more than one row
+ * group of context when shrinking the image; tough, we don't handle that.
+ * (This is justified by the assumption that downsizing will be handled mostly
+ * by adjusting the DCT_scaled_size values, so that the actual scale factor at
+ * the upsample step needn't be much less than one.)
+ *
+ * To provide the desired context, we have to retain the last two row groups
+ * of one iMCU row while reading in the next iMCU row. (The last row group
+ * can't be processed until we have another row group for its below-context,
+ * and so we have to save the next-to-last group too for its above-context.)
+ * We could do this most simply by copying data around in our buffer, but
+ * that'd be very slow. We can avoid copying any data by creating a rather
+ * strange pointer structure. Here's how it works. We allocate a workspace
+ * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
+ * of row groups per iMCU row). We create two sets of redundant pointers to
+ * the workspace. Labeling the physical row groups 0 to M+1, the synthesized
+ * pointer lists look like this:
+ * M+1 M-1
+ * master pointer --> 0 master pointer --> 0
+ * 1 1
+ * ... ...
+ * M-3 M-3
+ * M-2 M
+ * M-1 M+1
+ * M M-2
+ * M+1 M-1
+ * 0 0
+ * We read alternate iMCU rows using each master pointer; thus the last two
+ * row groups of the previous iMCU row remain un-overwritten in the workspace.
+ * The pointer lists are set up so that the required context rows appear to
+ * be adjacent to the proper places when we pass the pointer lists to the
+ * upsampler.
+ *
+ * The above pictures describe the normal state of the pointer lists.
+ * At top and bottom of the image, we diddle the pointer lists to duplicate
+ * the first or last sample row as necessary (this is cheaper than copying
+ * sample rows around).
+ *
+ * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that
+ * situation each iMCU row provides only one row group so the buffering logic
+ * must be different (eg, we must read two iMCU rows before we can emit the
+ * first row group). For now, we simply do not support providing context
+ * rows when min_DCT_scaled_size is 1. That combination seems unlikely to
+ * be worth providing --- if someone wants a 1/8th-size preview, they probably
+ * want it quick and dirty, so a context-free upsampler is sufficient.
+ */
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_d_main_controller pub; /* public fields */
+
+ /* Pointer to allocated workspace (M or M+2 row groups). */
+ JSAMPARRAY buffer[MAX_COMPONENTS];
+
+ boolean buffer_full; /* Have we gotten an iMCU row from decoder? */
+ JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */
+
+ /* Remaining fields are only used in the context case. */
+
+ /* These are the master pointers to the funny-order pointer lists. */
+ JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */
+
+ int whichptr; /* indicates which pointer set is now in use */
+ int context_state; /* process_data state machine status */
+ JDIMENSION rowgroups_avail; /* row groups available to postprocessor */
+ JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */
+} my_main_controller;
+
+typedef my_main_controller * my_main_ptr;
+
+/* context_state values: */
+#define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */
+#define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */
+#define CTX_POSTPONED_ROW 2 /* feeding postponed row group */
+
+
+/* Forward declarations */
+METHODDEF(void) process_data_simple_main
+ JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+METHODDEF(void) process_data_context_main
+ JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+#ifdef QUANT_2PASS_SUPPORTED
+METHODDEF(void) process_data_crank_post
+ JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
+#endif
+
+
+LOCAL(void)
+alloc_funny_pointers (j_decompress_ptr cinfo)
+/* Allocate space for the funny pointer lists.
+ * This is done only once, not once per pass.
+ */
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci, rgroup;
+ int M = cinfo->min_DCT_scaled_size;
+ jpeg_component_info *compptr;
+ JSAMPARRAY xbuf;
+
+ /* Get top-level space for component array pointers.
+ * We alloc both arrays with one call to save a few cycles.
+ */
+ main->xbuffer[0] = (JSAMPIMAGE)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
+ main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size; /* height of a row group of component */
+ /* Get space for pointer lists --- M+4 row groups in each list.
+ * We alloc both pointer lists with one call to save a few cycles.
+ */
+ xbuf = (JSAMPARRAY)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ 2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
+ xbuf += rgroup; /* want one row group at negative offsets */
+ main->xbuffer[0][ci] = xbuf;
+ xbuf += rgroup * (M + 4);
+ main->xbuffer[1][ci] = xbuf;
+ }
+}
+
+
+LOCAL(void)
+make_funny_pointers (j_decompress_ptr cinfo)
+/* Create the funny pointer lists discussed in the comments above.
+ * The actual workspace is already allocated (in main->buffer),
+ * and the space for the pointer lists is allocated too.
+ * This routine just fills in the curiously ordered lists.
+ * This will be repeated at the beginning of each pass.
+ */
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci, i, rgroup;
+ int M = cinfo->min_DCT_scaled_size;
+ jpeg_component_info *compptr;
+ JSAMPARRAY buf, xbuf0, xbuf1;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size; /* height of a row group of component */
+ xbuf0 = main->xbuffer[0][ci];
+ xbuf1 = main->xbuffer[1][ci];
+ /* First copy the workspace pointers as-is */
+ buf = main->buffer[ci];
+ for (i = 0; i < rgroup * (M + 2); i++) {
+ xbuf0[i] = xbuf1[i] = buf[i];
+ }
+ /* In the second list, put the last four row groups in swapped order */
+ for (i = 0; i < rgroup * 2; i++) {
+ xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
+ xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
+ }
+ /* The wraparound pointers at top and bottom will be filled later
+ * (see set_wraparound_pointers, below). Initially we want the "above"
+ * pointers to duplicate the first actual data line. This only needs
+ * to happen in xbuffer[0].
+ */
+ for (i = 0; i < rgroup; i++) {
+ xbuf0[i - rgroup] = xbuf0[0];
+ }
+ }
+}
+
+
+LOCAL(void)
+set_wraparound_pointers (j_decompress_ptr cinfo)
+/* Set up the "wraparound" pointers at top and bottom of the pointer lists.
+ * This changes the pointer list state from top-of-image to the normal state.
+ */
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci, i, rgroup;
+ int M = cinfo->min_DCT_scaled_size;
+ jpeg_component_info *compptr;
+ JSAMPARRAY xbuf0, xbuf1;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size; /* height of a row group of component */
+ xbuf0 = main->xbuffer[0][ci];
+ xbuf1 = main->xbuffer[1][ci];
+ for (i = 0; i < rgroup; i++) {
+ xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
+ xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
+ xbuf0[rgroup*(M+2) + i] = xbuf0[i];
+ xbuf1[rgroup*(M+2) + i] = xbuf1[i];
+ }
+ }
+}
+
+
+LOCAL(void)
+set_bottom_pointers (j_decompress_ptr cinfo)
+/* Change the pointer lists to duplicate the last sample row at the bottom
+ * of the image. whichptr indicates which xbuffer holds the final iMCU row.
+ * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
+ */
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ int ci, i, rgroup, iMCUheight, rows_left;
+ jpeg_component_info *compptr;
+ JSAMPARRAY xbuf;
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Count sample rows in one iMCU row and in one row group */
+ iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
+ rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
+ /* Count nondummy sample rows remaining for this component */
+ rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
+ if (rows_left == 0) rows_left = iMCUheight;
+ /* Count nondummy row groups. Should get same answer for each component,
+ * so we need only do it once.
+ */
+ if (ci == 0) {
+ main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
+ }
+ /* Duplicate the last real sample row rgroup*2 times; this pads out the
+ * last partial rowgroup and ensures at least one full rowgroup of context.
+ */
+ xbuf = main->xbuffer[main->whichptr][ci];
+ for (i = 0; i < rgroup * 2; i++) {
+ xbuf[rows_left + i] = xbuf[rows_left-1];
+ }
+ }
+}
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+
+ switch (pass_mode) {
+ case JBUF_PASS_THRU:
+ if (cinfo->upsample->need_context_rows) {
+ main->pub.process_data = process_data_context_main;
+ make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
+ main->whichptr = 0; /* Read first iMCU row into xbuffer[0] */
+ main->context_state = CTX_PREPARE_FOR_IMCU;
+ main->iMCU_row_ctr = 0;
+ } else {
+ /* Simple case with no context needed */
+ main->pub.process_data = process_data_simple_main;
+ }
+ main->buffer_full = FALSE; /* Mark buffer empty */
+ main->rowgroup_ctr = 0;
+ break;
+#ifdef QUANT_2PASS_SUPPORTED
+ case JBUF_CRANK_DEST:
+ /* For last pass of 2-pass quantization, just crank the postprocessor */
+ main->pub.process_data = process_data_crank_post;
+ break;
+#endif
+ default:
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ break;
+ }
+}
+
+
+/*
+ * Process some data.
+ * This handles the simple case where no context is required.
+ */
+
+METHODDEF(void)
+process_data_simple_main (j_decompress_ptr cinfo,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+ JDIMENSION rowgroups_avail;
+
+ /* Read input data if we haven't filled the main buffer yet */
+ if (! main->buffer_full) {
+ if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
+ return; /* suspension forced, can do nothing more */
+ main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
+ }
+
+ /* There are always min_DCT_scaled_size row groups in an iMCU row. */
+ rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
+ /* Note: at the bottom of the image, we may pass extra garbage row groups
+ * to the postprocessor. The postprocessor has to check for bottom
+ * of image anyway (at row resolution), so no point in us doing it too.
+ */
+
+ /* Feed the postprocessor */
+ (*cinfo->post->post_process_data) (cinfo, main->buffer,
+ &main->rowgroup_ctr, rowgroups_avail,
+ output_buf, out_row_ctr, out_rows_avail);
+
+ /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
+ if (main->rowgroup_ctr >= rowgroups_avail) {
+ main->buffer_full = FALSE;
+ main->rowgroup_ctr = 0;
+ }
+}
+
+
+/*
+ * Process some data.
+ * This handles the case where context rows must be provided.
+ */
+
+METHODDEF(void)
+process_data_context_main (j_decompress_ptr cinfo,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_main_ptr main = (my_main_ptr) cinfo->main;
+
+ /* Read input data if we haven't filled the main buffer yet */
+ if (! main->buffer_full) {
+ if (! (*cinfo->coef->decompress_data) (cinfo,
+ main->xbuffer[main->whichptr]))
+ return; /* suspension forced, can do nothing more */
+ main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
+ main->iMCU_row_ctr++; /* count rows received */
+ }
+
+ /* Postprocessor typically will not swallow all the input data it is handed
+ * in one call (due to filling the output buffer first). Must be prepared
+ * to exit and restart. This switch lets us keep track of how far we got.
+ * Note that each case falls through to the next on successful completion.
+ */
+ switch (main->context_state) {
+ case CTX_POSTPONED_ROW:
+ /* Call postprocessor using previously set pointers for postponed row */
+ (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
+ &main->rowgroup_ctr, main->rowgroups_avail,
+ output_buf, out_row_ctr, out_rows_avail);
+ if (main->rowgroup_ctr < main->rowgroups_avail)
+ return; /* Need to suspend */
+ main->context_state = CTX_PREPARE_FOR_IMCU;
+ if (*out_row_ctr >= out_rows_avail)
+ return; /* Postprocessor exactly filled output buf */
+ /*FALLTHROUGH*/
+ case CTX_PREPARE_FOR_IMCU:
+ /* Prepare to process first M-1 row groups of this iMCU row */
+ main->rowgroup_ctr = 0;
+ main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
+ /* Check for bottom of image: if so, tweak pointers to "duplicate"
+ * the last sample row, and adjust rowgroups_avail to ignore padding rows.
+ */
+ if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
+ set_bottom_pointers(cinfo);
+ main->context_state = CTX_PROCESS_IMCU;
+ /*FALLTHROUGH*/
+ case CTX_PROCESS_IMCU:
+ /* Call postprocessor using previously set pointers */
+ (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
+ &main->rowgroup_ctr, main->rowgroups_avail,
+ output_buf, out_row_ctr, out_rows_avail);
+ if (main->rowgroup_ctr < main->rowgroups_avail)
+ return; /* Need to suspend */
+ /* After the first iMCU, change wraparound pointers to normal state */
+ if (main->iMCU_row_ctr == 1)
+ set_wraparound_pointers(cinfo);
+ /* Prepare to load new iMCU row using other xbuffer list */
+ main->whichptr ^= 1; /* 0=>1 or 1=>0 */
+ main->buffer_full = FALSE;
+ /* Still need to process last row group of this iMCU row, */
+ /* which is saved at index M+1 of the other xbuffer */
+ main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
+ main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
+ main->context_state = CTX_POSTPONED_ROW;
+ }
+}
+
+
+/*
+ * Process some data.
+ * Final pass of two-pass quantization: just call the postprocessor.
+ * Source data will be the postprocessor controller's internal buffer.
+ */
+
+#ifdef QUANT_2PASS_SUPPORTED
+
+METHODDEF(void)
+process_data_crank_post (j_decompress_ptr cinfo,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
+ (JDIMENSION *) NULL, (JDIMENSION) 0,
+ output_buf, out_row_ctr, out_rows_avail);
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
+
+
+/*
+ * Initialize main buffer controller.
+ */
+
+GLOBAL(void)
+jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+ my_main_ptr main;
+ int ci, rgroup, ngroups;
+ jpeg_component_info *compptr;
+
+ main = (my_main_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_main_controller));
+ cinfo->main = (struct jpeg_d_main_controller *) main;
+ main->pub.start_pass = start_pass_main;
+
+ if (need_full_buffer) /* shouldn't happen */
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+
+ /* Allocate the workspace.
+ * ngroups is the number of row groups we need.
+ */
+ if (cinfo->upsample->need_context_rows) {
+ if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
+ ERREXIT(cinfo, JERR_NOTIMPL);
+ alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
+ ngroups = cinfo->min_DCT_scaled_size + 2;
+ } else {
+ ngroups = cinfo->min_DCT_scaled_size;
+ }
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ rgroup = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size; /* height of a row group of component */
+ main->buffer[ci] = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ compptr->width_in_blocks * compptr->DCT_scaled_size,
+ (JDIMENSION) (rgroup * ngroups));
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c b/core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c
index 60f2139f58..bcd017f920 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdmarker.c
@@ -1,1396 +1,1396 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdmarker.c
- *
- * Copyright (C) 1991-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 routines to decode JPEG datastream markers.
- * Most of the complexity arises from our desire to support input
- * suspension: if not all of the data for a marker is available,
- * we must exit back to the application. On resumption, we reprocess
- * the marker.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-typedef enum { /* JPEG marker codes */
- M_SOF0 = 0xc0,
- M_SOF1 = 0xc1,
- M_SOF2 = 0xc2,
- M_SOF3 = 0xc3,
-
- M_SOF5 = 0xc5,
- M_SOF6 = 0xc6,
- M_SOF7 = 0xc7,
-
- M_JPG = 0xc8,
- M_SOF9 = 0xc9,
- M_SOF10 = 0xca,
- M_SOF11 = 0xcb,
-
- M_SOF13 = 0xcd,
- M_SOF14 = 0xce,
- M_SOF15 = 0xcf,
-
- M_DHT = 0xc4,
-
- M_DAC = 0xcc,
-
- M_RST0 = 0xd0,
- M_RST1 = 0xd1,
- M_RST2 = 0xd2,
- M_RST3 = 0xd3,
- M_RST4 = 0xd4,
- M_RST5 = 0xd5,
- M_RST6 = 0xd6,
- M_RST7 = 0xd7,
-
- M_SOI = 0xd8,
- M_EOI = 0xd9,
- M_SOS = 0xda,
- M_DQT = 0xdb,
- M_DNL = 0xdc,
- M_DRI = 0xdd,
- M_DHP = 0xde,
- M_EXP = 0xdf,
-
- M_APP0 = 0xe0,
- M_APP1 = 0xe1,
- M_APP2 = 0xe2,
- M_APP3 = 0xe3,
- M_APP4 = 0xe4,
- M_APP5 = 0xe5,
- M_APP6 = 0xe6,
- M_APP7 = 0xe7,
- M_APP8 = 0xe8,
- M_APP9 = 0xe9,
- M_APP10 = 0xea,
- M_APP11 = 0xeb,
- M_APP12 = 0xec,
- M_APP13 = 0xed,
- M_APP14 = 0xee,
- M_APP15 = 0xef,
-
- M_JPG0 = 0xf0,
- M_JPG13 = 0xfd,
- M_COM = 0xfe,
-
- M_TEM = 0x01,
-
- M_ERROR = 0x100
-} JPEG_MARKER;
-
-
-/* Private state */
-
-typedef struct {
- struct jpeg_marker_reader pub; /* public fields */
-
- /* Application-overridable marker processing methods */
- jpeg_marker_parser_method process_COM;
- jpeg_marker_parser_method process_APPn[16];
-
- /* Limit on marker data length to save for each marker type */
- unsigned int length_limit_COM;
- unsigned int length_limit_APPn[16];
-
- /* Status of COM/APPn marker saving */
- jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */
- unsigned int bytes_read; /* data bytes read so far in marker */
- /* Note: cur_marker is not linked into marker_list until it's all read. */
-} my_marker_reader;
-
-typedef my_marker_reader * my_marker_ptr;
-
-
-/*
- * Macros for fetching data from the data source module.
- *
- * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
- * the current restart point; we update them only when we have reached a
- * suitable place to restart if a suspension occurs.
- */
-
-/* Declare and initialize local copies of input pointer/count */
-#define INPUT_VARS(cinfo) \
- struct jpeg_source_mgr * datasrc = (cinfo)->src; \
- const JOCTET * next_input_byte = datasrc->next_input_byte; \
- size_t bytes_in_buffer = datasrc->bytes_in_buffer
-
-/* Unload the local copies --- do this only at a restart boundary */
-#define INPUT_SYNC(cinfo) \
- ( datasrc->next_input_byte = next_input_byte, \
- datasrc->bytes_in_buffer = bytes_in_buffer )
-
-/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
-#define INPUT_RELOAD(cinfo) \
- ( next_input_byte = datasrc->next_input_byte, \
- bytes_in_buffer = datasrc->bytes_in_buffer )
-
-/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
- * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
- * but we must reload the local copies after a successful fill.
- */
-#define MAKE_BYTE_AVAIL(cinfo,action) \
- if (bytes_in_buffer == 0) { \
- if (! (*datasrc->fill_input_buffer) (cinfo)) \
- { action; } \
- INPUT_RELOAD(cinfo); \
- }
-
-/* Read a byte into variable V.
- * If must suspend, take the specified action (typically "return FALSE").
- */
-#define INPUT_BYTE(cinfo,V,action) \
- MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
- bytes_in_buffer--; \
- V = GETJOCTET(*next_input_byte++); )
-
-/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
- * V should be declared unsigned int or perhaps INT32.
- */
-#define INPUT_2BYTES(cinfo,V,action) \
- MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
- bytes_in_buffer--; \
- V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
- MAKE_BYTE_AVAIL(cinfo,action); \
- bytes_in_buffer--; \
- V += GETJOCTET(*next_input_byte++); )
-
-
-/*
- * Routines to process JPEG markers.
- *
- * Entry condition: JPEG marker itself has been read and its code saved
- * in cinfo->unread_marker; input restart point is just after the marker.
- *
- * Exit: if return TRUE, have read and processed any parameters, and have
- * updated the restart point to point after the parameters.
- * If return FALSE, was forced to suspend before reaching end of
- * marker parameters; restart point has not been moved. Same routine
- * will be called again after application supplies more input data.
- *
- * This approach to suspension assumes that all of a marker's parameters
- * can fit into a single input bufferload. This should hold for "normal"
- * markers. Some COM/APPn markers might have large parameter segments
- * that might not fit. If we are simply dropping such a marker, we use
- * skip_input_data to get past it, and thereby put the problem on the
- * source manager's shoulders. If we are saving the marker's contents
- * into memory, we use a slightly different convention: when forced to
- * suspend, the marker processor updates the restart point to the end of
- * what it's consumed (ie, the end of the buffer) before returning FALSE.
- * On resumption, cinfo->unread_marker still contains the marker code,
- * but the data source will point to the next chunk of marker data.
- * The marker processor must retain internal state to deal with this.
- *
- * Note that we don't bother to avoid duplicate trace messages if a
- * suspension occurs within marker parameters. Other side effects
- * require more care.
- */
-
-
-LOCAL(boolean)
-get_soi (j_decompress_ptr cinfo)
-/* Process an SOI marker */
-{
- int i;
-
- TRACEMS(cinfo, 1, JTRC_SOI);
-
- if (cinfo->marker->saw_SOI)
- ERREXIT(cinfo, JERR_SOI_DUPLICATE);
-
- /* Reset all parameters that are defined to be reset by SOI */
-
- for (i = 0; i < NUM_ARITH_TBLS; i++) {
- cinfo->arith_dc_L[i] = 0;
- cinfo->arith_dc_U[i] = 1;
- cinfo->arith_ac_K[i] = 5;
- }
- cinfo->restart_interval = 0;
-
- /* Set initial assumptions for colorspace etc */
-
- cinfo->jpeg_color_space = JCS_UNKNOWN;
- cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
-
- cinfo->saw_JFIF_marker = FALSE;
- cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
- cinfo->JFIF_minor_version = 1;
- cinfo->density_unit = 0;
- cinfo->X_density = 1;
- cinfo->Y_density = 1;
- cinfo->saw_Adobe_marker = FALSE;
- cinfo->Adobe_transform = 0;
-
- cinfo->marker->saw_SOI = TRUE;
-
- return TRUE;
-}
-
-
-LOCAL(boolean)
-get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
-/* Process a SOFn marker */
-{
- INT32 length;
- int c, ci;
- jpeg_component_info * compptr;
- /* LiuSunliang added 20111209 */
- JDIMENSION image_width, image_height;
- INPUT_VARS(cinfo);
-
- cinfo->progressive_mode = is_prog;
- cinfo->arith_code = is_arith;
-
- INPUT_2BYTES(cinfo, length, return FALSE);
-
- INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
- INPUT_2BYTES(cinfo, image_height, return FALSE);
- INPUT_2BYTES(cinfo, image_width, return FALSE);
- INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
-
- if (image_width <= JPEG_MAX_DIMENSION)
- cinfo->image_width = image_width;
-
- if (image_height <= JPEG_MAX_DIMENSION)
- cinfo->image_height = image_height;
-
- length -= 8;
-
- TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
- (int) cinfo->image_width, (int) cinfo->image_height,
- cinfo->num_components);
-
- if (cinfo->marker->saw_SOF)
- ERREXIT(cinfo, JERR_SOF_DUPLICATE);
-
- /* We don't support files in which the image height is initially specified */
- /* as 0 and is later redefined by DNL. As long as we have to check that, */
- /* might as well have a general sanity check. */
- if (cinfo->image_height <= 0 || cinfo->image_width <= 0
- || cinfo->num_components <= 0)
- ERREXIT(cinfo, JERR_EMPTY_IMAGE);
-
- if (length != (cinfo->num_components * 3))
- ERREXIT(cinfo, JERR_BAD_LENGTH);
-
- if (cinfo->comp_info == NULL) /* do only once, even if suspend */
- cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- cinfo->num_components * SIZEOF(jpeg_component_info));
-
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- compptr->component_index = ci;
- INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
- /* XYQ 2008-03-25: Adobe CMYK JPEG has serious flaw: the K channel has same component id as C channel */
- {
- int i;
- for (i = 0; i < ci; i ++)
- if (compptr->component_id == cinfo->comp_info[i].component_id) break;
- if (i < ci)
- /* Found the error! We replace the id with something unlikely used elsewhere */
- compptr->component_id += 0xf0;
- }
- /* end of modification */
- INPUT_BYTE(cinfo, c, return FALSE);
- compptr->h_samp_factor = (c >> 4) & 15;
- compptr->v_samp_factor = (c ) & 15;
- INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
-
- TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
- compptr->component_id, compptr->h_samp_factor,
- compptr->v_samp_factor, compptr->quant_tbl_no);
- }
-
- cinfo->marker->saw_SOF = TRUE;
-
- INPUT_SYNC(cinfo);
- return TRUE;
-}
-
-
-LOCAL(boolean)
-get_sos (j_decompress_ptr cinfo)
-/* Process a SOS marker */
-{
- INT32 length;
- int i, ci, n, c, cc;
- jpeg_component_info * compptr;
- INPUT_VARS(cinfo);
-
- if (! cinfo->marker->saw_SOF)
- ERREXIT(cinfo, JERR_SOS_NO_SOF);
-
- INPUT_2BYTES(cinfo, length, return FALSE);
-
- INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
-
- TRACEMS1(cinfo, 1, JTRC_SOS, n);
-
- if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
- ERREXIT(cinfo, JERR_BAD_LENGTH);
-
- cinfo->comps_in_scan = n;
-
- /* Collect the component-spec parameters */
-
- for (i = 0; i < n; i++) {
- INPUT_BYTE(cinfo, cc, return FALSE);
- INPUT_BYTE(cinfo, c, return FALSE);
-
- /* XYQ 2008-03-25: Adobe CMYK JPEG has serious flaw: the K channel has same component id as C channel */
- {
- int j;
- for (j = 0; j < i; j ++)
- if (cc == cinfo->cur_comp_info[j]->component_id) break;
- if (j < i)
- /* found the error! */
- cc += 0xf0;
- }
- /* end of modification */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- if (cc == compptr->component_id)
- goto id_found;
- }
-
- ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
-
- id_found:
-
- cinfo->cur_comp_info[i] = compptr;
- compptr->dc_tbl_no = (c >> 4) & 15;
- compptr->ac_tbl_no = (c ) & 15;
-
- TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
- compptr->dc_tbl_no, compptr->ac_tbl_no);
- /* This CSi (cc) should differ from the previous CSi */
- for (ci = 0; ci < i; ci++) {
- if (cinfo->cur_comp_info[ci] == compptr)
- ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
- }
- }
-
- /* Collect the additional scan parameters Ss, Se, Ah/Al. */
- INPUT_BYTE(cinfo, c, return FALSE);
- cinfo->Ss = c;
- INPUT_BYTE(cinfo, c, return FALSE);
- cinfo->Se = c;
- INPUT_BYTE(cinfo, c, return FALSE);
- cinfo->Ah = (c >> 4) & 15;
- cinfo->Al = (c ) & 15;
-
- TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
- cinfo->Ah, cinfo->Al);
-
- /* Prepare to scan data & restart markers */
- cinfo->marker->next_restart_num = 0;
-
- /* Count another SOS marker */
- cinfo->input_scan_number++;
-
- INPUT_SYNC(cinfo);
- return TRUE;
-}
-
-
-#ifdef D_ARITH_CODING_SUPPORTED
-
-LOCAL(boolean)
-get_dac (j_decompress_ptr cinfo)
-/* Process a DAC marker */
-{
- INT32 length;
- int index, val;
- INPUT_VARS(cinfo);
-
- INPUT_2BYTES(cinfo, length, return FALSE);
- length -= 2;
-
- while (length > 0) {
- INPUT_BYTE(cinfo, index, return FALSE);
- INPUT_BYTE(cinfo, val, return FALSE);
-
- length -= 2;
-
- TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
-
- if (index < 0 || index >= (2*NUM_ARITH_TBLS))
- ERREXIT1(cinfo, JERR_DAC_INDEX, index);
-
- if (index >= NUM_ARITH_TBLS) { /* define AC table */
- cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
- } else { /* define DC table */
- cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
- cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
- if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
- ERREXIT1(cinfo, JERR_DAC_VALUE, val);
- }
- }
-
- if (length != 0)
- ERREXIT(cinfo, JERR_BAD_LENGTH);
-
- INPUT_SYNC(cinfo);
- return TRUE;
-}
-
-#else /* ! D_ARITH_CODING_SUPPORTED */
-
-#define get_dac(cinfo) skip_variable(cinfo)
-
-#endif /* D_ARITH_CODING_SUPPORTED */
-
-
-LOCAL(boolean)
-get_dht (j_decompress_ptr cinfo)
-/* Process a DHT marker */
-{
- INT32 length;
- UINT8 bits[17];
- UINT8 huffval[256];
- int i, index, count;
- JHUFF_TBL **htblptr;
- INPUT_VARS(cinfo);
-
- INPUT_2BYTES(cinfo, length, return FALSE);
- length -= 2;
-
- while (length > 16) {
- INPUT_BYTE(cinfo, index, return FALSE);
-
- TRACEMS1(cinfo, 1, JTRC_DHT, index);
-
- bits[0] = 0;
- count = 0;
- for (i = 1; i <= 16; i++) {
- INPUT_BYTE(cinfo, bits[i], return FALSE);
- count += bits[i];
- }
-
- length -= 1 + 16;
-
- TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
- bits[1], bits[2], bits[3], bits[4],
- bits[5], bits[6], bits[7], bits[8]);
- TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
- bits[9], bits[10], bits[11], bits[12],
- bits[13], bits[14], bits[15], bits[16]);
-
- /* Here we just do minimal validation of the counts to avoid walking
- * off the end of our table space. jdhuff.c will check more carefully.
- */
- if (count > 256 || ((INT32) count) > length)
- ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
-
- for (i = 0; i < count; i++)
- INPUT_BYTE(cinfo, huffval[i], return FALSE);
-
- length -= count;
-
- if (index & 0x10) { /* AC table definition */
- index -= 0x10;
- htblptr = &cinfo->ac_huff_tbl_ptrs[index];
- } else { /* DC table definition */
- htblptr = &cinfo->dc_huff_tbl_ptrs[index];
- }
-
- if (index < 0 || index >= NUM_HUFF_TBLS)
- ERREXIT1(cinfo, JERR_DHT_INDEX, index);
-
- if (*htblptr == NULL)
- *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
-
- MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
- MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
- }
-
- if (length != 0)
- ERREXIT(cinfo, JERR_BAD_LENGTH);
-
- INPUT_SYNC(cinfo);
- return TRUE;
-}
-
-
-LOCAL(boolean)
-get_dqt (j_decompress_ptr cinfo)
-/* Process a DQT marker */
-{
- INT32 length;
- int n, i, prec;
- unsigned int tmp;
- JQUANT_TBL *quant_ptr;
- INPUT_VARS(cinfo);
-
- INPUT_2BYTES(cinfo, length, return FALSE);
- length -= 2;
-
- while (length > 0) {
- INPUT_BYTE(cinfo, n, return FALSE);
- prec = n >> 4;
- n &= 0x0F;
-
- TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
-
- if (n >= NUM_QUANT_TBLS)
- ERREXIT1(cinfo, JERR_DQT_INDEX, n);
-
- if (cinfo->quant_tbl_ptrs[n] == NULL)
- cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
- quant_ptr = cinfo->quant_tbl_ptrs[n];
-
- for (i = 0; i < DCTSIZE2; i++) {
- if (prec)
- INPUT_2BYTES(cinfo, tmp, return FALSE);
- else
- INPUT_BYTE(cinfo, tmp, return FALSE);
- /* We convert the zigzag-order table to natural array order. */
- quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
- }
-
- if (cinfo->err->trace_level >= 2) {
- for (i = 0; i < DCTSIZE2; i += 8) {
- TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
- quant_ptr->quantval[i], quant_ptr->quantval[i+1],
- quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
- quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
- quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
- }
- }
-
- length -= DCTSIZE2+1;
- if (prec) length -= DCTSIZE2;
- }
-
- if (length != 0)
- ERREXIT(cinfo, JERR_BAD_LENGTH);
-
- INPUT_SYNC(cinfo);
- return TRUE;
-}
-
-
-LOCAL(boolean)
-get_dri (j_decompress_ptr cinfo)
-/* Process a DRI marker */
-{
- INT32 length;
- unsigned int tmp;
- INPUT_VARS(cinfo);
-
- INPUT_2BYTES(cinfo, length, return FALSE);
-
- if (length != 4)
- ERREXIT(cinfo, JERR_BAD_LENGTH);
-
- INPUT_2BYTES(cinfo, tmp, return FALSE);
-
- TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
-
- cinfo->restart_interval = tmp;
-
- INPUT_SYNC(cinfo);
- return TRUE;
-}
-
-
-/*
- * Routines for processing APPn and COM markers.
- * These are either saved in memory or discarded, per application request.
- * APP0 and APP14 are specially checked to see if they are
- * JFIF and Adobe markers, respectively.
- */
-
-#define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */
-#define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */
-#define APPN_DATA_LEN 14 /* Must be the largest of the above!! */
-
-
-LOCAL(void)
-examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
- unsigned int datalen, INT32 remaining)
-/* Examine first few bytes from an APP0.
- * Take appropriate action if it is a JFIF marker.
- * datalen is # of bytes at data[], remaining is length of rest of marker data.
- */
-{
- INT32 totallen = (INT32) datalen + remaining;
-
- if (datalen >= APP0_DATA_LEN &&
- GETJOCTET(data[0]) == 0x4A &&
- GETJOCTET(data[1]) == 0x46 &&
- GETJOCTET(data[2]) == 0x49 &&
- GETJOCTET(data[3]) == 0x46 &&
- GETJOCTET(data[4]) == 0) {
- /* Found JFIF APP0 marker: save info */
- cinfo->saw_JFIF_marker = TRUE;
- cinfo->JFIF_major_version = GETJOCTET(data[5]);
- cinfo->JFIF_minor_version = GETJOCTET(data[6]);
- cinfo->density_unit = GETJOCTET(data[7]);
- cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
- cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
- /* Check version.
- * Major version must be 1, anything else signals an incompatible change.
- * (We used to treat this as an error, but now it's a nonfatal warning,
- * because some bozo at Hijaak couldn't read the spec.)
- * Minor version should be 0..2, but process anyway if newer.
- */
- if (cinfo->JFIF_major_version != 1)
- WARNMS2(cinfo, JWRN_JFIF_MAJOR,
- cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
- /* Generate trace messages */
- TRACEMS5(cinfo, 1, JTRC_JFIF,
- cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
- cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
- /* Validate thumbnail dimensions and issue appropriate messages */
- if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
- TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
- GETJOCTET(data[12]), GETJOCTET(data[13]));
- totallen -= APP0_DATA_LEN;
- if (totallen !=
- ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
- TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
- } else if (datalen >= 6 &&
- GETJOCTET(data[0]) == 0x4A &&
- GETJOCTET(data[1]) == 0x46 &&
- GETJOCTET(data[2]) == 0x58 &&
- GETJOCTET(data[3]) == 0x58 &&
- GETJOCTET(data[4]) == 0) {
- /* Found JFIF "JFXX" extension APP0 marker */
- /* The library doesn't actually do anything with these,
- * but we try to produce a helpful trace message.
- */
- switch (GETJOCTET(data[5])) {
- case 0x10:
- TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
- break;
- case 0x11:
- TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
- break;
- case 0x13:
- TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
- break;
- default:
- TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
- GETJOCTET(data[5]), (int) totallen);
- break;
- }
- } else {
- /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
- TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
- }
-}
-
-
-LOCAL(void)
-examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
- unsigned int datalen, INT32 remaining)
-/* Examine first few bytes from an APP14.
- * Take appropriate action if it is an Adobe marker.
- * datalen is # of bytes at data[], remaining is length of rest of marker data.
- */
-{
- unsigned int version, flags0, flags1, transform;
-
- if (datalen >= APP14_DATA_LEN &&
- GETJOCTET(data[0]) == 0x41 &&
- GETJOCTET(data[1]) == 0x64 &&
- GETJOCTET(data[2]) == 0x6F &&
- GETJOCTET(data[3]) == 0x62 &&
- GETJOCTET(data[4]) == 0x65) {
- /* Found Adobe APP14 marker */
- version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
- flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
- flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
- transform = GETJOCTET(data[11]);
- TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
- cinfo->saw_Adobe_marker = TRUE;
- cinfo->Adobe_transform = (UINT8) transform;
- } else {
- /* Start of APP14 does not match "Adobe", or too short */
- TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
- }
-}
-
-
-METHODDEF(boolean)
-get_interesting_appn (j_decompress_ptr cinfo)
-/* Process an APP0 or APP14 marker without saving it */
-{
- INT32 length;
- JOCTET b[APPN_DATA_LEN];
- unsigned int i, numtoread;
- INPUT_VARS(cinfo);
-
- INPUT_2BYTES(cinfo, length, return FALSE);
- length -= 2;
-
- /* get the interesting part of the marker data */
- if (length >= APPN_DATA_LEN)
- numtoread = APPN_DATA_LEN;
- else if (length > 0)
- numtoread = (unsigned int) length;
- else
- numtoread = 0;
- for (i = 0; i < numtoread; i++)
- INPUT_BYTE(cinfo, b[i], return FALSE);
- length -= numtoread;
-
- /* process it */
- switch (cinfo->unread_marker) {
- case M_APP0:
- examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
- break;
- case M_APP14:
- examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
- break;
- default:
- /* can't get here unless jpeg_save_markers chooses wrong processor */
- ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
- break;
- }
-
- /* skip any remaining data -- could be lots */
- INPUT_SYNC(cinfo);
- if (length > 0)
- (*cinfo->src->skip_input_data) (cinfo, (long) length);
-
- return TRUE;
-}
-
-
-#ifdef SAVE_MARKERS_SUPPORTED
-
-METHODDEF(boolean)
-save_marker (j_decompress_ptr cinfo)
-/* Save an APPn or COM marker into the marker list */
-{
- my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
- jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
- unsigned int bytes_read, data_length;
- JOCTET FAR * data;
- INT32 length = 0;
- INPUT_VARS(cinfo);
-
- if (cur_marker == NULL) {
- /* begin reading a marker */
- INPUT_2BYTES(cinfo, length, return FALSE);
- length -= 2;
- if (length >= 0) { /* watch out for bogus length word */
- /* figure out how much we want to save */
- unsigned int limit;
- if (cinfo->unread_marker == (int) M_COM)
- limit = marker->length_limit_COM;
- else
- limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
- if ((unsigned int) length < limit)
- limit = (unsigned int) length;
- /* allocate and initialize the marker item */
- cur_marker = (jpeg_saved_marker_ptr)
- (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(struct jpeg_marker_struct) + limit);
- cur_marker->next = NULL;
- cur_marker->marker = (UINT8) cinfo->unread_marker;
- cur_marker->original_length = (unsigned int) length;
- cur_marker->data_length = limit;
- /* data area is just beyond the jpeg_marker_struct */
- data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
- marker->cur_marker = cur_marker;
- marker->bytes_read = 0;
- bytes_read = 0;
- data_length = limit;
- } else {
- /* deal with bogus length word */
- bytes_read = data_length = 0;
- data = NULL;
- }
- } else {
- /* resume reading a marker */
- bytes_read = marker->bytes_read;
- data_length = cur_marker->data_length;
- data = cur_marker->data + bytes_read;
- }
-
- while (bytes_read < data_length) {
- INPUT_SYNC(cinfo); /* move the restart point to here */
- marker->bytes_read = bytes_read;
- /* If there's not at least one byte in buffer, suspend */
- MAKE_BYTE_AVAIL(cinfo, return FALSE);
- /* Copy bytes with reasonable rapidity */
- while (bytes_read < data_length && bytes_in_buffer > 0) {
- *data++ = *next_input_byte++;
- bytes_in_buffer--;
- bytes_read++;
- }
- }
-
- /* Done reading what we want to read */
- if (cur_marker != NULL) { /* will be NULL if bogus length word */
- /* Add new marker to end of list */
- if (cinfo->marker_list == NULL) {
- cinfo->marker_list = cur_marker;
- } else {
- jpeg_saved_marker_ptr prev = cinfo->marker_list;
- while (prev->next != NULL)
- prev = prev->next;
- prev->next = cur_marker;
- }
- /* Reset pointer & calc remaining data length */
- data = cur_marker->data;
- length = cur_marker->original_length - data_length;
- }
- /* Reset to initial state for next marker */
- marker->cur_marker = NULL;
-
- /* Process the marker if interesting; else just make a generic trace msg */
- switch (cinfo->unread_marker) {
- case M_APP0:
- examine_app0(cinfo, data, data_length, length);
- break;
- case M_APP14:
- examine_app14(cinfo, data, data_length, length);
- break;
- default:
- TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
- (int) (data_length + length));
- break;
- }
-
- /* skip any remaining data -- could be lots */
- INPUT_SYNC(cinfo); /* do before skip_input_data */
- if (length > 0)
- (*cinfo->src->skip_input_data) (cinfo, (long) length);
-
- return TRUE;
-}
-
-#endif /* SAVE_MARKERS_SUPPORTED */
-
-
-METHODDEF(boolean)
-skip_variable (j_decompress_ptr cinfo)
-/* Skip over an unknown or uninteresting variable-length marker */
-{
- INT32 length;
- INPUT_VARS(cinfo);
-
- INPUT_2BYTES(cinfo, length, return FALSE);
- length -= 2;
-
- TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
-
- INPUT_SYNC(cinfo); /* do before skip_input_data */
- if (length > 0)
- (*cinfo->src->skip_input_data) (cinfo, (long) length);
-
- return TRUE;
-}
-
-
-/*
- * Find the next JPEG marker, save it in cinfo->unread_marker.
- * Returns FALSE if had to suspend before reaching a marker;
- * in that case cinfo->unread_marker is unchanged.
- *
- * Note that the result might not be a valid marker code,
- * but it will never be 0 or FF.
- */
-
-LOCAL(boolean)
-next_marker (j_decompress_ptr cinfo)
-{
- int c;
- INPUT_VARS(cinfo);
-
- for (;;) {
- INPUT_BYTE(cinfo, c, return FALSE);
- /* Skip any non-FF bytes.
- * This may look a bit inefficient, but it will not occur in a valid file.
- * We sync after each discarded byte so that a suspending data source
- * can discard the byte from its buffer.
- */
- while (c != 0xFF) {
- cinfo->marker->discarded_bytes++;
- INPUT_SYNC(cinfo);
- INPUT_BYTE(cinfo, c, return FALSE);
- }
- /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
- * pad bytes, so don't count them in discarded_bytes. We assume there
- * will not be so many consecutive FF bytes as to overflow a suspending
- * data source's input buffer.
- */
- do {
- INPUT_BYTE(cinfo, c, return FALSE);
- } while (c == 0xFF);
- if (c != 0)
- break; /* found a valid marker, exit loop */
- /* Reach here if we found a stuffed-zero data sequence (FF/00).
- * Discard it and loop back to try again.
- */
- cinfo->marker->discarded_bytes += 2;
- INPUT_SYNC(cinfo);
- }
-
- if (cinfo->marker->discarded_bytes != 0) {
- WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
- cinfo->marker->discarded_bytes = 0;
- }
-
- cinfo->unread_marker = c;
-
- INPUT_SYNC(cinfo);
- return TRUE;
-}
-
-
-LOCAL(boolean)
-first_marker (j_decompress_ptr cinfo)
-/* Like next_marker, but used to obtain the initial SOI marker. */
-/* For this marker, we do not allow preceding garbage or fill; otherwise,
- * we might well scan an entire input file before realizing it ain't JPEG.
- * If an application wants to process non-JFIF files, it must seek to the
- * SOI before calling the JPEG library.
- */
-{
- int c, c2;
- INPUT_VARS(cinfo);
-
- INPUT_BYTE(cinfo, c, return FALSE);
- INPUT_BYTE(cinfo, c2, return FALSE);
- if (c != 0xFF || c2 != (int) M_SOI)
- ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
-
- cinfo->unread_marker = c2;
-
- INPUT_SYNC(cinfo);
- return TRUE;
-}
-
-
-/*
- * Read markers until SOS or EOI.
- *
- * Returns same codes as are defined for jpeg_consume_input:
- * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
- */
-
-METHODDEF(int)
-read_markers (j_decompress_ptr cinfo)
-{
- /* Outer loop repeats once for each marker. */
- for (;;) {
- /* Collect the marker proper, unless we already did. */
- /* NB: first_marker() enforces the requirement that SOI appear first. */
- if (cinfo->unread_marker == 0) {
- if (! cinfo->marker->saw_SOI) {
- if (! first_marker(cinfo))
- return JPEG_SUSPENDED;
- } else {
- if (! next_marker(cinfo))
- return JPEG_SUSPENDED;
- }
- }
- /* At this point cinfo->unread_marker contains the marker code and the
- * input point is just past the marker proper, but before any parameters.
- * A suspension will cause us to return with this state still true.
- */
- switch (cinfo->unread_marker) {
- case M_SOI:
- if (! get_soi(cinfo))
- return JPEG_SUSPENDED;
- break;
-
- case M_SOF0: /* Baseline */
- case M_SOF1: /* Extended sequential, Huffman */
- if (! get_sof(cinfo, FALSE, FALSE))
- return JPEG_SUSPENDED;
- break;
-
- case M_SOF2: /* Progressive, Huffman */
- if (! get_sof(cinfo, TRUE, FALSE))
- return JPEG_SUSPENDED;
- break;
-
- case M_SOF9: /* Extended sequential, arithmetic */
- if (! get_sof(cinfo, FALSE, TRUE))
- return JPEG_SUSPENDED;
- break;
-
- case M_SOF10: /* Progressive, arithmetic */
- if (! get_sof(cinfo, TRUE, TRUE))
- return JPEG_SUSPENDED;
- break;
-
- /* Currently unsupported SOFn types */
- case M_SOF3: /* Lossless, Huffman */
- case M_SOF5: /* Differential sequential, Huffman */
- case M_SOF6: /* Differential progressive, Huffman */
- case M_SOF7: /* Differential lossless, Huffman */
- case M_JPG: /* Reserved for JPEG extensions */
- case M_SOF11: /* Lossless, arithmetic */
- case M_SOF13: /* Differential sequential, arithmetic */
- case M_SOF14: /* Differential progressive, arithmetic */
- case M_SOF15: /* Differential lossless, arithmetic */
- ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
- break;
-
- case M_SOS:
- if (! get_sos(cinfo))
- return JPEG_SUSPENDED;
- cinfo->unread_marker = 0; /* processed the marker */
- return JPEG_REACHED_SOS;
-
- case M_EOI:
- TRACEMS(cinfo, 1, JTRC_EOI);
- cinfo->unread_marker = 0; /* processed the marker */
- return JPEG_REACHED_EOI;
-
- case M_DAC:
- if (! get_dac(cinfo))
- return JPEG_SUSPENDED;
- break;
-
- case M_DHT:
- if (! get_dht(cinfo))
- return JPEG_SUSPENDED;
- break;
-
- case M_DQT:
- if (! get_dqt(cinfo))
- return JPEG_SUSPENDED;
- break;
-
- case M_DRI:
- if (! get_dri(cinfo))
- return JPEG_SUSPENDED;
- break;
-
- case M_APP0:
- case M_APP1:
- case M_APP2:
- case M_APP3:
- case M_APP4:
- case M_APP5:
- case M_APP6:
- case M_APP7:
- case M_APP8:
- case M_APP9:
- case M_APP10:
- case M_APP11:
- case M_APP12:
- case M_APP13:
- case M_APP14:
- case M_APP15:
- if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
- cinfo->unread_marker - (int) M_APP0]) (cinfo))
- return JPEG_SUSPENDED;
- break;
-
- case M_COM:
- if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
- return JPEG_SUSPENDED;
- break;
-
- case M_RST0: /* these are all parameterless */
- case M_RST1:
- case M_RST2:
- case M_RST3:
- case M_RST4:
- case M_RST5:
- case M_RST6:
- case M_RST7:
- case M_TEM:
- TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
- break;
-
- case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
- if (! skip_variable(cinfo))
- return JPEG_SUSPENDED;
- break;
-
- default: /* must be DHP, EXP, JPGn, or RESn */
- /* For now, we treat the reserved markers as fatal errors since they are
- * likely to be used to signal incompatible JPEG Part 3 extensions.
- * Once the JPEG 3 version-number marker is well defined, this code
- * ought to change!
- */
- ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
- break;
- }
- /* Successfully processed marker, so reset state variable */
- cinfo->unread_marker = 0;
- } /* end loop */
-}
-
-
-/*
- * Read a restart marker, which is expected to appear next in the datastream;
- * if the marker is not there, take appropriate recovery action.
- * Returns FALSE if suspension is required.
- *
- * This is called by the entropy decoder after it has read an appropriate
- * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
- * has already read a marker from the data source. Under normal conditions
- * cinfo->unread_marker will be reset to 0 before returning; if not reset,
- * it holds a marker which the decoder will be unable to read past.
- */
-
-METHODDEF(boolean)
-read_restart_marker (j_decompress_ptr cinfo)
-{
- /* Obtain a marker unless we already did. */
- /* Note that next_marker will complain if it skips any data. */
- if (cinfo->unread_marker == 0) {
- if (! next_marker(cinfo))
- return FALSE;
- }
-
- if (cinfo->unread_marker ==
- ((int) M_RST0 + cinfo->marker->next_restart_num)) {
- /* Normal case --- swallow the marker and let entropy decoder continue */
- TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
- cinfo->unread_marker = 0;
- } else {
- /* Uh-oh, the restart markers have been messed up. */
- /* Let the data source manager determine how to resync. */
- if (! (*cinfo->src->resync_to_restart) (cinfo,
- cinfo->marker->next_restart_num))
- return FALSE;
- }
-
- /* Update next-restart state */
- cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
-
- return TRUE;
-}
-
-
-/*
- * This is the default resync_to_restart method for data source managers
- * to use if they don't have any better approach. Some data source managers
- * may be able to back up, or may have additional knowledge about the data
- * which permits a more intelligent recovery strategy; such managers would
- * presumably supply their own resync method.
- *
- * read_restart_marker calls resync_to_restart if it finds a marker other than
- * the restart marker it was expecting. (This code is *not* used unless
- * a nonzero restart interval has been declared.) cinfo->unread_marker is
- * the marker code actually found (might be anything, except 0 or FF).
- * The desired restart marker number (0..7) is passed as a parameter.
- * This routine is supposed to apply whatever error recovery strategy seems
- * appropriate in order to position the input stream to the next data segment.
- * Note that cinfo->unread_marker is treated as a marker appearing before
- * the current data-source input point; usually it should be reset to zero
- * before returning.
- * Returns FALSE if suspension is required.
- *
- * This implementation is substantially constrained by wanting to treat the
- * input as a data stream; this means we can't back up. Therefore, we have
- * only the following actions to work with:
- * 1. Simply discard the marker and let the entropy decoder resume at next
- * byte of file.
- * 2. Read forward until we find another marker, discarding intervening
- * data. (In theory we could look ahead within the current bufferload,
- * without having to discard data if we don't find the desired marker.
- * This idea is not implemented here, in part because it makes behavior
- * dependent on buffer size and chance buffer-boundary positions.)
- * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
- * This will cause the entropy decoder to process an empty data segment,
- * inserting dummy zeroes, and then we will reprocess the marker.
- *
- * #2 is appropriate if we think the desired marker lies ahead, while #3 is
- * appropriate if the found marker is a future restart marker (indicating
- * that we have missed the desired restart marker, probably because it got
- * corrupted).
- * We apply #2 or #3 if the found marker is a restart marker no more than
- * two counts behind or ahead of the expected one. We also apply #2 if the
- * found marker is not a legal JPEG marker code (it's certainly bogus data).
- * If the found marker is a restart marker more than 2 counts away, we do #1
- * (too much risk that the marker is erroneous; with luck we will be able to
- * resync at some future point).
- * For any valid non-restart JPEG marker, we apply #3. This keeps us from
- * overrunning the end of a scan. An implementation limited to single-scan
- * files might find it better to apply #2 for markers other than EOI, since
- * any other marker would have to be bogus data in that case.
- */
-
-GLOBAL(boolean)
-jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
-{
- int marker = cinfo->unread_marker;
- int action = 1;
-
- /* Always put up a warning. */
- WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
-
- /* Outer loop handles repeated decision after scanning forward. */
- for (;;) {
- if (marker < (int) M_SOF0)
- action = 2; /* invalid marker */
- else if (marker < (int) M_RST0 || marker > (int) M_RST7)
- action = 3; /* valid non-restart marker */
- else {
- if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
- marker == ((int) M_RST0 + ((desired+2) & 7)))
- action = 3; /* one of the next two expected restarts */
- else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
- marker == ((int) M_RST0 + ((desired-2) & 7)))
- action = 2; /* a prior restart, so advance */
- else
- action = 1; /* desired restart or too far away */
- }
- TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
- switch (action) {
- case 1:
- /* Discard marker and let entropy decoder resume processing. */
- cinfo->unread_marker = 0;
- return TRUE;
- case 2:
- /* Scan to the next marker, and repeat the decision loop. */
- if (! next_marker(cinfo))
- return FALSE;
- marker = cinfo->unread_marker;
- break;
- case 3:
- /* Return without advancing past this marker. */
- /* Entropy decoder will be forced to process an empty segment. */
- return TRUE;
- }
- } /* end loop */
-}
-
-
-/*
- * Reset marker processing state to begin a fresh datastream.
- */
-
-METHODDEF(void)
-reset_marker_reader (j_decompress_ptr cinfo)
-{
- my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
-
- cinfo->comp_info = NULL; /* until allocated by get_sof */
- cinfo->input_scan_number = 0; /* no SOS seen yet */
- cinfo->unread_marker = 0; /* no pending marker */
- marker->pub.saw_SOI = FALSE; /* set internal state too */
- marker->pub.saw_SOF = FALSE;
- marker->pub.discarded_bytes = 0;
- marker->cur_marker = NULL;
-}
-
-
-/*
- * Initialize the marker reader module.
- * This is called only once, when the decompression object is created.
- */
-
-GLOBAL(void)
-jinit_marker_reader (j_decompress_ptr cinfo)
-{
- my_marker_ptr marker;
- int i;
-
- /* Create subobject in permanent pool */
- marker = (my_marker_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
- SIZEOF(my_marker_reader));
- cinfo->marker = (struct jpeg_marker_reader *) marker;
- /* Initialize public method pointers */
- marker->pub.reset_marker_reader = reset_marker_reader;
- marker->pub.read_markers = read_markers;
- marker->pub.read_restart_marker = read_restart_marker;
- /* Initialize COM/APPn processing.
- * By default, we examine and then discard APP0 and APP14,
- * but simply discard COM and all other APPn.
- */
- marker->process_COM = skip_variable;
- marker->length_limit_COM = 0;
- for (i = 0; i < 16; i++) {
- marker->process_APPn[i] = skip_variable;
- marker->length_limit_APPn[i] = 0;
- }
- marker->process_APPn[0] = get_interesting_appn;
- marker->process_APPn[14] = get_interesting_appn;
- /* Reset marker processing state */
- reset_marker_reader(cinfo);
-}
-
-
-/*
- * Control saving of COM and APPn markers into marker_list.
- */
-
-#ifdef SAVE_MARKERS_SUPPORTED
-
-GLOBAL(void)
-jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
- unsigned int length_limit)
-{
- my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
- long maxlength;
- jpeg_marker_parser_method processor;
-
- /* Length limit mustn't be larger than what we can allocate
- * (should only be a concern in a 16-bit environment).
- */
- maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
- if (((long) length_limit) > maxlength)
- length_limit = (unsigned int) maxlength;
-
- /* Choose processor routine to use.
- * APP0/APP14 have special requirements.
- */
- if (length_limit) {
- processor = save_marker;
- /* If saving APP0/APP14, save at least enough for our internal use. */
- if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
- length_limit = APP0_DATA_LEN;
- else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
- length_limit = APP14_DATA_LEN;
- } else {
- processor = skip_variable;
- /* If discarding APP0/APP14, use our regular on-the-fly processor. */
- if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
- processor = get_interesting_appn;
- }
-
- if (marker_code == (int) M_COM) {
- marker->process_COM = processor;
- marker->length_limit_COM = length_limit;
- } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
- marker->process_APPn[marker_code - (int) M_APP0] = processor;
- marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
- } else
- ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
-}
-
-#endif /* SAVE_MARKERS_SUPPORTED */
-
-
-/*
- * Install a special processing method for COM or APPn markers.
- */
-
-GLOBAL(void)
-jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
- jpeg_marker_parser_method routine)
-{
- my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
-
- if (marker_code == (int) M_COM)
- marker->process_COM = routine;
- else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
- marker->process_APPn[marker_code - (int) M_APP0] = routine;
- else
- ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdmarker.c
+ *
+ * Copyright (C) 1991-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 routines to decode JPEG datastream markers.
+ * Most of the complexity arises from our desire to support input
+ * suspension: if not all of the data for a marker is available,
+ * we must exit back to the application. On resumption, we reprocess
+ * the marker.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+typedef enum { /* JPEG marker codes */
+ M_SOF0 = 0xc0,
+ M_SOF1 = 0xc1,
+ M_SOF2 = 0xc2,
+ M_SOF3 = 0xc3,
+
+ M_SOF5 = 0xc5,
+ M_SOF6 = 0xc6,
+ M_SOF7 = 0xc7,
+
+ M_JPG = 0xc8,
+ M_SOF9 = 0xc9,
+ M_SOF10 = 0xca,
+ M_SOF11 = 0xcb,
+
+ M_SOF13 = 0xcd,
+ M_SOF14 = 0xce,
+ M_SOF15 = 0xcf,
+
+ M_DHT = 0xc4,
+
+ M_DAC = 0xcc,
+
+ M_RST0 = 0xd0,
+ M_RST1 = 0xd1,
+ M_RST2 = 0xd2,
+ M_RST3 = 0xd3,
+ M_RST4 = 0xd4,
+ M_RST5 = 0xd5,
+ M_RST6 = 0xd6,
+ M_RST7 = 0xd7,
+
+ M_SOI = 0xd8,
+ M_EOI = 0xd9,
+ M_SOS = 0xda,
+ M_DQT = 0xdb,
+ M_DNL = 0xdc,
+ M_DRI = 0xdd,
+ M_DHP = 0xde,
+ M_EXP = 0xdf,
+
+ M_APP0 = 0xe0,
+ M_APP1 = 0xe1,
+ M_APP2 = 0xe2,
+ M_APP3 = 0xe3,
+ M_APP4 = 0xe4,
+ M_APP5 = 0xe5,
+ M_APP6 = 0xe6,
+ M_APP7 = 0xe7,
+ M_APP8 = 0xe8,
+ M_APP9 = 0xe9,
+ M_APP10 = 0xea,
+ M_APP11 = 0xeb,
+ M_APP12 = 0xec,
+ M_APP13 = 0xed,
+ M_APP14 = 0xee,
+ M_APP15 = 0xef,
+
+ M_JPG0 = 0xf0,
+ M_JPG13 = 0xfd,
+ M_COM = 0xfe,
+
+ M_TEM = 0x01,
+
+ M_ERROR = 0x100
+} JPEG_MARKER;
+
+
+/* Private state */
+
+typedef struct {
+ struct jpeg_marker_reader pub; /* public fields */
+
+ /* Application-overridable marker processing methods */
+ jpeg_marker_parser_method process_COM;
+ jpeg_marker_parser_method process_APPn[16];
+
+ /* Limit on marker data length to save for each marker type */
+ unsigned int length_limit_COM;
+ unsigned int length_limit_APPn[16];
+
+ /* Status of COM/APPn marker saving */
+ jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */
+ unsigned int bytes_read; /* data bytes read so far in marker */
+ /* Note: cur_marker is not linked into marker_list until it's all read. */
+} my_marker_reader;
+
+typedef my_marker_reader * my_marker_ptr;
+
+
+/*
+ * Macros for fetching data from the data source module.
+ *
+ * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
+ * the current restart point; we update them only when we have reached a
+ * suitable place to restart if a suspension occurs.
+ */
+
+/* Declare and initialize local copies of input pointer/count */
+#define INPUT_VARS(cinfo) \
+ struct jpeg_source_mgr * datasrc = (cinfo)->src; \
+ const JOCTET * next_input_byte = datasrc->next_input_byte; \
+ size_t bytes_in_buffer = datasrc->bytes_in_buffer
+
+/* Unload the local copies --- do this only at a restart boundary */
+#define INPUT_SYNC(cinfo) \
+ ( datasrc->next_input_byte = next_input_byte, \
+ datasrc->bytes_in_buffer = bytes_in_buffer )
+
+/* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
+#define INPUT_RELOAD(cinfo) \
+ ( next_input_byte = datasrc->next_input_byte, \
+ bytes_in_buffer = datasrc->bytes_in_buffer )
+
+/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
+ * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
+ * but we must reload the local copies after a successful fill.
+ */
+#define MAKE_BYTE_AVAIL(cinfo,action) \
+ if (bytes_in_buffer == 0) { \
+ if (! (*datasrc->fill_input_buffer) (cinfo)) \
+ { action; } \
+ INPUT_RELOAD(cinfo); \
+ }
+
+/* Read a byte into variable V.
+ * If must suspend, take the specified action (typically "return FALSE").
+ */
+#define INPUT_BYTE(cinfo,V,action) \
+ MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
+ bytes_in_buffer--; \
+ V = GETJOCTET(*next_input_byte++); )
+
+/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
+ * V should be declared unsigned int or perhaps INT32.
+ */
+#define INPUT_2BYTES(cinfo,V,action) \
+ MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
+ bytes_in_buffer--; \
+ V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
+ MAKE_BYTE_AVAIL(cinfo,action); \
+ bytes_in_buffer--; \
+ V += GETJOCTET(*next_input_byte++); )
+
+
+/*
+ * Routines to process JPEG markers.
+ *
+ * Entry condition: JPEG marker itself has been read and its code saved
+ * in cinfo->unread_marker; input restart point is just after the marker.
+ *
+ * Exit: if return TRUE, have read and processed any parameters, and have
+ * updated the restart point to point after the parameters.
+ * If return FALSE, was forced to suspend before reaching end of
+ * marker parameters; restart point has not been moved. Same routine
+ * will be called again after application supplies more input data.
+ *
+ * This approach to suspension assumes that all of a marker's parameters
+ * can fit into a single input bufferload. This should hold for "normal"
+ * markers. Some COM/APPn markers might have large parameter segments
+ * that might not fit. If we are simply dropping such a marker, we use
+ * skip_input_data to get past it, and thereby put the problem on the
+ * source manager's shoulders. If we are saving the marker's contents
+ * into memory, we use a slightly different convention: when forced to
+ * suspend, the marker processor updates the restart point to the end of
+ * what it's consumed (ie, the end of the buffer) before returning FALSE.
+ * On resumption, cinfo->unread_marker still contains the marker code,
+ * but the data source will point to the next chunk of marker data.
+ * The marker processor must retain internal state to deal with this.
+ *
+ * Note that we don't bother to avoid duplicate trace messages if a
+ * suspension occurs within marker parameters. Other side effects
+ * require more care.
+ */
+
+
+LOCAL(boolean)
+get_soi (j_decompress_ptr cinfo)
+/* Process an SOI marker */
+{
+ int i;
+
+ TRACEMS(cinfo, 1, JTRC_SOI);
+
+ if (cinfo->marker->saw_SOI)
+ ERREXIT(cinfo, JERR_SOI_DUPLICATE);
+
+ /* Reset all parameters that are defined to be reset by SOI */
+
+ for (i = 0; i < NUM_ARITH_TBLS; i++) {
+ cinfo->arith_dc_L[i] = 0;
+ cinfo->arith_dc_U[i] = 1;
+ cinfo->arith_ac_K[i] = 5;
+ }
+ cinfo->restart_interval = 0;
+
+ /* Set initial assumptions for colorspace etc */
+
+ cinfo->jpeg_color_space = JCS_UNKNOWN;
+ cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
+
+ cinfo->saw_JFIF_marker = FALSE;
+ cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
+ cinfo->JFIF_minor_version = 1;
+ cinfo->density_unit = 0;
+ cinfo->X_density = 1;
+ cinfo->Y_density = 1;
+ cinfo->saw_Adobe_marker = FALSE;
+ cinfo->Adobe_transform = 0;
+
+ cinfo->marker->saw_SOI = TRUE;
+
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
+/* Process a SOFn marker */
+{
+ INT32 length;
+ int c, ci;
+ jpeg_component_info * compptr;
+ /* LiuSunliang added 20111209 */
+ JDIMENSION image_width, image_height;
+ INPUT_VARS(cinfo);
+
+ cinfo->progressive_mode = is_prog;
+ cinfo->arith_code = is_arith;
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+
+ INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
+ INPUT_2BYTES(cinfo, image_height, return FALSE);
+ INPUT_2BYTES(cinfo, image_width, return FALSE);
+ INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
+
+ if (image_width <= JPEG_MAX_DIMENSION)
+ cinfo->image_width = image_width;
+
+ if (image_height <= JPEG_MAX_DIMENSION)
+ cinfo->image_height = image_height;
+
+ length -= 8;
+
+ TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
+ (int) cinfo->image_width, (int) cinfo->image_height,
+ cinfo->num_components);
+
+ if (cinfo->marker->saw_SOF)
+ ERREXIT(cinfo, JERR_SOF_DUPLICATE);
+
+ /* We don't support files in which the image height is initially specified */
+ /* as 0 and is later redefined by DNL. As long as we have to check that, */
+ /* might as well have a general sanity check. */
+ if (cinfo->image_height <= 0 || cinfo->image_width <= 0
+ || cinfo->num_components <= 0)
+ ERREXIT(cinfo, JERR_EMPTY_IMAGE);
+
+ if (length != (cinfo->num_components * 3))
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ if (cinfo->comp_info == NULL) /* do only once, even if suspend */
+ cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->num_components * SIZEOF(jpeg_component_info));
+
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ compptr->component_index = ci;
+ INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
+ /* XYQ 2008-03-25: Adobe CMYK JPEG has serious flaw: the K channel has same component id as C channel */
+ {
+ int i;
+ for (i = 0; i < ci; i ++)
+ if (compptr->component_id == cinfo->comp_info[i].component_id) break;
+ if (i < ci)
+ /* Found the error! We replace the id with something unlikely used elsewhere */
+ compptr->component_id += 0xf0;
+ }
+ /* end of modification */
+ INPUT_BYTE(cinfo, c, return FALSE);
+ compptr->h_samp_factor = (c >> 4) & 15;
+ compptr->v_samp_factor = (c ) & 15;
+ INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
+
+ TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
+ compptr->component_id, compptr->h_samp_factor,
+ compptr->v_samp_factor, compptr->quant_tbl_no);
+ }
+
+ cinfo->marker->saw_SOF = TRUE;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+get_sos (j_decompress_ptr cinfo)
+/* Process a SOS marker */
+{
+ INT32 length;
+ int i, ci, n, c, cc;
+ jpeg_component_info * compptr;
+ INPUT_VARS(cinfo);
+
+ if (! cinfo->marker->saw_SOF)
+ ERREXIT(cinfo, JERR_SOS_NO_SOF);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+
+ INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
+
+ TRACEMS1(cinfo, 1, JTRC_SOS, n);
+
+ if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ cinfo->comps_in_scan = n;
+
+ /* Collect the component-spec parameters */
+
+ for (i = 0; i < n; i++) {
+ INPUT_BYTE(cinfo, cc, return FALSE);
+ INPUT_BYTE(cinfo, c, return FALSE);
+
+ /* XYQ 2008-03-25: Adobe CMYK JPEG has serious flaw: the K channel has same component id as C channel */
+ {
+ int j;
+ for (j = 0; j < i; j ++)
+ if (cc == cinfo->cur_comp_info[j]->component_id) break;
+ if (j < i)
+ /* found the error! */
+ cc += 0xf0;
+ }
+ /* end of modification */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ if (cc == compptr->component_id)
+ goto id_found;
+ }
+
+ ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
+
+ id_found:
+
+ cinfo->cur_comp_info[i] = compptr;
+ compptr->dc_tbl_no = (c >> 4) & 15;
+ compptr->ac_tbl_no = (c ) & 15;
+
+ TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
+ compptr->dc_tbl_no, compptr->ac_tbl_no);
+ /* This CSi (cc) should differ from the previous CSi */
+ for (ci = 0; ci < i; ci++) {
+ if (cinfo->cur_comp_info[ci] == compptr)
+ ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
+ }
+ }
+
+ /* Collect the additional scan parameters Ss, Se, Ah/Al. */
+ INPUT_BYTE(cinfo, c, return FALSE);
+ cinfo->Ss = c;
+ INPUT_BYTE(cinfo, c, return FALSE);
+ cinfo->Se = c;
+ INPUT_BYTE(cinfo, c, return FALSE);
+ cinfo->Ah = (c >> 4) & 15;
+ cinfo->Al = (c ) & 15;
+
+ TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
+ cinfo->Ah, cinfo->Al);
+
+ /* Prepare to scan data & restart markers */
+ cinfo->marker->next_restart_num = 0;
+
+ /* Count another SOS marker */
+ cinfo->input_scan_number++;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+#ifdef D_ARITH_CODING_SUPPORTED
+
+LOCAL(boolean)
+get_dac (j_decompress_ptr cinfo)
+/* Process a DAC marker */
+{
+ INT32 length;
+ int index, val;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ while (length > 0) {
+ INPUT_BYTE(cinfo, index, return FALSE);
+ INPUT_BYTE(cinfo, val, return FALSE);
+
+ length -= 2;
+
+ TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
+
+ if (index < 0 || index >= (2*NUM_ARITH_TBLS))
+ ERREXIT1(cinfo, JERR_DAC_INDEX, index);
+
+ if (index >= NUM_ARITH_TBLS) { /* define AC table */
+ cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
+ } else { /* define DC table */
+ cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
+ cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
+ if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
+ ERREXIT1(cinfo, JERR_DAC_VALUE, val);
+ }
+ }
+
+ if (length != 0)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+#else /* ! D_ARITH_CODING_SUPPORTED */
+
+#define get_dac(cinfo) skip_variable(cinfo)
+
+#endif /* D_ARITH_CODING_SUPPORTED */
+
+
+LOCAL(boolean)
+get_dht (j_decompress_ptr cinfo)
+/* Process a DHT marker */
+{
+ INT32 length;
+ UINT8 bits[17];
+ UINT8 huffval[256];
+ int i, index, count;
+ JHUFF_TBL **htblptr;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ while (length > 16) {
+ INPUT_BYTE(cinfo, index, return FALSE);
+
+ TRACEMS1(cinfo, 1, JTRC_DHT, index);
+
+ bits[0] = 0;
+ count = 0;
+ for (i = 1; i <= 16; i++) {
+ INPUT_BYTE(cinfo, bits[i], return FALSE);
+ count += bits[i];
+ }
+
+ length -= 1 + 16;
+
+ TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
+ bits[1], bits[2], bits[3], bits[4],
+ bits[5], bits[6], bits[7], bits[8]);
+ TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
+ bits[9], bits[10], bits[11], bits[12],
+ bits[13], bits[14], bits[15], bits[16]);
+
+ /* Here we just do minimal validation of the counts to avoid walking
+ * off the end of our table space. jdhuff.c will check more carefully.
+ */
+ if (count > 256 || ((INT32) count) > length)
+ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
+
+ for (i = 0; i < count; i++)
+ INPUT_BYTE(cinfo, huffval[i], return FALSE);
+
+ length -= count;
+
+ if (index & 0x10) { /* AC table definition */
+ index -= 0x10;
+ htblptr = &cinfo->ac_huff_tbl_ptrs[index];
+ } else { /* DC table definition */
+ htblptr = &cinfo->dc_huff_tbl_ptrs[index];
+ }
+
+ if (index < 0 || index >= NUM_HUFF_TBLS)
+ ERREXIT1(cinfo, JERR_DHT_INDEX, index);
+
+ if (*htblptr == NULL)
+ *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
+
+ MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
+ MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
+ }
+
+ if (length != 0)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+get_dqt (j_decompress_ptr cinfo)
+/* Process a DQT marker */
+{
+ INT32 length;
+ int n, i, prec;
+ unsigned int tmp;
+ JQUANT_TBL *quant_ptr;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ while (length > 0) {
+ INPUT_BYTE(cinfo, n, return FALSE);
+ prec = n >> 4;
+ n &= 0x0F;
+
+ TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
+
+ if (n >= NUM_QUANT_TBLS)
+ ERREXIT1(cinfo, JERR_DQT_INDEX, n);
+
+ if (cinfo->quant_tbl_ptrs[n] == NULL)
+ cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
+ quant_ptr = cinfo->quant_tbl_ptrs[n];
+
+ for (i = 0; i < DCTSIZE2; i++) {
+ if (prec)
+ INPUT_2BYTES(cinfo, tmp, return FALSE);
+ else
+ INPUT_BYTE(cinfo, tmp, return FALSE);
+ /* We convert the zigzag-order table to natural array order. */
+ quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
+ }
+
+ if (cinfo->err->trace_level >= 2) {
+ for (i = 0; i < DCTSIZE2; i += 8) {
+ TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
+ quant_ptr->quantval[i], quant_ptr->quantval[i+1],
+ quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
+ quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
+ quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
+ }
+ }
+
+ length -= DCTSIZE2+1;
+ if (prec) length -= DCTSIZE2;
+ }
+
+ if (length != 0)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+get_dri (j_decompress_ptr cinfo)
+/* Process a DRI marker */
+{
+ INT32 length;
+ unsigned int tmp;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+
+ if (length != 4)
+ ERREXIT(cinfo, JERR_BAD_LENGTH);
+
+ INPUT_2BYTES(cinfo, tmp, return FALSE);
+
+ TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
+
+ cinfo->restart_interval = tmp;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+/*
+ * Routines for processing APPn and COM markers.
+ * These are either saved in memory or discarded, per application request.
+ * APP0 and APP14 are specially checked to see if they are
+ * JFIF and Adobe markers, respectively.
+ */
+
+#define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */
+#define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */
+#define APPN_DATA_LEN 14 /* Must be the largest of the above!! */
+
+
+LOCAL(void)
+examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
+ unsigned int datalen, INT32 remaining)
+/* Examine first few bytes from an APP0.
+ * Take appropriate action if it is a JFIF marker.
+ * datalen is # of bytes at data[], remaining is length of rest of marker data.
+ */
+{
+ INT32 totallen = (INT32) datalen + remaining;
+
+ if (datalen >= APP0_DATA_LEN &&
+ GETJOCTET(data[0]) == 0x4A &&
+ GETJOCTET(data[1]) == 0x46 &&
+ GETJOCTET(data[2]) == 0x49 &&
+ GETJOCTET(data[3]) == 0x46 &&
+ GETJOCTET(data[4]) == 0) {
+ /* Found JFIF APP0 marker: save info */
+ cinfo->saw_JFIF_marker = TRUE;
+ cinfo->JFIF_major_version = GETJOCTET(data[5]);
+ cinfo->JFIF_minor_version = GETJOCTET(data[6]);
+ cinfo->density_unit = GETJOCTET(data[7]);
+ cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
+ cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
+ /* Check version.
+ * Major version must be 1, anything else signals an incompatible change.
+ * (We used to treat this as an error, but now it's a nonfatal warning,
+ * because some bozo at Hijaak couldn't read the spec.)
+ * Minor version should be 0..2, but process anyway if newer.
+ */
+ if (cinfo->JFIF_major_version != 1)
+ WARNMS2(cinfo, JWRN_JFIF_MAJOR,
+ cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
+ /* Generate trace messages */
+ TRACEMS5(cinfo, 1, JTRC_JFIF,
+ cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
+ cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
+ /* Validate thumbnail dimensions and issue appropriate messages */
+ if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
+ TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
+ GETJOCTET(data[12]), GETJOCTET(data[13]));
+ totallen -= APP0_DATA_LEN;
+ if (totallen !=
+ ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
+ TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
+ } else if (datalen >= 6 &&
+ GETJOCTET(data[0]) == 0x4A &&
+ GETJOCTET(data[1]) == 0x46 &&
+ GETJOCTET(data[2]) == 0x58 &&
+ GETJOCTET(data[3]) == 0x58 &&
+ GETJOCTET(data[4]) == 0) {
+ /* Found JFIF "JFXX" extension APP0 marker */
+ /* The library doesn't actually do anything with these,
+ * but we try to produce a helpful trace message.
+ */
+ switch (GETJOCTET(data[5])) {
+ case 0x10:
+ TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
+ break;
+ case 0x11:
+ TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
+ break;
+ case 0x13:
+ TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
+ break;
+ default:
+ TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
+ GETJOCTET(data[5]), (int) totallen);
+ break;
+ }
+ } else {
+ /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
+ TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
+ }
+}
+
+
+LOCAL(void)
+examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
+ unsigned int datalen, INT32 remaining)
+/* Examine first few bytes from an APP14.
+ * Take appropriate action if it is an Adobe marker.
+ * datalen is # of bytes at data[], remaining is length of rest of marker data.
+ */
+{
+ unsigned int version, flags0, flags1, transform;
+
+ if (datalen >= APP14_DATA_LEN &&
+ GETJOCTET(data[0]) == 0x41 &&
+ GETJOCTET(data[1]) == 0x64 &&
+ GETJOCTET(data[2]) == 0x6F &&
+ GETJOCTET(data[3]) == 0x62 &&
+ GETJOCTET(data[4]) == 0x65) {
+ /* Found Adobe APP14 marker */
+ version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
+ flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
+ flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
+ transform = GETJOCTET(data[11]);
+ TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
+ cinfo->saw_Adobe_marker = TRUE;
+ cinfo->Adobe_transform = (UINT8) transform;
+ } else {
+ /* Start of APP14 does not match "Adobe", or too short */
+ TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
+ }
+}
+
+
+METHODDEF(boolean)
+get_interesting_appn (j_decompress_ptr cinfo)
+/* Process an APP0 or APP14 marker without saving it */
+{
+ INT32 length;
+ JOCTET b[APPN_DATA_LEN];
+ unsigned int i, numtoread;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ /* get the interesting part of the marker data */
+ if (length >= APPN_DATA_LEN)
+ numtoread = APPN_DATA_LEN;
+ else if (length > 0)
+ numtoread = (unsigned int) length;
+ else
+ numtoread = 0;
+ for (i = 0; i < numtoread; i++)
+ INPUT_BYTE(cinfo, b[i], return FALSE);
+ length -= numtoread;
+
+ /* process it */
+ switch (cinfo->unread_marker) {
+ case M_APP0:
+ examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
+ break;
+ case M_APP14:
+ examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
+ break;
+ default:
+ /* can't get here unless jpeg_save_markers chooses wrong processor */
+ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
+ break;
+ }
+
+ /* skip any remaining data -- could be lots */
+ INPUT_SYNC(cinfo);
+ if (length > 0)
+ (*cinfo->src->skip_input_data) (cinfo, (long) length);
+
+ return TRUE;
+}
+
+
+#ifdef SAVE_MARKERS_SUPPORTED
+
+METHODDEF(boolean)
+save_marker (j_decompress_ptr cinfo)
+/* Save an APPn or COM marker into the marker list */
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+ jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
+ unsigned int bytes_read, data_length;
+ JOCTET FAR * data;
+ INT32 length = 0;
+ INPUT_VARS(cinfo);
+
+ if (cur_marker == NULL) {
+ /* begin reading a marker */
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+ if (length >= 0) { /* watch out for bogus length word */
+ /* figure out how much we want to save */
+ unsigned int limit;
+ if (cinfo->unread_marker == (int) M_COM)
+ limit = marker->length_limit_COM;
+ else
+ limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
+ if ((unsigned int) length < limit)
+ limit = (unsigned int) length;
+ /* allocate and initialize the marker item */
+ cur_marker = (jpeg_saved_marker_ptr)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(struct jpeg_marker_struct) + limit);
+ cur_marker->next = NULL;
+ cur_marker->marker = (UINT8) cinfo->unread_marker;
+ cur_marker->original_length = (unsigned int) length;
+ cur_marker->data_length = limit;
+ /* data area is just beyond the jpeg_marker_struct */
+ data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
+ marker->cur_marker = cur_marker;
+ marker->bytes_read = 0;
+ bytes_read = 0;
+ data_length = limit;
+ } else {
+ /* deal with bogus length word */
+ bytes_read = data_length = 0;
+ data = NULL;
+ }
+ } else {
+ /* resume reading a marker */
+ bytes_read = marker->bytes_read;
+ data_length = cur_marker->data_length;
+ data = cur_marker->data + bytes_read;
+ }
+
+ while (bytes_read < data_length) {
+ INPUT_SYNC(cinfo); /* move the restart point to here */
+ marker->bytes_read = bytes_read;
+ /* If there's not at least one byte in buffer, suspend */
+ MAKE_BYTE_AVAIL(cinfo, return FALSE);
+ /* Copy bytes with reasonable rapidity */
+ while (bytes_read < data_length && bytes_in_buffer > 0) {
+ *data++ = *next_input_byte++;
+ bytes_in_buffer--;
+ bytes_read++;
+ }
+ }
+
+ /* Done reading what we want to read */
+ if (cur_marker != NULL) { /* will be NULL if bogus length word */
+ /* Add new marker to end of list */
+ if (cinfo->marker_list == NULL) {
+ cinfo->marker_list = cur_marker;
+ } else {
+ jpeg_saved_marker_ptr prev = cinfo->marker_list;
+ while (prev->next != NULL)
+ prev = prev->next;
+ prev->next = cur_marker;
+ }
+ /* Reset pointer & calc remaining data length */
+ data = cur_marker->data;
+ length = cur_marker->original_length - data_length;
+ }
+ /* Reset to initial state for next marker */
+ marker->cur_marker = NULL;
+
+ /* Process the marker if interesting; else just make a generic trace msg */
+ switch (cinfo->unread_marker) {
+ case M_APP0:
+ examine_app0(cinfo, data, data_length, length);
+ break;
+ case M_APP14:
+ examine_app14(cinfo, data, data_length, length);
+ break;
+ default:
+ TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
+ (int) (data_length + length));
+ break;
+ }
+
+ /* skip any remaining data -- could be lots */
+ INPUT_SYNC(cinfo); /* do before skip_input_data */
+ if (length > 0)
+ (*cinfo->src->skip_input_data) (cinfo, (long) length);
+
+ return TRUE;
+}
+
+#endif /* SAVE_MARKERS_SUPPORTED */
+
+
+METHODDEF(boolean)
+skip_variable (j_decompress_ptr cinfo)
+/* Skip over an unknown or uninteresting variable-length marker */
+{
+ INT32 length;
+ INPUT_VARS(cinfo);
+
+ INPUT_2BYTES(cinfo, length, return FALSE);
+ length -= 2;
+
+ TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
+
+ INPUT_SYNC(cinfo); /* do before skip_input_data */
+ if (length > 0)
+ (*cinfo->src->skip_input_data) (cinfo, (long) length);
+
+ return TRUE;
+}
+
+
+/*
+ * Find the next JPEG marker, save it in cinfo->unread_marker.
+ * Returns FALSE if had to suspend before reaching a marker;
+ * in that case cinfo->unread_marker is unchanged.
+ *
+ * Note that the result might not be a valid marker code,
+ * but it will never be 0 or FF.
+ */
+
+LOCAL(boolean)
+next_marker (j_decompress_ptr cinfo)
+{
+ int c;
+ INPUT_VARS(cinfo);
+
+ for (;;) {
+ INPUT_BYTE(cinfo, c, return FALSE);
+ /* Skip any non-FF bytes.
+ * This may look a bit inefficient, but it will not occur in a valid file.
+ * We sync after each discarded byte so that a suspending data source
+ * can discard the byte from its buffer.
+ */
+ while (c != 0xFF) {
+ cinfo->marker->discarded_bytes++;
+ INPUT_SYNC(cinfo);
+ INPUT_BYTE(cinfo, c, return FALSE);
+ }
+ /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
+ * pad bytes, so don't count them in discarded_bytes. We assume there
+ * will not be so many consecutive FF bytes as to overflow a suspending
+ * data source's input buffer.
+ */
+ do {
+ INPUT_BYTE(cinfo, c, return FALSE);
+ } while (c == 0xFF);
+ if (c != 0)
+ break; /* found a valid marker, exit loop */
+ /* Reach here if we found a stuffed-zero data sequence (FF/00).
+ * Discard it and loop back to try again.
+ */
+ cinfo->marker->discarded_bytes += 2;
+ INPUT_SYNC(cinfo);
+ }
+
+ if (cinfo->marker->discarded_bytes != 0) {
+ WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
+ cinfo->marker->discarded_bytes = 0;
+ }
+
+ cinfo->unread_marker = c;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+LOCAL(boolean)
+first_marker (j_decompress_ptr cinfo)
+/* Like next_marker, but used to obtain the initial SOI marker. */
+/* For this marker, we do not allow preceding garbage or fill; otherwise,
+ * we might well scan an entire input file before realizing it ain't JPEG.
+ * If an application wants to process non-JFIF files, it must seek to the
+ * SOI before calling the JPEG library.
+ */
+{
+ int c, c2;
+ INPUT_VARS(cinfo);
+
+ INPUT_BYTE(cinfo, c, return FALSE);
+ INPUT_BYTE(cinfo, c2, return FALSE);
+ if (c != 0xFF || c2 != (int) M_SOI)
+ ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
+
+ cinfo->unread_marker = c2;
+
+ INPUT_SYNC(cinfo);
+ return TRUE;
+}
+
+
+/*
+ * Read markers until SOS or EOI.
+ *
+ * Returns same codes as are defined for jpeg_consume_input:
+ * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
+ */
+
+METHODDEF(int)
+read_markers (j_decompress_ptr cinfo)
+{
+ /* Outer loop repeats once for each marker. */
+ for (;;) {
+ /* Collect the marker proper, unless we already did. */
+ /* NB: first_marker() enforces the requirement that SOI appear first. */
+ if (cinfo->unread_marker == 0) {
+ if (! cinfo->marker->saw_SOI) {
+ if (! first_marker(cinfo))
+ return JPEG_SUSPENDED;
+ } else {
+ if (! next_marker(cinfo))
+ return JPEG_SUSPENDED;
+ }
+ }
+ /* At this point cinfo->unread_marker contains the marker code and the
+ * input point is just past the marker proper, but before any parameters.
+ * A suspension will cause us to return with this state still true.
+ */
+ switch (cinfo->unread_marker) {
+ case M_SOI:
+ if (! get_soi(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_SOF0: /* Baseline */
+ case M_SOF1: /* Extended sequential, Huffman */
+ if (! get_sof(cinfo, FALSE, FALSE))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_SOF2: /* Progressive, Huffman */
+ if (! get_sof(cinfo, TRUE, FALSE))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_SOF9: /* Extended sequential, arithmetic */
+ if (! get_sof(cinfo, FALSE, TRUE))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_SOF10: /* Progressive, arithmetic */
+ if (! get_sof(cinfo, TRUE, TRUE))
+ return JPEG_SUSPENDED;
+ break;
+
+ /* Currently unsupported SOFn types */
+ case M_SOF3: /* Lossless, Huffman */
+ case M_SOF5: /* Differential sequential, Huffman */
+ case M_SOF6: /* Differential progressive, Huffman */
+ case M_SOF7: /* Differential lossless, Huffman */
+ case M_JPG: /* Reserved for JPEG extensions */
+ case M_SOF11: /* Lossless, arithmetic */
+ case M_SOF13: /* Differential sequential, arithmetic */
+ case M_SOF14: /* Differential progressive, arithmetic */
+ case M_SOF15: /* Differential lossless, arithmetic */
+ ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
+ break;
+
+ case M_SOS:
+ if (! get_sos(cinfo))
+ return JPEG_SUSPENDED;
+ cinfo->unread_marker = 0; /* processed the marker */
+ return JPEG_REACHED_SOS;
+
+ case M_EOI:
+ TRACEMS(cinfo, 1, JTRC_EOI);
+ cinfo->unread_marker = 0; /* processed the marker */
+ return JPEG_REACHED_EOI;
+
+ case M_DAC:
+ if (! get_dac(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_DHT:
+ if (! get_dht(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_DQT:
+ if (! get_dqt(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_DRI:
+ if (! get_dri(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_APP0:
+ case M_APP1:
+ case M_APP2:
+ case M_APP3:
+ case M_APP4:
+ case M_APP5:
+ case M_APP6:
+ case M_APP7:
+ case M_APP8:
+ case M_APP9:
+ case M_APP10:
+ case M_APP11:
+ case M_APP12:
+ case M_APP13:
+ case M_APP14:
+ case M_APP15:
+ if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
+ cinfo->unread_marker - (int) M_APP0]) (cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_COM:
+ if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ case M_RST0: /* these are all parameterless */
+ case M_RST1:
+ case M_RST2:
+ case M_RST3:
+ case M_RST4:
+ case M_RST5:
+ case M_RST6:
+ case M_RST7:
+ case M_TEM:
+ TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
+ break;
+
+ case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
+ if (! skip_variable(cinfo))
+ return JPEG_SUSPENDED;
+ break;
+
+ default: /* must be DHP, EXP, JPGn, or RESn */
+ /* For now, we treat the reserved markers as fatal errors since they are
+ * likely to be used to signal incompatible JPEG Part 3 extensions.
+ * Once the JPEG 3 version-number marker is well defined, this code
+ * ought to change!
+ */
+ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
+ break;
+ }
+ /* Successfully processed marker, so reset state variable */
+ cinfo->unread_marker = 0;
+ } /* end loop */
+}
+
+
+/*
+ * Read a restart marker, which is expected to appear next in the datastream;
+ * if the marker is not there, take appropriate recovery action.
+ * Returns FALSE if suspension is required.
+ *
+ * This is called by the entropy decoder after it has read an appropriate
+ * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
+ * has already read a marker from the data source. Under normal conditions
+ * cinfo->unread_marker will be reset to 0 before returning; if not reset,
+ * it holds a marker which the decoder will be unable to read past.
+ */
+
+METHODDEF(boolean)
+read_restart_marker (j_decompress_ptr cinfo)
+{
+ /* Obtain a marker unless we already did. */
+ /* Note that next_marker will complain if it skips any data. */
+ if (cinfo->unread_marker == 0) {
+ if (! next_marker(cinfo))
+ return FALSE;
+ }
+
+ if (cinfo->unread_marker ==
+ ((int) M_RST0 + cinfo->marker->next_restart_num)) {
+ /* Normal case --- swallow the marker and let entropy decoder continue */
+ TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
+ cinfo->unread_marker = 0;
+ } else {
+ /* Uh-oh, the restart markers have been messed up. */
+ /* Let the data source manager determine how to resync. */
+ if (! (*cinfo->src->resync_to_restart) (cinfo,
+ cinfo->marker->next_restart_num))
+ return FALSE;
+ }
+
+ /* Update next-restart state */
+ cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
+
+ return TRUE;
+}
+
+
+/*
+ * This is the default resync_to_restart method for data source managers
+ * to use if they don't have any better approach. Some data source managers
+ * may be able to back up, or may have additional knowledge about the data
+ * which permits a more intelligent recovery strategy; such managers would
+ * presumably supply their own resync method.
+ *
+ * read_restart_marker calls resync_to_restart if it finds a marker other than
+ * the restart marker it was expecting. (This code is *not* used unless
+ * a nonzero restart interval has been declared.) cinfo->unread_marker is
+ * the marker code actually found (might be anything, except 0 or FF).
+ * The desired restart marker number (0..7) is passed as a parameter.
+ * This routine is supposed to apply whatever error recovery strategy seems
+ * appropriate in order to position the input stream to the next data segment.
+ * Note that cinfo->unread_marker is treated as a marker appearing before
+ * the current data-source input point; usually it should be reset to zero
+ * before returning.
+ * Returns FALSE if suspension is required.
+ *
+ * This implementation is substantially constrained by wanting to treat the
+ * input as a data stream; this means we can't back up. Therefore, we have
+ * only the following actions to work with:
+ * 1. Simply discard the marker and let the entropy decoder resume at next
+ * byte of file.
+ * 2. Read forward until we find another marker, discarding intervening
+ * data. (In theory we could look ahead within the current bufferload,
+ * without having to discard data if we don't find the desired marker.
+ * This idea is not implemented here, in part because it makes behavior
+ * dependent on buffer size and chance buffer-boundary positions.)
+ * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
+ * This will cause the entropy decoder to process an empty data segment,
+ * inserting dummy zeroes, and then we will reprocess the marker.
+ *
+ * #2 is appropriate if we think the desired marker lies ahead, while #3 is
+ * appropriate if the found marker is a future restart marker (indicating
+ * that we have missed the desired restart marker, probably because it got
+ * corrupted).
+ * We apply #2 or #3 if the found marker is a restart marker no more than
+ * two counts behind or ahead of the expected one. We also apply #2 if the
+ * found marker is not a legal JPEG marker code (it's certainly bogus data).
+ * If the found marker is a restart marker more than 2 counts away, we do #1
+ * (too much risk that the marker is erroneous; with luck we will be able to
+ * resync at some future point).
+ * For any valid non-restart JPEG marker, we apply #3. This keeps us from
+ * overrunning the end of a scan. An implementation limited to single-scan
+ * files might find it better to apply #2 for markers other than EOI, since
+ * any other marker would have to be bogus data in that case.
+ */
+
+GLOBAL(boolean)
+jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
+{
+ int marker = cinfo->unread_marker;
+ int action = 1;
+
+ /* Always put up a warning. */
+ WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
+
+ /* Outer loop handles repeated decision after scanning forward. */
+ for (;;) {
+ if (marker < (int) M_SOF0)
+ action = 2; /* invalid marker */
+ else if (marker < (int) M_RST0 || marker > (int) M_RST7)
+ action = 3; /* valid non-restart marker */
+ else {
+ if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
+ marker == ((int) M_RST0 + ((desired+2) & 7)))
+ action = 3; /* one of the next two expected restarts */
+ else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
+ marker == ((int) M_RST0 + ((desired-2) & 7)))
+ action = 2; /* a prior restart, so advance */
+ else
+ action = 1; /* desired restart or too far away */
+ }
+ TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
+ switch (action) {
+ case 1:
+ /* Discard marker and let entropy decoder resume processing. */
+ cinfo->unread_marker = 0;
+ return TRUE;
+ case 2:
+ /* Scan to the next marker, and repeat the decision loop. */
+ if (! next_marker(cinfo))
+ return FALSE;
+ marker = cinfo->unread_marker;
+ break;
+ case 3:
+ /* Return without advancing past this marker. */
+ /* Entropy decoder will be forced to process an empty segment. */
+ return TRUE;
+ }
+ } /* end loop */
+}
+
+
+/*
+ * Reset marker processing state to begin a fresh datastream.
+ */
+
+METHODDEF(void)
+reset_marker_reader (j_decompress_ptr cinfo)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+
+ cinfo->comp_info = NULL; /* until allocated by get_sof */
+ cinfo->input_scan_number = 0; /* no SOS seen yet */
+ cinfo->unread_marker = 0; /* no pending marker */
+ marker->pub.saw_SOI = FALSE; /* set internal state too */
+ marker->pub.saw_SOF = FALSE;
+ marker->pub.discarded_bytes = 0;
+ marker->cur_marker = NULL;
+}
+
+
+/*
+ * Initialize the marker reader module.
+ * This is called only once, when the decompression object is created.
+ */
+
+GLOBAL(void)
+jinit_marker_reader (j_decompress_ptr cinfo)
+{
+ my_marker_ptr marker;
+ int i;
+
+ /* Create subobject in permanent pool */
+ marker = (my_marker_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
+ SIZEOF(my_marker_reader));
+ cinfo->marker = (struct jpeg_marker_reader *) marker;
+ /* Initialize public method pointers */
+ marker->pub.reset_marker_reader = reset_marker_reader;
+ marker->pub.read_markers = read_markers;
+ marker->pub.read_restart_marker = read_restart_marker;
+ /* Initialize COM/APPn processing.
+ * By default, we examine and then discard APP0 and APP14,
+ * but simply discard COM and all other APPn.
+ */
+ marker->process_COM = skip_variable;
+ marker->length_limit_COM = 0;
+ for (i = 0; i < 16; i++) {
+ marker->process_APPn[i] = skip_variable;
+ marker->length_limit_APPn[i] = 0;
+ }
+ marker->process_APPn[0] = get_interesting_appn;
+ marker->process_APPn[14] = get_interesting_appn;
+ /* Reset marker processing state */
+ reset_marker_reader(cinfo);
+}
+
+
+/*
+ * Control saving of COM and APPn markers into marker_list.
+ */
+
+#ifdef SAVE_MARKERS_SUPPORTED
+
+GLOBAL(void)
+jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
+ unsigned int length_limit)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+ long maxlength;
+ jpeg_marker_parser_method processor;
+
+ /* Length limit mustn't be larger than what we can allocate
+ * (should only be a concern in a 16-bit environment).
+ */
+ maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
+ if (((long) length_limit) > maxlength)
+ length_limit = (unsigned int) maxlength;
+
+ /* Choose processor routine to use.
+ * APP0/APP14 have special requirements.
+ */
+ if (length_limit) {
+ processor = save_marker;
+ /* If saving APP0/APP14, save at least enough for our internal use. */
+ if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
+ length_limit = APP0_DATA_LEN;
+ else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
+ length_limit = APP14_DATA_LEN;
+ } else {
+ processor = skip_variable;
+ /* If discarding APP0/APP14, use our regular on-the-fly processor. */
+ if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
+ processor = get_interesting_appn;
+ }
+
+ if (marker_code == (int) M_COM) {
+ marker->process_COM = processor;
+ marker->length_limit_COM = length_limit;
+ } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
+ marker->process_APPn[marker_code - (int) M_APP0] = processor;
+ marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
+ } else
+ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
+}
+
+#endif /* SAVE_MARKERS_SUPPORTED */
+
+
+/*
+ * Install a special processing method for COM or APPn markers.
+ */
+
+GLOBAL(void)
+jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
+ jpeg_marker_parser_method routine)
+{
+ my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
+
+ if (marker_code == (int) M_COM)
+ marker->process_COM = routine;
+ else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
+ marker->process_APPn[marker_code - (int) M_APP0] = routine;
+ else
+ ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c b/core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c
index 4adfd82375..dae51e0fd2 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdmaster.c
@@ -1,560 +1,560 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdmaster.c
- *
- * 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 master control logic for the JPEG decompressor.
- * These routines are concerned with selecting the modules to be executed
- * and with determining the number of passes and the work to be done in each
- * pass.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Private state */
-
-typedef struct {
- struct jpeg_decomp_master pub; /* public fields */
-
- int pass_number; /* # of passes completed */
-
- boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
-
- /* Saved references to initialized quantizer modules,
- * in case we need to switch modes.
- */
- struct jpeg_color_quantizer * quantizer_1pass;
- struct jpeg_color_quantizer * quantizer_2pass;
-} my_decomp_master;
-
-typedef my_decomp_master * my_master_ptr;
-
-
-/*
- * Determine whether merged upsample/color conversion should be used.
- * CRUCIAL: this must match the actual capabilities of jdmerge.c!
- */
-
-LOCAL(boolean)
-use_merged_upsample (j_decompress_ptr cinfo)
-{
-#ifdef UPSAMPLE_MERGING_SUPPORTED
- /* Merging is the equivalent of plain box-filter upsampling */
- if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
- return FALSE;
- /* jdmerge.c only supports YCC=>RGB color conversion */
- if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
- cinfo->out_color_space != JCS_RGB ||
- cinfo->out_color_components != RGB_PIXELSIZE)
- return FALSE;
- /* and it only handles 2h1v or 2h2v sampling ratios */
- if (cinfo->comp_info[0].h_samp_factor != 2 ||
- cinfo->comp_info[1].h_samp_factor != 1 ||
- cinfo->comp_info[2].h_samp_factor != 1 ||
- cinfo->comp_info[0].v_samp_factor > 2 ||
- cinfo->comp_info[1].v_samp_factor != 1 ||
- cinfo->comp_info[2].v_samp_factor != 1)
- return FALSE;
- /* furthermore, it doesn't work if we've scaled the IDCTs differently */
- if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
- cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
- cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
- return FALSE;
- /* ??? also need to test for upsample-time rescaling, when & if supported */
- return TRUE; /* by golly, it'll work... */
-#else
- return FALSE;
-#endif
-}
-
-
-/*
- * Compute output image dimensions and related values.
- * NOTE: this is exported for possible use by application.
- * Hence it mustn't do anything that can't be done twice.
- * Also note that it may be called before the master module is initialized!
- */
-
-GLOBAL(void)
-jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
-/* Do computations that are needed before master selection phase */
-{
-#ifdef IDCT_SCALING_SUPPORTED
- int ci;
- jpeg_component_info *compptr;
-#endif
-
- /* Prevent application from calling me at wrong times */
- if (cinfo->global_state != DSTATE_READY)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
-#ifdef IDCT_SCALING_SUPPORTED
-
- /* Compute actual output image dimensions and DCT scaling choices. */
- if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
- /* Provide 1/8 scaling */
- cinfo->output_width = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width, 8L);
- cinfo->output_height = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height, 8L);
- cinfo->min_DCT_scaled_size = 1;
- } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
- /* Provide 1/4 scaling */
- cinfo->output_width = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width, 4L);
- cinfo->output_height = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height, 4L);
- cinfo->min_DCT_scaled_size = 2;
- } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
- /* Provide 1/2 scaling */
- cinfo->output_width = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width, 2L);
- cinfo->output_height = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height, 2L);
- cinfo->min_DCT_scaled_size = 4;
- } else {
- /* Provide 1/1 scaling */
- cinfo->output_width = cinfo->image_width;
- cinfo->output_height = cinfo->image_height;
- cinfo->min_DCT_scaled_size = DCTSIZE;
- }
- /* In selecting the actual DCT scaling for each component, we try to
- * scale up the chroma components via IDCT scaling rather than upsampling.
- * This saves time if the upsampler gets to use 1:1 scaling.
- * Note this code assumes that the supported DCT scalings are powers of 2.
- */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- int ssize = cinfo->min_DCT_scaled_size;
- while (ssize < DCTSIZE &&
- (compptr->h_samp_factor * ssize * 2 <=
- cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
- (compptr->v_samp_factor * ssize * 2 <=
- cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
- ssize = ssize * 2;
- }
- compptr->DCT_scaled_size = ssize;
- }
-
- /* Recompute downsampled dimensions of components;
- * application needs to know these if using raw downsampled data.
- */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Size in samples, after IDCT scaling */
- compptr->downsampled_width = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_width *
- (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
- (long) (cinfo->max_h_samp_factor * DCTSIZE));
- compptr->downsampled_height = (JDIMENSION)
- jdiv_round_up((long) cinfo->image_height *
- (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
- (long) (cinfo->max_v_samp_factor * DCTSIZE));
- }
-
-#else /* !IDCT_SCALING_SUPPORTED */
-
- /* Hardwire it to "no scaling" */
- cinfo->output_width = cinfo->image_width;
- cinfo->output_height = cinfo->image_height;
- /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
- * and has computed unscaled downsampled_width and downsampled_height.
- */
-
-#endif /* IDCT_SCALING_SUPPORTED */
-
- /* Report number of components in selected colorspace. */
- /* Probably this should be in the color conversion module... */
- switch (cinfo->out_color_space) {
- case JCS_GRAYSCALE:
- cinfo->out_color_components = 1;
- break;
- case JCS_RGB:
-#if RGB_PIXELSIZE != 3
- cinfo->out_color_components = RGB_PIXELSIZE;
- break;
-#endif /* else share code with YCbCr */
- case JCS_YCbCr:
- cinfo->out_color_components = 3;
- break;
- case JCS_CMYK:
- case JCS_YCCK:
- cinfo->out_color_components = 4;
- break;
- default: /* else must be same colorspace as in file */
- cinfo->out_color_components = cinfo->num_components;
- break;
- }
- cinfo->output_components = (cinfo->quantize_colors ? 1 :
- cinfo->out_color_components);
-
- /* See if upsampler will want to emit more than one row at a time */
- if (use_merged_upsample(cinfo))
- cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
- else
- cinfo->rec_outbuf_height = 1;
-}
-
-
-/*
- * Several decompression processes need to range-limit values to the range
- * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
- * due to noise introduced by quantization, roundoff error, etc. These
- * processes are inner loops and need to be as fast as possible. On most
- * machines, particularly CPUs with pipelines or instruction prefetch,
- * a (subscript-check-less) C table lookup
- * x = sample_range_limit[x];
- * is faster than explicit tests
- * if (x < 0) x = 0;
- * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
- * These processes all use a common table prepared by the routine below.
- *
- * For most steps we can mathematically guarantee that the initial value
- * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
- * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
- * limiting step (just after the IDCT), a wildly out-of-range value is
- * possible if the input data is corrupt. To avoid any chance of indexing
- * off the end of memory and getting a bad-pointer trap, we perform the
- * post-IDCT limiting thus:
- * x = range_limit[x & MASK];
- * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
- * samples. Under normal circumstances this is more than enough range and
- * a correct output will be generated; with bogus input data the mask will
- * cause wraparound, and we will safely generate a bogus-but-in-range output.
- * For the post-IDCT step, we want to convert the data from signed to unsigned
- * representation by adding CENTERJSAMPLE at the same time that we limit it.
- * So the post-IDCT limiting table ends up looking like this:
- * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
- * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
- * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
- * 0,1,...,CENTERJSAMPLE-1
- * Negative inputs select values from the upper half of the table after
- * masking.
- *
- * We can save some space by overlapping the start of the post-IDCT table
- * with the simpler range limiting table. The post-IDCT table begins at
- * sample_range_limit + CENTERJSAMPLE.
- *
- * Note that the table is allocated in near data space on PCs; it's small
- * enough and used often enough to justify this.
- */
-
-LOCAL(void)
-prepare_range_limit_table (j_decompress_ptr cinfo)
-/* Allocate and fill in the sample_range_limit table */
-{
- JSAMPLE * table;
- int i;
-
- table = (JSAMPLE *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
- table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
- cinfo->sample_range_limit = table;
- /* First segment of "simple" table: limit[x] = 0 for x < 0 */
- MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
- /* Main part of "simple" table: limit[x] = x */
- for (i = 0; i <= MAXJSAMPLE; i++)
- table[i] = (JSAMPLE) i;
- table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
- /* End of simple table, rest of first half of post-IDCT table */
- for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
- table[i] = MAXJSAMPLE;
- /* Second half of post-IDCT table */
- MEMZERO(table + (2 * (MAXJSAMPLE+1)),
- (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
- MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
- cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
-}
-
-
-/*
- * Master selection of decompression modules.
- * This is done once at jpeg_start_decompress time. We determine
- * which modules will be used and give them appropriate initialization calls.
- * We also initialize the decompressor input side to begin consuming data.
- *
- * Since jpeg_read_header has finished, we know what is in the SOF
- * and (first) SOS markers. We also have all the application parameter
- * settings.
- */
-
-LOCAL(void)
-master_selection (j_decompress_ptr cinfo)
-{
- my_master_ptr master = (my_master_ptr) cinfo->master;
- boolean use_c_buffer;
- long samplesperrow;
- JDIMENSION jd_samplesperrow;
-
- /* Initialize dimensions and other stuff */
- jpeg_calc_output_dimensions(cinfo);
- prepare_range_limit_table(cinfo);
-
- /* Width of an output scanline must be representable as JDIMENSION. */
- samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
- jd_samplesperrow = (JDIMENSION) samplesperrow;
- if ((long) jd_samplesperrow != samplesperrow)
- ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
-
- /* Initialize my private state */
- master->pass_number = 0;
- master->using_merged_upsample = use_merged_upsample(cinfo);
-
- /* Color quantizer selection */
- master->quantizer_1pass = NULL;
- master->quantizer_2pass = NULL;
- /* No mode changes if not using buffered-image mode. */
- if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
- cinfo->enable_1pass_quant = FALSE;
- cinfo->enable_external_quant = FALSE;
- cinfo->enable_2pass_quant = FALSE;
- }
- if (cinfo->quantize_colors) {
- if (cinfo->raw_data_out)
- ERREXIT(cinfo, JERR_NOTIMPL);
- /* 2-pass quantizer only works in 3-component color space. */
- if (cinfo->out_color_components != 3) {
- cinfo->enable_1pass_quant = TRUE;
- cinfo->enable_external_quant = FALSE;
- cinfo->enable_2pass_quant = FALSE;
- cinfo->colormap = NULL;
- } else if (cinfo->colormap != NULL) {
- cinfo->enable_external_quant = TRUE;
- } else if (cinfo->two_pass_quantize) {
- cinfo->enable_2pass_quant = TRUE;
- } else {
- cinfo->enable_1pass_quant = TRUE;
- }
-
- if (cinfo->enable_1pass_quant) {
-#ifdef QUANT_1PASS_SUPPORTED
- jinit_1pass_quantizer(cinfo);
- master->quantizer_1pass = cinfo->cquantize;
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- }
-
- /* We use the 2-pass code to map to external colormaps. */
- if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
-#ifdef QUANT_2PASS_SUPPORTED
- jinit_2pass_quantizer(cinfo);
- master->quantizer_2pass = cinfo->cquantize;
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- }
- /* If both quantizers are initialized, the 2-pass one is left active;
- * this is necessary for starting with quantization to an external map.
- */
- }
-
- /* Post-processing: in particular, color conversion first */
- if (! cinfo->raw_data_out) {
- if (master->using_merged_upsample) {
-#ifdef UPSAMPLE_MERGING_SUPPORTED
- jinit_merged_upsampler(cinfo); /* does color conversion too */
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else {
- jinit_color_deconverter(cinfo);
- jinit_upsampler(cinfo);
- }
- jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
- }
- /* Inverse DCT */
- jinit_inverse_dct(cinfo);
- /* Entropy decoding: either Huffman or arithmetic coding. */
- if (cinfo->arith_code) {
- ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
- } else {
- if (cinfo->progressive_mode) {
-#ifdef D_PROGRESSIVE_SUPPORTED
- jinit_phuff_decoder(cinfo);
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else
- jinit_huff_decoder(cinfo);
- }
-
- /* Initialize principal buffer controllers. */
- use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
- jinit_d_coef_controller(cinfo, use_c_buffer);
-
- if (! cinfo->raw_data_out)
- jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
-
- /* We can now tell the memory manager to allocate virtual arrays. */
- (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
-
- /* Initialize input side of decompressor to consume first scan. */
- (*cinfo->inputctl->start_input_pass) (cinfo);
-
-#ifdef D_MULTISCAN_FILES_SUPPORTED
- /* If jpeg_start_decompress will read the whole file, initialize
- * progress monitoring appropriately. The input step is counted
- * as one pass.
- */
- if (cinfo->progress != NULL && ! cinfo->buffered_image &&
- cinfo->inputctl->has_multiple_scans) {
- int nscans;
- /* Estimate number of scans to set pass_limit. */
- if (cinfo->progressive_mode) {
- /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
- nscans = 2 + 3 * cinfo->num_components;
- } else {
- /* For a nonprogressive multiscan file, estimate 1 scan per component. */
- nscans = cinfo->num_components;
- }
- cinfo->progress->pass_counter = 0L;
- cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
- cinfo->progress->completed_passes = 0;
- cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
- /* Count the input pass as done */
- master->pass_number++;
- }
-#endif /* D_MULTISCAN_FILES_SUPPORTED */
-}
-
-
-/*
- * Per-pass setup.
- * This is called at the beginning of each output pass. We determine which
- * modules will be active during this pass and give them appropriate
- * start_pass calls. We also set is_dummy_pass to indicate whether this
- * is a "real" output pass or a dummy pass for color quantization.
- * (In the latter case, jdapistd.c will crank the pass to completion.)
- */
-
-METHODDEF(void)
-prepare_for_output_pass (j_decompress_ptr cinfo)
-{
- my_master_ptr master = (my_master_ptr) cinfo->master;
-
- if (master->pub.is_dummy_pass) {
-#ifdef QUANT_2PASS_SUPPORTED
- /* Final pass of 2-pass quantization */
- master->pub.is_dummy_pass = FALSE;
- (*cinfo->cquantize->start_pass) (cinfo, FALSE);
- (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
- (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif /* QUANT_2PASS_SUPPORTED */
- } else {
- if (cinfo->quantize_colors && cinfo->colormap == NULL) {
- /* Select new quantization method */
- if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
- cinfo->cquantize = master->quantizer_2pass;
- master->pub.is_dummy_pass = TRUE;
- } else if (cinfo->enable_1pass_quant) {
- cinfo->cquantize = master->quantizer_1pass;
- } else {
- ERREXIT(cinfo, JERR_MODE_CHANGE);
- }
- }
- (*cinfo->idct->start_pass) (cinfo);
- (*cinfo->coef->start_output_pass) (cinfo);
- if (! cinfo->raw_data_out) {
- if (! master->using_merged_upsample)
- (*cinfo->cconvert->start_pass) (cinfo);
- (*cinfo->upsample->start_pass) (cinfo);
- if (cinfo->quantize_colors)
- (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
- (*cinfo->post->start_pass) (cinfo,
- (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
- (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
- }
- }
-
- /* Set up progress monitor's pass info if present */
- if (cinfo->progress != NULL) {
- cinfo->progress->completed_passes = master->pass_number;
- cinfo->progress->total_passes = master->pass_number +
- (master->pub.is_dummy_pass ? 2 : 1);
- /* In buffered-image mode, we assume one more output pass if EOI not
- * yet reached, but no more passes if EOI has been reached.
- */
- if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
- cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
- }
- }
-}
-
-
-/*
- * Finish up at end of an output pass.
- */
-
-METHODDEF(void)
-finish_output_pass (j_decompress_ptr cinfo)
-{
- my_master_ptr master = (my_master_ptr) cinfo->master;
-
- if (cinfo->quantize_colors)
- (*cinfo->cquantize->finish_pass) (cinfo);
- master->pass_number++;
-}
-
-
-#ifdef D_MULTISCAN_FILES_SUPPORTED
-
-/*
- * Switch to a new external colormap between output passes.
- */
-
-GLOBAL(void)
-jpeg_new_colormap (j_decompress_ptr cinfo)
-{
- my_master_ptr master = (my_master_ptr) cinfo->master;
-
- /* Prevent application from calling me at wrong times */
- if (cinfo->global_state != DSTATE_BUFIMAGE)
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
-
- if (cinfo->quantize_colors && cinfo->enable_external_quant &&
- cinfo->colormap != NULL) {
- /* Select 2-pass quantizer for external colormap use */
- cinfo->cquantize = master->quantizer_2pass;
- /* Notify quantizer of colormap change */
- (*cinfo->cquantize->new_color_map) (cinfo);
- master->pub.is_dummy_pass = FALSE; /* just in case */
- } else
- ERREXIT(cinfo, JERR_MODE_CHANGE);
-}
-
-#endif /* D_MULTISCAN_FILES_SUPPORTED */
-
-
-/*
- * Initialize master decompression control and select active modules.
- * This is performed at the start of jpeg_start_decompress.
- */
-
-GLOBAL(void)
-jinit_master_decompress (j_decompress_ptr cinfo)
-{
- my_master_ptr master;
-
- master = (my_master_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_decomp_master));
- cinfo->master = (struct jpeg_decomp_master *) master;
- master->pub.prepare_for_output_pass = prepare_for_output_pass;
- master->pub.finish_output_pass = finish_output_pass;
-
- master->pub.is_dummy_pass = FALSE;
-
- master_selection(cinfo);
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdmaster.c
+ *
+ * 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 master control logic for the JPEG decompressor.
+ * These routines are concerned with selecting the modules to be executed
+ * and with determining the number of passes and the work to be done in each
+ * pass.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private state */
+
+typedef struct {
+ struct jpeg_decomp_master pub; /* public fields */
+
+ int pass_number; /* # of passes completed */
+
+ boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
+
+ /* Saved references to initialized quantizer modules,
+ * in case we need to switch modes.
+ */
+ struct jpeg_color_quantizer * quantizer_1pass;
+ struct jpeg_color_quantizer * quantizer_2pass;
+} my_decomp_master;
+
+typedef my_decomp_master * my_master_ptr;
+
+
+/*
+ * Determine whether merged upsample/color conversion should be used.
+ * CRUCIAL: this must match the actual capabilities of jdmerge.c!
+ */
+
+LOCAL(boolean)
+use_merged_upsample (j_decompress_ptr cinfo)
+{
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+ /* Merging is the equivalent of plain box-filter upsampling */
+ if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
+ return FALSE;
+ /* jdmerge.c only supports YCC=>RGB color conversion */
+ if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
+ cinfo->out_color_space != JCS_RGB ||
+ cinfo->out_color_components != RGB_PIXELSIZE)
+ return FALSE;
+ /* and it only handles 2h1v or 2h2v sampling ratios */
+ if (cinfo->comp_info[0].h_samp_factor != 2 ||
+ cinfo->comp_info[1].h_samp_factor != 1 ||
+ cinfo->comp_info[2].h_samp_factor != 1 ||
+ cinfo->comp_info[0].v_samp_factor > 2 ||
+ cinfo->comp_info[1].v_samp_factor != 1 ||
+ cinfo->comp_info[2].v_samp_factor != 1)
+ return FALSE;
+ /* furthermore, it doesn't work if we've scaled the IDCTs differently */
+ if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
+ cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
+ cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
+ return FALSE;
+ /* ??? also need to test for upsample-time rescaling, when & if supported */
+ return TRUE; /* by golly, it'll work... */
+#else
+ return FALSE;
+#endif
+}
+
+
+/*
+ * Compute output image dimensions and related values.
+ * NOTE: this is exported for possible use by application.
+ * Hence it mustn't do anything that can't be done twice.
+ * Also note that it may be called before the master module is initialized!
+ */
+
+GLOBAL(void)
+jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
+/* Do computations that are needed before master selection phase */
+{
+#ifdef IDCT_SCALING_SUPPORTED
+ int ci;
+ jpeg_component_info *compptr;
+#endif
+
+ /* Prevent application from calling me at wrong times */
+ if (cinfo->global_state != DSTATE_READY)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+#ifdef IDCT_SCALING_SUPPORTED
+
+ /* Compute actual output image dimensions and DCT scaling choices. */
+ if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
+ /* Provide 1/8 scaling */
+ cinfo->output_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width, 8L);
+ cinfo->output_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height, 8L);
+ cinfo->min_DCT_scaled_size = 1;
+ } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
+ /* Provide 1/4 scaling */
+ cinfo->output_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width, 4L);
+ cinfo->output_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height, 4L);
+ cinfo->min_DCT_scaled_size = 2;
+ } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
+ /* Provide 1/2 scaling */
+ cinfo->output_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width, 2L);
+ cinfo->output_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height, 2L);
+ cinfo->min_DCT_scaled_size = 4;
+ } else {
+ /* Provide 1/1 scaling */
+ cinfo->output_width = cinfo->image_width;
+ cinfo->output_height = cinfo->image_height;
+ cinfo->min_DCT_scaled_size = DCTSIZE;
+ }
+ /* In selecting the actual DCT scaling for each component, we try to
+ * scale up the chroma components via IDCT scaling rather than upsampling.
+ * This saves time if the upsampler gets to use 1:1 scaling.
+ * Note this code assumes that the supported DCT scalings are powers of 2.
+ */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ int ssize = cinfo->min_DCT_scaled_size;
+ while (ssize < DCTSIZE &&
+ (compptr->h_samp_factor * ssize * 2 <=
+ cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
+ (compptr->v_samp_factor * ssize * 2 <=
+ cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
+ ssize = ssize * 2;
+ }
+ compptr->DCT_scaled_size = ssize;
+ }
+
+ /* Recompute downsampled dimensions of components;
+ * application needs to know these if using raw downsampled data.
+ */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Size in samples, after IDCT scaling */
+ compptr->downsampled_width = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_width *
+ (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
+ (long) (cinfo->max_h_samp_factor * DCTSIZE));
+ compptr->downsampled_height = (JDIMENSION)
+ jdiv_round_up((long) cinfo->image_height *
+ (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
+ (long) (cinfo->max_v_samp_factor * DCTSIZE));
+ }
+
+#else /* !IDCT_SCALING_SUPPORTED */
+
+ /* Hardwire it to "no scaling" */
+ cinfo->output_width = cinfo->image_width;
+ cinfo->output_height = cinfo->image_height;
+ /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
+ * and has computed unscaled downsampled_width and downsampled_height.
+ */
+
+#endif /* IDCT_SCALING_SUPPORTED */
+
+ /* Report number of components in selected colorspace. */
+ /* Probably this should be in the color conversion module... */
+ switch (cinfo->out_color_space) {
+ case JCS_GRAYSCALE:
+ cinfo->out_color_components = 1;
+ break;
+ case JCS_RGB:
+#if RGB_PIXELSIZE != 3
+ cinfo->out_color_components = RGB_PIXELSIZE;
+ break;
+#endif /* else share code with YCbCr */
+ case JCS_YCbCr:
+ cinfo->out_color_components = 3;
+ break;
+ case JCS_CMYK:
+ case JCS_YCCK:
+ cinfo->out_color_components = 4;
+ break;
+ default: /* else must be same colorspace as in file */
+ cinfo->out_color_components = cinfo->num_components;
+ break;
+ }
+ cinfo->output_components = (cinfo->quantize_colors ? 1 :
+ cinfo->out_color_components);
+
+ /* See if upsampler will want to emit more than one row at a time */
+ if (use_merged_upsample(cinfo))
+ cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
+ else
+ cinfo->rec_outbuf_height = 1;
+}
+
+
+/*
+ * Several decompression processes need to range-limit values to the range
+ * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
+ * due to noise introduced by quantization, roundoff error, etc. These
+ * processes are inner loops and need to be as fast as possible. On most
+ * machines, particularly CPUs with pipelines or instruction prefetch,
+ * a (subscript-check-less) C table lookup
+ * x = sample_range_limit[x];
+ * is faster than explicit tests
+ * if (x < 0) x = 0;
+ * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
+ * These processes all use a common table prepared by the routine below.
+ *
+ * For most steps we can mathematically guarantee that the initial value
+ * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
+ * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
+ * limiting step (just after the IDCT), a wildly out-of-range value is
+ * possible if the input data is corrupt. To avoid any chance of indexing
+ * off the end of memory and getting a bad-pointer trap, we perform the
+ * post-IDCT limiting thus:
+ * x = range_limit[x & MASK];
+ * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
+ * samples. Under normal circumstances this is more than enough range and
+ * a correct output will be generated; with bogus input data the mask will
+ * cause wraparound, and we will safely generate a bogus-but-in-range output.
+ * For the post-IDCT step, we want to convert the data from signed to unsigned
+ * representation by adding CENTERJSAMPLE at the same time that we limit it.
+ * So the post-IDCT limiting table ends up looking like this:
+ * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
+ * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
+ * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
+ * 0,1,...,CENTERJSAMPLE-1
+ * Negative inputs select values from the upper half of the table after
+ * masking.
+ *
+ * We can save some space by overlapping the start of the post-IDCT table
+ * with the simpler range limiting table. The post-IDCT table begins at
+ * sample_range_limit + CENTERJSAMPLE.
+ *
+ * Note that the table is allocated in near data space on PCs; it's small
+ * enough and used often enough to justify this.
+ */
+
+LOCAL(void)
+prepare_range_limit_table (j_decompress_ptr cinfo)
+/* Allocate and fill in the sample_range_limit table */
+{
+ JSAMPLE * table;
+ int i;
+
+ table = (JSAMPLE *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
+ table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
+ cinfo->sample_range_limit = table;
+ /* First segment of "simple" table: limit[x] = 0 for x < 0 */
+ MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
+ /* Main part of "simple" table: limit[x] = x */
+ for (i = 0; i <= MAXJSAMPLE; i++)
+ table[i] = (JSAMPLE) i;
+ table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
+ /* End of simple table, rest of first half of post-IDCT table */
+ for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
+ table[i] = MAXJSAMPLE;
+ /* Second half of post-IDCT table */
+ MEMZERO(table + (2 * (MAXJSAMPLE+1)),
+ (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
+ MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
+ cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
+}
+
+
+/*
+ * Master selection of decompression modules.
+ * This is done once at jpeg_start_decompress time. We determine
+ * which modules will be used and give them appropriate initialization calls.
+ * We also initialize the decompressor input side to begin consuming data.
+ *
+ * Since jpeg_read_header has finished, we know what is in the SOF
+ * and (first) SOS markers. We also have all the application parameter
+ * settings.
+ */
+
+LOCAL(void)
+master_selection (j_decompress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+ boolean use_c_buffer;
+ long samplesperrow;
+ JDIMENSION jd_samplesperrow;
+
+ /* Initialize dimensions and other stuff */
+ jpeg_calc_output_dimensions(cinfo);
+ prepare_range_limit_table(cinfo);
+
+ /* Width of an output scanline must be representable as JDIMENSION. */
+ samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
+ jd_samplesperrow = (JDIMENSION) samplesperrow;
+ if ((long) jd_samplesperrow != samplesperrow)
+ ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+
+ /* Initialize my private state */
+ master->pass_number = 0;
+ master->using_merged_upsample = use_merged_upsample(cinfo);
+
+ /* Color quantizer selection */
+ master->quantizer_1pass = NULL;
+ master->quantizer_2pass = NULL;
+ /* No mode changes if not using buffered-image mode. */
+ if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
+ cinfo->enable_1pass_quant = FALSE;
+ cinfo->enable_external_quant = FALSE;
+ cinfo->enable_2pass_quant = FALSE;
+ }
+ if (cinfo->quantize_colors) {
+ if (cinfo->raw_data_out)
+ ERREXIT(cinfo, JERR_NOTIMPL);
+ /* 2-pass quantizer only works in 3-component color space. */
+ if (cinfo->out_color_components != 3) {
+ cinfo->enable_1pass_quant = TRUE;
+ cinfo->enable_external_quant = FALSE;
+ cinfo->enable_2pass_quant = FALSE;
+ cinfo->colormap = NULL;
+ } else if (cinfo->colormap != NULL) {
+ cinfo->enable_external_quant = TRUE;
+ } else if (cinfo->two_pass_quantize) {
+ cinfo->enable_2pass_quant = TRUE;
+ } else {
+ cinfo->enable_1pass_quant = TRUE;
+ }
+
+ if (cinfo->enable_1pass_quant) {
+#ifdef QUANT_1PASS_SUPPORTED
+ jinit_1pass_quantizer(cinfo);
+ master->quantizer_1pass = cinfo->cquantize;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ }
+
+ /* We use the 2-pass code to map to external colormaps. */
+ if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
+#ifdef QUANT_2PASS_SUPPORTED
+ jinit_2pass_quantizer(cinfo);
+ master->quantizer_2pass = cinfo->cquantize;
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ }
+ /* If both quantizers are initialized, the 2-pass one is left active;
+ * this is necessary for starting with quantization to an external map.
+ */
+ }
+
+ /* Post-processing: in particular, color conversion first */
+ if (! cinfo->raw_data_out) {
+ if (master->using_merged_upsample) {
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+ jinit_merged_upsampler(cinfo); /* does color conversion too */
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else {
+ jinit_color_deconverter(cinfo);
+ jinit_upsampler(cinfo);
+ }
+ jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
+ }
+ /* Inverse DCT */
+ jinit_inverse_dct(cinfo);
+ /* Entropy decoding: either Huffman or arithmetic coding. */
+ if (cinfo->arith_code) {
+ ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+ } else {
+ if (cinfo->progressive_mode) {
+#ifdef D_PROGRESSIVE_SUPPORTED
+ jinit_phuff_decoder(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else
+ jinit_huff_decoder(cinfo);
+ }
+
+ /* Initialize principal buffer controllers. */
+ use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
+ jinit_d_coef_controller(cinfo, use_c_buffer);
+
+ if (! cinfo->raw_data_out)
+ jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
+
+ /* We can now tell the memory manager to allocate virtual arrays. */
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+ /* Initialize input side of decompressor to consume first scan. */
+ (*cinfo->inputctl->start_input_pass) (cinfo);
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+ /* If jpeg_start_decompress will read the whole file, initialize
+ * progress monitoring appropriately. The input step is counted
+ * as one pass.
+ */
+ if (cinfo->progress != NULL && ! cinfo->buffered_image &&
+ cinfo->inputctl->has_multiple_scans) {
+ int nscans;
+ /* Estimate number of scans to set pass_limit. */
+ if (cinfo->progressive_mode) {
+ /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
+ nscans = 2 + 3 * cinfo->num_components;
+ } else {
+ /* For a nonprogressive multiscan file, estimate 1 scan per component. */
+ nscans = cinfo->num_components;
+ }
+ cinfo->progress->pass_counter = 0L;
+ cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
+ cinfo->progress->completed_passes = 0;
+ cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
+ /* Count the input pass as done */
+ master->pass_number++;
+ }
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+}
+
+
+/*
+ * Per-pass setup.
+ * This is called at the beginning of each output pass. We determine which
+ * modules will be active during this pass and give them appropriate
+ * start_pass calls. We also set is_dummy_pass to indicate whether this
+ * is a "real" output pass or a dummy pass for color quantization.
+ * (In the latter case, jdapistd.c will crank the pass to completion.)
+ */
+
+METHODDEF(void)
+prepare_for_output_pass (j_decompress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ if (master->pub.is_dummy_pass) {
+#ifdef QUANT_2PASS_SUPPORTED
+ /* Final pass of 2-pass quantization */
+ master->pub.is_dummy_pass = FALSE;
+ (*cinfo->cquantize->start_pass) (cinfo, FALSE);
+ (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
+ (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif /* QUANT_2PASS_SUPPORTED */
+ } else {
+ if (cinfo->quantize_colors && cinfo->colormap == NULL) {
+ /* Select new quantization method */
+ if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
+ cinfo->cquantize = master->quantizer_2pass;
+ master->pub.is_dummy_pass = TRUE;
+ } else if (cinfo->enable_1pass_quant) {
+ cinfo->cquantize = master->quantizer_1pass;
+ } else {
+ ERREXIT(cinfo, JERR_MODE_CHANGE);
+ }
+ }
+ (*cinfo->idct->start_pass) (cinfo);
+ (*cinfo->coef->start_output_pass) (cinfo);
+ if (! cinfo->raw_data_out) {
+ if (! master->using_merged_upsample)
+ (*cinfo->cconvert->start_pass) (cinfo);
+ (*cinfo->upsample->start_pass) (cinfo);
+ if (cinfo->quantize_colors)
+ (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
+ (*cinfo->post->start_pass) (cinfo,
+ (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
+ (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
+ }
+ }
+
+ /* Set up progress monitor's pass info if present */
+ if (cinfo->progress != NULL) {
+ cinfo->progress->completed_passes = master->pass_number;
+ cinfo->progress->total_passes = master->pass_number +
+ (master->pub.is_dummy_pass ? 2 : 1);
+ /* In buffered-image mode, we assume one more output pass if EOI not
+ * yet reached, but no more passes if EOI has been reached.
+ */
+ if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
+ cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
+ }
+ }
+}
+
+
+/*
+ * Finish up at end of an output pass.
+ */
+
+METHODDEF(void)
+finish_output_pass (j_decompress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ if (cinfo->quantize_colors)
+ (*cinfo->cquantize->finish_pass) (cinfo);
+ master->pass_number++;
+}
+
+
+#ifdef D_MULTISCAN_FILES_SUPPORTED
+
+/*
+ * Switch to a new external colormap between output passes.
+ */
+
+GLOBAL(void)
+jpeg_new_colormap (j_decompress_ptr cinfo)
+{
+ my_master_ptr master = (my_master_ptr) cinfo->master;
+
+ /* Prevent application from calling me at wrong times */
+ if (cinfo->global_state != DSTATE_BUFIMAGE)
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+ if (cinfo->quantize_colors && cinfo->enable_external_quant &&
+ cinfo->colormap != NULL) {
+ /* Select 2-pass quantizer for external colormap use */
+ cinfo->cquantize = master->quantizer_2pass;
+ /* Notify quantizer of colormap change */
+ (*cinfo->cquantize->new_color_map) (cinfo);
+ master->pub.is_dummy_pass = FALSE; /* just in case */
+ } else
+ ERREXIT(cinfo, JERR_MODE_CHANGE);
+}
+
+#endif /* D_MULTISCAN_FILES_SUPPORTED */
+
+
+/*
+ * Initialize master decompression control and select active modules.
+ * This is performed at the start of jpeg_start_decompress.
+ */
+
+GLOBAL(void)
+jinit_master_decompress (j_decompress_ptr cinfo)
+{
+ my_master_ptr master;
+
+ master = (my_master_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_decomp_master));
+ cinfo->master = (struct jpeg_decomp_master *) master;
+ master->pub.prepare_for_output_pass = prepare_for_output_pass;
+ master->pub.finish_output_pass = finish_output_pass;
+
+ master->pub.is_dummy_pass = FALSE;
+
+ master_selection(cinfo);
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c b/core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c
index 29a996c063..c229f86aa5 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdmerge.c
@@ -1,406 +1,406 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdmerge.c
- *
- * Copyright (C) 1994-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 contains code for merged upsampling/color conversion.
- *
- * This file combines functions from jdsample.c and jdcolor.c;
- * read those files first to understand what's going on.
- *
- * When the chroma components are to be upsampled by simple replication
- * (ie, box filtering), we can save some work in color conversion by
- * calculating all the output pixels corresponding to a pair of chroma
- * samples at one time. In the conversion equations
- * R = Y + K1 * Cr
- * G = Y + K2 * Cb + K3 * Cr
- * B = Y + K4 * Cb
- * only the Y term varies among the group of pixels corresponding to a pair
- * of chroma samples, so the rest of the terms can be calculated just once.
- * At typical sampling ratios, this eliminates half or three-quarters of the
- * multiplications needed for color conversion.
- *
- * This file currently provides implementations for the following cases:
- * YCbCr => RGB color conversion only.
- * Sampling ratios of 2h1v or 2h2v.
- * No scaling needed at upsample time.
- * Corner-aligned (non-CCIR601) sampling alignment.
- * Other special cases could be added, but in most applications these are
- * the only common cases. (For uncommon cases we fall back on the more
- * general code in jdsample.c and jdcolor.c.)
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-#ifdef UPSAMPLE_MERGING_SUPPORTED
-
-#ifdef _FX_MANAGED_CODE_
-#define my_upsampler my_upsampler_m
-#endif
-
-/* Private subobject */
-
-typedef struct {
- struct jpeg_upsampler pub; /* public fields */
-
- /* Pointer to routine to do actual upsampling/conversion of one row group */
- JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
- JSAMPARRAY output_buf));
-
- /* Private state for YCC->RGB conversion */
- int * Cr_r_tab; /* => table for Cr to R conversion */
- int * Cb_b_tab; /* => table for Cb to B conversion */
- INT32 * Cr_g_tab; /* => table for Cr to G conversion */
- INT32 * Cb_g_tab; /* => table for Cb to G conversion */
-
- /* For 2:1 vertical sampling, we produce two output rows at a time.
- * We need a "spare" row buffer to hold the second output row if the
- * application provides just a one-row buffer; we also use the spare
- * to discard the dummy last row if the image height is odd.
- */
- JSAMPROW spare_row;
- boolean spare_full; /* T if spare buffer is occupied */
-
- JDIMENSION out_row_width; /* samples per output row */
- JDIMENSION rows_to_go; /* counts rows remaining in image */
-} my_upsampler;
-
-typedef my_upsampler * my_upsample_ptr;
-
-#define SCALEBITS 16 /* speediest right-shift on some machines */
-#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
-#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
-
-
-/*
- * Initialize tables for YCC->RGB colorspace conversion.
- * This is taken directly from jdcolor.c; see that file for more info.
- */
-
-LOCAL(void)
-build_ycc_rgb_table (j_decompress_ptr cinfo)
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
- int i;
- INT32 x;
- SHIFT_TEMPS
-
- upsample->Cr_r_tab = (int *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (MAXJSAMPLE+1) * SIZEOF(int));
- upsample->Cb_b_tab = (int *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (MAXJSAMPLE+1) * SIZEOF(int));
- upsample->Cr_g_tab = (INT32 *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (MAXJSAMPLE+1) * SIZEOF(INT32));
- upsample->Cb_g_tab = (INT32 *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (MAXJSAMPLE+1) * SIZEOF(INT32));
-
- for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
- /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
- /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
- /* Cr=>R value is nearest int to 1.40200 * x */
- upsample->Cr_r_tab[i] = (int)
- RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
- /* Cb=>B value is nearest int to 1.77200 * x */
- upsample->Cb_b_tab[i] = (int)
- RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
- /* Cr=>G value is scaled-up -0.71414 * x */
- upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
- /* Cb=>G value is scaled-up -0.34414 * x */
- /* We also add in ONE_HALF so that need not do it in inner loop */
- upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
- }
-}
-
-
-/*
- * Initialize for an upsampling pass.
- */
-
-METHODDEF(void)
-start_pass_merged_upsample (j_decompress_ptr cinfo)
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
-
- /* Mark the spare buffer empty */
- upsample->spare_full = FALSE;
- /* Initialize total-height counter for detecting bottom of image */
- upsample->rows_to_go = cinfo->output_height;
-}
-
-
-/*
- * Control routine to do upsampling (and color conversion).
- *
- * The control routine just handles the row buffering considerations.
- */
-
-METHODDEF(void)
-merged_2v_upsample (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-/* 2:1 vertical sampling case: may need a spare row. */
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
- JSAMPROW work_ptrs[2];
- JDIMENSION num_rows; /* number of rows returned to caller */
-
- if (upsample->spare_full) {
- /* If we have a spare row saved from a previous cycle, just return it. */
- jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
- 1, upsample->out_row_width);
- num_rows = 1;
- upsample->spare_full = FALSE;
- } else {
- /* Figure number of rows to return to caller. */
- num_rows = 2;
- /* Not more than the distance to the end of the image. */
- if (num_rows > upsample->rows_to_go)
- num_rows = upsample->rows_to_go;
- /* And not more than what the client can accept: */
- out_rows_avail -= *out_row_ctr;
- if (num_rows > out_rows_avail)
- num_rows = out_rows_avail;
- /* Create output pointer array for upsampler. */
- work_ptrs[0] = output_buf[*out_row_ctr];
- if (num_rows > 1) {
- work_ptrs[1] = output_buf[*out_row_ctr + 1];
- } else {
- work_ptrs[1] = upsample->spare_row;
- upsample->spare_full = TRUE;
- }
- /* Now do the upsampling. */
- (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
- }
-
- /* Adjust counts */
- *out_row_ctr += num_rows;
- upsample->rows_to_go -= num_rows;
- /* When the buffer is emptied, declare this input row group consumed */
- if (! upsample->spare_full)
- (*in_row_group_ctr)++;
-}
-
-
-METHODDEF(void)
-merged_1v_upsample (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-/* 1:1 vertical sampling case: much easier, never need a spare row. */
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
-
- /* Just do the upsampling. */
- (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
- output_buf + *out_row_ctr);
- /* Adjust counts */
- (*out_row_ctr)++;
- (*in_row_group_ctr)++;
-}
-
-
-/*
- * These are the routines invoked by the control routines to do
- * the actual upsampling/conversion. One row group is processed per call.
- *
- * Note: since we may be writing directly into application-supplied buffers,
- * we have to be honest about the output width; we can't assume the buffer
- * has been rounded up to an even width.
- */
-
-
-/*
- * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
- */
-
-METHODDEF(void)
-h2v1_merged_upsample (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
- JSAMPARRAY output_buf)
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
- register int y, cred, cgreen, cblue;
- int cb, cr;
- register JSAMPROW outptr;
- JSAMPROW inptr0, inptr1, inptr2;
- JDIMENSION col;
- /* copy these pointers into registers if possible */
- register JSAMPLE * range_limit = cinfo->sample_range_limit;
- int * Crrtab = upsample->Cr_r_tab;
- int * Cbbtab = upsample->Cb_b_tab;
- INT32 * Crgtab = upsample->Cr_g_tab;
- INT32 * Cbgtab = upsample->Cb_g_tab;
- SHIFT_TEMPS
-
- inptr0 = input_buf[0][in_row_group_ctr];
- inptr1 = input_buf[1][in_row_group_ctr];
- inptr2 = input_buf[2][in_row_group_ctr];
- outptr = output_buf[0];
- /* Loop for each pair of output pixels */
- for (col = cinfo->output_width >> 1; col > 0; col--) {
- /* Do the chroma part of the calculation */
- cb = GETJSAMPLE(*inptr1++);
- cr = GETJSAMPLE(*inptr2++);
- cred = Crrtab[cr];
- cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
- cblue = Cbbtab[cb];
- /* Fetch 2 Y values and emit 2 pixels */
- y = GETJSAMPLE(*inptr0++);
- outptr[RGB_RED] = range_limit[y + cred];
- outptr[RGB_GREEN] = range_limit[y + cgreen];
- outptr[RGB_BLUE] = range_limit[y + cblue];
- outptr += RGB_PIXELSIZE;
- y = GETJSAMPLE(*inptr0++);
- outptr[RGB_RED] = range_limit[y + cred];
- outptr[RGB_GREEN] = range_limit[y + cgreen];
- outptr[RGB_BLUE] = range_limit[y + cblue];
- outptr += RGB_PIXELSIZE;
- }
- /* If image width is odd, do the last output column separately */
- if (cinfo->output_width & 1) {
- cb = GETJSAMPLE(*inptr1);
- cr = GETJSAMPLE(*inptr2);
- cred = Crrtab[cr];
- cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
- cblue = Cbbtab[cb];
- y = GETJSAMPLE(*inptr0);
- outptr[RGB_RED] = range_limit[y + cred];
- outptr[RGB_GREEN] = range_limit[y + cgreen];
- outptr[RGB_BLUE] = range_limit[y + cblue];
- }
-}
-
-
-/*
- * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
- */
-
-METHODDEF(void)
-h2v2_merged_upsample (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
- JSAMPARRAY output_buf)
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
- register int y, cred, cgreen, cblue;
- int cb, cr;
- register JSAMPROW outptr0, outptr1;
- JSAMPROW inptr00, inptr01, inptr1, inptr2;
- JDIMENSION col;
- /* copy these pointers into registers if possible */
- register JSAMPLE * range_limit = cinfo->sample_range_limit;
- int * Crrtab = upsample->Cr_r_tab;
- int * Cbbtab = upsample->Cb_b_tab;
- INT32 * Crgtab = upsample->Cr_g_tab;
- INT32 * Cbgtab = upsample->Cb_g_tab;
- SHIFT_TEMPS
-
- inptr00 = input_buf[0][in_row_group_ctr*2];
- inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
- inptr1 = input_buf[1][in_row_group_ctr];
- inptr2 = input_buf[2][in_row_group_ctr];
- outptr0 = output_buf[0];
- outptr1 = output_buf[1];
- /* Loop for each group of output pixels */
- for (col = cinfo->output_width >> 1; col > 0; col--) {
- /* Do the chroma part of the calculation */
- cb = GETJSAMPLE(*inptr1++);
- cr = GETJSAMPLE(*inptr2++);
- cred = Crrtab[cr];
- cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
- cblue = Cbbtab[cb];
- /* Fetch 4 Y values and emit 4 pixels */
- y = GETJSAMPLE(*inptr00++);
- outptr0[RGB_RED] = range_limit[y + cred];
- outptr0[RGB_GREEN] = range_limit[y + cgreen];
- outptr0[RGB_BLUE] = range_limit[y + cblue];
- outptr0 += RGB_PIXELSIZE;
- y = GETJSAMPLE(*inptr00++);
- outptr0[RGB_RED] = range_limit[y + cred];
- outptr0[RGB_GREEN] = range_limit[y + cgreen];
- outptr0[RGB_BLUE] = range_limit[y + cblue];
- outptr0 += RGB_PIXELSIZE;
- y = GETJSAMPLE(*inptr01++);
- outptr1[RGB_RED] = range_limit[y + cred];
- outptr1[RGB_GREEN] = range_limit[y + cgreen];
- outptr1[RGB_BLUE] = range_limit[y + cblue];
- outptr1 += RGB_PIXELSIZE;
- y = GETJSAMPLE(*inptr01++);
- outptr1[RGB_RED] = range_limit[y + cred];
- outptr1[RGB_GREEN] = range_limit[y + cgreen];
- outptr1[RGB_BLUE] = range_limit[y + cblue];
- outptr1 += RGB_PIXELSIZE;
- }
- /* If image width is odd, do the last output column separately */
- if (cinfo->output_width & 1) {
- cb = GETJSAMPLE(*inptr1);
- cr = GETJSAMPLE(*inptr2);
- cred = Crrtab[cr];
- cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
- cblue = Cbbtab[cb];
- y = GETJSAMPLE(*inptr00);
- outptr0[RGB_RED] = range_limit[y + cred];
- outptr0[RGB_GREEN] = range_limit[y + cgreen];
- outptr0[RGB_BLUE] = range_limit[y + cblue];
- y = GETJSAMPLE(*inptr01);
- outptr1[RGB_RED] = range_limit[y + cred];
- outptr1[RGB_GREEN] = range_limit[y + cgreen];
- outptr1[RGB_BLUE] = range_limit[y + cblue];
- }
-}
-
-
-/*
- * Module initialization routine for merged upsampling/color conversion.
- *
- * NB: this is called under the conditions determined by use_merged_upsample()
- * in jdmaster.c. That routine MUST correspond to the actual capabilities
- * of this module; no safety checks are made here.
- */
-
-GLOBAL(void)
-jinit_merged_upsampler (j_decompress_ptr cinfo)
-{
- my_upsample_ptr upsample;
-
- upsample = (my_upsample_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_upsampler));
- cinfo->upsample = (struct jpeg_upsampler *) upsample;
- upsample->pub.start_pass = start_pass_merged_upsample;
- upsample->pub.need_context_rows = FALSE;
-
- upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
-
- if (cinfo->max_v_samp_factor == 2) {
- upsample->pub.upsample = merged_2v_upsample;
- upsample->upmethod = h2v2_merged_upsample;
- /* Allocate a spare row buffer */
- upsample->spare_row = (JSAMPROW)
- (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
- } else {
- upsample->pub.upsample = merged_1v_upsample;
- upsample->upmethod = h2v1_merged_upsample;
- /* No spare row needed */
- upsample->spare_row = NULL;
- }
-
- build_ycc_rgb_table(cinfo);
-}
-
-#endif /* UPSAMPLE_MERGING_SUPPORTED */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdmerge.c
+ *
+ * Copyright (C) 1994-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 contains code for merged upsampling/color conversion.
+ *
+ * This file combines functions from jdsample.c and jdcolor.c;
+ * read those files first to understand what's going on.
+ *
+ * When the chroma components are to be upsampled by simple replication
+ * (ie, box filtering), we can save some work in color conversion by
+ * calculating all the output pixels corresponding to a pair of chroma
+ * samples at one time. In the conversion equations
+ * R = Y + K1 * Cr
+ * G = Y + K2 * Cb + K3 * Cr
+ * B = Y + K4 * Cb
+ * only the Y term varies among the group of pixels corresponding to a pair
+ * of chroma samples, so the rest of the terms can be calculated just once.
+ * At typical sampling ratios, this eliminates half or three-quarters of the
+ * multiplications needed for color conversion.
+ *
+ * This file currently provides implementations for the following cases:
+ * YCbCr => RGB color conversion only.
+ * Sampling ratios of 2h1v or 2h2v.
+ * No scaling needed at upsample time.
+ * Corner-aligned (non-CCIR601) sampling alignment.
+ * Other special cases could be added, but in most applications these are
+ * the only common cases. (For uncommon cases we fall back on the more
+ * general code in jdsample.c and jdcolor.c.)
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+#ifdef UPSAMPLE_MERGING_SUPPORTED
+
+#ifdef _FX_MANAGED_CODE_
+#define my_upsampler my_upsampler_m
+#endif
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_upsampler pub; /* public fields */
+
+ /* Pointer to routine to do actual upsampling/conversion of one row group */
+ JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+ JSAMPARRAY output_buf));
+
+ /* Private state for YCC->RGB conversion */
+ int * Cr_r_tab; /* => table for Cr to R conversion */
+ int * Cb_b_tab; /* => table for Cb to B conversion */
+ INT32 * Cr_g_tab; /* => table for Cr to G conversion */
+ INT32 * Cb_g_tab; /* => table for Cb to G conversion */
+
+ /* For 2:1 vertical sampling, we produce two output rows at a time.
+ * We need a "spare" row buffer to hold the second output row if the
+ * application provides just a one-row buffer; we also use the spare
+ * to discard the dummy last row if the image height is odd.
+ */
+ JSAMPROW spare_row;
+ boolean spare_full; /* T if spare buffer is occupied */
+
+ JDIMENSION out_row_width; /* samples per output row */
+ JDIMENSION rows_to_go; /* counts rows remaining in image */
+} my_upsampler;
+
+typedef my_upsampler * my_upsample_ptr;
+
+#define SCALEBITS 16 /* speediest right-shift on some machines */
+#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
+#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
+
+
+/*
+ * Initialize tables for YCC->RGB colorspace conversion.
+ * This is taken directly from jdcolor.c; see that file for more info.
+ */
+
+LOCAL(void)
+build_ycc_rgb_table (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ int i;
+ INT32 x;
+ SHIFT_TEMPS
+
+ upsample->Cr_r_tab = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(int));
+ upsample->Cb_b_tab = (int *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(int));
+ upsample->Cr_g_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(INT32));
+ upsample->Cb_g_tab = (INT32 *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (MAXJSAMPLE+1) * SIZEOF(INT32));
+
+ for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
+ /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
+ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
+ /* Cr=>R value is nearest int to 1.40200 * x */
+ upsample->Cr_r_tab[i] = (int)
+ RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
+ /* Cb=>B value is nearest int to 1.77200 * x */
+ upsample->Cb_b_tab[i] = (int)
+ RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
+ /* Cr=>G value is scaled-up -0.71414 * x */
+ upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
+ /* Cb=>G value is scaled-up -0.34414 * x */
+ /* We also add in ONE_HALF so that need not do it in inner loop */
+ upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
+ }
+}
+
+
+/*
+ * Initialize for an upsampling pass.
+ */
+
+METHODDEF(void)
+start_pass_merged_upsample (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+
+ /* Mark the spare buffer empty */
+ upsample->spare_full = FALSE;
+ /* Initialize total-height counter for detecting bottom of image */
+ upsample->rows_to_go = cinfo->output_height;
+}
+
+
+/*
+ * Control routine to do upsampling (and color conversion).
+ *
+ * The control routine just handles the row buffering considerations.
+ */
+
+METHODDEF(void)
+merged_2v_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+/* 2:1 vertical sampling case: may need a spare row. */
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ JSAMPROW work_ptrs[2];
+ JDIMENSION num_rows; /* number of rows returned to caller */
+
+ if (upsample->spare_full) {
+ /* If we have a spare row saved from a previous cycle, just return it. */
+ jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
+ 1, upsample->out_row_width);
+ num_rows = 1;
+ upsample->spare_full = FALSE;
+ } else {
+ /* Figure number of rows to return to caller. */
+ num_rows = 2;
+ /* Not more than the distance to the end of the image. */
+ if (num_rows > upsample->rows_to_go)
+ num_rows = upsample->rows_to_go;
+ /* And not more than what the client can accept: */
+ out_rows_avail -= *out_row_ctr;
+ if (num_rows > out_rows_avail)
+ num_rows = out_rows_avail;
+ /* Create output pointer array for upsampler. */
+ work_ptrs[0] = output_buf[*out_row_ctr];
+ if (num_rows > 1) {
+ work_ptrs[1] = output_buf[*out_row_ctr + 1];
+ } else {
+ work_ptrs[1] = upsample->spare_row;
+ upsample->spare_full = TRUE;
+ }
+ /* Now do the upsampling. */
+ (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
+ }
+
+ /* Adjust counts */
+ *out_row_ctr += num_rows;
+ upsample->rows_to_go -= num_rows;
+ /* When the buffer is emptied, declare this input row group consumed */
+ if (! upsample->spare_full)
+ (*in_row_group_ctr)++;
+}
+
+
+METHODDEF(void)
+merged_1v_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+/* 1:1 vertical sampling case: much easier, never need a spare row. */
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+
+ /* Just do the upsampling. */
+ (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
+ output_buf + *out_row_ctr);
+ /* Adjust counts */
+ (*out_row_ctr)++;
+ (*in_row_group_ctr)++;
+}
+
+
+/*
+ * These are the routines invoked by the control routines to do
+ * the actual upsampling/conversion. One row group is processed per call.
+ *
+ * Note: since we may be writing directly into application-supplied buffers,
+ * we have to be honest about the output width; we can't assume the buffer
+ * has been rounded up to an even width.
+ */
+
+
+/*
+ * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
+ */
+
+METHODDEF(void)
+h2v1_merged_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+ JSAMPARRAY output_buf)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ register int y, cred, cgreen, cblue;
+ int cb, cr;
+ register JSAMPROW outptr;
+ JSAMPROW inptr0, inptr1, inptr2;
+ JDIMENSION col;
+ /* copy these pointers into registers if possible */
+ register JSAMPLE * range_limit = cinfo->sample_range_limit;
+ int * Crrtab = upsample->Cr_r_tab;
+ int * Cbbtab = upsample->Cb_b_tab;
+ INT32 * Crgtab = upsample->Cr_g_tab;
+ INT32 * Cbgtab = upsample->Cb_g_tab;
+ SHIFT_TEMPS
+
+ inptr0 = input_buf[0][in_row_group_ctr];
+ inptr1 = input_buf[1][in_row_group_ctr];
+ inptr2 = input_buf[2][in_row_group_ctr];
+ outptr = output_buf[0];
+ /* Loop for each pair of output pixels */
+ for (col = cinfo->output_width >> 1; col > 0; col--) {
+ /* Do the chroma part of the calculation */
+ cb = GETJSAMPLE(*inptr1++);
+ cr = GETJSAMPLE(*inptr2++);
+ cred = Crrtab[cr];
+ cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+ cblue = Cbbtab[cb];
+ /* Fetch 2 Y values and emit 2 pixels */
+ y = GETJSAMPLE(*inptr0++);
+ outptr[RGB_RED] = range_limit[y + cred];
+ outptr[RGB_GREEN] = range_limit[y + cgreen];
+ outptr[RGB_BLUE] = range_limit[y + cblue];
+ outptr += RGB_PIXELSIZE;
+ y = GETJSAMPLE(*inptr0++);
+ outptr[RGB_RED] = range_limit[y + cred];
+ outptr[RGB_GREEN] = range_limit[y + cgreen];
+ outptr[RGB_BLUE] = range_limit[y + cblue];
+ outptr += RGB_PIXELSIZE;
+ }
+ /* If image width is odd, do the last output column separately */
+ if (cinfo->output_width & 1) {
+ cb = GETJSAMPLE(*inptr1);
+ cr = GETJSAMPLE(*inptr2);
+ cred = Crrtab[cr];
+ cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+ cblue = Cbbtab[cb];
+ y = GETJSAMPLE(*inptr0);
+ outptr[RGB_RED] = range_limit[y + cred];
+ outptr[RGB_GREEN] = range_limit[y + cgreen];
+ outptr[RGB_BLUE] = range_limit[y + cblue];
+ }
+}
+
+
+/*
+ * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
+ */
+
+METHODDEF(void)
+h2v2_merged_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
+ JSAMPARRAY output_buf)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ register int y, cred, cgreen, cblue;
+ int cb, cr;
+ register JSAMPROW outptr0, outptr1;
+ JSAMPROW inptr00, inptr01, inptr1, inptr2;
+ JDIMENSION col;
+ /* copy these pointers into registers if possible */
+ register JSAMPLE * range_limit = cinfo->sample_range_limit;
+ int * Crrtab = upsample->Cr_r_tab;
+ int * Cbbtab = upsample->Cb_b_tab;
+ INT32 * Crgtab = upsample->Cr_g_tab;
+ INT32 * Cbgtab = upsample->Cb_g_tab;
+ SHIFT_TEMPS
+
+ inptr00 = input_buf[0][in_row_group_ctr*2];
+ inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
+ inptr1 = input_buf[1][in_row_group_ctr];
+ inptr2 = input_buf[2][in_row_group_ctr];
+ outptr0 = output_buf[0];
+ outptr1 = output_buf[1];
+ /* Loop for each group of output pixels */
+ for (col = cinfo->output_width >> 1; col > 0; col--) {
+ /* Do the chroma part of the calculation */
+ cb = GETJSAMPLE(*inptr1++);
+ cr = GETJSAMPLE(*inptr2++);
+ cred = Crrtab[cr];
+ cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+ cblue = Cbbtab[cb];
+ /* Fetch 4 Y values and emit 4 pixels */
+ y = GETJSAMPLE(*inptr00++);
+ outptr0[RGB_RED] = range_limit[y + cred];
+ outptr0[RGB_GREEN] = range_limit[y + cgreen];
+ outptr0[RGB_BLUE] = range_limit[y + cblue];
+ outptr0 += RGB_PIXELSIZE;
+ y = GETJSAMPLE(*inptr00++);
+ outptr0[RGB_RED] = range_limit[y + cred];
+ outptr0[RGB_GREEN] = range_limit[y + cgreen];
+ outptr0[RGB_BLUE] = range_limit[y + cblue];
+ outptr0 += RGB_PIXELSIZE;
+ y = GETJSAMPLE(*inptr01++);
+ outptr1[RGB_RED] = range_limit[y + cred];
+ outptr1[RGB_GREEN] = range_limit[y + cgreen];
+ outptr1[RGB_BLUE] = range_limit[y + cblue];
+ outptr1 += RGB_PIXELSIZE;
+ y = GETJSAMPLE(*inptr01++);
+ outptr1[RGB_RED] = range_limit[y + cred];
+ outptr1[RGB_GREEN] = range_limit[y + cgreen];
+ outptr1[RGB_BLUE] = range_limit[y + cblue];
+ outptr1 += RGB_PIXELSIZE;
+ }
+ /* If image width is odd, do the last output column separately */
+ if (cinfo->output_width & 1) {
+ cb = GETJSAMPLE(*inptr1);
+ cr = GETJSAMPLE(*inptr2);
+ cred = Crrtab[cr];
+ cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
+ cblue = Cbbtab[cb];
+ y = GETJSAMPLE(*inptr00);
+ outptr0[RGB_RED] = range_limit[y + cred];
+ outptr0[RGB_GREEN] = range_limit[y + cgreen];
+ outptr0[RGB_BLUE] = range_limit[y + cblue];
+ y = GETJSAMPLE(*inptr01);
+ outptr1[RGB_RED] = range_limit[y + cred];
+ outptr1[RGB_GREEN] = range_limit[y + cgreen];
+ outptr1[RGB_BLUE] = range_limit[y + cblue];
+ }
+}
+
+
+/*
+ * Module initialization routine for merged upsampling/color conversion.
+ *
+ * NB: this is called under the conditions determined by use_merged_upsample()
+ * in jdmaster.c. That routine MUST correspond to the actual capabilities
+ * of this module; no safety checks are made here.
+ */
+
+GLOBAL(void)
+jinit_merged_upsampler (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample;
+
+ upsample = (my_upsample_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_upsampler));
+ cinfo->upsample = (struct jpeg_upsampler *) upsample;
+ upsample->pub.start_pass = start_pass_merged_upsample;
+ upsample->pub.need_context_rows = FALSE;
+
+ upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
+
+ if (cinfo->max_v_samp_factor == 2) {
+ upsample->pub.upsample = merged_2v_upsample;
+ upsample->upmethod = h2v2_merged_upsample;
+ /* Allocate a spare row buffer */
+ upsample->spare_row = (JSAMPROW)
+ (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
+ } else {
+ upsample->pub.upsample = merged_1v_upsample;
+ upsample->upmethod = h2v1_merged_upsample;
+ /* No spare row needed */
+ upsample->spare_row = NULL;
+ }
+
+ build_ycc_rgb_table(cinfo);
+}
+
+#endif /* UPSAMPLE_MERGING_SUPPORTED */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c b/core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c
index 3c19d0ab24..128e412662 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdphuff.c
@@ -1,671 +1,671 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * 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 */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * 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 */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c b/core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c
index 855be5c8aa..13b1b1bb0c 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdpostct.c
@@ -1,293 +1,293 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdpostct.c
- *
- * Copyright (C) 1994-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 contains the decompression postprocessing controller.
- * This controller manages the upsampling, color conversion, and color
- * quantization/reduction steps; specifically, it controls the buffering
- * between upsample/color conversion and color quantization/reduction.
- *
- * If no color quantization/reduction is required, then this module has no
- * work to do, and it just hands off to the upsample/color conversion code.
- * An integrated upsample/convert/quantize process would replace this module
- * entirely.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Private buffer controller object */
-
-typedef struct {
- struct jpeg_d_post_controller pub; /* public fields */
-
- /* Color quantization source buffer: this holds output data from
- * the upsample/color conversion step to be passed to the quantizer.
- * For two-pass color quantization, we need a full-image buffer;
- * for one-pass operation, a strip buffer is sufficient.
- */
- jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
- JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
- JDIMENSION strip_height; /* buffer size in rows */
- /* for two-pass mode only: */
- JDIMENSION starting_row; /* row # of first row in current strip */
- JDIMENSION next_row; /* index of next row to fill/empty in strip */
-} my_post_controller;
-
-typedef my_post_controller * my_post_ptr;
-
-
-/* Forward declarations */
-METHODDEF(void) post_process_1pass
- JPP((j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail));
-#ifdef QUANT_2PASS_SUPPORTED
-METHODDEF(void) post_process_prepass
- JPP((j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail));
-METHODDEF(void) post_process_2pass
- JPP((j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail));
-#endif
-
-
-/*
- * Initialize for a processing pass.
- */
-
-METHODDEF(void)
-start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
-{
- my_post_ptr post = (my_post_ptr) cinfo->post;
-
- switch (pass_mode) {
- case JBUF_PASS_THRU:
- if (cinfo->quantize_colors) {
- /* Single-pass processing with color quantization. */
- post->pub.post_process_data = post_process_1pass;
- /* We could be doing buffered-image output before starting a 2-pass
- * color quantization; in that case, jinit_d_post_controller did not
- * allocate a strip buffer. Use the virtual-array buffer as workspace.
- */
- if (post->buffer == NULL) {
- post->buffer = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr) cinfo, post->whole_image,
- (JDIMENSION) 0, post->strip_height, TRUE);
- }
- } else {
- /* For single-pass processing without color quantization,
- * I have no work to do; just call the upsampler directly.
- */
- post->pub.post_process_data = cinfo->upsample->upsample;
- }
- break;
-#ifdef QUANT_2PASS_SUPPORTED
- case JBUF_SAVE_AND_PASS:
- /* First pass of 2-pass quantization */
- if (post->whole_image == NULL)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- post->pub.post_process_data = post_process_prepass;
- break;
- case JBUF_CRANK_DEST:
- /* Second pass of 2-pass quantization */
- if (post->whole_image == NULL)
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- post->pub.post_process_data = post_process_2pass;
- break;
-#endif /* QUANT_2PASS_SUPPORTED */
- default:
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
- break;
- }
- post->starting_row = post->next_row = 0;
-}
-
-
-/*
- * Process some data in the one-pass (strip buffer) case.
- * This is used for color precision reduction as well as one-pass quantization.
- */
-
-METHODDEF(void)
-post_process_1pass (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-{
- my_post_ptr post = (my_post_ptr) cinfo->post;
- JDIMENSION num_rows, max_rows;
-
- /* Fill the buffer, but not more than what we can dump out in one go. */
- /* Note we rely on the upsampler to detect bottom of image. */
- max_rows = out_rows_avail - *out_row_ctr;
- if (max_rows > post->strip_height)
- max_rows = post->strip_height;
- num_rows = 0;
- (*cinfo->upsample->upsample) (cinfo,
- input_buf, in_row_group_ctr, in_row_groups_avail,
- post->buffer, &num_rows, max_rows);
- /* Quantize and emit data. */
- (*cinfo->cquantize->color_quantize) (cinfo,
- post->buffer, output_buf + *out_row_ctr, (int) num_rows);
- *out_row_ctr += num_rows;
-}
-
-
-#ifdef QUANT_2PASS_SUPPORTED
-
-/*
- * Process some data in the first pass of 2-pass quantization.
- */
-
-METHODDEF(void)
-post_process_prepass (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-{
- my_post_ptr post = (my_post_ptr) cinfo->post;
- JDIMENSION old_next_row, num_rows;
-
- /* Reposition virtual buffer if at start of strip. */
- if (post->next_row == 0) {
- post->buffer = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr) cinfo, post->whole_image,
- post->starting_row, post->strip_height, TRUE);
- }
-
- /* Upsample some data (up to a strip height's worth). */
- old_next_row = post->next_row;
- (*cinfo->upsample->upsample) (cinfo,
- input_buf, in_row_group_ctr, in_row_groups_avail,
- post->buffer, &post->next_row, post->strip_height);
-
- /* Allow quantizer to scan new data. No data is emitted, */
- /* but we advance out_row_ctr so outer loop can tell when we're done. */
- if (post->next_row > old_next_row) {
- num_rows = post->next_row - old_next_row;
- (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
- (JSAMPARRAY) NULL, (int) num_rows);
- *out_row_ctr += num_rows;
- }
-
- /* Advance if we filled the strip. */
- if (post->next_row >= post->strip_height) {
- post->starting_row += post->strip_height;
- post->next_row = 0;
- }
-}
-
-
-/*
- * Process some data in the second pass of 2-pass quantization.
- */
-
-METHODDEF(void)
-post_process_2pass (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-{
- my_post_ptr post = (my_post_ptr) cinfo->post;
- JDIMENSION num_rows, max_rows;
-
- /* Reposition virtual buffer if at start of strip. */
- if (post->next_row == 0) {
- post->buffer = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr) cinfo, post->whole_image,
- post->starting_row, post->strip_height, FALSE);
- }
-
- /* Determine number of rows to emit. */
- num_rows = post->strip_height - post->next_row; /* available in strip */
- max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
- if (num_rows > max_rows)
- num_rows = max_rows;
- /* We have to check bottom of image here, can't depend on upsampler. */
- max_rows = cinfo->output_height - post->starting_row;
- if (num_rows > max_rows)
- num_rows = max_rows;
-
- /* Quantize and emit data. */
- (*cinfo->cquantize->color_quantize) (cinfo,
- post->buffer + post->next_row, output_buf + *out_row_ctr,
- (int) num_rows);
- *out_row_ctr += num_rows;
-
- /* Advance if we filled the strip. */
- post->next_row += num_rows;
- if (post->next_row >= post->strip_height) {
- post->starting_row += post->strip_height;
- post->next_row = 0;
- }
-}
-
-#endif /* QUANT_2PASS_SUPPORTED */
-
-
-/*
- * Initialize postprocessing controller.
- */
-
-GLOBAL(void)
-jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
-{
- my_post_ptr post;
-
- post = (my_post_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_post_controller));
- cinfo->post = (struct jpeg_d_post_controller *) post;
- post->pub.start_pass = start_pass_dpost;
- post->whole_image = NULL; /* flag for no virtual arrays */
- post->buffer = NULL; /* flag for no strip buffer */
-
- /* Create the quantization buffer, if needed */
- if (cinfo->quantize_colors) {
- /* The buffer strip height is max_v_samp_factor, which is typically
- * an efficient number of rows for upsampling to return.
- * (In the presence of output rescaling, we might want to be smarter?)
- */
- post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
- if (need_full_buffer) {
- /* Two-pass color quantization: need full-image storage. */
- /* We round up the number of rows to a multiple of the strip height. */
-#ifdef QUANT_2PASS_SUPPORTED
- post->whole_image = (*cinfo->mem->request_virt_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
- cinfo->output_width * cinfo->out_color_components,
- (JDIMENSION) jround_up((long) cinfo->output_height,
- (long) post->strip_height),
- post->strip_height);
-#else
- ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
-#endif /* QUANT_2PASS_SUPPORTED */
- } else {
- /* One-pass color quantization: just make a strip buffer. */
- post->buffer = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- cinfo->output_width * cinfo->out_color_components,
- post->strip_height);
- }
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdpostct.c
+ *
+ * Copyright (C) 1994-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 contains the decompression postprocessing controller.
+ * This controller manages the upsampling, color conversion, and color
+ * quantization/reduction steps; specifically, it controls the buffering
+ * between upsample/color conversion and color quantization/reduction.
+ *
+ * If no color quantization/reduction is required, then this module has no
+ * work to do, and it just hands off to the upsample/color conversion code.
+ * An integrated upsample/convert/quantize process would replace this module
+ * entirely.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Private buffer controller object */
+
+typedef struct {
+ struct jpeg_d_post_controller pub; /* public fields */
+
+ /* Color quantization source buffer: this holds output data from
+ * the upsample/color conversion step to be passed to the quantizer.
+ * For two-pass color quantization, we need a full-image buffer;
+ * for one-pass operation, a strip buffer is sufficient.
+ */
+ jvirt_sarray_ptr whole_image; /* virtual array, or NULL if one-pass */
+ JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
+ JDIMENSION strip_height; /* buffer size in rows */
+ /* for two-pass mode only: */
+ JDIMENSION starting_row; /* row # of first row in current strip */
+ JDIMENSION next_row; /* index of next row to fill/empty in strip */
+} my_post_controller;
+
+typedef my_post_controller * my_post_ptr;
+
+
+/* Forward declarations */
+METHODDEF(void) post_process_1pass
+ JPP((j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+#ifdef QUANT_2PASS_SUPPORTED
+METHODDEF(void) post_process_prepass
+ JPP((j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+METHODDEF(void) post_process_2pass
+ JPP((j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+#endif
+
+
+/*
+ * Initialize for a processing pass.
+ */
+
+METHODDEF(void)
+start_pass_dpost (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
+{
+ my_post_ptr post = (my_post_ptr) cinfo->post;
+
+ switch (pass_mode) {
+ case JBUF_PASS_THRU:
+ if (cinfo->quantize_colors) {
+ /* Single-pass processing with color quantization. */
+ post->pub.post_process_data = post_process_1pass;
+ /* We could be doing buffered-image output before starting a 2-pass
+ * color quantization; in that case, jinit_d_post_controller did not
+ * allocate a strip buffer. Use the virtual-array buffer as workspace.
+ */
+ if (post->buffer == NULL) {
+ post->buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, post->whole_image,
+ (JDIMENSION) 0, post->strip_height, TRUE);
+ }
+ } else {
+ /* For single-pass processing without color quantization,
+ * I have no work to do; just call the upsampler directly.
+ */
+ post->pub.post_process_data = cinfo->upsample->upsample;
+ }
+ break;
+#ifdef QUANT_2PASS_SUPPORTED
+ case JBUF_SAVE_AND_PASS:
+ /* First pass of 2-pass quantization */
+ if (post->whole_image == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ post->pub.post_process_data = post_process_prepass;
+ break;
+ case JBUF_CRANK_DEST:
+ /* Second pass of 2-pass quantization */
+ if (post->whole_image == NULL)
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ post->pub.post_process_data = post_process_2pass;
+ break;
+#endif /* QUANT_2PASS_SUPPORTED */
+ default:
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+ break;
+ }
+ post->starting_row = post->next_row = 0;
+}
+
+
+/*
+ * Process some data in the one-pass (strip buffer) case.
+ * This is used for color precision reduction as well as one-pass quantization.
+ */
+
+METHODDEF(void)
+post_process_1pass (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_post_ptr post = (my_post_ptr) cinfo->post;
+ JDIMENSION num_rows, max_rows;
+
+ /* Fill the buffer, but not more than what we can dump out in one go. */
+ /* Note we rely on the upsampler to detect bottom of image. */
+ max_rows = out_rows_avail - *out_row_ctr;
+ if (max_rows > post->strip_height)
+ max_rows = post->strip_height;
+ num_rows = 0;
+ (*cinfo->upsample->upsample) (cinfo,
+ input_buf, in_row_group_ctr, in_row_groups_avail,
+ post->buffer, &num_rows, max_rows);
+ /* Quantize and emit data. */
+ (*cinfo->cquantize->color_quantize) (cinfo,
+ post->buffer, output_buf + *out_row_ctr, (int) num_rows);
+ *out_row_ctr += num_rows;
+}
+
+
+#ifdef QUANT_2PASS_SUPPORTED
+
+/*
+ * Process some data in the first pass of 2-pass quantization.
+ */
+
+METHODDEF(void)
+post_process_prepass (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_post_ptr post = (my_post_ptr) cinfo->post;
+ JDIMENSION old_next_row, num_rows;
+
+ /* Reposition virtual buffer if at start of strip. */
+ if (post->next_row == 0) {
+ post->buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, post->whole_image,
+ post->starting_row, post->strip_height, TRUE);
+ }
+
+ /* Upsample some data (up to a strip height's worth). */
+ old_next_row = post->next_row;
+ (*cinfo->upsample->upsample) (cinfo,
+ input_buf, in_row_group_ctr, in_row_groups_avail,
+ post->buffer, &post->next_row, post->strip_height);
+
+ /* Allow quantizer to scan new data. No data is emitted, */
+ /* but we advance out_row_ctr so outer loop can tell when we're done. */
+ if (post->next_row > old_next_row) {
+ num_rows = post->next_row - old_next_row;
+ (*cinfo->cquantize->color_quantize) (cinfo, post->buffer + old_next_row,
+ (JSAMPARRAY) NULL, (int) num_rows);
+ *out_row_ctr += num_rows;
+ }
+
+ /* Advance if we filled the strip. */
+ if (post->next_row >= post->strip_height) {
+ post->starting_row += post->strip_height;
+ post->next_row = 0;
+ }
+}
+
+
+/*
+ * Process some data in the second pass of 2-pass quantization.
+ */
+
+METHODDEF(void)
+post_process_2pass (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_post_ptr post = (my_post_ptr) cinfo->post;
+ JDIMENSION num_rows, max_rows;
+
+ /* Reposition virtual buffer if at start of strip. */
+ if (post->next_row == 0) {
+ post->buffer = (*cinfo->mem->access_virt_sarray)
+ ((j_common_ptr) cinfo, post->whole_image,
+ post->starting_row, post->strip_height, FALSE);
+ }
+
+ /* Determine number of rows to emit. */
+ num_rows = post->strip_height - post->next_row; /* available in strip */
+ max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
+ if (num_rows > max_rows)
+ num_rows = max_rows;
+ /* We have to check bottom of image here, can't depend on upsampler. */
+ max_rows = cinfo->output_height - post->starting_row;
+ if (num_rows > max_rows)
+ num_rows = max_rows;
+
+ /* Quantize and emit data. */
+ (*cinfo->cquantize->color_quantize) (cinfo,
+ post->buffer + post->next_row, output_buf + *out_row_ctr,
+ (int) num_rows);
+ *out_row_ctr += num_rows;
+
+ /* Advance if we filled the strip. */
+ post->next_row += num_rows;
+ if (post->next_row >= post->strip_height) {
+ post->starting_row += post->strip_height;
+ post->next_row = 0;
+ }
+}
+
+#endif /* QUANT_2PASS_SUPPORTED */
+
+
+/*
+ * Initialize postprocessing controller.
+ */
+
+GLOBAL(void)
+jinit_d_post_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
+{
+ my_post_ptr post;
+
+ post = (my_post_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_post_controller));
+ cinfo->post = (struct jpeg_d_post_controller *) post;
+ post->pub.start_pass = start_pass_dpost;
+ post->whole_image = NULL; /* flag for no virtual arrays */
+ post->buffer = NULL; /* flag for no strip buffer */
+
+ /* Create the quantization buffer, if needed */
+ if (cinfo->quantize_colors) {
+ /* The buffer strip height is max_v_samp_factor, which is typically
+ * an efficient number of rows for upsampling to return.
+ * (In the presence of output rescaling, we might want to be smarter?)
+ */
+ post->strip_height = (JDIMENSION) cinfo->max_v_samp_factor;
+ if (need_full_buffer) {
+ /* Two-pass color quantization: need full-image storage. */
+ /* We round up the number of rows to a multiple of the strip height. */
+#ifdef QUANT_2PASS_SUPPORTED
+ post->whole_image = (*cinfo->mem->request_virt_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
+ cinfo->output_width * cinfo->out_color_components,
+ (JDIMENSION) jround_up((long) cinfo->output_height,
+ (long) post->strip_height),
+ post->strip_height);
+#else
+ ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
+#endif /* QUANT_2PASS_SUPPORTED */
+ } else {
+ /* One-pass color quantization: just make a strip buffer. */
+ post->buffer = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ cinfo->output_width * cinfo->out_color_components,
+ post->strip_height);
+ }
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdsample.c b/core/src/fxcodec/libjpeg/fpdfapi_jdsample.c
index a2eae954b5..da9c38ce38 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdsample.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdsample.c
@@ -1,481 +1,481 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdsample.c
- *
- * Copyright (C) 1991-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 contains upsampling routines.
- *
- * Upsampling input data is counted in "row groups". A row group
- * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
- * sample rows of each component. Upsampling will normally produce
- * max_v_samp_factor pixel rows from each row group (but this could vary
- * if the upsampler is applying a scale factor of its own).
- *
- * An excellent reference for image resampling is
- * Digital Image Warping, George Wolberg, 1990.
- * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Pointer to routine to upsample a single component */
-typedef JMETHOD(void, upsample1_ptr,
- (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
-
-/* Private subobject */
-
-typedef struct {
- struct jpeg_upsampler pub; /* public fields */
-
- /* Color conversion buffer. When using separate upsampling and color
- * conversion steps, this buffer holds one upsampled row group until it
- * has been color converted and output.
- * Note: we do not allocate any storage for component(s) which are full-size,
- * ie do not need rescaling. The corresponding entry of color_buf[] is
- * simply set to point to the input data array, thereby avoiding copying.
- */
- JSAMPARRAY color_buf[MAX_COMPONENTS];
-
- /* Per-component upsampling method pointers */
- upsample1_ptr methods[MAX_COMPONENTS];
-
- int next_row_out; /* counts rows emitted from color_buf */
- JDIMENSION rows_to_go; /* counts rows remaining in image */
-
- /* Height of an input row group for each component. */
- int rowgroup_height[MAX_COMPONENTS];
-
- /* These arrays save pixel expansion factors so that int_expand need not
- * recompute them each time. They are unused for other upsampling methods.
- */
- UINT8 h_expand[MAX_COMPONENTS];
- UINT8 v_expand[MAX_COMPONENTS];
-} my_upsampler;
-
-typedef my_upsampler * my_upsample_ptr;
-
-
-/*
- * Initialize for an upsampling pass.
- */
-
-METHODDEF(void)
-start_pass_upsample (j_decompress_ptr cinfo)
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
-
- /* Mark the conversion buffer empty */
- upsample->next_row_out = cinfo->max_v_samp_factor;
- /* Initialize total-height counter for detecting bottom of image */
- upsample->rows_to_go = cinfo->output_height;
-}
-
-
-/*
- * Control routine to do upsampling (and color conversion).
- *
- * In this version we upsample each component independently.
- * We upsample one row group into the conversion buffer, then apply
- * color conversion a row at a time.
- */
-
-METHODDEF(void)
-sep_upsample (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail)
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
- int ci;
- jpeg_component_info * compptr;
- JDIMENSION num_rows;
-
- /* Fill the conversion buffer, if it's empty */
- if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Invoke per-component upsample method. Notice we pass a POINTER
- * to color_buf[ci], so that fullsize_upsample can change it.
- */
- (*upsample->methods[ci]) (cinfo, compptr,
- input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
- upsample->color_buf + ci);
- }
- upsample->next_row_out = 0;
- }
-
- /* Color-convert and emit rows */
-
- /* How many we have in the buffer: */
- num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
- /* Not more than the distance to the end of the image. Need this test
- * in case the image height is not a multiple of max_v_samp_factor:
- */
- if (num_rows > upsample->rows_to_go)
- num_rows = upsample->rows_to_go;
- /* And not more than what the client can accept: */
- out_rows_avail -= *out_row_ctr;
- if (num_rows > out_rows_avail)
- num_rows = out_rows_avail;
-
- (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
- (JDIMENSION) upsample->next_row_out,
- output_buf + *out_row_ctr,
- (int) num_rows);
-
- /* Adjust counts */
- *out_row_ctr += num_rows;
- upsample->rows_to_go -= num_rows;
- upsample->next_row_out += num_rows;
- /* When the buffer is emptied, declare this input row group consumed */
- if (upsample->next_row_out >= cinfo->max_v_samp_factor)
- (*in_row_group_ctr)++;
-}
-
-
-/*
- * These are the routines invoked by sep_upsample to upsample pixel values
- * of a single component. One row group is processed per call.
- */
-
-
-/*
- * For full-size components, we just make color_buf[ci] point at the
- * input buffer, and thus avoid copying any data. Note that this is
- * safe only because sep_upsample doesn't declare the input row group
- * "consumed" until we are done color converting and emitting it.
- */
-
-METHODDEF(void)
-fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
-{
- *output_data_ptr = input_data;
-}
-
-
-/*
- * This is a no-op version used for "uninteresting" components.
- * These components will not be referenced by color conversion.
- */
-
-METHODDEF(void)
-noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
-{
- *output_data_ptr = NULL; /* safety check */
-}
-
-
-/*
- * This version handles any integral sampling ratios.
- * This is not used for typical JPEG files, so it need not be fast.
- * Nor, for that matter, is it particularly accurate: the algorithm is
- * simple replication of the input pixel onto the corresponding output
- * pixels. The hi-falutin sampling literature refers to this as a
- * "box filter". A box filter tends to introduce visible artifacts,
- * so if you are actually going to use 3:1 or 4:1 sampling ratios
- * you would be well advised to improve this code.
- */
-
-METHODDEF(void)
-int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
-{
- my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
- JSAMPARRAY output_data = *output_data_ptr;
- register JSAMPROW inptr, outptr;
- register JSAMPLE invalue;
- register int h;
- JSAMPROW outend;
- int h_expand, v_expand;
- int inrow, outrow;
-
- h_expand = upsample->h_expand[compptr->component_index];
- v_expand = upsample->v_expand[compptr->component_index];
-
- inrow = outrow = 0;
- while (outrow < cinfo->max_v_samp_factor) {
- /* Generate one output row with proper horizontal expansion */
- inptr = input_data[inrow];
- outptr = output_data[outrow];
- outend = outptr + cinfo->output_width;
- while (outptr < outend) {
- invalue = *inptr++; /* don't need GETJSAMPLE() here */
- for (h = h_expand; h > 0; h--) {
- *outptr++ = invalue;
- }
- }
- /* Generate any additional output rows by duplicating the first one */
- if (v_expand > 1) {
- jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
- v_expand-1, cinfo->output_width);
- }
- inrow++;
- outrow += v_expand;
- }
-}
-
-
-/*
- * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
- * It's still a box filter.
- */
-
-METHODDEF(void)
-h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
-{
- JSAMPARRAY output_data = *output_data_ptr;
- register JSAMPROW inptr, outptr;
- register JSAMPLE invalue;
- JSAMPROW outend;
- int inrow;
-
- for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
- inptr = input_data[inrow];
- outptr = output_data[inrow];
- outend = outptr + cinfo->output_width;
- while (outptr < outend) {
- invalue = *inptr++; /* don't need GETJSAMPLE() here */
- *outptr++ = invalue;
- *outptr++ = invalue;
- }
- }
-}
-
-
-/*
- * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
- * It's still a box filter.
- */
-
-METHODDEF(void)
-h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
-{
- JSAMPARRAY output_data = *output_data_ptr;
- register JSAMPROW inptr, outptr;
- register JSAMPLE invalue;
- JSAMPROW outend;
- int inrow, outrow;
-
- inrow = outrow = 0;
- while (outrow < cinfo->max_v_samp_factor) {
- inptr = input_data[inrow];
- outptr = output_data[outrow];
- outend = outptr + cinfo->output_width;
- while (outptr < outend) {
- invalue = *inptr++; /* don't need GETJSAMPLE() here */
- *outptr++ = invalue;
- *outptr++ = invalue;
- }
- jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
- 1, cinfo->output_width);
- inrow++;
- outrow += 2;
- }
-}
-
-
-/*
- * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
- *
- * The upsampling algorithm is linear interpolation between pixel centers,
- * also known as a "triangle filter". This is a good compromise between
- * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
- * of the way between input pixel centers.
- *
- * A note about the "bias" calculations: when rounding fractional values to
- * integer, we do not want to always round 0.5 up to the next integer.
- * If we did that, we'd introduce a noticeable bias towards larger values.
- * Instead, this code is arranged so that 0.5 will be rounded up or down at
- * alternate pixel locations (a simple ordered dither pattern).
- */
-
-METHODDEF(void)
-h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
-{
- JSAMPARRAY output_data = *output_data_ptr;
- register JSAMPROW inptr, outptr;
- register int invalue;
- register JDIMENSION colctr;
- int inrow;
-
- for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
- inptr = input_data[inrow];
- outptr = output_data[inrow];
- /* Special case for first column */
- invalue = GETJSAMPLE(*inptr++);
- *outptr++ = (JSAMPLE) invalue;
- *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
-
- for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
- /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
- invalue = GETJSAMPLE(*inptr++) * 3;
- *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
- *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
- }
-
- /* Special case for last column */
- invalue = GETJSAMPLE(*inptr);
- *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
- *outptr++ = (JSAMPLE) invalue;
- }
-}
-
-
-/*
- * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
- * Again a triangle filter; see comments for h2v1 case, above.
- *
- * It is OK for us to reference the adjacent input rows because we demanded
- * context from the main buffer controller (see initialization code).
- */
-
-METHODDEF(void)
-h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
-{
- JSAMPARRAY output_data = *output_data_ptr;
- register JSAMPROW inptr0, inptr1, outptr;
-#if BITS_IN_JSAMPLE == 8
- register int thiscolsum, lastcolsum, nextcolsum;
-#else
- register INT32 thiscolsum, lastcolsum, nextcolsum;
-#endif
- register JDIMENSION colctr;
- int inrow, outrow, v;
-
- inrow = outrow = 0;
- while (outrow < cinfo->max_v_samp_factor) {
- for (v = 0; v < 2; v++) {
- /* inptr0 points to nearest input row, inptr1 points to next nearest */
- inptr0 = input_data[inrow];
- if (v == 0) /* next nearest is row above */
- inptr1 = input_data[inrow-1];
- else /* next nearest is row below */
- inptr1 = input_data[inrow+1];
- outptr = output_data[outrow++];
-
- /* Special case for first column */
- thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
- nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
- *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
- *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
- lastcolsum = thiscolsum; thiscolsum = nextcolsum;
-
- for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
- /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
- /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
- nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
- *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
- *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
- lastcolsum = thiscolsum; thiscolsum = nextcolsum;
- }
-
- /* Special case for last column */
- *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
- *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
- }
- inrow++;
- }
-}
-
-
-/*
- * Module initialization routine for upsampling.
- */
-
-GLOBAL(void)
-jinit_upsampler (j_decompress_ptr cinfo)
-{
- my_upsample_ptr upsample;
- int ci;
- jpeg_component_info * compptr;
- boolean need_buffer, do_fancy;
- int h_in_group, v_in_group, h_out_group, v_out_group;
-
- upsample = (my_upsample_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- SIZEOF(my_upsampler));
- cinfo->upsample = (struct jpeg_upsampler *) upsample;
- upsample->pub.start_pass = start_pass_upsample;
- upsample->pub.upsample = sep_upsample;
- upsample->pub.need_context_rows = FALSE; /* until we find out differently */
-
- if (cinfo->CCIR601_sampling) /* this isn't supported */
- ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
-
- /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
- * so don't ask for it.
- */
- do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
-
- /* Verify we can handle the sampling factors, select per-component methods,
- * and create storage as needed.
- */
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
- ci++, compptr++) {
- /* Compute size of an "input group" after IDCT scaling. This many samples
- * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
- */
- h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
- cinfo->min_DCT_scaled_size;
- v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
- cinfo->min_DCT_scaled_size;
- h_out_group = cinfo->max_h_samp_factor;
- v_out_group = cinfo->max_v_samp_factor;
- upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
- need_buffer = TRUE;
- if (! compptr->component_needed) {
- /* Don't bother to upsample an uninteresting component. */
- upsample->methods[ci] = noop_upsample;
- need_buffer = FALSE;
- } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
- /* Fullsize components can be processed without any work. */
- upsample->methods[ci] = fullsize_upsample;
- need_buffer = FALSE;
- } else if (h_in_group * 2 == h_out_group &&
- v_in_group == v_out_group) {
- /* Special cases for 2h1v upsampling */
- if (do_fancy && compptr->downsampled_width > 2)
- upsample->methods[ci] = h2v1_fancy_upsample;
- else
- upsample->methods[ci] = h2v1_upsample;
- } else if (h_in_group * 2 == h_out_group &&
- v_in_group * 2 == v_out_group) {
- /* Special cases for 2h2v upsampling */
- if (do_fancy && compptr->downsampled_width > 2) {
- upsample->methods[ci] = h2v2_fancy_upsample;
- upsample->pub.need_context_rows = TRUE;
- } else
- upsample->methods[ci] = h2v2_upsample;
- } else if ((h_out_group % h_in_group) == 0 &&
- (v_out_group % v_in_group) == 0) {
- /* Generic integral-factors upsampling method */
- upsample->methods[ci] = int_upsample;
- upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
- upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
- } else
- ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
- if (need_buffer) {
- upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (JDIMENSION) jround_up((long) cinfo->output_width,
- (long) cinfo->max_h_samp_factor),
- (JDIMENSION) cinfo->max_v_samp_factor);
- }
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdsample.c
+ *
+ * Copyright (C) 1991-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 contains upsampling routines.
+ *
+ * Upsampling input data is counted in "row groups". A row group
+ * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
+ * sample rows of each component. Upsampling will normally produce
+ * max_v_samp_factor pixel rows from each row group (but this could vary
+ * if the upsampler is applying a scale factor of its own).
+ *
+ * An excellent reference for image resampling is
+ * Digital Image Warping, George Wolberg, 1990.
+ * Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Pointer to routine to upsample a single component */
+typedef JMETHOD(void, upsample1_ptr,
+ (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
+
+/* Private subobject */
+
+typedef struct {
+ struct jpeg_upsampler pub; /* public fields */
+
+ /* Color conversion buffer. When using separate upsampling and color
+ * conversion steps, this buffer holds one upsampled row group until it
+ * has been color converted and output.
+ * Note: we do not allocate any storage for component(s) which are full-size,
+ * ie do not need rescaling. The corresponding entry of color_buf[] is
+ * simply set to point to the input data array, thereby avoiding copying.
+ */
+ JSAMPARRAY color_buf[MAX_COMPONENTS];
+
+ /* Per-component upsampling method pointers */
+ upsample1_ptr methods[MAX_COMPONENTS];
+
+ int next_row_out; /* counts rows emitted from color_buf */
+ JDIMENSION rows_to_go; /* counts rows remaining in image */
+
+ /* Height of an input row group for each component. */
+ int rowgroup_height[MAX_COMPONENTS];
+
+ /* These arrays save pixel expansion factors so that int_expand need not
+ * recompute them each time. They are unused for other upsampling methods.
+ */
+ UINT8 h_expand[MAX_COMPONENTS];
+ UINT8 v_expand[MAX_COMPONENTS];
+} my_upsampler;
+
+typedef my_upsampler * my_upsample_ptr;
+
+
+/*
+ * Initialize for an upsampling pass.
+ */
+
+METHODDEF(void)
+start_pass_upsample (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+
+ /* Mark the conversion buffer empty */
+ upsample->next_row_out = cinfo->max_v_samp_factor;
+ /* Initialize total-height counter for detecting bottom of image */
+ upsample->rows_to_go = cinfo->output_height;
+}
+
+
+/*
+ * Control routine to do upsampling (and color conversion).
+ *
+ * In this version we upsample each component independently.
+ * We upsample one row group into the conversion buffer, then apply
+ * color conversion a row at a time.
+ */
+
+METHODDEF(void)
+sep_upsample (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ int ci;
+ jpeg_component_info * compptr;
+ JDIMENSION num_rows;
+
+ /* Fill the conversion buffer, if it's empty */
+ if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Invoke per-component upsample method. Notice we pass a POINTER
+ * to color_buf[ci], so that fullsize_upsample can change it.
+ */
+ (*upsample->methods[ci]) (cinfo, compptr,
+ input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
+ upsample->color_buf + ci);
+ }
+ upsample->next_row_out = 0;
+ }
+
+ /* Color-convert and emit rows */
+
+ /* How many we have in the buffer: */
+ num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
+ /* Not more than the distance to the end of the image. Need this test
+ * in case the image height is not a multiple of max_v_samp_factor:
+ */
+ if (num_rows > upsample->rows_to_go)
+ num_rows = upsample->rows_to_go;
+ /* And not more than what the client can accept: */
+ out_rows_avail -= *out_row_ctr;
+ if (num_rows > out_rows_avail)
+ num_rows = out_rows_avail;
+
+ (*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
+ (JDIMENSION) upsample->next_row_out,
+ output_buf + *out_row_ctr,
+ (int) num_rows);
+
+ /* Adjust counts */
+ *out_row_ctr += num_rows;
+ upsample->rows_to_go -= num_rows;
+ upsample->next_row_out += num_rows;
+ /* When the buffer is emptied, declare this input row group consumed */
+ if (upsample->next_row_out >= cinfo->max_v_samp_factor)
+ (*in_row_group_ctr)++;
+}
+
+
+/*
+ * These are the routines invoked by sep_upsample to upsample pixel values
+ * of a single component. One row group is processed per call.
+ */
+
+
+/*
+ * For full-size components, we just make color_buf[ci] point at the
+ * input buffer, and thus avoid copying any data. Note that this is
+ * safe only because sep_upsample doesn't declare the input row group
+ * "consumed" until we are done color converting and emitting it.
+ */
+
+METHODDEF(void)
+fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ *output_data_ptr = input_data;
+}
+
+
+/*
+ * This is a no-op version used for "uninteresting" components.
+ * These components will not be referenced by color conversion.
+ */
+
+METHODDEF(void)
+noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ *output_data_ptr = NULL; /* safety check */
+}
+
+
+/*
+ * This version handles any integral sampling ratios.
+ * This is not used for typical JPEG files, so it need not be fast.
+ * Nor, for that matter, is it particularly accurate: the algorithm is
+ * simple replication of the input pixel onto the corresponding output
+ * pixels. The hi-falutin sampling literature refers to this as a
+ * "box filter". A box filter tends to introduce visible artifacts,
+ * so if you are actually going to use 3:1 or 4:1 sampling ratios
+ * you would be well advised to improve this code.
+ */
+
+METHODDEF(void)
+int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr, outptr;
+ register JSAMPLE invalue;
+ register int h;
+ JSAMPROW outend;
+ int h_expand, v_expand;
+ int inrow, outrow;
+
+ h_expand = upsample->h_expand[compptr->component_index];
+ v_expand = upsample->v_expand[compptr->component_index];
+
+ inrow = outrow = 0;
+ while (outrow < cinfo->max_v_samp_factor) {
+ /* Generate one output row with proper horizontal expansion */
+ inptr = input_data[inrow];
+ outptr = output_data[outrow];
+ outend = outptr + cinfo->output_width;
+ while (outptr < outend) {
+ invalue = *inptr++; /* don't need GETJSAMPLE() here */
+ for (h = h_expand; h > 0; h--) {
+ *outptr++ = invalue;
+ }
+ }
+ /* Generate any additional output rows by duplicating the first one */
+ if (v_expand > 1) {
+ jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
+ v_expand-1, cinfo->output_width);
+ }
+ inrow++;
+ outrow += v_expand;
+ }
+}
+
+
+/*
+ * Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
+ * It's still a box filter.
+ */
+
+METHODDEF(void)
+h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr, outptr;
+ register JSAMPLE invalue;
+ JSAMPROW outend;
+ int inrow;
+
+ for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
+ inptr = input_data[inrow];
+ outptr = output_data[inrow];
+ outend = outptr + cinfo->output_width;
+ while (outptr < outend) {
+ invalue = *inptr++; /* don't need GETJSAMPLE() here */
+ *outptr++ = invalue;
+ *outptr++ = invalue;
+ }
+ }
+}
+
+
+/*
+ * Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
+ * It's still a box filter.
+ */
+
+METHODDEF(void)
+h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr, outptr;
+ register JSAMPLE invalue;
+ JSAMPROW outend;
+ int inrow, outrow;
+
+ inrow = outrow = 0;
+ while (outrow < cinfo->max_v_samp_factor) {
+ inptr = input_data[inrow];
+ outptr = output_data[outrow];
+ outend = outptr + cinfo->output_width;
+ while (outptr < outend) {
+ invalue = *inptr++; /* don't need GETJSAMPLE() here */
+ *outptr++ = invalue;
+ *outptr++ = invalue;
+ }
+ jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
+ 1, cinfo->output_width);
+ inrow++;
+ outrow += 2;
+ }
+}
+
+
+/*
+ * Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
+ *
+ * The upsampling algorithm is linear interpolation between pixel centers,
+ * also known as a "triangle filter". This is a good compromise between
+ * speed and visual quality. The centers of the output pixels are 1/4 and 3/4
+ * of the way between input pixel centers.
+ *
+ * A note about the "bias" calculations: when rounding fractional values to
+ * integer, we do not want to always round 0.5 up to the next integer.
+ * If we did that, we'd introduce a noticeable bias towards larger values.
+ * Instead, this code is arranged so that 0.5 will be rounded up or down at
+ * alternate pixel locations (a simple ordered dither pattern).
+ */
+
+METHODDEF(void)
+h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr, outptr;
+ register int invalue;
+ register JDIMENSION colctr;
+ int inrow;
+
+ for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
+ inptr = input_data[inrow];
+ outptr = output_data[inrow];
+ /* Special case for first column */
+ invalue = GETJSAMPLE(*inptr++);
+ *outptr++ = (JSAMPLE) invalue;
+ *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
+
+ for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
+ /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
+ invalue = GETJSAMPLE(*inptr++) * 3;
+ *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
+ *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
+ }
+
+ /* Special case for last column */
+ invalue = GETJSAMPLE(*inptr);
+ *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
+ *outptr++ = (JSAMPLE) invalue;
+ }
+}
+
+
+/*
+ * Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
+ * Again a triangle filter; see comments for h2v1 case, above.
+ *
+ * It is OK for us to reference the adjacent input rows because we demanded
+ * context from the main buffer controller (see initialization code).
+ */
+
+METHODDEF(void)
+h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
+{
+ JSAMPARRAY output_data = *output_data_ptr;
+ register JSAMPROW inptr0, inptr1, outptr;
+#if BITS_IN_JSAMPLE == 8
+ register int thiscolsum, lastcolsum, nextcolsum;
+#else
+ register INT32 thiscolsum, lastcolsum, nextcolsum;
+#endif
+ register JDIMENSION colctr;
+ int inrow, outrow, v;
+
+ inrow = outrow = 0;
+ while (outrow < cinfo->max_v_samp_factor) {
+ for (v = 0; v < 2; v++) {
+ /* inptr0 points to nearest input row, inptr1 points to next nearest */
+ inptr0 = input_data[inrow];
+ if (v == 0) /* next nearest is row above */
+ inptr1 = input_data[inrow-1];
+ else /* next nearest is row below */
+ inptr1 = input_data[inrow+1];
+ outptr = output_data[outrow++];
+
+ /* Special case for first column */
+ thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
+ nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
+ lastcolsum = thiscolsum; thiscolsum = nextcolsum;
+
+ for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
+ /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
+ /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
+ nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
+ lastcolsum = thiscolsum; thiscolsum = nextcolsum;
+ }
+
+ /* Special case for last column */
+ *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
+ *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
+ }
+ inrow++;
+ }
+}
+
+
+/*
+ * Module initialization routine for upsampling.
+ */
+
+GLOBAL(void)
+jinit_upsampler (j_decompress_ptr cinfo)
+{
+ my_upsample_ptr upsample;
+ int ci;
+ jpeg_component_info * compptr;
+ boolean need_buffer, do_fancy;
+ int h_in_group, v_in_group, h_out_group, v_out_group;
+
+ upsample = (my_upsample_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(my_upsampler));
+ cinfo->upsample = (struct jpeg_upsampler *) upsample;
+ upsample->pub.start_pass = start_pass_upsample;
+ upsample->pub.upsample = sep_upsample;
+ upsample->pub.need_context_rows = FALSE; /* until we find out differently */
+
+ if (cinfo->CCIR601_sampling) /* this isn't supported */
+ ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
+
+ /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
+ * so don't ask for it.
+ */
+ do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
+
+ /* Verify we can handle the sampling factors, select per-component methods,
+ * and create storage as needed.
+ */
+ for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
+ ci++, compptr++) {
+ /* Compute size of an "input group" after IDCT scaling. This many samples
+ * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
+ */
+ h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size;
+ v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
+ cinfo->min_DCT_scaled_size;
+ h_out_group = cinfo->max_h_samp_factor;
+ v_out_group = cinfo->max_v_samp_factor;
+ upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
+ need_buffer = TRUE;
+ if (! compptr->component_needed) {
+ /* Don't bother to upsample an uninteresting component. */
+ upsample->methods[ci] = noop_upsample;
+ need_buffer = FALSE;
+ } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
+ /* Fullsize components can be processed without any work. */
+ upsample->methods[ci] = fullsize_upsample;
+ need_buffer = FALSE;
+ } else if (h_in_group * 2 == h_out_group &&
+ v_in_group == v_out_group) {
+ /* Special cases for 2h1v upsampling */
+ if (do_fancy && compptr->downsampled_width > 2)
+ upsample->methods[ci] = h2v1_fancy_upsample;
+ else
+ upsample->methods[ci] = h2v1_upsample;
+ } else if (h_in_group * 2 == h_out_group &&
+ v_in_group * 2 == v_out_group) {
+ /* Special cases for 2h2v upsampling */
+ if (do_fancy && compptr->downsampled_width > 2) {
+ upsample->methods[ci] = h2v2_fancy_upsample;
+ upsample->pub.need_context_rows = TRUE;
+ } else
+ upsample->methods[ci] = h2v2_upsample;
+ } else if ((h_out_group % h_in_group) == 0 &&
+ (v_out_group % v_in_group) == 0) {
+ /* Generic integral-factors upsampling method */
+ upsample->methods[ci] = int_upsample;
+ upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
+ upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
+ } else
+ ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
+ if (need_buffer) {
+ upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ (JDIMENSION) jround_up((long) cinfo->output_width,
+ (long) cinfo->max_h_samp_factor),
+ (JDIMENSION) cinfo->max_v_samp_factor);
+ }
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c b/core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c
index c076a6c2bd..ae44f36043 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jdtrans.c
@@ -1,146 +1,146 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jdtrans.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 library routines for transcoding decompression,
- * that is, reading raw DCT coefficient arrays from an input JPEG file.
- * The routines in jdapimin.c will also be needed by a transcoder.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/* Forward declarations */
-LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
-
-
-/*
- * Read the coefficient arrays from a JPEG file.
- * jpeg_read_header must be completed before calling this.
- *
- * The entire image is read into a set of virtual coefficient-block arrays,
- * one per component. The return value is a pointer to the array of
- * virtual-array descriptors. These can be manipulated directly via the
- * JPEG memory manager, or handed off to jpeg_write_coefficients().
- * To release the memory occupied by the virtual arrays, call
- * jpeg_finish_decompress() when done with the data.
- *
- * An alternative usage is to simply obtain access to the coefficient arrays
- * during a buffered-image-mode decompression operation. This is allowed
- * after any jpeg_finish_output() call. The arrays can be accessed until
- * jpeg_finish_decompress() is called. (Note that any call to the library
- * may reposition the arrays, so don't rely on access_virt_barray() results
- * to stay valid across library calls.)
- *
- * Returns NULL if suspended. This case need be checked only if
- * a suspending data source is used.
- */
-
-GLOBAL(jvirt_barray_ptr *)
-jpeg_read_coefficients (j_decompress_ptr cinfo)
-{
- if (cinfo->global_state == DSTATE_READY) {
- /* First call: initialize active modules */
- transdecode_master_selection(cinfo);
- cinfo->global_state = DSTATE_RDCOEFS;
- }
- if (cinfo->global_state == DSTATE_RDCOEFS) {
- /* Absorb whole file into the coef buffer */
- for (;;) {
- int retcode;
- /* Call progress monitor hook if present */
- if (cinfo->progress != NULL)
- (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
- /* Absorb some more input */
- retcode = (*cinfo->inputctl->consume_input) (cinfo);
- if (retcode == JPEG_SUSPENDED)
- return NULL;
- if (retcode == JPEG_REACHED_EOI)
- break;
- /* Advance progress counter if appropriate */
- if (cinfo->progress != NULL &&
- (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
- if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
- /* startup underestimated number of scans; ratchet up one scan */
- cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
- }
- }
- }
- /* Set state so that jpeg_finish_decompress does the right thing */
- cinfo->global_state = DSTATE_STOPPING;
- }
- /* At this point we should be in state DSTATE_STOPPING if being used
- * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
- * to the coefficients during a full buffered-image-mode decompression.
- */
- if ((cinfo->global_state == DSTATE_STOPPING ||
- cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
- return cinfo->coef->coef_arrays;
- }
- /* Oops, improper usage */
- ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
- return NULL; /* keep compiler happy */
-}
-
-
-/*
- * Master selection of decompression modules for transcoding.
- * This substitutes for jdmaster.c's initialization of the full decompressor.
- */
-
-LOCAL(void)
-transdecode_master_selection (j_decompress_ptr cinfo)
-{
- /* This is effectively a buffered-image operation. */
- cinfo->buffered_image = TRUE;
-
- /* Entropy decoding: either Huffman or arithmetic coding. */
- if (cinfo->arith_code) {
- ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
- } else {
- if (cinfo->progressive_mode) {
-#ifdef D_PROGRESSIVE_SUPPORTED
- jinit_phuff_decoder(cinfo);
-#else
- ERREXIT(cinfo, JERR_NOT_COMPILED);
-#endif
- } else
- jinit_huff_decoder(cinfo);
- }
-
- /* Always get a full-image coefficient buffer. */
- jinit_d_coef_controller(cinfo, TRUE);
-
- /* We can now tell the memory manager to allocate virtual arrays. */
- (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
-
- /* Initialize input side of decompressor to consume first scan. */
- (*cinfo->inputctl->start_input_pass) (cinfo);
-
- /* Initialize progress monitoring. */
- if (cinfo->progress != NULL) {
- int nscans;
- /* Estimate number of scans to set pass_limit. */
- if (cinfo->progressive_mode) {
- /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
- nscans = 2 + 3 * cinfo->num_components;
- } else if (cinfo->inputctl->has_multiple_scans) {
- /* For a nonprogressive multiscan file, estimate 1 scan per component. */
- nscans = cinfo->num_components;
- } else {
- nscans = 1;
- }
- cinfo->progress->pass_counter = 0L;
- cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
- cinfo->progress->completed_passes = 0;
- cinfo->progress->total_passes = 1;
- }
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jdtrans.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 library routines for transcoding decompression,
+ * that is, reading raw DCT coefficient arrays from an input JPEG file.
+ * The routines in jdapimin.c will also be needed by a transcoder.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/* Forward declarations */
+LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
+
+
+/*
+ * Read the coefficient arrays from a JPEG file.
+ * jpeg_read_header must be completed before calling this.
+ *
+ * The entire image is read into a set of virtual coefficient-block arrays,
+ * one per component. The return value is a pointer to the array of
+ * virtual-array descriptors. These can be manipulated directly via the
+ * JPEG memory manager, or handed off to jpeg_write_coefficients().
+ * To release the memory occupied by the virtual arrays, call
+ * jpeg_finish_decompress() when done with the data.
+ *
+ * An alternative usage is to simply obtain access to the coefficient arrays
+ * during a buffered-image-mode decompression operation. This is allowed
+ * after any jpeg_finish_output() call. The arrays can be accessed until
+ * jpeg_finish_decompress() is called. (Note that any call to the library
+ * may reposition the arrays, so don't rely on access_virt_barray() results
+ * to stay valid across library calls.)
+ *
+ * Returns NULL if suspended. This case need be checked only if
+ * a suspending data source is used.
+ */
+
+GLOBAL(jvirt_barray_ptr *)
+jpeg_read_coefficients (j_decompress_ptr cinfo)
+{
+ if (cinfo->global_state == DSTATE_READY) {
+ /* First call: initialize active modules */
+ transdecode_master_selection(cinfo);
+ cinfo->global_state = DSTATE_RDCOEFS;
+ }
+ if (cinfo->global_state == DSTATE_RDCOEFS) {
+ /* Absorb whole file into the coef buffer */
+ for (;;) {
+ int retcode;
+ /* Call progress monitor hook if present */
+ if (cinfo->progress != NULL)
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
+ /* Absorb some more input */
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
+ if (retcode == JPEG_SUSPENDED)
+ return NULL;
+ if (retcode == JPEG_REACHED_EOI)
+ break;
+ /* Advance progress counter if appropriate */
+ if (cinfo->progress != NULL &&
+ (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
+ if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
+ /* startup underestimated number of scans; ratchet up one scan */
+ cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
+ }
+ }
+ }
+ /* Set state so that jpeg_finish_decompress does the right thing */
+ cinfo->global_state = DSTATE_STOPPING;
+ }
+ /* At this point we should be in state DSTATE_STOPPING if being used
+ * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
+ * to the coefficients during a full buffered-image-mode decompression.
+ */
+ if ((cinfo->global_state == DSTATE_STOPPING ||
+ cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
+ return cinfo->coef->coef_arrays;
+ }
+ /* Oops, improper usage */
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+ return NULL; /* keep compiler happy */
+}
+
+
+/*
+ * Master selection of decompression modules for transcoding.
+ * This substitutes for jdmaster.c's initialization of the full decompressor.
+ */
+
+LOCAL(void)
+transdecode_master_selection (j_decompress_ptr cinfo)
+{
+ /* This is effectively a buffered-image operation. */
+ cinfo->buffered_image = TRUE;
+
+ /* Entropy decoding: either Huffman or arithmetic coding. */
+ if (cinfo->arith_code) {
+ ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
+ } else {
+ if (cinfo->progressive_mode) {
+#ifdef D_PROGRESSIVE_SUPPORTED
+ jinit_phuff_decoder(cinfo);
+#else
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
+#endif
+ } else
+ jinit_huff_decoder(cinfo);
+ }
+
+ /* Always get a full-image coefficient buffer. */
+ jinit_d_coef_controller(cinfo, TRUE);
+
+ /* We can now tell the memory manager to allocate virtual arrays. */
+ (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
+
+ /* Initialize input side of decompressor to consume first scan. */
+ (*cinfo->inputctl->start_input_pass) (cinfo);
+
+ /* Initialize progress monitoring. */
+ if (cinfo->progress != NULL) {
+ int nscans;
+ /* Estimate number of scans to set pass_limit. */
+ if (cinfo->progressive_mode) {
+ /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
+ nscans = 2 + 3 * cinfo->num_components;
+ } else if (cinfo->inputctl->has_multiple_scans) {
+ /* For a nonprogressive multiscan file, estimate 1 scan per component. */
+ nscans = cinfo->num_components;
+ } else {
+ nscans = 1;
+ }
+ cinfo->progress->pass_counter = 0L;
+ cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
+ cinfo->progress->completed_passes = 0;
+ cinfo->progress->total_passes = 1;
+ }
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jerror.c b/core/src/fxcodec/libjpeg/fpdfapi_jerror.c
index 9d673ba845..42e4066757 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jerror.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jerror.c
@@ -1,255 +1,255 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jerror.c
- *
- * Copyright (C) 1991-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 simple error-reporting and trace-message routines.
- * These are suitable for Unix-like systems and others where writing to
- * stderr is the right thing to do. Many applications will want to replace
- * some or all of these routines.
- *
- * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
- * you get a Windows-specific hack to display error messages in a dialog box.
- * It ain't much, but it beats dropping error messages into the bit bucket,
- * which is what happens to output to stderr under most Windows C compilers.
- *
- * These routines are used by both the compression and decompression code.
- */
-
-/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jversion.h"
-#include "jerror.h"
-
-#ifndef EXIT_FAILURE /* define exit() codes if not provided */
-#define EXIT_FAILURE 1
-#endif
-
-
-/*
- * Create the message string table.
- * We do this from the master message list in jerror.h by re-reading
- * jerror.h with a suitable definition for macro JMESSAGE.
- * The message table is made an external symbol just in case any applications
- * want to refer to it directly.
- */
-
-#ifdef NEED_SHORT_EXTERNAL_NAMES
-#define jpeg_std_message_table jMsgTable
-#endif
-
-#define JMESSAGE(code,string) string ,
-
-const char * const jpeg_std_message_table[] = {
-#include "jerror.h"
- NULL
-};
-
-
-/*
- * Error exit handler: must not return to caller.
- *
- * Applications may override this if they want to get control back after
- * an error. Typically one would longjmp somewhere instead of exiting.
- * The setjmp buffer can be made a private field within an expanded error
- * handler object. Note that the info needed to generate an error message
- * is stored in the error object, so you can generate the message now or
- * later, at your convenience.
- * You should make sure that the JPEG object is cleaned up (with jpeg_abort
- * or jpeg_destroy) at some point.
- */
-
-METHODDEF(void)
-error_exit (j_common_ptr cinfo)
-{
- /* Always display the message */
- (*cinfo->err->output_message) (cinfo);
-
- /* Let the memory manager delete any temp files before we die */
- jpeg_destroy(cinfo);
-
-// exit(EXIT_FAILURE);
-}
-
-
-/*
- * Actual output of an error or trace message.
- * Applications may override this method to send JPEG messages somewhere
- * other than stderr.
- *
- * On Windows, printing to stderr is generally completely useless,
- * so we provide optional code to produce an error-dialog popup.
- * Most Windows applications will still prefer to override this routine,
- * but if they don't, it'll do something at least marginally useful.
- *
- * NOTE: to use the library in an environment that doesn't support the
- * C stdio library, you may have to delete the call to fprintf() entirely,
- * not just not use this routine.
- */
-
-METHODDEF(void)
-output_message (j_common_ptr cinfo)
-{
- char buffer[JMSG_LENGTH_MAX];
-
- /* Create the message */
- (*cinfo->err->format_message) (cinfo, buffer);
-
-#ifdef USE_WINDOWS_MESSAGEBOX
- /* Display it in a message dialog box */
- MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
- MB_OK | MB_ICONERROR);
-#else
- /* Send it to stderr, adding a newline */
-#ifndef _FPDFAPI_MINI_
- FXSYS_fprintf(stderr, "%s\n", buffer);
-#endif
-#endif
-}
-
-
-/*
- * Decide whether to emit a trace or warning message.
- * msg_level is one of:
- * -1: recoverable corrupt-data warning, may want to abort.
- * 0: important advisory messages (always display to user).
- * 1: first level of tracing detail.
- * 2,3,...: successively more detailed tracing messages.
- * An application might override this method if it wanted to abort on warnings
- * or change the policy about which messages to display.
- */
-
-METHODDEF(void)
-emit_message (j_common_ptr cinfo, int msg_level)
-{
- struct jpeg_error_mgr * err = cinfo->err;
-
- if (msg_level < 0) {
- /* It's a warning message. Since corrupt files may generate many warnings,
- * the policy implemented here is to show only the first warning,
- * unless trace_level >= 3.
- */
- if (err->num_warnings == 0 || err->trace_level >= 3)
- (*err->output_message) (cinfo);
- /* Always count warnings in num_warnings. */
- err->num_warnings++;
- } else {
- /* It's a trace message. Show it if trace_level >= msg_level. */
- if (err->trace_level >= msg_level)
- (*err->output_message) (cinfo);
- }
-}
-
-
-/*
- * Format a message string for the most recent JPEG error or message.
- * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
- * characters. Note that no '\n' character is added to the string.
- * Few applications should need to override this method.
- */
-
-METHODDEF(void)
-format_message (j_common_ptr cinfo, char * buffer)
-{
-#if 0 /* XYQ */
- struct jpeg_error_mgr * err = cinfo->err;
- int msg_code = err->msg_code;
- const char * msgtext = NULL;
- const char * msgptr;
- char ch;
- boolean isstring;
-
- /* Look up message string in proper table */
- if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
- msgtext = err->jpeg_message_table[msg_code];
- } else if (err->addon_message_table != NULL &&
- msg_code >= err->first_addon_message &&
- msg_code <= err->last_addon_message) {
- msgtext = err->addon_message_table[msg_code - err->first_addon_message];
- }
-
- /* Defend against bogus message number */
- if (msgtext == NULL) {
- err->msg_parm.i[0] = msg_code;
- msgtext = err->jpeg_message_table[0];
- }
-
- /* Check for string parameter, as indicated by %s in the message text */
- isstring = FALSE;
- msgptr = msgtext;
- while ((ch = *msgptr++) != '\0') {
- if (ch == '%') {
- if (*msgptr == 's') isstring = TRUE;
- break;
- }
- }
-
- /* Format the message into the passed buffer */
- if (isstring)
- FXSYS_sprintf(buffer, msgtext, err->msg_parm.s);
- else
- FXSYS_sprintf(buffer, msgtext,
- err->msg_parm.i[0], err->msg_parm.i[1],
- err->msg_parm.i[2], err->msg_parm.i[3],
- err->msg_parm.i[4], err->msg_parm.i[5],
- err->msg_parm.i[6], err->msg_parm.i[7]);
-#endif
-}
-
-
-/*
- * Reset error state variables at start of a new image.
- * This is called during compression startup to reset trace/error
- * processing to default state, without losing any application-specific
- * method pointers. An application might possibly want to override
- * this method if it has additional error processing state.
- */
-
-METHODDEF(void)
-reset_error_mgr (j_common_ptr cinfo)
-{
- cinfo->err->num_warnings = 0;
- /* trace_level is not reset since it is an application-supplied parameter */
- cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
-}
-
-
-/*
- * Fill in the standard error-handling methods in a jpeg_error_mgr object.
- * Typical call is:
- * struct jpeg_compress_struct cinfo;
- * struct jpeg_error_mgr err;
- *
- * cinfo.err = jpeg_std_error(&err);
- * after which the application may override some of the methods.
- */
-
-GLOBAL(struct jpeg_error_mgr *)
-jpeg_std_error (struct jpeg_error_mgr * err)
-{
- err->error_exit = error_exit;
- err->emit_message = emit_message;
- err->output_message = output_message;
- err->format_message = format_message;
- err->reset_error_mgr = reset_error_mgr;
-
- err->trace_level = 0; /* default = no tracing */
- err->num_warnings = 0; /* no warnings emitted yet */
- err->msg_code = 0; /* may be useful as a flag for "no error" */
-
- /* Initialize message table pointers */
- err->jpeg_message_table = jpeg_std_message_table;
- err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
-
- err->addon_message_table = NULL;
- err->first_addon_message = 0; /* for safety */
- err->last_addon_message = 0;
-
- return err;
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jerror.c
+ *
+ * Copyright (C) 1991-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 simple error-reporting and trace-message routines.
+ * These are suitable for Unix-like systems and others where writing to
+ * stderr is the right thing to do. Many applications will want to replace
+ * some or all of these routines.
+ *
+ * If you define USE_WINDOWS_MESSAGEBOX in jconfig.h or in the makefile,
+ * you get a Windows-specific hack to display error messages in a dialog box.
+ * It ain't much, but it beats dropping error messages into the bit bucket,
+ * which is what happens to output to stderr under most Windows C compilers.
+ *
+ * These routines are used by both the compression and decompression code.
+ */
+
+/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jversion.h"
+#include "jerror.h"
+
+#ifndef EXIT_FAILURE /* define exit() codes if not provided */
+#define EXIT_FAILURE 1
+#endif
+
+
+/*
+ * Create the message string table.
+ * We do this from the master message list in jerror.h by re-reading
+ * jerror.h with a suitable definition for macro JMESSAGE.
+ * The message table is made an external symbol just in case any applications
+ * want to refer to it directly.
+ */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_std_message_table jMsgTable
+#endif
+
+#define JMESSAGE(code,string) string ,
+
+const char * const jpeg_std_message_table[] = {
+#include "jerror.h"
+ NULL
+};
+
+
+/*
+ * Error exit handler: must not return to caller.
+ *
+ * Applications may override this if they want to get control back after
+ * an error. Typically one would longjmp somewhere instead of exiting.
+ * The setjmp buffer can be made a private field within an expanded error
+ * handler object. Note that the info needed to generate an error message
+ * is stored in the error object, so you can generate the message now or
+ * later, at your convenience.
+ * You should make sure that the JPEG object is cleaned up (with jpeg_abort
+ * or jpeg_destroy) at some point.
+ */
+
+METHODDEF(void)
+error_exit (j_common_ptr cinfo)
+{
+ /* Always display the message */
+ (*cinfo->err->output_message) (cinfo);
+
+ /* Let the memory manager delete any temp files before we die */
+ jpeg_destroy(cinfo);
+
+// exit(EXIT_FAILURE);
+}
+
+
+/*
+ * Actual output of an error or trace message.
+ * Applications may override this method to send JPEG messages somewhere
+ * other than stderr.
+ *
+ * On Windows, printing to stderr is generally completely useless,
+ * so we provide optional code to produce an error-dialog popup.
+ * Most Windows applications will still prefer to override this routine,
+ * but if they don't, it'll do something at least marginally useful.
+ *
+ * NOTE: to use the library in an environment that doesn't support the
+ * C stdio library, you may have to delete the call to fprintf() entirely,
+ * not just not use this routine.
+ */
+
+METHODDEF(void)
+output_message (j_common_ptr cinfo)
+{
+ char buffer[JMSG_LENGTH_MAX];
+
+ /* Create the message */
+ (*cinfo->err->format_message) (cinfo, buffer);
+
+#ifdef USE_WINDOWS_MESSAGEBOX
+ /* Display it in a message dialog box */
+ MessageBox(GetActiveWindow(), buffer, "JPEG Library Error",
+ MB_OK | MB_ICONERROR);
+#else
+ /* Send it to stderr, adding a newline */
+#ifndef _FPDFAPI_MINI_
+ FXSYS_fprintf(stderr, "%s\n", buffer);
+#endif
+#endif
+}
+
+
+/*
+ * Decide whether to emit a trace or warning message.
+ * msg_level is one of:
+ * -1: recoverable corrupt-data warning, may want to abort.
+ * 0: important advisory messages (always display to user).
+ * 1: first level of tracing detail.
+ * 2,3,...: successively more detailed tracing messages.
+ * An application might override this method if it wanted to abort on warnings
+ * or change the policy about which messages to display.
+ */
+
+METHODDEF(void)
+emit_message (j_common_ptr cinfo, int msg_level)
+{
+ struct jpeg_error_mgr * err = cinfo->err;
+
+ if (msg_level < 0) {
+ /* It's a warning message. Since corrupt files may generate many warnings,
+ * the policy implemented here is to show only the first warning,
+ * unless trace_level >= 3.
+ */
+ if (err->num_warnings == 0 || err->trace_level >= 3)
+ (*err->output_message) (cinfo);
+ /* Always count warnings in num_warnings. */
+ err->num_warnings++;
+ } else {
+ /* It's a trace message. Show it if trace_level >= msg_level. */
+ if (err->trace_level >= msg_level)
+ (*err->output_message) (cinfo);
+ }
+}
+
+
+/*
+ * Format a message string for the most recent JPEG error or message.
+ * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
+ * characters. Note that no '\n' character is added to the string.
+ * Few applications should need to override this method.
+ */
+
+METHODDEF(void)
+format_message (j_common_ptr cinfo, char * buffer)
+{
+#if 0 /* XYQ */
+ struct jpeg_error_mgr * err = cinfo->err;
+ int msg_code = err->msg_code;
+ const char * msgtext = NULL;
+ const char * msgptr;
+ char ch;
+ boolean isstring;
+
+ /* Look up message string in proper table */
+ if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
+ msgtext = err->jpeg_message_table[msg_code];
+ } else if (err->addon_message_table != NULL &&
+ msg_code >= err->first_addon_message &&
+ msg_code <= err->last_addon_message) {
+ msgtext = err->addon_message_table[msg_code - err->first_addon_message];
+ }
+
+ /* Defend against bogus message number */
+ if (msgtext == NULL) {
+ err->msg_parm.i[0] = msg_code;
+ msgtext = err->jpeg_message_table[0];
+ }
+
+ /* Check for string parameter, as indicated by %s in the message text */
+ isstring = FALSE;
+ msgptr = msgtext;
+ while ((ch = *msgptr++) != '\0') {
+ if (ch == '%') {
+ if (*msgptr == 's') isstring = TRUE;
+ break;
+ }
+ }
+
+ /* Format the message into the passed buffer */
+ if (isstring)
+ FXSYS_sprintf(buffer, msgtext, err->msg_parm.s);
+ else
+ FXSYS_sprintf(buffer, msgtext,
+ err->msg_parm.i[0], err->msg_parm.i[1],
+ err->msg_parm.i[2], err->msg_parm.i[3],
+ err->msg_parm.i[4], err->msg_parm.i[5],
+ err->msg_parm.i[6], err->msg_parm.i[7]);
+#endif
+}
+
+
+/*
+ * Reset error state variables at start of a new image.
+ * This is called during compression startup to reset trace/error
+ * processing to default state, without losing any application-specific
+ * method pointers. An application might possibly want to override
+ * this method if it has additional error processing state.
+ */
+
+METHODDEF(void)
+reset_error_mgr (j_common_ptr cinfo)
+{
+ cinfo->err->num_warnings = 0;
+ /* trace_level is not reset since it is an application-supplied parameter */
+ cinfo->err->msg_code = 0; /* may be useful as a flag for "no error" */
+}
+
+
+/*
+ * Fill in the standard error-handling methods in a jpeg_error_mgr object.
+ * Typical call is:
+ * struct jpeg_compress_struct cinfo;
+ * struct jpeg_error_mgr err;
+ *
+ * cinfo.err = jpeg_std_error(&err);
+ * after which the application may override some of the methods.
+ */
+
+GLOBAL(struct jpeg_error_mgr *)
+jpeg_std_error (struct jpeg_error_mgr * err)
+{
+ err->error_exit = error_exit;
+ err->emit_message = emit_message;
+ err->output_message = output_message;
+ err->format_message = format_message;
+ err->reset_error_mgr = reset_error_mgr;
+
+ err->trace_level = 0; /* default = no tracing */
+ err->num_warnings = 0; /* no warnings emitted yet */
+ err->msg_code = 0; /* may be useful as a flag for "no error" */
+
+ /* Initialize message table pointers */
+ err->jpeg_message_table = jpeg_std_message_table;
+ err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
+
+ err->addon_message_table = NULL;
+ err->first_addon_message = 0; /* for safety */
+ err->last_addon_message = 0;
+
+ return err;
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c b/core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c
index 9cd3dd798e..b978b468d7 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jfdctfst.c
@@ -1,227 +1,227 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jfdctfst.c
- *
- * Copyright (C) 1994-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 contains a fast, not so accurate integer implementation of the
- * forward DCT (Discrete Cosine Transform).
- *
- * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
- * on each column. Direct algorithms are also available, but they are
- * much more complex and seem not to be any faster when reduced to code.
- *
- * This implementation is based on Arai, Agui, and Nakajima's algorithm for
- * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
- * Japanese, but the algorithm is described in the Pennebaker & Mitchell
- * JPEG textbook (see REFERENCES section in file README). The following code
- * is based directly on figure 4-8 in P&M.
- * While an 8-point DCT cannot be done in less than 11 multiplies, it is
- * possible to arrange the computation so that many of the multiplies are
- * simple scalings of the final outputs. These multiplies can then be
- * folded into the multiplications or divisions by the JPEG quantization
- * table entries. The AA&N method leaves only 5 multiplies and 29 adds
- * to be done in the DCT itself.
- * The primary disadvantage of this method is that with fixed-point math,
- * accuracy is lost due to imprecise representation of the scaled
- * quantization values. The smaller the quantization table entry, the less
- * precise the scaled value, so this implementation does worse with high-
- * quality-setting files than with low-quality ones.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jdct.h" /* Private declarations for DCT subsystem */
-
-#ifdef DCT_IFAST_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 decisions are generally the same as in the LL&M algorithm;
- * see jfdctint.c for more details. However, we choose to descale
- * (right shift) multiplication products as soon as they are formed,
- * rather than carrying additional fractional bits into subsequent additions.
- * This compromises accuracy slightly, but it lets us save a few shifts.
- * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
- * everywhere except in the multiplications proper; this saves a good deal
- * of work on 16-bit-int machines.
- *
- * Again to save a few shifts, the intermediate results between pass 1 and
- * pass 2 are not upscaled, but are represented only to integral precision.
- *
- * A final compromise is to represent the multiplicative constants to only
- * 8 fractional bits, rather than 13. This saves some shifting work on some
- * machines, and may also reduce the cost of multiplication (since there
- * are fewer one-bits in the constants).
- */
-
-#define CONST_BITS 8
-
-
-/* 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 == 8
-#define FIX_0_382683433 ((INT32) 98) /* FIX(0.382683433) */
-#define FIX_0_541196100 ((INT32) 139) /* FIX(0.541196100) */
-#define FIX_0_707106781 ((INT32) 181) /* FIX(0.707106781) */
-#define FIX_1_306562965 ((INT32) 334) /* FIX(1.306562965) */
-#else
-#define FIX_0_382683433 FIX(0.382683433)
-#define FIX_0_541196100 FIX(0.541196100)
-#define FIX_0_707106781 FIX(0.707106781)
-#define FIX_1_306562965 FIX(1.306562965)
-#endif
-
-
-/* We can gain a little more speed, with a further compromise in accuracy,
- * by omitting the addition in a descaling shift. This yields an incorrectly
- * rounded result half the time...
- */
-
-#ifndef USE_ACCURATE_ROUNDING
-#undef DESCALE
-#define DESCALE(x,n) RIGHT_SHIFT(x, n)
-#endif
-
-
-/* Multiply a DCTELEM variable by an INT32 constant, and immediately
- * descale to yield a DCTELEM result.
- */
-
-#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
-
-
-/*
- * Perform the forward DCT on one block of samples.
- */
-
-GLOBAL(void)
-jpeg_fdct_ifast (DCTELEM * data)
-{
- DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
- DCTELEM tmp10, tmp11, tmp12, tmp13;
- DCTELEM z1, z2, z3, z4, z5, z11, z13;
- DCTELEM *dataptr;
- int ctr;
- SHIFT_TEMPS
-
- /* Pass 1: process rows. */
-
- dataptr = data;
- for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
- tmp0 = dataptr[0] + dataptr[7];
- tmp7 = dataptr[0] - dataptr[7];
- tmp1 = dataptr[1] + dataptr[6];
- tmp6 = dataptr[1] - dataptr[6];
- tmp2 = dataptr[2] + dataptr[5];
- tmp5 = dataptr[2] - dataptr[5];
- tmp3 = dataptr[3] + dataptr[4];
- tmp4 = dataptr[3] - dataptr[4];
-
- /* Even part */
-
- tmp10 = tmp0 + tmp3; /* phase 2 */
- tmp13 = tmp0 - tmp3;
- tmp11 = tmp1 + tmp2;
- tmp12 = tmp1 - tmp2;
-
- dataptr[0] = tmp10 + tmp11; /* phase 3 */
- dataptr[4] = tmp10 - tmp11;
-
- z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
- dataptr[2] = tmp13 + z1; /* phase 5 */
- dataptr[6] = tmp13 - z1;
-
- /* Odd part */
-
- tmp10 = tmp4 + tmp5; /* phase 2 */
- tmp11 = tmp5 + tmp6;
- tmp12 = tmp6 + tmp7;
-
- /* The rotator is modified from fig 4-8 to avoid extra negations. */
- z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
- z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
- z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
- z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
-
- z11 = tmp7 + z3; /* phase 5 */
- z13 = tmp7 - z3;
-
- dataptr[5] = z13 + z2; /* phase 6 */
- dataptr[3] = z13 - z2;
- dataptr[1] = z11 + z4;
- dataptr[7] = z11 - z4;
-
- dataptr += DCTSIZE; /* advance pointer to next row */
- }
-
- /* Pass 2: process columns. */
-
- dataptr = data;
- for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
- tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
- tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
- tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
- tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
- tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
- tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
- tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
- tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
-
- /* Even part */
-
- tmp10 = tmp0 + tmp3; /* phase 2 */
- tmp13 = tmp0 - tmp3;
- tmp11 = tmp1 + tmp2;
- tmp12 = tmp1 - tmp2;
-
- dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
- dataptr[DCTSIZE*4] = tmp10 - tmp11;
-
- z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
- dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
- dataptr[DCTSIZE*6] = tmp13 - z1;
-
- /* Odd part */
-
- tmp10 = tmp4 + tmp5; /* phase 2 */
- tmp11 = tmp5 + tmp6;
- tmp12 = tmp6 + tmp7;
-
- /* The rotator is modified from fig 4-8 to avoid extra negations. */
- z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
- z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
- z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
- z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
-
- z11 = tmp7 + z3; /* phase 5 */
- z13 = tmp7 - z3;
-
- dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
- dataptr[DCTSIZE*3] = z13 - z2;
- dataptr[DCTSIZE*1] = z11 + z4;
- dataptr[DCTSIZE*7] = z11 - z4;
-
- dataptr++; /* advance pointer to next column */
- }
-}
-
-#endif /* DCT_IFAST_SUPPORTED */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jfdctfst.c
+ *
+ * Copyright (C) 1994-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 contains a fast, not so accurate integer implementation of the
+ * forward DCT (Discrete Cosine Transform).
+ *
+ * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
+ * on each column. Direct algorithms are also available, but they are
+ * much more complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README). The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs. These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries. The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with fixed-point math,
+ * accuracy is lost due to imprecise representation of the scaled
+ * quantization values. The smaller the quantization table entry, the less
+ * precise the scaled value, so this implementation does worse with high-
+ * quality-setting files than with low-quality ones.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_IFAST_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 decisions are generally the same as in the LL&M algorithm;
+ * see jfdctint.c for more details. However, we choose to descale
+ * (right shift) multiplication products as soon as they are formed,
+ * rather than carrying additional fractional bits into subsequent additions.
+ * This compromises accuracy slightly, but it lets us save a few shifts.
+ * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
+ * everywhere except in the multiplications proper; this saves a good deal
+ * of work on 16-bit-int machines.
+ *
+ * Again to save a few shifts, the intermediate results between pass 1 and
+ * pass 2 are not upscaled, but are represented only to integral precision.
+ *
+ * A final compromise is to represent the multiplicative constants to only
+ * 8 fractional bits, rather than 13. This saves some shifting work on some
+ * machines, and may also reduce the cost of multiplication (since there
+ * are fewer one-bits in the constants).
+ */
+
+#define CONST_BITS 8
+
+
+/* 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 == 8
+#define FIX_0_382683433 ((INT32) 98) /* FIX(0.382683433) */
+#define FIX_0_541196100 ((INT32) 139) /* FIX(0.541196100) */
+#define FIX_0_707106781 ((INT32) 181) /* FIX(0.707106781) */
+#define FIX_1_306562965 ((INT32) 334) /* FIX(1.306562965) */
+#else
+#define FIX_0_382683433 FIX(0.382683433)
+#define FIX_0_541196100 FIX(0.541196100)
+#define FIX_0_707106781 FIX(0.707106781)
+#define FIX_1_306562965 FIX(1.306562965)
+#endif
+
+
+/* We can gain a little more speed, with a further compromise in accuracy,
+ * by omitting the addition in a descaling shift. This yields an incorrectly
+ * rounded result half the time...
+ */
+
+#ifndef USE_ACCURATE_ROUNDING
+#undef DESCALE
+#define DESCALE(x,n) RIGHT_SHIFT(x, n)
+#endif
+
+
+/* Multiply a DCTELEM variable by an INT32 constant, and immediately
+ * descale to yield a DCTELEM result.
+ */
+
+#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
+
+
+/*
+ * Perform the forward DCT on one block of samples.
+ */
+
+GLOBAL(void)
+jpeg_fdct_ifast (DCTELEM * data)
+{
+ DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+ DCTELEM tmp10, tmp11, tmp12, tmp13;
+ DCTELEM z1, z2, z3, z4, z5, z11, z13;
+ DCTELEM *dataptr;
+ int ctr;
+ SHIFT_TEMPS
+
+ /* Pass 1: process rows. */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[0] + dataptr[7];
+ tmp7 = dataptr[0] - dataptr[7];
+ tmp1 = dataptr[1] + dataptr[6];
+ tmp6 = dataptr[1] - dataptr[6];
+ tmp2 = dataptr[2] + dataptr[5];
+ tmp5 = dataptr[2] - dataptr[5];
+ tmp3 = dataptr[3] + dataptr[4];
+ tmp4 = dataptr[3] - dataptr[4];
+
+ /* Even part */
+
+ tmp10 = tmp0 + tmp3; /* phase 2 */
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[0] = tmp10 + tmp11; /* phase 3 */
+ dataptr[4] = tmp10 - tmp11;
+
+ z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
+ dataptr[2] = tmp13 + z1; /* phase 5 */
+ dataptr[6] = tmp13 - z1;
+
+ /* Odd part */
+
+ tmp10 = tmp4 + tmp5; /* phase 2 */
+ tmp11 = tmp5 + tmp6;
+ tmp12 = tmp6 + tmp7;
+
+ /* The rotator is modified from fig 4-8 to avoid extra negations. */
+ z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
+ z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
+ z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
+ z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
+
+ z11 = tmp7 + z3; /* phase 5 */
+ z13 = tmp7 - z3;
+
+ dataptr[5] = z13 + z2; /* phase 6 */
+ dataptr[3] = z13 - z2;
+ dataptr[1] = z11 + z4;
+ dataptr[7] = z11 - z4;
+
+ dataptr += DCTSIZE; /* advance pointer to next row */
+ }
+
+ /* Pass 2: process columns. */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
+ tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
+ tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
+ tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
+ tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
+ tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
+ tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
+ tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
+
+ /* Even part */
+
+ tmp10 = tmp0 + tmp3; /* phase 2 */
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
+ dataptr[DCTSIZE*4] = tmp10 - tmp11;
+
+ z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
+ dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
+ dataptr[DCTSIZE*6] = tmp13 - z1;
+
+ /* Odd part */
+
+ tmp10 = tmp4 + tmp5; /* phase 2 */
+ tmp11 = tmp5 + tmp6;
+ tmp12 = tmp6 + tmp7;
+
+ /* The rotator is modified from fig 4-8 to avoid extra negations. */
+ z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
+ z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
+ z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
+ z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
+
+ z11 = tmp7 + z3; /* phase 5 */
+ z13 = tmp7 - z3;
+
+ dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
+ dataptr[DCTSIZE*3] = z13 - z2;
+ dataptr[DCTSIZE*1] = z11 + z4;
+ dataptr[DCTSIZE*7] = z11 - z4;
+
+ dataptr++; /* advance pointer to next column */
+ }
+}
+
+#endif /* DCT_IFAST_SUPPORTED */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c b/core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c
index 88810a7d8e..488505b062 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jfdctint.c
@@ -1,286 +1,286 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jfdctint.c
- *
- * Copyright (C) 1991-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 contains a slow-but-accurate integer implementation of the
- * forward DCT (Discrete Cosine Transform).
- *
- * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
- * on each column. Direct algorithms are also available, but they are
- * much more complex and seem not to be any faster when reduced to code.
- *
- * This implementation is based on an algorithm described in
- * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
- * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
- * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
- * The primary algorithm described there uses 11 multiplies and 29 adds.
- * We use their alternate method with 12 multiplies and 32 adds.
- * The advantage of this method is that no data path contains more than one
- * multiplication; this allows a very simple and accurate implementation in
- * scaled fixed-point arithmetic, with a minimal number of shifts.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jdct.h" /* Private declarations for DCT subsystem */
-
-#ifdef DCT_ISLOW_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
-
-
-/*
- * The poop on this scaling stuff is as follows:
- *
- * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
- * larger than the true DCT outputs. The final outputs are therefore
- * a factor of N larger than desired; since N=8 this can be cured by
- * a simple right shift at the end of the algorithm. The advantage of
- * this arrangement is that we save two multiplications per 1-D DCT,
- * because the y0 and y4 outputs need not be divided by sqrt(N).
- * In the IJG code, this factor of 8 is removed by the quantization step
- * (in jcdctmgr.c), NOT in this module.
- *
- * We have to do addition and subtraction of the integer inputs, which
- * is no problem, and multiplication by fractional constants, which is
- * a problem to do in integer arithmetic. We multiply all the constants
- * by CONST_SCALE and convert them to integer constants (thus retaining
- * CONST_BITS bits of precision in the constants). After doing a
- * multiplication we have to divide the product by CONST_SCALE, with proper
- * rounding, to produce the correct output. This division can be done
- * cheaply as a right shift of CONST_BITS bits. We postpone shifting
- * as long as possible so that partial sums can be added together with
- * full fractional precision.
- *
- * The outputs of the first pass are scaled up by PASS1_BITS bits so that
- * they are represented to better-than-integral precision. These outputs
- * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
- * with the recommended scaling. (For 12-bit sample data, the intermediate
- * array is INT32 anyway.)
- *
- * To avoid overflow of the 32-bit intermediate results in pass 2, we must
- * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
- * shows that the values given below are the most effective.
- */
-
-#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_298631336 ((INT32) 2446) /* FIX(0.298631336) */
-#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */
-#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
-#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
-#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
-#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */
-#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */
-#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
-#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */
-#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */
-#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
-#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */
-#else
-#define FIX_0_298631336 FIX(0.298631336)
-#define FIX_0_390180644 FIX(0.390180644)
-#define FIX_0_541196100 FIX(0.541196100)
-#define FIX_0_765366865 FIX(0.765366865)
-#define FIX_0_899976223 FIX(0.899976223)
-#define FIX_1_175875602 FIX(1.175875602)
-#define FIX_1_501321110 FIX(1.501321110)
-#define FIX_1_847759065 FIX(1.847759065)
-#define FIX_1_961570560 FIX(1.961570560)
-#define FIX_2_053119869 FIX(2.053119869)
-#define FIX_2_562915447 FIX(2.562915447)
-#define FIX_3_072711026 FIX(3.072711026)
-#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
-
-
-/*
- * Perform the forward DCT on one block of samples.
- */
-
-GLOBAL(void)
-jpeg_fdct_islow (DCTELEM * data)
-{
- INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
- INT32 tmp10, tmp11, tmp12, tmp13;
- INT32 z1, z2, z3, z4, z5;
- DCTELEM *dataptr;
- int ctr;
- SHIFT_TEMPS
-
- /* Pass 1: process rows. */
- /* Note results are scaled up by sqrt(8) compared to a true DCT; */
- /* furthermore, we scale the results by 2**PASS1_BITS. */
-
- dataptr = data;
- for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
- tmp0 = dataptr[0] + dataptr[7];
- tmp7 = dataptr[0] - dataptr[7];
- tmp1 = dataptr[1] + dataptr[6];
- tmp6 = dataptr[1] - dataptr[6];
- tmp2 = dataptr[2] + dataptr[5];
- tmp5 = dataptr[2] - dataptr[5];
- tmp3 = dataptr[3] + dataptr[4];
- tmp4 = dataptr[3] - dataptr[4];
-
- /* Even part per LL&M figure 1 --- note that published figure is faulty;
- * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
- */
-
- tmp10 = tmp0 + tmp3;
- tmp13 = tmp0 - tmp3;
- tmp11 = tmp1 + tmp2;
- tmp12 = tmp1 - tmp2;
-
- dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS);
- dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
-
- z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
- dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
- CONST_BITS-PASS1_BITS);
- dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
- CONST_BITS-PASS1_BITS);
-
- /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
- * cK represents cos(K*pi/16).
- * i0..i3 in the paper are tmp4..tmp7 here.
- */
-
- z1 = tmp4 + tmp7;
- z2 = tmp5 + tmp6;
- z3 = tmp4 + tmp6;
- z4 = tmp5 + tmp7;
- z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
-
- tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
- tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
- tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
- tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
- z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
- z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
- z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
- z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
-
- z3 += z5;
- z4 += z5;
-
- dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS);
- dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS);
- dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS);
- dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS);
-
- dataptr += DCTSIZE; /* advance pointer to next row */
- }
-
- /* Pass 2: process columns.
- * We remove the PASS1_BITS scaling, but leave the results scaled up
- * by an overall factor of 8.
- */
-
- dataptr = data;
- for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
- tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
- tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
- tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
- tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
- tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
- tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
- tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
- tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
-
- /* Even part per LL&M figure 1 --- note that published figure is faulty;
- * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
- */
-
- tmp10 = tmp0 + tmp3;
- tmp13 = tmp0 - tmp3;
- tmp11 = tmp1 + tmp2;
- tmp12 = tmp1 - tmp2;
-
- dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS);
- dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS);
-
- z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
- dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
- CONST_BITS+PASS1_BITS);
- dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
- CONST_BITS+PASS1_BITS);
-
- /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
- * cK represents cos(K*pi/16).
- * i0..i3 in the paper are tmp4..tmp7 here.
- */
-
- z1 = tmp4 + tmp7;
- z2 = tmp5 + tmp6;
- z3 = tmp4 + tmp6;
- z4 = tmp5 + tmp7;
- z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
-
- tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
- tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
- tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
- tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
- z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
- z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
- z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
- z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
-
- z3 += z5;
- z4 += z5;
-
- dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3,
- CONST_BITS+PASS1_BITS);
- dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4,
- CONST_BITS+PASS1_BITS);
- dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3,
- CONST_BITS+PASS1_BITS);
- dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4,
- CONST_BITS+PASS1_BITS);
-
- dataptr++; /* advance pointer to next column */
- }
-}
-
-#endif /* DCT_ISLOW_SUPPORTED */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jfdctint.c
+ *
+ * Copyright (C) 1991-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 contains a slow-but-accurate integer implementation of the
+ * forward DCT (Discrete Cosine Transform).
+ *
+ * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
+ * on each column. Direct algorithms are also available, but they are
+ * much more complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on an algorithm described in
+ * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
+ * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
+ * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
+ * The primary algorithm described there uses 11 multiplies and 29 adds.
+ * We use their alternate method with 12 multiplies and 32 adds.
+ * The advantage of this method is that no data path contains more than one
+ * multiplication; this allows a very simple and accurate implementation in
+ * scaled fixed-point arithmetic, with a minimal number of shifts.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_ISLOW_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
+
+
+/*
+ * The poop on this scaling stuff is as follows:
+ *
+ * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
+ * larger than the true DCT outputs. The final outputs are therefore
+ * a factor of N larger than desired; since N=8 this can be cured by
+ * a simple right shift at the end of the algorithm. The advantage of
+ * this arrangement is that we save two multiplications per 1-D DCT,
+ * because the y0 and y4 outputs need not be divided by sqrt(N).
+ * In the IJG code, this factor of 8 is removed by the quantization step
+ * (in jcdctmgr.c), NOT in this module.
+ *
+ * We have to do addition and subtraction of the integer inputs, which
+ * is no problem, and multiplication by fractional constants, which is
+ * a problem to do in integer arithmetic. We multiply all the constants
+ * by CONST_SCALE and convert them to integer constants (thus retaining
+ * CONST_BITS bits of precision in the constants). After doing a
+ * multiplication we have to divide the product by CONST_SCALE, with proper
+ * rounding, to produce the correct output. This division can be done
+ * cheaply as a right shift of CONST_BITS bits. We postpone shifting
+ * as long as possible so that partial sums can be added together with
+ * full fractional precision.
+ *
+ * The outputs of the first pass are scaled up by PASS1_BITS bits so that
+ * they are represented to better-than-integral precision. These outputs
+ * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
+ * with the recommended scaling. (For 12-bit sample data, the intermediate
+ * array is INT32 anyway.)
+ *
+ * To avoid overflow of the 32-bit intermediate results in pass 2, we must
+ * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
+ * shows that the values given below are the most effective.
+ */
+
+#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_298631336 ((INT32) 2446) /* FIX(0.298631336) */
+#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */
+#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
+#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
+#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
+#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */
+#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */
+#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
+#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */
+#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */
+#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
+#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */
+#else
+#define FIX_0_298631336 FIX(0.298631336)
+#define FIX_0_390180644 FIX(0.390180644)
+#define FIX_0_541196100 FIX(0.541196100)
+#define FIX_0_765366865 FIX(0.765366865)
+#define FIX_0_899976223 FIX(0.899976223)
+#define FIX_1_175875602 FIX(1.175875602)
+#define FIX_1_501321110 FIX(1.501321110)
+#define FIX_1_847759065 FIX(1.847759065)
+#define FIX_1_961570560 FIX(1.961570560)
+#define FIX_2_053119869 FIX(2.053119869)
+#define FIX_2_562915447 FIX(2.562915447)
+#define FIX_3_072711026 FIX(3.072711026)
+#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
+
+
+/*
+ * Perform the forward DCT on one block of samples.
+ */
+
+GLOBAL(void)
+jpeg_fdct_islow (DCTELEM * data)
+{
+ INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+ INT32 tmp10, tmp11, tmp12, tmp13;
+ INT32 z1, z2, z3, z4, z5;
+ DCTELEM *dataptr;
+ int ctr;
+ SHIFT_TEMPS
+
+ /* Pass 1: process rows. */
+ /* Note results are scaled up by sqrt(8) compared to a true DCT; */
+ /* furthermore, we scale the results by 2**PASS1_BITS. */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[0] + dataptr[7];
+ tmp7 = dataptr[0] - dataptr[7];
+ tmp1 = dataptr[1] + dataptr[6];
+ tmp6 = dataptr[1] - dataptr[6];
+ tmp2 = dataptr[2] + dataptr[5];
+ tmp5 = dataptr[2] - dataptr[5];
+ tmp3 = dataptr[3] + dataptr[4];
+ tmp4 = dataptr[3] - dataptr[4];
+
+ /* Even part per LL&M figure 1 --- note that published figure is faulty;
+ * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+ */
+
+ tmp10 = tmp0 + tmp3;
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS);
+ dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
+
+ z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
+ dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
+ CONST_BITS-PASS1_BITS);
+ dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
+ CONST_BITS-PASS1_BITS);
+
+ /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
+ * cK represents cos(K*pi/16).
+ * i0..i3 in the paper are tmp4..tmp7 here.
+ */
+
+ z1 = tmp4 + tmp7;
+ z2 = tmp5 + tmp6;
+ z3 = tmp4 + tmp6;
+ z4 = tmp5 + tmp7;
+ z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+
+ tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+ tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+ tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+ tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+
+ z3 += z5;
+ z4 += z5;
+
+ dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS);
+ dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS);
+ dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS);
+ dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS);
+
+ dataptr += DCTSIZE; /* advance pointer to next row */
+ }
+
+ /* Pass 2: process columns.
+ * We remove the PASS1_BITS scaling, but leave the results scaled up
+ * by an overall factor of 8.
+ */
+
+ dataptr = data;
+ for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
+ tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
+ tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
+ tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
+ tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
+ tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
+ tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
+ tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
+ tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
+
+ /* Even part per LL&M figure 1 --- note that published figure is faulty;
+ * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
+ */
+
+ tmp10 = tmp0 + tmp3;
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS);
+ dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS);
+
+ z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
+ dataptr[DCTSIZE*2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
+ CONST_BITS+PASS1_BITS);
+ dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
+ CONST_BITS+PASS1_BITS);
+
+ /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
+ * cK represents cos(K*pi/16).
+ * i0..i3 in the paper are tmp4..tmp7 here.
+ */
+
+ z1 = tmp4 + tmp7;
+ z2 = tmp5 + tmp6;
+ z3 = tmp4 + tmp6;
+ z4 = tmp5 + tmp7;
+ z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+
+ tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+ tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+ tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+ tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+
+ z3 += z5;
+ z4 += z5;
+
+ dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3,
+ CONST_BITS+PASS1_BITS);
+ dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4,
+ CONST_BITS+PASS1_BITS);
+ dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3,
+ CONST_BITS+PASS1_BITS);
+ dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4,
+ CONST_BITS+PASS1_BITS);
+
+ dataptr++; /* advance pointer to next column */
+ }
+}
+
+#endif /* DCT_ISLOW_SUPPORTED */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c b/core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c
index c0c504c2a6..26a8f68a27 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jidctfst.c
@@ -1,371 +1,371 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jidctfst.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 a fast, not so accurate integer implementation of the
- * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
- * must also perform dequantization of the input coefficients.
- *
- * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
- * on each row (or vice versa, but it's more convenient to emit a row at
- * a time). Direct algorithms are also available, but they are much more
- * complex and seem not to be any faster when reduced to code.
- *
- * This implementation is based on Arai, Agui, and Nakajima's algorithm for
- * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
- * Japanese, but the algorithm is described in the Pennebaker & Mitchell
- * JPEG textbook (see REFERENCES section in file README). The following code
- * is based directly on figure 4-8 in P&M.
- * While an 8-point DCT cannot be done in less than 11 multiplies, it is
- * possible to arrange the computation so that many of the multiplies are
- * simple scalings of the final outputs. These multiplies can then be
- * folded into the multiplications or divisions by the JPEG quantization
- * table entries. The AA&N method leaves only 5 multiplies and 29 adds
- * to be done in the DCT itself.
- * The primary disadvantage of this method is that with fixed-point math,
- * accuracy is lost due to imprecise representation of the scaled
- * quantization values. The smaller the quantization table entry, the less
- * precise the scaled value, so this implementation does worse with high-
- * quality-setting files than with low-quality ones.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jdct.h" /* Private declarations for DCT subsystem */
-
-#ifdef DCT_IFAST_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 decisions are generally the same as in the LL&M algorithm;
- * see jidctint.c for more details. However, we choose to descale
- * (right shift) multiplication products as soon as they are formed,
- * rather than carrying additional fractional bits into subsequent additions.
- * This compromises accuracy slightly, but it lets us save a few shifts.
- * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
- * everywhere except in the multiplications proper; this saves a good deal
- * of work on 16-bit-int machines.
- *
- * The dequantized coefficients are not integers because the AA&N scaling
- * factors have been incorporated. We represent them scaled up by PASS1_BITS,
- * so that the first and second IDCT rounds have the same input scaling.
- * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
- * avoid a descaling shift; this compromises accuracy rather drastically
- * for small quantization table entries, but it saves a lot of shifts.
- * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
- * so we use a much larger scaling factor to preserve accuracy.
- *
- * A final compromise is to represent the multiplicative constants to only
- * 8 fractional bits, rather than 13. This saves some shifting work on some
- * machines, and may also reduce the cost of multiplication (since there
- * are fewer one-bits in the constants).
- */
-
-#if BITS_IN_JSAMPLE == 8
-#define CONST_BITS 8
-#define PASS1_BITS 2
-#else
-#define CONST_BITS 8
-#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 == 8
-#define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */
-#define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */
-#define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */
-#define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */
-#else
-#define FIX_1_082392200 FIX(1.082392200)
-#define FIX_1_414213562 FIX(1.414213562)
-#define FIX_1_847759065 FIX(1.847759065)
-#define FIX_2_613125930 FIX(2.613125930)
-#endif
-
-
-/* We can gain a little more speed, with a further compromise in accuracy,
- * by omitting the addition in a descaling shift. This yields an incorrectly
- * rounded result half the time...
- */
-
-#ifndef USE_ACCURATE_ROUNDING
-#undef DESCALE
-#define DESCALE(x,n) RIGHT_SHIFT(x, n)
-#endif
-
-
-/* Multiply a DCTELEM variable by an INT32 constant, and immediately
- * descale to yield a DCTELEM result.
- */
-
-#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
-
-
-/* Dequantize a coefficient by multiplying it by the multiplier-table
- * entry; produce a DCTELEM result. For 8-bit data a 16x16->16
- * multiplication will do. For 12-bit data, the multiplier table is
- * declared INT32, so a 32-bit multiply will be used.
- */
-
-#if BITS_IN_JSAMPLE == 8
-#define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval))
-#else
-#define DEQUANTIZE(coef,quantval) \
- DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
-#endif
-
-
-/* Like DESCALE, but applies to a DCTELEM and produces an int.
- * We assume that int right shift is unsigned if INT32 right shift is.
- */
-
-#ifdef RIGHT_SHIFT_IS_UNSIGNED
-#define ISHIFT_TEMPS DCTELEM ishift_temp;
-#if BITS_IN_JSAMPLE == 8
-#define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */
-#else
-#define DCTELEMBITS 32 /* DCTELEM must be 32 bits */
-#endif
-#define IRIGHT_SHIFT(x,shft) \
- ((ishift_temp = (x)) < 0 ? \
- (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
- (ishift_temp >> (shft)))
-#else
-#define ISHIFT_TEMPS
-#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
-#endif
-
-#ifdef USE_ACCURATE_ROUNDING
-#define IDESCALE(x,n) ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
-#else
-#define IDESCALE(x,n) ((int) IRIGHT_SHIFT(x, n))
-#endif
-
-
-/*
- * Perform dequantization and inverse DCT on one block of coefficients.
- */
-
-GLOBAL(void)
-jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block,
- JSAMPARRAY output_buf, JDIMENSION output_col)
-{
- DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
- DCTELEM tmp10, tmp11, tmp12, tmp13;
- DCTELEM z5, z10, z11, z12, z13;
- JCOEFPTR inptr;
- IFAST_MULT_TYPE * quantptr;
- int * wsptr;
- JSAMPROW outptr;
- JSAMPLE *range_limit = IDCT_range_limit(cinfo);
- int ctr;
- int workspace[DCTSIZE2]; /* buffers data between passes */
- SHIFT_TEMPS /* for DESCALE */
- ISHIFT_TEMPS /* for IDESCALE */
-
- /* Pass 1: process columns from input, store into work array. */
-
- inptr = coef_block;
- quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
- wsptr = workspace;
- for (ctr = DCTSIZE; ctr > 0; ctr--) {
- /* Due to quantization, we will usually find that many of the input
- * coefficients are zero, especially the AC terms. We can exploit this
- * by short-circuiting the IDCT calculation for any column in which all
- * the AC terms are zero. In that case each output is equal to the
- * DC coefficient (with scale factor as needed).
- * With typical images and quantization tables, half or more of the
- * column DCT calculations can be simplified this way.
- */
-
- if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
- inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
- inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
- inptr[DCTSIZE*7] == 0) {
- /* AC terms all zero */
- int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
-
- wsptr[DCTSIZE*0] = dcval;
- wsptr[DCTSIZE*1] = dcval;
- wsptr[DCTSIZE*2] = dcval;
- wsptr[DCTSIZE*3] = dcval;
- wsptr[DCTSIZE*4] = dcval;
- wsptr[DCTSIZE*5] = dcval;
- wsptr[DCTSIZE*6] = dcval;
- wsptr[DCTSIZE*7] = dcval;
-
- inptr++; /* advance pointers to next column */
- quantptr++;
- wsptr++;
- continue;
- }
-
- /* Even part */
-
- tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
- tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
- tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
- tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
-
- tmp10 = tmp0 + tmp2; /* phase 3 */
- tmp11 = tmp0 - tmp2;
-
- tmp13 = tmp1 + tmp3; /* phases 5-3 */
- tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
-
- tmp0 = tmp10 + tmp13; /* phase 2 */
- tmp3 = tmp10 - tmp13;
- tmp1 = tmp11 + tmp12;
- tmp2 = tmp11 - tmp12;
-
- /* Odd part */
-
- tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
- tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
- tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
- tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
-
- z13 = tmp6 + tmp5; /* phase 6 */
- z10 = tmp6 - tmp5;
- z11 = tmp4 + tmp7;
- z12 = tmp4 - tmp7;
-
- tmp7 = z11 + z13; /* phase 5 */
- tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
-
- z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
- tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
- tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
-
- tmp6 = tmp12 - tmp7; /* phase 2 */
- tmp5 = tmp11 - tmp6;
- tmp4 = tmp10 + tmp5;
-
- wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
- wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
- wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
- wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
- wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
- wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
- wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
- wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);
-
- inptr++; /* advance pointers to next column */
- quantptr++;
- wsptr++;
- }
-
- /* Pass 2: process rows from work array, store into output array. */
- /* Note that we must descale the results by a factor of 8 == 2**3, */
- /* and also undo the PASS1_BITS scaling. */
-
- wsptr = workspace;
- for (ctr = 0; ctr < DCTSIZE; ctr++) {
- outptr = output_buf[ctr] + output_col;
- /* Rows of zeroes can be exploited in the same way as we did with columns.
- * However, the column calculation has created many nonzero AC terms, so
- * the simplification applies less often (typically 5% to 10% of the time).
- * On machines with very fast multiplication, it's possible that the
- * test takes more time than it's worth. In that case this section
- * may be commented out.
- */
-
-#ifndef NO_ZERO_ROW_TEST
- if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
- wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
- /* AC terms all zero */
- JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
- & RANGE_MASK];
-
- outptr[0] = dcval;
- outptr[1] = dcval;
- outptr[2] = dcval;
- outptr[3] = dcval;
- outptr[4] = dcval;
- outptr[5] = dcval;
- outptr[6] = dcval;
- outptr[7] = dcval;
-
- wsptr += DCTSIZE; /* advance pointer to next row */
- continue;
- }
-#endif
-
- /* Even part */
-
- tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
- tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
-
- tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
- tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
- - tmp13;
-
- tmp0 = tmp10 + tmp13;
- tmp3 = tmp10 - tmp13;
- tmp1 = tmp11 + tmp12;
- tmp2 = tmp11 - tmp12;
-
- /* Odd part */
-
- z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
- z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
- z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
- z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
-
- tmp7 = z11 + z13; /* phase 5 */
- tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
-
- z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
- tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
- tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
-
- tmp6 = tmp12 - tmp7; /* phase 2 */
- tmp5 = tmp11 - tmp6;
- tmp4 = tmp10 + tmp5;
-
- /* Final output stage: scale down by a factor of 8 and range-limit */
-
- outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
- & RANGE_MASK];
- outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
- & RANGE_MASK];
- outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
- & RANGE_MASK];
- outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
- & RANGE_MASK];
- outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
- & RANGE_MASK];
- outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
- & RANGE_MASK];
- outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
- & RANGE_MASK];
- outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
- & RANGE_MASK];
-
- wsptr += DCTSIZE; /* advance pointer to next row */
- }
-}
-
-#endif /* DCT_IFAST_SUPPORTED */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jidctfst.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 a fast, not so accurate integer implementation of the
+ * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
+ * must also perform dequantization of the input coefficients.
+ *
+ * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
+ * on each row (or vice versa, but it's more convenient to emit a row at
+ * a time). Direct algorithms are also available, but they are much more
+ * complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on Arai, Agui, and Nakajima's algorithm for
+ * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
+ * Japanese, but the algorithm is described in the Pennebaker & Mitchell
+ * JPEG textbook (see REFERENCES section in file README). The following code
+ * is based directly on figure 4-8 in P&M.
+ * While an 8-point DCT cannot be done in less than 11 multiplies, it is
+ * possible to arrange the computation so that many of the multiplies are
+ * simple scalings of the final outputs. These multiplies can then be
+ * folded into the multiplications or divisions by the JPEG quantization
+ * table entries. The AA&N method leaves only 5 multiplies and 29 adds
+ * to be done in the DCT itself.
+ * The primary disadvantage of this method is that with fixed-point math,
+ * accuracy is lost due to imprecise representation of the scaled
+ * quantization values. The smaller the quantization table entry, the less
+ * precise the scaled value, so this implementation does worse with high-
+ * quality-setting files than with low-quality ones.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_IFAST_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 decisions are generally the same as in the LL&M algorithm;
+ * see jidctint.c for more details. However, we choose to descale
+ * (right shift) multiplication products as soon as they are formed,
+ * rather than carrying additional fractional bits into subsequent additions.
+ * This compromises accuracy slightly, but it lets us save a few shifts.
+ * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
+ * everywhere except in the multiplications proper; this saves a good deal
+ * of work on 16-bit-int machines.
+ *
+ * The dequantized coefficients are not integers because the AA&N scaling
+ * factors have been incorporated. We represent them scaled up by PASS1_BITS,
+ * so that the first and second IDCT rounds have the same input scaling.
+ * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
+ * avoid a descaling shift; this compromises accuracy rather drastically
+ * for small quantization table entries, but it saves a lot of shifts.
+ * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
+ * so we use a much larger scaling factor to preserve accuracy.
+ *
+ * A final compromise is to represent the multiplicative constants to only
+ * 8 fractional bits, rather than 13. This saves some shifting work on some
+ * machines, and may also reduce the cost of multiplication (since there
+ * are fewer one-bits in the constants).
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define CONST_BITS 8
+#define PASS1_BITS 2
+#else
+#define CONST_BITS 8
+#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 == 8
+#define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */
+#define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */
+#define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */
+#define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */
+#else
+#define FIX_1_082392200 FIX(1.082392200)
+#define FIX_1_414213562 FIX(1.414213562)
+#define FIX_1_847759065 FIX(1.847759065)
+#define FIX_2_613125930 FIX(2.613125930)
+#endif
+
+
+/* We can gain a little more speed, with a further compromise in accuracy,
+ * by omitting the addition in a descaling shift. This yields an incorrectly
+ * rounded result half the time...
+ */
+
+#ifndef USE_ACCURATE_ROUNDING
+#undef DESCALE
+#define DESCALE(x,n) RIGHT_SHIFT(x, n)
+#endif
+
+
+/* Multiply a DCTELEM variable by an INT32 constant, and immediately
+ * descale to yield a DCTELEM result.
+ */
+
+#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
+
+
+/* Dequantize a coefficient by multiplying it by the multiplier-table
+ * entry; produce a DCTELEM result. For 8-bit data a 16x16->16
+ * multiplication will do. For 12-bit data, the multiplier table is
+ * declared INT32, so a 32-bit multiply will be used.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+#define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval))
+#else
+#define DEQUANTIZE(coef,quantval) \
+ DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
+#endif
+
+
+/* Like DESCALE, but applies to a DCTELEM and produces an int.
+ * We assume that int right shift is unsigned if INT32 right shift is.
+ */
+
+#ifdef RIGHT_SHIFT_IS_UNSIGNED
+#define ISHIFT_TEMPS DCTELEM ishift_temp;
+#if BITS_IN_JSAMPLE == 8
+#define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */
+#else
+#define DCTELEMBITS 32 /* DCTELEM must be 32 bits */
+#endif
+#define IRIGHT_SHIFT(x,shft) \
+ ((ishift_temp = (x)) < 0 ? \
+ (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
+ (ishift_temp >> (shft)))
+#else
+#define ISHIFT_TEMPS
+#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
+#endif
+
+#ifdef USE_ACCURATE_ROUNDING
+#define IDESCALE(x,n) ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
+#else
+#define IDESCALE(x,n) ((int) IRIGHT_SHIFT(x, n))
+#endif
+
+
+/*
+ * Perform dequantization and inverse DCT on one block of coefficients.
+ */
+
+GLOBAL(void)
+jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+ DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+ DCTELEM tmp10, tmp11, tmp12, tmp13;
+ DCTELEM z5, z10, z11, z12, z13;
+ JCOEFPTR inptr;
+ IFAST_MULT_TYPE * quantptr;
+ int * wsptr;
+ JSAMPROW outptr;
+ JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+ int ctr;
+ int workspace[DCTSIZE2]; /* buffers data between passes */
+ SHIFT_TEMPS /* for DESCALE */
+ ISHIFT_TEMPS /* for IDESCALE */
+
+ /* Pass 1: process columns from input, store into work array. */
+
+ inptr = coef_block;
+ quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
+ wsptr = workspace;
+ for (ctr = DCTSIZE; ctr > 0; ctr--) {
+ /* Due to quantization, we will usually find that many of the input
+ * coefficients are zero, especially the AC terms. We can exploit this
+ * by short-circuiting the IDCT calculation for any column in which all
+ * the AC terms are zero. In that case each output is equal to the
+ * DC coefficient (with scale factor as needed).
+ * With typical images and quantization tables, half or more of the
+ * column DCT calculations can be simplified this way.
+ */
+
+ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
+ inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+ inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+ inptr[DCTSIZE*7] == 0) {
+ /* AC terms all zero */
+ int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+
+ wsptr[DCTSIZE*0] = dcval;
+ wsptr[DCTSIZE*1] = dcval;
+ wsptr[DCTSIZE*2] = dcval;
+ wsptr[DCTSIZE*3] = dcval;
+ wsptr[DCTSIZE*4] = dcval;
+ wsptr[DCTSIZE*5] = dcval;
+ wsptr[DCTSIZE*6] = dcval;
+ wsptr[DCTSIZE*7] = dcval;
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ continue;
+ }
+
+ /* Even part */
+
+ tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+ tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+ tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
+ tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+ tmp10 = tmp0 + tmp2; /* phase 3 */
+ tmp11 = tmp0 - tmp2;
+
+ tmp13 = tmp1 + tmp3; /* phases 5-3 */
+ tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
+
+ tmp0 = tmp10 + tmp13; /* phase 2 */
+ tmp3 = tmp10 - tmp13;
+ tmp1 = tmp11 + tmp12;
+ tmp2 = tmp11 - tmp12;
+
+ /* Odd part */
+
+ tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+ tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+ tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+ tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+
+ z13 = tmp6 + tmp5; /* phase 6 */
+ z10 = tmp6 - tmp5;
+ z11 = tmp4 + tmp7;
+ z12 = tmp4 - tmp7;
+
+ tmp7 = z11 + z13; /* phase 5 */
+ tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
+
+ z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
+ tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
+ tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
+
+ tmp6 = tmp12 - tmp7; /* phase 2 */
+ tmp5 = tmp11 - tmp6;
+ tmp4 = tmp10 + tmp5;
+
+ wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
+ wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
+ wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
+ wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
+ wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
+ wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
+ wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
+ wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ }
+
+ /* Pass 2: process rows from work array, store into output array. */
+ /* Note that we must descale the results by a factor of 8 == 2**3, */
+ /* and also undo the PASS1_BITS scaling. */
+
+ wsptr = workspace;
+ for (ctr = 0; ctr < DCTSIZE; ctr++) {
+ outptr = output_buf[ctr] + output_col;
+ /* Rows of zeroes can be exploited in the same way as we did with columns.
+ * However, the column calculation has created many nonzero AC terms, so
+ * the simplification applies less often (typically 5% to 10% of the time).
+ * On machines with very fast multiplication, it's possible that the
+ * test takes more time than it's worth. In that case this section
+ * may be commented out.
+ */
+
+#ifndef NO_ZERO_ROW_TEST
+ if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
+ wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
+ /* AC terms all zero */
+ JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
+ & RANGE_MASK];
+
+ outptr[0] = dcval;
+ outptr[1] = dcval;
+ outptr[2] = dcval;
+ outptr[3] = dcval;
+ outptr[4] = dcval;
+ outptr[5] = dcval;
+ outptr[6] = dcval;
+ outptr[7] = dcval;
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ continue;
+ }
+#endif
+
+ /* Even part */
+
+ tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
+ tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
+
+ tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
+ tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
+ - tmp13;
+
+ tmp0 = tmp10 + tmp13;
+ tmp3 = tmp10 - tmp13;
+ tmp1 = tmp11 + tmp12;
+ tmp2 = tmp11 - tmp12;
+
+ /* Odd part */
+
+ z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
+ z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
+ z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
+ z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
+
+ tmp7 = z11 + z13; /* phase 5 */
+ tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
+
+ z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
+ tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
+ tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
+
+ tmp6 = tmp12 - tmp7; /* phase 2 */
+ tmp5 = tmp11 - tmp6;
+ tmp4 = tmp10 + tmp5;
+
+ /* Final output stage: scale down by a factor of 8 and range-limit */
+
+ outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
+ & RANGE_MASK];
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ }
+}
+
+#endif /* DCT_IFAST_SUPPORTED */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jidctint.c b/core/src/fxcodec/libjpeg/fpdfapi_jidctint.c
index 82f5cc742a..78a8d66552 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jidctint.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jidctint.c
@@ -1,392 +1,392 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jidctint.c
- *
- * Copyright (C) 1991-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 a slow-but-accurate integer implementation of the
- * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
- * must also perform dequantization of the input coefficients.
- *
- * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
- * on each row (or vice versa, but it's more convenient to emit a row at
- * a time). Direct algorithms are also available, but they are much more
- * complex and seem not to be any faster when reduced to code.
- *
- * This implementation is based on an algorithm described in
- * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
- * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
- * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
- * The primary algorithm described there uses 11 multiplies and 29 adds.
- * We use their alternate method with 12 multiplies and 32 adds.
- * The advantage of this method is that no data path contains more than one
- * multiplication; this allows a very simple and accurate implementation in
- * scaled fixed-point arithmetic, with a minimal number of shifts.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jdct.h" /* Private declarations for DCT subsystem */
-
-#ifdef DCT_ISLOW_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
-
-
-/*
- * The poop on this scaling stuff is as follows:
- *
- * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
- * larger than the true IDCT outputs. The final outputs are therefore
- * a factor of N larger than desired; since N=8 this can be cured by
- * a simple right shift at the end of the algorithm. The advantage of
- * this arrangement is that we save two multiplications per 1-D IDCT,
- * because the y0 and y4 inputs need not be divided by sqrt(N).
- *
- * We have to do addition and subtraction of the integer inputs, which
- * is no problem, and multiplication by fractional constants, which is
- * a problem to do in integer arithmetic. We multiply all the constants
- * by CONST_SCALE and convert them to integer constants (thus retaining
- * CONST_BITS bits of precision in the constants). After doing a
- * multiplication we have to divide the product by CONST_SCALE, with proper
- * rounding, to produce the correct output. This division can be done
- * cheaply as a right shift of CONST_BITS bits. We postpone shifting
- * as long as possible so that partial sums can be added together with
- * full fractional precision.
- *
- * The outputs of the first pass are scaled up by PASS1_BITS bits so that
- * they are represented to better-than-integral precision. These outputs
- * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
- * with the recommended scaling. (To scale up 12-bit sample data further, an
- * intermediate INT32 array would be needed.)
- *
- * To avoid overflow of the 32-bit intermediate results in pass 2, we must
- * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
- * shows that the values given below are the most effective.
- */
-
-#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_298631336 ((INT32) 2446) /* FIX(0.298631336) */
-#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */
-#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
-#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
-#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
-#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */
-#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */
-#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
-#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */
-#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */
-#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
-#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */
-#else
-#define FIX_0_298631336 FIX(0.298631336)
-#define FIX_0_390180644 FIX(0.390180644)
-#define FIX_0_541196100 FIX(0.541196100)
-#define FIX_0_765366865 FIX(0.765366865)
-#define FIX_0_899976223 FIX(0.899976223)
-#define FIX_1_175875602 FIX(1.175875602)
-#define FIX_1_501321110 FIX(1.501321110)
-#define FIX_1_847759065 FIX(1.847759065)
-#define FIX_1_961570560 FIX(1.961570560)
-#define FIX_2_053119869 FIX(2.053119869)
-#define FIX_2_562915447 FIX(2.562915447)
-#define FIX_3_072711026 FIX(3.072711026)
-#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.
- */
-
-GLOBAL(void)
-jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block,
- JSAMPARRAY output_buf, JDIMENSION output_col)
-{
- INT32 tmp0, tmp1, tmp2, tmp3;
- INT32 tmp10, tmp11, tmp12, tmp13;
- INT32 z1, z2, z3, z4, z5;
- JCOEFPTR inptr;
- ISLOW_MULT_TYPE * quantptr;
- int * wsptr;
- JSAMPROW outptr;
- JSAMPLE *range_limit = IDCT_range_limit(cinfo);
- int ctr;
- int workspace[DCTSIZE2]; /* buffers data between passes */
- SHIFT_TEMPS
-
- /* Pass 1: process columns from input, store into work array. */
- /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
- /* furthermore, we scale the results by 2**PASS1_BITS. */
-
- inptr = coef_block;
- quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
- wsptr = workspace;
- for (ctr = DCTSIZE; ctr > 0; ctr--) {
- /* Due to quantization, we will usually find that many of the input
- * coefficients are zero, especially the AC terms. We can exploit this
- * by short-circuiting the IDCT calculation for any column in which all
- * the AC terms are zero. In that case each output is equal to the
- * DC coefficient (with scale factor as needed).
- * With typical images and quantization tables, half or more of the
- * column DCT calculations can be simplified this way.
- */
-
- if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
- inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
- inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
- inptr[DCTSIZE*7] == 0) {
- /* AC terms all zero */
- 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;
- wsptr[DCTSIZE*4] = dcval;
- wsptr[DCTSIZE*5] = dcval;
- wsptr[DCTSIZE*6] = dcval;
- wsptr[DCTSIZE*7] = dcval;
-
- inptr++; /* advance pointers to next column */
- quantptr++;
- wsptr++;
- continue;
- }
-
- /* Even part: reverse the even part of the forward DCT. */
- /* The rotator is sqrt(2)*c(-6). */
-
- z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
- z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
-
- z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
- tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
- tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
-
- z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
- z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
-
- tmp0 = (z2 + z3) << CONST_BITS;
- tmp1 = (z2 - z3) << CONST_BITS;
-
- tmp10 = tmp0 + tmp3;
- tmp13 = tmp0 - tmp3;
- tmp11 = tmp1 + tmp2;
- tmp12 = tmp1 - tmp2;
-
- /* Odd part per figure 8; the matrix is unitary and hence its
- * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
- */
-
- tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
- tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
- tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
- tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
-
- z1 = tmp0 + tmp3;
- z2 = tmp1 + tmp2;
- z3 = tmp0 + tmp2;
- z4 = tmp1 + tmp3;
- z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
-
- tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
- tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
- tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
- tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
- z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
- z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
- z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
- z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
-
- z3 += z5;
- z4 += z5;
-
- tmp0 += z1 + z3;
- tmp1 += z2 + z4;
- tmp2 += z2 + z3;
- tmp3 += z1 + z4;
-
- /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
-
- wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
- wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
- wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
- wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
- wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
- wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
- wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
- wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
-
- inptr++; /* advance pointers to next column */
- quantptr++;
- wsptr++;
- }
-
- /* Pass 2: process rows from work array, store into output array. */
- /* Note that we must descale the results by a factor of 8 == 2**3, */
- /* and also undo the PASS1_BITS scaling. */
-
- wsptr = workspace;
- for (ctr = 0; ctr < DCTSIZE; ctr++) {
- outptr = output_buf[ctr] + output_col;
- /* Rows of zeroes can be exploited in the same way as we did with columns.
- * However, the column calculation has created many nonzero AC terms, so
- * the simplification applies less often (typically 5% to 10% of the time).
- * On machines with very fast multiplication, it's possible that the
- * test takes more time than it's worth. In that case this section
- * may be commented out.
- */
-
-#ifndef NO_ZERO_ROW_TEST
- if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 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;
- outptr[4] = dcval;
- outptr[5] = dcval;
- outptr[6] = dcval;
- outptr[7] = dcval;
-
- wsptr += DCTSIZE; /* advance pointer to next row */
- continue;
- }
-#endif
-
- /* Even part: reverse the even part of the forward DCT. */
- /* The rotator is sqrt(2)*c(-6). */
-
- z2 = (INT32) wsptr[2];
- z3 = (INT32) wsptr[6];
-
- z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
- tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
- tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
-
- tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
- tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;
-
- tmp10 = tmp0 + tmp3;
- tmp13 = tmp0 - tmp3;
- tmp11 = tmp1 + tmp2;
- tmp12 = tmp1 - tmp2;
-
- /* Odd part per figure 8; the matrix is unitary and hence its
- * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
- */
-
- tmp0 = (INT32) wsptr[7];
- tmp1 = (INT32) wsptr[5];
- tmp2 = (INT32) wsptr[3];
- tmp3 = (INT32) wsptr[1];
-
- z1 = tmp0 + tmp3;
- z2 = tmp1 + tmp2;
- z3 = tmp0 + tmp2;
- z4 = tmp1 + tmp3;
- z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
-
- tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
- tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
- tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
- tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
- z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
- z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
- z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
- z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
-
- z3 += z5;
- z4 += z5;
-
- tmp0 += z1 + z3;
- tmp1 += z2 + z4;
- tmp2 += z2 + z3;
- tmp3 += z1 + z4;
-
- /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
-
- outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
- CONST_BITS+PASS1_BITS+3)
- & RANGE_MASK];
- outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
- CONST_BITS+PASS1_BITS+3)
- & RANGE_MASK];
- outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
- CONST_BITS+PASS1_BITS+3)
- & RANGE_MASK];
- outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
- CONST_BITS+PASS1_BITS+3)
- & RANGE_MASK];
- outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
- CONST_BITS+PASS1_BITS+3)
- & RANGE_MASK];
- outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
- CONST_BITS+PASS1_BITS+3)
- & RANGE_MASK];
- outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
- CONST_BITS+PASS1_BITS+3)
- & RANGE_MASK];
- outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
- CONST_BITS+PASS1_BITS+3)
- & RANGE_MASK];
-
- wsptr += DCTSIZE; /* advance pointer to next row */
- }
-}
-
-#endif /* DCT_ISLOW_SUPPORTED */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jidctint.c
+ *
+ * Copyright (C) 1991-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 a slow-but-accurate integer implementation of the
+ * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
+ * must also perform dequantization of the input coefficients.
+ *
+ * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
+ * on each row (or vice versa, but it's more convenient to emit a row at
+ * a time). Direct algorithms are also available, but they are much more
+ * complex and seem not to be any faster when reduced to code.
+ *
+ * This implementation is based on an algorithm described in
+ * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
+ * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
+ * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
+ * The primary algorithm described there uses 11 multiplies and 29 adds.
+ * We use their alternate method with 12 multiplies and 32 adds.
+ * The advantage of this method is that no data path contains more than one
+ * multiplication; this allows a very simple and accurate implementation in
+ * scaled fixed-point arithmetic, with a minimal number of shifts.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jdct.h" /* Private declarations for DCT subsystem */
+
+#ifdef DCT_ISLOW_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
+
+
+/*
+ * The poop on this scaling stuff is as follows:
+ *
+ * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
+ * larger than the true IDCT outputs. The final outputs are therefore
+ * a factor of N larger than desired; since N=8 this can be cured by
+ * a simple right shift at the end of the algorithm. The advantage of
+ * this arrangement is that we save two multiplications per 1-D IDCT,
+ * because the y0 and y4 inputs need not be divided by sqrt(N).
+ *
+ * We have to do addition and subtraction of the integer inputs, which
+ * is no problem, and multiplication by fractional constants, which is
+ * a problem to do in integer arithmetic. We multiply all the constants
+ * by CONST_SCALE and convert them to integer constants (thus retaining
+ * CONST_BITS bits of precision in the constants). After doing a
+ * multiplication we have to divide the product by CONST_SCALE, with proper
+ * rounding, to produce the correct output. This division can be done
+ * cheaply as a right shift of CONST_BITS bits. We postpone shifting
+ * as long as possible so that partial sums can be added together with
+ * full fractional precision.
+ *
+ * The outputs of the first pass are scaled up by PASS1_BITS bits so that
+ * they are represented to better-than-integral precision. These outputs
+ * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
+ * with the recommended scaling. (To scale up 12-bit sample data further, an
+ * intermediate INT32 array would be needed.)
+ *
+ * To avoid overflow of the 32-bit intermediate results in pass 2, we must
+ * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
+ * shows that the values given below are the most effective.
+ */
+
+#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_298631336 ((INT32) 2446) /* FIX(0.298631336) */
+#define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */
+#define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
+#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
+#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
+#define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */
+#define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */
+#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
+#define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */
+#define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */
+#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
+#define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */
+#else
+#define FIX_0_298631336 FIX(0.298631336)
+#define FIX_0_390180644 FIX(0.390180644)
+#define FIX_0_541196100 FIX(0.541196100)
+#define FIX_0_765366865 FIX(0.765366865)
+#define FIX_0_899976223 FIX(0.899976223)
+#define FIX_1_175875602 FIX(1.175875602)
+#define FIX_1_501321110 FIX(1.501321110)
+#define FIX_1_847759065 FIX(1.847759065)
+#define FIX_1_961570560 FIX(1.961570560)
+#define FIX_2_053119869 FIX(2.053119869)
+#define FIX_2_562915447 FIX(2.562915447)
+#define FIX_3_072711026 FIX(3.072711026)
+#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.
+ */
+
+GLOBAL(void)
+jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col)
+{
+ INT32 tmp0, tmp1, tmp2, tmp3;
+ INT32 tmp10, tmp11, tmp12, tmp13;
+ INT32 z1, z2, z3, z4, z5;
+ JCOEFPTR inptr;
+ ISLOW_MULT_TYPE * quantptr;
+ int * wsptr;
+ JSAMPROW outptr;
+ JSAMPLE *range_limit = IDCT_range_limit(cinfo);
+ int ctr;
+ int workspace[DCTSIZE2]; /* buffers data between passes */
+ SHIFT_TEMPS
+
+ /* Pass 1: process columns from input, store into work array. */
+ /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
+ /* furthermore, we scale the results by 2**PASS1_BITS. */
+
+ inptr = coef_block;
+ quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
+ wsptr = workspace;
+ for (ctr = DCTSIZE; ctr > 0; ctr--) {
+ /* Due to quantization, we will usually find that many of the input
+ * coefficients are zero, especially the AC terms. We can exploit this
+ * by short-circuiting the IDCT calculation for any column in which all
+ * the AC terms are zero. In that case each output is equal to the
+ * DC coefficient (with scale factor as needed).
+ * With typical images and quantization tables, half or more of the
+ * column DCT calculations can be simplified this way.
+ */
+
+ if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
+ inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
+ inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
+ inptr[DCTSIZE*7] == 0) {
+ /* AC terms all zero */
+ 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;
+ wsptr[DCTSIZE*4] = dcval;
+ wsptr[DCTSIZE*5] = dcval;
+ wsptr[DCTSIZE*6] = dcval;
+ wsptr[DCTSIZE*7] = dcval;
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ continue;
+ }
+
+ /* Even part: reverse the even part of the forward DCT. */
+ /* The rotator is sqrt(2)*c(-6). */
+
+ z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
+ z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
+
+ z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
+ tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
+ tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
+
+ z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
+ z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
+
+ tmp0 = (z2 + z3) << CONST_BITS;
+ tmp1 = (z2 - z3) << CONST_BITS;
+
+ tmp10 = tmp0 + tmp3;
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ /* Odd part per figure 8; the matrix is unitary and hence its
+ * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
+ */
+
+ tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
+ tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
+ tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
+ tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
+
+ z1 = tmp0 + tmp3;
+ z2 = tmp1 + tmp2;
+ z3 = tmp0 + tmp2;
+ z4 = tmp1 + tmp3;
+ z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+
+ tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+ tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+ tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+ tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+
+ z3 += z5;
+ z4 += z5;
+
+ tmp0 += z1 + z3;
+ tmp1 += z2 + z4;
+ tmp2 += z2 + z3;
+ tmp3 += z1 + z4;
+
+ /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
+
+ wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
+ wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
+
+ inptr++; /* advance pointers to next column */
+ quantptr++;
+ wsptr++;
+ }
+
+ /* Pass 2: process rows from work array, store into output array. */
+ /* Note that we must descale the results by a factor of 8 == 2**3, */
+ /* and also undo the PASS1_BITS scaling. */
+
+ wsptr = workspace;
+ for (ctr = 0; ctr < DCTSIZE; ctr++) {
+ outptr = output_buf[ctr] + output_col;
+ /* Rows of zeroes can be exploited in the same way as we did with columns.
+ * However, the column calculation has created many nonzero AC terms, so
+ * the simplification applies less often (typically 5% to 10% of the time).
+ * On machines with very fast multiplication, it's possible that the
+ * test takes more time than it's worth. In that case this section
+ * may be commented out.
+ */
+
+#ifndef NO_ZERO_ROW_TEST
+ if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 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;
+ outptr[4] = dcval;
+ outptr[5] = dcval;
+ outptr[6] = dcval;
+ outptr[7] = dcval;
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ continue;
+ }
+#endif
+
+ /* Even part: reverse the even part of the forward DCT. */
+ /* The rotator is sqrt(2)*c(-6). */
+
+ z2 = (INT32) wsptr[2];
+ z3 = (INT32) wsptr[6];
+
+ z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
+ tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
+ tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
+
+ tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
+ tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;
+
+ tmp10 = tmp0 + tmp3;
+ tmp13 = tmp0 - tmp3;
+ tmp11 = tmp1 + tmp2;
+ tmp12 = tmp1 - tmp2;
+
+ /* Odd part per figure 8; the matrix is unitary and hence its
+ * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
+ */
+
+ tmp0 = (INT32) wsptr[7];
+ tmp1 = (INT32) wsptr[5];
+ tmp2 = (INT32) wsptr[3];
+ tmp3 = (INT32) wsptr[1];
+
+ z1 = tmp0 + tmp3;
+ z2 = tmp1 + tmp2;
+ z3 = tmp0 + tmp2;
+ z4 = tmp1 + tmp3;
+ z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
+
+ tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
+ tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
+ tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
+ tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
+ z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
+ z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
+ z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
+ z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
+
+ z3 += z5;
+ z4 += z5;
+
+ tmp0 += z1 + z3;
+ tmp1 += z2 + z4;
+ tmp2 += z2 + z3;
+ tmp3 += z1 + z4;
+
+ /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
+
+ outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+ outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
+ CONST_BITS+PASS1_BITS+3)
+ & RANGE_MASK];
+
+ wsptr += DCTSIZE; /* advance pointer to next row */
+ }
+}
+
+#endif /* DCT_ISLOW_SUPPORTED */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jidctred.c b/core/src/fxcodec/libjpeg/fpdfapi_jidctred.c
index 7eb1d74afa..8b4b807192 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jidctred.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jidctred.c
@@ -1,401 +1,401 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * 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 */
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * 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 */
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c b/core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c
index 19ee66597b..630102fc2e 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jmemmgr.c
@@ -1,1123 +1,1123 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jmemmgr.c
- *
- * 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 the JPEG system-independent memory management
- * routines. This code is usable across a wide variety of machines; most
- * of the system dependencies have been isolated in a separate file.
- * The major functions provided here are:
- * * pool-based allocation and freeing of memory;
- * * policy decisions about how to divide available memory among the
- * virtual arrays;
- * * control logic for swapping virtual arrays between main memory and
- * backing storage.
- * The separate system-dependent file provides the actual backing-storage
- * access code, and it contains the policy decision about how much total
- * main memory to use.
- * This file is system-dependent in the sense that some of its functions
- * are unnecessary in some systems. For example, if there is enough virtual
- * memory so that backing storage will never be used, much of the virtual
- * array control logic could be removed. (Of course, if you have that much
- * memory then you shouldn't care about a little bit of unused code...)
- */
-
-#define JPEG_INTERNALS
-#define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */
-#include "jinclude.h"
-#include "jpeglib.h"
-#include "jmemsys.h" /* import the system-dependent declarations */
-
-#define NO_GETENV /* XYQ: 2007-5-22 Don't use it */
-
-#ifndef NO_GETENV
-#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */
-extern char * getenv JPP((const char * name));
-#endif
-#endif
-
-
-/*
- * Some important notes:
- * The allocation routines provided here must never return NULL.
- * They should exit to error_exit if unsuccessful.
- *
- * It's not a good idea to try to merge the sarray and barray routines,
- * even though they are textually almost the same, because samples are
- * usually stored as bytes while coefficients are shorts or ints. Thus,
- * in machines where byte pointers have a different representation from
- * word pointers, the resulting machine code could not be the same.
- */
-
-
-/*
- * Many machines require storage alignment: longs must start on 4-byte
- * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
- * always returns pointers that are multiples of the worst-case alignment
- * requirement, and we had better do so too.
- * There isn't any really portable way to determine the worst-case alignment
- * requirement. This module assumes that the alignment requirement is
- * multiples of sizeof(ALIGN_TYPE).
- * By default, we define ALIGN_TYPE as double. This is necessary on some
- * workstations (where doubles really do need 8-byte alignment) and will work
- * fine on nearly everything. If your machine has lesser alignment needs,
- * you can save a few bytes by making ALIGN_TYPE smaller.
- * The only place I know of where this will NOT work is certain Macintosh
- * 680x0 compilers that define double as a 10-byte IEEE extended float.
- * Doing 10-byte alignment is counterproductive because longwords won't be
- * aligned well. Put "#define ALIGN_TYPE long" in jconfig.h if you have
- * such a compiler.
- */
-
-#ifndef ALIGN_TYPE /* so can override from jconfig.h */
-#define ALIGN_TYPE double
-#endif
-
-
-/*
- * We allocate objects from "pools", where each pool is gotten with a single
- * request to jpeg_get_small() or jpeg_get_large(). There is no per-object
- * overhead within a pool, except for alignment padding. Each pool has a
- * header with a link to the next pool of the same class.
- * Small and large pool headers are identical except that the latter's
- * link pointer must be FAR on 80x86 machines.
- * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
- * field. This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
- * of the alignment requirement of ALIGN_TYPE.
- */
-
-typedef union small_pool_struct * small_pool_ptr;
-
-typedef union small_pool_struct {
- struct {
- small_pool_ptr next; /* next in list of pools */
- size_t bytes_used; /* how many bytes already used within pool */
- size_t bytes_left; /* bytes still available in this pool */
- } hdr;
- ALIGN_TYPE dummy; /* included in union to ensure alignment */
-} small_pool_hdr;
-
-typedef union large_pool_struct FAR * large_pool_ptr;
-
-typedef union large_pool_struct {
- struct {
- large_pool_ptr next; /* next in list of pools */
- size_t bytes_used; /* how many bytes already used within pool */
- size_t bytes_left; /* bytes still available in this pool */
- } hdr;
- ALIGN_TYPE dummy; /* included in union to ensure alignment */
-} large_pool_hdr;
-
-
-/*
- * Here is the full definition of a memory manager object.
- */
-
-typedef struct {
- struct jpeg_memory_mgr pub; /* public fields */
-
- /* Each pool identifier (lifetime class) names a linked list of pools. */
- small_pool_ptr small_list[JPOOL_NUMPOOLS];
- large_pool_ptr large_list[JPOOL_NUMPOOLS];
-
- /* Since we only have one lifetime class of virtual arrays, only one
- * linked list is necessary (for each datatype). Note that the virtual
- * array control blocks being linked together are actually stored somewhere
- * in the small-pool list.
- */
- jvirt_sarray_ptr virt_sarray_list;
- jvirt_barray_ptr virt_barray_list;
-
- /* This counts total space obtained from jpeg_get_small/large */
- long total_space_allocated;
-
- /* alloc_sarray and alloc_barray set this value for use by virtual
- * array routines.
- */
- JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
-} my_memory_mgr;
-
-typedef my_memory_mgr * my_mem_ptr;
-
-
-/*
- * The control blocks for virtual arrays.
- * Note that these blocks are allocated in the "small" pool area.
- * System-dependent info for the associated backing store (if any) is hidden
- * inside the backing_store_info struct.
- */
-
-struct jvirt_sarray_control {
- JSAMPARRAY mem_buffer; /* => the in-memory buffer */
- JDIMENSION rows_in_array; /* total virtual array height */
- JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
- JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */
- JDIMENSION rows_in_mem; /* height of memory buffer */
- JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
- JDIMENSION cur_start_row; /* first logical row # in the buffer */
- JDIMENSION first_undef_row; /* row # of first uninitialized row */
- boolean pre_zero; /* pre-zero mode requested? */
- boolean dirty; /* do current buffer contents need written? */
- boolean b_s_open; /* is backing-store data valid? */
- jvirt_sarray_ptr next; /* link to next virtual sarray control block */
- backing_store_info b_s_info; /* System-dependent control info */
-};
-
-struct jvirt_barray_control {
- JBLOCKARRAY mem_buffer; /* => the in-memory buffer */
- JDIMENSION rows_in_array; /* total virtual array height */
- JDIMENSION blocksperrow; /* width of array (and of memory buffer) */
- JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */
- JDIMENSION rows_in_mem; /* height of memory buffer */
- JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
- JDIMENSION cur_start_row; /* first logical row # in the buffer */
- JDIMENSION first_undef_row; /* row # of first uninitialized row */
- boolean pre_zero; /* pre-zero mode requested? */
- boolean dirty; /* do current buffer contents need written? */
- boolean b_s_open; /* is backing-store data valid? */
- jvirt_barray_ptr next; /* link to next virtual barray control block */
- backing_store_info b_s_info; /* System-dependent control info */
-};
-
-
-#ifdef MEM_STATS /* optional extra stuff for statistics */
-
-LOCAL(void)
-print_mem_stats (j_common_ptr cinfo, int pool_id)
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- small_pool_ptr shdr_ptr;
- large_pool_ptr lhdr_ptr;
-
- /* Since this is only a debugging stub, we can cheat a little by using
- * fprintf directly rather than going through the trace message code.
- * This is helpful because message parm array can't handle longs.
- */
- FXSYS_fprintf(stderr, "Freeing pool %d, total space = %ld\n",
- pool_id, mem->total_space_allocated);
-
- for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
- lhdr_ptr = lhdr_ptr->hdr.next) {
- FXSYS_fprintf(stderr, " Large chunk used %ld\n",
- (long) lhdr_ptr->hdr.bytes_used);
- }
-
- for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
- shdr_ptr = shdr_ptr->hdr.next) {
- FXSYS_fprintf(stderr, " Small chunk used %ld free %ld\n",
- (long) shdr_ptr->hdr.bytes_used,
- (long) shdr_ptr->hdr.bytes_left);
- }
-}
-
-#endif /* MEM_STATS */
-
-
-LOCAL(void)
-out_of_memory (j_common_ptr cinfo, int which)
-/* Report an out-of-memory error and stop execution */
-/* If we compiled MEM_STATS support, report alloc requests before dying */
-{
-#ifdef MEM_STATS
- cinfo->err->trace_level = 2; /* force self_destruct to report stats */
-#endif
- ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
-}
-
-
-/*
- * Allocation of "small" objects.
- *
- * For these, we use pooled storage. When a new pool must be created,
- * we try to get enough space for the current request plus a "slop" factor,
- * where the slop will be the amount of leftover space in the new pool.
- * The speed vs. space tradeoff is largely determined by the slop values.
- * A different slop value is provided for each pool class (lifetime),
- * and we also distinguish the first pool of a class from later ones.
- * NOTE: the values given work fairly well on both 16- and 32-bit-int
- * machines, but may be too small if longs are 64 bits or more.
- */
-
-static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
-{
- 1600, /* first PERMANENT pool */
- 16000 /* first IMAGE pool */
-};
-
-static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
-{
- 0, /* additional PERMANENT pools */
- 5000 /* additional IMAGE pools */
-};
-
-#define MIN_SLOP 50 /* greater than 0 to avoid futile looping */
-
-
-METHODDEF(void *)
-alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
-/* Allocate a "small" object */
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- small_pool_ptr hdr_ptr, prev_hdr_ptr;
- char * data_ptr;
- size_t odd_bytes, min_request, slop;
-
- /* Check for unsatisfiable request (do now to ensure no overflow below) */
- if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
- out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
-
- /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
- odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
- if (odd_bytes > 0)
- sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
-
- /* See if space is available in any existing pool */
- if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
- ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
- prev_hdr_ptr = NULL;
- hdr_ptr = mem->small_list[pool_id];
- while (hdr_ptr != NULL) {
- if (hdr_ptr->hdr.bytes_left >= sizeofobject)
- break; /* found pool with enough space */
- prev_hdr_ptr = hdr_ptr;
- hdr_ptr = hdr_ptr->hdr.next;
- }
-
- /* Time to make a new pool? */
- if (hdr_ptr == NULL) {
- /* min_request is what we need now, slop is what will be leftover */
- min_request = sizeofobject + SIZEOF(small_pool_hdr);
- if (prev_hdr_ptr == NULL) /* first pool in class? */
- slop = first_pool_slop[pool_id];
- else
- slop = extra_pool_slop[pool_id];
- /* Don't ask for more than MAX_ALLOC_CHUNK */
- if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
- slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
- /* Try to get space, if fail reduce slop and try again */
- for (;;) {
- hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
- if (hdr_ptr != NULL)
- break;
- slop /= 2;
- if (slop < MIN_SLOP) /* give up when it gets real small */
- out_of_memory(cinfo, 2); /* jpeg_get_small failed */
- }
- mem->total_space_allocated += min_request + slop;
- /* Success, initialize the new pool header and add to end of list */
- hdr_ptr->hdr.next = NULL;
- hdr_ptr->hdr.bytes_used = 0;
- hdr_ptr->hdr.bytes_left = sizeofobject + slop;
- if (prev_hdr_ptr == NULL) /* first pool in class? */
- mem->small_list[pool_id] = hdr_ptr;
- else
- prev_hdr_ptr->hdr.next = hdr_ptr;
- }
-
- /* OK, allocate the object from the current pool */
- data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
- data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
- hdr_ptr->hdr.bytes_used += sizeofobject;
- hdr_ptr->hdr.bytes_left -= sizeofobject;
-
- return (void *) data_ptr;
-}
-
-
-/*
- * Allocation of "large" objects.
- *
- * The external semantics of these are the same as "small" objects,
- * except that FAR pointers are used on 80x86. However the pool
- * management heuristics are quite different. We assume that each
- * request is large enough that it may as well be passed directly to
- * jpeg_get_large; the pool management just links everything together
- * so that we can free it all on demand.
- * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
- * structures. The routines that create these structures (see below)
- * deliberately bunch rows together to ensure a large request size.
- */
-
-METHODDEF(void FAR *)
-alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
-/* Allocate a "large" object */
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- large_pool_ptr hdr_ptr;
- size_t odd_bytes;
-
- /* Check for unsatisfiable request (do now to ensure no overflow below) */
- if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
- out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
-
- /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
- odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
- if (odd_bytes > 0)
- sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
-
- /* Always make a new pool */
- if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
- ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
-
- hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
- SIZEOF(large_pool_hdr));
- if (hdr_ptr == NULL)
- out_of_memory(cinfo, 4); /* jpeg_get_large failed */
- mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
-
- /* Success, initialize the new pool header and add to list */
- hdr_ptr->hdr.next = mem->large_list[pool_id];
- /* We maintain space counts in each pool header for statistical purposes,
- * even though they are not needed for allocation.
- */
- hdr_ptr->hdr.bytes_used = sizeofobject;
- hdr_ptr->hdr.bytes_left = 0;
- mem->large_list[pool_id] = hdr_ptr;
-
- return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
-}
-
-
-/*
- * Creation of 2-D sample arrays.
- * The pointers are in near heap, the samples themselves in FAR heap.
- *
- * To minimize allocation overhead and to allow I/O of large contiguous
- * blocks, we allocate the sample rows in groups of as many rows as possible
- * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
- * NB: the virtual array control routines, later in this file, know about
- * this chunking of rows. The rowsperchunk value is left in the mem manager
- * object so that it can be saved away if this sarray is the workspace for
- * a virtual array.
- */
-
-METHODDEF(JSAMPARRAY)
-alloc_sarray (j_common_ptr cinfo, int pool_id,
- JDIMENSION samplesperrow, JDIMENSION numrows)
-/* Allocate a 2-D sample array */
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- JSAMPARRAY result;
- JSAMPROW workspace;
- JDIMENSION rowsperchunk, currow, i;
- long ltemp;
-
- /* Calculate max # of rows allowed in one allocation chunk */
- ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
- ((long) samplesperrow * SIZEOF(JSAMPLE));
- if (ltemp <= 0)
- ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
- if (ltemp < (long) numrows)
- rowsperchunk = (JDIMENSION) ltemp;
- else
- rowsperchunk = numrows;
- mem->last_rowsperchunk = rowsperchunk;
-
- /* Get space for row pointers (small object) */
- result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
- (size_t) (numrows * SIZEOF(JSAMPROW)));
-
- /* Get the rows themselves (large objects) */
- currow = 0;
- while (currow < numrows) {
- rowsperchunk = MIN(rowsperchunk, numrows - currow);
- workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
- (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
- * SIZEOF(JSAMPLE)));
- for (i = rowsperchunk; i > 0; i--) {
- result[currow++] = workspace;
- workspace += samplesperrow;
- }
- }
-
- return result;
-}
-
-
-/*
- * Creation of 2-D coefficient-block arrays.
- * This is essentially the same as the code for sample arrays, above.
- */
-
-METHODDEF(JBLOCKARRAY)
-alloc_barray (j_common_ptr cinfo, int pool_id,
- JDIMENSION blocksperrow, JDIMENSION numrows)
-/* Allocate a 2-D coefficient-block array */
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- JBLOCKARRAY result;
- JBLOCKROW workspace;
- JDIMENSION rowsperchunk, currow, i;
- long ltemp;
-
- /* Calculate max # of rows allowed in one allocation chunk */
- ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
- ((long) blocksperrow * SIZEOF(JBLOCK));
- if (ltemp <= 0)
- ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
- if (ltemp < (long) numrows)
- rowsperchunk = (JDIMENSION) ltemp;
- else
- rowsperchunk = numrows;
- mem->last_rowsperchunk = rowsperchunk;
-
- /* Get space for row pointers (small object) */
- result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
- (size_t) (numrows * SIZEOF(JBLOCKROW)));
-
- /* Get the rows themselves (large objects) */
- currow = 0;
- while (currow < numrows) {
- rowsperchunk = MIN(rowsperchunk, numrows - currow);
- workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
- (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
- * SIZEOF(JBLOCK)));
- for (i = rowsperchunk; i > 0; i--) {
- result[currow++] = workspace;
- workspace += blocksperrow;
- }
- }
-
- return result;
-}
-
-
-/*
- * About virtual array management:
- *
- * The above "normal" array routines are only used to allocate strip buffers
- * (as wide as the image, but just a few rows high). Full-image-sized buffers
- * are handled as "virtual" arrays. The array is still accessed a strip at a
- * time, but the memory manager must save the whole array for repeated
- * accesses. The intended implementation is that there is a strip buffer in
- * memory (as high as is possible given the desired memory limit), plus a
- * backing file that holds the rest of the array.
- *
- * The request_virt_array routines are told the total size of the image and
- * the maximum number of rows that will be accessed at once. The in-memory
- * buffer must be at least as large as the maxaccess value.
- *
- * The request routines create control blocks but not the in-memory buffers.
- * That is postponed until realize_virt_arrays is called. At that time the
- * total amount of space needed is known (approximately, anyway), so free
- * memory can be divided up fairly.
- *
- * The access_virt_array routines are responsible for making a specific strip
- * area accessible (after reading or writing the backing file, if necessary).
- * Note that the access routines are told whether the caller intends to modify
- * the accessed strip; during a read-only pass this saves having to rewrite
- * data to disk. The access routines are also responsible for pre-zeroing
- * any newly accessed rows, if pre-zeroing was requested.
- *
- * In current usage, the access requests are usually for nonoverlapping
- * strips; that is, successive access start_row numbers differ by exactly
- * num_rows = maxaccess. This means we can get good performance with simple
- * buffer dump/reload logic, by making the in-memory buffer be a multiple
- * of the access height; then there will never be accesses across bufferload
- * boundaries. The code will still work with overlapping access requests,
- * but it doesn't handle bufferload overlaps very efficiently.
- */
-
-
-METHODDEF(jvirt_sarray_ptr)
-request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
- JDIMENSION samplesperrow, JDIMENSION numrows,
- JDIMENSION maxaccess)
-/* Request a virtual 2-D sample array */
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- jvirt_sarray_ptr result;
-
- /* Only IMAGE-lifetime virtual arrays are currently supported */
- if (pool_id != JPOOL_IMAGE)
- ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
-
- /* get control block */
- result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
- SIZEOF(struct jvirt_sarray_control));
-
- result->mem_buffer = NULL; /* marks array not yet realized */
- result->rows_in_array = numrows;
- result->samplesperrow = samplesperrow;
- result->maxaccess = maxaccess;
- result->pre_zero = pre_zero;
- result->b_s_open = FALSE; /* no associated backing-store object */
- result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
- mem->virt_sarray_list = result;
-
- return result;
-}
-
-
-METHODDEF(jvirt_barray_ptr)
-request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
- JDIMENSION blocksperrow, JDIMENSION numrows,
- JDIMENSION maxaccess)
-/* Request a virtual 2-D coefficient-block array */
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- jvirt_barray_ptr result;
-
- /* Only IMAGE-lifetime virtual arrays are currently supported */
- if (pool_id != JPOOL_IMAGE)
- ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
-
- /* get control block */
- result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
- SIZEOF(struct jvirt_barray_control));
-
- result->mem_buffer = NULL; /* marks array not yet realized */
- result->rows_in_array = numrows;
- result->blocksperrow = blocksperrow;
- result->maxaccess = maxaccess;
- result->pre_zero = pre_zero;
- result->b_s_open = FALSE; /* no associated backing-store object */
- result->next = mem->virt_barray_list; /* add to list of virtual arrays */
- mem->virt_barray_list = result;
-
- return result;
-}
-
-
-METHODDEF(void)
-realize_virt_arrays (j_common_ptr cinfo)
-/* Allocate the in-memory buffers for any unrealized virtual arrays */
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- long space_per_minheight, maximum_space, avail_mem;
- long minheights, max_minheights;
- jvirt_sarray_ptr sptr;
- jvirt_barray_ptr bptr;
-
- /* Compute the minimum space needed (maxaccess rows in each buffer)
- * and the maximum space needed (full image height in each buffer).
- * These may be of use to the system-dependent jpeg_mem_available routine.
- */
- space_per_minheight = 0;
- maximum_space = 0;
- for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
- if (sptr->mem_buffer == NULL) { /* if not realized yet */
- space_per_minheight += (long) sptr->maxaccess *
- (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
- maximum_space += (long) sptr->rows_in_array *
- (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
- }
- }
- for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
- if (bptr->mem_buffer == NULL) { /* if not realized yet */
- space_per_minheight += (long) bptr->maxaccess *
- (long) bptr->blocksperrow * SIZEOF(JBLOCK);
- maximum_space += (long) bptr->rows_in_array *
- (long) bptr->blocksperrow * SIZEOF(JBLOCK);
- }
- }
-
- if (space_per_minheight <= 0)
- return; /* no unrealized arrays, no work */
-
- /* Determine amount of memory to actually use; this is system-dependent. */
- avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
- mem->total_space_allocated);
-
- /* If the maximum space needed is available, make all the buffers full
- * height; otherwise parcel it out with the same number of minheights
- * in each buffer.
- */
- if (avail_mem >= maximum_space)
- max_minheights = 1000000000L;
- else {
- max_minheights = avail_mem / space_per_minheight;
- /* If there doesn't seem to be enough space, try to get the minimum
- * anyway. This allows a "stub" implementation of jpeg_mem_available().
- */
- if (max_minheights <= 0)
- max_minheights = 1;
- }
-
- /* Allocate the in-memory buffers and initialize backing store as needed. */
-
- for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
- if (sptr->mem_buffer == NULL) { /* if not realized yet */
- minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
- if (minheights <= max_minheights) {
- /* This buffer fits in memory */
- sptr->rows_in_mem = sptr->rows_in_array;
- } else {
- /* It doesn't fit in memory, create backing store. */
- sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
- jpeg_open_backing_store(cinfo, & sptr->b_s_info,
- (long) sptr->rows_in_array *
- (long) sptr->samplesperrow *
- (long) SIZEOF(JSAMPLE));
- sptr->b_s_open = TRUE;
- }
- sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
- sptr->samplesperrow, sptr->rows_in_mem);
- sptr->rowsperchunk = mem->last_rowsperchunk;
- sptr->cur_start_row = 0;
- sptr->first_undef_row = 0;
- sptr->dirty = FALSE;
- }
- }
-
- for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
- if (bptr->mem_buffer == NULL) { /* if not realized yet */
- minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
- if (minheights <= max_minheights) {
- /* This buffer fits in memory */
- bptr->rows_in_mem = bptr->rows_in_array;
- } else {
- /* It doesn't fit in memory, create backing store. */
- bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
- jpeg_open_backing_store(cinfo, & bptr->b_s_info,
- (long) bptr->rows_in_array *
- (long) bptr->blocksperrow *
- (long) SIZEOF(JBLOCK));
- bptr->b_s_open = TRUE;
- }
- bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
- bptr->blocksperrow, bptr->rows_in_mem);
- bptr->rowsperchunk = mem->last_rowsperchunk;
- bptr->cur_start_row = 0;
- bptr->first_undef_row = 0;
- bptr->dirty = FALSE;
- }
- }
-}
-
-
-LOCAL(void)
-do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
-/* Do backing store read or write of a virtual sample array */
-{
- long bytesperrow, file_offset, byte_count, rows, thisrow, i;
-
- bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
- file_offset = ptr->cur_start_row * bytesperrow;
- /* Loop to read or write each allocation chunk in mem_buffer */
- for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
- /* One chunk, but check for short chunk at end of buffer */
- rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
- /* Transfer no more than is currently defined */
- thisrow = (long) ptr->cur_start_row + i;
- rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
- /* Transfer no more than fits in file */
- rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
- if (rows <= 0) /* this chunk might be past end of file! */
- break;
- byte_count = rows * bytesperrow;
- if (writing)
- (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
- (void FAR *) ptr->mem_buffer[i],
- file_offset, byte_count);
- else
- (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
- (void FAR *) ptr->mem_buffer[i],
- file_offset, byte_count);
- file_offset += byte_count;
- }
-}
-
-
-LOCAL(void)
-do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
-/* Do backing store read or write of a virtual coefficient-block array */
-{
- long bytesperrow, file_offset, byte_count, rows, thisrow, i;
-
- bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
- file_offset = ptr->cur_start_row * bytesperrow;
- /* Loop to read or write each allocation chunk in mem_buffer */
- for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
- /* One chunk, but check for short chunk at end of buffer */
- rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
- /* Transfer no more than is currently defined */
- thisrow = (long) ptr->cur_start_row + i;
- rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
- /* Transfer no more than fits in file */
- rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
- if (rows <= 0) /* this chunk might be past end of file! */
- break;
- byte_count = rows * bytesperrow;
- if (writing)
- (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
- (void FAR *) ptr->mem_buffer[i],
- file_offset, byte_count);
- else
- (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
- (void FAR *) ptr->mem_buffer[i],
- file_offset, byte_count);
- file_offset += byte_count;
- }
-}
-
-
-METHODDEF(JSAMPARRAY)
-access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
- JDIMENSION start_row, JDIMENSION num_rows,
- boolean writable)
-/* Access the part of a virtual sample array starting at start_row */
-/* and extending for num_rows rows. writable is true if */
-/* caller intends to modify the accessed area. */
-{
- JDIMENSION end_row = start_row + num_rows;
- JDIMENSION undef_row;
-
- /* debugging check */
- if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
- ptr->mem_buffer == NULL)
- ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
-
- /* Make the desired part of the virtual array accessible */
- if (start_row < ptr->cur_start_row ||
- end_row > ptr->cur_start_row+ptr->rows_in_mem) {
- if (! ptr->b_s_open)
- ERREXIT(cinfo, JERR_VIRTUAL_BUG);
- /* Flush old buffer contents if necessary */
- if (ptr->dirty) {
- do_sarray_io(cinfo, ptr, TRUE);
- ptr->dirty = FALSE;
- }
- /* Decide what part of virtual array to access.
- * Algorithm: if target address > current window, assume forward scan,
- * load starting at target address. If target address < current window,
- * assume backward scan, load so that target area is top of window.
- * Note that when switching from forward write to forward read, will have
- * start_row = 0, so the limiting case applies and we load from 0 anyway.
- */
- if (start_row > ptr->cur_start_row) {
- ptr->cur_start_row = start_row;
- } else {
- /* use long arithmetic here to avoid overflow & unsigned problems */
- long ltemp;
-
- ltemp = (long) end_row - (long) ptr->rows_in_mem;
- if (ltemp < 0)
- ltemp = 0; /* don't fall off front end of file */
- ptr->cur_start_row = (JDIMENSION) ltemp;
- }
- /* Read in the selected part of the array.
- * During the initial write pass, we will do no actual read
- * because the selected part is all undefined.
- */
- do_sarray_io(cinfo, ptr, FALSE);
- }
- /* Ensure the accessed part of the array is defined; prezero if needed.
- * To improve locality of access, we only prezero the part of the array
- * that the caller is about to access, not the entire in-memory array.
- */
- if (ptr->first_undef_row < end_row) {
- if (ptr->first_undef_row < start_row) {
- if (writable) /* writer skipped over a section of array */
- ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
- undef_row = start_row; /* but reader is allowed to read ahead */
- } else {
- undef_row = ptr->first_undef_row;
- }
- if (writable)
- ptr->first_undef_row = end_row;
- if (ptr->pre_zero) {
- size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
- undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
- end_row -= ptr->cur_start_row;
- while (undef_row < end_row) {
- jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
- undef_row++;
- }
- } else {
- if (! writable) /* reader looking at undefined data */
- ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
- }
- }
- /* Flag the buffer dirty if caller will write in it */
- if (writable)
- ptr->dirty = TRUE;
- /* Return address of proper part of the buffer */
- return ptr->mem_buffer + (start_row - ptr->cur_start_row);
-}
-
-
-METHODDEF(JBLOCKARRAY)
-access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
- JDIMENSION start_row, JDIMENSION num_rows,
- boolean writable)
-/* Access the part of a virtual block array starting at start_row */
-/* and extending for num_rows rows. writable is true if */
-/* caller intends to modify the accessed area. */
-{
- JDIMENSION end_row = start_row + num_rows;
- JDIMENSION undef_row;
-
- /* debugging check */
- if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
- ptr->mem_buffer == NULL)
- ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
-
- /* Make the desired part of the virtual array accessible */
- if (start_row < ptr->cur_start_row ||
- end_row > ptr->cur_start_row+ptr->rows_in_mem) {
- if (! ptr->b_s_open)
- ERREXIT(cinfo, JERR_VIRTUAL_BUG);
- /* Flush old buffer contents if necessary */
- if (ptr->dirty) {
- do_barray_io(cinfo, ptr, TRUE);
- ptr->dirty = FALSE;
- }
- /* Decide what part of virtual array to access.
- * Algorithm: if target address > current window, assume forward scan,
- * load starting at target address. If target address < current window,
- * assume backward scan, load so that target area is top of window.
- * Note that when switching from forward write to forward read, will have
- * start_row = 0, so the limiting case applies and we load from 0 anyway.
- */
- if (start_row > ptr->cur_start_row) {
- ptr->cur_start_row = start_row;
- } else {
- /* use long arithmetic here to avoid overflow & unsigned problems */
- long ltemp;
-
- ltemp = (long) end_row - (long) ptr->rows_in_mem;
- if (ltemp < 0)
- ltemp = 0; /* don't fall off front end of file */
- ptr->cur_start_row = (JDIMENSION) ltemp;
- }
- /* Read in the selected part of the array.
- * During the initial write pass, we will do no actual read
- * because the selected part is all undefined.
- */
- do_barray_io(cinfo, ptr, FALSE);
- }
- /* Ensure the accessed part of the array is defined; prezero if needed.
- * To improve locality of access, we only prezero the part of the array
- * that the caller is about to access, not the entire in-memory array.
- */
- if (ptr->first_undef_row < end_row) {
- if (ptr->first_undef_row < start_row) {
- if (writable) /* writer skipped over a section of array */
- ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
- undef_row = start_row; /* but reader is allowed to read ahead */
- } else {
- undef_row = ptr->first_undef_row;
- }
- if (writable)
- ptr->first_undef_row = end_row;
- if (ptr->pre_zero) {
- size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
- undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
- end_row -= ptr->cur_start_row;
- while (undef_row < end_row) {
- jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
- undef_row++;
- }
- } else {
- if (! writable) /* reader looking at undefined data */
- ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
- }
- }
- /* Flag the buffer dirty if caller will write in it */
- if (writable)
- ptr->dirty = TRUE;
- /* Return address of proper part of the buffer */
- return ptr->mem_buffer + (start_row - ptr->cur_start_row);
-}
-
-
-/*
- * Release all objects belonging to a specified pool.
- */
-
-METHODDEF(void)
-free_pool (j_common_ptr cinfo, int pool_id)
-{
- my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
- small_pool_ptr shdr_ptr;
- large_pool_ptr lhdr_ptr;
- size_t space_freed;
-
- if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
- ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
-
-#ifdef MEM_STATS
- if (cinfo->err->trace_level > 1)
- print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
-#endif
-
- /* If freeing IMAGE pool, close any virtual arrays first */
- if (pool_id == JPOOL_IMAGE) {
- jvirt_sarray_ptr sptr;
- jvirt_barray_ptr bptr;
-
- for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
- if (sptr->b_s_open) { /* there may be no backing store */
- sptr->b_s_open = FALSE; /* prevent recursive close if error */
- (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
- }
- }
- mem->virt_sarray_list = NULL;
- for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
- if (bptr->b_s_open) { /* there may be no backing store */
- bptr->b_s_open = FALSE; /* prevent recursive close if error */
- (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
- }
- }
- mem->virt_barray_list = NULL;
- }
-
- /* Release large objects */
- lhdr_ptr = mem->large_list[pool_id];
- mem->large_list[pool_id] = NULL;
-
- while (lhdr_ptr != NULL) {
- large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
- space_freed = lhdr_ptr->hdr.bytes_used +
- lhdr_ptr->hdr.bytes_left +
- SIZEOF(large_pool_hdr);
- jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
- mem->total_space_allocated -= space_freed;
- lhdr_ptr = next_lhdr_ptr;
- }
-
- /* Release small objects */
- shdr_ptr = mem->small_list[pool_id];
- mem->small_list[pool_id] = NULL;
-
- while (shdr_ptr != NULL) {
- small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
- space_freed = shdr_ptr->hdr.bytes_used +
- shdr_ptr->hdr.bytes_left +
- SIZEOF(small_pool_hdr);
- jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
- mem->total_space_allocated -= space_freed;
- shdr_ptr = next_shdr_ptr;
- }
-}
-
-
-/*
- * Close up shop entirely.
- * Note that this cannot be called unless cinfo->mem is non-NULL.
- */
-
-METHODDEF(void)
-self_destruct (j_common_ptr cinfo)
-{
- int pool;
-
- /* Close all backing store, release all memory.
- * Releasing pools in reverse order might help avoid fragmentation
- * with some (brain-damaged) malloc libraries.
- */
- for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
- free_pool(cinfo, pool);
- }
-
- /* Release the memory manager control block too. */
- jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
- cinfo->mem = NULL; /* ensures I will be called only once */
-
- jpeg_mem_term(cinfo); /* system-dependent cleanup */
-}
-
-
-/*
- * Memory manager initialization.
- * When this is called, only the error manager pointer is valid in cinfo!
- */
-
-GLOBAL(void)
-jinit_memory_mgr (j_common_ptr cinfo)
-{
- my_mem_ptr mem;
- long max_to_use;
- int pool;
- size_t test_mac;
-
- cinfo->mem = NULL; /* for safety if init fails */
-
- /* Check for configuration errors.
- * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
- * doesn't reflect any real hardware alignment requirement.
- * The test is a little tricky: for X>0, X and X-1 have no one-bits
- * in common if and only if X is a power of 2, ie has only one one-bit.
- * Some compilers may give an "unreachable code" warning here; ignore it.
- */
- if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
- ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
- /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
- * a multiple of SIZEOF(ALIGN_TYPE).
- * Again, an "unreachable code" warning may be ignored here.
- * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
- */
- test_mac = (size_t) MAX_ALLOC_CHUNK;
- if ((long) test_mac != MAX_ALLOC_CHUNK ||
- (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
- ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
-
- max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
-
- /* Attempt to allocate memory manager's control block */
- mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
-
- if (mem == NULL) {
- jpeg_mem_term(cinfo); /* system-dependent cleanup */
- ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
- }
-
- /* OK, fill in the method pointers */
- mem->pub.alloc_small = alloc_small;
- mem->pub.alloc_large = alloc_large;
- mem->pub.alloc_sarray = alloc_sarray;
- mem->pub.alloc_barray = alloc_barray;
- mem->pub.request_virt_sarray = request_virt_sarray;
- mem->pub.request_virt_barray = request_virt_barray;
- mem->pub.realize_virt_arrays = realize_virt_arrays;
- mem->pub.access_virt_sarray = access_virt_sarray;
- mem->pub.access_virt_barray = access_virt_barray;
- mem->pub.free_pool = free_pool;
- mem->pub.self_destruct = self_destruct;
-
- /* Make MAX_ALLOC_CHUNK accessible to other modules */
- mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
-
- /* Initialize working state */
- mem->pub.max_memory_to_use = max_to_use;
-
- for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
- mem->small_list[pool] = NULL;
- mem->large_list[pool] = NULL;
- }
- mem->virt_sarray_list = NULL;
- mem->virt_barray_list = NULL;
-
- mem->total_space_allocated = SIZEOF(my_memory_mgr);
-
- /* Declare ourselves open for business */
- cinfo->mem = & mem->pub;
-
- /* Check for an environment variable JPEGMEM; if found, override the
- * default max_memory setting from jpeg_mem_init. Note that the
- * surrounding application may again override this value.
- * If your system doesn't support getenv(), define NO_GETENV to disable
- * this feature.
- */
-#ifndef NO_GETENV
- { char * memenv;
-
- if ((memenv = getenv("JPEGMEM")) != NULL) {
- char ch = 'x';
-
- if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
- if (ch == 'm' || ch == 'M')
- max_to_use *= 1000L;
- mem->pub.max_memory_to_use = max_to_use * 1000L;
- }
- }
- }
-#endif
-
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jmemmgr.c
+ *
+ * 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 the JPEG system-independent memory management
+ * routines. This code is usable across a wide variety of machines; most
+ * of the system dependencies have been isolated in a separate file.
+ * The major functions provided here are:
+ * * pool-based allocation and freeing of memory;
+ * * policy decisions about how to divide available memory among the
+ * virtual arrays;
+ * * control logic for swapping virtual arrays between main memory and
+ * backing storage.
+ * The separate system-dependent file provides the actual backing-storage
+ * access code, and it contains the policy decision about how much total
+ * main memory to use.
+ * This file is system-dependent in the sense that some of its functions
+ * are unnecessary in some systems. For example, if there is enough virtual
+ * memory so that backing storage will never be used, much of the virtual
+ * array control logic could be removed. (Of course, if you have that much
+ * memory then you shouldn't care about a little bit of unused code...)
+ */
+
+#define JPEG_INTERNALS
+#define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */
+#include "jinclude.h"
+#include "jpeglib.h"
+#include "jmemsys.h" /* import the system-dependent declarations */
+
+#define NO_GETENV /* XYQ: 2007-5-22 Don't use it */
+
+#ifndef NO_GETENV
+#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */
+extern char * getenv JPP((const char * name));
+#endif
+#endif
+
+
+/*
+ * Some important notes:
+ * The allocation routines provided here must never return NULL.
+ * They should exit to error_exit if unsuccessful.
+ *
+ * It's not a good idea to try to merge the sarray and barray routines,
+ * even though they are textually almost the same, because samples are
+ * usually stored as bytes while coefficients are shorts or ints. Thus,
+ * in machines where byte pointers have a different representation from
+ * word pointers, the resulting machine code could not be the same.
+ */
+
+
+/*
+ * Many machines require storage alignment: longs must start on 4-byte
+ * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
+ * always returns pointers that are multiples of the worst-case alignment
+ * requirement, and we had better do so too.
+ * There isn't any really portable way to determine the worst-case alignment
+ * requirement. This module assumes that the alignment requirement is
+ * multiples of sizeof(ALIGN_TYPE).
+ * By default, we define ALIGN_TYPE as double. This is necessary on some
+ * workstations (where doubles really do need 8-byte alignment) and will work
+ * fine on nearly everything. If your machine has lesser alignment needs,
+ * you can save a few bytes by making ALIGN_TYPE smaller.
+ * The only place I know of where this will NOT work is certain Macintosh
+ * 680x0 compilers that define double as a 10-byte IEEE extended float.
+ * Doing 10-byte alignment is counterproductive because longwords won't be
+ * aligned well. Put "#define ALIGN_TYPE long" in jconfig.h if you have
+ * such a compiler.
+ */
+
+#ifndef ALIGN_TYPE /* so can override from jconfig.h */
+#define ALIGN_TYPE double
+#endif
+
+
+/*
+ * We allocate objects from "pools", where each pool is gotten with a single
+ * request to jpeg_get_small() or jpeg_get_large(). There is no per-object
+ * overhead within a pool, except for alignment padding. Each pool has a
+ * header with a link to the next pool of the same class.
+ * Small and large pool headers are identical except that the latter's
+ * link pointer must be FAR on 80x86 machines.
+ * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
+ * field. This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
+ * of the alignment requirement of ALIGN_TYPE.
+ */
+
+typedef union small_pool_struct * small_pool_ptr;
+
+typedef union small_pool_struct {
+ struct {
+ small_pool_ptr next; /* next in list of pools */
+ size_t bytes_used; /* how many bytes already used within pool */
+ size_t bytes_left; /* bytes still available in this pool */
+ } hdr;
+ ALIGN_TYPE dummy; /* included in union to ensure alignment */
+} small_pool_hdr;
+
+typedef union large_pool_struct FAR * large_pool_ptr;
+
+typedef union large_pool_struct {
+ struct {
+ large_pool_ptr next; /* next in list of pools */
+ size_t bytes_used; /* how many bytes already used within pool */
+ size_t bytes_left; /* bytes still available in this pool */
+ } hdr;
+ ALIGN_TYPE dummy; /* included in union to ensure alignment */
+} large_pool_hdr;
+
+
+/*
+ * Here is the full definition of a memory manager object.
+ */
+
+typedef struct {
+ struct jpeg_memory_mgr pub; /* public fields */
+
+ /* Each pool identifier (lifetime class) names a linked list of pools. */
+ small_pool_ptr small_list[JPOOL_NUMPOOLS];
+ large_pool_ptr large_list[JPOOL_NUMPOOLS];
+
+ /* Since we only have one lifetime class of virtual arrays, only one
+ * linked list is necessary (for each datatype). Note that the virtual
+ * array control blocks being linked together are actually stored somewhere
+ * in the small-pool list.
+ */
+ jvirt_sarray_ptr virt_sarray_list;
+ jvirt_barray_ptr virt_barray_list;
+
+ /* This counts total space obtained from jpeg_get_small/large */
+ long total_space_allocated;
+
+ /* alloc_sarray and alloc_barray set this value for use by virtual
+ * array routines.
+ */
+ JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
+} my_memory_mgr;
+
+typedef my_memory_mgr * my_mem_ptr;
+
+
+/*
+ * The control blocks for virtual arrays.
+ * Note that these blocks are allocated in the "small" pool area.
+ * System-dependent info for the associated backing store (if any) is hidden
+ * inside the backing_store_info struct.
+ */
+
+struct jvirt_sarray_control {
+ JSAMPARRAY mem_buffer; /* => the in-memory buffer */
+ JDIMENSION rows_in_array; /* total virtual array height */
+ JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
+ JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */
+ JDIMENSION rows_in_mem; /* height of memory buffer */
+ JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
+ JDIMENSION cur_start_row; /* first logical row # in the buffer */
+ JDIMENSION first_undef_row; /* row # of first uninitialized row */
+ boolean pre_zero; /* pre-zero mode requested? */
+ boolean dirty; /* do current buffer contents need written? */
+ boolean b_s_open; /* is backing-store data valid? */
+ jvirt_sarray_ptr next; /* link to next virtual sarray control block */
+ backing_store_info b_s_info; /* System-dependent control info */
+};
+
+struct jvirt_barray_control {
+ JBLOCKARRAY mem_buffer; /* => the in-memory buffer */
+ JDIMENSION rows_in_array; /* total virtual array height */
+ JDIMENSION blocksperrow; /* width of array (and of memory buffer) */
+ JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */
+ JDIMENSION rows_in_mem; /* height of memory buffer */
+ JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
+ JDIMENSION cur_start_row; /* first logical row # in the buffer */
+ JDIMENSION first_undef_row; /* row # of first uninitialized row */
+ boolean pre_zero; /* pre-zero mode requested? */
+ boolean dirty; /* do current buffer contents need written? */
+ boolean b_s_open; /* is backing-store data valid? */
+ jvirt_barray_ptr next; /* link to next virtual barray control block */
+ backing_store_info b_s_info; /* System-dependent control info */
+};
+
+
+#ifdef MEM_STATS /* optional extra stuff for statistics */
+
+LOCAL(void)
+print_mem_stats (j_common_ptr cinfo, int pool_id)
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ small_pool_ptr shdr_ptr;
+ large_pool_ptr lhdr_ptr;
+
+ /* Since this is only a debugging stub, we can cheat a little by using
+ * fprintf directly rather than going through the trace message code.
+ * This is helpful because message parm array can't handle longs.
+ */
+ FXSYS_fprintf(stderr, "Freeing pool %d, total space = %ld\n",
+ pool_id, mem->total_space_allocated);
+
+ for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
+ lhdr_ptr = lhdr_ptr->hdr.next) {
+ FXSYS_fprintf(stderr, " Large chunk used %ld\n",
+ (long) lhdr_ptr->hdr.bytes_used);
+ }
+
+ for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
+ shdr_ptr = shdr_ptr->hdr.next) {
+ FXSYS_fprintf(stderr, " Small chunk used %ld free %ld\n",
+ (long) shdr_ptr->hdr.bytes_used,
+ (long) shdr_ptr->hdr.bytes_left);
+ }
+}
+
+#endif /* MEM_STATS */
+
+
+LOCAL(void)
+out_of_memory (j_common_ptr cinfo, int which)
+/* Report an out-of-memory error and stop execution */
+/* If we compiled MEM_STATS support, report alloc requests before dying */
+{
+#ifdef MEM_STATS
+ cinfo->err->trace_level = 2; /* force self_destruct to report stats */
+#endif
+ ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
+}
+
+
+/*
+ * Allocation of "small" objects.
+ *
+ * For these, we use pooled storage. When a new pool must be created,
+ * we try to get enough space for the current request plus a "slop" factor,
+ * where the slop will be the amount of leftover space in the new pool.
+ * The speed vs. space tradeoff is largely determined by the slop values.
+ * A different slop value is provided for each pool class (lifetime),
+ * and we also distinguish the first pool of a class from later ones.
+ * NOTE: the values given work fairly well on both 16- and 32-bit-int
+ * machines, but may be too small if longs are 64 bits or more.
+ */
+
+static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
+{
+ 1600, /* first PERMANENT pool */
+ 16000 /* first IMAGE pool */
+};
+
+static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
+{
+ 0, /* additional PERMANENT pools */
+ 5000 /* additional IMAGE pools */
+};
+
+#define MIN_SLOP 50 /* greater than 0 to avoid futile looping */
+
+
+METHODDEF(void *)
+alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
+/* Allocate a "small" object */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ small_pool_ptr hdr_ptr, prev_hdr_ptr;
+ char * data_ptr;
+ size_t odd_bytes, min_request, slop;
+
+ /* Check for unsatisfiable request (do now to ensure no overflow below) */
+ if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
+ out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
+
+ /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
+ odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
+ if (odd_bytes > 0)
+ sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
+
+ /* See if space is available in any existing pool */
+ if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+ prev_hdr_ptr = NULL;
+ hdr_ptr = mem->small_list[pool_id];
+ while (hdr_ptr != NULL) {
+ if (hdr_ptr->hdr.bytes_left >= sizeofobject)
+ break; /* found pool with enough space */
+ prev_hdr_ptr = hdr_ptr;
+ hdr_ptr = hdr_ptr->hdr.next;
+ }
+
+ /* Time to make a new pool? */
+ if (hdr_ptr == NULL) {
+ /* min_request is what we need now, slop is what will be leftover */
+ min_request = sizeofobject + SIZEOF(small_pool_hdr);
+ if (prev_hdr_ptr == NULL) /* first pool in class? */
+ slop = first_pool_slop[pool_id];
+ else
+ slop = extra_pool_slop[pool_id];
+ /* Don't ask for more than MAX_ALLOC_CHUNK */
+ if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
+ slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
+ /* Try to get space, if fail reduce slop and try again */
+ for (;;) {
+ hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
+ if (hdr_ptr != NULL)
+ break;
+ slop /= 2;
+ if (slop < MIN_SLOP) /* give up when it gets real small */
+ out_of_memory(cinfo, 2); /* jpeg_get_small failed */
+ }
+ mem->total_space_allocated += min_request + slop;
+ /* Success, initialize the new pool header and add to end of list */
+ hdr_ptr->hdr.next = NULL;
+ hdr_ptr->hdr.bytes_used = 0;
+ hdr_ptr->hdr.bytes_left = sizeofobject + slop;
+ if (prev_hdr_ptr == NULL) /* first pool in class? */
+ mem->small_list[pool_id] = hdr_ptr;
+ else
+ prev_hdr_ptr->hdr.next = hdr_ptr;
+ }
+
+ /* OK, allocate the object from the current pool */
+ data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
+ data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
+ hdr_ptr->hdr.bytes_used += sizeofobject;
+ hdr_ptr->hdr.bytes_left -= sizeofobject;
+
+ return (void *) data_ptr;
+}
+
+
+/*
+ * Allocation of "large" objects.
+ *
+ * The external semantics of these are the same as "small" objects,
+ * except that FAR pointers are used on 80x86. However the pool
+ * management heuristics are quite different. We assume that each
+ * request is large enough that it may as well be passed directly to
+ * jpeg_get_large; the pool management just links everything together
+ * so that we can free it all on demand.
+ * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
+ * structures. The routines that create these structures (see below)
+ * deliberately bunch rows together to ensure a large request size.
+ */
+
+METHODDEF(void FAR *)
+alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
+/* Allocate a "large" object */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ large_pool_ptr hdr_ptr;
+ size_t odd_bytes;
+
+ /* Check for unsatisfiable request (do now to ensure no overflow below) */
+ if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
+ out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
+
+ /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
+ odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
+ if (odd_bytes > 0)
+ sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
+
+ /* Always make a new pool */
+ if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+
+ hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
+ SIZEOF(large_pool_hdr));
+ if (hdr_ptr == NULL)
+ out_of_memory(cinfo, 4); /* jpeg_get_large failed */
+ mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
+
+ /* Success, initialize the new pool header and add to list */
+ hdr_ptr->hdr.next = mem->large_list[pool_id];
+ /* We maintain space counts in each pool header for statistical purposes,
+ * even though they are not needed for allocation.
+ */
+ hdr_ptr->hdr.bytes_used = sizeofobject;
+ hdr_ptr->hdr.bytes_left = 0;
+ mem->large_list[pool_id] = hdr_ptr;
+
+ return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
+}
+
+
+/*
+ * Creation of 2-D sample arrays.
+ * The pointers are in near heap, the samples themselves in FAR heap.
+ *
+ * To minimize allocation overhead and to allow I/O of large contiguous
+ * blocks, we allocate the sample rows in groups of as many rows as possible
+ * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
+ * NB: the virtual array control routines, later in this file, know about
+ * this chunking of rows. The rowsperchunk value is left in the mem manager
+ * object so that it can be saved away if this sarray is the workspace for
+ * a virtual array.
+ */
+
+METHODDEF(JSAMPARRAY)
+alloc_sarray (j_common_ptr cinfo, int pool_id,
+ JDIMENSION samplesperrow, JDIMENSION numrows)
+/* Allocate a 2-D sample array */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ JSAMPARRAY result;
+ JSAMPROW workspace;
+ JDIMENSION rowsperchunk, currow, i;
+ long ltemp;
+
+ /* Calculate max # of rows allowed in one allocation chunk */
+ ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
+ ((long) samplesperrow * SIZEOF(JSAMPLE));
+ if (ltemp <= 0)
+ ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+ if (ltemp < (long) numrows)
+ rowsperchunk = (JDIMENSION) ltemp;
+ else
+ rowsperchunk = numrows;
+ mem->last_rowsperchunk = rowsperchunk;
+
+ /* Get space for row pointers (small object) */
+ result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
+ (size_t) (numrows * SIZEOF(JSAMPROW)));
+
+ /* Get the rows themselves (large objects) */
+ currow = 0;
+ while (currow < numrows) {
+ rowsperchunk = MIN(rowsperchunk, numrows - currow);
+ workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
+ (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
+ * SIZEOF(JSAMPLE)));
+ for (i = rowsperchunk; i > 0; i--) {
+ result[currow++] = workspace;
+ workspace += samplesperrow;
+ }
+ }
+
+ return result;
+}
+
+
+/*
+ * Creation of 2-D coefficient-block arrays.
+ * This is essentially the same as the code for sample arrays, above.
+ */
+
+METHODDEF(JBLOCKARRAY)
+alloc_barray (j_common_ptr cinfo, int pool_id,
+ JDIMENSION blocksperrow, JDIMENSION numrows)
+/* Allocate a 2-D coefficient-block array */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ JBLOCKARRAY result;
+ JBLOCKROW workspace;
+ JDIMENSION rowsperchunk, currow, i;
+ long ltemp;
+
+ /* Calculate max # of rows allowed in one allocation chunk */
+ ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
+ ((long) blocksperrow * SIZEOF(JBLOCK));
+ if (ltemp <= 0)
+ ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+ if (ltemp < (long) numrows)
+ rowsperchunk = (JDIMENSION) ltemp;
+ else
+ rowsperchunk = numrows;
+ mem->last_rowsperchunk = rowsperchunk;
+
+ /* Get space for row pointers (small object) */
+ result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
+ (size_t) (numrows * SIZEOF(JBLOCKROW)));
+
+ /* Get the rows themselves (large objects) */
+ currow = 0;
+ while (currow < numrows) {
+ rowsperchunk = MIN(rowsperchunk, numrows - currow);
+ workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
+ (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
+ * SIZEOF(JBLOCK)));
+ for (i = rowsperchunk; i > 0; i--) {
+ result[currow++] = workspace;
+ workspace += blocksperrow;
+ }
+ }
+
+ return result;
+}
+
+
+/*
+ * About virtual array management:
+ *
+ * The above "normal" array routines are only used to allocate strip buffers
+ * (as wide as the image, but just a few rows high). Full-image-sized buffers
+ * are handled as "virtual" arrays. The array is still accessed a strip at a
+ * time, but the memory manager must save the whole array for repeated
+ * accesses. The intended implementation is that there is a strip buffer in
+ * memory (as high as is possible given the desired memory limit), plus a
+ * backing file that holds the rest of the array.
+ *
+ * The request_virt_array routines are told the total size of the image and
+ * the maximum number of rows that will be accessed at once. The in-memory
+ * buffer must be at least as large as the maxaccess value.
+ *
+ * The request routines create control blocks but not the in-memory buffers.
+ * That is postponed until realize_virt_arrays is called. At that time the
+ * total amount of space needed is known (approximately, anyway), so free
+ * memory can be divided up fairly.
+ *
+ * The access_virt_array routines are responsible for making a specific strip
+ * area accessible (after reading or writing the backing file, if necessary).
+ * Note that the access routines are told whether the caller intends to modify
+ * the accessed strip; during a read-only pass this saves having to rewrite
+ * data to disk. The access routines are also responsible for pre-zeroing
+ * any newly accessed rows, if pre-zeroing was requested.
+ *
+ * In current usage, the access requests are usually for nonoverlapping
+ * strips; that is, successive access start_row numbers differ by exactly
+ * num_rows = maxaccess. This means we can get good performance with simple
+ * buffer dump/reload logic, by making the in-memory buffer be a multiple
+ * of the access height; then there will never be accesses across bufferload
+ * boundaries. The code will still work with overlapping access requests,
+ * but it doesn't handle bufferload overlaps very efficiently.
+ */
+
+
+METHODDEF(jvirt_sarray_ptr)
+request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
+ JDIMENSION samplesperrow, JDIMENSION numrows,
+ JDIMENSION maxaccess)
+/* Request a virtual 2-D sample array */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ jvirt_sarray_ptr result;
+
+ /* Only IMAGE-lifetime virtual arrays are currently supported */
+ if (pool_id != JPOOL_IMAGE)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+
+ /* get control block */
+ result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
+ SIZEOF(struct jvirt_sarray_control));
+
+ result->mem_buffer = NULL; /* marks array not yet realized */
+ result->rows_in_array = numrows;
+ result->samplesperrow = samplesperrow;
+ result->maxaccess = maxaccess;
+ result->pre_zero = pre_zero;
+ result->b_s_open = FALSE; /* no associated backing-store object */
+ result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
+ mem->virt_sarray_list = result;
+
+ return result;
+}
+
+
+METHODDEF(jvirt_barray_ptr)
+request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
+ JDIMENSION blocksperrow, JDIMENSION numrows,
+ JDIMENSION maxaccess)
+/* Request a virtual 2-D coefficient-block array */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ jvirt_barray_ptr result;
+
+ /* Only IMAGE-lifetime virtual arrays are currently supported */
+ if (pool_id != JPOOL_IMAGE)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+
+ /* get control block */
+ result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
+ SIZEOF(struct jvirt_barray_control));
+
+ result->mem_buffer = NULL; /* marks array not yet realized */
+ result->rows_in_array = numrows;
+ result->blocksperrow = blocksperrow;
+ result->maxaccess = maxaccess;
+ result->pre_zero = pre_zero;
+ result->b_s_open = FALSE; /* no associated backing-store object */
+ result->next = mem->virt_barray_list; /* add to list of virtual arrays */
+ mem->virt_barray_list = result;
+
+ return result;
+}
+
+
+METHODDEF(void)
+realize_virt_arrays (j_common_ptr cinfo)
+/* Allocate the in-memory buffers for any unrealized virtual arrays */
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ long space_per_minheight, maximum_space, avail_mem;
+ long minheights, max_minheights;
+ jvirt_sarray_ptr sptr;
+ jvirt_barray_ptr bptr;
+
+ /* Compute the minimum space needed (maxaccess rows in each buffer)
+ * and the maximum space needed (full image height in each buffer).
+ * These may be of use to the system-dependent jpeg_mem_available routine.
+ */
+ space_per_minheight = 0;
+ maximum_space = 0;
+ for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+ if (sptr->mem_buffer == NULL) { /* if not realized yet */
+ space_per_minheight += (long) sptr->maxaccess *
+ (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+ maximum_space += (long) sptr->rows_in_array *
+ (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+ }
+ }
+ for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+ if (bptr->mem_buffer == NULL) { /* if not realized yet */
+ space_per_minheight += (long) bptr->maxaccess *
+ (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+ maximum_space += (long) bptr->rows_in_array *
+ (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+ }
+ }
+
+ if (space_per_minheight <= 0)
+ return; /* no unrealized arrays, no work */
+
+ /* Determine amount of memory to actually use; this is system-dependent. */
+ avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
+ mem->total_space_allocated);
+
+ /* If the maximum space needed is available, make all the buffers full
+ * height; otherwise parcel it out with the same number of minheights
+ * in each buffer.
+ */
+ if (avail_mem >= maximum_space)
+ max_minheights = 1000000000L;
+ else {
+ max_minheights = avail_mem / space_per_minheight;
+ /* If there doesn't seem to be enough space, try to get the minimum
+ * anyway. This allows a "stub" implementation of jpeg_mem_available().
+ */
+ if (max_minheights <= 0)
+ max_minheights = 1;
+ }
+
+ /* Allocate the in-memory buffers and initialize backing store as needed. */
+
+ for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+ if (sptr->mem_buffer == NULL) { /* if not realized yet */
+ minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
+ if (minheights <= max_minheights) {
+ /* This buffer fits in memory */
+ sptr->rows_in_mem = sptr->rows_in_array;
+ } else {
+ /* It doesn't fit in memory, create backing store. */
+ sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
+ jpeg_open_backing_store(cinfo, & sptr->b_s_info,
+ (long) sptr->rows_in_array *
+ (long) sptr->samplesperrow *
+ (long) SIZEOF(JSAMPLE));
+ sptr->b_s_open = TRUE;
+ }
+ sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
+ sptr->samplesperrow, sptr->rows_in_mem);
+ sptr->rowsperchunk = mem->last_rowsperchunk;
+ sptr->cur_start_row = 0;
+ sptr->first_undef_row = 0;
+ sptr->dirty = FALSE;
+ }
+ }
+
+ for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+ if (bptr->mem_buffer == NULL) { /* if not realized yet */
+ minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
+ if (minheights <= max_minheights) {
+ /* This buffer fits in memory */
+ bptr->rows_in_mem = bptr->rows_in_array;
+ } else {
+ /* It doesn't fit in memory, create backing store. */
+ bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
+ jpeg_open_backing_store(cinfo, & bptr->b_s_info,
+ (long) bptr->rows_in_array *
+ (long) bptr->blocksperrow *
+ (long) SIZEOF(JBLOCK));
+ bptr->b_s_open = TRUE;
+ }
+ bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
+ bptr->blocksperrow, bptr->rows_in_mem);
+ bptr->rowsperchunk = mem->last_rowsperchunk;
+ bptr->cur_start_row = 0;
+ bptr->first_undef_row = 0;
+ bptr->dirty = FALSE;
+ }
+ }
+}
+
+
+LOCAL(void)
+do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
+/* Do backing store read or write of a virtual sample array */
+{
+ long bytesperrow, file_offset, byte_count, rows, thisrow, i;
+
+ bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
+ file_offset = ptr->cur_start_row * bytesperrow;
+ /* Loop to read or write each allocation chunk in mem_buffer */
+ for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
+ /* One chunk, but check for short chunk at end of buffer */
+ rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
+ /* Transfer no more than is currently defined */
+ thisrow = (long) ptr->cur_start_row + i;
+ rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
+ /* Transfer no more than fits in file */
+ rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
+ if (rows <= 0) /* this chunk might be past end of file! */
+ break;
+ byte_count = rows * bytesperrow;
+ if (writing)
+ (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
+ (void FAR *) ptr->mem_buffer[i],
+ file_offset, byte_count);
+ else
+ (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
+ (void FAR *) ptr->mem_buffer[i],
+ file_offset, byte_count);
+ file_offset += byte_count;
+ }
+}
+
+
+LOCAL(void)
+do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
+/* Do backing store read or write of a virtual coefficient-block array */
+{
+ long bytesperrow, file_offset, byte_count, rows, thisrow, i;
+
+ bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
+ file_offset = ptr->cur_start_row * bytesperrow;
+ /* Loop to read or write each allocation chunk in mem_buffer */
+ for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
+ /* One chunk, but check for short chunk at end of buffer */
+ rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
+ /* Transfer no more than is currently defined */
+ thisrow = (long) ptr->cur_start_row + i;
+ rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
+ /* Transfer no more than fits in file */
+ rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
+ if (rows <= 0) /* this chunk might be past end of file! */
+ break;
+ byte_count = rows * bytesperrow;
+ if (writing)
+ (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
+ (void FAR *) ptr->mem_buffer[i],
+ file_offset, byte_count);
+ else
+ (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
+ (void FAR *) ptr->mem_buffer[i],
+ file_offset, byte_count);
+ file_offset += byte_count;
+ }
+}
+
+
+METHODDEF(JSAMPARRAY)
+access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
+ JDIMENSION start_row, JDIMENSION num_rows,
+ boolean writable)
+/* Access the part of a virtual sample array starting at start_row */
+/* and extending for num_rows rows. writable is true if */
+/* caller intends to modify the accessed area. */
+{
+ JDIMENSION end_row = start_row + num_rows;
+ JDIMENSION undef_row;
+
+ /* debugging check */
+ if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
+ ptr->mem_buffer == NULL)
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+ /* Make the desired part of the virtual array accessible */
+ if (start_row < ptr->cur_start_row ||
+ end_row > ptr->cur_start_row+ptr->rows_in_mem) {
+ if (! ptr->b_s_open)
+ ERREXIT(cinfo, JERR_VIRTUAL_BUG);
+ /* Flush old buffer contents if necessary */
+ if (ptr->dirty) {
+ do_sarray_io(cinfo, ptr, TRUE);
+ ptr->dirty = FALSE;
+ }
+ /* Decide what part of virtual array to access.
+ * Algorithm: if target address > current window, assume forward scan,
+ * load starting at target address. If target address < current window,
+ * assume backward scan, load so that target area is top of window.
+ * Note that when switching from forward write to forward read, will have
+ * start_row = 0, so the limiting case applies and we load from 0 anyway.
+ */
+ if (start_row > ptr->cur_start_row) {
+ ptr->cur_start_row = start_row;
+ } else {
+ /* use long arithmetic here to avoid overflow & unsigned problems */
+ long ltemp;
+
+ ltemp = (long) end_row - (long) ptr->rows_in_mem;
+ if (ltemp < 0)
+ ltemp = 0; /* don't fall off front end of file */
+ ptr->cur_start_row = (JDIMENSION) ltemp;
+ }
+ /* Read in the selected part of the array.
+ * During the initial write pass, we will do no actual read
+ * because the selected part is all undefined.
+ */
+ do_sarray_io(cinfo, ptr, FALSE);
+ }
+ /* Ensure the accessed part of the array is defined; prezero if needed.
+ * To improve locality of access, we only prezero the part of the array
+ * that the caller is about to access, not the entire in-memory array.
+ */
+ if (ptr->first_undef_row < end_row) {
+ if (ptr->first_undef_row < start_row) {
+ if (writable) /* writer skipped over a section of array */
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+ undef_row = start_row; /* but reader is allowed to read ahead */
+ } else {
+ undef_row = ptr->first_undef_row;
+ }
+ if (writable)
+ ptr->first_undef_row = end_row;
+ if (ptr->pre_zero) {
+ size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
+ undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
+ end_row -= ptr->cur_start_row;
+ while (undef_row < end_row) {
+ jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
+ undef_row++;
+ }
+ } else {
+ if (! writable) /* reader looking at undefined data */
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+ }
+ }
+ /* Flag the buffer dirty if caller will write in it */
+ if (writable)
+ ptr->dirty = TRUE;
+ /* Return address of proper part of the buffer */
+ return ptr->mem_buffer + (start_row - ptr->cur_start_row);
+}
+
+
+METHODDEF(JBLOCKARRAY)
+access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
+ JDIMENSION start_row, JDIMENSION num_rows,
+ boolean writable)
+/* Access the part of a virtual block array starting at start_row */
+/* and extending for num_rows rows. writable is true if */
+/* caller intends to modify the accessed area. */
+{
+ JDIMENSION end_row = start_row + num_rows;
+ JDIMENSION undef_row;
+
+ /* debugging check */
+ if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
+ ptr->mem_buffer == NULL)
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+ /* Make the desired part of the virtual array accessible */
+ if (start_row < ptr->cur_start_row ||
+ end_row > ptr->cur_start_row+ptr->rows_in_mem) {
+ if (! ptr->b_s_open)
+ ERREXIT(cinfo, JERR_VIRTUAL_BUG);
+ /* Flush old buffer contents if necessary */
+ if (ptr->dirty) {
+ do_barray_io(cinfo, ptr, TRUE);
+ ptr->dirty = FALSE;
+ }
+ /* Decide what part of virtual array to access.
+ * Algorithm: if target address > current window, assume forward scan,
+ * load starting at target address. If target address < current window,
+ * assume backward scan, load so that target area is top of window.
+ * Note that when switching from forward write to forward read, will have
+ * start_row = 0, so the limiting case applies and we load from 0 anyway.
+ */
+ if (start_row > ptr->cur_start_row) {
+ ptr->cur_start_row = start_row;
+ } else {
+ /* use long arithmetic here to avoid overflow & unsigned problems */
+ long ltemp;
+
+ ltemp = (long) end_row - (long) ptr->rows_in_mem;
+ if (ltemp < 0)
+ ltemp = 0; /* don't fall off front end of file */
+ ptr->cur_start_row = (JDIMENSION) ltemp;
+ }
+ /* Read in the selected part of the array.
+ * During the initial write pass, we will do no actual read
+ * because the selected part is all undefined.
+ */
+ do_barray_io(cinfo, ptr, FALSE);
+ }
+ /* Ensure the accessed part of the array is defined; prezero if needed.
+ * To improve locality of access, we only prezero the part of the array
+ * that the caller is about to access, not the entire in-memory array.
+ */
+ if (ptr->first_undef_row < end_row) {
+ if (ptr->first_undef_row < start_row) {
+ if (writable) /* writer skipped over a section of array */
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+ undef_row = start_row; /* but reader is allowed to read ahead */
+ } else {
+ undef_row = ptr->first_undef_row;
+ }
+ if (writable)
+ ptr->first_undef_row = end_row;
+ if (ptr->pre_zero) {
+ size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
+ undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
+ end_row -= ptr->cur_start_row;
+ while (undef_row < end_row) {
+ jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
+ undef_row++;
+ }
+ } else {
+ if (! writable) /* reader looking at undefined data */
+ ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+ }
+ }
+ /* Flag the buffer dirty if caller will write in it */
+ if (writable)
+ ptr->dirty = TRUE;
+ /* Return address of proper part of the buffer */
+ return ptr->mem_buffer + (start_row - ptr->cur_start_row);
+}
+
+
+/*
+ * Release all objects belonging to a specified pool.
+ */
+
+METHODDEF(void)
+free_pool (j_common_ptr cinfo, int pool_id)
+{
+ my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+ small_pool_ptr shdr_ptr;
+ large_pool_ptr lhdr_ptr;
+ size_t space_freed;
+
+ if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+ ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
+
+#ifdef MEM_STATS
+ if (cinfo->err->trace_level > 1)
+ print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
+#endif
+
+ /* If freeing IMAGE pool, close any virtual arrays first */
+ if (pool_id == JPOOL_IMAGE) {
+ jvirt_sarray_ptr sptr;
+ jvirt_barray_ptr bptr;
+
+ for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+ if (sptr->b_s_open) { /* there may be no backing store */
+ sptr->b_s_open = FALSE; /* prevent recursive close if error */
+ (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
+ }
+ }
+ mem->virt_sarray_list = NULL;
+ for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+ if (bptr->b_s_open) { /* there may be no backing store */
+ bptr->b_s_open = FALSE; /* prevent recursive close if error */
+ (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
+ }
+ }
+ mem->virt_barray_list = NULL;
+ }
+
+ /* Release large objects */
+ lhdr_ptr = mem->large_list[pool_id];
+ mem->large_list[pool_id] = NULL;
+
+ while (lhdr_ptr != NULL) {
+ large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
+ space_freed = lhdr_ptr->hdr.bytes_used +
+ lhdr_ptr->hdr.bytes_left +
+ SIZEOF(large_pool_hdr);
+ jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
+ mem->total_space_allocated -= space_freed;
+ lhdr_ptr = next_lhdr_ptr;
+ }
+
+ /* Release small objects */
+ shdr_ptr = mem->small_list[pool_id];
+ mem->small_list[pool_id] = NULL;
+
+ while (shdr_ptr != NULL) {
+ small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
+ space_freed = shdr_ptr->hdr.bytes_used +
+ shdr_ptr->hdr.bytes_left +
+ SIZEOF(small_pool_hdr);
+ jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
+ mem->total_space_allocated -= space_freed;
+ shdr_ptr = next_shdr_ptr;
+ }
+}
+
+
+/*
+ * Close up shop entirely.
+ * Note that this cannot be called unless cinfo->mem is non-NULL.
+ */
+
+METHODDEF(void)
+self_destruct (j_common_ptr cinfo)
+{
+ int pool;
+
+ /* Close all backing store, release all memory.
+ * Releasing pools in reverse order might help avoid fragmentation
+ * with some (brain-damaged) malloc libraries.
+ */
+ for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
+ free_pool(cinfo, pool);
+ }
+
+ /* Release the memory manager control block too. */
+ jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
+ cinfo->mem = NULL; /* ensures I will be called only once */
+
+ jpeg_mem_term(cinfo); /* system-dependent cleanup */
+}
+
+
+/*
+ * Memory manager initialization.
+ * When this is called, only the error manager pointer is valid in cinfo!
+ */
+
+GLOBAL(void)
+jinit_memory_mgr (j_common_ptr cinfo)
+{
+ my_mem_ptr mem;
+ long max_to_use;
+ int pool;
+ size_t test_mac;
+
+ cinfo->mem = NULL; /* for safety if init fails */
+
+ /* Check for configuration errors.
+ * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
+ * doesn't reflect any real hardware alignment requirement.
+ * The test is a little tricky: for X>0, X and X-1 have no one-bits
+ * in common if and only if X is a power of 2, ie has only one one-bit.
+ * Some compilers may give an "unreachable code" warning here; ignore it.
+ */
+ if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
+ ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
+ /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
+ * a multiple of SIZEOF(ALIGN_TYPE).
+ * Again, an "unreachable code" warning may be ignored here.
+ * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
+ */
+ test_mac = (size_t) MAX_ALLOC_CHUNK;
+ if ((long) test_mac != MAX_ALLOC_CHUNK ||
+ (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
+ ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
+
+ max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
+
+ /* Attempt to allocate memory manager's control block */
+ mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
+
+ if (mem == NULL) {
+ jpeg_mem_term(cinfo); /* system-dependent cleanup */
+ ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
+ }
+
+ /* OK, fill in the method pointers */
+ mem->pub.alloc_small = alloc_small;
+ mem->pub.alloc_large = alloc_large;
+ mem->pub.alloc_sarray = alloc_sarray;
+ mem->pub.alloc_barray = alloc_barray;
+ mem->pub.request_virt_sarray = request_virt_sarray;
+ mem->pub.request_virt_barray = request_virt_barray;
+ mem->pub.realize_virt_arrays = realize_virt_arrays;
+ mem->pub.access_virt_sarray = access_virt_sarray;
+ mem->pub.access_virt_barray = access_virt_barray;
+ mem->pub.free_pool = free_pool;
+ mem->pub.self_destruct = self_destruct;
+
+ /* Make MAX_ALLOC_CHUNK accessible to other modules */
+ mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
+
+ /* Initialize working state */
+ mem->pub.max_memory_to_use = max_to_use;
+
+ for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
+ mem->small_list[pool] = NULL;
+ mem->large_list[pool] = NULL;
+ }
+ mem->virt_sarray_list = NULL;
+ mem->virt_barray_list = NULL;
+
+ mem->total_space_allocated = SIZEOF(my_memory_mgr);
+
+ /* Declare ourselves open for business */
+ cinfo->mem = & mem->pub;
+
+ /* Check for an environment variable JPEGMEM; if found, override the
+ * default max_memory setting from jpeg_mem_init. Note that the
+ * surrounding application may again override this value.
+ * If your system doesn't support getenv(), define NO_GETENV to disable
+ * this feature.
+ */
+#ifndef NO_GETENV
+ { char * memenv;
+
+ if ((memenv = getenv("JPEGMEM")) != NULL) {
+ char ch = 'x';
+
+ if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
+ if (ch == 'm' || ch == 'M')
+ max_to_use *= 1000L;
+ mem->pub.max_memory_to_use = max_to_use * 1000L;
+ }
+ }
+ }
+#endif
+
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c b/core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c
index 98aed723a6..f1f789a1a6 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jmemnobs.c
@@ -1,126 +1,126 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * 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
-
-#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
-extern "C" {
-#endif
-
-void* FXMEM_DefaultAlloc(int byte_size, int);
-void FXMEM_DefaultFree(void* pointer, int);
-
-#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
-}
-#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);
- return FXMEM_DefaultAlloc(sizeofobject, 0);
-}
-
-GLOBAL(void)
-jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
-{
-// free(object);
- FXMEM_DefaultFree(object, 0);
-}
-
-
-/*
- * "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);
- return FXMEM_DefaultAlloc(sizeofobject, 0);
-}
-
-GLOBAL(void)
-jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
-{
-// free(object);
- FXMEM_DefaultFree(object, 0);
-}
-
-
-/*
- * 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 */
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * 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
+
+#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
+extern "C" {
+#endif
+
+void* FXMEM_DefaultAlloc(int byte_size, int);
+void FXMEM_DefaultFree(void* pointer, int);
+
+#if defined(_FX_MANAGED_CODE_) && defined(__cplusplus)
+}
+#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);
+ return FXMEM_DefaultAlloc(sizeofobject, 0);
+}
+
+GLOBAL(void)
+jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
+{
+// free(object);
+ FXMEM_DefaultFree(object, 0);
+}
+
+
+/*
+ * "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);
+ return FXMEM_DefaultAlloc(sizeofobject, 0);
+}
+
+GLOBAL(void)
+jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
+{
+// free(object);
+ FXMEM_DefaultFree(object, 0);
+}
+
+
+/*
+ * 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 */
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/fpdfapi_jutils.c b/core/src/fxcodec/libjpeg/fpdfapi_jutils.c
index 251fd504e5..c6f7248af8 100644
--- a/core/src/fxcodec/libjpeg/fpdfapi_jutils.c
+++ b/core/src/fxcodec/libjpeg/fpdfapi_jutils.c
@@ -1,182 +1,182 @@
-#if !defined(_FX_JPEG_TURBO_)
-/*
- * jutils.c
- *
- * Copyright (C) 1991-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 contains tables and miscellaneous utility routines needed
- * for both compression and decompression.
- * Note we prefix all global names with "j" to minimize conflicts with
- * a surrounding application.
- */
-
-#define JPEG_INTERNALS
-#include "jinclude.h"
-#include "jpeglib.h"
-
-
-/*
- * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
- * of a DCT block read in natural order (left to right, top to bottom).
- */
-
-#if 0 /* This table is not actually needed in v6a */
-
-const int jpeg_zigzag_order[DCTSIZE2] = {
- 0, 1, 5, 6, 14, 15, 27, 28,
- 2, 4, 7, 13, 16, 26, 29, 42,
- 3, 8, 12, 17, 25, 30, 41, 43,
- 9, 11, 18, 24, 31, 40, 44, 53,
- 10, 19, 23, 32, 39, 45, 52, 54,
- 20, 22, 33, 38, 46, 51, 55, 60,
- 21, 34, 37, 47, 50, 56, 59, 61,
- 35, 36, 48, 49, 57, 58, 62, 63
-};
-
-#endif
-
-/*
- * jpeg_natural_order[i] is the natural-order position of the i'th element
- * of zigzag order.
- *
- * When reading corrupted data, the Huffman decoders could attempt
- * to reference an entry beyond the end of this array (if the decoded
- * zero run length reaches past the end of the block). To prevent
- * wild stores without adding an inner-loop test, we put some extra
- * "63"s after the real entries. This will cause the extra coefficient
- * to be stored in location 63 of the block, not somewhere random.
- * The worst case would be a run-length of 15, which means we need 16
- * fake entries.
- */
-
-const int jpeg_natural_order[DCTSIZE2+16] = {
- 0, 1, 8, 16, 9, 2, 3, 10,
- 17, 24, 32, 25, 18, 11, 4, 5,
- 12, 19, 26, 33, 40, 48, 41, 34,
- 27, 20, 13, 6, 7, 14, 21, 28,
- 35, 42, 49, 56, 57, 50, 43, 36,
- 29, 22, 15, 23, 30, 37, 44, 51,
- 58, 59, 52, 45, 38, 31, 39, 46,
- 53, 60, 61, 54, 47, 55, 62, 63,
- 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
- 63, 63, 63, 63, 63, 63, 63, 63
-};
-
-
-/*
- * Arithmetic utilities
- */
-
-GLOBAL(long)
-jdiv_round_up (long a, long b)
-/* Compute a/b rounded up to next integer, ie, ceil(a/b) */
-/* Assumes a >= 0, b > 0 */
-{
- return (a + b - 1L) / b;
-}
-
-
-GLOBAL(long)
-jround_up (long a, long b)
-/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
-/* Assumes a >= 0, b > 0 */
-{
- a += b - 1L;
- return a - (a % b);
-}
-
-
-/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
- * and coefficient-block arrays. This won't work on 80x86 because the arrays
- * are FAR and we're assuming a small-pointer memory model. However, some
- * DOS compilers provide far-pointer versions of memcpy() and memset() even
- * in the small-model libraries. These will be used if USE_FMEM is defined.
- * Otherwise, the routines below do it the hard way. (The performance cost
- * is not all that great, because these routines aren't very heavily used.)
- */
-
-#ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */
-#define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
-#define FMEMZERO(target,size) MEMZERO(target,size)
-#else /* 80x86 case, define if we can */
-#ifdef USE_FMEM
-#define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
-#define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size))
-#endif
-#endif
-
-
-GLOBAL(void)
-jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
- JSAMPARRAY output_array, int dest_row,
- int num_rows, JDIMENSION num_cols)
-/* Copy some rows of samples from one place to another.
- * num_rows rows are copied from input_array[source_row++]
- * to output_array[dest_row++]; these areas may overlap for duplication.
- * The source and destination arrays must be at least as wide as num_cols.
- */
-{
- register JSAMPROW inptr, outptr;
-#ifdef FMEMCOPY
- register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
-#else
- register JDIMENSION count;
-#endif
- register int row;
-
- input_array += source_row;
- output_array += dest_row;
-
- for (row = num_rows; row > 0; row--) {
- inptr = *input_array++;
- outptr = *output_array++;
-#ifdef FMEMCOPY
- FMEMCOPY(outptr, inptr, count);
-#else
- for (count = num_cols; count > 0; count--)
- *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */
-#endif
- }
-}
-
-
-GLOBAL(void)
-jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
- JDIMENSION num_blocks)
-/* Copy a row of coefficient blocks from one place to another. */
-{
-#ifdef FMEMCOPY
- FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
-#else
- register JCOEFPTR inptr, outptr;
- register long count;
-
- inptr = (JCOEFPTR) input_row;
- outptr = (JCOEFPTR) output_row;
- for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
- *outptr++ = *inptr++;
- }
-#endif
-}
-
-
-GLOBAL(void)
-jzero_far (void FAR * target, size_t bytestozero)
-/* Zero out a chunk of FAR memory. */
-/* This might be sample-array data, block-array data, or alloc_large data. */
-{
-#ifdef FMEMZERO
- FMEMZERO(target, bytestozero);
-#else
- register char FAR * ptr = (char FAR *) target;
- register size_t count;
-
- for (count = bytestozero; count > 0; count--) {
- *ptr++ = 0;
- }
-#endif
-}
-
-#endif //_FX_JPEG_TURBO_
+#if !defined(_FX_JPEG_TURBO_)
+/*
+ * jutils.c
+ *
+ * Copyright (C) 1991-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 contains tables and miscellaneous utility routines needed
+ * for both compression and decompression.
+ * Note we prefix all global names with "j" to minimize conflicts with
+ * a surrounding application.
+ */
+
+#define JPEG_INTERNALS
+#include "jinclude.h"
+#include "jpeglib.h"
+
+
+/*
+ * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
+ * of a DCT block read in natural order (left to right, top to bottom).
+ */
+
+#if 0 /* This table is not actually needed in v6a */
+
+const int jpeg_zigzag_order[DCTSIZE2] = {
+ 0, 1, 5, 6, 14, 15, 27, 28,
+ 2, 4, 7, 13, 16, 26, 29, 42,
+ 3, 8, 12, 17, 25, 30, 41, 43,
+ 9, 11, 18, 24, 31, 40, 44, 53,
+ 10, 19, 23, 32, 39, 45, 52, 54,
+ 20, 22, 33, 38, 46, 51, 55, 60,
+ 21, 34, 37, 47, 50, 56, 59, 61,
+ 35, 36, 48, 49, 57, 58, 62, 63
+};
+
+#endif
+
+/*
+ * jpeg_natural_order[i] is the natural-order position of the i'th element
+ * of zigzag order.
+ *
+ * When reading corrupted data, the Huffman decoders could attempt
+ * to reference an entry beyond the end of this array (if the decoded
+ * zero run length reaches past the end of the block). To prevent
+ * wild stores without adding an inner-loop test, we put some extra
+ * "63"s after the real entries. This will cause the extra coefficient
+ * to be stored in location 63 of the block, not somewhere random.
+ * The worst case would be a run-length of 15, which means we need 16
+ * fake entries.
+ */
+
+const int jpeg_natural_order[DCTSIZE2+16] = {
+ 0, 1, 8, 16, 9, 2, 3, 10,
+ 17, 24, 32, 25, 18, 11, 4, 5,
+ 12, 19, 26, 33, 40, 48, 41, 34,
+ 27, 20, 13, 6, 7, 14, 21, 28,
+ 35, 42, 49, 56, 57, 50, 43, 36,
+ 29, 22, 15, 23, 30, 37, 44, 51,
+ 58, 59, 52, 45, 38, 31, 39, 46,
+ 53, 60, 61, 54, 47, 55, 62, 63,
+ 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
+ 63, 63, 63, 63, 63, 63, 63, 63
+};
+
+
+/*
+ * Arithmetic utilities
+ */
+
+GLOBAL(long)
+jdiv_round_up (long a, long b)
+/* Compute a/b rounded up to next integer, ie, ceil(a/b) */
+/* Assumes a >= 0, b > 0 */
+{
+ return (a + b - 1L) / b;
+}
+
+
+GLOBAL(long)
+jround_up (long a, long b)
+/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
+/* Assumes a >= 0, b > 0 */
+{
+ a += b - 1L;
+ return a - (a % b);
+}
+
+
+/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
+ * and coefficient-block arrays. This won't work on 80x86 because the arrays
+ * are FAR and we're assuming a small-pointer memory model. However, some
+ * DOS compilers provide far-pointer versions of memcpy() and memset() even
+ * in the small-model libraries. These will be used if USE_FMEM is defined.
+ * Otherwise, the routines below do it the hard way. (The performance cost
+ * is not all that great, because these routines aren't very heavily used.)
+ */
+
+#ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */
+#define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
+#define FMEMZERO(target,size) MEMZERO(target,size)
+#else /* 80x86 case, define if we can */
+#ifdef USE_FMEM
+#define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
+#define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size))
+#endif
+#endif
+
+
+GLOBAL(void)
+jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
+ JSAMPARRAY output_array, int dest_row,
+ int num_rows, JDIMENSION num_cols)
+/* Copy some rows of samples from one place to another.
+ * num_rows rows are copied from input_array[source_row++]
+ * to output_array[dest_row++]; these areas may overlap for duplication.
+ * The source and destination arrays must be at least as wide as num_cols.
+ */
+{
+ register JSAMPROW inptr, outptr;
+#ifdef FMEMCOPY
+ register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
+#else
+ register JDIMENSION count;
+#endif
+ register int row;
+
+ input_array += source_row;
+ output_array += dest_row;
+
+ for (row = num_rows; row > 0; row--) {
+ inptr = *input_array++;
+ outptr = *output_array++;
+#ifdef FMEMCOPY
+ FMEMCOPY(outptr, inptr, count);
+#else
+ for (count = num_cols; count > 0; count--)
+ *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */
+#endif
+ }
+}
+
+
+GLOBAL(void)
+jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
+ JDIMENSION num_blocks)
+/* Copy a row of coefficient blocks from one place to another. */
+{
+#ifdef FMEMCOPY
+ FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
+#else
+ register JCOEFPTR inptr, outptr;
+ register long count;
+
+ inptr = (JCOEFPTR) input_row;
+ outptr = (JCOEFPTR) output_row;
+ for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
+ *outptr++ = *inptr++;
+ }
+#endif
+}
+
+
+GLOBAL(void)
+jzero_far (void FAR * target, size_t bytestozero)
+/* Zero out a chunk of FAR memory. */
+/* This might be sample-array data, block-array data, or alloc_large data. */
+{
+#ifdef FMEMZERO
+ FMEMZERO(target, bytestozero);
+#else
+ register char FAR * ptr = (char FAR *) target;
+ register size_t count;
+
+ for (count = bytestozero; count > 0; count--) {
+ *ptr++ = 0;
+ }
+#endif
+}
+
+#endif //_FX_JPEG_TURBO_
diff --git a/core/src/fxcodec/libjpeg/jchuff.h b/core/src/fxcodec/libjpeg/jchuff.h
index 8c02c09ad0..a9599fc1e6 100644
--- a/core/src/fxcodec/libjpeg/jchuff.h
+++ b/core/src/fxcodec/libjpeg/jchuff.h
@@ -1,47 +1,47 @@
-/*
- * 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[]));
+/*
+ * 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[]));
diff --git a/core/src/fxcodec/libjpeg/jconfig.h b/core/src/fxcodec/libjpeg/jconfig.h
index 2f4da14c54..7e291c75bd 100644
--- a/core/src/fxcodec/libjpeg/jconfig.h
+++ b/core/src/fxcodec/libjpeg/jconfig.h
@@ -1,45 +1,45 @@
-/* jconfig.vc --- jconfig.h for Microsoft Visual C++ on Windows 95 or NT. */
-/* see jconfig.doc for explanations */
-
-#define HAVE_PROTOTYPES
-#define HAVE_UNSIGNED_CHAR
-#define HAVE_UNSIGNED_SHORT
-/* #define void char */
-/* #define const */
-#undef CHAR_IS_UNSIGNED
-#define HAVE_STDDEF_H
-#define HAVE_STDLIB_H
-#undef NEED_BSD_STRINGS
-#undef NEED_SYS_TYPES_H
-#undef NEED_FAR_POINTERS /* we presume a 32-bit flat memory model */
-#undef NEED_SHORT_EXTERNAL_NAMES
-#undef INCOMPLETE_TYPES_BROKEN
-
-/* Define "boolean" as unsigned char, not int, per Windows custom */
-#ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
-typedef unsigned char boolean;
-#endif
-#define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
-
-
-#ifdef JPEG_INTERNALS
-
-#undef RIGHT_SHIFT_IS_UNSIGNED
-
-#endif /* JPEG_INTERNALS */
-
-#ifdef JPEG_CJPEG_DJPEG
-
-#define BMP_SUPPORTED /* BMP image file format */
-#define GIF_SUPPORTED /* GIF image file format */
-#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
-#undef RLE_SUPPORTED /* Utah RLE image file format */
-#define TARGA_SUPPORTED /* Targa image file format */
-
-#define TWO_FILE_COMMANDLINE /* optional */
-#define USE_SETMODE /* Microsoft has setmode() */
-#undef NEED_SIGNAL_CATCHER
-#undef DONT_USE_B_MODE
-#undef PROGRESS_REPORT /* optional */
-
-#endif /* JPEG_CJPEG_DJPEG */
+/* jconfig.vc --- jconfig.h for Microsoft Visual C++ on Windows 95 or NT. */
+/* see jconfig.doc for explanations */
+
+#define HAVE_PROTOTYPES
+#define HAVE_UNSIGNED_CHAR
+#define HAVE_UNSIGNED_SHORT
+/* #define void char */
+/* #define const */
+#undef CHAR_IS_UNSIGNED
+#define HAVE_STDDEF_H
+#define HAVE_STDLIB_H
+#undef NEED_BSD_STRINGS
+#undef NEED_SYS_TYPES_H
+#undef NEED_FAR_POINTERS /* we presume a 32-bit flat memory model */
+#undef NEED_SHORT_EXTERNAL_NAMES
+#undef INCOMPLETE_TYPES_BROKEN
+
+/* Define "boolean" as unsigned char, not int, per Windows custom */
+#ifndef __RPCNDR_H__ /* don't conflict if rpcndr.h already read */
+typedef unsigned char boolean;
+#endif
+#define HAVE_BOOLEAN /* prevent jmorecfg.h from redefining it */
+
+
+#ifdef JPEG_INTERNALS
+
+#undef RIGHT_SHIFT_IS_UNSIGNED
+
+#endif /* JPEG_INTERNALS */
+
+#ifdef JPEG_CJPEG_DJPEG
+
+#define BMP_SUPPORTED /* BMP image file format */
+#define GIF_SUPPORTED /* GIF image file format */
+#define PPM_SUPPORTED /* PBMPLUS PPM/PGM image file format */
+#undef RLE_SUPPORTED /* Utah RLE image file format */
+#define TARGA_SUPPORTED /* Targa image file format */
+
+#define TWO_FILE_COMMANDLINE /* optional */
+#define USE_SETMODE /* Microsoft has setmode() */
+#undef NEED_SIGNAL_CATCHER
+#undef DONT_USE_B_MODE
+#undef PROGRESS_REPORT /* optional */
+
+#endif /* JPEG_CJPEG_DJPEG */
diff --git a/core/src/fxcodec/libjpeg/jdct.h b/core/src/fxcodec/libjpeg/jdct.h
index b664cab0d2..04192a266a 100644
--- a/core/src/fxcodec/libjpeg/jdct.h
+++ b/core/src/fxcodec/libjpeg/jdct.h
@@ -1,176 +1,176 @@
-/*
- * jdct.h
- *
- * Copyright (C) 1994-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 include file contains common declarations for the forward and
- * inverse DCT modules. These declarations are private to the DCT managers
- * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms.
- * The individual DCT algorithms are kept in separate files to ease
- * machine-dependent tuning (e.g., assembly coding).
- */
-
-
-/*
- * A forward DCT routine is given a pointer to a work area of type DCTELEM[];
- * the DCT is to be performed in-place in that buffer. Type DCTELEM is int
- * for 8-bit samples, INT32 for 12-bit samples. (NOTE: Floating-point DCT
- * implementations use an array of type FAST_FLOAT, instead.)
- * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
- * The DCT outputs are returned scaled up by a factor of 8; they therefore
- * have a range of +-8K for 8-bit data, +-128K for 12-bit data. This
- * convention improves accuracy in integer implementations and saves some
- * work in floating-point ones.
- * Quantization of the output coefficients is done by jcdctmgr.c.
- */
-
-#if BITS_IN_JSAMPLE == 8
-typedef int DCTELEM; /* 16 or 32 bits is fine */
-#else
-typedef INT32 DCTELEM; /* must have 32 bits */
-#endif
-
-typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data));
-typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data));
-
-
-/*
- * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer
- * to an output sample array. The routine must dequantize the input data as
- * well as perform the IDCT; for dequantization, it uses the multiplier table
- * pointed to by compptr->dct_table. The output data is to be placed into the
- * sample array starting at a specified column. (Any row offset needed will
- * be applied to the array pointer before it is passed to the IDCT code.)
- * Note that the number of samples emitted by the IDCT routine is
- * DCT_scaled_size * DCT_scaled_size.
- */
-
-/* typedef inverse_DCT_method_ptr is declared in jpegint.h */
-
-/*
- * Each IDCT routine has its own ideas about the best dct_table element type.
- */
-
-typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
-#if BITS_IN_JSAMPLE == 8
-typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
-#define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */
-#else
-typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */
-#define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */
-#endif
-typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
-
-
-/*
- * Each IDCT routine is responsible for range-limiting its results and
- * converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
- * be quite far out of range if the input data is corrupt, so a bulletproof
- * range-limiting step is required. We use a mask-and-table-lookup method
- * to do the combined operations quickly. See the comments with
- * prepare_range_limit_table (in jdmaster.c) for more info.
- */
-
-#define IDCT_range_limit(cinfo) ((cinfo)->sample_range_limit + CENTERJSAMPLE)
-
-#define RANGE_MASK (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */
-
-
-/* Short forms of external names for systems with brain-damaged linkers. */
-
-#ifdef NEED_SHORT_EXTERNAL_NAMES
-#define jpeg_fdct_islow jFDislow
-#define jpeg_fdct_ifast jFDifast
-#define jpeg_fdct_float jFDfloat
-#define jpeg_idct_islow jRDislow
-#define jpeg_idct_ifast jRDifast
-#define jpeg_idct_float jRDfloat
-#define jpeg_idct_4x4 jRD4x4
-#define jpeg_idct_2x2 jRD2x2
-#define jpeg_idct_1x1 jRD1x1
-#endif /* NEED_SHORT_EXTERNAL_NAMES */
-
-/* Extern declarations for the forward and inverse DCT routines. */
-
-EXTERN(void) jpeg_fdct_islow JPP((DCTELEM * data));
-EXTERN(void) jpeg_fdct_ifast JPP((DCTELEM * data));
-EXTERN(void) jpeg_fdct_float JPP((FAST_FLOAT * data));
-
-EXTERN(void) jpeg_idct_islow
- JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
-EXTERN(void) jpeg_idct_ifast
- JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
-EXTERN(void) jpeg_idct_float
- JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
-EXTERN(void) jpeg_idct_4x4
- JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
-EXTERN(void) jpeg_idct_2x2
- JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
-EXTERN(void) jpeg_idct_1x1
- JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
-
-
-/*
- * Macros for handling fixed-point arithmetic; these are used by many
- * but not all of the DCT/IDCT modules.
- *
- * All values are expected to be of type INT32.
- * Fractional constants are scaled left by CONST_BITS bits.
- * CONST_BITS is defined within each module using these macros,
- * and may differ from one module to the next.
- */
-
-#define ONE ((INT32) 1)
-#define CONST_SCALE (ONE << CONST_BITS)
-
-/* Convert a positive real constant to an integer scaled by CONST_SCALE.
- * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
- * thus causing a lot of useless floating-point operations at run time.
- */
-
-#define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5))
-
-/* Descale and correctly round an INT32 value that's scaled by N bits.
- * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
- * the fudge factor is correct for either sign of X.
- */
-
-#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
-
-/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
- * This macro is used only when the two inputs will actually be no more than
- * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a
- * full 32x32 multiply. This provides a useful speedup on many machines.
- * Unfortunately there is no way to specify a 16x16->32 multiply portably
- * in C, but some C compilers will do the right thing if you provide the
- * correct combination of casts.
- */
-
-#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
-#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const)))
-#endif
-#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
-#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const)))
-#endif
-
-#ifndef MULTIPLY16C16 /* default definition */
-#define MULTIPLY16C16(var,const) ((var) * (const))
-#endif
-
-/* Same except both inputs are variables. */
-
-#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
-#define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2)))
-#endif
-
-#ifndef MULTIPLY16V16 /* default definition */
-#define MULTIPLY16V16(var1,var2) ((var1) * (var2))
-#endif
+/*
+ * jdct.h
+ *
+ * Copyright (C) 1994-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 include file contains common declarations for the forward and
+ * inverse DCT modules. These declarations are private to the DCT managers
+ * (jcdctmgr.c, jddctmgr.c) and the individual DCT algorithms.
+ * The individual DCT algorithms are kept in separate files to ease
+ * machine-dependent tuning (e.g., assembly coding).
+ */
+
+
+/*
+ * A forward DCT routine is given a pointer to a work area of type DCTELEM[];
+ * the DCT is to be performed in-place in that buffer. Type DCTELEM is int
+ * for 8-bit samples, INT32 for 12-bit samples. (NOTE: Floating-point DCT
+ * implementations use an array of type FAST_FLOAT, instead.)
+ * The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
+ * The DCT outputs are returned scaled up by a factor of 8; they therefore
+ * have a range of +-8K for 8-bit data, +-128K for 12-bit data. This
+ * convention improves accuracy in integer implementations and saves some
+ * work in floating-point ones.
+ * Quantization of the output coefficients is done by jcdctmgr.c.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+typedef int DCTELEM; /* 16 or 32 bits is fine */
+#else
+typedef INT32 DCTELEM; /* must have 32 bits */
+#endif
+
+typedef JMETHOD(void, forward_DCT_method_ptr, (DCTELEM * data));
+typedef JMETHOD(void, float_DCT_method_ptr, (FAST_FLOAT * data));
+
+
+/*
+ * An inverse DCT routine is given a pointer to the input JBLOCK and a pointer
+ * to an output sample array. The routine must dequantize the input data as
+ * well as perform the IDCT; for dequantization, it uses the multiplier table
+ * pointed to by compptr->dct_table. The output data is to be placed into the
+ * sample array starting at a specified column. (Any row offset needed will
+ * be applied to the array pointer before it is passed to the IDCT code.)
+ * Note that the number of samples emitted by the IDCT routine is
+ * DCT_scaled_size * DCT_scaled_size.
+ */
+
+/* typedef inverse_DCT_method_ptr is declared in jpegint.h */
+
+/*
+ * Each IDCT routine has its own ideas about the best dct_table element type.
+ */
+
+typedef MULTIPLIER ISLOW_MULT_TYPE; /* short or int, whichever is faster */
+#if BITS_IN_JSAMPLE == 8
+typedef MULTIPLIER IFAST_MULT_TYPE; /* 16 bits is OK, use short if faster */
+#define IFAST_SCALE_BITS 2 /* fractional bits in scale factors */
+#else
+typedef INT32 IFAST_MULT_TYPE; /* need 32 bits for scaled quantizers */
+#define IFAST_SCALE_BITS 13 /* fractional bits in scale factors */
+#endif
+typedef FAST_FLOAT FLOAT_MULT_TYPE; /* preferred floating type */
+
+
+/*
+ * Each IDCT routine is responsible for range-limiting its results and
+ * converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
+ * be quite far out of range if the input data is corrupt, so a bulletproof
+ * range-limiting step is required. We use a mask-and-table-lookup method
+ * to do the combined operations quickly. See the comments with
+ * prepare_range_limit_table (in jdmaster.c) for more info.
+ */
+
+#define IDCT_range_limit(cinfo) ((cinfo)->sample_range_limit + CENTERJSAMPLE)
+
+#define RANGE_MASK (MAXJSAMPLE * 4 + 3) /* 2 bits wider than legal samples */
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_fdct_islow jFDislow
+#define jpeg_fdct_ifast jFDifast
+#define jpeg_fdct_float jFDfloat
+#define jpeg_idct_islow jRDislow
+#define jpeg_idct_ifast jRDifast
+#define jpeg_idct_float jRDfloat
+#define jpeg_idct_4x4 jRD4x4
+#define jpeg_idct_2x2 jRD2x2
+#define jpeg_idct_1x1 jRD1x1
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+/* Extern declarations for the forward and inverse DCT routines. */
+
+EXTERN(void) jpeg_fdct_islow JPP((DCTELEM * data));
+EXTERN(void) jpeg_fdct_ifast JPP((DCTELEM * data));
+EXTERN(void) jpeg_fdct_float JPP((FAST_FLOAT * data));
+
+EXTERN(void) jpeg_idct_islow
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_ifast
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_float
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_4x4
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_2x2
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+EXTERN(void) jpeg_idct_1x1
+ JPP((j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col));
+
+
+/*
+ * Macros for handling fixed-point arithmetic; these are used by many
+ * but not all of the DCT/IDCT modules.
+ *
+ * All values are expected to be of type INT32.
+ * Fractional constants are scaled left by CONST_BITS bits.
+ * CONST_BITS is defined within each module using these macros,
+ * and may differ from one module to the next.
+ */
+
+#define ONE ((INT32) 1)
+#define CONST_SCALE (ONE << CONST_BITS)
+
+/* Convert a positive real constant to an integer scaled by CONST_SCALE.
+ * Caution: some C compilers fail to reduce "FIX(constant)" at compile time,
+ * thus causing a lot of useless floating-point operations at run time.
+ */
+
+#define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5))
+
+/* Descale and correctly round an INT32 value that's scaled by N bits.
+ * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
+ * the fudge factor is correct for either sign of X.
+ */
+
+#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
+
+/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
+ * This macro is used only when the two inputs will actually be no more than
+ * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a
+ * full 32x32 multiply. This provides a useful speedup on many machines.
+ * Unfortunately there is no way to specify a 16x16->32 multiply portably
+ * in C, but some C compilers will do the right thing if you provide the
+ * correct combination of casts.
+ */
+
+#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
+#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT16) (const)))
+#endif
+#ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
+#define MULTIPLY16C16(var,const) (((INT16) (var)) * ((INT32) (const)))
+#endif
+
+#ifndef MULTIPLY16C16 /* default definition */
+#define MULTIPLY16C16(var,const) ((var) * (const))
+#endif
+
+/* Same except both inputs are variables. */
+
+#ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
+#define MULTIPLY16V16(var1,var2) (((INT16) (var1)) * ((INT16) (var2)))
+#endif
+
+#ifndef MULTIPLY16V16 /* default definition */
+#define MULTIPLY16V16(var1,var2) ((var1) * (var2))
+#endif
diff --git a/core/src/fxcodec/libjpeg/jdhuff.h b/core/src/fxcodec/libjpeg/jdhuff.h
index 12c0747709..ae19b6cafd 100644
--- a/core/src/fxcodec/libjpeg/jdhuff.h
+++ b/core/src/fxcodec/libjpeg/jdhuff.h
@@ -1,201 +1,201 @@
-/*
- * 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));
+/*
+ * 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));
diff --git a/core/src/fxcodec/libjpeg/jerror.h b/core/src/fxcodec/libjpeg/jerror.h
index a2b8f96f8f..06d344067a 100644
--- a/core/src/fxcodec/libjpeg/jerror.h
+++ b/core/src/fxcodec/libjpeg/jerror.h
@@ -1,291 +1,291 @@
-/*
- * jerror.h
- *
- * Copyright (C) 1994-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 defines the error and message codes for the JPEG library.
- * Edit this file to add new codes, or to translate the message strings to
- * some other language.
- * A set of error-reporting macros are defined too. Some applications using
- * the JPEG library may wish to include this file to get the error codes
- * and/or the macros.
- */
-
-/*
- * To define the enum list of message codes, include this file without
- * defining macro JMESSAGE. To create a message string table, include it
- * again with a suitable JMESSAGE definition (see jerror.c for an example).
- */
-#ifndef JMESSAGE
-#ifndef JERROR_H
-/* First time through, define the enum list */
-#define JMAKE_ENUM_LIST
-#else
-/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
-#define JMESSAGE(code,string)
-#endif /* JERROR_H */
-#endif /* JMESSAGE */
-
-#ifdef JMAKE_ENUM_LIST
-
-typedef enum {
-
-#define JMESSAGE(code,string) code ,
-
-#endif /* JMAKE_ENUM_LIST */
-
-JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */
-
-/* For maintenance convenience, list is alphabetical by message code name */
-JMESSAGE(JERR_ARITH_NOTIMPL,
- "Sorry, there are legal restrictions on arithmetic coding")
-JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
-JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
-JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
-JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
-JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
-JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
-JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
-JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
-JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
-JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
-JMESSAGE(JERR_BAD_LIB_VERSION,
- "Wrong JPEG library version: library is %d, caller expects %d")
-JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
-JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
-JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
-JMESSAGE(JERR_BAD_PROGRESSION,
- "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
-JMESSAGE(JERR_BAD_PROG_SCRIPT,
- "Invalid progressive parameters at scan script entry %d")
-JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
-JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
-JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
-JMESSAGE(JERR_BAD_STRUCT_SIZE,
- "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
-JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
-JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
-JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
-JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
-JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
-JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
-JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
-JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
-JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
-JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
-JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
-JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
-JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
-JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
-JMESSAGE(JERR_FILE_READ, "Input file read error")
-JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
-JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
-JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
-JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
-JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
-JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
-JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
-JMESSAGE(JERR_MISMATCHED_QUANT_TABLE,
- "Cannot transcode due to multiple use of quantization table %d")
-JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
-JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
-JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
-JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
-JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
-JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
-JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
-JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
-JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
-JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
-JMESSAGE(JERR_QUANT_COMPONENTS,
- "Cannot quantize more than %d color components")
-JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
-JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
-JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
-JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
-JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
-JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
-JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
-JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
-JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
-JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
-JMESSAGE(JERR_TFILE_WRITE,
- "Write failed on temporary file --- out of disk space?")
-JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
-JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
-JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
-JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
-JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
-JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
-JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
-JMESSAGE(JMSG_VERSION, JVERSION)
-JMESSAGE(JTRC_16BIT_TABLES,
- "Caution: quantization tables are too coarse for baseline JPEG")
-JMESSAGE(JTRC_ADOBE,
- "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
-JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
-JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
-JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
-JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
-JMESSAGE(JTRC_DQT, "Define Quantization Table %d precision %d")
-JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
-JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
-JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
-JMESSAGE(JTRC_EOI, "End Of Image")
-JMESSAGE(JTRC_HUFFBITS, " %3d %3d %3d %3d %3d %3d %3d %3d")
-JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d %d")
-JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
- "Warning: thumbnail image size does not match data length %u")
-JMESSAGE(JTRC_JFIF_EXTENSION,
- "JFIF extension marker: type 0x%02x, length %u")
-JMESSAGE(JTRC_JFIF_THUMBNAIL, " with %d x %d thumbnail image")
-JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
-JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
-JMESSAGE(JTRC_QUANTVALS, " %4u %4u %4u %4u %4u %4u %4u %4u")
-JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
-JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
-JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
-JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
-JMESSAGE(JTRC_RST, "RST%d")
-JMESSAGE(JTRC_SMOOTH_NOTIMPL,
- "Smoothing not supported with nonstandard sampling ratios")
-JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
-JMESSAGE(JTRC_SOF_COMPONENT, " Component %d: %dhx%dv q=%d")
-JMESSAGE(JTRC_SOI, "Start of Image")
-JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
-JMESSAGE(JTRC_SOS_COMPONENT, " Component %d: dc=%d ac=%d")
-JMESSAGE(JTRC_SOS_PARAMS, " Ss=%d, Se=%d, Ah=%d, Al=%d")
-JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
-JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
-JMESSAGE(JTRC_THUMB_JPEG,
- "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
-JMESSAGE(JTRC_THUMB_PALETTE,
- "JFIF extension marker: palette thumbnail image, length %u")
-JMESSAGE(JTRC_THUMB_RGB,
- "JFIF extension marker: RGB thumbnail image, length %u")
-JMESSAGE(JTRC_UNKNOWN_IDS,
- "Unrecognized component IDs %d %d %d, assuming YCbCr")
-JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
-JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
-JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
-JMESSAGE(JWRN_BOGUS_PROGRESSION,
- "Inconsistent progression sequence for component %d coefficient %d")
-JMESSAGE(JWRN_EXTRANEOUS_DATA,
- "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
-JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
-JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
-JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
-JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
-JMESSAGE(JWRN_MUST_RESYNC,
- "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
-JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
-JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
-
-#ifdef JMAKE_ENUM_LIST
-
- JMSG_LASTMSGCODE
-} J_MESSAGE_CODE;
-
-#undef JMAKE_ENUM_LIST
-#endif /* JMAKE_ENUM_LIST */
-
-/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
-#undef JMESSAGE
-
-
-#ifndef JERROR_H
-#define JERROR_H
-
-/* Macros to simplify using the error and trace message stuff */
-/* The first parameter is either type of cinfo pointer */
-
-/* Fatal errors (print message and exit) */
-#define ERREXIT(cinfo,code) \
- ((cinfo)->err->msg_code = (code), \
- (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
-#define ERREXIT1(cinfo,code,p1) \
- ((cinfo)->err->msg_code = (code), \
- (cinfo)->err->msg_parm.i[0] = (p1), \
- (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
-#define ERREXIT2(cinfo,code,p1,p2) \
- ((cinfo)->err->msg_code = (code), \
- (cinfo)->err->msg_parm.i[0] = (p1), \
- (cinfo)->err->msg_parm.i[1] = (p2), \
- (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
-#define ERREXIT3(cinfo,code,p1,p2,p3) \
- ((cinfo)->err->msg_code = (code), \
- (cinfo)->err->msg_parm.i[0] = (p1), \
- (cinfo)->err->msg_parm.i[1] = (p2), \
- (cinfo)->err->msg_parm.i[2] = (p3), \
- (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
-#define ERREXIT4(cinfo,code,p1,p2,p3,p4) \
- ((cinfo)->err->msg_code = (code), \
- (cinfo)->err->msg_parm.i[0] = (p1), \
- (cinfo)->err->msg_parm.i[1] = (p2), \
- (cinfo)->err->msg_parm.i[2] = (p3), \
- (cinfo)->err->msg_parm.i[3] = (p4), \
- (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
-#define ERREXITS(cinfo,code,str) \
- ((cinfo)->err->msg_code = (code), \
- FXSYS_strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
- (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
-
-#define MAKESTMT(stuff) do { stuff } while (0)
-
-/* Nonfatal errors (we can keep going, but the data is probably corrupt) */
-#define WARNMS(cinfo,code) \
- ((cinfo)->err->msg_code = (code), \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
-#define WARNMS1(cinfo,code,p1) \
- ((cinfo)->err->msg_code = (code), \
- (cinfo)->err->msg_parm.i[0] = (p1), \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
-#define WARNMS2(cinfo,code,p1,p2) \
- ((cinfo)->err->msg_code = (code), \
- (cinfo)->err->msg_parm.i[0] = (p1), \
- (cinfo)->err->msg_parm.i[1] = (p2), \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
-
-/* Informational/debugging messages */
-#define TRACEMS(cinfo,lvl,code) \
- ((cinfo)->err->msg_code = (code), \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
-#define TRACEMS1(cinfo,lvl,code,p1) \
- ((cinfo)->err->msg_code = (code), \
- (cinfo)->err->msg_parm.i[0] = (p1), \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
-#define TRACEMS2(cinfo,lvl,code,p1,p2) \
- ((cinfo)->err->msg_code = (code), \
- (cinfo)->err->msg_parm.i[0] = (p1), \
- (cinfo)->err->msg_parm.i[1] = (p2), \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
-#define TRACEMS3(cinfo,lvl,code,p1,p2,p3) \
- MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
- _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
- (cinfo)->err->msg_code = (code); \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
-#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4) \
- MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
- _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
- (cinfo)->err->msg_code = (code); \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
-#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5) \
- MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
- _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
- _mp[4] = (p5); \
- (cinfo)->err->msg_code = (code); \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
-#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8) \
- MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
- _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
- _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
- (cinfo)->err->msg_code = (code); \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
-#define TRACEMSS(cinfo,lvl,code,str) \
- ((cinfo)->err->msg_code = (code), \
- FXSYS_strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
- (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
-
-#endif /* JERROR_H */
+/*
+ * jerror.h
+ *
+ * Copyright (C) 1994-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 defines the error and message codes for the JPEG library.
+ * Edit this file to add new codes, or to translate the message strings to
+ * some other language.
+ * A set of error-reporting macros are defined too. Some applications using
+ * the JPEG library may wish to include this file to get the error codes
+ * and/or the macros.
+ */
+
+/*
+ * To define the enum list of message codes, include this file without
+ * defining macro JMESSAGE. To create a message string table, include it
+ * again with a suitable JMESSAGE definition (see jerror.c for an example).
+ */
+#ifndef JMESSAGE
+#ifndef JERROR_H
+/* First time through, define the enum list */
+#define JMAKE_ENUM_LIST
+#else
+/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
+#define JMESSAGE(code,string)
+#endif /* JERROR_H */
+#endif /* JMESSAGE */
+
+#ifdef JMAKE_ENUM_LIST
+
+typedef enum {
+
+#define JMESSAGE(code,string) code ,
+
+#endif /* JMAKE_ENUM_LIST */
+
+JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */
+
+/* For maintenance convenience, list is alphabetical by message code name */
+JMESSAGE(JERR_ARITH_NOTIMPL,
+ "Sorry, there are legal restrictions on arithmetic coding")
+JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
+JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
+JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
+JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
+JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
+JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
+JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
+JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
+JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
+JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
+JMESSAGE(JERR_BAD_LIB_VERSION,
+ "Wrong JPEG library version: library is %d, caller expects %d")
+JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
+JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
+JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
+JMESSAGE(JERR_BAD_PROGRESSION,
+ "Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
+JMESSAGE(JERR_BAD_PROG_SCRIPT,
+ "Invalid progressive parameters at scan script entry %d")
+JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
+JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
+JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
+JMESSAGE(JERR_BAD_STRUCT_SIZE,
+ "JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
+JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
+JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
+JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
+JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
+JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
+JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
+JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
+JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
+JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
+JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
+JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
+JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
+JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
+JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
+JMESSAGE(JERR_FILE_READ, "Input file read error")
+JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
+JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
+JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
+JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
+JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
+JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
+JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
+JMESSAGE(JERR_MISMATCHED_QUANT_TABLE,
+ "Cannot transcode due to multiple use of quantization table %d")
+JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
+JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
+JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
+JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
+JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
+JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
+JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
+JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
+JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
+JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
+JMESSAGE(JERR_QUANT_COMPONENTS,
+ "Cannot quantize more than %d color components")
+JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
+JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
+JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
+JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
+JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
+JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
+JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
+JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
+JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
+JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
+JMESSAGE(JERR_TFILE_WRITE,
+ "Write failed on temporary file --- out of disk space?")
+JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
+JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
+JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
+JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
+JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
+JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
+JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
+JMESSAGE(JMSG_VERSION, JVERSION)
+JMESSAGE(JTRC_16BIT_TABLES,
+ "Caution: quantization tables are too coarse for baseline JPEG")
+JMESSAGE(JTRC_ADOBE,
+ "Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
+JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
+JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
+JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
+JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
+JMESSAGE(JTRC_DQT, "Define Quantization Table %d precision %d")
+JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
+JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
+JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
+JMESSAGE(JTRC_EOI, "End Of Image")
+JMESSAGE(JTRC_HUFFBITS, " %3d %3d %3d %3d %3d %3d %3d %3d")
+JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d %d")
+JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
+ "Warning: thumbnail image size does not match data length %u")
+JMESSAGE(JTRC_JFIF_EXTENSION,
+ "JFIF extension marker: type 0x%02x, length %u")
+JMESSAGE(JTRC_JFIF_THUMBNAIL, " with %d x %d thumbnail image")
+JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
+JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
+JMESSAGE(JTRC_QUANTVALS, " %4u %4u %4u %4u %4u %4u %4u %4u")
+JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
+JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
+JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
+JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
+JMESSAGE(JTRC_RST, "RST%d")
+JMESSAGE(JTRC_SMOOTH_NOTIMPL,
+ "Smoothing not supported with nonstandard sampling ratios")
+JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
+JMESSAGE(JTRC_SOF_COMPONENT, " Component %d: %dhx%dv q=%d")
+JMESSAGE(JTRC_SOI, "Start of Image")
+JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
+JMESSAGE(JTRC_SOS_COMPONENT, " Component %d: dc=%d ac=%d")
+JMESSAGE(JTRC_SOS_PARAMS, " Ss=%d, Se=%d, Ah=%d, Al=%d")
+JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
+JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
+JMESSAGE(JTRC_THUMB_JPEG,
+ "JFIF extension marker: JPEG-compressed thumbnail image, length %u")
+JMESSAGE(JTRC_THUMB_PALETTE,
+ "JFIF extension marker: palette thumbnail image, length %u")
+JMESSAGE(JTRC_THUMB_RGB,
+ "JFIF extension marker: RGB thumbnail image, length %u")
+JMESSAGE(JTRC_UNKNOWN_IDS,
+ "Unrecognized component IDs %d %d %d, assuming YCbCr")
+JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
+JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
+JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
+JMESSAGE(JWRN_BOGUS_PROGRESSION,
+ "Inconsistent progression sequence for component %d coefficient %d")
+JMESSAGE(JWRN_EXTRANEOUS_DATA,
+ "Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
+JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
+JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
+JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
+JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
+JMESSAGE(JWRN_MUST_RESYNC,
+ "Corrupt JPEG data: found marker 0x%02x instead of RST%d")
+JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
+JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
+
+#ifdef JMAKE_ENUM_LIST
+
+ JMSG_LASTMSGCODE
+} J_MESSAGE_CODE;
+
+#undef JMAKE_ENUM_LIST
+#endif /* JMAKE_ENUM_LIST */
+
+/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
+#undef JMESSAGE
+
+
+#ifndef JERROR_H
+#define JERROR_H
+
+/* Macros to simplify using the error and trace message stuff */
+/* The first parameter is either type of cinfo pointer */
+
+/* Fatal errors (print message and exit) */
+#define ERREXIT(cinfo,code) \
+ ((cinfo)->err->msg_code = (code), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT1(cinfo,code,p1) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT2(cinfo,code,p1,p2) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT3(cinfo,code,p1,p2,p3) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (cinfo)->err->msg_parm.i[2] = (p3), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXIT4(cinfo,code,p1,p2,p3,p4) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (cinfo)->err->msg_parm.i[2] = (p3), \
+ (cinfo)->err->msg_parm.i[3] = (p4), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+#define ERREXITS(cinfo,code,str) \
+ ((cinfo)->err->msg_code = (code), \
+ FXSYS_strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
+ (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
+
+#define MAKESTMT(stuff) do { stuff } while (0)
+
+/* Nonfatal errors (we can keep going, but the data is probably corrupt) */
+#define WARNMS(cinfo,code) \
+ ((cinfo)->err->msg_code = (code), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+#define WARNMS1(cinfo,code,p1) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+#define WARNMS2(cinfo,code,p1,p2) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
+
+/* Informational/debugging messages */
+#define TRACEMS(cinfo,lvl,code) \
+ ((cinfo)->err->msg_code = (code), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS1(cinfo,lvl,code,p1) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS2(cinfo,lvl,code,p1,p2) \
+ ((cinfo)->err->msg_code = (code), \
+ (cinfo)->err->msg_parm.i[0] = (p1), \
+ (cinfo)->err->msg_parm.i[1] = (p2), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+#define TRACEMS3(cinfo,lvl,code,p1,p2,p3) \
+ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
+ (cinfo)->err->msg_code = (code); \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4) \
+ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+ (cinfo)->err->msg_code = (code); \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5) \
+ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+ _mp[4] = (p5); \
+ (cinfo)->err->msg_code = (code); \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8) \
+ MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
+ _mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
+ _mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
+ (cinfo)->err->msg_code = (code); \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
+#define TRACEMSS(cinfo,lvl,code,str) \
+ ((cinfo)->err->msg_code = (code), \
+ FXSYS_strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
+ (*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
+
+#endif /* JERROR_H */
diff --git a/core/src/fxcodec/libjpeg/jinclude.h b/core/src/fxcodec/libjpeg/jinclude.h
index 070188a9b6..0c7aa68360 100644
--- a/core/src/fxcodec/libjpeg/jinclude.h
+++ b/core/src/fxcodec/libjpeg/jinclude.h
@@ -1,102 +1,102 @@
-/*
- * jinclude.h
- *
- * Copyright (C) 1991-1994, 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 exists to provide a single place to fix any problems with
- * including the wrong system include files. (Common problems are taken
- * care of by the standard jconfig symbols, but on really weird systems
- * you may have to edit this file.)
- *
- * NOTE: this file is NOT intended to be included by applications using the
- * JPEG library. Most applications need only include jpeglib.h.
- */
-
-
-/* Include auto-config file to find out which system include files we need. */
-
-#include "jconfig.h" /* auto configuration options */
-#define JCONFIG_INCLUDED /* so that jpeglib.h doesn't do it again */
-
-#include "../../../include/fxcrt/fx_system.h"
-/*
- * We need the NULL macro and size_t typedef.
- * On an ANSI-conforming system it is sufficient to include <stddef.h>.
- * Otherwise, we get them from <stdlib.h> or <stdio.h>; we may have to
- * pull in <sys/types.h> as well.
- * Note that the core JPEG library does not require <stdio.h>;
- * only the default error handler and data source/destination modules do.
- * But we must pull it in because of the references to FILE in jpeglib.h.
- * You can remove those references if you want to compile without <stdio.h>.
- */
-
-#ifdef _DEBUG
-#define CRTDBG_MAP_ALLOC
-//#include <stdlib.h>
-//#include <crtdbg.h>
-#endif
-
-#ifdef HAVE_STDDEF_H
-#include <stddef.h>
-#endif
-
-#ifdef HAVE_STDLIB_H
-//#include <stdlib.h>
-#endif
-
-#ifdef NEED_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-
-#ifndef FAR
-#define FAR
-#endif
-
-//#include <stdio.h>
-
-/*
- * We need memory copying and zeroing functions, plus strncpy().
- * ANSI and System V implementations declare these in <string.h>.
- * BSD doesn't have the mem() functions, but it does have bcopy()/bzero().
- * Some systems may declare memset and memcpy in <memory.h>.
- *
- * NOTE: we assume the size parameters to these functions are of type size_t.
- * Change the casts in these macros if not!
- */
-
-#ifdef NEED_BSD_STRINGS
-
-//#include <strings.h>
-#define MEMZERO(target,size) bzero((void *)(target), (size_t)(size))
-#define MEMCOPY(dest,src,size) bcopy((const void *)(src), (void *)(dest), (size_t)(size))
-
-#else /* not BSD, assume ANSI/SysV string lib */
-
-//#include <string.h>
-#define MEMZERO(target,size) FXSYS_memset32((void *)(target), 0, (size_t)(size))
-#define MEMCOPY(dest,src,size) FXSYS_memcpy32((void *)(dest), (const void *)(src), (size_t)(size))
-
-#endif
-
-/*
- * In ANSI C, and indeed any rational implementation, size_t is also the
- * type returned by sizeof(). However, it seems there are some irrational
- * implementations out there, in which sizeof() returns an int even though
- * size_t is defined as long or unsigned long. To ensure consistent results
- * we always use this SIZEOF() macro in place of using sizeof() directly.
- */
-
-#define SIZEOF(object) ((size_t) sizeof(object))
-
-/*
- * The modules that use fread() and fwrite() always invoke them through
- * these macros. On some systems you may need to twiddle the argument casts.
- * CAUTION: argument order is different from underlying functions!
- */
-
-#define JFREAD(file,buf,sizeofbuf) \
- ((size_t) FXSYS_fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
-#define JFWRITE(file,buf,sizeofbuf) \
- ((size_t) FXSYS_fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
+/*
+ * jinclude.h
+ *
+ * Copyright (C) 1991-1994, 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 exists to provide a single place to fix any problems with
+ * including the wrong system include files. (Common problems are taken
+ * care of by the standard jconfig symbols, but on really weird systems
+ * you may have to edit this file.)
+ *
+ * NOTE: this file is NOT intended to be included by applications using the
+ * JPEG library. Most applications need only include jpeglib.h.
+ */
+
+
+/* Include auto-config file to find out which system include files we need. */
+
+#include "jconfig.h" /* auto configuration options */
+#define JCONFIG_INCLUDED /* so that jpeglib.h doesn't do it again */
+
+#include "../../../include/fxcrt/fx_system.h"
+/*
+ * We need the NULL macro and size_t typedef.
+ * On an ANSI-conforming system it is sufficient to include <stddef.h>.
+ * Otherwise, we get them from <stdlib.h> or <stdio.h>; we may have to
+ * pull in <sys/types.h> as well.
+ * Note that the core JPEG library does not require <stdio.h>;
+ * only the default error handler and data source/destination modules do.
+ * But we must pull it in because of the references to FILE in jpeglib.h.
+ * You can remove those references if you want to compile without <stdio.h>.
+ */
+
+#ifdef _DEBUG
+#define CRTDBG_MAP_ALLOC
+//#include <stdlib.h>
+//#include <crtdbg.h>
+#endif
+
+#ifdef HAVE_STDDEF_H
+#include <stddef.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+//#include <stdlib.h>
+#endif
+
+#ifdef NEED_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#ifndef FAR
+#define FAR
+#endif
+
+//#include <stdio.h>
+
+/*
+ * We need memory copying and zeroing functions, plus strncpy().
+ * ANSI and System V implementations declare these in <string.h>.
+ * BSD doesn't have the mem() functions, but it does have bcopy()/bzero().
+ * Some systems may declare memset and memcpy in <memory.h>.
+ *
+ * NOTE: we assume the size parameters to these functions are of type size_t.
+ * Change the casts in these macros if not!
+ */
+
+#ifdef NEED_BSD_STRINGS
+
+//#include <strings.h>
+#define MEMZERO(target,size) bzero((void *)(target), (size_t)(size))
+#define MEMCOPY(dest,src,size) bcopy((const void *)(src), (void *)(dest), (size_t)(size))
+
+#else /* not BSD, assume ANSI/SysV string lib */
+
+//#include <string.h>
+#define MEMZERO(target,size) FXSYS_memset32((void *)(target), 0, (size_t)(size))
+#define MEMCOPY(dest,src,size) FXSYS_memcpy32((void *)(dest), (const void *)(src), (size_t)(size))
+
+#endif
+
+/*
+ * In ANSI C, and indeed any rational implementation, size_t is also the
+ * type returned by sizeof(). However, it seems there are some irrational
+ * implementations out there, in which sizeof() returns an int even though
+ * size_t is defined as long or unsigned long. To ensure consistent results
+ * we always use this SIZEOF() macro in place of using sizeof() directly.
+ */
+
+#define SIZEOF(object) ((size_t) sizeof(object))
+
+/*
+ * The modules that use fread() and fwrite() always invoke them through
+ * these macros. On some systems you may need to twiddle the argument casts.
+ * CAUTION: argument order is different from underlying functions!
+ */
+
+#define JFREAD(file,buf,sizeofbuf) \
+ ((size_t) FXSYS_fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
+#define JFWRITE(file,buf,sizeofbuf) \
+ ((size_t) FXSYS_fwrite((const void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
diff --git a/core/src/fxcodec/libjpeg/jmemsys.h b/core/src/fxcodec/libjpeg/jmemsys.h
index baa7b7f993..ef1481514b 100644
--- a/core/src/fxcodec/libjpeg/jmemsys.h
+++ b/core/src/fxcodec/libjpeg/jmemsys.h
@@ -1,200 +1,200 @@
-/*
- * jmemsys.h
- *
- * Copyright (C) 1992-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 include file defines the interface between the system-independent
- * and system-dependent portions of the JPEG memory manager. No other
- * modules need include it. (The system-independent portion is jmemmgr.c;
- * there are several different versions of the system-dependent portion.)
- *
- * This file works as-is for the system-dependent memory managers supplied
- * in the IJG distribution. You may need to modify it if you write a
- * custom memory manager. If system-dependent changes are needed in
- * this file, the best method is to #ifdef them based on a configuration
- * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR
- * and USE_MAC_MEMMGR.
- */
-
-
-/* Short forms of external names for systems with brain-damaged linkers. */
-
-#ifdef NEED_SHORT_EXTERNAL_NAMES
-#define jpeg_get_small jGetSmall
-#define jpeg_free_small jFreeSmall
-#define jpeg_get_large jGetLarge
-#define jpeg_free_large jFreeLarge
-#define jpeg_mem_available jMemAvail
-#define jpeg_open_backing_store jOpenBackStore
-#define jpeg_mem_init jMemInit
-#define jpeg_mem_term jMemTerm
-#endif /* NEED_SHORT_EXTERNAL_NAMES */
-
-
-/*
- * These two functions are used to allocate and release small chunks of
- * memory. (Typically the total amount requested through jpeg_get_small is
- * no more than 20K or so; this will be requested in chunks of a few K each.)
- * Behavior should be the same as for the standard library functions malloc
- * and free; in particular, jpeg_get_small must return NULL on failure.
- * On most systems, these ARE malloc and free. jpeg_free_small is passed the
- * size of the object being freed, just in case it's needed.
- * On an 80x86 machine using small-data memory model, these manage near heap.
- */
-
-EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
-EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
- size_t sizeofobject));
-
-/*
- * These two functions are used to allocate and release large chunks of
- * memory (up to the total free space designated by jpeg_mem_available).
- * The interface is the same as above, except that on an 80x86 machine,
- * far pointers are used. On most other machines these are identical to
- * the jpeg_get/free_small routines; but we keep them separate anyway,
- * in case a different allocation strategy is desirable for large chunks.
- */
-
-EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
- size_t sizeofobject));
-EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
- size_t sizeofobject));
-
-/*
- * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
- * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
- * matter, but that case should never come into play). This macro is needed
- * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
- * On those machines, we expect that jconfig.h will provide a proper value.
- * On machines with 32-bit flat address spaces, any large constant may be used.
- *
- * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
- * size_t and will be a multiple of sizeof(align_type).
- */
-
-#ifndef MAX_ALLOC_CHUNK /* may be overridden in jconfig.h */
-#define MAX_ALLOC_CHUNK 1000000000L
-#endif
-
-/*
- * This routine computes the total space still available for allocation by
- * jpeg_get_large. If more space than this is needed, backing store will be
- * used. NOTE: any memory already allocated must not be counted.
- *
- * There is a minimum space requirement, corresponding to the minimum
- * feasible buffer sizes; jmemmgr.c will request that much space even if
- * jpeg_mem_available returns zero. The maximum space needed, enough to hold
- * all working storage in memory, is also passed in case it is useful.
- * Finally, the total space already allocated is passed. If no better
- * method is available, cinfo->mem->max_memory_to_use - already_allocated
- * is often a suitable calculation.
- *
- * It is OK for jpeg_mem_available to underestimate the space available
- * (that'll just lead to more backing-store access than is really necessary).
- * However, an overestimate will lead to failure. Hence it's wise to subtract
- * a slop factor from the true available space. 5% should be enough.
- *
- * On machines with lots of virtual memory, any large constant may be returned.
- * Conversely, zero may be returned to always use the minimum amount of memory.
- */
-
-EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,
- long min_bytes_needed,
- long max_bytes_needed,
- long already_allocated));
-
-
-/*
- * This structure holds whatever state is needed to access a single
- * backing-store object. The read/write/close method pointers are called
- * by jmemmgr.c to manipulate the backing-store object; all other fields
- * are private to the system-dependent backing store routines.
- */
-
-#define TEMP_NAME_LENGTH 64 /* max length of a temporary file's name */
-
-
-#ifdef USE_MSDOS_MEMMGR /* DOS-specific junk */
-
-typedef unsigned short XMSH; /* type of extended-memory handles */
-typedef unsigned short EMSH; /* type of expanded-memory handles */
-
-typedef union {
- short file_handle; /* DOS file handle if it's a temp file */
- XMSH xms_handle; /* handle if it's a chunk of XMS */
- EMSH ems_handle; /* handle if it's a chunk of EMS */
-} handle_union;
-
-#endif /* USE_MSDOS_MEMMGR */
-
-#ifdef USE_MAC_MEMMGR /* Mac-specific junk */
-#include <Files.h>
-#endif /* USE_MAC_MEMMGR */
-
-
-typedef struct backing_store_struct * backing_store_ptr;
-
-typedef struct backing_store_struct {
- /* Methods for reading/writing/closing this backing-store object */
- JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
- backing_store_ptr info,
- void FAR * buffer_address,
- long file_offset, long byte_count));
- JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
- backing_store_ptr info,
- void FAR * buffer_address,
- long file_offset, long byte_count));
- JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
- backing_store_ptr info));
-
- /* Private fields for system-dependent backing-store management */
-#ifdef USE_MSDOS_MEMMGR
- /* For the MS-DOS manager (jmemdos.c), we need: */
- handle_union handle; /* reference to backing-store storage object */
- char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
-#else
-#ifdef USE_MAC_MEMMGR
- /* For the Mac manager (jmemmac.c), we need: */
- short temp_file; /* file reference number to temp file */
- FSSpec tempSpec; /* the FSSpec for the temp file */
- char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
-#else
- /* For a typical implementation with temp files, we need: */
-#ifndef _FPDFAPI_MINI_
- FXSYS_FILE * temp_file; /* stdio reference to temp file */
- char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
-#endif
-#endif
-#endif
-} backing_store_info;
-
-
-/*
- * Initial opening of a backing-store object. This must fill in the
- * read/write/close pointers in the object. The read/write routines
- * may take an error exit if the specified maximum file size is exceeded.
- * (If jpeg_mem_available always returns a large value, this routine can
- * just take an error exit.)
- */
-
-EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
- backing_store_ptr info,
- long total_bytes_needed));
-
-
-/*
- * These routines take care of any system-dependent initialization and
- * cleanup required. jpeg_mem_init will be called before anything is
- * allocated (and, therefore, nothing in cinfo is of use except the error
- * manager pointer). It should return a suitable default value for
- * max_memory_to_use; this may subsequently be overridden by the surrounding
- * application. (Note that max_memory_to_use is only important if
- * jpeg_mem_available chooses to consult it ... no one else will.)
- * jpeg_mem_term may assume that all requested memory has been freed and that
- * all opened backing-store objects have been closed.
- */
-
-EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo));
-EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));
+/*
+ * jmemsys.h
+ *
+ * Copyright (C) 1992-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 include file defines the interface between the system-independent
+ * and system-dependent portions of the JPEG memory manager. No other
+ * modules need include it. (The system-independent portion is jmemmgr.c;
+ * there are several different versions of the system-dependent portion.)
+ *
+ * This file works as-is for the system-dependent memory managers supplied
+ * in the IJG distribution. You may need to modify it if you write a
+ * custom memory manager. If system-dependent changes are needed in
+ * this file, the best method is to #ifdef them based on a configuration
+ * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR
+ * and USE_MAC_MEMMGR.
+ */
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_get_small jGetSmall
+#define jpeg_free_small jFreeSmall
+#define jpeg_get_large jGetLarge
+#define jpeg_free_large jFreeLarge
+#define jpeg_mem_available jMemAvail
+#define jpeg_open_backing_store jOpenBackStore
+#define jpeg_mem_init jMemInit
+#define jpeg_mem_term jMemTerm
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/*
+ * These two functions are used to allocate and release small chunks of
+ * memory. (Typically the total amount requested through jpeg_get_small is
+ * no more than 20K or so; this will be requested in chunks of a few K each.)
+ * Behavior should be the same as for the standard library functions malloc
+ * and free; in particular, jpeg_get_small must return NULL on failure.
+ * On most systems, these ARE malloc and free. jpeg_free_small is passed the
+ * size of the object being freed, just in case it's needed.
+ * On an 80x86 machine using small-data memory model, these manage near heap.
+ */
+
+EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
+EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
+ size_t sizeofobject));
+
+/*
+ * These two functions are used to allocate and release large chunks of
+ * memory (up to the total free space designated by jpeg_mem_available).
+ * The interface is the same as above, except that on an 80x86 machine,
+ * far pointers are used. On most other machines these are identical to
+ * the jpeg_get/free_small routines; but we keep them separate anyway,
+ * in case a different allocation strategy is desirable for large chunks.
+ */
+
+EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
+ size_t sizeofobject));
+EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
+ size_t sizeofobject));
+
+/*
+ * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
+ * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
+ * matter, but that case should never come into play). This macro is needed
+ * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
+ * On those machines, we expect that jconfig.h will provide a proper value.
+ * On machines with 32-bit flat address spaces, any large constant may be used.
+ *
+ * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
+ * size_t and will be a multiple of sizeof(align_type).
+ */
+
+#ifndef MAX_ALLOC_CHUNK /* may be overridden in jconfig.h */
+#define MAX_ALLOC_CHUNK 1000000000L
+#endif
+
+/*
+ * This routine computes the total space still available for allocation by
+ * jpeg_get_large. If more space than this is needed, backing store will be
+ * used. NOTE: any memory already allocated must not be counted.
+ *
+ * There is a minimum space requirement, corresponding to the minimum
+ * feasible buffer sizes; jmemmgr.c will request that much space even if
+ * jpeg_mem_available returns zero. The maximum space needed, enough to hold
+ * all working storage in memory, is also passed in case it is useful.
+ * Finally, the total space already allocated is passed. If no better
+ * method is available, cinfo->mem->max_memory_to_use - already_allocated
+ * is often a suitable calculation.
+ *
+ * It is OK for jpeg_mem_available to underestimate the space available
+ * (that'll just lead to more backing-store access than is really necessary).
+ * However, an overestimate will lead to failure. Hence it's wise to subtract
+ * a slop factor from the true available space. 5% should be enough.
+ *
+ * On machines with lots of virtual memory, any large constant may be returned.
+ * Conversely, zero may be returned to always use the minimum amount of memory.
+ */
+
+EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,
+ long min_bytes_needed,
+ long max_bytes_needed,
+ long already_allocated));
+
+
+/*
+ * This structure holds whatever state is needed to access a single
+ * backing-store object. The read/write/close method pointers are called
+ * by jmemmgr.c to manipulate the backing-store object; all other fields
+ * are private to the system-dependent backing store routines.
+ */
+
+#define TEMP_NAME_LENGTH 64 /* max length of a temporary file's name */
+
+
+#ifdef USE_MSDOS_MEMMGR /* DOS-specific junk */
+
+typedef unsigned short XMSH; /* type of extended-memory handles */
+typedef unsigned short EMSH; /* type of expanded-memory handles */
+
+typedef union {
+ short file_handle; /* DOS file handle if it's a temp file */
+ XMSH xms_handle; /* handle if it's a chunk of XMS */
+ EMSH ems_handle; /* handle if it's a chunk of EMS */
+} handle_union;
+
+#endif /* USE_MSDOS_MEMMGR */
+
+#ifdef USE_MAC_MEMMGR /* Mac-specific junk */
+#include <Files.h>
+#endif /* USE_MAC_MEMMGR */
+
+
+typedef struct backing_store_struct * backing_store_ptr;
+
+typedef struct backing_store_struct {
+ /* Methods for reading/writing/closing this backing-store object */
+ JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
+ backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count));
+ JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
+ backing_store_ptr info,
+ void FAR * buffer_address,
+ long file_offset, long byte_count));
+ JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
+ backing_store_ptr info));
+
+ /* Private fields for system-dependent backing-store management */
+#ifdef USE_MSDOS_MEMMGR
+ /* For the MS-DOS manager (jmemdos.c), we need: */
+ handle_union handle; /* reference to backing-store storage object */
+ char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
+#else
+#ifdef USE_MAC_MEMMGR
+ /* For the Mac manager (jmemmac.c), we need: */
+ short temp_file; /* file reference number to temp file */
+ FSSpec tempSpec; /* the FSSpec for the temp file */
+ char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
+#else
+ /* For a typical implementation with temp files, we need: */
+#ifndef _FPDFAPI_MINI_
+ FXSYS_FILE * temp_file; /* stdio reference to temp file */
+ char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
+#endif
+#endif
+#endif
+} backing_store_info;
+
+
+/*
+ * Initial opening of a backing-store object. This must fill in the
+ * read/write/close pointers in the object. The read/write routines
+ * may take an error exit if the specified maximum file size is exceeded.
+ * (If jpeg_mem_available always returns a large value, this routine can
+ * just take an error exit.)
+ */
+
+EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
+ backing_store_ptr info,
+ long total_bytes_needed));
+
+
+/*
+ * These routines take care of any system-dependent initialization and
+ * cleanup required. jpeg_mem_init will be called before anything is
+ * allocated (and, therefore, nothing in cinfo is of use except the error
+ * manager pointer). It should return a suitable default value for
+ * max_memory_to_use; this may subsequently be overridden by the surrounding
+ * application. (Note that max_memory_to_use is only important if
+ * jpeg_mem_available chooses to consult it ... no one else will.)
+ * jpeg_mem_term may assume that all requested memory has been freed and that
+ * all opened backing-store objects have been closed.
+ */
+
+EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo));
+EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));
diff --git a/core/src/fxcodec/libjpeg/jmorecfg.h b/core/src/fxcodec/libjpeg/jmorecfg.h
index 3602114a5c..88d210954c 100644
--- a/core/src/fxcodec/libjpeg/jmorecfg.h
+++ b/core/src/fxcodec/libjpeg/jmorecfg.h
@@ -1,376 +1,376 @@
-/*
- * jmorecfg.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 additional configuration options that customize the
- * JPEG software for special applications or support machine-dependent
- * optimizations. Most users will not need to touch this file.
- */
-
-#ifdef _MSC_VER
-#pragma warning (disable : 4142)
-#endif
-
-/*
- * Define BITS_IN_JSAMPLE as either
- * 8 for 8-bit sample values (the usual setting)
- * 12 for 12-bit sample values
- * Only 8 and 12 are legal data precisions for lossy JPEG according to the
- * JPEG standard, and the IJG code does not support anything else!
- * We do not support run-time selection of data precision, sorry.
- */
-
-#define BITS_IN_JSAMPLE 8 /* use 8 or 12 */
-
-
-/*
- * Maximum number of components (color channels) allowed in JPEG image.
- * To meet the letter of the JPEG spec, set this to 255. However, darn
- * few applications need more than 4 channels (maybe 5 for CMYK + alpha
- * mask). We recommend 10 as a reasonable compromise; use 4 if you are
- * really short on memory. (Each allowed component costs a hundred or so
- * bytes of storage, whether actually used in an image or not.)
- */
-
-#define MAX_COMPONENTS 10 /* maximum number of image components */
-
-
-/*
- * Basic data types.
- * You may need to change these if you have a machine with unusual data
- * type sizes; for example, "char" not 8 bits, "short" not 16 bits,
- * or "long" not 32 bits. We don't care whether "int" is 16 or 32 bits,
- * but it had better be at least 16.
- */
-
-/* Representation of a single sample (pixel element value).
- * We frequently allocate large arrays of these, so it's important to keep
- * them small. But if you have memory to burn and access to char or short
- * arrays is very slow on your hardware, you might want to change these.
- */
-
-#if BITS_IN_JSAMPLE == 8
-/* JSAMPLE should be the smallest type that will hold the values 0..255.
- * You can use a signed char by having GETJSAMPLE mask it with 0xFF.
- */
-
-#ifdef HAVE_UNSIGNED_CHAR
-
-typedef unsigned char JSAMPLE;
-#define GETJSAMPLE(value) ((int) (value))
-
-#else /* not HAVE_UNSIGNED_CHAR */
-
-typedef char JSAMPLE;
-#ifdef CHAR_IS_UNSIGNED
-#define GETJSAMPLE(value) ((int) (value))
-#else
-#define GETJSAMPLE(value) ((int) (value) & 0xFF)
-#endif /* CHAR_IS_UNSIGNED */
-
-#endif /* HAVE_UNSIGNED_CHAR */
-
-#define MAXJSAMPLE 255
-#define CENTERJSAMPLE 128
-
-#endif /* BITS_IN_JSAMPLE == 8 */
-
-
-#if BITS_IN_JSAMPLE == 12
-/* JSAMPLE should be the smallest type that will hold the values 0..4095.
- * On nearly all machines "short" will do nicely.
- */
-
-typedef short JSAMPLE;
-#define GETJSAMPLE(value) ((int) (value))
-
-#define MAXJSAMPLE 4095
-#define CENTERJSAMPLE 2048
-
-#endif /* BITS_IN_JSAMPLE == 12 */
-
-
-/* Representation of a DCT frequency coefficient.
- * This should be a signed value of at least 16 bits; "short" is usually OK.
- * Again, we allocate large arrays of these, but you can change to int
- * if you have memory to burn and "short" is really slow.
- */
-
-typedef short JCOEF;
-
-
-/* Compressed datastreams are represented as arrays of JOCTET.
- * These must be EXACTLY 8 bits wide, at least once they are written to
- * external storage. Note that when using the stdio data source/destination
- * managers, this is also the data type passed to fread/fwrite.
- */
-
-#ifdef HAVE_UNSIGNED_CHAR
-
-typedef unsigned char JOCTET;
-#define GETJOCTET(value) (value)
-
-#else /* not HAVE_UNSIGNED_CHAR */
-
-typedef char JOCTET;
-#ifdef CHAR_IS_UNSIGNED
-#define GETJOCTET(value) (value)
-#else
-#define GETJOCTET(value) ((value) & 0xFF)
-#endif /* CHAR_IS_UNSIGNED */
-
-#endif /* HAVE_UNSIGNED_CHAR */
-
-
-/* These typedefs are used for various table entries and so forth.
- * They must be at least as wide as specified; but making them too big
- * won't cost a huge amount of memory, so we don't provide special
- * extraction code like we did for JSAMPLE. (In other words, these
- * typedefs live at a different point on the speed/space tradeoff curve.)
- */
-
-#if _FX_OS_ != _FX_VXWORKS_
-
-/* UINT8 must hold at least the values 0..255. */
-
-#ifdef HAVE_UNSIGNED_CHAR
-typedef unsigned char UINT8;
-#else /* not HAVE_UNSIGNED_CHAR */
-#ifdef CHAR_IS_UNSIGNED
-typedef char UINT8;
-#else /* not CHAR_IS_UNSIGNED */
-typedef short UINT8;
-#endif /* CHAR_IS_UNSIGNED */
-#endif /* HAVE_UNSIGNED_CHAR */
-
-
-/* UINT16 must hold at least the values 0..65535. */
-
-#ifdef HAVE_UNSIGNED_SHORT
-typedef unsigned short UINT16;
-#else /* not HAVE_UNSIGNED_SHORT */
-typedef unsigned int UINT16;
-#endif /* HAVE_UNSIGNED_SHORT */
-
-/* INT16 must hold at least the values -32768..32767. */
-
-#ifndef XMD_H /* X11/xmd.h correctly defines INT16 */
-typedef short INT16;
-#endif
-
-/* INT32 must hold at least signed 32-bit values. */
-
-#ifndef XMD_H /* X11/xmd.h correctly defines INT32 */
-typedef int INT32;
-#endif
-
-#endif
-
-/* Datatype used for image dimensions. The JPEG standard only supports
- * images up to 64K*64K due to 16-bit fields in SOF markers. Therefore
- * "unsigned int" is sufficient on all machines. However, if you need to
- * handle larger images and you don't mind deviating from the spec, you
- * can change this datatype.
- */
-
-typedef unsigned int JDIMENSION;
-
-#define JPEG_MAX_DIMENSION 65500L /* a tad under 64K to prevent overflows */
-
-
-/* These macros are used in all function definitions and extern declarations.
- * You could modify them if you need to change function linkage conventions;
- * in particular, you'll need to do that to make the library a Windows DLL.
- * Another application is to make all functions global for use with debuggers
- * or code profilers that require it.
- */
-
-/* a function called through method pointers: */
-#define METHODDEF(type) static type
-/* a function used only in its module: */
-#define LOCAL(type) static type
-/* a function referenced thru EXTERNs: */
-#define GLOBAL(type) type
-
-#ifdef _FX_MANAGED_CODE_
-#define EXTERN(type) extern "C" type
-#else
-/* a reference to a GLOBAL function: */
-#define EXTERN(type) extern type
-#endif
-
-
-/* This macro is used to declare a "method", that is, a function pointer.
- * We want to supply prototype parameters if the compiler can cope.
- * Note that the arglist parameter must be parenthesized!
- * Again, you can customize this if you need special linkage keywords.
- */
-
-#ifdef HAVE_PROTOTYPES
-#define JMETHOD(type,methodname,arglist) type (*methodname) arglist
-#else
-#define JMETHOD(type,methodname,arglist) type (*methodname) ()
-#endif
-
-
-/* Here is the pseudo-keyword for declaring pointers that must be "far"
- * on 80x86 machines. Most of the specialized coding for 80x86 is handled
- * by just saying "FAR *" where such a pointer is needed. In a few places
- * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
- */
-
-#ifdef NEED_FAR_POINTERS
-#define FAR far
-#else
-//#define FAR
-#endif
-
-
-/*
- * On a few systems, type boolean and/or its values FALSE, TRUE may appear
- * in standard header files. Or you may have conflicts with application-
- * specific header files that you want to include together with these files.
- * Defining HAVE_BOOLEAN before including jpeglib.h should make it work.
- */
-
-#ifndef HAVE_BOOLEAN
-typedef int boolean;
-#endif
-#ifndef FALSE /* in case these macros already exist */
-#define FALSE 0 /* values of boolean */
-#endif
-#ifndef TRUE
-#define TRUE 1
-#endif
-
-
-/*
- * The remaining options affect code selection within the JPEG library,
- * but they don't need to be visible to most applications using the library.
- * To minimize application namespace pollution, the symbols won't be
- * defined unless JPEG_INTERNALS or JPEG_INTERNAL_OPTIONS has been defined.
- */
-
-#ifdef JPEG_INTERNALS
-#define JPEG_INTERNAL_OPTIONS
-#endif
-
-#ifdef JPEG_INTERNAL_OPTIONS
-
-
-/*
- * These defines indicate whether to include various optional functions.
- * Undefining some of these symbols will produce a smaller but less capable
- * library. Note that you can leave certain source files out of the
- * compilation/linking process if you've #undef'd the corresponding symbols.
- * (You may HAVE to do that if your compiler doesn't like null source files.)
- */
-
-/* Arithmetic coding is unsupported for legal reasons. Complaints to IBM. */
-
-/* Capability options common to encoder and decoder: */
-
-#define DCT_ISLOW_SUPPORTED /* slow but accurate integer algorithm */
-#define DCT_IFAST_SUPPORTED /* faster, less accurate integer method */
-#undef DCT_FLOAT_SUPPORTED /* floating-point: accurate, fast on fast HW */
-
-/* Encoder capability options: */
-
-#undef C_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */
-#define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
-#define C_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/
-#define ENTROPY_OPT_SUPPORTED /* Optimization of entropy coding parms? */
-/* Note: if you selected 12-bit data precision, it is dangerous to turn off
- * ENTROPY_OPT_SUPPORTED. The standard Huffman tables are only good for 8-bit
- * precision, so jchuff.c normally uses entropy optimization to compute
- * usable tables for higher precision. If you don't want to do optimization,
- * you'll have to supply different default Huffman tables.
- * The exact same statements apply for progressive JPEG: the default tables
- * don't work for progressive mode. (This may get fixed, however.)
- */
-#define INPUT_SMOOTHING_SUPPORTED /* Input image smoothing option? */
-
-/* Decoder capability options: */
-
-#undef D_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */
-#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
-#define D_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/
-#define SAVE_MARKERS_SUPPORTED /* jpeg_save_markers() needed? */
-#define BLOCK_SMOOTHING_SUPPORTED /* Block smoothing? (Progressive only) */
-#define IDCT_SCALING_SUPPORTED /* Output rescaling via IDCT? */
-#undef UPSAMPLE_SCALING_SUPPORTED /* Output rescaling at upsample stage? */
-#define UPSAMPLE_MERGING_SUPPORTED /* Fast path for sloppy upsampling? */
-#undef QUANT_1PASS_SUPPORTED /* 1-pass color quantization? */
-#undef QUANT_2PASS_SUPPORTED /* 2-pass color quantization? */
-
-/* more capability options later, no doubt */
-
-
-/*
- * Ordering of RGB data in scanlines passed to or from the application.
- * If your application wants to deal with data in the order B,G,R, just
- * change these macros. You can also deal with formats such as R,G,B,X
- * (one extra byte per pixel) by changing RGB_PIXELSIZE. Note that changing
- * the offsets will also change the order in which colormap data is organized.
- * RESTRICTIONS:
- * 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats.
- * 2. These macros only affect RGB<=>YCbCr color conversion, so they are not
- * useful if you are using JPEG color spaces other than YCbCr or grayscale.
- * 3. The color quantizer modules will not behave desirably if RGB_PIXELSIZE
- * is not 3 (they don't understand about dummy color components!). So you
- * can't use color quantization if you change that value.
- */
-
-#define RGB_RED 0 /* Offset of Red in an RGB scanline element */
-#define RGB_GREEN 1 /* Offset of Green */
-#define RGB_BLUE 2 /* Offset of Blue */
-#define RGB_PIXELSIZE 3 /* JSAMPLEs per RGB scanline element */
-
-
-/* Definitions for speed-related optimizations. */
-
-
-/* If your compiler supports inline functions, define INLINE
- * as the inline keyword; otherwise define it as empty.
- */
-
-#ifndef INLINE
-#ifdef __GNUC__ /* for instance, GNU C knows about inline */
-#define INLINE __inline__
-#endif
-#ifndef INLINE
-#define INLINE /* default is to define it as empty */
-#endif
-#endif
-
-
-/* On some machines (notably 68000 series) "int" is 32 bits, but multiplying
- * two 16-bit shorts is faster than multiplying two ints. Define MULTIPLIER
- * as short on such a machine. MULTIPLIER must be at least 16 bits wide.
- */
-
-#ifndef MULTIPLIER
-#define MULTIPLIER int /* type for fastest integer multiply */
-#endif
-
-
-/* FAST_FLOAT should be either float or double, whichever is done faster
- * by your compiler. (Note that this type is only used in the floating point
- * DCT routines, so it only matters if you've defined DCT_FLOAT_SUPPORTED.)
- * Typically, float is faster in ANSI C compilers, while double is faster in
- * pre-ANSI compilers (because they insist on converting to double anyway).
- * The code below therefore chooses float if we have ANSI-style prototypes.
- */
-
-#ifndef FAST_FLOAT
-#ifdef HAVE_PROTOTYPES
-#define FAST_FLOAT float
-#else
-#define FAST_FLOAT double
-#endif
-#endif
-
-#endif /* JPEG_INTERNAL_OPTIONS */
+/*
+ * jmorecfg.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 additional configuration options that customize the
+ * JPEG software for special applications or support machine-dependent
+ * optimizations. Most users will not need to touch this file.
+ */
+
+#ifdef _MSC_VER
+#pragma warning (disable : 4142)
+#endif
+
+/*
+ * Define BITS_IN_JSAMPLE as either
+ * 8 for 8-bit sample values (the usual setting)
+ * 12 for 12-bit sample values
+ * Only 8 and 12 are legal data precisions for lossy JPEG according to the
+ * JPEG standard, and the IJG code does not support anything else!
+ * We do not support run-time selection of data precision, sorry.
+ */
+
+#define BITS_IN_JSAMPLE 8 /* use 8 or 12 */
+
+
+/*
+ * Maximum number of components (color channels) allowed in JPEG image.
+ * To meet the letter of the JPEG spec, set this to 255. However, darn
+ * few applications need more than 4 channels (maybe 5 for CMYK + alpha
+ * mask). We recommend 10 as a reasonable compromise; use 4 if you are
+ * really short on memory. (Each allowed component costs a hundred or so
+ * bytes of storage, whether actually used in an image or not.)
+ */
+
+#define MAX_COMPONENTS 10 /* maximum number of image components */
+
+
+/*
+ * Basic data types.
+ * You may need to change these if you have a machine with unusual data
+ * type sizes; for example, "char" not 8 bits, "short" not 16 bits,
+ * or "long" not 32 bits. We don't care whether "int" is 16 or 32 bits,
+ * but it had better be at least 16.
+ */
+
+/* Representation of a single sample (pixel element value).
+ * We frequently allocate large arrays of these, so it's important to keep
+ * them small. But if you have memory to burn and access to char or short
+ * arrays is very slow on your hardware, you might want to change these.
+ */
+
+#if BITS_IN_JSAMPLE == 8
+/* JSAMPLE should be the smallest type that will hold the values 0..255.
+ * You can use a signed char by having GETJSAMPLE mask it with 0xFF.
+ */
+
+#ifdef HAVE_UNSIGNED_CHAR
+
+typedef unsigned char JSAMPLE;
+#define GETJSAMPLE(value) ((int) (value))
+
+#else /* not HAVE_UNSIGNED_CHAR */
+
+typedef char JSAMPLE;
+#ifdef CHAR_IS_UNSIGNED
+#define GETJSAMPLE(value) ((int) (value))
+#else
+#define GETJSAMPLE(value) ((int) (value) & 0xFF)
+#endif /* CHAR_IS_UNSIGNED */
+
+#endif /* HAVE_UNSIGNED_CHAR */
+
+#define MAXJSAMPLE 255
+#define CENTERJSAMPLE 128
+
+#endif /* BITS_IN_JSAMPLE == 8 */
+
+
+#if BITS_IN_JSAMPLE == 12
+/* JSAMPLE should be the smallest type that will hold the values 0..4095.
+ * On nearly all machines "short" will do nicely.
+ */
+
+typedef short JSAMPLE;
+#define GETJSAMPLE(value) ((int) (value))
+
+#define MAXJSAMPLE 4095
+#define CENTERJSAMPLE 2048
+
+#endif /* BITS_IN_JSAMPLE == 12 */
+
+
+/* Representation of a DCT frequency coefficient.
+ * This should be a signed value of at least 16 bits; "short" is usually OK.
+ * Again, we allocate large arrays of these, but you can change to int
+ * if you have memory to burn and "short" is really slow.
+ */
+
+typedef short JCOEF;
+
+
+/* Compressed datastreams are represented as arrays of JOCTET.
+ * These must be EXACTLY 8 bits wide, at least once they are written to
+ * external storage. Note that when using the stdio data source/destination
+ * managers, this is also the data type passed to fread/fwrite.
+ */
+
+#ifdef HAVE_UNSIGNED_CHAR
+
+typedef unsigned char JOCTET;
+#define GETJOCTET(value) (value)
+
+#else /* not HAVE_UNSIGNED_CHAR */
+
+typedef char JOCTET;
+#ifdef CHAR_IS_UNSIGNED
+#define GETJOCTET(value) (value)
+#else
+#define GETJOCTET(value) ((value) & 0xFF)
+#endif /* CHAR_IS_UNSIGNED */
+
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+/* These typedefs are used for various table entries and so forth.
+ * They must be at least as wide as specified; but making them too big
+ * won't cost a huge amount of memory, so we don't provide special
+ * extraction code like we did for JSAMPLE. (In other words, these
+ * typedefs live at a different point on the speed/space tradeoff curve.)
+ */
+
+#if _FX_OS_ != _FX_VXWORKS_
+
+/* UINT8 must hold at least the values 0..255. */
+
+#ifdef HAVE_UNSIGNED_CHAR
+typedef unsigned char UINT8;
+#else /* not HAVE_UNSIGNED_CHAR */
+#ifdef CHAR_IS_UNSIGNED
+typedef char UINT8;
+#else /* not CHAR_IS_UNSIGNED */
+typedef short UINT8;
+#endif /* CHAR_IS_UNSIGNED */
+#endif /* HAVE_UNSIGNED_CHAR */
+
+
+/* UINT16 must hold at least the values 0..65535. */
+
+#ifdef HAVE_UNSIGNED_SHORT
+typedef unsigned short UINT16;
+#else /* not HAVE_UNSIGNED_SHORT */
+typedef unsigned int UINT16;
+#endif /* HAVE_UNSIGNED_SHORT */
+
+/* INT16 must hold at least the values -32768..32767. */
+
+#ifndef XMD_H /* X11/xmd.h correctly defines INT16 */
+typedef short INT16;
+#endif
+
+/* INT32 must hold at least signed 32-bit values. */
+
+#ifndef XMD_H /* X11/xmd.h correctly defines INT32 */
+typedef int INT32;
+#endif
+
+#endif
+
+/* Datatype used for image dimensions. The JPEG standard only supports
+ * images up to 64K*64K due to 16-bit fields in SOF markers. Therefore
+ * "unsigned int" is sufficient on all machines. However, if you need to
+ * handle larger images and you don't mind deviating from the spec, you
+ * can change this datatype.
+ */
+
+typedef unsigned int JDIMENSION;
+
+#define JPEG_MAX_DIMENSION 65500L /* a tad under 64K to prevent overflows */
+
+
+/* These macros are used in all function definitions and extern declarations.
+ * You could modify them if you need to change function linkage conventions;
+ * in particular, you'll need to do that to make the library a Windows DLL.
+ * Another application is to make all functions global for use with debuggers
+ * or code profilers that require it.
+ */
+
+/* a function called through method pointers: */
+#define METHODDEF(type) static type
+/* a function used only in its module: */
+#define LOCAL(type) static type
+/* a function referenced thru EXTERNs: */
+#define GLOBAL(type) type
+
+#ifdef _FX_MANAGED_CODE_
+#define EXTERN(type) extern "C" type
+#else
+/* a reference to a GLOBAL function: */
+#define EXTERN(type) extern type
+#endif
+
+
+/* This macro is used to declare a "method", that is, a function pointer.
+ * We want to supply prototype parameters if the compiler can cope.
+ * Note that the arglist parameter must be parenthesized!
+ * Again, you can customize this if you need special linkage keywords.
+ */
+
+#ifdef HAVE_PROTOTYPES
+#define JMETHOD(type,methodname,arglist) type (*methodname) arglist
+#else
+#define JMETHOD(type,methodname,arglist) type (*methodname) ()
+#endif
+
+
+/* Here is the pseudo-keyword for declaring pointers that must be "far"
+ * on 80x86 machines. Most of the specialized coding for 80x86 is handled
+ * by just saying "FAR *" where such a pointer is needed. In a few places
+ * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
+ */
+
+#ifdef NEED_FAR_POINTERS
+#define FAR far
+#else
+//#define FAR
+#endif
+
+
+/*
+ * On a few systems, type boolean and/or its values FALSE, TRUE may appear
+ * in standard header files. Or you may have conflicts with application-
+ * specific header files that you want to include together with these files.
+ * Defining HAVE_BOOLEAN before including jpeglib.h should make it work.
+ */
+
+#ifndef HAVE_BOOLEAN
+typedef int boolean;
+#endif
+#ifndef FALSE /* in case these macros already exist */
+#define FALSE 0 /* values of boolean */
+#endif
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+
+/*
+ * The remaining options affect code selection within the JPEG library,
+ * but they don't need to be visible to most applications using the library.
+ * To minimize application namespace pollution, the symbols won't be
+ * defined unless JPEG_INTERNALS or JPEG_INTERNAL_OPTIONS has been defined.
+ */
+
+#ifdef JPEG_INTERNALS
+#define JPEG_INTERNAL_OPTIONS
+#endif
+
+#ifdef JPEG_INTERNAL_OPTIONS
+
+
+/*
+ * These defines indicate whether to include various optional functions.
+ * Undefining some of these symbols will produce a smaller but less capable
+ * library. Note that you can leave certain source files out of the
+ * compilation/linking process if you've #undef'd the corresponding symbols.
+ * (You may HAVE to do that if your compiler doesn't like null source files.)
+ */
+
+/* Arithmetic coding is unsupported for legal reasons. Complaints to IBM. */
+
+/* Capability options common to encoder and decoder: */
+
+#define DCT_ISLOW_SUPPORTED /* slow but accurate integer algorithm */
+#define DCT_IFAST_SUPPORTED /* faster, less accurate integer method */
+#undef DCT_FLOAT_SUPPORTED /* floating-point: accurate, fast on fast HW */
+
+/* Encoder capability options: */
+
+#undef C_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */
+#define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
+#define C_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/
+#define ENTROPY_OPT_SUPPORTED /* Optimization of entropy coding parms? */
+/* Note: if you selected 12-bit data precision, it is dangerous to turn off
+ * ENTROPY_OPT_SUPPORTED. The standard Huffman tables are only good for 8-bit
+ * precision, so jchuff.c normally uses entropy optimization to compute
+ * usable tables for higher precision. If you don't want to do optimization,
+ * you'll have to supply different default Huffman tables.
+ * The exact same statements apply for progressive JPEG: the default tables
+ * don't work for progressive mode. (This may get fixed, however.)
+ */
+#define INPUT_SMOOTHING_SUPPORTED /* Input image smoothing option? */
+
+/* Decoder capability options: */
+
+#undef D_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */
+#define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
+#define D_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/
+#define SAVE_MARKERS_SUPPORTED /* jpeg_save_markers() needed? */
+#define BLOCK_SMOOTHING_SUPPORTED /* Block smoothing? (Progressive only) */
+#define IDCT_SCALING_SUPPORTED /* Output rescaling via IDCT? */
+#undef UPSAMPLE_SCALING_SUPPORTED /* Output rescaling at upsample stage? */
+#define UPSAMPLE_MERGING_SUPPORTED /* Fast path for sloppy upsampling? */
+#undef QUANT_1PASS_SUPPORTED /* 1-pass color quantization? */
+#undef QUANT_2PASS_SUPPORTED /* 2-pass color quantization? */
+
+/* more capability options later, no doubt */
+
+
+/*
+ * Ordering of RGB data in scanlines passed to or from the application.
+ * If your application wants to deal with data in the order B,G,R, just
+ * change these macros. You can also deal with formats such as R,G,B,X
+ * (one extra byte per pixel) by changing RGB_PIXELSIZE. Note that changing
+ * the offsets will also change the order in which colormap data is organized.
+ * RESTRICTIONS:
+ * 1. The sample applications cjpeg,djpeg do NOT support modified RGB formats.
+ * 2. These macros only affect RGB<=>YCbCr color conversion, so they are not
+ * useful if you are using JPEG color spaces other than YCbCr or grayscale.
+ * 3. The color quantizer modules will not behave desirably if RGB_PIXELSIZE
+ * is not 3 (they don't understand about dummy color components!). So you
+ * can't use color quantization if you change that value.
+ */
+
+#define RGB_RED 0 /* Offset of Red in an RGB scanline element */
+#define RGB_GREEN 1 /* Offset of Green */
+#define RGB_BLUE 2 /* Offset of Blue */
+#define RGB_PIXELSIZE 3 /* JSAMPLEs per RGB scanline element */
+
+
+/* Definitions for speed-related optimizations. */
+
+
+/* If your compiler supports inline functions, define INLINE
+ * as the inline keyword; otherwise define it as empty.
+ */
+
+#ifndef INLINE
+#ifdef __GNUC__ /* for instance, GNU C knows about inline */
+#define INLINE __inline__
+#endif
+#ifndef INLINE
+#define INLINE /* default is to define it as empty */
+#endif
+#endif
+
+
+/* On some machines (notably 68000 series) "int" is 32 bits, but multiplying
+ * two 16-bit shorts is faster than multiplying two ints. Define MULTIPLIER
+ * as short on such a machine. MULTIPLIER must be at least 16 bits wide.
+ */
+
+#ifndef MULTIPLIER
+#define MULTIPLIER int /* type for fastest integer multiply */
+#endif
+
+
+/* FAST_FLOAT should be either float or double, whichever is done faster
+ * by your compiler. (Note that this type is only used in the floating point
+ * DCT routines, so it only matters if you've defined DCT_FLOAT_SUPPORTED.)
+ * Typically, float is faster in ANSI C compilers, while double is faster in
+ * pre-ANSI compilers (because they insist on converting to double anyway).
+ * The code below therefore chooses float if we have ANSI-style prototypes.
+ */
+
+#ifndef FAST_FLOAT
+#ifdef HAVE_PROTOTYPES
+#define FAST_FLOAT float
+#else
+#define FAST_FLOAT double
+#endif
+#endif
+
+#endif /* JPEG_INTERNAL_OPTIONS */
diff --git a/core/src/fxcodec/libjpeg/jpegint.h b/core/src/fxcodec/libjpeg/jpegint.h
index 685a3610b2..95b00d405c 100644
--- a/core/src/fxcodec/libjpeg/jpegint.h
+++ b/core/src/fxcodec/libjpeg/jpegint.h
@@ -1,392 +1,392 @@
-/*
- * jpegint.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 provides common declarations for the various JPEG modules.
- * These declarations are considered internal to the JPEG library; most
- * applications using the library shouldn't need to include this file.
- */
-
-
-/* Declarations for both compression & decompression */
-
-typedef enum { /* Operating modes for buffer controllers */
- JBUF_PASS_THRU, /* Plain stripwise operation */
- /* Remaining modes require a full-image buffer to have been created */
- JBUF_SAVE_SOURCE, /* Run source subobject only, save output */
- JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */
- JBUF_SAVE_AND_PASS /* Run both subobjects, save output */
-} J_BUF_MODE;
-
-/* Values of global_state field (jdapi.c has some dependencies on ordering!) */
-#define CSTATE_START 100 /* after create_compress */
-#define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */
-#define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */
-#define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */
-#define DSTATE_START 200 /* after create_decompress */
-#define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */
-#define DSTATE_READY 202 /* found SOS, ready for start_decompress */
-#define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/
-#define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */
-#define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */
-#define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */
-#define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */
-#define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */
-#define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */
-#define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
-
-
-/* Declarations for compression modules */
-
-/* Master control module */
-struct jpeg_comp_master {
- JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
- JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
- JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
-
- /* State variables made visible to other modules */
- boolean call_pass_startup; /* True if pass_startup must be called */
- boolean is_last_pass; /* True during last pass */
-};
-
-/* Main buffer control (downsampled-data buffer) */
-struct jpeg_c_main_controller {
- JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
- JMETHOD(void, process_data, (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
- JDIMENSION in_rows_avail));
-};
-
-/* Compression preprocessing (downsampling input buffer control) */
-struct jpeg_c_prep_controller {
- JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
- JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
- JSAMPARRAY input_buf,
- JDIMENSION *in_row_ctr,
- JDIMENSION in_rows_avail,
- JSAMPIMAGE output_buf,
- JDIMENSION *out_row_group_ctr,
- JDIMENSION out_row_groups_avail));
-};
-
-/* Coefficient buffer control */
-struct jpeg_c_coef_controller {
- JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
- JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
- JSAMPIMAGE input_buf));
-};
-
-/* Colorspace conversion */
-struct jpeg_color_converter {
- JMETHOD(void, start_pass, (j_compress_ptr cinfo));
- JMETHOD(void, color_convert, (j_compress_ptr cinfo,
- JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
- JDIMENSION output_row, int num_rows));
-};
-
-/* Downsampling */
-struct jpeg_downsampler {
- JMETHOD(void, start_pass, (j_compress_ptr cinfo));
- JMETHOD(void, downsample, (j_compress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION in_row_index,
- JSAMPIMAGE output_buf,
- JDIMENSION out_row_group_index));
-
- boolean need_context_rows; /* TRUE if need rows above & below */
-};
-
-/* Forward DCT (also controls coefficient quantization) */
-struct jpeg_forward_dct {
- JMETHOD(void, start_pass, (j_compress_ptr cinfo));
- /* perhaps this should be an array??? */
- JMETHOD(void, forward_DCT, (j_compress_ptr cinfo,
- jpeg_component_info * compptr,
- JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
- JDIMENSION start_row, JDIMENSION start_col,
- JDIMENSION num_blocks));
-};
-
-/* Entropy encoding */
-struct jpeg_entropy_encoder {
- JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
- JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
- JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
-};
-
-/* Marker writing */
-struct jpeg_marker_writer {
- JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
- JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
- JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
- JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
- JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
- /* These routines are exported to allow insertion of extra markers */
- /* Probably only COM and APPn markers should be written this way */
- JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
- unsigned int datalen));
- JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
-};
-
-
-/* Declarations for decompression modules */
-
-/* Master control module */
-struct jpeg_decomp_master {
- JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
- JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
-
- /* State variables made visible to other modules */
- boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */
-};
-
-/* Input control module */
-struct jpeg_input_controller {
- JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
- JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
- JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
- JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
-
- /* State variables made visible to other modules */
- boolean has_multiple_scans; /* True if file has multiple scans */
- boolean eoi_reached; /* True when EOI has been consumed */
-};
-
-/* Main buffer control (downsampled-data buffer) */
-struct jpeg_d_main_controller {
- JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
- JMETHOD(void, process_data, (j_decompress_ptr cinfo,
- JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail));
-};
-
-/* Coefficient buffer control */
-struct jpeg_d_coef_controller {
- JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
- JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
- JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
- JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
- JSAMPIMAGE output_buf));
- /* Pointer to array of coefficient virtual arrays, or NULL if none */
- jvirt_barray_ptr *coef_arrays;
-};
-
-/* Decompression postprocessing (color quantization buffer control) */
-struct jpeg_d_post_controller {
- JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
- JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf,
- JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf,
- JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail));
-};
-
-/* Marker reading & parsing */
-struct jpeg_marker_reader {
- JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
- /* Read markers until SOS or EOI.
- * Returns same codes as are defined for jpeg_consume_input:
- * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
- */
- JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
- /* Read a restart marker --- exported for use by entropy decoder only */
- jpeg_marker_parser_method read_restart_marker;
-
- /* State of marker reader --- nominally internal, but applications
- * supplying COM or APPn handlers might like to know the state.
- */
- boolean saw_SOI; /* found SOI? */
- boolean saw_SOF; /* found SOF? */
- int next_restart_num; /* next restart number expected (0-7) */
- unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
-};
-
-/* Entropy decoding */
-struct jpeg_entropy_decoder {
- JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
- JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
- JBLOCKROW *MCU_data));
-
- /* This is here to share code between baseline and progressive decoders; */
- /* other modules probably should not use it */
- boolean insufficient_data; /* set TRUE after emitting warning */
-};
-
-/* Inverse DCT (also performs dequantization) */
-typedef JMETHOD(void, inverse_DCT_method_ptr,
- (j_decompress_ptr cinfo, jpeg_component_info * compptr,
- JCOEFPTR coef_block,
- JSAMPARRAY output_buf, JDIMENSION output_col));
-
-struct jpeg_inverse_dct {
- JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
- /* It is useful to allow each component to have a separate IDCT method. */
- inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
-};
-
-/* Upsampling (note that upsampler must also call color converter) */
-struct jpeg_upsampler {
- JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
- JMETHOD(void, upsample, (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf,
- JDIMENSION *in_row_group_ctr,
- JDIMENSION in_row_groups_avail,
- JSAMPARRAY output_buf,
- JDIMENSION *out_row_ctr,
- JDIMENSION out_rows_avail));
-
- boolean need_context_rows; /* TRUE if need rows above & below */
-};
-
-/* Colorspace conversion */
-struct jpeg_color_deconverter {
- JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
- JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
- JSAMPIMAGE input_buf, JDIMENSION input_row,
- JSAMPARRAY output_buf, int num_rows));
-};
-
-/* Color quantization or color precision reduction */
-struct jpeg_color_quantizer {
- JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
- JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
- JSAMPARRAY input_buf, JSAMPARRAY output_buf,
- int num_rows));
- JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
- JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
-};
-
-
-/* Miscellaneous useful macros */
-
-#undef MAX
-#define MAX(a,b) ((a) > (b) ? (a) : (b))
-#undef MIN
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
-
-
-/* We assume that right shift corresponds to signed division by 2 with
- * rounding towards minus infinity. This is correct for typical "arithmetic
- * shift" instructions that shift in copies of the sign bit. But some
- * C compilers implement >> with an unsigned shift. For these machines you
- * must define RIGHT_SHIFT_IS_UNSIGNED.
- * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
- * It is only applied with constant shift counts. SHIFT_TEMPS must be
- * included in the variables of any routine using RIGHT_SHIFT.
- */
-
-#ifdef RIGHT_SHIFT_IS_UNSIGNED
-#define SHIFT_TEMPS INT32 shift_temp;
-#define RIGHT_SHIFT(x,shft) \
- ((shift_temp = (x)) < 0 ? \
- (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
- (shift_temp >> (shft)))
-#else
-#define SHIFT_TEMPS
-#define RIGHT_SHIFT(x,shft) ((x) >> (shft))
-#endif
-
-
-/* Short forms of external names for systems with brain-damaged linkers. */
-
-#ifdef NEED_SHORT_EXTERNAL_NAMES
-#define jinit_compress_master jICompress
-#define jinit_c_master_control jICMaster
-#define jinit_c_main_controller jICMainC
-#define jinit_c_prep_controller jICPrepC
-#define jinit_c_coef_controller jICCoefC
-#define jinit_color_converter jICColor
-#define jinit_downsampler jIDownsampler
-#define jinit_forward_dct jIFDCT
-#define jinit_huff_encoder jIHEncoder
-#define jinit_phuff_encoder jIPHEncoder
-#define jinit_marker_writer jIMWriter
-#define jinit_master_decompress jIDMaster
-#define jinit_d_main_controller jIDMainC
-#define jinit_d_coef_controller jIDCoefC
-#define jinit_d_post_controller jIDPostC
-#define jinit_input_controller jIInCtlr
-#define jinit_marker_reader jIMReader
-#define jinit_huff_decoder jIHDecoder
-#define jinit_phuff_decoder jIPHDecoder
-#define jinit_inverse_dct jIIDCT
-#define jinit_upsampler jIUpsampler
-#define jinit_color_deconverter jIDColor
-#define jinit_1pass_quantizer jI1Quant
-#define jinit_2pass_quantizer jI2Quant
-#define jinit_merged_upsampler jIMUpsampler
-#define jinit_memory_mgr jIMemMgr
-#define jdiv_round_up jDivRound
-#define jround_up jRound
-#define jcopy_sample_rows jCopySamples
-#define jcopy_block_row jCopyBlocks
-#define jzero_far jZeroFar
-#define jpeg_zigzag_order jZIGTable
-#define jpeg_natural_order jZAGTable
-#endif /* NEED_SHORT_EXTERNAL_NAMES */
-
-
-/* Compression module initialization routines */
-EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
-EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
- boolean transcode_only));
-EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
- boolean need_full_buffer));
-EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
- boolean need_full_buffer));
-EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
- boolean need_full_buffer));
-EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
-EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
-EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
-EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
-EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo));
-EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
-/* Decompression module initialization routines */
-EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
- boolean need_full_buffer));
-EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
- boolean need_full_buffer));
-EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
- boolean need_full_buffer));
-EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
-EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
-/* Memory manager initialization */
-EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
-
-/* Utility routines in jutils.c */
-EXTERN(long) jdiv_round_up JPP((long a, long b));
-EXTERN(long) jround_up JPP((long a, long b));
-EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
- JSAMPARRAY output_array, int dest_row,
- int num_rows, JDIMENSION num_cols));
-EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
- JDIMENSION num_blocks));
-EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
-/* Constant tables in jutils.c */
-#if 0 /* This table is not actually needed in v6a */
-extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
-#endif
-extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
-
-/* Suppress undefined-structure complaints if necessary. */
-
-#ifdef INCOMPLETE_TYPES_BROKEN
-#ifndef AM_MEMORY_MANAGER /* only jmemmgr.c defines these */
-struct jvirt_sarray_control { long dummy; };
-struct jvirt_barray_control { long dummy; };
-#endif
-#endif /* INCOMPLETE_TYPES_BROKEN */
+/*
+ * jpegint.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 provides common declarations for the various JPEG modules.
+ * These declarations are considered internal to the JPEG library; most
+ * applications using the library shouldn't need to include this file.
+ */
+
+
+/* Declarations for both compression & decompression */
+
+typedef enum { /* Operating modes for buffer controllers */
+ JBUF_PASS_THRU, /* Plain stripwise operation */
+ /* Remaining modes require a full-image buffer to have been created */
+ JBUF_SAVE_SOURCE, /* Run source subobject only, save output */
+ JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */
+ JBUF_SAVE_AND_PASS /* Run both subobjects, save output */
+} J_BUF_MODE;
+
+/* Values of global_state field (jdapi.c has some dependencies on ordering!) */
+#define CSTATE_START 100 /* after create_compress */
+#define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */
+#define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */
+#define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */
+#define DSTATE_START 200 /* after create_decompress */
+#define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */
+#define DSTATE_READY 202 /* found SOS, ready for start_decompress */
+#define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/
+#define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */
+#define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */
+#define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */
+#define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */
+#define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */
+#define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */
+#define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
+
+
+/* Declarations for compression modules */
+
+/* Master control module */
+struct jpeg_comp_master {
+ JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
+ JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
+ JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
+
+ /* State variables made visible to other modules */
+ boolean call_pass_startup; /* True if pass_startup must be called */
+ boolean is_last_pass; /* True during last pass */
+};
+
+/* Main buffer control (downsampled-data buffer) */
+struct jpeg_c_main_controller {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(void, process_data, (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail));
+};
+
+/* Compression preprocessing (downsampling input buffer control) */
+struct jpeg_c_prep_controller {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf,
+ JDIMENSION *in_row_ctr,
+ JDIMENSION in_rows_avail,
+ JSAMPIMAGE output_buf,
+ JDIMENSION *out_row_group_ctr,
+ JDIMENSION out_row_groups_avail));
+};
+
+/* Coefficient buffer control */
+struct jpeg_c_coef_controller {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
+ JSAMPIMAGE input_buf));
+};
+
+/* Colorspace conversion */
+struct jpeg_color_converter {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+ JMETHOD(void, color_convert, (j_compress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
+ JDIMENSION output_row, int num_rows));
+};
+
+/* Downsampling */
+struct jpeg_downsampler {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+ JMETHOD(void, downsample, (j_compress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION in_row_index,
+ JSAMPIMAGE output_buf,
+ JDIMENSION out_row_group_index));
+
+ boolean need_context_rows; /* TRUE if need rows above & below */
+};
+
+/* Forward DCT (also controls coefficient quantization) */
+struct jpeg_forward_dct {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo));
+ /* perhaps this should be an array??? */
+ JMETHOD(void, forward_DCT, (j_compress_ptr cinfo,
+ jpeg_component_info * compptr,
+ JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
+ JDIMENSION start_row, JDIMENSION start_col,
+ JDIMENSION num_blocks));
+};
+
+/* Entropy encoding */
+struct jpeg_entropy_encoder {
+ JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
+ JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
+ JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
+};
+
+/* Marker writing */
+struct jpeg_marker_writer {
+ JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
+ JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
+ JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
+ JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
+ JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
+ /* These routines are exported to allow insertion of extra markers */
+ /* Probably only COM and APPn markers should be written this way */
+ JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
+ unsigned int datalen));
+ JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
+};
+
+
+/* Declarations for decompression modules */
+
+/* Master control module */
+struct jpeg_decomp_master {
+ JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
+
+ /* State variables made visible to other modules */
+ boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */
+};
+
+/* Input control module */
+struct jpeg_input_controller {
+ JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
+ JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
+ JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
+
+ /* State variables made visible to other modules */
+ boolean has_multiple_scans; /* True if file has multiple scans */
+ boolean eoi_reached; /* True when EOI has been consumed */
+};
+
+/* Main buffer control (downsampled-data buffer) */
+struct jpeg_d_main_controller {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(void, process_data, (j_decompress_ptr cinfo,
+ JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+};
+
+/* Coefficient buffer control */
+struct jpeg_d_coef_controller {
+ JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
+ JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
+ JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
+ JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
+ JSAMPIMAGE output_buf));
+ /* Pointer to array of coefficient virtual arrays, or NULL if none */
+ jvirt_barray_ptr *coef_arrays;
+};
+
+/* Decompression postprocessing (color quantization buffer control) */
+struct jpeg_d_post_controller {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
+ JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf,
+ JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+};
+
+/* Marker reading & parsing */
+struct jpeg_marker_reader {
+ JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
+ /* Read markers until SOS or EOI.
+ * Returns same codes as are defined for jpeg_consume_input:
+ * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
+ */
+ JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
+ /* Read a restart marker --- exported for use by entropy decoder only */
+ jpeg_marker_parser_method read_restart_marker;
+
+ /* State of marker reader --- nominally internal, but applications
+ * supplying COM or APPn handlers might like to know the state.
+ */
+ boolean saw_SOI; /* found SOI? */
+ boolean saw_SOF; /* found SOF? */
+ int next_restart_num; /* next restart number expected (0-7) */
+ unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
+};
+
+/* Entropy decoding */
+struct jpeg_entropy_decoder {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+ JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo,
+ JBLOCKROW *MCU_data));
+
+ /* This is here to share code between baseline and progressive decoders; */
+ /* other modules probably should not use it */
+ boolean insufficient_data; /* set TRUE after emitting warning */
+};
+
+/* Inverse DCT (also performs dequantization) */
+typedef JMETHOD(void, inverse_DCT_method_ptr,
+ (j_decompress_ptr cinfo, jpeg_component_info * compptr,
+ JCOEFPTR coef_block,
+ JSAMPARRAY output_buf, JDIMENSION output_col));
+
+struct jpeg_inverse_dct {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+ /* It is useful to allow each component to have a separate IDCT method. */
+ inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
+};
+
+/* Upsampling (note that upsampler must also call color converter) */
+struct jpeg_upsampler {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, upsample, (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf,
+ JDIMENSION *in_row_group_ctr,
+ JDIMENSION in_row_groups_avail,
+ JSAMPARRAY output_buf,
+ JDIMENSION *out_row_ctr,
+ JDIMENSION out_rows_avail));
+
+ boolean need_context_rows; /* TRUE if need rows above & below */
+};
+
+/* Colorspace conversion */
+struct jpeg_color_deconverter {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
+ JSAMPIMAGE input_buf, JDIMENSION input_row,
+ JSAMPARRAY output_buf, int num_rows));
+};
+
+/* Color quantization or color precision reduction */
+struct jpeg_color_quantizer {
+ JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
+ JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
+ JSAMPARRAY input_buf, JSAMPARRAY output_buf,
+ int num_rows));
+ JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
+ JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
+};
+
+
+/* Miscellaneous useful macros */
+
+#undef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#undef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+
+
+/* We assume that right shift corresponds to signed division by 2 with
+ * rounding towards minus infinity. This is correct for typical "arithmetic
+ * shift" instructions that shift in copies of the sign bit. But some
+ * C compilers implement >> with an unsigned shift. For these machines you
+ * must define RIGHT_SHIFT_IS_UNSIGNED.
+ * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
+ * It is only applied with constant shift counts. SHIFT_TEMPS must be
+ * included in the variables of any routine using RIGHT_SHIFT.
+ */
+
+#ifdef RIGHT_SHIFT_IS_UNSIGNED
+#define SHIFT_TEMPS INT32 shift_temp;
+#define RIGHT_SHIFT(x,shft) \
+ ((shift_temp = (x)) < 0 ? \
+ (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
+ (shift_temp >> (shft)))
+#else
+#define SHIFT_TEMPS
+#define RIGHT_SHIFT(x,shft) ((x) >> (shft))
+#endif
+
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jinit_compress_master jICompress
+#define jinit_c_master_control jICMaster
+#define jinit_c_main_controller jICMainC
+#define jinit_c_prep_controller jICPrepC
+#define jinit_c_coef_controller jICCoefC
+#define jinit_color_converter jICColor
+#define jinit_downsampler jIDownsampler
+#define jinit_forward_dct jIFDCT
+#define jinit_huff_encoder jIHEncoder
+#define jinit_phuff_encoder jIPHEncoder
+#define jinit_marker_writer jIMWriter
+#define jinit_master_decompress jIDMaster
+#define jinit_d_main_controller jIDMainC
+#define jinit_d_coef_controller jIDCoefC
+#define jinit_d_post_controller jIDPostC
+#define jinit_input_controller jIInCtlr
+#define jinit_marker_reader jIMReader
+#define jinit_huff_decoder jIHDecoder
+#define jinit_phuff_decoder jIPHDecoder
+#define jinit_inverse_dct jIIDCT
+#define jinit_upsampler jIUpsampler
+#define jinit_color_deconverter jIDColor
+#define jinit_1pass_quantizer jI1Quant
+#define jinit_2pass_quantizer jI2Quant
+#define jinit_merged_upsampler jIMUpsampler
+#define jinit_memory_mgr jIMemMgr
+#define jdiv_round_up jDivRound
+#define jround_up jRound
+#define jcopy_sample_rows jCopySamples
+#define jcopy_block_row jCopyBlocks
+#define jzero_far jZeroFar
+#define jpeg_zigzag_order jZIGTable
+#define jpeg_natural_order jZAGTable
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/* Compression module initialization routines */
+EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
+ boolean transcode_only));
+EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo));
+EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
+/* Decompression module initialization routines */
+EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
+ boolean need_full_buffer));
+EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
+EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
+/* Memory manager initialization */
+EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
+
+/* Utility routines in jutils.c */
+EXTERN(long) jdiv_round_up JPP((long a, long b));
+EXTERN(long) jround_up JPP((long a, long b));
+EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
+ JSAMPARRAY output_array, int dest_row,
+ int num_rows, JDIMENSION num_cols));
+EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
+ JDIMENSION num_blocks));
+EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
+/* Constant tables in jutils.c */
+#if 0 /* This table is not actually needed in v6a */
+extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
+#endif
+extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
+
+/* Suppress undefined-structure complaints if necessary. */
+
+#ifdef INCOMPLETE_TYPES_BROKEN
+#ifndef AM_MEMORY_MANAGER /* only jmemmgr.c defines these */
+struct jvirt_sarray_control { long dummy; };
+struct jvirt_barray_control { long dummy; };
+#endif
+#endif /* INCOMPLETE_TYPES_BROKEN */
diff --git a/core/src/fxcodec/libjpeg/jpeglib.h b/core/src/fxcodec/libjpeg/jpeglib.h
index ac2aff90fd..7de5ab726e 100644
--- a/core/src/fxcodec/libjpeg/jpeglib.h
+++ b/core/src/fxcodec/libjpeg/jpeglib.h
@@ -1,1165 +1,1165 @@
-/*
- * jpeglib.h
- *
- * Copyright (C) 1991-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 defines the application interface for the JPEG library.
- * Most applications using the library need only include this file,
- * and perhaps jerror.h if they want to know the exact error codes.
- */
-
-#ifndef JPEGLIB_H
-#define JPEGLIB_H
-
-#define FOXIT_PREFIX(origName) FPDFAPIJPEG_##origName
-
-#define jcopy_block_row FOXIT_PREFIX(jcopy_block_row)
-#define jcopy_sample_rows FOXIT_PREFIX(jcopy_sample_rows)
-#define jdiv_round_up FOXIT_PREFIX(jdiv_round_up)
-#define jinit_1pass_quantizer FOXIT_PREFIX(jinit_1pass_quantizer)
-#define jinit_2pass_quantizer FOXIT_PREFIX(jinit_2pass_quantizer)
-#define jinit_color_deconverter FOXIT_PREFIX(jinit_color_deconverter)
-#define jinit_d_coef_controller FOXIT_PREFIX(jinit_d_coef_controller)
-#define jinit_d_main_controller FOXIT_PREFIX(jinit_d_main_controller)
-#define jinit_d_post_controller FOXIT_PREFIX(jinit_d_post_controller)
-#define jinit_huff_decoder FOXIT_PREFIX(jinit_huff_decoder)
-#define jinit_input_controller FOXIT_PREFIX(jinit_input_controller)
-#define jinit_inverse_dct FOXIT_PREFIX(jinit_inverse_dct)
-#define jinit_marker_reader FOXIT_PREFIX(jinit_marker_reader)
-#define jinit_master_decompress FOXIT_PREFIX(jinit_master_decompress)
-#define jinit_memory_mgr FOXIT_PREFIX(jinit_memory_mgr)
-#define jinit_merged_upsampler FOXIT_PREFIX(jinit_merged_upsampler)
-#define jinit_phuff_decoder FOXIT_PREFIX(jinit_phuff_decoder)
-#define jinit_upsampler FOXIT_PREFIX(jinit_upsampler)
-#define jpeg_CreateDecompress FOXIT_PREFIX(jpeg_CreateDecompress)
-#define jpeg_abort FOXIT_PREFIX(jpeg_abort)
-#define jpeg_abort_decompress FOXIT_PREFIX(jpeg_abort_decompress)
-#define jpeg_alloc_huff_table FOXIT_PREFIX(jpeg_alloc_huff_table)
-#define jpeg_alloc_quant_table FOXIT_PREFIX(jpeg_alloc_quant_table)
-#define jpeg_calc_output_dimensions FOXIT_PREFIX(jpeg_calc_output_dimensions)
-#define jpeg_consume_input FOXIT_PREFIX(jpeg_consume_input)
-#define jpeg_destroy FOXIT_PREFIX(jpeg_destroy)
-#define jpeg_destroy_decompress FOXIT_PREFIX(jpeg_destroy_decompress)
-#define jpeg_fill_bit_buffer FOXIT_PREFIX(jpeg_fill_bit_buffer)
-#define jpeg_finish_decompress FOXIT_PREFIX(jpeg_finish_decompress)
-#define jpeg_finish_output FOXIT_PREFIX(jpeg_finish_output)
-#define jpeg_free_large FOXIT_PREFIX(jpeg_free_large)
-#define jpeg_free_small FOXIT_PREFIX(jpeg_free_small)
-#define jpeg_get_large FOXIT_PREFIX(jpeg_get_large)
-#define jpeg_get_small FOXIT_PREFIX(jpeg_get_small)
-#define jpeg_has_multiple_scans FOXIT_PREFIX(jpeg_has_multiple_scans)
-#define jpeg_huff_decode FOXIT_PREFIX(jpeg_huff_decode)
-#define jpeg_idct_1x1 FOXIT_PREFIX(jpeg_idct_1x1)
-#define jpeg_idct_2x2 FOXIT_PREFIX(jpeg_idct_2x2)
-#define jpeg_idct_4x4 FOXIT_PREFIX(jpeg_idct_4x4)
-#define jpeg_idct_float FOXIT_PREFIX(jpeg_idct_float)
-#define jpeg_idct_ifast FOXIT_PREFIX(jpeg_idct_ifast)
-#define jpeg_idct_islow FOXIT_PREFIX(jpeg_idct_islow)
-#define jpeg_input_complete FOXIT_PREFIX(jpeg_input_complete)
-#define jpeg_make_d_derived_tbl FOXIT_PREFIX(jpeg_make_d_derived_tbl)
-#define jpeg_mem_available FOXIT_PREFIX(jpeg_mem_available)
-#define jpeg_mem_init FOXIT_PREFIX(jpeg_mem_init)
-#define jpeg_mem_term FOXIT_PREFIX(jpeg_mem_term)
-#define jpeg_natural_order FOXIT_PREFIX(jpeg_natural_order)
-#define jpeg_new_colormap FOXIT_PREFIX(jpeg_new_colormap)
-#define jpeg_open_backing_store FOXIT_PREFIX(jpeg_open_backing_store)
-#define jpeg_read_coefficients FOXIT_PREFIX(jpeg_read_coefficients)
-#define jpeg_read_header FOXIT_PREFIX(jpeg_read_header)
-#define jpeg_read_raw_data FOXIT_PREFIX(jpeg_read_raw_data)
-#define jpeg_read_scanlines FOXIT_PREFIX(jpeg_read_scanlines)
-#define jpeg_resync_to_restart FOXIT_PREFIX(jpeg_resync_to_restart)
-#define jpeg_save_markers FOXIT_PREFIX(jpeg_save_markers)
-#define jpeg_set_marker_processor FOXIT_PREFIX(jpeg_set_marker_processor)
-#define jpeg_start_decompress FOXIT_PREFIX(jpeg_start_decompress)
-#define jpeg_start_output FOXIT_PREFIX(jpeg_start_output)
-#define jpeg_std_error FOXIT_PREFIX(jpeg_std_error)
-#define jpeg_std_message_table FOXIT_PREFIX(jpeg_std_message_table)
-#define jpeg_stdio_src FOXIT_PREFIX(jpeg_stdio_src)
-#define jround_up FOXIT_PREFIX(jround_up)
-#define jzero_far FOXIT_PREFIX(jzero_far)
-
- /*
- * First we include the configuration files that record how this
- * installation of the JPEG library is set up. jconfig.h can be
- * generated automatically for many systems. jmorecfg.h contains
- * manual configuration options that most people need not worry about.
- */
-
-#ifndef JCONFIG_INCLUDED /* in case jinclude.h already did */
-#include "jconfig.h" /* widely used configuration options */
-#endif
-#include "jmorecfg.h" /* seldom changed options */
-
-
-/* Version ID for the JPEG library.
- * Might be useful for tests like "#if JPEG_LIB_VERSION >= 60".
- */
-
-#define JPEG_LIB_VERSION 62 /* Version 6b */
-
-
-/* Various constants determining the sizes of things.
- * All of these are specified by the JPEG standard, so don't change them
- * if you want to be compatible.
- */
-
-#define DCTSIZE 8 /* The basic DCT block is 8x8 samples */
-#define DCTSIZE2 64 /* DCTSIZE squared; # of elements in a block */
-#define NUM_QUANT_TBLS 4 /* Quantization tables are numbered 0..3 */
-#define NUM_HUFF_TBLS 4 /* Huffman tables are numbered 0..3 */
-#define NUM_ARITH_TBLS 16 /* Arith-coding tables are numbered 0..15 */
-#define MAX_COMPS_IN_SCAN 4 /* JPEG limit on # of components in one scan */
-#define MAX_SAMP_FACTOR 4 /* JPEG limit on sampling factors */
-/* Unfortunately, some bozo at Adobe saw no reason to be bound by the standard;
- * the PostScript DCT filter can emit files with many more than 10 blocks/MCU.
- * If you happen to run across such a file, you can up D_MAX_BLOCKS_IN_MCU
- * to handle it. We even let you do this from the jconfig.h file. However,
- * we strongly discourage changing C_MAX_BLOCKS_IN_MCU; just because Adobe
- * sometimes emits noncompliant files doesn't mean you should too.
- */
-#define C_MAX_BLOCKS_IN_MCU 10 /* compressor's limit on blocks per MCU */
-#ifndef D_MAX_BLOCKS_IN_MCU
-#define D_MAX_BLOCKS_IN_MCU 10 /* decompressor's limit on blocks per MCU */
-#endif
-
-
-/* Data structures for images (arrays of samples and of DCT coefficients).
- * On 80x86 machines, the image arrays are too big for near pointers,
- * but the pointer arrays can fit in near memory.
- */
-
-typedef JSAMPLE *JSAMPROW; /* ptr to one image row of pixel samples. */
-typedef JSAMPROW *JSAMPARRAY; /* ptr to some rows (a 2-D sample array) */
-typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */
-
-typedef JCOEF JBLOCK[DCTSIZE2]; /* one block of coefficients */
-typedef JBLOCK *JBLOCKROW; /* pointer to one row of coefficient blocks */
-typedef JBLOCKROW *JBLOCKARRAY; /* a 2-D array of coefficient blocks */
-typedef JBLOCKARRAY *JBLOCKIMAGE; /* a 3-D array of coefficient blocks */
-
-typedef JCOEF *JCOEFPTR; /* useful in a couple of places */
-
-
-/* Types for JPEG compression parameters and working tables. */
-
-
-/* DCT coefficient quantization tables. */
-
-typedef struct {
- /* This array gives the coefficient quantizers in natural array order
- * (not the zigzag order in which they are stored in a JPEG DQT marker).
- * CAUTION: IJG versions prior to v6a kept this array in zigzag order.
- */
- UINT16 quantval[DCTSIZE2]; /* quantization step for each coefficient */
- /* This field is used only during compression. It's initialized FALSE when
- * the table is created, and set TRUE when it's been output to the file.
- * You could suppress output of a table by setting this to TRUE.
- * (See jpeg_suppress_tables for an example.)
- */
- boolean sent_table; /* TRUE when table has been output */
-} JQUANT_TBL;
-
-
-/* Huffman coding tables. */
-
-typedef struct {
- /* These two fields directly represent the contents of a JPEG DHT marker */
- UINT8 bits[17]; /* bits[k] = # of symbols with codes of */
- /* length k bits; bits[0] is unused */
- UINT8 huffval[256]; /* The symbols, in order of incr code length */
- /* This field is used only during compression. It's initialized FALSE when
- * the table is created, and set TRUE when it's been output to the file.
- * You could suppress output of a table by setting this to TRUE.
- * (See jpeg_suppress_tables for an example.)
- */
- boolean sent_table; /* TRUE when table has been output */
-} JHUFF_TBL;
-
-
-/* Basic info about one component (color channel). */
-
-typedef struct {
- /* These values are fixed over the whole image. */
- /* For compression, they must be supplied by parameter setup; */
- /* for decompression, they are read from the SOF marker. */
- int component_id; /* identifier for this component (0..255) */
- int component_index; /* its index in SOF or cinfo->comp_info[] */
- int h_samp_factor; /* horizontal sampling factor (1..4) */
- int v_samp_factor; /* vertical sampling factor (1..4) */
- int quant_tbl_no; /* quantization table selector (0..3) */
- /* These values may vary between scans. */
- /* For compression, they must be supplied by parameter setup; */
- /* for decompression, they are read from the SOS marker. */
- /* The decompressor output side may not use these variables. */
- int dc_tbl_no; /* DC entropy table selector (0..3) */
- int ac_tbl_no; /* AC entropy table selector (0..3) */
-
- /* Remaining fields should be treated as private by applications. */
-
- /* These values are computed during compression or decompression startup: */
- /* Component's size in DCT blocks.
- * Any dummy blocks added to complete an MCU are not counted; therefore
- * these values do not depend on whether a scan is interleaved or not.
- */
- JDIMENSION width_in_blocks;
- JDIMENSION height_in_blocks;
- /* Size of a DCT block in samples. Always DCTSIZE for compression.
- * For decompression this is the size of the output from one DCT block,
- * reflecting any scaling we choose to apply during the IDCT step.
- * Values of 1,2,4,8 are likely to be supported. Note that different
- * components may receive different IDCT scalings.
- */
- int DCT_scaled_size;
- /* The downsampled dimensions are the component's actual, unpadded number
- * of samples at the main buffer (preprocessing/compression interface), thus
- * downsampled_width = ceil(image_width * Hi/Hmax)
- * and similarly for height. For decompression, IDCT scaling is included, so
- * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
- */
- JDIMENSION downsampled_width; /* actual width in samples */
- JDIMENSION downsampled_height; /* actual height in samples */
- /* This flag is used only for decompression. In cases where some of the
- * components will be ignored (eg grayscale output from YCbCr image),
- * we can skip most computations for the unused components.
- */
- boolean component_needed; /* do we need the value of this component? */
-
- /* These values are computed before starting a scan of the component. */
- /* The decompressor output side may not use these variables. */
- int MCU_width; /* number of blocks per MCU, horizontally */
- int MCU_height; /* number of blocks per MCU, vertically */
- int MCU_blocks; /* MCU_width * MCU_height */
- int MCU_sample_width; /* MCU width in samples, MCU_width*DCT_scaled_size */
- int last_col_width; /* # of non-dummy blocks across in last MCU */
- int last_row_height; /* # of non-dummy blocks down in last MCU */
-
- /* Saved quantization table for component; NULL if none yet saved.
- * See jdinput.c comments about the need for this information.
- * This field is currently used only for decompression.
- */
- JQUANT_TBL * quant_table;
-
- /* Private per-component storage for DCT or IDCT subsystem. */
- void * dct_table;
-} jpeg_component_info;
-
-
-/* The script for encoding a multiple-scan file is an array of these: */
-
-typedef struct {
- int comps_in_scan; /* number of components encoded in this scan */
- int component_index[MAX_COMPS_IN_SCAN]; /* their SOF/comp_info[] indexes */
- int Ss, Se; /* progressive JPEG spectral selection parms */
- int Ah, Al; /* progressive JPEG successive approx. parms */
-} jpeg_scan_info;
-
-/* The decompressor can save APPn and COM markers in a list of these: */
-
-typedef struct jpeg_marker_struct * jpeg_saved_marker_ptr;
-
-struct jpeg_marker_struct {
- jpeg_saved_marker_ptr next; /* next in list, or NULL */
- UINT8 marker; /* marker code: JPEG_COM, or JPEG_APP0+n */
- unsigned int original_length; /* # bytes of data in the file */
- unsigned int data_length; /* # bytes of data saved at data[] */
- JOCTET * data; /* the data contained in the marker */
- /* the marker length word is not counted in data_length or original_length */
-};
-
-/* Known color spaces. */
-
-typedef enum {
- JCS_UNKNOWN, /* error/unspecified */
- JCS_GRAYSCALE, /* monochrome */
- JCS_RGB, /* red/green/blue */
- JCS_YCbCr, /* Y/Cb/Cr (also known as YUV) */
- JCS_CMYK, /* C/M/Y/K */
- JCS_YCCK /* Y/Cb/Cr/K */
-} J_COLOR_SPACE;
-
-/* DCT/IDCT algorithm options. */
-
-typedef enum {
- JDCT_ISLOW, /* slow but accurate integer algorithm */
- JDCT_IFAST, /* faster, less accurate integer method */
- JDCT_FLOAT /* floating-point: accurate, fast on fast HW */
-} J_DCT_METHOD;
-
-#ifndef JDCT_DEFAULT /* may be overridden in jconfig.h */
-#define JDCT_DEFAULT JDCT_ISLOW
-#endif
-#ifndef JDCT_FASTEST /* may be overridden in jconfig.h */
-#define JDCT_FASTEST JDCT_IFAST
-#endif
-
-/* Dithering options for decompression. */
-
-typedef enum {
- JDITHER_NONE, /* no dithering */
- JDITHER_ORDERED, /* simple ordered dither */
- JDITHER_FS /* Floyd-Steinberg error diffusion dither */
-} J_DITHER_MODE;
-
-
-/* Common fields between JPEG compression and decompression master structs. */
-
-#define jpeg_common_fields \
- struct jpeg_error_mgr * err; /* Error handler module */\
- struct jpeg_memory_mgr * mem; /* Memory manager module */\
- struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\
- void * client_data; /* Available for use by application */\
- boolean is_decompressor; /* So common code can tell which is which */\
- int global_state /* For checking call sequence validity */
-
-/* Routines that are to be used by both halves of the library are declared
- * to receive a pointer to this structure. There are no actual instances of
- * jpeg_common_struct, only of jpeg_compress_struct and jpeg_decompress_struct.
- */
-struct jpeg_common_struct {
- jpeg_common_fields; /* Fields common to both master struct types */
- /* Additional fields follow in an actual jpeg_compress_struct or
- * jpeg_decompress_struct. All three structs must agree on these
- * initial fields! (This would be a lot cleaner in C++.)
- */
-};
-
-typedef struct jpeg_common_struct * j_common_ptr;
-typedef struct jpeg_compress_struct * j_compress_ptr;
-typedef struct jpeg_decompress_struct * j_decompress_ptr;
-
-
-/* Master record for a compression instance */
-
-struct jpeg_compress_struct {
- jpeg_common_fields; /* Fields shared with jpeg_decompress_struct */
-
- /* Destination for compressed data */
- struct jpeg_destination_mgr * dest;
-
- /* Description of source image --- these fields must be filled in by
- * outer application before starting compression. in_color_space must
- * be correct before you can even call jpeg_set_defaults().
- */
-
- JDIMENSION image_width; /* input image width */
- JDIMENSION image_height; /* input image height */
- int input_components; /* # of color components in input image */
- J_COLOR_SPACE in_color_space; /* colorspace of input image */
-
- double input_gamma; /* image gamma of input image */
-
- /* Compression parameters --- these fields must be set before calling
- * jpeg_start_compress(). We recommend calling jpeg_set_defaults() to
- * initialize everything to reasonable defaults, then changing anything
- * the application specifically wants to change. That way you won't get
- * burnt when new parameters are added. Also note that there are several
- * helper routines to simplify changing parameters.
- */
-
- int data_precision; /* bits of precision in image data */
-
- int num_components; /* # of color components in JPEG image */
- J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
-
- jpeg_component_info * comp_info;
- /* comp_info[i] describes component that appears i'th in SOF */
-
- JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
- /* ptrs to coefficient quantization tables, or NULL if not defined */
-
- JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
- JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
- /* ptrs to Huffman coding tables, or NULL if not defined */
-
- UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
- UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
- UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
-
- int num_scans; /* # of entries in scan_info array */
- const jpeg_scan_info * scan_info; /* script for multi-scan file, or NULL */
- /* The default value of scan_info is NULL, which causes a single-scan
- * sequential JPEG file to be emitted. To create a multi-scan file,
- * set num_scans and scan_info to point to an array of scan definitions.
- */
-
- boolean raw_data_in; /* TRUE=caller supplies downsampled data */
- boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */
- boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
- boolean CCIR601_sampling; /* TRUE=first samples are cosited */
- int smoothing_factor; /* 1..100, or 0 for no input smoothing */
- J_DCT_METHOD dct_method; /* DCT algorithm selector */
-
- /* The restart interval can be specified in absolute MCUs by setting
- * restart_interval, or in MCU rows by setting restart_in_rows
- * (in which case the correct restart_interval will be figured
- * for each scan).
- */
- unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */
- int restart_in_rows; /* if > 0, MCU rows per restart interval */
-
- /* Parameters controlling emission of special markers. */
-
- boolean write_JFIF_header; /* should a JFIF marker be written? */
- UINT8 JFIF_major_version; /* What to write for the JFIF version number */
- UINT8 JFIF_minor_version;
- /* These three values are not used by the JPEG code, merely copied */
- /* into the JFIF APP0 marker. density_unit can be 0 for unknown, */
- /* 1 for dots/inch, or 2 for dots/cm. Note that the pixel aspect */
- /* ratio is defined by X_density/Y_density even when density_unit=0. */
- UINT8 density_unit; /* JFIF code for pixel size units */
- UINT16 X_density; /* Horizontal pixel density */
- UINT16 Y_density; /* Vertical pixel density */
- boolean write_Adobe_marker; /* should an Adobe marker be written? */
-
- /* State variable: index of next scanline to be written to
- * jpeg_write_scanlines(). Application may use this to control its
- * processing loop, e.g., "while (next_scanline < image_height)".
- */
-
- JDIMENSION next_scanline; /* 0 .. image_height-1 */
-
- /* Remaining fields are known throughout compressor, but generally
- * should not be touched by a surrounding application.
- */
-
- /*
- * These fields are computed during compression startup
- */
- boolean progressive_mode; /* TRUE if scan script uses progressive mode */
- int max_h_samp_factor; /* largest h_samp_factor */
- int max_v_samp_factor; /* largest v_samp_factor */
-
- JDIMENSION total_iMCU_rows; /* # of iMCU rows to be input to coef ctlr */
- /* The coefficient controller receives data in units of MCU rows as defined
- * for fully interleaved scans (whether the JPEG file is interleaved or not).
- * There are v_samp_factor * DCTSIZE sample rows of each component in an
- * "iMCU" (interleaved MCU) row.
- */
-
- /*
- * These fields are valid during any one scan.
- * They describe the components and MCUs actually appearing in the scan.
- */
- int comps_in_scan; /* # of JPEG components in this scan */
- jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
- /* *cur_comp_info[i] describes component that appears i'th in SOS */
-
- JDIMENSION MCUs_per_row; /* # of MCUs across the image */
- JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */
-
- int blocks_in_MCU; /* # of DCT blocks per MCU */
- int MCU_membership[C_MAX_BLOCKS_IN_MCU];
- /* MCU_membership[i] is index in cur_comp_info of component owning */
- /* i'th block in an MCU */
-
- int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */
-
- /*
- * Links to compression subobjects (methods and private variables of modules)
- */
- struct jpeg_comp_master * master;
- struct jpeg_c_main_controller * main;
- struct jpeg_c_prep_controller * prep;
- struct jpeg_c_coef_controller * coef;
- struct jpeg_marker_writer * marker;
- struct jpeg_color_converter * cconvert;
- struct jpeg_downsampler * downsample;
- struct jpeg_forward_dct * fdct;
- struct jpeg_entropy_encoder * entropy;
- jpeg_scan_info * script_space; /* workspace for jpeg_simple_progression */
- int script_space_size;
-};
-
-
-/* Master record for a decompression instance */
-
-struct jpeg_decompress_struct {
- jpeg_common_fields; /* Fields shared with jpeg_compress_struct */
-
- /* Source of compressed data */
- struct jpeg_source_mgr * src;
-
- /* Basic description of image --- filled in by jpeg_read_header(). */
- /* Application may inspect these values to decide how to process image. */
-
- JDIMENSION image_width; /* nominal image width (from SOF marker) */
- JDIMENSION image_height; /* nominal image height */
- int num_components; /* # of color components in JPEG image */
- J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
-
- /* Decompression processing parameters --- these fields must be set before
- * calling jpeg_start_decompress(). Note that jpeg_read_header() initializes
- * them to default values.
- */
-
- J_COLOR_SPACE out_color_space; /* colorspace for output */
-
- unsigned int scale_num, scale_denom; /* fraction by which to scale image */
-
- double output_gamma; /* image gamma wanted in output */
-
- boolean buffered_image; /* TRUE=multiple output passes */
- boolean raw_data_out; /* TRUE=downsampled data wanted */
-
- J_DCT_METHOD dct_method; /* IDCT algorithm selector */
- boolean do_fancy_upsampling; /* TRUE=apply fancy upsampling */
- boolean do_block_smoothing; /* TRUE=apply interblock smoothing */
-
- boolean quantize_colors; /* TRUE=colormapped output wanted */
- /* the following are ignored if not quantize_colors: */
- J_DITHER_MODE dither_mode; /* type of color dithering to use */
- boolean two_pass_quantize; /* TRUE=use two-pass color quantization */
- int desired_number_of_colors; /* max # colors to use in created colormap */
- /* these are significant only in buffered-image mode: */
- boolean enable_1pass_quant; /* enable future use of 1-pass quantizer */
- boolean enable_external_quant;/* enable future use of external colormap */
- boolean enable_2pass_quant; /* enable future use of 2-pass quantizer */
-
- /* Description of actual output image that will be returned to application.
- * These fields are computed by jpeg_start_decompress().
- * You can also use jpeg_calc_output_dimensions() to determine these values
- * in advance of calling jpeg_start_decompress().
- */
-
- JDIMENSION output_width; /* scaled image width */
- JDIMENSION output_height; /* scaled image height */
- int out_color_components; /* # of color components in out_color_space */
- int output_components; /* # of color components returned */
- /* output_components is 1 (a colormap index) when quantizing colors;
- * otherwise it equals out_color_components.
- */
- int rec_outbuf_height; /* min recommended height of scanline buffer */
- /* If the buffer passed to jpeg_read_scanlines() is less than this many rows
- * high, space and time will be wasted due to unnecessary data copying.
- * Usually rec_outbuf_height will be 1 or 2, at most 4.
- */
-
- /* When quantizing colors, the output colormap is described by these fields.
- * The application can supply a colormap by setting colormap non-NULL before
- * calling jpeg_start_decompress; otherwise a colormap is created during
- * jpeg_start_decompress or jpeg_start_output.
- * The map has out_color_components rows and actual_number_of_colors columns.
- */
- int actual_number_of_colors; /* number of entries in use */
- JSAMPARRAY colormap; /* The color map as a 2-D pixel array */
-
- /* State variables: these variables indicate the progress of decompression.
- * The application may examine these but must not modify them.
- */
-
- /* Row index of next scanline to be read from jpeg_read_scanlines().
- * Application may use this to control its processing loop, e.g.,
- * "while (output_scanline < output_height)".
- */
- JDIMENSION output_scanline; /* 0 .. output_height-1 */
-
- /* Current input scan number and number of iMCU rows completed in scan.
- * These indicate the progress of the decompressor input side.
- */
- int input_scan_number; /* Number of SOS markers seen so far */
- JDIMENSION input_iMCU_row; /* Number of iMCU rows completed */
-
- /* The "output scan number" is the notional scan being displayed by the
- * output side. The decompressor will not allow output scan/row number
- * to get ahead of input scan/row, but it can fall arbitrarily far behind.
- */
- int output_scan_number; /* Nominal scan number being displayed */
- JDIMENSION output_iMCU_row; /* Number of iMCU rows read */
-
- /* Current progression status. coef_bits[c][i] indicates the precision
- * with which component c's DCT coefficient i (in zigzag order) is known.
- * It is -1 when no data has yet been received, otherwise it is the point
- * transform (shift) value for the most recent scan of the coefficient
- * (thus, 0 at completion of the progression).
- * This pointer is NULL when reading a non-progressive file.
- */
- int (*coef_bits)[DCTSIZE2]; /* -1 or current Al value for each coef */
-
- /* Internal JPEG parameters --- the application usually need not look at
- * these fields. Note that the decompressor output side may not use
- * any parameters that can change between scans.
- */
-
- /* Quantization and Huffman tables are carried forward across input
- * datastreams when processing abbreviated JPEG datastreams.
- */
-
- JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
- /* ptrs to coefficient quantization tables, or NULL if not defined */
-
- JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
- JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
- /* ptrs to Huffman coding tables, or NULL if not defined */
-
- /* These parameters are never carried across datastreams, since they
- * are given in SOF/SOS markers or defined to be reset by SOI.
- */
-
- int data_precision; /* bits of precision in image data */
-
- jpeg_component_info * comp_info;
- /* comp_info[i] describes component that appears i'th in SOF */
-
- boolean progressive_mode; /* TRUE if SOFn specifies progressive mode */
- boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */
-
- UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
- UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
- UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
-
- unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */
-
- /* These fields record data obtained from optional markers recognized by
- * the JPEG library.
- */
- boolean saw_JFIF_marker; /* TRUE iff a JFIF APP0 marker was found */
- /* Data copied from JFIF marker; only valid if saw_JFIF_marker is TRUE: */
- UINT8 JFIF_major_version; /* JFIF version number */
- UINT8 JFIF_minor_version;
- UINT8 density_unit; /* JFIF code for pixel size units */
- UINT16 X_density; /* Horizontal pixel density */
- UINT16 Y_density; /* Vertical pixel density */
- boolean saw_Adobe_marker; /* TRUE iff an Adobe APP14 marker was found */
- UINT8 Adobe_transform; /* Color transform code from Adobe marker */
-
- boolean CCIR601_sampling; /* TRUE=first samples are cosited */
-
- /* Aside from the specific data retained from APPn markers known to the
- * library, the uninterpreted contents of any or all APPn and COM markers
- * can be saved in a list for examination by the application.
- */
- jpeg_saved_marker_ptr marker_list; /* Head of list of saved markers */
-
- /* Remaining fields are known throughout decompressor, but generally
- * should not be touched by a surrounding application.
- */
-
- /*
- * These fields are computed during decompression startup
- */
- int max_h_samp_factor; /* largest h_samp_factor */
- int max_v_samp_factor; /* largest v_samp_factor */
-
- int min_DCT_scaled_size; /* smallest DCT_scaled_size of any component */
-
- JDIMENSION total_iMCU_rows; /* # of iMCU rows in image */
- /* The coefficient controller's input and output progress is measured in
- * units of "iMCU" (interleaved MCU) rows. These are the same as MCU rows
- * in fully interleaved JPEG scans, but are used whether the scan is
- * interleaved or not. We define an iMCU row as v_samp_factor DCT block
- * rows of each component. Therefore, the IDCT output contains
- * v_samp_factor*DCT_scaled_size sample rows of a component per iMCU row.
- */
-
- JSAMPLE * sample_range_limit; /* table for fast range-limiting */
-
- /*
- * These fields are valid during any one scan.
- * They describe the components and MCUs actually appearing in the scan.
- * Note that the decompressor output side must not use these fields.
- */
- int comps_in_scan; /* # of JPEG components in this scan */
- jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
- /* *cur_comp_info[i] describes component that appears i'th in SOS */
-
- JDIMENSION MCUs_per_row; /* # of MCUs across the image */
- JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */
-
- int blocks_in_MCU; /* # of DCT blocks per MCU */
- int MCU_membership[D_MAX_BLOCKS_IN_MCU];
- /* MCU_membership[i] is index in cur_comp_info of component owning */
- /* i'th block in an MCU */
-
- int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */
-
- /* This field is shared between entropy decoder and marker parser.
- * It is either zero or the code of a JPEG marker that has been
- * read from the data source, but has not yet been processed.
- */
- int unread_marker;
-
- /*
- * Links to decompression subobjects (methods, private variables of modules)
- */
- struct jpeg_decomp_master * master;
- struct jpeg_d_main_controller * main;
- struct jpeg_d_coef_controller * coef;
- struct jpeg_d_post_controller * post;
- struct jpeg_input_controller * inputctl;
- struct jpeg_marker_reader * marker;
- struct jpeg_entropy_decoder * entropy;
- struct jpeg_inverse_dct * idct;
- struct jpeg_upsampler * upsample;
- struct jpeg_color_deconverter * cconvert;
- struct jpeg_color_quantizer * cquantize;
-};
-
-
-/* "Object" declarations for JPEG modules that may be supplied or called
- * directly by the surrounding application.
- * As with all objects in the JPEG library, these structs only define the
- * publicly visible methods and state variables of a module. Additional
- * private fields may exist after the public ones.
- */
-
-
-/* Error handler object */
-
-struct jpeg_error_mgr {
- /* Error exit handler: does not return to caller */
- JMETHOD(void, error_exit, (j_common_ptr cinfo));
- /* Conditionally emit a trace or warning message */
- JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level));
- /* Routine that actually outputs a trace or error message */
- JMETHOD(void, output_message, (j_common_ptr cinfo));
- /* Format a message string for the most recent JPEG error or message */
- JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer));
-#define JMSG_LENGTH_MAX 200 /* recommended size of format_message buffer */
- /* Reset error state variables at start of a new image */
- JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo));
-
- /* The message ID code and any parameters are saved here.
- * A message can have one string parameter or up to 8 int parameters.
- */
- int msg_code;
-#define JMSG_STR_PARM_MAX 80
- union {
- int i[8];
- char s[JMSG_STR_PARM_MAX];
- } msg_parm;
-
- /* Standard state variables for error facility */
-
- int trace_level; /* max msg_level that will be displayed */
-
- /* For recoverable corrupt-data errors, we emit a warning message,
- * but keep going unless emit_message chooses to abort. emit_message
- * should count warnings in num_warnings. The surrounding application
- * can check for bad data by seeing if num_warnings is nonzero at the
- * end of processing.
- */
- long num_warnings; /* number of corrupt-data warnings */
-
- /* These fields point to the table(s) of error message strings.
- * An application can change the table pointer to switch to a different
- * message list (typically, to change the language in which errors are
- * reported). Some applications may wish to add additional error codes
- * that will be handled by the JPEG library error mechanism; the second
- * table pointer is used for this purpose.
- *
- * First table includes all errors generated by JPEG library itself.
- * Error code 0 is reserved for a "no such error string" message.
- */
- const char * const * jpeg_message_table; /* Library errors */
- int last_jpeg_message; /* Table contains strings 0..last_jpeg_message */
- /* Second table can be added by application (see cjpeg/djpeg for example).
- * It contains strings numbered first_addon_message..last_addon_message.
- */
- const char * const * addon_message_table; /* Non-library errors */
- int first_addon_message; /* code for first string in addon table */
- int last_addon_message; /* code for last string in addon table */
-};
-
-
-/* Progress monitor object */
-
-struct jpeg_progress_mgr {
- JMETHOD(void, progress_monitor, (j_common_ptr cinfo));
-
- long pass_counter; /* work units completed in this pass */
- long pass_limit; /* total number of work units in this pass */
- int completed_passes; /* passes completed so far */
- int total_passes; /* total number of passes expected */
-};
-
-
-/* Data destination object for compression */
-
-struct jpeg_destination_mgr {
- JOCTET * next_output_byte; /* => next byte to write in buffer */
- size_t free_in_buffer; /* # of byte spaces remaining in buffer */
-
- JMETHOD(void, init_destination, (j_compress_ptr cinfo));
- JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo));
- JMETHOD(void, term_destination, (j_compress_ptr cinfo));
-};
-
-
-/* Data source object for decompression */
-
-struct jpeg_source_mgr {
- const JOCTET * next_input_byte; /* => next byte to read from buffer */
- size_t bytes_in_buffer; /* # of bytes remaining in buffer */
-
- JMETHOD(void, init_source, (j_decompress_ptr cinfo));
- JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo));
- JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes));
- JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo, int desired));
- JMETHOD(void, term_source, (j_decompress_ptr cinfo));
-};
-
-
-/* Memory manager object.
- * Allocates "small" objects (a few K total), "large" objects (tens of K),
- * and "really big" objects (virtual arrays with backing store if needed).
- * The memory manager does not allow individual objects to be freed; rather,
- * each created object is assigned to a pool, and whole pools can be freed
- * at once. This is faster and more convenient than remembering exactly what
- * to free, especially where malloc()/free() are not too speedy.
- * NB: alloc routines never return NULL. They exit to error_exit if not
- * successful.
- */
-
-#define JPOOL_PERMANENT 0 /* lasts until master record is destroyed */
-#define JPOOL_IMAGE 1 /* lasts until done with image/datastream */
-#define JPOOL_NUMPOOLS 2
-
-typedef struct jvirt_sarray_control * jvirt_sarray_ptr;
-typedef struct jvirt_barray_control * jvirt_barray_ptr;
-
-
-struct jpeg_memory_mgr {
- /* Method pointers */
- JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id,
- size_t sizeofobject));
- JMETHOD(void *, alloc_large, (j_common_ptr cinfo, int pool_id,
- size_t sizeofobject));
- JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id,
- JDIMENSION samplesperrow,
- JDIMENSION numrows));
- JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id,
- JDIMENSION blocksperrow,
- JDIMENSION numrows));
- JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo,
- int pool_id,
- boolean pre_zero,
- JDIMENSION samplesperrow,
- JDIMENSION numrows,
- JDIMENSION maxaccess));
- JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo,
- int pool_id,
- boolean pre_zero,
- JDIMENSION blocksperrow,
- JDIMENSION numrows,
- JDIMENSION maxaccess));
- JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo));
- JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo,
- jvirt_sarray_ptr ptr,
- JDIMENSION start_row,
- JDIMENSION num_rows,
- boolean writable));
- JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo,
- jvirt_barray_ptr ptr,
- JDIMENSION start_row,
- JDIMENSION num_rows,
- boolean writable));
- JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id));
- JMETHOD(void, self_destruct, (j_common_ptr cinfo));
-
- /* Limit on memory allocation for this JPEG object. (Note that this is
- * merely advisory, not a guaranteed maximum; it only affects the space
- * used for virtual-array buffers.) May be changed by outer application
- * after creating the JPEG object.
- */
- long max_memory_to_use;
-
- /* Maximum allocation request accepted by alloc_large. */
- long max_alloc_chunk;
-};
-
-
-/* Routine signature for application-supplied marker processing methods.
- * Need not pass marker code since it is stored in cinfo->unread_marker.
- */
-typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
-
-
-/* Declarations for routines called by application.
- * The JPP macro hides prototype parameters from compilers that can't cope.
- * Note JPP requires double parentheses.
- */
-
-#ifdef HAVE_PROTOTYPES
-#define JPP(arglist) arglist
-#else
-#define JPP(arglist) ()
-#endif
-
-
-/* Short forms of external names for systems with brain-damaged linkers.
- * We shorten external names to be unique in the first six letters, which
- * is good enough for all known systems.
- * (If your compiler itself needs names to be unique in less than 15
- * characters, you are out of luck. Get a better compiler.)
- */
-
-#ifdef NEED_SHORT_EXTERNAL_NAMES
-#define jpeg_std_error jStdError
-#define jpeg_CreateCompress jCreaCompress
-#define jpeg_CreateDecompress jCreaDecompress
-#define jpeg_destroy_compress jDestCompress
-#define jpeg_destroy_decompress jDestDecompress
-#define jpeg_stdio_dest jStdDest
-#define jpeg_stdio_src jStdSrc
-#define jpeg_set_defaults jSetDefaults
-#define jpeg_set_colorspace jSetColorspace
-#define jpeg_default_colorspace jDefColorspace
-#define jpeg_set_quality jSetQuality
-#define jpeg_set_linear_quality jSetLQuality
-#define jpeg_add_quant_table jAddQuantTable
-#define jpeg_quality_scaling jQualityScaling
-#define jpeg_simple_progression jSimProgress
-#define jpeg_suppress_tables jSuppressTables
-#define jpeg_alloc_quant_table jAlcQTable
-#define jpeg_alloc_huff_table jAlcHTable
-#define jpeg_start_compress jStrtCompress
-#define jpeg_write_scanlines jWrtScanlines
-#define jpeg_finish_compress jFinCompress
-#define jpeg_write_raw_data jWrtRawData
-#define jpeg_write_marker jWrtMarker
-#define jpeg_write_m_header jWrtMHeader
-#define jpeg_write_m_byte jWrtMByte
-#define jpeg_write_tables jWrtTables
-#define jpeg_read_header jReadHeader
-#define jpeg_start_decompress jStrtDecompress
-#define jpeg_read_scanlines jReadScanlines
-#define jpeg_finish_decompress jFinDecompress
-#define jpeg_read_raw_data jReadRawData
-#define jpeg_has_multiple_scans jHasMultScn
-#define jpeg_start_output jStrtOutput
-#define jpeg_finish_output jFinOutput
-#define jpeg_input_complete jInComplete
-#define jpeg_new_colormap jNewCMap
-#define jpeg_consume_input jConsumeInput
-#define jpeg_calc_output_dimensions jCalcDimensions
-#define jpeg_save_markers jSaveMarkers
-#define jpeg_set_marker_processor jSetMarker
-#define jpeg_read_coefficients jReadCoefs
-#define jpeg_write_coefficients jWrtCoefs
-#define jpeg_copy_critical_parameters jCopyCrit
-#define jpeg_abort_compress jAbrtCompress
-#define jpeg_abort_decompress jAbrtDecompress
-#define jpeg_abort jAbort
-#define jpeg_destroy jDestroy
-#define jpeg_resync_to_restart jResyncRestart
-#endif /* NEED_SHORT_EXTERNAL_NAMES */
-
-
-/* Default error-management setup */
-EXTERN(struct jpeg_error_mgr *) jpeg_std_error
- JPP((struct jpeg_error_mgr * err));
-
-/* Initialization of JPEG compression objects.
- * jpeg_create_compress() and jpeg_create_decompress() are the exported
- * names that applications should call. These expand to calls on
- * jpeg_CreateCompress and jpeg_CreateDecompress with additional information
- * passed for version mismatch checking.
- * NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.
- */
-#define jpeg_create_compress(cinfo) \
- jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
- (size_t) sizeof(struct jpeg_compress_struct))
-#define jpeg_create_decompress(cinfo) \
- jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
- (size_t) sizeof(struct jpeg_decompress_struct))
-EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo,
- int version, size_t structsize));
-EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo,
- int version, size_t structsize));
-/* Destruction of JPEG compression objects */
-EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo));
-EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo));
-
-#if 0
-/* Standard data source and destination managers: stdio streams. */
-/* Caller is responsible for opening the file before and closing after. */
-EXTERN(void) jpeg_stdio_dest JPP((j_compress_ptr cinfo, FXSYS_FILE * outfile));
-EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FXSYS_FILE * infile));
-#endif
-
-/* Default parameter setup for compression */
-EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo));
-/* Compression parameter setup aids */
-EXTERN(void) jpeg_set_colorspace JPP((j_compress_ptr cinfo,
- J_COLOR_SPACE colorspace));
-EXTERN(void) jpeg_default_colorspace JPP((j_compress_ptr cinfo));
-EXTERN(void) jpeg_set_quality JPP((j_compress_ptr cinfo, int quality,
- boolean force_baseline));
-EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo,
- int scale_factor,
- boolean force_baseline));
-EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl,
- const unsigned int *basic_table,
- int scale_factor,
- boolean force_baseline));
-EXTERN(int) jpeg_quality_scaling JPP((int quality));
-EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo));
-EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
- boolean suppress));
-EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo));
-EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo));
-
-/* Main entry points for compression */
-EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo,
- boolean write_all_tables));
-EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo,
- JSAMPARRAY scanlines,
- JDIMENSION num_lines));
-EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo));
-
-/* Replaces jpeg_write_scanlines when writing raw downsampled data. */
-EXTERN(JDIMENSION) jpeg_write_raw_data JPP((j_compress_ptr cinfo,
- JSAMPIMAGE data,
- JDIMENSION num_lines));
-
-/* Write a special marker. See libjpeg.doc concerning safe usage. */
-EXTERN(void) jpeg_write_marker
- JPP((j_compress_ptr cinfo, int marker,
- const JOCTET * dataptr, unsigned int datalen));
-/* Same, but piecemeal. */
-EXTERN(void) jpeg_write_m_header
- JPP((j_compress_ptr cinfo, int marker, unsigned int datalen));
-EXTERN(void) jpeg_write_m_byte
- JPP((j_compress_ptr cinfo, int val));
-
-/* Alternate compression function: just write an abbreviated table file */
-EXTERN(void) jpeg_write_tables JPP((j_compress_ptr cinfo));
-
-/* Decompression startup: read start of JPEG datastream to see what's there */
-EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo,
- boolean require_image));
-/* Return value is one of: */
-#define JPEG_SUSPENDED 0 /* Suspended due to lack of input data */
-#define JPEG_HEADER_OK 1 /* Found valid image datastream */
-#define JPEG_HEADER_TABLES_ONLY 2 /* Found valid table-specs-only datastream */
-/* If you pass require_image = TRUE (normal case), you need not check for
- * a TABLES_ONLY return code; an abbreviated file will cause an error exit.
- * JPEG_SUSPENDED is only possible if you use a data source module that can
- * give a suspension return (the stdio source module doesn't).
- */
-
-/* Main entry points for decompression */
-EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo));
-EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo,
- JSAMPARRAY scanlines,
- JDIMENSION max_lines));
-EXTERN(boolean) jpeg_finish_decompress JPP((j_decompress_ptr cinfo));
-
-/* Replaces jpeg_read_scanlines when reading raw downsampled data. */
-EXTERN(JDIMENSION) jpeg_read_raw_data JPP((j_decompress_ptr cinfo,
- JSAMPIMAGE data,
- JDIMENSION max_lines));
-
-/* Additional entry points for buffered-image mode. */
-EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo));
-EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo,
- int scan_number));
-EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo));
-EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo));
-EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo));
-EXTERN(int) jpeg_consume_input JPP((j_decompress_ptr cinfo));
-/* Return value is one of: */
-/* #define JPEG_SUSPENDED 0 Suspended due to lack of input data */
-#define JPEG_REACHED_SOS 1 /* Reached start of new scan */
-#define JPEG_REACHED_EOI 2 /* Reached end of image */
-#define JPEG_ROW_COMPLETED 3 /* Completed one iMCU row */
-#define JPEG_SCAN_COMPLETED 4 /* Completed last iMCU row of a scan */
-
-/* Precalculate output dimensions for current decompression parameters. */
-EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo));
-
-/* Control saving of COM and APPn markers into marker_list. */
-EXTERN(void) jpeg_save_markers
- JPP((j_decompress_ptr cinfo, int marker_code,
- unsigned int length_limit));
-
-/* Install a special processing method for COM or APPn markers. */
-EXTERN(void) jpeg_set_marker_processor
- JPP((j_decompress_ptr cinfo, int marker_code,
- jpeg_marker_parser_method routine));
-
-/* Read or write raw DCT coefficients --- useful for lossless transcoding. */
-EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo));
-EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo,
- jvirt_barray_ptr * coef_arrays));
-EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo,
- j_compress_ptr dstinfo));
-
-/* If you choose to abort compression or decompression before completing
- * jpeg_finish_(de)compress, then you need to clean up to release memory,
- * temporary files, etc. You can just call jpeg_destroy_(de)compress
- * if you're done with the JPEG object, but if you want to clean it up and
- * reuse it, call this:
- */
-EXTERN(void) jpeg_abort_compress JPP((j_compress_ptr cinfo));
-EXTERN(void) jpeg_abort_decompress JPP((j_decompress_ptr cinfo));
-
-/* Generic versions of jpeg_abort and jpeg_destroy that work on either
- * flavor of JPEG object. These may be more convenient in some places.
- */
-EXTERN(void) jpeg_abort JPP((j_common_ptr cinfo));
-EXTERN(void) jpeg_destroy JPP((j_common_ptr cinfo));
-
-/* Default restart-marker-resync procedure for use by data source modules */
-EXTERN(boolean) jpeg_resync_to_restart JPP((j_decompress_ptr cinfo,
- int desired));
-
-
-/* These marker codes are exported since applications and data source modules
- * are likely to want to use them.
- */
-
-#define JPEG_RST0 0xD0 /* RST0 marker code */
-#define JPEG_EOI 0xD9 /* EOI marker code */
-#define JPEG_APP0 0xE0 /* APP0 marker code */
-#define JPEG_COM 0xFE /* COM marker code */
-
-
-/* If we have a brain-damaged compiler that emits warnings (or worse, errors)
- * for structure definitions that are never filled in, keep it quiet by
- * supplying dummy definitions for the various substructures.
- */
-
-#ifdef INCOMPLETE_TYPES_BROKEN
-#ifndef JPEG_INTERNALS /* will be defined in jpegint.h */
-struct jvirt_sarray_control { long dummy; };
-struct jvirt_barray_control { long dummy; };
-struct jpeg_comp_master { long dummy; };
-struct jpeg_c_main_controller { long dummy; };
-struct jpeg_c_prep_controller { long dummy; };
-struct jpeg_c_coef_controller { long dummy; };
-struct jpeg_marker_writer { long dummy; };
-struct jpeg_color_converter { long dummy; };
-struct jpeg_downsampler { long dummy; };
-struct jpeg_forward_dct { long dummy; };
-struct jpeg_entropy_encoder { long dummy; };
-struct jpeg_decomp_master { long dummy; };
-struct jpeg_d_main_controller { long dummy; };
-struct jpeg_d_coef_controller { long dummy; };
-struct jpeg_d_post_controller { long dummy; };
-struct jpeg_input_controller { long dummy; };
-struct jpeg_marker_reader { long dummy; };
-struct jpeg_entropy_decoder { long dummy; };
-struct jpeg_inverse_dct { long dummy; };
-struct jpeg_upsampler { long dummy; };
-struct jpeg_color_deconverter { long dummy; };
-struct jpeg_color_quantizer { long dummy; };
-#endif /* JPEG_INTERNALS */
-#endif /* INCOMPLETE_TYPES_BROKEN */
-
-
-/*
- * The JPEG library modules define JPEG_INTERNALS before including this file.
- * The internal structure declarations are read only when that is true.
- * Applications using the library should not include jpegint.h, but may wish
- * to include jerror.h.
- */
-
-#ifdef JPEG_INTERNALS
-#include "jpegint.h" /* fetch private declarations */
-#include "jerror.h" /* fetch error codes too */
-#endif
-
-#endif /* JPEGLIB_H */
+/*
+ * jpeglib.h
+ *
+ * Copyright (C) 1991-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 defines the application interface for the JPEG library.
+ * Most applications using the library need only include this file,
+ * and perhaps jerror.h if they want to know the exact error codes.
+ */
+
+#ifndef JPEGLIB_H
+#define JPEGLIB_H
+
+#define FOXIT_PREFIX(origName) FPDFAPIJPEG_##origName
+
+#define jcopy_block_row FOXIT_PREFIX(jcopy_block_row)
+#define jcopy_sample_rows FOXIT_PREFIX(jcopy_sample_rows)
+#define jdiv_round_up FOXIT_PREFIX(jdiv_round_up)
+#define jinit_1pass_quantizer FOXIT_PREFIX(jinit_1pass_quantizer)
+#define jinit_2pass_quantizer FOXIT_PREFIX(jinit_2pass_quantizer)
+#define jinit_color_deconverter FOXIT_PREFIX(jinit_color_deconverter)
+#define jinit_d_coef_controller FOXIT_PREFIX(jinit_d_coef_controller)
+#define jinit_d_main_controller FOXIT_PREFIX(jinit_d_main_controller)
+#define jinit_d_post_controller FOXIT_PREFIX(jinit_d_post_controller)
+#define jinit_huff_decoder FOXIT_PREFIX(jinit_huff_decoder)
+#define jinit_input_controller FOXIT_PREFIX(jinit_input_controller)
+#define jinit_inverse_dct FOXIT_PREFIX(jinit_inverse_dct)
+#define jinit_marker_reader FOXIT_PREFIX(jinit_marker_reader)
+#define jinit_master_decompress FOXIT_PREFIX(jinit_master_decompress)
+#define jinit_memory_mgr FOXIT_PREFIX(jinit_memory_mgr)
+#define jinit_merged_upsampler FOXIT_PREFIX(jinit_merged_upsampler)
+#define jinit_phuff_decoder FOXIT_PREFIX(jinit_phuff_decoder)
+#define jinit_upsampler FOXIT_PREFIX(jinit_upsampler)
+#define jpeg_CreateDecompress FOXIT_PREFIX(jpeg_CreateDecompress)
+#define jpeg_abort FOXIT_PREFIX(jpeg_abort)
+#define jpeg_abort_decompress FOXIT_PREFIX(jpeg_abort_decompress)
+#define jpeg_alloc_huff_table FOXIT_PREFIX(jpeg_alloc_huff_table)
+#define jpeg_alloc_quant_table FOXIT_PREFIX(jpeg_alloc_quant_table)
+#define jpeg_calc_output_dimensions FOXIT_PREFIX(jpeg_calc_output_dimensions)
+#define jpeg_consume_input FOXIT_PREFIX(jpeg_consume_input)
+#define jpeg_destroy FOXIT_PREFIX(jpeg_destroy)
+#define jpeg_destroy_decompress FOXIT_PREFIX(jpeg_destroy_decompress)
+#define jpeg_fill_bit_buffer FOXIT_PREFIX(jpeg_fill_bit_buffer)
+#define jpeg_finish_decompress FOXIT_PREFIX(jpeg_finish_decompress)
+#define jpeg_finish_output FOXIT_PREFIX(jpeg_finish_output)
+#define jpeg_free_large FOXIT_PREFIX(jpeg_free_large)
+#define jpeg_free_small FOXIT_PREFIX(jpeg_free_small)
+#define jpeg_get_large FOXIT_PREFIX(jpeg_get_large)
+#define jpeg_get_small FOXIT_PREFIX(jpeg_get_small)
+#define jpeg_has_multiple_scans FOXIT_PREFIX(jpeg_has_multiple_scans)
+#define jpeg_huff_decode FOXIT_PREFIX(jpeg_huff_decode)
+#define jpeg_idct_1x1 FOXIT_PREFIX(jpeg_idct_1x1)
+#define jpeg_idct_2x2 FOXIT_PREFIX(jpeg_idct_2x2)
+#define jpeg_idct_4x4 FOXIT_PREFIX(jpeg_idct_4x4)
+#define jpeg_idct_float FOXIT_PREFIX(jpeg_idct_float)
+#define jpeg_idct_ifast FOXIT_PREFIX(jpeg_idct_ifast)
+#define jpeg_idct_islow FOXIT_PREFIX(jpeg_idct_islow)
+#define jpeg_input_complete FOXIT_PREFIX(jpeg_input_complete)
+#define jpeg_make_d_derived_tbl FOXIT_PREFIX(jpeg_make_d_derived_tbl)
+#define jpeg_mem_available FOXIT_PREFIX(jpeg_mem_available)
+#define jpeg_mem_init FOXIT_PREFIX(jpeg_mem_init)
+#define jpeg_mem_term FOXIT_PREFIX(jpeg_mem_term)
+#define jpeg_natural_order FOXIT_PREFIX(jpeg_natural_order)
+#define jpeg_new_colormap FOXIT_PREFIX(jpeg_new_colormap)
+#define jpeg_open_backing_store FOXIT_PREFIX(jpeg_open_backing_store)
+#define jpeg_read_coefficients FOXIT_PREFIX(jpeg_read_coefficients)
+#define jpeg_read_header FOXIT_PREFIX(jpeg_read_header)
+#define jpeg_read_raw_data FOXIT_PREFIX(jpeg_read_raw_data)
+#define jpeg_read_scanlines FOXIT_PREFIX(jpeg_read_scanlines)
+#define jpeg_resync_to_restart FOXIT_PREFIX(jpeg_resync_to_restart)
+#define jpeg_save_markers FOXIT_PREFIX(jpeg_save_markers)
+#define jpeg_set_marker_processor FOXIT_PREFIX(jpeg_set_marker_processor)
+#define jpeg_start_decompress FOXIT_PREFIX(jpeg_start_decompress)
+#define jpeg_start_output FOXIT_PREFIX(jpeg_start_output)
+#define jpeg_std_error FOXIT_PREFIX(jpeg_std_error)
+#define jpeg_std_message_table FOXIT_PREFIX(jpeg_std_message_table)
+#define jpeg_stdio_src FOXIT_PREFIX(jpeg_stdio_src)
+#define jround_up FOXIT_PREFIX(jround_up)
+#define jzero_far FOXIT_PREFIX(jzero_far)
+
+ /*
+ * First we include the configuration files that record how this
+ * installation of the JPEG library is set up. jconfig.h can be
+ * generated automatically for many systems. jmorecfg.h contains
+ * manual configuration options that most people need not worry about.
+ */
+
+#ifndef JCONFIG_INCLUDED /* in case jinclude.h already did */
+#include "jconfig.h" /* widely used configuration options */
+#endif
+#include "jmorecfg.h" /* seldom changed options */
+
+
+/* Version ID for the JPEG library.
+ * Might be useful for tests like "#if JPEG_LIB_VERSION >= 60".
+ */
+
+#define JPEG_LIB_VERSION 62 /* Version 6b */
+
+
+/* Various constants determining the sizes of things.
+ * All of these are specified by the JPEG standard, so don't change them
+ * if you want to be compatible.
+ */
+
+#define DCTSIZE 8 /* The basic DCT block is 8x8 samples */
+#define DCTSIZE2 64 /* DCTSIZE squared; # of elements in a block */
+#define NUM_QUANT_TBLS 4 /* Quantization tables are numbered 0..3 */
+#define NUM_HUFF_TBLS 4 /* Huffman tables are numbered 0..3 */
+#define NUM_ARITH_TBLS 16 /* Arith-coding tables are numbered 0..15 */
+#define MAX_COMPS_IN_SCAN 4 /* JPEG limit on # of components in one scan */
+#define MAX_SAMP_FACTOR 4 /* JPEG limit on sampling factors */
+/* Unfortunately, some bozo at Adobe saw no reason to be bound by the standard;
+ * the PostScript DCT filter can emit files with many more than 10 blocks/MCU.
+ * If you happen to run across such a file, you can up D_MAX_BLOCKS_IN_MCU
+ * to handle it. We even let you do this from the jconfig.h file. However,
+ * we strongly discourage changing C_MAX_BLOCKS_IN_MCU; just because Adobe
+ * sometimes emits noncompliant files doesn't mean you should too.
+ */
+#define C_MAX_BLOCKS_IN_MCU 10 /* compressor's limit on blocks per MCU */
+#ifndef D_MAX_BLOCKS_IN_MCU
+#define D_MAX_BLOCKS_IN_MCU 10 /* decompressor's limit on blocks per MCU */
+#endif
+
+
+/* Data structures for images (arrays of samples and of DCT coefficients).
+ * On 80x86 machines, the image arrays are too big for near pointers,
+ * but the pointer arrays can fit in near memory.
+ */
+
+typedef JSAMPLE *JSAMPROW; /* ptr to one image row of pixel samples. */
+typedef JSAMPROW *JSAMPARRAY; /* ptr to some rows (a 2-D sample array) */
+typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */
+
+typedef JCOEF JBLOCK[DCTSIZE2]; /* one block of coefficients */
+typedef JBLOCK *JBLOCKROW; /* pointer to one row of coefficient blocks */
+typedef JBLOCKROW *JBLOCKARRAY; /* a 2-D array of coefficient blocks */
+typedef JBLOCKARRAY *JBLOCKIMAGE; /* a 3-D array of coefficient blocks */
+
+typedef JCOEF *JCOEFPTR; /* useful in a couple of places */
+
+
+/* Types for JPEG compression parameters and working tables. */
+
+
+/* DCT coefficient quantization tables. */
+
+typedef struct {
+ /* This array gives the coefficient quantizers in natural array order
+ * (not the zigzag order in which they are stored in a JPEG DQT marker).
+ * CAUTION: IJG versions prior to v6a kept this array in zigzag order.
+ */
+ UINT16 quantval[DCTSIZE2]; /* quantization step for each coefficient */
+ /* This field is used only during compression. It's initialized FALSE when
+ * the table is created, and set TRUE when it's been output to the file.
+ * You could suppress output of a table by setting this to TRUE.
+ * (See jpeg_suppress_tables for an example.)
+ */
+ boolean sent_table; /* TRUE when table has been output */
+} JQUANT_TBL;
+
+
+/* Huffman coding tables. */
+
+typedef struct {
+ /* These two fields directly represent the contents of a JPEG DHT marker */
+ UINT8 bits[17]; /* bits[k] = # of symbols with codes of */
+ /* length k bits; bits[0] is unused */
+ UINT8 huffval[256]; /* The symbols, in order of incr code length */
+ /* This field is used only during compression. It's initialized FALSE when
+ * the table is created, and set TRUE when it's been output to the file.
+ * You could suppress output of a table by setting this to TRUE.
+ * (See jpeg_suppress_tables for an example.)
+ */
+ boolean sent_table; /* TRUE when table has been output */
+} JHUFF_TBL;
+
+
+/* Basic info about one component (color channel). */
+
+typedef struct {
+ /* These values are fixed over the whole image. */
+ /* For compression, they must be supplied by parameter setup; */
+ /* for decompression, they are read from the SOF marker. */
+ int component_id; /* identifier for this component (0..255) */
+ int component_index; /* its index in SOF or cinfo->comp_info[] */
+ int h_samp_factor; /* horizontal sampling factor (1..4) */
+ int v_samp_factor; /* vertical sampling factor (1..4) */
+ int quant_tbl_no; /* quantization table selector (0..3) */
+ /* These values may vary between scans. */
+ /* For compression, they must be supplied by parameter setup; */
+ /* for decompression, they are read from the SOS marker. */
+ /* The decompressor output side may not use these variables. */
+ int dc_tbl_no; /* DC entropy table selector (0..3) */
+ int ac_tbl_no; /* AC entropy table selector (0..3) */
+
+ /* Remaining fields should be treated as private by applications. */
+
+ /* These values are computed during compression or decompression startup: */
+ /* Component's size in DCT blocks.
+ * Any dummy blocks added to complete an MCU are not counted; therefore
+ * these values do not depend on whether a scan is interleaved or not.
+ */
+ JDIMENSION width_in_blocks;
+ JDIMENSION height_in_blocks;
+ /* Size of a DCT block in samples. Always DCTSIZE for compression.
+ * For decompression this is the size of the output from one DCT block,
+ * reflecting any scaling we choose to apply during the IDCT step.
+ * Values of 1,2,4,8 are likely to be supported. Note that different
+ * components may receive different IDCT scalings.
+ */
+ int DCT_scaled_size;
+ /* The downsampled dimensions are the component's actual, unpadded number
+ * of samples at the main buffer (preprocessing/compression interface), thus
+ * downsampled_width = ceil(image_width * Hi/Hmax)
+ * and similarly for height. For decompression, IDCT scaling is included, so
+ * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
+ */
+ JDIMENSION downsampled_width; /* actual width in samples */
+ JDIMENSION downsampled_height; /* actual height in samples */
+ /* This flag is used only for decompression. In cases where some of the
+ * components will be ignored (eg grayscale output from YCbCr image),
+ * we can skip most computations for the unused components.
+ */
+ boolean component_needed; /* do we need the value of this component? */
+
+ /* These values are computed before starting a scan of the component. */
+ /* The decompressor output side may not use these variables. */
+ int MCU_width; /* number of blocks per MCU, horizontally */
+ int MCU_height; /* number of blocks per MCU, vertically */
+ int MCU_blocks; /* MCU_width * MCU_height */
+ int MCU_sample_width; /* MCU width in samples, MCU_width*DCT_scaled_size */
+ int last_col_width; /* # of non-dummy blocks across in last MCU */
+ int last_row_height; /* # of non-dummy blocks down in last MCU */
+
+ /* Saved quantization table for component; NULL if none yet saved.
+ * See jdinput.c comments about the need for this information.
+ * This field is currently used only for decompression.
+ */
+ JQUANT_TBL * quant_table;
+
+ /* Private per-component storage for DCT or IDCT subsystem. */
+ void * dct_table;
+} jpeg_component_info;
+
+
+/* The script for encoding a multiple-scan file is an array of these: */
+
+typedef struct {
+ int comps_in_scan; /* number of components encoded in this scan */
+ int component_index[MAX_COMPS_IN_SCAN]; /* their SOF/comp_info[] indexes */
+ int Ss, Se; /* progressive JPEG spectral selection parms */
+ int Ah, Al; /* progressive JPEG successive approx. parms */
+} jpeg_scan_info;
+
+/* The decompressor can save APPn and COM markers in a list of these: */
+
+typedef struct jpeg_marker_struct * jpeg_saved_marker_ptr;
+
+struct jpeg_marker_struct {
+ jpeg_saved_marker_ptr next; /* next in list, or NULL */
+ UINT8 marker; /* marker code: JPEG_COM, or JPEG_APP0+n */
+ unsigned int original_length; /* # bytes of data in the file */
+ unsigned int data_length; /* # bytes of data saved at data[] */
+ JOCTET * data; /* the data contained in the marker */
+ /* the marker length word is not counted in data_length or original_length */
+};
+
+/* Known color spaces. */
+
+typedef enum {
+ JCS_UNKNOWN, /* error/unspecified */
+ JCS_GRAYSCALE, /* monochrome */
+ JCS_RGB, /* red/green/blue */
+ JCS_YCbCr, /* Y/Cb/Cr (also known as YUV) */
+ JCS_CMYK, /* C/M/Y/K */
+ JCS_YCCK /* Y/Cb/Cr/K */
+} J_COLOR_SPACE;
+
+/* DCT/IDCT algorithm options. */
+
+typedef enum {
+ JDCT_ISLOW, /* slow but accurate integer algorithm */
+ JDCT_IFAST, /* faster, less accurate integer method */
+ JDCT_FLOAT /* floating-point: accurate, fast on fast HW */
+} J_DCT_METHOD;
+
+#ifndef JDCT_DEFAULT /* may be overridden in jconfig.h */
+#define JDCT_DEFAULT JDCT_ISLOW
+#endif
+#ifndef JDCT_FASTEST /* may be overridden in jconfig.h */
+#define JDCT_FASTEST JDCT_IFAST
+#endif
+
+/* Dithering options for decompression. */
+
+typedef enum {
+ JDITHER_NONE, /* no dithering */
+ JDITHER_ORDERED, /* simple ordered dither */
+ JDITHER_FS /* Floyd-Steinberg error diffusion dither */
+} J_DITHER_MODE;
+
+
+/* Common fields between JPEG compression and decompression master structs. */
+
+#define jpeg_common_fields \
+ struct jpeg_error_mgr * err; /* Error handler module */\
+ struct jpeg_memory_mgr * mem; /* Memory manager module */\
+ struct jpeg_progress_mgr * progress; /* Progress monitor, or NULL if none */\
+ void * client_data; /* Available for use by application */\
+ boolean is_decompressor; /* So common code can tell which is which */\
+ int global_state /* For checking call sequence validity */
+
+/* Routines that are to be used by both halves of the library are declared
+ * to receive a pointer to this structure. There are no actual instances of
+ * jpeg_common_struct, only of jpeg_compress_struct and jpeg_decompress_struct.
+ */
+struct jpeg_common_struct {
+ jpeg_common_fields; /* Fields common to both master struct types */
+ /* Additional fields follow in an actual jpeg_compress_struct or
+ * jpeg_decompress_struct. All three structs must agree on these
+ * initial fields! (This would be a lot cleaner in C++.)
+ */
+};
+
+typedef struct jpeg_common_struct * j_common_ptr;
+typedef struct jpeg_compress_struct * j_compress_ptr;
+typedef struct jpeg_decompress_struct * j_decompress_ptr;
+
+
+/* Master record for a compression instance */
+
+struct jpeg_compress_struct {
+ jpeg_common_fields; /* Fields shared with jpeg_decompress_struct */
+
+ /* Destination for compressed data */
+ struct jpeg_destination_mgr * dest;
+
+ /* Description of source image --- these fields must be filled in by
+ * outer application before starting compression. in_color_space must
+ * be correct before you can even call jpeg_set_defaults().
+ */
+
+ JDIMENSION image_width; /* input image width */
+ JDIMENSION image_height; /* input image height */
+ int input_components; /* # of color components in input image */
+ J_COLOR_SPACE in_color_space; /* colorspace of input image */
+
+ double input_gamma; /* image gamma of input image */
+
+ /* Compression parameters --- these fields must be set before calling
+ * jpeg_start_compress(). We recommend calling jpeg_set_defaults() to
+ * initialize everything to reasonable defaults, then changing anything
+ * the application specifically wants to change. That way you won't get
+ * burnt when new parameters are added. Also note that there are several
+ * helper routines to simplify changing parameters.
+ */
+
+ int data_precision; /* bits of precision in image data */
+
+ int num_components; /* # of color components in JPEG image */
+ J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
+
+ jpeg_component_info * comp_info;
+ /* comp_info[i] describes component that appears i'th in SOF */
+
+ JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
+ /* ptrs to coefficient quantization tables, or NULL if not defined */
+
+ JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
+ JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
+ /* ptrs to Huffman coding tables, or NULL if not defined */
+
+ UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
+ UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
+ UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
+
+ int num_scans; /* # of entries in scan_info array */
+ const jpeg_scan_info * scan_info; /* script for multi-scan file, or NULL */
+ /* The default value of scan_info is NULL, which causes a single-scan
+ * sequential JPEG file to be emitted. To create a multi-scan file,
+ * set num_scans and scan_info to point to an array of scan definitions.
+ */
+
+ boolean raw_data_in; /* TRUE=caller supplies downsampled data */
+ boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */
+ boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
+ boolean CCIR601_sampling; /* TRUE=first samples are cosited */
+ int smoothing_factor; /* 1..100, or 0 for no input smoothing */
+ J_DCT_METHOD dct_method; /* DCT algorithm selector */
+
+ /* The restart interval can be specified in absolute MCUs by setting
+ * restart_interval, or in MCU rows by setting restart_in_rows
+ * (in which case the correct restart_interval will be figured
+ * for each scan).
+ */
+ unsigned int restart_interval; /* MCUs per restart, or 0 for no restart */
+ int restart_in_rows; /* if > 0, MCU rows per restart interval */
+
+ /* Parameters controlling emission of special markers. */
+
+ boolean write_JFIF_header; /* should a JFIF marker be written? */
+ UINT8 JFIF_major_version; /* What to write for the JFIF version number */
+ UINT8 JFIF_minor_version;
+ /* These three values are not used by the JPEG code, merely copied */
+ /* into the JFIF APP0 marker. density_unit can be 0 for unknown, */
+ /* 1 for dots/inch, or 2 for dots/cm. Note that the pixel aspect */
+ /* ratio is defined by X_density/Y_density even when density_unit=0. */
+ UINT8 density_unit; /* JFIF code for pixel size units */
+ UINT16 X_density; /* Horizontal pixel density */
+ UINT16 Y_density; /* Vertical pixel density */
+ boolean write_Adobe_marker; /* should an Adobe marker be written? */
+
+ /* State variable: index of next scanline to be written to
+ * jpeg_write_scanlines(). Application may use this to control its
+ * processing loop, e.g., "while (next_scanline < image_height)".
+ */
+
+ JDIMENSION next_scanline; /* 0 .. image_height-1 */
+
+ /* Remaining fields are known throughout compressor, but generally
+ * should not be touched by a surrounding application.
+ */
+
+ /*
+ * These fields are computed during compression startup
+ */
+ boolean progressive_mode; /* TRUE if scan script uses progressive mode */
+ int max_h_samp_factor; /* largest h_samp_factor */
+ int max_v_samp_factor; /* largest v_samp_factor */
+
+ JDIMENSION total_iMCU_rows; /* # of iMCU rows to be input to coef ctlr */
+ /* The coefficient controller receives data in units of MCU rows as defined
+ * for fully interleaved scans (whether the JPEG file is interleaved or not).
+ * There are v_samp_factor * DCTSIZE sample rows of each component in an
+ * "iMCU" (interleaved MCU) row.
+ */
+
+ /*
+ * These fields are valid during any one scan.
+ * They describe the components and MCUs actually appearing in the scan.
+ */
+ int comps_in_scan; /* # of JPEG components in this scan */
+ jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
+ /* *cur_comp_info[i] describes component that appears i'th in SOS */
+
+ JDIMENSION MCUs_per_row; /* # of MCUs across the image */
+ JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */
+
+ int blocks_in_MCU; /* # of DCT blocks per MCU */
+ int MCU_membership[C_MAX_BLOCKS_IN_MCU];
+ /* MCU_membership[i] is index in cur_comp_info of component owning */
+ /* i'th block in an MCU */
+
+ int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */
+
+ /*
+ * Links to compression subobjects (methods and private variables of modules)
+ */
+ struct jpeg_comp_master * master;
+ struct jpeg_c_main_controller * main;
+ struct jpeg_c_prep_controller * prep;
+ struct jpeg_c_coef_controller * coef;
+ struct jpeg_marker_writer * marker;
+ struct jpeg_color_converter * cconvert;
+ struct jpeg_downsampler * downsample;
+ struct jpeg_forward_dct * fdct;
+ struct jpeg_entropy_encoder * entropy;
+ jpeg_scan_info * script_space; /* workspace for jpeg_simple_progression */
+ int script_space_size;
+};
+
+
+/* Master record for a decompression instance */
+
+struct jpeg_decompress_struct {
+ jpeg_common_fields; /* Fields shared with jpeg_compress_struct */
+
+ /* Source of compressed data */
+ struct jpeg_source_mgr * src;
+
+ /* Basic description of image --- filled in by jpeg_read_header(). */
+ /* Application may inspect these values to decide how to process image. */
+
+ JDIMENSION image_width; /* nominal image width (from SOF marker) */
+ JDIMENSION image_height; /* nominal image height */
+ int num_components; /* # of color components in JPEG image */
+ J_COLOR_SPACE jpeg_color_space; /* colorspace of JPEG image */
+
+ /* Decompression processing parameters --- these fields must be set before
+ * calling jpeg_start_decompress(). Note that jpeg_read_header() initializes
+ * them to default values.
+ */
+
+ J_COLOR_SPACE out_color_space; /* colorspace for output */
+
+ unsigned int scale_num, scale_denom; /* fraction by which to scale image */
+
+ double output_gamma; /* image gamma wanted in output */
+
+ boolean buffered_image; /* TRUE=multiple output passes */
+ boolean raw_data_out; /* TRUE=downsampled data wanted */
+
+ J_DCT_METHOD dct_method; /* IDCT algorithm selector */
+ boolean do_fancy_upsampling; /* TRUE=apply fancy upsampling */
+ boolean do_block_smoothing; /* TRUE=apply interblock smoothing */
+
+ boolean quantize_colors; /* TRUE=colormapped output wanted */
+ /* the following are ignored if not quantize_colors: */
+ J_DITHER_MODE dither_mode; /* type of color dithering to use */
+ boolean two_pass_quantize; /* TRUE=use two-pass color quantization */
+ int desired_number_of_colors; /* max # colors to use in created colormap */
+ /* these are significant only in buffered-image mode: */
+ boolean enable_1pass_quant; /* enable future use of 1-pass quantizer */
+ boolean enable_external_quant;/* enable future use of external colormap */
+ boolean enable_2pass_quant; /* enable future use of 2-pass quantizer */
+
+ /* Description of actual output image that will be returned to application.
+ * These fields are computed by jpeg_start_decompress().
+ * You can also use jpeg_calc_output_dimensions() to determine these values
+ * in advance of calling jpeg_start_decompress().
+ */
+
+ JDIMENSION output_width; /* scaled image width */
+ JDIMENSION output_height; /* scaled image height */
+ int out_color_components; /* # of color components in out_color_space */
+ int output_components; /* # of color components returned */
+ /* output_components is 1 (a colormap index) when quantizing colors;
+ * otherwise it equals out_color_components.
+ */
+ int rec_outbuf_height; /* min recommended height of scanline buffer */
+ /* If the buffer passed to jpeg_read_scanlines() is less than this many rows
+ * high, space and time will be wasted due to unnecessary data copying.
+ * Usually rec_outbuf_height will be 1 or 2, at most 4.
+ */
+
+ /* When quantizing colors, the output colormap is described by these fields.
+ * The application can supply a colormap by setting colormap non-NULL before
+ * calling jpeg_start_decompress; otherwise a colormap is created during
+ * jpeg_start_decompress or jpeg_start_output.
+ * The map has out_color_components rows and actual_number_of_colors columns.
+ */
+ int actual_number_of_colors; /* number of entries in use */
+ JSAMPARRAY colormap; /* The color map as a 2-D pixel array */
+
+ /* State variables: these variables indicate the progress of decompression.
+ * The application may examine these but must not modify them.
+ */
+
+ /* Row index of next scanline to be read from jpeg_read_scanlines().
+ * Application may use this to control its processing loop, e.g.,
+ * "while (output_scanline < output_height)".
+ */
+ JDIMENSION output_scanline; /* 0 .. output_height-1 */
+
+ /* Current input scan number and number of iMCU rows completed in scan.
+ * These indicate the progress of the decompressor input side.
+ */
+ int input_scan_number; /* Number of SOS markers seen so far */
+ JDIMENSION input_iMCU_row; /* Number of iMCU rows completed */
+
+ /* The "output scan number" is the notional scan being displayed by the
+ * output side. The decompressor will not allow output scan/row number
+ * to get ahead of input scan/row, but it can fall arbitrarily far behind.
+ */
+ int output_scan_number; /* Nominal scan number being displayed */
+ JDIMENSION output_iMCU_row; /* Number of iMCU rows read */
+
+ /* Current progression status. coef_bits[c][i] indicates the precision
+ * with which component c's DCT coefficient i (in zigzag order) is known.
+ * It is -1 when no data has yet been received, otherwise it is the point
+ * transform (shift) value for the most recent scan of the coefficient
+ * (thus, 0 at completion of the progression).
+ * This pointer is NULL when reading a non-progressive file.
+ */
+ int (*coef_bits)[DCTSIZE2]; /* -1 or current Al value for each coef */
+
+ /* Internal JPEG parameters --- the application usually need not look at
+ * these fields. Note that the decompressor output side may not use
+ * any parameters that can change between scans.
+ */
+
+ /* Quantization and Huffman tables are carried forward across input
+ * datastreams when processing abbreviated JPEG datastreams.
+ */
+
+ JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS];
+ /* ptrs to coefficient quantization tables, or NULL if not defined */
+
+ JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
+ JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
+ /* ptrs to Huffman coding tables, or NULL if not defined */
+
+ /* These parameters are never carried across datastreams, since they
+ * are given in SOF/SOS markers or defined to be reset by SOI.
+ */
+
+ int data_precision; /* bits of precision in image data */
+
+ jpeg_component_info * comp_info;
+ /* comp_info[i] describes component that appears i'th in SOF */
+
+ boolean progressive_mode; /* TRUE if SOFn specifies progressive mode */
+ boolean arith_code; /* TRUE=arithmetic coding, FALSE=Huffman */
+
+ UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
+ UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
+ UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
+
+ unsigned int restart_interval; /* MCUs per restart interval, or 0 for no restart */
+
+ /* These fields record data obtained from optional markers recognized by
+ * the JPEG library.
+ */
+ boolean saw_JFIF_marker; /* TRUE iff a JFIF APP0 marker was found */
+ /* Data copied from JFIF marker; only valid if saw_JFIF_marker is TRUE: */
+ UINT8 JFIF_major_version; /* JFIF version number */
+ UINT8 JFIF_minor_version;
+ UINT8 density_unit; /* JFIF code for pixel size units */
+ UINT16 X_density; /* Horizontal pixel density */
+ UINT16 Y_density; /* Vertical pixel density */
+ boolean saw_Adobe_marker; /* TRUE iff an Adobe APP14 marker was found */
+ UINT8 Adobe_transform; /* Color transform code from Adobe marker */
+
+ boolean CCIR601_sampling; /* TRUE=first samples are cosited */
+
+ /* Aside from the specific data retained from APPn markers known to the
+ * library, the uninterpreted contents of any or all APPn and COM markers
+ * can be saved in a list for examination by the application.
+ */
+ jpeg_saved_marker_ptr marker_list; /* Head of list of saved markers */
+
+ /* Remaining fields are known throughout decompressor, but generally
+ * should not be touched by a surrounding application.
+ */
+
+ /*
+ * These fields are computed during decompression startup
+ */
+ int max_h_samp_factor; /* largest h_samp_factor */
+ int max_v_samp_factor; /* largest v_samp_factor */
+
+ int min_DCT_scaled_size; /* smallest DCT_scaled_size of any component */
+
+ JDIMENSION total_iMCU_rows; /* # of iMCU rows in image */
+ /* The coefficient controller's input and output progress is measured in
+ * units of "iMCU" (interleaved MCU) rows. These are the same as MCU rows
+ * in fully interleaved JPEG scans, but are used whether the scan is
+ * interleaved or not. We define an iMCU row as v_samp_factor DCT block
+ * rows of each component. Therefore, the IDCT output contains
+ * v_samp_factor*DCT_scaled_size sample rows of a component per iMCU row.
+ */
+
+ JSAMPLE * sample_range_limit; /* table for fast range-limiting */
+
+ /*
+ * These fields are valid during any one scan.
+ * They describe the components and MCUs actually appearing in the scan.
+ * Note that the decompressor output side must not use these fields.
+ */
+ int comps_in_scan; /* # of JPEG components in this scan */
+ jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
+ /* *cur_comp_info[i] describes component that appears i'th in SOS */
+
+ JDIMENSION MCUs_per_row; /* # of MCUs across the image */
+ JDIMENSION MCU_rows_in_scan; /* # of MCU rows in the image */
+
+ int blocks_in_MCU; /* # of DCT blocks per MCU */
+ int MCU_membership[D_MAX_BLOCKS_IN_MCU];
+ /* MCU_membership[i] is index in cur_comp_info of component owning */
+ /* i'th block in an MCU */
+
+ int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */
+
+ /* This field is shared between entropy decoder and marker parser.
+ * It is either zero or the code of a JPEG marker that has been
+ * read from the data source, but has not yet been processed.
+ */
+ int unread_marker;
+
+ /*
+ * Links to decompression subobjects (methods, private variables of modules)
+ */
+ struct jpeg_decomp_master * master;
+ struct jpeg_d_main_controller * main;
+ struct jpeg_d_coef_controller * coef;
+ struct jpeg_d_post_controller * post;
+ struct jpeg_input_controller * inputctl;
+ struct jpeg_marker_reader * marker;
+ struct jpeg_entropy_decoder * entropy;
+ struct jpeg_inverse_dct * idct;
+ struct jpeg_upsampler * upsample;
+ struct jpeg_color_deconverter * cconvert;
+ struct jpeg_color_quantizer * cquantize;
+};
+
+
+/* "Object" declarations for JPEG modules that may be supplied or called
+ * directly by the surrounding application.
+ * As with all objects in the JPEG library, these structs only define the
+ * publicly visible methods and state variables of a module. Additional
+ * private fields may exist after the public ones.
+ */
+
+
+/* Error handler object */
+
+struct jpeg_error_mgr {
+ /* Error exit handler: does not return to caller */
+ JMETHOD(void, error_exit, (j_common_ptr cinfo));
+ /* Conditionally emit a trace or warning message */
+ JMETHOD(void, emit_message, (j_common_ptr cinfo, int msg_level));
+ /* Routine that actually outputs a trace or error message */
+ JMETHOD(void, output_message, (j_common_ptr cinfo));
+ /* Format a message string for the most recent JPEG error or message */
+ JMETHOD(void, format_message, (j_common_ptr cinfo, char * buffer));
+#define JMSG_LENGTH_MAX 200 /* recommended size of format_message buffer */
+ /* Reset error state variables at start of a new image */
+ JMETHOD(void, reset_error_mgr, (j_common_ptr cinfo));
+
+ /* The message ID code and any parameters are saved here.
+ * A message can have one string parameter or up to 8 int parameters.
+ */
+ int msg_code;
+#define JMSG_STR_PARM_MAX 80
+ union {
+ int i[8];
+ char s[JMSG_STR_PARM_MAX];
+ } msg_parm;
+
+ /* Standard state variables for error facility */
+
+ int trace_level; /* max msg_level that will be displayed */
+
+ /* For recoverable corrupt-data errors, we emit a warning message,
+ * but keep going unless emit_message chooses to abort. emit_message
+ * should count warnings in num_warnings. The surrounding application
+ * can check for bad data by seeing if num_warnings is nonzero at the
+ * end of processing.
+ */
+ long num_warnings; /* number of corrupt-data warnings */
+
+ /* These fields point to the table(s) of error message strings.
+ * An application can change the table pointer to switch to a different
+ * message list (typically, to change the language in which errors are
+ * reported). Some applications may wish to add additional error codes
+ * that will be handled by the JPEG library error mechanism; the second
+ * table pointer is used for this purpose.
+ *
+ * First table includes all errors generated by JPEG library itself.
+ * Error code 0 is reserved for a "no such error string" message.
+ */
+ const char * const * jpeg_message_table; /* Library errors */
+ int last_jpeg_message; /* Table contains strings 0..last_jpeg_message */
+ /* Second table can be added by application (see cjpeg/djpeg for example).
+ * It contains strings numbered first_addon_message..last_addon_message.
+ */
+ const char * const * addon_message_table; /* Non-library errors */
+ int first_addon_message; /* code for first string in addon table */
+ int last_addon_message; /* code for last string in addon table */
+};
+
+
+/* Progress monitor object */
+
+struct jpeg_progress_mgr {
+ JMETHOD(void, progress_monitor, (j_common_ptr cinfo));
+
+ long pass_counter; /* work units completed in this pass */
+ long pass_limit; /* total number of work units in this pass */
+ int completed_passes; /* passes completed so far */
+ int total_passes; /* total number of passes expected */
+};
+
+
+/* Data destination object for compression */
+
+struct jpeg_destination_mgr {
+ JOCTET * next_output_byte; /* => next byte to write in buffer */
+ size_t free_in_buffer; /* # of byte spaces remaining in buffer */
+
+ JMETHOD(void, init_destination, (j_compress_ptr cinfo));
+ JMETHOD(boolean, empty_output_buffer, (j_compress_ptr cinfo));
+ JMETHOD(void, term_destination, (j_compress_ptr cinfo));
+};
+
+
+/* Data source object for decompression */
+
+struct jpeg_source_mgr {
+ const JOCTET * next_input_byte; /* => next byte to read from buffer */
+ size_t bytes_in_buffer; /* # of bytes remaining in buffer */
+
+ JMETHOD(void, init_source, (j_decompress_ptr cinfo));
+ JMETHOD(boolean, fill_input_buffer, (j_decompress_ptr cinfo));
+ JMETHOD(void, skip_input_data, (j_decompress_ptr cinfo, long num_bytes));
+ JMETHOD(boolean, resync_to_restart, (j_decompress_ptr cinfo, int desired));
+ JMETHOD(void, term_source, (j_decompress_ptr cinfo));
+};
+
+
+/* Memory manager object.
+ * Allocates "small" objects (a few K total), "large" objects (tens of K),
+ * and "really big" objects (virtual arrays with backing store if needed).
+ * The memory manager does not allow individual objects to be freed; rather,
+ * each created object is assigned to a pool, and whole pools can be freed
+ * at once. This is faster and more convenient than remembering exactly what
+ * to free, especially where malloc()/free() are not too speedy.
+ * NB: alloc routines never return NULL. They exit to error_exit if not
+ * successful.
+ */
+
+#define JPOOL_PERMANENT 0 /* lasts until master record is destroyed */
+#define JPOOL_IMAGE 1 /* lasts until done with image/datastream */
+#define JPOOL_NUMPOOLS 2
+
+typedef struct jvirt_sarray_control * jvirt_sarray_ptr;
+typedef struct jvirt_barray_control * jvirt_barray_ptr;
+
+
+struct jpeg_memory_mgr {
+ /* Method pointers */
+ JMETHOD(void *, alloc_small, (j_common_ptr cinfo, int pool_id,
+ size_t sizeofobject));
+ JMETHOD(void *, alloc_large, (j_common_ptr cinfo, int pool_id,
+ size_t sizeofobject));
+ JMETHOD(JSAMPARRAY, alloc_sarray, (j_common_ptr cinfo, int pool_id,
+ JDIMENSION samplesperrow,
+ JDIMENSION numrows));
+ JMETHOD(JBLOCKARRAY, alloc_barray, (j_common_ptr cinfo, int pool_id,
+ JDIMENSION blocksperrow,
+ JDIMENSION numrows));
+ JMETHOD(jvirt_sarray_ptr, request_virt_sarray, (j_common_ptr cinfo,
+ int pool_id,
+ boolean pre_zero,
+ JDIMENSION samplesperrow,
+ JDIMENSION numrows,
+ JDIMENSION maxaccess));
+ JMETHOD(jvirt_barray_ptr, request_virt_barray, (j_common_ptr cinfo,
+ int pool_id,
+ boolean pre_zero,
+ JDIMENSION blocksperrow,
+ JDIMENSION numrows,
+ JDIMENSION maxaccess));
+ JMETHOD(void, realize_virt_arrays, (j_common_ptr cinfo));
+ JMETHOD(JSAMPARRAY, access_virt_sarray, (j_common_ptr cinfo,
+ jvirt_sarray_ptr ptr,
+ JDIMENSION start_row,
+ JDIMENSION num_rows,
+ boolean writable));
+ JMETHOD(JBLOCKARRAY, access_virt_barray, (j_common_ptr cinfo,
+ jvirt_barray_ptr ptr,
+ JDIMENSION start_row,
+ JDIMENSION num_rows,
+ boolean writable));
+ JMETHOD(void, free_pool, (j_common_ptr cinfo, int pool_id));
+ JMETHOD(void, self_destruct, (j_common_ptr cinfo));
+
+ /* Limit on memory allocation for this JPEG object. (Note that this is
+ * merely advisory, not a guaranteed maximum; it only affects the space
+ * used for virtual-array buffers.) May be changed by outer application
+ * after creating the JPEG object.
+ */
+ long max_memory_to_use;
+
+ /* Maximum allocation request accepted by alloc_large. */
+ long max_alloc_chunk;
+};
+
+
+/* Routine signature for application-supplied marker processing methods.
+ * Need not pass marker code since it is stored in cinfo->unread_marker.
+ */
+typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
+
+
+/* Declarations for routines called by application.
+ * The JPP macro hides prototype parameters from compilers that can't cope.
+ * Note JPP requires double parentheses.
+ */
+
+#ifdef HAVE_PROTOTYPES
+#define JPP(arglist) arglist
+#else
+#define JPP(arglist) ()
+#endif
+
+
+/* Short forms of external names for systems with brain-damaged linkers.
+ * We shorten external names to be unique in the first six letters, which
+ * is good enough for all known systems.
+ * (If your compiler itself needs names to be unique in less than 15
+ * characters, you are out of luck. Get a better compiler.)
+ */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jpeg_std_error jStdError
+#define jpeg_CreateCompress jCreaCompress
+#define jpeg_CreateDecompress jCreaDecompress
+#define jpeg_destroy_compress jDestCompress
+#define jpeg_destroy_decompress jDestDecompress
+#define jpeg_stdio_dest jStdDest
+#define jpeg_stdio_src jStdSrc
+#define jpeg_set_defaults jSetDefaults
+#define jpeg_set_colorspace jSetColorspace
+#define jpeg_default_colorspace jDefColorspace
+#define jpeg_set_quality jSetQuality
+#define jpeg_set_linear_quality jSetLQuality
+#define jpeg_add_quant_table jAddQuantTable
+#define jpeg_quality_scaling jQualityScaling
+#define jpeg_simple_progression jSimProgress
+#define jpeg_suppress_tables jSuppressTables
+#define jpeg_alloc_quant_table jAlcQTable
+#define jpeg_alloc_huff_table jAlcHTable
+#define jpeg_start_compress jStrtCompress
+#define jpeg_write_scanlines jWrtScanlines
+#define jpeg_finish_compress jFinCompress
+#define jpeg_write_raw_data jWrtRawData
+#define jpeg_write_marker jWrtMarker
+#define jpeg_write_m_header jWrtMHeader
+#define jpeg_write_m_byte jWrtMByte
+#define jpeg_write_tables jWrtTables
+#define jpeg_read_header jReadHeader
+#define jpeg_start_decompress jStrtDecompress
+#define jpeg_read_scanlines jReadScanlines
+#define jpeg_finish_decompress jFinDecompress
+#define jpeg_read_raw_data jReadRawData
+#define jpeg_has_multiple_scans jHasMultScn
+#define jpeg_start_output jStrtOutput
+#define jpeg_finish_output jFinOutput
+#define jpeg_input_complete jInComplete
+#define jpeg_new_colormap jNewCMap
+#define jpeg_consume_input jConsumeInput
+#define jpeg_calc_output_dimensions jCalcDimensions
+#define jpeg_save_markers jSaveMarkers
+#define jpeg_set_marker_processor jSetMarker
+#define jpeg_read_coefficients jReadCoefs
+#define jpeg_write_coefficients jWrtCoefs
+#define jpeg_copy_critical_parameters jCopyCrit
+#define jpeg_abort_compress jAbrtCompress
+#define jpeg_abort_decompress jAbrtDecompress
+#define jpeg_abort jAbort
+#define jpeg_destroy jDestroy
+#define jpeg_resync_to_restart jResyncRestart
+#endif /* NEED_SHORT_EXTERNAL_NAMES */
+
+
+/* Default error-management setup */
+EXTERN(struct jpeg_error_mgr *) jpeg_std_error
+ JPP((struct jpeg_error_mgr * err));
+
+/* Initialization of JPEG compression objects.
+ * jpeg_create_compress() and jpeg_create_decompress() are the exported
+ * names that applications should call. These expand to calls on
+ * jpeg_CreateCompress and jpeg_CreateDecompress with additional information
+ * passed for version mismatch checking.
+ * NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.
+ */
+#define jpeg_create_compress(cinfo) \
+ jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
+ (size_t) sizeof(struct jpeg_compress_struct))
+#define jpeg_create_decompress(cinfo) \
+ jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
+ (size_t) sizeof(struct jpeg_decompress_struct))
+EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo,
+ int version, size_t structsize));
+EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo,
+ int version, size_t structsize));
+/* Destruction of JPEG compression objects */
+EXTERN(void) jpeg_destroy_compress JPP((j_compress_ptr cinfo));
+EXTERN(void) jpeg_destroy_decompress JPP((j_decompress_ptr cinfo));
+
+#if 0
+/* Standard data source and destination managers: stdio streams. */
+/* Caller is responsible for opening the file before and closing after. */
+EXTERN(void) jpeg_stdio_dest JPP((j_compress_ptr cinfo, FXSYS_FILE * outfile));
+EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FXSYS_FILE * infile));
+#endif
+
+/* Default parameter setup for compression */
+EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo));
+/* Compression parameter setup aids */
+EXTERN(void) jpeg_set_colorspace JPP((j_compress_ptr cinfo,
+ J_COLOR_SPACE colorspace));
+EXTERN(void) jpeg_default_colorspace JPP((j_compress_ptr cinfo));
+EXTERN(void) jpeg_set_quality JPP((j_compress_ptr cinfo, int quality,
+ boolean force_baseline));
+EXTERN(void) jpeg_set_linear_quality JPP((j_compress_ptr cinfo,
+ int scale_factor,
+ boolean force_baseline));
+EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl,
+ const unsigned int *basic_table,
+ int scale_factor,
+ boolean force_baseline));
+EXTERN(int) jpeg_quality_scaling JPP((int quality));
+EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo));
+EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
+ boolean suppress));
+EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo));
+EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo));
+
+/* Main entry points for compression */
+EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo,
+ boolean write_all_tables));
+EXTERN(JDIMENSION) jpeg_write_scanlines JPP((j_compress_ptr cinfo,
+ JSAMPARRAY scanlines,
+ JDIMENSION num_lines));
+EXTERN(void) jpeg_finish_compress JPP((j_compress_ptr cinfo));
+
+/* Replaces jpeg_write_scanlines when writing raw downsampled data. */
+EXTERN(JDIMENSION) jpeg_write_raw_data JPP((j_compress_ptr cinfo,
+ JSAMPIMAGE data,
+ JDIMENSION num_lines));
+
+/* Write a special marker. See libjpeg.doc concerning safe usage. */
+EXTERN(void) jpeg_write_marker
+ JPP((j_compress_ptr cinfo, int marker,
+ const JOCTET * dataptr, unsigned int datalen));
+/* Same, but piecemeal. */
+EXTERN(void) jpeg_write_m_header
+ JPP((j_compress_ptr cinfo, int marker, unsigned int datalen));
+EXTERN(void) jpeg_write_m_byte
+ JPP((j_compress_ptr cinfo, int val));
+
+/* Alternate compression function: just write an abbreviated table file */
+EXTERN(void) jpeg_write_tables JPP((j_compress_ptr cinfo));
+
+/* Decompression startup: read start of JPEG datastream to see what's there */
+EXTERN(int) jpeg_read_header JPP((j_decompress_ptr cinfo,
+ boolean require_image));
+/* Return value is one of: */
+#define JPEG_SUSPENDED 0 /* Suspended due to lack of input data */
+#define JPEG_HEADER_OK 1 /* Found valid image datastream */
+#define JPEG_HEADER_TABLES_ONLY 2 /* Found valid table-specs-only datastream */
+/* If you pass require_image = TRUE (normal case), you need not check for
+ * a TABLES_ONLY return code; an abbreviated file will cause an error exit.
+ * JPEG_SUSPENDED is only possible if you use a data source module that can
+ * give a suspension return (the stdio source module doesn't).
+ */
+
+/* Main entry points for decompression */
+EXTERN(boolean) jpeg_start_decompress JPP((j_decompress_ptr cinfo));
+EXTERN(JDIMENSION) jpeg_read_scanlines JPP((j_decompress_ptr cinfo,
+ JSAMPARRAY scanlines,
+ JDIMENSION max_lines));
+EXTERN(boolean) jpeg_finish_decompress JPP((j_decompress_ptr cinfo));
+
+/* Replaces jpeg_read_scanlines when reading raw downsampled data. */
+EXTERN(JDIMENSION) jpeg_read_raw_data JPP((j_decompress_ptr cinfo,
+ JSAMPIMAGE data,
+ JDIMENSION max_lines));
+
+/* Additional entry points for buffered-image mode. */
+EXTERN(boolean) jpeg_has_multiple_scans JPP((j_decompress_ptr cinfo));
+EXTERN(boolean) jpeg_start_output JPP((j_decompress_ptr cinfo,
+ int scan_number));
+EXTERN(boolean) jpeg_finish_output JPP((j_decompress_ptr cinfo));
+EXTERN(boolean) jpeg_input_complete JPP((j_decompress_ptr cinfo));
+EXTERN(void) jpeg_new_colormap JPP((j_decompress_ptr cinfo));
+EXTERN(int) jpeg_consume_input JPP((j_decompress_ptr cinfo));
+/* Return value is one of: */
+/* #define JPEG_SUSPENDED 0 Suspended due to lack of input data */
+#define JPEG_REACHED_SOS 1 /* Reached start of new scan */
+#define JPEG_REACHED_EOI 2 /* Reached end of image */
+#define JPEG_ROW_COMPLETED 3 /* Completed one iMCU row */
+#define JPEG_SCAN_COMPLETED 4 /* Completed last iMCU row of a scan */
+
+/* Precalculate output dimensions for current decompression parameters. */
+EXTERN(void) jpeg_calc_output_dimensions JPP((j_decompress_ptr cinfo));
+
+/* Control saving of COM and APPn markers into marker_list. */
+EXTERN(void) jpeg_save_markers
+ JPP((j_decompress_ptr cinfo, int marker_code,
+ unsigned int length_limit));
+
+/* Install a special processing method for COM or APPn markers. */
+EXTERN(void) jpeg_set_marker_processor
+ JPP((j_decompress_ptr cinfo, int marker_code,
+ jpeg_marker_parser_method routine));
+
+/* Read or write raw DCT coefficients --- useful for lossless transcoding. */
+EXTERN(jvirt_barray_ptr *) jpeg_read_coefficients JPP((j_decompress_ptr cinfo));
+EXTERN(void) jpeg_write_coefficients JPP((j_compress_ptr cinfo,
+ jvirt_barray_ptr * coef_arrays));
+EXTERN(void) jpeg_copy_critical_parameters JPP((j_decompress_ptr srcinfo,
+ j_compress_ptr dstinfo));
+
+/* If you choose to abort compression or decompression before completing
+ * jpeg_finish_(de)compress, then you need to clean up to release memory,
+ * temporary files, etc. You can just call jpeg_destroy_(de)compress
+ * if you're done with the JPEG object, but if you want to clean it up and
+ * reuse it, call this:
+ */
+EXTERN(void) jpeg_abort_compress JPP((j_compress_ptr cinfo));
+EXTERN(void) jpeg_abort_decompress JPP((j_decompress_ptr cinfo));
+
+/* Generic versions of jpeg_abort and jpeg_destroy that work on either
+ * flavor of JPEG object. These may be more convenient in some places.
+ */
+EXTERN(void) jpeg_abort JPP((j_common_ptr cinfo));
+EXTERN(void) jpeg_destroy JPP((j_common_ptr cinfo));
+
+/* Default restart-marker-resync procedure for use by data source modules */
+EXTERN(boolean) jpeg_resync_to_restart JPP((j_decompress_ptr cinfo,
+ int desired));
+
+
+/* These marker codes are exported since applications and data source modules
+ * are likely to want to use them.
+ */
+
+#define JPEG_RST0 0xD0 /* RST0 marker code */
+#define JPEG_EOI 0xD9 /* EOI marker code */
+#define JPEG_APP0 0xE0 /* APP0 marker code */
+#define JPEG_COM 0xFE /* COM marker code */
+
+
+/* If we have a brain-damaged compiler that emits warnings (or worse, errors)
+ * for structure definitions that are never filled in, keep it quiet by
+ * supplying dummy definitions for the various substructures.
+ */
+
+#ifdef INCOMPLETE_TYPES_BROKEN
+#ifndef JPEG_INTERNALS /* will be defined in jpegint.h */
+struct jvirt_sarray_control { long dummy; };
+struct jvirt_barray_control { long dummy; };
+struct jpeg_comp_master { long dummy; };
+struct jpeg_c_main_controller { long dummy; };
+struct jpeg_c_prep_controller { long dummy; };
+struct jpeg_c_coef_controller { long dummy; };
+struct jpeg_marker_writer { long dummy; };
+struct jpeg_color_converter { long dummy; };
+struct jpeg_downsampler { long dummy; };
+struct jpeg_forward_dct { long dummy; };
+struct jpeg_entropy_encoder { long dummy; };
+struct jpeg_decomp_master { long dummy; };
+struct jpeg_d_main_controller { long dummy; };
+struct jpeg_d_coef_controller { long dummy; };
+struct jpeg_d_post_controller { long dummy; };
+struct jpeg_input_controller { long dummy; };
+struct jpeg_marker_reader { long dummy; };
+struct jpeg_entropy_decoder { long dummy; };
+struct jpeg_inverse_dct { long dummy; };
+struct jpeg_upsampler { long dummy; };
+struct jpeg_color_deconverter { long dummy; };
+struct jpeg_color_quantizer { long dummy; };
+#endif /* JPEG_INTERNALS */
+#endif /* INCOMPLETE_TYPES_BROKEN */
+
+
+/*
+ * The JPEG library modules define JPEG_INTERNALS before including this file.
+ * The internal structure declarations are read only when that is true.
+ * Applications using the library should not include jpegint.h, but may wish
+ * to include jerror.h.
+ */
+
+#ifdef JPEG_INTERNALS
+#include "jpegint.h" /* fetch private declarations */
+#include "jerror.h" /* fetch error codes too */
+#endif
+
+#endif /* JPEGLIB_H */
diff --git a/core/src/fxcodec/libjpeg/jversion.h b/core/src/fxcodec/libjpeg/jversion.h
index dadd453a41..6472c58d35 100644
--- a/core/src/fxcodec/libjpeg/jversion.h
+++ b/core/src/fxcodec/libjpeg/jversion.h
@@ -1,14 +1,14 @@
-/*
- * jversion.h
- *
- * Copyright (C) 1991-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 software version identification.
- */
-
-
-#define JVERSION "6b 27-Mar-1998"
-
-#define JCOPYRIGHT "Copyright (C) 1998, Thomas G. Lane"
+/*
+ * jversion.h
+ *
+ * Copyright (C) 1991-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 software version identification.
+ */
+
+
+#define JVERSION "6b 27-Mar-1998"
+
+#define JCOPYRIGHT "Copyright (C) 1998, Thomas G. Lane"
diff --git a/core/src/fxcodec/libjpeg/transupp.h b/core/src/fxcodec/libjpeg/transupp.h
index eb0b05566a..5c2d32aff5 100644
--- a/core/src/fxcodec/libjpeg/transupp.h
+++ b/core/src/fxcodec/libjpeg/transupp.h
@@ -1,135 +1,135 @@
-/*
- * transupp.h
- *
- * Copyright (C) 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 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
-
-/* Short forms of external names for systems with brain-damaged linkers. */
-
-#ifdef NEED_SHORT_EXTERNAL_NAMES
-#define jtransform_request_workspace jTrRequest
-#define jtransform_adjust_parameters jTrAdjust
-#define jtransform_execute_transformation jTrExec
-#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;
-
-/*
- * 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 "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.
- */
-
-typedef struct {
- /* Options: set by caller */
- JXFORM_CODE transform; /* image transform operator */
- boolean trim; /* if TRUE, trim partial MCUs as needed */
- boolean force_grayscale; /* if TRUE, convert color image to grayscale */
-
- /* Internal workspace: caller should not touch these */
- int num_components; /* # of components in workspace */
- jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
-} jpeg_transform_info;
-
-
-#if TRANSFORMS_SUPPORTED
-
-/* 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_transformation
- JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
- jvirt_barray_ptr *src_coef_arrays,
- jpeg_transform_info *info));
-
-#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));
+/*
+ * transupp.h
+ *
+ * Copyright (C) 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 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
+
+/* Short forms of external names for systems with brain-damaged linkers. */
+
+#ifdef NEED_SHORT_EXTERNAL_NAMES
+#define jtransform_request_workspace jTrRequest
+#define jtransform_adjust_parameters jTrAdjust
+#define jtransform_execute_transformation jTrExec
+#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;
+
+/*
+ * 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 "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.
+ */
+
+typedef struct {
+ /* Options: set by caller */
+ JXFORM_CODE transform; /* image transform operator */
+ boolean trim; /* if TRUE, trim partial MCUs as needed */
+ boolean force_grayscale; /* if TRUE, convert color image to grayscale */
+
+ /* Internal workspace: caller should not touch these */
+ int num_components; /* # of components in workspace */
+ jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
+} jpeg_transform_info;
+
+
+#if TRANSFORMS_SUPPORTED
+
+/* 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_transformation
+ JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
+ jvirt_barray_ptr *src_coef_arrays,
+ jpeg_transform_info *info));
+
+#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));